1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright 1998, 2000 Marshall Kirk McKusick.
5 * Copyright 2009, 2010 Jeffrey W. Roberson <[email protected]>
6 * All rights reserved.
7 *
8 * The soft updates code is derived from the appendix of a University
9 * of Michigan technical report (Gregory R. Ganger and Yale N. Patt,
10 * "Soft Updates: A Solution to the Metadata Update Problem in File
11 * Systems", CSE-TR-254-95, August 1995).
12 *
13 * Further information about soft updates can be obtained from:
14 *
15 * Marshall Kirk McKusick http://www.mckusick.com/softdep/
16 * 1614 Oxford Street [email protected]
17 * Berkeley, CA 94709-1608 +1-510-843-9542
18 * USA
19 *
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
23 *
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
35 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
36 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
37 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
38 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
39 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * from: @(#)ffs_softdep.c 9.59 (McKusick) 6/21/00
42 */
43
44 #include <sys/cdefs.h>
45 #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
3638 ump = VFSTOUFS(mp);
3639 if (ump->um_softdep == NULL || ump->um_softdep->sd_jblocks == NULL)
3640 return;
3641 shouldflush = softdep_flushcache;
3642 bio = NULL;
3643 jseg = NULL;
3644 LOCK_OWNED(ump);
3645 fs = ump->um_fs;
3646 jblocks = ump->softdep_jblocks;
3647 devbsize = ump->um_devvp->v_bufobj.bo_bsize;
3648 /*
3649 * We write anywhere between a disk block and fs block. The upper
3650 * bound is picked to prevent buffer cache fragmentation and limit
3651 * processing time per I/O.
3652 */
3653 jrecmin = (devbsize / JREC_SIZE) - 1; /* -1 for seg header */
3654 jrecmax = (fs->fs_bsize / devbsize) * jrecmin;
3655 segwritten = 0;
3656 for (;;) {
3657 cnt = ump->softdep_on_journal;
3658 /*
3659 * Criteria for writing a segment:
3660 * 1) We have a full block.
3661 * 2) We're called from jwait() and haven't found the
3662 * journal item yet.
3663 * 3) Always write if needseg is set.
3664 * 4) If we are called from process_worklist and have
3665 * not yet written anything we write a partial block
3666 * to enforce a 1 second maximum latency on journal
3667 * entries.
3668 */
3669 if (cnt < (jrecmax - 1) && needwk == NULL &&
3670 jblocks->jb_needseg == 0 && (segwritten || cnt == 0))
3671 break;
3672 cnt++;
3673 /*
3674 * Verify some free journal space. softdep_prealloc() should
3675 * guarantee that we don't run out so this is indicative of
3676 * a problem with the flow control. Try to recover
3677 * gracefully in any event.
3678 */
3679 while (jblocks->jb_free == 0) {
3680 if (flags != MNT_WAIT)
3681 break;
3682 printf("softdep: Out of journal space!\n");
3683 softdep_speedup(ump);
3684 msleep(jblocks, LOCK_PTR(ump), PRIBIO, "jblocks", hz);
3685 }
3686 FREE_LOCK(ump);
3687 jseg = malloc(sizeof(*jseg), M_JSEG, M_SOFTDEP_FLAGS);
3688 workitem_alloc(&jseg->js_list, D_JSEG, mp);
3689 LIST_INIT(&jseg->js_entries);
3690 LIST_INIT(&jseg->js_indirs);
3691 jseg->js_state = ATTACHED;
3692 if (shouldflush == 0)
3693 jseg->js_state |= COMPLETE;
3694 else if (bio == NULL)
3695 bio = g_alloc_bio();
3696 jseg->js_jblocks = jblocks;
3697 bp = geteblk(fs->fs_bsize, 0);
3698 ACQUIRE_LOCK(ump);
3699 /*
3700 * If there was a race while we were allocating the block
3701 * and jseg the entry we care about was likely written.
3702 * We bail out in both the WAIT and NOWAIT case and assume
3703 * the caller will loop if the entry it cares about is
3704 * not written.
3705 */
3706 cnt = ump->softdep_on_journal;
3707 if (cnt + jblocks->jb_needseg == 0 || jblocks->jb_free == 0) {
3708 bp->b_flags |= B_INVAL | B_NOCACHE;
3709 WORKITEM_FREE(jseg, D_JSEG);
3710 FREE_LOCK(ump);
3711 brelse(bp);
3712 ACQUIRE_LOCK(ump);
3713 break;
3714 }
3715 /*
3716 * Calculate the disk block size required for the available
3717 * records rounded to the min size.
3718 */
3719 if (cnt == 0)
3720 size = devbsize;
3721 else if (cnt < jrecmax)
3722 size = howmany(cnt, jrecmin) * devbsize;
3723 else
3724 size = fs->fs_bsize;
3725 /*
3726 * Allocate a disk block for this journal data and account
3727 * for truncation of the requested size if enough contiguous
3728 * space was not available.
3729 */
3730 bp->b_blkno = jblocks_alloc(jblocks, size, &size);
3731 bp->b_lblkno = bp->b_blkno;
3732 bp->b_offset = bp->b_blkno * DEV_BSIZE;
3733 bp->b_bcount = size;
3734 bp->b_flags &= ~B_INVAL;
3735 bp->b_flags |= B_VALIDSUSPWRT | B_NOCOPY;
3736 /*
3737 * Initialize our jseg with cnt records. Assign the next
3738 * sequence number to it and link it in-order.
3739 */
3740 cnt = MIN(cnt, (size / devbsize) * jrecmin);
3741 jseg->js_buf = bp;
3742 jseg->js_cnt = cnt;
3743 jseg->js_refs = cnt + 1; /* Self ref. */
3744 jseg->js_size = size;
3745 jseg->js_seq = jblocks->jb_nextseq++;
3746 if (jblocks->jb_oldestseg == NULL)
3747 jblocks->jb_oldestseg = jseg;
3748 jseg->js_oldseq = jblocks->jb_oldestseg->js_seq;
3749 TAILQ_INSERT_TAIL(&jblocks->jb_segs, jseg, js_next);
3750 if (jblocks->jb_writeseg == NULL)
3751 jblocks->jb_writeseg = jseg;
3752 /*
3753 * Start filling in records from the pending list.
3754 */
3755 data = bp->b_data;
3756 off = 0;
3757
3758 /*
3759 * Always put a header on the first block.
3760 * XXX As with below, there might not be a chance to get
3761 * into the loop. Ensure that something valid is written.
3762 */
3763 jseg_write(ump, jseg, data);
3764 off += JREC_SIZE;
3765 data = bp->b_data + off;
3766
3767 /*
3768 * XXX Something is wrong here. There's no work to do,
3769 * but we need to perform and I/O and allow it to complete
3770 * anyways.
3771 */
3772 if (LIST_EMPTY(&ump->softdep_journal_pending))
3773 stat_emptyjblocks++;
3774
3775 while ((wk = LIST_FIRST(&ump->softdep_journal_pending))
3776 != NULL) {
3777 if (cnt == 0)
3778 break;
3779 /* Place a segment header on every device block. */
3780 if ((off % devbsize) == 0) {
3781 jseg_write(ump, jseg, data);
3782 off += JREC_SIZE;
3783 data = bp->b_data + off;
3784 }
3785 if (wk == needwk)
3786 needwk = NULL;
3787 remove_from_journal(wk);
3788 wk->wk_state |= INPROGRESS;
3789 WORKLIST_INSERT(&jseg->js_entries, wk);
3790 switch (wk->wk_type) {
3791 case D_JADDREF:
3792 jaddref_write(WK_JADDREF(wk), jseg, data);
3793 break;
3794 case D_JREMREF:
3795 jremref_write(WK_JREMREF(wk), jseg, data);
3796 break;
3797 case D_JMVREF:
3798 jmvref_write(WK_JMVREF(wk), jseg, data);
3799 break;
3800 case D_JNEWBLK:
3801 jnewblk_write(WK_JNEWBLK(wk), jseg, data);
3802 break;
3803 case D_JFREEBLK:
3804 jfreeblk_write(WK_JFREEBLK(wk), jseg, data);
3805 break;
3806 case D_JFREEFRAG:
3807 jfreefrag_write(WK_JFREEFRAG(wk), jseg, data);
3808 break;
3809 case D_JTRUNC:
3810 jtrunc_write(WK_JTRUNC(wk), jseg, data);
3811 break;
3812 case D_JFSYNC:
3813 jfsync_write(WK_JFSYNC(wk), jseg, data);
3814 break;
3815 default:
3816 panic("process_journal: Unknown type %s",
3817 TYPENAME(wk->wk_type));
3818 /* NOTREACHED */
3819 }
3820 off += JREC_SIZE;
3821 data = bp->b_data + off;
3822 cnt--;
3823 }
3824
3825 /* Clear any remaining space so we don't leak kernel data */
3826 if (size > off)
3827 bzero(data, size - off);
3828
3829 /*
3830 * Write this one buffer and continue.
3831 */
3832 segwritten = 1;
3833 jblocks->jb_needseg = 0;
3834 WORKLIST_INSERT(&bp->b_dep, &jseg->js_list);
3835 FREE_LOCK(ump);
3836 bp->b_xflags |= BX_CVTENXIO;
3837 pbgetvp(ump->um_devvp, bp);
3838 /*
3839 * We only do the blocking wait once we find the journal
3840 * entry we're looking for.
3841 */
3842 if (needwk == NULL && flags == MNT_WAIT)
3843 bwrite(bp);
3844 else
3845 bawrite(bp);
3846 ACQUIRE_LOCK(ump);
3847 }
3848 /*
3849 * If we wrote a segment issue a synchronize cache so the journal
3850 * is reflected on disk before the data is written. Since reclaiming
3851 * journal space also requires writing a journal record this
3852 * process also enforces a barrier before reclamation.
3853 */
3854 if (segwritten && shouldflush) {
3855 softdep_synchronize(bio, ump,
3856 TAILQ_LAST(&jblocks->jb_segs, jseglst));
3857 } else if (bio)
3858 g_destroy_bio(bio);
3859 /*
3860 * If we've suspended the filesystem because we ran out of journal
3861 * space either try to sync it here to make some progress or
3862 * unsuspend it if we already have.
3863 */
3864 if (flags == 0 && jblocks->jb_suspended) {
3865 if (journal_unsuspend(ump))
3866 return;
3867 FREE_LOCK(ump);
3868 VFS_SYNC(mp, MNT_NOWAIT);
3869 ffs_sbupdate(ump, MNT_WAIT, 0);
3870 ACQUIRE_LOCK(ump);
3871 }
3872 }
3873
3874 /*
3875 * Complete a jseg, allowing all dependencies awaiting journal writes
3876 * to proceed. Each journal dependency also attaches a jsegdep to dependent
3877 * structures so that the journal segment can be freed to reclaim space.
3878 */
3879 static void
complete_jseg(struct jseg * jseg)3880 complete_jseg(struct jseg *jseg)
3881 {
3882 struct worklist *wk;
3883 struct jmvref *jmvref;
3884 #ifdef INVARIANTS
3885 int i = 0;
3886 #endif
3887
3888 while ((wk = LIST_FIRST(&jseg->js_entries)) != NULL) {
3889 WORKLIST_REMOVE(wk);
3890 wk->wk_state &= ~INPROGRESS;
3891 wk->wk_state |= COMPLETE;
3892 KASSERT(i++ < jseg->js_cnt,
3893 ("handle_written_jseg: overflow %d >= %d",
3894 i - 1, jseg->js_cnt));
3895 switch (wk->wk_type) {
3896 case D_JADDREF:
3897 handle_written_jaddref(WK_JADDREF(wk));
3898 break;
3899 case D_JREMREF:
3900 handle_written_jremref(WK_JREMREF(wk));
3901 break;
3902 case D_JMVREF:
3903 rele_jseg(jseg); /* No jsegdep. */
3904 jmvref = WK_JMVREF(wk);
3905 LIST_REMOVE(jmvref, jm_deps);
3906 if ((jmvref->jm_pagedep->pd_state & ONWORKLIST) == 0)
3907 free_pagedep(jmvref->jm_pagedep);
3908 WORKITEM_FREE(jmvref, D_JMVREF);
3909 break;
3910 case D_JNEWBLK:
3911 handle_written_jnewblk(WK_JNEWBLK(wk));
3912 break;
3913 case D_JFREEBLK:
3914 handle_written_jblkdep(&WK_JFREEBLK(wk)->jf_dep);
3915 break;
3916 case D_JTRUNC:
3917 handle_written_jblkdep(&WK_JTRUNC(wk)->jt_dep);
3918 break;
3919 case D_JFSYNC:
3920 rele_jseg(jseg); /* No jsegdep. */
3921 WORKITEM_FREE(wk, D_JFSYNC);
3922 break;
3923 case D_JFREEFRAG:
3924 handle_written_jfreefrag(WK_JFREEFRAG(wk));
3925 break;
3926 default:
3927 panic("handle_written_jseg: Unknown type %s",
3928 TYPENAME(wk->wk_type));
3929 /* NOTREACHED */
3930 }
3931 }
3932 /* Release the self reference so the structure may be freed. */
3933 rele_jseg(jseg);
3934 }
3935
3936 /*
3937 * Determine which jsegs are ready for completion processing. Waits for
3938 * synchronize cache to complete as well as forcing in-order completion
3939 * of journal entries.
3940 */
3941 static void
complete_jsegs(struct jseg * jseg)3942 complete_jsegs(struct jseg *jseg)
3943 {
3944 struct jblocks *jblocks;
3945 struct jseg *jsegn;
3946
3947 jblocks = jseg->js_jblocks;
3948 /*
3949 * Don't allow out of order completions. If this isn't the first
3950 * block wait for it to write before we're done.
3951 */
3952 if (jseg != jblocks->jb_writeseg)
3953 return;
3954 /* Iterate through available jsegs processing their entries. */
3955 while (jseg && (jseg->js_state & ALLCOMPLETE) == ALLCOMPLETE) {
3956 jblocks->jb_oldestwrseq = jseg->js_oldseq;
3957 jsegn = TAILQ_NEXT(jseg, js_next);
3958 complete_jseg(jseg);
3959 jseg = jsegn;
3960 }
3961 jblocks->jb_writeseg = jseg;
3962 /*
3963 * Attempt to free jsegs now that oldestwrseq may have advanced.
3964 */
3965 free_jsegs(jblocks);
3966 }
3967
3968 /*
3969 * Mark a jseg as DEPCOMPLETE and throw away the buffer. Attempt to handle
3970 * the final completions.
3971 */
3972 static void
handle_written_jseg(struct jseg * jseg,struct buf * bp)3973 handle_written_jseg(struct jseg *jseg, struct buf *bp)
3974 {
3975
3976 if (jseg->js_refs == 0)
3977 panic("handle_written_jseg: No self-reference on %p", jseg);
3978 jseg->js_state |= DEPCOMPLETE;
3979 /*
3980 * We'll never need this buffer again, set flags so it will be
3981 * discarded.
3982 */
3983 bp->b_flags |= B_INVAL | B_NOCACHE;
3984 pbrelvp(bp);
3985 complete_jsegs(jseg);
3986 }
3987
3988 static inline struct jsegdep *
inoref_jseg(struct inoref * inoref)3989 inoref_jseg(struct inoref *inoref)
3990 {
3991 struct jsegdep *jsegdep;
3992
3993 jsegdep = inoref->if_jsegdep;
3994 inoref->if_jsegdep = NULL;
3995
3996 return (jsegdep);
3997 }
3998
3999 /*
4000 * Called once a jremref has made it to stable store. The jremref is marked
4001 * complete and we attempt to free it. Any pagedeps writes sleeping waiting
4002 * for the jremref to complete will be awoken by free_jremref.
4003 */
4004 static void
handle_written_jremref(struct jremref * jremref)4005 handle_written_jremref(struct jremref *jremref)
4006 {
4007 struct inodedep *inodedep;
4008 struct jsegdep *jsegdep;
4009 struct dirrem *dirrem;
4010
4011 /* Grab the jsegdep. */
4012 jsegdep = inoref_jseg(&jremref->jr_ref);
4013 /*
4014 * Remove us from the inoref list.
4015 */
4016 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino,
4017 0, &inodedep) == 0)
4018 panic("handle_written_jremref: Lost inodedep");
4019 TAILQ_REMOVE(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps);
4020 /*
4021 * Complete the dirrem.
4022 */
4023 dirrem = jremref->jr_dirrem;
4024 jremref->jr_dirrem = NULL;
4025 LIST_REMOVE(jremref, jr_deps);
4026 jsegdep->jd_state |= jremref->jr_state & MKDIR_PARENT;
4027 jwork_insert(&dirrem->dm_jwork, jsegdep);
4028 if (LIST_EMPTY(&dirrem->dm_jremrefhd) &&
4029 (dirrem->dm_state & COMPLETE) != 0)
4030 add_to_worklist(&dirrem->dm_list, 0);
4031 free_jremref(jremref);
4032 }
4033
4034 /*
4035 * Called once a jaddref has made it to stable store. The dependency is
4036 * marked complete and any dependent structures are added to the inode
4037 * bufwait list to be completed as soon as it is written. If a bitmap write
4038 * depends on this entry we move the inode into the inodedephd of the
4039 * bmsafemap dependency and attempt to remove the jaddref from the bmsafemap.
4040 */
4041 static void
handle_written_jaddref(struct jaddref * jaddref)4042 handle_written_jaddref(struct jaddref *jaddref)
4043 {
4044 struct jsegdep *jsegdep;
4045 struct inodedep *inodedep;
4046 struct diradd *diradd;
4047 struct mkdir *mkdir;
4048
4049 /* Grab the jsegdep. */
4050 jsegdep = inoref_jseg(&jaddref->ja_ref);
4051 mkdir = NULL;
4052 diradd = NULL;
4053 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino,
4054 0, &inodedep) == 0)
4055 panic("handle_written_jaddref: Lost inodedep.");
4056 if (jaddref->ja_diradd == NULL)
4057 panic("handle_written_jaddref: No dependency");
4058 if (jaddref->ja_diradd->da_list.wk_type == D_DIRADD) {
4059 diradd = jaddref->ja_diradd;
4060 WORKLIST_INSERT(&inodedep->id_bufwait, &diradd->da_list);
4061 } else if (jaddref->ja_state & MKDIR_PARENT) {
4062 mkdir = jaddref->ja_mkdir;
4063 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir->md_list);
4064 } else if (jaddref->ja_state & MKDIR_BODY)
4065 mkdir = jaddref->ja_mkdir;
4066 else
4067 panic("handle_written_jaddref: Unknown dependency %p",
4068 jaddref->ja_diradd);
4069 jaddref->ja_diradd = NULL; /* also clears ja_mkdir */
4070 /*
4071 * Remove us from the inode list.
4072 */
4073 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, if_deps);
4074 /*
4075 * The mkdir may be waiting on the jaddref to clear before freeing.
4076 */
4077 if (mkdir) {
4078 KASSERT(mkdir->md_list.wk_type == D_MKDIR,
4079 ("handle_written_jaddref: Incorrect type for mkdir %s",
4080 TYPENAME(mkdir->md_list.wk_type)));
4081 mkdir->md_jaddref = NULL;
4082 diradd = mkdir->md_diradd;
4083 mkdir->md_state |= DEPCOMPLETE;
4084 complete_mkdir(mkdir);
4085 }
4086 jwork_insert(&diradd->da_jwork, jsegdep);
4087 if (jaddref->ja_state & NEWBLOCK) {
4088 inodedep->id_state |= ONDEPLIST;
4089 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_inodedephd,
4090 inodedep, id_deps);
4091 }
4092 free_jaddref(jaddref);
4093 }
4094
4095 /*
4096 * Called once a jnewblk journal is written. The allocdirect or allocindir
4097 * is placed in the bmsafemap to await notification of a written bitmap. If
4098 * the operation was canceled we add the segdep to the appropriate
4099 * dependency to free the journal space once the canceling operation
4100 * completes.
4101 */
4102 static void
handle_written_jnewblk(struct jnewblk * jnewblk)4103 handle_written_jnewblk(struct jnewblk *jnewblk)
4104 {
4105 struct bmsafemap *bmsafemap;
4106 struct freefrag *freefrag;
4107 struct freework *freework;
4108 struct jsegdep *jsegdep;
4109 struct newblk *newblk;
4110
4111 /* Grab the jsegdep. */
4112 jsegdep = jnewblk->jn_jsegdep;
4113 jnewblk->jn_jsegdep = NULL;
4114 if (jnewblk->jn_dep == NULL)
4115 panic("handle_written_jnewblk: No dependency for the segdep.");
4116 switch (jnewblk->jn_dep->wk_type) {
4117 case D_NEWBLK:
4118 case D_ALLOCDIRECT:
4119 case D_ALLOCINDIR:
4120 /*
4121 * Add the written block to the bmsafemap so it can
4122 * be notified when the bitmap is on disk.
4123 */
4124 newblk = WK_NEWBLK(jnewblk->jn_dep);
4125 newblk->nb_jnewblk = NULL;
4126 if ((newblk->nb_state & GOINGAWAY) == 0) {
4127 bmsafemap = newblk->nb_bmsafemap;
4128 newblk->nb_state |= ONDEPLIST;
4129 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk,
4130 nb_deps);
4131 }
4132 jwork_insert(&newblk->nb_jwork, jsegdep);
4133 break;
4134 case D_FREEFRAG:
4135 /*
4136 * A newblock being removed by a freefrag when replaced by
4137 * frag extension.
4138 */
4139 freefrag = WK_FREEFRAG(jnewblk->jn_dep);
4140 freefrag->ff_jdep = NULL;
4141 jwork_insert(&freefrag->ff_jwork, jsegdep);
4142 break;
4143 case D_FREEWORK:
4144 /*
4145 * A direct block was removed by truncate.
4146 */
4147 freework = WK_FREEWORK(jnewblk->jn_dep);
4148 freework->fw_jnewblk = NULL;
4149 jwork_insert(&freework->fw_freeblks->fb_jwork, jsegdep);
4150 break;
4151 default:
4152 panic("handle_written_jnewblk: Unknown type %d.",
4153 jnewblk->jn_dep->wk_type);
4154 }
4155 jnewblk->jn_dep = NULL;
4156 free_jnewblk(jnewblk);
4157 }
4158
4159 /*
4160 * Cancel a jfreefrag that won't be needed, probably due to colliding with
4161 * an in-flight allocation that has not yet been committed. Divorce us
4162 * from the freefrag and mark it DEPCOMPLETE so that it may be added
4163 * to the worklist.
4164 */
4165 static void
cancel_jfreefrag(struct jfreefrag * jfreefrag)4166 cancel_jfreefrag(struct jfreefrag *jfreefrag)
4167 {
4168 struct freefrag *freefrag;
4169
4170 if (jfreefrag->fr_jsegdep) {
4171 free_jsegdep(jfreefrag->fr_jsegdep);
4172 jfreefrag->fr_jsegdep = NULL;
4173 }
4174 freefrag = jfreefrag->fr_freefrag;
4175 jfreefrag->fr_freefrag = NULL;
4176 free_jfreefrag(jfreefrag);
4177 freefrag->ff_state |= DEPCOMPLETE;
4178 CTR1(KTR_SUJ, "cancel_jfreefrag: blkno %jd", freefrag->ff_blkno);
4179 }
4180
4181 /*
4182 * Free a jfreefrag when the parent freefrag is rendered obsolete.
4183 */
4184 static void
free_jfreefrag(struct jfreefrag * jfreefrag)4185 free_jfreefrag(struct jfreefrag *jfreefrag)
4186 {
4187
4188 if (jfreefrag->fr_state & INPROGRESS)
4189 WORKLIST_REMOVE(&jfreefrag->fr_list);
4190 else if (jfreefrag->fr_state & ONWORKLIST)
4191 remove_from_journal(&jfreefrag->fr_list);
4192 if (jfreefrag->fr_freefrag != NULL)
4193 panic("free_jfreefrag: Still attached to a freefrag.");
4194 WORKITEM_FREE(jfreefrag, D_JFREEFRAG);
4195 }
4196
4197 /*
4198 * Called when the journal write for a jfreefrag completes. The parent
4199 * freefrag is added to the worklist if this completes its dependencies.
4200 */
4201 static void
handle_written_jfreefrag(struct jfreefrag * jfreefrag)4202 handle_written_jfreefrag(struct jfreefrag *jfreefrag)
4203 {
4204 struct jsegdep *jsegdep;
4205 struct freefrag *freefrag;
4206
4207 /* Grab the jsegdep. */
4208 jsegdep = jfreefrag->fr_jsegdep;
4209 jfreefrag->fr_jsegdep = NULL;
4210 freefrag = jfreefrag->fr_freefrag;
4211 if (freefrag == NULL)
4212 panic("handle_written_jfreefrag: No freefrag.");
4213 freefrag->ff_state |= DEPCOMPLETE;
4214 freefrag->ff_jdep = NULL;
4215 jwork_insert(&freefrag->ff_jwork, jsegdep);
4216 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE)
4217 add_to_worklist(&freefrag->ff_list, 0);
4218 jfreefrag->fr_freefrag = NULL;
4219 free_jfreefrag(jfreefrag);
4220 }
4221
4222 /*
4223 * Called when the journal write for a jfreeblk completes. The jfreeblk
4224 * is removed from the freeblks list of pending journal writes and the
4225 * jsegdep is moved to the freeblks jwork to be completed when all blocks
4226 * have been reclaimed.
4227 */
4228 static void
handle_written_jblkdep(struct jblkdep * jblkdep)4229 handle_written_jblkdep(struct jblkdep *jblkdep)
4230 {
4231 struct freeblks *freeblks;
4232 struct jsegdep *jsegdep;
4233
4234 /* Grab the jsegdep. */
4235 jsegdep = jblkdep->jb_jsegdep;
4236 jblkdep->jb_jsegdep = NULL;
4237 freeblks = jblkdep->jb_freeblks;
4238 LIST_REMOVE(jblkdep, jb_deps);
4239 jwork_insert(&freeblks->fb_jwork, jsegdep);
4240 /*
4241 * If the freeblks is all journaled, we can add it to the worklist.
4242 */
4243 if (LIST_EMPTY(&freeblks->fb_jblkdephd) &&
4244 (freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE)
4245 add_to_worklist(&freeblks->fb_list, WK_NODELAY);
4246
4247 free_jblkdep(jblkdep);
4248 }
4249
4250 static struct jsegdep *
newjsegdep(struct worklist * wk)4251 newjsegdep(struct worklist *wk)
4252 {
4253 struct jsegdep *jsegdep;
4254
4255 jsegdep = malloc(sizeof(*jsegdep), M_JSEGDEP, M_SOFTDEP_FLAGS);
4256 workitem_alloc(&jsegdep->jd_list, D_JSEGDEP, wk->wk_mp);
4257 jsegdep->jd_seg = NULL;
4258
4259 return (jsegdep);
4260 }
4261
4262 static struct jmvref *
newjmvref(struct inode * dp,ino_t ino,off_t oldoff,off_t newoff)4263 newjmvref(struct inode *dp,
4264 ino_t ino,
4265 off_t oldoff,
4266 off_t newoff)
4267 {
4268 struct jmvref *jmvref;
4269
4270 jmvref = malloc(sizeof(*jmvref), M_JMVREF, M_SOFTDEP_FLAGS);
4271 workitem_alloc(&jmvref->jm_list, D_JMVREF, ITOVFS(dp));
4272 jmvref->jm_list.wk_state = ATTACHED | DEPCOMPLETE;
4273 jmvref->jm_parent = dp->i_number;
4274 jmvref->jm_ino = ino;
4275 jmvref->jm_oldoff = oldoff;
4276 jmvref->jm_newoff = newoff;
4277
4278 return (jmvref);
4279 }
4280
4281 /*
4282 * Allocate a new jremref that tracks the removal of ip from dp with the
4283 * directory entry offset of diroff. Mark the entry as ATTACHED and
4284 * DEPCOMPLETE as we have all the information required for the journal write
4285 * and the directory has already been removed from the buffer. The caller
4286 * is responsible for linking the jremref into the pagedep and adding it
4287 * to the journal to write. The MKDIR_PARENT flag is set if we're doing
4288 * a DOTDOT addition so handle_workitem_remove() can properly assign
4289 * the jsegdep when we're done.
4290 */
4291 static struct jremref *
newjremref(struct dirrem * dirrem,struct inode * dp,struct inode * ip,off_t diroff,nlink_t nlink)4292 newjremref(struct dirrem *dirrem,
4293 struct inode *dp,
4294 struct inode *ip,
4295 off_t diroff,
4296 nlink_t nlink)
4297 {
4298 struct jremref *jremref;
4299
4300 jremref = malloc(sizeof(*jremref), M_JREMREF, M_SOFTDEP_FLAGS);
4301 workitem_alloc(&jremref->jr_list, D_JREMREF, ITOVFS(dp));
4302 jremref->jr_state = ATTACHED;
4303 newinoref(&jremref->jr_ref, ip->i_number, dp->i_number, diroff,
4304 nlink, ip->i_mode);
4305 jremref->jr_dirrem = dirrem;
4306
4307 return (jremref);
4308 }
4309
4310 static inline void
newinoref(struct inoref * inoref,ino_t ino,ino_t parent,off_t diroff,nlink_t nlink,uint16_t mode)4311 newinoref(struct inoref *inoref,
4312 ino_t ino,
4313 ino_t parent,
4314 off_t diroff,
4315 nlink_t nlink,
4316 uint16_t mode)
4317 {
4318
4319 inoref->if_jsegdep = newjsegdep(&inoref->if_list);
4320 inoref->if_diroff = diroff;
4321 inoref->if_ino = ino;
4322 inoref->if_parent = parent;
4323 inoref->if_nlink = nlink;
4324 inoref->if_mode = mode;
4325 }
4326
4327 /*
4328 * Allocate a new jaddref to track the addition of ino to dp at diroff. The
4329 * directory offset may not be known until later. The caller is responsible
4330 * adding the entry to the journal when this information is available. nlink
4331 * should be the link count prior to the addition and mode is only required
4332 * to have the correct FMT.
4333 */
4334 static struct jaddref *
newjaddref(struct inode * dp,ino_t ino,off_t diroff,int16_t nlink,uint16_t mode)4335 newjaddref(struct inode *dp,
4336 ino_t ino,
4337 off_t diroff,
4338 int16_t nlink,
4339 uint16_t mode)
4340 {
4341 struct jaddref *jaddref;
4342
4343 jaddref = malloc(sizeof(*jaddref), M_JADDREF, M_SOFTDEP_FLAGS);
4344 workitem_alloc(&jaddref->ja_list, D_JADDREF, ITOVFS(dp));
4345 jaddref->ja_state = ATTACHED;
4346 jaddref->ja_mkdir = NULL;
4347 newinoref(&jaddref->ja_ref, ino, dp->i_number, diroff, nlink, mode);
4348
4349 return (jaddref);
4350 }
4351
4352 /*
4353 * Create a new free dependency for a freework. The caller is responsible
4354 * for adjusting the reference count when it has the lock held. The freedep
4355 * will track an outstanding bitmap write that will ultimately clear the
4356 * freework to continue.
4357 */
4358 static struct freedep *
newfreedep(struct freework * freework)4359 newfreedep(struct freework *freework)
4360 {
4361 struct freedep *freedep;
4362
4363 freedep = malloc(sizeof(*freedep), M_FREEDEP, M_SOFTDEP_FLAGS);
4364 workitem_alloc(&freedep->fd_list, D_FREEDEP, freework->fw_list.wk_mp);
4365 freedep->fd_freework = freework;
4366
4367 return (freedep);
4368 }
4369
4370 /*
4371 * Free a freedep structure once the buffer it is linked to is written. If
4372 * this is the last reference to the freework schedule it for completion.
4373 */
4374 static void
free_freedep(struct freedep * freedep)4375 free_freedep(struct freedep *freedep)
4376 {
4377 struct freework *freework;
4378
4379 freework = freedep->fd_freework;
4380 freework->fw_freeblks->fb_cgwait--;
4381 if (--freework->fw_ref == 0)
4382 freework_enqueue(freework);
4383 WORKITEM_FREE(freedep, D_FREEDEP);
4384 }
4385
4386 /*
4387 * Allocate a new freework structure that may be a level in an indirect
4388 * when parent is not NULL or a top level block when it is. The top level
4389 * freework structures are allocated without the per-filesystem lock held
4390 * and before the freeblks is visible outside of softdep_setup_freeblocks().
4391 */
4392 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)4393 newfreework(struct ufsmount *ump,
4394 struct freeblks *freeblks,
4395 struct freework *parent,
4396 ufs_lbn_t lbn,
4397 ufs2_daddr_t nb,
4398 int frags,
4399 int off,
4400 int journal)
4401 {
4402 struct freework *freework;
4403
4404 freework = malloc(sizeof(*freework), M_FREEWORK, M_SOFTDEP_FLAGS);
4405 workitem_alloc(&freework->fw_list, D_FREEWORK, freeblks->fb_list.wk_mp);
4406 freework->fw_state = ATTACHED;
4407 freework->fw_jnewblk = NULL;
4408 freework->fw_freeblks = freeblks;
4409 freework->fw_parent = parent;
4410 freework->fw_lbn = lbn;
4411 freework->fw_blkno = nb;
4412 freework->fw_frags = frags;
4413 freework->fw_indir = NULL;
4414 freework->fw_ref = (MOUNTEDSUJ(UFSTOVFS(ump)) == 0 ||
4415 lbn >= -UFS_NXADDR) ? 0 : NINDIR(ump->um_fs) + 1;
4416 freework->fw_start = freework->fw_off = off;
4417 if (journal)
4418 newjfreeblk(freeblks, lbn, nb, frags);
4419 if (parent == NULL) {
4420 ACQUIRE_LOCK(ump);
4421 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list);
4422 freeblks->fb_ref++;
4423 FREE_LOCK(ump);
4424 }
4425
4426 return (freework);
4427 }
4428
4429 /*
4430 * Eliminate a jfreeblk for a block that does not need journaling.
4431 */
4432 static void
cancel_jfreeblk(struct freeblks * freeblks,ufs2_daddr_t blkno)4433 cancel_jfreeblk(struct freeblks *freeblks, ufs2_daddr_t blkno)
4434 {
4435 struct jfreeblk *jfreeblk;
4436 struct jblkdep *jblkdep;
4437
4438 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps) {
4439 if (jblkdep->jb_list.wk_type != D_JFREEBLK)
4440 continue;
4441 jfreeblk = WK_JFREEBLK(&jblkdep->jb_list);
4442 if (jfreeblk->jf_blkno == blkno)
4443 break;
4444 }
4445 if (jblkdep == NULL)
4446 return;
4447 CTR1(KTR_SUJ, "cancel_jfreeblk: blkno %jd", blkno);
4448 free_jsegdep(jblkdep->jb_jsegdep);
4449 LIST_REMOVE(jblkdep, jb_deps);
4450 WORKITEM_FREE(jfreeblk, D_JFREEBLK);
4451 }
4452
4453 /*
4454 * Allocate a new jfreeblk to journal top level block pointer when truncating
4455 * a file. The caller must add this to the worklist when the per-filesystem
4456 * lock is held.
4457 */
4458 static struct jfreeblk *
newjfreeblk(struct freeblks * freeblks,ufs_lbn_t lbn,ufs2_daddr_t blkno,int frags)4459 newjfreeblk(struct freeblks *freeblks,
4460 ufs_lbn_t lbn,
4461 ufs2_daddr_t blkno,
4462 int frags)
4463 {
4464 struct jfreeblk *jfreeblk;
4465
4466 jfreeblk = malloc(sizeof(*jfreeblk), M_JFREEBLK, M_SOFTDEP_FLAGS);
4467 workitem_alloc(&jfreeblk->jf_dep.jb_list, D_JFREEBLK,
4468 freeblks->fb_list.wk_mp);
4469 jfreeblk->jf_dep.jb_jsegdep = newjsegdep(&jfreeblk->jf_dep.jb_list);
4470 jfreeblk->jf_dep.jb_freeblks = freeblks;
4471 jfreeblk->jf_ino = freeblks->fb_inum;
4472 jfreeblk->jf_lbn = lbn;
4473 jfreeblk->jf_blkno = blkno;
4474 jfreeblk->jf_frags = frags;
4475 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jfreeblk->jf_dep, jb_deps);
4476
4477 return (jfreeblk);
4478 }
4479
4480 /*
4481 * The journal is only prepared to handle full-size block numbers, so we
4482 * have to adjust the record to reflect the change to a full-size block.
4483 * For example, suppose we have a block made up of fragments 8-15 and
4484 * want to free its last two fragments. We are given a request that says:
4485 * FREEBLK ino=5, blkno=14, lbn=0, frags=2, oldfrags=0
4486 * where frags are the number of fragments to free and oldfrags are the
4487 * number of fragments to keep. To block align it, we have to change it to
4488 * have a valid full-size blkno, so it becomes:
4489 * FREEBLK ino=5, blkno=8, lbn=0, frags=2, oldfrags=6
4490 */
4491 static void
adjust_newfreework(struct freeblks * freeblks,int frag_offset)4492 adjust_newfreework(struct freeblks *freeblks, int frag_offset)
4493 {
4494 struct jfreeblk *jfreeblk;
4495
4496 KASSERT((LIST_FIRST(&freeblks->fb_jblkdephd) != NULL &&
4497 LIST_FIRST(&freeblks->fb_jblkdephd)->jb_list.wk_type == D_JFREEBLK),
4498 ("adjust_newfreework: Missing freeblks dependency"));
4499
4500 jfreeblk = WK_JFREEBLK(LIST_FIRST(&freeblks->fb_jblkdephd));
4501 jfreeblk->jf_blkno -= frag_offset;
4502 jfreeblk->jf_frags += frag_offset;
4503 }
4504
4505 /*
4506 * Allocate a new jtrunc to track a partial truncation.
4507 */
4508 static struct jtrunc *
newjtrunc(struct freeblks * freeblks,off_t size,int extsize)4509 newjtrunc(struct freeblks *freeblks,
4510 off_t size,
4511 int extsize)
4512 {
4513 struct jtrunc *jtrunc;
4514
4515 jtrunc = malloc(sizeof(*jtrunc), M_JTRUNC, M_SOFTDEP_FLAGS);
4516 workitem_alloc(&jtrunc->jt_dep.jb_list, D_JTRUNC,
4517 freeblks->fb_list.wk_mp);
4518 jtrunc->jt_dep.jb_jsegdep = newjsegdep(&jtrunc->jt_dep.jb_list);
4519 jtrunc->jt_dep.jb_freeblks = freeblks;
4520 jtrunc->jt_ino = freeblks->fb_inum;
4521 jtrunc->jt_size = size;
4522 jtrunc->jt_extsize = extsize;
4523 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jtrunc->jt_dep, jb_deps);
4524
4525 return (jtrunc);
4526 }
4527
4528 /*
4529 * If we're canceling a new bitmap we have to search for another ref
4530 * to move into the bmsafemap dep. This might be better expressed
4531 * with another structure.
4532 */
4533 static void
move_newblock_dep(struct jaddref * jaddref,struct inodedep * inodedep)4534 move_newblock_dep(struct jaddref *jaddref, struct inodedep *inodedep)
4535 {
4536 struct inoref *inoref;
4537 struct jaddref *jaddrefn;
4538
4539 jaddrefn = NULL;
4540 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref;
4541 inoref = TAILQ_NEXT(inoref, if_deps)) {
4542 if ((jaddref->ja_state & NEWBLOCK) &&
4543 inoref->if_list.wk_type == D_JADDREF) {
4544 jaddrefn = (struct jaddref *)inoref;
4545 break;
4546 }
4547 }
4548 if (jaddrefn == NULL)
4549 return;
4550 jaddrefn->ja_state &= ~(ATTACHED | UNDONE);
4551 jaddrefn->ja_state |= jaddref->ja_state &
4552 (ATTACHED | UNDONE | NEWBLOCK);
4553 jaddref->ja_state &= ~(ATTACHED | UNDONE | NEWBLOCK);
4554 jaddref->ja_state |= ATTACHED;
4555 LIST_REMOVE(jaddref, ja_bmdeps);
4556 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_jaddrefhd, jaddrefn,
4557 ja_bmdeps);
4558 }
4559
4560 /*
4561 * Cancel a jaddref either before it has been written or while it is being
4562 * written. This happens when a link is removed before the add reaches
4563 * the disk. The jaddref dependency is kept linked into the bmsafemap
4564 * and inode to prevent the link count or bitmap from reaching the disk
4565 * until handle_workitem_remove() re-adjusts the counts and bitmaps as
4566 * required.
4567 *
4568 * Returns 1 if the canceled addref requires journaling of the remove and
4569 * 0 otherwise.
4570 */
4571 static int
cancel_jaddref(struct jaddref * jaddref,struct inodedep * inodedep,struct workhead * wkhd)4572 cancel_jaddref(struct jaddref *jaddref,
4573 struct inodedep *inodedep,
4574 struct workhead *wkhd)
4575 {
4576 struct inoref *inoref;
4577 struct jsegdep *jsegdep;
4578 int needsj;
4579
4580 KASSERT((jaddref->ja_state & COMPLETE) == 0,
4581 ("cancel_jaddref: Canceling complete jaddref"));
4582 if (jaddref->ja_state & (INPROGRESS | COMPLETE))
4583 needsj = 1;
4584 else
4585 needsj = 0;
4586 if (inodedep == NULL)
4587 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino,
4588 0, &inodedep) == 0)
4589 panic("cancel_jaddref: Lost inodedep");
4590 /*
4591 * We must adjust the nlink of any reference operation that follows
4592 * us so that it is consistent with the in-memory reference. This
4593 * ensures that inode nlink rollbacks always have the correct link.
4594 */
4595 if (needsj == 0) {
4596 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref;
4597 inoref = TAILQ_NEXT(inoref, if_deps)) {
4598 if (inoref->if_state & GOINGAWAY)
4599 break;
4600 inoref->if_nlink--;
4601 }
4602 }
4603 jsegdep = inoref_jseg(&jaddref->ja_ref);
4604 if (jaddref->ja_state & NEWBLOCK)
4605 move_newblock_dep(jaddref, inodedep);
4606 wake_worklist(&jaddref->ja_list);
4607 jaddref->ja_mkdir = NULL;
4608 if (jaddref->ja_state & INPROGRESS) {
4609 jaddref->ja_state &= ~INPROGRESS;
4610 WORKLIST_REMOVE(&jaddref->ja_list);
4611 jwork_insert(wkhd, jsegdep);
4612 } else {
4613 free_jsegdep(jsegdep);
4614 if (jaddref->ja_state & DEPCOMPLETE)
4615 remove_from_journal(&jaddref->ja_list);
4616 }
4617 jaddref->ja_state |= (GOINGAWAY | DEPCOMPLETE);
4618 /*
4619 * Leave NEWBLOCK jaddrefs on the inodedep so handle_workitem_remove
4620 * can arrange for them to be freed with the bitmap. Otherwise we
4621 * no longer need this addref attached to the inoreflst and it
4622 * will incorrectly adjust nlink if we leave it.
4623 */
4624 if ((jaddref->ja_state & NEWBLOCK) == 0) {
4625 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref,
4626 if_deps);
4627 jaddref->ja_state |= COMPLETE;
4628 free_jaddref(jaddref);
4629 return (needsj);
4630 }
4631 /*
4632 * Leave the head of the list for jsegdeps for fast merging.
4633 */
4634 if (LIST_FIRST(wkhd) != NULL) {
4635 jaddref->ja_state |= ONWORKLIST;
4636 LIST_INSERT_AFTER(LIST_FIRST(wkhd), &jaddref->ja_list, wk_list);
4637 } else
4638 WORKLIST_INSERT(wkhd, &jaddref->ja_list);
4639
4640 return (needsj);
4641 }
4642
4643 /*
4644 * Attempt to free a jaddref structure when some work completes. This
4645 * should only succeed once the entry is written and all dependencies have
4646 * been notified.
4647 */
4648 static void
free_jaddref(struct jaddref * jaddref)4649 free_jaddref(struct jaddref *jaddref)
4650 {
4651
4652 if ((jaddref->ja_state & ALLCOMPLETE) != ALLCOMPLETE)
4653 return;
4654 if (jaddref->ja_ref.if_jsegdep)
4655 panic("free_jaddref: segdep attached to jaddref %p(0x%X)\n",
4656 jaddref, jaddref->ja_state);
4657 if (jaddref->ja_state & NEWBLOCK)
4658 LIST_REMOVE(jaddref, ja_bmdeps);
4659 if (jaddref->ja_state & (INPROGRESS | ONWORKLIST))
4660 panic("free_jaddref: Bad state %p(0x%X)",
4661 jaddref, jaddref->ja_state);
4662 if (jaddref->ja_mkdir != NULL)
4663 panic("free_jaddref: Work pending, 0x%X\n", jaddref->ja_state);
4664 WORKITEM_FREE(jaddref, D_JADDREF);
4665 }
4666
4667 /*
4668 * Free a jremref structure once it has been written or discarded.
4669 */
4670 static void
free_jremref(struct jremref * jremref)4671 free_jremref(struct jremref *jremref)
4672 {
4673
4674 if (jremref->jr_ref.if_jsegdep)
4675 free_jsegdep(jremref->jr_ref.if_jsegdep);
4676 if (jremref->jr_state & INPROGRESS)
4677 panic("free_jremref: IO still pending");
4678 WORKITEM_FREE(jremref, D_JREMREF);
4679 }
4680
4681 /*
4682 * Free a jnewblk structure.
4683 */
4684 static void
free_jnewblk(struct jnewblk * jnewblk)4685 free_jnewblk(struct jnewblk *jnewblk)
4686 {
4687
4688 if ((jnewblk->jn_state & ALLCOMPLETE) != ALLCOMPLETE)
4689 return;
4690 LIST_REMOVE(jnewblk, jn_deps);
4691 if (jnewblk->jn_dep != NULL)
4692 panic("free_jnewblk: Dependency still attached.");
4693 WORKITEM_FREE(jnewblk, D_JNEWBLK);
4694 }
4695
4696 /*
4697 * Cancel a jnewblk which has been been made redundant by frag extension.
4698 */
4699 static void
cancel_jnewblk(struct jnewblk * jnewblk,struct workhead * wkhd)4700 cancel_jnewblk(struct jnewblk *jnewblk, struct workhead *wkhd)
4701 {
4702 struct jsegdep *jsegdep;
4703
4704 CTR1(KTR_SUJ, "cancel_jnewblk: blkno %jd", jnewblk->jn_blkno);
4705 jsegdep = jnewblk->jn_jsegdep;
4706 if (jnewblk->jn_jsegdep == NULL || jnewblk->jn_dep == NULL)
4707 panic("cancel_jnewblk: Invalid state");
4708 jnewblk->jn_jsegdep = NULL;
4709 jnewblk->jn_dep = NULL;
4710 jnewblk->jn_state |= GOINGAWAY;
4711 if (jnewblk->jn_state & INPROGRESS) {
4712 jnewblk->jn_state &= ~INPROGRESS;
4713 WORKLIST_REMOVE(&jnewblk->jn_list);
4714 jwork_insert(wkhd, jsegdep);
4715 } else {
4716 free_jsegdep(jsegdep);
4717 remove_from_journal(&jnewblk->jn_list);
4718 }
4719 wake_worklist(&jnewblk->jn_list);
4720 WORKLIST_INSERT(wkhd, &jnewblk->jn_list);
4721 }
4722
4723 static void
free_jblkdep(struct jblkdep * jblkdep)4724 free_jblkdep(struct jblkdep *jblkdep)
4725 {
4726
4727 if (jblkdep->jb_list.wk_type == D_JFREEBLK)
4728 WORKITEM_FREE(jblkdep, D_JFREEBLK);
4729 else if (jblkdep->jb_list.wk_type == D_JTRUNC)
4730 WORKITEM_FREE(jblkdep, D_JTRUNC);
4731 else
4732 panic("free_jblkdep: Unexpected type %s",
4733 TYPENAME(jblkdep->jb_list.wk_type));
4734 }
4735
4736 /*
4737 * Free a single jseg once it is no longer referenced in memory or on
4738 * disk. Reclaim journal blocks and dependencies waiting for the segment
4739 * to disappear.
4740 */
4741 static void
free_jseg(struct jseg * jseg,struct jblocks * jblocks)4742 free_jseg(struct jseg *jseg, struct jblocks *jblocks)
4743 {
4744 struct freework *freework;
4745
4746 /*
4747 * Free freework structures that were lingering to indicate freed
4748 * indirect blocks that forced journal write ordering on reallocate.
4749 */
4750 while ((freework = LIST_FIRST(&jseg->js_indirs)) != NULL)
4751 indirblk_remove(freework);
4752 if (jblocks->jb_oldestseg == jseg)
4753 jblocks->jb_oldestseg = TAILQ_NEXT(jseg, js_next);
4754 TAILQ_REMOVE(&jblocks->jb_segs, jseg, js_next);
4755 jblocks_free(jblocks, jseg->js_list.wk_mp, jseg->js_size);
4756 KASSERT(LIST_EMPTY(&jseg->js_entries),
4757 ("free_jseg: Freed jseg has valid entries."));
4758 WORKITEM_FREE(jseg, D_JSEG);
4759 }
4760
4761 /*
4762 * Free all jsegs that meet the criteria for being reclaimed and update
4763 * oldestseg.
4764 */
4765 static void
free_jsegs(struct jblocks * jblocks)4766 free_jsegs(struct jblocks *jblocks)
4767 {
4768 struct jseg *jseg;
4769
4770 /*
4771 * Free only those jsegs which have none allocated before them to
4772 * preserve the journal space ordering.
4773 */
4774 while ((jseg = TAILQ_FIRST(&jblocks->jb_segs)) != NULL) {
4775 /*
4776 * Only reclaim space when nothing depends on this journal
4777 * set and another set has written that it is no longer
4778 * valid.
4779 */
4780 if (jseg->js_refs != 0) {
4781 jblocks->jb_oldestseg = jseg;
4782 return;
4783 }
4784 if ((jseg->js_state & ALLCOMPLETE) != ALLCOMPLETE)
4785 break;
4786 if (jseg->js_seq > jblocks->jb_oldestwrseq)
4787 break;
4788 /*
4789 * We can free jsegs that didn't write entries when
4790 * oldestwrseq == js_seq.
4791 */
4792 if (jseg->js_seq == jblocks->jb_oldestwrseq &&
4793 jseg->js_cnt != 0)
4794 break;
4795 free_jseg(jseg, jblocks);
4796 }
4797 /*
4798 * If we exited the loop above we still must discover the
4799 * oldest valid segment.
4800 */
4801 if (jseg)
4802 for (jseg = jblocks->jb_oldestseg; jseg != NULL;
4803 jseg = TAILQ_NEXT(jseg, js_next))
4804 if (jseg->js_refs != 0)
4805 break;
4806 jblocks->jb_oldestseg = jseg;
4807 /*
4808 * The journal has no valid records but some jsegs may still be
4809 * waiting on oldestwrseq to advance. We force a small record
4810 * out to permit these lingering records to be reclaimed.
4811 */
4812 if (jblocks->jb_oldestseg == NULL && !TAILQ_EMPTY(&jblocks->jb_segs))
4813 jblocks->jb_needseg = 1;
4814 }
4815
4816 /*
4817 * Release one reference to a jseg and free it if the count reaches 0. This
4818 * should eventually reclaim journal space as well.
4819 */
4820 static void
rele_jseg(struct jseg * jseg)4821 rele_jseg(struct jseg *jseg)
4822 {
4823
4824 KASSERT(jseg->js_refs > 0,
4825 ("free_jseg: Invalid refcnt %d", jseg->js_refs));
4826 if (--jseg->js_refs != 0)
4827 return;
4828 free_jsegs(jseg->js_jblocks);
4829 }
4830
4831 /*
4832 * Release a jsegdep and decrement the jseg count.
4833 */
4834 static void
free_jsegdep(struct jsegdep * jsegdep)4835 free_jsegdep(struct jsegdep *jsegdep)
4836 {
4837
4838 if (jsegdep->jd_seg)
4839 rele_jseg(jsegdep->jd_seg);
4840 WORKITEM_FREE(jsegdep, D_JSEGDEP);
4841 }
4842
4843 /*
4844 * Wait for a journal item to make it to disk. Initiate journal processing
4845 * if required.
4846 */
4847 static int
jwait(struct worklist * wk,int waitfor)4848 jwait(struct worklist *wk, int waitfor)
4849 {
4850
4851 LOCK_OWNED(VFSTOUFS(wk->wk_mp));
4852 /*
4853 * Blocking journal waits cause slow synchronous behavior. Record
4854 * stats on the frequency of these blocking operations.
4855 */
4856 if (waitfor == MNT_WAIT) {
4857 stat_journal_wait++;
4858 switch (wk->wk_type) {
4859 case D_JREMREF:
4860 case D_JMVREF:
4861 stat_jwait_filepage++;
4862 break;
4863 case D_JTRUNC:
4864 case D_JFREEBLK:
4865 stat_jwait_freeblks++;
4866 break;
4867 case D_JNEWBLK:
4868 stat_jwait_newblk++;
4869 break;
4870 case D_JADDREF:
4871 stat_jwait_inode++;
4872 break;
4873 default:
4874 break;
4875 }
4876 }
4877 /*
4878 * If IO has not started we process the journal. We can't mark the
4879 * worklist item as IOWAITING because we drop the lock while
4880 * processing the journal and the worklist entry may be freed after
4881 * this point. The caller may call back in and re-issue the request.
4882 */
4883 if ((wk->wk_state & INPROGRESS) == 0) {
4884 softdep_process_journal(wk->wk_mp, wk, waitfor);
4885 if (waitfor != MNT_WAIT)
4886 return (EBUSY);
4887 return (0);
4888 }
4889 if (waitfor != MNT_WAIT)
4890 return (EBUSY);
4891 wait_worklist(wk, "jwait");
4892 return (0);
4893 }
4894
4895 /*
4896 * Lookup an inodedep based on an inode pointer and set the nlinkdelta as
4897 * appropriate. This is a convenience function to reduce duplicate code
4898 * for the setup and revert functions below.
4899 */
4900 static struct inodedep *
inodedep_lookup_ip(struct inode * ip)4901 inodedep_lookup_ip(struct inode *ip)
4902 {
4903 struct inodedep *inodedep;
4904
4905 KASSERT(ip->i_nlink >= ip->i_effnlink,
4906 ("inodedep_lookup_ip: bad delta"));
4907 (void) inodedep_lookup(ITOVFS(ip), ip->i_number, DEPALLOC,
4908 &inodedep);
4909 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
4910 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked"));
4911
4912 return (inodedep);
4913 }
4914
4915 /*
4916 * Called prior to creating a new inode and linking it to a directory. The
4917 * jaddref structure must already be allocated by softdep_setup_inomapdep
4918 * and it is discovered here so we can initialize the mode and update
4919 * nlinkdelta.
4920 */
4921 void
softdep_setup_create(struct inode * dp,struct inode * ip)4922 softdep_setup_create(struct inode *dp, struct inode *ip)
4923 {
4924 struct inodedep *inodedep;
4925 struct jaddref *jaddref __diagused;
4926 struct vnode *dvp;
4927
4928 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
4929 ("softdep_setup_create called on non-softdep filesystem"));
4930 KASSERT(ip->i_nlink == 1,
4931 ("softdep_setup_create: Invalid link count."));
4932 dvp = ITOV(dp);
4933 ACQUIRE_LOCK(ITOUMP(dp));
4934 inodedep = inodedep_lookup_ip(ip);
4935 if (DOINGSUJ(dvp)) {
4936 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
4937 inoreflst);
4938 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number,
4939 ("softdep_setup_create: No addref structure present."));
4940 }
4941 FREE_LOCK(ITOUMP(dp));
4942 }
4943
4944 /*
4945 * Create a jaddref structure to track the addition of a DOTDOT link when
4946 * we are reparenting an inode as part of a rename. This jaddref will be
4947 * found by softdep_setup_directory_change. Adjusts nlinkdelta for
4948 * non-journaling softdep.
4949 */
4950 void
softdep_setup_dotdot_link(struct inode * dp,struct inode * ip)4951 softdep_setup_dotdot_link(struct inode *dp, struct inode *ip)
4952 {
4953 struct inodedep *inodedep;
4954 struct jaddref *jaddref;
4955 struct vnode *dvp;
4956
4957 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
4958 ("softdep_setup_dotdot_link called on non-softdep filesystem"));
4959 dvp = ITOV(dp);
4960 jaddref = NULL;
4961 /*
4962 * We don't set MKDIR_PARENT as this is not tied to a mkdir and
4963 * is used as a normal link would be.
4964 */
4965 if (DOINGSUJ(dvp))
4966 jaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET,
4967 dp->i_effnlink - 1, dp->i_mode);
4968 ACQUIRE_LOCK(ITOUMP(dp));
4969 inodedep = inodedep_lookup_ip(dp);
4970 if (jaddref)
4971 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref,
4972 if_deps);
4973 FREE_LOCK(ITOUMP(dp));
4974 }
4975
4976 /*
4977 * Create a jaddref structure to track a new link to an inode. The directory
4978 * offset is not known until softdep_setup_directory_add or
4979 * softdep_setup_directory_change. Adjusts nlinkdelta for non-journaling
4980 * softdep.
4981 */
4982 void
softdep_setup_link(struct inode * dp,struct inode * ip)4983 softdep_setup_link(struct inode *dp, struct inode *ip)
4984 {
4985 struct inodedep *inodedep;
4986 struct jaddref *jaddref;
4987 struct vnode *dvp;
4988
4989 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
4990 ("softdep_setup_link called on non-softdep filesystem"));
4991 dvp = ITOV(dp);
4992 jaddref = NULL;
4993 if (DOINGSUJ(dvp))
4994 jaddref = newjaddref(dp, ip->i_number, 0, ip->i_effnlink - 1,
4995 ip->i_mode);
4996 ACQUIRE_LOCK(ITOUMP(dp));
4997 inodedep = inodedep_lookup_ip(ip);
4998 if (jaddref)
4999 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref,
5000 if_deps);
5001 FREE_LOCK(ITOUMP(dp));
5002 }
5003
5004 /*
5005 * Called to create the jaddref structures to track . and .. references as
5006 * well as lookup and further initialize the incomplete jaddref created
5007 * by softdep_setup_inomapdep when the inode was allocated. Adjusts
5008 * nlinkdelta for non-journaling softdep.
5009 */
5010 void
softdep_setup_mkdir(struct inode * dp,struct inode * ip)5011 softdep_setup_mkdir(struct inode *dp, struct inode *ip)
5012 {
5013 struct inodedep *inodedep;
5014 struct jaddref *dotdotaddref;
5015 struct jaddref *dotaddref;
5016 struct jaddref *jaddref;
5017 struct vnode *dvp;
5018
5019 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5020 ("softdep_setup_mkdir called on non-softdep filesystem"));
5021 dvp = ITOV(dp);
5022 dotaddref = dotdotaddref = NULL;
5023 if (DOINGSUJ(dvp)) {
5024 dotaddref = newjaddref(ip, ip->i_number, DOT_OFFSET, 1,
5025 ip->i_mode);
5026 dotaddref->ja_state |= MKDIR_BODY;
5027 dotdotaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET,
5028 dp->i_effnlink - 1, dp->i_mode);
5029 dotdotaddref->ja_state |= MKDIR_PARENT;
5030 }
5031 ACQUIRE_LOCK(ITOUMP(dp));
5032 inodedep = inodedep_lookup_ip(ip);
5033 if (DOINGSUJ(dvp)) {
5034 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
5035 inoreflst);
5036 KASSERT(jaddref != NULL,
5037 ("softdep_setup_mkdir: No addref structure present."));
5038 KASSERT(jaddref->ja_parent == dp->i_number,
5039 ("softdep_setup_mkdir: bad parent %ju",
5040 (uintmax_t)jaddref->ja_parent));
5041 TAILQ_INSERT_BEFORE(&jaddref->ja_ref, &dotaddref->ja_ref,
5042 if_deps);
5043 }
5044 inodedep = inodedep_lookup_ip(dp);
5045 if (DOINGSUJ(dvp))
5046 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst,
5047 &dotdotaddref->ja_ref, if_deps);
5048 FREE_LOCK(ITOUMP(dp));
5049 }
5050
5051 /*
5052 * Called to track nlinkdelta of the inode and parent directories prior to
5053 * unlinking a directory.
5054 */
5055 void
softdep_setup_rmdir(struct inode * dp,struct inode * ip)5056 softdep_setup_rmdir(struct inode *dp, struct inode *ip)
5057 {
5058
5059 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5060 ("softdep_setup_rmdir called on non-softdep filesystem"));
5061 ACQUIRE_LOCK(ITOUMP(dp));
5062 (void) inodedep_lookup_ip(ip);
5063 (void) inodedep_lookup_ip(dp);
5064 FREE_LOCK(ITOUMP(dp));
5065 }
5066
5067 /*
5068 * Called to track nlinkdelta of the inode and parent directories prior to
5069 * unlink.
5070 */
5071 void
softdep_setup_unlink(struct inode * dp,struct inode * ip)5072 softdep_setup_unlink(struct inode *dp, struct inode *ip)
5073 {
5074
5075 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5076 ("softdep_setup_unlink called on non-softdep filesystem"));
5077 ACQUIRE_LOCK(ITOUMP(dp));
5078 (void) inodedep_lookup_ip(ip);
5079 (void) inodedep_lookup_ip(dp);
5080 FREE_LOCK(ITOUMP(dp));
5081 }
5082
5083 /*
5084 * Called to release the journal structures created by a failed non-directory
5085 * creation. Adjusts nlinkdelta for non-journaling softdep.
5086 */
5087 void
softdep_revert_create(struct inode * dp,struct inode * ip)5088 softdep_revert_create(struct inode *dp, struct inode *ip)
5089 {
5090 struct inodedep *inodedep;
5091 struct jaddref *jaddref;
5092 struct vnode *dvp;
5093
5094 KASSERT(MOUNTEDSOFTDEP(ITOVFS((dp))) != 0,
5095 ("softdep_revert_create called on non-softdep filesystem"));
5096 dvp = ITOV(dp);
5097 ACQUIRE_LOCK(ITOUMP(dp));
5098 inodedep = inodedep_lookup_ip(ip);
5099 if (DOINGSUJ(dvp)) {
5100 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
5101 inoreflst);
5102 KASSERT(jaddref->ja_parent == dp->i_number,
5103 ("softdep_revert_create: addref parent mismatch"));
5104 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait);
5105 }
5106 FREE_LOCK(ITOUMP(dp));
5107 }
5108
5109 /*
5110 * Called to release the journal structures created by a failed link
5111 * addition. Adjusts nlinkdelta for non-journaling softdep.
5112 */
5113 void
softdep_revert_link(struct inode * dp,struct inode * ip)5114 softdep_revert_link(struct inode *dp, struct inode *ip)
5115 {
5116 struct inodedep *inodedep;
5117 struct jaddref *jaddref;
5118 struct vnode *dvp;
5119
5120 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5121 ("softdep_revert_link called on non-softdep filesystem"));
5122 dvp = ITOV(dp);
5123 ACQUIRE_LOCK(ITOUMP(dp));
5124 inodedep = inodedep_lookup_ip(ip);
5125 if (DOINGSUJ(dvp)) {
5126 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
5127 inoreflst);
5128 KASSERT(jaddref->ja_parent == dp->i_number,
5129 ("softdep_revert_link: addref parent mismatch"));
5130 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait);
5131 }
5132 FREE_LOCK(ITOUMP(dp));
5133 }
5134
5135 /*
5136 * Called to release the journal structures created by a failed mkdir
5137 * attempt. Adjusts nlinkdelta for non-journaling softdep.
5138 */
5139 void
softdep_revert_mkdir(struct inode * dp,struct inode * ip)5140 softdep_revert_mkdir(struct inode *dp, struct inode *ip)
5141 {
5142 struct inodedep *inodedep;
5143 struct jaddref *jaddref;
5144 struct jaddref *dotaddref;
5145 struct vnode *dvp;
5146
5147 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5148 ("softdep_revert_mkdir called on non-softdep filesystem"));
5149 dvp = ITOV(dp);
5150
5151 ACQUIRE_LOCK(ITOUMP(dp));
5152 inodedep = inodedep_lookup_ip(dp);
5153 if (DOINGSUJ(dvp)) {
5154 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
5155 inoreflst);
5156 KASSERT(jaddref->ja_parent == ip->i_number,
5157 ("softdep_revert_mkdir: dotdot addref parent mismatch"));
5158 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait);
5159 }
5160 inodedep = inodedep_lookup_ip(ip);
5161 if (DOINGSUJ(dvp)) {
5162 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
5163 inoreflst);
5164 KASSERT(jaddref->ja_parent == dp->i_number,
5165 ("softdep_revert_mkdir: addref parent mismatch"));
5166 dotaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref,
5167 inoreflst, if_deps);
5168 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait);
5169 KASSERT(dotaddref->ja_parent == ip->i_number,
5170 ("softdep_revert_mkdir: dot addref parent mismatch"));
5171 cancel_jaddref(dotaddref, inodedep, &inodedep->id_inowait);
5172 }
5173 FREE_LOCK(ITOUMP(dp));
5174 }
5175
5176 /*
5177 * Called to correct nlinkdelta after a failed rmdir.
5178 */
5179 void
softdep_revert_rmdir(struct inode * dp,struct inode * ip)5180 softdep_revert_rmdir(struct inode *dp, struct inode *ip)
5181 {
5182
5183 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5184 ("softdep_revert_rmdir called on non-softdep filesystem"));
5185 ACQUIRE_LOCK(ITOUMP(dp));
5186 (void) inodedep_lookup_ip(ip);
5187 (void) inodedep_lookup_ip(dp);
5188 FREE_LOCK(ITOUMP(dp));
5189 }
5190
5191 /*
5192 * Protecting the freemaps (or bitmaps).
5193 *
5194 * To eliminate the need to execute fsck before mounting a filesystem
5195 * after a power failure, one must (conservatively) guarantee that the
5196 * on-disk copy of the bitmaps never indicate that a live inode or block is
5197 * free. So, when a block or inode is allocated, the bitmap should be
5198 * updated (on disk) before any new pointers. When a block or inode is
5199 * freed, the bitmap should not be updated until all pointers have been
5200 * reset. The latter dependency is handled by the delayed de-allocation
5201 * approach described below for block and inode de-allocation. The former
5202 * dependency is handled by calling the following procedure when a block or
5203 * inode is allocated. When an inode is allocated an "inodedep" is created
5204 * with its DEPCOMPLETE flag cleared until its bitmap is written to disk.
5205 * Each "inodedep" is also inserted into the hash indexing structure so
5206 * that any additional link additions can be made dependent on the inode
5207 * allocation.
5208 *
5209 * The ufs filesystem maintains a number of free block counts (e.g., per
5210 * cylinder group, per cylinder and per <cylinder, rotational position> pair)
5211 * in addition to the bitmaps. These counts are used to improve efficiency
5212 * during allocation and therefore must be consistent with the bitmaps.
5213 * There is no convenient way to guarantee post-crash consistency of these
5214 * counts with simple update ordering, for two main reasons: (1) The counts
5215 * and bitmaps for a single cylinder group block are not in the same disk
5216 * sector. If a disk write is interrupted (e.g., by power failure), one may
5217 * be written and the other not. (2) Some of the counts are located in the
5218 * superblock rather than the cylinder group block. So, we focus our soft
5219 * updates implementation on protecting the bitmaps. When mounting a
5220 * filesystem, we recompute the auxiliary counts from the bitmaps.
5221 */
5222
5223 /*
5224 * Called just after updating the cylinder group block to allocate an inode.
5225 */
5226 void
softdep_setup_inomapdep(struct buf * bp,struct inode * ip,ino_t newinum,int mode)5227 softdep_setup_inomapdep(
5228 struct buf *bp, /* buffer for cylgroup block with inode map */
5229 struct inode *ip, /* inode related to allocation */
5230 ino_t newinum, /* new inode number being allocated */
5231 int mode)
5232 {
5233 struct inodedep *inodedep;
5234 struct bmsafemap *bmsafemap;
5235 struct jaddref *jaddref;
5236 struct mount *mp;
5237 struct fs *fs;
5238
5239 mp = ITOVFS(ip);
5240 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5241 ("softdep_setup_inomapdep called on non-softdep filesystem"));
5242 fs = VFSTOUFS(mp)->um_fs;
5243 jaddref = NULL;
5244
5245 /*
5246 * Allocate the journal reference add structure so that the bitmap
5247 * can be dependent on it.
5248 */
5249 if (MOUNTEDSUJ(mp)) {
5250 jaddref = newjaddref(ip, newinum, 0, 0, mode);
5251 jaddref->ja_state |= NEWBLOCK;
5252 }
5253
5254 /*
5255 * Create a dependency for the newly allocated inode.
5256 * Panic if it already exists as something is seriously wrong.
5257 * Otherwise add it to the dependency list for the buffer holding
5258 * the cylinder group map from which it was allocated.
5259 *
5260 * We have to preallocate a bmsafemap entry in case it is needed
5261 * in bmsafemap_lookup since once we allocate the inodedep, we
5262 * have to finish initializing it before we can FREE_LOCK().
5263 * By preallocating, we avoid FREE_LOCK() while doing a malloc
5264 * in bmsafemap_lookup. We cannot call bmsafemap_lookup before
5265 * creating the inodedep as it can be freed during the time
5266 * that we FREE_LOCK() while allocating the inodedep. We must
5267 * call workitem_alloc() before entering the locked section as
5268 * it also acquires the lock and we must avoid trying doing so
5269 * recursively.
5270 */
5271 bmsafemap = malloc(sizeof(struct bmsafemap),
5272 M_BMSAFEMAP, M_SOFTDEP_FLAGS);
5273 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp);
5274 ACQUIRE_LOCK(ITOUMP(ip));
5275 if ((inodedep_lookup(mp, newinum, DEPALLOC, &inodedep)))
5276 panic("softdep_setup_inomapdep: dependency %p for new"
5277 "inode already exists", inodedep);
5278 bmsafemap = bmsafemap_lookup(mp, bp, ino_to_cg(fs, newinum), bmsafemap);
5279 if (jaddref) {
5280 LIST_INSERT_HEAD(&bmsafemap->sm_jaddrefhd, jaddref, ja_bmdeps);
5281 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref,
5282 if_deps);
5283 } else {
5284 inodedep->id_state |= ONDEPLIST;
5285 LIST_INSERT_HEAD(&bmsafemap->sm_inodedephd, inodedep, id_deps);
5286 }
5287 inodedep->id_bmsafemap = bmsafemap;
5288 inodedep->id_state &= ~DEPCOMPLETE;
5289 FREE_LOCK(ITOUMP(ip));
5290 }
5291
5292 /*
5293 * Called just after updating the cylinder group block to
5294 * allocate block or fragment.
5295 */
5296 void
softdep_setup_blkmapdep(struct buf * bp,struct mount * mp,ufs2_daddr_t newblkno,int frags,int oldfrags)5297 softdep_setup_blkmapdep(
5298 struct buf *bp, /* buffer for cylgroup block with block map */
5299 struct mount *mp, /* filesystem doing allocation */
5300 ufs2_daddr_t newblkno, /* number of newly allocated block */
5301 int frags, /* Number of fragments. */
5302 int oldfrags) /* Previous number of fragments for extend. */
5303 {
5304 struct newblk *newblk;
5305 struct bmsafemap *bmsafemap;
5306 struct jnewblk *jnewblk;
5307 struct ufsmount *ump;
5308 struct fs *fs;
5309
5310 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5311 ("softdep_setup_blkmapdep called on non-softdep filesystem"));
5312 ump = VFSTOUFS(mp);
5313 fs = ump->um_fs;
5314 jnewblk = NULL;
5315 /*
5316 * Create a dependency for the newly allocated block.
5317 * Add it to the dependency list for the buffer holding
5318 * the cylinder group map from which it was allocated.
5319 */
5320 if (MOUNTEDSUJ(mp)) {
5321 jnewblk = malloc(sizeof(*jnewblk), M_JNEWBLK, M_SOFTDEP_FLAGS);
5322 workitem_alloc(&jnewblk->jn_list, D_JNEWBLK, mp);
5323 jnewblk->jn_jsegdep = newjsegdep(&jnewblk->jn_list);
5324 jnewblk->jn_state = ATTACHED;
5325 jnewblk->jn_blkno = newblkno;
5326 jnewblk->jn_frags = frags;
5327 jnewblk->jn_oldfrags = oldfrags;
5328 #ifdef INVARIANTS
5329 {
5330 struct cg *cgp;
5331 uint8_t *blksfree;
5332 long bno;
5333 int i;
5334
5335 cgp = (struct cg *)bp->b_data;
5336 blksfree = cg_blksfree(cgp);
5337 bno = dtogd(fs, jnewblk->jn_blkno);
5338 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags;
5339 i++) {
5340 if (isset(blksfree, bno + i))
5341 panic("softdep_setup_blkmapdep: "
5342 "free fragment %d from %d-%d "
5343 "state 0x%X dep %p", i,
5344 jnewblk->jn_oldfrags,
5345 jnewblk->jn_frags,
5346 jnewblk->jn_state,
5347 jnewblk->jn_dep);
5348 }
5349 }
5350 #endif
5351 }
5352
5353 CTR3(KTR_SUJ,
5354 "softdep_setup_blkmapdep: blkno %jd frags %d oldfrags %d",
5355 newblkno, frags, oldfrags);
5356 ACQUIRE_LOCK(ump);
5357 if (newblk_lookup(mp, newblkno, DEPALLOC, &newblk) != 0)
5358 panic("softdep_setup_blkmapdep: found block");
5359 newblk->nb_bmsafemap = bmsafemap = bmsafemap_lookup(mp, bp,
5360 dtog(fs, newblkno), NULL);
5361 if (jnewblk) {
5362 jnewblk->jn_dep = (struct worklist *)newblk;
5363 LIST_INSERT_HEAD(&bmsafemap->sm_jnewblkhd, jnewblk, jn_deps);
5364 } else {
5365 newblk->nb_state |= ONDEPLIST;
5366 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, nb_deps);
5367 }
5368 newblk->nb_bmsafemap = bmsafemap;
5369 newblk->nb_jnewblk = jnewblk;
5370 FREE_LOCK(ump);
5371 }
5372
5373 #define BMSAFEMAP_HASH(ump, cg) \
5374 (&(ump)->bmsafemap_hashtbl[(cg) & (ump)->bmsafemap_hash_size])
5375
5376 static int
bmsafemap_find(struct bmsafemap_hashhead * bmsafemaphd,int cg,struct bmsafemap ** bmsafemapp)5377 bmsafemap_find(
5378 struct bmsafemap_hashhead *bmsafemaphd,
5379 int cg,
5380 struct bmsafemap **bmsafemapp)
5381 {
5382 struct bmsafemap *bmsafemap;
5383
5384 LIST_FOREACH(bmsafemap, bmsafemaphd, sm_hash)
5385 if (bmsafemap->sm_cg == cg)
5386 break;
5387 if (bmsafemap) {
5388 *bmsafemapp = bmsafemap;
5389 return (1);
5390 }
5391 *bmsafemapp = NULL;
5392
5393 return (0);
5394 }
5395
5396 /*
5397 * Find the bmsafemap associated with a cylinder group buffer.
5398 * If none exists, create one. The buffer must be locked when
5399 * this routine is called and this routine must be called with
5400 * the softdep lock held. To avoid giving up the lock while
5401 * allocating a new bmsafemap, a preallocated bmsafemap may be
5402 * provided. If it is provided but not needed, it is freed.
5403 */
5404 static struct bmsafemap *
bmsafemap_lookup(struct mount * mp,struct buf * bp,int cg,struct bmsafemap * newbmsafemap)5405 bmsafemap_lookup(struct mount *mp,
5406 struct buf *bp,
5407 int cg,
5408 struct bmsafemap *newbmsafemap)
5409 {
5410 struct bmsafemap_hashhead *bmsafemaphd;
5411 struct bmsafemap *bmsafemap, *collision;
5412 struct worklist *wk;
5413 struct ufsmount *ump;
5414
5415 ump = VFSTOUFS(mp);
5416 LOCK_OWNED(ump);
5417 KASSERT(bp != NULL, ("bmsafemap_lookup: missing buffer"));
5418 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
5419 if (wk->wk_type == D_BMSAFEMAP) {
5420 if (newbmsafemap)
5421 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP);
5422 return (WK_BMSAFEMAP(wk));
5423 }
5424 }
5425 bmsafemaphd = BMSAFEMAP_HASH(ump, cg);
5426 if (bmsafemap_find(bmsafemaphd, cg, &bmsafemap) == 1) {
5427 if (newbmsafemap)
5428 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP);
5429 return (bmsafemap);
5430 }
5431 if (newbmsafemap) {
5432 bmsafemap = newbmsafemap;
5433 } else {
5434 FREE_LOCK(ump);
5435 bmsafemap = malloc(sizeof(struct bmsafemap),
5436 M_BMSAFEMAP, M_SOFTDEP_FLAGS);
5437 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp);
5438 ACQUIRE_LOCK(ump);
5439 }
5440 bmsafemap->sm_buf = bp;
5441 LIST_INIT(&bmsafemap->sm_inodedephd);
5442 LIST_INIT(&bmsafemap->sm_inodedepwr);
5443 LIST_INIT(&bmsafemap->sm_newblkhd);
5444 LIST_INIT(&bmsafemap->sm_newblkwr);
5445 LIST_INIT(&bmsafemap->sm_jaddrefhd);
5446 LIST_INIT(&bmsafemap->sm_jnewblkhd);
5447 LIST_INIT(&bmsafemap->sm_freehd);
5448 LIST_INIT(&bmsafemap->sm_freewr);
5449 if (bmsafemap_find(bmsafemaphd, cg, &collision) == 1) {
5450 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP);
5451 return (collision);
5452 }
5453 bmsafemap->sm_cg = cg;
5454 LIST_INSERT_HEAD(bmsafemaphd, bmsafemap, sm_hash);
5455 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next);
5456 WORKLIST_INSERT(&bp->b_dep, &bmsafemap->sm_list);
5457 return (bmsafemap);
5458 }
5459
5460 /*
5461 * Direct block allocation dependencies.
5462 *
5463 * When a new block is allocated, the corresponding disk locations must be
5464 * initialized (with zeros or new data) before the on-disk inode points to
5465 * them. Also, the freemap from which the block was allocated must be
5466 * updated (on disk) before the inode's pointer. These two dependencies are
5467 * independent of each other and are needed for all file blocks and indirect
5468 * blocks that are pointed to directly by the inode. Just before the
5469 * "in-core" version of the inode is updated with a newly allocated block
5470 * number, a procedure (below) is called to setup allocation dependency
5471 * structures. These structures are removed when the corresponding
5472 * dependencies are satisfied or when the block allocation becomes obsolete
5473 * (i.e., the file is deleted, the block is de-allocated, or the block is a
5474 * fragment that gets upgraded). All of these cases are handled in
5475 * procedures described later.
5476 *
5477 * When a file extension causes a fragment to be upgraded, either to a larger
5478 * fragment or to a full block, the on-disk location may change (if the
5479 * previous fragment could not simply be extended). In this case, the old
5480 * fragment must be de-allocated, but not until after the inode's pointer has
5481 * been updated. In most cases, this is handled by later procedures, which
5482 * will construct a "freefrag" structure to be added to the workitem queue
5483 * when the inode update is complete (or obsolete). The main exception to
5484 * this is when an allocation occurs while a pending allocation dependency
5485 * (for the same block pointer) remains. This case is handled in the main
5486 * allocation dependency setup procedure by immediately freeing the
5487 * unreferenced fragments.
5488 */
5489 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)5490 softdep_setup_allocdirect(
5491 struct inode *ip, /* inode to which block is being added */
5492 ufs_lbn_t off, /* block pointer within inode */
5493 ufs2_daddr_t newblkno, /* disk block number being added */
5494 ufs2_daddr_t oldblkno, /* previous block number, 0 unless frag */
5495 long newsize, /* size of new block */
5496 long oldsize, /* size of new block */
5497 struct buf *bp) /* bp for allocated block */
5498 {
5499 struct allocdirect *adp, *oldadp;
5500 struct allocdirectlst *adphead;
5501 struct freefrag *freefrag;
5502 struct inodedep *inodedep;
5503 struct pagedep *pagedep;
5504 struct jnewblk *jnewblk;
5505 struct newblk *newblk;
5506 struct mount *mp;
5507 ufs_lbn_t lbn;
5508
5509 lbn = bp->b_lblkno;
5510 mp = ITOVFS(ip);
5511 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5512 ("softdep_setup_allocdirect called on non-softdep filesystem"));
5513 if (oldblkno && oldblkno != newblkno)
5514 /*
5515 * The usual case is that a smaller fragment that
5516 * was just allocated has been replaced with a bigger
5517 * fragment or a full-size block. If it is marked as
5518 * B_DELWRI, the current contents have not been written
5519 * to disk. It is possible that the block was written
5520 * earlier, but very uncommon. If the block has never
5521 * been written, there is no need to send a BIO_DELETE
5522 * for it when it is freed. The gain from avoiding the
5523 * TRIMs for the common case of unwritten blocks far
5524 * exceeds the cost of the write amplification for the
5525 * uncommon case of failing to send a TRIM for a block
5526 * that had been written.
5527 */
5528 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn,
5529 (bp->b_flags & B_DELWRI) != 0 ? NOTRIM_KEY : SINGLETON_KEY);
5530 else
5531 freefrag = NULL;
5532
5533 CTR6(KTR_SUJ,
5534 "softdep_setup_allocdirect: ino %d blkno %jd oldblkno %jd "
5535 "off %jd newsize %ld oldsize %d",
5536 ip->i_number, newblkno, oldblkno, off, newsize, oldsize);
5537 ACQUIRE_LOCK(ITOUMP(ip));
5538 if (off >= UFS_NDADDR) {
5539 if (lbn > 0)
5540 panic("softdep_setup_allocdirect: bad lbn %jd, off %jd",
5541 lbn, off);
5542 /* allocating an indirect block */
5543 if (oldblkno != 0)
5544 panic("softdep_setup_allocdirect: non-zero indir");
5545 } else {
5546 if (off != lbn)
5547 panic("softdep_setup_allocdirect: lbn %jd != off %jd",
5548 lbn, off);
5549 /*
5550 * Allocating a direct block.
5551 *
5552 * If we are allocating a directory block, then we must
5553 * allocate an associated pagedep to track additions and
5554 * deletions.
5555 */
5556 if ((ip->i_mode & IFMT) == IFDIR)
5557 pagedep_lookup(mp, bp, ip->i_number, off, DEPALLOC,
5558 &pagedep);
5559 }
5560 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0)
5561 panic("softdep_setup_allocdirect: lost block");
5562 KASSERT(newblk->nb_list.wk_type == D_NEWBLK,
5563 ("softdep_setup_allocdirect: newblk already initialized"));
5564 /*
5565 * Convert the newblk to an allocdirect.
5566 */
5567 WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT);
5568 adp = (struct allocdirect *)newblk;
5569 newblk->nb_freefrag = freefrag;
5570 adp->ad_offset = off;
5571 adp->ad_oldblkno = oldblkno;
5572 adp->ad_newsize = newsize;
5573 adp->ad_oldsize = oldsize;
5574
5575 /*
5576 * Finish initializing the journal.
5577 */
5578 if ((jnewblk = newblk->nb_jnewblk) != NULL) {
5579 jnewblk->jn_ino = ip->i_number;
5580 jnewblk->jn_lbn = lbn;
5581 add_to_journal(&jnewblk->jn_list);
5582 }
5583 if (freefrag && freefrag->ff_jdep != NULL &&
5584 freefrag->ff_jdep->wk_type == D_JFREEFRAG)
5585 add_to_journal(freefrag->ff_jdep);
5586 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
5587 adp->ad_inodedep = inodedep;
5588
5589 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list);
5590 /*
5591 * The list of allocdirects must be kept in sorted and ascending
5592 * order so that the rollback routines can quickly determine the
5593 * first uncommitted block (the size of the file stored on disk
5594 * ends at the end of the lowest committed fragment, or if there
5595 * are no fragments, at the end of the highest committed block).
5596 * Since files generally grow, the typical case is that the new
5597 * block is to be added at the end of the list. We speed this
5598 * special case by checking against the last allocdirect in the
5599 * list before laboriously traversing the list looking for the
5600 * insertion point.
5601 */
5602 adphead = &inodedep->id_newinoupdt;
5603 oldadp = TAILQ_LAST(adphead, allocdirectlst);
5604 if (oldadp == NULL || oldadp->ad_offset <= off) {
5605 /* insert at end of list */
5606 TAILQ_INSERT_TAIL(adphead, adp, ad_next);
5607 if (oldadp != NULL && oldadp->ad_offset == off)
5608 allocdirect_merge(adphead, adp, oldadp);
5609 FREE_LOCK(ITOUMP(ip));
5610 return;
5611 }
5612 TAILQ_FOREACH(oldadp, adphead, ad_next) {
5613 if (oldadp->ad_offset >= off)
5614 break;
5615 }
5616 if (oldadp == NULL)
5617 panic("softdep_setup_allocdirect: lost entry");
5618 /* insert in middle of list */
5619 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next);
5620 if (oldadp->ad_offset == off)
5621 allocdirect_merge(adphead, adp, oldadp);
5622
5623 FREE_LOCK(ITOUMP(ip));
5624 }
5625
5626 /*
5627 * Merge a newer and older journal record to be stored either in a
5628 * newblock or freefrag. This handles aggregating journal records for
5629 * fragment allocation into a second record as well as replacing a
5630 * journal free with an aborted journal allocation. A segment for the
5631 * oldest record will be placed on wkhd if it has been written. If not
5632 * the segment for the newer record will suffice.
5633 */
5634 static struct worklist *
jnewblk_merge(struct worklist * new,struct worklist * old,struct workhead * wkhd)5635 jnewblk_merge(struct worklist *new,
5636 struct worklist *old,
5637 struct workhead *wkhd)
5638 {
5639 struct jnewblk *njnewblk;
5640 struct jnewblk *jnewblk;
5641
5642 /* Handle NULLs to simplify callers. */
5643 if (new == NULL)
5644 return (old);
5645 if (old == NULL)
5646 return (new);
5647 /* Replace a jfreefrag with a jnewblk. */
5648 if (new->wk_type == D_JFREEFRAG) {
5649 if (WK_JNEWBLK(old)->jn_blkno != WK_JFREEFRAG(new)->fr_blkno)
5650 panic("jnewblk_merge: blkno mismatch: %p, %p",
5651 old, new);
5652 cancel_jfreefrag(WK_JFREEFRAG(new));
5653 return (old);
5654 }
5655 if (old->wk_type != D_JNEWBLK || new->wk_type != D_JNEWBLK)
5656 panic("jnewblk_merge: Bad type: old %d new %d\n",
5657 old->wk_type, new->wk_type);
5658 /*
5659 * Handle merging of two jnewblk records that describe
5660 * different sets of fragments in the same block.
5661 */
5662 jnewblk = WK_JNEWBLK(old);
5663 njnewblk = WK_JNEWBLK(new);
5664 if (jnewblk->jn_blkno != njnewblk->jn_blkno)
5665 panic("jnewblk_merge: Merging disparate blocks.");
5666 /*
5667 * The record may be rolled back in the cg.
5668 */
5669 if (jnewblk->jn_state & UNDONE) {
5670 jnewblk->jn_state &= ~UNDONE;
5671 njnewblk->jn_state |= UNDONE;
5672 njnewblk->jn_state &= ~ATTACHED;
5673 }
5674 /*
5675 * We modify the newer addref and free the older so that if neither
5676 * has been written the most up-to-date copy will be on disk. If
5677 * both have been written but rolled back we only temporarily need
5678 * one of them to fix the bits when the cg write completes.
5679 */
5680 jnewblk->jn_state |= ATTACHED | COMPLETE;
5681 njnewblk->jn_oldfrags = jnewblk->jn_oldfrags;
5682 cancel_jnewblk(jnewblk, wkhd);
5683 WORKLIST_REMOVE(&jnewblk->jn_list);
5684 free_jnewblk(jnewblk);
5685 return (new);
5686 }
5687
5688 /*
5689 * Replace an old allocdirect dependency with a newer one.
5690 */
5691 static void
allocdirect_merge(struct allocdirectlst * adphead,struct allocdirect * newadp,struct allocdirect * oldadp)5692 allocdirect_merge(
5693 struct allocdirectlst *adphead, /* head of list holding allocdirects */
5694 struct allocdirect *newadp, /* allocdirect being added */
5695 struct allocdirect *oldadp) /* existing allocdirect being checked */
5696 {
5697 struct worklist *wk;
5698 struct freefrag *freefrag;
5699
5700 freefrag = NULL;
5701 LOCK_OWNED(VFSTOUFS(newadp->ad_list.wk_mp));
5702 if (newadp->ad_oldblkno != oldadp->ad_newblkno ||
5703 newadp->ad_oldsize != oldadp->ad_newsize ||
5704 newadp->ad_offset >= UFS_NDADDR)
5705 panic("%s %jd != new %jd || old size %ld != new %ld",
5706 "allocdirect_merge: old blkno",
5707 (intmax_t)newadp->ad_oldblkno,
5708 (intmax_t)oldadp->ad_newblkno,
5709 newadp->ad_oldsize, oldadp->ad_newsize);
5710 newadp->ad_oldblkno = oldadp->ad_oldblkno;
5711 newadp->ad_oldsize = oldadp->ad_oldsize;
5712 /*
5713 * If the old dependency had a fragment to free or had never
5714 * previously had a block allocated, then the new dependency
5715 * can immediately post its freefrag and adopt the old freefrag.
5716 * This action is done by swapping the freefrag dependencies.
5717 * The new dependency gains the old one's freefrag, and the
5718 * old one gets the new one and then immediately puts it on
5719 * the worklist when it is freed by free_newblk. It is
5720 * not possible to do this swap when the old dependency had a
5721 * non-zero size but no previous fragment to free. This condition
5722 * arises when the new block is an extension of the old block.
5723 * Here, the first part of the fragment allocated to the new
5724 * dependency is part of the block currently claimed on disk by
5725 * the old dependency, so cannot legitimately be freed until the
5726 * conditions for the new dependency are fulfilled.
5727 */
5728 freefrag = newadp->ad_freefrag;
5729 if (oldadp->ad_freefrag != NULL || oldadp->ad_oldblkno == 0) {
5730 newadp->ad_freefrag = oldadp->ad_freefrag;
5731 oldadp->ad_freefrag = freefrag;
5732 }
5733 /*
5734 * If we are tracking a new directory-block allocation,
5735 * move it from the old allocdirect to the new allocdirect.
5736 */
5737 if ((wk = LIST_FIRST(&oldadp->ad_newdirblk)) != NULL) {
5738 WORKLIST_REMOVE(wk);
5739 if (!LIST_EMPTY(&oldadp->ad_newdirblk))
5740 panic("allocdirect_merge: extra newdirblk");
5741 WORKLIST_INSERT(&newadp->ad_newdirblk, wk);
5742 }
5743 TAILQ_REMOVE(adphead, oldadp, ad_next);
5744 /*
5745 * We need to move any journal dependencies over to the freefrag
5746 * that releases this block if it exists. Otherwise we are
5747 * extending an existing block and we'll wait until that is
5748 * complete to release the journal space and extend the
5749 * new journal to cover this old space as well.
5750 */
5751 if (freefrag == NULL) {
5752 if (oldadp->ad_newblkno != newadp->ad_newblkno)
5753 panic("allocdirect_merge: %jd != %jd",
5754 oldadp->ad_newblkno, newadp->ad_newblkno);
5755 newadp->ad_block.nb_jnewblk = (struct jnewblk *)
5756 jnewblk_merge(&newadp->ad_block.nb_jnewblk->jn_list,
5757 &oldadp->ad_block.nb_jnewblk->jn_list,
5758 &newadp->ad_block.nb_jwork);
5759 oldadp->ad_block.nb_jnewblk = NULL;
5760 cancel_newblk(&oldadp->ad_block, NULL,
5761 &newadp->ad_block.nb_jwork);
5762 } else {
5763 wk = (struct worklist *) cancel_newblk(&oldadp->ad_block,
5764 &freefrag->ff_list, &freefrag->ff_jwork);
5765 freefrag->ff_jdep = jnewblk_merge(freefrag->ff_jdep, wk,
5766 &freefrag->ff_jwork);
5767 }
5768 free_newblk(&oldadp->ad_block);
5769 }
5770
5771 /*
5772 * Allocate a jfreefrag structure to journal a single block free.
5773 */
5774 static struct jfreefrag *
newjfreefrag(struct freefrag * freefrag,struct inode * ip,ufs2_daddr_t blkno,long size,ufs_lbn_t lbn)5775 newjfreefrag(struct freefrag *freefrag,
5776 struct inode *ip,
5777 ufs2_daddr_t blkno,
5778 long size,
5779 ufs_lbn_t lbn)
5780 {
5781 struct jfreefrag *jfreefrag;
5782 struct fs *fs;
5783
5784 fs = ITOFS(ip);
5785 jfreefrag = malloc(sizeof(struct jfreefrag), M_JFREEFRAG,
5786 M_SOFTDEP_FLAGS);
5787 workitem_alloc(&jfreefrag->fr_list, D_JFREEFRAG, ITOVFS(ip));
5788 jfreefrag->fr_jsegdep = newjsegdep(&jfreefrag->fr_list);
5789 jfreefrag->fr_state = ATTACHED | DEPCOMPLETE;
5790 jfreefrag->fr_ino = ip->i_number;
5791 jfreefrag->fr_lbn = lbn;
5792 jfreefrag->fr_blkno = blkno;
5793 jfreefrag->fr_frags = numfrags(fs, size);
5794 jfreefrag->fr_freefrag = freefrag;
5795
5796 return (jfreefrag);
5797 }
5798
5799 /*
5800 * Allocate a new freefrag structure.
5801 */
5802 static struct freefrag *
newfreefrag(struct inode * ip,ufs2_daddr_t blkno,long size,ufs_lbn_t lbn,uint64_t key)5803 newfreefrag(struct inode *ip,
5804 ufs2_daddr_t blkno,
5805 long size,
5806 ufs_lbn_t lbn,
5807 uint64_t key)
5808 {
5809 struct freefrag *freefrag;
5810 struct ufsmount *ump;
5811 struct fs *fs;
5812
5813 CTR4(KTR_SUJ, "newfreefrag: ino %d blkno %jd size %ld lbn %jd",
5814 ip->i_number, blkno, size, lbn);
5815 ump = ITOUMP(ip);
5816 fs = ump->um_fs;
5817 if (fragnum(fs, blkno) + numfrags(fs, size) > fs->fs_frag)
5818 panic("newfreefrag: frag size");
5819 freefrag = malloc(sizeof(struct freefrag),
5820 M_FREEFRAG, M_SOFTDEP_FLAGS);
5821 workitem_alloc(&freefrag->ff_list, D_FREEFRAG, UFSTOVFS(ump));
5822 freefrag->ff_state = ATTACHED;
5823 LIST_INIT(&freefrag->ff_jwork);
5824 freefrag->ff_inum = ip->i_number;
5825 freefrag->ff_vtype = ITOV(ip)->v_type;
5826 freefrag->ff_blkno = blkno;
5827 freefrag->ff_fragsize = size;
5828 freefrag->ff_key = key;
5829
5830 if (MOUNTEDSUJ(UFSTOVFS(ump))) {
5831 freefrag->ff_jdep = (struct worklist *)
5832 newjfreefrag(freefrag, ip, blkno, size, lbn);
5833 } else {
5834 freefrag->ff_state |= DEPCOMPLETE;
5835 freefrag->ff_jdep = NULL;
5836 }
5837
5838 return (freefrag);
5839 }
5840
5841 /*
5842 * This workitem de-allocates fragments that were replaced during
5843 * file block allocation.
5844 */
5845 static void
handle_workitem_freefrag(struct freefrag * freefrag)5846 handle_workitem_freefrag(struct freefrag *freefrag)
5847 {
5848 struct ufsmount *ump = VFSTOUFS(freefrag->ff_list.wk_mp);
5849 struct workhead wkhd;
5850
5851 CTR3(KTR_SUJ,
5852 "handle_workitem_freefrag: ino %d blkno %jd size %ld",
5853 freefrag->ff_inum, freefrag->ff_blkno, freefrag->ff_fragsize);
5854 /*
5855 * It would be illegal to add new completion items to the
5856 * freefrag after it was schedule to be done so it must be
5857 * safe to modify the list head here.
5858 */
5859 LIST_INIT(&wkhd);
5860 ACQUIRE_LOCK(ump);
5861 LIST_SWAP(&freefrag->ff_jwork, &wkhd, worklist, wk_list);
5862 /*
5863 * If the journal has not been written we must cancel it here.
5864 */
5865 if (freefrag->ff_jdep) {
5866 if (freefrag->ff_jdep->wk_type != D_JNEWBLK)
5867 panic("handle_workitem_freefrag: Unexpected type %d\n",
5868 freefrag->ff_jdep->wk_type);
5869 cancel_jnewblk(WK_JNEWBLK(freefrag->ff_jdep), &wkhd);
5870 }
5871 FREE_LOCK(ump);
5872 ffs_blkfree(ump, ump->um_fs, ump->um_devvp, freefrag->ff_blkno,
5873 freefrag->ff_fragsize, freefrag->ff_inum, freefrag->ff_vtype,
5874 &wkhd, freefrag->ff_key);
5875 ACQUIRE_LOCK(ump);
5876 WORKITEM_FREE(freefrag, D_FREEFRAG);
5877 FREE_LOCK(ump);
5878 }
5879
5880 /*
5881 * Set up a dependency structure for an external attributes data block.
5882 * This routine follows much of the structure of softdep_setup_allocdirect.
5883 * See the description of softdep_setup_allocdirect above for details.
5884 */
5885 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)5886 softdep_setup_allocext(
5887 struct inode *ip,
5888 ufs_lbn_t off,
5889 ufs2_daddr_t newblkno,
5890 ufs2_daddr_t oldblkno,
5891 long newsize,
5892 long oldsize,
5893 struct buf *bp)
5894 {
5895 struct allocdirect *adp, *oldadp;
5896 struct allocdirectlst *adphead;
5897 struct freefrag *freefrag;
5898 struct inodedep *inodedep;
5899 struct jnewblk *jnewblk;
5900 struct newblk *newblk;
5901 struct mount *mp;
5902 struct ufsmount *ump;
5903 ufs_lbn_t lbn;
5904
5905 mp = ITOVFS(ip);
5906 ump = VFSTOUFS(mp);
5907 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5908 ("softdep_setup_allocext called on non-softdep filesystem"));
5909 KASSERT(off < UFS_NXADDR,
5910 ("softdep_setup_allocext: lbn %lld > UFS_NXADDR", (long long)off));
5911
5912 lbn = bp->b_lblkno;
5913 if (oldblkno && oldblkno != newblkno)
5914 /*
5915 * The usual case is that a smaller fragment that
5916 * was just allocated has been replaced with a bigger
5917 * fragment or a full-size block. If it is marked as
5918 * B_DELWRI, the current contents have not been written
5919 * to disk. It is possible that the block was written
5920 * earlier, but very uncommon. If the block has never
5921 * been written, there is no need to send a BIO_DELETE
5922 * for it when it is freed. The gain from avoiding the
5923 * TRIMs for the common case of unwritten blocks far
5924 * exceeds the cost of the write amplification for the
5925 * uncommon case of failing to send a TRIM for a block
5926 * that had been written.
5927 */
5928 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn,
5929 (bp->b_flags & B_DELWRI) != 0 ? NOTRIM_KEY : SINGLETON_KEY);
5930 else
5931 freefrag = NULL;
5932
5933 ACQUIRE_LOCK(ump);
5934 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0)
5935 panic("softdep_setup_allocext: lost block");
5936 KASSERT(newblk->nb_list.wk_type == D_NEWBLK,
5937 ("softdep_setup_allocext: newblk already initialized"));
5938 /*
5939 * Convert the newblk to an allocdirect.
5940 */
5941 WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT);
5942 adp = (struct allocdirect *)newblk;
5943 newblk->nb_freefrag = freefrag;
5944 adp->ad_offset = off;
5945 adp->ad_oldblkno = oldblkno;
5946 adp->ad_newsize = newsize;
5947 adp->ad_oldsize = oldsize;
5948 adp->ad_state |= EXTDATA;
5949
5950 /*
5951 * Finish initializing the journal.
5952 */
5953 if ((jnewblk = newblk->nb_jnewblk) != NULL) {
5954 jnewblk->jn_ino = ip->i_number;
5955 jnewblk->jn_lbn = lbn;
5956 add_to_journal(&jnewblk->jn_list);
5957 }
5958 if (freefrag && freefrag->ff_jdep != NULL &&
5959 freefrag->ff_jdep->wk_type == D_JFREEFRAG)
5960 add_to_journal(freefrag->ff_jdep);
5961 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
5962 adp->ad_inodedep = inodedep;
5963
5964 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list);
5965 /*
5966 * The list of allocdirects must be kept in sorted and ascending
5967 * order so that the rollback routines can quickly determine the
5968 * first uncommitted block (the size of the file stored on disk
5969 * ends at the end of the lowest committed fragment, or if there
5970 * are no fragments, at the end of the highest committed block).
5971 * Since files generally grow, the typical case is that the new
5972 * block is to be added at the end of the list. We speed this
5973 * special case by checking against the last allocdirect in the
5974 * list before laboriously traversing the list looking for the
5975 * insertion point.
5976 */
5977 adphead = &inodedep->id_newextupdt;
5978 oldadp = TAILQ_LAST(adphead, allocdirectlst);
5979 if (oldadp == NULL || oldadp->ad_offset <= off) {
5980 /* insert at end of list */
5981 TAILQ_INSERT_TAIL(adphead, adp, ad_next);
5982 if (oldadp != NULL && oldadp->ad_offset == off)
5983 allocdirect_merge(adphead, adp, oldadp);
5984 FREE_LOCK(ump);
5985 return;
5986 }
5987 TAILQ_FOREACH(oldadp, adphead, ad_next) {
5988 if (oldadp->ad_offset >= off)
5989 break;
5990 }
5991 if (oldadp == NULL)
5992 panic("softdep_setup_allocext: lost entry");
5993 /* insert in middle of list */
5994 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next);
5995 if (oldadp->ad_offset == off)
5996 allocdirect_merge(adphead, adp, oldadp);
5997 FREE_LOCK(ump);
5998 }
5999
6000 /*
6001 * Indirect block allocation dependencies.
6002 *
6003 * The same dependencies that exist for a direct block also exist when
6004 * a new block is allocated and pointed to by an entry in a block of
6005 * indirect pointers. The undo/redo states described above are also
6006 * used here. Because an indirect block contains many pointers that
6007 * may have dependencies, a second copy of the entire in-memory indirect
6008 * block is kept. The buffer cache copy is always completely up-to-date.
6009 * The second copy, which is used only as a source for disk writes,
6010 * contains only the safe pointers (i.e., those that have no remaining
6011 * update dependencies). The second copy is freed when all pointers
6012 * are safe. The cache is not allowed to replace indirect blocks with
6013 * pending update dependencies. If a buffer containing an indirect
6014 * block with dependencies is written, these routines will mark it
6015 * dirty again. It can only be successfully written once all the
6016 * dependencies are removed. The ffs_fsync routine in conjunction with
6017 * softdep_sync_metadata work together to get all the dependencies
6018 * removed so that a file can be successfully written to disk. Three
6019 * procedures are used when setting up indirect block pointer
6020 * dependencies. The division is necessary because of the organization
6021 * of the "balloc" routine and because of the distinction between file
6022 * pages and file metadata blocks.
6023 */
6024
6025 /*
6026 * Allocate a new allocindir structure.
6027 */
6028 static struct allocindir *
newallocindir(struct inode * ip,int ptrno,ufs2_daddr_t newblkno,ufs2_daddr_t oldblkno,ufs_lbn_t lbn)6029 newallocindir(
6030 struct inode *ip, /* inode for file being extended */
6031 int ptrno, /* offset of pointer in indirect block */
6032 ufs2_daddr_t newblkno, /* disk block number being added */
6033 ufs2_daddr_t oldblkno, /* previous block number, 0 if none */
6034 ufs_lbn_t lbn)
6035 {
6036 struct newblk *newblk;
6037 struct allocindir *aip;
6038 struct freefrag *freefrag;
6039 struct jnewblk *jnewblk;
6040
6041 if (oldblkno)
6042 freefrag = newfreefrag(ip, oldblkno, ITOFS(ip)->fs_bsize, lbn,
6043 SINGLETON_KEY);
6044 else
6045 freefrag = NULL;
6046 ACQUIRE_LOCK(ITOUMP(ip));
6047 if (newblk_lookup(ITOVFS(ip), newblkno, 0, &newblk) == 0)
6048 panic("new_allocindir: lost block");
6049 KASSERT(newblk->nb_list.wk_type == D_NEWBLK,
6050 ("newallocindir: newblk already initialized"));
6051 WORKITEM_REASSIGN(newblk, D_ALLOCINDIR);
6052 newblk->nb_freefrag = freefrag;
6053 aip = (struct allocindir *)newblk;
6054 aip->ai_offset = ptrno;
6055 aip->ai_oldblkno = oldblkno;
6056 aip->ai_lbn = lbn;
6057 if ((jnewblk = newblk->nb_jnewblk) != NULL) {
6058 jnewblk->jn_ino = ip->i_number;
6059 jnewblk->jn_lbn = lbn;
6060 add_to_journal(&jnewblk->jn_list);
6061 }
6062 if (freefrag && freefrag->ff_jdep != NULL &&
6063 freefrag->ff_jdep->wk_type == D_JFREEFRAG)
6064 add_to_journal(freefrag->ff_jdep);
6065 return (aip);
6066 }
6067
6068 /*
6069 * Called just before setting an indirect block pointer
6070 * to a newly allocated file page.
6071 */
6072 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)6073 softdep_setup_allocindir_page(
6074 struct inode *ip, /* inode for file being extended */
6075 ufs_lbn_t lbn, /* allocated block number within file */
6076 struct buf *bp, /* buffer with indirect blk referencing page */
6077 int ptrno, /* offset of pointer in indirect block */
6078 ufs2_daddr_t newblkno, /* disk block number being added */
6079 ufs2_daddr_t oldblkno, /* previous block number, 0 if none */
6080 struct buf *nbp) /* buffer holding allocated page */
6081 {
6082 struct inodedep *inodedep;
6083 struct freefrag *freefrag;
6084 struct allocindir *aip;
6085 struct pagedep *pagedep;
6086 struct mount *mp;
6087 struct ufsmount *ump;
6088
6089 mp = ITOVFS(ip);
6090 ump = VFSTOUFS(mp);
6091 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
6092 ("softdep_setup_allocindir_page called on non-softdep filesystem"));
6093 KASSERT(lbn == nbp->b_lblkno,
6094 ("softdep_setup_allocindir_page: lbn %jd != lblkno %jd",
6095 lbn, bp->b_lblkno));
6096 CTR4(KTR_SUJ,
6097 "softdep_setup_allocindir_page: ino %d blkno %jd oldblkno %jd "
6098 "lbn %jd", ip->i_number, newblkno, oldblkno, lbn);
6099 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_page");
6100 aip = newallocindir(ip, ptrno, newblkno, oldblkno, lbn);
6101 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
6102 /*
6103 * If we are allocating a directory page, then we must
6104 * allocate an associated pagedep to track additions and
6105 * deletions.
6106 */
6107 if ((ip->i_mode & IFMT) == IFDIR)
6108 pagedep_lookup(mp, nbp, ip->i_number, lbn, DEPALLOC, &pagedep);
6109 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list);
6110 freefrag = setup_allocindir_phase2(bp, ip, inodedep, aip, lbn);
6111 FREE_LOCK(ump);
6112 if (freefrag)
6113 handle_workitem_freefrag(freefrag);
6114 }
6115
6116 /*
6117 * Called just before setting an indirect block pointer to a
6118 * newly allocated indirect block.
6119 */
6120 void
softdep_setup_allocindir_meta(struct buf * nbp,struct inode * ip,struct buf * bp,int ptrno,ufs2_daddr_t newblkno)6121 softdep_setup_allocindir_meta(
6122 struct buf *nbp, /* newly allocated indirect block */
6123 struct inode *ip, /* inode for file being extended */
6124 struct buf *bp, /* indirect block referencing allocated block */
6125 int ptrno, /* offset of pointer in indirect block */
6126 ufs2_daddr_t newblkno) /* disk block number being added */
6127 {
6128 struct inodedep *inodedep;
6129 struct allocindir *aip;
6130 struct ufsmount *ump;
6131 ufs_lbn_t lbn;
6132
6133 ump = ITOUMP(ip);
6134 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
6135 ("softdep_setup_allocindir_meta called on non-softdep filesystem"));
6136 CTR3(KTR_SUJ,
6137 "softdep_setup_allocindir_meta: ino %d blkno %jd ptrno %d",
6138 ip->i_number, newblkno, ptrno);
6139 lbn = nbp->b_lblkno;
6140 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_meta");
6141 aip = newallocindir(ip, ptrno, newblkno, 0, lbn);
6142 inodedep_lookup(UFSTOVFS(ump), ip->i_number, DEPALLOC, &inodedep);
6143 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list);
6144 if (setup_allocindir_phase2(bp, ip, inodedep, aip, lbn))
6145 panic("softdep_setup_allocindir_meta: Block already existed");
6146 FREE_LOCK(ump);
6147 }
6148
6149 static void
indirdep_complete(struct indirdep * indirdep)6150 indirdep_complete(struct indirdep *indirdep)
6151 {
6152 struct allocindir *aip;
6153
6154 LIST_REMOVE(indirdep, ir_next);
6155 indirdep->ir_state |= DEPCOMPLETE;
6156
6157 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL) {
6158 LIST_REMOVE(aip, ai_next);
6159 free_newblk(&aip->ai_block);
6160 }
6161 /*
6162 * If this indirdep is not attached to a buf it was simply waiting
6163 * on completion to clear completehd. free_indirdep() asserts
6164 * that nothing is dangling.
6165 */
6166 if ((indirdep->ir_state & ONWORKLIST) == 0)
6167 free_indirdep(indirdep);
6168 }
6169
6170 static struct indirdep *
indirdep_lookup(struct mount * mp,struct inode * ip,struct buf * bp)6171 indirdep_lookup(struct mount *mp,
6172 struct inode *ip,
6173 struct buf *bp)
6174 {
6175 struct indirdep *indirdep, *newindirdep;
6176 struct newblk *newblk;
6177 struct ufsmount *ump;
6178 struct worklist *wk;
6179 struct fs *fs;
6180 ufs2_daddr_t blkno;
6181
6182 ump = VFSTOUFS(mp);
6183 LOCK_OWNED(ump);
6184 indirdep = NULL;
6185 newindirdep = NULL;
6186 fs = ump->um_fs;
6187 for (;;) {
6188 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
6189 if (wk->wk_type != D_INDIRDEP)
6190 continue;
6191 indirdep = WK_INDIRDEP(wk);
6192 break;
6193 }
6194 /* Found on the buffer worklist, no new structure to free. */
6195 if (indirdep != NULL && newindirdep == NULL)
6196 return (indirdep);
6197 if (indirdep != NULL && newindirdep != NULL)
6198 panic("indirdep_lookup: simultaneous create");
6199 /* None found on the buffer and a new structure is ready. */
6200 if (indirdep == NULL && newindirdep != NULL)
6201 break;
6202 /* None found and no new structure available. */
6203 FREE_LOCK(ump);
6204 newindirdep = malloc(sizeof(struct indirdep),
6205 M_INDIRDEP, M_SOFTDEP_FLAGS);
6206 workitem_alloc(&newindirdep->ir_list, D_INDIRDEP, mp);
6207 newindirdep->ir_state = ATTACHED;
6208 if (I_IS_UFS1(ip))
6209 newindirdep->ir_state |= UFS1FMT;
6210 TAILQ_INIT(&newindirdep->ir_trunc);
6211 newindirdep->ir_saveddata = NULL;
6212 LIST_INIT(&newindirdep->ir_deplisthd);
6213 LIST_INIT(&newindirdep->ir_donehd);
6214 LIST_INIT(&newindirdep->ir_writehd);
6215 LIST_INIT(&newindirdep->ir_completehd);
6216 if (bp->b_blkno == bp->b_lblkno) {
6217 ufs_bmaparray(bp->b_vp, bp->b_lblkno, &blkno, bp,
6218 NULL, NULL);
6219 bp->b_blkno = blkno;
6220 }
6221 newindirdep->ir_freeblks = NULL;
6222 newindirdep->ir_savebp =
6223 getblk(ump->um_devvp, bp->b_blkno, bp->b_bcount, 0, 0, 0);
6224 newindirdep->ir_bp = bp;
6225 BUF_KERNPROC(newindirdep->ir_savebp);
6226 bcopy(bp->b_data, newindirdep->ir_savebp->b_data, bp->b_bcount);
6227 ACQUIRE_LOCK(ump);
6228 }
6229 indirdep = newindirdep;
6230 WORKLIST_INSERT(&bp->b_dep, &indirdep->ir_list);
6231 /*
6232 * If the block is not yet allocated we don't set DEPCOMPLETE so
6233 * that we don't free dependencies until the pointers are valid.
6234 * This could search b_dep for D_ALLOCDIRECT/D_ALLOCINDIR rather
6235 * than using the hash.
6236 */
6237 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk))
6238 LIST_INSERT_HEAD(&newblk->nb_indirdeps, indirdep, ir_next);
6239 else
6240 indirdep->ir_state |= DEPCOMPLETE;
6241 return (indirdep);
6242 }
6243
6244 /*
6245 * Called to finish the allocation of the "aip" allocated
6246 * by one of the two routines above.
6247 */
6248 static struct freefrag *
setup_allocindir_phase2(struct buf * bp,struct inode * ip,struct inodedep * inodedep,struct allocindir * aip,ufs_lbn_t lbn)6249 setup_allocindir_phase2(
6250 struct buf *bp, /* in-memory copy of the indirect block */
6251 struct inode *ip, /* inode for file being extended */
6252 struct inodedep *inodedep, /* Inodedep for ip */
6253 struct allocindir *aip, /* allocindir allocated by the above routines */
6254 ufs_lbn_t lbn) /* Logical block number for this block. */
6255 {
6256 struct fs *fs __diagused;
6257 struct indirdep *indirdep;
6258 struct allocindir *oldaip;
6259 struct freefrag *freefrag;
6260 struct mount *mp;
6261 struct ufsmount *ump;
6262
6263 mp = ITOVFS(ip);
6264 ump = VFSTOUFS(mp);
6265 LOCK_OWNED(ump);
6266 fs = ump->um_fs;
6267 if (bp->b_lblkno >= 0)
6268 panic("setup_allocindir_phase2: not indir blk");
6269 KASSERT(aip->ai_offset >= 0 && aip->ai_offset < NINDIR(fs),
6270 ("setup_allocindir_phase2: Bad offset %d", aip->ai_offset));
6271 indirdep = indirdep_lookup(mp, ip, bp);
6272 KASSERT(indirdep->ir_savebp != NULL,
6273 ("setup_allocindir_phase2 NULL ir_savebp"));
6274 aip->ai_indirdep = indirdep;
6275 /*
6276 * Check for an unwritten dependency for this indirect offset. If
6277 * there is, merge the old dependency into the new one. This happens
6278 * as a result of reallocblk only.
6279 */
6280 freefrag = NULL;
6281 if (aip->ai_oldblkno != 0) {
6282 LIST_FOREACH(oldaip, &indirdep->ir_deplisthd, ai_next) {
6283 if (oldaip->ai_offset == aip->ai_offset) {
6284 freefrag = allocindir_merge(aip, oldaip);
6285 goto done;
6286 }
6287 }
6288 LIST_FOREACH(oldaip, &indirdep->ir_donehd, ai_next) {
6289 if (oldaip->ai_offset == aip->ai_offset) {
6290 freefrag = allocindir_merge(aip, oldaip);
6291 goto done;
6292 }
6293 }
6294 }
6295 done:
6296 LIST_INSERT_HEAD(&indirdep->ir_deplisthd, aip, ai_next);
6297 return (freefrag);
6298 }
6299
6300 /*
6301 * Merge two allocindirs which refer to the same block. Move newblock
6302 * dependencies and setup the freefrags appropriately.
6303 */
6304 static struct freefrag *
allocindir_merge(struct allocindir * aip,struct allocindir * oldaip)6305 allocindir_merge(
6306 struct allocindir *aip,
6307 struct allocindir *oldaip)
6308 {
6309 struct freefrag *freefrag;
6310 struct worklist *wk;
6311
6312 if (oldaip->ai_newblkno != aip->ai_oldblkno)
6313 panic("allocindir_merge: blkno");
6314 aip->ai_oldblkno = oldaip->ai_oldblkno;
6315 freefrag = aip->ai_freefrag;
6316 aip->ai_freefrag = oldaip->ai_freefrag;
6317 oldaip->ai_freefrag = NULL;
6318 KASSERT(freefrag != NULL, ("setup_allocindir_phase2: No freefrag"));
6319 /*
6320 * If we are tracking a new directory-block allocation,
6321 * move it from the old allocindir to the new allocindir.
6322 */
6323 if ((wk = LIST_FIRST(&oldaip->ai_newdirblk)) != NULL) {
6324 WORKLIST_REMOVE(wk);
6325 if (!LIST_EMPTY(&oldaip->ai_newdirblk))
6326 panic("allocindir_merge: extra newdirblk");
6327 WORKLIST_INSERT(&aip->ai_newdirblk, wk);
6328 }
6329 /*
6330 * We can skip journaling for this freefrag and just complete
6331 * any pending journal work for the allocindir that is being
6332 * removed after the freefrag completes.
6333 */
6334 if (freefrag->ff_jdep)
6335 cancel_jfreefrag(WK_JFREEFRAG(freefrag->ff_jdep));
6336 LIST_REMOVE(oldaip, ai_next);
6337 freefrag->ff_jdep = (struct worklist *)cancel_newblk(&oldaip->ai_block,
6338 &freefrag->ff_list, &freefrag->ff_jwork);
6339 free_newblk(&oldaip->ai_block);
6340
6341 return (freefrag);
6342 }
6343
6344 static inline void
setup_freedirect(struct freeblks * freeblks,struct inode * ip,int i,int needj)6345 setup_freedirect(
6346 struct freeblks *freeblks,
6347 struct inode *ip,
6348 int i,
6349 int needj)
6350 {
6351 struct ufsmount *ump;
6352 ufs2_daddr_t blkno;
6353 int frags;
6354
6355 blkno = DIP(ip, i_db[i]);
6356 if (blkno == 0)
6357 return;
6358 DIP_SET(ip, i_db[i], 0);
6359 ump = ITOUMP(ip);
6360 frags = sblksize(ump->um_fs, ip->i_size, i);
6361 frags = numfrags(ump->um_fs, frags);
6362 newfreework(ump, freeblks, NULL, i, blkno, frags, 0, needj);
6363 }
6364
6365 static inline void
setup_freeext(struct freeblks * freeblks,struct inode * ip,int i,int needj)6366 setup_freeext(
6367 struct freeblks *freeblks,
6368 struct inode *ip,
6369 int i,
6370 int needj)
6371 {
6372 struct ufsmount *ump;
6373 ufs2_daddr_t blkno;
6374 int frags;
6375
6376 blkno = ip->i_din2->di_extb[i];
6377 if (blkno == 0)
6378 return;
6379 ip->i_din2->di_extb[i] = 0;
6380 ump = ITOUMP(ip);
6381 frags = sblksize(ump->um_fs, ip->i_din2->di_extsize, i);
6382 frags = numfrags(ump->um_fs, frags);
6383 newfreework(ump, freeblks, NULL, -1 - i, blkno, frags, 0, needj);
6384 }
6385
6386 static inline void
setup_freeindir(struct freeblks * freeblks,struct inode * ip,int i,ufs_lbn_t lbn,int needj)6387 setup_freeindir(
6388 struct freeblks *freeblks,
6389 struct inode *ip,
6390 int i,
6391 ufs_lbn_t lbn,
6392 int needj)
6393 {
6394 struct ufsmount *ump;
6395 ufs2_daddr_t blkno;
6396
6397 blkno = DIP(ip, i_ib[i]);
6398 if (blkno == 0)
6399 return;
6400 DIP_SET(ip, i_ib[i], 0);
6401 ump = ITOUMP(ip);
6402 newfreework(ump, freeblks, NULL, lbn, blkno, ump->um_fs->fs_frag,
6403 0, needj);
6404 }
6405
6406 static inline struct freeblks *
newfreeblks(struct mount * mp,struct inode * ip)6407 newfreeblks(struct mount *mp, struct inode *ip)
6408 {
6409 struct freeblks *freeblks;
6410
6411 freeblks = malloc(sizeof(struct freeblks),
6412 M_FREEBLKS, M_SOFTDEP_FLAGS|M_ZERO);
6413 workitem_alloc(&freeblks->fb_list, D_FREEBLKS, mp);
6414 LIST_INIT(&freeblks->fb_jblkdephd);
6415 LIST_INIT(&freeblks->fb_jwork);
6416 freeblks->fb_ref = 0;
6417 freeblks->fb_cgwait = 0;
6418 freeblks->fb_state = ATTACHED;
6419 freeblks->fb_uid = ip->i_uid;
6420 freeblks->fb_inum = ip->i_number;
6421 freeblks->fb_vtype = ITOV(ip)->v_type;
6422 freeblks->fb_modrev = DIP(ip, i_modrev);
6423 freeblks->fb_devvp = ITODEVVP(ip);
6424 freeblks->fb_chkcnt = 0;
6425 freeblks->fb_len = 0;
6426
6427 return (freeblks);
6428 }
6429
6430 static void
trunc_indirdep(struct indirdep * indirdep,struct freeblks * freeblks,struct buf * bp,int off)6431 trunc_indirdep(
6432 struct indirdep *indirdep,
6433 struct freeblks *freeblks,
6434 struct buf *bp,
6435 int off)
6436 {
6437 struct allocindir *aip, *aipn;
6438
6439 /*
6440 * The first set of allocindirs won't be in savedbp.
6441 */
6442 LIST_FOREACH_SAFE(aip, &indirdep->ir_deplisthd, ai_next, aipn)
6443 if (aip->ai_offset > off)
6444 cancel_allocindir(aip, bp, freeblks, 1);
6445 LIST_FOREACH_SAFE(aip, &indirdep->ir_donehd, ai_next, aipn)
6446 if (aip->ai_offset > off)
6447 cancel_allocindir(aip, bp, freeblks, 1);
6448 /*
6449 * These will exist in savedbp.
6450 */
6451 LIST_FOREACH_SAFE(aip, &indirdep->ir_writehd, ai_next, aipn)
6452 if (aip->ai_offset > off)
6453 cancel_allocindir(aip, NULL, freeblks, 0);
6454 LIST_FOREACH_SAFE(aip, &indirdep->ir_completehd, ai_next, aipn)
6455 if (aip->ai_offset > off)
6456 cancel_allocindir(aip, NULL, freeblks, 0);
6457 }
6458
6459 /*
6460 * Follow the chain of indirects down to lastlbn creating a freework
6461 * structure for each. This will be used to start indir_trunc() at
6462 * the right offset and create the journal records for the parrtial
6463 * truncation. A second step will handle the truncated dependencies.
6464 */
6465 static int
setup_trunc_indir(struct freeblks * freeblks,struct inode * ip,ufs_lbn_t lbn,ufs_lbn_t lastlbn,ufs2_daddr_t blkno)6466 setup_trunc_indir(
6467 struct freeblks *freeblks,
6468 struct inode *ip,
6469 ufs_lbn_t lbn,
6470 ufs_lbn_t lastlbn,
6471 ufs2_daddr_t blkno)
6472 {
6473 struct indirdep *indirdep;
6474 struct indirdep *indirn;
6475 struct freework *freework;
6476 struct newblk *newblk;
6477 struct mount *mp;
6478 struct ufsmount *ump;
6479 struct buf *bp;
6480 uint8_t *start;
6481 uint8_t *end;
6482 ufs_lbn_t lbnadd;
6483 int level;
6484 int error;
6485 int off;
6486
6487 freework = NULL;
6488 if (blkno == 0)
6489 return (0);
6490 mp = freeblks->fb_list.wk_mp;
6491 ump = VFSTOUFS(mp);
6492 /*
6493 * Here, calls to VOP_BMAP() will fail. However, we already have
6494 * the on-disk address, so we just pass it to bread() instead of
6495 * having bread() attempt to calculate it using VOP_BMAP().
6496 */
6497 error = ffs_breadz(ump, ITOV(ip), lbn, blkptrtodb(ump, blkno),
6498 (int)mp->mnt_stat.f_iosize, NULL, NULL, 0, NOCRED, 0, NULL, &bp);
6499 if (error)
6500 return (error);
6501 level = lbn_level(lbn);
6502 lbnadd = lbn_offset(ump->um_fs, level);
6503 /*
6504 * Compute the offset of the last block we want to keep. Store
6505 * in the freework the first block we want to completely free.
6506 */
6507 off = (lastlbn - -(lbn + level)) / lbnadd;
6508 if (off + 1 == NINDIR(ump->um_fs))
6509 goto nowork;
6510 freework = newfreework(ump, freeblks, NULL, lbn, blkno, 0, off + 1, 0);
6511 /*
6512 * Link the freework into the indirdep. This will prevent any new
6513 * allocations from proceeding until we are finished with the
6514 * truncate and the block is written.
6515 */
6516 ACQUIRE_LOCK(ump);
6517 indirdep = indirdep_lookup(mp, ip, bp);
6518 if (indirdep->ir_freeblks)
6519 panic("setup_trunc_indir: indirdep already truncated.");
6520 TAILQ_INSERT_TAIL(&indirdep->ir_trunc, freework, fw_next);
6521 freework->fw_indir = indirdep;
6522 /*
6523 * Cancel any allocindirs that will not make it to disk.
6524 * We have to do this for all copies of the indirdep that
6525 * live on this newblk.
6526 */
6527 if ((indirdep->ir_state & DEPCOMPLETE) == 0) {
6528 if (newblk_lookup(mp, dbtofsb(ump->um_fs, bp->b_blkno), 0,
6529 &newblk) == 0)
6530 panic("setup_trunc_indir: lost block");
6531 LIST_FOREACH(indirn, &newblk->nb_indirdeps, ir_next)
6532 trunc_indirdep(indirn, freeblks, bp, off);
6533 } else
6534 trunc_indirdep(indirdep, freeblks, bp, off);
6535 FREE_LOCK(ump);
6536 /*
6537 * Creation is protected by the buf lock. The saveddata is only
6538 * needed if a full truncation follows a partial truncation but it
6539 * is difficult to allocate in that case so we fetch it anyway.
6540 */
6541 if (indirdep->ir_saveddata == NULL)
6542 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP,
6543 M_SOFTDEP_FLAGS);
6544 nowork:
6545 /* Fetch the blkno of the child and the zero start offset. */
6546 if (I_IS_UFS1(ip)) {
6547 blkno = ((ufs1_daddr_t *)bp->b_data)[off];
6548 start = (uint8_t *)&((ufs1_daddr_t *)bp->b_data)[off+1];
6549 } else {
6550 blkno = ((ufs2_daddr_t *)bp->b_data)[off];
6551 start = (uint8_t *)&((ufs2_daddr_t *)bp->b_data)[off+1];
6552 }
6553 if (freework) {
6554 /* Zero the truncated pointers. */
6555 end = bp->b_data + bp->b_bcount;
6556 bzero(start, end - start);
6557 bdwrite(bp);
6558 } else
6559 bqrelse(bp);
6560 if (level == 0)
6561 return (0);
6562 lbn++; /* adjust level */
6563 lbn -= (off * lbnadd);
6564 return setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno);
6565 }
6566
6567 /*
6568 * Complete the partial truncation of an indirect block setup by
6569 * setup_trunc_indir(). This zeros the truncated pointers in the saved
6570 * copy and writes them to disk before the freeblks is allowed to complete.
6571 */
6572 static void
complete_trunc_indir(struct freework * freework)6573 complete_trunc_indir(struct freework *freework)
6574 {
6575 struct freework *fwn;
6576 struct indirdep *indirdep;
6577 struct ufsmount *ump;
6578 struct buf *bp;
6579 uintptr_t start;
6580 int count;
6581
6582 ump = VFSTOUFS(freework->fw_list.wk_mp);
6583 LOCK_OWNED(ump);
6584 indirdep = freework->fw_indir;
6585 for (;;) {
6586 bp = indirdep->ir_bp;
6587 /* See if the block was discarded. */
6588 if (bp == NULL)
6589 break;
6590 /* Inline part of getdirtybuf(). We dont want bremfree. */
6591 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) == 0)
6592 break;
6593 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
6594 LOCK_PTR(ump)) == 0)
6595 BUF_UNLOCK(bp);
6596 ACQUIRE_LOCK(ump);
6597 }
6598 freework->fw_state |= DEPCOMPLETE;
6599 TAILQ_REMOVE(&indirdep->ir_trunc, freework, fw_next);
6600 /*
6601 * Zero the pointers in the saved copy.
6602 */
6603 if (indirdep->ir_state & UFS1FMT)
6604 start = sizeof(ufs1_daddr_t);
6605 else
6606 start = sizeof(ufs2_daddr_t);
6607 start *= freework->fw_start;
6608 count = indirdep->ir_savebp->b_bcount - start;
6609 start += (uintptr_t)indirdep->ir_savebp->b_data;
6610 bzero((char *)start, count);
6611 /*
6612 * We need to start the next truncation in the list if it has not
6613 * been started yet.
6614 */
6615 fwn = TAILQ_FIRST(&indirdep->ir_trunc);
6616 if (fwn != NULL) {
6617 if (fwn->fw_freeblks == indirdep->ir_freeblks)
6618 TAILQ_REMOVE(&indirdep->ir_trunc, fwn, fw_next);
6619 if ((fwn->fw_state & ONWORKLIST) == 0)
6620 freework_enqueue(fwn);
6621 }
6622 /*
6623 * If bp is NULL the block was fully truncated, restore
6624 * the saved block list otherwise free it if it is no
6625 * longer needed.
6626 */
6627 if (TAILQ_EMPTY(&indirdep->ir_trunc)) {
6628 if (bp == NULL)
6629 bcopy(indirdep->ir_saveddata,
6630 indirdep->ir_savebp->b_data,
6631 indirdep->ir_savebp->b_bcount);
6632 free(indirdep->ir_saveddata, M_INDIRDEP);
6633 indirdep->ir_saveddata = NULL;
6634 }
6635 /*
6636 * When bp is NULL there is a full truncation pending. We
6637 * must wait for this full truncation to be journaled before
6638 * we can release this freework because the disk pointers will
6639 * never be written as zero.
6640 */
6641 if (bp == NULL) {
6642 if (LIST_EMPTY(&indirdep->ir_freeblks->fb_jblkdephd))
6643 handle_written_freework(freework);
6644 else
6645 WORKLIST_INSERT(&indirdep->ir_freeblks->fb_freeworkhd,
6646 &freework->fw_list);
6647 if (fwn == NULL) {
6648 freework->fw_indir = (void *)0x0000deadbeef0000;
6649 bp = indirdep->ir_savebp;
6650 indirdep->ir_savebp = NULL;
6651 free_indirdep(indirdep);
6652 FREE_LOCK(ump);
6653 brelse(bp);
6654 ACQUIRE_LOCK(ump);
6655 }
6656 } else {
6657 /* Complete when the real copy is written. */
6658 WORKLIST_INSERT(&bp->b_dep, &freework->fw_list);
6659 BUF_UNLOCK(bp);
6660 }
6661 }
6662
6663 /*
6664 * Calculate the number of blocks we are going to release where datablocks
6665 * is the current total and length is the new file size.
6666 */
6667 static ufs2_daddr_t
blkcount(struct fs * fs,ufs2_daddr_t datablocks,off_t length)6668 blkcount(struct fs *fs,
6669 ufs2_daddr_t datablocks,
6670 off_t length)
6671 {
6672 off_t totblks, numblks;
6673
6674 totblks = 0;
6675 numblks = howmany(length, fs->fs_bsize);
6676 if (numblks <= UFS_NDADDR) {
6677 totblks = howmany(length, fs->fs_fsize);
6678 goto out;
6679 }
6680 totblks = blkstofrags(fs, numblks);
6681 numblks -= UFS_NDADDR;
6682 /*
6683 * Count all single, then double, then triple indirects required.
6684 * Subtracting one indirects worth of blocks for each pass
6685 * acknowledges one of each pointed to by the inode.
6686 */
6687 for (;;) {
6688 totblks += blkstofrags(fs, howmany(numblks, NINDIR(fs)));
6689 numblks -= NINDIR(fs);
6690 if (numblks <= 0)
6691 break;
6692 numblks = howmany(numblks, NINDIR(fs));
6693 }
6694 out:
6695 totblks = fsbtodb(fs, totblks);
6696 /*
6697 * Handle sparse files. We can't reclaim more blocks than the inode
6698 * references. We will correct it later in handle_complete_freeblks()
6699 * when we know the real count.
6700 */
6701 if (totblks > datablocks)
6702 return (0);
6703 return (datablocks - totblks);
6704 }
6705
6706 /*
6707 * Handle freeblocks for journaled softupdate filesystems.
6708 *
6709 * Contrary to normal softupdates, we must preserve the block pointers in
6710 * indirects until their subordinates are free. This is to avoid journaling
6711 * every block that is freed which may consume more space than the journal
6712 * itself. The recovery program will see the free block journals at the
6713 * base of the truncated area and traverse them to reclaim space. The
6714 * pointers in the inode may be cleared immediately after the journal
6715 * records are written because each direct and indirect pointer in the
6716 * inode is recorded in a journal. This permits full truncation to proceed
6717 * asynchronously. The write order is journal -> inode -> cgs -> indirects.
6718 *
6719 * The algorithm is as follows:
6720 * 1) Traverse the in-memory state and create journal entries to release
6721 * the relevant blocks and full indirect trees.
6722 * 2) Traverse the indirect block chain adding partial truncation freework
6723 * records to indirects in the path to lastlbn. The freework will
6724 * prevent new allocation dependencies from being satisfied in this
6725 * indirect until the truncation completes.
6726 * 3) Read and lock the inode block, performing an update with the new size
6727 * and pointers. This prevents truncated data from becoming valid on
6728 * disk through step 4.
6729 * 4) Reap unsatisfied dependencies that are beyond the truncated area,
6730 * eliminate journal work for those records that do not require it.
6731 * 5) Schedule the journal records to be written followed by the inode block.
6732 * 6) Allocate any necessary frags for the end of file.
6733 * 7) Zero any partially truncated blocks.
6734 *
6735 * From this truncation proceeds asynchronously using the freework and
6736 * indir_trunc machinery. The file will not be extended again into a
6737 * partially truncated indirect block until all work is completed but
6738 * the normal dependency mechanism ensures that it is rolled back/forward
6739 * as appropriate. Further truncation may occur without delay and is
6740 * serialized in indir_trunc().
6741 */
6742 void
softdep_journal_freeblocks(struct inode * ip,struct ucred * cred,off_t length,int flags)6743 softdep_journal_freeblocks(
6744 struct inode *ip, /* The inode whose length is to be reduced */
6745 struct ucred *cred,
6746 off_t length, /* The new length for the file */
6747 int flags) /* IO_EXT and/or IO_NORMAL */
6748 {
6749 struct freeblks *freeblks, *fbn;
6750 struct worklist *wk, *wkn;
6751 struct inodedep *inodedep;
6752 struct jblkdep *jblkdep;
6753 struct allocdirect *adp, *adpn;
6754 struct ufsmount *ump;
6755 struct fs *fs;
6756 struct buf *bp;
6757 struct vnode *vp;
6758 struct mount *mp;
6759 daddr_t dbn;
6760 ufs2_daddr_t extblocks, datablocks;
6761 ufs_lbn_t tmpval, lbn, lastlbn;
6762 int frags, lastoff, iboff, allocblock, needj, error, i;
6763
6764 ump = ITOUMP(ip);
6765 mp = UFSTOVFS(ump);
6766 fs = ump->um_fs;
6767 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
6768 ("softdep_journal_freeblocks called on non-softdep filesystem"));
6769 vp = ITOV(ip);
6770 needj = 1;
6771 iboff = -1;
6772 allocblock = 0;
6773 extblocks = 0;
6774 datablocks = 0;
6775 frags = 0;
6776 freeblks = newfreeblks(mp, ip);
6777 ACQUIRE_LOCK(ump);
6778 /*
6779 * If we're truncating a removed file that will never be written
6780 * we don't need to journal the block frees. The canceled journals
6781 * for the allocations will suffice.
6782 */
6783 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
6784 if ((inodedep->id_state & (UNLINKED | DEPCOMPLETE)) == UNLINKED &&
6785 length == 0)
6786 needj = 0;
6787 CTR3(KTR_SUJ, "softdep_journal_freeblks: ip %d length %ld needj %d",
6788 ip->i_number, length, needj);
6789 FREE_LOCK(ump);
6790 /*
6791 * Calculate the lbn that we are truncating to. This results in -1
6792 * if we're truncating the 0 bytes. So it is the last lbn we want
6793 * to keep, not the first lbn we want to truncate.
6794 */
6795 lastlbn = lblkno(fs, length + fs->fs_bsize - 1) - 1;
6796 lastoff = blkoff(fs, length);
6797 /*
6798 * Compute frags we are keeping in lastlbn. 0 means all.
6799 */
6800 if (lastlbn >= 0 && lastlbn < UFS_NDADDR) {
6801 frags = fragroundup(fs, lastoff);
6802 /* adp offset of last valid allocdirect. */
6803 iboff = lastlbn;
6804 } else if (lastlbn > 0)
6805 iboff = UFS_NDADDR;
6806 if (fs->fs_magic == FS_UFS2_MAGIC)
6807 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize));
6808 /*
6809 * Handle normal data blocks and indirects. This section saves
6810 * values used after the inode update to complete frag and indirect
6811 * truncation.
6812 */
6813 if ((flags & IO_NORMAL) != 0) {
6814 /*
6815 * Handle truncation of whole direct and indirect blocks.
6816 */
6817 for (i = iboff + 1; i < UFS_NDADDR; i++)
6818 setup_freedirect(freeblks, ip, i, needj);
6819 for (i = 0, tmpval = NINDIR(fs), lbn = UFS_NDADDR;
6820 i < UFS_NIADDR;
6821 i++, lbn += tmpval, tmpval *= NINDIR(fs)) {
6822 /* Release a whole indirect tree. */
6823 if (lbn > lastlbn) {
6824 setup_freeindir(freeblks, ip, i, -lbn -i,
6825 needj);
6826 continue;
6827 }
6828 iboff = i + UFS_NDADDR;
6829 /*
6830 * Traverse partially truncated indirect tree.
6831 */
6832 if (lbn <= lastlbn && lbn + tmpval - 1 > lastlbn)
6833 setup_trunc_indir(freeblks, ip, -lbn - i,
6834 lastlbn, DIP(ip, i_ib[i]));
6835 }
6836 /*
6837 * Handle partial truncation to a frag boundary.
6838 */
6839 if (frags) {
6840 ufs2_daddr_t blkno;
6841 long oldfrags;
6842
6843 oldfrags = blksize(fs, ip, lastlbn);
6844 blkno = DIP(ip, i_db[lastlbn]);
6845 if (blkno && oldfrags != frags) {
6846 oldfrags -= frags;
6847 oldfrags = numfrags(fs, oldfrags);
6848 blkno += numfrags(fs, frags);
6849 newfreework(ump, freeblks, NULL, lastlbn,
6850 blkno, oldfrags, 0, needj);
6851 if (needj)
6852 adjust_newfreework(freeblks,
6853 numfrags(fs, frags));
6854 } else if (blkno == 0)
6855 allocblock = 1;
6856 }
6857 /*
6858 * Add a journal record for partial truncate if we are
6859 * handling indirect blocks. Non-indirects need no extra
6860 * journaling.
6861 */
6862 if (length != 0 && lastlbn >= UFS_NDADDR) {
6863 UFS_INODE_SET_FLAG(ip, IN_TRUNCATED);
6864 newjtrunc(freeblks, length, 0);
6865 }
6866 ip->i_size = length;
6867 DIP_SET(ip, i_size, ip->i_size);
6868 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
6869 datablocks = DIP(ip, i_blocks) - extblocks;
6870 if (length != 0)
6871 datablocks = blkcount(fs, datablocks, length);
6872 freeblks->fb_len = length;
6873 }
6874 if ((flags & IO_EXT) != 0) {
6875 for (i = 0; i < UFS_NXADDR; i++)
6876 setup_freeext(freeblks, ip, i, needj);
6877 ip->i_din2->di_extsize = 0;
6878 datablocks += extblocks;
6879 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
6880 }
6881 #ifdef QUOTA
6882 /* Reference the quotas in case the block count is wrong in the end. */
6883 quotaref(vp, freeblks->fb_quota);
6884 (void) chkdq(ip, -datablocks, NOCRED, FORCE);
6885 #endif
6886 freeblks->fb_chkcnt = -datablocks;
6887 UFS_LOCK(ump);
6888 fs->fs_pendingblocks += datablocks;
6889 UFS_UNLOCK(ump);
6890 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks);
6891 /*
6892 * Handle truncation of incomplete alloc direct dependencies. We
6893 * hold the inode block locked to prevent incomplete dependencies
6894 * from reaching the disk while we are eliminating those that
6895 * have been truncated. This is a partially inlined ffs_update().
6896 */
6897 ufs_itimes(vp);
6898 ip->i_flag &= ~(IN_LAZYACCESS | IN_LAZYMOD | IN_MODIFIED);
6899 dbn = fsbtodb(fs, ino_to_fsba(fs, ip->i_number));
6900 error = ffs_breadz(ump, ump->um_devvp, dbn, dbn, (int)fs->fs_bsize,
6901 NULL, NULL, 0, cred, 0, NULL, &bp);
6902 if (error) {
6903 softdep_error("softdep_journal_freeblocks", error);
6904 return;
6905 }
6906 if (bp->b_bufsize == fs->fs_bsize)
6907 bp->b_flags |= B_CLUSTEROK;
6908 softdep_update_inodeblock(ip, bp, 0);
6909 if (ump->um_fstype == UFS1) {
6910 *((struct ufs1_dinode *)bp->b_data +
6911 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din1;
6912 } else {
6913 ffs_update_dinode_ckhash(fs, ip->i_din2);
6914 *((struct ufs2_dinode *)bp->b_data +
6915 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din2;
6916 }
6917 ACQUIRE_LOCK(ump);
6918 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
6919 if ((inodedep->id_state & IOSTARTED) != 0)
6920 panic("softdep_setup_freeblocks: inode busy");
6921 /*
6922 * Add the freeblks structure to the list of operations that
6923 * must await the zero'ed inode being written to disk. If we
6924 * still have a bitmap dependency (needj), then the inode
6925 * has never been written to disk, so we can process the
6926 * freeblks below once we have deleted the dependencies.
6927 */
6928 if (needj)
6929 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list);
6930 else
6931 freeblks->fb_state |= COMPLETE;
6932 if ((flags & IO_NORMAL) != 0) {
6933 TAILQ_FOREACH_SAFE(adp, &inodedep->id_inoupdt, ad_next, adpn) {
6934 if (adp->ad_offset > iboff)
6935 cancel_allocdirect(&inodedep->id_inoupdt, adp,
6936 freeblks);
6937 /*
6938 * Truncate the allocdirect. We could eliminate
6939 * or modify journal records as well.
6940 */
6941 else if (adp->ad_offset == iboff && frags)
6942 adp->ad_newsize = frags;
6943 }
6944 }
6945 if ((flags & IO_EXT) != 0)
6946 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL)
6947 cancel_allocdirect(&inodedep->id_extupdt, adp,
6948 freeblks);
6949 /*
6950 * Scan the bufwait list for newblock dependencies that will never
6951 * make it to disk.
6952 */
6953 LIST_FOREACH_SAFE(wk, &inodedep->id_bufwait, wk_list, wkn) {
6954 if (wk->wk_type != D_ALLOCDIRECT)
6955 continue;
6956 adp = WK_ALLOCDIRECT(wk);
6957 if (((flags & IO_NORMAL) != 0 && (adp->ad_offset > iboff)) ||
6958 ((flags & IO_EXT) != 0 && (adp->ad_state & EXTDATA))) {
6959 cancel_jfreeblk(freeblks, adp->ad_newblkno);
6960 cancel_newblk(WK_NEWBLK(wk), NULL, &freeblks->fb_jwork);
6961 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk);
6962 }
6963 }
6964 /*
6965 * Add journal work.
6966 */
6967 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps)
6968 add_to_journal(&jblkdep->jb_list);
6969 FREE_LOCK(ump);
6970 bdwrite(bp);
6971 /*
6972 * Truncate dependency structures beyond length.
6973 */
6974 trunc_dependencies(ip, freeblks, lastlbn, frags, flags);
6975 /*
6976 * This is only set when we need to allocate a fragment because
6977 * none existed at the end of a frag-sized file. It handles only
6978 * allocating a new, zero filled block.
6979 */
6980 if (allocblock) {
6981 ip->i_size = length - lastoff;
6982 DIP_SET(ip, i_size, ip->i_size);
6983 error = UFS_BALLOC(vp, length - 1, 1, cred, BA_CLRBUF, &bp);
6984 if (error != 0) {
6985 softdep_error("softdep_journal_freeblks", error);
6986 return;
6987 }
6988 ip->i_size = length;
6989 DIP_SET(ip, i_size, length);
6990 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
6991 allocbuf(bp, frags);
6992 ffs_update(vp, 0);
6993 bawrite(bp);
6994 } else if (lastoff != 0 && vp->v_type != VDIR) {
6995 int size;
6996
6997 /*
6998 * Zero the end of a truncated frag or block.
6999 */
7000 size = sblksize(fs, length, lastlbn);
7001 error = bread(vp, lastlbn, size, cred, &bp);
7002 if (error == 0) {
7003 bzero((char *)bp->b_data + lastoff, size - lastoff);
7004 bawrite(bp);
7005 } else if (!ffs_fsfail_cleanup(ump, error)) {
7006 softdep_error("softdep_journal_freeblks", error);
7007 return;
7008 }
7009 }
7010 ACQUIRE_LOCK(ump);
7011 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
7012 TAILQ_INSERT_TAIL(&inodedep->id_freeblklst, freeblks, fb_next);
7013 freeblks->fb_state |= DEPCOMPLETE | ONDEPLIST;
7014 /*
7015 * We zero earlier truncations so they don't erroneously
7016 * update i_blocks.
7017 */
7018 if (freeblks->fb_len == 0 && (flags & IO_NORMAL) != 0)
7019 TAILQ_FOREACH(fbn, &inodedep->id_freeblklst, fb_next)
7020 fbn->fb_len = 0;
7021 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE &&
7022 LIST_EMPTY(&freeblks->fb_jblkdephd))
7023 freeblks->fb_state |= INPROGRESS;
7024 else
7025 freeblks = NULL;
7026 FREE_LOCK(ump);
7027 if (freeblks)
7028 handle_workitem_freeblocks(freeblks, 0);
7029 trunc_pages(ip, length, extblocks, flags);
7030
7031 }
7032
7033 /*
7034 * Flush a JOP_SYNC to the journal.
7035 */
7036 void
softdep_journal_fsync(struct inode * ip)7037 softdep_journal_fsync(struct inode *ip)
7038 {
7039 struct jfsync *jfsync;
7040 struct ufsmount *ump;
7041
7042 ump = ITOUMP(ip);
7043 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
7044 ("softdep_journal_fsync called on non-softdep filesystem"));
7045 if ((ip->i_flag & IN_TRUNCATED) == 0)
7046 return;
7047 ip->i_flag &= ~IN_TRUNCATED;
7048 jfsync = malloc(sizeof(*jfsync), M_JFSYNC, M_SOFTDEP_FLAGS | M_ZERO);
7049 workitem_alloc(&jfsync->jfs_list, D_JFSYNC, UFSTOVFS(ump));
7050 jfsync->jfs_size = ip->i_size;
7051 jfsync->jfs_ino = ip->i_number;
7052 ACQUIRE_LOCK(ump);
7053 add_to_journal(&jfsync->jfs_list);
7054 jwait(&jfsync->jfs_list, MNT_WAIT);
7055 FREE_LOCK(ump);
7056 }
7057
7058 /*
7059 * Block de-allocation dependencies.
7060 *
7061 * When blocks are de-allocated, the on-disk pointers must be nullified before
7062 * the blocks are made available for use by other files. (The true
7063 * requirement is that old pointers must be nullified before new on-disk
7064 * pointers are set. We chose this slightly more stringent requirement to
7065 * reduce complexity.) Our implementation handles this dependency by updating
7066 * the inode (or indirect block) appropriately but delaying the actual block
7067 * de-allocation (i.e., freemap and free space count manipulation) until
7068 * after the updated versions reach stable storage. After the disk is
7069 * updated, the blocks can be safely de-allocated whenever it is convenient.
7070 * This implementation handles only the common case of reducing a file's
7071 * length to zero. Other cases are handled by the conventional synchronous
7072 * write approach.
7073 *
7074 * The ffs implementation with which we worked double-checks
7075 * the state of the block pointers and file size as it reduces
7076 * a file's length. Some of this code is replicated here in our
7077 * soft updates implementation. The freeblks->fb_chkcnt field is
7078 * used to transfer a part of this information to the procedure
7079 * that eventually de-allocates the blocks.
7080 *
7081 * This routine should be called from the routine that shortens
7082 * a file's length, before the inode's size or block pointers
7083 * are modified. It will save the block pointer information for
7084 * later release and zero the inode so that the calling routine
7085 * can release it.
7086 */
7087 void
softdep_setup_freeblocks(struct inode * ip,off_t length,int flags)7088 softdep_setup_freeblocks(
7089 struct inode *ip, /* The inode whose length is to be reduced */
7090 off_t length, /* The new length for the file */
7091 int flags) /* IO_EXT and/or IO_NORMAL */
7092 {
7093 struct ufs1_dinode *dp1;
7094 struct ufs2_dinode *dp2;
7095 struct freeblks *freeblks;
7096 struct inodedep *inodedep;
7097 struct allocdirect *adp;
7098 struct ufsmount *ump;
7099 struct buf *bp;
7100 struct fs *fs;
7101 ufs2_daddr_t extblocks, datablocks;
7102 struct mount *mp;
7103 int i, delay, error;
7104 ufs_lbn_t tmpval;
7105 ufs_lbn_t lbn;
7106
7107 ump = ITOUMP(ip);
7108 mp = UFSTOVFS(ump);
7109 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
7110 ("softdep_setup_freeblocks called on non-softdep filesystem"));
7111 CTR2(KTR_SUJ, "softdep_setup_freeblks: ip %d length %ld",
7112 ip->i_number, length);
7113 KASSERT(length == 0, ("softdep_setup_freeblocks: non-zero length"));
7114 fs = ump->um_fs;
7115 if ((error = bread(ump->um_devvp,
7116 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
7117 (int)fs->fs_bsize, NOCRED, &bp)) != 0) {
7118 if (!ffs_fsfail_cleanup(ump, error))
7119 softdep_error("softdep_setup_freeblocks", error);
7120 return;
7121 }
7122 freeblks = newfreeblks(mp, ip);
7123 extblocks = 0;
7124 datablocks = 0;
7125 if (fs->fs_magic == FS_UFS2_MAGIC)
7126 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize));
7127 if ((flags & IO_NORMAL) != 0) {
7128 for (i = 0; i < UFS_NDADDR; i++)
7129 setup_freedirect(freeblks, ip, i, 0);
7130 for (i = 0, tmpval = NINDIR(fs), lbn = UFS_NDADDR;
7131 i < UFS_NIADDR;
7132 i++, lbn += tmpval, tmpval *= NINDIR(fs))
7133 setup_freeindir(freeblks, ip, i, -lbn -i, 0);
7134 ip->i_size = 0;
7135 DIP_SET(ip, i_size, 0);
7136 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
7137 datablocks = DIP(ip, i_blocks) - extblocks;
7138 }
7139 if ((flags & IO_EXT) != 0) {
7140 for (i = 0; i < UFS_NXADDR; i++)
7141 setup_freeext(freeblks, ip, i, 0);
7142 ip->i_din2->di_extsize = 0;
7143 datablocks += extblocks;
7144 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
7145 }
7146 #ifdef QUOTA
7147 /* Reference the quotas in case the block count is wrong in the end. */
7148 quotaref(ITOV(ip), freeblks->fb_quota);
7149 (void) chkdq(ip, -datablocks, NOCRED, FORCE);
7150 #endif
7151 freeblks->fb_chkcnt = -datablocks;
7152 UFS_LOCK(ump);
7153 fs->fs_pendingblocks += datablocks;
7154 UFS_UNLOCK(ump);
7155 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks);
7156 /*
7157 * Push the zero'ed inode to its disk buffer so that we are free
7158 * to delete its dependencies below. Once the dependencies are gone
7159 * the buffer can be safely released.
7160 */
7161 if (ump->um_fstype == UFS1) {
7162 dp1 = ((struct ufs1_dinode *)bp->b_data +
7163 ino_to_fsbo(fs, ip->i_number));
7164 ip->i_din1->di_freelink = dp1->di_freelink;
7165 *dp1 = *ip->i_din1;
7166 } else {
7167 dp2 = ((struct ufs2_dinode *)bp->b_data +
7168 ino_to_fsbo(fs, ip->i_number));
7169 ip->i_din2->di_freelink = dp2->di_freelink;
7170 ffs_update_dinode_ckhash(fs, ip->i_din2);
7171 *dp2 = *ip->i_din2;
7172 }
7173 /*
7174 * Find and eliminate any inode dependencies.
7175 */
7176 ACQUIRE_LOCK(ump);
7177 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
7178 if ((inodedep->id_state & IOSTARTED) != 0)
7179 panic("softdep_setup_freeblocks: inode busy");
7180 /*
7181 * Add the freeblks structure to the list of operations that
7182 * must await the zero'ed inode being written to disk. If we
7183 * still have a bitmap dependency (delay == 0), then the inode
7184 * has never been written to disk, so we can process the
7185 * freeblks below once we have deleted the dependencies.
7186 */
7187 delay = (inodedep->id_state & DEPCOMPLETE);
7188 if (delay)
7189 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list);
7190 else
7191 freeblks->fb_state |= COMPLETE;
7192 /*
7193 * Because the file length has been truncated to zero, any
7194 * pending block allocation dependency structures associated
7195 * with this inode are obsolete and can simply be de-allocated.
7196 * We must first merge the two dependency lists to get rid of
7197 * any duplicate freefrag structures, then purge the merged list.
7198 * If we still have a bitmap dependency, then the inode has never
7199 * been written to disk, so we can free any fragments without delay.
7200 */
7201 if (flags & IO_NORMAL) {
7202 merge_inode_lists(&inodedep->id_newinoupdt,
7203 &inodedep->id_inoupdt);
7204 while ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL)
7205 cancel_allocdirect(&inodedep->id_inoupdt, adp,
7206 freeblks);
7207 }
7208 if (flags & IO_EXT) {
7209 merge_inode_lists(&inodedep->id_newextupdt,
7210 &inodedep->id_extupdt);
7211 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL)
7212 cancel_allocdirect(&inodedep->id_extupdt, adp,
7213 freeblks);
7214 }
7215 FREE_LOCK(ump);
7216 bdwrite(bp);
7217 trunc_dependencies(ip, freeblks, -1, 0, flags);
7218 ACQUIRE_LOCK(ump);
7219 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0)
7220 (void) free_inodedep(inodedep);
7221 freeblks->fb_state |= DEPCOMPLETE;
7222 /*
7223 * If the inode with zeroed block pointers is now on disk
7224 * we can start freeing blocks.
7225 */
7226 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE)
7227 freeblks->fb_state |= INPROGRESS;
7228 else
7229 freeblks = NULL;
7230 FREE_LOCK(ump);
7231 if (freeblks)
7232 handle_workitem_freeblocks(freeblks, 0);
7233 trunc_pages(ip, length, extblocks, flags);
7234 }
7235
7236 /*
7237 * Eliminate pages from the page cache that back parts of this inode and
7238 * adjust the vnode pager's idea of our size. This prevents stale data
7239 * from hanging around in the page cache.
7240 */
7241 static void
trunc_pages(struct inode * ip,off_t length,ufs2_daddr_t extblocks,int flags)7242 trunc_pages(
7243 struct inode *ip,
7244 off_t length,
7245 ufs2_daddr_t extblocks,
7246 int flags)
7247 {
7248 struct vnode *vp;
7249 struct fs *fs;
7250 ufs_lbn_t lbn;
7251 off_t end, extend;
7252
7253 vp = ITOV(ip);
7254 fs = ITOFS(ip);
7255 extend = OFF_TO_IDX(lblktosize(fs, -extblocks));
7256 if ((flags & IO_EXT) != 0)
7257 vn_pages_remove(vp, extend, 0);
7258 if ((flags & IO_NORMAL) == 0)
7259 return;
7260 BO_LOCK(&vp->v_bufobj);
7261 drain_output(vp);
7262 BO_UNLOCK(&vp->v_bufobj);
7263 /*
7264 * The vnode pager eliminates file pages we eliminate indirects
7265 * below.
7266 */
7267 vnode_pager_setsize(vp, length);
7268 /*
7269 * Calculate the end based on the last indirect we want to keep. If
7270 * the block extends into indirects we can just use the negative of
7271 * its lbn. Doubles and triples exist at lower numbers so we must
7272 * be careful not to remove those, if they exist. double and triple
7273 * indirect lbns do not overlap with others so it is not important
7274 * to verify how many levels are required.
7275 */
7276 lbn = lblkno(fs, length);
7277 if (lbn >= UFS_NDADDR) {
7278 /* Calculate the virtual lbn of the triple indirect. */
7279 lbn = -lbn - (UFS_NIADDR - 1);
7280 end = OFF_TO_IDX(lblktosize(fs, lbn));
7281 } else
7282 end = extend;
7283 vn_pages_remove(vp, OFF_TO_IDX(OFF_MAX), end);
7284 }
7285
7286 /*
7287 * See if the buf bp is in the range eliminated by truncation.
7288 */
7289 static int
trunc_check_buf(struct buf * bp,int * blkoffp,ufs_lbn_t lastlbn,int lastoff,int flags)7290 trunc_check_buf(
7291 struct buf *bp,
7292 int *blkoffp,
7293 ufs_lbn_t lastlbn,
7294 int lastoff,
7295 int flags)
7296 {
7297 ufs_lbn_t lbn;
7298
7299 *blkoffp = 0;
7300 /* Only match ext/normal blocks as appropriate. */
7301 if (((flags & IO_EXT) == 0 && (bp->b_xflags & BX_ALTDATA)) ||
7302 ((flags & IO_NORMAL) == 0 && (bp->b_xflags & BX_ALTDATA) == 0))
7303 return (0);
7304 /* ALTDATA is always a full truncation. */
7305 if ((bp->b_xflags & BX_ALTDATA) != 0)
7306 return (1);
7307 /* -1 is full truncation. */
7308 if (lastlbn == -1)
7309 return (1);
7310 /*
7311 * If this is a partial truncate we only want those
7312 * blocks and indirect blocks that cover the range
7313 * we're after.
7314 */
7315 lbn = bp->b_lblkno;
7316 if (lbn < 0)
7317 lbn = -(lbn + lbn_level(lbn));
7318 if (lbn < lastlbn)
7319 return (0);
7320 /* Here we only truncate lblkno if it's partial. */
7321 if (lbn == lastlbn) {
7322 if (lastoff == 0)
7323 return (0);
7324 *blkoffp = lastoff;
7325 }
7326 return (1);
7327 }
7328
7329 /*
7330 * Eliminate any dependencies that exist in memory beyond lblkno:off
7331 */
7332 static void
trunc_dependencies(struct inode * ip,struct freeblks * freeblks,ufs_lbn_t lastlbn,int lastoff,int flags)7333 trunc_dependencies(
7334 struct inode *ip,
7335 struct freeblks *freeblks,
7336 ufs_lbn_t lastlbn,
7337 int lastoff,
7338 int flags)
7339 {
7340 struct bufobj *bo;
7341 struct vnode *vp;
7342 struct buf *bp;
7343 int blkoff;
7344
7345 /*
7346 * We must wait for any I/O in progress to finish so that
7347 * all potential buffers on the dirty list will be visible.
7348 * Once they are all there, walk the list and get rid of
7349 * any dependencies.
7350 */
7351 vp = ITOV(ip);
7352 bo = &vp->v_bufobj;
7353 BO_LOCK(bo);
7354 drain_output(vp);
7355 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs)
7356 bp->b_vflags &= ~BV_SCANNED;
7357 restart:
7358 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) {
7359 if (bp->b_vflags & BV_SCANNED)
7360 continue;
7361 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) {
7362 bp->b_vflags |= BV_SCANNED;
7363 continue;
7364 }
7365 KASSERT(bp->b_bufobj == bo, ("Wrong object in buffer"));
7366 if ((bp = getdirtybuf(bp, BO_LOCKPTR(bo), MNT_WAIT)) == NULL)
7367 goto restart;
7368 BO_UNLOCK(bo);
7369 if (deallocate_dependencies(bp, freeblks, blkoff))
7370 bqrelse(bp);
7371 else
7372 brelse(bp);
7373 BO_LOCK(bo);
7374 goto restart;
7375 }
7376 /*
7377 * Now do the work of vtruncbuf while also matching indirect blocks.
7378 */
7379 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs)
7380 bp->b_vflags &= ~BV_SCANNED;
7381 cleanrestart:
7382 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs) {
7383 if (bp->b_vflags & BV_SCANNED)
7384 continue;
7385 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) {
7386 bp->b_vflags |= BV_SCANNED;
7387 continue;
7388 }
7389 if (BUF_LOCK(bp,
7390 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
7391 BO_LOCKPTR(bo)) == ENOLCK) {
7392 BO_LOCK(bo);
7393 goto cleanrestart;
7394 }
7395 BO_LOCK(bo);
7396 bp->b_vflags |= BV_SCANNED;
7397 BO_UNLOCK(bo);
7398 bremfree(bp);
7399 if (blkoff != 0) {
7400 allocbuf(bp, blkoff);
7401 bqrelse(bp);
7402 } else {
7403 bp->b_flags |= B_INVAL | B_NOCACHE | B_RELBUF;
7404 brelse(bp);
7405 }
7406 BO_LOCK(bo);
7407 goto cleanrestart;
7408 }
7409 drain_output(vp);
7410 BO_UNLOCK(bo);
7411 }
7412
7413 static int
cancel_pagedep(struct pagedep * pagedep,struct freeblks * freeblks,int blkoff)7414 cancel_pagedep(
7415 struct pagedep *pagedep,
7416 struct freeblks *freeblks,
7417 int blkoff)
7418 {
7419 struct jremref *jremref;
7420 struct jmvref *jmvref;
7421 struct dirrem *dirrem, *tmp;
7422 int i;
7423
7424 /*
7425 * Copy any directory remove dependencies to the list
7426 * to be processed after the freeblks proceeds. If
7427 * directory entry never made it to disk they
7428 * can be dumped directly onto the work list.
7429 */
7430 LIST_FOREACH_SAFE(dirrem, &pagedep->pd_dirremhd, dm_next, tmp) {
7431 /* Skip this directory removal if it is intended to remain. */
7432 if (dirrem->dm_offset < blkoff)
7433 continue;
7434 /*
7435 * If there are any dirrems we wait for the journal write
7436 * to complete and then restart the buf scan as the lock
7437 * has been dropped.
7438 */
7439 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) {
7440 jwait(&jremref->jr_list, MNT_WAIT);
7441 return (ERESTART);
7442 }
7443 LIST_REMOVE(dirrem, dm_next);
7444 dirrem->dm_dirinum = pagedep->pd_ino;
7445 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &dirrem->dm_list);
7446 }
7447 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) {
7448 jwait(&jmvref->jm_list, MNT_WAIT);
7449 return (ERESTART);
7450 }
7451 /*
7452 * When we're partially truncating a pagedep we just want to flush
7453 * journal entries and return. There can not be any adds in the
7454 * truncated portion of the directory and newblk must remain if
7455 * part of the block remains.
7456 */
7457 if (blkoff != 0) {
7458 struct diradd *dap;
7459
7460 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist)
7461 if (dap->da_offset > blkoff)
7462 panic("cancel_pagedep: diradd %p off %d > %d",
7463 dap, dap->da_offset, blkoff);
7464 for (i = 0; i < DAHASHSZ; i++)
7465 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist)
7466 if (dap->da_offset > blkoff)
7467 panic("cancel_pagedep: diradd %p off %d > %d",
7468 dap, dap->da_offset, blkoff);
7469 return (0);
7470 }
7471 /*
7472 * There should be no directory add dependencies present
7473 * as the directory could not be truncated until all
7474 * children were removed.
7475 */
7476 KASSERT(LIST_FIRST(&pagedep->pd_pendinghd) == NULL,
7477 ("deallocate_dependencies: pendinghd != NULL"));
7478 for (i = 0; i < DAHASHSZ; i++)
7479 KASSERT(LIST_FIRST(&pagedep->pd_diraddhd[i]) == NULL,
7480 ("deallocate_dependencies: diraddhd != NULL"));
7481 if ((pagedep->pd_state & NEWBLOCK) != 0)
7482 free_newdirblk(pagedep->pd_newdirblk);
7483 if (free_pagedep(pagedep) == 0)
7484 panic("Failed to free pagedep %p", pagedep);
7485 return (0);
7486 }
7487
7488 /*
7489 * Reclaim any dependency structures from a buffer that is about to
7490 * be reallocated to a new vnode. The buffer must be locked, thus,
7491 * no I/O completion operations can occur while we are manipulating
7492 * its associated dependencies. The mutex is held so that other I/O's
7493 * associated with related dependencies do not occur.
7494 */
7495 static int
deallocate_dependencies(struct buf * bp,struct freeblks * freeblks,int off)7496 deallocate_dependencies(
7497 struct buf *bp,
7498 struct freeblks *freeblks,
7499 int off)
7500 {
7501 struct indirdep *indirdep;
7502 struct pagedep *pagedep;
7503 struct worklist *wk, *wkn;
7504 struct ufsmount *ump;
7505
7506 ump = softdep_bp_to_mp(bp);
7507 if (ump == NULL)
7508 goto done;
7509 ACQUIRE_LOCK(ump);
7510 LIST_FOREACH_SAFE(wk, &bp->b_dep, wk_list, wkn) {
7511 switch (wk->wk_type) {
7512 case D_INDIRDEP:
7513 indirdep = WK_INDIRDEP(wk);
7514 if (bp->b_lblkno >= 0 ||
7515 bp->b_blkno != indirdep->ir_savebp->b_lblkno)
7516 panic("deallocate_dependencies: not indir");
7517 cancel_indirdep(indirdep, bp, freeblks);
7518 continue;
7519
7520 case D_PAGEDEP:
7521 pagedep = WK_PAGEDEP(wk);
7522 if (cancel_pagedep(pagedep, freeblks, off)) {
7523 FREE_LOCK(ump);
7524 return (ERESTART);
7525 }
7526 continue;
7527
7528 case D_ALLOCINDIR:
7529 /*
7530 * Simply remove the allocindir, we'll find it via
7531 * the indirdep where we can clear pointers if
7532 * needed.
7533 */
7534 WORKLIST_REMOVE(wk);
7535 continue;
7536
7537 case D_FREEWORK:
7538 /*
7539 * A truncation is waiting for the zero'd pointers
7540 * to be written. It can be freed when the freeblks
7541 * is journaled.
7542 */
7543 WORKLIST_REMOVE(wk);
7544 wk->wk_state |= ONDEPLIST;
7545 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk);
7546 break;
7547
7548 case D_ALLOCDIRECT:
7549 if (off != 0)
7550 continue;
7551 /* FALLTHROUGH */
7552 default:
7553 panic("deallocate_dependencies: Unexpected type %s",
7554 TYPENAME(wk->wk_type));
7555 /* NOTREACHED */
7556 }
7557 }
7558 FREE_LOCK(ump);
7559 done:
7560 /*
7561 * Don't throw away this buf, we were partially truncating and
7562 * some deps may always remain.
7563 */
7564 if (off) {
7565 allocbuf(bp, off);
7566 bp->b_vflags |= BV_SCANNED;
7567 return (EBUSY);
7568 }
7569 bp->b_flags |= B_INVAL | B_NOCACHE;
7570
7571 return (0);
7572 }
7573
7574 /*
7575 * An allocdirect is being canceled due to a truncate. We must make sure
7576 * the journal entry is released in concert with the blkfree that releases
7577 * the storage. Completed journal entries must not be released until the
7578 * space is no longer pointed to by the inode or in the bitmap.
7579 */
7580 static void
cancel_allocdirect(struct allocdirectlst * adphead,struct allocdirect * adp,struct freeblks * freeblks)7581 cancel_allocdirect(
7582 struct allocdirectlst *adphead,
7583 struct allocdirect *adp,
7584 struct freeblks *freeblks)
7585 {
7586 struct freework *freework;
7587 struct newblk *newblk;
7588 struct worklist *wk;
7589
7590 TAILQ_REMOVE(adphead, adp, ad_next);
7591 newblk = (struct newblk *)adp;
7592 freework = NULL;
7593 /*
7594 * Find the correct freework structure.
7595 */
7596 LIST_FOREACH(wk, &freeblks->fb_freeworkhd, wk_list) {
7597 if (wk->wk_type != D_FREEWORK)
7598 continue;
7599 freework = WK_FREEWORK(wk);
7600 if (freework->fw_blkno == newblk->nb_newblkno)
7601 break;
7602 }
7603 if (freework == NULL)
7604 panic("cancel_allocdirect: Freework not found");
7605 /*
7606 * If a newblk exists at all we still have the journal entry that
7607 * initiated the allocation so we do not need to journal the free.
7608 */
7609 cancel_jfreeblk(freeblks, freework->fw_blkno);
7610 /*
7611 * If the journal hasn't been written the jnewblk must be passed
7612 * to the call to ffs_blkfree that reclaims the space. We accomplish
7613 * this by linking the journal dependency into the freework to be
7614 * freed when freework_freeblock() is called. If the journal has
7615 * been written we can simply reclaim the journal space when the
7616 * freeblks work is complete.
7617 */
7618 freework->fw_jnewblk = cancel_newblk(newblk, &freework->fw_list,
7619 &freeblks->fb_jwork);
7620 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list);
7621 }
7622
7623 /*
7624 * Cancel a new block allocation. May be an indirect or direct block. We
7625 * remove it from various lists and return any journal record that needs to
7626 * be resolved by the caller.
7627 *
7628 * A special consideration is made for indirects which were never pointed
7629 * at on disk and will never be found once this block is released.
7630 */
7631 static struct jnewblk *
cancel_newblk(struct newblk * newblk,struct worklist * wk,struct workhead * wkhd)7632 cancel_newblk(
7633 struct newblk *newblk,
7634 struct worklist *wk,
7635 struct workhead *wkhd)
7636 {
7637 struct jnewblk *jnewblk;
7638
7639 CTR1(KTR_SUJ, "cancel_newblk: blkno %jd", newblk->nb_newblkno);
7640
7641 newblk->nb_state |= GOINGAWAY;
7642 /*
7643 * Previously we traversed the completedhd on each indirdep
7644 * attached to this newblk to cancel them and gather journal
7645 * work. Since we need only the oldest journal segment and
7646 * the lowest point on the tree will always have the oldest
7647 * journal segment we are free to release the segments
7648 * of any subordinates and may leave the indirdep list to
7649 * indirdep_complete() when this newblk is freed.
7650 */
7651 if (newblk->nb_state & ONDEPLIST) {
7652 newblk->nb_state &= ~ONDEPLIST;
7653 LIST_REMOVE(newblk, nb_deps);
7654 }
7655 if (newblk->nb_state & ONWORKLIST)
7656 WORKLIST_REMOVE(&newblk->nb_list);
7657 /*
7658 * If the journal entry hasn't been written we save a pointer to
7659 * the dependency that frees it until it is written or the
7660 * superseding operation completes.
7661 */
7662 jnewblk = newblk->nb_jnewblk;
7663 if (jnewblk != NULL && wk != NULL) {
7664 newblk->nb_jnewblk = NULL;
7665 jnewblk->jn_dep = wk;
7666 }
7667 if (!LIST_EMPTY(&newblk->nb_jwork))
7668 jwork_move(wkhd, &newblk->nb_jwork);
7669 /*
7670 * When truncating we must free the newdirblk early to remove
7671 * the pagedep from the hash before returning.
7672 */
7673 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL)
7674 free_newdirblk(WK_NEWDIRBLK(wk));
7675 if (!LIST_EMPTY(&newblk->nb_newdirblk))
7676 panic("cancel_newblk: extra newdirblk");
7677
7678 return (jnewblk);
7679 }
7680
7681 /*
7682 * Schedule the freefrag associated with a newblk to be released once
7683 * the pointers are written and the previous block is no longer needed.
7684 */
7685 static void
newblk_freefrag(struct newblk * newblk)7686 newblk_freefrag(struct newblk *newblk)
7687 {
7688 struct freefrag *freefrag;
7689
7690 if (newblk->nb_freefrag == NULL)
7691 return;
7692 freefrag = newblk->nb_freefrag;
7693 newblk->nb_freefrag = NULL;
7694 freefrag->ff_state |= COMPLETE;
7695 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE)
7696 add_to_worklist(&freefrag->ff_list, 0);
7697 }
7698
7699 /*
7700 * Free a newblk. Generate a new freefrag work request if appropriate.
7701 * This must be called after the inode pointer and any direct block pointers
7702 * are valid or fully removed via truncate or frag extension.
7703 */
7704 static void
free_newblk(struct newblk * newblk)7705 free_newblk(struct newblk *newblk)
7706 {
7707 struct indirdep *indirdep;
7708 struct worklist *wk;
7709
7710 KASSERT(newblk->nb_jnewblk == NULL,
7711 ("free_newblk: jnewblk %p still attached", newblk->nb_jnewblk));
7712 KASSERT(newblk->nb_list.wk_type != D_NEWBLK,
7713 ("free_newblk: unclaimed newblk"));
7714 LOCK_OWNED(VFSTOUFS(newblk->nb_list.wk_mp));
7715 newblk_freefrag(newblk);
7716 if (newblk->nb_state & ONDEPLIST)
7717 LIST_REMOVE(newblk, nb_deps);
7718 if (newblk->nb_state & ONWORKLIST)
7719 WORKLIST_REMOVE(&newblk->nb_list);
7720 LIST_REMOVE(newblk, nb_hash);
7721 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL)
7722 free_newdirblk(WK_NEWDIRBLK(wk));
7723 if (!LIST_EMPTY(&newblk->nb_newdirblk))
7724 panic("free_newblk: extra newdirblk");
7725 while ((indirdep = LIST_FIRST(&newblk->nb_indirdeps)) != NULL)
7726 indirdep_complete(indirdep);
7727 handle_jwork(&newblk->nb_jwork);
7728 WORKITEM_FREE(newblk, D_NEWBLK);
7729 }
7730
7731 /*
7732 * Free a newdirblk. Clear the NEWBLOCK flag on its associated pagedep.
7733 */
7734 static void
free_newdirblk(struct newdirblk * newdirblk)7735 free_newdirblk(struct newdirblk *newdirblk)
7736 {
7737 struct pagedep *pagedep;
7738 struct diradd *dap;
7739 struct worklist *wk;
7740
7741 LOCK_OWNED(VFSTOUFS(newdirblk->db_list.wk_mp));
7742 WORKLIST_REMOVE(&newdirblk->db_list);
7743 /*
7744 * If the pagedep is still linked onto the directory buffer
7745 * dependency chain, then some of the entries on the
7746 * pd_pendinghd list may not be committed to disk yet. In
7747 * this case, we will simply clear the NEWBLOCK flag and
7748 * let the pd_pendinghd list be processed when the pagedep
7749 * is next written. If the pagedep is no longer on the buffer
7750 * dependency chain, then all the entries on the pd_pending
7751 * list are committed to disk and we can free them here.
7752 */
7753 pagedep = newdirblk->db_pagedep;
7754 pagedep->pd_state &= ~NEWBLOCK;
7755 if ((pagedep->pd_state & ONWORKLIST) == 0) {
7756 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL)
7757 free_diradd(dap, NULL);
7758 /*
7759 * If no dependencies remain, the pagedep will be freed.
7760 */
7761 free_pagedep(pagedep);
7762 }
7763 /* Should only ever be one item in the list. */
7764 while ((wk = LIST_FIRST(&newdirblk->db_mkdir)) != NULL) {
7765 WORKLIST_REMOVE(wk);
7766 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY);
7767 }
7768 WORKITEM_FREE(newdirblk, D_NEWDIRBLK);
7769 }
7770
7771 /*
7772 * Prepare an inode to be freed. The actual free operation is not
7773 * done until the zero'ed inode has been written to disk.
7774 */
7775 void
softdep_freefile(struct vnode * pvp,ino_t ino,int mode)7776 softdep_freefile(
7777 struct vnode *pvp,
7778 ino_t ino,
7779 int mode)
7780 {
7781 struct inode *ip = VTOI(pvp);
7782 struct inodedep *inodedep;
7783 struct freefile *freefile;
7784 struct freeblks *freeblks;
7785 struct ufsmount *ump;
7786
7787 ump = ITOUMP(ip);
7788 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
7789 ("softdep_freefile called on non-softdep filesystem"));
7790 /*
7791 * This sets up the inode de-allocation dependency.
7792 */
7793 freefile = malloc(sizeof(struct freefile),
7794 M_FREEFILE, M_SOFTDEP_FLAGS);
7795 workitem_alloc(&freefile->fx_list, D_FREEFILE, pvp->v_mount);
7796 freefile->fx_mode = mode;
7797 freefile->fx_oldinum = ino;
7798 freefile->fx_devvp = ump->um_devvp;
7799 LIST_INIT(&freefile->fx_jwork);
7800 UFS_LOCK(ump);
7801 ump->um_fs->fs_pendinginodes += 1;
7802 UFS_UNLOCK(ump);
7803
7804 /*
7805 * If the inodedep does not exist, then the zero'ed inode has
7806 * been written to disk. If the allocated inode has never been
7807 * written to disk, then the on-disk inode is zero'ed. In either
7808 * case we can free the file immediately. If the journal was
7809 * canceled before being written the inode will never make it to
7810 * disk and we must send the canceled journal entrys to
7811 * ffs_freefile() to be cleared in conjunction with the bitmap.
7812 * Any blocks waiting on the inode to write can be safely freed
7813 * here as it will never been written.
7814 */
7815 ACQUIRE_LOCK(ump);
7816 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep);
7817 if (inodedep) {
7818 /*
7819 * Clear out freeblks that no longer need to reference
7820 * this inode.
7821 */
7822 while ((freeblks =
7823 TAILQ_FIRST(&inodedep->id_freeblklst)) != NULL) {
7824 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks,
7825 fb_next);
7826 freeblks->fb_state &= ~ONDEPLIST;
7827 }
7828 /*
7829 * Remove this inode from the unlinked list.
7830 */
7831 if (inodedep->id_state & UNLINKED) {
7832 /*
7833 * Save the journal work to be freed with the bitmap
7834 * before we clear UNLINKED. Otherwise it can be lost
7835 * if the inode block is written.
7836 */
7837 handle_bufwait(inodedep, &freefile->fx_jwork);
7838 clear_unlinked_inodedep(inodedep);
7839 /*
7840 * Re-acquire inodedep as we've dropped the
7841 * per-filesystem lock in clear_unlinked_inodedep().
7842 */
7843 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep);
7844 }
7845 }
7846 if (inodedep == NULL || check_inode_unwritten(inodedep)) {
7847 FREE_LOCK(ump);
7848 handle_workitem_freefile(freefile);
7849 return;
7850 }
7851 if ((inodedep->id_state & DEPCOMPLETE) == 0)
7852 inodedep->id_state |= GOINGAWAY;
7853 WORKLIST_INSERT(&inodedep->id_inowait, &freefile->fx_list);
7854 FREE_LOCK(ump);
7855 if (ip->i_number == ino)
7856 UFS_INODE_SET_FLAG(ip, IN_MODIFIED);
7857 }
7858
7859 /*
7860 * Check to see if an inode has never been written to disk. If
7861 * so free the inodedep and return success, otherwise return failure.
7862 *
7863 * If we still have a bitmap dependency, then the inode has never
7864 * been written to disk. Drop the dependency as it is no longer
7865 * necessary since the inode is being deallocated. We set the
7866 * ALLCOMPLETE flags since the bitmap now properly shows that the
7867 * inode is not allocated. Even if the inode is actively being
7868 * written, it has been rolled back to its zero'ed state, so we
7869 * are ensured that a zero inode is what is on the disk. For short
7870 * lived files, this change will usually result in removing all the
7871 * dependencies from the inode so that it can be freed immediately.
7872 */
7873 static int
check_inode_unwritten(struct inodedep * inodedep)7874 check_inode_unwritten(struct inodedep *inodedep)
7875 {
7876
7877 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp));
7878
7879 if ((inodedep->id_state & (DEPCOMPLETE | UNLINKED)) != 0 ||
7880 !LIST_EMPTY(&inodedep->id_dirremhd) ||
7881 !LIST_EMPTY(&inodedep->id_pendinghd) ||
7882 !LIST_EMPTY(&inodedep->id_bufwait) ||
7883 !LIST_EMPTY(&inodedep->id_inowait) ||
7884 !TAILQ_EMPTY(&inodedep->id_inoreflst) ||
7885 !TAILQ_EMPTY(&inodedep->id_inoupdt) ||
7886 !TAILQ_EMPTY(&inodedep->id_newinoupdt) ||
7887 !TAILQ_EMPTY(&inodedep->id_extupdt) ||
7888 !TAILQ_EMPTY(&inodedep->id_newextupdt) ||
7889 !TAILQ_EMPTY(&inodedep->id_freeblklst) ||
7890 inodedep->id_mkdiradd != NULL ||
7891 inodedep->id_nlinkdelta != 0)
7892 return (0);
7893 /*
7894 * Another process might be in initiate_write_inodeblock_ufs[12]
7895 * trying to allocate memory without holding "Softdep Lock".
7896 */
7897 if ((inodedep->id_state & IOSTARTED) != 0 &&
7898 inodedep->id_savedino1 == NULL)
7899 return (0);
7900
7901 if (inodedep->id_state & ONDEPLIST)
7902 LIST_REMOVE(inodedep, id_deps);
7903 inodedep->id_state &= ~ONDEPLIST;
7904 inodedep->id_state |= ALLCOMPLETE;
7905 inodedep->id_bmsafemap = NULL;
7906 if (inodedep->id_state & ONWORKLIST)
7907 WORKLIST_REMOVE(&inodedep->id_list);
7908 if (inodedep->id_savedino1 != NULL) {
7909 free(inodedep->id_savedino1, M_SAVEDINO);
7910 inodedep->id_savedino1 = NULL;
7911 }
7912 if (free_inodedep(inodedep) == 0)
7913 panic("check_inode_unwritten: busy inode");
7914 return (1);
7915 }
7916
7917 static int
check_inodedep_free(struct inodedep * inodedep)7918 check_inodedep_free(struct inodedep *inodedep)
7919 {
7920
7921 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp));
7922 if ((inodedep->id_state & ALLCOMPLETE) != ALLCOMPLETE ||
7923 !LIST_EMPTY(&inodedep->id_dirremhd) ||
7924 !LIST_EMPTY(&inodedep->id_pendinghd) ||
7925 !LIST_EMPTY(&inodedep->id_bufwait) ||
7926 !LIST_EMPTY(&inodedep->id_inowait) ||
7927 !TAILQ_EMPTY(&inodedep->id_inoreflst) ||
7928 !TAILQ_EMPTY(&inodedep->id_inoupdt) ||
7929 !TAILQ_EMPTY(&inodedep->id_newinoupdt) ||
7930 !TAILQ_EMPTY(&inodedep->id_extupdt) ||
7931 !TAILQ_EMPTY(&inodedep->id_newextupdt) ||
7932 !TAILQ_EMPTY(&inodedep->id_freeblklst) ||
7933 inodedep->id_mkdiradd != NULL ||
7934 inodedep->id_nlinkdelta != 0 ||
7935 inodedep->id_savedino1 != NULL)
7936 return (0);
7937 return (1);
7938 }
7939
7940 /*
7941 * Try to free an inodedep structure. Return 1 if it could be freed.
7942 */
7943 static int
free_inodedep(struct inodedep * inodedep)7944 free_inodedep(struct inodedep *inodedep)
7945 {
7946
7947 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp));
7948 if ((inodedep->id_state & (ONWORKLIST | UNLINKED)) != 0 ||
7949 !check_inodedep_free(inodedep))
7950 return (0);
7951 if (inodedep->id_state & ONDEPLIST)
7952 LIST_REMOVE(inodedep, id_deps);
7953 LIST_REMOVE(inodedep, id_hash);
7954 WORKITEM_FREE(inodedep, D_INODEDEP);
7955 return (1);
7956 }
7957
7958 /*
7959 * Free the block referenced by a freework structure. The parent freeblks
7960 * structure is released and completed when the final cg bitmap reaches
7961 * the disk. This routine may be freeing a jnewblk which never made it to
7962 * disk in which case we do not have to wait as the operation is undone
7963 * in memory immediately.
7964 */
7965 static void
freework_freeblock(struct freework * freework,uint64_t key)7966 freework_freeblock(struct freework *freework, uint64_t key)
7967 {
7968 struct freeblks *freeblks;
7969 struct jnewblk *jnewblk;
7970 struct ufsmount *ump;
7971 struct workhead wkhd;
7972 struct fs *fs;
7973 int bsize;
7974 int needj;
7975
7976 ump = VFSTOUFS(freework->fw_list.wk_mp);
7977 LOCK_OWNED(ump);
7978 /*
7979 * Handle partial truncate separately.
7980 */
7981 if (freework->fw_indir) {
7982 complete_trunc_indir(freework);
7983 return;
7984 }
7985 freeblks = freework->fw_freeblks;
7986 fs = ump->um_fs;
7987 needj = MOUNTEDSUJ(freeblks->fb_list.wk_mp) != 0;
7988 bsize = lfragtosize(fs, freework->fw_frags);
7989 LIST_INIT(&wkhd);
7990 /*
7991 * DEPCOMPLETE is cleared in indirblk_insert() if the block lives
7992 * on the indirblk hashtable and prevents premature freeing.
7993 */
7994 freework->fw_state |= DEPCOMPLETE;
7995 /*
7996 * SUJ needs to wait for the segment referencing freed indirect
7997 * blocks to expire so that we know the checker will not confuse
7998 * a re-allocated indirect block with its old contents.
7999 */
8000 if (needj && freework->fw_lbn <= -UFS_NDADDR)
8001 indirblk_insert(freework);
8002 /*
8003 * If we are canceling an existing jnewblk pass it to the free
8004 * routine, otherwise pass the freeblk which will ultimately
8005 * release the freeblks. If we're not journaling, we can just
8006 * free the freeblks immediately.
8007 */
8008 jnewblk = freework->fw_jnewblk;
8009 if (jnewblk != NULL) {
8010 cancel_jnewblk(jnewblk, &wkhd);
8011 needj = 0;
8012 } else if (needj) {
8013 freework->fw_state |= DELAYEDFREE;
8014 freeblks->fb_cgwait++;
8015 WORKLIST_INSERT(&wkhd, &freework->fw_list);
8016 }
8017 FREE_LOCK(ump);
8018 freeblks_free(ump, freeblks, btodb(bsize));
8019 CTR4(KTR_SUJ,
8020 "freework_freeblock: ino %jd blkno %jd lbn %jd size %d",
8021 freeblks->fb_inum, freework->fw_blkno, freework->fw_lbn, bsize);
8022 ffs_blkfree(ump, fs, freeblks->fb_devvp, freework->fw_blkno, bsize,
8023 freeblks->fb_inum, freeblks->fb_vtype, &wkhd, key);
8024 ACQUIRE_LOCK(ump);
8025 /*
8026 * The jnewblk will be discarded and the bits in the map never
8027 * made it to disk. We can immediately free the freeblk.
8028 */
8029 if (needj == 0)
8030 handle_written_freework(freework);
8031 }
8032
8033 /*
8034 * We enqueue freework items that need processing back on the freeblks and
8035 * add the freeblks to the worklist. This makes it easier to find all work
8036 * required to flush a truncation in process_truncates().
8037 */
8038 static void
freework_enqueue(struct freework * freework)8039 freework_enqueue(struct freework *freework)
8040 {
8041 struct freeblks *freeblks;
8042
8043 freeblks = freework->fw_freeblks;
8044 if ((freework->fw_state & INPROGRESS) == 0)
8045 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list);
8046 if ((freeblks->fb_state &
8047 (ONWORKLIST | INPROGRESS | ALLCOMPLETE)) == ALLCOMPLETE &&
8048 LIST_EMPTY(&freeblks->fb_jblkdephd))
8049 add_to_worklist(&freeblks->fb_list, WK_NODELAY);
8050 }
8051
8052 /*
8053 * Start, continue, or finish the process of freeing an indirect block tree.
8054 * The free operation may be paused at any point with fw_off containing the
8055 * offset to restart from. This enables us to implement some flow control
8056 * for large truncates which may fan out and generate a huge number of
8057 * dependencies.
8058 */
8059 static void
handle_workitem_indirblk(struct freework * freework)8060 handle_workitem_indirblk(struct freework *freework)
8061 {
8062 struct freeblks *freeblks;
8063 struct ufsmount *ump;
8064 struct fs *fs;
8065
8066 freeblks = freework->fw_freeblks;
8067 ump = VFSTOUFS(freeblks->fb_list.wk_mp);
8068 fs = ump->um_fs;
8069 if (freework->fw_state & DEPCOMPLETE) {
8070 handle_written_freework(freework);
8071 return;
8072 }
8073 if (freework->fw_off == NINDIR(fs)) {
8074 freework_freeblock(freework, SINGLETON_KEY);
8075 return;
8076 }
8077 freework->fw_state |= INPROGRESS;
8078 FREE_LOCK(ump);
8079 indir_trunc(freework, fsbtodb(fs, freework->fw_blkno),
8080 freework->fw_lbn);
8081 ACQUIRE_LOCK(ump);
8082 }
8083
8084 /*
8085 * Called when a freework structure attached to a cg buf is written. The
8086 * ref on either the parent or the freeblks structure is released and
8087 * the freeblks is added back to the worklist if there is more work to do.
8088 */
8089 static void
handle_written_freework(struct freework * freework)8090 handle_written_freework(struct freework *freework)
8091 {
8092 struct freeblks *freeblks;
8093 struct freework *parent;
8094
8095 freeblks = freework->fw_freeblks;
8096 parent = freework->fw_parent;
8097 if (freework->fw_state & DELAYEDFREE)
8098 freeblks->fb_cgwait--;
8099 freework->fw_state |= COMPLETE;
8100 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE)
8101 WORKITEM_FREE(freework, D_FREEWORK);
8102 if (parent) {
8103 if (--parent->fw_ref == 0)
8104 freework_enqueue(parent);
8105 return;
8106 }
8107 if (--freeblks->fb_ref != 0)
8108 return;
8109 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST | INPROGRESS)) ==
8110 ALLCOMPLETE && LIST_EMPTY(&freeblks->fb_jblkdephd))
8111 add_to_worklist(&freeblks->fb_list, WK_NODELAY);
8112 }
8113
8114 /*
8115 * This workitem routine performs the block de-allocation.
8116 * The workitem is added to the pending list after the updated
8117 * inode block has been written to disk. As mentioned above,
8118 * checks regarding the number of blocks de-allocated (compared
8119 * to the number of blocks allocated for the file) are also
8120 * performed in this function.
8121 */
8122 static int
handle_workitem_freeblocks(struct freeblks * freeblks,int flags)8123 handle_workitem_freeblocks(struct freeblks *freeblks, int flags)
8124 {
8125 struct freework *freework;
8126 struct newblk *newblk;
8127 struct allocindir *aip;
8128 struct ufsmount *ump;
8129 struct worklist *wk;
8130 uint64_t key;
8131
8132 KASSERT(LIST_EMPTY(&freeblks->fb_jblkdephd),
8133 ("handle_workitem_freeblocks: Journal entries not written."));
8134 ump = VFSTOUFS(freeblks->fb_list.wk_mp);
8135 key = ffs_blkrelease_start(ump, freeblks->fb_devvp, freeblks->fb_inum);
8136 ACQUIRE_LOCK(ump);
8137 while ((wk = LIST_FIRST(&freeblks->fb_freeworkhd)) != NULL) {
8138 WORKLIST_REMOVE(wk);
8139 switch (wk->wk_type) {
8140 case D_DIRREM:
8141 wk->wk_state |= COMPLETE;
8142 add_to_worklist(wk, 0);
8143 continue;
8144
8145 case D_ALLOCDIRECT:
8146 free_newblk(WK_NEWBLK(wk));
8147 continue;
8148
8149 case D_ALLOCINDIR:
8150 aip = WK_ALLOCINDIR(wk);
8151 freework = NULL;
8152 if (aip->ai_state & DELAYEDFREE) {
8153 FREE_LOCK(ump);
8154 freework = newfreework(ump, freeblks, NULL,
8155 aip->ai_lbn, aip->ai_newblkno,
8156 ump->um_fs->fs_frag, 0, 0);
8157 ACQUIRE_LOCK(ump);
8158 }
8159 newblk = WK_NEWBLK(wk);
8160 if (newblk->nb_jnewblk) {
8161 freework->fw_jnewblk = newblk->nb_jnewblk;
8162 newblk->nb_jnewblk->jn_dep = &freework->fw_list;
8163 newblk->nb_jnewblk = NULL;
8164 }
8165 free_newblk(newblk);
8166 continue;
8167
8168 case D_FREEWORK:
8169 freework = WK_FREEWORK(wk);
8170 if (freework->fw_lbn <= -UFS_NDADDR)
8171 handle_workitem_indirblk(freework);
8172 else
8173 freework_freeblock(freework, key);
8174 continue;
8175 default:
8176 panic("handle_workitem_freeblocks: Unknown type %s",
8177 TYPENAME(wk->wk_type));
8178 }
8179 }
8180 if (freeblks->fb_ref != 0) {
8181 freeblks->fb_state &= ~INPROGRESS;
8182 wake_worklist(&freeblks->fb_list);
8183 freeblks = NULL;
8184 }
8185 FREE_LOCK(ump);
8186 ffs_blkrelease_finish(ump, key);
8187 if (freeblks)
8188 return handle_complete_freeblocks(freeblks, flags);
8189 return (0);
8190 }
8191
8192 /*
8193 * Handle completion of block free via truncate. This allows fs_pending
8194 * to track the actual free block count more closely than if we only updated
8195 * it at the end. We must be careful to handle cases where the block count
8196 * on free was incorrect.
8197 */
8198 static void
freeblks_free(struct ufsmount * ump,struct freeblks * freeblks,int blocks)8199 freeblks_free(struct ufsmount *ump,
8200 struct freeblks *freeblks,
8201 int blocks)
8202 {
8203 struct fs *fs;
8204 ufs2_daddr_t remain;
8205
8206 UFS_LOCK(ump);
8207 remain = -freeblks->fb_chkcnt;
8208 freeblks->fb_chkcnt += blocks;
8209 if (remain > 0) {
8210 if (remain < blocks)
8211 blocks = remain;
8212 fs = ump->um_fs;
8213 fs->fs_pendingblocks -= blocks;
8214 }
8215 UFS_UNLOCK(ump);
8216 }
8217
8218 /*
8219 * Once all of the freework workitems are complete we can retire the
8220 * freeblocks dependency and any journal work awaiting completion. This
8221 * can not be called until all other dependencies are stable on disk.
8222 */
8223 static int
handle_complete_freeblocks(struct freeblks * freeblks,int flags)8224 handle_complete_freeblocks(struct freeblks *freeblks, int flags)
8225 {
8226 struct inodedep *inodedep;
8227 struct inode *ip;
8228 struct vnode *vp;
8229 struct fs *fs;
8230 struct ufsmount *ump;
8231 ufs2_daddr_t spare;
8232
8233 ump = VFSTOUFS(freeblks->fb_list.wk_mp);
8234 fs = ump->um_fs;
8235 flags = LK_EXCLUSIVE | flags;
8236 spare = freeblks->fb_chkcnt;
8237
8238 /*
8239 * If we did not release the expected number of blocks we may have
8240 * to adjust the inode block count here. Only do so if it wasn't
8241 * a truncation to zero and the modrev still matches.
8242 */
8243 if (spare && freeblks->fb_len != 0) {
8244 if (ffs_vgetf(freeblks->fb_list.wk_mp, freeblks->fb_inum,
8245 flags, &vp, FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP) != 0)
8246 return (EBUSY);
8247 ip = VTOI(vp);
8248 if (ip->i_mode == 0) {
8249 vgone(vp);
8250 } else if (DIP(ip, i_modrev) == freeblks->fb_modrev) {
8251 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - spare);
8252 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
8253 /*
8254 * We must wait so this happens before the
8255 * journal is reclaimed.
8256 */
8257 ffs_update(vp, 1);
8258 }
8259 vput(vp);
8260 }
8261 if (spare < 0) {
8262 UFS_LOCK(ump);
8263 fs->fs_pendingblocks += spare;
8264 UFS_UNLOCK(ump);
8265 }
8266 #ifdef QUOTA
8267 /* Handle spare. */
8268 if (spare)
8269 quotaadj(freeblks->fb_quota, ump, -spare);
8270 quotarele(freeblks->fb_quota);
8271 #endif
8272 ACQUIRE_LOCK(ump);
8273 if (freeblks->fb_state & ONDEPLIST) {
8274 inodedep_lookup(freeblks->fb_list.wk_mp, freeblks->fb_inum,
8275 0, &inodedep);
8276 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks, fb_next);
8277 freeblks->fb_state &= ~ONDEPLIST;
8278 if (TAILQ_EMPTY(&inodedep->id_freeblklst))
8279 free_inodedep(inodedep);
8280 }
8281 /*
8282 * All of the freeblock deps must be complete prior to this call
8283 * so it's now safe to complete earlier outstanding journal entries.
8284 */
8285 handle_jwork(&freeblks->fb_jwork);
8286 WORKITEM_FREE(freeblks, D_FREEBLKS);
8287 FREE_LOCK(ump);
8288 return (0);
8289 }
8290
8291 /*
8292 * Release blocks associated with the freeblks and stored in the indirect
8293 * block dbn. If level is greater than SINGLE, the block is an indirect block
8294 * and recursive calls to indirtrunc must be used to cleanse other indirect
8295 * blocks.
8296 *
8297 * This handles partial and complete truncation of blocks. Partial is noted
8298 * with goingaway == 0. In this case the freework is completed after the
8299 * zero'd indirects are written to disk. For full truncation the freework
8300 * is completed after the block is freed.
8301 */
8302 static void
indir_trunc(struct freework * freework,ufs2_daddr_t dbn,ufs_lbn_t lbn)8303 indir_trunc(struct freework *freework,
8304 ufs2_daddr_t dbn,
8305 ufs_lbn_t lbn)
8306 {
8307 struct freework *nfreework;
8308 struct workhead wkhd;
8309 struct freeblks *freeblks;
8310 struct buf *bp;
8311 struct fs *fs;
8312 struct indirdep *indirdep;
8313 struct mount *mp;
8314 struct ufsmount *ump;
8315 ufs1_daddr_t *bap1;
8316 ufs2_daddr_t nb, nnb, *bap2;
8317 ufs_lbn_t lbnadd, nlbn;
8318 uint64_t key;
8319 int nblocks, ufs1fmt, freedblocks;
8320 int goingaway, freedeps, needj, level, cnt, i, error;
8321
8322 freeblks = freework->fw_freeblks;
8323 mp = freeblks->fb_list.wk_mp;
8324 ump = VFSTOUFS(mp);
8325 fs = ump->um_fs;
8326 /*
8327 * Get buffer of block pointers to be freed. There are three cases:
8328 *
8329 * 1) Partial truncate caches the indirdep pointer in the freework
8330 * which provides us a back copy to the save bp which holds the
8331 * pointers we want to clear. When this completes the zero
8332 * pointers are written to the real copy.
8333 * 2) The indirect is being completely truncated, cancel_indirdep()
8334 * eliminated the real copy and placed the indirdep on the saved
8335 * copy. The indirdep and buf are discarded when this completes.
8336 * 3) The indirect was not in memory, we read a copy off of the disk
8337 * using the devvp and drop and invalidate the buffer when we're
8338 * done.
8339 */
8340 goingaway = 1;
8341 indirdep = NULL;
8342 if (freework->fw_indir != NULL) {
8343 goingaway = 0;
8344 indirdep = freework->fw_indir;
8345 bp = indirdep->ir_savebp;
8346 if (bp == NULL || bp->b_blkno != dbn)
8347 panic("indir_trunc: Bad saved buf %p blkno %jd",
8348 bp, (intmax_t)dbn);
8349 } else if ((bp = incore(&freeblks->fb_devvp->v_bufobj, dbn)) != NULL) {
8350 /*
8351 * The lock prevents the buf dep list from changing and
8352 * indirects on devvp should only ever have one dependency.
8353 */
8354 indirdep = WK_INDIRDEP(LIST_FIRST(&bp->b_dep));
8355 if (indirdep == NULL || (indirdep->ir_state & GOINGAWAY) == 0)
8356 panic("indir_trunc: Bad indirdep %p from buf %p",
8357 indirdep, bp);
8358 } else {
8359 error = ffs_breadz(ump, freeblks->fb_devvp, dbn, dbn,
8360 (int)fs->fs_bsize, NULL, NULL, 0, NOCRED, 0, NULL, &bp);
8361 if (error)
8362 return;
8363 }
8364 ACQUIRE_LOCK(ump);
8365 /* Protects against a race with complete_trunc_indir(). */
8366 freework->fw_state &= ~INPROGRESS;
8367 /*
8368 * If we have an indirdep we need to enforce the truncation order
8369 * and discard it when it is complete.
8370 */
8371 if (indirdep) {
8372 if (freework != TAILQ_FIRST(&indirdep->ir_trunc) &&
8373 !TAILQ_EMPTY(&indirdep->ir_trunc)) {
8374 /*
8375 * Add the complete truncate to the list on the
8376 * indirdep to enforce in-order processing.
8377 */
8378 if (freework->fw_indir == NULL)
8379 TAILQ_INSERT_TAIL(&indirdep->ir_trunc,
8380 freework, fw_next);
8381 FREE_LOCK(ump);
8382 return;
8383 }
8384 /*
8385 * If we're goingaway, free the indirdep. Otherwise it will
8386 * linger until the write completes.
8387 */
8388 if (goingaway) {
8389 KASSERT(indirdep->ir_savebp == bp,
8390 ("indir_trunc: losing ir_savebp %p",
8391 indirdep->ir_savebp));
8392 indirdep->ir_savebp = NULL;
8393 free_indirdep(indirdep);
8394 }
8395 }
8396 FREE_LOCK(ump);
8397 /* Initialize pointers depending on block size. */
8398 if (ump->um_fstype == UFS1) {
8399 bap1 = (ufs1_daddr_t *)bp->b_data;
8400 nb = bap1[freework->fw_off];
8401 ufs1fmt = 1;
8402 bap2 = NULL;
8403 } else {
8404 bap2 = (ufs2_daddr_t *)bp->b_data;
8405 nb = bap2[freework->fw_off];
8406 ufs1fmt = 0;
8407 bap1 = NULL;
8408 }
8409 level = lbn_level(lbn);
8410 needj = MOUNTEDSUJ(UFSTOVFS(ump)) != 0;
8411 lbnadd = lbn_offset(fs, level);
8412 nblocks = btodb(fs->fs_bsize);
8413 nfreework = freework;
8414 freedeps = 0;
8415 cnt = 0;
8416 /*
8417 * Reclaim blocks. Traverses into nested indirect levels and
8418 * arranges for the current level to be freed when subordinates
8419 * are free when journaling.
8420 */
8421 key = ffs_blkrelease_start(ump, freeblks->fb_devvp, freeblks->fb_inum);
8422 for (i = freework->fw_off; i < NINDIR(fs); i++, nb = nnb) {
8423 if (UFS_CHECK_BLKNO(mp, freeblks->fb_inum, nb,
8424 fs->fs_bsize) != 0)
8425 nb = 0;
8426 if (i != NINDIR(fs) - 1) {
8427 if (ufs1fmt)
8428 nnb = bap1[i+1];
8429 else
8430 nnb = bap2[i+1];
8431 } else
8432 nnb = 0;
8433 if (nb == 0)
8434 continue;
8435 cnt++;
8436 if (level != 0) {
8437 nlbn = (lbn + 1) - (i * lbnadd);
8438 if (needj != 0) {
8439 nfreework = newfreework(ump, freeblks, freework,
8440 nlbn, nb, fs->fs_frag, 0, 0);
8441 freedeps++;
8442 }
8443 indir_trunc(nfreework, fsbtodb(fs, nb), nlbn);
8444 } else {
8445 struct freedep *freedep;
8446
8447 /*
8448 * Attempt to aggregate freedep dependencies for
8449 * all blocks being released to the same CG.
8450 */
8451 LIST_INIT(&wkhd);
8452 if (needj != 0 &&
8453 (nnb == 0 || (dtog(fs, nb) != dtog(fs, nnb)))) {
8454 freedep = newfreedep(freework);
8455 WORKLIST_INSERT_UNLOCKED(&wkhd,
8456 &freedep->fd_list);
8457 freedeps++;
8458 }
8459 CTR3(KTR_SUJ,
8460 "indir_trunc: ino %jd blkno %jd size %d",
8461 freeblks->fb_inum, nb, fs->fs_bsize);
8462 ffs_blkfree(ump, fs, freeblks->fb_devvp, nb,
8463 fs->fs_bsize, freeblks->fb_inum,
8464 freeblks->fb_vtype, &wkhd, key);
8465 }
8466 }
8467 ffs_blkrelease_finish(ump, key);
8468 if (goingaway) {
8469 bp->b_flags |= B_INVAL | B_NOCACHE;
8470 brelse(bp);
8471 }
8472 freedblocks = 0;
8473 if (level == 0)
8474 freedblocks = (nblocks * cnt);
8475 if (needj == 0)
8476 freedblocks += nblocks;
8477 freeblks_free(ump, freeblks, freedblocks);
8478 /*
8479 * If we are journaling set up the ref counts and offset so this
8480 * indirect can be completed when its children are free.
8481 */
8482 if (needj) {
8483 ACQUIRE_LOCK(ump);
8484 freework->fw_off = i;
8485 freework->fw_ref += freedeps;
8486 freework->fw_ref -= NINDIR(fs) + 1;
8487 if (level == 0)
8488 freeblks->fb_cgwait += freedeps;
8489 if (freework->fw_ref == 0)
8490 freework_freeblock(freework, SINGLETON_KEY);
8491 FREE_LOCK(ump);
8492 return;
8493 }
8494 /*
8495 * If we're not journaling we can free the indirect now.
8496 */
8497 dbn = dbtofsb(fs, dbn);
8498 CTR3(KTR_SUJ,
8499 "indir_trunc 2: ino %jd blkno %jd size %d",
8500 freeblks->fb_inum, dbn, fs->fs_bsize);
8501 ffs_blkfree(ump, fs, freeblks->fb_devvp, dbn, fs->fs_bsize,
8502 freeblks->fb_inum, freeblks->fb_vtype, NULL, SINGLETON_KEY);
8503 /* Non SUJ softdep does single-threaded truncations. */
8504 if (freework->fw_blkno == dbn) {
8505 freework->fw_state |= ALLCOMPLETE;
8506 ACQUIRE_LOCK(ump);
8507 handle_written_freework(freework);
8508 FREE_LOCK(ump);
8509 }
8510 return;
8511 }
8512
8513 /*
8514 * Cancel an allocindir when it is removed via truncation. When bp is not
8515 * NULL the indirect never appeared on disk and is scheduled to be freed
8516 * independently of the indir so we can more easily track journal work.
8517 */
8518 static void
cancel_allocindir(struct allocindir * aip,struct buf * bp,struct freeblks * freeblks,int trunc)8519 cancel_allocindir(
8520 struct allocindir *aip,
8521 struct buf *bp,
8522 struct freeblks *freeblks,
8523 int trunc)
8524 {
8525 struct indirdep *indirdep;
8526 struct freefrag *freefrag;
8527 struct newblk *newblk;
8528
8529 newblk = (struct newblk *)aip;
8530 LIST_REMOVE(aip, ai_next);
8531 /*
8532 * We must eliminate the pointer in bp if it must be freed on its
8533 * own due to partial truncate or pending journal work.
8534 */
8535 if (bp && (trunc || newblk->nb_jnewblk)) {
8536 /*
8537 * Clear the pointer and mark the aip to be freed
8538 * directly if it never existed on disk.
8539 */
8540 aip->ai_state |= DELAYEDFREE;
8541 indirdep = aip->ai_indirdep;
8542 if (indirdep->ir_state & UFS1FMT)
8543 ((ufs1_daddr_t *)bp->b_data)[aip->ai_offset] = 0;
8544 else
8545 ((ufs2_daddr_t *)bp->b_data)[aip->ai_offset] = 0;
8546 }
8547 /*
8548 * When truncating the previous pointer will be freed via
8549 * savedbp. Eliminate the freefrag which would dup free.
8550 */
8551 if (trunc && (freefrag = newblk->nb_freefrag) != NULL) {
8552 newblk->nb_freefrag = NULL;
8553 if (freefrag->ff_jdep)
8554 cancel_jfreefrag(
8555 WK_JFREEFRAG(freefrag->ff_jdep));
8556 jwork_move(&freeblks->fb_jwork, &freefrag->ff_jwork);
8557 WORKITEM_FREE(freefrag, D_FREEFRAG);
8558 }
8559 /*
8560 * If the journal hasn't been written the jnewblk must be passed
8561 * to the call to ffs_blkfree that reclaims the space. We accomplish
8562 * this by leaving the journal dependency on the newblk to be freed
8563 * when a freework is created in handle_workitem_freeblocks().
8564 */
8565 cancel_newblk(newblk, NULL, &freeblks->fb_jwork);
8566 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list);
8567 }
8568
8569 /*
8570 * Create the mkdir dependencies for . and .. in a new directory. Link them
8571 * in to a newdirblk so any subsequent additions are tracked properly. The
8572 * caller is responsible for adding the mkdir1 dependency to the journal
8573 * and updating id_mkdiradd. This function returns with the per-filesystem
8574 * lock held.
8575 */
8576 static struct mkdir *
setup_newdir(struct diradd * dap,ino_t newinum,ino_t dinum,struct buf * newdirbp,struct mkdir ** mkdirp)8577 setup_newdir(
8578 struct diradd *dap,
8579 ino_t newinum,
8580 ino_t dinum,
8581 struct buf *newdirbp,
8582 struct mkdir **mkdirp)
8583 {
8584 struct newblk *newblk;
8585 struct pagedep *pagedep;
8586 struct inodedep *inodedep;
8587 struct newdirblk *newdirblk;
8588 struct mkdir *mkdir1, *mkdir2;
8589 struct worklist *wk;
8590 struct jaddref *jaddref;
8591 struct ufsmount *ump;
8592 struct mount *mp;
8593
8594 mp = dap->da_list.wk_mp;
8595 ump = VFSTOUFS(mp);
8596 newdirblk = malloc(sizeof(struct newdirblk), M_NEWDIRBLK,
8597 M_SOFTDEP_FLAGS);
8598 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp);
8599 LIST_INIT(&newdirblk->db_mkdir);
8600 mkdir1 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS);
8601 workitem_alloc(&mkdir1->md_list, D_MKDIR, mp);
8602 mkdir1->md_state = ATTACHED | MKDIR_BODY;
8603 mkdir1->md_diradd = dap;
8604 mkdir1->md_jaddref = NULL;
8605 mkdir2 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS);
8606 workitem_alloc(&mkdir2->md_list, D_MKDIR, mp);
8607 mkdir2->md_state = ATTACHED | MKDIR_PARENT;
8608 mkdir2->md_diradd = dap;
8609 mkdir2->md_jaddref = NULL;
8610 if (MOUNTEDSUJ(mp) == 0) {
8611 mkdir1->md_state |= DEPCOMPLETE;
8612 mkdir2->md_state |= DEPCOMPLETE;
8613 }
8614 /*
8615 * Dependency on "." and ".." being written to disk.
8616 */
8617 mkdir1->md_buf = newdirbp;
8618 ACQUIRE_LOCK(VFSTOUFS(mp));
8619 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir1, md_mkdirs);
8620 /*
8621 * We must link the pagedep, allocdirect, and newdirblk for
8622 * the initial file page so the pointer to the new directory
8623 * is not written until the directory contents are live and
8624 * any subsequent additions are not marked live until the
8625 * block is reachable via the inode.
8626 */
8627 if (pagedep_lookup(mp, newdirbp, newinum, 0, 0, &pagedep) == 0)
8628 panic("setup_newdir: lost pagedep");
8629 LIST_FOREACH(wk, &newdirbp->b_dep, wk_list)
8630 if (wk->wk_type == D_ALLOCDIRECT)
8631 break;
8632 if (wk == NULL)
8633 panic("setup_newdir: lost allocdirect");
8634 if (pagedep->pd_state & NEWBLOCK)
8635 panic("setup_newdir: NEWBLOCK already set");
8636 newblk = WK_NEWBLK(wk);
8637 pagedep->pd_state |= NEWBLOCK;
8638 pagedep->pd_newdirblk = newdirblk;
8639 newdirblk->db_pagedep = pagedep;
8640 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list);
8641 WORKLIST_INSERT(&newdirblk->db_mkdir, &mkdir1->md_list);
8642 /*
8643 * Look up the inodedep for the parent directory so that we
8644 * can link mkdir2 into the pending dotdot jaddref or
8645 * the inode write if there is none. If the inode is
8646 * ALLCOMPLETE and no jaddref is present all dependencies have
8647 * been satisfied and mkdir2 can be freed.
8648 */
8649 inodedep_lookup(mp, dinum, 0, &inodedep);
8650 if (MOUNTEDSUJ(mp)) {
8651 if (inodedep == NULL)
8652 panic("setup_newdir: Lost parent.");
8653 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
8654 inoreflst);
8655 KASSERT(jaddref != NULL && jaddref->ja_parent == newinum &&
8656 (jaddref->ja_state & MKDIR_PARENT),
8657 ("setup_newdir: bad dotdot jaddref %p", jaddref));
8658 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs);
8659 mkdir2->md_jaddref = jaddref;
8660 jaddref->ja_mkdir = mkdir2;
8661 } else if (inodedep == NULL ||
8662 (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) {
8663 dap->da_state &= ~MKDIR_PARENT;
8664 WORKITEM_FREE(mkdir2, D_MKDIR);
8665 mkdir2 = NULL;
8666 } else {
8667 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs);
8668 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir2->md_list);
8669 }
8670 *mkdirp = mkdir2;
8671
8672 return (mkdir1);
8673 }
8674
8675 /*
8676 * Directory entry addition dependencies.
8677 *
8678 * When adding a new directory entry, the inode (with its incremented link
8679 * count) must be written to disk before the directory entry's pointer to it.
8680 * Also, if the inode is newly allocated, the corresponding freemap must be
8681 * updated (on disk) before the directory entry's pointer. These requirements
8682 * are met via undo/redo on the directory entry's pointer, which consists
8683 * simply of the inode number.
8684 *
8685 * As directory entries are added and deleted, the free space within a
8686 * directory block can become fragmented. The ufs filesystem will compact
8687 * a fragmented directory block to make space for a new entry. When this
8688 * occurs, the offsets of previously added entries change. Any "diradd"
8689 * dependency structures corresponding to these entries must be updated with
8690 * the new offsets.
8691 */
8692
8693 /*
8694 * This routine is called after the in-memory inode's link
8695 * count has been incremented, but before the directory entry's
8696 * pointer to the inode has been set.
8697 */
8698 int
softdep_setup_directory_add(struct buf * bp,struct inode * dp,off_t diroffset,ino_t newinum,struct buf * newdirbp,int isnewblk)8699 softdep_setup_directory_add(
8700 struct buf *bp, /* buffer containing directory block */
8701 struct inode *dp, /* inode for directory */
8702 off_t diroffset, /* offset of new entry in directory */
8703 ino_t newinum, /* inode referenced by new directory entry */
8704 struct buf *newdirbp, /* non-NULL => contents of new mkdir */
8705 int isnewblk) /* entry is in a newly allocated block */
8706 {
8707 int offset; /* offset of new entry within directory block */
8708 ufs_lbn_t lbn; /* block in directory containing new entry */
8709 struct fs *fs;
8710 struct diradd *dap;
8711 struct newblk *newblk;
8712 struct pagedep *pagedep;
8713 struct inodedep *inodedep;
8714 struct newdirblk *newdirblk;
8715 struct mkdir *mkdir1, *mkdir2;
8716 struct jaddref *jaddref;
8717 struct ufsmount *ump;
8718 struct mount *mp;
8719 int isindir;
8720
8721 mp = ITOVFS(dp);
8722 ump = VFSTOUFS(mp);
8723 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
8724 ("softdep_setup_directory_add called on non-softdep filesystem"));
8725 /*
8726 * Whiteouts have no dependencies.
8727 */
8728 if (newinum == UFS_WINO) {
8729 if (newdirbp != NULL)
8730 bdwrite(newdirbp);
8731 return (0);
8732 }
8733 jaddref = NULL;
8734 mkdir1 = mkdir2 = NULL;
8735 fs = ump->um_fs;
8736 lbn = lblkno(fs, diroffset);
8737 offset = blkoff(fs, diroffset);
8738 dap = malloc(sizeof(struct diradd), M_DIRADD,
8739 M_SOFTDEP_FLAGS|M_ZERO);
8740 workitem_alloc(&dap->da_list, D_DIRADD, mp);
8741 dap->da_offset = offset;
8742 dap->da_newinum = newinum;
8743 dap->da_state = ATTACHED;
8744 LIST_INIT(&dap->da_jwork);
8745 isindir = bp->b_lblkno >= UFS_NDADDR;
8746 newdirblk = NULL;
8747 if (isnewblk &&
8748 (isindir ? blkoff(fs, diroffset) : fragoff(fs, diroffset)) == 0) {
8749 newdirblk = malloc(sizeof(struct newdirblk),
8750 M_NEWDIRBLK, M_SOFTDEP_FLAGS);
8751 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp);
8752 LIST_INIT(&newdirblk->db_mkdir);
8753 }
8754 /*
8755 * If we're creating a new directory setup the dependencies and set
8756 * the dap state to wait for them. Otherwise it's COMPLETE and
8757 * we can move on.
8758 */
8759 if (newdirbp == NULL) {
8760 dap->da_state |= DEPCOMPLETE;
8761 ACQUIRE_LOCK(ump);
8762 } else {
8763 dap->da_state |= MKDIR_BODY | MKDIR_PARENT;
8764 mkdir1 = setup_newdir(dap, newinum, dp->i_number, newdirbp,
8765 &mkdir2);
8766 }
8767 /*
8768 * Link into parent directory pagedep to await its being written.
8769 */
8770 pagedep_lookup(mp, bp, dp->i_number, lbn, DEPALLOC, &pagedep);
8771 #ifdef INVARIANTS
8772 if (diradd_lookup(pagedep, offset) != NULL)
8773 panic("softdep_setup_directory_add: %p already at off %d\n",
8774 diradd_lookup(pagedep, offset), offset);
8775 #endif
8776 dap->da_pagedep = pagedep;
8777 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], dap,
8778 da_pdlist);
8779 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep);
8780 /*
8781 * If we're journaling, link the diradd into the jaddref so it
8782 * may be completed after the journal entry is written. Otherwise,
8783 * link the diradd into its inodedep. If the inode is not yet
8784 * written place it on the bufwait list, otherwise do the post-inode
8785 * write processing to put it on the id_pendinghd list.
8786 */
8787 if (MOUNTEDSUJ(mp)) {
8788 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
8789 inoreflst);
8790 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number,
8791 ("softdep_setup_directory_add: bad jaddref %p", jaddref));
8792 jaddref->ja_diroff = diroffset;
8793 jaddref->ja_diradd = dap;
8794 add_to_journal(&jaddref->ja_list);
8795 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE)
8796 diradd_inode_written(dap, inodedep);
8797 else
8798 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list);
8799 /*
8800 * Add the journal entries for . and .. links now that the primary
8801 * link is written.
8802 */
8803 if (mkdir1 != NULL && MOUNTEDSUJ(mp)) {
8804 jaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref,
8805 inoreflst, if_deps);
8806 KASSERT(jaddref != NULL &&
8807 jaddref->ja_ino == jaddref->ja_parent &&
8808 (jaddref->ja_state & MKDIR_BODY),
8809 ("softdep_setup_directory_add: bad dot jaddref %p",
8810 jaddref));
8811 mkdir1->md_jaddref = jaddref;
8812 jaddref->ja_mkdir = mkdir1;
8813 /*
8814 * It is important that the dotdot journal entry
8815 * is added prior to the dot entry since dot writes
8816 * both the dot and dotdot links. These both must
8817 * be added after the primary link for the journal
8818 * to remain consistent.
8819 */
8820 add_to_journal(&mkdir2->md_jaddref->ja_list);
8821 add_to_journal(&jaddref->ja_list);
8822 }
8823 /*
8824 * If we are adding a new directory remember this diradd so that if
8825 * we rename it we can keep the dot and dotdot dependencies. If
8826 * we are adding a new name for an inode that has a mkdiradd we
8827 * must be in rename and we have to move the dot and dotdot
8828 * dependencies to this new name. The old name is being orphaned
8829 * soon.
8830 */
8831 if (mkdir1 != NULL) {
8832 if (inodedep->id_mkdiradd != NULL)
8833 panic("softdep_setup_directory_add: Existing mkdir");
8834 inodedep->id_mkdiradd = dap;
8835 } else if (inodedep->id_mkdiradd)
8836 merge_diradd(inodedep, dap);
8837 if (newdirblk != NULL) {
8838 /*
8839 * There is nothing to do if we are already tracking
8840 * this block.
8841 */
8842 if ((pagedep->pd_state & NEWBLOCK) != 0) {
8843 WORKITEM_FREE(newdirblk, D_NEWDIRBLK);
8844 FREE_LOCK(ump);
8845 return (0);
8846 }
8847 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk)
8848 == 0)
8849 panic("softdep_setup_directory_add: lost entry");
8850 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list);
8851 pagedep->pd_state |= NEWBLOCK;
8852 pagedep->pd_newdirblk = newdirblk;
8853 newdirblk->db_pagedep = pagedep;
8854 FREE_LOCK(ump);
8855 /*
8856 * If we extended into an indirect signal direnter to sync.
8857 */
8858 if (isindir)
8859 return (1);
8860 return (0);
8861 }
8862 FREE_LOCK(ump);
8863 return (0);
8864 }
8865
8866 /*
8867 * This procedure is called to change the offset of a directory
8868 * entry when compacting a directory block which must be owned
8869 * exclusively by the caller. Note that the actual entry movement
8870 * must be done in this procedure to ensure that no I/O completions
8871 * occur while the move is in progress.
8872 */
8873 void
softdep_change_directoryentry_offset(struct buf * bp,struct inode * dp,caddr_t base,caddr_t oldloc,caddr_t newloc,int entrysize)8874 softdep_change_directoryentry_offset(
8875 struct buf *bp, /* Buffer holding directory block. */
8876 struct inode *dp, /* inode for directory */
8877 caddr_t base, /* address of dp->i_offset */
8878 caddr_t oldloc, /* address of old directory location */
8879 caddr_t newloc, /* address of new directory location */
8880 int entrysize) /* size of directory entry */
8881 {
8882 int offset, oldoffset, newoffset;
8883 struct pagedep *pagedep;
8884 struct jmvref *jmvref;
8885 struct diradd *dap;
8886 struct direct *de;
8887 struct mount *mp;
8888 struct ufsmount *ump;
8889 ufs_lbn_t lbn;
8890 int flags;
8891
8892 mp = ITOVFS(dp);
8893 ump = VFSTOUFS(mp);
8894 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
8895 ("softdep_change_directoryentry_offset called on "
8896 "non-softdep filesystem"));
8897 de = (struct direct *)oldloc;
8898 jmvref = NULL;
8899 flags = 0;
8900 /*
8901 * Moves are always journaled as it would be too complex to
8902 * determine if any affected adds or removes are present in the
8903 * journal.
8904 */
8905 if (MOUNTEDSUJ(mp)) {
8906 flags = DEPALLOC;
8907 jmvref = newjmvref(dp, de->d_ino,
8908 I_OFFSET(dp) + (oldloc - base),
8909 I_OFFSET(dp) + (newloc - base));
8910 }
8911 lbn = lblkno(ump->um_fs, I_OFFSET(dp));
8912 offset = blkoff(ump->um_fs, I_OFFSET(dp));
8913 oldoffset = offset + (oldloc - base);
8914 newoffset = offset + (newloc - base);
8915 ACQUIRE_LOCK(ump);
8916 if (pagedep_lookup(mp, bp, dp->i_number, lbn, flags, &pagedep) == 0)
8917 goto done;
8918 dap = diradd_lookup(pagedep, oldoffset);
8919 if (dap) {
8920 dap->da_offset = newoffset;
8921 newoffset = DIRADDHASH(newoffset);
8922 oldoffset = DIRADDHASH(oldoffset);
8923 if ((dap->da_state & ALLCOMPLETE) != ALLCOMPLETE &&
8924 newoffset != oldoffset) {
8925 LIST_REMOVE(dap, da_pdlist);
8926 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[newoffset],
8927 dap, da_pdlist);
8928 }
8929 }
8930 done:
8931 if (jmvref) {
8932 jmvref->jm_pagedep = pagedep;
8933 LIST_INSERT_HEAD(&pagedep->pd_jmvrefhd, jmvref, jm_deps);
8934 add_to_journal(&jmvref->jm_list);
8935 }
8936 bcopy(oldloc, newloc, entrysize);
8937 FREE_LOCK(ump);
8938 }
8939
8940 /*
8941 * Move the mkdir dependencies and journal work from one diradd to another
8942 * when renaming a directory. The new name must depend on the mkdir deps
8943 * completing as the old name did. Directories can only have one valid link
8944 * at a time so one must be canonical.
8945 */
8946 static void
merge_diradd(struct inodedep * inodedep,struct diradd * newdap)8947 merge_diradd(struct inodedep *inodedep, struct diradd *newdap)
8948 {
8949 struct diradd *olddap;
8950 struct mkdir *mkdir, *nextmd;
8951 struct ufsmount *ump;
8952 short state;
8953
8954 olddap = inodedep->id_mkdiradd;
8955 inodedep->id_mkdiradd = newdap;
8956 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
8957 newdap->da_state &= ~DEPCOMPLETE;
8958 ump = VFSTOUFS(inodedep->id_list.wk_mp);
8959 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir;
8960 mkdir = nextmd) {
8961 nextmd = LIST_NEXT(mkdir, md_mkdirs);
8962 if (mkdir->md_diradd != olddap)
8963 continue;
8964 mkdir->md_diradd = newdap;
8965 state = mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY);
8966 newdap->da_state |= state;
8967 olddap->da_state &= ~state;
8968 if ((olddap->da_state &
8969 (MKDIR_PARENT | MKDIR_BODY)) == 0)
8970 break;
8971 }
8972 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0)
8973 panic("merge_diradd: unfound ref");
8974 }
8975 /*
8976 * Any mkdir related journal items are not safe to be freed until
8977 * the new name is stable.
8978 */
8979 jwork_move(&newdap->da_jwork, &olddap->da_jwork);
8980 olddap->da_state |= DEPCOMPLETE;
8981 complete_diradd(olddap);
8982 }
8983
8984 /*
8985 * Move the diradd to the pending list when all diradd dependencies are
8986 * complete.
8987 */
8988 static void
complete_diradd(struct diradd * dap)8989 complete_diradd(struct diradd *dap)
8990 {
8991 struct pagedep *pagedep;
8992
8993 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
8994 if (dap->da_state & DIRCHG)
8995 pagedep = dap->da_previous->dm_pagedep;
8996 else
8997 pagedep = dap->da_pagedep;
8998 LIST_REMOVE(dap, da_pdlist);
8999 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
9000 }
9001 }
9002
9003 /*
9004 * Cancel a diradd when a dirrem overlaps with it. We must cancel the journal
9005 * add entries and conditionally journal the remove.
9006 */
9007 static void
cancel_diradd(struct diradd * dap,struct dirrem * dirrem,struct jremref * jremref,struct jremref * dotremref,struct jremref * dotdotremref)9008 cancel_diradd(
9009 struct diradd *dap,
9010 struct dirrem *dirrem,
9011 struct jremref *jremref,
9012 struct jremref *dotremref,
9013 struct jremref *dotdotremref)
9014 {
9015 struct inodedep *inodedep;
9016 struct jaddref *jaddref;
9017 struct inoref *inoref;
9018 struct ufsmount *ump;
9019 struct mkdir *mkdir;
9020
9021 /*
9022 * If no remove references were allocated we're on a non-journaled
9023 * filesystem and can skip the cancel step.
9024 */
9025 if (jremref == NULL) {
9026 free_diradd(dap, NULL);
9027 return;
9028 }
9029 /*
9030 * Cancel the primary name an free it if it does not require
9031 * journaling.
9032 */
9033 if (inodedep_lookup(dap->da_list.wk_mp, dap->da_newinum,
9034 0, &inodedep) != 0) {
9035 /* Abort the addref that reference this diradd. */
9036 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
9037 if (inoref->if_list.wk_type != D_JADDREF)
9038 continue;
9039 jaddref = (struct jaddref *)inoref;
9040 if (jaddref->ja_diradd != dap)
9041 continue;
9042 if (cancel_jaddref(jaddref, inodedep,
9043 &dirrem->dm_jwork) == 0) {
9044 free_jremref(jremref);
9045 jremref = NULL;
9046 }
9047 break;
9048 }
9049 }
9050 /*
9051 * Cancel subordinate names and free them if they do not require
9052 * journaling.
9053 */
9054 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
9055 ump = VFSTOUFS(dap->da_list.wk_mp);
9056 LIST_FOREACH(mkdir, &ump->softdep_mkdirlisthd, md_mkdirs) {
9057 if (mkdir->md_diradd != dap)
9058 continue;
9059 if ((jaddref = mkdir->md_jaddref) == NULL)
9060 continue;
9061 mkdir->md_jaddref = NULL;
9062 if (mkdir->md_state & MKDIR_PARENT) {
9063 if (cancel_jaddref(jaddref, NULL,
9064 &dirrem->dm_jwork) == 0) {
9065 free_jremref(dotdotremref);
9066 dotdotremref = NULL;
9067 }
9068 } else {
9069 if (cancel_jaddref(jaddref, inodedep,
9070 &dirrem->dm_jwork) == 0) {
9071 free_jremref(dotremref);
9072 dotremref = NULL;
9073 }
9074 }
9075 }
9076 }
9077
9078 if (jremref)
9079 journal_jremref(dirrem, jremref, inodedep);
9080 if (dotremref)
9081 journal_jremref(dirrem, dotremref, inodedep);
9082 if (dotdotremref)
9083 journal_jremref(dirrem, dotdotremref, NULL);
9084 jwork_move(&dirrem->dm_jwork, &dap->da_jwork);
9085 free_diradd(dap, &dirrem->dm_jwork);
9086 }
9087
9088 /*
9089 * Free a diradd dependency structure.
9090 */
9091 static void
free_diradd(struct diradd * dap,struct workhead * wkhd)9092 free_diradd(struct diradd *dap, struct workhead *wkhd)
9093 {
9094 struct dirrem *dirrem;
9095 struct pagedep *pagedep;
9096 struct inodedep *inodedep;
9097 struct mkdir *mkdir, *nextmd;
9098 struct ufsmount *ump;
9099
9100 ump = VFSTOUFS(dap->da_list.wk_mp);
9101 LOCK_OWNED(ump);
9102 LIST_REMOVE(dap, da_pdlist);
9103 if (dap->da_state & ONWORKLIST)
9104 WORKLIST_REMOVE(&dap->da_list);
9105 if ((dap->da_state & DIRCHG) == 0) {
9106 pagedep = dap->da_pagedep;
9107 } else {
9108 dirrem = dap->da_previous;
9109 pagedep = dirrem->dm_pagedep;
9110 dirrem->dm_dirinum = pagedep->pd_ino;
9111 dirrem->dm_state |= COMPLETE;
9112 if (LIST_EMPTY(&dirrem->dm_jremrefhd))
9113 add_to_worklist(&dirrem->dm_list, 0);
9114 }
9115 if (inodedep_lookup(pagedep->pd_list.wk_mp, dap->da_newinum,
9116 0, &inodedep) != 0)
9117 if (inodedep->id_mkdiradd == dap)
9118 inodedep->id_mkdiradd = NULL;
9119 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
9120 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir;
9121 mkdir = nextmd) {
9122 nextmd = LIST_NEXT(mkdir, md_mkdirs);
9123 if (mkdir->md_diradd != dap)
9124 continue;
9125 dap->da_state &=
9126 ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY));
9127 LIST_REMOVE(mkdir, md_mkdirs);
9128 if (mkdir->md_state & ONWORKLIST)
9129 WORKLIST_REMOVE(&mkdir->md_list);
9130 if (mkdir->md_jaddref != NULL)
9131 panic("free_diradd: Unexpected jaddref");
9132 WORKITEM_FREE(mkdir, D_MKDIR);
9133 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0)
9134 break;
9135 }
9136 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0)
9137 panic("free_diradd: unfound ref");
9138 }
9139 if (inodedep)
9140 free_inodedep(inodedep);
9141 /*
9142 * Free any journal segments waiting for the directory write.
9143 */
9144 handle_jwork(&dap->da_jwork);
9145 WORKITEM_FREE(dap, D_DIRADD);
9146 }
9147
9148 /*
9149 * Directory entry removal dependencies.
9150 *
9151 * When removing a directory entry, the entry's inode pointer must be
9152 * zero'ed on disk before the corresponding inode's link count is decremented
9153 * (possibly freeing the inode for re-use). This dependency is handled by
9154 * updating the directory entry but delaying the inode count reduction until
9155 * after the directory block has been written to disk. After this point, the
9156 * inode count can be decremented whenever it is convenient.
9157 */
9158
9159 /*
9160 * This routine should be called immediately after removing
9161 * a directory entry. The inode's link count should not be
9162 * decremented by the calling procedure -- the soft updates
9163 * code will do this task when it is safe.
9164 */
9165 void
softdep_setup_remove(struct buf * bp,struct inode * dp,struct inode * ip,int isrmdir)9166 softdep_setup_remove(
9167 struct buf *bp, /* buffer containing directory block */
9168 struct inode *dp, /* inode for the directory being modified */
9169 struct inode *ip, /* inode for directory entry being removed */
9170 int isrmdir) /* indicates if doing RMDIR */
9171 {
9172 struct dirrem *dirrem, *prevdirrem;
9173 struct inodedep *inodedep;
9174 struct ufsmount *ump;
9175 int direct;
9176
9177 ump = ITOUMP(ip);
9178 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
9179 ("softdep_setup_remove called on non-softdep filesystem"));
9180 /*
9181 * Allocate a new dirrem if appropriate and ACQUIRE_LOCK. We want
9182 * newdirrem() to setup the full directory remove which requires
9183 * isrmdir > 1.
9184 */
9185 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem);
9186 /*
9187 * Add the dirrem to the inodedep's pending remove list for quick
9188 * discovery later.
9189 */
9190 if (inodedep_lookup(UFSTOVFS(ump), ip->i_number, 0, &inodedep) == 0)
9191 panic("softdep_setup_remove: Lost inodedep.");
9192 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked"));
9193 dirrem->dm_state |= ONDEPLIST;
9194 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext);
9195
9196 /*
9197 * If the COMPLETE flag is clear, then there were no active
9198 * entries and we want to roll back to a zeroed entry until
9199 * the new inode is committed to disk. If the COMPLETE flag is
9200 * set then we have deleted an entry that never made it to
9201 * disk. If the entry we deleted resulted from a name change,
9202 * then the old name still resides on disk. We cannot delete
9203 * its inode (returned to us in prevdirrem) until the zeroed
9204 * directory entry gets to disk. The new inode has never been
9205 * referenced on the disk, so can be deleted immediately.
9206 */
9207 if ((dirrem->dm_state & COMPLETE) == 0) {
9208 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, dirrem,
9209 dm_next);
9210 FREE_LOCK(ump);
9211 } else {
9212 if (prevdirrem != NULL)
9213 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd,
9214 prevdirrem, dm_next);
9215 dirrem->dm_dirinum = dirrem->dm_pagedep->pd_ino;
9216 direct = LIST_EMPTY(&dirrem->dm_jremrefhd);
9217 FREE_LOCK(ump);
9218 if (direct)
9219 handle_workitem_remove(dirrem, 0);
9220 }
9221 }
9222
9223 /*
9224 * Check for an entry matching 'offset' on both the pd_dirraddhd list and the
9225 * pd_pendinghd list of a pagedep.
9226 */
9227 static struct diradd *
diradd_lookup(struct pagedep * pagedep,int offset)9228 diradd_lookup(struct pagedep *pagedep, int offset)
9229 {
9230 struct diradd *dap;
9231
9232 LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(offset)], da_pdlist)
9233 if (dap->da_offset == offset)
9234 return (dap);
9235 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist)
9236 if (dap->da_offset == offset)
9237 return (dap);
9238 return (NULL);
9239 }
9240
9241 /*
9242 * Search for a .. diradd dependency in a directory that is being removed.
9243 * If the directory was renamed to a new parent we have a diradd rather
9244 * than a mkdir for the .. entry. We need to cancel it now before
9245 * it is found in truncate().
9246 */
9247 static struct jremref *
cancel_diradd_dotdot(struct inode * ip,struct dirrem * dirrem,struct jremref * jremref)9248 cancel_diradd_dotdot(struct inode *ip,
9249 struct dirrem *dirrem,
9250 struct jremref *jremref)
9251 {
9252 struct pagedep *pagedep;
9253 struct diradd *dap;
9254 struct worklist *wk;
9255
9256 if (pagedep_lookup(ITOVFS(ip), NULL, ip->i_number, 0, 0, &pagedep) == 0)
9257 return (jremref);
9258 dap = diradd_lookup(pagedep, DOTDOT_OFFSET);
9259 if (dap == NULL)
9260 return (jremref);
9261 cancel_diradd(dap, dirrem, jremref, NULL, NULL);
9262 /*
9263 * Mark any journal work as belonging to the parent so it is freed
9264 * with the .. reference.
9265 */
9266 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list)
9267 wk->wk_state |= MKDIR_PARENT;
9268 return (NULL);
9269 }
9270
9271 /*
9272 * Cancel the MKDIR_PARENT mkdir component of a diradd when we're going to
9273 * replace it with a dirrem/diradd pair as a result of re-parenting a
9274 * directory. This ensures that we don't simultaneously have a mkdir and
9275 * a diradd for the same .. entry.
9276 */
9277 static struct jremref *
cancel_mkdir_dotdot(struct inode * ip,struct dirrem * dirrem,struct jremref * jremref)9278 cancel_mkdir_dotdot(struct inode *ip,
9279 struct dirrem *dirrem,
9280 struct jremref *jremref)
9281 {
9282 struct inodedep *inodedep;
9283 struct jaddref *jaddref;
9284 struct ufsmount *ump;
9285 struct mkdir *mkdir;
9286 struct diradd *dap;
9287 struct mount *mp;
9288
9289 mp = ITOVFS(ip);
9290 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0)
9291 return (jremref);
9292 dap = inodedep->id_mkdiradd;
9293 if (dap == NULL || (dap->da_state & MKDIR_PARENT) == 0)
9294 return (jremref);
9295 ump = VFSTOUFS(inodedep->id_list.wk_mp);
9296 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir;
9297 mkdir = LIST_NEXT(mkdir, md_mkdirs))
9298 if (mkdir->md_diradd == dap && mkdir->md_state & MKDIR_PARENT)
9299 break;
9300 if (mkdir == NULL)
9301 panic("cancel_mkdir_dotdot: Unable to find mkdir\n");
9302 if ((jaddref = mkdir->md_jaddref) != NULL) {
9303 mkdir->md_jaddref = NULL;
9304 jaddref->ja_state &= ~MKDIR_PARENT;
9305 if (inodedep_lookup(mp, jaddref->ja_ino, 0, &inodedep) == 0)
9306 panic("cancel_mkdir_dotdot: Lost parent inodedep");
9307 if (cancel_jaddref(jaddref, inodedep, &dirrem->dm_jwork)) {
9308 journal_jremref(dirrem, jremref, inodedep);
9309 jremref = NULL;
9310 }
9311 }
9312 if (mkdir->md_state & ONWORKLIST)
9313 WORKLIST_REMOVE(&mkdir->md_list);
9314 mkdir->md_state |= ALLCOMPLETE;
9315 complete_mkdir(mkdir);
9316 return (jremref);
9317 }
9318
9319 static void
journal_jremref(struct dirrem * dirrem,struct jremref * jremref,struct inodedep * inodedep)9320 journal_jremref(struct dirrem *dirrem,
9321 struct jremref *jremref,
9322 struct inodedep *inodedep)
9323 {
9324
9325 if (inodedep == NULL)
9326 if (inodedep_lookup(jremref->jr_list.wk_mp,
9327 jremref->jr_ref.if_ino, 0, &inodedep) == 0)
9328 panic("journal_jremref: Lost inodedep");
9329 LIST_INSERT_HEAD(&dirrem->dm_jremrefhd, jremref, jr_deps);
9330 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps);
9331 add_to_journal(&jremref->jr_list);
9332 }
9333
9334 static void
dirrem_journal(struct dirrem * dirrem,struct jremref * jremref,struct jremref * dotremref,struct jremref * dotdotremref)9335 dirrem_journal(
9336 struct dirrem *dirrem,
9337 struct jremref *jremref,
9338 struct jremref *dotremref,
9339 struct jremref *dotdotremref)
9340 {
9341 struct inodedep *inodedep;
9342
9343 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 0,
9344 &inodedep) == 0)
9345 panic("dirrem_journal: Lost inodedep");
9346 journal_jremref(dirrem, jremref, inodedep);
9347 if (dotremref)
9348 journal_jremref(dirrem, dotremref, inodedep);
9349 if (dotdotremref)
9350 journal_jremref(dirrem, dotdotremref, NULL);
9351 }
9352
9353 /*
9354 * Allocate a new dirrem if appropriate and return it along with
9355 * its associated pagedep. Called without a lock, returns with lock.
9356 */
9357 static struct dirrem *
newdirrem(struct buf * bp,struct inode * dp,struct inode * ip,int isrmdir,struct dirrem ** prevdirremp)9358 newdirrem(
9359 struct buf *bp, /* buffer containing directory block */
9360 struct inode *dp, /* inode for the directory being modified */
9361 struct inode *ip, /* inode for directory entry being removed */
9362 int isrmdir, /* indicates if doing RMDIR */
9363 struct dirrem **prevdirremp) /* previously referenced inode, if any */
9364 {
9365 int offset;
9366 ufs_lbn_t lbn;
9367 struct diradd *dap;
9368 struct dirrem *dirrem;
9369 struct pagedep *pagedep;
9370 struct jremref *jremref;
9371 struct jremref *dotremref;
9372 struct jremref *dotdotremref;
9373 struct vnode *dvp;
9374 struct ufsmount *ump;
9375
9376 /*
9377 * Whiteouts have no deletion dependencies.
9378 */
9379 if (ip == NULL)
9380 panic("newdirrem: whiteout");
9381 dvp = ITOV(dp);
9382 ump = ITOUMP(dp);
9383
9384 /*
9385 * If the system is over its limit and our filesystem is
9386 * responsible for more than our share of that usage and
9387 * we are not a snapshot, request some inodedep cleanup.
9388 * Limiting the number of dirrem structures will also limit
9389 * the number of freefile and freeblks structures.
9390 */
9391 ACQUIRE_LOCK(ump);
9392 if (!IS_SNAPSHOT(ip) && softdep_excess_items(ump, D_DIRREM))
9393 schedule_cleanup(UFSTOVFS(ump));
9394 else
9395 FREE_LOCK(ump);
9396 dirrem = malloc(sizeof(struct dirrem), M_DIRREM, M_SOFTDEP_FLAGS |
9397 M_ZERO);
9398 workitem_alloc(&dirrem->dm_list, D_DIRREM, dvp->v_mount);
9399 LIST_INIT(&dirrem->dm_jremrefhd);
9400 LIST_INIT(&dirrem->dm_jwork);
9401 dirrem->dm_state = isrmdir ? RMDIR : 0;
9402 dirrem->dm_oldinum = ip->i_number;
9403 *prevdirremp = NULL;
9404 /*
9405 * Allocate remove reference structures to track journal write
9406 * dependencies. We will always have one for the link and
9407 * when doing directories we will always have one more for dot.
9408 * When renaming a directory we skip the dotdot link change so
9409 * this is not needed.
9410 */
9411 jremref = dotremref = dotdotremref = NULL;
9412 if (DOINGSUJ(dvp)) {
9413 if (isrmdir) {
9414 jremref = newjremref(dirrem, dp, ip, I_OFFSET(dp),
9415 ip->i_effnlink + 2);
9416 dotremref = newjremref(dirrem, ip, ip, DOT_OFFSET,
9417 ip->i_effnlink + 1);
9418 dotdotremref = newjremref(dirrem, ip, dp, DOTDOT_OFFSET,
9419 dp->i_effnlink + 1);
9420 dotdotremref->jr_state |= MKDIR_PARENT;
9421 } else
9422 jremref = newjremref(dirrem, dp, ip, I_OFFSET(dp),
9423 ip->i_effnlink + 1);
9424 }
9425 ACQUIRE_LOCK(ump);
9426 lbn = lblkno(ump->um_fs, I_OFFSET(dp));
9427 offset = blkoff(ump->um_fs, I_OFFSET(dp));
9428 pagedep_lookup(UFSTOVFS(ump), bp, dp->i_number, lbn, DEPALLOC,
9429 &pagedep);
9430 dirrem->dm_pagedep = pagedep;
9431 dirrem->dm_offset = offset;
9432 /*
9433 * If we're renaming a .. link to a new directory, cancel any
9434 * existing MKDIR_PARENT mkdir. If it has already been canceled
9435 * the jremref is preserved for any potential diradd in this
9436 * location. This can not coincide with a rmdir.
9437 */
9438 if (I_OFFSET(dp) == DOTDOT_OFFSET) {
9439 if (isrmdir)
9440 panic("newdirrem: .. directory change during remove?");
9441 jremref = cancel_mkdir_dotdot(dp, dirrem, jremref);
9442 }
9443 /*
9444 * If we're removing a directory search for the .. dependency now and
9445 * cancel it. Any pending journal work will be added to the dirrem
9446 * to be completed when the workitem remove completes.
9447 */
9448 if (isrmdir)
9449 dotdotremref = cancel_diradd_dotdot(ip, dirrem, dotdotremref);
9450 /*
9451 * Check for a diradd dependency for the same directory entry.
9452 * If present, then both dependencies become obsolete and can
9453 * be de-allocated.
9454 */
9455 dap = diradd_lookup(pagedep, offset);
9456 if (dap == NULL) {
9457 /*
9458 * Link the jremref structures into the dirrem so they are
9459 * written prior to the pagedep.
9460 */
9461 if (jremref)
9462 dirrem_journal(dirrem, jremref, dotremref,
9463 dotdotremref);
9464 return (dirrem);
9465 }
9466 /*
9467 * Must be ATTACHED at this point.
9468 */
9469 if ((dap->da_state & ATTACHED) == 0)
9470 panic("newdirrem: not ATTACHED");
9471 if (dap->da_newinum != ip->i_number)
9472 panic("newdirrem: inum %ju should be %ju",
9473 (uintmax_t)ip->i_number, (uintmax_t)dap->da_newinum);
9474 /*
9475 * If we are deleting a changed name that never made it to disk,
9476 * then return the dirrem describing the previous inode (which
9477 * represents the inode currently referenced from this entry on disk).
9478 */
9479 if ((dap->da_state & DIRCHG) != 0) {
9480 *prevdirremp = dap->da_previous;
9481 dap->da_state &= ~DIRCHG;
9482 dap->da_pagedep = pagedep;
9483 }
9484 /*
9485 * We are deleting an entry that never made it to disk.
9486 * Mark it COMPLETE so we can delete its inode immediately.
9487 */
9488 dirrem->dm_state |= COMPLETE;
9489 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref);
9490 #ifdef INVARIANTS
9491 if (isrmdir == 0) {
9492 struct worklist *wk;
9493
9494 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list)
9495 if (wk->wk_state & (MKDIR_BODY | MKDIR_PARENT))
9496 panic("bad wk %p (0x%X)\n", wk, wk->wk_state);
9497 }
9498 #endif
9499
9500 return (dirrem);
9501 }
9502
9503 /*
9504 * Directory entry change dependencies.
9505 *
9506 * Changing an existing directory entry requires that an add operation
9507 * be completed first followed by a deletion. The semantics for the addition
9508 * are identical to the description of adding a new entry above except
9509 * that the rollback is to the old inode number rather than zero. Once
9510 * the addition dependency is completed, the removal is done as described
9511 * in the removal routine above.
9512 */
9513
9514 /*
9515 * This routine should be called immediately after changing
9516 * a directory entry. The inode's link count should not be
9517 * decremented by the calling procedure -- the soft updates
9518 * code will perform this task when it is safe.
9519 */
9520 void
softdep_setup_directory_change(struct buf * bp,struct inode * dp,struct inode * ip,ino_t newinum,int isrmdir)9521 softdep_setup_directory_change(
9522 struct buf *bp, /* buffer containing directory block */
9523 struct inode *dp, /* inode for the directory being modified */
9524 struct inode *ip, /* inode for directory entry being removed */
9525 ino_t newinum, /* new inode number for changed entry */
9526 int isrmdir) /* indicates if doing RMDIR */
9527 {
9528 int offset;
9529 struct diradd *dap = NULL;
9530 struct dirrem *dirrem, *prevdirrem;
9531 struct pagedep *pagedep;
9532 struct inodedep *inodedep;
9533 struct jaddref *jaddref;
9534 struct mount *mp;
9535 struct ufsmount *ump;
9536
9537 mp = ITOVFS(dp);
9538 ump = VFSTOUFS(mp);
9539 offset = blkoff(ump->um_fs, I_OFFSET(dp));
9540 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
9541 ("softdep_setup_directory_change called on non-softdep filesystem"));
9542
9543 /*
9544 * Whiteouts do not need diradd dependencies.
9545 */
9546 if (newinum != UFS_WINO) {
9547 dap = malloc(sizeof(struct diradd),
9548 M_DIRADD, M_SOFTDEP_FLAGS|M_ZERO);
9549 workitem_alloc(&dap->da_list, D_DIRADD, mp);
9550 dap->da_state = DIRCHG | ATTACHED | DEPCOMPLETE;
9551 dap->da_offset = offset;
9552 dap->da_newinum = newinum;
9553 LIST_INIT(&dap->da_jwork);
9554 }
9555
9556 /*
9557 * Allocate a new dirrem and ACQUIRE_LOCK.
9558 */
9559 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem);
9560 pagedep = dirrem->dm_pagedep;
9561 /*
9562 * The possible values for isrmdir:
9563 * 0 - non-directory file rename
9564 * 1 - directory rename within same directory
9565 * inum - directory rename to new directory of given inode number
9566 * When renaming to a new directory, we are both deleting and
9567 * creating a new directory entry, so the link count on the new
9568 * directory should not change. Thus we do not need the followup
9569 * dirrem which is usually done in handle_workitem_remove. We set
9570 * the DIRCHG flag to tell handle_workitem_remove to skip the
9571 * followup dirrem.
9572 */
9573 if (isrmdir > 1)
9574 dirrem->dm_state |= DIRCHG;
9575
9576 /*
9577 * Whiteouts have no additional dependencies,
9578 * so just put the dirrem on the correct list.
9579 */
9580 if (newinum == UFS_WINO) {
9581 if ((dirrem->dm_state & COMPLETE) == 0) {
9582 LIST_INSERT_HEAD(&pagedep->pd_dirremhd, dirrem,
9583 dm_next);
9584 } else {
9585 dirrem->dm_dirinum = pagedep->pd_ino;
9586 if (LIST_EMPTY(&dirrem->dm_jremrefhd))
9587 add_to_worklist(&dirrem->dm_list, 0);
9588 }
9589 FREE_LOCK(ump);
9590 return;
9591 }
9592 /*
9593 * Add the dirrem to the inodedep's pending remove list for quick
9594 * discovery later. A valid nlinkdelta ensures that this lookup
9595 * will not fail.
9596 */
9597 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0)
9598 panic("softdep_setup_directory_change: Lost inodedep.");
9599 dirrem->dm_state |= ONDEPLIST;
9600 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext);
9601
9602 /*
9603 * If the COMPLETE flag is clear, then there were no active
9604 * entries and we want to roll back to the previous inode until
9605 * the new inode is committed to disk. If the COMPLETE flag is
9606 * set, then we have deleted an entry that never made it to disk.
9607 * If the entry we deleted resulted from a name change, then the old
9608 * inode reference still resides on disk. Any rollback that we do
9609 * needs to be to that old inode (returned to us in prevdirrem). If
9610 * the entry we deleted resulted from a create, then there is
9611 * no entry on the disk, so we want to roll back to zero rather
9612 * than the uncommitted inode. In either of the COMPLETE cases we
9613 * want to immediately free the unwritten and unreferenced inode.
9614 */
9615 if ((dirrem->dm_state & COMPLETE) == 0) {
9616 dap->da_previous = dirrem;
9617 } else {
9618 if (prevdirrem != NULL) {
9619 dap->da_previous = prevdirrem;
9620 } else {
9621 dap->da_state &= ~DIRCHG;
9622 dap->da_pagedep = pagedep;
9623 }
9624 dirrem->dm_dirinum = pagedep->pd_ino;
9625 if (LIST_EMPTY(&dirrem->dm_jremrefhd))
9626 add_to_worklist(&dirrem->dm_list, 0);
9627 }
9628 /*
9629 * Lookup the jaddref for this journal entry. We must finish
9630 * initializing it and make the diradd write dependent on it.
9631 * If we're not journaling, put it on the id_bufwait list if the
9632 * inode is not yet written. If it is written, do the post-inode
9633 * write processing to put it on the id_pendinghd list.
9634 */
9635 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep);
9636 if (MOUNTEDSUJ(mp)) {
9637 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
9638 inoreflst);
9639 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number,
9640 ("softdep_setup_directory_change: bad jaddref %p",
9641 jaddref));
9642 jaddref->ja_diroff = I_OFFSET(dp);
9643 jaddref->ja_diradd = dap;
9644 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)],
9645 dap, da_pdlist);
9646 add_to_journal(&jaddref->ja_list);
9647 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) {
9648 dap->da_state |= COMPLETE;
9649 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
9650 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list);
9651 } else {
9652 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)],
9653 dap, da_pdlist);
9654 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list);
9655 }
9656 /*
9657 * If we're making a new name for a directory that has not been
9658 * committed when need to move the dot and dotdot references to
9659 * this new name.
9660 */
9661 if (inodedep->id_mkdiradd && I_OFFSET(dp) != DOTDOT_OFFSET)
9662 merge_diradd(inodedep, dap);
9663 FREE_LOCK(ump);
9664 }
9665
9666 /*
9667 * Called whenever the link count on an inode is changed.
9668 * It creates an inode dependency so that the new reference(s)
9669 * to the inode cannot be committed to disk until the updated
9670 * inode has been written.
9671 */
9672 void
softdep_change_linkcnt(struct inode * ip)9673 softdep_change_linkcnt(
9674 struct inode *ip) /* the inode with the increased link count */
9675 {
9676 struct inodedep *inodedep;
9677 struct ufsmount *ump;
9678
9679 ump = ITOUMP(ip);
9680 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
9681 ("softdep_change_linkcnt called on non-softdep filesystem"));
9682 ACQUIRE_LOCK(ump);
9683 inodedep_lookup(UFSTOVFS(ump), ip->i_number, DEPALLOC, &inodedep);
9684 if (ip->i_nlink < ip->i_effnlink)
9685 panic("softdep_change_linkcnt: bad delta");
9686 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
9687 FREE_LOCK(ump);
9688 }
9689
9690 /*
9691 * Attach a sbdep dependency to the superblock buf so that we can keep
9692 * track of the head of the linked list of referenced but unlinked inodes.
9693 */
9694 void
softdep_setup_sbupdate(struct ufsmount * ump,struct fs * fs,struct buf * bp)9695 softdep_setup_sbupdate(
9696 struct ufsmount *ump,
9697 struct fs *fs,
9698 struct buf *bp)
9699 {
9700 struct sbdep *sbdep;
9701 struct worklist *wk;
9702
9703 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
9704 ("softdep_setup_sbupdate called on non-softdep filesystem"));
9705 LIST_FOREACH(wk, &bp->b_dep, wk_list)
9706 if (wk->wk_type == D_SBDEP)
9707 break;
9708 if (wk != NULL)
9709 return;
9710 sbdep = malloc(sizeof(struct sbdep), M_SBDEP, M_SOFTDEP_FLAGS);
9711 workitem_alloc(&sbdep->sb_list, D_SBDEP, UFSTOVFS(ump));
9712 sbdep->sb_fs = fs;
9713 sbdep->sb_ump = ump;
9714 ACQUIRE_LOCK(ump);
9715 WORKLIST_INSERT(&bp->b_dep, &sbdep->sb_list);
9716 FREE_LOCK(ump);
9717 }
9718
9719 /*
9720 * Return the first unlinked inodedep which is ready to be the head of the
9721 * list. The inodedep and all those after it must have valid next pointers.
9722 */
9723 static struct inodedep *
first_unlinked_inodedep(struct ufsmount * ump)9724 first_unlinked_inodedep(struct ufsmount *ump)
9725 {
9726 struct inodedep *inodedep;
9727 struct inodedep *idp;
9728
9729 LOCK_OWNED(ump);
9730 for (inodedep = TAILQ_LAST(&ump->softdep_unlinked, inodedeplst);
9731 inodedep; inodedep = idp) {
9732 if ((inodedep->id_state & UNLINKNEXT) == 0)
9733 return (NULL);
9734 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked);
9735 if (idp == NULL || (idp->id_state & UNLINKNEXT) == 0)
9736 break;
9737 if ((inodedep->id_state & UNLINKPREV) == 0)
9738 break;
9739 }
9740 return (inodedep);
9741 }
9742
9743 /*
9744 * Set the sujfree unlinked head pointer prior to writing a superblock.
9745 */
9746 static void
initiate_write_sbdep(struct sbdep * sbdep)9747 initiate_write_sbdep(struct sbdep *sbdep)
9748 {
9749 struct inodedep *inodedep;
9750 struct fs *bpfs;
9751 struct fs *fs;
9752
9753 bpfs = sbdep->sb_fs;
9754 fs = sbdep->sb_ump->um_fs;
9755 inodedep = first_unlinked_inodedep(sbdep->sb_ump);
9756 if (inodedep) {
9757 fs->fs_sujfree = inodedep->id_ino;
9758 inodedep->id_state |= UNLINKPREV;
9759 } else
9760 fs->fs_sujfree = 0;
9761 bpfs->fs_sujfree = fs->fs_sujfree;
9762 /*
9763 * Because we have made changes to the superblock, we need to
9764 * recompute its check-hash.
9765 */
9766 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs);
9767 }
9768
9769 /*
9770 * After a superblock is written determine whether it must be written again
9771 * due to a changing unlinked list head.
9772 */
9773 static int
handle_written_sbdep(struct sbdep * sbdep,struct buf * bp)9774 handle_written_sbdep(struct sbdep *sbdep, struct buf *bp)
9775 {
9776 struct inodedep *inodedep;
9777 struct fs *fs;
9778
9779 LOCK_OWNED(sbdep->sb_ump);
9780 fs = sbdep->sb_fs;
9781 /*
9782 * If the superblock doesn't match the in-memory list start over.
9783 */
9784 inodedep = first_unlinked_inodedep(sbdep->sb_ump);
9785 if ((inodedep && fs->fs_sujfree != inodedep->id_ino) ||
9786 (inodedep == NULL && fs->fs_sujfree != 0)) {
9787 bdirty(bp);
9788 return (1);
9789 }
9790 WORKITEM_FREE(sbdep, D_SBDEP);
9791 if (fs->fs_sujfree == 0)
9792 return (0);
9793 /*
9794 * Now that we have a record of this inode in stable store allow it
9795 * to be written to free up pending work. Inodes may see a lot of
9796 * write activity after they are unlinked which we must not hold up.
9797 */
9798 for (; inodedep != NULL; inodedep = TAILQ_NEXT(inodedep, id_unlinked)) {
9799 if ((inodedep->id_state & UNLINKLINKS) != UNLINKLINKS)
9800 panic("handle_written_sbdep: Bad inodedep %p (0x%X)",
9801 inodedep, inodedep->id_state);
9802 if (inodedep->id_state & UNLINKONLIST)
9803 break;
9804 inodedep->id_state |= DEPCOMPLETE | UNLINKONLIST;
9805 }
9806
9807 return (0);
9808 }
9809
9810 /*
9811 * Mark an inodedep as unlinked and insert it into the in-memory unlinked list.
9812 */
9813 static void
unlinked_inodedep(struct mount * mp,struct inodedep * inodedep)9814 unlinked_inodedep( struct mount *mp, struct inodedep *inodedep)
9815 {
9816 struct ufsmount *ump;
9817
9818 ump = VFSTOUFS(mp);
9819 LOCK_OWNED(ump);
9820 if (MOUNTEDSUJ(mp) == 0)
9821 return;
9822 ump->um_fs->fs_fmod = 1;
9823 if (inodedep->id_state & UNLINKED)
9824 panic("unlinked_inodedep: %p already unlinked\n", inodedep);
9825 inodedep->id_state |= UNLINKED;
9826 TAILQ_INSERT_HEAD(&ump->softdep_unlinked, inodedep, id_unlinked);
9827 }
9828
9829 /*
9830 * Remove an inodedep from the unlinked inodedep list. This may require
9831 * disk writes if the inode has made it that far.
9832 */
9833 static void
clear_unlinked_inodedep(struct inodedep * inodedep)9834 clear_unlinked_inodedep( struct inodedep *inodedep)
9835 {
9836 struct ufs2_dinode *dip;
9837 struct ufsmount *ump;
9838 struct inodedep *idp;
9839 struct inodedep *idn;
9840 struct fs *fs, *bpfs;
9841 struct buf *bp;
9842 daddr_t dbn;
9843 ino_t ino;
9844 ino_t nino;
9845 ino_t pino;
9846 int error;
9847
9848 ump = VFSTOUFS(inodedep->id_list.wk_mp);
9849 fs = ump->um_fs;
9850 ino = inodedep->id_ino;
9851 error = 0;
9852 for (;;) {
9853 LOCK_OWNED(ump);
9854 KASSERT((inodedep->id_state & UNLINKED) != 0,
9855 ("clear_unlinked_inodedep: inodedep %p not unlinked",
9856 inodedep));
9857 /*
9858 * If nothing has yet been written simply remove us from
9859 * the in memory list and return. This is the most common
9860 * case where handle_workitem_remove() loses the final
9861 * reference.
9862 */
9863 if ((inodedep->id_state & UNLINKLINKS) == 0)
9864 break;
9865 /*
9866 * If we have a NEXT pointer and no PREV pointer we can simply
9867 * clear NEXT's PREV and remove ourselves from the list. Be
9868 * careful not to clear PREV if the superblock points at
9869 * next as well.
9870 */
9871 idn = TAILQ_NEXT(inodedep, id_unlinked);
9872 if ((inodedep->id_state & UNLINKLINKS) == UNLINKNEXT) {
9873 if (idn && fs->fs_sujfree != idn->id_ino)
9874 idn->id_state &= ~UNLINKPREV;
9875 break;
9876 }
9877 /*
9878 * Here we have an inodedep which is actually linked into
9879 * the list. We must remove it by forcing a write to the
9880 * link before us, whether it be the superblock or an inode.
9881 * Unfortunately the list may change while we're waiting
9882 * on the buf lock for either resource so we must loop until
9883 * we lock the right one. If both the superblock and an
9884 * inode point to this inode we must clear the inode first
9885 * followed by the superblock.
9886 */
9887 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked);
9888 pino = 0;
9889 if (idp && (idp->id_state & UNLINKNEXT))
9890 pino = idp->id_ino;
9891 FREE_LOCK(ump);
9892 if (pino == 0) {
9893 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc),
9894 (int)fs->fs_sbsize, 0, 0, 0);
9895 } else {
9896 dbn = fsbtodb(fs, ino_to_fsba(fs, pino));
9897 error = ffs_breadz(ump, ump->um_devvp, dbn, dbn,
9898 (int)fs->fs_bsize, NULL, NULL, 0, NOCRED, 0, NULL,
9899 &bp);
9900 }
9901 ACQUIRE_LOCK(ump);
9902 if (error)
9903 break;
9904 /* If the list has changed restart the loop. */
9905 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked);
9906 nino = 0;
9907 if (idp && (idp->id_state & UNLINKNEXT))
9908 nino = idp->id_ino;
9909 if (nino != pino ||
9910 (inodedep->id_state & UNLINKPREV) != UNLINKPREV) {
9911 FREE_LOCK(ump);
9912 brelse(bp);
9913 ACQUIRE_LOCK(ump);
9914 continue;
9915 }
9916 nino = 0;
9917 idn = TAILQ_NEXT(inodedep, id_unlinked);
9918 if (idn)
9919 nino = idn->id_ino;
9920 /*
9921 * Remove us from the in memory list. After this we cannot
9922 * access the inodedep.
9923 */
9924 KASSERT((inodedep->id_state & UNLINKED) != 0,
9925 ("clear_unlinked_inodedep: inodedep %p not unlinked",
9926 inodedep));
9927 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST);
9928 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked);
9929 FREE_LOCK(ump);
9930 /*
9931 * The predecessor's next pointer is manually updated here
9932 * so that the NEXT flag is never cleared for an element
9933 * that is in the list.
9934 */
9935 if (pino == 0) {
9936 bcopy((caddr_t)fs, bp->b_data, (uint64_t)fs->fs_sbsize);
9937 bpfs = (struct fs *)bp->b_data;
9938 ffs_oldfscompat_write(bpfs, ump);
9939 softdep_setup_sbupdate(ump, bpfs, bp);
9940 /*
9941 * Because we may have made changes to the superblock,
9942 * we need to recompute its check-hash.
9943 */
9944 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs);
9945 } else if (fs->fs_magic == FS_UFS1_MAGIC) {
9946 ((struct ufs1_dinode *)bp->b_data +
9947 ino_to_fsbo(fs, pino))->di_freelink = nino;
9948 } else {
9949 dip = (struct ufs2_dinode *)bp->b_data +
9950 ino_to_fsbo(fs, pino);
9951 dip->di_freelink = nino;
9952 ffs_update_dinode_ckhash(fs, dip);
9953 }
9954 /*
9955 * If the bwrite fails we have no recourse to recover. The
9956 * filesystem is corrupted already.
9957 */
9958 bwrite(bp);
9959 ACQUIRE_LOCK(ump);
9960 /*
9961 * If the superblock pointer still needs to be cleared force
9962 * a write here.
9963 */
9964 if (fs->fs_sujfree == ino) {
9965 FREE_LOCK(ump);
9966 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc),
9967 (int)fs->fs_sbsize, 0, 0, 0);
9968 bcopy((caddr_t)fs, bp->b_data, (uint64_t)fs->fs_sbsize);
9969 bpfs = (struct fs *)bp->b_data;
9970 ffs_oldfscompat_write(bpfs, ump);
9971 softdep_setup_sbupdate(ump, bpfs, bp);
9972 /*
9973 * Because we may have made changes to the superblock,
9974 * we need to recompute its check-hash.
9975 */
9976 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs);
9977 bwrite(bp);
9978 ACQUIRE_LOCK(ump);
9979 }
9980
9981 if (fs->fs_sujfree != ino)
9982 return;
9983 panic("clear_unlinked_inodedep: Failed to clear free head");
9984 }
9985 if (inodedep->id_ino == fs->fs_sujfree)
9986 panic("clear_unlinked_inodedep: Freeing head of free list");
9987 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST);
9988 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked);
9989 return;
9990 }
9991
9992 /*
9993 * This workitem decrements the inode's link count.
9994 * If the link count reaches zero, the file is removed.
9995 */
9996 static int
handle_workitem_remove(struct dirrem * dirrem,int flags)9997 handle_workitem_remove(struct dirrem *dirrem, int flags)
9998 {
9999 struct inodedep *inodedep;
10000 struct workhead dotdotwk;
10001 struct worklist *wk;
10002 struct ufsmount *ump;
10003 struct mount *mp;
10004 struct vnode *vp;
10005 struct inode *ip;
10006 ino_t oldinum;
10007
10008 if (dirrem->dm_state & ONWORKLIST)
10009 panic("handle_workitem_remove: dirrem %p still on worklist",
10010 dirrem);
10011 oldinum = dirrem->dm_oldinum;
10012 mp = dirrem->dm_list.wk_mp;
10013 ump = VFSTOUFS(mp);
10014 flags |= LK_EXCLUSIVE;
10015 if (ffs_vgetf(mp, oldinum, flags, &vp, FFSV_FORCEINSMQ |
10016 FFSV_FORCEINODEDEP) != 0)
10017 return (EBUSY);
10018 ip = VTOI(vp);
10019 MPASS(ip->i_mode != 0);
10020 ACQUIRE_LOCK(ump);
10021 if ((inodedep_lookup(mp, oldinum, 0, &inodedep)) == 0)
10022 panic("handle_workitem_remove: lost inodedep");
10023 if (dirrem->dm_state & ONDEPLIST)
10024 LIST_REMOVE(dirrem, dm_inonext);
10025 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd),
10026 ("handle_workitem_remove: Journal entries not written."));
10027
10028 /*
10029 * Move all dependencies waiting on the remove to complete
10030 * from the dirrem to the inode inowait list to be completed
10031 * after the inode has been updated and written to disk.
10032 *
10033 * Any marked MKDIR_PARENT are saved to be completed when the
10034 * dotdot ref is removed unless DIRCHG is specified. For
10035 * directory change operations there will be no further
10036 * directory writes and the jsegdeps need to be moved along
10037 * with the rest to be completed when the inode is free or
10038 * stable in the inode free list.
10039 */
10040 LIST_INIT(&dotdotwk);
10041 while ((wk = LIST_FIRST(&dirrem->dm_jwork)) != NULL) {
10042 WORKLIST_REMOVE(wk);
10043 if ((dirrem->dm_state & DIRCHG) == 0 &&
10044 wk->wk_state & MKDIR_PARENT) {
10045 wk->wk_state &= ~MKDIR_PARENT;
10046 WORKLIST_INSERT(&dotdotwk, wk);
10047 continue;
10048 }
10049 WORKLIST_INSERT(&inodedep->id_inowait, wk);
10050 }
10051 LIST_SWAP(&dirrem->dm_jwork, &dotdotwk, worklist, wk_list);
10052 /*
10053 * Normal file deletion.
10054 */
10055 if ((dirrem->dm_state & RMDIR) == 0) {
10056 ip->i_nlink--;
10057 KASSERT(ip->i_nlink >= 0, ("handle_workitem_remove: file ino "
10058 "%ju negative i_nlink %d", (intmax_t)ip->i_number,
10059 ip->i_nlink));
10060 DIP_SET_NLINK(ip, ip->i_nlink);
10061 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
10062 if (ip->i_nlink < ip->i_effnlink)
10063 panic("handle_workitem_remove: bad file delta");
10064 if (ip->i_nlink == 0)
10065 unlinked_inodedep(mp, inodedep);
10066 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
10067 KASSERT(LIST_EMPTY(&dirrem->dm_jwork),
10068 ("handle_workitem_remove: worklist not empty. %s",
10069 TYPENAME(LIST_FIRST(&dirrem->dm_jwork)->wk_type)));
10070 WORKITEM_FREE(dirrem, D_DIRREM);
10071 FREE_LOCK(ump);
10072 goto out;
10073 }
10074 /*
10075 * Directory deletion. Decrement reference count for both the
10076 * just deleted parent directory entry and the reference for ".".
10077 * Arrange to have the reference count on the parent decremented
10078 * to account for the loss of "..".
10079 */
10080 ip->i_nlink -= 2;
10081 KASSERT(ip->i_nlink >= 0, ("handle_workitem_remove: directory ino "
10082 "%ju negative i_nlink %d", (intmax_t)ip->i_number, ip->i_nlink));
10083 DIP_SET_NLINK(ip, ip->i_nlink);
10084 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
10085 if (ip->i_nlink < ip->i_effnlink)
10086 panic("handle_workitem_remove: bad dir delta");
10087 if (ip->i_nlink == 0)
10088 unlinked_inodedep(mp, inodedep);
10089 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
10090 /*
10091 * Rename a directory to a new parent. Since, we are both deleting
10092 * and creating a new directory entry, the link count on the new
10093 * directory should not change. Thus we skip the followup dirrem.
10094 */
10095 if (dirrem->dm_state & DIRCHG) {
10096 KASSERT(LIST_EMPTY(&dirrem->dm_jwork),
10097 ("handle_workitem_remove: DIRCHG and worklist not empty."));
10098 WORKITEM_FREE(dirrem, D_DIRREM);
10099 FREE_LOCK(ump);
10100 goto out;
10101 }
10102 dirrem->dm_state = ONDEPLIST;
10103 dirrem->dm_oldinum = dirrem->dm_dirinum;
10104 /*
10105 * Place the dirrem on the parent's diremhd list.
10106 */
10107 if (inodedep_lookup(mp, dirrem->dm_oldinum, 0, &inodedep) == 0)
10108 panic("handle_workitem_remove: lost dir inodedep");
10109 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext);
10110 /*
10111 * If the allocated inode has never been written to disk, then
10112 * the on-disk inode is zero'ed and we can remove the file
10113 * immediately. When journaling if the inode has been marked
10114 * unlinked and not DEPCOMPLETE we know it can never be written.
10115 */
10116 inodedep_lookup(mp, oldinum, 0, &inodedep);
10117 if (inodedep == NULL ||
10118 (inodedep->id_state & (DEPCOMPLETE | UNLINKED)) == UNLINKED ||
10119 check_inode_unwritten(inodedep)) {
10120 FREE_LOCK(ump);
10121 vput(vp);
10122 return handle_workitem_remove(dirrem, flags);
10123 }
10124 WORKLIST_INSERT(&inodedep->id_inowait, &dirrem->dm_list);
10125 FREE_LOCK(ump);
10126 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
10127 out:
10128 ffs_update(vp, 0);
10129 vput(vp);
10130 return (0);
10131 }
10132
10133 /*
10134 * Inode de-allocation dependencies.
10135 *
10136 * When an inode's link count is reduced to zero, it can be de-allocated. We
10137 * found it convenient to postpone de-allocation until after the inode is
10138 * written to disk with its new link count (zero). At this point, all of the
10139 * on-disk inode's block pointers are nullified and, with careful dependency
10140 * list ordering, all dependencies related to the inode will be satisfied and
10141 * the corresponding dependency structures de-allocated. So, if/when the
10142 * inode is reused, there will be no mixing of old dependencies with new
10143 * ones. This artificial dependency is set up by the block de-allocation
10144 * procedure above (softdep_setup_freeblocks) and completed by the
10145 * following procedure.
10146 */
10147 static void
handle_workitem_freefile(struct freefile * freefile)10148 handle_workitem_freefile(struct freefile *freefile)
10149 {
10150 struct workhead wkhd;
10151 struct fs *fs;
10152 struct ufsmount *ump;
10153 int error;
10154 #ifdef INVARIANTS
10155 struct inodedep *idp;
10156 #endif
10157
10158 ump = VFSTOUFS(freefile->fx_list.wk_mp);
10159 fs = ump->um_fs;
10160 #ifdef INVARIANTS
10161 ACQUIRE_LOCK(ump);
10162 error = inodedep_lookup(UFSTOVFS(ump), freefile->fx_oldinum, 0, &idp);
10163 FREE_LOCK(ump);
10164 if (error)
10165 panic("handle_workitem_freefile: inodedep %p survived", idp);
10166 #endif
10167 UFS_LOCK(ump);
10168 fs->fs_pendinginodes -= 1;
10169 UFS_UNLOCK(ump);
10170 LIST_INIT(&wkhd);
10171 LIST_SWAP(&freefile->fx_jwork, &wkhd, worklist, wk_list);
10172 if ((error = ffs_freefile(ump, fs, freefile->fx_devvp,
10173 freefile->fx_oldinum, freefile->fx_mode, &wkhd)) != 0)
10174 softdep_error("handle_workitem_freefile", error);
10175 ACQUIRE_LOCK(ump);
10176 WORKITEM_FREE(freefile, D_FREEFILE);
10177 FREE_LOCK(ump);
10178 }
10179
10180 /*
10181 * Helper function which unlinks marker element from work list and returns
10182 * the next element on the list.
10183 */
10184 static __inline struct worklist *
markernext(struct worklist * marker)10185 markernext(struct worklist *marker)
10186 {
10187 struct worklist *next;
10188
10189 next = LIST_NEXT(marker, wk_list);
10190 LIST_REMOVE(marker, wk_list);
10191 return next;
10192 }
10193
10194 /*
10195 * Disk writes.
10196 *
10197 * The dependency structures constructed above are most actively used when file
10198 * system blocks are written to disk. No constraints are placed on when a
10199 * block can be written, but unsatisfied update dependencies are made safe by
10200 * modifying (or replacing) the source memory for the duration of the disk
10201 * write. When the disk write completes, the memory block is again brought
10202 * up-to-date.
10203 *
10204 * In-core inode structure reclamation.
10205 *
10206 * Because there are a finite number of "in-core" inode structures, they are
10207 * reused regularly. By transferring all inode-related dependencies to the
10208 * in-memory inode block and indexing them separately (via "inodedep"s), we
10209 * can allow "in-core" inode structures to be reused at any time and avoid
10210 * any increase in contention.
10211 *
10212 * Called just before entering the device driver to initiate a new disk I/O.
10213 * The buffer must be locked, thus, no I/O completion operations can occur
10214 * while we are manipulating its associated dependencies.
10215 */
10216 static void
softdep_disk_io_initiation(struct buf * bp)10217 softdep_disk_io_initiation(
10218 struct buf *bp) /* structure describing disk write to occur */
10219 {
10220 struct worklist *wk;
10221 struct worklist marker;
10222 struct inodedep *inodedep;
10223 struct freeblks *freeblks;
10224 struct jblkdep *jblkdep;
10225 struct newblk *newblk;
10226 struct ufsmount *ump;
10227
10228 /*
10229 * We only care about write operations. There should never
10230 * be dependencies for reads.
10231 */
10232 if (bp->b_iocmd != BIO_WRITE)
10233 panic("softdep_disk_io_initiation: not write");
10234
10235 if (bp->b_vflags & BV_BKGRDINPROG)
10236 panic("softdep_disk_io_initiation: Writing buffer with "
10237 "background write in progress: %p", bp);
10238
10239 ump = softdep_bp_to_mp(bp);
10240 if (ump == NULL)
10241 return;
10242
10243 marker.wk_type = D_LAST + 1; /* Not a normal workitem */
10244 PHOLD(curproc); /* Don't swap out kernel stack */
10245 ACQUIRE_LOCK(ump);
10246 /*
10247 * Do any necessary pre-I/O processing.
10248 */
10249 for (wk = LIST_FIRST(&bp->b_dep); wk != NULL;
10250 wk = markernext(&marker)) {
10251 LIST_INSERT_AFTER(wk, &marker, wk_list);
10252 switch (wk->wk_type) {
10253 case D_PAGEDEP:
10254 initiate_write_filepage(WK_PAGEDEP(wk), bp);
10255 continue;
10256
10257 case D_INODEDEP:
10258 inodedep = WK_INODEDEP(wk);
10259 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC)
10260 initiate_write_inodeblock_ufs1(inodedep, bp);
10261 else
10262 initiate_write_inodeblock_ufs2(inodedep, bp);
10263 continue;
10264
10265 case D_INDIRDEP:
10266 initiate_write_indirdep(WK_INDIRDEP(wk), bp);
10267 continue;
10268
10269 case D_BMSAFEMAP:
10270 initiate_write_bmsafemap(WK_BMSAFEMAP(wk), bp);
10271 continue;
10272
10273 case D_JSEG:
10274 WK_JSEG(wk)->js_buf = NULL;
10275 continue;
10276
10277 case D_FREEBLKS:
10278 freeblks = WK_FREEBLKS(wk);
10279 jblkdep = LIST_FIRST(&freeblks->fb_jblkdephd);
10280 /*
10281 * We have to wait for the freeblks to be journaled
10282 * before we can write an inodeblock with updated
10283 * pointers. Be careful to arrange the marker so
10284 * we revisit the freeblks if it's not removed by
10285 * the first jwait().
10286 */
10287 if (jblkdep != NULL) {
10288 LIST_REMOVE(&marker, wk_list);
10289 LIST_INSERT_BEFORE(wk, &marker, wk_list);
10290 jwait(&jblkdep->jb_list, MNT_WAIT);
10291 }
10292 continue;
10293 case D_ALLOCDIRECT:
10294 case D_ALLOCINDIR:
10295 /*
10296 * We have to wait for the jnewblk to be journaled
10297 * before we can write to a block if the contents
10298 * may be confused with an earlier file's indirect
10299 * at recovery time. Handle the marker as described
10300 * above.
10301 */
10302 newblk = WK_NEWBLK(wk);
10303 if (newblk->nb_jnewblk != NULL &&
10304 indirblk_lookup(newblk->nb_list.wk_mp,
10305 newblk->nb_newblkno)) {
10306 LIST_REMOVE(&marker, wk_list);
10307 LIST_INSERT_BEFORE(wk, &marker, wk_list);
10308 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT);
10309 }
10310 continue;
10311
10312 case D_SBDEP:
10313 initiate_write_sbdep(WK_SBDEP(wk));
10314 continue;
10315
10316 case D_MKDIR:
10317 case D_FREEWORK:
10318 case D_FREEDEP:
10319 case D_JSEGDEP:
10320 continue;
10321
10322 default:
10323 panic("handle_disk_io_initiation: Unexpected type %s",
10324 TYPENAME(wk->wk_type));
10325 /* NOTREACHED */
10326 }
10327 }
10328 FREE_LOCK(ump);
10329 PRELE(curproc); /* Allow swapout of kernel stack */
10330 }
10331
10332 /*
10333 * Called from within the procedure above to deal with unsatisfied
10334 * allocation dependencies in a directory. The buffer must be locked,
10335 * thus, no I/O completion operations can occur while we are
10336 * manipulating its associated dependencies.
10337 */
10338 static void
initiate_write_filepage(struct pagedep * pagedep,struct buf * bp)10339 initiate_write_filepage(struct pagedep *pagedep, struct buf *bp)
10340 {
10341 struct jremref *jremref;
10342 struct jmvref *jmvref;
10343 struct dirrem *dirrem;
10344 struct diradd *dap;
10345 struct direct *ep;
10346 int i;
10347
10348 if (pagedep->pd_state & IOSTARTED) {
10349 /*
10350 * This can only happen if there is a driver that does not
10351 * understand chaining. Here biodone will reissue the call
10352 * to strategy for the incomplete buffers.
10353 */
10354 printf("initiate_write_filepage: already started\n");
10355 return;
10356 }
10357 pagedep->pd_state |= IOSTARTED;
10358 /*
10359 * Wait for all journal remove dependencies to hit the disk.
10360 * We can not allow any potentially conflicting directory adds
10361 * to be visible before removes and rollback is too difficult.
10362 * The per-filesystem lock may be dropped and re-acquired, however
10363 * we hold the buf locked so the dependency can not go away.
10364 */
10365 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next)
10366 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL)
10367 jwait(&jremref->jr_list, MNT_WAIT);
10368 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL)
10369 jwait(&jmvref->jm_list, MNT_WAIT);
10370 for (i = 0; i < DAHASHSZ; i++) {
10371 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) {
10372 ep = (struct direct *)
10373 ((char *)bp->b_data + dap->da_offset);
10374 if (ep->d_ino != dap->da_newinum)
10375 panic("%s: dir inum %ju != new %ju",
10376 "initiate_write_filepage",
10377 (uintmax_t)ep->d_ino,
10378 (uintmax_t)dap->da_newinum);
10379 if (dap->da_state & DIRCHG)
10380 ep->d_ino = dap->da_previous->dm_oldinum;
10381 else
10382 ep->d_ino = 0;
10383 dap->da_state &= ~ATTACHED;
10384 dap->da_state |= UNDONE;
10385 }
10386 }
10387 }
10388
10389 /*
10390 * Version of initiate_write_inodeblock that handles UFS1 dinodes.
10391 * Note that any bug fixes made to this routine must be done in the
10392 * version found below.
10393 *
10394 * Called from within the procedure above to deal with unsatisfied
10395 * allocation dependencies in an inodeblock. The buffer must be
10396 * locked, thus, no I/O completion operations can occur while we
10397 * are manipulating its associated dependencies.
10398 */
10399 static void
initiate_write_inodeblock_ufs1(struct inodedep * inodedep,struct buf * bp)10400 initiate_write_inodeblock_ufs1(
10401 struct inodedep *inodedep,
10402 struct buf *bp) /* The inode block */
10403 {
10404 struct allocdirect *adp, *lastadp;
10405 struct ufs1_dinode *dp;
10406 struct ufs1_dinode *sip;
10407 struct inoref *inoref;
10408 struct ufsmount *ump;
10409 struct fs *fs;
10410 ufs_lbn_t i;
10411 #ifdef INVARIANTS
10412 ufs_lbn_t prevlbn = 0;
10413 #endif
10414 int deplist __diagused;
10415
10416 if (inodedep->id_state & IOSTARTED)
10417 panic("initiate_write_inodeblock_ufs1: already started");
10418 inodedep->id_state |= IOSTARTED;
10419 fs = inodedep->id_fs;
10420 ump = VFSTOUFS(inodedep->id_list.wk_mp);
10421 LOCK_OWNED(ump);
10422 dp = (struct ufs1_dinode *)bp->b_data +
10423 ino_to_fsbo(fs, inodedep->id_ino);
10424
10425 /*
10426 * If we're on the unlinked list but have not yet written our
10427 * next pointer initialize it here.
10428 */
10429 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) {
10430 struct inodedep *inon;
10431
10432 inon = TAILQ_NEXT(inodedep, id_unlinked);
10433 dp->di_freelink = inon ? inon->id_ino : 0;
10434 }
10435 /*
10436 * If the bitmap is not yet written, then the allocated
10437 * inode cannot be written to disk.
10438 */
10439 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
10440 if (inodedep->id_savedino1 != NULL)
10441 panic("initiate_write_inodeblock_ufs1: I/O underway");
10442 FREE_LOCK(ump);
10443 sip = malloc(sizeof(struct ufs1_dinode),
10444 M_SAVEDINO, M_SOFTDEP_FLAGS);
10445 ACQUIRE_LOCK(ump);
10446 inodedep->id_savedino1 = sip;
10447 *inodedep->id_savedino1 = *dp;
10448 bzero((caddr_t)dp, sizeof(struct ufs1_dinode));
10449 dp->di_gen = inodedep->id_savedino1->di_gen;
10450 dp->di_freelink = inodedep->id_savedino1->di_freelink;
10451 return;
10452 }
10453 /*
10454 * If no dependencies, then there is nothing to roll back.
10455 */
10456 inodedep->id_savedsize = dp->di_size;
10457 inodedep->id_savedextsize = 0;
10458 inodedep->id_savednlink = dp->di_nlink;
10459 if (TAILQ_EMPTY(&inodedep->id_inoupdt) &&
10460 TAILQ_EMPTY(&inodedep->id_inoreflst))
10461 return;
10462 /*
10463 * Revert the link count to that of the first unwritten journal entry.
10464 */
10465 inoref = TAILQ_FIRST(&inodedep->id_inoreflst);
10466 if (inoref)
10467 dp->di_nlink = inoref->if_nlink;
10468 /*
10469 * Set the dependencies to busy.
10470 */
10471 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10472 adp = TAILQ_NEXT(adp, ad_next)) {
10473 #ifdef INVARIANTS
10474 if (deplist != 0 && prevlbn >= adp->ad_offset)
10475 panic("softdep_write_inodeblock: lbn order");
10476 prevlbn = adp->ad_offset;
10477 if (adp->ad_offset < UFS_NDADDR &&
10478 dp->di_db[adp->ad_offset] != adp->ad_newblkno)
10479 panic("initiate_write_inodeblock_ufs1: "
10480 "direct pointer #%jd mismatch %d != %jd",
10481 (intmax_t)adp->ad_offset,
10482 dp->di_db[adp->ad_offset],
10483 (intmax_t)adp->ad_newblkno);
10484 if (adp->ad_offset >= UFS_NDADDR &&
10485 dp->di_ib[adp->ad_offset - UFS_NDADDR] != adp->ad_newblkno)
10486 panic("initiate_write_inodeblock_ufs1: "
10487 "indirect pointer #%jd mismatch %d != %jd",
10488 (intmax_t)adp->ad_offset - UFS_NDADDR,
10489 dp->di_ib[adp->ad_offset - UFS_NDADDR],
10490 (intmax_t)adp->ad_newblkno);
10491 deplist |= 1 << adp->ad_offset;
10492 if ((adp->ad_state & ATTACHED) == 0)
10493 panic("initiate_write_inodeblock_ufs1: "
10494 "Unknown state 0x%x", adp->ad_state);
10495 #endif /* INVARIANTS */
10496 adp->ad_state &= ~ATTACHED;
10497 adp->ad_state |= UNDONE;
10498 }
10499 /*
10500 * The on-disk inode cannot claim to be any larger than the last
10501 * fragment that has been written. Otherwise, the on-disk inode
10502 * might have fragments that were not the last block in the file
10503 * which would corrupt the filesystem.
10504 */
10505 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10506 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
10507 if (adp->ad_offset >= UFS_NDADDR)
10508 break;
10509 dp->di_db[adp->ad_offset] = adp->ad_oldblkno;
10510 /* keep going until hitting a rollback to a frag */
10511 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
10512 continue;
10513 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize;
10514 for (i = adp->ad_offset + 1; i < UFS_NDADDR; i++) {
10515 #ifdef INVARIANTS
10516 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0)
10517 panic("initiate_write_inodeblock_ufs1: "
10518 "lost dep1");
10519 #endif /* INVARIANTS */
10520 dp->di_db[i] = 0;
10521 }
10522 for (i = 0; i < UFS_NIADDR; i++) {
10523 #ifdef INVARIANTS
10524 if (dp->di_ib[i] != 0 &&
10525 (deplist & ((1 << UFS_NDADDR) << i)) == 0)
10526 panic("initiate_write_inodeblock_ufs1: "
10527 "lost dep2");
10528 #endif /* INVARIANTS */
10529 dp->di_ib[i] = 0;
10530 }
10531 return;
10532 }
10533 /*
10534 * If we have zero'ed out the last allocated block of the file,
10535 * roll back the size to the last currently allocated block.
10536 * We know that this last allocated block is a full-sized as
10537 * we already checked for fragments in the loop above.
10538 */
10539 if (lastadp != NULL &&
10540 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) {
10541 for (i = lastadp->ad_offset; i >= 0; i--)
10542 if (dp->di_db[i] != 0)
10543 break;
10544 dp->di_size = (i + 1) * fs->fs_bsize;
10545 }
10546 /*
10547 * The only dependencies are for indirect blocks.
10548 *
10549 * The file size for indirect block additions is not guaranteed.
10550 * Such a guarantee would be non-trivial to achieve. The conventional
10551 * synchronous write implementation also does not make this guarantee.
10552 * Fsck should catch and fix discrepancies. Arguably, the file size
10553 * can be over-estimated without destroying integrity when the file
10554 * moves into the indirect blocks (i.e., is large). If we want to
10555 * postpone fsck, we are stuck with this argument.
10556 */
10557 for (; adp; adp = TAILQ_NEXT(adp, ad_next))
10558 dp->di_ib[adp->ad_offset - UFS_NDADDR] = 0;
10559 }
10560
10561 /*
10562 * Version of initiate_write_inodeblock that handles UFS2 dinodes.
10563 * Note that any bug fixes made to this routine must be done in the
10564 * version found above.
10565 *
10566 * Called from within the procedure above to deal with unsatisfied
10567 * allocation dependencies in an inodeblock. The buffer must be
10568 * locked, thus, no I/O completion operations can occur while we
10569 * are manipulating its associated dependencies.
10570 */
10571 static void
initiate_write_inodeblock_ufs2(struct inodedep * inodedep,struct buf * bp)10572 initiate_write_inodeblock_ufs2(
10573 struct inodedep *inodedep,
10574 struct buf *bp) /* The inode block */
10575 {
10576 struct allocdirect *adp, *lastadp;
10577 struct ufs2_dinode *dp;
10578 struct ufs2_dinode *sip;
10579 struct inoref *inoref;
10580 struct ufsmount *ump;
10581 struct fs *fs;
10582 ufs_lbn_t i;
10583 #ifdef INVARIANTS
10584 ufs_lbn_t prevlbn = 0;
10585 #endif
10586 int deplist __diagused;
10587
10588 if (inodedep->id_state & IOSTARTED)
10589 panic("initiate_write_inodeblock_ufs2: already started");
10590 inodedep->id_state |= IOSTARTED;
10591 fs = inodedep->id_fs;
10592 ump = VFSTOUFS(inodedep->id_list.wk_mp);
10593 LOCK_OWNED(ump);
10594 dp = (struct ufs2_dinode *)bp->b_data +
10595 ino_to_fsbo(fs, inodedep->id_ino);
10596
10597 /*
10598 * If we're on the unlinked list but have not yet written our
10599 * next pointer initialize it here.
10600 */
10601 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) {
10602 struct inodedep *inon;
10603
10604 inon = TAILQ_NEXT(inodedep, id_unlinked);
10605 dp->di_freelink = inon ? inon->id_ino : 0;
10606 ffs_update_dinode_ckhash(fs, dp);
10607 }
10608 /*
10609 * If the bitmap is not yet written, then the allocated
10610 * inode cannot be written to disk.
10611 */
10612 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
10613 if (inodedep->id_savedino2 != NULL)
10614 panic("initiate_write_inodeblock_ufs2: I/O underway");
10615 FREE_LOCK(ump);
10616 sip = malloc(sizeof(struct ufs2_dinode),
10617 M_SAVEDINO, M_SOFTDEP_FLAGS);
10618 ACQUIRE_LOCK(ump);
10619 inodedep->id_savedino2 = sip;
10620 *inodedep->id_savedino2 = *dp;
10621 bzero((caddr_t)dp, sizeof(struct ufs2_dinode));
10622 dp->di_gen = inodedep->id_savedino2->di_gen;
10623 dp->di_freelink = inodedep->id_savedino2->di_freelink;
10624 return;
10625 }
10626 /*
10627 * If no dependencies, then there is nothing to roll back.
10628 */
10629 inodedep->id_savedsize = dp->di_size;
10630 inodedep->id_savedextsize = dp->di_extsize;
10631 inodedep->id_savednlink = dp->di_nlink;
10632 if (TAILQ_EMPTY(&inodedep->id_inoupdt) &&
10633 TAILQ_EMPTY(&inodedep->id_extupdt) &&
10634 TAILQ_EMPTY(&inodedep->id_inoreflst))
10635 return;
10636 /*
10637 * Revert the link count to that of the first unwritten journal entry.
10638 */
10639 inoref = TAILQ_FIRST(&inodedep->id_inoreflst);
10640 if (inoref)
10641 dp->di_nlink = inoref->if_nlink;
10642
10643 /*
10644 * Set the ext data dependencies to busy.
10645 */
10646 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp;
10647 adp = TAILQ_NEXT(adp, ad_next)) {
10648 #ifdef INVARIANTS
10649 if (deplist != 0 && prevlbn >= adp->ad_offset)
10650 panic("initiate_write_inodeblock_ufs2: lbn order");
10651 prevlbn = adp->ad_offset;
10652 if (dp->di_extb[adp->ad_offset] != adp->ad_newblkno)
10653 panic("initiate_write_inodeblock_ufs2: "
10654 "ext pointer #%jd mismatch %jd != %jd",
10655 (intmax_t)adp->ad_offset,
10656 (intmax_t)dp->di_extb[adp->ad_offset],
10657 (intmax_t)adp->ad_newblkno);
10658 deplist |= 1 << adp->ad_offset;
10659 if ((adp->ad_state & ATTACHED) == 0)
10660 panic("initiate_write_inodeblock_ufs2: Unknown "
10661 "state 0x%x", adp->ad_state);
10662 #endif /* INVARIANTS */
10663 adp->ad_state &= ~ATTACHED;
10664 adp->ad_state |= UNDONE;
10665 }
10666 /*
10667 * The on-disk inode cannot claim to be any larger than the last
10668 * fragment that has been written. Otherwise, the on-disk inode
10669 * might have fragments that were not the last block in the ext
10670 * data which would corrupt the filesystem.
10671 */
10672 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp;
10673 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
10674 dp->di_extb[adp->ad_offset] = adp->ad_oldblkno;
10675 /* keep going until hitting a rollback to a frag */
10676 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
10677 continue;
10678 dp->di_extsize = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize;
10679 for (i = adp->ad_offset + 1; i < UFS_NXADDR; i++) {
10680 #ifdef INVARIANTS
10681 if (dp->di_extb[i] != 0 && (deplist & (1 << i)) == 0)
10682 panic("initiate_write_inodeblock_ufs2: "
10683 "lost dep1");
10684 #endif /* INVARIANTS */
10685 dp->di_extb[i] = 0;
10686 }
10687 lastadp = NULL;
10688 break;
10689 }
10690 /*
10691 * If we have zero'ed out the last allocated block of the ext
10692 * data, roll back the size to the last currently allocated block.
10693 * We know that this last allocated block is a full-sized as
10694 * we already checked for fragments in the loop above.
10695 */
10696 if (lastadp != NULL &&
10697 dp->di_extsize <= (lastadp->ad_offset + 1) * fs->fs_bsize) {
10698 for (i = lastadp->ad_offset; i >= 0; i--)
10699 if (dp->di_extb[i] != 0)
10700 break;
10701 dp->di_extsize = (i + 1) * fs->fs_bsize;
10702 }
10703 /*
10704 * Set the file data dependencies to busy.
10705 */
10706 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10707 adp = TAILQ_NEXT(adp, ad_next)) {
10708 #ifdef INVARIANTS
10709 if (deplist != 0 && prevlbn >= adp->ad_offset)
10710 panic("softdep_write_inodeblock: lbn order");
10711 if ((adp->ad_state & ATTACHED) == 0)
10712 panic("inodedep %p and adp %p not attached", inodedep, adp);
10713 prevlbn = adp->ad_offset;
10714 if (!ffs_fsfail_cleanup(ump, 0) &&
10715 adp->ad_offset < UFS_NDADDR &&
10716 dp->di_db[adp->ad_offset] != adp->ad_newblkno)
10717 panic("initiate_write_inodeblock_ufs2: "
10718 "direct pointer #%jd mismatch %jd != %jd",
10719 (intmax_t)adp->ad_offset,
10720 (intmax_t)dp->di_db[adp->ad_offset],
10721 (intmax_t)adp->ad_newblkno);
10722 if (!ffs_fsfail_cleanup(ump, 0) &&
10723 adp->ad_offset >= UFS_NDADDR &&
10724 dp->di_ib[adp->ad_offset - UFS_NDADDR] != adp->ad_newblkno)
10725 panic("initiate_write_inodeblock_ufs2: "
10726 "indirect pointer #%jd mismatch %jd != %jd",
10727 (intmax_t)adp->ad_offset - UFS_NDADDR,
10728 (intmax_t)dp->di_ib[adp->ad_offset - UFS_NDADDR],
10729 (intmax_t)adp->ad_newblkno);
10730 deplist |= 1 << adp->ad_offset;
10731 if ((adp->ad_state & ATTACHED) == 0)
10732 panic("initiate_write_inodeblock_ufs2: Unknown "
10733 "state 0x%x", adp->ad_state);
10734 #endif /* INVARIANTS */
10735 adp->ad_state &= ~ATTACHED;
10736 adp->ad_state |= UNDONE;
10737 }
10738 /*
10739 * The on-disk inode cannot claim to be any larger than the last
10740 * fragment that has been written. Otherwise, the on-disk inode
10741 * might have fragments that were not the last block in the file
10742 * which would corrupt the filesystem.
10743 */
10744 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10745 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
10746 if (adp->ad_offset >= UFS_NDADDR)
10747 break;
10748 dp->di_db[adp->ad_offset] = adp->ad_oldblkno;
10749 /* keep going until hitting a rollback to a frag */
10750 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
10751 continue;
10752 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize;
10753 for (i = adp->ad_offset + 1; i < UFS_NDADDR; i++) {
10754 #ifdef INVARIANTS
10755 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0)
10756 panic("initiate_write_inodeblock_ufs2: "
10757 "lost dep2");
10758 #endif /* INVARIANTS */
10759 dp->di_db[i] = 0;
10760 }
10761 for (i = 0; i < UFS_NIADDR; i++) {
10762 #ifdef INVARIANTS
10763 if (dp->di_ib[i] != 0 &&
10764 (deplist & ((1 << UFS_NDADDR) << i)) == 0)
10765 panic("initiate_write_inodeblock_ufs2: "
10766 "lost dep3");
10767 #endif /* INVARIANTS */
10768 dp->di_ib[i] = 0;
10769 }
10770 ffs_update_dinode_ckhash(fs, dp);
10771 return;
10772 }
10773 /*
10774 * If we have zero'ed out the last allocated block of the file,
10775 * roll back the size to the last currently allocated block.
10776 * We know that this last allocated block is a full-sized as
10777 * we already checked for fragments in the loop above.
10778 */
10779 if (lastadp != NULL &&
10780 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) {
10781 for (i = lastadp->ad_offset; i >= 0; i--)
10782 if (dp->di_db[i] != 0)
10783 break;
10784 dp->di_size = (i + 1) * fs->fs_bsize;
10785 }
10786 /*
10787 * The only dependencies are for indirect blocks.
10788 *
10789 * The file size for indirect block additions is not guaranteed.
10790 * Such a guarantee would be non-trivial to achieve. The conventional
10791 * synchronous write implementation also does not make this guarantee.
10792 * Fsck should catch and fix discrepancies. Arguably, the file size
10793 * can be over-estimated without destroying integrity when the file
10794 * moves into the indirect blocks (i.e., is large). If we want to
10795 * postpone fsck, we are stuck with this argument.
10796 */
10797 for (; adp; adp = TAILQ_NEXT(adp, ad_next))
10798 dp->di_ib[adp->ad_offset - UFS_NDADDR] = 0;
10799 ffs_update_dinode_ckhash(fs, dp);
10800 }
10801
10802 /*
10803 * Cancel an indirdep as a result of truncation. Release all of the
10804 * children allocindirs and place their journal work on the appropriate
10805 * list.
10806 */
10807 static void
cancel_indirdep(struct indirdep * indirdep,struct buf * bp,struct freeblks * freeblks)10808 cancel_indirdep(
10809 struct indirdep *indirdep,
10810 struct buf *bp,
10811 struct freeblks *freeblks)
10812 {
10813 struct allocindir *aip;
10814
10815 /*
10816 * None of the indirect pointers will ever be visible,
10817 * so they can simply be tossed. GOINGAWAY ensures
10818 * that allocated pointers will be saved in the buffer
10819 * cache until they are freed. Note that they will
10820 * only be able to be found by their physical address
10821 * since the inode mapping the logical address will
10822 * be gone. The save buffer used for the safe copy
10823 * was allocated in setup_allocindir_phase2 using
10824 * the physical address so it could be used for this
10825 * purpose. Hence we swap the safe copy with the real
10826 * copy, allowing the safe copy to be freed and holding
10827 * on to the real copy for later use in indir_trunc.
10828 */
10829 if (indirdep->ir_state & GOINGAWAY)
10830 panic("cancel_indirdep: already gone");
10831 if ((indirdep->ir_state & DEPCOMPLETE) == 0) {
10832 indirdep->ir_state |= DEPCOMPLETE;
10833 LIST_REMOVE(indirdep, ir_next);
10834 }
10835 indirdep->ir_state |= GOINGAWAY;
10836 /*
10837 * Pass in bp for blocks still have journal writes
10838 * pending so we can cancel them on their own.
10839 */
10840 while ((aip = LIST_FIRST(&indirdep->ir_deplisthd)) != NULL)
10841 cancel_allocindir(aip, bp, freeblks, 0);
10842 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL)
10843 cancel_allocindir(aip, NULL, freeblks, 0);
10844 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL)
10845 cancel_allocindir(aip, NULL, freeblks, 0);
10846 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL)
10847 cancel_allocindir(aip, NULL, freeblks, 0);
10848 /*
10849 * If there are pending partial truncations we need to keep the
10850 * old block copy around until they complete. This is because
10851 * the current b_data is not a perfect superset of the available
10852 * blocks.
10853 */
10854 if (TAILQ_EMPTY(&indirdep->ir_trunc))
10855 bcopy(bp->b_data, indirdep->ir_savebp->b_data, bp->b_bcount);
10856 else
10857 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount);
10858 WORKLIST_REMOVE(&indirdep->ir_list);
10859 WORKLIST_INSERT(&indirdep->ir_savebp->b_dep, &indirdep->ir_list);
10860 indirdep->ir_bp = NULL;
10861 indirdep->ir_freeblks = freeblks;
10862 }
10863
10864 /*
10865 * Free an indirdep once it no longer has new pointers to track.
10866 */
10867 static void
free_indirdep(struct indirdep * indirdep)10868 free_indirdep(struct indirdep *indirdep)
10869 {
10870
10871 KASSERT(TAILQ_EMPTY(&indirdep->ir_trunc),
10872 ("free_indirdep: Indir trunc list not empty."));
10873 KASSERT(LIST_EMPTY(&indirdep->ir_completehd),
10874 ("free_indirdep: Complete head not empty."));
10875 KASSERT(LIST_EMPTY(&indirdep->ir_writehd),
10876 ("free_indirdep: write head not empty."));
10877 KASSERT(LIST_EMPTY(&indirdep->ir_donehd),
10878 ("free_indirdep: done head not empty."));
10879 KASSERT(LIST_EMPTY(&indirdep->ir_deplisthd),
10880 ("free_indirdep: deplist head not empty."));
10881 KASSERT((indirdep->ir_state & DEPCOMPLETE),
10882 ("free_indirdep: %p still on newblk list.", indirdep));
10883 KASSERT(indirdep->ir_saveddata == NULL,
10884 ("free_indirdep: %p still has saved data.", indirdep));
10885 KASSERT(indirdep->ir_savebp == NULL,
10886 ("free_indirdep: %p still has savebp buffer.", indirdep));
10887 if (indirdep->ir_state & ONWORKLIST)
10888 WORKLIST_REMOVE(&indirdep->ir_list);
10889 WORKITEM_FREE(indirdep, D_INDIRDEP);
10890 }
10891
10892 /*
10893 * Called before a write to an indirdep. This routine is responsible for
10894 * rolling back pointers to a safe state which includes only those
10895 * allocindirs which have been completed.
10896 */
10897 static void
initiate_write_indirdep(struct indirdep * indirdep,struct buf * bp)10898 initiate_write_indirdep(struct indirdep *indirdep, struct buf *bp)
10899 {
10900 struct ufsmount *ump;
10901
10902 indirdep->ir_state |= IOSTARTED;
10903 if (indirdep->ir_state & GOINGAWAY)
10904 panic("disk_io_initiation: indirdep gone");
10905 /*
10906 * If there are no remaining dependencies, this will be writing
10907 * the real pointers.
10908 */
10909 if (LIST_EMPTY(&indirdep->ir_deplisthd) &&
10910 TAILQ_EMPTY(&indirdep->ir_trunc))
10911 return;
10912 /*
10913 * Replace up-to-date version with safe version.
10914 */
10915 if (indirdep->ir_saveddata == NULL) {
10916 ump = VFSTOUFS(indirdep->ir_list.wk_mp);
10917 LOCK_OWNED(ump);
10918 FREE_LOCK(ump);
10919 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP,
10920 M_SOFTDEP_FLAGS);
10921 ACQUIRE_LOCK(ump);
10922 }
10923 indirdep->ir_state &= ~ATTACHED;
10924 indirdep->ir_state |= UNDONE;
10925 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount);
10926 bcopy(indirdep->ir_savebp->b_data, bp->b_data,
10927 bp->b_bcount);
10928 }
10929
10930 /*
10931 * Called when an inode has been cleared in a cg bitmap. This finally
10932 * eliminates any canceled jaddrefs
10933 */
10934 void
softdep_setup_inofree(struct mount * mp,struct buf * bp,ino_t ino,struct workhead * wkhd,bool doingrecovery)10935 softdep_setup_inofree(struct mount *mp,
10936 struct buf *bp,
10937 ino_t ino,
10938 struct workhead *wkhd,
10939 bool doingrecovery)
10940 {
10941 struct worklist *wk, *wkn;
10942 struct ufsmount *ump;
10943 #ifdef INVARIANTS
10944 struct inodedep *inodedep;
10945 #endif
10946
10947 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
10948 ("softdep_setup_inofree called on non-softdep filesystem"));
10949 ump = VFSTOUFS(mp);
10950 ACQUIRE_LOCK(ump);
10951 KASSERT(doingrecovery || ffs_fsfail_cleanup(ump, 0) ||
10952 isclr(cg_inosused((struct cg *)bp->b_data),
10953 ino % ump->um_fs->fs_ipg),
10954 ("softdep_setup_inofree: inode %ju not freed.", (uintmax_t)ino));
10955 KASSERT(inodedep_lookup(mp, ino, 0, &inodedep) == 0,
10956 ("softdep_setup_inofree: ino %ju has existing inodedep %p",
10957 (uintmax_t)ino, inodedep));
10958 if (wkhd) {
10959 LIST_FOREACH_SAFE(wk, wkhd, wk_list, wkn) {
10960 if (wk->wk_type != D_JADDREF)
10961 continue;
10962 WORKLIST_REMOVE(wk);
10963 /*
10964 * We can free immediately even if the jaddref
10965 * isn't attached in a background write as now
10966 * the bitmaps are reconciled.
10967 */
10968 wk->wk_state |= COMPLETE | ATTACHED;
10969 free_jaddref(WK_JADDREF(wk));
10970 }
10971 jwork_move(&bp->b_dep, wkhd);
10972 }
10973 FREE_LOCK(ump);
10974 }
10975
10976 /*
10977 * Called via ffs_blkfree() after a set of frags has been cleared from a cg
10978 * map. Any dependencies waiting for the write to clear are added to the
10979 * buf's list and any jnewblks that are being canceled are discarded
10980 * immediately.
10981 */
10982 void
softdep_setup_blkfree(struct mount * mp,struct buf * bp,ufs2_daddr_t blkno,int frags,struct workhead * wkhd,bool doingrecovery)10983 softdep_setup_blkfree(
10984 struct mount *mp,
10985 struct buf *bp,
10986 ufs2_daddr_t blkno,
10987 int frags,
10988 struct workhead *wkhd,
10989 bool doingrecovery)
10990 {
10991 struct bmsafemap *bmsafemap;
10992 struct jnewblk *jnewblk;
10993 struct ufsmount *ump;
10994 struct worklist *wk;
10995 struct fs *fs;
10996 #ifdef INVARIANTS
10997 uint8_t *blksfree;
10998 struct cg *cgp;
10999 ufs2_daddr_t jstart;
11000 ufs2_daddr_t jend;
11001 ufs2_daddr_t end;
11002 long bno;
11003 int i;
11004 #endif
11005
11006 CTR3(KTR_SUJ,
11007 "softdep_setup_blkfree: blkno %jd frags %d wk head %p",
11008 blkno, frags, wkhd);
11009
11010 ump = VFSTOUFS(mp);
11011 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
11012 ("softdep_setup_blkfree called on non-softdep filesystem"));
11013 ACQUIRE_LOCK(ump);
11014 /* Lookup the bmsafemap so we track when it is dirty. */
11015 fs = ump->um_fs;
11016 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL);
11017 /*
11018 * Detach any jnewblks which have been canceled. They must linger
11019 * until the bitmap is cleared again by ffs_blkfree() to prevent
11020 * an unjournaled allocation from hitting the disk.
11021 */
11022 if (wkhd) {
11023 while ((wk = LIST_FIRST(wkhd)) != NULL) {
11024 CTR2(KTR_SUJ,
11025 "softdep_setup_blkfree: blkno %jd wk type %d",
11026 blkno, wk->wk_type);
11027 WORKLIST_REMOVE(wk);
11028 if (wk->wk_type != D_JNEWBLK) {
11029 WORKLIST_INSERT(&bmsafemap->sm_freehd, wk);
11030 continue;
11031 }
11032 jnewblk = WK_JNEWBLK(wk);
11033 KASSERT(jnewblk->jn_state & GOINGAWAY,
11034 ("softdep_setup_blkfree: jnewblk not canceled."));
11035 #ifdef INVARIANTS
11036 if (!doingrecovery && !ffs_fsfail_cleanup(ump, 0)) {
11037 /*
11038 * Assert that this block is free in the
11039 * bitmap before we discard the jnewblk.
11040 */
11041 cgp = (struct cg *)bp->b_data;
11042 blksfree = cg_blksfree(cgp);
11043 bno = dtogd(fs, jnewblk->jn_blkno);
11044 for (i = jnewblk->jn_oldfrags;
11045 i < jnewblk->jn_frags; i++) {
11046 if (isset(blksfree, bno + i))
11047 continue;
11048 panic("softdep_setup_blkfree: block "
11049 "%ju not freed.",
11050 (uintmax_t)jnewblk->jn_blkno);
11051 }
11052 }
11053 #endif
11054 /*
11055 * Even if it's not attached we can free immediately
11056 * as the new bitmap is correct.
11057 */
11058 wk->wk_state |= COMPLETE | ATTACHED;
11059 free_jnewblk(jnewblk);
11060 }
11061 }
11062
11063 #ifdef INVARIANTS
11064 /*
11065 * Assert that we are not freeing a block which has an outstanding
11066 * allocation dependency.
11067 */
11068 fs = VFSTOUFS(mp)->um_fs;
11069 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL);
11070 end = blkno + frags;
11071 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) {
11072 /*
11073 * Don't match against blocks that will be freed when the
11074 * background write is done.
11075 */
11076 if ((jnewblk->jn_state & (ATTACHED | COMPLETE | DEPCOMPLETE)) ==
11077 (COMPLETE | DEPCOMPLETE))
11078 continue;
11079 jstart = jnewblk->jn_blkno + jnewblk->jn_oldfrags;
11080 jend = jnewblk->jn_blkno + jnewblk->jn_frags;
11081 if ((blkno >= jstart && blkno < jend) ||
11082 (end > jstart && end <= jend)) {
11083 printf("state 0x%X %jd - %d %d dep %p\n",
11084 jnewblk->jn_state, jnewblk->jn_blkno,
11085 jnewblk->jn_oldfrags, jnewblk->jn_frags,
11086 jnewblk->jn_dep);
11087 panic("softdep_setup_blkfree: "
11088 "%jd-%jd(%d) overlaps with %jd-%jd",
11089 blkno, end, frags, jstart, jend);
11090 }
11091 }
11092 #endif
11093 FREE_LOCK(ump);
11094 }
11095
11096 /*
11097 * Revert a block allocation when the journal record that describes it
11098 * is not yet written.
11099 */
11100 static int
jnewblk_rollback(struct jnewblk * jnewblk,struct fs * fs,struct cg * cgp,uint8_t * blksfree)11101 jnewblk_rollback(
11102 struct jnewblk *jnewblk,
11103 struct fs *fs,
11104 struct cg *cgp,
11105 uint8_t *blksfree)
11106 {
11107 ufs1_daddr_t fragno;
11108 long cgbno, bbase;
11109 int frags, blk;
11110 int i;
11111
11112 frags = 0;
11113 cgbno = dtogd(fs, jnewblk->jn_blkno);
11114 /*
11115 * We have to test which frags need to be rolled back. We may
11116 * be operating on a stale copy when doing background writes.
11117 */
11118 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++)
11119 if (isclr(blksfree, cgbno + i))
11120 frags++;
11121 if (frags == 0)
11122 return (0);
11123 /*
11124 * This is mostly ffs_blkfree() sans some validation and
11125 * superblock updates.
11126 */
11127 if (frags == fs->fs_frag) {
11128 fragno = fragstoblks(fs, cgbno);
11129 ffs_setblock(fs, blksfree, fragno);
11130 ffs_clusteracct(fs, cgp, fragno, 1);
11131 cgp->cg_cs.cs_nbfree++;
11132 } else {
11133 cgbno += jnewblk->jn_oldfrags;
11134 bbase = cgbno - fragnum(fs, cgbno);
11135 /* Decrement the old frags. */
11136 blk = blkmap(fs, blksfree, bbase);
11137 ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
11138 /* Deallocate the fragment */
11139 for (i = 0; i < frags; i++)
11140 setbit(blksfree, cgbno + i);
11141 cgp->cg_cs.cs_nffree += frags;
11142 /* Add back in counts associated with the new frags */
11143 blk = blkmap(fs, blksfree, bbase);
11144 ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
11145 /* If a complete block has been reassembled, account for it. */
11146 fragno = fragstoblks(fs, bbase);
11147 if (ffs_isblock(fs, blksfree, fragno)) {
11148 cgp->cg_cs.cs_nffree -= fs->fs_frag;
11149 ffs_clusteracct(fs, cgp, fragno, 1);
11150 cgp->cg_cs.cs_nbfree++;
11151 }
11152 }
11153 stat_jnewblk++;
11154 jnewblk->jn_state &= ~ATTACHED;
11155 jnewblk->jn_state |= UNDONE;
11156
11157 return (frags);
11158 }
11159
11160 static void
initiate_write_bmsafemap(struct bmsafemap * bmsafemap,struct buf * bp)11161 initiate_write_bmsafemap(
11162 struct bmsafemap *bmsafemap,
11163 struct buf *bp) /* The cg block. */
11164 {
11165 struct jaddref *jaddref;
11166 struct jnewblk *jnewblk;
11167 uint8_t *inosused;
11168 uint8_t *blksfree;
11169 struct cg *cgp;
11170 struct fs *fs;
11171 ino_t ino;
11172
11173 /*
11174 * If this is a background write, we did this at the time that
11175 * the copy was made, so do not need to do it again.
11176 */
11177 if (bmsafemap->sm_state & IOSTARTED)
11178 return;
11179 bmsafemap->sm_state |= IOSTARTED;
11180 /*
11181 * Clear any inode allocations which are pending journal writes.
11182 */
11183 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd) != NULL) {
11184 cgp = (struct cg *)bp->b_data;
11185 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
11186 inosused = cg_inosused(cgp);
11187 LIST_FOREACH(jaddref, &bmsafemap->sm_jaddrefhd, ja_bmdeps) {
11188 ino = jaddref->ja_ino % fs->fs_ipg;
11189 if (isset(inosused, ino)) {
11190 if ((jaddref->ja_mode & IFMT) == IFDIR)
11191 cgp->cg_cs.cs_ndir--;
11192 cgp->cg_cs.cs_nifree++;
11193 clrbit(inosused, ino);
11194 jaddref->ja_state &= ~ATTACHED;
11195 jaddref->ja_state |= UNDONE;
11196 stat_jaddref++;
11197 } else
11198 panic("initiate_write_bmsafemap: inode %ju "
11199 "marked free", (uintmax_t)jaddref->ja_ino);
11200 }
11201 }
11202 /*
11203 * Clear any block allocations which are pending journal writes.
11204 */
11205 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) {
11206 cgp = (struct cg *)bp->b_data;
11207 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
11208 blksfree = cg_blksfree(cgp);
11209 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) {
11210 if (jnewblk_rollback(jnewblk, fs, cgp, blksfree))
11211 continue;
11212 panic("initiate_write_bmsafemap: block %jd "
11213 "marked free", jnewblk->jn_blkno);
11214 }
11215 }
11216 /*
11217 * Move allocation lists to the written lists so they can be
11218 * cleared once the block write is complete.
11219 */
11220 LIST_SWAP(&bmsafemap->sm_inodedephd, &bmsafemap->sm_inodedepwr,
11221 inodedep, id_deps);
11222 LIST_SWAP(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr,
11223 newblk, nb_deps);
11224 LIST_SWAP(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, worklist,
11225 wk_list);
11226 }
11227
11228 void
softdep_handle_error(struct buf * bp)11229 softdep_handle_error(struct buf *bp)
11230 {
11231 struct ufsmount *ump;
11232
11233 ump = softdep_bp_to_mp(bp);
11234 if (ump == NULL)
11235 return;
11236
11237 if (ffs_fsfail_cleanup(ump, bp->b_error)) {
11238 /*
11239 * No future writes will succeed, so the on-disk image is safe.
11240 * Pretend that this write succeeded so that the softdep state
11241 * will be cleaned up naturally.
11242 */
11243 bp->b_ioflags &= ~BIO_ERROR;
11244 bp->b_error = 0;
11245 }
11246 }
11247
11248 /*
11249 * This routine is called during the completion interrupt
11250 * service routine for a disk write (from the procedure called
11251 * by the device driver to inform the filesystem caches of
11252 * a request completion). It should be called early in this
11253 * procedure, before the block is made available to other
11254 * processes or other routines are called.
11255 *
11256 */
11257 static void
softdep_disk_write_complete(struct buf * bp)11258 softdep_disk_write_complete(
11259 struct buf *bp) /* describes the completed disk write */
11260 {
11261 struct worklist *wk;
11262 struct worklist *owk;
11263 struct ufsmount *ump;
11264 struct workhead reattach;
11265 struct freeblks *freeblks;
11266 struct buf *sbp;
11267
11268 ump = softdep_bp_to_mp(bp);
11269 KASSERT(LIST_EMPTY(&bp->b_dep) || ump != NULL,
11270 ("softdep_disk_write_complete: softdep_bp_to_mp returned NULL "
11271 "with outstanding dependencies for buffer %p", bp));
11272 if (ump == NULL)
11273 return;
11274 if ((bp->b_ioflags & BIO_ERROR) != 0)
11275 softdep_handle_error(bp);
11276 /*
11277 * If an error occurred while doing the write, then the data
11278 * has not hit the disk and the dependencies cannot be processed.
11279 * But we do have to go through and roll forward any dependencies
11280 * that were rolled back before the disk write.
11281 */
11282 sbp = NULL;
11283 ACQUIRE_LOCK(ump);
11284 if ((bp->b_ioflags & BIO_ERROR) != 0 && (bp->b_flags & B_INVAL) == 0) {
11285 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
11286 switch (wk->wk_type) {
11287 case D_PAGEDEP:
11288 handle_written_filepage(WK_PAGEDEP(wk), bp, 0);
11289 continue;
11290
11291 case D_INODEDEP:
11292 handle_written_inodeblock(WK_INODEDEP(wk),
11293 bp, 0);
11294 continue;
11295
11296 case D_BMSAFEMAP:
11297 handle_written_bmsafemap(WK_BMSAFEMAP(wk),
11298 bp, 0);
11299 continue;
11300
11301 case D_INDIRDEP:
11302 handle_written_indirdep(WK_INDIRDEP(wk),
11303 bp, &sbp, 0);
11304 continue;
11305 default:
11306 /* nothing to roll forward */
11307 continue;
11308 }
11309 }
11310 FREE_LOCK(ump);
11311 if (sbp)
11312 brelse(sbp);
11313 return;
11314 }
11315 LIST_INIT(&reattach);
11316
11317 /*
11318 * Ump SU lock must not be released anywhere in this code segment.
11319 */
11320 owk = NULL;
11321 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) {
11322 WORKLIST_REMOVE(wk);
11323 atomic_add_long(&dep_write[wk->wk_type], 1);
11324 if (wk == owk)
11325 panic("duplicate worklist: %p\n", wk);
11326 owk = wk;
11327 switch (wk->wk_type) {
11328 case D_PAGEDEP:
11329 if (handle_written_filepage(WK_PAGEDEP(wk), bp,
11330 WRITESUCCEEDED))
11331 WORKLIST_INSERT(&reattach, wk);
11332 continue;
11333
11334 case D_INODEDEP:
11335 if (handle_written_inodeblock(WK_INODEDEP(wk), bp,
11336 WRITESUCCEEDED))
11337 WORKLIST_INSERT(&reattach, wk);
11338 continue;
11339
11340 case D_BMSAFEMAP:
11341 if (handle_written_bmsafemap(WK_BMSAFEMAP(wk), bp,
11342 WRITESUCCEEDED))
11343 WORKLIST_INSERT(&reattach, wk);
11344 continue;
11345
11346 case D_MKDIR:
11347 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY);
11348 continue;
11349
11350 case D_ALLOCDIRECT:
11351 wk->wk_state |= COMPLETE;
11352 handle_allocdirect_partdone(WK_ALLOCDIRECT(wk), NULL);
11353 continue;
11354
11355 case D_ALLOCINDIR:
11356 wk->wk_state |= COMPLETE;
11357 handle_allocindir_partdone(WK_ALLOCINDIR(wk));
11358 continue;
11359
11360 case D_INDIRDEP:
11361 if (handle_written_indirdep(WK_INDIRDEP(wk), bp, &sbp,
11362 WRITESUCCEEDED))
11363 WORKLIST_INSERT(&reattach, wk);
11364 continue;
11365
11366 case D_FREEBLKS:
11367 wk->wk_state |= COMPLETE;
11368 freeblks = WK_FREEBLKS(wk);
11369 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE &&
11370 LIST_EMPTY(&freeblks->fb_jblkdephd))
11371 add_to_worklist(wk, WK_NODELAY);
11372 continue;
11373
11374 case D_FREEWORK:
11375 handle_written_freework(WK_FREEWORK(wk));
11376 break;
11377
11378 case D_JSEGDEP:
11379 free_jsegdep(WK_JSEGDEP(wk));
11380 continue;
11381
11382 case D_JSEG:
11383 handle_written_jseg(WK_JSEG(wk), bp);
11384 continue;
11385
11386 case D_SBDEP:
11387 if (handle_written_sbdep(WK_SBDEP(wk), bp))
11388 WORKLIST_INSERT(&reattach, wk);
11389 continue;
11390
11391 case D_FREEDEP:
11392 free_freedep(WK_FREEDEP(wk));
11393 continue;
11394
11395 default:
11396 panic("handle_disk_write_complete: Unknown type %s",
11397 TYPENAME(wk->wk_type));
11398 /* NOTREACHED */
11399 }
11400 }
11401 /*
11402 * Reattach any requests that must be redone.
11403 */
11404 while ((wk = LIST_FIRST(&reattach)) != NULL) {
11405 WORKLIST_REMOVE(wk);
11406 WORKLIST_INSERT(&bp->b_dep, wk);
11407 }
11408 FREE_LOCK(ump);
11409 if (sbp)
11410 brelse(sbp);
11411 }
11412
11413 /*
11414 * Called from within softdep_disk_write_complete above.
11415 */
11416 static void
handle_allocdirect_partdone(struct allocdirect * adp,struct workhead * wkhd)11417 handle_allocdirect_partdone(
11418 struct allocdirect *adp, /* the completed allocdirect */
11419 struct workhead *wkhd) /* Work to do when inode is writtne. */
11420 {
11421 struct allocdirectlst *listhead;
11422 struct allocdirect *listadp;
11423 struct inodedep *inodedep;
11424 long bsize;
11425
11426 LOCK_OWNED(VFSTOUFS(adp->ad_block.nb_list.wk_mp));
11427 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE)
11428 return;
11429 /*
11430 * The on-disk inode cannot claim to be any larger than the last
11431 * fragment that has been written. Otherwise, the on-disk inode
11432 * might have fragments that were not the last block in the file
11433 * which would corrupt the filesystem. Thus, we cannot free any
11434 * allocdirects after one whose ad_oldblkno claims a fragment as
11435 * these blocks must be rolled back to zero before writing the inode.
11436 * We check the currently active set of allocdirects in id_inoupdt
11437 * or id_extupdt as appropriate.
11438 */
11439 inodedep = adp->ad_inodedep;
11440 bsize = inodedep->id_fs->fs_bsize;
11441 if (adp->ad_state & EXTDATA)
11442 listhead = &inodedep->id_extupdt;
11443 else
11444 listhead = &inodedep->id_inoupdt;
11445 TAILQ_FOREACH(listadp, listhead, ad_next) {
11446 /* found our block */
11447 if (listadp == adp)
11448 break;
11449 /* continue if ad_oldlbn is not a fragment */
11450 if (listadp->ad_oldsize == 0 ||
11451 listadp->ad_oldsize == bsize)
11452 continue;
11453 /* hit a fragment */
11454 return;
11455 }
11456 /*
11457 * If we have reached the end of the current list without
11458 * finding the just finished dependency, then it must be
11459 * on the future dependency list. Future dependencies cannot
11460 * be freed until they are moved to the current list.
11461 */
11462 if (listadp == NULL) {
11463 #ifdef INVARIANTS
11464 if (adp->ad_state & EXTDATA)
11465 listhead = &inodedep->id_newextupdt;
11466 else
11467 listhead = &inodedep->id_newinoupdt;
11468 TAILQ_FOREACH(listadp, listhead, ad_next)
11469 /* found our block */
11470 if (listadp == adp)
11471 break;
11472 if (listadp == NULL)
11473 panic("handle_allocdirect_partdone: lost dep");
11474 #endif /* INVARIANTS */
11475 return;
11476 }
11477 /*
11478 * If we have found the just finished dependency, then queue
11479 * it along with anything that follows it that is complete.
11480 * Since the pointer has not yet been written in the inode
11481 * as the dependency prevents it, place the allocdirect on the
11482 * bufwait list where it will be freed once the pointer is
11483 * valid.
11484 */
11485 if (wkhd == NULL)
11486 wkhd = &inodedep->id_bufwait;
11487 for (; adp; adp = listadp) {
11488 listadp = TAILQ_NEXT(adp, ad_next);
11489 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE)
11490 return;
11491 TAILQ_REMOVE(listhead, adp, ad_next);
11492 WORKLIST_INSERT(wkhd, &adp->ad_block.nb_list);
11493 }
11494 }
11495
11496 /*
11497 * Called from within softdep_disk_write_complete above. This routine
11498 * completes successfully written allocindirs.
11499 */
11500 static void
handle_allocindir_partdone(struct allocindir * aip)11501 handle_allocindir_partdone(
11502 struct allocindir *aip) /* the completed allocindir */
11503 {
11504 struct indirdep *indirdep;
11505
11506 if ((aip->ai_state & ALLCOMPLETE) != ALLCOMPLETE)
11507 return;
11508 indirdep = aip->ai_indirdep;
11509 LIST_REMOVE(aip, ai_next);
11510 /*
11511 * Don't set a pointer while the buffer is undergoing IO or while
11512 * we have active truncations.
11513 */
11514 if (indirdep->ir_state & UNDONE || !TAILQ_EMPTY(&indirdep->ir_trunc)) {
11515 LIST_INSERT_HEAD(&indirdep->ir_donehd, aip, ai_next);
11516 return;
11517 }
11518 if (indirdep->ir_state & UFS1FMT)
11519 ((ufs1_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] =
11520 aip->ai_newblkno;
11521 else
11522 ((ufs2_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] =
11523 aip->ai_newblkno;
11524 /*
11525 * Await the pointer write before freeing the allocindir.
11526 */
11527 LIST_INSERT_HEAD(&indirdep->ir_writehd, aip, ai_next);
11528 }
11529
11530 /*
11531 * Release segments held on a jwork list.
11532 */
11533 static void
handle_jwork(struct workhead * wkhd)11534 handle_jwork(struct workhead *wkhd)
11535 {
11536 struct worklist *wk;
11537
11538 while ((wk = LIST_FIRST(wkhd)) != NULL) {
11539 WORKLIST_REMOVE(wk);
11540 switch (wk->wk_type) {
11541 case D_JSEGDEP:
11542 free_jsegdep(WK_JSEGDEP(wk));
11543 continue;
11544 case D_FREEDEP:
11545 free_freedep(WK_FREEDEP(wk));
11546 continue;
11547 case D_FREEFRAG:
11548 rele_jseg(WK_JSEG(WK_FREEFRAG(wk)->ff_jdep));
11549 WORKITEM_FREE(wk, D_FREEFRAG);
11550 continue;
11551 case D_FREEWORK:
11552 handle_written_freework(WK_FREEWORK(wk));
11553 continue;
11554 default:
11555 panic("handle_jwork: Unknown type %s\n",
11556 TYPENAME(wk->wk_type));
11557 }
11558 }
11559 }
11560
11561 /*
11562 * Handle the bufwait list on an inode when it is safe to release items
11563 * held there. This normally happens after an inode block is written but
11564 * may be delayed and handled later if there are pending journal items that
11565 * are not yet safe to be released.
11566 */
11567 static struct freefile *
handle_bufwait(struct inodedep * inodedep,struct workhead * refhd)11568 handle_bufwait(
11569 struct inodedep *inodedep,
11570 struct workhead *refhd)
11571 {
11572 struct jaddref *jaddref;
11573 struct freefile *freefile;
11574 struct worklist *wk;
11575
11576 freefile = NULL;
11577 while ((wk = LIST_FIRST(&inodedep->id_bufwait)) != NULL) {
11578 WORKLIST_REMOVE(wk);
11579 switch (wk->wk_type) {
11580 case D_FREEFILE:
11581 /*
11582 * We defer adding freefile to the worklist
11583 * until all other additions have been made to
11584 * ensure that it will be done after all the
11585 * old blocks have been freed.
11586 */
11587 if (freefile != NULL)
11588 panic("handle_bufwait: freefile");
11589 freefile = WK_FREEFILE(wk);
11590 continue;
11591
11592 case D_MKDIR:
11593 handle_written_mkdir(WK_MKDIR(wk), MKDIR_PARENT);
11594 continue;
11595
11596 case D_DIRADD:
11597 diradd_inode_written(WK_DIRADD(wk), inodedep);
11598 continue;
11599
11600 case D_FREEFRAG:
11601 wk->wk_state |= COMPLETE;
11602 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE)
11603 add_to_worklist(wk, 0);
11604 continue;
11605
11606 case D_DIRREM:
11607 wk->wk_state |= COMPLETE;
11608 add_to_worklist(wk, 0);
11609 continue;
11610
11611 case D_ALLOCDIRECT:
11612 case D_ALLOCINDIR:
11613 free_newblk(WK_NEWBLK(wk));
11614 continue;
11615
11616 case D_JNEWBLK:
11617 wk->wk_state |= COMPLETE;
11618 free_jnewblk(WK_JNEWBLK(wk));
11619 continue;
11620
11621 /*
11622 * Save freed journal segments and add references on
11623 * the supplied list which will delay their release
11624 * until the cg bitmap is cleared on disk.
11625 */
11626 case D_JSEGDEP:
11627 if (refhd == NULL)
11628 free_jsegdep(WK_JSEGDEP(wk));
11629 else
11630 WORKLIST_INSERT(refhd, wk);
11631 continue;
11632
11633 case D_JADDREF:
11634 jaddref = WK_JADDREF(wk);
11635 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref,
11636 if_deps);
11637 /*
11638 * Transfer any jaddrefs to the list to be freed with
11639 * the bitmap if we're handling a removed file.
11640 */
11641 if (refhd == NULL) {
11642 wk->wk_state |= COMPLETE;
11643 free_jaddref(jaddref);
11644 } else
11645 WORKLIST_INSERT(refhd, wk);
11646 continue;
11647
11648 default:
11649 panic("handle_bufwait: Unknown type %p(%s)",
11650 wk, TYPENAME(wk->wk_type));
11651 /* NOTREACHED */
11652 }
11653 }
11654 return (freefile);
11655 }
11656 /*
11657 * Called from within softdep_disk_write_complete above to restore
11658 * in-memory inode block contents to their most up-to-date state. Note
11659 * that this routine is always called from interrupt level with further
11660 * interrupts from this device blocked.
11661 *
11662 * If the write did not succeed, we will do all the roll-forward
11663 * operations, but we will not take the actions that will allow its
11664 * dependencies to be processed.
11665 */
11666 static int
handle_written_inodeblock(struct inodedep * inodedep,struct buf * bp,int flags)11667 handle_written_inodeblock(
11668 struct inodedep *inodedep,
11669 struct buf *bp, /* buffer containing the inode block */
11670 int flags)
11671 {
11672 struct freefile *freefile;
11673 struct allocdirect *adp, *nextadp;
11674 struct ufs1_dinode *dp1 = NULL;
11675 struct ufs2_dinode *dp2 = NULL;
11676 struct workhead wkhd;
11677 int hadchanges, fstype;
11678 ino_t freelink;
11679
11680 LIST_INIT(&wkhd);
11681 hadchanges = 0;
11682 freefile = NULL;
11683 if ((inodedep->id_state & IOSTARTED) == 0)
11684 panic("handle_written_inodeblock: not started");
11685 inodedep->id_state &= ~IOSTARTED;
11686 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) {
11687 fstype = UFS1;
11688 dp1 = (struct ufs1_dinode *)bp->b_data +
11689 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino);
11690 freelink = dp1->di_freelink;
11691 } else {
11692 fstype = UFS2;
11693 dp2 = (struct ufs2_dinode *)bp->b_data +
11694 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino);
11695 freelink = dp2->di_freelink;
11696 }
11697 /*
11698 * Leave this inodeblock dirty until it's in the list.
11699 */
11700 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) == UNLINKED &&
11701 (flags & WRITESUCCEEDED)) {
11702 struct inodedep *inon;
11703
11704 inon = TAILQ_NEXT(inodedep, id_unlinked);
11705 if ((inon == NULL && freelink == 0) ||
11706 (inon && inon->id_ino == freelink)) {
11707 if (inon)
11708 inon->id_state |= UNLINKPREV;
11709 inodedep->id_state |= UNLINKNEXT;
11710 }
11711 hadchanges = 1;
11712 }
11713 /*
11714 * If we had to rollback the inode allocation because of
11715 * bitmaps being incomplete, then simply restore it.
11716 * Keep the block dirty so that it will not be reclaimed until
11717 * all associated dependencies have been cleared and the
11718 * corresponding updates written to disk.
11719 */
11720 if (inodedep->id_savedino1 != NULL) {
11721 hadchanges = 1;
11722 if (fstype == UFS1)
11723 *dp1 = *inodedep->id_savedino1;
11724 else
11725 *dp2 = *inodedep->id_savedino2;
11726 free(inodedep->id_savedino1, M_SAVEDINO);
11727 inodedep->id_savedino1 = NULL;
11728 if ((bp->b_flags & B_DELWRI) == 0)
11729 stat_inode_bitmap++;
11730 bdirty(bp);
11731 /*
11732 * If the inode is clear here and GOINGAWAY it will never
11733 * be written. Process the bufwait and clear any pending
11734 * work which may include the freefile.
11735 */
11736 if (inodedep->id_state & GOINGAWAY)
11737 goto bufwait;
11738 return (1);
11739 }
11740 if (flags & WRITESUCCEEDED)
11741 inodedep->id_state |= COMPLETE;
11742 /*
11743 * Roll forward anything that had to be rolled back before
11744 * the inode could be updated.
11745 */
11746 for (adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; adp = nextadp) {
11747 nextadp = TAILQ_NEXT(adp, ad_next);
11748 if (adp->ad_state & ATTACHED)
11749 panic("handle_written_inodeblock: new entry");
11750 if (fstype == UFS1) {
11751 if (adp->ad_offset < UFS_NDADDR) {
11752 if (dp1->di_db[adp->ad_offset]!=adp->ad_oldblkno)
11753 panic("%s %s #%jd mismatch %d != %jd",
11754 "handle_written_inodeblock:",
11755 "direct pointer",
11756 (intmax_t)adp->ad_offset,
11757 dp1->di_db[adp->ad_offset],
11758 (intmax_t)adp->ad_oldblkno);
11759 dp1->di_db[adp->ad_offset] = adp->ad_newblkno;
11760 } else {
11761 if (dp1->di_ib[adp->ad_offset - UFS_NDADDR] !=
11762 0)
11763 panic("%s: %s #%jd allocated as %d",
11764 "handle_written_inodeblock",
11765 "indirect pointer",
11766 (intmax_t)adp->ad_offset -
11767 UFS_NDADDR,
11768 dp1->di_ib[adp->ad_offset -
11769 UFS_NDADDR]);
11770 dp1->di_ib[adp->ad_offset - UFS_NDADDR] =
11771 adp->ad_newblkno;
11772 }
11773 } else {
11774 if (adp->ad_offset < UFS_NDADDR) {
11775 if (dp2->di_db[adp->ad_offset]!=adp->ad_oldblkno)
11776 panic("%s: %s #%jd %s %jd != %jd",
11777 "handle_written_inodeblock",
11778 "direct pointer",
11779 (intmax_t)adp->ad_offset, "mismatch",
11780 (intmax_t)dp2->di_db[adp->ad_offset],
11781 (intmax_t)adp->ad_oldblkno);
11782 dp2->di_db[adp->ad_offset] = adp->ad_newblkno;
11783 } else {
11784 if (dp2->di_ib[adp->ad_offset - UFS_NDADDR] !=
11785 0)
11786 panic("%s: %s #%jd allocated as %jd",
11787 "handle_written_inodeblock",
11788 "indirect pointer",
11789 (intmax_t)adp->ad_offset -
11790 UFS_NDADDR,
11791 (intmax_t)
11792 dp2->di_ib[adp->ad_offset -
11793 UFS_NDADDR]);
11794 dp2->di_ib[adp->ad_offset - UFS_NDADDR] =
11795 adp->ad_newblkno;
11796 }
11797 }
11798 adp->ad_state &= ~UNDONE;
11799 adp->ad_state |= ATTACHED;
11800 hadchanges = 1;
11801 }
11802 for (adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; adp = nextadp) {
11803 nextadp = TAILQ_NEXT(adp, ad_next);
11804 if (adp->ad_state & ATTACHED)
11805 panic("handle_written_inodeblock: new entry");
11806 if (dp2->di_extb[adp->ad_offset] != adp->ad_oldblkno)
11807 panic("%s: direct pointers #%jd %s %jd != %jd",
11808 "handle_written_inodeblock",
11809 (intmax_t)adp->ad_offset, "mismatch",
11810 (intmax_t)dp2->di_extb[adp->ad_offset],
11811 (intmax_t)adp->ad_oldblkno);
11812 dp2->di_extb[adp->ad_offset] = adp->ad_newblkno;
11813 adp->ad_state &= ~UNDONE;
11814 adp->ad_state |= ATTACHED;
11815 hadchanges = 1;
11816 }
11817 if (hadchanges && (bp->b_flags & B_DELWRI) == 0)
11818 stat_direct_blk_ptrs++;
11819 /*
11820 * Reset the file size to its most up-to-date value.
11821 */
11822 if (inodedep->id_savedsize == -1 || inodedep->id_savedextsize == -1)
11823 panic("handle_written_inodeblock: bad size");
11824 if (inodedep->id_savednlink > UFS_LINK_MAX)
11825 panic("handle_written_inodeblock: Invalid link count "
11826 "%jd for inodedep %p", (uintmax_t)inodedep->id_savednlink,
11827 inodedep);
11828 if (fstype == UFS1) {
11829 if (dp1->di_nlink != inodedep->id_savednlink) {
11830 dp1->di_nlink = inodedep->id_savednlink;
11831 hadchanges = 1;
11832 }
11833 if (dp1->di_size != inodedep->id_savedsize) {
11834 dp1->di_size = inodedep->id_savedsize;
11835 hadchanges = 1;
11836 }
11837 } else {
11838 if (dp2->di_nlink != inodedep->id_savednlink) {
11839 dp2->di_nlink = inodedep->id_savednlink;
11840 hadchanges = 1;
11841 }
11842 if (dp2->di_size != inodedep->id_savedsize) {
11843 dp2->di_size = inodedep->id_savedsize;
11844 hadchanges = 1;
11845 }
11846 if (dp2->di_extsize != inodedep->id_savedextsize) {
11847 dp2->di_extsize = inodedep->id_savedextsize;
11848 hadchanges = 1;
11849 }
11850 }
11851 inodedep->id_savedsize = -1;
11852 inodedep->id_savedextsize = -1;
11853 inodedep->id_savednlink = -1;
11854 /*
11855 * If there were any rollbacks in the inode block, then it must be
11856 * marked dirty so that its will eventually get written back in
11857 * its correct form.
11858 */
11859 if (hadchanges) {
11860 if (fstype == UFS2)
11861 ffs_update_dinode_ckhash(inodedep->id_fs, dp2);
11862 bdirty(bp);
11863 }
11864 bufwait:
11865 /*
11866 * If the write did not succeed, we have done all the roll-forward
11867 * operations, but we cannot take the actions that will allow its
11868 * dependencies to be processed.
11869 */
11870 if ((flags & WRITESUCCEEDED) == 0)
11871 return (hadchanges);
11872 /*
11873 * Process any allocdirects that completed during the update.
11874 */
11875 if ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL)
11876 handle_allocdirect_partdone(adp, &wkhd);
11877 if ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL)
11878 handle_allocdirect_partdone(adp, &wkhd);
11879 /*
11880 * Process deallocations that were held pending until the
11881 * inode had been written to disk. Freeing of the inode
11882 * is delayed until after all blocks have been freed to
11883 * avoid creation of new <vfsid, inum, lbn> triples
11884 * before the old ones have been deleted. Completely
11885 * unlinked inodes are not processed until the unlinked
11886 * inode list is written or the last reference is removed.
11887 */
11888 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) != UNLINKED) {
11889 freefile = handle_bufwait(inodedep, NULL);
11890 if (freefile && !LIST_EMPTY(&wkhd)) {
11891 WORKLIST_INSERT(&wkhd, &freefile->fx_list);
11892 freefile = NULL;
11893 }
11894 }
11895 /*
11896 * Move rolled forward dependency completions to the bufwait list
11897 * now that those that were already written have been processed.
11898 */
11899 if (!LIST_EMPTY(&wkhd) && hadchanges == 0)
11900 panic("handle_written_inodeblock: bufwait but no changes");
11901 jwork_move(&inodedep->id_bufwait, &wkhd);
11902
11903 if (freefile != NULL) {
11904 /*
11905 * If the inode is goingaway it was never written. Fake up
11906 * the state here so free_inodedep() can succeed.
11907 */
11908 if (inodedep->id_state & GOINGAWAY)
11909 inodedep->id_state |= COMPLETE | DEPCOMPLETE;
11910 if (free_inodedep(inodedep) == 0)
11911 panic("handle_written_inodeblock: live inodedep %p",
11912 inodedep);
11913 add_to_worklist(&freefile->fx_list, 0);
11914 return (0);
11915 }
11916
11917 /*
11918 * If no outstanding dependencies, free it.
11919 */
11920 if (free_inodedep(inodedep) ||
11921 (TAILQ_FIRST(&inodedep->id_inoreflst) == 0 &&
11922 TAILQ_FIRST(&inodedep->id_inoupdt) == 0 &&
11923 TAILQ_FIRST(&inodedep->id_extupdt) == 0 &&
11924 LIST_FIRST(&inodedep->id_bufwait) == 0))
11925 return (0);
11926 return (hadchanges);
11927 }
11928
11929 /*
11930 * Perform needed roll-forwards and kick off any dependencies that
11931 * can now be processed.
11932 *
11933 * If the write did not succeed, we will do all the roll-forward
11934 * operations, but we will not take the actions that will allow its
11935 * dependencies to be processed.
11936 */
11937 static int
handle_written_indirdep(struct indirdep * indirdep,struct buf * bp,struct buf ** bpp,int flags)11938 handle_written_indirdep(
11939 struct indirdep *indirdep,
11940 struct buf *bp,
11941 struct buf **bpp,
11942 int flags)
11943 {
11944 struct allocindir *aip;
11945 struct buf *sbp;
11946 int chgs;
11947
11948 if (indirdep->ir_state & GOINGAWAY)
11949 panic("handle_written_indirdep: indirdep gone");
11950 if ((indirdep->ir_state & IOSTARTED) == 0)
11951 panic("handle_written_indirdep: IO not started");
11952 chgs = 0;
11953 /*
11954 * If there were rollbacks revert them here.
11955 */
11956 if (indirdep->ir_saveddata) {
11957 bcopy(indirdep->ir_saveddata, bp->b_data, bp->b_bcount);
11958 if (TAILQ_EMPTY(&indirdep->ir_trunc)) {
11959 free(indirdep->ir_saveddata, M_INDIRDEP);
11960 indirdep->ir_saveddata = NULL;
11961 }
11962 chgs = 1;
11963 }
11964 indirdep->ir_state &= ~(UNDONE | IOSTARTED);
11965 indirdep->ir_state |= ATTACHED;
11966 /*
11967 * If the write did not succeed, we have done all the roll-forward
11968 * operations, but we cannot take the actions that will allow its
11969 * dependencies to be processed.
11970 */
11971 if ((flags & WRITESUCCEEDED) == 0) {
11972 stat_indir_blk_ptrs++;
11973 bdirty(bp);
11974 return (1);
11975 }
11976 /*
11977 * Move allocindirs with written pointers to the completehd if
11978 * the indirdep's pointer is not yet written. Otherwise
11979 * free them here.
11980 */
11981 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL) {
11982 LIST_REMOVE(aip, ai_next);
11983 if ((indirdep->ir_state & DEPCOMPLETE) == 0) {
11984 LIST_INSERT_HEAD(&indirdep->ir_completehd, aip,
11985 ai_next);
11986 newblk_freefrag(&aip->ai_block);
11987 continue;
11988 }
11989 free_newblk(&aip->ai_block);
11990 }
11991 /*
11992 * Move allocindirs that have finished dependency processing from
11993 * the done list to the write list after updating the pointers.
11994 */
11995 if (TAILQ_EMPTY(&indirdep->ir_trunc)) {
11996 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL) {
11997 handle_allocindir_partdone(aip);
11998 if (aip == LIST_FIRST(&indirdep->ir_donehd))
11999 panic("disk_write_complete: not gone");
12000 chgs = 1;
12001 }
12002 }
12003 /*
12004 * Preserve the indirdep if there were any changes or if it is not
12005 * yet valid on disk.
12006 */
12007 if (chgs) {
12008 stat_indir_blk_ptrs++;
12009 bdirty(bp);
12010 return (1);
12011 }
12012 /*
12013 * If there were no changes we can discard the savedbp and detach
12014 * ourselves from the buf. We are only carrying completed pointers
12015 * in this case.
12016 */
12017 sbp = indirdep->ir_savebp;
12018 sbp->b_flags |= B_INVAL | B_NOCACHE;
12019 indirdep->ir_savebp = NULL;
12020 indirdep->ir_bp = NULL;
12021 if (*bpp != NULL)
12022 panic("handle_written_indirdep: bp already exists.");
12023 *bpp = sbp;
12024 /*
12025 * The indirdep may not be freed until its parent points at it.
12026 */
12027 if (indirdep->ir_state & DEPCOMPLETE)
12028 free_indirdep(indirdep);
12029
12030 return (0);
12031 }
12032
12033 /*
12034 * Process a diradd entry after its dependent inode has been written.
12035 */
12036 static void
diradd_inode_written(struct diradd * dap,struct inodedep * inodedep)12037 diradd_inode_written(
12038 struct diradd *dap,
12039 struct inodedep *inodedep)
12040 {
12041
12042 LOCK_OWNED(VFSTOUFS(dap->da_list.wk_mp));
12043 dap->da_state |= COMPLETE;
12044 complete_diradd(dap);
12045 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list);
12046 }
12047
12048 /*
12049 * Returns true if the bmsafemap will have rollbacks when written. Must only
12050 * be called with the per-filesystem lock and the buf lock on the cg held.
12051 */
12052 static int
bmsafemap_backgroundwrite(struct bmsafemap * bmsafemap,struct buf * bp)12053 bmsafemap_backgroundwrite(
12054 struct bmsafemap *bmsafemap,
12055 struct buf *bp)
12056 {
12057 int dirty;
12058
12059 LOCK_OWNED(VFSTOUFS(bmsafemap->sm_list.wk_mp));
12060 dirty = !LIST_EMPTY(&bmsafemap->sm_jaddrefhd) |
12061 !LIST_EMPTY(&bmsafemap->sm_jnewblkhd);
12062 /*
12063 * If we're initiating a background write we need to process the
12064 * rollbacks as they exist now, not as they exist when IO starts.
12065 * No other consumers will look at the contents of the shadowed
12066 * buf so this is safe to do here.
12067 */
12068 if (bp->b_xflags & BX_BKGRDMARKER)
12069 initiate_write_bmsafemap(bmsafemap, bp);
12070
12071 return (dirty);
12072 }
12073
12074 /*
12075 * Re-apply an allocation when a cg write is complete.
12076 */
12077 static int
jnewblk_rollforward(struct jnewblk * jnewblk,struct fs * fs,struct cg * cgp,uint8_t * blksfree)12078 jnewblk_rollforward(
12079 struct jnewblk *jnewblk,
12080 struct fs *fs,
12081 struct cg *cgp,
12082 uint8_t *blksfree)
12083 {
12084 ufs1_daddr_t fragno;
12085 ufs2_daddr_t blkno;
12086 long cgbno, bbase;
12087 int frags, blk;
12088 int i;
12089
12090 frags = 0;
12091 cgbno = dtogd(fs, jnewblk->jn_blkno);
12092 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) {
12093 if (isclr(blksfree, cgbno + i))
12094 panic("jnewblk_rollforward: re-allocated fragment");
12095 frags++;
12096 }
12097 if (frags == fs->fs_frag) {
12098 blkno = fragstoblks(fs, cgbno);
12099 ffs_clrblock(fs, blksfree, (long)blkno);
12100 ffs_clusteracct(fs, cgp, blkno, -1);
12101 cgp->cg_cs.cs_nbfree--;
12102 } else {
12103 bbase = cgbno - fragnum(fs, cgbno);
12104 cgbno += jnewblk->jn_oldfrags;
12105 /* If a complete block had been reassembled, account for it. */
12106 fragno = fragstoblks(fs, bbase);
12107 if (ffs_isblock(fs, blksfree, fragno)) {
12108 cgp->cg_cs.cs_nffree += fs->fs_frag;
12109 ffs_clusteracct(fs, cgp, fragno, -1);
12110 cgp->cg_cs.cs_nbfree--;
12111 }
12112 /* Decrement the old frags. */
12113 blk = blkmap(fs, blksfree, bbase);
12114 ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
12115 /* Allocate the fragment */
12116 for (i = 0; i < frags; i++)
12117 clrbit(blksfree, cgbno + i);
12118 cgp->cg_cs.cs_nffree -= frags;
12119 /* Add back in counts associated with the new frags */
12120 blk = blkmap(fs, blksfree, bbase);
12121 ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
12122 }
12123 return (frags);
12124 }
12125
12126 /*
12127 * Complete a write to a bmsafemap structure. Roll forward any bitmap
12128 * changes if it's not a background write. Set all written dependencies
12129 * to DEPCOMPLETE and free the structure if possible.
12130 *
12131 * If the write did not succeed, we will do all the roll-forward
12132 * operations, but we will not take the actions that will allow its
12133 * dependencies to be processed.
12134 */
12135 static int
handle_written_bmsafemap(struct bmsafemap * bmsafemap,struct buf * bp,int flags)12136 handle_written_bmsafemap(
12137 struct bmsafemap *bmsafemap,
12138 struct buf *bp,
12139 int flags)
12140 {
12141 struct newblk *newblk;
12142 struct inodedep *inodedep;
12143 struct jaddref *jaddref, *jatmp;
12144 struct jnewblk *jnewblk, *jntmp;
12145 struct ufsmount *ump;
12146 uint8_t *inosused;
12147 uint8_t *blksfree;
12148 struct cg *cgp;
12149 struct fs *fs;
12150 ino_t ino;
12151 int foreground;
12152 int chgs;
12153
12154 if ((bmsafemap->sm_state & IOSTARTED) == 0)
12155 panic("handle_written_bmsafemap: Not started\n");
12156 ump = VFSTOUFS(bmsafemap->sm_list.wk_mp);
12157 chgs = 0;
12158 bmsafemap->sm_state &= ~IOSTARTED;
12159 foreground = (bp->b_xflags & BX_BKGRDMARKER) == 0;
12160 /*
12161 * If write was successful, release journal work that was waiting
12162 * on the write. Otherwise move the work back.
12163 */
12164 if (flags & WRITESUCCEEDED)
12165 handle_jwork(&bmsafemap->sm_freewr);
12166 else
12167 LIST_CONCAT(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr,
12168 worklist, wk_list);
12169
12170 /*
12171 * Restore unwritten inode allocation pending jaddref writes.
12172 */
12173 if (!LIST_EMPTY(&bmsafemap->sm_jaddrefhd)) {
12174 cgp = (struct cg *)bp->b_data;
12175 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
12176 inosused = cg_inosused(cgp);
12177 LIST_FOREACH_SAFE(jaddref, &bmsafemap->sm_jaddrefhd,
12178 ja_bmdeps, jatmp) {
12179 if ((jaddref->ja_state & UNDONE) == 0)
12180 continue;
12181 ino = jaddref->ja_ino % fs->fs_ipg;
12182 if (isset(inosused, ino))
12183 panic("handle_written_bmsafemap: "
12184 "re-allocated inode");
12185 /* Do the roll-forward only if it's a real copy. */
12186 if (foreground) {
12187 if ((jaddref->ja_mode & IFMT) == IFDIR)
12188 cgp->cg_cs.cs_ndir++;
12189 cgp->cg_cs.cs_nifree--;
12190 setbit(inosused, ino);
12191 chgs = 1;
12192 }
12193 jaddref->ja_state &= ~UNDONE;
12194 jaddref->ja_state |= ATTACHED;
12195 free_jaddref(jaddref);
12196 }
12197 }
12198 /*
12199 * Restore any block allocations which are pending journal writes.
12200 */
12201 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) {
12202 cgp = (struct cg *)bp->b_data;
12203 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
12204 blksfree = cg_blksfree(cgp);
12205 LIST_FOREACH_SAFE(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps,
12206 jntmp) {
12207 if ((jnewblk->jn_state & UNDONE) == 0)
12208 continue;
12209 /* Do the roll-forward only if it's a real copy. */
12210 if (foreground &&
12211 jnewblk_rollforward(jnewblk, fs, cgp, blksfree))
12212 chgs = 1;
12213 jnewblk->jn_state &= ~(UNDONE | NEWBLOCK);
12214 jnewblk->jn_state |= ATTACHED;
12215 free_jnewblk(jnewblk);
12216 }
12217 }
12218 /*
12219 * If the write did not succeed, we have done all the roll-forward
12220 * operations, but we cannot take the actions that will allow its
12221 * dependencies to be processed.
12222 */
12223 if ((flags & WRITESUCCEEDED) == 0) {
12224 LIST_CONCAT(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr,
12225 newblk, nb_deps);
12226 LIST_CONCAT(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr,
12227 worklist, wk_list);
12228 if (foreground)
12229 bdirty(bp);
12230 return (1);
12231 }
12232 while ((newblk = LIST_FIRST(&bmsafemap->sm_newblkwr))) {
12233 newblk->nb_state |= DEPCOMPLETE;
12234 newblk->nb_state &= ~ONDEPLIST;
12235 newblk->nb_bmsafemap = NULL;
12236 LIST_REMOVE(newblk, nb_deps);
12237 if (newblk->nb_list.wk_type == D_ALLOCDIRECT)
12238 handle_allocdirect_partdone(
12239 WK_ALLOCDIRECT(&newblk->nb_list), NULL);
12240 else if (newblk->nb_list.wk_type == D_ALLOCINDIR)
12241 handle_allocindir_partdone(
12242 WK_ALLOCINDIR(&newblk->nb_list));
12243 else if (newblk->nb_list.wk_type != D_NEWBLK)
12244 panic("handle_written_bmsafemap: Unexpected type: %s",
12245 TYPENAME(newblk->nb_list.wk_type));
12246 }
12247 while ((inodedep = LIST_FIRST(&bmsafemap->sm_inodedepwr)) != NULL) {
12248 inodedep->id_state |= DEPCOMPLETE;
12249 inodedep->id_state &= ~ONDEPLIST;
12250 LIST_REMOVE(inodedep, id_deps);
12251 inodedep->id_bmsafemap = NULL;
12252 }
12253 LIST_REMOVE(bmsafemap, sm_next);
12254 if (chgs == 0 && LIST_EMPTY(&bmsafemap->sm_jaddrefhd) &&
12255 LIST_EMPTY(&bmsafemap->sm_jnewblkhd) &&
12256 LIST_EMPTY(&bmsafemap->sm_newblkhd) &&
12257 LIST_EMPTY(&bmsafemap->sm_inodedephd) &&
12258 LIST_EMPTY(&bmsafemap->sm_freehd)) {
12259 LIST_REMOVE(bmsafemap, sm_hash);
12260 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP);
12261 return (0);
12262 }
12263 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next);
12264 if (foreground)
12265 bdirty(bp);
12266 return (1);
12267 }
12268
12269 /*
12270 * Try to free a mkdir dependency.
12271 */
12272 static void
complete_mkdir(struct mkdir * mkdir)12273 complete_mkdir(struct mkdir *mkdir)
12274 {
12275 struct diradd *dap;
12276
12277 if ((mkdir->md_state & ALLCOMPLETE) != ALLCOMPLETE)
12278 return;
12279 LIST_REMOVE(mkdir, md_mkdirs);
12280 dap = mkdir->md_diradd;
12281 dap->da_state &= ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY));
12282 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) {
12283 dap->da_state |= DEPCOMPLETE;
12284 complete_diradd(dap);
12285 }
12286 WORKITEM_FREE(mkdir, D_MKDIR);
12287 }
12288
12289 /*
12290 * Handle the completion of a mkdir dependency.
12291 */
12292 static void
handle_written_mkdir(struct mkdir * mkdir,int type)12293 handle_written_mkdir(struct mkdir *mkdir, int type)
12294 {
12295
12296 if ((mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)) != type)
12297 panic("handle_written_mkdir: bad type");
12298 mkdir->md_state |= COMPLETE;
12299 complete_mkdir(mkdir);
12300 }
12301
12302 static int
free_pagedep(struct pagedep * pagedep)12303 free_pagedep(struct pagedep *pagedep)
12304 {
12305 int i;
12306
12307 if (pagedep->pd_state & NEWBLOCK)
12308 return (0);
12309 if (!LIST_EMPTY(&pagedep->pd_dirremhd))
12310 return (0);
12311 for (i = 0; i < DAHASHSZ; i++)
12312 if (!LIST_EMPTY(&pagedep->pd_diraddhd[i]))
12313 return (0);
12314 if (!LIST_EMPTY(&pagedep->pd_pendinghd))
12315 return (0);
12316 if (!LIST_EMPTY(&pagedep->pd_jmvrefhd))
12317 return (0);
12318 if (pagedep->pd_state & ONWORKLIST)
12319 WORKLIST_REMOVE(&pagedep->pd_list);
12320 LIST_REMOVE(pagedep, pd_hash);
12321 WORKITEM_FREE(pagedep, D_PAGEDEP);
12322
12323 return (1);
12324 }
12325
12326 /*
12327 * Called from within softdep_disk_write_complete above.
12328 * A write operation was just completed. Removed inodes can
12329 * now be freed and associated block pointers may be committed.
12330 * Note that this routine is always called from interrupt level
12331 * with further interrupts from this device blocked.
12332 *
12333 * If the write did not succeed, we will do all the roll-forward
12334 * operations, but we will not take the actions that will allow its
12335 * dependencies to be processed.
12336 */
12337 static int
handle_written_filepage(struct pagedep * pagedep,struct buf * bp,int flags)12338 handle_written_filepage(
12339 struct pagedep *pagedep,
12340 struct buf *bp, /* buffer containing the written page */
12341 int flags)
12342 {
12343 struct dirrem *dirrem;
12344 struct diradd *dap, *nextdap;
12345 struct direct *ep;
12346 int i, chgs;
12347
12348 if ((pagedep->pd_state & IOSTARTED) == 0)
12349 panic("handle_written_filepage: not started");
12350 pagedep->pd_state &= ~IOSTARTED;
12351 if ((flags & WRITESUCCEEDED) == 0)
12352 goto rollforward;
12353 /*
12354 * Process any directory removals that have been committed.
12355 */
12356 while ((dirrem = LIST_FIRST(&pagedep->pd_dirremhd)) != NULL) {
12357 LIST_REMOVE(dirrem, dm_next);
12358 dirrem->dm_state |= COMPLETE;
12359 dirrem->dm_dirinum = pagedep->pd_ino;
12360 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd),
12361 ("handle_written_filepage: Journal entries not written."));
12362 add_to_worklist(&dirrem->dm_list, 0);
12363 }
12364 /*
12365 * Free any directory additions that have been committed.
12366 * If it is a newly allocated block, we have to wait until
12367 * the on-disk directory inode claims the new block.
12368 */
12369 if ((pagedep->pd_state & NEWBLOCK) == 0)
12370 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL)
12371 free_diradd(dap, NULL);
12372 rollforward:
12373 /*
12374 * Uncommitted directory entries must be restored.
12375 */
12376 for (chgs = 0, i = 0; i < DAHASHSZ; i++) {
12377 for (dap = LIST_FIRST(&pagedep->pd_diraddhd[i]); dap;
12378 dap = nextdap) {
12379 nextdap = LIST_NEXT(dap, da_pdlist);
12380 if (dap->da_state & ATTACHED)
12381 panic("handle_written_filepage: attached");
12382 ep = (struct direct *)
12383 ((char *)bp->b_data + dap->da_offset);
12384 ep->d_ino = dap->da_newinum;
12385 dap->da_state &= ~UNDONE;
12386 dap->da_state |= ATTACHED;
12387 chgs = 1;
12388 /*
12389 * If the inode referenced by the directory has
12390 * been written out, then the dependency can be
12391 * moved to the pending list.
12392 */
12393 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
12394 LIST_REMOVE(dap, da_pdlist);
12395 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap,
12396 da_pdlist);
12397 }
12398 }
12399 }
12400 /*
12401 * If there were any rollbacks in the directory, then it must be
12402 * marked dirty so that its will eventually get written back in
12403 * its correct form.
12404 */
12405 if (chgs || (flags & WRITESUCCEEDED) == 0) {
12406 if ((bp->b_flags & B_DELWRI) == 0)
12407 stat_dir_entry++;
12408 bdirty(bp);
12409 return (1);
12410 }
12411 /*
12412 * If we are not waiting for a new directory block to be
12413 * claimed by its inode, then the pagedep will be freed.
12414 * Otherwise it will remain to track any new entries on
12415 * the page in case they are fsync'ed.
12416 */
12417 free_pagedep(pagedep);
12418 return (0);
12419 }
12420
12421 /*
12422 * Writing back in-core inode structures.
12423 *
12424 * The filesystem only accesses an inode's contents when it occupies an
12425 * "in-core" inode structure. These "in-core" structures are separate from
12426 * the page frames used to cache inode blocks. Only the latter are
12427 * transferred to/from the disk. So, when the updated contents of the
12428 * "in-core" inode structure are copied to the corresponding in-memory inode
12429 * block, the dependencies are also transferred. The following procedure is
12430 * called when copying a dirty "in-core" inode to a cached inode block.
12431 */
12432
12433 /*
12434 * Called when an inode is loaded from disk. If the effective link count
12435 * differed from the actual link count when it was last flushed, then we
12436 * need to ensure that the correct effective link count is put back.
12437 */
12438 void
softdep_load_inodeblock(struct inode * ip)12439 softdep_load_inodeblock(
12440 struct inode *ip) /* the "in_core" copy of the inode */
12441 {
12442 struct inodedep *inodedep;
12443 struct ufsmount *ump;
12444
12445 ump = ITOUMP(ip);
12446 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
12447 ("softdep_load_inodeblock called on non-softdep filesystem"));
12448 /*
12449 * Check for alternate nlink count.
12450 */
12451 ip->i_effnlink = ip->i_nlink;
12452 ACQUIRE_LOCK(ump);
12453 if (inodedep_lookup(UFSTOVFS(ump), ip->i_number, 0, &inodedep) == 0) {
12454 FREE_LOCK(ump);
12455 return;
12456 }
12457 if (ip->i_nlink != inodedep->id_nlinkwrote &&
12458 inodedep->id_nlinkwrote != -1) {
12459 KASSERT(ip->i_nlink == 0 &&
12460 (ump->um_flags & UM_FSFAIL_CLEANUP) != 0,
12461 ("read bad i_nlink value"));
12462 ip->i_effnlink = ip->i_nlink = inodedep->id_nlinkwrote;
12463 }
12464 ip->i_effnlink -= inodedep->id_nlinkdelta;
12465 KASSERT(ip->i_effnlink >= 0,
12466 ("softdep_load_inodeblock: negative i_effnlink"));
12467 FREE_LOCK(ump);
12468 }
12469
12470 /*
12471 * This routine is called just before the "in-core" inode
12472 * information is to be copied to the in-memory inode block.
12473 * Recall that an inode block contains several inodes. If
12474 * the force flag is set, then the dependencies will be
12475 * cleared so that the update can always be made. Note that
12476 * the buffer is locked when this routine is called, so we
12477 * will never be in the middle of writing the inode block
12478 * to disk.
12479 */
12480 void
softdep_update_inodeblock(struct inode * ip,struct buf * bp,int waitfor)12481 softdep_update_inodeblock(
12482 struct inode *ip, /* the "in_core" copy of the inode */
12483 struct buf *bp, /* the buffer containing the inode block */
12484 int waitfor) /* nonzero => update must be allowed */
12485 {
12486 struct inodedep *inodedep;
12487 struct inoref *inoref;
12488 struct ufsmount *ump;
12489 struct worklist *wk;
12490 struct mount *mp;
12491 struct buf *ibp;
12492 struct fs *fs;
12493 int error;
12494
12495 ump = ITOUMP(ip);
12496 mp = UFSTOVFS(ump);
12497 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
12498 ("softdep_update_inodeblock called on non-softdep filesystem"));
12499 fs = ump->um_fs;
12500 /*
12501 * If the effective link count is not equal to the actual link
12502 * count, then we must track the difference in an inodedep while
12503 * the inode is (potentially) tossed out of the cache. Otherwise,
12504 * if there is no existing inodedep, then there are no dependencies
12505 * to track.
12506 */
12507 ACQUIRE_LOCK(ump);
12508 again:
12509 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) {
12510 FREE_LOCK(ump);
12511 if (ip->i_effnlink != ip->i_nlink)
12512 panic("softdep_update_inodeblock: bad link count");
12513 return;
12514 }
12515 /*
12516 * Preserve the freelink that is on disk. clear_unlinked_inodedep()
12517 * does not have access to the in-core ip so must write directly into
12518 * the inode block buffer when setting freelink.
12519 */
12520 if ((inodedep->id_state & UNLINKED) != 0) {
12521 if (fs->fs_magic == FS_UFS1_MAGIC)
12522 DIP_SET(ip, i_freelink,
12523 ((struct ufs1_dinode *)bp->b_data +
12524 ino_to_fsbo(fs, ip->i_number))->di_freelink);
12525 else
12526 DIP_SET(ip, i_freelink,
12527 ((struct ufs2_dinode *)bp->b_data +
12528 ino_to_fsbo(fs, ip->i_number))->di_freelink);
12529 }
12530 KASSERT(ip->i_nlink >= inodedep->id_nlinkdelta,
12531 ("softdep_update_inodeblock inconsistent ip %p i_nlink %d "
12532 "inodedep %p id_nlinkdelta %jd",
12533 ip, ip->i_nlink, inodedep, (intmax_t)inodedep->id_nlinkdelta));
12534 inodedep->id_nlinkwrote = ip->i_nlink;
12535 if (inodedep->id_nlinkdelta != ip->i_nlink - ip->i_effnlink)
12536 panic("softdep_update_inodeblock: bad delta");
12537 /*
12538 * If we're flushing all dependencies we must also move any waiting
12539 * for journal writes onto the bufwait list prior to I/O.
12540 */
12541 if (waitfor) {
12542 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
12543 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
12544 == DEPCOMPLETE) {
12545 jwait(&inoref->if_list, MNT_WAIT);
12546 goto again;
12547 }
12548 }
12549 }
12550 /*
12551 * Changes have been initiated. Anything depending on these
12552 * changes cannot occur until this inode has been written.
12553 */
12554 inodedep->id_state &= ~COMPLETE;
12555 if ((inodedep->id_state & ONWORKLIST) == 0)
12556 WORKLIST_INSERT(&bp->b_dep, &inodedep->id_list);
12557 /*
12558 * Any new dependencies associated with the incore inode must
12559 * now be moved to the list associated with the buffer holding
12560 * the in-memory copy of the inode. Once merged process any
12561 * allocdirects that are completed by the merger.
12562 */
12563 merge_inode_lists(&inodedep->id_newinoupdt, &inodedep->id_inoupdt);
12564 if (!TAILQ_EMPTY(&inodedep->id_inoupdt))
12565 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_inoupdt),
12566 NULL);
12567 merge_inode_lists(&inodedep->id_newextupdt, &inodedep->id_extupdt);
12568 if (!TAILQ_EMPTY(&inodedep->id_extupdt))
12569 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_extupdt),
12570 NULL);
12571 /*
12572 * Now that the inode has been pushed into the buffer, the
12573 * operations dependent on the inode being written to disk
12574 * can be moved to the id_bufwait so that they will be
12575 * processed when the buffer I/O completes.
12576 */
12577 while ((wk = LIST_FIRST(&inodedep->id_inowait)) != NULL) {
12578 WORKLIST_REMOVE(wk);
12579 WORKLIST_INSERT(&inodedep->id_bufwait, wk);
12580 }
12581 /*
12582 * Newly allocated inodes cannot be written until the bitmap
12583 * that allocates them have been written (indicated by
12584 * DEPCOMPLETE being set in id_state). If we are doing a
12585 * forced sync (e.g., an fsync on a file), we force the bitmap
12586 * to be written so that the update can be done.
12587 */
12588 if (waitfor == 0) {
12589 FREE_LOCK(ump);
12590 return;
12591 }
12592 retry:
12593 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) != 0) {
12594 FREE_LOCK(ump);
12595 return;
12596 }
12597 ibp = inodedep->id_bmsafemap->sm_buf;
12598 ibp = getdirtybuf(ibp, LOCK_PTR(ump), MNT_WAIT);
12599 if (ibp == NULL) {
12600 /*
12601 * If ibp came back as NULL, the dependency could have been
12602 * freed while we slept. Look it up again, and check to see
12603 * that it has completed.
12604 */
12605 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0)
12606 goto retry;
12607 FREE_LOCK(ump);
12608 return;
12609 }
12610 FREE_LOCK(ump);
12611 if ((error = bwrite(ibp)) != 0)
12612 softdep_error("softdep_update_inodeblock: bwrite", error);
12613 }
12614
12615 /*
12616 * Merge the a new inode dependency list (such as id_newinoupdt) into an
12617 * old inode dependency list (such as id_inoupdt).
12618 */
12619 static void
merge_inode_lists(struct allocdirectlst * newlisthead,struct allocdirectlst * oldlisthead)12620 merge_inode_lists(
12621 struct allocdirectlst *newlisthead,
12622 struct allocdirectlst *oldlisthead)
12623 {
12624 struct allocdirect *listadp, *newadp;
12625
12626 newadp = TAILQ_FIRST(newlisthead);
12627 if (newadp != NULL)
12628 LOCK_OWNED(VFSTOUFS(newadp->ad_block.nb_list.wk_mp));
12629 for (listadp = TAILQ_FIRST(oldlisthead); listadp && newadp;) {
12630 if (listadp->ad_offset < newadp->ad_offset) {
12631 listadp = TAILQ_NEXT(listadp, ad_next);
12632 continue;
12633 }
12634 TAILQ_REMOVE(newlisthead, newadp, ad_next);
12635 TAILQ_INSERT_BEFORE(listadp, newadp, ad_next);
12636 if (listadp->ad_offset == newadp->ad_offset) {
12637 allocdirect_merge(oldlisthead, newadp,
12638 listadp);
12639 listadp = newadp;
12640 }
12641 newadp = TAILQ_FIRST(newlisthead);
12642 }
12643 while ((newadp = TAILQ_FIRST(newlisthead)) != NULL) {
12644 TAILQ_REMOVE(newlisthead, newadp, ad_next);
12645 TAILQ_INSERT_TAIL(oldlisthead, newadp, ad_next);
12646 }
12647 }
12648
12649 /*
12650 * If we are doing an fsync, then we must ensure that any directory
12651 * entries for the inode have been written after the inode gets to disk.
12652 */
12653 int
softdep_fsync(struct vnode * vp)12654 softdep_fsync(
12655 struct vnode *vp) /* the "in_core" copy of the inode */
12656 {
12657 struct inodedep *inodedep;
12658 struct pagedep *pagedep;
12659 struct inoref *inoref;
12660 struct ufsmount *ump;
12661 struct worklist *wk;
12662 struct diradd *dap;
12663 struct mount *mp;
12664 struct vnode *pvp;
12665 struct inode *ip;
12666 struct buf *bp;
12667 struct fs *fs;
12668 struct thread *td = curthread;
12669 int error, flushparent, pagedep_new_block;
12670 ino_t parentino;
12671 ufs_lbn_t lbn;
12672
12673 ip = VTOI(vp);
12674 mp = vp->v_mount;
12675 ump = VFSTOUFS(mp);
12676 fs = ump->um_fs;
12677 if (MOUNTEDSOFTDEP(mp) == 0)
12678 return (0);
12679 ACQUIRE_LOCK(ump);
12680 restart:
12681 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) {
12682 FREE_LOCK(ump);
12683 return (0);
12684 }
12685 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
12686 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
12687 == DEPCOMPLETE) {
12688 jwait(&inoref->if_list, MNT_WAIT);
12689 goto restart;
12690 }
12691 }
12692 if (!LIST_EMPTY(&inodedep->id_inowait) ||
12693 !TAILQ_EMPTY(&inodedep->id_extupdt) ||
12694 !TAILQ_EMPTY(&inodedep->id_newextupdt) ||
12695 !TAILQ_EMPTY(&inodedep->id_inoupdt) ||
12696 !TAILQ_EMPTY(&inodedep->id_newinoupdt))
12697 panic("softdep_fsync: pending ops %p", inodedep);
12698 for (error = 0, flushparent = 0; ; ) {
12699 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) == NULL)
12700 break;
12701 if (wk->wk_type != D_DIRADD)
12702 panic("softdep_fsync: Unexpected type %s",
12703 TYPENAME(wk->wk_type));
12704 dap = WK_DIRADD(wk);
12705 /*
12706 * Flush our parent if this directory entry has a MKDIR_PARENT
12707 * dependency or is contained in a newly allocated block.
12708 */
12709 if (dap->da_state & DIRCHG)
12710 pagedep = dap->da_previous->dm_pagedep;
12711 else
12712 pagedep = dap->da_pagedep;
12713 parentino = pagedep->pd_ino;
12714 lbn = pagedep->pd_lbn;
12715 if ((dap->da_state & (MKDIR_BODY | COMPLETE)) != COMPLETE)
12716 panic("softdep_fsync: dirty");
12717 if ((dap->da_state & MKDIR_PARENT) ||
12718 (pagedep->pd_state & NEWBLOCK))
12719 flushparent = 1;
12720 else
12721 flushparent = 0;
12722 /*
12723 * If we are being fsync'ed as part of vgone'ing this vnode,
12724 * then we will not be able to release and recover the
12725 * vnode below, so we just have to give up on writing its
12726 * directory entry out. It will eventually be written, just
12727 * not now, but then the user was not asking to have it
12728 * written, so we are not breaking any promises.
12729 */
12730 if (VN_IS_DOOMED(vp))
12731 break;
12732 /*
12733 * We prevent deadlock by always fetching inodes from the
12734 * root, moving down the directory tree. Thus, when fetching
12735 * our parent directory, we first try to get the lock. If
12736 * that fails, we must unlock ourselves before requesting
12737 * the lock on our parent. See the comment in ufs_lookup
12738 * for details on possible races.
12739 */
12740 FREE_LOCK(ump);
12741 error = get_parent_vp(vp, mp, parentino, NULL, NULL, NULL,
12742 &pvp);
12743 if (error == ERELOOKUP)
12744 error = 0;
12745 if (error != 0)
12746 return (error);
12747 /*
12748 * All MKDIR_PARENT dependencies and all the NEWBLOCK pagedeps
12749 * that are contained in direct blocks will be resolved by
12750 * doing a ffs_update. Pagedeps contained in indirect blocks
12751 * may require a complete sync'ing of the directory. So, we
12752 * try the cheap and fast ffs_update first, and if that fails,
12753 * then we do the slower ffs_syncvnode of the directory.
12754 */
12755 if (flushparent) {
12756 int locked;
12757
12758 if ((error = ffs_update(pvp, 1)) != 0) {
12759 vput(pvp);
12760 return (error);
12761 }
12762 ACQUIRE_LOCK(ump);
12763 locked = 1;
12764 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) {
12765 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) != NULL) {
12766 if (wk->wk_type != D_DIRADD)
12767 panic("softdep_fsync: Unexpected type %s",
12768 TYPENAME(wk->wk_type));
12769 dap = WK_DIRADD(wk);
12770 if (dap->da_state & DIRCHG)
12771 pagedep = dap->da_previous->dm_pagedep;
12772 else
12773 pagedep = dap->da_pagedep;
12774 pagedep_new_block = pagedep->pd_state & NEWBLOCK;
12775 FREE_LOCK(ump);
12776 locked = 0;
12777 if (pagedep_new_block) {
12778 VOP_UNLOCK(vp);
12779 error = ffs_syncvnode(pvp,
12780 MNT_WAIT, 0);
12781 if (error == 0)
12782 error = ERELOOKUP;
12783 vput(pvp);
12784 vn_lock(vp, LK_EXCLUSIVE |
12785 LK_RETRY);
12786 return (error);
12787 }
12788 }
12789 }
12790 if (locked)
12791 FREE_LOCK(ump);
12792 }
12793 /*
12794 * Flush directory page containing the inode's name.
12795 */
12796 error = bread(pvp, lbn, blksize(fs, VTOI(pvp), lbn), td->td_ucred,
12797 &bp);
12798 if (error == 0)
12799 error = bwrite(bp);
12800 else
12801 brelse(bp);
12802 vput(pvp);
12803 if (!ffs_fsfail_cleanup(ump, error))
12804 return (error);
12805 ACQUIRE_LOCK(ump);
12806 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0)
12807 break;
12808 }
12809 FREE_LOCK(ump);
12810 return (0);
12811 }
12812
12813 /*
12814 * Flush all the dirty bitmaps associated with the block device
12815 * before flushing the rest of the dirty blocks so as to reduce
12816 * the number of dependencies that will have to be rolled back.
12817 *
12818 * XXX Unused?
12819 */
12820 void
softdep_fsync_mountdev(struct vnode * vp)12821 softdep_fsync_mountdev(struct vnode *vp)
12822 {
12823 struct buf *bp, *nbp;
12824 struct worklist *wk;
12825 struct bufobj *bo;
12826
12827 if (!vn_isdisk(vp))
12828 panic("softdep_fsync_mountdev: vnode not a disk");
12829 bo = &vp->v_bufobj;
12830 restart:
12831 BO_LOCK(bo);
12832 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
12833 /*
12834 * If it is already scheduled, skip to the next buffer.
12835 */
12836 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL))
12837 continue;
12838
12839 if ((bp->b_flags & B_DELWRI) == 0)
12840 panic("softdep_fsync_mountdev: not dirty");
12841 /*
12842 * We are only interested in bitmaps with outstanding
12843 * dependencies.
12844 */
12845 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL ||
12846 wk->wk_type != D_BMSAFEMAP ||
12847 (bp->b_vflags & BV_BKGRDINPROG)) {
12848 BUF_UNLOCK(bp);
12849 continue;
12850 }
12851 BO_UNLOCK(bo);
12852 bremfree(bp);
12853 (void) bawrite(bp);
12854 goto restart;
12855 }
12856 drain_output(vp);
12857 BO_UNLOCK(bo);
12858 }
12859
12860 /*
12861 * Sync all cylinder groups that were dirty at the time this function is
12862 * called. Newly dirtied cgs will be inserted before the sentinel. This
12863 * is used to flush freedep activity that may be holding up writes to a
12864 * indirect block.
12865 */
12866 static int
sync_cgs(struct mount * mp,int waitfor)12867 sync_cgs(struct mount *mp, int waitfor)
12868 {
12869 struct bmsafemap *bmsafemap;
12870 struct bmsafemap *sentinel;
12871 struct ufsmount *ump;
12872 struct buf *bp;
12873 int error;
12874
12875 sentinel = malloc(sizeof(*sentinel), M_BMSAFEMAP, M_ZERO | M_WAITOK);
12876 sentinel->sm_cg = -1;
12877 ump = VFSTOUFS(mp);
12878 error = 0;
12879 ACQUIRE_LOCK(ump);
12880 LIST_INSERT_HEAD(&ump->softdep_dirtycg, sentinel, sm_next);
12881 for (bmsafemap = LIST_NEXT(sentinel, sm_next); bmsafemap != NULL;
12882 bmsafemap = LIST_NEXT(sentinel, sm_next)) {
12883 /* Skip sentinels and cgs with no work to release. */
12884 if (bmsafemap->sm_cg == -1 ||
12885 (LIST_EMPTY(&bmsafemap->sm_freehd) &&
12886 LIST_EMPTY(&bmsafemap->sm_freewr))) {
12887 LIST_REMOVE(sentinel, sm_next);
12888 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next);
12889 continue;
12890 }
12891 /*
12892 * If we don't get the lock and we're waiting try again, if
12893 * not move on to the next buf and try to sync it.
12894 */
12895 bp = getdirtybuf(bmsafemap->sm_buf, LOCK_PTR(ump), waitfor);
12896 if (bp == NULL && waitfor == MNT_WAIT)
12897 continue;
12898 LIST_REMOVE(sentinel, sm_next);
12899 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next);
12900 if (bp == NULL)
12901 continue;
12902 FREE_LOCK(ump);
12903 if (waitfor == MNT_NOWAIT)
12904 bawrite(bp);
12905 else
12906 error = bwrite(bp);
12907 ACQUIRE_LOCK(ump);
12908 if (error)
12909 break;
12910 }
12911 LIST_REMOVE(sentinel, sm_next);
12912 FREE_LOCK(ump);
12913 free(sentinel, M_BMSAFEMAP);
12914 return (error);
12915 }
12916
12917 /*
12918 * This routine is called when we are trying to synchronously flush a
12919 * file. This routine must eliminate any filesystem metadata dependencies
12920 * so that the syncing routine can succeed.
12921 */
12922 int
softdep_sync_metadata(struct vnode * vp)12923 softdep_sync_metadata(struct vnode *vp)
12924 {
12925 struct inode *ip;
12926 int error;
12927
12928 ip = VTOI(vp);
12929 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0,
12930 ("softdep_sync_metadata called on non-softdep filesystem"));
12931 /*
12932 * Ensure that any direct block dependencies have been cleared,
12933 * truncations are started, and inode references are journaled.
12934 */
12935 ACQUIRE_LOCK(VFSTOUFS(vp->v_mount));
12936 /*
12937 * Write all journal records to prevent rollbacks on devvp.
12938 */
12939 if (vp->v_type == VCHR)
12940 softdep_flushjournal(vp->v_mount);
12941 error = flush_inodedep_deps(vp, vp->v_mount, ip->i_number);
12942 /*
12943 * Ensure that all truncates are written so we won't find deps on
12944 * indirect blocks.
12945 */
12946 process_truncates(vp);
12947 FREE_LOCK(VFSTOUFS(vp->v_mount));
12948
12949 return (error);
12950 }
12951
12952 /*
12953 * This routine is called when we are attempting to sync a buf with
12954 * dependencies. If waitfor is MNT_NOWAIT it attempts to schedule any
12955 * other IO it can but returns EBUSY if the buffer is not yet able to
12956 * be written. Dependencies which will not cause rollbacks will always
12957 * return 0.
12958 */
12959 int
softdep_sync_buf(struct vnode * vp,struct buf * bp,int waitfor)12960 softdep_sync_buf(struct vnode *vp,
12961 struct buf *bp,
12962 int waitfor)
12963 {
12964 struct indirdep *indirdep;
12965 struct pagedep *pagedep;
12966 struct allocindir *aip;
12967 struct newblk *newblk;
12968 struct ufsmount *ump;
12969 struct buf *nbp;
12970 struct worklist *wk;
12971 int i, error;
12972
12973 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0,
12974 ("softdep_sync_buf called on non-softdep filesystem"));
12975 /*
12976 * For VCHR we just don't want to force flush any dependencies that
12977 * will cause rollbacks.
12978 */
12979 if (vp->v_type == VCHR) {
12980 if (waitfor == MNT_NOWAIT && softdep_count_dependencies(bp, 0))
12981 return (EBUSY);
12982 return (0);
12983 }
12984 ump = VFSTOUFS(vp->v_mount);
12985 ACQUIRE_LOCK(ump);
12986 /*
12987 * As we hold the buffer locked, none of its dependencies
12988 * will disappear.
12989 */
12990 error = 0;
12991 top:
12992 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
12993 switch (wk->wk_type) {
12994 case D_ALLOCDIRECT:
12995 case D_ALLOCINDIR:
12996 newblk = WK_NEWBLK(wk);
12997 if (newblk->nb_jnewblk != NULL) {
12998 if (waitfor == MNT_NOWAIT) {
12999 error = EBUSY;
13000 goto out_unlock;
13001 }
13002 jwait(&newblk->nb_jnewblk->jn_list, waitfor);
13003 goto top;
13004 }
13005 if (newblk->nb_state & DEPCOMPLETE ||
13006 waitfor == MNT_NOWAIT)
13007 continue;
13008 nbp = newblk->nb_bmsafemap->sm_buf;
13009 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor);
13010 if (nbp == NULL)
13011 goto top;
13012 FREE_LOCK(ump);
13013 if ((error = bwrite(nbp)) != 0)
13014 goto out;
13015 ACQUIRE_LOCK(ump);
13016 continue;
13017
13018 case D_INDIRDEP:
13019 indirdep = WK_INDIRDEP(wk);
13020 if (waitfor == MNT_NOWAIT) {
13021 if (!TAILQ_EMPTY(&indirdep->ir_trunc) ||
13022 !LIST_EMPTY(&indirdep->ir_deplisthd)) {
13023 error = EBUSY;
13024 goto out_unlock;
13025 }
13026 }
13027 if (!TAILQ_EMPTY(&indirdep->ir_trunc))
13028 panic("softdep_sync_buf: truncation pending.");
13029 restart:
13030 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) {
13031 newblk = (struct newblk *)aip;
13032 if (newblk->nb_jnewblk != NULL) {
13033 jwait(&newblk->nb_jnewblk->jn_list,
13034 waitfor);
13035 goto restart;
13036 }
13037 if (newblk->nb_state & DEPCOMPLETE)
13038 continue;
13039 nbp = newblk->nb_bmsafemap->sm_buf;
13040 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor);
13041 if (nbp == NULL)
13042 goto restart;
13043 FREE_LOCK(ump);
13044 if ((error = bwrite(nbp)) != 0)
13045 goto out;
13046 ACQUIRE_LOCK(ump);
13047 goto restart;
13048 }
13049 continue;
13050
13051 case D_PAGEDEP:
13052 /*
13053 * Only flush directory entries in synchronous passes.
13054 */
13055 if (waitfor != MNT_WAIT) {
13056 error = EBUSY;
13057 goto out_unlock;
13058 }
13059 /*
13060 * While syncing snapshots, we must allow recursive
13061 * lookups.
13062 */
13063 BUF_AREC(bp);
13064 /*
13065 * We are trying to sync a directory that may
13066 * have dependencies on both its own metadata
13067 * and/or dependencies on the inodes of any
13068 * recently allocated files. We walk its diradd
13069 * lists pushing out the associated inode.
13070 */
13071 pagedep = WK_PAGEDEP(wk);
13072 for (i = 0; i < DAHASHSZ; i++) {
13073 if (LIST_FIRST(&pagedep->pd_diraddhd[i]) == 0)
13074 continue;
13075 error = flush_pagedep_deps(vp, wk->wk_mp,
13076 &pagedep->pd_diraddhd[i], bp);
13077 if (error != 0) {
13078 if (error != ERELOOKUP)
13079 BUF_NOREC(bp);
13080 goto out_unlock;
13081 }
13082 }
13083 BUF_NOREC(bp);
13084 continue;
13085
13086 case D_FREEWORK:
13087 case D_FREEDEP:
13088 case D_JSEGDEP:
13089 case D_JNEWBLK:
13090 continue;
13091
13092 default:
13093 panic("softdep_sync_buf: Unknown type %s",
13094 TYPENAME(wk->wk_type));
13095 /* NOTREACHED */
13096 }
13097 }
13098 out_unlock:
13099 FREE_LOCK(ump);
13100 out:
13101 return (error);
13102 }
13103
13104 /*
13105 * Flush the dependencies associated with an inodedep.
13106 */
13107 static int
flush_inodedep_deps(struct vnode * vp,struct mount * mp,ino_t ino)13108 flush_inodedep_deps(
13109 struct vnode *vp,
13110 struct mount *mp,
13111 ino_t ino)
13112 {
13113 struct inodedep *inodedep;
13114 struct inoref *inoref;
13115 struct ufsmount *ump;
13116 int error, waitfor;
13117
13118 /*
13119 * This work is done in two passes. The first pass grabs most
13120 * of the buffers and begins asynchronously writing them. The
13121 * only way to wait for these asynchronous writes is to sleep
13122 * on the filesystem vnode which may stay busy for a long time
13123 * if the filesystem is active. So, instead, we make a second
13124 * pass over the dependencies blocking on each write. In the
13125 * usual case we will be blocking against a write that we
13126 * initiated, so when it is done the dependency will have been
13127 * resolved. Thus the second pass is expected to end quickly.
13128 * We give a brief window at the top of the loop to allow
13129 * any pending I/O to complete.
13130 */
13131 ump = VFSTOUFS(mp);
13132 LOCK_OWNED(ump);
13133 for (error = 0, waitfor = MNT_NOWAIT; ; ) {
13134 if (error)
13135 return (error);
13136 FREE_LOCK(ump);
13137 ACQUIRE_LOCK(ump);
13138 restart:
13139 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0)
13140 return (0);
13141 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
13142 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
13143 == DEPCOMPLETE) {
13144 jwait(&inoref->if_list, MNT_WAIT);
13145 goto restart;
13146 }
13147 }
13148 if (flush_deplist(&inodedep->id_inoupdt, waitfor, &error) ||
13149 flush_deplist(&inodedep->id_newinoupdt, waitfor, &error) ||
13150 flush_deplist(&inodedep->id_extupdt, waitfor, &error) ||
13151 flush_deplist(&inodedep->id_newextupdt, waitfor, &error))
13152 continue;
13153 /*
13154 * If pass2, we are done, otherwise do pass 2.
13155 */
13156 if (waitfor == MNT_WAIT)
13157 break;
13158 waitfor = MNT_WAIT;
13159 }
13160 /*
13161 * Try freeing inodedep in case all dependencies have been removed.
13162 */
13163 if (inodedep_lookup(mp, ino, 0, &inodedep) != 0)
13164 (void) free_inodedep(inodedep);
13165 return (0);
13166 }
13167
13168 /*
13169 * Flush an inode dependency list.
13170 */
13171 static int
flush_deplist(struct allocdirectlst * listhead,int waitfor,int * errorp)13172 flush_deplist(
13173 struct allocdirectlst *listhead,
13174 int waitfor,
13175 int *errorp)
13176 {
13177 struct allocdirect *adp;
13178 struct newblk *newblk;
13179 struct ufsmount *ump;
13180 struct buf *bp;
13181
13182 if ((adp = TAILQ_FIRST(listhead)) == NULL)
13183 return (0);
13184 ump = VFSTOUFS(adp->ad_list.wk_mp);
13185 LOCK_OWNED(ump);
13186 TAILQ_FOREACH(adp, listhead, ad_next) {
13187 newblk = (struct newblk *)adp;
13188 if (newblk->nb_jnewblk != NULL) {
13189 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT);
13190 return (1);
13191 }
13192 if (newblk->nb_state & DEPCOMPLETE)
13193 continue;
13194 bp = newblk->nb_bmsafemap->sm_buf;
13195 bp = getdirtybuf(bp, LOCK_PTR(ump), waitfor);
13196 if (bp == NULL) {
13197 if (waitfor == MNT_NOWAIT)
13198 continue;
13199 return (1);
13200 }
13201 FREE_LOCK(ump);
13202 if (waitfor == MNT_NOWAIT)
13203 bawrite(bp);
13204 else
13205 *errorp = bwrite(bp);
13206 ACQUIRE_LOCK(ump);
13207 return (1);
13208 }
13209 return (0);
13210 }
13211
13212 /*
13213 * Flush dependencies associated with an allocdirect block.
13214 */
13215 static int
flush_newblk_dep(struct vnode * vp,struct mount * mp,ufs_lbn_t lbn)13216 flush_newblk_dep(
13217 struct vnode *vp,
13218 struct mount *mp,
13219 ufs_lbn_t lbn)
13220 {
13221 struct newblk *newblk;
13222 struct ufsmount *ump;
13223 struct bufobj *bo;
13224 struct inode *ip;
13225 struct buf *bp;
13226 ufs2_daddr_t blkno;
13227 int error;
13228
13229 error = 0;
13230 bo = &vp->v_bufobj;
13231 ip = VTOI(vp);
13232 blkno = DIP(ip, i_db[lbn]);
13233 if (blkno == 0)
13234 panic("flush_newblk_dep: Missing block");
13235 ump = VFSTOUFS(mp);
13236 ACQUIRE_LOCK(ump);
13237 /*
13238 * Loop until all dependencies related to this block are satisfied.
13239 * We must be careful to restart after each sleep in case a write
13240 * completes some part of this process for us.
13241 */
13242 for (;;) {
13243 if (newblk_lookup(mp, blkno, 0, &newblk) == 0) {
13244 FREE_LOCK(ump);
13245 break;
13246 }
13247 if (newblk->nb_list.wk_type != D_ALLOCDIRECT)
13248 panic("flush_newblk_dep: Bad newblk %p", newblk);
13249 /*
13250 * Flush the journal.
13251 */
13252 if (newblk->nb_jnewblk != NULL) {
13253 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT);
13254 continue;
13255 }
13256 /*
13257 * Write the bitmap dependency.
13258 */
13259 if ((newblk->nb_state & DEPCOMPLETE) == 0) {
13260 bp = newblk->nb_bmsafemap->sm_buf;
13261 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT);
13262 if (bp == NULL)
13263 continue;
13264 FREE_LOCK(ump);
13265 error = bwrite(bp);
13266 if (error)
13267 break;
13268 ACQUIRE_LOCK(ump);
13269 continue;
13270 }
13271 /*
13272 * Write the buffer.
13273 */
13274 FREE_LOCK(ump);
13275 BO_LOCK(bo);
13276 bp = gbincore(bo, lbn);
13277 if (bp != NULL) {
13278 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
13279 LK_INTERLOCK, BO_LOCKPTR(bo));
13280 if (error == ENOLCK) {
13281 ACQUIRE_LOCK(ump);
13282 error = 0;
13283 continue; /* Slept, retry */
13284 }
13285 if (error != 0)
13286 break; /* Failed */
13287 if (bp->b_flags & B_DELWRI) {
13288 bremfree(bp);
13289 error = bwrite(bp);
13290 if (error)
13291 break;
13292 } else
13293 BUF_UNLOCK(bp);
13294 } else
13295 BO_UNLOCK(bo);
13296 /*
13297 * We have to wait for the direct pointers to
13298 * point at the newdirblk before the dependency
13299 * will go away.
13300 */
13301 error = ffs_update(vp, 1);
13302 if (error)
13303 break;
13304 ACQUIRE_LOCK(ump);
13305 }
13306 return (error);
13307 }
13308
13309 /*
13310 * Eliminate a pagedep dependency by flushing out all its diradd dependencies.
13311 */
13312 static int
flush_pagedep_deps(struct vnode * pvp,struct mount * mp,struct diraddhd * diraddhdp,struct buf * locked_bp)13313 flush_pagedep_deps(
13314 struct vnode *pvp,
13315 struct mount *mp,
13316 struct diraddhd *diraddhdp,
13317 struct buf *locked_bp)
13318 {
13319 struct inodedep *inodedep;
13320 struct inoref *inoref;
13321 struct ufsmount *ump;
13322 struct diradd *dap;
13323 struct vnode *vp;
13324 int error = 0;
13325 struct buf *bp;
13326 ino_t inum;
13327 struct diraddhd unfinished;
13328
13329 LIST_INIT(&unfinished);
13330 ump = VFSTOUFS(mp);
13331 LOCK_OWNED(ump);
13332 restart:
13333 while ((dap = LIST_FIRST(diraddhdp)) != NULL) {
13334 /*
13335 * Flush ourselves if this directory entry
13336 * has a MKDIR_PARENT dependency.
13337 */
13338 if (dap->da_state & MKDIR_PARENT) {
13339 FREE_LOCK(ump);
13340 if ((error = ffs_update(pvp, 1)) != 0)
13341 break;
13342 ACQUIRE_LOCK(ump);
13343 /*
13344 * If that cleared dependencies, go on to next.
13345 */
13346 if (dap != LIST_FIRST(diraddhdp))
13347 continue;
13348 /*
13349 * All MKDIR_PARENT dependencies and all the
13350 * NEWBLOCK pagedeps that are contained in direct
13351 * blocks were resolved by doing above ffs_update.
13352 * Pagedeps contained in indirect blocks may
13353 * require a complete sync'ing of the directory.
13354 * We are in the midst of doing a complete sync,
13355 * so if they are not resolved in this pass we
13356 * defer them for now as they will be sync'ed by
13357 * our caller shortly.
13358 */
13359 LIST_REMOVE(dap, da_pdlist);
13360 LIST_INSERT_HEAD(&unfinished, dap, da_pdlist);
13361 continue;
13362 }
13363 /*
13364 * A newly allocated directory must have its "." and
13365 * ".." entries written out before its name can be
13366 * committed in its parent.
13367 */
13368 inum = dap->da_newinum;
13369 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0)
13370 panic("flush_pagedep_deps: lost inode1");
13371 /*
13372 * Wait for any pending journal adds to complete so we don't
13373 * cause rollbacks while syncing.
13374 */
13375 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
13376 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
13377 == DEPCOMPLETE) {
13378 jwait(&inoref->if_list, MNT_WAIT);
13379 goto restart;
13380 }
13381 }
13382 if (dap->da_state & MKDIR_BODY) {
13383 FREE_LOCK(ump);
13384 error = get_parent_vp(pvp, mp, inum, locked_bp,
13385 diraddhdp, &unfinished, &vp);
13386 if (error != 0)
13387 break;
13388 error = flush_newblk_dep(vp, mp, 0);
13389 /*
13390 * If we still have the dependency we might need to
13391 * update the vnode to sync the new link count to
13392 * disk.
13393 */
13394 if (error == 0 && dap == LIST_FIRST(diraddhdp))
13395 error = ffs_update(vp, 1);
13396 vput(vp);
13397 if (error != 0)
13398 break;
13399 ACQUIRE_LOCK(ump);
13400 /*
13401 * If that cleared dependencies, go on to next.
13402 */
13403 if (dap != LIST_FIRST(diraddhdp))
13404 continue;
13405 if (dap->da_state & MKDIR_BODY) {
13406 inodedep_lookup(UFSTOVFS(ump), inum, 0,
13407 &inodedep);
13408 panic("flush_pagedep_deps: MKDIR_BODY "
13409 "inodedep %p dap %p vp %p",
13410 inodedep, dap, vp);
13411 }
13412 }
13413 /*
13414 * Flush the inode on which the directory entry depends.
13415 * Having accounted for MKDIR_PARENT and MKDIR_BODY above,
13416 * the only remaining dependency is that the updated inode
13417 * count must get pushed to disk. The inode has already
13418 * been pushed into its inode buffer (via VOP_UPDATE) at
13419 * the time of the reference count change. So we need only
13420 * locate that buffer, ensure that there will be no rollback
13421 * caused by a bitmap dependency, then write the inode buffer.
13422 */
13423 retry:
13424 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0)
13425 panic("flush_pagedep_deps: lost inode");
13426 /*
13427 * If the inode still has bitmap dependencies,
13428 * push them to disk.
13429 */
13430 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) == 0) {
13431 bp = inodedep->id_bmsafemap->sm_buf;
13432 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT);
13433 if (bp == NULL)
13434 goto retry;
13435 FREE_LOCK(ump);
13436 if ((error = bwrite(bp)) != 0)
13437 break;
13438 ACQUIRE_LOCK(ump);
13439 if (dap != LIST_FIRST(diraddhdp))
13440 continue;
13441 }
13442 /*
13443 * If the inode is still sitting in a buffer waiting
13444 * to be written or waiting for the link count to be
13445 * adjusted update it here to flush it to disk.
13446 */
13447 if (dap == LIST_FIRST(diraddhdp)) {
13448 FREE_LOCK(ump);
13449 error = get_parent_vp(pvp, mp, inum, locked_bp,
13450 diraddhdp, &unfinished, &vp);
13451 if (error != 0)
13452 break;
13453 error = ffs_update(vp, 1);
13454 vput(vp);
13455 if (error)
13456 break;
13457 ACQUIRE_LOCK(ump);
13458 }
13459 /*
13460 * If we have failed to get rid of all the dependencies
13461 * then something is seriously wrong.
13462 */
13463 if (dap == LIST_FIRST(diraddhdp)) {
13464 inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep);
13465 panic("flush_pagedep_deps: failed to flush "
13466 "inodedep %p ino %ju dap %p",
13467 inodedep, (uintmax_t)inum, dap);
13468 }
13469 }
13470 if (error)
13471 ACQUIRE_LOCK(ump);
13472 while ((dap = LIST_FIRST(&unfinished)) != NULL) {
13473 LIST_REMOVE(dap, da_pdlist);
13474 LIST_INSERT_HEAD(diraddhdp, dap, da_pdlist);
13475 }
13476 return (error);
13477 }
13478
13479 /*
13480 * A large burst of file addition or deletion activity can drive the
13481 * memory load excessively high. First attempt to slow things down
13482 * using the techniques below. If that fails, this routine requests
13483 * the offending operations to fall back to running synchronously
13484 * until the memory load returns to a reasonable level.
13485 */
13486 int
softdep_slowdown(struct vnode * vp)13487 softdep_slowdown(struct vnode *vp)
13488 {
13489 struct ufsmount *ump;
13490 int jlow;
13491 int max_softdeps_hard;
13492
13493 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0,
13494 ("softdep_slowdown called on non-softdep filesystem"));
13495 ump = VFSTOUFS(vp->v_mount);
13496 ACQUIRE_LOCK(ump);
13497 jlow = 0;
13498 /*
13499 * Check for journal space if needed.
13500 */
13501 if (DOINGSUJ(vp)) {
13502 if (journal_space(ump, 0) == 0)
13503 jlow = 1;
13504 }
13505 /*
13506 * If the system is under its limits and our filesystem is
13507 * not responsible for more than our share of the usage and
13508 * we are not low on journal space, then no need to slow down.
13509 */
13510 max_softdeps_hard = max_softdeps * 11 / 10;
13511 if (dep_current[D_DIRREM] < max_softdeps_hard / 2 &&
13512 dep_current[D_INODEDEP] < max_softdeps_hard &&
13513 dep_current[D_INDIRDEP] < max_softdeps_hard / 1000 &&
13514 dep_current[D_FREEBLKS] < max_softdeps_hard && jlow == 0 &&
13515 ump->softdep_curdeps[D_DIRREM] <
13516 (max_softdeps_hard / 2) / stat_flush_threads &&
13517 ump->softdep_curdeps[D_INODEDEP] <
13518 max_softdeps_hard / stat_flush_threads &&
13519 ump->softdep_curdeps[D_INDIRDEP] <
13520 (max_softdeps_hard / 1000) / stat_flush_threads &&
13521 ump->softdep_curdeps[D_FREEBLKS] <
13522 max_softdeps_hard / stat_flush_threads) {
13523 FREE_LOCK(ump);
13524 return (0);
13525 }
13526 /*
13527 * If the journal is low or our filesystem is over its limit
13528 * then speedup the cleanup.
13529 */
13530 if (ump->softdep_curdeps[D_INDIRDEP] <
13531 (max_softdeps_hard / 1000) / stat_flush_threads || jlow)
13532 softdep_speedup(ump);
13533 stat_sync_limit_hit += 1;
13534 FREE_LOCK(ump);
13535 /*
13536 * We only slow down the rate at which new dependencies are
13537 * generated if we are not using journaling. With journaling,
13538 * the cleanup should always be sufficient to keep things
13539 * under control.
13540 */
13541 if (DOINGSUJ(vp))
13542 return (0);
13543 return (1);
13544 }
13545
13546 static int
softdep_request_cleanup_filter(struct vnode * vp,void * arg __unused)13547 softdep_request_cleanup_filter(struct vnode *vp, void *arg __unused)
13548 {
13549 return ((vp->v_iflag & VI_OWEINACT) != 0 && vp->v_usecount == 0 &&
13550 ((vp->v_vflag & VV_NOSYNC) != 0 || VTOI(vp)->i_effnlink == 0));
13551 }
13552
13553 static void
softdep_request_cleanup_inactivate(struct mount * mp)13554 softdep_request_cleanup_inactivate(struct mount *mp)
13555 {
13556 struct vnode *vp, *mvp;
13557 int error;
13558
13559 MNT_VNODE_FOREACH_LAZY(vp, mp, mvp, softdep_request_cleanup_filter,
13560 NULL) {
13561 vholdl(vp);
13562 vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY);
13563 VI_LOCK(vp);
13564 if (IS_UFS(vp) && vp->v_usecount == 0) {
13565 while ((vp->v_iflag & VI_OWEINACT) != 0) {
13566 error = vinactive(vp);
13567 if (error != 0 && error != ERELOOKUP)
13568 break;
13569 }
13570 atomic_add_int(&stat_delayed_inact, 1);
13571 }
13572 VOP_UNLOCK(vp);
13573 vdropl(vp);
13574 }
13575 }
13576
13577 /*
13578 * Called by the allocation routines when they are about to fail
13579 * in the hope that we can free up the requested resource (inodes
13580 * or disk space).
13581 *
13582 * First check to see if the work list has anything on it. If it has,
13583 * clean up entries until we successfully free the requested resource.
13584 * Because this process holds inodes locked, we cannot handle any remove
13585 * requests that might block on a locked inode as that could lead to
13586 * deadlock. If the worklist yields none of the requested resource,
13587 * start syncing out vnodes to free up the needed space.
13588 */
13589 int
softdep_request_cleanup(struct fs * fs,struct vnode * vp,struct ucred * cred,int resource)13590 softdep_request_cleanup(
13591 struct fs *fs,
13592 struct vnode *vp,
13593 struct ucred *cred,
13594 int resource)
13595 {
13596 struct ufsmount *ump;
13597 struct mount *mp;
13598 long starttime;
13599 ufs2_daddr_t needed;
13600 int error, failed_vnode;
13601
13602 /*
13603 * If we are being called because of a process doing a
13604 * copy-on-write, then it is not safe to process any
13605 * worklist items as we will recurse into the copyonwrite
13606 * routine. This will result in an incoherent snapshot.
13607 * If the vnode that we hold is a snapshot, we must avoid
13608 * handling other resources that could cause deadlock.
13609 */
13610 if ((curthread->td_pflags & TDP_COWINPROGRESS) || IS_SNAPSHOT(VTOI(vp)))
13611 return (0);
13612
13613 if (resource == FLUSH_BLOCKS_WAIT)
13614 stat_cleanup_blkrequests += 1;
13615 else
13616 stat_cleanup_inorequests += 1;
13617
13618 mp = vp->v_mount;
13619 ump = VFSTOUFS(mp);
13620 mtx_assert(UFS_MTX(ump), MA_OWNED);
13621 UFS_UNLOCK(ump);
13622 error = ffs_update(vp, 1);
13623 if (error != 0 || MOUNTEDSOFTDEP(mp) == 0) {
13624 UFS_LOCK(ump);
13625 return (0);
13626 }
13627 /*
13628 * If we are in need of resources, start by cleaning up
13629 * any block removals associated with our inode.
13630 */
13631 ACQUIRE_LOCK(ump);
13632 process_removes(vp);
13633 process_truncates(vp);
13634 FREE_LOCK(ump);
13635 /*
13636 * Now clean up at least as many resources as we will need.
13637 *
13638 * When requested to clean up inodes, the number that are needed
13639 * is set by the number of simultaneous writers (mnt_writeopcount)
13640 * plus a bit of slop (2) in case some more writers show up while
13641 * we are cleaning.
13642 *
13643 * When requested to free up space, the amount of space that
13644 * we need is enough blocks to allocate a full-sized segment
13645 * (fs_contigsumsize). The number of such segments that will
13646 * be needed is set by the number of simultaneous writers
13647 * (mnt_writeopcount) plus a bit of slop (2) in case some more
13648 * writers show up while we are cleaning.
13649 *
13650 * Additionally, if we are unpriviledged and allocating space,
13651 * we need to ensure that we clean up enough blocks to get the
13652 * needed number of blocks over the threshold of the minimum
13653 * number of blocks required to be kept free by the filesystem
13654 * (fs_minfree).
13655 */
13656 if (resource == FLUSH_INODES_WAIT) {
13657 needed = vfs_mount_fetch_counter(vp->v_mount,
13658 MNT_COUNT_WRITEOPCOUNT) + 2;
13659 } else if (resource == FLUSH_BLOCKS_WAIT) {
13660 needed = (vfs_mount_fetch_counter(vp->v_mount,
13661 MNT_COUNT_WRITEOPCOUNT) + 2) * fs->fs_contigsumsize;
13662 if (priv_check_cred(cred, PRIV_VFS_BLOCKRESERVE))
13663 needed += fragstoblks(fs,
13664 roundup((fs->fs_dsize * fs->fs_minfree / 100) -
13665 fs->fs_cstotal.cs_nffree, fs->fs_frag));
13666 } else {
13667 printf("softdep_request_cleanup: Unknown resource type %d\n",
13668 resource);
13669 UFS_LOCK(ump);
13670 return (0);
13671 }
13672 starttime = time_second;
13673 retry:
13674 if (resource == FLUSH_BLOCKS_WAIT &&
13675 fs->fs_cstotal.cs_nbfree <= needed)
13676 softdep_send_speedup(ump, needed * fs->fs_bsize,
13677 BIO_SPEEDUP_TRIM);
13678 if ((resource == FLUSH_BLOCKS_WAIT && ump->softdep_on_worklist > 0 &&
13679 fs->fs_cstotal.cs_nbfree <= needed) ||
13680 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 &&
13681 fs->fs_cstotal.cs_nifree <= needed)) {
13682 ACQUIRE_LOCK(ump);
13683 if (ump->softdep_on_worklist > 0 &&
13684 process_worklist_item(UFSTOVFS(ump),
13685 ump->softdep_on_worklist, LK_NOWAIT) != 0)
13686 stat_worklist_push += 1;
13687 FREE_LOCK(ump);
13688 }
13689
13690 /*
13691 * Check that there are vnodes pending inactivation. As they
13692 * have been unlinked, inactivating them will free up their
13693 * inodes.
13694 */
13695 ACQUIRE_LOCK(ump);
13696 if (resource == FLUSH_INODES_WAIT &&
13697 fs->fs_cstotal.cs_nifree <= needed &&
13698 fs->fs_pendinginodes <= needed) {
13699 if ((ump->um_softdep->sd_flags & FLUSH_DI_ACTIVE) == 0) {
13700 ump->um_softdep->sd_flags |= FLUSH_DI_ACTIVE;
13701 FREE_LOCK(ump);
13702 softdep_request_cleanup_inactivate(mp);
13703 ACQUIRE_LOCK(ump);
13704 ump->um_softdep->sd_flags &= ~FLUSH_DI_ACTIVE;
13705 wakeup(&ump->um_softdep->sd_flags);
13706 } else {
13707 while ((ump->um_softdep->sd_flags &
13708 FLUSH_DI_ACTIVE) != 0) {
13709 msleep(&ump->um_softdep->sd_flags,
13710 LOCK_PTR(ump), PVM, "ffsvina", hz);
13711 }
13712 }
13713 }
13714 FREE_LOCK(ump);
13715
13716 /*
13717 * If we still need resources and there are no more worklist
13718 * entries to process to obtain them, we have to start flushing
13719 * the dirty vnodes to force the release of additional requests
13720 * to the worklist that we can then process to reap addition
13721 * resources. We walk the vnodes associated with the mount point
13722 * until we get the needed worklist requests that we can reap.
13723 *
13724 * If there are several threads all needing to clean the same
13725 * mount point, only one is allowed to walk the mount list.
13726 * When several threads all try to walk the same mount list,
13727 * they end up competing with each other and often end up in
13728 * livelock. This approach ensures that forward progress is
13729 * made at the cost of occational ENOSPC errors being returned
13730 * that might otherwise have been avoided.
13731 */
13732 error = 1;
13733 if ((resource == FLUSH_BLOCKS_WAIT &&
13734 fs->fs_cstotal.cs_nbfree <= needed) ||
13735 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 &&
13736 fs->fs_cstotal.cs_nifree <= needed)) {
13737 ACQUIRE_LOCK(ump);
13738 if ((ump->um_softdep->sd_flags & FLUSH_RC_ACTIVE) == 0) {
13739 ump->um_softdep->sd_flags |= FLUSH_RC_ACTIVE;
13740 FREE_LOCK(ump);
13741 failed_vnode = softdep_request_cleanup_flush(mp, ump);
13742 ACQUIRE_LOCK(ump);
13743 ump->um_softdep->sd_flags &= ~FLUSH_RC_ACTIVE;
13744 wakeup(&ump->um_softdep->sd_flags);
13745 FREE_LOCK(ump);
13746 if (ump->softdep_on_worklist > 0) {
13747 stat_cleanup_retries += 1;
13748 if (!failed_vnode)
13749 goto retry;
13750 }
13751 } else {
13752 while ((ump->um_softdep->sd_flags &
13753 FLUSH_RC_ACTIVE) != 0) {
13754 msleep(&ump->um_softdep->sd_flags,
13755 LOCK_PTR(ump), PVM, "ffsrca", hz);
13756 }
13757 FREE_LOCK(ump);
13758 error = 0;
13759 }
13760 stat_cleanup_failures += 1;
13761 }
13762 if (time_second - starttime > stat_cleanup_high_delay)
13763 stat_cleanup_high_delay = time_second - starttime;
13764 UFS_LOCK(ump);
13765 return (error);
13766 }
13767
13768 /*
13769 * Scan the vnodes for the specified mount point flushing out any
13770 * vnodes that can be locked without waiting. Finally, try to flush
13771 * the device associated with the mount point if it can be locked
13772 * without waiting.
13773 *
13774 * We return 0 if we were able to lock every vnode in our scan.
13775 * If we had to skip one or more vnodes, we return 1.
13776 */
13777 static int
softdep_request_cleanup_flush(struct mount * mp,struct ufsmount * ump)13778 softdep_request_cleanup_flush(struct mount *mp, struct ufsmount *ump)
13779 {
13780 struct thread *td;
13781 struct vnode *lvp, *mvp;
13782 int failed_vnode;
13783
13784 failed_vnode = 0;
13785 td = curthread;
13786 MNT_VNODE_FOREACH_ALL(lvp, mp, mvp) {
13787 if (TAILQ_FIRST(&lvp->v_bufobj.bo_dirty.bv_hd) == 0) {
13788 VI_UNLOCK(lvp);
13789 continue;
13790 }
13791 if (vget(lvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_NOWAIT) != 0) {
13792 failed_vnode = 1;
13793 continue;
13794 }
13795 if (lvp->v_vflag & VV_NOSYNC) { /* unlinked */
13796 vput(lvp);
13797 continue;
13798 }
13799 (void) ffs_syncvnode(lvp, MNT_NOWAIT, 0);
13800 vput(lvp);
13801 }
13802 lvp = ump->um_devvp;
13803 if (vn_lock(lvp, LK_EXCLUSIVE | LK_NOWAIT) == 0) {
13804 VOP_FSYNC(lvp, MNT_NOWAIT, td);
13805 VOP_UNLOCK(lvp);
13806 }
13807 return (failed_vnode);
13808 }
13809
13810 static bool
softdep_excess_items(struct ufsmount * ump,int item)13811 softdep_excess_items(struct ufsmount *ump, int item)
13812 {
13813
13814 KASSERT(item >= 0 && item < D_LAST, ("item %d", item));
13815 return (dep_current[item] > max_softdeps &&
13816 ump->softdep_curdeps[item] > max_softdeps /
13817 stat_flush_threads);
13818 }
13819
13820 static void
schedule_cleanup(struct mount * mp)13821 schedule_cleanup(struct mount *mp)
13822 {
13823 struct ufsmount *ump;
13824 struct thread *td;
13825
13826 ump = VFSTOUFS(mp);
13827 LOCK_OWNED(ump);
13828 FREE_LOCK(ump);
13829 td = curthread;
13830 if ((td->td_pflags & TDP_KTHREAD) != 0 &&
13831 (td->td_proc->p_flag2 & P2_AST_SU) == 0) {
13832 /*
13833 * No ast is delivered to kernel threads, so nobody
13834 * would deref the mp. Some kernel threads
13835 * explicitly check for AST, e.g. NFS daemon does
13836 * this in the serving loop.
13837 */
13838 return;
13839 }
13840 if (td->td_su != NULL)
13841 vfs_rel(td->td_su);
13842 vfs_ref(mp);
13843 td->td_su = mp;
13844 ast_sched(td, TDA_UFS);
13845 }
13846
13847 static void
softdep_ast_cleanup_proc(struct thread * td,int ast __unused)13848 softdep_ast_cleanup_proc(struct thread *td, int ast __unused)
13849 {
13850 struct mount *mp;
13851 struct ufsmount *ump;
13852 int error;
13853 bool req;
13854
13855 while ((mp = td->td_su) != NULL) {
13856 td->td_su = NULL;
13857 error = vfs_busy(mp, MBF_NOWAIT);
13858 vfs_rel(mp);
13859 if (error != 0)
13860 return;
13861 if (ffs_own_mount(mp) && MOUNTEDSOFTDEP(mp)) {
13862 ump = VFSTOUFS(mp);
13863 for (;;) {
13864 req = false;
13865 ACQUIRE_LOCK(ump);
13866 if (softdep_excess_items(ump, D_INODEDEP)) {
13867 req = true;
13868 request_cleanup(mp, FLUSH_INODES);
13869 }
13870 if (softdep_excess_items(ump, D_DIRREM)) {
13871 req = true;
13872 request_cleanup(mp, FLUSH_BLOCKS);
13873 }
13874 FREE_LOCK(ump);
13875 if (softdep_excess_items(ump, D_NEWBLK) ||
13876 softdep_excess_items(ump, D_ALLOCDIRECT) ||
13877 softdep_excess_items(ump, D_ALLOCINDIR)) {
13878 error = vn_start_write(NULL, &mp,
13879 V_WAIT);
13880 if (error == 0) {
13881 req = true;
13882 VFS_SYNC(mp, MNT_WAIT);
13883 vn_finished_write(mp);
13884 }
13885 }
13886 if ((td->td_pflags & TDP_KTHREAD) != 0 || !req)
13887 break;
13888 }
13889 }
13890 vfs_unbusy(mp);
13891 }
13892 if ((mp = td->td_su) != NULL) {
13893 td->td_su = NULL;
13894 vfs_rel(mp);
13895 }
13896 }
13897
13898 /*
13899 * If memory utilization has gotten too high, deliberately slow things
13900 * down and speed up the I/O processing.
13901 */
13902 static int
request_cleanup(struct mount * mp,int resource)13903 request_cleanup(struct mount *mp, int resource)
13904 {
13905 struct thread *td = curthread;
13906 struct ufsmount *ump;
13907
13908 ump = VFSTOUFS(mp);
13909 LOCK_OWNED(ump);
13910 /*
13911 * We never hold up the filesystem syncer or buf daemon.
13912 */
13913 if (td->td_pflags & (TDP_SOFTDEP|TDP_NORUNNINGBUF))
13914 return (0);
13915 /*
13916 * First check to see if the work list has gotten backlogged.
13917 * If it has, co-opt this process to help clean up two entries.
13918 * Because this process may hold inodes locked, we cannot
13919 * handle any remove requests that might block on a locked
13920 * inode as that could lead to deadlock. We set TDP_SOFTDEP
13921 * to avoid recursively processing the worklist.
13922 */
13923 if (ump->softdep_on_worklist > max_softdeps / 10) {
13924 td->td_pflags |= TDP_SOFTDEP;
13925 process_worklist_item(mp, 2, LK_NOWAIT);
13926 td->td_pflags &= ~TDP_SOFTDEP;
13927 stat_worklist_push += 2;
13928 return(1);
13929 }
13930 /*
13931 * Next, we attempt to speed up the syncer process. If that
13932 * is successful, then we allow the process to continue.
13933 */
13934 if (softdep_speedup(ump) &&
13935 resource != FLUSH_BLOCKS_WAIT &&
13936 resource != FLUSH_INODES_WAIT)
13937 return(0);
13938 /*
13939 * If we are resource constrained on inode dependencies, try
13940 * flushing some dirty inodes. Otherwise, we are constrained
13941 * by file deletions, so try accelerating flushes of directories
13942 * with removal dependencies. We would like to do the cleanup
13943 * here, but we probably hold an inode locked at this point and
13944 * that might deadlock against one that we try to clean. So,
13945 * the best that we can do is request the syncer daemon to do
13946 * the cleanup for us.
13947 */
13948 switch (resource) {
13949 case FLUSH_INODES:
13950 case FLUSH_INODES_WAIT:
13951 ACQUIRE_GBLLOCK(&lk);
13952 stat_ino_limit_push += 1;
13953 req_clear_inodedeps += 1;
13954 FREE_GBLLOCK(&lk);
13955 stat_countp = &stat_ino_limit_hit;
13956 break;
13957
13958 case FLUSH_BLOCKS:
13959 case FLUSH_BLOCKS_WAIT:
13960 ACQUIRE_GBLLOCK(&lk);
13961 stat_blk_limit_push += 1;
13962 req_clear_remove += 1;
13963 FREE_GBLLOCK(&lk);
13964 stat_countp = &stat_blk_limit_hit;
13965 break;
13966
13967 default:
13968 panic("request_cleanup: unknown type");
13969 }
13970 /*
13971 * Hopefully the syncer daemon will catch up and awaken us.
13972 * We wait at most tickdelay before proceeding in any case.
13973 */
13974 ACQUIRE_GBLLOCK(&lk);
13975 FREE_LOCK(ump);
13976 proc_waiting += 1;
13977 if (callout_pending(&softdep_callout) == FALSE)
13978 callout_reset(&softdep_callout, tickdelay > 2 ? tickdelay : 2,
13979 pause_timer, 0);
13980
13981 if ((td->td_pflags & TDP_KTHREAD) == 0)
13982 msleep((caddr_t)&proc_waiting, &lk, PPAUSE, "softupdate", 0);
13983 proc_waiting -= 1;
13984 FREE_GBLLOCK(&lk);
13985 ACQUIRE_LOCK(ump);
13986 return (1);
13987 }
13988
13989 /*
13990 * Awaken processes pausing in request_cleanup and clear proc_waiting
13991 * to indicate that there is no longer a timer running. Pause_timer
13992 * will be called with the global softdep mutex (&lk) locked.
13993 */
13994 static void
pause_timer(void * arg)13995 pause_timer(void *arg)
13996 {
13997
13998 GBLLOCK_OWNED(&lk);
13999 /*
14000 * The callout_ API has acquired mtx and will hold it around this
14001 * function call.
14002 */
14003 *stat_countp += proc_waiting;
14004 wakeup(&proc_waiting);
14005 }
14006
14007 /*
14008 * If requested, try removing inode or removal dependencies.
14009 */
14010 static void
check_clear_deps(struct mount * mp)14011 check_clear_deps(struct mount *mp)
14012 {
14013 struct ufsmount *ump;
14014 bool suj_susp;
14015
14016 /*
14017 * Tell the lower layers that any TRIM or WRITE transactions that have
14018 * been delayed for performance reasons should proceed to help alleviate
14019 * the shortage faster. The race between checking req_* and the softdep
14020 * mutex (lk) is fine since this is an advisory operation that at most
14021 * causes deferred work to be done sooner.
14022 */
14023 ump = VFSTOUFS(mp);
14024 suj_susp = ump->um_softdep->sd_jblocks != NULL &&
14025 ump->softdep_jblocks->jb_suspended;
14026 if (req_clear_remove || req_clear_inodedeps || suj_susp) {
14027 FREE_LOCK(ump);
14028 softdep_send_speedup(ump, 0, BIO_SPEEDUP_TRIM | BIO_SPEEDUP_WRITE);
14029 ACQUIRE_LOCK(ump);
14030 }
14031
14032 /*
14033 * If we are suspended, it may be because of our using
14034 * too many inodedeps, so help clear them out.
14035 */
14036 if (suj_susp)
14037 clear_inodedeps(mp);
14038
14039 /*
14040 * General requests for cleanup of backed up dependencies
14041 */
14042 ACQUIRE_GBLLOCK(&lk);
14043 if (req_clear_inodedeps) {
14044 req_clear_inodedeps -= 1;
14045 FREE_GBLLOCK(&lk);
14046 clear_inodedeps(mp);
14047 ACQUIRE_GBLLOCK(&lk);
14048 wakeup(&proc_waiting);
14049 }
14050 if (req_clear_remove) {
14051 req_clear_remove -= 1;
14052 FREE_GBLLOCK(&lk);
14053 clear_remove(mp);
14054 ACQUIRE_GBLLOCK(&lk);
14055 wakeup(&proc_waiting);
14056 }
14057 FREE_GBLLOCK(&lk);
14058 }
14059
14060 /*
14061 * Flush out a directory with at least one removal dependency in an effort to
14062 * reduce the number of dirrem, freefile, and freeblks dependency structures.
14063 */
14064 static void
clear_remove(struct mount * mp)14065 clear_remove(struct mount *mp)
14066 {
14067 struct pagedep_hashhead *pagedephd;
14068 struct pagedep *pagedep;
14069 struct ufsmount *ump;
14070 struct vnode *vp;
14071 struct bufobj *bo;
14072 int error, cnt;
14073 ino_t ino;
14074
14075 ump = VFSTOUFS(mp);
14076 LOCK_OWNED(ump);
14077
14078 for (cnt = 0; cnt <= ump->pagedep_hash_size; cnt++) {
14079 pagedephd = &ump->pagedep_hashtbl[ump->pagedep_nextclean++];
14080 if (ump->pagedep_nextclean > ump->pagedep_hash_size)
14081 ump->pagedep_nextclean = 0;
14082 LIST_FOREACH(pagedep, pagedephd, pd_hash) {
14083 if (LIST_EMPTY(&pagedep->pd_dirremhd))
14084 continue;
14085 ino = pagedep->pd_ino;
14086 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0)
14087 continue;
14088 FREE_LOCK(ump);
14089
14090 /*
14091 * Let unmount clear deps
14092 */
14093 error = vfs_busy(mp, MBF_NOWAIT);
14094 if (error != 0)
14095 goto finish_write;
14096 error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp,
14097 FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP);
14098 vfs_unbusy(mp);
14099 if (error != 0) {
14100 softdep_error("clear_remove: vget", error);
14101 goto finish_write;
14102 }
14103 MPASS(VTOI(vp)->i_mode != 0);
14104 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0)))
14105 softdep_error("clear_remove: fsync", error);
14106 bo = &vp->v_bufobj;
14107 BO_LOCK(bo);
14108 drain_output(vp);
14109 BO_UNLOCK(bo);
14110 vput(vp);
14111 finish_write:
14112 vn_finished_write(mp);
14113 ACQUIRE_LOCK(ump);
14114 return;
14115 }
14116 }
14117 }
14118
14119 /*
14120 * Clear out a block of dirty inodes in an effort to reduce
14121 * the number of inodedep dependency structures.
14122 */
14123 static void
clear_inodedeps(struct mount * mp)14124 clear_inodedeps(struct mount *mp)
14125 {
14126 struct inodedep_hashhead *inodedephd;
14127 struct inodedep *inodedep;
14128 struct ufsmount *ump;
14129 struct vnode *vp;
14130 struct fs *fs;
14131 int error, cnt;
14132 ino_t firstino, lastino, ino;
14133
14134 ump = VFSTOUFS(mp);
14135 fs = ump->um_fs;
14136 LOCK_OWNED(ump);
14137 /*
14138 * Pick a random inode dependency to be cleared.
14139 * We will then gather up all the inodes in its block
14140 * that have dependencies and flush them out.
14141 */
14142 for (cnt = 0; cnt <= ump->inodedep_hash_size; cnt++) {
14143 inodedephd = &ump->inodedep_hashtbl[ump->inodedep_nextclean++];
14144 if (ump->inodedep_nextclean > ump->inodedep_hash_size)
14145 ump->inodedep_nextclean = 0;
14146 if ((inodedep = LIST_FIRST(inodedephd)) != NULL)
14147 break;
14148 }
14149 if (inodedep == NULL)
14150 return;
14151 /*
14152 * Find the last inode in the block with dependencies.
14153 */
14154 firstino = rounddown2(inodedep->id_ino, INOPB(fs));
14155 for (lastino = firstino + INOPB(fs) - 1; lastino > firstino; lastino--)
14156 if (inodedep_lookup(mp, lastino, 0, &inodedep) != 0)
14157 break;
14158 /*
14159 * Asynchronously push all but the last inode with dependencies.
14160 * Synchronously push the last inode with dependencies to ensure
14161 * that the inode block gets written to free up the inodedeps.
14162 */
14163 for (ino = firstino; ino <= lastino; ino++) {
14164 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0)
14165 continue;
14166 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0)
14167 continue;
14168 FREE_LOCK(ump);
14169 error = vfs_busy(mp, MBF_NOWAIT); /* Let unmount clear deps */
14170 if (error != 0) {
14171 vn_finished_write(mp);
14172 ACQUIRE_LOCK(ump);
14173 return;
14174 }
14175 if ((error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp,
14176 FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP)) != 0) {
14177 softdep_error("clear_inodedeps: vget", error);
14178 vfs_unbusy(mp);
14179 vn_finished_write(mp);
14180 ACQUIRE_LOCK(ump);
14181 return;
14182 }
14183 vfs_unbusy(mp);
14184 if (VTOI(vp)->i_mode == 0) {
14185 vgone(vp);
14186 } else if (ino == lastino) {
14187 do {
14188 error = ffs_syncvnode(vp, MNT_WAIT, 0);
14189 } while (error == ERELOOKUP);
14190 if (error != 0)
14191 softdep_error("clear_inodedeps: fsync1", error);
14192 } else {
14193 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0)))
14194 softdep_error("clear_inodedeps: fsync2", error);
14195 BO_LOCK(&vp->v_bufobj);
14196 drain_output(vp);
14197 BO_UNLOCK(&vp->v_bufobj);
14198 }
14199 vput(vp);
14200 vn_finished_write(mp);
14201 ACQUIRE_LOCK(ump);
14202 }
14203 }
14204
14205 void
softdep_buf_append(struct buf * bp,struct workhead * wkhd)14206 softdep_buf_append(struct buf *bp, struct workhead *wkhd)
14207 {
14208 struct worklist *wk;
14209 struct ufsmount *ump;
14210
14211 if ((wk = LIST_FIRST(wkhd)) == NULL)
14212 return;
14213 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0,
14214 ("softdep_buf_append called on non-softdep filesystem"));
14215 ump = VFSTOUFS(wk->wk_mp);
14216 ACQUIRE_LOCK(ump);
14217 while ((wk = LIST_FIRST(wkhd)) != NULL) {
14218 WORKLIST_REMOVE(wk);
14219 WORKLIST_INSERT(&bp->b_dep, wk);
14220 }
14221 FREE_LOCK(ump);
14222
14223 }
14224
14225 void
softdep_inode_append(struct inode * ip,struct ucred * cred,struct workhead * wkhd)14226 softdep_inode_append(
14227 struct inode *ip,
14228 struct ucred *cred,
14229 struct workhead *wkhd)
14230 {
14231 struct buf *bp;
14232 struct fs *fs;
14233 struct ufsmount *ump;
14234 int error;
14235
14236 ump = ITOUMP(ip);
14237 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
14238 ("softdep_inode_append called on non-softdep filesystem"));
14239 fs = ump->um_fs;
14240 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
14241 (int)fs->fs_bsize, cred, &bp);
14242 if (error) {
14243 bqrelse(bp);
14244 softdep_freework(wkhd);
14245 return;
14246 }
14247 softdep_buf_append(bp, wkhd);
14248 bqrelse(bp);
14249 }
14250
14251 void
softdep_freework(struct workhead * wkhd)14252 softdep_freework(struct workhead *wkhd)
14253 {
14254 struct worklist *wk;
14255 struct ufsmount *ump;
14256
14257 if ((wk = LIST_FIRST(wkhd)) == NULL)
14258 return;
14259 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0,
14260 ("softdep_freework called on non-softdep filesystem"));
14261 ump = VFSTOUFS(wk->wk_mp);
14262 ACQUIRE_LOCK(ump);
14263 handle_jwork(wkhd);
14264 FREE_LOCK(ump);
14265 }
14266
14267 static struct ufsmount *
softdep_bp_to_mp(struct buf * bp)14268 softdep_bp_to_mp(struct buf *bp)
14269 {
14270 struct mount *mp;
14271 struct vnode *vp;
14272
14273 if (LIST_EMPTY(&bp->b_dep))
14274 return (NULL);
14275 vp = bp->b_vp;
14276 KASSERT(vp != NULL,
14277 ("%s, buffer with dependencies lacks vnode", __func__));
14278
14279 /*
14280 * The ump mount point is stable after we get a correct
14281 * pointer, since bp is locked and this prevents unmount from
14282 * proceeding. But to get to it, we cannot dereference bp->b_dep
14283 * head wk_mp, because we do not yet own SU ump lock and
14284 * workitem might be freed while dereferenced.
14285 */
14286 retry:
14287 switch (vp->v_type) {
14288 case VCHR:
14289 VI_LOCK(vp);
14290 mp = vp->v_type == VCHR ? vp->v_rdev->si_mountpt : NULL;
14291 VI_UNLOCK(vp);
14292 if (mp == NULL)
14293 goto retry;
14294 break;
14295 case VREG:
14296 case VDIR:
14297 case VLNK:
14298 case VFIFO:
14299 case VSOCK:
14300 mp = vp->v_mount;
14301 break;
14302 case VBLK:
14303 vn_printf(vp, "softdep_bp_to_mp: unexpected block device\n");
14304 /* FALLTHROUGH */
14305 case VNON:
14306 case VBAD:
14307 case VMARKER:
14308 mp = NULL;
14309 break;
14310 default:
14311 vn_printf(vp, "unknown vnode type");
14312 mp = NULL;
14313 break;
14314 }
14315 return (VFSTOUFS(mp));
14316 }
14317
14318 /*
14319 * Function to determine if the buffer has outstanding dependencies
14320 * that will cause a roll-back if the buffer is written. If wantcount
14321 * is set, return number of dependencies, otherwise just yes or no.
14322 */
14323 static int
softdep_count_dependencies(struct buf * bp,int wantcount)14324 softdep_count_dependencies(struct buf *bp, int wantcount)
14325 {
14326 struct worklist *wk;
14327 struct ufsmount *ump;
14328 struct bmsafemap *bmsafemap;
14329 struct freework *freework;
14330 struct inodedep *inodedep;
14331 struct indirdep *indirdep;
14332 struct freeblks *freeblks;
14333 struct allocindir *aip;
14334 struct pagedep *pagedep;
14335 struct dirrem *dirrem;
14336 struct newblk *newblk;
14337 struct mkdir *mkdir;
14338 struct diradd *dap;
14339 int i, retval;
14340
14341 ump = softdep_bp_to_mp(bp);
14342 if (ump == NULL)
14343 return (0);
14344 retval = 0;
14345 ACQUIRE_LOCK(ump);
14346 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
14347 switch (wk->wk_type) {
14348 case D_INODEDEP:
14349 inodedep = WK_INODEDEP(wk);
14350 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
14351 /* bitmap allocation dependency */
14352 retval += 1;
14353 if (!wantcount)
14354 goto out;
14355 }
14356 if (TAILQ_FIRST(&inodedep->id_inoupdt)) {
14357 /* direct block pointer dependency */
14358 retval += 1;
14359 if (!wantcount)
14360 goto out;
14361 }
14362 if (TAILQ_FIRST(&inodedep->id_extupdt)) {
14363 /* direct block pointer dependency */
14364 retval += 1;
14365 if (!wantcount)
14366 goto out;
14367 }
14368 if (TAILQ_FIRST(&inodedep->id_inoreflst)) {
14369 /* Add reference dependency. */
14370 retval += 1;
14371 if (!wantcount)
14372 goto out;
14373 }
14374 continue;
14375
14376 case D_INDIRDEP:
14377 indirdep = WK_INDIRDEP(wk);
14378
14379 TAILQ_FOREACH(freework, &indirdep->ir_trunc, fw_next) {
14380 /* indirect truncation dependency */
14381 retval += 1;
14382 if (!wantcount)
14383 goto out;
14384 }
14385
14386 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) {
14387 /* indirect block pointer dependency */
14388 retval += 1;
14389 if (!wantcount)
14390 goto out;
14391 }
14392 continue;
14393
14394 case D_PAGEDEP:
14395 pagedep = WK_PAGEDEP(wk);
14396 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) {
14397 if (LIST_FIRST(&dirrem->dm_jremrefhd)) {
14398 /* Journal remove ref dependency. */
14399 retval += 1;
14400 if (!wantcount)
14401 goto out;
14402 }
14403 }
14404 for (i = 0; i < DAHASHSZ; i++) {
14405 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) {
14406 /* directory entry dependency */
14407 retval += 1;
14408 if (!wantcount)
14409 goto out;
14410 }
14411 }
14412 continue;
14413
14414 case D_BMSAFEMAP:
14415 bmsafemap = WK_BMSAFEMAP(wk);
14416 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd)) {
14417 /* Add reference dependency. */
14418 retval += 1;
14419 if (!wantcount)
14420 goto out;
14421 }
14422 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd)) {
14423 /* Allocate block dependency. */
14424 retval += 1;
14425 if (!wantcount)
14426 goto out;
14427 }
14428 continue;
14429
14430 case D_FREEBLKS:
14431 freeblks = WK_FREEBLKS(wk);
14432 if (LIST_FIRST(&freeblks->fb_jblkdephd)) {
14433 /* Freeblk journal dependency. */
14434 retval += 1;
14435 if (!wantcount)
14436 goto out;
14437 }
14438 continue;
14439
14440 case D_ALLOCDIRECT:
14441 case D_ALLOCINDIR:
14442 newblk = WK_NEWBLK(wk);
14443 if (newblk->nb_jnewblk) {
14444 /* Journal allocate dependency. */
14445 retval += 1;
14446 if (!wantcount)
14447 goto out;
14448 }
14449 continue;
14450
14451 case D_MKDIR:
14452 mkdir = WK_MKDIR(wk);
14453 if (mkdir->md_jaddref) {
14454 /* Journal reference dependency. */
14455 retval += 1;
14456 if (!wantcount)
14457 goto out;
14458 }
14459 continue;
14460
14461 case D_FREEWORK:
14462 case D_FREEDEP:
14463 case D_JSEGDEP:
14464 case D_JSEG:
14465 case D_SBDEP:
14466 /* never a dependency on these blocks */
14467 continue;
14468
14469 default:
14470 panic("softdep_count_dependencies: Unexpected type %s",
14471 TYPENAME(wk->wk_type));
14472 /* NOTREACHED */
14473 }
14474 }
14475 out:
14476 FREE_LOCK(ump);
14477 return (retval);
14478 }
14479
14480 /*
14481 * Acquire exclusive access to a buffer.
14482 * Must be called with a locked mtx parameter.
14483 * Return acquired buffer or NULL on failure.
14484 */
14485 static struct buf *
getdirtybuf(struct buf * bp,struct rwlock * lock,int waitfor)14486 getdirtybuf(struct buf *bp,
14487 struct rwlock *lock,
14488 int waitfor)
14489 {
14490 int error;
14491
14492 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0) {
14493 if (waitfor != MNT_WAIT)
14494 return (NULL);
14495 error = BUF_LOCK(bp,
14496 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, lock);
14497 /*
14498 * Even if we successfully acquire bp here, we have dropped
14499 * lock, which may violates our guarantee.
14500 */
14501 if (error == 0)
14502 BUF_UNLOCK(bp);
14503 else if (error != ENOLCK)
14504 panic("getdirtybuf: inconsistent lock: %d", error);
14505 rw_wlock(lock);
14506 return (NULL);
14507 }
14508 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) {
14509 if (lock != BO_LOCKPTR(bp->b_bufobj) && waitfor == MNT_WAIT) {
14510 rw_wunlock(lock);
14511 BO_LOCK(bp->b_bufobj);
14512 BUF_UNLOCK(bp);
14513 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) {
14514 bp->b_vflags |= BV_BKGRDWAIT;
14515 msleep(&bp->b_xflags, BO_LOCKPTR(bp->b_bufobj),
14516 PRIBIO | PDROP, "getbuf", 0);
14517 } else
14518 BO_UNLOCK(bp->b_bufobj);
14519 rw_wlock(lock);
14520 return (NULL);
14521 }
14522 BUF_UNLOCK(bp);
14523 if (waitfor != MNT_WAIT)
14524 return (NULL);
14525 #ifdef DEBUG_VFS_LOCKS
14526 if (bp->b_vp->v_type != VCHR)
14527 ASSERT_BO_WLOCKED(bp->b_bufobj);
14528 #endif
14529 bp->b_vflags |= BV_BKGRDWAIT;
14530 rw_sleep(&bp->b_xflags, lock, PRIBIO, "getbuf", 0);
14531 return (NULL);
14532 }
14533 if ((bp->b_flags & B_DELWRI) == 0) {
14534 BUF_UNLOCK(bp);
14535 return (NULL);
14536 }
14537 bremfree(bp);
14538 return (bp);
14539 }
14540
14541 /*
14542 * Check if it is safe to suspend the file system now. On entry,
14543 * the vnode interlock for devvp should be held. Return 0 with
14544 * the mount interlock held if the file system can be suspended now,
14545 * otherwise return EAGAIN with the mount interlock held.
14546 */
14547 int
softdep_check_suspend(struct mount * mp,struct vnode * devvp,int softdep_depcnt,int softdep_accdepcnt,int secondary_writes,int secondary_accwrites)14548 softdep_check_suspend(struct mount *mp,
14549 struct vnode *devvp,
14550 int softdep_depcnt,
14551 int softdep_accdepcnt,
14552 int secondary_writes,
14553 int secondary_accwrites)
14554 {
14555 struct buf *bp;
14556 struct bufobj *bo;
14557 struct ufsmount *ump;
14558 struct inodedep *inodedep;
14559 struct indirdep *indirdep;
14560 struct worklist *wk, *nextwk;
14561 int error, unlinked;
14562
14563 bo = &devvp->v_bufobj;
14564 ASSERT_BO_WLOCKED(bo);
14565
14566 /*
14567 * If we are not running with soft updates, then we need only
14568 * deal with secondary writes as we try to suspend.
14569 */
14570 if (MOUNTEDSOFTDEP(mp) == 0) {
14571 MNT_ILOCK(mp);
14572 while (mp->mnt_secondary_writes != 0) {
14573 BO_UNLOCK(bo);
14574 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp),
14575 (PUSER - 1) | PDROP, "secwr", 0);
14576 BO_LOCK(bo);
14577 MNT_ILOCK(mp);
14578 }
14579
14580 /*
14581 * Reasons for needing more work before suspend:
14582 * - Dirty buffers on devvp.
14583 * - Secondary writes occurred after start of vnode sync loop
14584 */
14585 error = 0;
14586 if (bo->bo_numoutput > 0 ||
14587 bo->bo_dirty.bv_cnt > 0 ||
14588 secondary_writes != 0 ||
14589 mp->mnt_secondary_writes != 0 ||
14590 secondary_accwrites != mp->mnt_secondary_accwrites)
14591 error = EAGAIN;
14592 BO_UNLOCK(bo);
14593 return (error);
14594 }
14595
14596 /*
14597 * If we are running with soft updates, then we need to coordinate
14598 * with them as we try to suspend.
14599 */
14600 ump = VFSTOUFS(mp);
14601 for (;;) {
14602 if (!TRY_ACQUIRE_LOCK(ump)) {
14603 BO_UNLOCK(bo);
14604 ACQUIRE_LOCK(ump);
14605 FREE_LOCK(ump);
14606 BO_LOCK(bo);
14607 continue;
14608 }
14609 MNT_ILOCK(mp);
14610 if (mp->mnt_secondary_writes != 0) {
14611 FREE_LOCK(ump);
14612 BO_UNLOCK(bo);
14613 msleep(&mp->mnt_secondary_writes,
14614 MNT_MTX(mp),
14615 (PUSER - 1) | PDROP, "secwr", 0);
14616 BO_LOCK(bo);
14617 continue;
14618 }
14619 break;
14620 }
14621
14622 unlinked = 0;
14623 if (MOUNTEDSUJ(mp)) {
14624 for (inodedep = TAILQ_FIRST(&ump->softdep_unlinked);
14625 inodedep != NULL;
14626 inodedep = TAILQ_NEXT(inodedep, id_unlinked)) {
14627 if ((inodedep->id_state & (UNLINKED | UNLINKLINKS |
14628 UNLINKONLIST)) != (UNLINKED | UNLINKLINKS |
14629 UNLINKONLIST) ||
14630 !check_inodedep_free(inodedep))
14631 continue;
14632 unlinked++;
14633 }
14634 }
14635
14636 /*
14637 * XXX Check for orphaned indirdep dependency structures.
14638 *
14639 * During forcible unmount after a disk failure there is a
14640 * bug that causes one or more indirdep dependency structures
14641 * to fail to be deallocated. We check for them here and clean
14642 * them up so that the unmount can succeed.
14643 */
14644 if ((ump->um_flags & UM_FSFAIL_CLEANUP) != 0 && ump->softdep_deps > 0 &&
14645 ump->softdep_deps == ump->softdep_curdeps[D_INDIRDEP]) {
14646 LIST_FOREACH_SAFE(wk, &ump->softdep_alldeps[D_INDIRDEP],
14647 wk_all, nextwk) {
14648 indirdep = WK_INDIRDEP(wk);
14649 if ((indirdep->ir_state & (GOINGAWAY | DEPCOMPLETE)) !=
14650 (GOINGAWAY | DEPCOMPLETE) ||
14651 !TAILQ_EMPTY(&indirdep->ir_trunc) ||
14652 !LIST_EMPTY(&indirdep->ir_completehd) ||
14653 !LIST_EMPTY(&indirdep->ir_writehd) ||
14654 !LIST_EMPTY(&indirdep->ir_donehd) ||
14655 !LIST_EMPTY(&indirdep->ir_deplisthd) ||
14656 indirdep->ir_saveddata != NULL ||
14657 indirdep->ir_savebp == NULL) {
14658 printf("%s: skipping orphaned indirdep %p\n",
14659 __FUNCTION__, indirdep);
14660 continue;
14661 }
14662 printf("%s: freeing orphaned indirdep %p\n",
14663 __FUNCTION__, indirdep);
14664 bp = indirdep->ir_savebp;
14665 indirdep->ir_savebp = NULL;
14666 free_indirdep(indirdep);
14667 FREE_LOCK(ump);
14668 brelse(bp);
14669 while (!TRY_ACQUIRE_LOCK(ump)) {
14670 BO_UNLOCK(bo);
14671 ACQUIRE_LOCK(ump);
14672 FREE_LOCK(ump);
14673 BO_LOCK(bo);
14674 }
14675 }
14676 }
14677
14678 /*
14679 * Reasons for needing more work before suspend:
14680 * - Dirty buffers on devvp.
14681 * - Dependency structures still exist
14682 * - Softdep activity occurred after start of vnode sync loop
14683 * - Secondary writes occurred after start of vnode sync loop
14684 */
14685 error = 0;
14686 if (bo->bo_numoutput > 0 ||
14687 bo->bo_dirty.bv_cnt > 0 ||
14688 softdep_depcnt != unlinked ||
14689 ump->softdep_deps != unlinked ||
14690 softdep_accdepcnt != ump->softdep_accdeps ||
14691 secondary_writes != 0 ||
14692 mp->mnt_secondary_writes != 0 ||
14693 secondary_accwrites != mp->mnt_secondary_accwrites)
14694 error = EAGAIN;
14695 FREE_LOCK(ump);
14696 BO_UNLOCK(bo);
14697 return (error);
14698 }
14699
14700 /*
14701 * Get the number of dependency structures for the file system, both
14702 * the current number and the total number allocated. These will
14703 * later be used to detect that softdep processing has occurred.
14704 */
14705 void
softdep_get_depcounts(struct mount * mp,int * softdep_depsp,int * softdep_accdepsp)14706 softdep_get_depcounts(struct mount *mp,
14707 int *softdep_depsp,
14708 int *softdep_accdepsp)
14709 {
14710 struct ufsmount *ump;
14711
14712 if (MOUNTEDSOFTDEP(mp) == 0) {
14713 *softdep_depsp = 0;
14714 *softdep_accdepsp = 0;
14715 return;
14716 }
14717 ump = VFSTOUFS(mp);
14718 ACQUIRE_LOCK(ump);
14719 *softdep_depsp = ump->softdep_deps;
14720 *softdep_accdepsp = ump->softdep_accdeps;
14721 FREE_LOCK(ump);
14722 }
14723
14724 /*
14725 * Wait for pending output on a vnode to complete.
14726 */
14727 static void
drain_output(struct vnode * vp)14728 drain_output(struct vnode *vp)
14729 {
14730
14731 ASSERT_VOP_LOCKED(vp, "drain_output");
14732 (void)bufobj_wwait(&vp->v_bufobj, 0, 0);
14733 }
14734
14735 /*
14736 * Called whenever a buffer that is being invalidated or reallocated
14737 * contains dependencies. This should only happen if an I/O error has
14738 * occurred. The routine is called with the buffer locked.
14739 */
14740 static void
softdep_deallocate_dependencies(struct buf * bp)14741 softdep_deallocate_dependencies(struct buf *bp)
14742 {
14743
14744 if ((bp->b_ioflags & BIO_ERROR) == 0)
14745 panic("softdep_deallocate_dependencies: dangling deps");
14746 if (bp->b_vp != NULL && bp->b_vp->v_mount != NULL)
14747 softdep_error(bp->b_vp->v_mount->mnt_stat.f_mntonname, bp->b_error);
14748 else
14749 printf("softdep_deallocate_dependencies: "
14750 "got error %d while accessing filesystem\n", bp->b_error);
14751 if (bp->b_error != ENXIO)
14752 panic("softdep_deallocate_dependencies: unrecovered I/O error");
14753 }
14754
14755 /*
14756 * Function to handle asynchronous write errors in the filesystem.
14757 */
14758 static void
softdep_error(char * func,int error)14759 softdep_error(char *func, int error)
14760 {
14761
14762 /* XXX should do something better! */
14763 printf("%s: got error %d while accessing filesystem\n", func, error);
14764 }
14765
14766 #ifdef DDB
14767
14768 /* exported to ffs_vfsops.c */
14769 extern void db_print_ffs(struct ufsmount *ump);
14770 void
db_print_ffs(struct ufsmount * ump)14771 db_print_ffs(struct ufsmount *ump)
14772 {
14773 db_printf("mp %p (%s) devvp %p\n", ump->um_mountp,
14774 ump->um_mountp->mnt_stat.f_mntonname, ump->um_devvp);
14775 db_printf(" fs %p ", ump->um_fs);
14776
14777 if (ump->um_softdep != NULL) {
14778 db_printf("su_wl %d su_deps %d su_req %d\n",
14779 ump->softdep_on_worklist, ump->softdep_deps,
14780 ump->softdep_req);
14781 } else {
14782 db_printf("su disabled\n");
14783 }
14784 }
14785
14786 static void
worklist_print(struct worklist * wk,int verbose)14787 worklist_print(struct worklist *wk, int verbose)
14788 {
14789
14790 if (!verbose) {
14791 db_printf("%s: %p state 0x%b\n", TYPENAME(wk->wk_type), wk,
14792 wk->wk_state, PRINT_SOFTDEP_FLAGS);
14793 return;
14794 }
14795 db_printf("worklist: %p type %s state 0x%b next %p\n ", wk,
14796 TYPENAME(wk->wk_type), wk->wk_state, PRINT_SOFTDEP_FLAGS,
14797 LIST_NEXT(wk, wk_list));
14798 db_print_ffs(VFSTOUFS(wk->wk_mp));
14799 }
14800
14801 static void
inodedep_print(struct inodedep * inodedep,int verbose)14802 inodedep_print(struct inodedep *inodedep, int verbose)
14803 {
14804
14805 worklist_print(&inodedep->id_list, 0);
14806 db_printf(" fs %p ino %jd inoblk %jd delta %jd nlink %jd\n",
14807 inodedep->id_fs,
14808 (intmax_t)inodedep->id_ino,
14809 (intmax_t)fsbtodb(inodedep->id_fs,
14810 ino_to_fsba(inodedep->id_fs, inodedep->id_ino)),
14811 (intmax_t)inodedep->id_nlinkdelta,
14812 (intmax_t)inodedep->id_savednlink);
14813
14814 if (verbose == 0)
14815 return;
14816
14817 db_printf(" bmsafemap %p, mkdiradd %p, inoreflst %p\n",
14818 inodedep->id_bmsafemap,
14819 inodedep->id_mkdiradd,
14820 TAILQ_FIRST(&inodedep->id_inoreflst));
14821 db_printf(" dirremhd %p, pendinghd %p, bufwait %p\n",
14822 LIST_FIRST(&inodedep->id_dirremhd),
14823 LIST_FIRST(&inodedep->id_pendinghd),
14824 LIST_FIRST(&inodedep->id_bufwait));
14825 db_printf(" inowait %p, inoupdt %p, newinoupdt %p\n",
14826 LIST_FIRST(&inodedep->id_inowait),
14827 TAILQ_FIRST(&inodedep->id_inoupdt),
14828 TAILQ_FIRST(&inodedep->id_newinoupdt));
14829 db_printf(" extupdt %p, newextupdt %p, freeblklst %p\n",
14830 TAILQ_FIRST(&inodedep->id_extupdt),
14831 TAILQ_FIRST(&inodedep->id_newextupdt),
14832 TAILQ_FIRST(&inodedep->id_freeblklst));
14833 db_printf(" saveino %p, savedsize %jd, savedextsize %jd\n",
14834 inodedep->id_savedino1,
14835 (intmax_t)inodedep->id_savedsize,
14836 (intmax_t)inodedep->id_savedextsize);
14837 }
14838
14839 static void
newblk_print(struct newblk * nbp)14840 newblk_print(struct newblk *nbp)
14841 {
14842
14843 worklist_print(&nbp->nb_list, 0);
14844 db_printf(" newblkno %jd\n", (intmax_t)nbp->nb_newblkno);
14845 db_printf(" jnewblk %p, bmsafemap %p, freefrag %p\n",
14846 &nbp->nb_jnewblk,
14847 &nbp->nb_bmsafemap,
14848 &nbp->nb_freefrag);
14849 db_printf(" indirdeps %p, newdirblk %p, jwork %p\n",
14850 LIST_FIRST(&nbp->nb_indirdeps),
14851 LIST_FIRST(&nbp->nb_newdirblk),
14852 LIST_FIRST(&nbp->nb_jwork));
14853 }
14854
14855 static void
allocdirect_print(struct allocdirect * adp)14856 allocdirect_print(struct allocdirect *adp)
14857 {
14858
14859 newblk_print(&adp->ad_block);
14860 db_printf(" oldblkno %jd, oldsize %ld, newsize %ld\n",
14861 adp->ad_oldblkno, adp->ad_oldsize, adp->ad_newsize);
14862 db_printf(" offset %d, inodedep %p\n",
14863 adp->ad_offset, adp->ad_inodedep);
14864 }
14865
14866 static void
allocindir_print(struct allocindir * aip)14867 allocindir_print(struct allocindir *aip)
14868 {
14869
14870 newblk_print(&aip->ai_block);
14871 db_printf(" oldblkno %jd, lbn %jd\n",
14872 (intmax_t)aip->ai_oldblkno, (intmax_t)aip->ai_lbn);
14873 db_printf(" offset %d, indirdep %p\n",
14874 aip->ai_offset, aip->ai_indirdep);
14875 }
14876
14877 static void
mkdir_print(struct mkdir * mkdir)14878 mkdir_print(struct mkdir *mkdir)
14879 {
14880
14881 worklist_print(&mkdir->md_list, 0);
14882 db_printf(" diradd %p, jaddref %p, buf %p\n",
14883 mkdir->md_diradd, mkdir->md_jaddref, mkdir->md_buf);
14884 }
14885
DB_SHOW_COMMAND(sd_inodedep,db_show_sd_inodedep)14886 DB_SHOW_COMMAND(sd_inodedep, db_show_sd_inodedep)
14887 {
14888
14889 if (have_addr == 0) {
14890 db_printf("inodedep address required\n");
14891 return;
14892 }
14893 inodedep_print((struct inodedep*)addr, 1);
14894 }
14895
DB_SHOW_COMMAND(sd_allinodedeps,db_show_sd_allinodedeps)14896 DB_SHOW_COMMAND(sd_allinodedeps, db_show_sd_allinodedeps)
14897 {
14898 struct inodedep_hashhead *inodedephd;
14899 struct inodedep *inodedep;
14900 struct ufsmount *ump;
14901 int cnt;
14902
14903 if (have_addr == 0) {
14904 db_printf("ufsmount address required\n");
14905 return;
14906 }
14907 ump = (struct ufsmount *)addr;
14908 for (cnt = 0; cnt < ump->inodedep_hash_size; cnt++) {
14909 inodedephd = &ump->inodedep_hashtbl[cnt];
14910 LIST_FOREACH(inodedep, inodedephd, id_hash) {
14911 inodedep_print(inodedep, 0);
14912 }
14913 }
14914 }
14915
DB_SHOW_COMMAND(sd_worklist,db_show_sd_worklist)14916 DB_SHOW_COMMAND(sd_worklist, db_show_sd_worklist)
14917 {
14918
14919 if (have_addr == 0) {
14920 db_printf("worklist address required\n");
14921 return;
14922 }
14923 worklist_print((struct worklist *)addr, 1);
14924 }
14925
DB_SHOW_COMMAND(sd_workhead,db_show_sd_workhead)14926 DB_SHOW_COMMAND(sd_workhead, db_show_sd_workhead)
14927 {
14928 struct worklist *wk;
14929 struct workhead *wkhd;
14930
14931 if (have_addr == 0) {
14932 db_printf("worklist address required "
14933 "(for example value in bp->b_dep)\n");
14934 return;
14935 }
14936 /*
14937 * We often do not have the address of the worklist head but
14938 * instead a pointer to its first entry (e.g., we have the
14939 * contents of bp->b_dep rather than &bp->b_dep). But the back
14940 * pointer of bp->b_dep will point at the head of the list, so
14941 * we cheat and use that instead. If we are in the middle of
14942 * a list we will still get the same result, so nothing
14943 * unexpected will result.
14944 */
14945 wk = (struct worklist *)addr;
14946 if (wk == NULL)
14947 return;
14948 wkhd = (struct workhead *)wk->wk_list.le_prev;
14949 LIST_FOREACH(wk, wkhd, wk_list) {
14950 switch(wk->wk_type) {
14951 case D_INODEDEP:
14952 inodedep_print(WK_INODEDEP(wk), 0);
14953 continue;
14954 case D_ALLOCDIRECT:
14955 allocdirect_print(WK_ALLOCDIRECT(wk));
14956 continue;
14957 case D_ALLOCINDIR:
14958 allocindir_print(WK_ALLOCINDIR(wk));
14959 continue;
14960 case D_MKDIR:
14961 mkdir_print(WK_MKDIR(wk));
14962 continue;
14963 default:
14964 worklist_print(wk, 0);
14965 continue;
14966 }
14967 }
14968 }
14969
DB_SHOW_COMMAND(sd_mkdir,db_show_sd_mkdir)14970 DB_SHOW_COMMAND(sd_mkdir, db_show_sd_mkdir)
14971 {
14972 if (have_addr == 0) {
14973 db_printf("mkdir address required\n");
14974 return;
14975 }
14976 mkdir_print((struct mkdir *)addr);
14977 }
14978
DB_SHOW_COMMAND(sd_mkdir_list,db_show_sd_mkdir_list)14979 DB_SHOW_COMMAND(sd_mkdir_list, db_show_sd_mkdir_list)
14980 {
14981 struct mkdirlist *mkdirlisthd;
14982 struct mkdir *mkdir;
14983
14984 if (have_addr == 0) {
14985 db_printf("mkdir listhead address required\n");
14986 return;
14987 }
14988 mkdirlisthd = (struct mkdirlist *)addr;
14989 LIST_FOREACH(mkdir, mkdirlisthd, md_mkdirs) {
14990 mkdir_print(mkdir);
14991 if (mkdir->md_diradd != NULL) {
14992 db_printf(" ");
14993 worklist_print(&mkdir->md_diradd->da_list, 0);
14994 }
14995 if (mkdir->md_jaddref != NULL) {
14996 db_printf(" ");
14997 worklist_print(&mkdir->md_jaddref->ja_list, 0);
14998 }
14999 }
15000 }
15001
DB_SHOW_COMMAND(sd_allocdirect,db_show_sd_allocdirect)15002 DB_SHOW_COMMAND(sd_allocdirect, db_show_sd_allocdirect)
15003 {
15004 if (have_addr == 0) {
15005 db_printf("allocdirect address required\n");
15006 return;
15007 }
15008 allocdirect_print((struct allocdirect *)addr);
15009 }
15010
DB_SHOW_COMMAND(sd_allocindir,db_show_sd_allocindir)15011 DB_SHOW_COMMAND(sd_allocindir, db_show_sd_allocindir)
15012 {
15013 if (have_addr == 0) {
15014 db_printf("allocindir address required\n");
15015 return;
15016 }
15017 allocindir_print((struct allocindir *)addr);
15018 }
15019
15020 #endif /* DDB */
15021
15022 #endif /* SOFTUPDATES */
15023