xfs_attr_inactive.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. struct xfs_mount *mp = bp->b_target->bt_mount;
  129. leaf = bp->b_addr;
  130. xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, leaf);
  131. /*
  132. * Count the number of "remote" value extents.
  133. */
  134. count = 0;
  135. entry = xfs_attr3_leaf_entryp(leaf);
  136. for (i = 0; i < ichdr.count; entry++, i++) {
  137. if (be16_to_cpu(entry->nameidx) &&
  138. ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
  139. name_rmt = xfs_attr3_leaf_name_remote(leaf, i);
  140. if (name_rmt->valueblk)
  141. count++;
  142. }
  143. }
  144. /*
  145. * If there are no "remote" values, we're done.
  146. */
  147. if (count == 0) {
  148. xfs_trans_brelse(*trans, bp);
  149. return 0;
  150. }
  151. /*
  152. * Allocate storage for a list of all the "remote" value extents.
  153. */
  154. size = count * sizeof(xfs_attr_inactive_list_t);
  155. list = kmem_alloc(size, KM_SLEEP);
  156. /*
  157. * Identify each of the "remote" value extents.
  158. */
  159. lp = list;
  160. entry = xfs_attr3_leaf_entryp(leaf);
  161. for (i = 0; i < ichdr.count; entry++, i++) {
  162. if (be16_to_cpu(entry->nameidx) &&
  163. ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
  164. name_rmt = xfs_attr3_leaf_name_remote(leaf, i);
  165. if (name_rmt->valueblk) {
  166. lp->valueblk = be32_to_cpu(name_rmt->valueblk);
  167. lp->valuelen = xfs_attr3_rmt_blocks(dp->i_mount,
  168. be32_to_cpu(name_rmt->valuelen));
  169. lp++;
  170. }
  171. }
  172. }
  173. xfs_trans_brelse(*trans, bp); /* unlock for trans. in freextent() */
  174. /*
  175. * Invalidate each of the "remote" value extents.
  176. */
  177. error = 0;
  178. for (lp = list, i = 0; i < count; i++, lp++) {
  179. tmp = xfs_attr3_leaf_freextent(trans, dp,
  180. lp->valueblk, lp->valuelen);
  181. if (error == 0)
  182. error = tmp; /* save only the 1st errno */
  183. }
  184. kmem_free(list);
  185. return error;
  186. }
  187. /*
  188. * Recurse (gasp!) through the attribute nodes until we find leaves.
  189. * We're doing a depth-first traversal in order to invalidate everything.
  190. */
  191. STATIC int
  192. xfs_attr3_node_inactive(
  193. struct xfs_trans **trans,
  194. struct xfs_inode *dp,
  195. struct xfs_buf *bp,
  196. int level)
  197. {
  198. xfs_da_blkinfo_t *info;
  199. xfs_da_intnode_t *node;
  200. xfs_dablk_t child_fsb;
  201. xfs_daddr_t parent_blkno, child_blkno;
  202. int error, i;
  203. struct xfs_buf *child_bp;
  204. struct xfs_da_node_entry *btree;
  205. struct xfs_da3_icnode_hdr ichdr;
  206. /*
  207. * Since this code is recursive (gasp!) we must protect ourselves.
  208. */
  209. if (level > XFS_DA_NODE_MAXDEPTH) {
  210. xfs_trans_brelse(*trans, bp); /* no locks for later trans */
  211. return -EIO;
  212. }
  213. node = bp->b_addr;
  214. dp->d_ops->node_hdr_from_disk(&ichdr, node);
  215. parent_blkno = bp->b_bn;
  216. if (!ichdr.count) {
  217. xfs_trans_brelse(*trans, bp);
  218. return 0;
  219. }
  220. btree = dp->d_ops->node_tree_p(node);
  221. child_fsb = be32_to_cpu(btree[0].before);
  222. xfs_trans_brelse(*trans, bp); /* no locks for later trans */
  223. /*
  224. * If this is the node level just above the leaves, simply loop
  225. * over the leaves removing all of them. If this is higher up
  226. * in the tree, recurse downward.
  227. */
  228. for (i = 0; i < ichdr.count; i++) {
  229. /*
  230. * Read the subsidiary block to see what we have to work with.
  231. * Don't do this in a transaction. This is a depth-first
  232. * traversal of the tree so we may deal with many blocks
  233. * before we come back to this one.
  234. */
  235. error = xfs_da3_node_read(*trans, dp, child_fsb, -2, &child_bp,
  236. XFS_ATTR_FORK);
  237. if (error)
  238. return error;
  239. if (child_bp) {
  240. /* save for re-read later */
  241. child_blkno = XFS_BUF_ADDR(child_bp);
  242. /*
  243. * Invalidate the subtree, however we have to.
  244. */
  245. info = child_bp->b_addr;
  246. switch (info->magic) {
  247. case cpu_to_be16(XFS_DA_NODE_MAGIC):
  248. case cpu_to_be16(XFS_DA3_NODE_MAGIC):
  249. error = xfs_attr3_node_inactive(trans, dp,
  250. child_bp, level + 1);
  251. break;
  252. case cpu_to_be16(XFS_ATTR_LEAF_MAGIC):
  253. case cpu_to_be16(XFS_ATTR3_LEAF_MAGIC):
  254. error = xfs_attr3_leaf_inactive(trans, dp,
  255. child_bp);
  256. break;
  257. default:
  258. error = -EIO;
  259. xfs_trans_brelse(*trans, child_bp);
  260. break;
  261. }
  262. if (error)
  263. return error;
  264. /*
  265. * Remove the subsidiary block from the cache
  266. * and from the log.
  267. */
  268. error = xfs_da_get_buf(*trans, dp, 0, child_blkno,
  269. &child_bp, XFS_ATTR_FORK);
  270. if (error)
  271. return error;
  272. xfs_trans_binval(*trans, child_bp);
  273. }
  274. /*
  275. * If we're not done, re-read the parent to get the next
  276. * child block number.
  277. */
  278. if (i + 1 < ichdr.count) {
  279. error = xfs_da3_node_read(*trans, dp, 0, parent_blkno,
  280. &bp, XFS_ATTR_FORK);
  281. if (error)
  282. return error;
  283. child_fsb = be32_to_cpu(btree[i + 1].before);
  284. xfs_trans_brelse(*trans, bp);
  285. }
  286. /*
  287. * Atomically commit the whole invalidate stuff.
  288. */
  289. error = xfs_trans_roll(trans, dp);
  290. if (error)
  291. return error;
  292. }
  293. return 0;
  294. }
  295. /*
  296. * Indiscriminately delete the entire attribute fork
  297. *
  298. * Recurse (gasp!) through the attribute nodes until we find leaves.
  299. * We're doing a depth-first traversal in order to invalidate everything.
  300. */
  301. int
  302. xfs_attr3_root_inactive(
  303. struct xfs_trans **trans,
  304. struct xfs_inode *dp)
  305. {
  306. struct xfs_da_blkinfo *info;
  307. struct xfs_buf *bp;
  308. xfs_daddr_t blkno;
  309. int error;
  310. /*
  311. * Read block 0 to see what we have to work with.
  312. * We only get here if we have extents, since we remove
  313. * the extents in reverse order the extent containing
  314. * block 0 must still be there.
  315. */
  316. error = xfs_da3_node_read(*trans, dp, 0, -1, &bp, XFS_ATTR_FORK);
  317. if (error)
  318. return error;
  319. blkno = bp->b_bn;
  320. /*
  321. * Invalidate the tree, even if the "tree" is only a single leaf block.
  322. * This is a depth-first traversal!
  323. */
  324. info = bp->b_addr;
  325. switch (info->magic) {
  326. case cpu_to_be16(XFS_DA_NODE_MAGIC):
  327. case cpu_to_be16(XFS_DA3_NODE_MAGIC):
  328. error = xfs_attr3_node_inactive(trans, dp, bp, 1);
  329. break;
  330. case cpu_to_be16(XFS_ATTR_LEAF_MAGIC):
  331. case cpu_to_be16(XFS_ATTR3_LEAF_MAGIC):
  332. error = xfs_attr3_leaf_inactive(trans, dp, bp);
  333. break;
  334. default:
  335. error = -EIO;
  336. xfs_trans_brelse(*trans, bp);
  337. break;
  338. }
  339. if (error)
  340. return error;
  341. /*
  342. * Invalidate the incore copy of the root block.
  343. */
  344. error = xfs_da_get_buf(*trans, dp, 0, blkno, &bp, XFS_ATTR_FORK);
  345. if (error)
  346. return error;
  347. xfs_trans_binval(*trans, bp); /* remove from cache */
  348. /*
  349. * Commit the invalidate and start the next transaction.
  350. */
  351. error = xfs_trans_roll(trans, dp);
  352. return error;
  353. }
  354. /*
  355. * xfs_attr_inactive kills all traces of an attribute fork on an inode. It
  356. * removes both the on-disk and in-memory inode fork. Note that this also has to
  357. * handle the condition of inodes without attributes but with an attribute fork
  358. * configured, so we can't use xfs_inode_hasattr() here.
  359. *
  360. * The in-memory attribute fork is removed even on error.
  361. */
  362. int
  363. xfs_attr_inactive(
  364. struct xfs_inode *dp)
  365. {
  366. struct xfs_trans *trans;
  367. struct xfs_mount *mp;
  368. int lock_mode = XFS_ILOCK_SHARED;
  369. int error = 0;
  370. mp = dp->i_mount;
  371. ASSERT(! XFS_NOT_DQATTACHED(mp, dp));
  372. xfs_ilock(dp, lock_mode);
  373. if (!XFS_IFORK_Q(dp))
  374. goto out_destroy_fork;
  375. xfs_iunlock(dp, lock_mode);
  376. /*
  377. * Start our first transaction of the day.
  378. *
  379. * All future transactions during this code must be "chained" off
  380. * this one via the trans_dup() call. All transactions will contain
  381. * the inode, and the inode will always be marked with trans_ihold().
  382. * Since the inode will be locked in all transactions, we must log
  383. * the inode in every transaction to let it float upward through
  384. * the log.
  385. */
  386. lock_mode = 0;
  387. trans = xfs_trans_alloc(mp, XFS_TRANS_ATTRINVAL);
  388. error = xfs_trans_reserve(trans, &M_RES(mp)->tr_attrinval, 0, 0);
  389. if (error)
  390. goto out_cancel;
  391. lock_mode = XFS_ILOCK_EXCL;
  392. xfs_ilock(dp, lock_mode);
  393. if (!XFS_IFORK_Q(dp))
  394. goto out_cancel;
  395. /*
  396. * No need to make quota reservations here. We expect to release some
  397. * blocks, not allocate, in the common case.
  398. */
  399. xfs_trans_ijoin(trans, dp, 0);
  400. /*
  401. * Invalidate and truncate the attribute fork extents. Make sure the
  402. * fork actually has attributes as otherwise the invalidation has no
  403. * blocks to read and returns an error. In this case, just do the fork
  404. * removal below.
  405. */
  406. if (xfs_inode_hasattr(dp) &&
  407. dp->i_d.di_aformat != XFS_DINODE_FMT_LOCAL) {
  408. error = xfs_attr3_root_inactive(&trans, dp);
  409. if (error)
  410. goto out_cancel;
  411. error = xfs_itruncate_extents(&trans, dp, XFS_ATTR_FORK, 0);
  412. if (error)
  413. goto out_cancel;
  414. }
  415. /* Reset the attribute fork - this also destroys the in-core fork */
  416. xfs_attr_fork_remove(dp, trans);
  417. error = xfs_trans_commit(trans);
  418. xfs_iunlock(dp, lock_mode);
  419. return error;
  420. out_cancel:
  421. xfs_trans_cancel(trans);
  422. out_destroy_fork:
  423. /* kill the in-core attr fork before we drop the inode lock */
  424. if (dp->i_afp)
  425. xfs_idestroy_fork(dp, XFS_ATTR_FORK);
  426. if (lock_mode)
  427. xfs_iunlock(dp, lock_mode);
  428. return error;
  429. }