xfs_attr_inactive.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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_bit.h"
  14. #include "xfs_mount.h"
  15. #include "xfs_da_format.h"
  16. #include "xfs_da_btree.h"
  17. #include "xfs_inode.h"
  18. #include "xfs_attr.h"
  19. #include "xfs_attr_remote.h"
  20. #include "xfs_trans.h"
  21. #include "xfs_bmap.h"
  22. #include "xfs_attr_leaf.h"
  23. #include "xfs_quota.h"
  24. #include "xfs_dir2.h"
  25. #include "xfs_error.h"
  26. #include "xfs_health.h"
  27. /*
  28. * Invalidate any incore buffers associated with this remote attribute value
  29. * extent. We never log remote attribute value buffers, which means that they
  30. * won't be attached to a transaction and are therefore safe to mark stale.
  31. * The actual bunmapi will be taken care of later.
  32. */
  33. STATIC int
  34. xfs_attr3_rmt_stale(
  35. struct xfs_inode *dp,
  36. xfs_dablk_t blkno,
  37. int blkcnt)
  38. {
  39. struct xfs_bmbt_irec map;
  40. int nmap;
  41. int error;
  42. /*
  43. * Roll through the "value", invalidating the attribute value's
  44. * blocks.
  45. */
  46. while (blkcnt > 0) {
  47. /*
  48. * Try to remember where we decided to put the value.
  49. */
  50. nmap = 1;
  51. error = xfs_bmapi_read(dp, (xfs_fileoff_t)blkno, blkcnt,
  52. &map, &nmap, XFS_BMAPI_ATTRFORK);
  53. if (error)
  54. return error;
  55. if (XFS_IS_CORRUPT(dp->i_mount, nmap != 1))
  56. return -EFSCORRUPTED;
  57. /*
  58. * Mark any incore buffers for the remote value as stale. We
  59. * never log remote attr value buffers, so the buffer should be
  60. * easy to kill.
  61. */
  62. error = xfs_attr_rmtval_stale(dp, &map, 0);
  63. if (error)
  64. return error;
  65. blkno += map.br_blockcount;
  66. blkcnt -= map.br_blockcount;
  67. }
  68. return 0;
  69. }
  70. /*
  71. * Invalidate all of the "remote" value regions pointed to by a particular
  72. * leaf block.
  73. * Note that we must release the lock on the buffer so that we are not
  74. * caught holding something that the logging code wants to flush to disk.
  75. */
  76. STATIC int
  77. xfs_attr3_leaf_inactive(
  78. struct xfs_trans **trans,
  79. struct xfs_inode *dp,
  80. struct xfs_buf *bp)
  81. {
  82. struct xfs_attr3_icleaf_hdr ichdr;
  83. struct xfs_mount *mp = bp->b_mount;
  84. struct xfs_attr_leafblock *leaf = bp->b_addr;
  85. struct xfs_attr_leaf_entry *entry;
  86. struct xfs_attr_leaf_name_remote *name_rmt;
  87. int error = 0;
  88. int i;
  89. xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, leaf);
  90. /*
  91. * Find the remote value extents for this leaf and invalidate their
  92. * incore buffers.
  93. */
  94. entry = xfs_attr3_leaf_entryp(leaf);
  95. for (i = 0; i < ichdr.count; entry++, i++) {
  96. int blkcnt;
  97. if (!entry->nameidx || (entry->flags & XFS_ATTR_LOCAL))
  98. continue;
  99. name_rmt = xfs_attr3_leaf_name_remote(leaf, i);
  100. if (!name_rmt->valueblk)
  101. continue;
  102. blkcnt = xfs_attr3_rmt_blocks(dp->i_mount,
  103. be32_to_cpu(name_rmt->valuelen));
  104. error = xfs_attr3_rmt_stale(dp,
  105. be32_to_cpu(name_rmt->valueblk), blkcnt);
  106. if (error)
  107. goto err;
  108. }
  109. xfs_trans_brelse(*trans, bp);
  110. err:
  111. return error;
  112. }
  113. /*
  114. * Recurse (gasp!) through the attribute nodes until we find leaves.
  115. * We're doing a depth-first traversal in order to invalidate everything.
  116. */
  117. STATIC int
  118. xfs_attr3_node_inactive(
  119. struct xfs_trans **trans,
  120. struct xfs_inode *dp,
  121. struct xfs_buf *bp,
  122. int level)
  123. {
  124. struct xfs_mount *mp = dp->i_mount;
  125. struct xfs_da_blkinfo *info;
  126. xfs_dablk_t child_fsb;
  127. xfs_daddr_t parent_blkno, child_blkno;
  128. struct xfs_buf *child_bp;
  129. struct xfs_da3_icnode_hdr ichdr;
  130. int error, i;
  131. /*
  132. * Since this code is recursive (gasp!) we must protect ourselves.
  133. */
  134. if (level > XFS_DA_NODE_MAXDEPTH) {
  135. xfs_buf_mark_corrupt(bp);
  136. xfs_trans_brelse(*trans, bp); /* no locks for later trans */
  137. xfs_dirattr_mark_sick(dp, XFS_ATTR_FORK);
  138. return -EFSCORRUPTED;
  139. }
  140. xfs_da3_node_hdr_from_disk(dp->i_mount, &ichdr, bp->b_addr);
  141. parent_blkno = xfs_buf_daddr(bp);
  142. if (!ichdr.count) {
  143. xfs_trans_brelse(*trans, bp);
  144. return 0;
  145. }
  146. child_fsb = be32_to_cpu(ichdr.btree[0].before);
  147. xfs_trans_brelse(*trans, bp); /* no locks for later trans */
  148. bp = NULL;
  149. /*
  150. * If this is the node level just above the leaves, simply loop
  151. * over the leaves removing all of them. If this is higher up
  152. * in the tree, recurse downward.
  153. */
  154. for (i = 0; i < ichdr.count; i++) {
  155. /*
  156. * Read the subsidiary block to see what we have to work with.
  157. * Don't do this in a transaction. This is a depth-first
  158. * traversal of the tree so we may deal with many blocks
  159. * before we come back to this one.
  160. */
  161. error = xfs_da3_node_read(*trans, dp, child_fsb, &child_bp,
  162. XFS_ATTR_FORK);
  163. if (error)
  164. return error;
  165. /* save for re-read later */
  166. child_blkno = xfs_buf_daddr(child_bp);
  167. /*
  168. * Invalidate the subtree, however we have to.
  169. */
  170. info = child_bp->b_addr;
  171. switch (info->magic) {
  172. case cpu_to_be16(XFS_DA_NODE_MAGIC):
  173. case cpu_to_be16(XFS_DA3_NODE_MAGIC):
  174. error = xfs_attr3_node_inactive(trans, dp, child_bp,
  175. level + 1);
  176. break;
  177. case cpu_to_be16(XFS_ATTR_LEAF_MAGIC):
  178. case cpu_to_be16(XFS_ATTR3_LEAF_MAGIC):
  179. error = xfs_attr3_leaf_inactive(trans, dp, child_bp);
  180. break;
  181. default:
  182. xfs_buf_mark_corrupt(child_bp);
  183. xfs_trans_brelse(*trans, child_bp);
  184. xfs_dirattr_mark_sick(dp, XFS_ATTR_FORK);
  185. error = -EFSCORRUPTED;
  186. break;
  187. }
  188. if (error)
  189. return error;
  190. /*
  191. * Remove the subsidiary block from the cache and from the log.
  192. */
  193. error = xfs_trans_get_buf(*trans, mp->m_ddev_targp,
  194. child_blkno,
  195. XFS_FSB_TO_BB(mp, mp->m_attr_geo->fsbcount), 0,
  196. &child_bp);
  197. if (error)
  198. return error;
  199. xfs_trans_binval(*trans, child_bp);
  200. child_bp = NULL;
  201. /*
  202. * If we're not done, re-read the parent to get the next
  203. * child block number.
  204. */
  205. if (i + 1 < ichdr.count) {
  206. struct xfs_da3_icnode_hdr phdr;
  207. error = xfs_da3_node_read_mapped(*trans, dp,
  208. parent_blkno, &bp, XFS_ATTR_FORK);
  209. if (error)
  210. return error;
  211. xfs_da3_node_hdr_from_disk(dp->i_mount, &phdr,
  212. bp->b_addr);
  213. child_fsb = be32_to_cpu(phdr.btree[i + 1].before);
  214. xfs_trans_brelse(*trans, bp);
  215. bp = NULL;
  216. }
  217. /*
  218. * Atomically commit the whole invalidate stuff.
  219. */
  220. error = xfs_trans_roll_inode(trans, dp);
  221. if (error)
  222. return error;
  223. }
  224. return 0;
  225. }
  226. /*
  227. * Indiscriminately delete the entire attribute fork
  228. *
  229. * Recurse (gasp!) through the attribute nodes until we find leaves.
  230. * We're doing a depth-first traversal in order to invalidate everything.
  231. */
  232. static int
  233. xfs_attr3_root_inactive(
  234. struct xfs_trans **trans,
  235. struct xfs_inode *dp)
  236. {
  237. struct xfs_mount *mp = dp->i_mount;
  238. struct xfs_da_blkinfo *info;
  239. struct xfs_buf *bp;
  240. xfs_daddr_t blkno;
  241. int error;
  242. /*
  243. * Read block 0 to see what we have to work with.
  244. * We only get here if we have extents, since we remove
  245. * the extents in reverse order the extent containing
  246. * block 0 must still be there.
  247. */
  248. error = xfs_da3_node_read(*trans, dp, 0, &bp, XFS_ATTR_FORK);
  249. if (error)
  250. return error;
  251. blkno = xfs_buf_daddr(bp);
  252. /*
  253. * Invalidate the tree, even if the "tree" is only a single leaf block.
  254. * This is a depth-first traversal!
  255. */
  256. info = bp->b_addr;
  257. switch (info->magic) {
  258. case cpu_to_be16(XFS_DA_NODE_MAGIC):
  259. case cpu_to_be16(XFS_DA3_NODE_MAGIC):
  260. error = xfs_attr3_node_inactive(trans, dp, bp, 1);
  261. break;
  262. case cpu_to_be16(XFS_ATTR_LEAF_MAGIC):
  263. case cpu_to_be16(XFS_ATTR3_LEAF_MAGIC):
  264. error = xfs_attr3_leaf_inactive(trans, dp, bp);
  265. break;
  266. default:
  267. xfs_dirattr_mark_sick(dp, XFS_ATTR_FORK);
  268. error = -EFSCORRUPTED;
  269. xfs_buf_mark_corrupt(bp);
  270. xfs_trans_brelse(*trans, bp);
  271. break;
  272. }
  273. if (error)
  274. return error;
  275. /*
  276. * Invalidate the incore copy of the root block.
  277. */
  278. error = xfs_trans_get_buf(*trans, mp->m_ddev_targp, blkno,
  279. XFS_FSB_TO_BB(mp, mp->m_attr_geo->fsbcount), 0, &bp);
  280. if (error)
  281. return error;
  282. error = bp->b_error;
  283. if (error) {
  284. xfs_trans_brelse(*trans, bp);
  285. return error;
  286. }
  287. xfs_trans_binval(*trans, bp); /* remove from cache */
  288. /*
  289. * Commit the invalidate and start the next transaction.
  290. */
  291. error = xfs_trans_roll_inode(trans, dp);
  292. return error;
  293. }
  294. /*
  295. * xfs_attr_inactive kills all traces of an attribute fork on an inode. It
  296. * removes both the on-disk and in-memory inode fork. Note that this also has to
  297. * handle the condition of inodes without attributes but with an attribute fork
  298. * configured, so we can't use xfs_inode_hasattr() here.
  299. *
  300. * The in-memory attribute fork is removed even on error.
  301. */
  302. int
  303. xfs_attr_inactive(
  304. struct xfs_inode *dp)
  305. {
  306. struct xfs_trans *trans;
  307. struct xfs_mount *mp;
  308. int lock_mode = XFS_ILOCK_SHARED;
  309. int error = 0;
  310. mp = dp->i_mount;
  311. xfs_ilock(dp, lock_mode);
  312. if (!xfs_inode_has_attr_fork(dp))
  313. goto out_destroy_fork;
  314. xfs_iunlock(dp, lock_mode);
  315. lock_mode = 0;
  316. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_attrinval, 0, 0, 0, &trans);
  317. if (error)
  318. goto out_destroy_fork;
  319. lock_mode = XFS_ILOCK_EXCL;
  320. xfs_ilock(dp, lock_mode);
  321. if (!xfs_inode_has_attr_fork(dp))
  322. goto out_cancel;
  323. /*
  324. * No need to make quota reservations here. We expect to release some
  325. * blocks, not allocate, in the common case.
  326. */
  327. xfs_trans_ijoin(trans, dp, 0);
  328. /*
  329. * Invalidate and truncate the attribute fork extents. Make sure the
  330. * fork actually has xattr blocks as otherwise the invalidation has no
  331. * blocks to read and returns an error. In this case, just do the fork
  332. * removal below.
  333. */
  334. if (dp->i_af.if_nextents > 0) {
  335. error = xfs_attr3_root_inactive(&trans, dp);
  336. if (error)
  337. goto out_cancel;
  338. error = xfs_itruncate_extents(&trans, dp, XFS_ATTR_FORK, 0);
  339. if (error)
  340. goto out_cancel;
  341. }
  342. /* Reset the attribute fork - this also destroys the in-core fork */
  343. xfs_attr_fork_remove(dp, trans);
  344. error = xfs_trans_commit(trans);
  345. xfs_iunlock(dp, lock_mode);
  346. return error;
  347. out_cancel:
  348. xfs_trans_cancel(trans);
  349. out_destroy_fork:
  350. /* kill the in-core attr fork before we drop the inode lock */
  351. xfs_ifork_zap_attr(dp);
  352. if (lock_mode)
  353. xfs_iunlock(dp, lock_mode);
  354. return error;
  355. }