attr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2017 Oracle. All Rights Reserved.
  4. * Author: Darrick J. Wong <[email protected]>
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_trans_resv.h"
  11. #include "xfs_mount.h"
  12. #include "xfs_log_format.h"
  13. #include "xfs_inode.h"
  14. #include "xfs_da_format.h"
  15. #include "xfs_da_btree.h"
  16. #include "xfs_attr.h"
  17. #include "xfs_attr_leaf.h"
  18. #include "scrub/scrub.h"
  19. #include "scrub/common.h"
  20. #include "scrub/dabtree.h"
  21. #include "scrub/attr.h"
  22. /*
  23. * Allocate enough memory to hold an attr value and attr block bitmaps,
  24. * reallocating the buffer if necessary. Buffer contents are not preserved
  25. * across a reallocation.
  26. */
  27. static int
  28. xchk_setup_xattr_buf(
  29. struct xfs_scrub *sc,
  30. size_t value_size,
  31. gfp_t flags)
  32. {
  33. size_t sz;
  34. struct xchk_xattr_buf *ab = sc->buf;
  35. /*
  36. * We need enough space to read an xattr value from the file or enough
  37. * space to hold three copies of the xattr free space bitmap. We don't
  38. * need the buffer space for both purposes at the same time.
  39. */
  40. sz = 3 * sizeof(long) * BITS_TO_LONGS(sc->mp->m_attr_geo->blksize);
  41. sz = max_t(size_t, sz, value_size);
  42. /*
  43. * If there's already a buffer, figure out if we need to reallocate it
  44. * to accommodate a larger size.
  45. */
  46. if (ab) {
  47. if (sz <= ab->sz)
  48. return 0;
  49. kmem_free(ab);
  50. sc->buf = NULL;
  51. }
  52. /*
  53. * Don't zero the buffer upon allocation to avoid runtime overhead.
  54. * All users must be careful never to read uninitialized contents.
  55. */
  56. ab = kvmalloc(sizeof(*ab) + sz, flags);
  57. if (!ab)
  58. return -ENOMEM;
  59. ab->sz = sz;
  60. sc->buf = ab;
  61. return 0;
  62. }
  63. /* Set us up to scrub an inode's extended attributes. */
  64. int
  65. xchk_setup_xattr(
  66. struct xfs_scrub *sc)
  67. {
  68. int error;
  69. /*
  70. * We failed to get memory while checking attrs, so this time try to
  71. * get all the memory we're ever going to need. Allocate the buffer
  72. * without the inode lock held, which means we can sleep.
  73. */
  74. if (sc->flags & XCHK_TRY_HARDER) {
  75. error = xchk_setup_xattr_buf(sc, XATTR_SIZE_MAX, GFP_KERNEL);
  76. if (error)
  77. return error;
  78. }
  79. return xchk_setup_inode_contents(sc, 0);
  80. }
  81. /* Extended Attributes */
  82. struct xchk_xattr {
  83. struct xfs_attr_list_context context;
  84. struct xfs_scrub *sc;
  85. };
  86. /*
  87. * Check that an extended attribute key can be looked up by hash.
  88. *
  89. * We use the XFS attribute list iterator (i.e. xfs_attr_list_ilocked)
  90. * to call this function for every attribute key in an inode. Once
  91. * we're here, we load the attribute value to see if any errors happen,
  92. * or if we get more or less data than we expected.
  93. */
  94. static void
  95. xchk_xattr_listent(
  96. struct xfs_attr_list_context *context,
  97. int flags,
  98. unsigned char *name,
  99. int namelen,
  100. int valuelen)
  101. {
  102. struct xchk_xattr *sx;
  103. struct xfs_da_args args = { NULL };
  104. int error = 0;
  105. sx = container_of(context, struct xchk_xattr, context);
  106. if (xchk_should_terminate(sx->sc, &error)) {
  107. context->seen_enough = error;
  108. return;
  109. }
  110. if (flags & XFS_ATTR_INCOMPLETE) {
  111. /* Incomplete attr key, just mark the inode for preening. */
  112. xchk_ino_set_preen(sx->sc, context->dp->i_ino);
  113. return;
  114. }
  115. /* Does this name make sense? */
  116. if (!xfs_attr_namecheck(name, namelen)) {
  117. xchk_fblock_set_corrupt(sx->sc, XFS_ATTR_FORK, args.blkno);
  118. return;
  119. }
  120. /*
  121. * Try to allocate enough memory to extrat the attr value. If that
  122. * doesn't work, we overload the seen_enough variable to convey
  123. * the error message back to the main scrub function.
  124. */
  125. error = xchk_setup_xattr_buf(sx->sc, valuelen,
  126. GFP_KERNEL | __GFP_RETRY_MAYFAIL);
  127. if (error == -ENOMEM)
  128. error = -EDEADLOCK;
  129. if (error) {
  130. context->seen_enough = error;
  131. return;
  132. }
  133. args.op_flags = XFS_DA_OP_NOTIME;
  134. args.attr_filter = flags & XFS_ATTR_NSP_ONDISK_MASK;
  135. args.geo = context->dp->i_mount->m_attr_geo;
  136. args.whichfork = XFS_ATTR_FORK;
  137. args.dp = context->dp;
  138. args.name = name;
  139. args.namelen = namelen;
  140. args.hashval = xfs_da_hashname(args.name, args.namelen);
  141. args.trans = context->tp;
  142. args.value = xchk_xattr_valuebuf(sx->sc);
  143. args.valuelen = valuelen;
  144. error = xfs_attr_get_ilocked(&args);
  145. /* ENODATA means the hash lookup failed and the attr is bad */
  146. if (error == -ENODATA)
  147. error = -EFSCORRUPTED;
  148. if (!xchk_fblock_process_error(sx->sc, XFS_ATTR_FORK, args.blkno,
  149. &error))
  150. goto fail_xref;
  151. if (args.valuelen != valuelen)
  152. xchk_fblock_set_corrupt(sx->sc, XFS_ATTR_FORK,
  153. args.blkno);
  154. fail_xref:
  155. if (sx->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  156. context->seen_enough = 1;
  157. return;
  158. }
  159. /*
  160. * Mark a range [start, start+len) in this map. Returns true if the
  161. * region was free, and false if there's a conflict or a problem.
  162. *
  163. * Within a char, the lowest bit of the char represents the byte with
  164. * the smallest address
  165. */
  166. STATIC bool
  167. xchk_xattr_set_map(
  168. struct xfs_scrub *sc,
  169. unsigned long *map,
  170. unsigned int start,
  171. unsigned int len)
  172. {
  173. unsigned int mapsize = sc->mp->m_attr_geo->blksize;
  174. bool ret = true;
  175. if (start >= mapsize)
  176. return false;
  177. if (start + len > mapsize) {
  178. len = mapsize - start;
  179. ret = false;
  180. }
  181. if (find_next_bit(map, mapsize, start) < start + len)
  182. ret = false;
  183. bitmap_set(map, start, len);
  184. return ret;
  185. }
  186. /*
  187. * Check the leaf freemap from the usage bitmap. Returns false if the
  188. * attr freemap has problems or points to used space.
  189. */
  190. STATIC bool
  191. xchk_xattr_check_freemap(
  192. struct xfs_scrub *sc,
  193. unsigned long *map,
  194. struct xfs_attr3_icleaf_hdr *leafhdr)
  195. {
  196. unsigned long *freemap = xchk_xattr_freemap(sc);
  197. unsigned long *dstmap = xchk_xattr_dstmap(sc);
  198. unsigned int mapsize = sc->mp->m_attr_geo->blksize;
  199. int i;
  200. /* Construct bitmap of freemap contents. */
  201. bitmap_zero(freemap, mapsize);
  202. for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
  203. if (!xchk_xattr_set_map(sc, freemap,
  204. leafhdr->freemap[i].base,
  205. leafhdr->freemap[i].size))
  206. return false;
  207. }
  208. /* Look for bits that are set in freemap and are marked in use. */
  209. return bitmap_and(dstmap, freemap, map, mapsize) == 0;
  210. }
  211. /*
  212. * Check this leaf entry's relations to everything else.
  213. * Returns the number of bytes used for the name/value data.
  214. */
  215. STATIC void
  216. xchk_xattr_entry(
  217. struct xchk_da_btree *ds,
  218. int level,
  219. char *buf_end,
  220. struct xfs_attr_leafblock *leaf,
  221. struct xfs_attr3_icleaf_hdr *leafhdr,
  222. struct xfs_attr_leaf_entry *ent,
  223. int idx,
  224. unsigned int *usedbytes,
  225. __u32 *last_hashval)
  226. {
  227. struct xfs_mount *mp = ds->state->mp;
  228. unsigned long *usedmap = xchk_xattr_usedmap(ds->sc);
  229. char *name_end;
  230. struct xfs_attr_leaf_name_local *lentry;
  231. struct xfs_attr_leaf_name_remote *rentry;
  232. unsigned int nameidx;
  233. unsigned int namesize;
  234. if (ent->pad2 != 0)
  235. xchk_da_set_corrupt(ds, level);
  236. /* Hash values in order? */
  237. if (be32_to_cpu(ent->hashval) < *last_hashval)
  238. xchk_da_set_corrupt(ds, level);
  239. *last_hashval = be32_to_cpu(ent->hashval);
  240. nameidx = be16_to_cpu(ent->nameidx);
  241. if (nameidx < leafhdr->firstused ||
  242. nameidx >= mp->m_attr_geo->blksize) {
  243. xchk_da_set_corrupt(ds, level);
  244. return;
  245. }
  246. /* Check the name information. */
  247. if (ent->flags & XFS_ATTR_LOCAL) {
  248. lentry = xfs_attr3_leaf_name_local(leaf, idx);
  249. namesize = xfs_attr_leaf_entsize_local(lentry->namelen,
  250. be16_to_cpu(lentry->valuelen));
  251. name_end = (char *)lentry + namesize;
  252. if (lentry->namelen == 0)
  253. xchk_da_set_corrupt(ds, level);
  254. } else {
  255. rentry = xfs_attr3_leaf_name_remote(leaf, idx);
  256. namesize = xfs_attr_leaf_entsize_remote(rentry->namelen);
  257. name_end = (char *)rentry + namesize;
  258. if (rentry->namelen == 0 || rentry->valueblk == 0)
  259. xchk_da_set_corrupt(ds, level);
  260. }
  261. if (name_end > buf_end)
  262. xchk_da_set_corrupt(ds, level);
  263. if (!xchk_xattr_set_map(ds->sc, usedmap, nameidx, namesize))
  264. xchk_da_set_corrupt(ds, level);
  265. if (!(ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
  266. *usedbytes += namesize;
  267. }
  268. /* Scrub an attribute leaf. */
  269. STATIC int
  270. xchk_xattr_block(
  271. struct xchk_da_btree *ds,
  272. int level)
  273. {
  274. struct xfs_attr3_icleaf_hdr leafhdr;
  275. struct xfs_mount *mp = ds->state->mp;
  276. struct xfs_da_state_blk *blk = &ds->state->path.blk[level];
  277. struct xfs_buf *bp = blk->bp;
  278. xfs_dablk_t *last_checked = ds->private;
  279. struct xfs_attr_leafblock *leaf = bp->b_addr;
  280. struct xfs_attr_leaf_entry *ent;
  281. struct xfs_attr_leaf_entry *entries;
  282. unsigned long *usedmap;
  283. char *buf_end;
  284. size_t off;
  285. __u32 last_hashval = 0;
  286. unsigned int usedbytes = 0;
  287. unsigned int hdrsize;
  288. int i;
  289. int error;
  290. if (*last_checked == blk->blkno)
  291. return 0;
  292. /* Allocate memory for block usage checking. */
  293. error = xchk_setup_xattr_buf(ds->sc, 0,
  294. GFP_KERNEL | __GFP_RETRY_MAYFAIL);
  295. if (error == -ENOMEM)
  296. return -EDEADLOCK;
  297. if (error)
  298. return error;
  299. usedmap = xchk_xattr_usedmap(ds->sc);
  300. *last_checked = blk->blkno;
  301. bitmap_zero(usedmap, mp->m_attr_geo->blksize);
  302. /* Check all the padding. */
  303. if (xfs_has_crc(ds->sc->mp)) {
  304. struct xfs_attr3_leafblock *leaf = bp->b_addr;
  305. if (leaf->hdr.pad1 != 0 || leaf->hdr.pad2 != 0 ||
  306. leaf->hdr.info.hdr.pad != 0)
  307. xchk_da_set_corrupt(ds, level);
  308. } else {
  309. if (leaf->hdr.pad1 != 0 || leaf->hdr.info.pad != 0)
  310. xchk_da_set_corrupt(ds, level);
  311. }
  312. /* Check the leaf header */
  313. xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &leafhdr, leaf);
  314. hdrsize = xfs_attr3_leaf_hdr_size(leaf);
  315. if (leafhdr.usedbytes > mp->m_attr_geo->blksize)
  316. xchk_da_set_corrupt(ds, level);
  317. if (leafhdr.firstused > mp->m_attr_geo->blksize)
  318. xchk_da_set_corrupt(ds, level);
  319. if (leafhdr.firstused < hdrsize)
  320. xchk_da_set_corrupt(ds, level);
  321. if (!xchk_xattr_set_map(ds->sc, usedmap, 0, hdrsize))
  322. xchk_da_set_corrupt(ds, level);
  323. if (ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  324. goto out;
  325. entries = xfs_attr3_leaf_entryp(leaf);
  326. if ((char *)&entries[leafhdr.count] > (char *)leaf + leafhdr.firstused)
  327. xchk_da_set_corrupt(ds, level);
  328. buf_end = (char *)bp->b_addr + mp->m_attr_geo->blksize;
  329. for (i = 0, ent = entries; i < leafhdr.count; ent++, i++) {
  330. /* Mark the leaf entry itself. */
  331. off = (char *)ent - (char *)leaf;
  332. if (!xchk_xattr_set_map(ds->sc, usedmap, off,
  333. sizeof(xfs_attr_leaf_entry_t))) {
  334. xchk_da_set_corrupt(ds, level);
  335. goto out;
  336. }
  337. /* Check the entry and nameval. */
  338. xchk_xattr_entry(ds, level, buf_end, leaf, &leafhdr,
  339. ent, i, &usedbytes, &last_hashval);
  340. if (ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  341. goto out;
  342. }
  343. if (!xchk_xattr_check_freemap(ds->sc, usedmap, &leafhdr))
  344. xchk_da_set_corrupt(ds, level);
  345. if (leafhdr.usedbytes != usedbytes)
  346. xchk_da_set_corrupt(ds, level);
  347. out:
  348. return 0;
  349. }
  350. /* Scrub a attribute btree record. */
  351. STATIC int
  352. xchk_xattr_rec(
  353. struct xchk_da_btree *ds,
  354. int level)
  355. {
  356. struct xfs_mount *mp = ds->state->mp;
  357. struct xfs_da_state_blk *blk = &ds->state->path.blk[level];
  358. struct xfs_attr_leaf_name_local *lentry;
  359. struct xfs_attr_leaf_name_remote *rentry;
  360. struct xfs_buf *bp;
  361. struct xfs_attr_leaf_entry *ent;
  362. xfs_dahash_t calc_hash;
  363. xfs_dahash_t hash;
  364. int nameidx;
  365. int hdrsize;
  366. unsigned int badflags;
  367. int error;
  368. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  369. ent = xfs_attr3_leaf_entryp(blk->bp->b_addr) + blk->index;
  370. /* Check the whole block, if necessary. */
  371. error = xchk_xattr_block(ds, level);
  372. if (error)
  373. goto out;
  374. if (ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  375. goto out;
  376. /* Check the hash of the entry. */
  377. error = xchk_da_btree_hash(ds, level, &ent->hashval);
  378. if (error)
  379. goto out;
  380. /* Find the attr entry's location. */
  381. bp = blk->bp;
  382. hdrsize = xfs_attr3_leaf_hdr_size(bp->b_addr);
  383. nameidx = be16_to_cpu(ent->nameidx);
  384. if (nameidx < hdrsize || nameidx >= mp->m_attr_geo->blksize) {
  385. xchk_da_set_corrupt(ds, level);
  386. goto out;
  387. }
  388. /* Retrieve the entry and check it. */
  389. hash = be32_to_cpu(ent->hashval);
  390. badflags = ~(XFS_ATTR_LOCAL | XFS_ATTR_ROOT | XFS_ATTR_SECURE |
  391. XFS_ATTR_INCOMPLETE);
  392. if ((ent->flags & badflags) != 0)
  393. xchk_da_set_corrupt(ds, level);
  394. if (ent->flags & XFS_ATTR_LOCAL) {
  395. lentry = (struct xfs_attr_leaf_name_local *)
  396. (((char *)bp->b_addr) + nameidx);
  397. if (lentry->namelen <= 0) {
  398. xchk_da_set_corrupt(ds, level);
  399. goto out;
  400. }
  401. calc_hash = xfs_da_hashname(lentry->nameval, lentry->namelen);
  402. } else {
  403. rentry = (struct xfs_attr_leaf_name_remote *)
  404. (((char *)bp->b_addr) + nameidx);
  405. if (rentry->namelen <= 0) {
  406. xchk_da_set_corrupt(ds, level);
  407. goto out;
  408. }
  409. calc_hash = xfs_da_hashname(rentry->name, rentry->namelen);
  410. }
  411. if (calc_hash != hash)
  412. xchk_da_set_corrupt(ds, level);
  413. out:
  414. return error;
  415. }
  416. /* Scrub the extended attribute metadata. */
  417. int
  418. xchk_xattr(
  419. struct xfs_scrub *sc)
  420. {
  421. struct xchk_xattr sx;
  422. xfs_dablk_t last_checked = -1U;
  423. int error = 0;
  424. if (!xfs_inode_hasattr(sc->ip))
  425. return -ENOENT;
  426. memset(&sx, 0, sizeof(sx));
  427. /* Check attribute tree structure */
  428. error = xchk_da_btree(sc, XFS_ATTR_FORK, xchk_xattr_rec,
  429. &last_checked);
  430. if (error)
  431. goto out;
  432. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  433. goto out;
  434. /* Check that every attr key can also be looked up by hash. */
  435. sx.context.dp = sc->ip;
  436. sx.context.resynch = 1;
  437. sx.context.put_listent = xchk_xattr_listent;
  438. sx.context.tp = sc->tp;
  439. sx.context.allow_incomplete = true;
  440. sx.sc = sc;
  441. /*
  442. * Look up every xattr in this file by name.
  443. *
  444. * Use the backend implementation of xfs_attr_list to call
  445. * xchk_xattr_listent on every attribute key in this inode.
  446. * In other words, we use the same iterator/callback mechanism
  447. * that listattr uses to scrub extended attributes, though in our
  448. * _listent function, we check the value of the attribute.
  449. *
  450. * The VFS only locks i_rwsem when modifying attrs, so keep all
  451. * three locks held because that's the only way to ensure we're
  452. * the only thread poking into the da btree. We traverse the da
  453. * btree while holding a leaf buffer locked for the xattr name
  454. * iteration, which doesn't really follow the usual buffer
  455. * locking order.
  456. */
  457. error = xfs_attr_list_ilocked(&sx.context);
  458. if (!xchk_fblock_process_error(sc, XFS_ATTR_FORK, 0, &error))
  459. goto out;
  460. /* Did our listent function try to return any errors? */
  461. if (sx.context.seen_enough < 0)
  462. error = sx.context.seen_enough;
  463. out:
  464. return error;
  465. }