]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - repair/phase6.c
Merge branch 'libxfs-4.15-sync' into for-next
[thirdparty/xfsprogs-dev.git] / repair / phase6.c
1 /*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "libxfs.h"
20 #include "threads.h"
21 #include "prefetch.h"
22 #include "avl.h"
23 #include "globals.h"
24 #include "agheader.h"
25 #include "incore.h"
26 #include "dir2.h"
27 #include "protos.h"
28 #include "err_protos.h"
29 #include "dinode.h"
30 #include "progress.h"
31 #include "versions.h"
32
33 static struct cred zerocr;
34 static struct fsxattr zerofsx;
35 static xfs_ino_t orphanage_ino;
36
37 static struct xfs_name xfs_name_dot = {(unsigned char *)".",
38 1,
39 XFS_DIR3_FT_DIR};
40
41 /*
42 * Data structures used to keep track of directories where the ".."
43 * entries are updated. These must be rebuilt after the initial pass
44 */
45 typedef struct dotdot_update {
46 struct list_head list;
47 ino_tree_node_t *irec;
48 xfs_agnumber_t agno;
49 int ino_offset;
50 } dotdot_update_t;
51
52 static LIST_HEAD(dotdot_update_list);
53 static int dotdot_update;
54
55 static void
56 add_dotdot_update(
57 xfs_agnumber_t agno,
58 ino_tree_node_t *irec,
59 int ino_offset)
60 {
61 dotdot_update_t *dir = malloc(sizeof(dotdot_update_t));
62
63 if (!dir)
64 do_error(_("malloc failed add_dotdot_update (%zu bytes)\n"),
65 sizeof(dotdot_update_t));
66
67 INIT_LIST_HEAD(&dir->list);
68 dir->irec = irec;
69 dir->agno = agno;
70 dir->ino_offset = ino_offset;
71
72 list_add(&dir->list, &dotdot_update_list);
73 }
74
75 /*
76 * Data structures and routines to keep track of directory entries
77 * and whether their leaf entry has been seen. Also used for name
78 * duplicate checking and rebuilding step if required.
79 */
80 typedef struct dir_hash_ent {
81 struct dir_hash_ent *nextbyaddr; /* next in addr bucket */
82 struct dir_hash_ent *nextbyhash; /* next in name bucket */
83 struct dir_hash_ent *nextbyorder; /* next in order added */
84 xfs_dahash_t hashval; /* hash value of name */
85 uint32_t address; /* offset of data entry */
86 xfs_ino_t inum; /* inode num of entry */
87 short junkit; /* name starts with / */
88 short seen; /* have seen leaf entry */
89 struct xfs_name name;
90 } dir_hash_ent_t;
91
92 typedef struct dir_hash_tab {
93 int size; /* size of hash tables */
94 int names_duped; /* 1 = ent names malloced */
95 dir_hash_ent_t *first; /* ptr to first added entry */
96 dir_hash_ent_t *last; /* ptr to last added entry */
97 dir_hash_ent_t **byhash; /* ptr to name hash buckets */
98 dir_hash_ent_t **byaddr; /* ptr to addr hash buckets */
99 } dir_hash_tab_t;
100
101 #define DIR_HASH_TAB_SIZE(n) \
102 (sizeof(dir_hash_tab_t) + (sizeof(dir_hash_ent_t *) * (n) * 2))
103 #define DIR_HASH_FUNC(t,a) ((a) % (t)->size)
104
105 /*
106 * Track the contents of the freespace table in a directory.
107 */
108 typedef struct freetab {
109 int naents; /* expected number of data blocks */
110 int nents; /* number of data blocks processed */
111 struct freetab_ent {
112 xfs_dir2_data_off_t v;
113 short s;
114 } ents[1];
115 } freetab_t;
116 #define FREETAB_SIZE(n) \
117 (offsetof(freetab_t, ents) + (sizeof(struct freetab_ent) * (n)))
118
119 #define DIR_HASH_CK_OK 0
120 #define DIR_HASH_CK_DUPLEAF 1
121 #define DIR_HASH_CK_BADHASH 2
122 #define DIR_HASH_CK_NODATA 3
123 #define DIR_HASH_CK_NOLEAF 4
124 #define DIR_HASH_CK_BADSTALE 5
125 #define DIR_HASH_CK_TOTAL 6
126
127 /*
128 * Need to handle CRC and validation errors specially here. If there is a
129 * validator error, re-read without the verifier so that we get a buffer we can
130 * check and repair. Re-attach the ops to the buffer after the read so that when
131 * it is rewritten the CRC is recalculated.
132 *
133 * If the buffer was not read, we return an error. If the buffer was read but
134 * had a CRC or corruption error, we reread it without the verifier and if it is
135 * read successfully we increment *crc_error and return 0. Otherwise we
136 * return the read error.
137 */
138 static int
139 dir_read_buf(
140 struct xfs_inode *ip,
141 xfs_dablk_t bno,
142 xfs_daddr_t mappedbno,
143 struct xfs_buf **bpp,
144 const struct xfs_buf_ops *ops,
145 int *crc_error)
146 {
147 int error;
148 int error2;
149
150 error = -libxfs_da_read_buf(NULL, ip, bno, mappedbno, bpp,
151 XFS_DATA_FORK, ops);
152
153 if (error != EFSBADCRC && error != EFSCORRUPTED)
154 return error;
155
156 error2 = -libxfs_da_read_buf(NULL, ip, bno, mappedbno, bpp,
157 XFS_DATA_FORK, NULL);
158 if (error2)
159 return error2;
160
161 (*crc_error)++;
162 (*bpp)->b_ops = ops;
163 return 0;
164 }
165
166 /*
167 * Returns 0 if the name already exists (ie. a duplicate)
168 */
169 static int
170 dir_hash_add(
171 xfs_mount_t *mp,
172 dir_hash_tab_t *hashtab,
173 uint32_t addr,
174 xfs_ino_t inum,
175 int namelen,
176 unsigned char *name,
177 uint8_t ftype)
178 {
179 xfs_dahash_t hash = 0;
180 int byaddr;
181 int byhash = 0;
182 dir_hash_ent_t *p;
183 int dup;
184 short junk;
185 struct xfs_name xname;
186
187 ASSERT(!hashtab->names_duped);
188
189 xname.name = name;
190 xname.len = namelen;
191 xname.type = ftype;
192
193 junk = name[0] == '/';
194 byaddr = DIR_HASH_FUNC(hashtab, addr);
195 dup = 0;
196
197 if (!junk) {
198 hash = mp->m_dirnameops->hashname(&xname);
199 byhash = DIR_HASH_FUNC(hashtab, hash);
200
201 /*
202 * search hash bucket for existing name.
203 */
204 for (p = hashtab->byhash[byhash]; p; p = p->nextbyhash) {
205 if (p->hashval == hash && p->name.len == namelen) {
206 if (memcmp(p->name.name, name, namelen) == 0) {
207 dup = 1;
208 junk = 1;
209 break;
210 }
211 }
212 }
213 }
214
215 if ((p = malloc(sizeof(*p))) == NULL)
216 do_error(_("malloc failed in dir_hash_add (%zu bytes)\n"),
217 sizeof(*p));
218
219 p->nextbyaddr = hashtab->byaddr[byaddr];
220 hashtab->byaddr[byaddr] = p;
221 if (hashtab->last)
222 hashtab->last->nextbyorder = p;
223 else
224 hashtab->first = p;
225 p->nextbyorder = NULL;
226 hashtab->last = p;
227
228 if (!(p->junkit = junk)) {
229 p->hashval = hash;
230 p->nextbyhash = hashtab->byhash[byhash];
231 hashtab->byhash[byhash] = p;
232 }
233 p->address = addr;
234 p->inum = inum;
235 p->seen = 0;
236 p->name = xname;
237
238 return !dup;
239 }
240
241 /*
242 * checks to see if any data entries are not in the leaf blocks
243 */
244 static int
245 dir_hash_unseen(
246 dir_hash_tab_t *hashtab)
247 {
248 int i;
249 dir_hash_ent_t *p;
250
251 for (i = 0; i < hashtab->size; i++) {
252 for (p = hashtab->byaddr[i]; p; p = p->nextbyaddr) {
253 if (p->seen == 0)
254 return 1;
255 }
256 }
257 return 0;
258 }
259
260 static int
261 dir_hash_check(
262 dir_hash_tab_t *hashtab,
263 xfs_inode_t *ip,
264 int seeval)
265 {
266 static char *seevalstr[DIR_HASH_CK_TOTAL];
267 static int done;
268
269 if (!done) {
270 seevalstr[DIR_HASH_CK_OK] = _("ok");
271 seevalstr[DIR_HASH_CK_DUPLEAF] = _("duplicate leaf");
272 seevalstr[DIR_HASH_CK_BADHASH] = _("hash value mismatch");
273 seevalstr[DIR_HASH_CK_NODATA] = _("no data entry");
274 seevalstr[DIR_HASH_CK_NOLEAF] = _("no leaf entry");
275 seevalstr[DIR_HASH_CK_BADSTALE] = _("bad stale count");
276 done = 1;
277 }
278
279 if (seeval == DIR_HASH_CK_OK && dir_hash_unseen(hashtab))
280 seeval = DIR_HASH_CK_NOLEAF;
281 if (seeval == DIR_HASH_CK_OK)
282 return 0;
283 do_warn(_("bad hash table for directory inode %" PRIu64 " (%s): "),
284 ip->i_ino, seevalstr[seeval]);
285 if (!no_modify)
286 do_warn(_("rebuilding\n"));
287 else
288 do_warn(_("would rebuild\n"));
289 return 1;
290 }
291
292 static void
293 dir_hash_done(
294 dir_hash_tab_t *hashtab)
295 {
296 int i;
297 dir_hash_ent_t *n;
298 dir_hash_ent_t *p;
299
300 for (i = 0; i < hashtab->size; i++) {
301 for (p = hashtab->byaddr[i]; p; p = n) {
302 n = p->nextbyaddr;
303 if (hashtab->names_duped)
304 free((void *)p->name.name);
305 free(p);
306 }
307 }
308 free(hashtab);
309 }
310
311 static dir_hash_tab_t *
312 dir_hash_init(
313 xfs_fsize_t size)
314 {
315 dir_hash_tab_t *hashtab;
316 int hsize;
317
318 hsize = size / (16 * 4);
319 if (hsize > 65536)
320 hsize = 63336;
321 else if (hsize < 16)
322 hsize = 16;
323 if ((hashtab = calloc(DIR_HASH_TAB_SIZE(hsize), 1)) == NULL)
324 do_error(_("calloc failed in dir_hash_init\n"));
325 hashtab->size = hsize;
326 hashtab->byhash = (dir_hash_ent_t**)((char *)hashtab +
327 sizeof(dir_hash_tab_t));
328 hashtab->byaddr = (dir_hash_ent_t**)((char *)hashtab +
329 sizeof(dir_hash_tab_t) + sizeof(dir_hash_ent_t*) * hsize);
330 return hashtab;
331 }
332
333 static int
334 dir_hash_see(
335 dir_hash_tab_t *hashtab,
336 xfs_dahash_t hash,
337 xfs_dir2_dataptr_t addr)
338 {
339 int i;
340 dir_hash_ent_t *p;
341
342 i = DIR_HASH_FUNC(hashtab, addr);
343 for (p = hashtab->byaddr[i]; p; p = p->nextbyaddr) {
344 if (p->address != addr)
345 continue;
346 if (p->seen)
347 return DIR_HASH_CK_DUPLEAF;
348 if (p->junkit == 0 && p->hashval != hash)
349 return DIR_HASH_CK_BADHASH;
350 p->seen = 1;
351 return DIR_HASH_CK_OK;
352 }
353 return DIR_HASH_CK_NODATA;
354 }
355
356 static void
357 dir_hash_update_ftype(
358 dir_hash_tab_t *hashtab,
359 xfs_dir2_dataptr_t addr,
360 uint8_t ftype)
361 {
362 int i;
363 dir_hash_ent_t *p;
364
365 i = DIR_HASH_FUNC(hashtab, addr);
366 for (p = hashtab->byaddr[i]; p; p = p->nextbyaddr) {
367 if (p->address != addr)
368 continue;
369 p->name.type = ftype;
370 }
371 }
372
373 /*
374 * checks to make sure leafs match a data entry, and that the stale
375 * count is valid.
376 */
377 static int
378 dir_hash_see_all(
379 dir_hash_tab_t *hashtab,
380 xfs_dir2_leaf_entry_t *ents,
381 int count,
382 int stale)
383 {
384 int i;
385 int j;
386 int rval;
387
388 for (i = j = 0; i < count; i++) {
389 if (be32_to_cpu(ents[i].address) == XFS_DIR2_NULL_DATAPTR) {
390 j++;
391 continue;
392 }
393 rval = dir_hash_see(hashtab, be32_to_cpu(ents[i].hashval),
394 be32_to_cpu(ents[i].address));
395 if (rval != DIR_HASH_CK_OK)
396 return rval;
397 }
398 return j == stale ? DIR_HASH_CK_OK : DIR_HASH_CK_BADSTALE;
399 }
400
401 /*
402 * Convert name pointers into locally allocated memory.
403 * This must only be done after all the entries have been added.
404 */
405 static void
406 dir_hash_dup_names(dir_hash_tab_t *hashtab)
407 {
408 unsigned char *name;
409 dir_hash_ent_t *p;
410
411 if (hashtab->names_duped)
412 return;
413
414 for (p = hashtab->first; p; p = p->nextbyorder) {
415 name = malloc(p->name.len);
416 memcpy(name, p->name.name, p->name.len);
417 p->name.name = name;
418 }
419 hashtab->names_duped = 1;
420 }
421
422 /*
423 * Given a block number in a fork, return the next valid block number
424 * (not a hole).
425 * If this is the last block number then NULLFILEOFF is returned.
426 *
427 * This was originally in the kernel, but only used in xfs_repair.
428 */
429 static int
430 bmap_next_offset(
431 xfs_trans_t *tp, /* transaction pointer */
432 xfs_inode_t *ip, /* incore inode */
433 xfs_fileoff_t *bnop, /* current block */
434 int whichfork) /* data or attr fork */
435 {
436 xfs_fileoff_t bno; /* current block */
437 int error; /* error return value */
438 xfs_bmbt_irec_t got; /* current extent value */
439 xfs_ifork_t *ifp; /* inode fork pointer */
440 struct xfs_iext_cursor icur;
441
442 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
443 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
444 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
445 return EIO;
446 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
447 *bnop = NULLFILEOFF;
448 return 0;
449 }
450 ifp = XFS_IFORK_PTR(ip, whichfork);
451 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
452 (error = -libxfs_iread_extents(tp, ip, whichfork)))
453 return error;
454 bno = *bnop + 1;
455 if (!libxfs_iext_lookup_extent(ip, ifp, bno, &icur, &got))
456 *bnop = NULLFILEOFF;
457 else
458 *bnop = got.br_startoff < bno ? bno : got.br_startoff;
459 return 0;
460 }
461
462
463 static void
464 res_failed(
465 int err)
466 {
467 if (err == ENOSPC) {
468 do_error(_("ran out of disk space!\n"));
469 } else
470 do_error(_("xfs_trans_reserve returned %d\n"), err);
471 }
472
473 void
474 mk_rbmino(xfs_mount_t *mp)
475 {
476 xfs_trans_t *tp;
477 xfs_inode_t *ip;
478 xfs_bmbt_irec_t *ep;
479 xfs_fsblock_t first;
480 int i;
481 int nmap;
482 int error;
483 struct xfs_defer_ops dfops;
484 xfs_fileoff_t bno;
485 xfs_bmbt_irec_t map[XFS_BMAP_MAX_NMAP];
486 int vers;
487 int times;
488 struct xfs_trans_res tres = {0};
489
490 /*
491 * first set up inode
492 */
493 i = -libxfs_trans_alloc(mp, &tres, 10, 0, 0, &tp);
494 if (i)
495 res_failed(i);
496
497 error = -libxfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0, 0, &ip);
498 if (error) {
499 do_error(
500 _("couldn't iget realtime bitmap inode -- error - %d\n"),
501 error);
502 }
503
504 vers = xfs_sb_version_hascrc(&mp->m_sb) ? 3 : 2;
505 memset(&ip->i_d, 0, sizeof(ip->i_d));
506
507 VFS_I(ip)->i_mode = S_IFREG;
508 ip->i_d.di_version = vers;
509 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
510 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
511
512 set_nlink(VFS_I(ip), 1); /* account for sb ptr */
513
514 times = XFS_ICHGTIME_CHG | XFS_ICHGTIME_MOD;
515 if (ip->i_d.di_version == 3) {
516 VFS_I(ip)->i_version = 1;
517 ip->i_d.di_flags2 = 0;
518 times |= XFS_ICHGTIME_CREATE;
519 }
520 libxfs_trans_ichgtime(tp, ip, times);
521
522 /*
523 * now the ifork
524 */
525 ip->i_df.if_flags = XFS_IFEXTENTS;
526 ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
527 ip->i_df.if_u1.if_root = NULL;
528
529 ip->i_d.di_size = mp->m_sb.sb_rbmblocks * mp->m_sb.sb_blocksize;
530
531 /*
532 * commit changes
533 */
534 libxfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
535 libxfs_trans_commit(tp);
536
537 /*
538 * then allocate blocks for file and fill with zeroes (stolen
539 * from mkfs)
540 */
541 error = -libxfs_trans_alloc(mp, &tres,
542 mp->m_sb.sb_rbmblocks + (XFS_BM_MAXLEVELS(mp,XFS_DATA_FORK) - 1),
543 0, 0, &tp);
544 if (error)
545 res_failed(error);
546
547 libxfs_trans_ijoin(tp, ip, 0);
548 bno = 0;
549 libxfs_defer_init(&dfops, &first);
550 while (bno < mp->m_sb.sb_rbmblocks) {
551 nmap = XFS_BMAP_MAX_NMAP;
552 error = -libxfs_bmapi_write(tp, ip, bno,
553 (xfs_extlen_t)(mp->m_sb.sb_rbmblocks - bno),
554 0, &first, mp->m_sb.sb_rbmblocks,
555 map, &nmap, &dfops);
556 if (error) {
557 do_error(
558 _("couldn't allocate realtime bitmap, error = %d\n"),
559 error);
560 }
561 for (i = 0, ep = map; i < nmap; i++, ep++) {
562 libxfs_device_zero(mp->m_ddev_targp,
563 XFS_FSB_TO_DADDR(mp, ep->br_startblock),
564 XFS_FSB_TO_BB(mp, ep->br_blockcount));
565 bno += ep->br_blockcount;
566 }
567 }
568 libxfs_defer_ijoin(&dfops, ip);
569 error = -libxfs_defer_finish(&tp, &dfops);
570 if (error) {
571 do_error(
572 _("allocation of the realtime bitmap failed, error = %d\n"),
573 error);
574 }
575 libxfs_trans_commit(tp);
576 IRELE(ip);
577 }
578
579 static int
580 fill_rbmino(xfs_mount_t *mp)
581 {
582 xfs_buf_t *bp;
583 xfs_trans_t *tp;
584 xfs_inode_t *ip;
585 xfs_rtword_t *bmp;
586 xfs_fsblock_t first;
587 int nmap;
588 int error;
589 xfs_fileoff_t bno;
590 xfs_bmbt_irec_t map;
591 struct xfs_trans_res tres = {0};
592
593 bmp = btmcompute;
594 bno = 0;
595
596 error = -libxfs_trans_alloc(mp, &tres, 10, 0, 0, &tp);
597 if (error)
598 res_failed(error);
599
600 error = -libxfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0, 0, &ip);
601 if (error) {
602 do_error(
603 _("couldn't iget realtime bitmap inode -- error - %d\n"),
604 error);
605 }
606
607 first = NULLFSBLOCK;
608 while (bno < mp->m_sb.sb_rbmblocks) {
609 /*
610 * fill the file one block at a time
611 */
612 nmap = 1;
613 error = -libxfs_bmapi_write(tp, ip, bno, 1, 0,
614 &first, 1, &map, &nmap, NULL);
615 if (error || nmap != 1) {
616 do_error(
617 _("couldn't map realtime bitmap block %" PRIu64 ", error = %d\n"),
618 bno, error);
619 }
620
621 ASSERT(map.br_startblock != HOLESTARTBLOCK);
622
623 error = -libxfs_trans_read_buf(
624 mp, tp, mp->m_dev,
625 XFS_FSB_TO_DADDR(mp, map.br_startblock),
626 XFS_FSB_TO_BB(mp, 1), 1, &bp, NULL);
627
628 if (error) {
629 do_warn(
630 _("can't access block %" PRIu64 " (fsbno %" PRIu64 ") of realtime bitmap inode %" PRIu64 "\n"),
631 bno, map.br_startblock, mp->m_sb.sb_rbmino);
632 return(1);
633 }
634
635 memmove(XFS_BUF_PTR(bp), bmp, mp->m_sb.sb_blocksize);
636
637 libxfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
638
639 bmp = (xfs_rtword_t *)((intptr_t) bmp + mp->m_sb.sb_blocksize);
640 bno++;
641 }
642
643 libxfs_trans_commit(tp);
644 IRELE(ip);
645 return(0);
646 }
647
648 static int
649 fill_rsumino(xfs_mount_t *mp)
650 {
651 xfs_buf_t *bp;
652 xfs_trans_t *tp;
653 xfs_inode_t *ip;
654 xfs_suminfo_t *smp;
655 xfs_fsblock_t first;
656 int nmap;
657 int error;
658 xfs_fileoff_t bno;
659 xfs_fileoff_t end_bno;
660 xfs_bmbt_irec_t map;
661 struct xfs_trans_res tres = {0};
662
663 smp = sumcompute;
664 bno = 0;
665 end_bno = mp->m_rsumsize >> mp->m_sb.sb_blocklog;
666
667 error = -libxfs_trans_alloc(mp, &tres, 10, 0, 0, &tp);
668 if (error)
669 res_failed(error);
670
671 error = -libxfs_trans_iget(mp, tp, mp->m_sb.sb_rsumino, 0, 0, &ip);
672 if (error) {
673 do_error(
674 _("couldn't iget realtime summary inode -- error - %d\n"),
675 error);
676 }
677
678 first = NULLFSBLOCK;
679 while (bno < end_bno) {
680 /*
681 * fill the file one block at a time
682 */
683 nmap = 1;
684 error = -libxfs_bmapi_write(tp, ip, bno, 1, 0,
685 &first, 1, &map, &nmap, NULL);
686 if (error || nmap != 1) {
687 do_error(
688 _("couldn't map realtime summary inode block %" PRIu64 ", error = %d\n"),
689 bno, error);
690 }
691
692 ASSERT(map.br_startblock != HOLESTARTBLOCK);
693
694 error = -libxfs_trans_read_buf(
695 mp, tp, mp->m_dev,
696 XFS_FSB_TO_DADDR(mp, map.br_startblock),
697 XFS_FSB_TO_BB(mp, 1), 1, &bp, NULL);
698
699 if (error) {
700 do_warn(
701 _("can't access block %" PRIu64 " (fsbno %" PRIu64 ") of realtime summary inode %" PRIu64 "\n"),
702 bno, map.br_startblock, mp->m_sb.sb_rsumino);
703 IRELE(ip);
704 return(1);
705 }
706
707 memmove(XFS_BUF_PTR(bp), smp, mp->m_sb.sb_blocksize);
708
709 libxfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
710
711 smp = (xfs_suminfo_t *)((intptr_t)smp + mp->m_sb.sb_blocksize);
712 bno++;
713 }
714
715 libxfs_trans_commit(tp);
716 IRELE(ip);
717 return(0);
718 }
719
720 static void
721 mk_rsumino(xfs_mount_t *mp)
722 {
723 xfs_trans_t *tp;
724 xfs_inode_t *ip;
725 xfs_bmbt_irec_t *ep;
726 xfs_fsblock_t first;
727 int i;
728 int nmap;
729 int error;
730 int nsumblocks;
731 struct xfs_defer_ops dfops;
732 xfs_fileoff_t bno;
733 xfs_bmbt_irec_t map[XFS_BMAP_MAX_NMAP];
734 int vers;
735 int times;
736 struct xfs_trans_res tres = {0};
737
738 /*
739 * first set up inode
740 */
741 i = -libxfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 10, 0, 0, &tp);
742 if (i)
743 res_failed(i);
744
745 error = -libxfs_trans_iget(mp, tp, mp->m_sb.sb_rsumino, 0, 0, &ip);
746 if (error) {
747 do_error(
748 _("couldn't iget realtime summary inode -- error - %d\n"),
749 error);
750 }
751
752 vers = xfs_sb_version_hascrc(&mp->m_sb) ? 3 : 2;
753 memset(&ip->i_d, 0, sizeof(ip->i_d));
754
755 VFS_I(ip)->i_mode = S_IFREG;
756 ip->i_d.di_version = vers;
757 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
758 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
759
760 set_nlink(VFS_I(ip), 1); /* account for sb ptr */
761
762 times = XFS_ICHGTIME_CHG | XFS_ICHGTIME_MOD;
763 if (ip->i_d.di_version == 3) {
764 VFS_I(ip)->i_version = 1;
765 ip->i_d.di_flags2 = 0;
766 times |= XFS_ICHGTIME_CREATE;
767 }
768 libxfs_trans_ichgtime(tp, ip, times);
769
770 /*
771 * now the ifork
772 */
773 ip->i_df.if_flags = XFS_IFEXTENTS;
774 ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
775 ip->i_df.if_u1.if_root = NULL;
776
777 ip->i_d.di_size = mp->m_rsumsize;
778
779 /*
780 * commit changes
781 */
782 libxfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
783 libxfs_trans_commit(tp);
784
785 /*
786 * then allocate blocks for file and fill with zeroes (stolen
787 * from mkfs)
788 */
789 libxfs_defer_init(&dfops, &first);
790
791 nsumblocks = mp->m_rsumsize >> mp->m_sb.sb_blocklog;
792 tres.tr_logres = BBTOB(128);
793 tres.tr_logcount = XFS_DEFAULT_PERM_LOG_COUNT;
794 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
795 error = -libxfs_trans_alloc(mp, &tres,
796 mp->m_sb.sb_rbmblocks + (XFS_BM_MAXLEVELS(mp,XFS_DATA_FORK) - 1),
797 0, 0, &tp);
798 if (error)
799 res_failed(error);
800
801 libxfs_trans_ijoin(tp, ip, 0);
802 bno = 0;
803 libxfs_defer_init(&dfops, &first);
804 while (bno < nsumblocks) {
805 nmap = XFS_BMAP_MAX_NMAP;
806 error = -libxfs_bmapi_write(tp, ip, bno,
807 (xfs_extlen_t)(nsumblocks - bno),
808 0, &first, nsumblocks, map, &nmap, &dfops);
809 if (error) {
810 do_error(
811 _("couldn't allocate realtime summary inode, error = %d\n"),
812 error);
813 }
814 for (i = 0, ep = map; i < nmap; i++, ep++) {
815 libxfs_device_zero(mp->m_ddev_targp,
816 XFS_FSB_TO_DADDR(mp, ep->br_startblock),
817 XFS_FSB_TO_BB(mp, ep->br_blockcount));
818 bno += ep->br_blockcount;
819 }
820 }
821 libxfs_defer_ijoin(&dfops, ip);
822 error = -libxfs_defer_finish(&tp, &dfops);
823 if (error) {
824 do_error(
825 _("allocation of the realtime summary ino failed, error = %d\n"),
826 error);
827 }
828 libxfs_trans_commit(tp);
829 IRELE(ip);
830 }
831
832 /*
833 * makes a new root directory.
834 */
835 static void
836 mk_root_dir(xfs_mount_t *mp)
837 {
838 xfs_trans_t *tp;
839 xfs_inode_t *ip;
840 int i;
841 int error;
842 const mode_t mode = 0755;
843 ino_tree_node_t *irec;
844 int vers;
845 int times;
846
847 ip = NULL;
848 i = -libxfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 10, 0, 0, &tp);
849 if (i)
850 res_failed(i);
851
852 error = -libxfs_trans_iget(mp, tp, mp->m_sb.sb_rootino, 0, 0, &ip);
853 if (error) {
854 do_error(_("could not iget root inode -- error - %d\n"), error);
855 }
856
857 /*
858 * take care of the core -- initialization from xfs_ialloc()
859 */
860 vers = xfs_sb_version_hascrc(&mp->m_sb) ? 3 : 2;
861 memset(&ip->i_d, 0, sizeof(ip->i_d));
862
863 VFS_I(ip)->i_mode = mode|S_IFDIR;
864 ip->i_d.di_version = vers;
865 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
866 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
867
868 set_nlink(VFS_I(ip), 1); /* account for . */
869
870 times = XFS_ICHGTIME_CHG | XFS_ICHGTIME_MOD;
871 if (ip->i_d.di_version == 3) {
872 VFS_I(ip)->i_version = 1;
873 ip->i_d.di_flags2 = 0;
874 times |= XFS_ICHGTIME_CREATE;
875 }
876 libxfs_trans_ichgtime(tp, ip, times);
877
878 libxfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
879
880 /*
881 * now the ifork
882 */
883 ip->i_df.if_flags = XFS_IFEXTENTS;
884 ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
885 ip->i_df.if_u1.if_root = NULL;
886
887
888
889 /*
890 * initialize the directory
891 */
892 ip->d_ops = mp->m_dir_inode_ops;
893 libxfs_dir_init(tp, ip, ip);
894
895 libxfs_trans_commit(tp);
896 IRELE(ip);
897
898 irec = find_inode_rec(mp, XFS_INO_TO_AGNO(mp, mp->m_sb.sb_rootino),
899 XFS_INO_TO_AGINO(mp, mp->m_sb.sb_rootino));
900 set_inode_isadir(irec, XFS_INO_TO_AGINO(mp, mp->m_sb.sb_rootino) -
901 irec->ino_startnum);
902 }
903
904 /*
905 * orphanage name == lost+found
906 */
907 static xfs_ino_t
908 mk_orphanage(xfs_mount_t *mp)
909 {
910 xfs_ino_t ino;
911 xfs_trans_t *tp;
912 xfs_inode_t *ip;
913 xfs_inode_t *pip;
914 xfs_fsblock_t first;
915 ino_tree_node_t *irec;
916 int ino_offset = 0;
917 int i;
918 int error;
919 struct xfs_defer_ops dfops;
920 const int mode = 0755;
921 int nres;
922 struct xfs_name xname;
923
924 /*
925 * check for an existing lost+found first, if it exists, return
926 * its inode. Otherwise, we can create it. Bad lost+found inodes
927 * would have been cleared in phase3 and phase4.
928 */
929
930 if ((i = -libxfs_iget(mp, NULL, mp->m_sb.sb_rootino, 0, &pip)))
931 do_error(_("%d - couldn't iget root inode to obtain %s\n"),
932 i, ORPHANAGE);
933
934 xname.name = (unsigned char *)ORPHANAGE;
935 xname.len = strlen(ORPHANAGE);
936 xname.type = XFS_DIR3_FT_DIR;
937
938 if (libxfs_dir_lookup(NULL, pip, &xname, &ino, NULL) == 0)
939 return ino;
940
941 /*
942 * could not be found, create it
943 */
944 libxfs_defer_init(&dfops, &first);
945 nres = XFS_MKDIR_SPACE_RES(mp, xname.len);
946 i = -libxfs_trans_alloc(mp, &M_RES(mp)->tr_mkdir, nres, 0, 0, &tp);
947 if (i)
948 res_failed(i);
949
950 /*
951 * use iget/ijoin instead of trans_iget because the ialloc
952 * wrapper can commit the transaction and start a new one
953 */
954 /* if ((i = -libxfs_iget(mp, NULL, mp->m_sb.sb_rootino, 0, &pip)))
955 do_error(_("%d - couldn't iget root inode to make %s\n"),
956 i, ORPHANAGE);*/
957
958 error = -libxfs_inode_alloc(&tp, pip, mode|S_IFDIR,
959 1, 0, &zerocr, &zerofsx, &ip);
960 if (error) {
961 do_error(_("%s inode allocation failed %d\n"),
962 ORPHANAGE, error);
963 }
964 inc_nlink(VFS_I(ip)); /* account for . */
965 ino = ip->i_ino;
966
967 irec = find_inode_rec(mp,
968 XFS_INO_TO_AGNO(mp, ino),
969 XFS_INO_TO_AGINO(mp, ino));
970
971 if (irec == NULL) {
972 /*
973 * This inode is allocated from a newly created inode
974 * chunk and therefore did not exist when inode chunks
975 * were processed in phase3. Add this group of inodes to
976 * the entry avl tree as if they were discovered in phase3.
977 */
978 irec = set_inode_free_alloc(mp, XFS_INO_TO_AGNO(mp, ino),
979 XFS_INO_TO_AGINO(mp, ino));
980 alloc_ex_data(irec);
981
982 for (i = 0; i < XFS_INODES_PER_CHUNK; i++)
983 set_inode_free(irec, i);
984 }
985
986 ino_offset = get_inode_offset(mp, ino, irec);
987
988 /*
989 * Mark the inode allocated to lost+found as used in the AVL tree
990 * so it is not skipped in phase 7
991 */
992 set_inode_used(irec, ino_offset);
993 add_inode_ref(irec, ino_offset);
994
995 /*
996 * now that we know the transaction will stay around,
997 * add the root inode to it
998 */
999 libxfs_trans_ijoin(tp, pip, 0);
1000
1001 /*
1002 * create the actual entry
1003 */
1004 error = -libxfs_dir_createname(tp, pip, &xname, ip->i_ino, &first,
1005 &dfops, nres);
1006 if (error)
1007 do_error(
1008 _("can't make %s, createname error %d\n"),
1009 ORPHANAGE, error);
1010
1011 /*
1012 * bump up the link count in the root directory to account
1013 * for .. in the new directory
1014 */
1015 inc_nlink(VFS_I(pip));
1016 add_inode_ref(find_inode_rec(mp,
1017 XFS_INO_TO_AGNO(mp, mp->m_sb.sb_rootino),
1018 XFS_INO_TO_AGINO(mp, mp->m_sb.sb_rootino)), 0);
1019
1020
1021
1022 libxfs_trans_log_inode(tp, pip, XFS_ILOG_CORE);
1023 libxfs_dir_init(tp, ip, pip);
1024 libxfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1025
1026 libxfs_defer_ijoin(&dfops, ip);
1027 error = -libxfs_defer_finish(&tp, &dfops);
1028 if (error) {
1029 do_error(_("%s directory creation failed -- bmapf error %d\n"),
1030 ORPHANAGE, error);
1031 }
1032
1033
1034 libxfs_trans_commit(tp);
1035 IRELE(ip);
1036 IRELE(pip);
1037 add_inode_reached(irec,ino_offset);
1038
1039 return(ino);
1040 }
1041
1042 /*
1043 * move a file to the orphange.
1044 */
1045 static void
1046 mv_orphanage(
1047 xfs_mount_t *mp,
1048 xfs_ino_t ino, /* inode # to be moved */
1049 int isa_dir) /* 1 if inode is a directory */
1050 {
1051 xfs_inode_t *orphanage_ip;
1052 xfs_ino_t entry_ino_num;
1053 xfs_inode_t *ino_p;
1054 xfs_trans_t *tp;
1055 xfs_fsblock_t first;
1056 struct xfs_defer_ops dfops;
1057 int err;
1058 unsigned char fname[MAXPATHLEN + 1];
1059 int nres;
1060 int incr;
1061 ino_tree_node_t *irec;
1062 int ino_offset = 0;
1063 struct xfs_name xname;
1064
1065 xname.name = fname;
1066 xname.len = snprintf((char *)fname, sizeof(fname), "%llu",
1067 (unsigned long long)ino);
1068
1069 err = -libxfs_iget(mp, NULL, orphanage_ino, 0, &orphanage_ip);
1070 if (err)
1071 do_error(_("%d - couldn't iget orphanage inode\n"), err);
1072 /*
1073 * Make sure the filename is unique in the lost+found
1074 */
1075 incr = 0;
1076 while (libxfs_dir_lookup(NULL, orphanage_ip, &xname, &entry_ino_num,
1077 NULL) == 0)
1078 xname.len = snprintf((char *)fname, sizeof(fname), "%llu.%d",
1079 (unsigned long long)ino, ++incr);
1080
1081 if ((err = -libxfs_iget(mp, NULL, ino, 0, &ino_p)))
1082 do_error(_("%d - couldn't iget disconnected inode\n"), err);
1083
1084 xname.type = xfs_mode_to_ftype(VFS_I(ino_p)->i_mode);
1085
1086 if (isa_dir) {
1087 irec = find_inode_rec(mp, XFS_INO_TO_AGNO(mp, orphanage_ino),
1088 XFS_INO_TO_AGINO(mp, orphanage_ino));
1089 if (irec)
1090 ino_offset = XFS_INO_TO_AGINO(mp, orphanage_ino) -
1091 irec->ino_startnum;
1092 nres = XFS_DIRENTER_SPACE_RES(mp, fnamelen) +
1093 XFS_DIRENTER_SPACE_RES(mp, 2);
1094 err = -libxfs_dir_lookup(NULL, ino_p, &xfs_name_dotdot,
1095 &entry_ino_num, NULL);
1096 if (err) {
1097 ASSERT(err == ENOENT);
1098
1099 err = -libxfs_trans_alloc(mp, &M_RES(mp)->tr_rename,
1100 nres, 0, 0, &tp);
1101 if (err)
1102 do_error(
1103 _("space reservation failed (%d), filesystem may be out of space\n"),
1104 err);
1105
1106 libxfs_trans_ijoin(tp, orphanage_ip, 0);
1107 libxfs_trans_ijoin(tp, ino_p, 0);
1108
1109 libxfs_defer_init(&dfops, &first);
1110 err = -libxfs_dir_createname(tp, orphanage_ip, &xname,
1111 ino, &first, &dfops, nres);
1112 if (err)
1113 do_error(
1114 _("name create failed in %s (%d), filesystem may be out of space\n"),
1115 ORPHANAGE, err);
1116
1117 if (irec)
1118 add_inode_ref(irec, ino_offset);
1119 else
1120 inc_nlink(VFS_I(orphanage_ip));
1121 libxfs_trans_log_inode(tp, orphanage_ip, XFS_ILOG_CORE);
1122
1123 err = -libxfs_dir_createname(tp, ino_p, &xfs_name_dotdot,
1124 orphanage_ino, &first, &dfops, nres);
1125 if (err)
1126 do_error(
1127 _("creation of .. entry failed (%d), filesystem may be out of space\n"),
1128 err);
1129
1130 inc_nlink(VFS_I(ino_p));
1131 libxfs_trans_log_inode(tp, ino_p, XFS_ILOG_CORE);
1132
1133 libxfs_defer_ijoin(&dfops, ino_p);
1134 err = -libxfs_defer_finish(&tp, &dfops);
1135 if (err)
1136 do_error(
1137 _("bmap finish failed (err - %d), filesystem may be out of space\n"),
1138 err);
1139
1140 libxfs_trans_commit(tp);
1141 } else {
1142 err = -libxfs_trans_alloc(mp, &M_RES(mp)->tr_rename,
1143 nres, 0, 0, &tp);
1144 if (err)
1145 do_error(
1146 _("space reservation failed (%d), filesystem may be out of space\n"),
1147 err);
1148
1149 libxfs_trans_ijoin(tp, orphanage_ip, 0);
1150 libxfs_trans_ijoin(tp, ino_p, 0);
1151
1152 libxfs_defer_init(&dfops, &first);
1153
1154 err = -libxfs_dir_createname(tp, orphanage_ip, &xname,
1155 ino, &first, &dfops, nres);
1156 if (err)
1157 do_error(
1158 _("name create failed in %s (%d), filesystem may be out of space\n"),
1159 ORPHANAGE, err);
1160
1161 if (irec)
1162 add_inode_ref(irec, ino_offset);
1163 else
1164 inc_nlink(VFS_I(orphanage_ip));
1165 libxfs_trans_log_inode(tp, orphanage_ip, XFS_ILOG_CORE);
1166
1167 /*
1168 * don't replace .. value if it already points
1169 * to us. that'll pop a libxfs/kernel ASSERT.
1170 */
1171 if (entry_ino_num != orphanage_ino) {
1172 err = -libxfs_dir_replace(tp, ino_p,
1173 &xfs_name_dotdot, orphanage_ino,
1174 &first, &dfops, nres);
1175 if (err)
1176 do_error(
1177 _("name replace op failed (%d), filesystem may be out of space\n"),
1178 err);
1179 }
1180
1181 libxfs_defer_ijoin(&dfops, ino_p);
1182 err = -libxfs_defer_finish(&tp, &dfops);
1183 if (err)
1184 do_error(
1185 _("bmap finish failed (%d), filesystem may be out of space\n"),
1186 err);
1187
1188 libxfs_trans_commit(tp);
1189 }
1190
1191 } else {
1192 /*
1193 * use the remove log reservation as that's
1194 * more accurate. we're only creating the
1195 * links, we're not doing the inode allocation
1196 * also accounted for in the create
1197 */
1198 nres = XFS_DIRENTER_SPACE_RES(mp, xname.len);
1199 err = -libxfs_trans_alloc(mp, &M_RES(mp)->tr_remove,
1200 nres, 0, 0, &tp);
1201 if (err)
1202 do_error(
1203 _("space reservation failed (%d), filesystem may be out of space\n"),
1204 err);
1205
1206 libxfs_trans_ijoin(tp, orphanage_ip, 0);
1207 libxfs_trans_ijoin(tp, ino_p, 0);
1208
1209 libxfs_defer_init(&dfops, &first);
1210 err = -libxfs_dir_createname(tp, orphanage_ip, &xname, ino,
1211 &first, &dfops, nres);
1212 if (err)
1213 do_error(
1214 _("name create failed in %s (%d), filesystem may be out of space\n"),
1215 ORPHANAGE, err);
1216 ASSERT(err == 0);
1217
1218 set_nlink(VFS_I(ino_p), 1);
1219 libxfs_trans_log_inode(tp, ino_p, XFS_ILOG_CORE);
1220
1221 libxfs_defer_ijoin(&dfops, ino_p);
1222 err = -libxfs_defer_finish(&tp, &dfops);
1223 if (err)
1224 do_error(
1225 _("bmap finish failed (%d), filesystem may be out of space\n"),
1226 err);
1227
1228 libxfs_trans_commit(tp);
1229 }
1230 IRELE(ino_p);
1231 IRELE(orphanage_ip);
1232 }
1233
1234 static int
1235 entry_junked(
1236 const char *msg,
1237 const char *iname,
1238 xfs_ino_t ino1,
1239 xfs_ino_t ino2)
1240 {
1241 do_warn(msg, iname, ino1, ino2);
1242 if (!no_modify) {
1243 if (verbose)
1244 do_warn(_(", marking entry to be junked\n"));
1245 else
1246 do_warn("\n");
1247 } else
1248 do_warn(_(", would junk entry\n"));
1249 return !no_modify;
1250 }
1251
1252 /*
1253 * Unexpected failure during the rebuild will leave the entries in
1254 * lost+found on the next run
1255 */
1256
1257 static void
1258 longform_dir2_rebuild(
1259 xfs_mount_t *mp,
1260 xfs_ino_t ino,
1261 xfs_inode_t *ip,
1262 ino_tree_node_t *irec,
1263 int ino_offset,
1264 dir_hash_tab_t *hashtab)
1265 {
1266 int error;
1267 int nres;
1268 xfs_trans_t *tp;
1269 xfs_fileoff_t lastblock;
1270 xfs_fsblock_t firstblock;
1271 struct xfs_defer_ops dfops;
1272 xfs_inode_t pip;
1273 dir_hash_ent_t *p;
1274 int done;
1275
1276 /*
1277 * trash directory completely and rebuild from scratch using the
1278 * name/inode pairs in the hash table
1279 */
1280
1281 do_warn(_("rebuilding directory inode %" PRIu64 "\n"), ino);
1282
1283 /*
1284 * first attempt to locate the parent inode, if it can't be
1285 * found, set it to the root inode and it'll be moved to the
1286 * orphanage later (the inode number here needs to be valid
1287 * for the libxfs_dir_init() call).
1288 */
1289 pip.i_ino = get_inode_parent(irec, ino_offset);
1290 if (pip.i_ino == NULLFSINO ||
1291 libxfs_dir_ino_validate(mp, pip.i_ino))
1292 pip.i_ino = mp->m_sb.sb_rootino;
1293
1294 libxfs_defer_init(&dfops, &firstblock);
1295
1296 nres = XFS_REMOVE_SPACE_RES(mp);
1297 error = -libxfs_trans_alloc(mp, &M_RES(mp)->tr_remove, nres, 0, 0, &tp);
1298 if (error)
1299 res_failed(error);
1300 libxfs_trans_ijoin(tp, ip, 0);
1301
1302 if ((error = -libxfs_bmap_last_offset(ip, &lastblock, XFS_DATA_FORK)))
1303 do_error(_("xfs_bmap_last_offset failed -- error - %d\n"),
1304 error);
1305
1306 /* free all data, leaf, node and freespace blocks */
1307 error = -libxfs_bunmapi(tp, ip, 0, lastblock, XFS_BMAPI_METADATA, 0,
1308 &firstblock, &dfops, &done);
1309 if (error) {
1310 do_warn(_("xfs_bunmapi failed -- error - %d\n"), error);
1311 goto out_bmap_cancel;
1312 }
1313
1314 ASSERT(done);
1315
1316 error = -libxfs_dir_init(tp, ip, &pip);
1317 if (error) {
1318 do_warn(_("xfs_dir_init failed -- error - %d\n"), error);
1319 goto out_bmap_cancel;
1320 }
1321
1322 libxfs_defer_ijoin(&dfops, ip);
1323 error = -libxfs_defer_finish(&tp, &dfops);
1324
1325 libxfs_trans_commit(tp);
1326
1327 if (ino == mp->m_sb.sb_rootino)
1328 need_root_dotdot = 0;
1329
1330 /* go through the hash list and re-add the inodes */
1331
1332 for (p = hashtab->first; p; p = p->nextbyorder) {
1333
1334 if (p->name.name[0] == '/' || (p->name.name[0] == '.' &&
1335 (p->name.len == 1 || (p->name.len == 2 &&
1336 p->name.name[1] == '.'))))
1337 continue;
1338
1339 nres = XFS_CREATE_SPACE_RES(mp, p->name.len);
1340 error = -libxfs_trans_alloc(mp, &M_RES(mp)->tr_create,
1341 nres, 0, 0, &tp);
1342 if (error)
1343 res_failed(error);
1344
1345 libxfs_trans_ijoin(tp, ip, 0);
1346
1347 libxfs_defer_init(&dfops, &firstblock);
1348 error = -libxfs_dir_createname(tp, ip, &p->name, p->inum,
1349 &firstblock, &dfops, nres);
1350 if (error) {
1351 do_warn(
1352 _("name create failed in ino %" PRIu64 " (%d), filesystem may be out of space\n"),
1353 ino, error);
1354 goto out_bmap_cancel;
1355 }
1356
1357 libxfs_defer_ijoin(&dfops, ip);
1358 error = -libxfs_defer_finish(&tp, &dfops);
1359 if (error) {
1360 do_warn(
1361 _("bmap finish failed (%d), filesystem may be out of space\n"),
1362 error);
1363 goto out_bmap_cancel;
1364 }
1365
1366 libxfs_trans_commit(tp);
1367 }
1368
1369 return;
1370
1371 out_bmap_cancel:
1372 libxfs_defer_cancel(&dfops);
1373 libxfs_trans_cancel(tp);
1374 return;
1375 }
1376
1377
1378 /*
1379 * Kill a block in a version 2 inode.
1380 * Makes its own transaction.
1381 */
1382 static void
1383 dir2_kill_block(
1384 xfs_mount_t *mp,
1385 xfs_inode_t *ip,
1386 xfs_dablk_t da_bno,
1387 struct xfs_buf *bp)
1388 {
1389 xfs_da_args_t args;
1390 int error;
1391 xfs_fsblock_t firstblock;
1392 struct xfs_defer_ops dfops;
1393 int nres;
1394 xfs_trans_t *tp;
1395
1396 nres = XFS_REMOVE_SPACE_RES(mp);
1397 error = -libxfs_trans_alloc(mp, &M_RES(mp)->tr_remove, nres, 0, 0, &tp);
1398 if (error)
1399 res_failed(error);
1400 libxfs_trans_ijoin(tp, ip, 0);
1401 libxfs_trans_bjoin(tp, bp);
1402 memset(&args, 0, sizeof(args));
1403 libxfs_defer_init(&dfops, &firstblock);
1404 args.dp = ip;
1405 args.trans = tp;
1406 args.firstblock = &firstblock;
1407 args.dfops = &dfops;
1408 args.whichfork = XFS_DATA_FORK;
1409 args.geo = mp->m_dir_geo;
1410 if (da_bno >= mp->m_dir_geo->leafblk && da_bno < mp->m_dir_geo->freeblk)
1411 error = -libxfs_da_shrink_inode(&args, da_bno, bp);
1412 else
1413 error = -libxfs_dir2_shrink_inode(&args,
1414 xfs_dir2_da_to_db(mp->m_dir_geo, da_bno), bp);
1415 if (error)
1416 do_error(_("shrink_inode failed inode %" PRIu64 " block %u\n"),
1417 ip->i_ino, da_bno);
1418 libxfs_defer_ijoin(&dfops, ip);
1419 libxfs_defer_finish(&tp, &dfops);
1420 libxfs_trans_commit(tp);
1421 }
1422
1423 /*
1424 * process a data block, also checks for .. entry
1425 * and corrects it to match what we think .. should be
1426 */
1427 static void
1428 longform_dir2_entry_check_data(
1429 xfs_mount_t *mp,
1430 xfs_inode_t *ip,
1431 int *num_illegal,
1432 int *need_dot,
1433 ino_tree_node_t *current_irec,
1434 int current_ino_offset,
1435 struct xfs_buf **bpp,
1436 dir_hash_tab_t *hashtab,
1437 freetab_t **freetabp,
1438 xfs_dablk_t da_bno,
1439 int isblock)
1440 {
1441 xfs_dir2_dataptr_t addr;
1442 xfs_dir2_leaf_entry_t *blp;
1443 struct xfs_buf *bp;
1444 xfs_dir2_block_tail_t *btp;
1445 struct xfs_dir2_data_hdr *d;
1446 xfs_dir2_db_t db;
1447 xfs_dir2_data_entry_t *dep;
1448 xfs_dir2_data_unused_t *dup;
1449 struct xfs_dir2_data_free *bf;
1450 char *endptr;
1451 int error;
1452 xfs_fsblock_t firstblock;
1453 struct xfs_defer_ops dfops;
1454 char fname[MAXNAMELEN + 1];
1455 freetab_t *freetab;
1456 int i;
1457 int ino_offset;
1458 xfs_ino_t inum;
1459 ino_tree_node_t *irec;
1460 int junkit;
1461 int lastfree;
1462 int len;
1463 int nbad;
1464 int needlog;
1465 int needscan;
1466 xfs_ino_t parent;
1467 char *ptr;
1468 xfs_trans_t *tp;
1469 int wantmagic;
1470 struct xfs_da_args da = {
1471 .dp = ip,
1472 .geo = mp->m_dir_geo,
1473 };
1474
1475
1476 bp = *bpp;
1477 d = bp->b_addr;
1478 ptr = (char *)M_DIROPS(mp)->data_entry_p(d);
1479 nbad = 0;
1480 needscan = needlog = 0;
1481 junkit = 0;
1482 freetab = *freetabp;
1483 if (isblock) {
1484 btp = xfs_dir2_block_tail_p(mp->m_dir_geo, d);
1485 blp = xfs_dir2_block_leaf_p(btp);
1486 endptr = (char *)blp;
1487 if (endptr > (char *)btp)
1488 endptr = (char *)btp;
1489 if (xfs_sb_version_hascrc(&mp->m_sb))
1490 wantmagic = XFS_DIR3_BLOCK_MAGIC;
1491 else
1492 wantmagic = XFS_DIR2_BLOCK_MAGIC;
1493 } else {
1494 endptr = (char *)d + mp->m_dir_geo->blksize;
1495 if (xfs_sb_version_hascrc(&mp->m_sb))
1496 wantmagic = XFS_DIR3_DATA_MAGIC;
1497 else
1498 wantmagic = XFS_DIR2_DATA_MAGIC;
1499 }
1500 db = xfs_dir2_da_to_db(mp->m_dir_geo, da_bno);
1501
1502 /* check for data block beyond expected end */
1503 if (freetab->naents <= db) {
1504 struct freetab_ent e;
1505
1506 *freetabp = freetab = realloc(freetab, FREETAB_SIZE(db + 1));
1507 if (!freetab) {
1508 do_error(_("realloc failed in %s (%zu bytes)\n"),
1509 __func__, FREETAB_SIZE(db + 1));
1510 }
1511 e.v = NULLDATAOFF;
1512 e.s = 0;
1513 for (i = freetab->naents; i < db; i++)
1514 freetab->ents[i] = e;
1515 freetab->naents = db + 1;
1516 }
1517
1518 /* check the data block */
1519 while (ptr < endptr) {
1520
1521 /* check for freespace */
1522 dup = (xfs_dir2_data_unused_t *)ptr;
1523 if (XFS_DIR2_DATA_FREE_TAG == be16_to_cpu(dup->freetag)) {
1524
1525 /* check for invalid freespace length */
1526 if (ptr + be16_to_cpu(dup->length) > endptr ||
1527 be16_to_cpu(dup->length) == 0 ||
1528 (be16_to_cpu(dup->length) &
1529 (XFS_DIR2_DATA_ALIGN - 1)))
1530 break;
1531
1532 /* check for invalid tag */
1533 if (be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) !=
1534 (char *)dup - (char *)d)
1535 break;
1536
1537 /* check for block with no data entries */
1538 if ((ptr == (char *)M_DIROPS(mp)->data_entry_p(d)) &&
1539 (ptr + be16_to_cpu(dup->length) >= endptr)) {
1540 junkit = 1;
1541 *num_illegal += 1;
1542 break;
1543 }
1544
1545 /* continue at the end of the freespace */
1546 ptr += be16_to_cpu(dup->length);
1547 if (ptr >= endptr)
1548 break;
1549 }
1550
1551 /* validate data entry size */
1552 dep = (xfs_dir2_data_entry_t *)ptr;
1553 if (ptr + M_DIROPS(mp)->data_entsize(dep->namelen) > endptr)
1554 break;
1555 if (be16_to_cpu(*M_DIROPS(mp)->data_entry_tag_p(dep)) !=
1556 (char *)dep - (char *)d)
1557 break;
1558 ptr += M_DIROPS(mp)->data_entsize(dep->namelen);
1559 }
1560
1561 /* did we find an empty or corrupt block? */
1562 if (ptr != endptr) {
1563 if (junkit) {
1564 do_warn(
1565 _("empty data block %u in directory inode %" PRIu64 ": "),
1566 da_bno, ip->i_ino);
1567 } else {
1568 do_warn(_
1569 ("corrupt block %u in directory inode %" PRIu64 ": "),
1570 da_bno, ip->i_ino);
1571 }
1572 if (!no_modify) {
1573 do_warn(_("junking block\n"));
1574 dir2_kill_block(mp, ip, da_bno, bp);
1575 } else {
1576 do_warn(_("would junk block\n"));
1577 libxfs_putbuf(bp);
1578 }
1579 freetab->ents[db].v = NULLDATAOFF;
1580 *bpp = NULL;
1581 return;
1582 }
1583
1584 /* update number of data blocks processed */
1585 if (freetab->nents < db + 1)
1586 freetab->nents = db + 1;
1587
1588 error = -libxfs_trans_alloc(mp, &M_RES(mp)->tr_remove, 0, 0, 0, &tp);
1589 if (error)
1590 res_failed(error);
1591 da.trans = tp;
1592 libxfs_trans_ijoin(tp, ip, 0);
1593 libxfs_trans_bjoin(tp, bp);
1594 libxfs_trans_bhold(tp, bp);
1595 libxfs_defer_init(&dfops, &firstblock);
1596 if (be32_to_cpu(d->magic) != wantmagic) {
1597 do_warn(
1598 _("bad directory block magic # %#x for directory inode %" PRIu64 " block %d: "),
1599 be32_to_cpu(d->magic), ip->i_ino, da_bno);
1600 if (!no_modify) {
1601 do_warn(_("fixing magic # to %#x\n"), wantmagic);
1602 d->magic = cpu_to_be32(wantmagic);
1603 needlog = 1;
1604 } else
1605 do_warn(_("would fix magic # to %#x\n"), wantmagic);
1606 }
1607 lastfree = 0;
1608 ptr = (char *)M_DIROPS(mp)->data_entry_p(d);
1609 /*
1610 * look at each entry. reference inode pointed to by each
1611 * entry in the incore inode tree.
1612 * if not a directory, set reached flag, increment link count
1613 * if a directory and reached, mark entry as to be deleted.
1614 * if a directory, check to see if recorded parent
1615 * matches current inode #,
1616 * if so, then set reached flag, increment link count
1617 * of current and child dir inodes, push the child
1618 * directory inode onto the directory stack.
1619 * if current inode != parent, then mark entry to be deleted.
1620 */
1621 while (ptr < endptr) {
1622 dup = (xfs_dir2_data_unused_t *)ptr;
1623 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
1624 if (lastfree) {
1625 do_warn(
1626 _("directory inode %" PRIu64 " block %u has consecutive free entries: "),
1627 ip->i_ino, da_bno);
1628 if (!no_modify) {
1629
1630 do_warn(_("joining together\n"));
1631 len = be16_to_cpu(dup->length);
1632 libxfs_dir2_data_use_free(&da, bp, dup,
1633 ptr - (char *)d, len, &needlog,
1634 &needscan);
1635 libxfs_dir2_data_make_free(&da, bp,
1636 ptr - (char *)d, len, &needlog,
1637 &needscan);
1638 } else
1639 do_warn(_("would join together\n"));
1640 }
1641 ptr += be16_to_cpu(dup->length);
1642 lastfree = 1;
1643 continue;
1644 }
1645 addr = xfs_dir2_db_off_to_dataptr(mp->m_dir_geo, db,
1646 ptr - (char *)d);
1647 dep = (xfs_dir2_data_entry_t *)ptr;
1648 ptr += M_DIROPS(mp)->data_entsize(dep->namelen);
1649 inum = be64_to_cpu(dep->inumber);
1650 lastfree = 0;
1651 /*
1652 * skip bogus entries (leading '/'). they'll be deleted
1653 * later. must still log it, else we leak references to
1654 * buffers.
1655 */
1656 if (dep->name[0] == '/') {
1657 nbad++;
1658 if (!no_modify)
1659 libxfs_dir2_data_log_entry(&da, bp, dep);
1660 continue;
1661 }
1662
1663 memmove(fname, dep->name, dep->namelen);
1664 fname[dep->namelen] = '\0';
1665 ASSERT(inum != NULLFSINO);
1666
1667 irec = find_inode_rec(mp, XFS_INO_TO_AGNO(mp, inum),
1668 XFS_INO_TO_AGINO(mp, inum));
1669 if (irec == NULL) {
1670 nbad++;
1671 if (entry_junked(
1672 _("entry \"%s\" in directory inode %" PRIu64 " points to non-existent inode %" PRIu64 ""),
1673 fname, ip->i_ino, inum)) {
1674 dep->name[0] = '/';
1675 libxfs_dir2_data_log_entry(&da, bp, dep);
1676 }
1677 continue;
1678 }
1679 ino_offset = XFS_INO_TO_AGINO(mp, inum) - irec->ino_startnum;
1680
1681 /*
1682 * if it's a free inode, blow out the entry.
1683 * by now, any inode that we think is free
1684 * really is free.
1685 */
1686 if (is_inode_free(irec, ino_offset)) {
1687 nbad++;
1688 if (entry_junked(
1689 _("entry \"%s\" in directory inode %" PRIu64 " points to free inode %" PRIu64),
1690 fname, ip->i_ino, inum)) {
1691 dep->name[0] = '/';
1692 libxfs_dir2_data_log_entry(&da, bp, dep);
1693 }
1694 continue;
1695 }
1696
1697 /*
1698 * check if this inode is lost+found dir in the root
1699 */
1700 if (inum == mp->m_sb.sb_rootino && strcmp(fname, ORPHANAGE) == 0) {
1701 /*
1702 * if it's not a directory, trash it
1703 */
1704 if (!inode_isadir(irec, ino_offset)) {
1705 nbad++;
1706 if (entry_junked(
1707 _("%s (ino %" PRIu64 ") in root (%" PRIu64 ") is not a directory"),
1708 ORPHANAGE, inum, ip->i_ino)) {
1709 dep->name[0] = '/';
1710 libxfs_dir2_data_log_entry(&da, bp, dep);
1711 }
1712 continue;
1713 }
1714 /*
1715 * if this is a dup, it will be picked up below,
1716 * otherwise, mark it as the orphanage for later.
1717 */
1718 if (!orphanage_ino)
1719 orphanage_ino = inum;
1720 }
1721
1722 /*
1723 * check for duplicate names in directory.
1724 */
1725 if (!dir_hash_add(mp, hashtab, addr, inum, dep->namelen,
1726 dep->name, M_DIROPS(mp)->data_get_ftype(dep))) {
1727 nbad++;
1728 if (entry_junked(
1729 _("entry \"%s\" (ino %" PRIu64 ") in dir %" PRIu64 " is a duplicate name"),
1730 fname, inum, ip->i_ino)) {
1731 dep->name[0] = '/';
1732 libxfs_dir2_data_log_entry(&da, bp, dep);
1733 }
1734 if (inum == orphanage_ino)
1735 orphanage_ino = 0;
1736 continue;
1737 }
1738
1739 /*
1740 * if just scanning to rebuild a directory due to a ".."
1741 * update, just continue
1742 */
1743 if (dotdot_update)
1744 continue;
1745
1746 /*
1747 * skip the '..' entry since it's checked when the
1748 * directory is reached by something else. if it never
1749 * gets reached, it'll be moved to the orphanage and we'll
1750 * take care of it then. If it doesn't exist at all, the
1751 * directory needs to be rebuilt first before being added
1752 * to the orphanage.
1753 */
1754 if (dep->namelen == 2 && dep->name[0] == '.' &&
1755 dep->name[1] == '.') {
1756 if (da_bno != 0) {
1757 /* ".." should be in the first block */
1758 nbad++;
1759 if (entry_junked(
1760 _("entry \"%s\" (ino %" PRIu64 ") in dir %" PRIu64 " is not in the the first block"), fname,
1761 inum, ip->i_ino)) {
1762 dep->name[0] = '/';
1763 libxfs_dir2_data_log_entry(&da, bp, dep);
1764 }
1765 }
1766 continue;
1767 }
1768 ASSERT(no_modify || !verify_inum(mp, inum));
1769 /*
1770 * special case the . entry. we know there's only one
1771 * '.' and only '.' points to itself because bogus entries
1772 * got trashed in phase 3 if there were > 1.
1773 * bump up link count for '.' but don't set reached
1774 * until we're actually reached by another directory
1775 * '..' is already accounted for or will be taken care
1776 * of when directory is moved to orphanage.
1777 */
1778 if (ip->i_ino == inum) {
1779 ASSERT(dep->name[0] == '.' && dep->namelen == 1);
1780 add_inode_ref(current_irec, current_ino_offset);
1781 if (da_bno != 0 ||
1782 dep != M_DIROPS(mp)->data_entry_p(d)) {
1783 /* "." should be the first entry */
1784 nbad++;
1785 if (entry_junked(
1786 _("entry \"%s\" in dir %" PRIu64 " is not the first entry"),
1787 fname, inum, ip->i_ino)) {
1788 dep->name[0] = '/';
1789 libxfs_dir2_data_log_entry(&da, bp, dep);
1790 }
1791 }
1792 *need_dot = 0;
1793 continue;
1794 }
1795 /*
1796 * skip entries with bogus inumbers if we're in no modify mode
1797 */
1798 if (no_modify && verify_inum(mp, inum))
1799 continue;
1800
1801 /* validate ftype field if supported */
1802 if (xfs_sb_version_hasftype(&mp->m_sb)) {
1803 uint8_t dir_ftype;
1804 uint8_t ino_ftype;
1805
1806 dir_ftype = M_DIROPS(mp)->data_get_ftype(dep);
1807 ino_ftype = get_inode_ftype(irec, ino_offset);
1808
1809 if (dir_ftype != ino_ftype) {
1810 if (no_modify) {
1811 do_warn(
1812 _("would fix ftype mismatch (%d/%d) in directory/child inode %" PRIu64 "/%" PRIu64 "\n"),
1813 dir_ftype, ino_ftype,
1814 ip->i_ino, inum);
1815 } else {
1816 do_warn(
1817 _("fixing ftype mismatch (%d/%d) in directory/child inode %" PRIu64 "/%" PRIu64 "\n"),
1818 dir_ftype, ino_ftype,
1819 ip->i_ino, inum);
1820 M_DIROPS(mp)->data_put_ftype(dep,
1821 ino_ftype);
1822 libxfs_dir2_data_log_entry(&da, bp, dep);
1823 dir_hash_update_ftype(hashtab, addr,
1824 ino_ftype);
1825 }
1826 }
1827 }
1828
1829 /*
1830 * check easy case first, regular inode, just bump
1831 * the link count and continue
1832 */
1833 if (!inode_isadir(irec, ino_offset)) {
1834 add_inode_reached(irec, ino_offset);
1835 continue;
1836 }
1837 parent = get_inode_parent(irec, ino_offset);
1838 ASSERT(parent != 0);
1839 junkit = 0;
1840 /*
1841 * bump up the link counts in parent and child
1842 * directory but if the link doesn't agree with
1843 * the .. in the child, blow out the entry.
1844 * if the directory has already been reached,
1845 * blow away the entry also.
1846 */
1847 if (is_inode_reached(irec, ino_offset)) {
1848 junkit = 1;
1849 do_warn(
1850 _("entry \"%s\" in dir %" PRIu64" points to an already connected directory inode %" PRIu64 "\n"),
1851 fname, ip->i_ino, inum);
1852 } else if (parent == ip->i_ino) {
1853 add_inode_reached(irec, ino_offset);
1854 add_inode_ref(current_irec, current_ino_offset);
1855 } else if (parent == NULLFSINO) {
1856 /* ".." was missing, but this entry refers to it,
1857 so, set it as the parent and mark for rebuild */
1858 do_warn(
1859 _("entry \"%s\" in dir ino %" PRIu64 " doesn't have a .. entry, will set it in ino %" PRIu64 ".\n"),
1860 fname, ip->i_ino, inum);
1861 set_inode_parent(irec, ino_offset, ip->i_ino);
1862 add_inode_reached(irec, ino_offset);
1863 add_inode_ref(current_irec, current_ino_offset);
1864 add_dotdot_update(XFS_INO_TO_AGNO(mp, inum), irec,
1865 ino_offset);
1866 } else {
1867 junkit = 1;
1868 do_warn(
1869 _("entry \"%s\" in dir inode %" PRIu64 " inconsistent with .. value (%" PRIu64 ") in ino %" PRIu64 "\n"),
1870 fname, ip->i_ino, parent, inum);
1871 }
1872 if (junkit) {
1873 if (inum == orphanage_ino)
1874 orphanage_ino = 0;
1875 nbad++;
1876 if (!no_modify) {
1877 dep->name[0] = '/';
1878 libxfs_dir2_data_log_entry(&da, bp, dep);
1879 if (verbose)
1880 do_warn(
1881 _("\twill clear entry \"%s\"\n"),
1882 fname);
1883 } else {
1884 do_warn(_("\twould clear entry \"%s\"\n"),
1885 fname);
1886 }
1887 }
1888 }
1889 *num_illegal += nbad;
1890 if (needscan)
1891 libxfs_dir2_data_freescan_int(mp->m_dir_geo, M_DIROPS(mp),
1892 d, &i);
1893 if (needlog)
1894 libxfs_dir2_data_log_header(&da, bp);
1895 libxfs_defer_ijoin(&dfops, ip);
1896 libxfs_defer_finish(&tp, &dfops);
1897 libxfs_trans_commit(tp);
1898
1899 /* record the largest free space in the freetab for later checking */
1900 bf = M_DIROPS(mp)->data_bestfree_p(d);
1901 freetab->ents[db].v = be16_to_cpu(bf[0].length);
1902 freetab->ents[db].s = 0;
1903 }
1904
1905 /* check v5 metadata */
1906 static int
1907 __check_dir3_header(
1908 struct xfs_mount *mp,
1909 struct xfs_buf *bp,
1910 xfs_ino_t ino,
1911 __be64 owner,
1912 __be64 blkno,
1913 uuid_t *uuid)
1914 {
1915
1916 /* verify owner */
1917 if (be64_to_cpu(owner) != ino) {
1918 do_warn(
1919 _("expected owner inode %" PRIu64 ", got %llu, directory block %" PRIu64 "\n"),
1920 ino, (unsigned long long)be64_to_cpu(owner), bp->b_bn);
1921 return 1;
1922 }
1923 /* verify block number */
1924 if (be64_to_cpu(blkno) != bp->b_bn) {
1925 do_warn(
1926 _("expected block %" PRIu64 ", got %llu, directory inode %" PRIu64 "\n"),
1927 bp->b_bn, (unsigned long long)be64_to_cpu(blkno), ino);
1928 return 1;
1929 }
1930 /* verify uuid */
1931 if (platform_uuid_compare(uuid, &mp->m_sb.sb_meta_uuid) != 0) {
1932 do_warn(
1933 _("wrong FS UUID, directory inode %" PRIu64 " block %" PRIu64 "\n"),
1934 ino, bp->b_bn);
1935 return 1;
1936 }
1937
1938 return 0;
1939 }
1940
1941 static int
1942 check_da3_header(
1943 struct xfs_mount *mp,
1944 struct xfs_buf *bp,
1945 xfs_ino_t ino)
1946 {
1947 struct xfs_da3_blkinfo *info = bp->b_addr;
1948
1949 return __check_dir3_header(mp, bp, ino, info->owner, info->blkno,
1950 &info->uuid);
1951 }
1952
1953 static int
1954 check_dir3_header(
1955 struct xfs_mount *mp,
1956 struct xfs_buf *bp,
1957 xfs_ino_t ino)
1958 {
1959 struct xfs_dir3_blk_hdr *info = bp->b_addr;
1960
1961 return __check_dir3_header(mp, bp, ino, info->owner, info->blkno,
1962 &info->uuid);
1963 }
1964
1965 /*
1966 * Check contents of leaf-form block.
1967 */
1968 static int
1969 longform_dir2_check_leaf(
1970 xfs_mount_t *mp,
1971 xfs_inode_t *ip,
1972 dir_hash_tab_t *hashtab,
1973 freetab_t *freetab)
1974 {
1975 int badtail;
1976 __be16 *bestsp;
1977 struct xfs_buf *bp;
1978 xfs_dablk_t da_bno;
1979 int i;
1980 xfs_dir2_leaf_t *leaf;
1981 xfs_dir2_leaf_tail_t *ltp;
1982 int seeval;
1983 struct xfs_dir2_leaf_entry *ents;
1984 struct xfs_dir3_icleaf_hdr leafhdr;
1985 int error;
1986 int fixit = 0;
1987
1988 da_bno = mp->m_dir_geo->leafblk;
1989 error = dir_read_buf(ip, da_bno, -1, &bp, &xfs_dir3_leaf1_buf_ops,
1990 &fixit);
1991 if (error == EFSBADCRC || error == EFSCORRUPTED || fixit) {
1992 do_warn(
1993 _("leaf block %u for directory inode %" PRIu64 " bad CRC\n"),
1994 da_bno, ip->i_ino);
1995 return 1;
1996 } else if (error) {
1997 do_error(
1998 _("can't read block %u for directory inode %" PRIu64 ", error %d\n"),
1999 da_bno, ip->i_ino, error);
2000 /* NOTREACHED */
2001 }
2002
2003 leaf = bp->b_addr;
2004 M_DIROPS(mp)->leaf_hdr_from_disk(&leafhdr, leaf);
2005 ents = M_DIROPS(mp)->leaf_ents_p(leaf);
2006 ltp = xfs_dir2_leaf_tail_p(mp->m_dir_geo, leaf);
2007 bestsp = xfs_dir2_leaf_bests_p(ltp);
2008 if (!(leafhdr.magic == XFS_DIR2_LEAF1_MAGIC ||
2009 leafhdr.magic == XFS_DIR3_LEAF1_MAGIC) ||
2010 leafhdr.forw || leafhdr.back ||
2011 leafhdr.count < leafhdr.stale ||
2012 leafhdr.count >
2013 M_DIROPS(mp)->leaf_max_ents(mp->m_dir_geo) ||
2014 (char *)&ents[leafhdr.count] > (char *)bestsp) {
2015 do_warn(
2016 _("leaf block %u for directory inode %" PRIu64 " bad header\n"),
2017 da_bno, ip->i_ino);
2018 libxfs_putbuf(bp);
2019 return 1;
2020 }
2021
2022 if (leafhdr.magic == XFS_DIR3_LEAF1_MAGIC) {
2023 error = check_da3_header(mp, bp, ip->i_ino);
2024 if (error) {
2025 libxfs_putbuf(bp);
2026 return error;
2027 }
2028 }
2029
2030 seeval = dir_hash_see_all(hashtab, ents, leafhdr.count, leafhdr.stale);
2031 if (dir_hash_check(hashtab, ip, seeval)) {
2032 libxfs_putbuf(bp);
2033 return 1;
2034 }
2035 badtail = freetab->nents != be32_to_cpu(ltp->bestcount);
2036 for (i = 0; !badtail && i < be32_to_cpu(ltp->bestcount); i++) {
2037 freetab->ents[i].s = 1;
2038 badtail = freetab->ents[i].v != be16_to_cpu(bestsp[i]);
2039 }
2040 if (badtail) {
2041 do_warn(
2042 _("leaf block %u for directory inode %" PRIu64 " bad tail\n"),
2043 da_bno, ip->i_ino);
2044 libxfs_putbuf(bp);
2045 return 1;
2046 }
2047 libxfs_putbuf(bp);
2048 return fixit;
2049 }
2050
2051 /*
2052 * Check contents of the node blocks (leaves)
2053 * Looks for matching hash values for the data entries.
2054 */
2055 static int
2056 longform_dir2_check_node(
2057 xfs_mount_t *mp,
2058 xfs_inode_t *ip,
2059 dir_hash_tab_t *hashtab,
2060 freetab_t *freetab)
2061 {
2062 struct xfs_buf *bp;
2063 xfs_dablk_t da_bno;
2064 xfs_dir2_db_t fdb;
2065 xfs_dir2_free_t *free;
2066 int i;
2067 xfs_dir2_leaf_t *leaf;
2068 xfs_fileoff_t next_da_bno;
2069 int seeval = 0;
2070 int used;
2071 struct xfs_dir2_leaf_entry *ents;
2072 struct xfs_dir3_icleaf_hdr leafhdr;
2073 struct xfs_dir3_icfree_hdr freehdr;
2074 __be16 *bests;
2075 int error;
2076 int fixit = 0;
2077
2078 for (da_bno = mp->m_dir_geo->leafblk, next_da_bno = 0;
2079 next_da_bno != NULLFILEOFF && da_bno < mp->m_dir_geo->freeblk;
2080 da_bno = (xfs_dablk_t)next_da_bno) {
2081 next_da_bno = da_bno + mp->m_dir_geo->fsbcount - 1;
2082 if (bmap_next_offset(NULL, ip, &next_da_bno, XFS_DATA_FORK))
2083 break;
2084
2085 /*
2086 * we need to use the da3 node verifier here as it handles the
2087 * fact that reading the leaf hash tree blocks can return either
2088 * leaf or node blocks and calls the correct verifier. If we get
2089 * a node block, then we'll skip it below based on a magic
2090 * number check.
2091 */
2092 error = dir_read_buf(ip, da_bno, -1, &bp,
2093 &xfs_da3_node_buf_ops, &fixit);
2094 if (error) {
2095 do_warn(
2096 _("can't read leaf block %u for directory inode %" PRIu64 ", error %d\n"),
2097 da_bno, ip->i_ino, error);
2098 return 1;
2099 }
2100 leaf = bp->b_addr;
2101 M_DIROPS(mp)->leaf_hdr_from_disk(&leafhdr, leaf);
2102 ents = M_DIROPS(mp)->leaf_ents_p(leaf);
2103 if (!(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
2104 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC ||
2105 leafhdr.magic == XFS_DA_NODE_MAGIC ||
2106 leafhdr.magic == XFS_DA3_NODE_MAGIC)) {
2107 do_warn(
2108 _("unknown magic number %#x for block %u in directory inode %" PRIu64 "\n"),
2109 leafhdr.magic, da_bno, ip->i_ino);
2110 libxfs_putbuf(bp);
2111 return 1;
2112 }
2113
2114 /* check v5 metadata */
2115 if (leafhdr.magic == XFS_DIR3_LEAFN_MAGIC ||
2116 leafhdr.magic == XFS_DA3_NODE_MAGIC) {
2117 error = check_da3_header(mp, bp, ip->i_ino);
2118 if (error) {
2119 libxfs_putbuf(bp);
2120 return error;
2121 }
2122 }
2123
2124 /* ignore nodes */
2125 if (leafhdr.magic == XFS_DA_NODE_MAGIC ||
2126 leafhdr.magic == XFS_DA3_NODE_MAGIC) {
2127 libxfs_putbuf(bp);
2128 continue;
2129 }
2130
2131 /*
2132 * If there's a validator error, we need to ensure that we got
2133 * the right ops on the buffer for when we write it back out.
2134 */
2135 bp->b_ops = &xfs_dir3_leafn_buf_ops;
2136 if (leafhdr.count > M_DIROPS(mp)->leaf_max_ents(mp->m_dir_geo) ||
2137 leafhdr.count < leafhdr.stale) {
2138 do_warn(
2139 _("leaf block %u for directory inode %" PRIu64 " bad header\n"),
2140 da_bno, ip->i_ino);
2141 libxfs_putbuf(bp);
2142 return 1;
2143 }
2144 seeval = dir_hash_see_all(hashtab, ents,
2145 leafhdr.count, leafhdr.stale);
2146 libxfs_putbuf(bp);
2147 if (seeval != DIR_HASH_CK_OK)
2148 return 1;
2149 }
2150 if (dir_hash_check(hashtab, ip, seeval))
2151 return 1;
2152
2153 for (da_bno = mp->m_dir_geo->freeblk, next_da_bno = 0;
2154 next_da_bno != NULLFILEOFF;
2155 da_bno = (xfs_dablk_t)next_da_bno) {
2156 next_da_bno = da_bno + mp->m_dir_geo->fsbcount - 1;
2157 if (bmap_next_offset(NULL, ip, &next_da_bno, XFS_DATA_FORK))
2158 break;
2159
2160 error = dir_read_buf(ip, da_bno, -1, &bp,
2161 &xfs_dir3_free_buf_ops, &fixit);
2162 if (error) {
2163 do_warn(
2164 _("can't read freespace block %u for directory inode %" PRIu64 ", error %d\n"),
2165 da_bno, ip->i_ino, error);
2166 return 1;
2167 }
2168 free = bp->b_addr;
2169 M_DIROPS(mp)->free_hdr_from_disk(&freehdr, free);
2170 bests = M_DIROPS(mp)->free_bests_p(free);
2171 fdb = xfs_dir2_da_to_db(mp->m_dir_geo, da_bno);
2172 if (!(freehdr.magic == XFS_DIR2_FREE_MAGIC ||
2173 freehdr.magic == XFS_DIR3_FREE_MAGIC) ||
2174 freehdr.firstdb !=
2175 (fdb - xfs_dir2_byte_to_db(mp->m_dir_geo, XFS_DIR2_FREE_OFFSET)) *
2176 M_DIROPS(mp)->free_max_bests(mp->m_dir_geo) ||
2177 freehdr.nvalid < freehdr.nused) {
2178 do_warn(
2179 _("free block %u for directory inode %" PRIu64 " bad header\n"),
2180 da_bno, ip->i_ino);
2181 libxfs_putbuf(bp);
2182 return 1;
2183 }
2184
2185 if (freehdr.magic == XFS_DIR3_FREE_MAGIC) {
2186 error = check_dir3_header(mp, bp, ip->i_ino);
2187 if (error) {
2188 libxfs_putbuf(bp);
2189 return error;
2190 }
2191 }
2192 for (i = used = 0; i < freehdr.nvalid; i++) {
2193 if (i + freehdr.firstdb >= freetab->nents ||
2194 freetab->ents[i + freehdr.firstdb].v !=
2195 be16_to_cpu(bests[i])) {
2196 do_warn(
2197 _("free block %u entry %i for directory ino %" PRIu64 " bad\n"),
2198 da_bno, i, ip->i_ino);
2199 libxfs_putbuf(bp);
2200 return 1;
2201 }
2202 used += be16_to_cpu(bests[i]) != NULLDATAOFF;
2203 freetab->ents[i + freehdr.firstdb].s = 1;
2204 }
2205 if (used != freehdr.nused) {
2206 do_warn(
2207 _("free block %u for directory inode %" PRIu64 " bad nused\n"),
2208 da_bno, ip->i_ino);
2209 libxfs_putbuf(bp);
2210 return 1;
2211 }
2212 libxfs_putbuf(bp);
2213 }
2214 for (i = 0; i < freetab->nents; i++) {
2215 if ((freetab->ents[i].s == 0) &&
2216 (freetab->ents[i].v != NULLDATAOFF)) {
2217 do_warn(
2218 _("missing freetab entry %u for directory inode %" PRIu64 "\n"),
2219 i, ip->i_ino);
2220 return 1;
2221 }
2222 }
2223 return fixit;
2224 }
2225
2226 /*
2227 * If a directory is corrupt, we need to read in as many entries as possible,
2228 * destroy the entry and create a new one with recovered name/inode pairs.
2229 * (ie. get libxfs to do all the grunt work)
2230 */
2231 static void
2232 longform_dir2_entry_check(xfs_mount_t *mp,
2233 xfs_ino_t ino,
2234 xfs_inode_t *ip,
2235 int *num_illegal,
2236 int *need_dot,
2237 ino_tree_node_t *irec,
2238 int ino_offset,
2239 dir_hash_tab_t *hashtab)
2240 {
2241 struct xfs_buf **bplist;
2242 xfs_dablk_t da_bno;
2243 freetab_t *freetab;
2244 int num_bps;
2245 int i;
2246 int isblock;
2247 int isleaf;
2248 xfs_fileoff_t next_da_bno;
2249 int seeval;
2250 int fixit = 0;
2251 xfs_dir2_db_t db;
2252 struct xfs_da_args args;
2253
2254 *need_dot = 1;
2255 freetab = malloc(FREETAB_SIZE(ip->i_d.di_size / mp->m_dir_geo->blksize));
2256 if (!freetab) {
2257 do_error(_("malloc failed in %s (%" PRId64 " bytes)\n"),
2258 __func__,
2259 FREETAB_SIZE(ip->i_d.di_size / mp->m_dir_geo->blksize));
2260 exit(1);
2261 }
2262 freetab->naents = ip->i_d.di_size / mp->m_dir_geo->blksize;
2263 freetab->nents = 0;
2264 for (i = 0; i < freetab->naents; i++) {
2265 freetab->ents[i].v = NULLDATAOFF;
2266 freetab->ents[i].s = 0;
2267 }
2268 num_bps = freetab->naents;
2269 bplist = calloc(num_bps, sizeof(struct xfs_buf*));
2270 if (!bplist)
2271 do_error(_("calloc failed in %s (%zu bytes)\n"),
2272 __func__, num_bps * sizeof(struct xfs_buf*));
2273
2274 /* is this a block, leaf, or node directory? */
2275 args.dp = ip;
2276 args.geo = mp->m_dir_geo;
2277 libxfs_dir2_isblock(&args, &isblock);
2278 libxfs_dir2_isleaf(&args, &isleaf);
2279
2280 /* check directory "data" blocks (ie. name/inode pairs) */
2281 for (da_bno = 0, next_da_bno = 0;
2282 next_da_bno != NULLFILEOFF && da_bno < mp->m_dir_geo->leafblk;
2283 da_bno = (xfs_dablk_t)next_da_bno) {
2284 const struct xfs_buf_ops *ops;
2285 int error;
2286 struct xfs_dir2_data_hdr *d;
2287
2288 next_da_bno = da_bno + mp->m_dir_geo->fsbcount - 1;
2289 if (bmap_next_offset(NULL, ip, &next_da_bno, XFS_DATA_FORK)) {
2290 /*
2291 * if this is the first block, there isn't anything we
2292 * can recover so we just trash it.
2293 */
2294 if (da_bno == 0) {
2295 fixit++;
2296 goto out_fix;
2297 }
2298 break;
2299 }
2300
2301 db = xfs_dir2_da_to_db(mp->m_dir_geo, da_bno);
2302 if (db >= num_bps) {
2303 /* more data blocks than expected */
2304 num_bps = db + 1;
2305 bplist = realloc(bplist, num_bps * sizeof(struct xfs_buf*));
2306 if (!bplist)
2307 do_error(_("realloc failed in %s (%zu bytes)\n"),
2308 __func__,
2309 num_bps * sizeof(struct xfs_buf*));
2310 }
2311
2312 if (isblock)
2313 ops = &xfs_dir3_block_buf_ops;
2314 else
2315 ops = &xfs_dir3_data_buf_ops;
2316
2317 error = dir_read_buf(ip, da_bno, -1, &bplist[db], ops, &fixit);
2318 if (error) {
2319 do_warn(
2320 _("can't read data block %u for directory inode %" PRIu64 " error %d\n"),
2321 da_bno, ino, error);
2322 *num_illegal += 1;
2323
2324 /*
2325 * we try to read all "data" blocks, but if we are in
2326 * block form and we fail, there isn't anything else to
2327 * read, and nothing we can do but trash it.
2328 */
2329 if (isblock) {
2330 fixit++;
2331 goto out_fix;
2332 }
2333 continue;
2334 }
2335
2336 /* check v5 metadata */
2337 d = bplist[db]->b_addr;
2338 if (be32_to_cpu(d->magic) == XFS_DIR3_BLOCK_MAGIC ||
2339 be32_to_cpu(d->magic) == XFS_DIR3_DATA_MAGIC) {
2340 struct xfs_buf *bp = bplist[db];
2341
2342 error = check_dir3_header(mp, bp, ino);
2343 if (error) {
2344 fixit++;
2345 continue;
2346 }
2347 }
2348
2349 longform_dir2_entry_check_data(mp, ip, num_illegal, need_dot,
2350 irec, ino_offset, &bplist[db], hashtab,
2351 &freetab, da_bno, isblock);
2352 }
2353 fixit |= (*num_illegal != 0) || dir2_is_badino(ino) || *need_dot;
2354
2355 if (!dotdot_update) {
2356 /* check btree and freespace */
2357 if (isblock) {
2358 struct xfs_dir2_data_hdr *block;
2359 xfs_dir2_block_tail_t *btp;
2360 xfs_dir2_leaf_entry_t *blp;
2361
2362 block = bplist[0]->b_addr;
2363 btp = xfs_dir2_block_tail_p(mp->m_dir_geo, block);
2364 blp = xfs_dir2_block_leaf_p(btp);
2365 seeval = dir_hash_see_all(hashtab, blp,
2366 be32_to_cpu(btp->count),
2367 be32_to_cpu(btp->stale));
2368 if (dir_hash_check(hashtab, ip, seeval))
2369 fixit |= 1;
2370 } else if (isleaf) {
2371 fixit |= longform_dir2_check_leaf(mp, ip, hashtab,
2372 freetab);
2373 } else {
2374 fixit |= longform_dir2_check_node(mp, ip, hashtab,
2375 freetab);
2376 }
2377 }
2378 out_fix:
2379 if (!no_modify && (fixit || dotdot_update)) {
2380 dir_hash_dup_names(hashtab);
2381 for (i = 0; i < num_bps; i++)
2382 if (bplist[i])
2383 libxfs_putbuf(bplist[i]);
2384 longform_dir2_rebuild(mp, ino, ip, irec, ino_offset, hashtab);
2385 *num_illegal = 0;
2386 *need_dot = 0;
2387 } else {
2388 for (i = 0; i < num_bps; i++)
2389 if (bplist[i])
2390 libxfs_putbuf(bplist[i]);
2391 }
2392
2393 free(bplist);
2394 free(freetab);
2395 }
2396
2397 /*
2398 * shortform directory v2 processing routines -- entry verification and
2399 * bad entry deletion (pruning).
2400 */
2401 static struct xfs_dir2_sf_entry *
2402 shortform_dir2_junk(
2403 struct xfs_mount *mp,
2404 struct xfs_dir2_sf_hdr *sfp,
2405 struct xfs_dir2_sf_entry *sfep,
2406 xfs_ino_t lino,
2407 int *max_size,
2408 int *index,
2409 int *bytes_deleted,
2410 int *ino_dirty)
2411 {
2412 struct xfs_dir2_sf_entry *next_sfep;
2413 int next_len;
2414 int next_elen;
2415
2416 if (lino == orphanage_ino)
2417 orphanage_ino = 0;
2418
2419 next_elen = M_DIROPS(mp)->sf_entsize(sfp, sfep->namelen);
2420 next_sfep = M_DIROPS(mp)->sf_nextentry(sfp, sfep);
2421
2422 /*
2423 * if we are just checking, simply return the pointer to the next entry
2424 * here so that the checking loop can continue.
2425 */
2426 if (no_modify) {
2427 do_warn(_("would junk entry\n"));
2428 return next_sfep;
2429 }
2430
2431 /*
2432 * now move all the remaining entries down over the junked entry and
2433 * clear the newly unused bytes at the tail of the directory region.
2434 */
2435 next_len = *max_size - ((intptr_t)next_sfep - (intptr_t)sfp);
2436 *max_size -= next_elen;
2437 *bytes_deleted += next_elen;
2438
2439 memmove(sfep, next_sfep, next_len);
2440 memset((void *)((intptr_t)sfep + next_len), 0, next_elen);
2441 sfp->count -= 1;
2442 *ino_dirty = 1;
2443
2444 /*
2445 * WARNING: drop the index i by one so it matches the decremented count
2446 * for accurate comparisons in the loop test
2447 */
2448 (*index)--;
2449
2450 if (verbose)
2451 do_warn(_("junking entry\n"));
2452 else
2453 do_warn("\n");
2454 return sfep;
2455 }
2456
2457 static void
2458 shortform_dir2_entry_check(xfs_mount_t *mp,
2459 xfs_ino_t ino,
2460 xfs_inode_t *ip,
2461 int *ino_dirty,
2462 ino_tree_node_t *current_irec,
2463 int current_ino_offset,
2464 dir_hash_tab_t *hashtab)
2465 {
2466 xfs_ino_t lino;
2467 xfs_ino_t parent;
2468 struct xfs_dir2_sf_hdr *sfp;
2469 struct xfs_dir2_sf_entry *sfep;
2470 struct xfs_dir2_sf_entry *next_sfep;
2471 struct xfs_ifork *ifp;
2472 struct ino_tree_node *irec;
2473 int max_size;
2474 int ino_offset;
2475 int i;
2476 int bad_sfnamelen;
2477 int namelen;
2478 int bytes_deleted;
2479 char fname[MAXNAMELEN + 1];
2480 int i8;
2481
2482 ifp = &ip->i_df;
2483 sfp = (struct xfs_dir2_sf_hdr *) ifp->if_u1.if_data;
2484 *ino_dirty = 0;
2485 bytes_deleted = 0;
2486
2487 max_size = ifp->if_bytes;
2488 ASSERT(ip->i_d.di_size <= ifp->if_bytes);
2489
2490 /*
2491 * if just rebuild a directory due to a "..", update and return
2492 */
2493 if (dotdot_update) {
2494 parent = get_inode_parent(current_irec, current_ino_offset);
2495 if (no_modify) {
2496 do_warn(
2497 _("would set .. in sf dir inode %" PRIu64 " to %" PRIu64 "\n"),
2498 ino, parent);
2499 } else {
2500 do_warn(
2501 _("setting .. in sf dir inode %" PRIu64 " to %" PRIu64 "\n"),
2502 ino, parent);
2503 M_DIROPS(mp)->sf_put_parent_ino(sfp, parent);
2504 *ino_dirty = 1;
2505 }
2506 return;
2507 }
2508
2509 /*
2510 * no '.' entry in shortform dirs, just bump up ref count by 1
2511 * '..' was already (or will be) accounted for and checked when
2512 * the directory is reached or will be taken care of when the
2513 * directory is moved to orphanage.
2514 */
2515 add_inode_ref(current_irec, current_ino_offset);
2516
2517 /*
2518 * Initialise i8 counter -- the parent inode number counts as well.
2519 */
2520 i8 = M_DIROPS(mp)->sf_get_parent_ino(sfp) > XFS_DIR2_MAX_SHORT_INUM;
2521
2522 /*
2523 * now run through entries, stop at first bad entry, don't need
2524 * to skip over '..' since that's encoded in its own field and
2525 * no need to worry about '.' since it doesn't exist.
2526 */
2527 sfep = next_sfep = xfs_dir2_sf_firstentry(sfp);
2528
2529 for (i = 0; i < sfp->count && max_size >
2530 (intptr_t)next_sfep - (intptr_t)sfp;
2531 sfep = next_sfep, i++) {
2532 bad_sfnamelen = 0;
2533
2534 lino = M_DIROPS(mp)->sf_get_ino(sfp, sfep);
2535
2536 namelen = sfep->namelen;
2537
2538 ASSERT(no_modify || namelen > 0);
2539
2540 if (no_modify && namelen == 0) {
2541 /*
2542 * if we're really lucky, this is
2543 * the last entry in which case we
2544 * can use the dir size to set the
2545 * namelen value. otherwise, forget
2546 * it because we're not going to be
2547 * able to find the next entry.
2548 */
2549 bad_sfnamelen = 1;
2550
2551 if (i == sfp->count - 1) {
2552 namelen = ip->i_d.di_size -
2553 ((intptr_t) &sfep->name[0] -
2554 (intptr_t) sfp);
2555 } else {
2556 /*
2557 * don't process the rest of the directory,
2558 * break out of processing loop
2559 */
2560 break;
2561 }
2562 } else if (no_modify && (intptr_t) sfep - (intptr_t) sfp +
2563 + M_DIROPS(mp)->sf_entsize(sfp, sfep->namelen)
2564 > ip->i_d.di_size) {
2565 bad_sfnamelen = 1;
2566
2567 if (i == sfp->count - 1) {
2568 namelen = ip->i_d.di_size -
2569 ((intptr_t) &sfep->name[0] -
2570 (intptr_t) sfp);
2571 } else {
2572 /*
2573 * don't process the rest of the directory,
2574 * break out of processing loop
2575 */
2576 break;
2577 }
2578 }
2579
2580 memmove(fname, sfep->name, sfep->namelen);
2581 fname[sfep->namelen] = '\0';
2582
2583 ASSERT(no_modify || (lino != NULLFSINO && lino != 0));
2584 ASSERT(no_modify || !verify_inum(mp, lino));
2585
2586 /*
2587 * Also skip entries with bogus inode numbers if we're
2588 * in no modify mode.
2589 */
2590
2591 if (no_modify && verify_inum(mp, lino)) {
2592 next_sfep = M_DIROPS(mp)->sf_nextentry(sfp, sfep);
2593 continue;
2594 }
2595
2596 irec = find_inode_rec(mp, XFS_INO_TO_AGNO(mp, lino),
2597 XFS_INO_TO_AGINO(mp, lino));
2598
2599 if (irec == NULL) {
2600 do_warn(
2601 _("entry \"%s\" in shortform directory %" PRIu64 " references non-existent inode %" PRIu64 "\n"),
2602 fname, ino, lino);
2603 next_sfep = shortform_dir2_junk(mp, sfp, sfep, lino,
2604 &max_size, &i, &bytes_deleted,
2605 ino_dirty);
2606 continue;
2607 }
2608
2609 ino_offset = XFS_INO_TO_AGINO(mp, lino) - irec->ino_startnum;
2610
2611 /*
2612 * if it's a free inode, blow out the entry.
2613 * by now, any inode that we think is free
2614 * really is free.
2615 */
2616 if (is_inode_free(irec, ino_offset)) {
2617 do_warn(
2618 _("entry \"%s\" in shortform directory inode %" PRIu64 " points to free inode %" PRIu64 "\n"),
2619 fname, ino, lino);
2620 next_sfep = shortform_dir2_junk(mp, sfp, sfep, lino,
2621 &max_size, &i, &bytes_deleted,
2622 ino_dirty);
2623 continue;
2624 }
2625 /*
2626 * check if this inode is lost+found dir in the root
2627 */
2628 if (ino == mp->m_sb.sb_rootino && strcmp(fname, ORPHANAGE) == 0) {
2629 /*
2630 * if it's not a directory, trash it
2631 */
2632 if (!inode_isadir(irec, ino_offset)) {
2633 do_warn(
2634 _("%s (ino %" PRIu64 ") in root (%" PRIu64 ") is not a directory"),
2635 ORPHANAGE, lino, ino);
2636 next_sfep = shortform_dir2_junk(mp, sfp, sfep,
2637 lino, &max_size, &i,
2638 &bytes_deleted, ino_dirty);
2639 continue;
2640 }
2641 /*
2642 * if this is a dup, it will be picked up below,
2643 * otherwise, mark it as the orphanage for later.
2644 */
2645 if (!orphanage_ino)
2646 orphanage_ino = lino;
2647 }
2648 /*
2649 * check for duplicate names in directory.
2650 */
2651 if (!dir_hash_add(mp, hashtab, (xfs_dir2_dataptr_t)
2652 (sfep - xfs_dir2_sf_firstentry(sfp)),
2653 lino, sfep->namelen, sfep->name,
2654 M_DIROPS(mp)->sf_get_ftype(sfep))) {
2655 do_warn(
2656 _("entry \"%s\" (ino %" PRIu64 ") in dir %" PRIu64 " is a duplicate name"),
2657 fname, lino, ino);
2658 next_sfep = shortform_dir2_junk(mp, sfp, sfep, lino,
2659 &max_size, &i, &bytes_deleted,
2660 ino_dirty);
2661 continue;
2662 }
2663
2664 if (!inode_isadir(irec, ino_offset)) {
2665 /*
2666 * check easy case first, regular inode, just bump
2667 * the link count
2668 */
2669 add_inode_reached(irec, ino_offset);
2670 } else {
2671 parent = get_inode_parent(irec, ino_offset);
2672
2673 /*
2674 * bump up the link counts in parent and child.
2675 * directory but if the link doesn't agree with
2676 * the .. in the child, blow out the entry
2677 */
2678 if (is_inode_reached(irec, ino_offset)) {
2679 do_warn(
2680 _("entry \"%s\" in directory inode %" PRIu64
2681 " references already connected inode %" PRIu64 ".\n"),
2682 fname, ino, lino);
2683 next_sfep = shortform_dir2_junk(mp, sfp, sfep,
2684 lino, &max_size, &i,
2685 &bytes_deleted, ino_dirty);
2686 continue;
2687 } else if (parent == ino) {
2688 add_inode_reached(irec, ino_offset);
2689 add_inode_ref(current_irec, current_ino_offset);
2690 } else if (parent == NULLFSINO) {
2691 /* ".." was missing, but this entry refers to it,
2692 so, set it as the parent and mark for rebuild */
2693 do_warn(
2694 _("entry \"%s\" in dir ino %" PRIu64 " doesn't have a .. entry, will set it in ino %" PRIu64 ".\n"),
2695 fname, ino, lino);
2696 set_inode_parent(irec, ino_offset, ino);
2697 add_inode_reached(irec, ino_offset);
2698 add_inode_ref(current_irec, current_ino_offset);
2699 add_dotdot_update(XFS_INO_TO_AGNO(mp, lino),
2700 irec, ino_offset);
2701 } else {
2702 do_warn(
2703 _("entry \"%s\" in directory inode %" PRIu64
2704 " not consistent with .. value (%" PRIu64
2705 ") in inode %" PRIu64 ",\n"),
2706 fname, ino, parent, lino);
2707 next_sfep = shortform_dir2_junk(mp, sfp, sfep,
2708 lino, &max_size, &i,
2709 &bytes_deleted, ino_dirty);
2710 continue;
2711 }
2712 }
2713
2714 /* validate ftype field if supported */
2715 if (xfs_sb_version_hasftype(&mp->m_sb)) {
2716 uint8_t dir_ftype;
2717 uint8_t ino_ftype;
2718
2719 dir_ftype = M_DIROPS(mp)->sf_get_ftype(sfep);
2720 ino_ftype = get_inode_ftype(irec, ino_offset);
2721
2722 if (dir_ftype != ino_ftype) {
2723 if (no_modify) {
2724 do_warn(
2725 _("would fix ftype mismatch (%d/%d) in directory/child inode %" PRIu64 "/%" PRIu64 "\n"),
2726 dir_ftype, ino_ftype,
2727 ino, lino);
2728 } else {
2729 do_warn(
2730 _("fixing ftype mismatch (%d/%d) in directory/child inode %" PRIu64 "/%" PRIu64 "\n"),
2731 dir_ftype, ino_ftype,
2732 ino, lino);
2733 M_DIROPS(mp)->sf_put_ftype(sfep,
2734 ino_ftype);
2735 dir_hash_update_ftype(hashtab,
2736 (xfs_dir2_dataptr_t)(sfep - xfs_dir2_sf_firstentry(sfp)),
2737 ino_ftype);
2738 *ino_dirty = 1;
2739 }
2740 }
2741 }
2742
2743 if (lino > XFS_DIR2_MAX_SHORT_INUM)
2744 i8++;
2745
2746 /*
2747 * go onto next entry - we have to take entries with bad namelen
2748 * into account in no modify mode since we calculate size based
2749 * on next_sfep.
2750 */
2751 ASSERT(no_modify || bad_sfnamelen == 0);
2752 next_sfep = (struct xfs_dir2_sf_entry *)((intptr_t)sfep +
2753 (bad_sfnamelen
2754 ? M_DIROPS(mp)->sf_entsize(sfp, namelen)
2755 : M_DIROPS(mp)->sf_entsize(sfp, sfep->namelen)));
2756 }
2757
2758 if (sfp->i8count != i8) {
2759 if (no_modify) {
2760 do_warn(_("would fix i8count in inode %" PRIu64 "\n"),
2761 ino);
2762 } else {
2763 if (i8 == 0) {
2764 struct xfs_dir2_sf_entry *tmp_sfep;
2765
2766 tmp_sfep = next_sfep;
2767 process_sf_dir2_fixi8(mp, sfp, &tmp_sfep);
2768 bytes_deleted +=
2769 (intptr_t)next_sfep -
2770 (intptr_t)tmp_sfep;
2771 next_sfep = tmp_sfep;
2772 } else
2773 sfp->i8count = i8;
2774 *ino_dirty = 1;
2775 do_warn(_("fixing i8count in inode %" PRIu64 "\n"),
2776 ino);
2777 }
2778 }
2779
2780 /*
2781 * sync up sizes if required
2782 */
2783 if (*ino_dirty && bytes_deleted > 0) {
2784 ASSERT(!no_modify);
2785 libxfs_idata_realloc(ip, -bytes_deleted, XFS_DATA_FORK);
2786 ip->i_d.di_size -= bytes_deleted;
2787 }
2788
2789 if (ip->i_d.di_size != ip->i_df.if_bytes) {
2790 ASSERT(ip->i_df.if_bytes == (xfs_fsize_t)
2791 ((intptr_t) next_sfep - (intptr_t) sfp));
2792 ip->i_d.di_size = (xfs_fsize_t)
2793 ((intptr_t) next_sfep - (intptr_t) sfp);
2794 do_warn(
2795 _("setting size to %" PRId64 " bytes to reflect junked entries\n"),
2796 ip->i_d.di_size);
2797 *ino_dirty = 1;
2798 }
2799 }
2800
2801 /*
2802 * processes all reachable inodes in directories
2803 */
2804 static void
2805 process_dir_inode(
2806 xfs_mount_t *mp,
2807 xfs_agnumber_t agno,
2808 ino_tree_node_t *irec,
2809 int ino_offset)
2810 {
2811 xfs_ino_t ino;
2812 struct xfs_defer_ops dfops;
2813 xfs_fsblock_t first;
2814 xfs_inode_t *ip;
2815 xfs_trans_t *tp;
2816 dir_hash_tab_t *hashtab;
2817 int need_dot;
2818 int dirty, num_illegal, error, nres;
2819
2820 ino = XFS_AGINO_TO_INO(mp, agno, irec->ino_startnum + ino_offset);
2821
2822 /*
2823 * open up directory inode, check all entries,
2824 * then call prune_dir_entries to remove all
2825 * remaining illegal directory entries.
2826 */
2827
2828 ASSERT(!is_inode_refchecked(irec, ino_offset) || dotdot_update);
2829
2830 error = -libxfs_iget(mp, NULL, ino, 0, &ip);
2831 if (error) {
2832 if (!no_modify)
2833 do_error(
2834 _("couldn't map inode %" PRIu64 ", err = %d\n"),
2835 ino, error);
2836 else {
2837 do_warn(
2838 _("couldn't map inode %" PRIu64 ", err = %d\n"),
2839 ino, error);
2840 /*
2841 * see below for what we're doing if this
2842 * is root. Why do we need to do this here?
2843 * to ensure that the root doesn't show up
2844 * as being disconnected in the no_modify case.
2845 */
2846 if (mp->m_sb.sb_rootino == ino) {
2847 add_inode_reached(irec, 0);
2848 add_inode_ref(irec, 0);
2849 }
2850 }
2851
2852 add_inode_refchecked(irec, 0);
2853 return;
2854 }
2855
2856 need_dot = dirty = num_illegal = 0;
2857
2858 if (mp->m_sb.sb_rootino == ino) {
2859 /*
2860 * mark root inode reached and bump up
2861 * link count for root inode to account
2862 * for '..' entry since the root inode is
2863 * never reached by a parent. we know
2864 * that root's '..' is always good --
2865 * guaranteed by phase 3 and/or below.
2866 */
2867 add_inode_reached(irec, ino_offset);
2868 }
2869
2870 add_inode_refchecked(irec, ino_offset);
2871
2872 hashtab = dir_hash_init(ip->i_d.di_size);
2873
2874 /*
2875 * look for bogus entries
2876 */
2877 switch (ip->i_d.di_format) {
2878 case XFS_DINODE_FMT_EXTENTS:
2879 case XFS_DINODE_FMT_BTREE:
2880 /*
2881 * also check for missing '.' in longform dirs.
2882 * missing .. entries are added if required when
2883 * the directory is connected to lost+found. but
2884 * we need to create '.' entries here.
2885 */
2886 longform_dir2_entry_check(mp, ino, ip,
2887 &num_illegal, &need_dot,
2888 irec, ino_offset,
2889 hashtab);
2890 break;
2891
2892 case XFS_DINODE_FMT_LOCAL:
2893 /*
2894 * using the remove reservation is overkill
2895 * since at most we'll only need to log the
2896 * inode but it's easier than wedging a
2897 * new define in ourselves.
2898 */
2899 nres = no_modify ? 0 : XFS_REMOVE_SPACE_RES(mp);
2900 error = -libxfs_trans_alloc(mp, &M_RES(mp)->tr_remove,
2901 nres, 0, 0, &tp);
2902 if (error)
2903 res_failed(error);
2904
2905 libxfs_trans_ijoin(tp, ip, 0);
2906
2907 shortform_dir2_entry_check(mp, ino, ip, &dirty,
2908 irec, ino_offset,
2909 hashtab);
2910
2911 ASSERT(dirty == 0 || (dirty && !no_modify));
2912 if (dirty) {
2913 libxfs_trans_log_inode(tp, ip,
2914 XFS_ILOG_CORE | XFS_ILOG_DDATA);
2915 libxfs_trans_commit(tp);
2916 } else {
2917 libxfs_trans_cancel(tp);
2918 }
2919 break;
2920
2921 default:
2922 break;
2923 }
2924 dir_hash_done(hashtab);
2925
2926 /*
2927 * if we have to create a .. for /, do it now *before*
2928 * we delete the bogus entries, otherwise the directory
2929 * could transform into a shortform dir which would
2930 * probably cause the simulation to choke. Even
2931 * if the illegal entries get shifted around, it's ok
2932 * because the entries are structurally intact and in
2933 * in hash-value order so the simulation won't get confused
2934 * if it has to move them around.
2935 */
2936 if (!no_modify && need_root_dotdot && ino == mp->m_sb.sb_rootino) {
2937 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_LOCAL);
2938
2939 do_warn(_("recreating root directory .. entry\n"));
2940
2941 nres = XFS_MKDIR_SPACE_RES(mp, 2);
2942 error = -libxfs_trans_alloc(mp, &M_RES(mp)->tr_mkdir,
2943 nres, 0, 0, &tp);
2944 if (error)
2945 res_failed(error);
2946
2947 libxfs_trans_ijoin(tp, ip, 0);
2948
2949 libxfs_defer_init(&dfops, &first);
2950
2951 error = -libxfs_dir_createname(tp, ip, &xfs_name_dotdot,
2952 ip->i_ino, &first, &dfops, nres);
2953 if (error)
2954 do_error(
2955 _("can't make \"..\" entry in root inode %" PRIu64 ", createname error %d\n"), ino, error);
2956
2957 libxfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2958
2959 libxfs_defer_ijoin(&dfops, ip);
2960 error = -libxfs_defer_finish(&tp, &dfops);
2961 ASSERT(error == 0);
2962 libxfs_trans_commit(tp);
2963
2964 need_root_dotdot = 0;
2965 } else if (need_root_dotdot && ino == mp->m_sb.sb_rootino) {
2966 do_warn(_("would recreate root directory .. entry\n"));
2967 }
2968
2969 /*
2970 * if we need to create the '.' entry, do so only if
2971 * the directory is a longform dir. if it's been
2972 * turned into a shortform dir, then the inode is ok
2973 * since shortform dirs have no '.' entry and the inode
2974 * has already been committed by prune_lf_dir_entry().
2975 */
2976 if (need_dot) {
2977 /*
2978 * bump up our link count but don't
2979 * bump up the inode link count. chances
2980 * are good that even though we lost '.'
2981 * the inode link counts reflect '.' so
2982 * leave the inode link count alone and if
2983 * it turns out to be wrong, we'll catch
2984 * that in phase 7.
2985 */
2986 add_inode_ref(irec, ino_offset);
2987
2988 if (no_modify) {
2989 do_warn(
2990 _("would create missing \".\" entry in dir ino %" PRIu64 "\n"),
2991 ino);
2992 } else if (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL) {
2993 /*
2994 * need to create . entry in longform dir.
2995 */
2996 do_warn(
2997 _("creating missing \".\" entry in dir ino %" PRIu64 "\n"), ino);
2998
2999 nres = XFS_MKDIR_SPACE_RES(mp, 1);
3000 error = -libxfs_trans_alloc(mp, &M_RES(mp)->tr_mkdir,
3001 nres, 0, 0, &tp);
3002 if (error)
3003 res_failed(error);
3004
3005 libxfs_trans_ijoin(tp, ip, 0);
3006
3007 libxfs_defer_init(&dfops, &first);
3008
3009 error = -libxfs_dir_createname(tp, ip, &xfs_name_dot,
3010 ip->i_ino, &first, &dfops, nres);
3011 if (error)
3012 do_error(
3013 _("can't make \".\" entry in dir ino %" PRIu64 ", createname error %d\n"),
3014 ino, error);
3015
3016 libxfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3017
3018 libxfs_defer_ijoin(&dfops, ip);
3019 error = -libxfs_defer_finish(&tp, &dfops);
3020 ASSERT(error == 0);
3021 libxfs_trans_commit(tp);
3022 }
3023 }
3024 IRELE(ip);
3025 }
3026
3027 /*
3028 * mark realtime bitmap and summary inodes as reached.
3029 * quota inode will be marked here as well
3030 */
3031 static void
3032 mark_standalone_inodes(xfs_mount_t *mp)
3033 {
3034 ino_tree_node_t *irec;
3035 int offset;
3036
3037 irec = find_inode_rec(mp, XFS_INO_TO_AGNO(mp, mp->m_sb.sb_rbmino),
3038 XFS_INO_TO_AGINO(mp, mp->m_sb.sb_rbmino));
3039
3040 offset = XFS_INO_TO_AGINO(mp, mp->m_sb.sb_rbmino) -
3041 irec->ino_startnum;
3042
3043 add_inode_reached(irec, offset);
3044
3045 irec = find_inode_rec(mp, XFS_INO_TO_AGNO(mp, mp->m_sb.sb_rsumino),
3046 XFS_INO_TO_AGINO(mp, mp->m_sb.sb_rsumino));
3047
3048 offset = XFS_INO_TO_AGINO(mp, mp->m_sb.sb_rsumino) -
3049 irec->ino_startnum;
3050
3051 add_inode_reached(irec, offset);
3052
3053 if (fs_quotas) {
3054 if (mp->m_sb.sb_uquotino
3055 && mp->m_sb.sb_uquotino != NULLFSINO) {
3056 irec = find_inode_rec(mp, XFS_INO_TO_AGNO(mp,
3057 mp->m_sb.sb_uquotino),
3058 XFS_INO_TO_AGINO(mp, mp->m_sb.sb_uquotino));
3059 offset = XFS_INO_TO_AGINO(mp, mp->m_sb.sb_uquotino)
3060 - irec->ino_startnum;
3061 add_inode_reached(irec, offset);
3062 }
3063 if (mp->m_sb.sb_gquotino
3064 && mp->m_sb.sb_gquotino != NULLFSINO) {
3065 irec = find_inode_rec(mp, XFS_INO_TO_AGNO(mp,
3066 mp->m_sb.sb_gquotino),
3067 XFS_INO_TO_AGINO(mp, mp->m_sb.sb_gquotino));
3068 offset = XFS_INO_TO_AGINO(mp, mp->m_sb.sb_gquotino)
3069 - irec->ino_startnum;
3070 add_inode_reached(irec, offset);
3071 }
3072 if (mp->m_sb.sb_pquotino
3073 && mp->m_sb.sb_pquotino != NULLFSINO) {
3074 irec = find_inode_rec(mp, XFS_INO_TO_AGNO(mp,
3075 mp->m_sb.sb_pquotino),
3076 XFS_INO_TO_AGINO(mp, mp->m_sb.sb_pquotino));
3077 offset = XFS_INO_TO_AGINO(mp, mp->m_sb.sb_pquotino)
3078 - irec->ino_startnum;
3079 add_inode_reached(irec, offset);
3080 }
3081 }
3082 }
3083
3084 static void
3085 check_for_orphaned_inodes(
3086 xfs_mount_t *mp,
3087 xfs_agnumber_t agno,
3088 ino_tree_node_t *irec)
3089 {
3090 int i;
3091 xfs_ino_t ino;
3092
3093 for (i = 0; i < XFS_INODES_PER_CHUNK; i++) {
3094 ASSERT(is_inode_confirmed(irec, i));
3095 if (is_inode_free(irec, i))
3096 continue;
3097
3098 if (is_inode_reached(irec, i))
3099 continue;
3100
3101 ASSERT(inode_isadir(irec, i) ||
3102 num_inode_references(irec, i) == 0);
3103
3104 ino = XFS_AGINO_TO_INO(mp, agno, i + irec->ino_startnum);
3105 if (inode_isadir(irec, i))
3106 do_warn(_("disconnected dir inode %" PRIu64 ", "), ino);
3107 else
3108 do_warn(_("disconnected inode %" PRIu64 ", "), ino);
3109 if (!no_modify) {
3110 if (!orphanage_ino)
3111 orphanage_ino = mk_orphanage(mp);
3112 do_warn(_("moving to %s\n"), ORPHANAGE);
3113 mv_orphanage(mp, ino, inode_isadir(irec, i));
3114 } else {
3115 do_warn(_("would move to %s\n"), ORPHANAGE);
3116 }
3117 /*
3118 * for read-only case, even though the inode isn't
3119 * really reachable, set the flag (and bump our link
3120 * count) anyway to fool phase 7
3121 */
3122 add_inode_reached(irec, i);
3123 }
3124 }
3125
3126 static void
3127 traverse_function(
3128 struct workqueue *wq,
3129 xfs_agnumber_t agno,
3130 void *arg)
3131 {
3132 ino_tree_node_t *irec;
3133 int i;
3134 prefetch_args_t *pf_args = arg;
3135
3136 wait_for_inode_prefetch(pf_args);
3137
3138 if (verbose)
3139 do_log(_(" - agno = %d\n"), agno);
3140
3141 for (irec = findfirst_inode_rec(agno); irec; irec = next_ino_rec(irec)) {
3142 if (irec->ino_isa_dir == 0)
3143 continue;
3144
3145 if (pf_args) {
3146 sem_post(&pf_args->ra_count);
3147 #ifdef XR_PF_TRACE
3148 sem_getvalue(&pf_args->ra_count, &i);
3149 pftrace(
3150 "processing inode chunk %p in AG %d (sem count = %d)",
3151 irec, agno, i);
3152 #endif
3153 }
3154
3155 for (i = 0; i < XFS_INODES_PER_CHUNK; i++) {
3156 if (inode_isadir(irec, i))
3157 process_dir_inode(wq->wq_ctx, agno, irec, i);
3158 }
3159 }
3160 cleanup_inode_prefetch(pf_args);
3161 }
3162
3163 static void
3164 update_missing_dotdot_entries(
3165 xfs_mount_t *mp)
3166 {
3167 dotdot_update_t *dir;
3168
3169 /*
3170 * these entries parents were updated, rebuild them again
3171 * set dotdot_update flag so processing routines do not count links
3172 */
3173 dotdot_update = 1;
3174 while (!list_empty(&dotdot_update_list)) {
3175 dir = list_entry(dotdot_update_list.prev, struct dotdot_update,
3176 list);
3177 list_del(&dir->list);
3178 process_dir_inode(mp, dir->agno, dir->irec, dir->ino_offset);
3179 free(dir);
3180 }
3181 }
3182
3183 static void
3184 traverse_ags(
3185 struct xfs_mount *mp)
3186 {
3187 do_inode_prefetch(mp, 0, traverse_function, false, true);
3188 }
3189
3190 void
3191 phase6(xfs_mount_t *mp)
3192 {
3193 ino_tree_node_t *irec;
3194 int i;
3195
3196 memset(&zerocr, 0, sizeof(struct cred));
3197 memset(&zerofsx, 0, sizeof(struct fsxattr));
3198 orphanage_ino = 0;
3199
3200 do_log(_("Phase 6 - check inode connectivity...\n"));
3201
3202 incore_ext_teardown(mp);
3203
3204 add_ino_ex_data(mp);
3205
3206 /*
3207 * verify existence of root directory - if we have to
3208 * make one, it's ok for the incore data structs not to
3209 * know about it since everything about it (and the other
3210 * inodes in its chunk if a new chunk was created) are ok
3211 */
3212 if (need_root_inode) {
3213 if (!no_modify) {
3214 do_warn(_("reinitializing root directory\n"));
3215 mk_root_dir(mp);
3216 need_root_inode = 0;
3217 need_root_dotdot = 0;
3218 } else {
3219 do_warn(_("would reinitialize root directory\n"));
3220 }
3221 }
3222
3223 if (need_rbmino) {
3224 if (!no_modify) {
3225 do_warn(_("reinitializing realtime bitmap inode\n"));
3226 mk_rbmino(mp);
3227 need_rbmino = 0;
3228 } else {
3229 do_warn(_("would reinitialize realtime bitmap inode\n"));
3230 }
3231 }
3232
3233 if (need_rsumino) {
3234 if (!no_modify) {
3235 do_warn(_("reinitializing realtime summary inode\n"));
3236 mk_rsumino(mp);
3237 need_rsumino = 0;
3238 } else {
3239 do_warn(_("would reinitialize realtime summary inode\n"));
3240 }
3241 }
3242
3243 if (!no_modify) {
3244 do_log(
3245 _(" - resetting contents of realtime bitmap and summary inodes\n"));
3246 if (fill_rbmino(mp)) {
3247 do_warn(
3248 _("Warning: realtime bitmap may be inconsistent\n"));
3249 }
3250
3251 if (fill_rsumino(mp)) {
3252 do_warn(
3253 _("Warning: realtime bitmap may be inconsistent\n"));
3254 }
3255 }
3256
3257 mark_standalone_inodes(mp);
3258
3259 do_log(_(" - traversing filesystem ...\n"));
3260
3261 irec = find_inode_rec(mp, XFS_INO_TO_AGNO(mp, mp->m_sb.sb_rootino),
3262 XFS_INO_TO_AGINO(mp, mp->m_sb.sb_rootino));
3263
3264 /*
3265 * we always have a root inode, even if it's free...
3266 * if the root is free, forget it, lost+found is already gone
3267 */
3268 if (is_inode_free(irec, 0) || !inode_isadir(irec, 0)) {
3269 need_root_inode = 1;
3270 }
3271
3272 /*
3273 * then process all inodes by walking incore inode tree
3274 */
3275 traverse_ags(mp);
3276
3277 /*
3278 * any directories that had updated ".." entries, rebuild them now
3279 */
3280 update_missing_dotdot_entries(mp);
3281
3282 do_log(_(" - traversal finished ...\n"));
3283 do_log(_(" - moving disconnected inodes to %s ...\n"),
3284 ORPHANAGE);
3285
3286 /*
3287 * move all disconnected inodes to the orphanage
3288 */
3289 for (i = 0; i < glob_agcount; i++) {
3290 irec = findfirst_inode_rec(i);
3291 while (irec != NULL) {
3292 check_for_orphaned_inodes(mp, i, irec);
3293 irec = next_ino_rec(irec);
3294 }
3295 }
3296 }