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