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