xfs_attr_inactive.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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_remote.h"
  19. #include "xfs_trans.h"
  20. #include "xfs_bmap.h"
  21. #include "xfs_attr.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 = bp->b_bn;
  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. /*
  147. * If this is the node level just above the leaves, simply loop
  148. * over the leaves removing all of them. If this is higher up
  149. * in the tree, recurse downward.
  150. */
  151. for (i = 0; i < ichdr.count; i++) {
  152. /*
  153. * Read the subsidiary block to see what we have to work with.
  154. * Don't do this in a transaction. This is a depth-first
  155. * traversal of the tree so we may deal with many blocks
  156. * before we come back to this one.
  157. */
  158. error = xfs_da3_node_read(*trans, dp, child_fsb, &child_bp,
  159. XFS_ATTR_FORK);
  160. if (error)
  161. return error;
  162. /* save for re-read later */
  163. child_blkno = XFS_BUF_ADDR(child_bp);
  164. /*
  165. * Invalidate the subtree, however we have to.
  166. */
  167. info = child_bp->b_addr;
  168. switch (info->magic) {
  169. case cpu_to_be16(XFS_DA_NODE_MAGIC):
  170. case cpu_to_be16(XFS_DA3_NODE_MAGIC):
  171. error = xfs_attr3_node_inactive(trans, dp, child_bp,
  172. level + 1);
  173. break;
  174. case cpu_to_be16(XFS_ATTR_LEAF_MAGIC):
  175. case cpu_to_be16(XFS_ATTR3_LEAF_MAGIC):
  176. error = xfs_attr3_leaf_inactive(trans, dp, child_bp);
  177. break;
  178. default:
  179. xfs_buf_mark_corrupt(child_bp);
  180. xfs_trans_brelse(*trans, child_bp);
  181. error = -EFSCORRUPTED;
  182. break;
  183. }
  184. if (error)
  185. return error;
  186. /*
  187. * Remove the subsidiary block from the cache and from the log.
  188. */
  189. error = xfs_trans_get_buf(*trans, mp->m_ddev_targp,
  190. child_blkno,
  191. XFS_FSB_TO_BB(mp, mp->m_attr_geo->fsbcount), 0,
  192. &child_bp);
  193. if (error)
  194. return error;
  195. error = bp->b_error;
  196. if (error) {
  197. xfs_trans_brelse(*trans, child_bp);
  198. return error;
  199. }
  200. xfs_trans_binval(*trans, child_bp);
  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. }
  216. /*
  217. * Atomically commit the whole invalidate stuff.
  218. */
  219. error = xfs_trans_roll_inode(trans, dp);
  220. if (error)
  221. return error;
  222. }
  223. return 0;
  224. }
  225. /*
  226. * Indiscriminately delete the entire attribute fork
  227. *
  228. * Recurse (gasp!) through the attribute nodes until we find leaves.
  229. * We're doing a depth-first traversal in order to invalidate everything.
  230. */
  231. static int
  232. xfs_attr3_root_inactive(
  233. struct xfs_trans **trans,
  234. struct xfs_inode *dp)
  235. {
  236. struct xfs_mount *mp = dp->i_mount;
  237. struct xfs_da_blkinfo *info;
  238. struct xfs_buf *bp;
  239. xfs_daddr_t blkno;
  240. int error;
  241. /*
  242. * Read block 0 to see what we have to work with.
  243. * We only get here if we have extents, since we remove
  244. * the extents in reverse order the extent containing
  245. * block 0 must still be there.
  246. */
  247. error = xfs_da3_node_read(*trans, dp, 0, &bp, XFS_ATTR_FORK);
  248. if (error)
  249. return error;
  250. blkno = bp->b_bn;
  251. /*
  252. * Invalidate the tree, even if the "tree" is only a single leaf block.
  253. * This is a depth-first traversal!
  254. */
  255. info = bp->b_addr;
  256. switch (info->magic) {
  257. case cpu_to_be16(XFS_DA_NODE_MAGIC):
  258. case cpu_to_be16(XFS_DA3_NODE_MAGIC):
  259. error = xfs_attr3_node_inactive(trans, dp, bp, 1);
  260. break;
  261. case cpu_to_be16(XFS_ATTR_LEAF_MAGIC):
  262. case cpu_to_be16(XFS_ATTR3_LEAF_MAGIC):
  263. error = xfs_attr3_leaf_inactive(trans, dp, bp);
  264. break;
  265. default:
  266. error = -EFSCORRUPTED;
  267. xfs_buf_mark_corrupt(bp);
  268. xfs_trans_brelse(*trans, bp);
  269. break;
  270. }
  271. if (error)
  272. return error;
  273. /*
  274. * Invalidate the incore copy of the root block.
  275. */
  276. error = xfs_trans_get_buf(*trans, mp->m_ddev_targp, blkno,
  277. XFS_FSB_TO_BB(mp, mp->m_attr_geo->fsbcount), 0, &bp);
  278. if (error)
  279. return error;
  280. error = bp->b_error;
  281. if (error) {
  282. xfs_trans_brelse(*trans, bp);
  283. return error;
  284. }
  285. xfs_trans_binval(*trans, bp); /* remove from cache */
  286. /*
  287. * Commit the invalidate and start the next transaction.
  288. */
  289. error = xfs_trans_roll_inode(trans, dp);
  290. return error;
  291. }
  292. /*
  293. * xfs_attr_inactive kills all traces of an attribute fork on an inode. It
  294. * removes both the on-disk and in-memory inode fork. Note that this also has to
  295. * handle the condition of inodes without attributes but with an attribute fork
  296. * configured, so we can't use xfs_inode_hasattr() here.
  297. *
  298. * The in-memory attribute fork is removed even on error.
  299. */
  300. int
  301. xfs_attr_inactive(
  302. struct xfs_inode *dp)
  303. {
  304. struct xfs_trans *trans;
  305. struct xfs_mount *mp;
  306. int lock_mode = XFS_ILOCK_SHARED;
  307. int error = 0;
  308. mp = dp->i_mount;
  309. ASSERT(! XFS_NOT_DQATTACHED(mp, dp));
  310. xfs_ilock(dp, lock_mode);
  311. if (!XFS_IFORK_Q(dp))
  312. goto out_destroy_fork;
  313. xfs_iunlock(dp, lock_mode);
  314. lock_mode = 0;
  315. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_attrinval, 0, 0, 0, &trans);
  316. if (error)
  317. goto out_destroy_fork;
  318. lock_mode = XFS_ILOCK_EXCL;
  319. xfs_ilock(dp, lock_mode);
  320. if (!XFS_IFORK_Q(dp))
  321. goto out_cancel;
  322. /*
  323. * No need to make quota reservations here. We expect to release some
  324. * blocks, not allocate, in the common case.
  325. */
  326. xfs_trans_ijoin(trans, dp, 0);
  327. /*
  328. * Invalidate and truncate the attribute fork extents. Make sure the
  329. * fork actually has attributes as otherwise the invalidation has no
  330. * blocks to read and returns an error. In this case, just do the fork
  331. * removal below.
  332. */
  333. if (xfs_inode_hasattr(dp) &&
  334. dp->i_afp->if_format != XFS_DINODE_FMT_LOCAL) {
  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. if (dp->i_afp) {
  352. xfs_idestroy_fork(dp->i_afp);
  353. kmem_cache_free(xfs_ifork_zone, dp->i_afp);
  354. dp->i_afp = NULL;
  355. }
  356. if (lock_mode)
  357. xfs_iunlock(dp, lock_mode);
  358. return error;
  359. }