xfs_attr_list.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  4. * Copyright (c) 2013 Red Hat, Inc.
  5. * All Rights Reserved.
  6. */
  7. #include "xfs.h"
  8. #include "xfs_fs.h"
  9. #include "xfs_shared.h"
  10. #include "xfs_format.h"
  11. #include "xfs_log_format.h"
  12. #include "xfs_trans_resv.h"
  13. #include "xfs_mount.h"
  14. #include "xfs_da_format.h"
  15. #include "xfs_inode.h"
  16. #include "xfs_trans.h"
  17. #include "xfs_bmap.h"
  18. #include "xfs_da_btree.h"
  19. #include "xfs_attr.h"
  20. #include "xfs_attr_sf.h"
  21. #include "xfs_attr_leaf.h"
  22. #include "xfs_error.h"
  23. #include "xfs_trace.h"
  24. #include "xfs_dir2.h"
  25. #include "xfs_health.h"
  26. STATIC int
  27. xfs_attr_shortform_compare(const void *a, const void *b)
  28. {
  29. xfs_attr_sf_sort_t *sa, *sb;
  30. sa = (xfs_attr_sf_sort_t *)a;
  31. sb = (xfs_attr_sf_sort_t *)b;
  32. if (sa->hash < sb->hash) {
  33. return -1;
  34. } else if (sa->hash > sb->hash) {
  35. return 1;
  36. } else {
  37. return sa->entno - sb->entno;
  38. }
  39. }
  40. #define XFS_ISRESET_CURSOR(cursor) \
  41. (!((cursor)->initted) && !((cursor)->hashval) && \
  42. !((cursor)->blkno) && !((cursor)->offset))
  43. /*
  44. * Copy out entries of shortform attribute lists for attr_list().
  45. * Shortform attribute lists are not stored in hashval sorted order.
  46. * If the output buffer is not large enough to hold them all, then
  47. * we have to calculate each entries' hashvalue and sort them before
  48. * we can begin returning them to the user.
  49. */
  50. static int
  51. xfs_attr_shortform_list(
  52. struct xfs_attr_list_context *context)
  53. {
  54. struct xfs_attrlist_cursor_kern *cursor = &context->cursor;
  55. struct xfs_inode *dp = context->dp;
  56. struct xfs_attr_sf_sort *sbuf, *sbp;
  57. struct xfs_attr_sf_hdr *sf = dp->i_af.if_data;
  58. struct xfs_attr_sf_entry *sfe;
  59. int sbsize, nsbuf, count, i;
  60. int error = 0;
  61. ASSERT(sf != NULL);
  62. if (!sf->count)
  63. return 0;
  64. trace_xfs_attr_list_sf(context);
  65. /*
  66. * If the buffer is large enough and the cursor is at the start,
  67. * do not bother with sorting since we will return everything in
  68. * one buffer and another call using the cursor won't need to be
  69. * made.
  70. * Note the generous fudge factor of 16 overhead bytes per entry.
  71. * If bufsize is zero then put_listent must be a search function
  72. * and can just scan through what we have.
  73. */
  74. if (context->bufsize == 0 ||
  75. (XFS_ISRESET_CURSOR(cursor) &&
  76. (dp->i_af.if_bytes + sf->count * 16) < context->bufsize)) {
  77. for (i = 0, sfe = xfs_attr_sf_firstentry(sf); i < sf->count; i++) {
  78. if (XFS_IS_CORRUPT(context->dp->i_mount,
  79. !xfs_attr_namecheck(sfe->flags,
  80. sfe->nameval,
  81. sfe->namelen))) {
  82. xfs_dirattr_mark_sick(context->dp, XFS_ATTR_FORK);
  83. return -EFSCORRUPTED;
  84. }
  85. context->put_listent(context,
  86. sfe->flags,
  87. sfe->nameval,
  88. (int)sfe->namelen,
  89. &sfe->nameval[sfe->namelen],
  90. (int)sfe->valuelen);
  91. /*
  92. * Either search callback finished early or
  93. * didn't fit it all in the buffer after all.
  94. */
  95. if (context->seen_enough)
  96. break;
  97. sfe = xfs_attr_sf_nextentry(sfe);
  98. }
  99. trace_xfs_attr_list_sf_all(context);
  100. return 0;
  101. }
  102. /* do no more for a search callback */
  103. if (context->bufsize == 0)
  104. return 0;
  105. /*
  106. * It didn't all fit, so we have to sort everything on hashval.
  107. */
  108. sbsize = sf->count * sizeof(*sbuf);
  109. sbp = sbuf = kmalloc(sbsize, GFP_KERNEL | __GFP_NOFAIL);
  110. /*
  111. * Scan the attribute list for the rest of the entries, storing
  112. * the relevant info from only those that match into a buffer.
  113. */
  114. nsbuf = 0;
  115. for (i = 0, sfe = xfs_attr_sf_firstentry(sf); i < sf->count; i++) {
  116. if (unlikely(
  117. ((char *)sfe < (char *)sf) ||
  118. ((char *)sfe >= ((char *)sf + dp->i_af.if_bytes)) ||
  119. !xfs_attr_check_namespace(sfe->flags))) {
  120. XFS_CORRUPTION_ERROR("xfs_attr_shortform_list",
  121. XFS_ERRLEVEL_LOW,
  122. context->dp->i_mount, sfe,
  123. sizeof(*sfe));
  124. kfree(sbuf);
  125. xfs_dirattr_mark_sick(dp, XFS_ATTR_FORK);
  126. return -EFSCORRUPTED;
  127. }
  128. sbp->entno = i;
  129. sbp->name = sfe->nameval;
  130. sbp->namelen = sfe->namelen;
  131. /* These are bytes, and both on-disk, don't endian-flip */
  132. sbp->value = &sfe->nameval[sfe->namelen];
  133. sbp->valuelen = sfe->valuelen;
  134. sbp->flags = sfe->flags;
  135. sbp->hash = xfs_attr_hashval(dp->i_mount, sfe->flags,
  136. sfe->nameval, sfe->namelen,
  137. sfe->nameval + sfe->namelen,
  138. sfe->valuelen);
  139. sfe = xfs_attr_sf_nextentry(sfe);
  140. sbp++;
  141. nsbuf++;
  142. }
  143. /*
  144. * Sort the entries on hash then entno.
  145. */
  146. xfs_sort(sbuf, nsbuf, sizeof(*sbuf), xfs_attr_shortform_compare);
  147. /*
  148. * Re-find our place IN THE SORTED LIST.
  149. */
  150. count = 0;
  151. cursor->initted = 1;
  152. cursor->blkno = 0;
  153. for (sbp = sbuf, i = 0; i < nsbuf; i++, sbp++) {
  154. if (sbp->hash == cursor->hashval) {
  155. if (cursor->offset == count) {
  156. break;
  157. }
  158. count++;
  159. } else if (sbp->hash > cursor->hashval) {
  160. break;
  161. }
  162. }
  163. if (i == nsbuf)
  164. goto out;
  165. /*
  166. * Loop putting entries into the user buffer.
  167. */
  168. for ( ; i < nsbuf; i++, sbp++) {
  169. if (cursor->hashval != sbp->hash) {
  170. cursor->hashval = sbp->hash;
  171. cursor->offset = 0;
  172. }
  173. if (XFS_IS_CORRUPT(context->dp->i_mount,
  174. !xfs_attr_namecheck(sbp->flags, sbp->name,
  175. sbp->namelen))) {
  176. xfs_dirattr_mark_sick(context->dp, XFS_ATTR_FORK);
  177. error = -EFSCORRUPTED;
  178. goto out;
  179. }
  180. context->put_listent(context,
  181. sbp->flags,
  182. sbp->name,
  183. sbp->namelen,
  184. sbp->value,
  185. sbp->valuelen);
  186. if (context->seen_enough)
  187. break;
  188. cursor->offset++;
  189. }
  190. out:
  191. kfree(sbuf);
  192. return error;
  193. }
  194. /*
  195. * We didn't find the block & hash mentioned in the cursor state, so
  196. * walk down the attr btree looking for the hash.
  197. */
  198. STATIC int
  199. xfs_attr_node_list_lookup(
  200. struct xfs_attr_list_context *context,
  201. struct xfs_attrlist_cursor_kern *cursor,
  202. struct xfs_buf **pbp)
  203. {
  204. struct xfs_da3_icnode_hdr nodehdr;
  205. struct xfs_da_intnode *node;
  206. struct xfs_da_node_entry *btree;
  207. struct xfs_inode *dp = context->dp;
  208. struct xfs_mount *mp = dp->i_mount;
  209. struct xfs_trans *tp = context->tp;
  210. struct xfs_buf *bp;
  211. xfs_failaddr_t fa;
  212. int i;
  213. int error = 0;
  214. unsigned int expected_level = 0;
  215. uint16_t magic;
  216. ASSERT(*pbp == NULL);
  217. cursor->blkno = 0;
  218. for (;;) {
  219. error = xfs_da3_node_read(tp, dp, cursor->blkno, &bp,
  220. XFS_ATTR_FORK);
  221. if (error)
  222. return error;
  223. node = bp->b_addr;
  224. magic = be16_to_cpu(node->hdr.info.magic);
  225. if (magic == XFS_ATTR_LEAF_MAGIC ||
  226. magic == XFS_ATTR3_LEAF_MAGIC)
  227. break;
  228. if (magic != XFS_DA_NODE_MAGIC &&
  229. magic != XFS_DA3_NODE_MAGIC) {
  230. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
  231. node, sizeof(*node));
  232. goto out_corruptbuf;
  233. }
  234. fa = xfs_da3_node_header_check(bp, dp->i_ino);
  235. if (fa)
  236. goto out_corruptbuf;
  237. xfs_da3_node_hdr_from_disk(mp, &nodehdr, node);
  238. /* Tree taller than we can handle; bail out! */
  239. if (nodehdr.level >= XFS_DA_NODE_MAXDEPTH)
  240. goto out_corruptbuf;
  241. /* Check the level from the root node. */
  242. if (cursor->blkno == 0)
  243. expected_level = nodehdr.level - 1;
  244. else if (expected_level != nodehdr.level)
  245. goto out_corruptbuf;
  246. else
  247. expected_level--;
  248. btree = nodehdr.btree;
  249. for (i = 0; i < nodehdr.count; btree++, i++) {
  250. if (cursor->hashval <= be32_to_cpu(btree->hashval)) {
  251. cursor->blkno = be32_to_cpu(btree->before);
  252. trace_xfs_attr_list_node_descend(context,
  253. btree);
  254. break;
  255. }
  256. }
  257. xfs_trans_brelse(tp, bp);
  258. if (i == nodehdr.count)
  259. return 0;
  260. /* We can't point back to the root. */
  261. if (XFS_IS_CORRUPT(mp, cursor->blkno == 0)) {
  262. xfs_dirattr_mark_sick(dp, XFS_ATTR_FORK);
  263. return -EFSCORRUPTED;
  264. }
  265. }
  266. fa = xfs_attr3_leaf_header_check(bp, dp->i_ino);
  267. if (fa) {
  268. __xfs_buf_mark_corrupt(bp, fa);
  269. goto out_releasebuf;
  270. }
  271. if (expected_level != 0)
  272. goto out_corruptbuf;
  273. *pbp = bp;
  274. return 0;
  275. out_corruptbuf:
  276. xfs_buf_mark_corrupt(bp);
  277. out_releasebuf:
  278. xfs_trans_brelse(tp, bp);
  279. xfs_dirattr_mark_sick(dp, XFS_ATTR_FORK);
  280. return -EFSCORRUPTED;
  281. }
  282. STATIC int
  283. xfs_attr_node_list(
  284. struct xfs_attr_list_context *context)
  285. {
  286. struct xfs_attrlist_cursor_kern *cursor = &context->cursor;
  287. struct xfs_attr3_icleaf_hdr leafhdr;
  288. struct xfs_attr_leafblock *leaf;
  289. struct xfs_da_intnode *node;
  290. struct xfs_buf *bp;
  291. struct xfs_inode *dp = context->dp;
  292. struct xfs_mount *mp = dp->i_mount;
  293. xfs_failaddr_t fa;
  294. int error = 0;
  295. trace_xfs_attr_node_list(context);
  296. cursor->initted = 1;
  297. /*
  298. * Do all sorts of validation on the passed-in cursor structure.
  299. * If anything is amiss, ignore the cursor and look up the hashval
  300. * starting from the btree root.
  301. */
  302. bp = NULL;
  303. if (cursor->blkno > 0) {
  304. struct xfs_attr_leaf_entry *entries;
  305. error = xfs_da3_node_read(context->tp, dp, cursor->blkno, &bp,
  306. XFS_ATTR_FORK);
  307. if (xfs_metadata_is_sick(error))
  308. xfs_dirattr_mark_sick(dp, XFS_ATTR_FORK);
  309. if (error != 0 && error != -EFSCORRUPTED)
  310. return error;
  311. if (!bp)
  312. goto need_lookup;
  313. node = bp->b_addr;
  314. switch (be16_to_cpu(node->hdr.info.magic)) {
  315. case XFS_DA_NODE_MAGIC:
  316. case XFS_DA3_NODE_MAGIC:
  317. trace_xfs_attr_list_wrong_blk(context);
  318. fa = xfs_da3_node_header_check(bp, dp->i_ino);
  319. if (fa) {
  320. __xfs_buf_mark_corrupt(bp, fa);
  321. xfs_dirattr_mark_sick(dp, XFS_ATTR_FORK);
  322. }
  323. xfs_trans_brelse(context->tp, bp);
  324. bp = NULL;
  325. break;
  326. case XFS_ATTR_LEAF_MAGIC:
  327. case XFS_ATTR3_LEAF_MAGIC:
  328. leaf = bp->b_addr;
  329. fa = xfs_attr3_leaf_header_check(bp, dp->i_ino);
  330. if (fa) {
  331. __xfs_buf_mark_corrupt(bp, fa);
  332. xfs_trans_brelse(context->tp, bp);
  333. xfs_dirattr_mark_sick(dp, XFS_ATTR_FORK);
  334. bp = NULL;
  335. break;
  336. }
  337. xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo,
  338. &leafhdr, leaf);
  339. entries = xfs_attr3_leaf_entryp(leaf);
  340. if (cursor->hashval > be32_to_cpu(
  341. entries[leafhdr.count - 1].hashval)) {
  342. trace_xfs_attr_list_wrong_blk(context);
  343. xfs_trans_brelse(context->tp, bp);
  344. bp = NULL;
  345. } else if (cursor->hashval <= be32_to_cpu(
  346. entries[0].hashval)) {
  347. trace_xfs_attr_list_wrong_blk(context);
  348. xfs_trans_brelse(context->tp, bp);
  349. bp = NULL;
  350. }
  351. break;
  352. default:
  353. trace_xfs_attr_list_wrong_blk(context);
  354. xfs_trans_brelse(context->tp, bp);
  355. bp = NULL;
  356. }
  357. }
  358. /*
  359. * We did not find what we expected given the cursor's contents,
  360. * so we start from the top and work down based on the hash value.
  361. * Note that start of node block is same as start of leaf block.
  362. */
  363. if (bp == NULL) {
  364. need_lookup:
  365. error = xfs_attr_node_list_lookup(context, cursor, &bp);
  366. if (error || !bp)
  367. return error;
  368. }
  369. ASSERT(bp != NULL);
  370. /*
  371. * Roll upward through the blocks, processing each leaf block in
  372. * order. As long as there is space in the result buffer, keep
  373. * adding the information.
  374. */
  375. for (;;) {
  376. leaf = bp->b_addr;
  377. error = xfs_attr3_leaf_list_int(bp, context);
  378. if (error)
  379. break;
  380. xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &leafhdr, leaf);
  381. if (context->seen_enough || leafhdr.forw == 0)
  382. break;
  383. cursor->blkno = leafhdr.forw;
  384. xfs_trans_brelse(context->tp, bp);
  385. error = xfs_attr3_leaf_read(context->tp, dp, dp->i_ino,
  386. cursor->blkno, &bp);
  387. if (error)
  388. return error;
  389. }
  390. xfs_trans_brelse(context->tp, bp);
  391. return error;
  392. }
  393. /*
  394. * Copy out attribute list entries for attr_list(), for leaf attribute lists.
  395. */
  396. int
  397. xfs_attr3_leaf_list_int(
  398. struct xfs_buf *bp,
  399. struct xfs_attr_list_context *context)
  400. {
  401. struct xfs_attrlist_cursor_kern *cursor = &context->cursor;
  402. struct xfs_attr_leafblock *leaf;
  403. struct xfs_attr3_icleaf_hdr ichdr;
  404. struct xfs_attr_leaf_entry *entries;
  405. struct xfs_attr_leaf_entry *entry;
  406. int i;
  407. struct xfs_mount *mp = context->dp->i_mount;
  408. trace_xfs_attr_list_leaf(context);
  409. leaf = bp->b_addr;
  410. xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, leaf);
  411. entries = xfs_attr3_leaf_entryp(leaf);
  412. cursor->initted = 1;
  413. /*
  414. * Re-find our place in the leaf block if this is a new syscall.
  415. */
  416. if (context->resynch) {
  417. entry = &entries[0];
  418. for (i = 0; i < ichdr.count; entry++, i++) {
  419. if (be32_to_cpu(entry->hashval) == cursor->hashval) {
  420. if (cursor->offset == context->dupcnt) {
  421. context->dupcnt = 0;
  422. break;
  423. }
  424. context->dupcnt++;
  425. } else if (be32_to_cpu(entry->hashval) >
  426. cursor->hashval) {
  427. context->dupcnt = 0;
  428. break;
  429. }
  430. }
  431. if (i == ichdr.count) {
  432. trace_xfs_attr_list_notfound(context);
  433. return 0;
  434. }
  435. } else {
  436. entry = &entries[0];
  437. i = 0;
  438. }
  439. context->resynch = 0;
  440. /*
  441. * We have found our place, start copying out the new attributes.
  442. */
  443. for (; i < ichdr.count; entry++, i++) {
  444. char *name;
  445. void *value;
  446. int namelen, valuelen;
  447. if (be32_to_cpu(entry->hashval) != cursor->hashval) {
  448. cursor->hashval = be32_to_cpu(entry->hashval);
  449. cursor->offset = 0;
  450. }
  451. if ((entry->flags & XFS_ATTR_INCOMPLETE) &&
  452. !context->allow_incomplete)
  453. continue;
  454. if (entry->flags & XFS_ATTR_LOCAL) {
  455. xfs_attr_leaf_name_local_t *name_loc;
  456. name_loc = xfs_attr3_leaf_name_local(leaf, i);
  457. name = name_loc->nameval;
  458. namelen = name_loc->namelen;
  459. value = &name_loc->nameval[name_loc->namelen];
  460. valuelen = be16_to_cpu(name_loc->valuelen);
  461. } else {
  462. xfs_attr_leaf_name_remote_t *name_rmt;
  463. name_rmt = xfs_attr3_leaf_name_remote(leaf, i);
  464. name = name_rmt->name;
  465. namelen = name_rmt->namelen;
  466. value = NULL;
  467. valuelen = be32_to_cpu(name_rmt->valuelen);
  468. }
  469. if (XFS_IS_CORRUPT(context->dp->i_mount,
  470. !xfs_attr_namecheck(entry->flags, name,
  471. namelen))) {
  472. xfs_dirattr_mark_sick(context->dp, XFS_ATTR_FORK);
  473. return -EFSCORRUPTED;
  474. }
  475. context->put_listent(context, entry->flags,
  476. name, namelen, value, valuelen);
  477. if (context->seen_enough)
  478. break;
  479. cursor->offset++;
  480. }
  481. trace_xfs_attr_list_leaf_end(context);
  482. return 0;
  483. }
  484. /*
  485. * Copy out attribute entries for attr_list(), for leaf attribute lists.
  486. */
  487. STATIC int
  488. xfs_attr_leaf_list(
  489. struct xfs_attr_list_context *context)
  490. {
  491. struct xfs_buf *bp;
  492. int error;
  493. trace_xfs_attr_leaf_list(context);
  494. context->cursor.blkno = 0;
  495. error = xfs_attr3_leaf_read(context->tp, context->dp,
  496. context->dp->i_ino, 0, &bp);
  497. if (error)
  498. return error;
  499. error = xfs_attr3_leaf_list_int(bp, context);
  500. xfs_trans_brelse(context->tp, bp);
  501. return error;
  502. }
  503. int
  504. xfs_attr_list_ilocked(
  505. struct xfs_attr_list_context *context)
  506. {
  507. struct xfs_inode *dp = context->dp;
  508. int error;
  509. xfs_assert_ilocked(dp, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL);
  510. /*
  511. * Decide on what work routines to call based on the inode size.
  512. */
  513. if (!xfs_inode_hasattr(dp))
  514. return 0;
  515. if (dp->i_af.if_format == XFS_DINODE_FMT_LOCAL)
  516. return xfs_attr_shortform_list(context);
  517. /* Prerequisite for xfs_attr_is_leaf */
  518. error = xfs_iread_extents(NULL, dp, XFS_ATTR_FORK);
  519. if (error)
  520. return error;
  521. if (xfs_attr_is_leaf(dp))
  522. return xfs_attr_leaf_list(context);
  523. return xfs_attr_node_list(context);
  524. }
  525. int
  526. xfs_attr_list(
  527. struct xfs_attr_list_context *context)
  528. {
  529. struct xfs_inode *dp = context->dp;
  530. uint lock_mode;
  531. int error;
  532. XFS_STATS_INC(dp->i_mount, xs_attr_list);
  533. if (xfs_is_shutdown(dp->i_mount))
  534. return -EIO;
  535. lock_mode = xfs_ilock_attr_map_shared(dp);
  536. error = xfs_attr_list_ilocked(context);
  537. xfs_iunlock(dp, lock_mode);
  538. return error;
  539. }