xfs_attr_inactive.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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_alloc.h"
  19. #include "xfs_attr_remote.h"
  20. #include "xfs_trans.h"
  21. #include "xfs_inode_item.h"
  22. #include "xfs_bmap.h"
  23. #include "xfs_attr.h"
  24. #include "xfs_attr_leaf.h"
  25. #include "xfs_error.h"
  26. #include "xfs_quota.h"
  27. #include "xfs_trace.h"
  28. #include "xfs_dir2.h"
  29. /*
  30. * Look at all the extents for this logical region,
  31. * invalidate any buffers that are incore/in transactions.
  32. */
  33. STATIC int
  34. xfs_attr3_leaf_freextent(
  35. struct xfs_trans **trans,
  36. struct xfs_inode *dp,
  37. xfs_dablk_t blkno,
  38. int blkcnt)
  39. {
  40. struct xfs_bmbt_irec map;
  41. struct xfs_buf *bp;
  42. xfs_dablk_t tblkno;
  43. xfs_daddr_t dblkno;
  44. int tblkcnt;
  45. int dblkcnt;
  46. int nmap;
  47. int error;
  48. /*
  49. * Roll through the "value", invalidating the attribute value's
  50. * blocks.
  51. */
  52. tblkno = blkno;
  53. tblkcnt = blkcnt;
  54. while (tblkcnt > 0) {
  55. /*
  56. * Try to remember where we decided to put the value.
  57. */
  58. nmap = 1;
  59. error = xfs_bmapi_read(dp, (xfs_fileoff_t)tblkno, tblkcnt,
  60. &map, &nmap, XFS_BMAPI_ATTRFORK);
  61. if (error) {
  62. return error;
  63. }
  64. ASSERT(nmap == 1);
  65. ASSERT(map.br_startblock != DELAYSTARTBLOCK);
  66. /*
  67. * If it's a hole, these are already unmapped
  68. * so there's nothing to invalidate.
  69. */
  70. if (map.br_startblock != HOLESTARTBLOCK) {
  71. dblkno = XFS_FSB_TO_DADDR(dp->i_mount,
  72. map.br_startblock);
  73. dblkcnt = XFS_FSB_TO_BB(dp->i_mount,
  74. map.br_blockcount);
  75. bp = xfs_trans_get_buf(*trans,
  76. dp->i_mount->m_ddev_targp,
  77. dblkno, dblkcnt, 0);
  78. if (!bp)
  79. return -ENOMEM;
  80. xfs_trans_binval(*trans, bp);
  81. /*
  82. * Roll to next transaction.
  83. */
  84. error = xfs_trans_roll_inode(trans, dp);
  85. if (error)
  86. return error;
  87. }
  88. tblkno += map.br_blockcount;
  89. tblkcnt -= map.br_blockcount;
  90. }
  91. return 0;
  92. }
  93. /*
  94. * Invalidate all of the "remote" value regions pointed to by a particular
  95. * leaf block.
  96. * Note that we must release the lock on the buffer so that we are not
  97. * caught holding something that the logging code wants to flush to disk.
  98. */
  99. STATIC int
  100. xfs_attr3_leaf_inactive(
  101. struct xfs_trans **trans,
  102. struct xfs_inode *dp,
  103. struct xfs_buf *bp)
  104. {
  105. struct xfs_attr_leafblock *leaf;
  106. struct xfs_attr3_icleaf_hdr ichdr;
  107. struct xfs_attr_leaf_entry *entry;
  108. struct xfs_attr_leaf_name_remote *name_rmt;
  109. struct xfs_attr_inactive_list *list;
  110. struct xfs_attr_inactive_list *lp;
  111. int error;
  112. int count;
  113. int size;
  114. int tmp;
  115. int i;
  116. struct xfs_mount *mp = bp->b_target->bt_mount;
  117. leaf = bp->b_addr;
  118. xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, leaf);
  119. /*
  120. * Count the number of "remote" value extents.
  121. */
  122. count = 0;
  123. entry = xfs_attr3_leaf_entryp(leaf);
  124. for (i = 0; i < ichdr.count; entry++, i++) {
  125. if (be16_to_cpu(entry->nameidx) &&
  126. ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
  127. name_rmt = xfs_attr3_leaf_name_remote(leaf, i);
  128. if (name_rmt->valueblk)
  129. count++;
  130. }
  131. }
  132. /*
  133. * If there are no "remote" values, we're done.
  134. */
  135. if (count == 0) {
  136. xfs_trans_brelse(*trans, bp);
  137. return 0;
  138. }
  139. /*
  140. * Allocate storage for a list of all the "remote" value extents.
  141. */
  142. size = count * sizeof(xfs_attr_inactive_list_t);
  143. list = kmem_alloc(size, KM_SLEEP);
  144. /*
  145. * Identify each of the "remote" value extents.
  146. */
  147. lp = list;
  148. entry = xfs_attr3_leaf_entryp(leaf);
  149. for (i = 0; i < ichdr.count; entry++, i++) {
  150. if (be16_to_cpu(entry->nameidx) &&
  151. ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
  152. name_rmt = xfs_attr3_leaf_name_remote(leaf, i);
  153. if (name_rmt->valueblk) {
  154. lp->valueblk = be32_to_cpu(name_rmt->valueblk);
  155. lp->valuelen = xfs_attr3_rmt_blocks(dp->i_mount,
  156. be32_to_cpu(name_rmt->valuelen));
  157. lp++;
  158. }
  159. }
  160. }
  161. xfs_trans_brelse(*trans, bp); /* unlock for trans. in freextent() */
  162. /*
  163. * Invalidate each of the "remote" value extents.
  164. */
  165. error = 0;
  166. for (lp = list, i = 0; i < count; i++, lp++) {
  167. tmp = xfs_attr3_leaf_freextent(trans, dp,
  168. lp->valueblk, lp->valuelen);
  169. if (error == 0)
  170. error = tmp; /* save only the 1st errno */
  171. }
  172. kmem_free(list);
  173. return error;
  174. }
  175. /*
  176. * Recurse (gasp!) through the attribute nodes until we find leaves.
  177. * We're doing a depth-first traversal in order to invalidate everything.
  178. */
  179. STATIC int
  180. xfs_attr3_node_inactive(
  181. struct xfs_trans **trans,
  182. struct xfs_inode *dp,
  183. struct xfs_buf *bp,
  184. int level)
  185. {
  186. xfs_da_blkinfo_t *info;
  187. xfs_da_intnode_t *node;
  188. xfs_dablk_t child_fsb;
  189. xfs_daddr_t parent_blkno, child_blkno;
  190. int error, i;
  191. struct xfs_buf *child_bp;
  192. struct xfs_da_node_entry *btree;
  193. struct xfs_da3_icnode_hdr ichdr;
  194. /*
  195. * Since this code is recursive (gasp!) we must protect ourselves.
  196. */
  197. if (level > XFS_DA_NODE_MAXDEPTH) {
  198. xfs_trans_brelse(*trans, bp); /* no locks for later trans */
  199. return -EIO;
  200. }
  201. node = bp->b_addr;
  202. dp->d_ops->node_hdr_from_disk(&ichdr, node);
  203. parent_blkno = bp->b_bn;
  204. if (!ichdr.count) {
  205. xfs_trans_brelse(*trans, bp);
  206. return 0;
  207. }
  208. btree = dp->d_ops->node_tree_p(node);
  209. child_fsb = be32_to_cpu(btree[0].before);
  210. xfs_trans_brelse(*trans, bp); /* no locks for later trans */
  211. /*
  212. * If this is the node level just above the leaves, simply loop
  213. * over the leaves removing all of them. If this is higher up
  214. * in the tree, recurse downward.
  215. */
  216. for (i = 0; i < ichdr.count; i++) {
  217. /*
  218. * Read the subsidiary block to see what we have to work with.
  219. * Don't do this in a transaction. This is a depth-first
  220. * traversal of the tree so we may deal with many blocks
  221. * before we come back to this one.
  222. */
  223. error = xfs_da3_node_read(*trans, dp, child_fsb, -1, &child_bp,
  224. XFS_ATTR_FORK);
  225. if (error)
  226. return error;
  227. /* save for re-read later */
  228. child_blkno = XFS_BUF_ADDR(child_bp);
  229. /*
  230. * Invalidate the subtree, however we have to.
  231. */
  232. info = child_bp->b_addr;
  233. switch (info->magic) {
  234. case cpu_to_be16(XFS_DA_NODE_MAGIC):
  235. case cpu_to_be16(XFS_DA3_NODE_MAGIC):
  236. error = xfs_attr3_node_inactive(trans, dp, child_bp,
  237. level + 1);
  238. break;
  239. case cpu_to_be16(XFS_ATTR_LEAF_MAGIC):
  240. case cpu_to_be16(XFS_ATTR3_LEAF_MAGIC):
  241. error = xfs_attr3_leaf_inactive(trans, dp, child_bp);
  242. break;
  243. default:
  244. error = -EIO;
  245. xfs_trans_brelse(*trans, child_bp);
  246. break;
  247. }
  248. if (error)
  249. return error;
  250. /*
  251. * Remove the subsidiary block from the cache and from the log.
  252. */
  253. error = xfs_da_get_buf(*trans, dp, 0, child_blkno, &child_bp,
  254. XFS_ATTR_FORK);
  255. if (error)
  256. return error;
  257. xfs_trans_binval(*trans, child_bp);
  258. /*
  259. * If we're not done, re-read the parent to get the next
  260. * child block number.
  261. */
  262. if (i + 1 < ichdr.count) {
  263. error = xfs_da3_node_read(*trans, dp, 0, parent_blkno,
  264. &bp, XFS_ATTR_FORK);
  265. if (error)
  266. return error;
  267. node = bp->b_addr;
  268. btree = dp->d_ops->node_tree_p(node);
  269. child_fsb = be32_to_cpu(btree[i + 1].before);
  270. xfs_trans_brelse(*trans, bp);
  271. }
  272. /*
  273. * Atomically commit the whole invalidate stuff.
  274. */
  275. error = xfs_trans_roll_inode(trans, dp);
  276. if (error)
  277. return error;
  278. }
  279. return 0;
  280. }
  281. /*
  282. * Indiscriminately delete the entire attribute fork
  283. *
  284. * Recurse (gasp!) through the attribute nodes until we find leaves.
  285. * We're doing a depth-first traversal in order to invalidate everything.
  286. */
  287. static int
  288. xfs_attr3_root_inactive(
  289. struct xfs_trans **trans,
  290. struct xfs_inode *dp)
  291. {
  292. struct xfs_da_blkinfo *info;
  293. struct xfs_buf *bp;
  294. xfs_daddr_t blkno;
  295. int error;
  296. /*
  297. * Read block 0 to see what we have to work with.
  298. * We only get here if we have extents, since we remove
  299. * the extents in reverse order the extent containing
  300. * block 0 must still be there.
  301. */
  302. error = xfs_da3_node_read(*trans, dp, 0, -1, &bp, XFS_ATTR_FORK);
  303. if (error)
  304. return error;
  305. blkno = bp->b_bn;
  306. /*
  307. * Invalidate the tree, even if the "tree" is only a single leaf block.
  308. * This is a depth-first traversal!
  309. */
  310. info = bp->b_addr;
  311. switch (info->magic) {
  312. case cpu_to_be16(XFS_DA_NODE_MAGIC):
  313. case cpu_to_be16(XFS_DA3_NODE_MAGIC):
  314. error = xfs_attr3_node_inactive(trans, dp, bp, 1);
  315. break;
  316. case cpu_to_be16(XFS_ATTR_LEAF_MAGIC):
  317. case cpu_to_be16(XFS_ATTR3_LEAF_MAGIC):
  318. error = xfs_attr3_leaf_inactive(trans, dp, bp);
  319. break;
  320. default:
  321. error = -EIO;
  322. xfs_trans_brelse(*trans, bp);
  323. break;
  324. }
  325. if (error)
  326. return error;
  327. /*
  328. * Invalidate the incore copy of the root block.
  329. */
  330. error = xfs_da_get_buf(*trans, dp, 0, blkno, &bp, XFS_ATTR_FORK);
  331. if (error)
  332. return error;
  333. xfs_trans_binval(*trans, bp); /* remove from cache */
  334. /*
  335. * Commit the invalidate and start the next transaction.
  336. */
  337. error = xfs_trans_roll_inode(trans, dp);
  338. return error;
  339. }
  340. /*
  341. * xfs_attr_inactive kills all traces of an attribute fork on an inode. It
  342. * removes both the on-disk and in-memory inode fork. Note that this also has to
  343. * handle the condition of inodes without attributes but with an attribute fork
  344. * configured, so we can't use xfs_inode_hasattr() here.
  345. *
  346. * The in-memory attribute fork is removed even on error.
  347. */
  348. int
  349. xfs_attr_inactive(
  350. struct xfs_inode *dp)
  351. {
  352. struct xfs_trans *trans;
  353. struct xfs_mount *mp;
  354. int lock_mode = XFS_ILOCK_SHARED;
  355. int error = 0;
  356. mp = dp->i_mount;
  357. ASSERT(! XFS_NOT_DQATTACHED(mp, dp));
  358. xfs_ilock(dp, lock_mode);
  359. if (!XFS_IFORK_Q(dp))
  360. goto out_destroy_fork;
  361. xfs_iunlock(dp, lock_mode);
  362. lock_mode = 0;
  363. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_attrinval, 0, 0, 0, &trans);
  364. if (error)
  365. goto out_destroy_fork;
  366. lock_mode = XFS_ILOCK_EXCL;
  367. xfs_ilock(dp, lock_mode);
  368. if (!XFS_IFORK_Q(dp))
  369. goto out_cancel;
  370. /*
  371. * No need to make quota reservations here. We expect to release some
  372. * blocks, not allocate, in the common case.
  373. */
  374. xfs_trans_ijoin(trans, dp, 0);
  375. /*
  376. * Invalidate and truncate the attribute fork extents. Make sure the
  377. * fork actually has attributes as otherwise the invalidation has no
  378. * blocks to read and returns an error. In this case, just do the fork
  379. * removal below.
  380. */
  381. if (xfs_inode_hasattr(dp) &&
  382. dp->i_d.di_aformat != XFS_DINODE_FMT_LOCAL) {
  383. error = xfs_attr3_root_inactive(&trans, dp);
  384. if (error)
  385. goto out_cancel;
  386. error = xfs_itruncate_extents(&trans, dp, XFS_ATTR_FORK, 0);
  387. if (error)
  388. goto out_cancel;
  389. }
  390. /* Reset the attribute fork - this also destroys the in-core fork */
  391. xfs_attr_fork_remove(dp, trans);
  392. error = xfs_trans_commit(trans);
  393. xfs_iunlock(dp, lock_mode);
  394. return error;
  395. out_cancel:
  396. xfs_trans_cancel(trans);
  397. out_destroy_fork:
  398. /* kill the in-core attr fork before we drop the inode lock */
  399. if (dp->i_afp)
  400. xfs_idestroy_fork(dp, XFS_ATTR_FORK);
  401. if (lock_mode)
  402. xfs_iunlock(dp, lock_mode);
  403. return error;
  404. }