]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - repair/attr_repair.c
xfsprogs: remove write-only assignments
[thirdparty/xfsprogs-dev.git] / repair / attr_repair.c
1 /*
2 * Copyright (c) 2000-2002,2004-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 "globals.h"
21 #include "err_protos.h"
22 #include "attr_repair.h"
23 #include "dinode.h"
24 #include "bmap.h"
25 #include "protos.h"
26 #include "dir2.h"
27
28 static int xfs_acl_valid(xfs_acl_disk_t *daclp);
29 static int xfs_mac_valid(xfs_mac_label_t *lp);
30
31 /*
32 * da node check/verify functions that the attribute tree relies on are first in
33 * the file before the actual attribute code. This used to be shared with the
34 * dir v1 code, but that format is no longer supported yb the userspace
35 * utilities and hence is now specific to the attribute tree implementation.
36 */
37 #define XR_DA_LEAF_MAPSIZE XFS_ATTR_LEAF_MAPSIZE
38
39 typedef unsigned char da_freemap_t;
40
41 /*
42 * the cursor gets passed up and down the da btree processing
43 * routines. The interior block processing routines use the
44 * cursor to determine if the pointers to and from the preceding
45 * and succeeding sibling blocks are ok and whether the values in
46 * the current block are consistent with the entries in the parent
47 * nodes. When a block is traversed, a parent-verification routine
48 * is called to verify if the next logical entry in the next level up
49 * is consistent with the greatest hashval in the next block of the
50 * current level. The verification routine is itself recursive and
51 * calls itself if it has to traverse an interior block to get
52 * the next logical entry. The routine recurses upwards through
53 * the tree until it finds a block where it can simply step to
54 * the next entry. The hashval in that entry should be equal to
55 * the hashval being passed to it (the greatest hashval in the block
56 * that the entry points to). If that isn't true, then the tree
57 * is blown and we need to trash it, salvage and trash it, or fix it.
58 * Currently, we just trash it.
59 */
60 typedef struct da_level_state {
61 xfs_buf_t *bp; /* block bp */
62 #ifdef XR_DIR_TRACE
63 xfs_da_intnode_t *n; /* bp data */
64 #endif
65 xfs_dablk_t bno; /* file block number */
66 xfs_dahash_t hashval; /* last verified hashval */
67 int index; /* current index in block */
68 int dirty; /* is buffer dirty ? (1 == yes) */
69 } da_level_state_t;
70
71 typedef struct da_bt_cursor {
72 int active; /* highest level in tree (# levels-1) */
73 int type; /* 0 if dir, 1 if attr */
74 xfs_ino_t ino;
75 xfs_dablk_t greatest_bno;
76 xfs_dinode_t *dip;
77 da_level_state_t level[XFS_DA_NODE_MAXDEPTH];
78 struct blkmap *blkmap;
79 } da_bt_cursor_t;
80
81
82 /*
83 * Allocate a freespace map for directory or attr leaf blocks (1 bit per byte)
84 * 1 == used, 0 == free.
85 */
86 static da_freemap_t *
87 alloc_da_freemap(struct xfs_mount *mp)
88 {
89 return calloc(1, mp->m_sb.sb_blocksize / NBBY);
90 }
91
92 /*
93 * Set the he range [start, stop) in the directory freemap.
94 *
95 * Returns 1 if there is a conflict or 0 if everything's good.
96 *
97 * Within a char, the lowest bit of the char represents the byte with
98 * the smallest address
99 */
100 static int
101 set_da_freemap(xfs_mount_t *mp, da_freemap_t *map, int start, int stop)
102 {
103 const da_freemap_t mask = 0x1;
104 int i;
105
106 if (start > stop) {
107 /*
108 * allow == relation since [x, x) claims 1 byte
109 */
110 do_warn(_("bad range claimed [%d, %d) in da block\n"),
111 start, stop);
112 return(1);
113 }
114
115 if (stop > mp->m_sb.sb_blocksize) {
116 do_warn(
117 _("byte range end [%d %d) in da block larger than blocksize %d\n"),
118 start, stop, mp->m_sb.sb_blocksize);
119 return(1);
120 }
121
122 for (i = start; i < stop; i ++) {
123 if (map[i / NBBY] & (mask << i % NBBY)) {
124 do_warn(_("multiply claimed byte %d in da block\n"), i);
125 return(1);
126 }
127 map[i / NBBY] |= (mask << i % NBBY);
128 }
129
130 return(0);
131 }
132
133 /*
134 * walk tree from root to the left-most leaf block reading in
135 * blocks and setting up cursor. passes back file block number of the
136 * left-most leaf block if successful (bno). returns 1 if successful,
137 * 0 if unsuccessful.
138 */
139 static int
140 traverse_int_dablock(xfs_mount_t *mp,
141 da_bt_cursor_t *da_cursor,
142 xfs_dablk_t *rbno,
143 int whichfork)
144 {
145 xfs_dablk_t bno;
146 int i;
147 xfs_da_intnode_t *node;
148 xfs_dfsbno_t fsbno;
149 xfs_buf_t *bp;
150 struct xfs_da_node_entry *btree;
151 struct xfs_da3_icnode_hdr nodehdr;
152
153 /*
154 * traverse down left-side of tree until we hit the
155 * left-most leaf block setting up the btree cursor along
156 * the way.
157 */
158 bno = 0;
159 i = -1;
160 node = NULL;
161 da_cursor->active = 0;
162
163 do {
164 /*
165 * read in each block along the way and set up cursor
166 */
167 fsbno = blkmap_get(da_cursor->blkmap, bno);
168
169 if (fsbno == NULLDFSBNO)
170 goto error_out;
171
172 bp = libxfs_readbuf(mp->m_dev, XFS_FSB_TO_DADDR(mp, fsbno),
173 XFS_FSB_TO_BB(mp, 1), 0, &xfs_da3_node_buf_ops);
174 if (!bp) {
175 if (whichfork == XFS_DATA_FORK)
176 do_warn(
177 _("can't read block %u (fsbno %" PRIu64 ") for directory inode %" PRIu64 "\n"),
178 bno, fsbno, da_cursor->ino);
179 else
180 do_warn(
181 _("can't read block %u (fsbno %" PRIu64 ") for attrbute fork of inode %" PRIu64 "\n"),
182 bno, fsbno, da_cursor->ino);
183 goto error_out;
184 }
185
186 node = (xfs_da_intnode_t *)XFS_BUF_PTR(bp);
187 btree = xfs_da3_node_tree_p(node);
188 xfs_da3_node_hdr_from_disk(&nodehdr, node);
189
190 if (nodehdr.magic != XFS_DA_NODE_MAGIC &&
191 nodehdr.magic != XFS_DA3_NODE_MAGIC) {
192 do_warn(_("bad dir/attr magic number in inode %" PRIu64 ", "
193 "file bno = %u, fsbno = %" PRIu64 "\n"),
194 da_cursor->ino, bno, fsbno);
195 libxfs_putbuf(bp);
196 goto error_out;
197 }
198
199 if (nodehdr.count > mp->m_dir_node_ents) {
200 do_warn(_("bad record count in inode %" PRIu64 ", "
201 "count = %d, max = %d\n"),
202 da_cursor->ino,
203 nodehdr.count,
204 mp->m_dir_node_ents);
205 libxfs_putbuf(bp);
206 goto error_out;
207 }
208
209 /*
210 * maintain level counter
211 */
212 if (i == -1)
213 i = da_cursor->active = nodehdr.level;
214 else {
215 if (nodehdr.level == i - 1) {
216 i--;
217 } else {
218 if (whichfork == XFS_DATA_FORK)
219 do_warn(_("bad directory btree for "
220 "directory inode %" PRIu64 "\n"),
221 da_cursor->ino);
222 else
223 do_warn(_("bad attribute fork btree "
224 "for inode %" PRIu64 "\n"),
225 da_cursor->ino);
226 libxfs_putbuf(bp);
227 goto error_out;
228 }
229 }
230
231 da_cursor->level[i].hashval = be32_to_cpu(btree[0].hashval);
232 da_cursor->level[i].bp = bp;
233 da_cursor->level[i].bno = bno;
234 da_cursor->level[i].index = 0;
235 #ifdef XR_DIR_TRACE
236 da_cursor->level[i].n = XFS_BUF_TO_DA_INTNODE(bp);
237 #endif
238
239 /*
240 * set up new bno for next level down
241 */
242 bno = be32_to_cpu(btree[0].before);
243 } while (node != NULL && i > 1);
244
245 /*
246 * now return block number and get out
247 */
248 *rbno = da_cursor->level[0].bno = bno;
249 return(1);
250
251 error_out:
252 while (i > 1 && i <= da_cursor->active) {
253 libxfs_putbuf(da_cursor->level[i].bp);
254 i++;
255 }
256
257 return(0);
258 }
259
260 /*
261 * blow out buffer for this level and all the rest above as well
262 * if error == 0, we are not expecting to encounter any unreleased
263 * buffers (e.g. if we do, it's a mistake). if error == 1, we're
264 * in an error-handling case so unreleased buffers may exist.
265 */
266 static void
267 release_da_cursor_int(xfs_mount_t *mp,
268 da_bt_cursor_t *cursor,
269 int prev_level,
270 int error)
271 {
272 int level = prev_level + 1;
273
274 if (cursor->level[level].bp != NULL) {
275 if (!error) {
276 do_warn(_("release_da_cursor_int got unexpected "
277 "non-null bp, dabno = %u\n"),
278 cursor->level[level].bno);
279 }
280 ASSERT(error != 0);
281
282 libxfs_putbuf(cursor->level[level].bp);
283 cursor->level[level].bp = NULL;
284 }
285
286 if (level < cursor->active)
287 release_da_cursor_int(mp, cursor, level, error);
288
289 return;
290 }
291
292 static void
293 release_da_cursor(xfs_mount_t *mp,
294 da_bt_cursor_t *cursor,
295 int prev_level)
296 {
297 release_da_cursor_int(mp, cursor, prev_level, 0);
298 }
299
300 static void
301 err_release_da_cursor(xfs_mount_t *mp,
302 da_bt_cursor_t *cursor,
303 int prev_level)
304 {
305 release_da_cursor_int(mp, cursor, prev_level, 1);
306 }
307
308 /*
309 * make sure that all entries in all blocks along the right side of
310 * of the tree are used and hashval's are consistent. level is the
311 * level of the descendent block. returns 0 if good (even if it had
312 * to be fixed up), and 1 if bad. The right edge of the tree is
313 * technically a block boundary. this routine should be used then
314 * instead of verify_da_path().
315 */
316 static int
317 verify_final_da_path(xfs_mount_t *mp,
318 da_bt_cursor_t *cursor,
319 const int p_level)
320 {
321 xfs_da_intnode_t *node;
322 xfs_dahash_t hashval;
323 int bad = 0;
324 int entry;
325 int this_level = p_level + 1;
326 struct xfs_da_node_entry *btree;
327 struct xfs_da3_icnode_hdr nodehdr;
328
329 #ifdef XR_DIR_TRACE
330 fprintf(stderr, "in verify_final_da_path, this_level = %d\n",
331 this_level);
332 #endif
333 /*
334 * the index should point to the next "unprocessed" entry
335 * in the block which should be the final (rightmost) entry
336 */
337 entry = cursor->level[this_level].index;
338 node = (xfs_da_intnode_t *)XFS_BUF_PTR(cursor->level[this_level].bp);
339 btree = xfs_da3_node_tree_p(node);
340 xfs_da3_node_hdr_from_disk(&nodehdr, node);
341
342 /*
343 * check internal block consistency on this level -- ensure
344 * that all entries are used, encountered and expected hashvals
345 * match, etc.
346 */
347 if (entry != nodehdr.count - 1) {
348 do_warn(_("directory/attribute block used/count "
349 "inconsistency - %d/%hu\n"),
350 entry, nodehdr.count);
351 bad++;
352 }
353 /*
354 * hash values monotonically increasing ???
355 */
356 if (cursor->level[this_level].hashval >=
357 be32_to_cpu(btree[entry].hashval)) {
358 do_warn(_("directory/attribute block hashvalue inconsistency, "
359 "expected > %u / saw %u\n"),
360 cursor->level[this_level].hashval,
361 be32_to_cpu(btree[entry].hashval));
362 bad++;
363 }
364 if (nodehdr.forw != 0) {
365 do_warn(_("bad directory/attribute forward block pointer, "
366 "expected 0, saw %u\n"),
367 nodehdr.forw);
368 bad++;
369 }
370 if (bad) {
371 do_warn(_("bad directory block in dir ino %" PRIu64 "\n"),
372 cursor->ino);
373 return(1);
374 }
375 /*
376 * keep track of greatest block # -- that gets
377 * us the length of the directory
378 */
379 if (cursor->level[this_level].bno > cursor->greatest_bno)
380 cursor->greatest_bno = cursor->level[this_level].bno;
381
382 /*
383 * ok, now check descendant block number against this level
384 */
385 if (cursor->level[p_level].bno != be32_to_cpu(btree[entry].before)) {
386 #ifdef XR_DIR_TRACE
387 fprintf(stderr, "bad directory btree pointer, child bno should "
388 "be %d, block bno is %d, hashval is %u\n",
389 be16_to_cpu(btree[entry].before),
390 cursor->level[p_level].bno,
391 cursor->level[p_level].hashval);
392 fprintf(stderr, "verify_final_da_path returns 1 (bad) #1a\n");
393 #endif
394 return(1);
395 }
396
397 if (cursor->level[p_level].hashval != be32_to_cpu(btree[entry].hashval)) {
398 if (!no_modify) {
399 do_warn(_("correcting bad hashval in non-leaf "
400 "dir/attr block\n\tin (level %d) in "
401 "inode %" PRIu64 ".\n"),
402 this_level, cursor->ino);
403 btree[entry].hashval = cpu_to_be32(
404 cursor->level[p_level].hashval);
405 cursor->level[this_level].dirty++;
406 } else {
407 do_warn(_("would correct bad hashval in non-leaf "
408 "dir/attr block\n\tin (level %d) in "
409 "inode %" PRIu64 ".\n"),
410 this_level, cursor->ino);
411 }
412 }
413
414 /*
415 * Note: squirrel hashval away _before_ releasing the
416 * buffer, preventing a use-after-free problem.
417 */
418 hashval = be32_to_cpu(btree[entry].hashval);
419
420 /*
421 * release/write buffer
422 */
423 ASSERT(cursor->level[this_level].dirty == 0 ||
424 (cursor->level[this_level].dirty && !no_modify));
425
426 if (cursor->level[this_level].dirty && !no_modify)
427 libxfs_writebuf(cursor->level[this_level].bp, 0);
428 else
429 libxfs_putbuf(cursor->level[this_level].bp);
430
431 cursor->level[this_level].bp = NULL;
432
433 /*
434 * bail out if this is the root block (top of tree)
435 */
436 if (this_level >= cursor->active) {
437 #ifdef XR_DIR_TRACE
438 fprintf(stderr, "verify_final_da_path returns 0 (ok)\n");
439 #endif
440 return(0);
441 }
442 /*
443 * set hashvalue to correctly reflect the now-validated
444 * last entry in this block and continue upwards validation
445 */
446 cursor->level[this_level].hashval = hashval;
447 return(verify_final_da_path(mp, cursor, this_level));
448 }
449
450 /*
451 * Verifies the path from a descendant block up to the root.
452 * Should be called when the descendant level traversal hits
453 * a block boundary before crossing the boundary (reading in a new
454 * block).
455 *
456 * the directory/attr btrees work differently to the other fs btrees.
457 * each interior block contains records that are <hashval, bno>
458 * pairs. The bno is a file bno, not a filesystem bno. The last
459 * hashvalue in the block <bno> will be <hashval>. BUT unlike
460 * the freespace btrees, the *last* value in each block gets
461 * propagated up the tree instead of the first value in each block.
462 * that is, the interior records point to child blocks and the *greatest*
463 * hash value contained by the child block is the one the block above
464 * uses as the key for the child block.
465 *
466 * level is the level of the descendent block. returns 0 if good,
467 * and 1 if bad. The descendant block may be a leaf block.
468 *
469 * the invariant here is that the values in the cursor for the
470 * levels beneath this level (this_level) and the cursor index
471 * for this level *must* be valid.
472 *
473 * that is, the hashval/bno info is accurate for all
474 * DESCENDANTS and match what the node[index] information
475 * for the current index in the cursor for this level.
476 *
477 * the index values in the cursor for the descendant level
478 * are allowed to be off by one as they will reflect the
479 * next entry at those levels to be processed.
480 *
481 * the hashvalue for the current level can't be set until
482 * we hit the last entry in the block so, it's garbage
483 * until set by this routine.
484 *
485 * bno and bp for the current block/level are always valid
486 * since they have to be set so we can get a buffer for the
487 * block.
488 */
489 static int
490 verify_da_path(xfs_mount_t *mp,
491 da_bt_cursor_t *cursor,
492 const int p_level)
493 {
494 xfs_da_intnode_t *node;
495 xfs_da_intnode_t *newnode;
496 xfs_dfsbno_t fsbno;
497 xfs_dablk_t dabno;
498 xfs_buf_t *bp;
499 int bad;
500 int entry;
501 int this_level = p_level + 1;
502 struct xfs_da_node_entry *btree;
503 struct xfs_da3_icnode_hdr nodehdr;
504
505 /*
506 * index is currently set to point to the entry that
507 * should be processed now in this level.
508 */
509 entry = cursor->level[this_level].index;
510 node = (xfs_da_intnode_t *)XFS_BUF_PTR(cursor->level[this_level].bp);
511 btree = xfs_da3_node_tree_p(node);
512 xfs_da3_node_hdr_from_disk(&nodehdr, node);
513
514 /*
515 * if this block is out of entries, validate this
516 * block and move on to the next block.
517 * and update cursor value for said level
518 */
519 if (entry >= nodehdr.count) {
520 /*
521 * update the hash value for this level before
522 * validating it. bno value should be ok since
523 * it was set when the block was first read in.
524 */
525 cursor->level[this_level].hashval =
526 be32_to_cpu(btree[entry - 1].hashval);
527
528 /*
529 * keep track of greatest block # -- that gets
530 * us the length of the directory
531 */
532 if (cursor->level[this_level].bno > cursor->greatest_bno)
533 cursor->greatest_bno = cursor->level[this_level].bno;
534
535 /*
536 * validate the path for the current used-up block
537 * before we trash it
538 */
539 if (verify_da_path(mp, cursor, this_level))
540 return(1);
541 /*
542 * ok, now get the next buffer and check sibling pointers
543 */
544 dabno = nodehdr.forw;
545 ASSERT(dabno != 0);
546 fsbno = blkmap_get(cursor->blkmap, dabno);
547
548 if (fsbno == NULLDFSBNO) {
549 do_warn(_("can't get map info for block %u "
550 "of directory inode %" PRIu64 "\n"),
551 dabno, cursor->ino);
552 return(1);
553 }
554
555 bp = libxfs_readbuf(mp->m_dev, XFS_FSB_TO_DADDR(mp, fsbno),
556 XFS_FSB_TO_BB(mp, 1), 0, &xfs_da3_node_buf_ops);
557 if (!bp) {
558 do_warn(
559 _("can't read block %u (%" PRIu64 ") for directory inode %" PRIu64 "\n"),
560 dabno, fsbno, cursor->ino);
561 return(1);
562 }
563
564 newnode = (xfs_da_intnode_t *)XFS_BUF_PTR(bp);
565 btree = xfs_da3_node_tree_p(node);
566 xfs_da3_node_hdr_from_disk(&nodehdr, newnode);
567 /*
568 * verify magic number and back pointer, sanity-check
569 * entry count, verify level
570 */
571 bad = 0;
572 if (nodehdr.magic != XFS_DA_NODE_MAGIC ||
573 nodehdr.magic != XFS_DA3_NODE_MAGIC) {
574 do_warn(
575 _("bad magic number %x in block %u (%" PRIu64 ") for directory inode %" PRIu64 "\n"),
576 nodehdr.magic,
577 dabno, fsbno, cursor->ino);
578 bad++;
579 }
580 if (nodehdr.back != cursor->level[this_level].bno) {
581 do_warn(
582 _("bad back pointer in block %u (%"PRIu64 ") for directory inode %" PRIu64 "\n"),
583 dabno, fsbno, cursor->ino);
584 bad++;
585 }
586 if (nodehdr.count > mp->m_dir_node_ents) {
587 do_warn(
588 _("entry count %d too large in block %u (%" PRIu64 ") for directory inode %" PRIu64 "\n"),
589 nodehdr.count,
590 dabno, fsbno, cursor->ino);
591 bad++;
592 }
593 if (nodehdr.level != this_level) {
594 do_warn(
595 _("bad level %d in block %u (%" PRIu64 ") for directory inode %" PRIu64 "\n"),
596 nodehdr.level,
597 dabno, fsbno, cursor->ino);
598 bad++;
599 }
600 if (bad) {
601 #ifdef XR_DIR_TRACE
602 fprintf(stderr, "verify_da_path returns 1 (bad) #4\n");
603 #endif
604 libxfs_putbuf(bp);
605 return(1);
606 }
607 /*
608 * update cursor, write out the *current* level if
609 * required. don't write out the descendant level
610 */
611 ASSERT(cursor->level[this_level].dirty == 0 ||
612 (cursor->level[this_level].dirty && !no_modify));
613
614 if (cursor->level[this_level].dirty && !no_modify)
615 libxfs_writebuf(cursor->level[this_level].bp, 0);
616 else
617 libxfs_putbuf(cursor->level[this_level].bp);
618 cursor->level[this_level].bp = bp;
619 cursor->level[this_level].dirty = 0;
620 cursor->level[this_level].bno = dabno;
621 cursor->level[this_level].hashval =
622 be32_to_cpu(btree[0].hashval);
623 #ifdef XR_DIR_TRACE
624 cursor->level[this_level].n = newnode;
625 #endif
626 entry = cursor->level[this_level].index = 0;
627 }
628 /*
629 * ditto for block numbers
630 */
631 if (cursor->level[p_level].bno != be32_to_cpu(btree[entry].before)) {
632 #ifdef XR_DIR_TRACE
633 fprintf(stderr, "bad directory btree pointer, child bno "
634 "should be %d, block bno is %d, hashval is %u\n",
635 be32_to_cpu(btree[entry].before),
636 cursor->level[p_level].bno,
637 cursor->level[p_level].hashval);
638 fprintf(stderr, "verify_da_path returns 1 (bad) #1a\n");
639 #endif
640 return(1);
641 }
642 /*
643 * ok, now validate last hashvalue in the descendant
644 * block against the hashval in the current entry
645 */
646 if (cursor->level[p_level].hashval !=
647 be32_to_cpu(btree[entry].hashval)) {
648 if (!no_modify) {
649 do_warn(_("correcting bad hashval in interior "
650 "dir/attr block\n\tin (level %d) in "
651 "inode %" PRIu64 ".\n"),
652 this_level, cursor->ino);
653 btree[entry].hashval = cpu_to_be32(
654 cursor->level[p_level].hashval);
655 cursor->level[this_level].dirty++;
656 } else {
657 do_warn(_("would correct bad hashval in interior "
658 "dir/attr block\n\tin (level %d) in "
659 "inode %" PRIu64 ".\n"),
660 this_level, cursor->ino);
661 }
662 }
663 /*
664 * increment index for this level to point to next entry
665 * (which should point to the next descendant block)
666 */
667 cursor->level[this_level].index++;
668 #ifdef XR_DIR_TRACE
669 fprintf(stderr, "verify_da_path returns 0 (ok)\n");
670 #endif
671 return(0);
672 }
673
674 /*
675 * For attribute repair, there are 3 formats to worry about. First, is
676 * shortform attributes which reside in the inode. Second is the leaf
677 * form, and lastly the btree. Much of this models after the directory
678 * structure so code resembles the directory repair cases.
679 * For shortform case, if an attribute looks corrupt, it is removed.
680 * If that leaves the shortform down to 0 attributes, it's okay and
681 * will appear to just have a null attribute fork. Some checks are done
682 * for validity of the value field based on what the security needs are.
683 * Calls will be made to xfs_mac_valid or xfs_acl_valid routines if the
684 * security attributes exist. They will be cleared if invalid.
685 * No other values will be checked. The DMF folks do not have current
686 * requirements, but may in the future.
687 *
688 * For leaf block attributes, it requires more processing. One sticky
689 * point is that the attributes can be local (within the leaf) or
690 * remote (outside the leaf in other blocks). Thinking of local only
691 * if you get a bad attribute, and want to delete just one, it's a-okay
692 * if it remains large enough to still be a leaf block attribute. Otherwise,
693 * it may have to be converted to shortform. How to convert this and when
694 * is an issue. This call is happening in Phase3. Phase5 will capture empty
695 * blocks, but Phase6 allows you to use the libxfs library which knows
696 * how to handle attributes in the kernel for converting formats. What we
697 * could do is mark an attribute to be cleared now, but in phase6 somehow
698 * have it cleared for real and then the format changed to shortform if
699 * applicable. Since this requires more work than I anticipate can be
700 * accomplished for the next release, we will instead just say any bad
701 * attribute in the leaf block will make the entire attribute fork be
702 * cleared. The simplest way to do that is to ignore the leaf format, and
703 * call clear_dinode_attr to just make a shortform attribute fork with
704 * zero entries.
705 *
706 * Another issue with handling repair on leaf attributes is the remote
707 * blocks. To make sure that they look good and are not used multiple times
708 * by the attribute fork, some mechanism to keep track of all them is necessary.
709 * Do this in the future, time permitting. For now, note that there is no
710 * check for remote blocks and their allocations.
711 *
712 * For btree formatted attributes, the model can follow directories. That
713 * would mean go down the tree to the leftmost leaf. From there moving down
714 * the links and processing each. They would call back up the tree, to verify
715 * that the tree structure is okay. Any problems will result in the attribute
716 * fork being emptied and put in shortform format.
717 */
718
719 /*
720 * This routine just checks what security needs are for attribute values
721 * only called when root flag is set, otherwise these names could exist in
722 * in user attribute land without a conflict.
723 * If value is non-zero, then a remote attribute is being passed in
724 */
725 static int
726 valuecheck(char *namevalue, char *value, int namelen, int valuelen)
727 {
728 /* for proper alignment issues, get the structs and memmove the values */
729 xfs_mac_label_t macl;
730 xfs_acl_t thisacl;
731 void *valuep;
732 int clearit = 0;
733
734 if ((strncmp(namevalue, SGI_ACL_FILE, SGI_ACL_FILE_SIZE) == 0) ||
735 (strncmp(namevalue, SGI_ACL_DEFAULT,
736 SGI_ACL_DEFAULT_SIZE) == 0)) {
737 if (value == NULL) {
738 memset(&thisacl, 0, sizeof(xfs_acl_t));
739 memmove(&thisacl, namevalue+namelen, valuelen);
740 valuep = &thisacl;
741 } else
742 valuep = value;
743
744 if (xfs_acl_valid((xfs_acl_disk_t *)valuep) != 0) {
745 clearit = 1;
746 do_warn(
747 _("entry contains illegal value in attribute named SGI_ACL_FILE "
748 "or SGI_ACL_DEFAULT\n"));
749 }
750 } else if (strncmp(namevalue, SGI_MAC_FILE, SGI_MAC_FILE_SIZE) == 0) {
751 if (value == NULL) {
752 memset(&macl, 0, sizeof(xfs_mac_label_t));
753 memmove(&macl, namevalue+namelen, valuelen);
754 valuep = &macl;
755 } else
756 valuep = value;
757
758 if (xfs_mac_valid((xfs_mac_label_t *)valuep) != 1) { /* 1 is valid */
759 /*
760 * if sysconf says MAC enabled,
761 * temp = mac_from_text("msenhigh/mintlow", NULL)
762 * copy it to value, update valuelen, totsize
763 * This causes pushing up or down of all following
764 * attributes, forcing a attribute format change!!
765 * else clearit = 1;
766 */
767 clearit = 1;
768 do_warn(
769 _("entry contains illegal value in attribute named SGI_MAC_LABEL\n"));
770 }
771 } else if (strncmp(namevalue, SGI_CAP_FILE, SGI_CAP_FILE_SIZE) == 0) {
772 if ( valuelen != sizeof(xfs_cap_set_t)) {
773 clearit = 1;
774 do_warn(
775 _("entry contains illegal value in attribute named SGI_CAP_FILE\n"));
776 }
777 }
778
779 return(clearit);
780 }
781
782
783 /*
784 * this routine validates the attributes in shortform format.
785 * a non-zero return repair value means certain attributes are bogus
786 * and were cleared if possible. Warnings do not generate error conditions
787 * if you cannot modify the structures. repair is set to 1, if anything
788 * was fixed.
789 */
790 static int
791 process_shortform_attr(
792 xfs_ino_t ino,
793 xfs_dinode_t *dip,
794 int *repair)
795 {
796 xfs_attr_shortform_t *asf;
797 xfs_attr_sf_entry_t *currententry, *nextentry, *tempentry;
798 int i, junkit;
799 int currentsize, remainingspace;
800
801 *repair = 0;
802
803 asf = (xfs_attr_shortform_t *) XFS_DFORK_APTR(dip);
804
805 /* Assumption: hdr.totsize is less than a leaf block and was checked
806 * by lclinode for valid sizes. Check the count though.
807 */
808 if (asf->hdr.count == 0)
809 /* then the total size should just be the header length */
810 if (be16_to_cpu(asf->hdr.totsize) != sizeof(xfs_attr_sf_hdr_t)) {
811 /* whoops there's a discrepancy. Clear the hdr */
812 if (!no_modify) {
813 do_warn(
814 _("there are no attributes in the fork for inode %" PRIu64 "\n"),
815 ino);
816 asf->hdr.totsize =
817 cpu_to_be16(sizeof(xfs_attr_sf_hdr_t));
818 *repair = 1;
819 return(1);
820 } else {
821 do_warn(
822 _("would junk the attribute fork since count is 0 for inode %" PRIu64 "\n"),
823 ino);
824 return(1);
825 }
826 }
827
828 currentsize = sizeof(xfs_attr_sf_hdr_t);
829 remainingspace = be16_to_cpu(asf->hdr.totsize) - currentsize;
830 nextentry = &asf->list[0];
831 for (i = 0; i < asf->hdr.count; i++) {
832 currententry = nextentry;
833 junkit = 0;
834
835 /* don't go off the end if the hdr.count was off */
836 if ((currentsize + (sizeof(xfs_attr_sf_entry_t) - 1)) >
837 be16_to_cpu(asf->hdr.totsize))
838 break; /* get out and reset count and totSize */
839
840 /* if the namelen is 0, can't get to the rest of the entries */
841 if (currententry->namelen == 0) {
842 do_warn(_("zero length name entry in attribute fork,"));
843 if (!no_modify) {
844 do_warn(
845 _(" truncating attributes for inode %" PRIu64 " to %d\n"), ino, i);
846 *repair = 1;
847 break; /* and then update hdr fields */
848 } else {
849 do_warn(
850 _(" would truncate attributes for inode %" PRIu64 " to %d\n"), ino, i);
851 break;
852 }
853 } else {
854 /* It's okay to have a 0 length valuelen, but do a
855 * rough check to make sure we haven't gone outside of
856 * totsize.
857 */
858 if (remainingspace < currententry->namelen ||
859 ((remainingspace - currententry->
860 namelen) < currententry->valuelen)) {
861 do_warn(
862 _("name or value attribute lengths are too large,\n"));
863 if (!no_modify) {
864 do_warn(
865 _(" truncating attributes for inode %" PRIu64 " to %d\n"),
866 ino, i);
867 *repair = 1;
868 break; /* and then update hdr fields */
869 } else {
870 do_warn(
871 _(" would truncate attributes for inode %" PRIu64 " to %d\n"),
872 ino, i);
873 break;
874 }
875 }
876 }
877
878 /* namecheck checks for / and null terminated for file names.
879 * attributes names currently follow the same rules.
880 */
881 if (namecheck((char *)&currententry->nameval[0],
882 currententry->namelen)) {
883 do_warn(
884 _("entry contains illegal character in shortform attribute name\n"));
885 junkit = 1;
886 }
887
888 if (currententry->flags & XFS_ATTR_INCOMPLETE) {
889 do_warn(
890 _("entry has INCOMPLETE flag on in shortform attribute\n"));
891 junkit = 1;
892 }
893
894 /* Only check values for root security attributes */
895 if (currententry->flags & XFS_ATTR_ROOT)
896 junkit = valuecheck((char *)&currententry->nameval[0],
897 NULL, currententry->namelen,
898 currententry->valuelen);
899
900 remainingspace = remainingspace -
901 XFS_ATTR_SF_ENTSIZE(currententry);
902
903 if (junkit) {
904 if (!no_modify) {
905 /* get rid of only this entry */
906 do_warn(
907 _("removing attribute entry %d for inode %" PRIu64 "\n"),
908 i, ino);
909 tempentry = (xfs_attr_sf_entry_t *)
910 ((__psint_t) currententry +
911 XFS_ATTR_SF_ENTSIZE(currententry));
912 memmove(currententry,tempentry,remainingspace);
913 asf->hdr.count -= 1;
914 i--; /* no worries, it will wrap back to 0 */
915 *repair = 1;
916 continue; /* go back up now */
917 } else {
918 do_warn(
919 _("would remove attribute entry %d for inode %" PRIu64 "\n"),
920 i, ino);
921 }
922 }
923
924 /* Let's get ready for the next entry... */
925 nextentry = (xfs_attr_sf_entry_t *)((__psint_t) nextentry +
926 XFS_ATTR_SF_ENTSIZE(currententry));
927 currentsize = currentsize + XFS_ATTR_SF_ENTSIZE(currententry);
928
929 } /* end the loop */
930
931 if (asf->hdr.count != i) {
932 if (no_modify) {
933 do_warn(
934 _("would have corrected attribute entry count in inode %" PRIu64 " from %d to %d\n"),
935 ino, asf->hdr.count, i);
936 } else {
937 do_warn(
938 _("corrected attribute entry count in inode %" PRIu64 ", was %d, now %d\n"),
939 ino, asf->hdr.count, i);
940 asf->hdr.count = i;
941 *repair = 1;
942 }
943 }
944
945 /* ASSUMPTION: currentsize <= totsize */
946 if (be16_to_cpu(asf->hdr.totsize) != currentsize) {
947 if (no_modify) {
948 do_warn(
949 _("would have corrected attribute totsize in inode %" PRIu64 " from %d to %d\n"),
950 ino, be16_to_cpu(asf->hdr.totsize),
951 currentsize);
952 } else {
953 do_warn(
954 _("corrected attribute entry totsize in inode %" PRIu64 ", was %d, now %d\n"),
955 ino, be16_to_cpu(asf->hdr.totsize),
956 currentsize);
957 asf->hdr.totsize = cpu_to_be16(currentsize);
958 *repair = 1;
959 }
960 }
961
962 return(*repair);
963 }
964
965 /* This routine brings in blocks from disk one by one and assembles them
966 * in the value buffer. If get_bmapi gets smarter later to return an extent
967 * or list of extents, that would be great. For now, we don't expect too
968 * many blocks per remote value, so one by one is sufficient.
969 */
970 static int
971 rmtval_get(xfs_mount_t *mp, xfs_ino_t ino, blkmap_t *blkmap,
972 xfs_dablk_t blocknum, int valuelen, char* value)
973 {
974 xfs_dfsbno_t bno;
975 xfs_buf_t *bp;
976 int clearit = 0, i = 0, length = 0, amountdone = 0;
977
978 /* ASSUMPTION: valuelen is a valid number, so use it for looping */
979 /* Note that valuelen is not a multiple of blocksize */
980 while (amountdone < valuelen) {
981 bno = blkmap_get(blkmap, blocknum + i);
982 if (bno == NULLDFSBNO) {
983 do_warn(
984 _("remote block for attributes of inode %" PRIu64 " is missing\n"), ino);
985 clearit = 1;
986 break;
987 }
988 bp = libxfs_readbuf(mp->m_dev, XFS_FSB_TO_DADDR(mp, bno),
989 XFS_FSB_TO_BB(mp, 1), 0, NULL);
990 if (!bp) {
991 do_warn(
992 _("can't read remote block for attributes of inode %" PRIu64 "\n"), ino);
993 clearit = 1;
994 break;
995 }
996 ASSERT(mp->m_sb.sb_blocksize == XFS_BUF_COUNT(bp));
997 length = MIN(XFS_BUF_COUNT(bp), valuelen - amountdone);
998 memmove(value, XFS_BUF_PTR(bp), length);
999 amountdone += length;
1000 value += length;
1001 i++;
1002 libxfs_putbuf(bp);
1003 }
1004 return (clearit);
1005 }
1006
1007 /* The block is read in. The magic number and forward / backward
1008 * links are checked by the caller process_leaf_attr.
1009 * If any problems occur the routine returns with non-zero. In
1010 * this case the next step is to clear the attribute fork, by
1011 * changing it to shortform and zeroing it out. Forkoff need not
1012 * be changed.
1013 */
1014
1015 static int
1016 process_leaf_attr_local(
1017 xfs_attr_leafblock_t *leaf,
1018 int i,
1019 xfs_attr_leaf_entry_t *entry,
1020 xfs_dahash_t last_hashval,
1021 xfs_dablk_t da_bno,
1022 xfs_ino_t ino)
1023 {
1024 xfs_attr_leaf_name_local_t *local;
1025
1026 local = xfs_attr3_leaf_name_local(leaf, i);
1027 if (local->namelen == 0 || namecheck((char *)&local->nameval[0],
1028 local->namelen)) {
1029 do_warn(
1030 _("attribute entry %d in attr block %u, inode %" PRIu64 " has bad name (namelen = %d)\n"),
1031 i, da_bno, ino, local->namelen);
1032 return -1;
1033 }
1034
1035 /* Check on the hash value. Checking order of values
1036 * is not necessary, since one wrong clears the whole
1037 * fork. If the ordering's wrong, it's caught here or
1038 * the kernel code has a bug with transaction logging
1039 * or attributes itself. Being paranoid, let's check
1040 * ordering anyway in case both the name value and the
1041 * hashvalue were wrong but matched. Unlikely, however.
1042 */
1043 if (be32_to_cpu(entry->hashval) != libxfs_da_hashname(
1044 &local->nameval[0], local->namelen) ||
1045 be32_to_cpu(entry->hashval) < last_hashval) {
1046 do_warn(
1047 _("bad hashvalue for attribute entry %d in attr block %u, inode %" PRIu64 "\n"),
1048 i, da_bno, ino);
1049 return -1;
1050 }
1051
1052 /* Only check values for root security attributes */
1053 if (entry->flags & XFS_ATTR_ROOT) {
1054 if (valuecheck((char *)&local->nameval[0], NULL,
1055 local->namelen, be16_to_cpu(local->valuelen))) {
1056 do_warn(
1057 _("bad security value for attribute entry %d in attr block %u, inode %" PRIu64 "\n"),
1058 i, da_bno, ino);
1059 return -1;
1060 }
1061 }
1062 return xfs_attr_leaf_entsize_local(local->namelen,
1063 be16_to_cpu(local->valuelen));
1064 }
1065
1066 static int
1067 process_leaf_attr_remote(
1068 xfs_attr_leafblock_t *leaf,
1069 int i,
1070 xfs_attr_leaf_entry_t *entry,
1071 xfs_dahash_t last_hashval,
1072 xfs_dablk_t da_bno,
1073 xfs_ino_t ino,
1074 xfs_mount_t *mp,
1075 blkmap_t *blkmap)
1076 {
1077 xfs_attr_leaf_name_remote_t *remotep;
1078 char* value;
1079
1080 remotep = xfs_attr3_leaf_name_remote(leaf, i);
1081
1082 if (remotep->namelen == 0 || namecheck((char *)&remotep->name[0],
1083 remotep->namelen) ||
1084 be32_to_cpu(entry->hashval) !=
1085 libxfs_da_hashname((uchar_t *)&remotep->name[0],
1086 remotep->namelen) ||
1087 be32_to_cpu(entry->hashval) < last_hashval ||
1088 be32_to_cpu(remotep->valueblk) == 0) {
1089 do_warn(
1090 _("inconsistent remote attribute entry %d in attr block %u, ino %" PRIu64 "\n"), i, da_bno, ino);
1091 return -1;
1092 }
1093
1094 if (!(entry->flags & XFS_ATTR_ROOT))
1095 goto out;
1096
1097 value = malloc(be32_to_cpu(remotep->valuelen));
1098 if (value == NULL) {
1099 do_warn(
1100 _("cannot malloc enough for remotevalue attribute for inode %" PRIu64 "\n"),
1101 ino);
1102 do_warn(_("SKIPPING this remote attribute\n"));
1103 goto out;
1104 }
1105 if (rmtval_get(mp, ino, blkmap, be32_to_cpu(remotep->valueblk),
1106 be32_to_cpu(remotep->valuelen), value)) {
1107 do_warn(
1108 _("remote attribute get failed for entry %d, inode %" PRIu64 "\n"),
1109 i, ino);
1110 goto bad_free_out;
1111 }
1112 if (valuecheck((char *)&remotep->name[0], value, remotep->namelen,
1113 be32_to_cpu(remotep->valuelen))) {
1114 do_warn(
1115 _("remote attribute value check failed for entry %d, inode %" PRIu64 "\n"),
1116 i, ino);
1117 goto bad_free_out;
1118 }
1119 free(value);
1120 out:
1121 return xfs_attr_leaf_entsize_remote(remotep->namelen);
1122
1123 bad_free_out:
1124 free(value);
1125 return -1;
1126 }
1127
1128 static int
1129 process_leaf_attr_block(
1130 xfs_mount_t *mp,
1131 xfs_attr_leafblock_t *leaf,
1132 xfs_dablk_t da_bno,
1133 xfs_ino_t ino,
1134 blkmap_t *blkmap,
1135 xfs_dahash_t last_hashval,
1136 xfs_dahash_t *current_hashval,
1137 int *repair)
1138 {
1139 xfs_attr_leaf_entry_t *entry;
1140 int i, start, stop, clearit, usedbs, firstb, thissize;
1141 da_freemap_t *attr_freemap;
1142 struct xfs_attr3_icleaf_hdr leafhdr;
1143
1144 xfs_attr3_leaf_hdr_from_disk(&leafhdr, leaf);
1145 clearit = usedbs = 0;
1146 *repair = 0;
1147 firstb = mp->m_sb.sb_blocksize;
1148 stop = xfs_attr3_leaf_hdr_size(leaf);
1149
1150 /* does the count look sorta valid? */
1151 if (leafhdr.count * sizeof(xfs_attr_leaf_entry_t) + stop >
1152 XFS_LBSIZE(mp)) {
1153 do_warn(
1154 _("bad attribute count %d in attr block %u, inode %" PRIu64 "\n"),
1155 leafhdr.count, da_bno, ino);
1156 return 1;
1157 }
1158
1159 attr_freemap = alloc_da_freemap(mp);
1160 (void) set_da_freemap(mp, attr_freemap, 0, stop);
1161
1162 /* go thru each entry checking for problems */
1163 for (i = 0, entry = xfs_attr3_leaf_entryp(leaf);
1164 i < leafhdr.count; i++, entry++) {
1165
1166 /* check if index is within some boundary. */
1167 if (be16_to_cpu(entry->nameidx) > XFS_LBSIZE(mp)) {
1168 do_warn(
1169 _("bad attribute nameidx %d in attr block %u, inode %" PRIu64 "\n"),
1170 be16_to_cpu(entry->nameidx), da_bno, ino);
1171 clearit = 1;
1172 break;
1173 }
1174
1175 if (entry->flags & XFS_ATTR_INCOMPLETE) {
1176 /* we are inconsistent state. get rid of us */
1177 do_warn(
1178 _("attribute entry #%d in attr block %u, inode %" PRIu64 " is INCOMPLETE\n"),
1179 i, da_bno, ino);
1180 clearit = 1;
1181 break;
1182 }
1183
1184 /* mark the entry used */
1185 start = (__psint_t)entry - (__psint_t)leaf;
1186 stop = start + sizeof(xfs_attr_leaf_entry_t);
1187 if (set_da_freemap(mp, attr_freemap, start, stop)) {
1188 do_warn(
1189 _("attribute entry %d in attr block %u, inode %" PRIu64 " claims already used space\n"),
1190 i, da_bno, ino);
1191 clearit = 1;
1192 break; /* got an overlap */
1193 }
1194
1195 if (entry->flags & XFS_ATTR_LOCAL)
1196 thissize = process_leaf_attr_local(leaf, i, entry,
1197 last_hashval, da_bno, ino);
1198 else
1199 thissize = process_leaf_attr_remote(leaf, i, entry,
1200 last_hashval, da_bno, ino,
1201 mp, blkmap);
1202 if (thissize < 0) {
1203 clearit = 1;
1204 break;
1205 }
1206
1207 *current_hashval = last_hashval = be32_to_cpu(entry->hashval);
1208
1209 if (set_da_freemap(mp, attr_freemap, be16_to_cpu(entry->nameidx),
1210 be16_to_cpu(entry->nameidx) + thissize)) {
1211 do_warn(
1212 _("attribute entry %d in attr block %u, inode %" PRIu64 " claims used space\n"),
1213 i, da_bno, ino);
1214 clearit = 1;
1215 break; /* got an overlap */
1216 }
1217 usedbs += thissize;
1218 if (be16_to_cpu(entry->nameidx) < firstb)
1219 firstb = be16_to_cpu(entry->nameidx);
1220
1221 } /* end the loop */
1222
1223 if (!clearit) {
1224 /* verify the header information is correct */
1225
1226 /* if the holes flag is set, don't reset first_used unless it's
1227 * pointing to used bytes. we're being conservative here
1228 * since the block will get compacted anyhow by the kernel.
1229 */
1230
1231 if ((leafhdr.holes == 0 &&
1232 firstb != leafhdr.firstused) ||
1233 leafhdr.firstused > firstb) {
1234 if (!no_modify) {
1235 do_warn(
1236 _("- resetting first used heap value from %d to %d in "
1237 "block %u of attribute fork of inode %" PRIu64 "\n"),
1238 leafhdr.firstused,
1239 firstb, da_bno, ino);
1240 leafhdr.firstused = firstb;
1241 *repair = 1;
1242 } else {
1243 do_warn(
1244 _("- would reset first used value from %d to %d in "
1245 "block %u of attribute fork of inode %" PRIu64 "\n"),
1246 leafhdr.firstused,
1247 firstb, da_bno, ino);
1248 }
1249 }
1250
1251 if (usedbs != leafhdr.usedbytes) {
1252 if (!no_modify) {
1253 do_warn(
1254 _("- resetting usedbytes cnt from %d to %d in "
1255 "block %u of attribute fork of inode %" PRIu64 "\n"),
1256 leafhdr.usedbytes,
1257 usedbs, da_bno, ino);
1258 leafhdr.usedbytes = usedbs;
1259 *repair = 1;
1260 } else {
1261 do_warn(
1262 _("- would reset usedbytes cnt from %d to %d in "
1263 "block %u of attribute fork of %" PRIu64 "\n"),
1264 leafhdr.usedbytes,
1265 usedbs, da_bno, ino);
1266 }
1267 }
1268
1269 /* there's a lot of work in process_leaf_dir_block to go thru
1270 * checking for holes and compacting if appropiate. I don't think
1271 * attributes need all that, so let's just leave the holes. If
1272 * we discover later that this is a good place to do compaction
1273 * we can add it then.
1274 */
1275 }
1276 if (*repair)
1277 xfs_attr3_leaf_hdr_to_disk(leaf, &leafhdr);
1278
1279 free(attr_freemap);
1280 return (clearit); /* and repair */
1281 }
1282
1283
1284 /*
1285 * returns 0 if the attribute fork is ok, 1 if it has to be junked.
1286 */
1287 static int
1288 process_leaf_attr_level(xfs_mount_t *mp,
1289 da_bt_cursor_t *da_cursor)
1290 {
1291 int repair;
1292 xfs_attr_leafblock_t *leaf;
1293 xfs_buf_t *bp;
1294 xfs_ino_t ino;
1295 xfs_dfsbno_t dev_bno;
1296 xfs_dablk_t da_bno;
1297 xfs_dablk_t prev_bno;
1298 xfs_dahash_t current_hashval = 0;
1299 xfs_dahash_t greatest_hashval;
1300 struct xfs_attr3_icleaf_hdr leafhdr;
1301
1302 da_bno = da_cursor->level[0].bno;
1303 ino = da_cursor->ino;
1304 prev_bno = 0;
1305
1306 do {
1307 repair = 0;
1308 dev_bno = blkmap_get(da_cursor->blkmap, da_bno);
1309 /*
1310 * 0 is the root block and no block
1311 * pointer can point to the root block of the btree
1312 */
1313 ASSERT(da_bno != 0);
1314
1315 if (dev_bno == NULLDFSBNO) {
1316 do_warn(
1317 _("can't map block %u for attribute fork for inode %" PRIu64 "\n"),
1318 da_bno, ino);
1319 goto error_out;
1320 }
1321
1322 bp = libxfs_readbuf(mp->m_dev, XFS_FSB_TO_DADDR(mp, dev_bno),
1323 XFS_FSB_TO_BB(mp, 1), 0, NULL);
1324 if (!bp) {
1325 do_warn(
1326 _("can't read file block %u (fsbno %" PRIu64 ") for attribute fork of inode %" PRIu64 "\n"),
1327 da_bno, dev_bno, ino);
1328 goto error_out;
1329 }
1330
1331 leaf = bp->b_addr;
1332 xfs_attr3_leaf_hdr_from_disk(&leafhdr, leaf);
1333
1334 /* check magic number for leaf directory btree block */
1335 if (!(leafhdr.magic == XFS_ATTR_LEAF_MAGIC ||
1336 leafhdr.magic == XFS_ATTR3_LEAF_MAGIC)) {
1337 do_warn(
1338 _("bad attribute leaf magic %#x for inode %" PRIu64 "\n"),
1339 leafhdr.magic, ino);
1340 libxfs_putbuf(bp);
1341 goto error_out;
1342 }
1343
1344 /*
1345 * for each block, process the block, verify its path,
1346 * then get next block. update cursor values along the way
1347 */
1348 if (process_leaf_attr_block(mp, leaf, da_bno, ino,
1349 da_cursor->blkmap, current_hashval,
1350 &greatest_hashval, &repair)) {
1351 libxfs_putbuf(bp);
1352 goto error_out;
1353 }
1354
1355 /*
1356 * index can be set to hdr.count so match the
1357 * indexes of the interior blocks -- which at the
1358 * end of the block will point to 1 after the final
1359 * real entry in the block
1360 */
1361 da_cursor->level[0].hashval = greatest_hashval;
1362 da_cursor->level[0].bp = bp;
1363 da_cursor->level[0].bno = da_bno;
1364 da_cursor->level[0].index = leafhdr.count;
1365 da_cursor->level[0].dirty = repair;
1366
1367 if (leafhdr.back != prev_bno) {
1368 do_warn(
1369 _("bad sibling back pointer for block %u in attribute fork for inode %" PRIu64 "\n"),
1370 da_bno, ino);
1371 libxfs_putbuf(bp);
1372 goto error_out;
1373 }
1374
1375 prev_bno = da_bno;
1376 da_bno = leafhdr.forw;
1377
1378 if (da_bno != 0 && verify_da_path(mp, da_cursor, 0)) {
1379 libxfs_putbuf(bp);
1380 goto error_out;
1381 }
1382
1383 current_hashval = greatest_hashval;
1384
1385 if (repair && !no_modify)
1386 libxfs_writebuf(bp, 0);
1387 else
1388 libxfs_putbuf(bp);
1389 } while (da_bno != 0);
1390
1391 if (verify_final_da_path(mp, da_cursor, 0)) {
1392 /*
1393 * verify the final path up (right-hand-side) if still ok
1394 */
1395 do_warn(
1396 _("bad hash path in attribute fork for inode %" PRIu64 "\n"),
1397 da_cursor->ino);
1398 goto error_out;
1399 }
1400
1401 /* releases all buffers holding interior btree blocks */
1402 release_da_cursor(mp, da_cursor, 0);
1403 return(0);
1404
1405 error_out:
1406 /* release all buffers holding interior btree blocks */
1407 err_release_da_cursor(mp, da_cursor, 0);
1408 return(1);
1409 }
1410
1411
1412 /*
1413 * a node directory is a true btree -- where the attribute fork
1414 * has gotten big enough that it is represented as a non-trivial (e.g.
1415 * has more than just a block) btree.
1416 *
1417 * Note that if we run into any problems, we will trash the attribute fork.
1418 *
1419 * returns 0 if things are ok, 1 if bad
1420 * Note this code has been based off process_node_dir.
1421 */
1422 static int
1423 process_node_attr(
1424 xfs_mount_t *mp,
1425 xfs_ino_t ino,
1426 xfs_dinode_t *dip,
1427 blkmap_t *blkmap)
1428 {
1429 xfs_dablk_t bno;
1430 int error = 0;
1431 da_bt_cursor_t da_cursor;
1432
1433 /*
1434 * try again -- traverse down left-side of tree until we hit
1435 * the left-most leaf block setting up the btree cursor along
1436 * the way. Then walk the leaf blocks left-to-right, calling
1437 * a parent-verification routine each time we traverse a block.
1438 */
1439 memset(&da_cursor, 0, sizeof(da_bt_cursor_t));
1440 da_cursor.active = 0;
1441 da_cursor.type = 0;
1442 da_cursor.ino = ino;
1443 da_cursor.dip = dip;
1444 da_cursor.greatest_bno = 0;
1445 da_cursor.blkmap = blkmap;
1446
1447 /*
1448 * now process interior node. don't have any buffers held in this path.
1449 */
1450 error = traverse_int_dablock(mp, &da_cursor, &bno, XFS_ATTR_FORK);
1451 if (error == 0)
1452 return(1); /* 0 means unsuccessful */
1453
1454 /*
1455 * now pass cursor and bno into leaf-block processing routine
1456 * the leaf dir level routine checks the interior paths
1457 * up to the root including the final right-most path.
1458 */
1459
1460 return (process_leaf_attr_level(mp, &da_cursor));
1461 }
1462
1463 /*
1464 * Start processing for a leaf or fuller btree.
1465 * A leaf directory is one where the attribute fork is too big for
1466 * the inode but is small enough to fit into one btree block
1467 * outside the inode. This code is modelled after process_leaf_dir_block.
1468 *
1469 * returns 0 if things are ok, 1 if bad (attributes needs to be junked)
1470 * repair is set, if anything was changed, but attributes can live thru it
1471 */
1472 static int
1473 process_longform_attr(
1474 xfs_mount_t *mp,
1475 xfs_ino_t ino,
1476 xfs_dinode_t *dip,
1477 blkmap_t *blkmap,
1478 int *repair) /* out - 1 if something was fixed */
1479 {
1480 xfs_attr_leafblock_t *leaf;
1481 xfs_dfsbno_t bno;
1482 xfs_buf_t *bp;
1483 xfs_dahash_t next_hashval;
1484 int repairlinks = 0;
1485 struct xfs_attr3_icleaf_hdr leafhdr;
1486
1487 *repair = 0;
1488
1489 bno = blkmap_get(blkmap, 0);
1490
1491 if ( bno == NULLDFSBNO ) {
1492 if (dip->di_aformat == XFS_DINODE_FMT_EXTENTS &&
1493 be16_to_cpu(dip->di_anextents) == 0)
1494 return(0); /* the kernel can handle this state */
1495 do_warn(
1496 _("block 0 of inode %" PRIu64 " attribute fork is missing\n"),
1497 ino);
1498 return(1);
1499 }
1500 /* FIX FOR bug 653709 -- EKN */
1501 if (mp->m_sb.sb_agcount < XFS_FSB_TO_AGNO(mp, bno)) {
1502 do_warn(
1503 _("agno of attribute fork of inode %" PRIu64 " out of regular partition\n"), ino);
1504 return(1);
1505 }
1506
1507 bp = libxfs_readbuf(mp->m_dev, XFS_FSB_TO_DADDR(mp, bno),
1508 XFS_FSB_TO_BB(mp, 1), 0, &xfs_da3_node_buf_ops);
1509 if (!bp) {
1510 do_warn(
1511 _("can't read block 0 of inode %" PRIu64 " attribute fork\n"),
1512 ino);
1513 return(1);
1514 }
1515
1516 /* verify leaf block */
1517 leaf = (xfs_attr_leafblock_t *)XFS_BUF_PTR(bp);
1518 xfs_attr3_leaf_hdr_from_disk(&leafhdr, leaf);
1519
1520 /* check sibling pointers in leaf block or root block 0 before
1521 * we have to release the btree block
1522 */
1523 if (leafhdr.forw != 0 || leafhdr.back != 0) {
1524 if (!no_modify) {
1525 do_warn(
1526 _("clearing forw/back pointers in block 0 for attributes in inode %" PRIu64 "\n"),
1527 ino);
1528 repairlinks = 1;
1529 leafhdr.forw = 0;
1530 leafhdr.back = 0;
1531 xfs_attr3_leaf_hdr_to_disk(leaf, &leafhdr);
1532 } else {
1533 do_warn(
1534 _("would clear forw/back pointers in block 0 for attributes in inode %" PRIu64 "\n"), ino);
1535 }
1536 }
1537
1538 /*
1539 * use magic number to tell us what type of attribute this is.
1540 * it's possible to have a node or leaf attribute in either an
1541 * extent format or btree format attribute fork.
1542 */
1543 switch (leafhdr.magic) {
1544 case XFS_ATTR_LEAF_MAGIC: /* leaf-form attribute */
1545 case XFS_ATTR3_LEAF_MAGIC:
1546 if (process_leaf_attr_block(mp, leaf, 0, ino, blkmap,
1547 0, &next_hashval, repair)) {
1548 /* the block is bad. lose the attribute fork. */
1549 libxfs_putbuf(bp);
1550 return(1);
1551 }
1552 *repair = *repair || repairlinks;
1553 break;
1554
1555 case XFS_DA_NODE_MAGIC: /* btree-form attribute */
1556 case XFS_DA3_NODE_MAGIC:
1557 /* must do this now, to release block 0 before the traversal */
1558 if (repairlinks) {
1559 *repair = 1;
1560 libxfs_writebuf(bp, 0);
1561 } else
1562 libxfs_putbuf(bp);
1563 return (process_node_attr(mp, ino, dip, blkmap)); /* + repair */
1564 default:
1565 do_warn(
1566 _("bad attribute leaf magic # %#x for dir ino %" PRIu64 "\n"),
1567 be16_to_cpu(leaf->hdr.info.magic), ino);
1568 libxfs_putbuf(bp);
1569 return(1);
1570 }
1571
1572 if (*repair && !no_modify)
1573 libxfs_writebuf(bp, 0);
1574 else
1575 libxfs_putbuf(bp);
1576
1577 return(0); /* repair may be set */
1578 }
1579
1580
1581 static int
1582 xfs_acl_from_disk(struct xfs_acl **aclp, struct xfs_acl_disk *dacl)
1583 {
1584 int count;
1585 xfs_acl_t *acl;
1586 xfs_acl_entry_t *ace;
1587 xfs_acl_entry_disk_t *dace, *end;
1588
1589 count = be32_to_cpu(dacl->acl_cnt);
1590 if (count > XFS_ACL_MAX_ENTRIES) {
1591 do_warn(_("Too many ACL entries, count %d\n"), count);
1592 *aclp = NULL;
1593 return EINVAL;
1594 }
1595
1596
1597 end = &dacl->acl_entry[0] + count;
1598 acl = malloc((int)((char *)end - (char *)dacl));
1599 if (!acl) {
1600 do_warn(_("cannot malloc enough for ACL attribute\n"));
1601 do_warn(_("SKIPPING this ACL\n"));
1602 *aclp = NULL;
1603 return ENOMEM;
1604 }
1605
1606 acl->acl_cnt = count;
1607 ace = &acl->acl_entry[0];
1608 for (dace = &dacl->acl_entry[0]; dace < end; ace++, dace++) {
1609 ace->ae_tag = be32_to_cpu(dace->ae_tag);
1610 ace->ae_id = be32_to_cpu(dace->ae_id);
1611 ace->ae_perm = be16_to_cpu(dace->ae_perm);
1612 }
1613
1614 *aclp = acl;
1615 return 0;
1616 }
1617
1618 /*
1619 * returns 1 if attributes got cleared
1620 * and 0 if things are ok.
1621 */
1622 int
1623 process_attributes(
1624 xfs_mount_t *mp,
1625 xfs_ino_t ino,
1626 xfs_dinode_t *dip,
1627 blkmap_t *blkmap,
1628 int *repair) /* returned if we did repair */
1629 {
1630 int err;
1631 __u8 aformat = dip->di_aformat;
1632 #ifdef DEBUG
1633 xfs_attr_shortform_t *asf;
1634
1635 asf = (xfs_attr_shortform_t *) XFS_DFORK_APTR(dip);
1636 #endif
1637
1638 if (aformat == XFS_DINODE_FMT_LOCAL) {
1639 ASSERT(be16_to_cpu(asf->hdr.totsize) <=
1640 XFS_DFORK_ASIZE(dip, mp));
1641 err = process_shortform_attr(ino, dip, repair);
1642 } else if (aformat == XFS_DINODE_FMT_EXTENTS ||
1643 aformat == XFS_DINODE_FMT_BTREE) {
1644 err = process_longform_attr(mp, ino, dip, blkmap,
1645 repair);
1646 /* if err, convert this to shortform and clear it */
1647 /* if repair and no error, it's taken care of */
1648 } else {
1649 do_warn(_("illegal attribute format %d, ino %" PRIu64 "\n"),
1650 aformat, ino);
1651 err = 1;
1652 }
1653 return (err); /* and repair */
1654 }
1655
1656 /*
1657 * Validate an ACL
1658 */
1659 static int
1660 xfs_acl_valid(xfs_acl_disk_t *daclp)
1661 {
1662 xfs_acl_t *aclp = NULL;
1663 xfs_acl_entry_t *entry, *e;
1664 int user = 0, group = 0, other = 0, mask = 0, mask_required = 0;
1665 int i, j;
1666
1667 if (daclp == NULL)
1668 goto acl_invalid;
1669
1670 switch (xfs_acl_from_disk(&aclp, daclp)) {
1671 case ENOMEM:
1672 return 0;
1673 case EINVAL:
1674 goto acl_invalid;
1675 default:
1676 break;
1677 }
1678
1679 for (i = 0; i < aclp->acl_cnt; i++) {
1680 entry = &aclp->acl_entry[i];
1681 if (entry->ae_perm & ~(ACL_READ|ACL_WRITE|ACL_EXECUTE))
1682 goto acl_invalid;
1683 switch (entry->ae_tag) {
1684 case ACL_USER_OBJ:
1685 if (user++)
1686 goto acl_invalid;
1687 break;
1688 case ACL_GROUP_OBJ:
1689 if (group++)
1690 goto acl_invalid;
1691 break;
1692 case ACL_OTHER:
1693 if (other++)
1694 goto acl_invalid;
1695 break;
1696 case ACL_USER:
1697 case ACL_GROUP:
1698 for (j = i + 1; j < aclp->acl_cnt; j++) {
1699 e = &aclp->acl_entry[j];
1700 if (e->ae_id == entry->ae_id &&
1701 e->ae_tag == entry->ae_tag)
1702 goto acl_invalid;
1703 }
1704 mask_required++;
1705 break;
1706 case ACL_MASK:
1707 if (mask++)
1708 goto acl_invalid;
1709 break;
1710 default:
1711 goto acl_invalid;
1712 }
1713 }
1714 if (!user || !group || !other || (mask_required && !mask))
1715 goto acl_invalid;
1716 free(aclp);
1717 return 0;
1718 acl_invalid:
1719 free(aclp);
1720 errno = EINVAL;
1721 return (-1);
1722 }
1723
1724 /*
1725 * Check a category or division set to ensure that all values are in
1726 * ascending order and each division or category appears only once.
1727 */
1728 static int
1729 __check_setvalue(const unsigned short *list, unsigned short count)
1730 {
1731 unsigned short i;
1732
1733 for (i = 1; i < count ; i++)
1734 if (list[i] <= list[i-1])
1735 return -1;
1736 return 0;
1737 }
1738
1739 /*
1740 * xfs_mac_valid(lp)
1741 * Check the validity of a MAC label.
1742 */
1743 static int
1744 xfs_mac_valid(xfs_mac_label_t *lp)
1745 {
1746 if (lp == NULL)
1747 return (0);
1748
1749 /*
1750 * if the total category set and division set is greater than 250
1751 * report error
1752 */
1753 if ((lp->ml_catcount + lp->ml_divcount) > XFS_MAC_MAX_SETS)
1754 return(0);
1755
1756 /*
1757 * check whether the msentype value is valid, and do they have
1758 * appropriate level, category association.
1759 */
1760 switch (lp->ml_msen_type) {
1761 case XFS_MSEN_ADMIN_LABEL:
1762 case XFS_MSEN_EQUAL_LABEL:
1763 case XFS_MSEN_HIGH_LABEL:
1764 case XFS_MSEN_MLD_HIGH_LABEL:
1765 case XFS_MSEN_LOW_LABEL:
1766 case XFS_MSEN_MLD_LOW_LABEL:
1767 if (lp->ml_level != 0 || lp->ml_catcount > 0 )
1768 return (0);
1769 break;
1770 case XFS_MSEN_TCSEC_LABEL:
1771 case XFS_MSEN_MLD_LABEL:
1772 if (lp->ml_catcount > 0 &&
1773 __check_setvalue(lp->ml_list,
1774 lp->ml_catcount) == -1)
1775 return (0);
1776 break;
1777 case XFS_MSEN_UNKNOWN_LABEL:
1778 default:
1779 return (0);
1780 }
1781
1782 /*
1783 * check whether the minttype value is valid, and do they have
1784 * appropriate grade, division association.
1785 */
1786 switch (lp->ml_mint_type) {
1787 case XFS_MINT_BIBA_LABEL:
1788 if (lp->ml_divcount > 0 &&
1789 __check_setvalue(lp->ml_list + lp->ml_catcount,
1790 lp->ml_divcount) == -1)
1791 return(0);
1792 break;
1793 case XFS_MINT_EQUAL_LABEL:
1794 case XFS_MINT_HIGH_LABEL:
1795 case XFS_MINT_LOW_LABEL:
1796 if (lp->ml_grade != 0 || lp->ml_divcount > 0 )
1797 return(0);
1798 break;
1799 default:
1800 return(0);
1801 }
1802
1803 return (1);
1804 }