]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - repair/attr_repair.c
xfs_repair: allow '/' in attribute names
[thirdparty/xfsprogs-dev.git] / repair / attr_repair.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2000-2002,2004-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
5 */
6
7 #include "libxfs.h"
8 #include "globals.h"
9 #include "err_protos.h"
10 #include "attr_repair.h"
11 #include "dinode.h"
12 #include "bmap.h"
13 #include "protos.h"
14 #include "dir2.h"
15 #include "da_util.h"
16
17 static int xfs_acl_valid(struct xfs_mount *mp, struct xfs_acl *daclp);
18 static int xfs_mac_valid(xfs_mac_label_t *lp);
19
20 /*
21 * da node check/verify functions that the attribute tree relies on are first in
22 * the file before the actual attribute code. This used to be shared with the
23 * dir v1 code, but that format is no longer supported yb the userspace
24 * utilities and hence is now specific to the attribute tree implementation.
25 */
26
27 typedef unsigned char da_freemap_t;
28
29 /*
30 * Allocate a freespace map for directory or attr leaf blocks (1 bit per byte)
31 * 1 == used, 0 == free.
32 */
33 static da_freemap_t *
34 alloc_da_freemap(struct xfs_mount *mp)
35 {
36 return calloc(1, mp->m_sb.sb_blocksize / NBBY);
37 }
38
39 /*
40 * Set the he range [start, stop) in the directory freemap.
41 *
42 * Returns 1 if there is a conflict or 0 if everything's good.
43 *
44 * Within a char, the lowest bit of the char represents the byte with
45 * the smallest address
46 */
47 static int
48 set_da_freemap(xfs_mount_t *mp, da_freemap_t *map, int start, int stop)
49 {
50 const da_freemap_t mask = 0x1;
51 int i;
52
53 if (start > stop) {
54 /*
55 * allow == relation since [x, x) claims 1 byte
56 */
57 do_warn(_("bad range claimed [%d, %d) in da block\n"),
58 start, stop);
59 return(1);
60 }
61
62 if (stop > mp->m_sb.sb_blocksize) {
63 do_warn(
64 _("byte range end [%d %d) in da block larger than blocksize %d\n"),
65 start, stop, mp->m_sb.sb_blocksize);
66 return(1);
67 }
68
69 for (i = start; i < stop; i ++) {
70 if (map[i / NBBY] & (mask << i % NBBY)) {
71 do_warn(_("multiply claimed byte %d in da block\n"), i);
72 return(1);
73 }
74 map[i / NBBY] |= (mask << i % NBBY);
75 }
76
77 return(0);
78 }
79
80 /*
81 * For attribute repair, there are 3 formats to worry about. First, is
82 * shortform attributes which reside in the inode. Second is the leaf
83 * form, and lastly the btree. Much of this models after the directory
84 * structure so code resembles the directory repair cases.
85 * For shortform case, if an attribute looks corrupt, it is removed.
86 * If that leaves the shortform down to 0 attributes, it's okay and
87 * will appear to just have a null attribute fork. Some checks are done
88 * for validity of the value field based on what the security needs are.
89 * Calls will be made to xfs_mac_valid or xfs_acl_valid routines if the
90 * security attributes exist. They will be cleared if invalid.
91 * No other values will be checked. The DMF folks do not have current
92 * requirements, but may in the future.
93 *
94 * For leaf block attributes, it requires more processing. One sticky
95 * point is that the attributes can be local (within the leaf) or
96 * remote (outside the leaf in other blocks). Thinking of local only
97 * if you get a bad attribute, and want to delete just one, it's a-okay
98 * if it remains large enough to still be a leaf block attribute. Otherwise,
99 * it may have to be converted to shortform. How to convert this and when
100 * is an issue. This call is happening in Phase3. Phase5 will capture empty
101 * blocks, but Phase6 allows you to use the libxfs library which knows
102 * how to handle attributes in the kernel for converting formats. What we
103 * could do is mark an attribute to be cleared now, but in phase6 somehow
104 * have it cleared for real and then the format changed to shortform if
105 * applicable. Since this requires more work than I anticipate can be
106 * accomplished for the next release, we will instead just say any bad
107 * attribute in the leaf block will make the entire attribute fork be
108 * cleared. The simplest way to do that is to ignore the leaf format, and
109 * call clear_dinode_attr to just make a shortform attribute fork with
110 * zero entries.
111 *
112 * Another issue with handling repair on leaf attributes is the remote
113 * blocks. To make sure that they look good and are not used multiple times
114 * by the attribute fork, some mechanism to keep track of all them is necessary.
115 * Do this in the future, time permitting. For now, note that there is no
116 * check for remote blocks and their allocations.
117 *
118 * For btree formatted attributes, the model can follow directories. That
119 * would mean go down the tree to the leftmost leaf. From there moving down
120 * the links and processing each. They would call back up the tree, to verify
121 * that the tree structure is okay. Any problems will result in the attribute
122 * fork being emptied and put in shortform format.
123 */
124
125 static int
126 attr_namecheck(
127 uint8_t *name,
128 int length)
129 {
130 return namecheck((char *)name, length, false);
131 }
132
133 /*
134 * This routine just checks what security needs are for attribute values
135 * only called when root flag is set, otherwise these names could exist in
136 * in user attribute land without a conflict.
137 * If value is non-zero, then a remote attribute is being passed in
138 */
139 static int
140 valuecheck(
141 struct xfs_mount *mp,
142 char *namevalue,
143 char *value,
144 int namelen,
145 int valuelen)
146 {
147 /* for proper alignment issues, get the structs and memmove the values */
148 xfs_mac_label_t macl;
149 void *valuep;
150 int clearit = 0;
151
152 if ((namelen == SGI_ACL_FILE_SIZE &&
153 strncmp(namevalue, SGI_ACL_FILE, SGI_ACL_FILE_SIZE) == 0) ||
154 (namelen == SGI_ACL_DEFAULT_SIZE &&
155 strncmp(namevalue, SGI_ACL_DEFAULT, SGI_ACL_DEFAULT_SIZE) == 0)) {
156 if (value == NULL) {
157 valuep = malloc(valuelen);
158 if (!valuep)
159 do_error(_("No memory for ACL check!\n"));
160 memcpy(valuep, namevalue + namelen, valuelen);
161 } else
162 valuep = value;
163
164 if (xfs_acl_valid(mp, valuep) != 0) {
165 clearit = 1;
166 do_warn(
167 _("entry contains illegal value in attribute named SGI_ACL_FILE "
168 "or SGI_ACL_DEFAULT\n"));
169 }
170
171 if (valuep != value)
172 free(valuep);
173
174 } else if (strncmp(namevalue, SGI_MAC_FILE, SGI_MAC_FILE_SIZE) == 0) {
175 if (value == NULL) {
176 memset(&macl, 0, sizeof(xfs_mac_label_t));
177 memmove(&macl, namevalue+namelen, valuelen);
178 valuep = &macl;
179 } else
180 valuep = value;
181
182 if (xfs_mac_valid((xfs_mac_label_t *)valuep) != 1) { /* 1 is valid */
183 /*
184 * if sysconf says MAC enabled,
185 * temp = mac_from_text("msenhigh/mintlow", NULL)
186 * copy it to value, update valuelen, totsize
187 * This causes pushing up or down of all following
188 * attributes, forcing a attribute format change!!
189 * else clearit = 1;
190 */
191 clearit = 1;
192 do_warn(
193 _("entry contains illegal value in attribute named SGI_MAC_LABEL\n"));
194 }
195 } else if (strncmp(namevalue, SGI_CAP_FILE, SGI_CAP_FILE_SIZE) == 0) {
196 if ( valuelen != sizeof(xfs_cap_set_t)) {
197 clearit = 1;
198 do_warn(
199 _("entry contains illegal value in attribute named SGI_CAP_FILE\n"));
200 }
201 }
202
203 return(clearit);
204 }
205
206
207 /*
208 * this routine validates the attributes in shortform format.
209 * a non-zero return repair value means certain attributes are bogus
210 * and were cleared if possible. Warnings do not generate error conditions
211 * if you cannot modify the structures. repair is set to 1, if anything
212 * was fixed.
213 */
214 static int
215 process_shortform_attr(
216 struct xfs_mount *mp,
217 xfs_ino_t ino,
218 xfs_dinode_t *dip,
219 int *repair)
220 {
221 xfs_attr_shortform_t *asf;
222 xfs_attr_sf_entry_t *currententry, *nextentry, *tempentry;
223 int i, junkit;
224 int currentsize, remainingspace;
225
226 *repair = 0;
227
228 asf = (xfs_attr_shortform_t *) XFS_DFORK_APTR(dip);
229
230 /* Assumption: hdr.totsize is less than a leaf block and was checked
231 * by lclinode for valid sizes. Check the count though.
232 */
233 if (asf->hdr.count == 0)
234 /* then the total size should just be the header length */
235 if (be16_to_cpu(asf->hdr.totsize) != sizeof(xfs_attr_sf_hdr_t)) {
236 /* whoops there's a discrepancy. Clear the hdr */
237 if (!no_modify) {
238 do_warn(
239 _("there are no attributes in the fork for inode %" PRIu64 "\n"),
240 ino);
241 asf->hdr.totsize =
242 cpu_to_be16(sizeof(xfs_attr_sf_hdr_t));
243 *repair = 1;
244 return(1);
245 } else {
246 do_warn(
247 _("would junk the attribute fork since count is 0 for inode %" PRIu64 "\n"),
248 ino);
249 return(1);
250 }
251 }
252
253 currentsize = sizeof(xfs_attr_sf_hdr_t);
254 remainingspace = be16_to_cpu(asf->hdr.totsize) - currentsize;
255 nextentry = &asf->list[0];
256 for (i = 0; i < asf->hdr.count; i++) {
257 currententry = nextentry;
258 junkit = 0;
259
260 /* don't go off the end if the hdr.count was off */
261 if ((currentsize + (sizeof(xfs_attr_sf_entry_t) - 1)) >
262 be16_to_cpu(asf->hdr.totsize))
263 break; /* get out and reset count and totSize */
264
265 /* if the namelen is 0, can't get to the rest of the entries */
266 if (currententry->namelen == 0) {
267 do_warn(_("zero length name entry in attribute fork,"));
268 if (!no_modify) {
269 do_warn(
270 _(" truncating attributes for inode %" PRIu64 " to %d\n"), ino, i);
271 *repair = 1;
272 break; /* and then update hdr fields */
273 } else {
274 do_warn(
275 _(" would truncate attributes for inode %" PRIu64 " to %d\n"), ino, i);
276 break;
277 }
278 } else {
279 /* It's okay to have a 0 length valuelen, but do a
280 * rough check to make sure we haven't gone outside of
281 * totsize.
282 */
283 if (remainingspace < currententry->namelen ||
284 ((remainingspace - currententry->
285 namelen) < currententry->valuelen)) {
286 do_warn(
287 _("name or value attribute lengths are too large,\n"));
288 if (!no_modify) {
289 do_warn(
290 _(" truncating attributes for inode %" PRIu64 " to %d\n"),
291 ino, i);
292 *repair = 1;
293 break; /* and then update hdr fields */
294 } else {
295 do_warn(
296 _(" would truncate attributes for inode %" PRIu64 " to %d\n"),
297 ino, i);
298 break;
299 }
300 }
301 }
302
303 /* namecheck checks for null chars in attr names. */
304 if (attr_namecheck(currententry->nameval,
305 currententry->namelen)) {
306 do_warn(
307 _("entry contains illegal character in shortform attribute name\n"));
308 junkit = 1;
309 }
310
311 if (currententry->flags & XFS_ATTR_INCOMPLETE) {
312 do_warn(
313 _("entry has INCOMPLETE flag on in shortform attribute\n"));
314 junkit = 1;
315 }
316
317 /* Only check values for root security attributes */
318 if (currententry->flags & XFS_ATTR_ROOT)
319 junkit |= valuecheck(mp,
320 (char *)&currententry->nameval[0],
321 NULL, currententry->namelen,
322 currententry->valuelen);
323
324 remainingspace = remainingspace -
325 XFS_ATTR_SF_ENTSIZE(currententry);
326
327 if (junkit) {
328 if (!no_modify) {
329 /* get rid of only this entry */
330 do_warn(
331 _("removing attribute entry %d for inode %" PRIu64 "\n"),
332 i, ino);
333 tempentry = (xfs_attr_sf_entry_t *)
334 ((intptr_t) currententry +
335 XFS_ATTR_SF_ENTSIZE(currententry));
336 memmove(currententry,tempentry,remainingspace);
337 asf->hdr.count -= 1;
338 i--; /* no worries, it will wrap back to 0 */
339 *repair = 1;
340 continue; /* go back up now */
341 } else {
342 do_warn(
343 _("would remove attribute entry %d for inode %" PRIu64 "\n"),
344 i, ino);
345 }
346 }
347
348 /* Let's get ready for the next entry... */
349 nextentry = (xfs_attr_sf_entry_t *)((intptr_t) nextentry +
350 XFS_ATTR_SF_ENTSIZE(currententry));
351 currentsize = currentsize + XFS_ATTR_SF_ENTSIZE(currententry);
352
353 } /* end the loop */
354
355 if (asf->hdr.count != i) {
356 if (no_modify) {
357 do_warn(
358 _("would have corrected attribute entry count in inode %" PRIu64 " from %d to %d\n"),
359 ino, asf->hdr.count, i);
360 } else {
361 do_warn(
362 _("corrected attribute entry count in inode %" PRIu64 ", was %d, now %d\n"),
363 ino, asf->hdr.count, i);
364 asf->hdr.count = i;
365 *repair = 1;
366 }
367 }
368
369 /* ASSUMPTION: currentsize <= totsize */
370 if (be16_to_cpu(asf->hdr.totsize) != currentsize) {
371 if (no_modify) {
372 do_warn(
373 _("would have corrected attribute totsize in inode %" PRIu64 " from %d to %d\n"),
374 ino, be16_to_cpu(asf->hdr.totsize),
375 currentsize);
376 } else {
377 do_warn(
378 _("corrected attribute entry totsize in inode %" PRIu64 ", was %d, now %d\n"),
379 ino, be16_to_cpu(asf->hdr.totsize),
380 currentsize);
381 asf->hdr.totsize = cpu_to_be16(currentsize);
382 *repair = 1;
383 }
384 }
385
386 return(*repair);
387 }
388
389 /* This routine brings in blocks from disk one by one and assembles them
390 * in the value buffer. If get_bmapi gets smarter later to return an extent
391 * or list of extents, that would be great. For now, we don't expect too
392 * many blocks per remote value, so one by one is sufficient.
393 */
394 static int
395 rmtval_get(xfs_mount_t *mp, xfs_ino_t ino, blkmap_t *blkmap,
396 xfs_dablk_t blocknum, int valuelen, char* value)
397 {
398 xfs_fsblock_t bno;
399 xfs_buf_t *bp;
400 int clearit = 0, i = 0, length = 0, amountdone = 0;
401 int hdrsize = 0;
402
403 if (xfs_sb_version_hascrc(&mp->m_sb))
404 hdrsize = sizeof(struct xfs_attr3_rmt_hdr);
405
406 /* ASSUMPTION: valuelen is a valid number, so use it for looping */
407 /* Note that valuelen is not a multiple of blocksize */
408 while (amountdone < valuelen) {
409 bno = blkmap_get(blkmap, blocknum + i);
410 if (bno == NULLFSBLOCK) {
411 do_warn(
412 _("remote block for attributes of inode %" PRIu64 " is missing\n"), ino);
413 clearit = 1;
414 break;
415 }
416 bp = libxfs_readbuf(mp->m_dev, XFS_FSB_TO_DADDR(mp, bno),
417 XFS_FSB_TO_BB(mp, 1), 0,
418 &xfs_attr3_rmt_buf_ops);
419 if (!bp) {
420 do_warn(
421 _("can't read remote block for attributes of inode %" PRIu64 "\n"), ino);
422 clearit = 1;
423 break;
424 }
425
426 if (bp->b_error == -EFSBADCRC || bp->b_error == -EFSCORRUPTED) {
427 do_warn(
428 _("Corrupt remote block for attributes of inode %" PRIu64 "\n"), ino);
429 libxfs_putbuf(bp);
430 clearit = 1;
431 break;
432 }
433
434 ASSERT(mp->m_sb.sb_blocksize == bp->b_bcount);
435
436 length = min(bp->b_bcount - hdrsize, valuelen - amountdone);
437 memmove(value, bp->b_addr + hdrsize, length);
438 amountdone += length;
439 value += length;
440 i++;
441 libxfs_putbuf(bp);
442 }
443 return (clearit);
444 }
445
446 /* The block is read in. The magic number and forward / backward
447 * links are checked by the caller process_leaf_attr.
448 * If any problems occur the routine returns with non-zero. In
449 * this case the next step is to clear the attribute fork, by
450 * changing it to shortform and zeroing it out. Forkoff need not
451 * be changed.
452 */
453
454 static int
455 process_leaf_attr_local(
456 struct xfs_mount *mp,
457 xfs_attr_leafblock_t *leaf,
458 int i,
459 xfs_attr_leaf_entry_t *entry,
460 xfs_dahash_t last_hashval,
461 xfs_dablk_t da_bno,
462 xfs_ino_t ino)
463 {
464 xfs_attr_leaf_name_local_t *local;
465
466 local = xfs_attr3_leaf_name_local(leaf, i);
467 if (local->namelen == 0 || attr_namecheck(local->nameval,
468 local->namelen)) {
469 do_warn(
470 _("attribute entry %d in attr block %u, inode %" PRIu64 " has bad name (namelen = %d)\n"),
471 i, da_bno, ino, local->namelen);
472 return -1;
473 }
474
475 /* Check on the hash value. Checking order of values
476 * is not necessary, since one wrong clears the whole
477 * fork. If the ordering's wrong, it's caught here or
478 * the kernel code has a bug with transaction logging
479 * or attributes itself. Being paranoid, let's check
480 * ordering anyway in case both the name value and the
481 * hashvalue were wrong but matched. Unlikely, however.
482 */
483 if (be32_to_cpu(entry->hashval) != libxfs_da_hashname(
484 &local->nameval[0], local->namelen) ||
485 be32_to_cpu(entry->hashval) < last_hashval) {
486 do_warn(
487 _("bad hashvalue for attribute entry %d in attr block %u, inode %" PRIu64 "\n"),
488 i, da_bno, ino);
489 return -1;
490 }
491
492 /* Only check values for root security attributes */
493 if (entry->flags & XFS_ATTR_ROOT) {
494 if (valuecheck(mp, (char *)&local->nameval[0], NULL,
495 local->namelen, be16_to_cpu(local->valuelen))) {
496 do_warn(
497 _("bad security value for attribute entry %d in attr block %u, inode %" PRIu64 "\n"),
498 i, da_bno, ino);
499 return -1;
500 }
501 }
502 return xfs_attr_leaf_entsize_local(local->namelen,
503 be16_to_cpu(local->valuelen));
504 }
505
506 static int
507 process_leaf_attr_remote(
508 xfs_attr_leafblock_t *leaf,
509 int i,
510 xfs_attr_leaf_entry_t *entry,
511 xfs_dahash_t last_hashval,
512 xfs_dablk_t da_bno,
513 xfs_ino_t ino,
514 xfs_mount_t *mp,
515 blkmap_t *blkmap)
516 {
517 xfs_attr_leaf_name_remote_t *remotep;
518 char* value;
519
520 remotep = xfs_attr3_leaf_name_remote(leaf, i);
521
522 if (remotep->namelen == 0 || attr_namecheck(remotep->name,
523 remotep->namelen) ||
524 be32_to_cpu(entry->hashval) !=
525 libxfs_da_hashname((unsigned char *)&remotep->name[0],
526 remotep->namelen) ||
527 be32_to_cpu(entry->hashval) < last_hashval ||
528 be32_to_cpu(remotep->valueblk) == 0) {
529 do_warn(
530 _("inconsistent remote attribute entry %d in attr block %u, ino %" PRIu64 "\n"), i, da_bno, ino);
531 return -1;
532 }
533
534 value = malloc(be32_to_cpu(remotep->valuelen));
535 if (value == NULL) {
536 do_warn(
537 _("cannot malloc enough for remotevalue attribute for inode %" PRIu64 "\n"),
538 ino);
539 do_warn(_("SKIPPING this remote attribute\n"));
540 goto out;
541 }
542 if (rmtval_get(mp, ino, blkmap, be32_to_cpu(remotep->valueblk),
543 be32_to_cpu(remotep->valuelen), value)) {
544 do_warn(
545 _("remote attribute get failed for entry %d, inode %" PRIu64 "\n"),
546 i, ino);
547 goto bad_free_out;
548 }
549 if ((entry->flags & XFS_ATTR_ROOT) &&
550 valuecheck(mp, (char *)&remotep->name[0], value, remotep->namelen,
551 be32_to_cpu(remotep->valuelen))) {
552 do_warn(
553 _("remote attribute value check failed for entry %d, inode %" PRIu64 "\n"),
554 i, ino);
555 goto bad_free_out;
556 }
557 free(value);
558 out:
559 return xfs_attr_leaf_entsize_remote(remotep->namelen);
560
561 bad_free_out:
562 free(value);
563 return -1;
564 }
565
566 static int
567 process_leaf_attr_block(
568 xfs_mount_t *mp,
569 xfs_attr_leafblock_t *leaf,
570 xfs_dablk_t da_bno,
571 xfs_ino_t ino,
572 blkmap_t *blkmap,
573 xfs_dahash_t last_hashval,
574 xfs_dahash_t *current_hashval,
575 int *repair)
576 {
577 xfs_attr_leaf_entry_t *entry;
578 int i, start, stop, clearit, usedbs, firstb, thissize;
579 da_freemap_t *attr_freemap;
580 struct xfs_attr3_icleaf_hdr leafhdr;
581
582 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &leafhdr, leaf);
583 clearit = usedbs = 0;
584 firstb = mp->m_sb.sb_blocksize;
585 stop = xfs_attr3_leaf_hdr_size(leaf);
586
587 /* does the count look sorta valid? */
588 if (!leafhdr.count ||
589 leafhdr.count * sizeof(xfs_attr_leaf_entry_t) + stop >
590 mp->m_sb.sb_blocksize) {
591 do_warn(
592 _("bad attribute count %d in attr block %u, inode %" PRIu64 "\n"),
593 leafhdr.count, da_bno, ino);
594 return 1;
595 }
596
597 attr_freemap = alloc_da_freemap(mp);
598 (void) set_da_freemap(mp, attr_freemap, 0, stop);
599
600 /* go thru each entry checking for problems */
601 for (i = 0, entry = xfs_attr3_leaf_entryp(leaf);
602 i < leafhdr.count; i++, entry++) {
603
604 /* check if index is within some boundary. */
605 if (be16_to_cpu(entry->nameidx) > mp->m_sb.sb_blocksize) {
606 do_warn(
607 _("bad attribute nameidx %d in attr block %u, inode %" PRIu64 "\n"),
608 be16_to_cpu(entry->nameidx), da_bno, ino);
609 clearit = 1;
610 break;
611 }
612
613 if (entry->flags & XFS_ATTR_INCOMPLETE) {
614 /* we are inconsistent state. get rid of us */
615 do_warn(
616 _("attribute entry #%d in attr block %u, inode %" PRIu64 " is INCOMPLETE\n"),
617 i, da_bno, ino);
618 clearit = 1;
619 break;
620 }
621
622 /* mark the entry used */
623 start = (intptr_t)entry - (intptr_t)leaf;
624 stop = start + sizeof(xfs_attr_leaf_entry_t);
625 if (set_da_freemap(mp, attr_freemap, start, stop)) {
626 do_warn(
627 _("attribute entry %d in attr block %u, inode %" PRIu64 " claims already used space\n"),
628 i, da_bno, ino);
629 clearit = 1;
630 break; /* got an overlap */
631 }
632
633 if (entry->flags & XFS_ATTR_LOCAL)
634 thissize = process_leaf_attr_local(mp, leaf, i, entry,
635 last_hashval, da_bno, ino);
636 else
637 thissize = process_leaf_attr_remote(leaf, i, entry,
638 last_hashval, da_bno, ino,
639 mp, blkmap);
640 if (thissize < 0) {
641 clearit = 1;
642 break;
643 }
644
645 *current_hashval = last_hashval = be32_to_cpu(entry->hashval);
646
647 if (set_da_freemap(mp, attr_freemap, be16_to_cpu(entry->nameidx),
648 be16_to_cpu(entry->nameidx) + thissize)) {
649 do_warn(
650 _("attribute entry %d in attr block %u, inode %" PRIu64 " claims used space\n"),
651 i, da_bno, ino);
652 clearit = 1;
653 break; /* got an overlap */
654 }
655 usedbs += thissize;
656 if (be16_to_cpu(entry->nameidx) < firstb)
657 firstb = be16_to_cpu(entry->nameidx);
658
659 } /* end the loop */
660
661 if (!clearit) {
662 /* verify the header information is correct */
663
664 /* if the holes flag is set, don't reset first_used unless it's
665 * pointing to used bytes. we're being conservative here
666 * since the block will get compacted anyhow by the kernel.
667 */
668
669 if ((leafhdr.holes == 0 &&
670 firstb != leafhdr.firstused) ||
671 leafhdr.firstused > firstb) {
672 if (!no_modify) {
673 do_warn(
674 _("- resetting first used heap value from %d to %d in "
675 "block %u of attribute fork of inode %" PRIu64 "\n"),
676 leafhdr.firstused,
677 firstb, da_bno, ino);
678 leafhdr.firstused = firstb;
679 *repair = 1;
680 } else {
681 do_warn(
682 _("- would reset first used value from %d to %d in "
683 "block %u of attribute fork of inode %" PRIu64 "\n"),
684 leafhdr.firstused,
685 firstb, da_bno, ino);
686 }
687 }
688
689 if (usedbs != leafhdr.usedbytes) {
690 if (!no_modify) {
691 do_warn(
692 _("- resetting usedbytes cnt from %d to %d in "
693 "block %u of attribute fork of inode %" PRIu64 "\n"),
694 leafhdr.usedbytes,
695 usedbs, da_bno, ino);
696 leafhdr.usedbytes = usedbs;
697 *repair = 1;
698 } else {
699 do_warn(
700 _("- would reset usedbytes cnt from %d to %d in "
701 "block %u of attribute fork of %" PRIu64 "\n"),
702 leafhdr.usedbytes,
703 usedbs, da_bno, ino);
704 }
705 }
706
707 /* there's a lot of work in process_leaf_dir_block to go thru
708 * checking for holes and compacting if appropiate. I don't think
709 * attributes need all that, so let's just leave the holes. If
710 * we discover later that this is a good place to do compaction
711 * we can add it then.
712 */
713 }
714 /*
715 * If we're just going to zap the block, don't pretend like we
716 * repaired it, because repairing the block stops the clear
717 * operation.
718 */
719 if (clearit)
720 *repair = 0;
721 if (*repair)
722 xfs_attr3_leaf_hdr_to_disk(mp->m_attr_geo, leaf, &leafhdr);
723
724 free(attr_freemap);
725 return (clearit); /* and repair */
726 }
727
728
729 /*
730 * returns 0 if the attribute fork is ok, 1 if it has to be junked.
731 */
732 static int
733 process_leaf_attr_level(xfs_mount_t *mp,
734 da_bt_cursor_t *da_cursor)
735 {
736 int repair;
737 xfs_attr_leafblock_t *leaf;
738 xfs_buf_t *bp;
739 xfs_ino_t ino;
740 xfs_fsblock_t dev_bno;
741 xfs_dablk_t da_bno;
742 xfs_dablk_t prev_bno;
743 xfs_dahash_t current_hashval = 0;
744 xfs_dahash_t greatest_hashval;
745 struct xfs_attr3_icleaf_hdr leafhdr;
746
747 da_bno = da_cursor->level[0].bno;
748 ino = da_cursor->ino;
749 /*
750 * 0 is the root block and no block
751 * pointer can point to the root block of the btree
752 */
753 if (da_bno == 0) {
754 do_warn(
755 _("btree cycle detected in attribute fork for inode %" PRIu64 "\n"),
756 ino);
757 goto error_out;
758 }
759
760 prev_bno = 0;
761
762 do {
763 repair = 0;
764 dev_bno = blkmap_get(da_cursor->blkmap, da_bno);
765 if (dev_bno == NULLFSBLOCK) {
766 do_warn(
767 _("can't map block %u for attribute fork for inode %" PRIu64 "\n"),
768 da_bno, ino);
769 goto error_out;
770 }
771
772 bp = libxfs_readbuf(mp->m_dev, XFS_FSB_TO_DADDR(mp, dev_bno),
773 XFS_FSB_TO_BB(mp, 1), 0,
774 &xfs_attr3_leaf_buf_ops);
775 if (!bp) {
776 do_warn(
777 _("can't read file block %u (fsbno %" PRIu64 ") for attribute fork of inode %" PRIu64 "\n"),
778 da_bno, dev_bno, ino);
779 goto error_out;
780 }
781
782 leaf = bp->b_addr;
783 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &leafhdr, leaf);
784
785 /* check magic number for leaf directory btree block */
786 if (!(leafhdr.magic == XFS_ATTR_LEAF_MAGIC ||
787 leafhdr.magic == XFS_ATTR3_LEAF_MAGIC)) {
788 do_warn(
789 _("bad attribute leaf magic %#x for inode %" PRIu64 "\n"),
790 leafhdr.magic, ino);
791 libxfs_putbuf(bp);
792 goto error_out;
793 }
794
795 /*
796 * for each block, process the block, verify its path,
797 * then get next block. update cursor values along the way
798 */
799 if (process_leaf_attr_block(mp, leaf, da_bno, ino,
800 da_cursor->blkmap, current_hashval,
801 &greatest_hashval, &repair)) {
802 libxfs_putbuf(bp);
803 goto error_out;
804 }
805
806 /*
807 * index can be set to hdr.count so match the
808 * indexes of the interior blocks -- which at the
809 * end of the block will point to 1 after the final
810 * real entry in the block
811 */
812 da_cursor->level[0].hashval = greatest_hashval;
813 da_cursor->level[0].bp = bp;
814 da_cursor->level[0].bno = da_bno;
815 da_cursor->level[0].index = leafhdr.count;
816 da_cursor->level[0].dirty = repair;
817
818 if (leafhdr.back != prev_bno) {
819 do_warn(
820 _("bad sibling back pointer for block %u in attribute fork for inode %" PRIu64 "\n"),
821 da_bno, ino);
822 libxfs_putbuf(bp);
823 goto error_out;
824 }
825
826 prev_bno = da_bno;
827 da_bno = leafhdr.forw;
828
829 if (da_bno != 0) {
830 if (verify_da_path(mp, da_cursor, 0, XFS_ATTR_FORK)) {
831 libxfs_putbuf(bp);
832 goto error_out;
833 }
834 }
835
836 current_hashval = greatest_hashval;
837 /*
838 * If block looks ok but CRC didn't match, make sure to
839 * recompute it.
840 */
841 if (!no_modify && bp->b_error == -EFSBADCRC)
842 repair++;
843
844 if (repair && !no_modify)
845 libxfs_writebuf(bp, 0);
846 else
847 libxfs_putbuf(bp);
848 } while (da_bno != 0);
849
850 if (verify_final_da_path(mp, da_cursor, 0, XFS_ATTR_FORK)) {
851 /*
852 * verify the final path up (right-hand-side) if still ok
853 */
854 do_warn(
855 _("bad hash path in attribute fork for inode %" PRIu64 "\n"),
856 da_cursor->ino);
857 goto error_out;
858 }
859
860 /* releases all buffers holding interior btree blocks */
861 release_da_cursor(mp, da_cursor, 0);
862 return(0);
863
864 error_out:
865 /* release all buffers holding interior btree blocks */
866 err_release_da_cursor(mp, da_cursor, 0);
867 return(1);
868 }
869
870
871 /*
872 * a node directory is a true btree -- where the attribute fork
873 * has gotten big enough that it is represented as a non-trivial (e.g.
874 * has more than just a block) btree.
875 *
876 * Note that if we run into any problems, we will trash the attribute fork.
877 *
878 * returns 0 if things are ok, 1 if bad
879 * Note this code has been based off process_node_dir.
880 */
881 static int
882 process_node_attr(
883 xfs_mount_t *mp,
884 xfs_ino_t ino,
885 xfs_dinode_t *dip,
886 blkmap_t *blkmap)
887 {
888 xfs_dablk_t bno;
889 int error = 0;
890 da_bt_cursor_t da_cursor;
891
892 /*
893 * try again -- traverse down left-side of tree until we hit
894 * the left-most leaf block setting up the btree cursor along
895 * the way. Then walk the leaf blocks left-to-right, calling
896 * a parent-verification routine each time we traverse a block.
897 */
898 memset(&da_cursor, 0, sizeof(da_bt_cursor_t));
899 da_cursor.active = 0;
900 da_cursor.ino = ino;
901 da_cursor.dip = dip;
902 da_cursor.greatest_bno = 0;
903 da_cursor.blkmap = blkmap;
904
905 /*
906 * now process interior node. don't have any buffers held in this path.
907 */
908 error = traverse_int_dablock(mp, &da_cursor, &bno, XFS_ATTR_FORK);
909 if (error == 0)
910 return(1); /* 0 means unsuccessful */
911
912 /*
913 * now pass cursor and bno into leaf-block processing routine
914 * the leaf dir level routine checks the interior paths
915 * up to the root including the final right-most path.
916 */
917
918 return (process_leaf_attr_level(mp, &da_cursor));
919 }
920
921 /* check v5 metadata */
922 static int
923 __check_attr_header(
924 struct xfs_mount *mp,
925 struct xfs_buf *bp,
926 xfs_ino_t ino)
927 {
928 struct xfs_da3_blkinfo *info = bp->b_addr;
929
930 if (info->hdr.magic != cpu_to_be16(XFS_ATTR3_LEAF_MAGIC) &&
931 info->hdr.magic != cpu_to_be16(XFS_DA3_NODE_MAGIC))
932 return 0;
933
934 /* verify owner */
935 if (be64_to_cpu(info->owner) != ino) {
936 do_warn(
937 _("expected owner inode %" PRIu64 ", got %llu, attr block %" PRIu64 "\n"),
938 ino, (unsigned long long)be64_to_cpu(info->owner),
939 bp->b_bn);
940 return 1;
941 }
942 /* verify block number */
943 if (be64_to_cpu(info->blkno) != bp->b_bn) {
944 do_warn(
945 _("expected block %" PRIu64 ", got %llu, inode %" PRIu64 "attr block\n"),
946 bp->b_bn, (unsigned long long)be64_to_cpu(info->blkno),
947 ino);
948 return 1;
949 }
950 /* verify uuid */
951 if (platform_uuid_compare(&info->uuid, &mp->m_sb.sb_meta_uuid) != 0) {
952 do_warn(
953 _("wrong FS UUID, inode %" PRIu64 " attr block %" PRIu64 "\n"),
954 ino, bp->b_bn);
955 return 1;
956 }
957
958 return 0;
959 }
960
961 /*
962 * Start processing for a leaf or fuller btree.
963 * A leaf directory is one where the attribute fork is too big for
964 * the inode but is small enough to fit into one btree block
965 * outside the inode. This code is modelled after process_leaf_dir_block.
966 *
967 * returns 0 if things are ok, 1 if bad (attributes needs to be junked)
968 * repair is set, if anything was changed, but attributes can live thru it
969 */
970 static int
971 process_longform_attr(
972 xfs_mount_t *mp,
973 xfs_ino_t ino,
974 xfs_dinode_t *dip,
975 blkmap_t *blkmap,
976 int *repair) /* out - 1 if something was fixed */
977 {
978 xfs_attr_leafblock_t *leaf;
979 xfs_fsblock_t bno;
980 xfs_buf_t *bp;
981 xfs_dahash_t next_hashval;
982 int repairlinks = 0;
983 struct xfs_attr3_icleaf_hdr leafhdr;
984 int error;
985
986 *repair = 0;
987
988 bno = blkmap_get(blkmap, 0);
989
990 if ( bno == NULLFSBLOCK ) {
991 if (dip->di_aformat == XFS_DINODE_FMT_EXTENTS &&
992 be16_to_cpu(dip->di_anextents) == 0)
993 return(0); /* the kernel can handle this state */
994 do_warn(
995 _("block 0 of inode %" PRIu64 " attribute fork is missing\n"),
996 ino);
997 return(1);
998 }
999 /* FIX FOR bug 653709 -- EKN */
1000 if (mp->m_sb.sb_agcount < XFS_FSB_TO_AGNO(mp, bno)) {
1001 do_warn(
1002 _("agno of attribute fork of inode %" PRIu64 " out of regular partition\n"), ino);
1003 return(1);
1004 }
1005
1006 bp = libxfs_readbuf(mp->m_dev, XFS_FSB_TO_DADDR(mp, bno),
1007 XFS_FSB_TO_BB(mp, 1), 0, &xfs_da3_node_buf_ops);
1008 if (!bp) {
1009 do_warn(
1010 _("can't read block 0 of inode %" PRIu64 " attribute fork\n"),
1011 ino);
1012 return(1);
1013 }
1014 if (bp->b_error == -EFSBADCRC)
1015 (*repair)++;
1016
1017 /* is this block sane? */
1018 if (__check_attr_header(mp, bp, ino)) {
1019 *repair = 0;
1020 libxfs_putbuf(bp);
1021 return 1;
1022 }
1023
1024 /* verify leaf block */
1025 leaf = bp->b_addr;
1026 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &leafhdr, leaf);
1027
1028 /* check sibling pointers in leaf block or root block 0 before
1029 * we have to release the btree block
1030 */
1031 if (leafhdr.forw != 0 || leafhdr.back != 0) {
1032 if (!no_modify) {
1033 do_warn(
1034 _("clearing forw/back pointers in block 0 for attributes in inode %" PRIu64 "\n"),
1035 ino);
1036 repairlinks = 1;
1037 leafhdr.forw = 0;
1038 leafhdr.back = 0;
1039 xfs_attr3_leaf_hdr_to_disk(mp->m_attr_geo,
1040 leaf, &leafhdr);
1041 } else {
1042 do_warn(
1043 _("would clear forw/back pointers in block 0 for attributes in inode %" PRIu64 "\n"), ino);
1044 }
1045 }
1046
1047 /*
1048 * use magic number to tell us what type of attribute this is.
1049 * it's possible to have a node or leaf attribute in either an
1050 * extent format or btree format attribute fork.
1051 */
1052 switch (leafhdr.magic) {
1053 case XFS_ATTR_LEAF_MAGIC: /* leaf-form attribute */
1054 case XFS_ATTR3_LEAF_MAGIC:
1055 if (process_leaf_attr_block(mp, leaf, 0, ino, blkmap,
1056 0, &next_hashval, repair)) {
1057 *repair = 0;
1058 /* the block is bad. lose the attribute fork. */
1059 libxfs_putbuf(bp);
1060 return(1);
1061 }
1062 *repair = *repair || repairlinks;
1063 break;
1064
1065 case XFS_DA_NODE_MAGIC: /* btree-form attribute */
1066 case XFS_DA3_NODE_MAGIC:
1067 /* must do this now, to release block 0 before the traversal */
1068 if ((*repair || repairlinks) && !no_modify) {
1069 *repair = 1;
1070 libxfs_writebuf(bp, 0);
1071 } else
1072 libxfs_putbuf(bp);
1073 error = process_node_attr(mp, ino, dip, blkmap); /* + repair */
1074 if (error)
1075 *repair = 0;
1076 return error;
1077 default:
1078 do_warn(
1079 _("bad attribute leaf magic # %#x for dir ino %" PRIu64 "\n"),
1080 be16_to_cpu(leaf->hdr.info.magic), ino);
1081 libxfs_putbuf(bp);
1082 *repair = 0;
1083 return(1);
1084 }
1085
1086 if (*repair && !no_modify)
1087 libxfs_writebuf(bp, 0);
1088 else
1089 libxfs_putbuf(bp);
1090
1091 return(0); /* repair may be set */
1092 }
1093
1094
1095 static int
1096 xfs_acl_from_disk(
1097 struct xfs_mount *mp,
1098 struct xfs_icacl **aclp,
1099 struct xfs_acl *dacl)
1100 {
1101 struct xfs_icacl *acl;
1102 struct xfs_icacl_entry *ace;
1103 struct xfs_acl_entry *dace;
1104 int count;
1105 int i;
1106
1107 count = be32_to_cpu(dacl->acl_cnt);
1108 if (count > XFS_ACL_MAX_ENTRIES(mp)) {
1109 do_warn(_("Too many ACL entries, count %d\n"), count);
1110 *aclp = NULL;
1111 return EINVAL;
1112 }
1113
1114
1115 acl = malloc(sizeof(struct xfs_icacl) +
1116 count * sizeof(struct xfs_icacl_entry));
1117 if (!acl) {
1118 do_warn(_("cannot malloc enough for ACL attribute\n"));
1119 do_warn(_("SKIPPING this ACL\n"));
1120 *aclp = NULL;
1121 return ENOMEM;
1122 }
1123
1124 acl->acl_cnt = count;
1125 for (i = 0; i < count; i++) {
1126 ace = &acl->acl_entry[i];
1127 dace = &dacl->acl_entry[i];
1128
1129 ace->ae_tag = be32_to_cpu(dace->ae_tag);
1130 ace->ae_id = be32_to_cpu(dace->ae_id);
1131 ace->ae_perm = be16_to_cpu(dace->ae_perm);
1132 }
1133
1134 *aclp = acl;
1135 return 0;
1136 }
1137
1138 /*
1139 * returns 1 if attributes got cleared
1140 * and 0 if things are ok.
1141 */
1142 int
1143 process_attributes(
1144 xfs_mount_t *mp,
1145 xfs_ino_t ino,
1146 xfs_dinode_t *dip,
1147 blkmap_t *blkmap,
1148 int *repair) /* returned if we did repair */
1149 {
1150 int err;
1151 __u8 aformat = dip->di_aformat;
1152 #ifdef DEBUG
1153 xfs_attr_shortform_t *asf;
1154
1155 asf = (xfs_attr_shortform_t *) XFS_DFORK_APTR(dip);
1156 #endif
1157
1158 if (aformat == XFS_DINODE_FMT_LOCAL) {
1159 ASSERT(be16_to_cpu(asf->hdr.totsize) <=
1160 XFS_DFORK_ASIZE(dip, mp));
1161 err = process_shortform_attr(mp, ino, dip, repair);
1162 } else if (aformat == XFS_DINODE_FMT_EXTENTS ||
1163 aformat == XFS_DINODE_FMT_BTREE) {
1164 err = process_longform_attr(mp, ino, dip, blkmap,
1165 repair);
1166 /* if err, convert this to shortform and clear it */
1167 /* if repair and no error, it's taken care of */
1168 } else {
1169 do_warn(_("illegal attribute format %d, ino %" PRIu64 "\n"),
1170 aformat, ino);
1171 err = 1;
1172 }
1173 return (err); /* and repair */
1174 }
1175
1176 /*
1177 * Validate an ACL
1178 */
1179 static int
1180 xfs_acl_valid(
1181 struct xfs_mount *mp,
1182 struct xfs_acl *daclp)
1183 {
1184 struct xfs_icacl *aclp = NULL;
1185 struct xfs_icacl_entry *entry, *e;
1186 int user = 0, group = 0, other = 0, mask = 0, mask_required = 0;
1187 int i, j;
1188
1189 if (daclp == NULL)
1190 goto acl_invalid;
1191
1192 switch (xfs_acl_from_disk(mp, &aclp, daclp)) {
1193 case ENOMEM:
1194 return 0;
1195 case EINVAL:
1196 goto acl_invalid;
1197 default:
1198 break;
1199 }
1200
1201 for (i = 0; i < aclp->acl_cnt; i++) {
1202 entry = &aclp->acl_entry[i];
1203 if (entry->ae_perm & ~(ACL_READ|ACL_WRITE|ACL_EXECUTE))
1204 goto acl_invalid;
1205 switch (entry->ae_tag) {
1206 case ACL_USER_OBJ:
1207 if (user++)
1208 goto acl_invalid;
1209 break;
1210 case ACL_GROUP_OBJ:
1211 if (group++)
1212 goto acl_invalid;
1213 break;
1214 case ACL_OTHER:
1215 if (other++)
1216 goto acl_invalid;
1217 break;
1218 case ACL_USER:
1219 case ACL_GROUP:
1220 for (j = i + 1; j < aclp->acl_cnt; j++) {
1221 e = &aclp->acl_entry[j];
1222 if (e->ae_id == entry->ae_id &&
1223 e->ae_tag == entry->ae_tag)
1224 goto acl_invalid;
1225 }
1226 mask_required++;
1227 break;
1228 case ACL_MASK:
1229 if (mask++)
1230 goto acl_invalid;
1231 break;
1232 default:
1233 goto acl_invalid;
1234 }
1235 }
1236 if (!user || !group || !other || (mask_required && !mask))
1237 goto acl_invalid;
1238 free(aclp);
1239 return 0;
1240 acl_invalid:
1241 free(aclp);
1242 errno = EINVAL;
1243 return (-1);
1244 }
1245
1246 /*
1247 * Check a category or division set to ensure that all values are in
1248 * ascending order and each division or category appears only once.
1249 */
1250 static int
1251 __check_setvalue(const unsigned short *list, unsigned short count)
1252 {
1253 unsigned short i;
1254
1255 for (i = 1; i < count ; i++)
1256 if (list[i] <= list[i-1])
1257 return -1;
1258 return 0;
1259 }
1260
1261 /*
1262 * xfs_mac_valid(lp)
1263 * Check the validity of a MAC label.
1264 */
1265 static int
1266 xfs_mac_valid(xfs_mac_label_t *lp)
1267 {
1268 if (lp == NULL)
1269 return (0);
1270
1271 /*
1272 * if the total category set and division set is greater than 250
1273 * report error
1274 */
1275 if ((lp->ml_catcount + lp->ml_divcount) > XFS_MAC_MAX_SETS)
1276 return(0);
1277
1278 /*
1279 * check whether the msentype value is valid, and do they have
1280 * appropriate level, category association.
1281 */
1282 switch (lp->ml_msen_type) {
1283 case XFS_MSEN_ADMIN_LABEL:
1284 case XFS_MSEN_EQUAL_LABEL:
1285 case XFS_MSEN_HIGH_LABEL:
1286 case XFS_MSEN_MLD_HIGH_LABEL:
1287 case XFS_MSEN_LOW_LABEL:
1288 case XFS_MSEN_MLD_LOW_LABEL:
1289 if (lp->ml_level != 0 || lp->ml_catcount > 0 )
1290 return (0);
1291 break;
1292 case XFS_MSEN_TCSEC_LABEL:
1293 case XFS_MSEN_MLD_LABEL:
1294 if (lp->ml_catcount > 0 &&
1295 __check_setvalue(lp->ml_list,
1296 lp->ml_catcount) == -1)
1297 return (0);
1298 break;
1299 case XFS_MSEN_UNKNOWN_LABEL:
1300 default:
1301 return (0);
1302 }
1303
1304 /*
1305 * check whether the minttype value is valid, and do they have
1306 * appropriate grade, division association.
1307 */
1308 switch (lp->ml_mint_type) {
1309 case XFS_MINT_BIBA_LABEL:
1310 if (lp->ml_divcount > 0 &&
1311 __check_setvalue(lp->ml_list + lp->ml_catcount,
1312 lp->ml_divcount) == -1)
1313 return(0);
1314 break;
1315 case XFS_MINT_EQUAL_LABEL:
1316 case XFS_MINT_HIGH_LABEL:
1317 case XFS_MINT_LOW_LABEL:
1318 if (lp->ml_grade != 0 || lp->ml_divcount > 0 )
1319 return(0);
1320 break;
1321 default:
1322 return(0);
1323 }
1324
1325 return (1);
1326 }