xfs_attr_inactive.c 11 KB

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