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