xfs_attr_inactive.c 11 KB

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