xfs_attr_inactive.c 9.7 KB

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