xfs_attr_inactive.c 11 KB

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