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