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