]> www.infradead.org Git - users/hch/xfsprogs.git/blob
e607f0b
[users/hch/xfsprogs.git] /
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 "protos.h"
25 #include "err_protos.h"
26 #include "dir2.h"
27 #include "dinode.h"
28 #include "scan.h"
29 #include "versions.h"
30 #include "attr_repair.h"
31 #include "bmap.h"
32 #include "threads.h"
33
34 /*
35  * inode clearing routines
36  */
37
38 static int
39 clear_dinode_attr(xfs_mount_t *mp, xfs_dinode_t *dino, xfs_ino_t ino_num)
40 {
41         ASSERT(dino->di_forkoff != 0);
42
43         if (!no_modify)
44                 fprintf(stderr,
45 _("clearing inode %" PRIu64 " attributes\n"), ino_num);
46         else
47                 fprintf(stderr,
48 _("would have cleared inode %" PRIu64 " attributes\n"), ino_num);
49
50         if (be16_to_cpu(dino->di_anextents) != 0)  {
51                 if (no_modify)
52                         return(1);
53                 dino->di_anextents = cpu_to_be16(0);
54         }
55
56         if (dino->di_aformat != XFS_DINODE_FMT_EXTENTS)  {
57                 if (no_modify)
58                         return(1);
59                 dino->di_aformat = XFS_DINODE_FMT_EXTENTS;
60         }
61
62         /* get rid of the fork by clearing forkoff */
63
64         /* Originally, when the attr repair code was added, the fork was cleared
65          * by turning it into shortform status.  This meant clearing the
66          * hdr.totsize/count fields and also changing aformat to LOCAL
67          * (vs EXTENTS).  Over various fixes, the aformat and forkoff have
68          * been updated to not show an attribute fork at all, however.
69          * It could be possible that resetting totsize/count are not needed,
70          * but just to be safe, leave it in for now.
71          */
72
73         if (!no_modify) {
74                 xfs_attr_shortform_t *asf = (xfs_attr_shortform_t *)
75                                 XFS_DFORK_APTR(dino);
76                 asf->hdr.totsize = cpu_to_be16(sizeof(xfs_attr_sf_hdr_t));
77                 asf->hdr.count = 0;
78                 dino->di_forkoff = 0;  /* got to do this after asf is set */
79         }
80
81         /*
82          * always returns 1 since the fork gets zapped
83          */
84         return(1);
85 }
86
87 static int
88 clear_dinode_core(struct xfs_mount *mp, xfs_dinode_t *dinoc, xfs_ino_t ino_num)
89 {
90         int dirty = 0;
91         int i;
92
93 #define __dirty_no_modify_ret(dirty) \
94         ({ (dirty) = 1; if (no_modify) return 1; })
95
96         if (be16_to_cpu(dinoc->di_magic) != XFS_DINODE_MAGIC)  {
97                 __dirty_no_modify_ret(dirty);
98                 dinoc->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
99         }
100
101         if (!XFS_DINODE_GOOD_VERSION(dinoc->di_version) ||
102             (!fs_inode_nlink && dinoc->di_version > 1))  {
103                 __dirty_no_modify_ret(dirty);
104                 if (xfs_sb_version_hascrc(&mp->m_sb))
105                         dinoc->di_version = 3;
106                 else
107                         dinoc->di_version = (fs_inode_nlink) ? 2 : 1;
108         }
109
110         if (be16_to_cpu(dinoc->di_mode) != 0)  {
111                 __dirty_no_modify_ret(dirty);
112                 dinoc->di_mode = 0;
113         }
114
115         if (be16_to_cpu(dinoc->di_flags) != 0)  {
116                 __dirty_no_modify_ret(dirty);
117                 dinoc->di_flags = 0;
118         }
119
120         if (be32_to_cpu(dinoc->di_dmevmask) != 0)  {
121                 __dirty_no_modify_ret(dirty);
122                 dinoc->di_dmevmask = 0;
123         }
124
125         if (dinoc->di_forkoff != 0)  {
126                 __dirty_no_modify_ret(dirty);
127                 dinoc->di_forkoff = 0;
128         }
129
130         if (dinoc->di_format != XFS_DINODE_FMT_EXTENTS)  {
131                 __dirty_no_modify_ret(dirty);
132                 dinoc->di_format = XFS_DINODE_FMT_EXTENTS;
133         }
134
135         if (dinoc->di_aformat != XFS_DINODE_FMT_EXTENTS)  {
136                 __dirty_no_modify_ret(dirty);
137                 dinoc->di_aformat = XFS_DINODE_FMT_EXTENTS;
138         }
139
140         if (be64_to_cpu(dinoc->di_size) != 0)  {
141                 __dirty_no_modify_ret(dirty);
142                 dinoc->di_size = 0;
143         }
144
145         if (be64_to_cpu(dinoc->di_nblocks) != 0)  {
146                 __dirty_no_modify_ret(dirty);
147                 dinoc->di_nblocks = 0;
148         }
149
150         if (be16_to_cpu(dinoc->di_onlink) != 0)  {
151                 __dirty_no_modify_ret(dirty);
152                 dinoc->di_onlink = 0;
153         }
154
155         if (be32_to_cpu(dinoc->di_nextents) != 0)  {
156                 __dirty_no_modify_ret(dirty);
157                 dinoc->di_nextents = 0;
158         }
159
160         if (be16_to_cpu(dinoc->di_anextents) != 0)  {
161                 __dirty_no_modify_ret(dirty);
162                 dinoc->di_anextents = 0;
163         }
164
165         if (dinoc->di_version > 1 &&
166                         be32_to_cpu(dinoc->di_nlink) != 0)  {
167                 __dirty_no_modify_ret(dirty);
168                 dinoc->di_nlink = 0;
169         }
170
171         /* we are done for version 1/2 inodes */
172         if (dinoc->di_version < 3)
173                 return dirty;
174
175         if (be64_to_cpu(dinoc->di_ino) != ino_num) {
176                 __dirty_no_modify_ret(dirty);
177                 dinoc->di_ino = cpu_to_be64(ino_num);
178         }
179
180         if (platform_uuid_compare(&dinoc->di_uuid, &mp->m_sb.sb_uuid)) {
181                 __dirty_no_modify_ret(dirty);
182                 platform_uuid_copy(&dinoc->di_uuid, &mp->m_sb.sb_uuid);
183         }
184
185         for (i = 0; i < 16; i++) {
186                 if (dinoc->di_pad[i] != 0) {
187                         __dirty_no_modify_ret(dirty);
188                         memset(dinoc->di_pad, 0, 16);
189                         break;
190                 }
191         }
192
193         if (be64_to_cpu(dinoc->di_flags2) != 0)  {
194                 __dirty_no_modify_ret(dirty);
195                 dinoc->di_flags2 = 0;
196         }
197
198         if (be64_to_cpu(dinoc->di_lsn) != 0)  {
199                 __dirty_no_modify_ret(dirty);
200                 dinoc->di_lsn = 0;
201         }
202
203         if (be64_to_cpu(dinoc->di_changecount) != 0)  {
204                 __dirty_no_modify_ret(dirty);
205                 dinoc->di_changecount = 0;
206         }
207
208         return dirty;
209 }
210
211 static int
212 clear_dinode_unlinked(xfs_mount_t *mp, xfs_dinode_t *dino)
213 {
214
215         if (be32_to_cpu(dino->di_next_unlinked) != NULLAGINO)  {
216                 if (!no_modify)
217                         dino->di_next_unlinked = cpu_to_be32(NULLAGINO);
218                 return(1);
219         }
220
221         return(0);
222 }
223
224 /*
225  * this clears the unlinked list too so it should not be called
226  * until after the agi unlinked lists are walked in phase 3.
227  * returns > zero if the inode has been altered while being cleared
228  */
229 static int
230 clear_dinode(xfs_mount_t *mp, xfs_dinode_t *dino, xfs_ino_t ino_num)
231 {
232         int dirty;
233
234         dirty = clear_dinode_core(mp, dino, ino_num);
235         dirty += clear_dinode_unlinked(mp, dino);
236
237         /* and clear the forks */
238
239         if (dirty && !no_modify)
240                 memset(XFS_DFORK_DPTR(dino), 0,
241                        XFS_LITINO(mp, dino->di_version));
242
243         return(dirty);
244 }
245
246
247 /*
248  * misc. inode-related utility routines
249  */
250
251 /*
252  * verify_ag_bno is heavily used. In the common case, it
253  * performs just two number of compares
254  * Returns 1 for bad ag/bno pair or 0 if it's valid.
255  */
256 static __inline int
257 verify_ag_bno(xfs_sb_t *sbp,
258                 xfs_agnumber_t agno,
259                 xfs_agblock_t agbno)
260 {
261         if (agno < (sbp->sb_agcount - 1)) 
262                 return (agbno >= sbp->sb_agblocks);
263         if (agno == (sbp->sb_agcount - 1)) 
264                 return (agbno >= (sbp->sb_dblocks -
265                                 ((xfs_drfsbno_t)(sbp->sb_agcount - 1) *
266                                  sbp->sb_agblocks)));
267         return 1;
268 }
269
270 /*
271  * returns 0 if inode number is valid, 1 if bogus
272  */
273 int
274 verify_inum(xfs_mount_t         *mp,
275                 xfs_ino_t       ino)
276 {
277         xfs_agnumber_t  agno;
278         xfs_agino_t     agino;
279         xfs_agblock_t   agbno;
280         xfs_sb_t        *sbp = &mp->m_sb;;
281
282         /* range check ag #, ag block.  range-checking offset is pointless */
283
284         agno = XFS_INO_TO_AGNO(mp, ino);
285         agino = XFS_INO_TO_AGINO(mp, ino);
286         agbno = XFS_AGINO_TO_AGBNO(mp, agino);
287         if (agbno == 0)
288                 return 1;
289
290         if (ino == 0 || ino == NULLFSINO)
291                 return(1);
292
293         if (ino != XFS_AGINO_TO_INO(mp, agno, agino))
294                 return(1);
295
296         return verify_ag_bno(sbp, agno, agbno);
297 }
298
299 /*
300  * have a separate routine to ensure that we don't accidentally
301  * lose illegally set bits in the agino by turning it into an FSINO
302  * to feed to the above routine
303  */
304 int
305 verify_aginum(xfs_mount_t       *mp,
306                 xfs_agnumber_t  agno,
307                 xfs_agino_t     agino)
308 {
309         xfs_agblock_t   agbno;
310         xfs_sb_t        *sbp = &mp->m_sb;;
311
312         /* range check ag #, ag block.  range-checking offset is pointless */
313
314         if (agino == 0 || agino == NULLAGINO)
315                 return(1);
316
317         /*
318          * agino's can't be too close to NULLAGINO because the min blocksize
319          * is 9 bits and at most 1 bit of that gets used for the inode offset
320          * so if the agino gets shifted by the # of offset bits and compared
321          * to the legal agbno values, a bogus agino will be too large.  there
322          * will be extra bits set at the top that shouldn't be set.
323          */
324         agbno = XFS_AGINO_TO_AGBNO(mp, agino);
325         if (agbno == 0)
326                 return 1;
327
328         return verify_ag_bno(sbp, agno, agbno);
329 }
330
331 /*
332  * return 1 if block number is good, 0 if out of range
333  */
334 int
335 verify_dfsbno(xfs_mount_t       *mp,
336                 xfs_dfsbno_t    fsbno)
337 {
338         xfs_agnumber_t  agno;
339         xfs_agblock_t   agbno;
340         xfs_sb_t        *sbp = &mp->m_sb;;
341
342         /* range check ag #, ag block.  range-checking offset is pointless */
343
344         agno = XFS_FSB_TO_AGNO(mp, fsbno);
345         agbno = XFS_FSB_TO_AGBNO(mp, fsbno);
346
347         return verify_ag_bno(sbp, agno, agbno) == 0;
348 }
349
350 #define XR_DFSBNORANGE_VALID    0
351 #define XR_DFSBNORANGE_BADSTART 1
352 #define XR_DFSBNORANGE_BADEND   2
353 #define XR_DFSBNORANGE_OVERFLOW 3
354
355 static __inline int
356 verify_dfsbno_range(xfs_mount_t *mp,
357                 xfs_dfsbno_t    fsbno,
358                 xfs_dfilblks_t  count)
359 {
360         xfs_agnumber_t  agno;
361         xfs_agblock_t   agbno;
362         xfs_sb_t        *sbp = &mp->m_sb;;
363
364         /* the start and end blocks better be in the same allocation group */
365         agno = XFS_FSB_TO_AGNO(mp, fsbno);
366         if (agno != XFS_FSB_TO_AGNO(mp, fsbno + count - 1)) {
367                 return XR_DFSBNORANGE_OVERFLOW;
368         }
369
370         agbno = XFS_FSB_TO_AGBNO(mp, fsbno);
371         if (verify_ag_bno(sbp, agno, agbno)) {
372                 return XR_DFSBNORANGE_BADSTART;
373         }
374
375         agbno = XFS_FSB_TO_AGBNO(mp, fsbno + count - 1);
376         if (verify_ag_bno(sbp, agno, agbno)) {
377                 return XR_DFSBNORANGE_BADEND;
378         }
379
380         return (XR_DFSBNORANGE_VALID);
381 }
382
383 int
384 verify_agbno(xfs_mount_t        *mp,
385                 xfs_agnumber_t  agno,
386                 xfs_agblock_t   agbno)
387 {
388         xfs_sb_t        *sbp = &mp->m_sb;;
389
390         /* range check ag #, ag block.  range-checking offset is pointless */
391         return verify_ag_bno(sbp, agno, agbno) == 0;
392 }
393
394 static int
395 process_rt_rec(
396         xfs_mount_t             *mp,
397         xfs_bmbt_irec_t         *irec,
398         xfs_ino_t               ino,
399         xfs_drfsbno_t           *tot,
400         int                     check_dups)
401 {
402         xfs_dfsbno_t            b;
403         xfs_drtbno_t            ext;
404         int                     state;
405         int                     pwe;            /* partially-written extent */
406
407         /*
408          * check numeric validity of the extent
409          */
410         if (irec->br_startblock >= mp->m_sb.sb_rblocks) {
411                 do_warn(
412 _("inode %" PRIu64 " - bad rt extent start block number %" PRIu64 ", offset %" PRIu64 "\n"),
413                         ino,
414                         irec->br_startblock,
415                         irec->br_startoff);
416                 return 1;
417         }
418         if (irec->br_startblock + irec->br_blockcount - 1 >= mp->m_sb.sb_rblocks) {
419                 do_warn(
420 _("inode %" PRIu64 " - bad rt extent last block number %" PRIu64 ", offset %" PRIu64 "\n"),
421                         ino,
422                         irec->br_startblock + irec->br_blockcount - 1,
423                         irec->br_startoff);
424                 return 1;
425         }
426         if (irec->br_startblock + irec->br_blockcount - 1 < irec->br_startblock) {
427                 do_warn(
428 _("inode %" PRIu64 " - bad rt extent overflows - start %" PRIu64 ", "
429   "end %" PRIu64 ", offset %" PRIu64 "\n"),
430                         ino,
431                         irec->br_startblock,
432                         irec->br_startblock + irec->br_blockcount - 1,
433                         irec->br_startoff);
434                 return 1;
435         }
436
437         /*
438          * verify that the blocks listed in the record
439          * are multiples of an extent
440          */
441         if (xfs_sb_version_hasextflgbit(&mp->m_sb) == 0 &&
442                         (irec->br_startblock % mp->m_sb.sb_rextsize != 0 ||
443                          irec->br_blockcount % mp->m_sb.sb_rextsize != 0)) {
444                 do_warn(
445 _("malformed rt inode extent [%" PRIu64 " %" PRIu64 "] (fs rtext size = %u)\n"),
446                         irec->br_startblock,
447                         irec->br_blockcount,
448                         mp->m_sb.sb_rextsize);
449                 return 1;
450         }
451
452         /*
453          * set the appropriate number of extents
454          * this iterates block by block, this can be optimised using extents
455          */
456         for (b = irec->br_startblock; b < irec->br_startblock +
457                         irec->br_blockcount; b += mp->m_sb.sb_rextsize)  {
458                 ext = (xfs_drtbno_t) b / mp->m_sb.sb_rextsize;
459                 pwe = xfs_sb_version_hasextflgbit(&mp->m_sb) &&
460                                 irec->br_state == XFS_EXT_UNWRITTEN &&
461                                 (b % mp->m_sb.sb_rextsize != 0);
462
463                 if (check_dups == 1)  {
464                         if (search_rt_dup_extent(mp, ext) && !pwe)  {
465                                 do_warn(
466 _("data fork in rt ino %" PRIu64 " claims dup rt extent,"
467   "off - %" PRIu64 ", start - %" PRIu64 ", count %" PRIu64 "\n"),
468                                         ino,
469                                         irec->br_startoff,
470                                         irec->br_startblock,
471                                         irec->br_blockcount);
472                                 return 1;
473                         }
474                         continue;
475                 }
476
477                 state = get_rtbmap(ext);
478                 switch (state)  {
479                 case XR_E_FREE:
480                 case XR_E_UNKNOWN:
481                         set_rtbmap(ext, XR_E_INUSE);
482                         break;
483                 case XR_E_BAD_STATE:
484                         do_error(
485 _("bad state in rt block map %" PRIu64 "\n"),
486                                 ext);
487                 case XR_E_FS_MAP:
488                 case XR_E_INO:
489                 case XR_E_INUSE_FS:
490                         do_error(
491 _("data fork in rt inode %" PRIu64 " found metadata block %" PRIu64 " in rt bmap\n"),
492                                 ino, ext);
493                 case XR_E_INUSE:
494                         if (pwe)
495                                 break;
496                 case XR_E_MULT:
497                         set_rtbmap(ext, XR_E_MULT);
498                         do_warn(
499 _("data fork in rt inode %" PRIu64 " claims used rt block %" PRIu64 "\n"),
500                                 ino, ext);
501                         return 1;
502                 case XR_E_FREE1:
503                 default:
504                         do_error(
505 _("illegal state %d in rt block map %" PRIu64 "\n"),
506                                 state, b);
507                 }
508         }
509
510         /*
511          * bump up the block counter
512          */
513         *tot += irec->br_blockcount;
514
515         return 0;
516 }
517
518 /*
519  * return 1 if inode should be cleared, 0 otherwise
520  * if check_dups should be set to 1, that implies that
521  * the primary purpose of this call is to see if the
522  * file overlaps with any duplicate extents (in the
523  * duplicate extent list).
524  */
525 static int
526 process_bmbt_reclist_int(
527         xfs_mount_t             *mp,
528         xfs_bmbt_rec_t          *rp,
529         int                     *numrecs,
530         int                     type,
531         xfs_ino_t               ino,
532         xfs_drfsbno_t           *tot,
533         blkmap_t                **blkmapp,
534         xfs_dfiloff_t           *first_key,
535         xfs_dfiloff_t           *last_key,
536         int                     check_dups,
537         int                     whichfork)
538 {
539         xfs_bmbt_irec_t         irec;
540         xfs_dfilblks_t          cp = 0;         /* prev count */
541         xfs_dfsbno_t            sp = 0;         /* prev start */
542         xfs_dfiloff_t           op = 0;         /* prev offset */
543         xfs_dfsbno_t            b;
544         char                    *ftype;
545         char                    *forkname;
546         int                     i;
547         int                     state;
548         xfs_agnumber_t          agno;
549         xfs_agblock_t           agbno;
550         xfs_agblock_t           ebno;
551         xfs_extlen_t            blen;
552         xfs_agnumber_t          locked_agno = -1;
553         int                     error = 1;
554
555         if (whichfork == XFS_DATA_FORK)
556                 forkname = _("data");
557         else
558                 forkname = _("attr");
559
560         if (type == XR_INO_RTDATA)
561                 ftype = _("real-time");
562         else
563                 ftype = _("regular");
564
565         for (i = 0; i < *numrecs; i++) {
566                 libxfs_bmbt_disk_get_all(rp + i, &irec);
567                 if (i == 0)
568                         *last_key = *first_key = irec.br_startoff;
569                 else
570                         *last_key = irec.br_startoff;
571                 if (i > 0 && op + cp > irec.br_startoff)  {
572                         do_warn(
573 _("bmap rec out of order, inode %" PRIu64" entry %d "
574   "[o s c] [%" PRIu64 " %" PRIu64 " %" PRIu64 "], "
575   "%d [%" PRIu64 " %" PRIu64 " %" PRIu64 "]\n"),
576                                 ino, i, irec.br_startoff, irec.br_startblock,
577                                 irec.br_blockcount, i - 1, op, sp, cp);
578                         goto done;
579                 }
580                 op = irec.br_startoff;
581                 cp = irec.br_blockcount;
582                 sp = irec.br_startblock;
583
584                 /*
585                  * check numeric validity of the extent
586                  */
587                 if (irec.br_blockcount == 0)  {
588                         do_warn(
589 _("zero length extent (off = %" PRIu64 ", fsbno = %" PRIu64 ") in ino %" PRIu64 "\n"),
590                                 irec.br_startoff,
591                                 irec.br_startblock,
592                                 ino);
593                         goto done;
594                 }
595
596                 if (type == XR_INO_RTDATA && whichfork == XFS_DATA_FORK) {
597                         /*
598                          * realtime bitmaps don't use AG locks, so returning
599                          * immediately is fine for this code path.
600                          */
601                         if (process_rt_rec(mp, &irec, ino, tot, check_dups))
602                                 return 1;
603                         /*
604                          * skip rest of loop processing since that'irec.br_startblock
605                          * all for regular file forks and attr forks
606                          */
607                         continue;
608                 }
609
610                 /*
611                  * regular file data fork or attribute fork
612                  */
613                 switch (verify_dfsbno_range(mp, irec.br_startblock,
614                                                 irec.br_blockcount)) {
615                         case XR_DFSBNORANGE_VALID:
616                                 break;
617
618                         case XR_DFSBNORANGE_BADSTART:
619                                 do_warn(
620 _("inode %" PRIu64 " - bad extent starting block number %" PRIu64 ", offset %" PRIu64 "\n"),
621                                         ino,
622                                         irec.br_startblock,
623                                         irec.br_startoff);
624                                 goto done;
625
626                         case XR_DFSBNORANGE_BADEND:
627                                 do_warn(
628 _("inode %" PRIu64 " - bad extent last block number %" PRIu64 ", offset %" PRIu64 "\n"),
629                                         ino,
630                                         irec.br_startblock + irec.br_blockcount - 1,
631                                         irec.br_startoff);
632                                 goto done;
633
634                         case XR_DFSBNORANGE_OVERFLOW:
635                                 do_warn(
636 _("inode %" PRIu64 " - bad extent overflows - start %" PRIu64 ", "
637   "end %" PRIu64 ", offset %" PRIu64 "\n"),
638                                         ino,
639                                         irec.br_startblock,
640                                         irec.br_startblock + irec.br_blockcount - 1,
641                                         irec.br_startoff);
642                                 goto done;
643                 }
644                 if (irec.br_startoff >= fs_max_file_offset)  {
645                         do_warn(
646 _("inode %" PRIu64 " - extent offset too large - start %" PRIu64 ", "
647   "count %" PRIu64 ", offset %" PRIu64 "\n"),
648                                 ino, irec.br_startblock, irec.br_blockcount,
649                                 irec.br_startoff);
650                         goto done;
651                 }
652
653                 if (blkmapp && *blkmapp) {
654                         error = blkmap_set_ext(blkmapp, irec.br_startoff,
655                                         irec.br_startblock, irec.br_blockcount);
656                         if (error) {
657                                 /*
658                                  * we don't want to clear the inode due to an
659                                  * internal bmap tracking error, but if we've
660                                  * run out of memory then we simply can't
661                                  * validate that the filesystem is consistent.
662                                  * Hence just abort at this point with an ENOMEM
663                                  * error.
664                                  */
665                                 do_abort(
666 _("Fatal error: inode %" PRIu64 " - blkmap_set_ext(): %s\n"
667   "\t%s fork, off - %" PRIu64 ", start - %" PRIu64 ", cnt %" PRIu64 "\n"),
668                                         ino, strerror(error), forkname,
669                                         irec.br_startoff, irec.br_startblock,
670                                         irec.br_blockcount);
671                         }
672                 }
673
674                 /*
675                  * Profiling shows that the following loop takes the
676                  * most time in all of xfs_repair.
677                  */
678                 agno = XFS_FSB_TO_AGNO(mp, irec.br_startblock);
679                 agbno = XFS_FSB_TO_AGBNO(mp, irec.br_startblock);
680                 ebno = agbno + irec.br_blockcount;
681                 if (agno != locked_agno) {
682                         if (locked_agno != -1)
683                                 pthread_mutex_unlock(&ag_locks[locked_agno]);
684                         pthread_mutex_lock(&ag_locks[agno]);
685                         locked_agno = agno;
686                 }
687
688                 if (check_dups) {
689                         /*
690                          * if we're just checking the bmap for dups,
691                          * return if we find one, otherwise, continue
692                          * checking each entry without setting the
693                          * block bitmap
694                          */
695                         if (search_dup_extent(agno, agbno, ebno)) {
696                                 do_warn(
697 _("%s fork in ino %" PRIu64 " claims dup extent, "
698   "off - %" PRIu64 ", start - %" PRIu64 ", cnt %" PRIu64 "\n"),
699                                         forkname, ino, irec.br_startoff,
700                                         irec.br_startblock,
701                                         irec.br_blockcount);
702                                 goto done;
703                         }
704                         *tot += irec.br_blockcount;
705                         continue;
706                 }
707
708                 for (b = irec.br_startblock;
709                      agbno < ebno;
710                      b += blen, agbno += blen) {
711                         state = get_bmap_ext(agno, agbno, ebno, &blen);
712                         switch (state)  {
713                         case XR_E_FREE:
714                         case XR_E_FREE1:
715                                 do_warn(
716 _("%s fork in ino %" PRIu64 " claims free block %" PRIu64 "\n"),
717                                         forkname, ino, (__uint64_t) b);
718                                 /* fall through ... */
719                         case XR_E_UNKNOWN:
720                                 set_bmap_ext(agno, agbno, blen, XR_E_INUSE);
721                                 break;
722
723                         case XR_E_BAD_STATE:
724                                 do_error(_("bad state in block map %" PRIu64 "\n"), b);
725
726                         case XR_E_FS_MAP:
727                         case XR_E_INO:
728                         case XR_E_INUSE_FS:
729                                 do_warn(
730 _("%s fork in inode %" PRIu64 " claims metadata block %" PRIu64 "\n"),
731                                         forkname, ino, b);
732                                 goto done;
733
734                         case XR_E_INUSE:
735                         case XR_E_MULT:
736                                 set_bmap_ext(agno, agbno, blen, XR_E_MULT);
737                                 do_warn(
738 _("%s fork in %s inode %" PRIu64 " claims used block %" PRIu64 "\n"),
739                                         forkname, ftype, ino, b);
740                                 goto done;
741
742                         default:
743                                 do_error(
744 _("illegal state %d in block map %" PRIu64 "\n"),
745                                         state, b);
746                         }
747                 }
748                 *tot += irec.br_blockcount;
749         }
750         error = 0;
751 done:
752         if (locked_agno != -1)
753                 pthread_mutex_unlock(&ag_locks[locked_agno]);
754
755         if (i != *numrecs) {
756                 ASSERT(i < *numrecs);
757                 do_warn(_("correcting nextents for inode %" PRIu64 "\n"), ino);
758                 *numrecs = i;
759         }
760
761         return error;
762 }
763
764 /*
765  * return 1 if inode should be cleared, 0 otherwise, sets block bitmap
766  * as a side-effect
767  */
768 int
769 process_bmbt_reclist(
770         xfs_mount_t             *mp,
771         xfs_bmbt_rec_t          *rp,
772         int                     *numrecs,
773         int                     type,
774         xfs_ino_t               ino,
775         xfs_drfsbno_t           *tot,
776         blkmap_t                **blkmapp,
777         xfs_dfiloff_t           *first_key,
778         xfs_dfiloff_t           *last_key,
779         int                     whichfork)
780 {
781         return process_bmbt_reclist_int(mp, rp, numrecs, type, ino, tot,
782                                 blkmapp, first_key, last_key, 0, whichfork);
783 }
784
785 /*
786  * return 1 if inode should be cleared, 0 otherwise, does not set
787  * block bitmap
788  */
789 int
790 scan_bmbt_reclist(
791         xfs_mount_t             *mp,
792         xfs_bmbt_rec_t          *rp,
793         int                     *numrecs,
794         int                     type,
795         xfs_ino_t               ino,
796         xfs_drfsbno_t           *tot,
797         int                     whichfork)
798 {
799         xfs_dfiloff_t           first_key = 0;
800         xfs_dfiloff_t           last_key = 0;
801
802         return process_bmbt_reclist_int(mp, rp, numrecs, type, ino, tot,
803                                 NULL, &first_key, &last_key, 1, whichfork);
804 }
805
806 /*
807  * these two are meant for routines that read and work with inodes
808  * one at a time where the inodes may be in any order (like walking
809  * the unlinked lists to look for inodes).  the caller is responsible
810  * for writing/releasing the buffer.
811  */
812 xfs_buf_t *
813 get_agino_buf(xfs_mount_t        *mp,
814                 xfs_agnumber_t  agno,
815                 xfs_agino_t     agino,
816                 xfs_dinode_t    **dipp)
817 {
818         ino_tree_node_t *irec;
819         xfs_buf_t *bp;
820         int size;
821
822         if ((irec = find_inode_rec(mp, agno, agino)) == NULL)
823                 return(NULL);
824
825         size = XFS_FSB_TO_BB(mp, MAX(1, XFS_INODES_PER_CHUNK/inodes_per_block));
826         bp = libxfs_readbuf(mp->m_dev, XFS_AGB_TO_DADDR(mp, agno,
827                 XFS_AGINO_TO_AGBNO(mp, irec->ino_startnum)), size, 0,
828                 &xfs_inode_buf_ops);
829         if (!bp) {
830                 do_warn(_("cannot read inode (%u/%u), disk block %" PRIu64 "\n"),
831                         agno, irec->ino_startnum,
832                         XFS_AGB_TO_DADDR(mp, agno,
833                                 XFS_AGINO_TO_AGBNO(mp, irec->ino_startnum)));
834                 return(NULL);
835         }
836
837         *dipp = xfs_make_iptr(mp, bp, agino -
838                 XFS_OFFBNO_TO_AGINO(mp, XFS_AGINO_TO_AGBNO(mp,
839                                                 irec->ino_startnum),
840                 0));
841
842         return(bp);
843 }
844
845 /*
846  * these next routines return the filesystem blockno of the
847  * block containing the block "bno" in the file whose bmap
848  * tree (or extent list) is rooted by "rootblock".
849  *
850  * the next routines are utility routines for the third
851  * routine, get_bmapi().
852  *
853  * NOTE: getfunc_extlist only used by dirv1 checking code
854  */
855 static xfs_dfsbno_t
856 getfunc_extlist(xfs_mount_t             *mp,
857                 xfs_ino_t               ino,
858                 xfs_dinode_t            *dip,
859                 xfs_dfiloff_t           bno,
860                 int                     whichfork)
861 {
862         xfs_bmbt_irec_t         irec;
863         xfs_dfsbno_t            final_fsbno = NULLDFSBNO;
864         xfs_bmbt_rec_t          *rootblock = (xfs_bmbt_rec_t *)
865                                                 XFS_DFORK_PTR(dip, whichfork);
866         xfs_extnum_t            nextents = XFS_DFORK_NEXTENTS(dip, whichfork);
867         int                     i;
868
869         for (i = 0; i < nextents; i++)  {
870                 libxfs_bmbt_disk_get_all(rootblock + i, &irec);
871                 if (irec.br_startoff <= bno &&
872                                 bno < irec.br_startoff + irec.br_blockcount) {
873                         final_fsbno = bno - irec.br_startoff + irec.br_startblock;
874                         break;
875                 }
876         }
877
878         return(final_fsbno);
879 }
880
881 /*
882  * NOTE: getfunc_btree only used by dirv1 checking code... 
883  */
884 static xfs_dfsbno_t
885 getfunc_btree(xfs_mount_t               *mp,
886                 xfs_ino_t               ino,
887                 xfs_dinode_t            *dip,
888                 xfs_dfiloff_t           bno,
889                 int                     whichfork)
890 {
891         int                     i;
892 #ifdef DEBUG
893         int                     prev_level;
894 #endif
895         int                     found;
896         int                     numrecs;
897         xfs_bmbt_rec_t          *rec;
898         xfs_bmbt_irec_t         irec;
899         xfs_bmbt_ptr_t          *pp;
900         xfs_bmbt_key_t          *key;
901         xfs_bmdr_key_t          *rkey;
902         xfs_bmdr_ptr_t          *rp;
903         xfs_dfsbno_t            fsbno;
904         xfs_buf_t               *bp;
905         xfs_dfsbno_t            final_fsbno = NULLDFSBNO;
906         struct xfs_btree_block  *block;
907         xfs_bmdr_block_t        *rootblock = (xfs_bmdr_block_t *)
908                                                 XFS_DFORK_PTR(dip, whichfork);
909
910         ASSERT(rootblock->bb_level != 0);
911         /*
912          * deal with root block, it's got a slightly different
913          * header structure than interior nodes.  We know that
914          * a btree should have at least 2 levels otherwise it
915          * would be an extent list.
916          */
917         rkey = XFS_BMDR_KEY_ADDR(rootblock, 1);
918         rp = XFS_BMDR_PTR_ADDR(rootblock, 1,
919                 xfs_bmdr_maxrecs(mp, XFS_DFORK_SIZE(dip, mp, whichfork), 1));
920         found = -1;
921         for (i = 0; i < be16_to_cpu(rootblock->bb_numrecs) - 1; i++) {
922                 if (be64_to_cpu(rkey[i].br_startoff) <= bno && 
923                                 bno < be64_to_cpu(rkey[i + 1].br_startoff)) {
924                         found = i;
925                         break;
926                 }
927         }
928         if (i == be16_to_cpu(rootblock->bb_numrecs) - 1 && 
929                                 bno >= be64_to_cpu(rkey[i].br_startoff))
930                 found = i;
931
932         ASSERT(found != -1);
933
934         fsbno = be64_to_cpu(rp[found]);
935
936         ASSERT(verify_dfsbno(mp, fsbno));
937
938         bp = libxfs_readbuf(mp->m_dev, XFS_FSB_TO_DADDR(mp, fsbno),
939                                 XFS_FSB_TO_BB(mp, 1), 0, NULL);
940         if (!bp) {
941                 do_error(_("cannot read bmap block %" PRIu64 "\n"), fsbno);
942                 return(NULLDFSBNO);
943         }
944         block = XFS_BUF_TO_BLOCK(bp);
945         numrecs = be16_to_cpu(block->bb_numrecs);
946
947         /*
948          * ok, now traverse any interior btree nodes
949          */
950 #ifdef DEBUG
951         prev_level = be16_to_cpu(block->bb_level);
952 #endif
953
954         while (be16_to_cpu(block->bb_level) > 0)  {
955 #ifdef DEBUG
956                 ASSERT(be16_to_cpu(block->bb_level) < prev_level);
957
958                 prev_level = be16_to_cpu(block->bb_level);
959 #endif
960                 if (numrecs > mp->m_bmap_dmxr[1]) {
961                         do_warn(
962 _("# of bmap records in inode %" PRIu64 " exceeds max (%u, max - %u)\n"),
963                                 ino, numrecs,
964                                 mp->m_bmap_dmxr[1]);
965                         libxfs_putbuf(bp);
966                         return(NULLDFSBNO);
967                 }
968                 if (verbose && numrecs < mp->m_bmap_dmnr[1]) {
969                         do_warn(
970 _("- # of bmap records in inode %" PRIu64 " less than minimum (%u, min - %u), proceeding ...\n"),
971                                 ino, numrecs, mp->m_bmap_dmnr[1]);
972                 }
973                 key = XFS_BMBT_KEY_ADDR(mp, block, 1);
974                 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
975                 for (found = -1, i = 0; i < numrecs - 1; i++) {
976                         if (be64_to_cpu(key[i].br_startoff) <= bno && bno < 
977                                         be64_to_cpu(key[i + 1].br_startoff)) {
978                                 found = i;
979                                 break;
980                         }
981                 }
982                 if (i == numrecs - 1 && bno >= be64_to_cpu(key[i].br_startoff))
983                         found = i;
984
985                 ASSERT(found != -1);
986                 fsbno = be64_to_cpu(pp[found]);
987
988                 ASSERT(verify_dfsbno(mp, fsbno));
989
990                 /*
991                  * release current btree block and read in the
992                  * next btree block to be traversed
993                  */
994                 libxfs_putbuf(bp);
995                 bp = libxfs_readbuf(mp->m_dev, XFS_FSB_TO_DADDR(mp, fsbno),
996                                         XFS_FSB_TO_BB(mp, 1), 0, NULL);
997                 if (!bp) {
998                         do_error(_("cannot read bmap block %" PRIu64 "\n"),
999                                 fsbno);
1000                         return(NULLDFSBNO);
1001                 }
1002                 block = XFS_BUF_TO_BLOCK(bp);
1003                 numrecs = be16_to_cpu(block->bb_numrecs);
1004         }
1005
1006         /*
1007          * current block must be a leaf block
1008          */
1009         ASSERT(be16_to_cpu(block->bb_level) == 0);
1010         if (numrecs > mp->m_bmap_dmxr[0]) {
1011                 do_warn(
1012 _("# of bmap records in inode %" PRIu64 " greater than maximum (%u, max - %u)\n"),
1013                         ino, numrecs, mp->m_bmap_dmxr[0]);
1014                 libxfs_putbuf(bp);
1015                 return(NULLDFSBNO);
1016         }
1017         if (verbose && numrecs < mp->m_bmap_dmnr[0])
1018                 do_warn(
1019 _("- # of bmap records in inode %" PRIu64 " less than minimum (%u, min - %u), continuing...\n"),
1020                         ino, numrecs, mp->m_bmap_dmnr[0]);
1021
1022         rec = XFS_BMBT_REC_ADDR(mp, block, 1);
1023         for (i = 0; i < numrecs; i++)  {
1024                 libxfs_bmbt_disk_get_all(rec + i, &irec);
1025                 if (irec.br_startoff <= bno &&
1026                                 bno < irec.br_startoff + irec.br_blockcount) {
1027                         final_fsbno = bno - irec.br_startoff +
1028                                                         irec.br_startblock;
1029                         break;
1030                 }
1031         }
1032         libxfs_putbuf(bp);
1033
1034         if (final_fsbno == NULLDFSBNO)
1035                 do_warn(_("could not map block %" PRIu64 "\n"), bno);
1036
1037         return(final_fsbno);
1038 }
1039
1040 /*
1041  * this could be smarter.  maybe we should have an open inode
1042  * routine that would get the inode buffer and return back
1043  * an inode handle.  I'm betting for the moment that this
1044  * is used only by the directory and attribute checking code
1045  * and that the avl tree find and buffer cache search are
1046  * relatively cheap.  If they're too expensive, we'll just
1047  * have to fix this and add an inode handle to the da btree
1048  * cursor.
1049  *
1050  * caller is responsible for checking doubly referenced blocks
1051  * and references to holes
1052  *
1053  * NOTE: get_bmapi only used by dirv1 checking code
1054  */
1055 xfs_dfsbno_t
1056 get_bmapi(xfs_mount_t *mp, xfs_dinode_t *dino_p,
1057                 xfs_ino_t ino_num, xfs_dfiloff_t bno, int whichfork)
1058 {
1059         xfs_dfsbno_t            fsbno;
1060
1061         switch (XFS_DFORK_FORMAT(dino_p, whichfork)) {
1062         case XFS_DINODE_FMT_EXTENTS:
1063                 fsbno = getfunc_extlist(mp, ino_num, dino_p, bno, whichfork);
1064                 break;
1065         case XFS_DINODE_FMT_BTREE:
1066                 fsbno = getfunc_btree(mp, ino_num, dino_p, bno, whichfork);
1067                 break;
1068         case XFS_DINODE_FMT_LOCAL:
1069                 do_error(_("get_bmapi() called for local inode %" PRIu64 "\n"),
1070                         ino_num);
1071                 fsbno = NULLDFSBNO;
1072                 break;
1073         default:
1074                 /*
1075                  * shouldn't happen
1076                  */
1077                 do_error(_("bad inode format for inode %" PRIu64 "\n"), ino_num);
1078                 fsbno = NULLDFSBNO;
1079         }
1080
1081         return(fsbno);
1082 }
1083
1084 /*
1085  * higher level inode processing stuff starts here:
1086  * first, one utility routine for each type of inode
1087  */
1088
1089 /*
1090  * return 1 if inode should be cleared, 0 otherwise
1091  */
1092 static int
1093 process_btinode(
1094         xfs_mount_t             *mp,
1095         xfs_agnumber_t          agno,
1096         xfs_agino_t             ino,
1097         xfs_dinode_t            *dip,
1098         int                     type,
1099         int                     *dirty,
1100         xfs_drfsbno_t           *tot,
1101         __uint64_t              *nex,
1102         blkmap_t                **blkmapp,
1103         int                     whichfork,
1104         int                     check_dups)
1105 {
1106         xfs_bmdr_block_t        *dib;
1107         xfs_dfiloff_t           last_key;
1108         xfs_dfiloff_t           first_key = 0;
1109         xfs_ino_t               lino;
1110         xfs_bmbt_ptr_t          *pp;
1111         xfs_bmbt_key_t          *pkey;
1112         char                    *forkname;
1113         int                     i;
1114         int                     level;
1115         int                     numrecs;
1116         bmap_cursor_t           cursor;
1117         __uint64_t              magic;
1118
1119         dib = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
1120         lino = XFS_AGINO_TO_INO(mp, agno, ino);
1121         *tot = 0;
1122         *nex = 0;
1123
1124         if (whichfork == XFS_DATA_FORK)
1125                 forkname = _("data");
1126         else
1127                 forkname = _("attr");
1128
1129         magic = xfs_sb_version_hascrc(&mp->m_sb) ? XFS_BMAP_CRC_MAGIC
1130                                                  : XFS_BMAP_MAGIC;
1131
1132         level = be16_to_cpu(dib->bb_level);
1133         numrecs = be16_to_cpu(dib->bb_numrecs);
1134
1135         if ((level == 0) || (level > XFS_BM_MAXLEVELS(mp, whichfork))) {
1136                 /*
1137                  * XXX - if we were going to fix up the inode,
1138                  * we'd try to treat the fork as an interior
1139                  * node and see if we could get an accurate
1140                  * level value from one of the blocks pointed
1141                  * to by the pointers in the fork.  For now
1142                  * though, we just bail (and blow out the inode).
1143                  */
1144                 do_warn(
1145 _("bad level %d in inode %" PRIu64 " bmap btree root block\n"),
1146                         level, XFS_AGINO_TO_INO(mp, agno, ino));
1147                 return(1);
1148         }
1149         if (numrecs == 0) {
1150                 do_warn(
1151 _("bad numrecs 0 in inode %" PRIu64 " bmap btree root block\n"),
1152                         XFS_AGINO_TO_INO(mp, agno, ino));
1153                 return(1);
1154         }
1155         /*
1156          * use bmdr/dfork_dsize since the root block is in the data fork
1157          */
1158         if (XFS_BMDR_SPACE_CALC(numrecs) > XFS_DFORK_SIZE(dip, mp, whichfork)) {
1159                 do_warn(
1160         _("indicated size of %s btree root (%d bytes) greater than space in "
1161           "inode %" PRIu64 " %s fork\n"),
1162                         forkname, XFS_BMDR_SPACE_CALC(numrecs), lino, forkname);
1163                 return(1);
1164         }
1165
1166         init_bm_cursor(&cursor, level + 1);
1167
1168         pp = XFS_BMDR_PTR_ADDR(dib, 1,
1169                 xfs_bmdr_maxrecs(mp, XFS_DFORK_SIZE(dip, mp, whichfork), 0));
1170         pkey = XFS_BMDR_KEY_ADDR(dib, 1);
1171         last_key = NULLDFILOFF;
1172
1173         for (i = 0; i < numrecs; i++)  {
1174                 /*
1175                  * XXX - if we were going to do more to fix up the inode
1176                  * btree, we'd do it right here.  For now, if there's a
1177                  * problem, we'll bail out and presumably clear the inode.
1178                  */
1179                 if (!verify_dfsbno(mp, be64_to_cpu(pp[i])))  {
1180                         do_warn(_("bad bmap btree ptr 0x%llx in ino %" PRIu64 "\n"),
1181                                (unsigned long long) be64_to_cpu(pp[i]), lino);
1182                         return(1);
1183                 }
1184
1185                 if (scan_lbtree(be64_to_cpu(pp[i]), level, scan_bmapbt, type,
1186                                 whichfork, lino, tot, nex, blkmapp, &cursor,
1187                                 1, check_dups, magic, &xfs_bmbt_buf_ops))
1188                         return(1);
1189                 /*
1190                  * fix key (offset) mismatches between the keys in root
1191                  * block records and the first key of each child block.
1192                  * fixes cases where entries have been shifted between
1193                  * blocks but the parent hasn't been updated
1194                  */
1195                 if (!check_dups && cursor.level[level-1].first_key !=
1196                                         be64_to_cpu(pkey[i].br_startoff))  {
1197                         if (!no_modify)  {
1198                                 do_warn(
1199         _("correcting key in bmbt root (was %llu, now %" PRIu64") in inode "
1200           "%" PRIu64" %s fork\n"),
1201                                        (unsigned long long)
1202                                                be64_to_cpu(pkey[i].br_startoff),
1203                                         cursor.level[level-1].first_key,
1204                                         XFS_AGINO_TO_INO(mp, agno, ino),
1205                                         forkname);
1206                                 *dirty = 1;
1207                                 pkey[i].br_startoff = cpu_to_be64(
1208                                         cursor.level[level-1].first_key);
1209                         } else  {
1210                                 do_warn(
1211         _("bad key in bmbt root (is %llu, would reset to %" PRIu64 ") in inode "
1212           "%" PRIu64 " %s fork\n"),
1213                                        (unsigned long long)
1214                                                be64_to_cpu(pkey[i].br_startoff),
1215                                         cursor.level[level-1].first_key,
1216                                         XFS_AGINO_TO_INO(mp, agno, ino),
1217                                         forkname);
1218                         }
1219                 }
1220                 /*
1221                  * make sure that keys are in ascending order.  blow out
1222                  * inode if the ordering doesn't hold
1223                  */
1224                 if (check_dups == 0)  {
1225                         if (last_key != NULLDFILOFF && last_key >=
1226                             cursor.level[level-1].first_key)  {
1227                                 do_warn(
1228         _("out of order bmbt root key %" PRIu64 " in inode %" PRIu64 " %s fork\n"),
1229                                         first_key,
1230                                         XFS_AGINO_TO_INO(mp, agno, ino),
1231                                         forkname);
1232                                 return(1);
1233                         }
1234                         last_key = cursor.level[level-1].first_key;
1235                 }
1236         }
1237         /*
1238          * Ideally if all the extents are ok (perhaps after further
1239          * checks below?) we'd just move this back into extents format.
1240          * But for now clear it, as the kernel will choke on this
1241          */
1242         if (*nex <= XFS_DFORK_SIZE(dip, mp, whichfork) /
1243                         sizeof(xfs_bmbt_rec_t)) {
1244                 do_warn(
1245         _("extent count for ino %" PRIu64 " %s fork too low (%" PRIu64 ") for file format\n"),
1246                                 lino, forkname, *nex);
1247                 return(1);
1248         }
1249         /*
1250          * Check that the last child block's forward sibling pointer
1251          * is NULL.
1252          */
1253         if (check_dups == 0 &&
1254                 cursor.level[0].right_fsbno != NULLDFSBNO)  {
1255                 do_warn(
1256         _("bad fwd (right) sibling pointer (saw %" PRIu64 " should be NULLDFSBNO)\n"),
1257                         cursor.level[0].right_fsbno);
1258                 do_warn(
1259         _("\tin inode %" PRIu64 " (%s fork) bmap btree block %" PRIu64 "\n"),
1260                         XFS_AGINO_TO_INO(mp, agno, ino), forkname,
1261                         cursor.level[0].fsbno);
1262                 return(1);
1263         }
1264
1265         return(0);
1266 }
1267
1268 /*
1269  * return 1 if inode should be cleared, 0 otherwise
1270  */
1271 static int
1272 process_exinode(
1273         xfs_mount_t             *mp,
1274         xfs_agnumber_t          agno,
1275         xfs_agino_t             ino,
1276         xfs_dinode_t            *dip,
1277         int                     type,
1278         int                     *dirty,
1279         xfs_drfsbno_t           *tot,
1280         __uint64_t              *nex,
1281         blkmap_t                **blkmapp,
1282         int                     whichfork,
1283         int                     check_dups)
1284 {
1285         xfs_ino_t               lino;
1286         xfs_bmbt_rec_t          *rp;
1287         xfs_dfiloff_t           first_key;
1288         xfs_dfiloff_t           last_key;
1289         int32_t                 numrecs;
1290         int                     ret;
1291
1292         lino = XFS_AGINO_TO_INO(mp, agno, ino);
1293         rp = (xfs_bmbt_rec_t *)XFS_DFORK_PTR(dip, whichfork);
1294         *tot = 0;
1295         numrecs = XFS_DFORK_NEXTENTS(dip, whichfork);
1296
1297         /*
1298          * We've already decided on the maximum number of extents on the inode,
1299          * and numrecs may be corrupt. Hence make sure we only allow numrecs to
1300          * be in the range of valid on-disk numbers, which is:
1301          *      0 < numrecs < 2^31 - 1
1302          */
1303         if (numrecs < 0)
1304                 numrecs = *nex;
1305
1306         /*
1307          * XXX - if we were going to fix up the btree record,
1308          * we'd do it right here.  For now, if there's a problem,
1309          * we'll bail out and presumably clear the inode.
1310          */
1311         if (check_dups == 0)
1312                 ret = process_bmbt_reclist(mp, rp, &numrecs, type, lino,
1313                                         tot, blkmapp, &first_key, &last_key,
1314                                         whichfork);
1315         else
1316                 ret = scan_bmbt_reclist(mp, rp, &numrecs, type, lino, tot,
1317                                         whichfork);
1318
1319         *nex = numrecs;
1320         return ret;
1321 }
1322
1323 /*
1324  * return 1 if inode should be cleared, 0 otherwise
1325  */
1326 static int
1327 process_lclinode(
1328         xfs_mount_t             *mp,
1329         xfs_agnumber_t          agno,
1330         xfs_agino_t             ino,
1331         xfs_dinode_t            *dip,
1332         int                     whichfork)
1333 {
1334         xfs_attr_shortform_t    *asf;
1335         xfs_ino_t               lino;
1336
1337         lino = XFS_AGINO_TO_INO(mp, agno, ino);
1338         if (whichfork == XFS_DATA_FORK && be64_to_cpu(dip->di_size) >
1339                                                 XFS_DFORK_DSIZE(dip, mp)) {
1340                 do_warn(
1341         _("local inode %" PRIu64 " data fork is too large (size = %lld, max = %d)\n"),
1342                        lino, (unsigned long long) be64_to_cpu(dip->di_size),
1343                         XFS_DFORK_DSIZE(dip, mp));
1344                 return(1);
1345         } else if (whichfork == XFS_ATTR_FORK) {
1346                 asf = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
1347                 if (be16_to_cpu(asf->hdr.totsize) > XFS_DFORK_ASIZE(dip, mp)) {
1348                         do_warn(
1349         _("local inode %" PRIu64 " attr fork too large (size %d, max = %d)\n"),
1350                                 lino, be16_to_cpu(asf->hdr.totsize),
1351                                 XFS_DFORK_ASIZE(dip, mp));
1352                         return(1);
1353                 }
1354                 if (be16_to_cpu(asf->hdr.totsize) < sizeof(xfs_attr_sf_hdr_t)) {
1355                         do_warn(
1356         _("local inode %" PRIu64 " attr too small (size = %d, min size = %zd)\n"),
1357                                 lino, be16_to_cpu(asf->hdr.totsize),
1358                                 sizeof(xfs_attr_sf_hdr_t));
1359                         return(1);
1360                 }
1361         }
1362
1363         return(0);
1364 }
1365
1366 static int
1367 process_symlink_extlist(xfs_mount_t *mp, xfs_ino_t lino, xfs_dinode_t *dino)
1368 {
1369         xfs_dfiloff_t           expected_offset;
1370         xfs_bmbt_rec_t          *rp;
1371         xfs_bmbt_irec_t         irec;
1372         int                     numrecs;
1373         int                     i;
1374         int                     max_blocks;
1375
1376         if (be64_to_cpu(dino->di_size) <= XFS_DFORK_DSIZE(dino, mp)) {
1377                 if (dino->di_format == XFS_DINODE_FMT_LOCAL)  
1378                         return 0;
1379                 do_warn(
1380 _("mismatch between format (%d) and size (%" PRId64 ") in symlink ino %" PRIu64 "\n"),
1381                         dino->di_format,
1382                         (__int64_t)be64_to_cpu(dino->di_size), lino);
1383                 return 1;
1384         }
1385         if (dino->di_format == XFS_DINODE_FMT_LOCAL) {
1386                 do_warn(
1387 _("mismatch between format (%d) and size (%" PRId64 ") in symlink inode %" PRIu64 "\n"),
1388                         dino->di_format,
1389                         (__int64_t)be64_to_cpu(dino->di_size), lino);
1390                 return 1;
1391         }
1392
1393         rp = (xfs_bmbt_rec_t *)XFS_DFORK_DPTR(dino);
1394         numrecs = be32_to_cpu(dino->di_nextents);
1395
1396         /*
1397          * the max # of extents in a symlink inode is equal to the
1398          * number of max # of blocks required to store the symlink
1399          */
1400         if (numrecs > max_symlink_blocks)  {
1401                 do_warn(
1402 _("bad number of extents (%d) in symlink %" PRIu64 " data fork\n"),
1403                         numrecs, lino);
1404                 return(1);
1405         }
1406
1407         max_blocks = max_symlink_blocks;
1408         expected_offset = 0;
1409
1410         for (i = 0; i < numrecs; i++)  {
1411                 libxfs_bmbt_disk_get_all(rp + i, &irec);
1412
1413                 if (irec.br_startoff != expected_offset)  {
1414                         do_warn(
1415 _("bad extent #%d offset (%" PRIu64 ") in symlink %" PRIu64 " data fork\n"),
1416                                 i, irec.br_startoff, lino);
1417                         return(1);
1418                 }
1419                 if (irec.br_blockcount == 0 || irec.br_blockcount > max_blocks) {
1420                         do_warn(
1421 _("bad extent #%d count (%" PRIu64 ") in symlink %" PRIu64 " data fork\n"),
1422                                 i, irec.br_blockcount, lino);
1423                         return(1);
1424                 }
1425
1426                 max_blocks -= irec.br_blockcount;
1427                 expected_offset += irec.br_blockcount;
1428         }
1429
1430         return(0);
1431 }
1432
1433 /*
1434  * takes a name and length and returns 1 if the name contains
1435  * a \0, returns 0 otherwise
1436  */
1437 static int
1438 null_check(char *name, int length)
1439 {
1440         int i;
1441
1442         ASSERT(length < MAXPATHLEN);
1443
1444         for (i = 0; i < length; i++, name++)  {
1445                 if (*name == '\0')
1446                         return(1);
1447         }
1448
1449         return(0);
1450 }
1451
1452 static int
1453 process_symlink_remote(
1454         struct xfs_mount        *mp,
1455         xfs_ino_t               lino,
1456         struct xfs_dinode       *dino,
1457         struct blkmap           *blkmap,
1458         char                    *dst)
1459 {
1460         xfs_dfsbno_t            fsbno;
1461         struct xfs_buf          *bp;
1462         char                    *src;
1463         int                     pathlen;
1464         int                     offset;
1465         int                     i;
1466
1467         offset = 0;
1468         pathlen = be64_to_cpu(dino->di_size);
1469         i = 0;
1470
1471         while (pathlen > 0) {
1472                 int     blk_cnt = 1;
1473                 int     byte_cnt;
1474
1475                 fsbno = blkmap_get(blkmap, i);
1476                 if (fsbno == NULLDFSBNO) {
1477                         do_warn(
1478 _("cannot read inode %" PRIu64 ", file block %d, NULL disk block\n"),
1479                                 lino, i);
1480                         return 1;
1481                 }
1482
1483                 /*
1484                  * There's a symlink header for each contiguous extent. If
1485                  * there are contiguous blocks, read them in one go.
1486                  */
1487                 while (blk_cnt <= max_symlink_blocks) {
1488                         if (blkmap_get(blkmap, i + 1) != fsbno + 1)
1489                                 break;
1490                         blk_cnt++;
1491                         i++;
1492                 }
1493
1494                 byte_cnt = XFS_FSB_TO_B(mp, blk_cnt);
1495
1496                 bp = libxfs_readbuf(mp->m_dev, XFS_FSB_TO_DADDR(mp, fsbno),
1497                                     BTOBB(byte_cnt), 0, &xfs_symlink_buf_ops);
1498                 if (!bp) {
1499                         do_warn(
1500 _("cannot read inode %" PRIu64 ", file block %d, disk block %" PRIu64 "\n"),
1501                                 lino, i, fsbno);
1502                         return 1;
1503                 }
1504
1505                 byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt);
1506                 byte_cnt = MIN(pathlen, byte_cnt);
1507
1508                 src = bp->b_addr;
1509                 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1510                         if (!libxfs_symlink_hdr_ok(mp, lino, offset,
1511                                                 byte_cnt, bp)) {
1512                                 do_warn(
1513 _("bad symlink header ino %" PRIu64 ", file block %d, disk block %" PRIu64 "\n"),
1514                                         lino, i, fsbno);
1515                                 libxfs_putbuf(bp);
1516                                 return 1;
1517                         }
1518                         src += sizeof(struct xfs_dsymlink_hdr);
1519                 }
1520
1521                 memmove(dst + offset, src, byte_cnt);
1522
1523                 pathlen -= byte_cnt;
1524                 offset += byte_cnt;
1525                 i++;
1526
1527                 libxfs_putbuf(bp);
1528         }
1529         return 0;
1530 }
1531
1532 /*
1533  * like usual, returns 0 if everything's ok and 1 if something's
1534  * bogus
1535  */
1536 static int
1537 process_symlink(
1538         xfs_mount_t     *mp,
1539         xfs_ino_t       lino,
1540         xfs_dinode_t    *dino,
1541         blkmap_t        *blkmap)
1542 {
1543         char                    *symlink, *cptr;
1544         char                    data[MAXPATHLEN];
1545
1546         /*
1547          * check size against kernel symlink limits.  we know
1548          * size is consistent with inode storage format -- e.g.
1549          * the inode is structurally ok so we don't have to check
1550          * for that
1551          */
1552         if (be64_to_cpu(dino->di_size) >= MAXPATHLEN)  {
1553                do_warn(_("symlink in inode %" PRIu64 " too long (%llu chars)\n"),
1554                        lino, (unsigned long long) be64_to_cpu(dino->di_size));
1555                 return(1);
1556         }
1557
1558         /*
1559          * have to check symlink component by component.
1560          * get symlink contents into data area
1561          */
1562         symlink = &data[0];
1563         if (be64_to_cpu(dino->di_size) <= XFS_DFORK_DSIZE(dino, mp))  {
1564                 /*
1565                  * local symlink, just copy the symlink out of the
1566                  * inode into the data area
1567                  */
1568                 memmove(symlink, XFS_DFORK_DPTR(dino), 
1569                                                 be64_to_cpu(dino->di_size));
1570         } else {
1571                 int error;
1572
1573                 error = process_symlink_remote(mp, lino, dino, blkmap, symlink);
1574                 if (error)
1575                         return error;
1576         }
1577
1578         data[be64_to_cpu(dino->di_size)] = '\0';
1579
1580         /*
1581          * check for nulls
1582          */
1583         if (null_check(symlink, be64_to_cpu(dino->di_size)))  {
1584                 do_warn(
1585 _("found illegal null character in symlink inode %" PRIu64 "\n"),
1586                         lino);
1587                 return(1);
1588         }
1589
1590         /*
1591          * check for any component being too long
1592          */
1593         if (be64_to_cpu(dino->di_size) >= MAXNAMELEN)  {
1594                 cptr = strchr(symlink, '/');
1595
1596                 while (cptr != NULL)  {
1597                         if (cptr - symlink >= MAXNAMELEN)  {
1598                                 do_warn(
1599 _("component of symlink in inode %" PRIu64 " too long\n"),
1600                                         lino);
1601                                 return(1);
1602                         }
1603                         symlink = cptr + 1;
1604                         cptr = strchr(symlink, '/');
1605                 }
1606
1607                 if (strlen(symlink) >= MAXNAMELEN)  {
1608                         do_warn(
1609 _("component of symlink in inode %" PRIu64 " too long\n"),
1610                                 lino);
1611                         return(1);
1612                 }
1613         }
1614
1615         return(0);
1616 }
1617
1618 /*
1619  * called to process the set of misc inode special inode types
1620  * that have no associated data storage (fifos, pipes, devices, etc.).
1621  */
1622 static int
1623 process_misc_ino_types(xfs_mount_t      *mp,
1624                         xfs_dinode_t    *dino,
1625                         xfs_ino_t       lino,
1626                         int             type)
1627 {
1628         /*
1629          * disallow mountpoint inodes until such time as the
1630          * kernel actually allows them to be created (will
1631          * probably require a superblock version rev, sigh).
1632          */
1633         if (type == XR_INO_MOUNTPOINT)  {
1634                 do_warn(
1635 _("inode %" PRIu64 " has bad inode type (IFMNT)\n"), lino);
1636                 return(1);
1637         }
1638
1639         /*
1640          * must also have a zero size
1641          */
1642         if (be64_to_cpu(dino->di_size) != 0)  {
1643                 switch (type)  {
1644                 case XR_INO_CHRDEV:
1645                         do_warn(
1646 _("size of character device inode %" PRIu64 " != 0 (%" PRId64 " bytes)\n"), lino,
1647                                 (__int64_t)be64_to_cpu(dino->di_size));
1648                         break;
1649                 case XR_INO_BLKDEV:
1650                         do_warn(
1651 _("size of block device inode %" PRIu64 " != 0 (%" PRId64 " bytes)\n"), lino,
1652                                 (__int64_t)be64_to_cpu(dino->di_size));
1653                         break;
1654                 case XR_INO_SOCK:
1655                         do_warn(
1656 _("size of socket inode %" PRIu64 " != 0 (%" PRId64 " bytes)\n"), lino,
1657                                 (__int64_t)be64_to_cpu(dino->di_size));
1658                         break;
1659                 case XR_INO_FIFO:
1660                         do_warn(
1661 _("size of fifo inode %" PRIu64 " != 0 (%" PRId64 " bytes)\n"), lino,
1662                                 (__int64_t)be64_to_cpu(dino->di_size));
1663                         break;
1664                 default:
1665                         do_warn(_("Internal error - process_misc_ino_types, "
1666                                   "illegal type %d\n"), type);
1667                         abort();
1668                 }
1669
1670                 return(1);
1671         }
1672
1673         return(0);
1674 }
1675
1676 static int
1677 process_misc_ino_types_blocks(xfs_drfsbno_t totblocks, xfs_ino_t lino, int type)
1678 {
1679         /*
1680          * you can not enforce all misc types have zero data fork blocks
1681          * by checking dino->di_nblocks because atotblocks (attribute
1682          * blocks) are part of nblocks. We must check this later when atotblocks
1683          * has been calculated or by doing a simple check that anExtents == 0.
1684          * We must also guarantee that totblocks is 0. Thus nblocks checking
1685          * will be done later in process_dinode_int for misc types.
1686          */
1687
1688         if (totblocks != 0)  {
1689                 switch (type)  {
1690                 case XR_INO_CHRDEV:
1691                         do_warn(
1692 _("size of character device inode %" PRIu64 " != 0 (%" PRIu64 " blocks)\n"),
1693                                 lino, totblocks);
1694                         break;
1695                 case XR_INO_BLKDEV:
1696                         do_warn(
1697 _("size of block device inode %" PRIu64 " != 0 (%" PRIu64 " blocks)\n"),
1698                                 lino, totblocks);
1699                         break;
1700                 case XR_INO_SOCK:
1701                         do_warn(
1702 _("size of socket inode %" PRIu64 " != 0 (%" PRIu64 " blocks)\n"),
1703                                 lino, totblocks);
1704                         break;
1705                 case XR_INO_FIFO:
1706                         do_warn(
1707 _("size of fifo inode %" PRIu64 " != 0 (%" PRIu64 " blocks)\n"),
1708                                 lino, totblocks);
1709                         break;
1710                 default:
1711                         return(0);
1712                 }
1713                 return(1);
1714         }
1715         return (0);
1716 }
1717
1718 static inline int
1719 dinode_fmt(
1720         xfs_dinode_t *dino)
1721 {
1722         return be16_to_cpu(dino->di_mode) & S_IFMT;
1723 }
1724
1725 static inline void
1726 change_dinode_fmt(
1727         xfs_dinode_t    *dino,
1728         int             new_fmt)
1729 {
1730         int             mode = be16_to_cpu(dino->di_mode);
1731
1732         ASSERT((new_fmt & ~S_IFMT) == 0);
1733
1734         mode &= ~S_IFMT;
1735         mode |= new_fmt;
1736         dino->di_mode = cpu_to_be16(mode);
1737 }
1738
1739 static int
1740 check_dinode_mode_format(
1741         xfs_dinode_t *dinoc)
1742 {
1743         if (dinoc->di_format >= XFS_DINODE_FMT_UUID)
1744                 return -1;      /* FMT_UUID is not used */
1745
1746         switch (dinode_fmt(dinoc)) {
1747         case S_IFIFO:
1748         case S_IFCHR:
1749         case S_IFBLK:
1750         case S_IFSOCK:
1751                 return (dinoc->di_format != XFS_DINODE_FMT_DEV) ? -1 : 0;
1752
1753         case S_IFDIR:
1754                 return (dinoc->di_format < XFS_DINODE_FMT_LOCAL ||
1755                         dinoc->di_format > XFS_DINODE_FMT_BTREE) ? -1 : 0;
1756
1757         case S_IFREG:
1758                 return (dinoc->di_format < XFS_DINODE_FMT_EXTENTS ||
1759                         dinoc->di_format > XFS_DINODE_FMT_BTREE) ? -1 : 0;
1760
1761         case S_IFLNK:
1762                 return (dinoc->di_format < XFS_DINODE_FMT_LOCAL ||
1763                         dinoc->di_format > XFS_DINODE_FMT_EXTENTS) ? -1 : 0;
1764
1765         default: ;
1766         }
1767         return 0;       /* invalid modes are checked elsewhere */
1768 }
1769
1770 /*
1771  * If inode is a superblock inode, does type check to make sure is it valid.
1772  * Returns 0 if it's valid, non-zero if it needs to be cleared.
1773  */
1774
1775 static int
1776 process_check_sb_inodes(
1777         xfs_mount_t     *mp,
1778         xfs_dinode_t    *dinoc,
1779         xfs_ino_t       lino,
1780         int             *type,
1781         int             *dirty)
1782 {
1783         if (lino == mp->m_sb.sb_rootino) {
1784                 if (*type != XR_INO_DIR)  {
1785                         do_warn(_("root inode %" PRIu64 " has bad type 0x%x\n"),
1786                                 lino, dinode_fmt(dinoc));
1787                         *type = XR_INO_DIR;
1788                         if (!no_modify)  {
1789                                 do_warn(_("resetting to directory\n"));
1790                                 change_dinode_fmt(dinoc, S_IFDIR);
1791                                 *dirty = 1;
1792                         } else
1793                                 do_warn(_("would reset to directory\n"));
1794                 }
1795                 return 0;
1796         }
1797         if (lino == mp->m_sb.sb_uquotino)  {
1798                 if (*type != XR_INO_DATA)  {
1799                         do_warn(_("user quota inode %" PRIu64 " has bad type 0x%x\n"),
1800                                 lino, dinode_fmt(dinoc));
1801                         mp->m_sb.sb_uquotino = NULLFSINO;
1802                         return 1;
1803                 }
1804                 return 0;
1805         }
1806         if (lino == mp->m_sb.sb_gquotino)  {
1807                 if (*type != XR_INO_DATA)  {
1808                         do_warn(_("group quota inode %" PRIu64 " has bad type 0x%x\n"),
1809                                 lino, dinode_fmt(dinoc));
1810                         mp->m_sb.sb_gquotino = NULLFSINO;
1811                         return 1;
1812                 }
1813                 return 0;
1814         }
1815         if (lino == mp->m_sb.sb_rsumino) {
1816                 if (*type != XR_INO_RTSUM) {
1817                         do_warn(
1818 _("realtime summary inode %" PRIu64 " has bad type 0x%x, "),
1819                                 lino, dinode_fmt(dinoc));
1820                         if (!no_modify)  {
1821                                 do_warn(_("resetting to regular file\n"));
1822                                 change_dinode_fmt(dinoc, S_IFREG);
1823                                 *dirty = 1;
1824                         } else  {
1825                                 do_warn(_("would reset to regular file\n"));
1826                         }
1827                 }
1828                 if (mp->m_sb.sb_rblocks == 0 && dinoc->di_nextents != 0)  {
1829                         do_warn(
1830 _("bad # of extents (%u) for realtime summary inode %" PRIu64 "\n"),
1831                                 be32_to_cpu(dinoc->di_nextents), lino);
1832                         return 1;
1833                 }
1834                 return 0;
1835         }
1836         if (lino == mp->m_sb.sb_rbmino) {
1837                 if (*type != XR_INO_RTBITMAP) {
1838                         do_warn(
1839 _("realtime bitmap inode %" PRIu64 " has bad type 0x%x, "),
1840                                 lino, dinode_fmt(dinoc));
1841                         if (!no_modify)  {
1842                                 do_warn(_("resetting to regular file\n"));
1843                                 change_dinode_fmt(dinoc, S_IFREG);
1844                                 *dirty = 1;
1845                         } else  {
1846                                 do_warn(_("would reset to regular file\n"));
1847                         }
1848                 }
1849                 if (mp->m_sb.sb_rblocks == 0 && dinoc->di_nextents != 0)  {
1850                         do_warn(
1851 _("bad # of extents (%u) for realtime bitmap inode %" PRIu64 "\n"),
1852                                 be32_to_cpu(dinoc->di_nextents), lino);
1853                         return 1;
1854                 }
1855                 return 0;
1856         }
1857         return 0;
1858 }
1859
1860 /*
1861  * general size/consistency checks:
1862  *
1863  * if the size <= size of the data fork, directories  must be
1864  * local inodes unlike regular files which would be extent inodes.
1865  * all the other mentioned types have to have a zero size value.
1866  *
1867  * if the size and format don't match, get out now rather than
1868  * risk trying to process a non-existent extents or btree
1869  * type data fork.
1870  */
1871 static int
1872 process_check_inode_sizes(
1873         xfs_mount_t     *mp,
1874         xfs_dinode_t    *dino,
1875         xfs_ino_t       lino,
1876         int             type)
1877 {
1878         xfs_fsize_t     size = be64_to_cpu(dino->di_size);
1879
1880         switch (type)  {
1881
1882         case XR_INO_DIR:
1883                 if (size <= XFS_DFORK_DSIZE(dino, mp) &&
1884                                 dino->di_format != XFS_DINODE_FMT_LOCAL) {
1885                         do_warn(
1886 _("mismatch between format (%d) and size (%" PRId64 ") in directory ino %" PRIu64 "\n"),
1887                                 dino->di_format, size, lino);
1888                         return 1;
1889                 }
1890                 if (size > XFS_DIR2_LEAF_OFFSET) {
1891                         do_warn(
1892 _("directory inode %" PRIu64 " has bad size %" PRId64 "\n"),
1893                                 lino, size);
1894                         return 1;
1895                 }
1896                 break;
1897
1898         case XR_INO_SYMLINK:
1899                 if (process_symlink_extlist(mp, lino, dino))  {
1900                         do_warn(_("bad data fork in symlink %" PRIu64 "\n"), lino);
1901                         return 1;
1902                 }
1903                 break;
1904
1905         case XR_INO_CHRDEV:     /* fall through to FIFO case ... */
1906         case XR_INO_BLKDEV:     /* fall through to FIFO case ... */
1907         case XR_INO_SOCK:       /* fall through to FIFO case ... */
1908         case XR_INO_MOUNTPOINT: /* fall through to FIFO case ... */
1909         case XR_INO_FIFO:
1910                 if (process_misc_ino_types(mp, dino, lino, type))
1911                         return 1;
1912                 break;
1913
1914         case XR_INO_RTDATA:
1915                 /*
1916                  * if we have no realtime blocks, any inode claiming
1917                  * to be a real-time file is bogus
1918                  */
1919                 if (mp->m_sb.sb_rblocks == 0)  {
1920                         do_warn(
1921 _("found inode %" PRIu64 " claiming to be a real-time file\n"), lino);
1922                         return 1;
1923                 }
1924                 break;
1925
1926         case XR_INO_RTBITMAP:
1927                 if (size != (__int64_t)mp->m_sb.sb_rbmblocks *
1928                                         mp->m_sb.sb_blocksize) {
1929                         do_warn(
1930 _("realtime bitmap inode %" PRIu64 " has bad size %" PRId64 " (should be %" PRIu64 ")\n"),
1931                                 lino, size,
1932                                 (__int64_t) mp->m_sb.sb_rbmblocks *
1933                                         mp->m_sb.sb_blocksize);
1934                         return 1;
1935                 }
1936                 break;
1937
1938         case XR_INO_RTSUM:
1939                 if (size != mp->m_rsumsize)  {
1940                         do_warn(
1941 _("realtime summary inode %" PRIu64 " has bad size %" PRId64 " (should be %d)\n"),
1942                                 lino, size, mp->m_rsumsize);
1943                         return 1;
1944                 }
1945                 break;
1946
1947         default:
1948                 break;
1949         }
1950         return 0;
1951 }
1952
1953 /*
1954  * check for illegal values of forkoff
1955  */
1956 static int
1957 process_check_inode_forkoff(
1958         xfs_mount_t     *mp,
1959         xfs_dinode_t    *dino,
1960         xfs_ino_t       lino)
1961 {
1962         if (dino->di_forkoff == 0)
1963                 return 0;
1964
1965         switch (dino->di_format)  {
1966         case XFS_DINODE_FMT_DEV:
1967                 if (dino->di_forkoff != (roundup(sizeof(xfs_dev_t), 8) >> 3)) {
1968                         do_warn(
1969 _("bad attr fork offset %d in dev inode %" PRIu64 ", should be %d\n"),
1970                                 dino->di_forkoff, lino,
1971                                 (int)(roundup(sizeof(xfs_dev_t), 8) >> 3));
1972                         return 1;
1973                 }
1974                 break;
1975         case XFS_DINODE_FMT_LOCAL:      /* fall through ... */
1976         case XFS_DINODE_FMT_EXTENTS:    /* fall through ... */
1977         case XFS_DINODE_FMT_BTREE:
1978                 if (dino->di_forkoff >=
1979                                 (XFS_LITINO(mp, dino->di_version) >> 3)) {
1980                         do_warn(
1981 _("bad attr fork offset %d in inode %" PRIu64 ", max=%d\n"),
1982                                 dino->di_forkoff, lino,
1983                                 XFS_LITINO(mp, dino->di_version) >> 3);
1984                         return 1;
1985                 }
1986                 break;
1987         default:
1988                 do_error(_("unexpected inode format %d\n"), dino->di_format);
1989                 break;
1990         }
1991         return 0;
1992 }
1993
1994 /*
1995  * Updates the inodes block and extent counts if they are wrong
1996  */
1997 static int
1998 process_inode_blocks_and_extents(
1999         xfs_dinode_t    *dino,
2000         xfs_drfsbno_t   nblocks,
2001         __uint64_t      nextents,
2002         __uint64_t      anextents,
2003         xfs_ino_t       lino,
2004         int             *dirty)
2005 {
2006         if (nblocks != be64_to_cpu(dino->di_nblocks))  {
2007                 if (!no_modify)  {
2008                         do_warn(
2009 _("correcting nblocks for inode %" PRIu64 ", was %llu - counted %" PRIu64 "\n"), lino,
2010                                (unsigned long long) be64_to_cpu(dino->di_nblocks),
2011                                nblocks);
2012                         dino->di_nblocks = cpu_to_be64(nblocks);
2013                         *dirty = 1;
2014                 } else  {
2015                         do_warn(
2016 _("bad nblocks %llu for inode %" PRIu64 ", would reset to %" PRIu64 "\n"),
2017                                (unsigned long long) be64_to_cpu(dino->di_nblocks),
2018                                lino, nblocks);
2019                 }
2020         }
2021
2022         if (nextents > MAXEXTNUM)  {
2023                 do_warn(
2024 _("too many data fork extents (%" PRIu64 ") in inode %" PRIu64 "\n"),
2025                         nextents, lino);
2026                 return 1;
2027         }
2028         if (nextents != be32_to_cpu(dino->di_nextents))  {
2029                 if (!no_modify)  {
2030                         do_warn(
2031 _("correcting nextents for inode %" PRIu64 ", was %d - counted %" PRIu64 "\n"),
2032                                 lino,
2033                                 be32_to_cpu(dino->di_nextents),
2034                                 nextents);
2035                         dino->di_nextents = cpu_to_be32(nextents);
2036                         *dirty = 1;
2037                 } else  {
2038                         do_warn(
2039 _("bad nextents %d for inode %" PRIu64 ", would reset to %" PRIu64 "\n"),
2040                                 be32_to_cpu(dino->di_nextents),
2041                                 lino, nextents);
2042                 }
2043         }
2044
2045         if (anextents > MAXAEXTNUM)  {
2046                 do_warn(
2047 _("too many attr fork extents (%" PRIu64 ") in inode %" PRIu64 "\n"),
2048                         anextents, lino);
2049                 return 1;
2050         }
2051         if (anextents != be16_to_cpu(dino->di_anextents))  {
2052                 if (!no_modify)  {
2053                         do_warn(
2054 _("correcting anextents for inode %" PRIu64 ", was %d - counted %" PRIu64 "\n"),
2055                                 lino,
2056                                 be16_to_cpu(dino->di_anextents), anextents);
2057                         dino->di_anextents = cpu_to_be16(anextents);
2058                         *dirty = 1;
2059                 } else  {
2060                         do_warn(
2061 _("bad anextents %d for inode %" PRIu64 ", would reset to %" PRIu64 "\n"),
2062                                 be16_to_cpu(dino->di_anextents),
2063                                 lino, anextents);
2064                 }
2065         }
2066
2067         /*
2068          * We are comparing different units here, but that's fine given that
2069          * an extent has to have at least a block in it.
2070          */
2071         if (nblocks < nextents + anextents) {
2072                 do_warn(
2073 _("nblocks (%" PRIu64 ") smaller than nextents for inode %" PRIu64 "\n"), nblocks, lino);
2074                 return 1;
2075         }
2076
2077         return 0;
2078 }
2079
2080 /*
2081  * check data fork -- if it's bad, clear the inode
2082  */
2083 static int
2084 process_inode_data_fork(
2085         xfs_mount_t     *mp,
2086         xfs_agnumber_t  agno,
2087         xfs_agino_t     ino,
2088         xfs_dinode_t    *dino,
2089         int             type,
2090         int             *dirty,
2091         xfs_drfsbno_t   *totblocks,
2092         __uint64_t      *nextents,
2093         blkmap_t        **dblkmap,
2094         int             check_dups)
2095 {
2096         xfs_ino_t       lino = XFS_AGINO_TO_INO(mp, agno, ino);
2097         int             err = 0;
2098         int             nex;
2099
2100         /*
2101          * extent count on disk is only valid for positive values. The kernel
2102          * uses negative values in memory. hence if we see negative numbers
2103          * here, trash it!
2104          */
2105         nex = be32_to_cpu(dino->di_nextents);
2106         if (nex < 0)
2107                 *nextents = 1;
2108         else
2109                 *nextents = nex;
2110
2111         if (*nextents > be64_to_cpu(dino->di_nblocks))
2112                 *nextents = 1;
2113
2114
2115         if (dino->di_format != XFS_DINODE_FMT_LOCAL && type != XR_INO_RTDATA)
2116                 *dblkmap = blkmap_alloc(*nextents, XFS_DATA_FORK);
2117         *nextents = 0;
2118
2119         switch (dino->di_format) {
2120         case XFS_DINODE_FMT_LOCAL:
2121                 err = process_lclinode(mp, agno, ino, dino, XFS_DATA_FORK);
2122                 *totblocks = 0;
2123                 break;
2124         case XFS_DINODE_FMT_EXTENTS:
2125                 err = process_exinode(mp, agno, ino, dino, type, dirty,
2126                         totblocks, nextents, dblkmap, XFS_DATA_FORK,
2127                         check_dups);
2128                 break;
2129         case XFS_DINODE_FMT_BTREE:
2130                 err = process_btinode(mp, agno, ino, dino, type, dirty,
2131                         totblocks, nextents, dblkmap, XFS_DATA_FORK,
2132                         check_dups);
2133                 break;
2134         case XFS_DINODE_FMT_DEV:        /* fall through */
2135                 err = 0;
2136                 break;
2137         default:
2138                 do_error(_("unknown format %d, ino %" PRIu64 " (mode = %d)\n"),
2139                         dino->di_format, lino, be16_to_cpu(dino->di_mode));
2140         }
2141
2142         if (err)  {
2143                 do_warn(_("bad data fork in inode %" PRIu64 "\n"), lino);
2144                 if (!no_modify)  {
2145                         *dirty += clear_dinode(mp, dino, lino);
2146                         ASSERT(*dirty > 0);
2147                 }
2148                 return 1;
2149         }
2150
2151         if (check_dups)  {
2152                 /*
2153                  * if check_dups was non-zero, we have to
2154                  * re-process data fork to set bitmap since the
2155                  * bitmap wasn't set the first time through
2156                  */
2157                 switch (dino->di_format) {
2158                 case XFS_DINODE_FMT_LOCAL:
2159                         err = process_lclinode(mp, agno, ino, dino, 
2160                                                 XFS_DATA_FORK);
2161                         break;
2162                 case XFS_DINODE_FMT_EXTENTS:
2163                         err = process_exinode(mp, agno, ino, dino, type,
2164                                 dirty, totblocks, nextents, dblkmap,
2165                                 XFS_DATA_FORK, 0);
2166                         break;
2167                 case XFS_DINODE_FMT_BTREE:
2168                         err = process_btinode(mp, agno, ino, dino, type,
2169                                 dirty, totblocks, nextents, dblkmap,
2170                                 XFS_DATA_FORK, 0);
2171                         break;
2172                 case XFS_DINODE_FMT_DEV:        /* fall through */
2173                         err = 0;
2174                         break;
2175                 default:
2176                         do_error(_("unknown format %d, ino %" PRIu64 " (mode = %d)\n"),
2177                                 dino->di_format, lino,
2178                                 be16_to_cpu(dino->di_mode));
2179                 }
2180
2181                 if (no_modify && err != 0)
2182                         return 1;
2183
2184                 ASSERT(err == 0);
2185         }
2186         return 0;
2187 }
2188
2189 /*
2190  * Process extended attribute fork in inode
2191  */
2192 static int
2193 process_inode_attr_fork(
2194         xfs_mount_t     *mp,
2195         xfs_agnumber_t  agno,
2196         xfs_agino_t     ino,
2197         xfs_dinode_t    *dino,
2198         int             type,
2199         int             *dirty,
2200         xfs_drfsbno_t   *atotblocks,
2201         __uint64_t      *anextents,
2202         int             check_dups,
2203         int             extra_attr_check,
2204         int             *retval)
2205 {
2206         xfs_ino_t       lino = XFS_AGINO_TO_INO(mp, agno, ino);
2207         blkmap_t        *ablkmap = NULL;
2208         int             repair = 0;
2209         int             err;
2210
2211         if (!XFS_DFORK_Q(dino)) {
2212                 *anextents = 0;
2213                 if (dino->di_aformat != XFS_DINODE_FMT_EXTENTS) {
2214                         do_warn(_("bad attribute format %d in inode %" PRIu64 ", "),
2215                                 dino->di_aformat, lino);
2216                         if (!no_modify) {
2217                                 do_warn(_("resetting value\n"));
2218                                 dino->di_aformat = XFS_DINODE_FMT_EXTENTS;
2219                                 *dirty = 1;
2220                         } else
2221                                 do_warn(_("would reset value\n"));
2222                 }
2223                 return 0;
2224         }
2225
2226         *anextents = be16_to_cpu(dino->di_anextents);
2227         if (*anextents > be64_to_cpu(dino->di_nblocks))
2228                 *anextents = 1;
2229
2230         switch (dino->di_aformat) {
2231         case XFS_DINODE_FMT_LOCAL:
2232                 *anextents = 0;
2233                 *atotblocks = 0;
2234                 err = process_lclinode(mp, agno, ino, dino, XFS_ATTR_FORK);
2235                 break;
2236         case XFS_DINODE_FMT_EXTENTS:
2237                 ablkmap = blkmap_alloc(*anextents, XFS_ATTR_FORK);
2238                 *anextents = 0;
2239                 err = process_exinode(mp, agno, ino, dino, type, dirty,
2240                                 atotblocks, anextents, &ablkmap,
2241                                 XFS_ATTR_FORK, check_dups);
2242                 break;
2243         case XFS_DINODE_FMT_BTREE:
2244                 ablkmap = blkmap_alloc(*anextents, XFS_ATTR_FORK);
2245                 *anextents = 0;
2246                 err = process_btinode(mp, agno, ino, dino, type, dirty,
2247                                 atotblocks, anextents, &ablkmap,
2248                                 XFS_ATTR_FORK, check_dups);
2249                 break;
2250         default:
2251                 do_warn(_("illegal attribute format %d, ino %" PRIu64 "\n"),
2252                                 dino->di_aformat, lino);
2253                 err = 1;
2254                 break;
2255         }
2256
2257         if (err) {
2258                 /*
2259                  * clear the attribute fork if necessary.  we can't
2260                  * clear the inode because we've already put the
2261                  * inode space info into the blockmap.
2262                  *
2263                  * XXX - put the inode onto the "move it" list and
2264                  *      log the the attribute scrubbing
2265                  */
2266                 do_warn(_("bad attribute fork in inode %" PRIu64), lino);
2267
2268                 if (!no_modify)  {
2269                         if (delete_attr_ok)  {
2270                                 do_warn(_(", clearing attr fork\n"));
2271                                 *dirty += clear_dinode_attr(mp, dino, lino);
2272                                 dino->di_aformat = XFS_DINODE_FMT_LOCAL;
2273                         } else  {
2274                                 do_warn("\n");
2275                                 *dirty += clear_dinode(mp, dino, lino);
2276                         }
2277                         ASSERT(*dirty > 0);
2278                 } else  {
2279                         do_warn(_(", would clear attr fork\n"));
2280                 }
2281
2282                 *atotblocks = 0;
2283                 *anextents = 0;
2284                 blkmap_free(ablkmap);
2285                 *retval = 1;
2286
2287                 return delete_attr_ok ? 0 : 1;
2288         }
2289
2290         if (check_dups)  {
2291                 switch (dino->di_aformat) {
2292                 case XFS_DINODE_FMT_LOCAL:
2293                         err = process_lclinode(mp, agno, ino, dino, 
2294                                                 XFS_ATTR_FORK);
2295                         break;
2296                 case XFS_DINODE_FMT_EXTENTS:
2297                         err = process_exinode(mp, agno, ino, dino,
2298                                 type, dirty, atotblocks, anextents,
2299                                 &ablkmap, XFS_ATTR_FORK, 0);
2300                         break;
2301                 case XFS_DINODE_FMT_BTREE:
2302                         err = process_btinode(mp, agno, ino, dino,
2303                                 type, dirty, atotblocks, anextents,
2304                                 &ablkmap, XFS_ATTR_FORK, 0);
2305                         break;
2306                 default:
2307                         do_error(_("illegal attribute fmt %d, ino %" PRIu64 "\n"),
2308                                 dino->di_aformat, lino);
2309                 }
2310
2311                 if (no_modify && err != 0) {
2312                         blkmap_free(ablkmap);
2313                         return 1;
2314                 }
2315
2316                 ASSERT(err == 0);
2317         }
2318
2319         /*
2320          * do attribute semantic-based consistency checks now
2321          */
2322
2323         /* get this only in phase 3, not in both phase 3 and 4 */
2324         if (extra_attr_check &&
2325                         process_attributes(mp, lino, dino, ablkmap, &repair)) {
2326                 do_warn(
2327         _("problem with attribute contents in inode %" PRIu64 "\n"),
2328                         lino);
2329                 if (!repair) {
2330                         /* clear attributes if not done already */
2331                         if (!no_modify)  {
2332                                 *dirty += clear_dinode_attr(mp, dino, lino);
2333                                 dino->di_aformat = XFS_DINODE_FMT_LOCAL;
2334                         } else  {
2335                                 do_warn(_("would clear attr fork\n"));
2336                         }
2337                         *atotblocks = 0;
2338                         *anextents = 0;
2339                 }
2340                 else {
2341                         *dirty = 1; /* it's been repaired */
2342                 }
2343         }
2344         blkmap_free(ablkmap);
2345         return 0;
2346 }
2347
2348 /*
2349  * check nlinks feature, if it's a version 1 inode,
2350  * just leave nlinks alone.  even if it's set wrong,
2351  * it'll be reset when read in.
2352  */
2353
2354 static int
2355 process_check_inode_nlink_version(
2356         xfs_dinode_t    *dino,
2357         xfs_ino_t       lino)
2358 {
2359         int             dirty = 0;
2360
2361         if (dino->di_version > 1 && !fs_inode_nlink)  {
2362                 /*
2363                  * do we have a fs/inode version mismatch with a valid
2364                  * version 2 inode here that has to stay version 2 or
2365                  * lose links?
2366                  */
2367                 if (be32_to_cpu(dino->di_nlink) > XFS_MAXLINK_1)  {
2368                         /*
2369                          * yes.  are nlink inodes allowed?
2370                          */
2371                         if (fs_inode_nlink_allowed)  {
2372                                 /*
2373                                  * yes, update status variable which will
2374                                  * cause sb to be updated later.
2375                                  */
2376                                 fs_inode_nlink = 1;
2377                                 do_warn
2378         (_("version 2 inode %" PRIu64 " claims > %u links, "),
2379                                         lino, XFS_MAXLINK_1);
2380                                 if (!no_modify)  {
2381                                         do_warn(
2382         _("updating superblock version number\n"));
2383                                 } else  {
2384                                         do_warn(
2385         _("would update superblock version number\n"));
2386                                 }
2387                         } else  {
2388                                 /*
2389                                  * no, have to convert back to onlinks
2390                                  * even if we lose some links
2391                                  */
2392                                 do_warn(
2393         _("WARNING:  version 2 inode %" PRIu64 " claims > %u links, "),
2394                                         lino, XFS_MAXLINK_1);
2395                                 if (!no_modify)  {
2396                                         do_warn(_("converting back to version 1,\n"
2397                                                 "this may destroy %d links\n"),
2398                                                 be32_to_cpu(dino->di_nlink) -
2399                                                         XFS_MAXLINK_1);
2400
2401                                         dino->di_version = 1;
2402                                         dino->di_nlink = cpu_to_be32(XFS_MAXLINK_1);
2403                                         dino->di_onlink = cpu_to_be16(XFS_MAXLINK_1);
2404                                         dirty = 1;
2405                                 } else  {
2406                                         do_warn(_("would convert back to version 1,\n"
2407                                                 "\tthis might destroy %d links\n"),
2408                                                 be32_to_cpu(dino->di_nlink) -
2409                                                         XFS_MAXLINK_1);
2410                                 }
2411                         }
2412                 } else  {
2413                         /*
2414                          * do we have a v2 inode that we could convert back
2415                          * to v1 without losing any links?  if we do and
2416                          * we have a mismatch between superblock bits and the
2417                          * version bit, alter the version bit in this case.
2418                          *
2419                          * the case where we lost links was handled above.
2420                          */
2421                         do_warn(_("found version 2 inode %" PRIu64 ", "), lino);
2422                         if (!no_modify)  {
2423                                 do_warn(_("converting back to version 1\n"));
2424                                 dino->di_version = 1;
2425                                 dino->di_onlink = cpu_to_be16(
2426                                         be32_to_cpu(dino->di_nlink));
2427                                 dirty = 1;
2428                         } else  {
2429                                 do_warn(_("would convert back to version 1\n"));
2430                         }
2431                 }
2432         }
2433
2434         /*
2435          * ok, if it's still a version 2 inode, it's going
2436          * to stay a version 2 inode.  it should have a zero
2437          * onlink field, so clear it.
2438          */
2439         if (dino->di_version > 1 &&
2440                         dino->di_onlink != 0 && fs_inode_nlink > 0) {
2441                 if (!no_modify) {
2442                         do_warn(
2443 _("clearing obsolete nlink field in version 2 inode %" PRIu64 ", was %d, now 0\n"),
2444                                 lino, be16_to_cpu(dino->di_onlink));
2445                         dino->di_onlink = 0;
2446                         dirty = 1;
2447                 } else  {
2448                         do_warn(
2449 _("would clear obsolete nlink field in version 2 inode %" PRIu64 ", currently %d\n"),
2450                                 lino, be16_to_cpu(dino->di_onlink));
2451                 }
2452         }
2453         return dirty;
2454 }
2455
2456 /*
2457  * returns 0 if the inode is ok, 1 if the inode is corrupt
2458  * check_dups can be set to 1 *only* when called by the
2459  * first pass of the duplicate block checking of phase 4.
2460  * *dirty is set > 0 if the dinode has been altered and
2461  * needs to be written out.
2462  *
2463  * for detailed, info, look at process_dinode() comments.
2464  */
2465 static int
2466 process_dinode_int(xfs_mount_t *mp,
2467                 xfs_dinode_t *dino,
2468                 xfs_agnumber_t agno,
2469                 xfs_agino_t ino,
2470                 int was_free,           /* 1 if inode is currently free */
2471                 int *dirty,             /* out == > 0 if inode is now dirty */
2472                 int *used,              /* out == 1 if inode is in use */
2473                 int verify_mode,        /* 1 == verify but don't modify inode */
2474                 int uncertain,          /* 1 == inode is uncertain */
2475                 int ino_discovery,      /* 1 == check dirs for unknown inodes */
2476                 int check_dups,         /* 1 == check if inode claims
2477                                          * duplicate blocks             */
2478                 int extra_attr_check, /* 1 == do attribute format and value checks */
2479                 int *isa_dir,           /* out == 1 if inode is a directory */
2480                 xfs_ino_t *parent)      /* out -- parent if ino is a dir */
2481 {
2482         xfs_drfsbno_t           totblocks = 0;
2483         xfs_drfsbno_t           atotblocks = 0;
2484         int                     di_mode;
2485         int                     type;
2486         int                     retval = 0;
2487         __uint64_t              nextents;
2488         __uint64_t              anextents;
2489         xfs_ino_t               lino;
2490         const int               is_free = 0;
2491         const int               is_used = 1;
2492         blkmap_t                *dblkmap = NULL;
2493
2494         *dirty = *isa_dir = 0;
2495         *used = is_used;
2496         type = XR_INO_UNKNOWN;
2497
2498         lino = XFS_AGINO_TO_INO(mp, agno, ino);
2499         di_mode = be16_to_cpu(dino->di_mode);
2500
2501         /*
2502          * if in verify mode, don't modify the inode.
2503          *
2504          * if correcting, reset stuff that has known values
2505          *
2506          * if in uncertain mode, be silent on errors since we're
2507          * trying to find out if these are inodes as opposed
2508          * to assuming that they are.  Just return the appropriate
2509          * return code in that case.
2510          *
2511          * If uncertain is set, verify_mode MUST be set.
2512          */
2513         ASSERT(uncertain == 0 || verify_mode != 0);
2514
2515         if (be16_to_cpu(dino->di_magic) != XFS_DINODE_MAGIC)  {
2516                 retval = 1;
2517                 if (!uncertain)
2518                         do_warn(_("bad magic number 0x%x on inode %" PRIu64 "%c"),
2519                                 be16_to_cpu(dino->di_magic), lino,
2520                                 verify_mode ? '\n' : ',');
2521                 if (!verify_mode) {
2522                         if (!no_modify)  {
2523                                 do_warn(_(" resetting magic number\n"));
2524                                 dino->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
2525                                 *dirty = 1;
2526                         } else
2527                                 do_warn(_(" would reset magic number\n"));
2528                 }
2529         }
2530
2531         if (!XFS_DINODE_GOOD_VERSION(dino->di_version) ||
2532             (!fs_inode_nlink && dino->di_version > 1) ||
2533             (xfs_sb_version_hascrc(&mp->m_sb) && dino->di_version < 3) )  {
2534                 retval = 1;
2535                 if (!uncertain)
2536                         do_warn(_("bad version number 0x%x on inode %" PRIu64 "%c"),
2537                                 (__s8)dino->di_version, lino,
2538                                 verify_mode ? '\n' : ',');
2539                 if (!verify_mode) {
2540                         if (!no_modify) {
2541                                 do_warn(_(" resetting version number\n"));
2542                                 dino->di_version =
2543                                         xfs_sb_version_hascrc(&mp->m_sb) ? 3 :
2544                                         (fs_inode_nlink) ?  2 : 1;
2545                                 *dirty = 1;
2546                         } else
2547                                 do_warn(_(" would reset version number\n"));
2548                 }
2549         }
2550
2551         /*
2552          * We don't bother checking the CRC here - we cannot guarantee that when
2553          * we are called here that the inode has not already been modified in
2554          * memory and hence invalidated the CRC.
2555          */
2556         if (xfs_sb_version_hascrc(&mp->m_sb)) {
2557                 if (be64_to_cpu(dino->di_ino) != lino) {
2558                         if (!uncertain)
2559                                 do_warn(
2560 _("inode identifier %llu mismatch on inode %" PRIu64 "\n"),
2561                                         be64_to_cpu(dino->di_ino), lino);
2562                         if (verify_mode)
2563                                 return 1;
2564                         goto clear_bad_out;
2565                 }
2566                 if (platform_uuid_compare(&dino->di_uuid, &mp->m_sb.sb_uuid)) {
2567                         if (!uncertain)
2568                                 do_warn(
2569                         _("UUID mismatch on inode %" PRIu64 "\n"), lino);
2570                         if (verify_mode)
2571                                 return 1;
2572                         goto clear_bad_out;
2573                 }
2574         }
2575
2576         /*
2577          * blow out of here if the inode size is < 0
2578          */
2579         if ((xfs_fsize_t)be64_to_cpu(dino->di_size) < 0)  {
2580                 if (!uncertain)
2581                         do_warn(
2582 _("bad (negative) size %" PRId64 " on inode %" PRIu64 "\n"),
2583                                 (__int64_t)be64_to_cpu(dino->di_size),
2584                                 lino);
2585                 if (verify_mode)
2586                         return 1;
2587                 goto clear_bad_out;
2588         }
2589
2590         /*
2591          * if not in verify mode, check to sii if the inode and imap
2592          * agree that the inode is free
2593          */
2594         if (!verify_mode && di_mode == 0) {
2595                 /*
2596                  * was_free value is not meaningful if we're in verify mode
2597                  */
2598                 if (was_free) {
2599                         /*
2600                          * easy case, inode free -- inode and map agree, clear
2601                          * it just in case to ensure that format, etc. are
2602                          * set correctly
2603                          */
2604                         if (!no_modify)
2605                                 *dirty += clear_dinode(mp, dino, lino);
2606                         *used = is_free;
2607                         return 0;
2608                 }
2609                 /*
2610                  * the inode looks free but the map says it's in use.
2611                  * clear the inode just to be safe and mark the inode
2612                  * free.
2613                  */
2614                 do_warn(
2615         _("imap claims a free inode %" PRIu64 " is in use, "), lino);
2616                 if (!no_modify)  {
2617                         do_warn(_("correcting imap and clearing inode\n"));
2618                         *dirty += clear_dinode(mp, dino, lino);
2619                         retval = 1;
2620                 } else
2621                         do_warn(_("would correct imap and clear inode\n"));
2622                 *used = is_free;
2623                 return retval;
2624         }
2625
2626         /*
2627          * because of the lack of any write ordering guarantee, it's
2628          * possible that the core got updated but the forks didn't.
2629          * so rather than be ambitious (and probably incorrect),
2630          * if there's an inconsistency, we get conservative and
2631          * just pitch the file.  blow off checking formats of
2632          * free inodes since technically any format is legal
2633          * as we reset the inode when we re-use it.
2634          */
2635         if (di_mode != 0 && check_dinode_mode_format(dino) != 0) {
2636                 if (!uncertain)
2637                         do_warn(
2638         _("bad inode format in inode %" PRIu64 "\n"), lino);
2639                 if (verify_mode)
2640                         return 1;
2641                 goto clear_bad_out;
2642         }
2643
2644         /*
2645          * check that we only have valid flags set, and those that are set make
2646          * sense.
2647          */
2648         if (dino->di_flags) {
2649                 uint16_t flags = be16_to_cpu(dino->di_flags);
2650
2651                 if (flags & ~XFS_DIFLAG_ANY) {
2652                         if (!uncertain) {
2653                                 do_warn(
2654         _("Bad flags set in inode %" PRIu64 "\n"),
2655                                         lino);
2656                         }
2657                         flags &= ~XFS_DIFLAG_ANY;
2658                 }
2659
2660                 if (flags & (XFS_DIFLAG_REALTIME | XFS_DIFLAG_RTINHERIT)) {
2661                         /* need an rt-dev! */
2662                         if (!rt_name) {
2663                                 if (!uncertain) {
2664                                         do_warn(
2665         _("inode %" PRIu64 " has RT flag set but there is no RT device\n"),
2666                                                 lino);
2667                                 }
2668                                 flags &= ~(XFS_DIFLAG_REALTIME |
2669                                                 XFS_DIFLAG_RTINHERIT);
2670                         }
2671                 }
2672                 if (flags & XFS_DIFLAG_NEWRTBM) {
2673                         /* must be a rt bitmap inode */
2674                         if (lino != mp->m_sb.sb_rbmino) {
2675                                 if (!uncertain) {
2676                                         do_warn(
2677         _("inode %" PRIu64 " not rt bitmap\n"),
2678                                                 lino);
2679                                 }
2680                                 flags &= ~XFS_DIFLAG_NEWRTBM;
2681                         }
2682                 }
2683                 if (flags & (XFS_DIFLAG_RTINHERIT |
2684                              XFS_DIFLAG_EXTSZINHERIT |
2685                              XFS_DIFLAG_PROJINHERIT |
2686                              XFS_DIFLAG_NOSYMLINKS)) {
2687                         /* must be a directory */
2688                         if (di_mode && !S_ISDIR(di_mode)) {
2689                                 if (!uncertain) {
2690                                         do_warn(
2691         _("directory flags set on non-directory inode %" PRIu64 "\n" ),
2692                                                 lino);
2693                                 }
2694                                 flags &= ~(XFS_DIFLAG_RTINHERIT |
2695                                                 XFS_DIFLAG_EXTSZINHERIT |
2696                                                 XFS_DIFLAG_PROJINHERIT |
2697                                                 XFS_DIFLAG_NOSYMLINKS);
2698                         }
2699                 }
2700                 if (flags & (XFS_DIFLAG_REALTIME | XFS_XFLAG_EXTSIZE)) {
2701                         /* must be a file */
2702                         if (di_mode && !S_ISREG(di_mode)) {
2703                                 if (!uncertain) {
2704                                         do_warn(
2705         _("file flags set on non-file inode %" PRIu64 "\n"),
2706                                                 lino);
2707                                 }
2708                                 flags &= ~(XFS_DIFLAG_REALTIME |
2709                                                 XFS_XFLAG_EXTSIZE);
2710                         }
2711                 }
2712                 if (!verify_mode && flags != be16_to_cpu(dino->di_flags)) {
2713                         if (!no_modify) {
2714                                 do_warn(_(", fixing bad flags.\n"));
2715                                 dino->di_flags = cpu_to_be16(flags);
2716                                 *dirty = 1;
2717                         } else
2718                                 do_warn(_(", would fix bad flags.\n"));
2719                 }
2720         }
2721
2722         if (verify_mode)
2723                 return retval;
2724
2725         /*
2726          * clear the next unlinked field if necessary on a good
2727          * inode only during phase 4 -- when checking for inodes
2728          * referencing duplicate blocks.  then it's safe because
2729          * we've done the inode discovery and have found all the inodes
2730          * we're going to find.  check_dups is set to 1 only during
2731          * phase 4.  Ugly.
2732          */
2733         if (check_dups && !no_modify)
2734                 *dirty += clear_dinode_unlinked(mp, dino);
2735
2736         /* set type and map type info */
2737
2738         switch (di_mode & S_IFMT) {
2739         case S_IFDIR:
2740                 type = XR_INO_DIR;
2741                 *isa_dir = 1;
2742                 break;
2743         case S_IFREG:
2744                 if (be16_to_cpu(dino->di_flags) & XFS_DIFLAG_REALTIME)
2745                         type = XR_INO_RTDATA;
2746                 else if (lino == mp->m_sb.sb_rbmino)
2747                         type = XR_INO_RTBITMAP;
2748                 else if (lino == mp->m_sb.sb_rsumino)
2749                         type = XR_INO_RTSUM;
2750                 else
2751                         type = XR_INO_DATA;
2752                 break;
2753         case S_IFLNK:
2754                 type = XR_INO_SYMLINK;
2755                 break;
2756         case S_IFCHR:
2757                 type = XR_INO_CHRDEV;
2758                 break;
2759         case S_IFBLK:
2760                 type = XR_INO_BLKDEV;
2761                 break;
2762         case S_IFSOCK:
2763                 type = XR_INO_SOCK;
2764                 break;
2765         case S_IFIFO:
2766                 type = XR_INO_FIFO;
2767                 break;
2768         default:
2769                 do_warn(_("bad inode type %#o inode %" PRIu64 "\n"),
2770                                 di_mode & S_IFMT, lino);
2771                 goto clear_bad_out;
2772         }
2773
2774         /*
2775          * type checks for superblock inodes
2776          */
2777         if (process_check_sb_inodes(mp, dino, lino, &type, dirty) != 0)
2778                 goto clear_bad_out;
2779
2780         /*
2781          * only regular files with REALTIME or EXTSIZE flags set can have
2782          * extsize set, or directories with EXTSZINHERIT.
2783          */
2784         if (be32_to_cpu(dino->di_extsize) != 0) {
2785                 if ((type == XR_INO_RTDATA) ||
2786                     (type == XR_INO_DIR && (be16_to_cpu(dino->di_flags) &
2787                                         XFS_DIFLAG_EXTSZINHERIT)) ||
2788                     (type == XR_INO_DATA && (be16_to_cpu(dino->di_flags) &
2789                                  XFS_DIFLAG_EXTSIZE)))  {
2790                         /* s'okay */ ;
2791                 } else {
2792                         do_warn(
2793 _("bad non-zero extent size %u for non-realtime/extsize inode %" PRIu64 ", "),
2794                                         be32_to_cpu(dino->di_extsize), lino);
2795                         if (!no_modify)  {
2796                                 do_warn(_("resetting to zero\n"));
2797                                 dino->di_extsize = 0;
2798                                 *dirty = 1;
2799                         } else
2800                                 do_warn(_("would reset to zero\n"));
2801                 }
2802         }
2803
2804         /*
2805          * general size/consistency checks:
2806          */
2807         if (process_check_inode_sizes(mp, dino, lino, type) != 0)
2808                 goto clear_bad_out;
2809
2810         /*
2811          * check for illegal values of forkoff
2812          */
2813         if (process_check_inode_forkoff(mp, dino, lino) != 0)
2814                 goto clear_bad_out;
2815
2816         /*
2817          * check data fork -- if it's bad, clear the inode
2818          */
2819         if (process_inode_data_fork(mp, agno, ino, dino, type, dirty,
2820                         &totblocks, &nextents, &dblkmap, check_dups) != 0)
2821                 goto bad_out;
2822
2823         /*
2824          * check attribute fork if necessary.  attributes are
2825          * always stored in the regular filesystem.
2826          */
2827         if (process_inode_attr_fork(mp, agno, ino, dino, type, dirty,
2828                         &atotblocks, &anextents, check_dups, extra_attr_check,
2829                         &retval))
2830                 goto bad_out;
2831
2832         /*
2833          * enforce totblocks is 0 for misc types
2834          */
2835         if (process_misc_ino_types_blocks(totblocks, lino, type))
2836                 goto clear_bad_out;
2837
2838         /*
2839          * correct space counters if required
2840          */
2841         if (process_inode_blocks_and_extents(dino, totblocks + atotblocks,
2842                         nextents, anextents, lino, dirty) != 0)
2843                 goto clear_bad_out;
2844
2845         /*
2846          * do any semantic type-based checking here
2847          */
2848         switch (type)  {
2849         case XR_INO_DIR:
2850                 if (process_dir2(mp, lino, dino, ino_discovery,
2851                                                 dirty, "", parent, dblkmap)) {
2852                         do_warn(
2853         _("problem with directory contents in inode %" PRIu64 "\n"),
2854                                 lino);
2855                         goto clear_bad_out;
2856                 }
2857                 break;
2858         case XR_INO_SYMLINK:
2859                 if (process_symlink(mp, lino, dino, dblkmap) != 0) {
2860                         do_warn(
2861         _("problem with symbolic link in inode %" PRIu64 "\n"),
2862                                 lino);
2863                         goto clear_bad_out;
2864                 }
2865                 break;
2866         default:
2867                 break;
2868         }
2869
2870         blkmap_free(dblkmap);
2871
2872         /*
2873          * check nlinks feature, if it's a version 1 inode,
2874          * just leave nlinks alone.  even if it's set wrong,
2875          * it'll be reset when read in.
2876          */
2877         *dirty += process_check_inode_nlink_version(dino, lino);
2878
2879         return retval;
2880
2881 clear_bad_out:
2882         if (!no_modify)  {
2883                 *dirty += clear_dinode(mp, dino, lino);
2884                 ASSERT(*dirty > 0);
2885         }
2886 bad_out:
2887         *used = is_free;
2888         *isa_dir = 0;
2889         blkmap_free(dblkmap);
2890         return 1;
2891 }
2892
2893 /*
2894  * returns 1 if inode is used, 0 if free.
2895  * performs any necessary salvaging actions.
2896  * note that we leave the generation count alone
2897  * because nothing we could set it to would be
2898  * guaranteed to be correct so the best guess for
2899  * the correct value is just to leave it alone.
2900  *
2901  * The trick is detecting empty files.  For those,
2902  * the core and the forks should all be in the "empty"
2903  * or zero-length state -- a zero or possibly minimum length
2904  * (in the case of dirs) extent list -- although inline directories
2905  * and symlinks might be handled differently.  So it should be
2906  * possible to sanity check them against each other.
2907  *
2908  * If the forks are an empty extent list though, then forget it.
2909  * The file is toast anyway since we can't recover its storage.
2910  *
2911  * Parameters:
2912  *      Ins:
2913  *              mp -- mount structure
2914  *              dino -- pointer to on-disk inode structure
2915  *              agno/ino -- inode numbers
2916  *              free -- whether the map thinks the inode is free (1 == free)
2917  *              ino_discovery -- whether we should examine directory
2918  *                              contents to discover new inodes
2919  *              check_dups -- whether we should check to see if the
2920  *                              inode references duplicate blocks
2921  *                              if so, we compare the inode's claimed
2922  *                              blocks against the contents of the
2923  *                              duplicate extent list but we don't
2924  *                              set the bitmap.  If not, we set the
2925  *                              bitmap and try and detect multiply
2926  *                              claimed blocks using the bitmap.
2927  *      Outs:
2928  *              dirty -- whether we changed the inode (1 == yes)
2929  *              used -- 1 if the inode is used, 0 if free.  In no modify
2930  *                      mode, whether the inode should be used or free
2931  *              isa_dir -- 1 if the inode is a directory, 0 if not.  In
2932  *                      no modify mode, if the inode would be a dir or not.
2933  *
2934  *      Return value -- 0 if the inode is good, 1 if it is/was corrupt
2935  */
2936
2937 int
2938 process_dinode(
2939         xfs_mount_t     *mp,
2940         xfs_dinode_t    *dino,
2941         xfs_agnumber_t  agno,
2942         xfs_agino_t     ino,
2943         int             was_free,
2944         int             *dirty,
2945         int             *used,
2946         int             ino_discovery,
2947         int             check_dups,
2948         int             extra_attr_check,
2949         int             *isa_dir,
2950         xfs_ino_t       *parent)
2951 {
2952         const int       verify_mode = 0;
2953         const int       uncertain = 0;
2954
2955 #ifdef XR_INODE_TRACE
2956         fprintf(stderr, _("processing inode %d/%d\n"), agno, ino);
2957 #endif
2958         return process_dinode_int(mp, dino, agno, ino, was_free, dirty, used,
2959                                 verify_mode, uncertain, ino_discovery,
2960                                 check_dups, extra_attr_check, isa_dir, parent);
2961 }
2962
2963 /*
2964  * a more cursory check, check inode core, *DON'T* check forks
2965  * this basically just verifies whether the inode is an inode
2966  * and whether or not it has been totally trashed.  returns 0
2967  * if the inode passes the cursory sanity check, 1 otherwise.
2968  */
2969 int
2970 verify_dinode(
2971         xfs_mount_t     *mp,
2972         xfs_dinode_t    *dino,
2973         xfs_agnumber_t  agno,
2974         xfs_agino_t     ino)
2975 {
2976         xfs_ino_t       parent;
2977         int             used = 0;
2978         int             dirty = 0;
2979         int             isa_dir = 0;
2980         const int       verify_mode = 1;
2981         const int       check_dups = 0;
2982         const int       ino_discovery = 0;
2983         const int       uncertain = 0;
2984
2985         return process_dinode_int(mp, dino, agno, ino, 0, &dirty, &used,
2986                                 verify_mode, uncertain, ino_discovery,
2987                                 check_dups, 0, &isa_dir, &parent);
2988 }
2989
2990 /*
2991  * like above only for inode on the uncertain list.  it sets
2992  * the uncertain flag which makes process_dinode_int quieter.
2993  * returns 0 if the inode passes the cursory sanity check, 1 otherwise.
2994  */
2995 int
2996 verify_uncertain_dinode(
2997         xfs_mount_t     *mp,
2998         xfs_dinode_t    *dino,
2999         xfs_agnumber_t  agno,
3000         xfs_agino_t     ino)
3001 {
3002         xfs_ino_t       parent;
3003         int             used = 0;
3004         int             dirty = 0;
3005         int             isa_dir = 0;
3006         const int       verify_mode = 1;
3007         const int       check_dups = 0;
3008         const int       ino_discovery = 0;
3009         const int       uncertain = 1;
3010
3011         return process_dinode_int(mp, dino, agno, ino, 0, &dirty, &used,
3012                                 verify_mode, uncertain, ino_discovery,
3013                                 check_dups, 0, &isa_dir, &parent);
3014 }