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