xfs_attr_inactive.c 11 KB

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