xfs_attr_remote.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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_types.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_error.h"
  29. #include "xfs_da_btree.h"
  30. #include "xfs_bmap_btree.h"
  31. #include "xfs_dinode.h"
  32. #include "xfs_inode.h"
  33. #include "xfs_alloc.h"
  34. #include "xfs_inode_item.h"
  35. #include "xfs_bmap.h"
  36. #include "xfs_attr.h"
  37. #include "xfs_attr_leaf.h"
  38. #include "xfs_attr_remote.h"
  39. #include "xfs_trans_space.h"
  40. #include "xfs_trace.h"
  41. #include "xfs_cksum.h"
  42. #include "xfs_buf_item.h"
  43. #define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
  44. /*
  45. * Each contiguous block has a header, so it is not just a simple attribute
  46. * length to FSB conversion.
  47. */
  48. static int
  49. xfs_attr3_rmt_blocks(
  50. struct xfs_mount *mp,
  51. int attrlen)
  52. {
  53. int buflen = XFS_ATTR3_RMT_BUF_SPACE(mp,
  54. mp->m_sb.sb_blocksize);
  55. return (attrlen + buflen - 1) / buflen;
  56. }
  57. static bool
  58. xfs_attr3_rmt_verify(
  59. struct xfs_buf *bp)
  60. {
  61. struct xfs_mount *mp = bp->b_target->bt_mount;
  62. struct xfs_attr3_rmt_hdr *rmt = bp->b_addr;
  63. if (!xfs_sb_version_hascrc(&mp->m_sb))
  64. return false;
  65. if (rmt->rm_magic != cpu_to_be32(XFS_ATTR3_RMT_MAGIC))
  66. return false;
  67. if (!uuid_equal(&rmt->rm_uuid, &mp->m_sb.sb_uuid))
  68. return false;
  69. if (bp->b_bn != be64_to_cpu(rmt->rm_blkno))
  70. return false;
  71. if (be32_to_cpu(rmt->rm_offset) +
  72. be32_to_cpu(rmt->rm_bytes) >= XATTR_SIZE_MAX)
  73. return false;
  74. if (rmt->rm_owner == 0)
  75. return false;
  76. return true;
  77. }
  78. static void
  79. xfs_attr3_rmt_read_verify(
  80. struct xfs_buf *bp)
  81. {
  82. struct xfs_mount *mp = bp->b_target->bt_mount;
  83. /* no verification of non-crc buffers */
  84. if (!xfs_sb_version_hascrc(&mp->m_sb))
  85. return;
  86. if (!xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
  87. XFS_ATTR3_RMT_CRC_OFF) ||
  88. !xfs_attr3_rmt_verify(bp)) {
  89. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
  90. xfs_buf_ioerror(bp, EFSCORRUPTED);
  91. }
  92. }
  93. static void
  94. xfs_attr3_rmt_write_verify(
  95. struct xfs_buf *bp)
  96. {
  97. struct xfs_mount *mp = bp->b_target->bt_mount;
  98. struct xfs_buf_log_item *bip = bp->b_fspriv;
  99. /* no verification of non-crc buffers */
  100. if (!xfs_sb_version_hascrc(&mp->m_sb))
  101. return;
  102. if (!xfs_attr3_rmt_verify(bp)) {
  103. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
  104. xfs_buf_ioerror(bp, EFSCORRUPTED);
  105. return;
  106. }
  107. if (bip) {
  108. struct xfs_attr3_rmt_hdr *rmt = bp->b_addr;
  109. rmt->rm_lsn = cpu_to_be64(bip->bli_item.li_lsn);
  110. }
  111. xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length),
  112. XFS_ATTR3_RMT_CRC_OFF);
  113. }
  114. const struct xfs_buf_ops xfs_attr3_rmt_buf_ops = {
  115. .verify_read = xfs_attr3_rmt_read_verify,
  116. .verify_write = xfs_attr3_rmt_write_verify,
  117. };
  118. static int
  119. xfs_attr3_rmt_hdr_set(
  120. struct xfs_mount *mp,
  121. xfs_ino_t ino,
  122. uint32_t offset,
  123. uint32_t size,
  124. struct xfs_buf *bp)
  125. {
  126. struct xfs_attr3_rmt_hdr *rmt = bp->b_addr;
  127. if (!xfs_sb_version_hascrc(&mp->m_sb))
  128. return 0;
  129. rmt->rm_magic = cpu_to_be32(XFS_ATTR3_RMT_MAGIC);
  130. rmt->rm_offset = cpu_to_be32(offset);
  131. rmt->rm_bytes = cpu_to_be32(size);
  132. uuid_copy(&rmt->rm_uuid, &mp->m_sb.sb_uuid);
  133. rmt->rm_owner = cpu_to_be64(ino);
  134. rmt->rm_blkno = cpu_to_be64(bp->b_bn);
  135. bp->b_ops = &xfs_attr3_rmt_buf_ops;
  136. return sizeof(struct xfs_attr3_rmt_hdr);
  137. }
  138. /*
  139. * Checking of the remote attribute header is split into two parts. the verifier
  140. * does CRC, location and bounds checking, the unpacking function checks the
  141. * attribute parameters and owner.
  142. */
  143. static bool
  144. xfs_attr3_rmt_hdr_ok(
  145. struct xfs_mount *mp,
  146. xfs_ino_t ino,
  147. uint32_t offset,
  148. uint32_t size,
  149. struct xfs_buf *bp)
  150. {
  151. struct xfs_attr3_rmt_hdr *rmt = bp->b_addr;
  152. if (offset != be32_to_cpu(rmt->rm_offset))
  153. return false;
  154. if (size != be32_to_cpu(rmt->rm_bytes))
  155. return false;
  156. if (ino != be64_to_cpu(rmt->rm_owner))
  157. return false;
  158. /* ok */
  159. return true;
  160. }
  161. /*
  162. * Read the value associated with an attribute from the out-of-line buffer
  163. * that we stored it in.
  164. */
  165. int
  166. xfs_attr_rmtval_get(
  167. struct xfs_da_args *args)
  168. {
  169. struct xfs_bmbt_irec map[ATTR_RMTVALUE_MAPSIZE];
  170. struct xfs_mount *mp = args->dp->i_mount;
  171. struct xfs_buf *bp;
  172. xfs_daddr_t dblkno;
  173. xfs_dablk_t lblkno = args->rmtblkno;
  174. void *dst = args->value;
  175. int valuelen = args->valuelen;
  176. int nmap;
  177. int error;
  178. int blkcnt;
  179. int i;
  180. int offset = 0;
  181. trace_xfs_attr_rmtval_get(args);
  182. ASSERT(!(args->flags & ATTR_KERNOVAL));
  183. while (valuelen > 0) {
  184. nmap = ATTR_RMTVALUE_MAPSIZE;
  185. error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
  186. args->rmtblkcnt, map, &nmap,
  187. XFS_BMAPI_ATTRFORK);
  188. if (error)
  189. return error;
  190. ASSERT(nmap >= 1);
  191. for (i = 0; (i < nmap) && (valuelen > 0); i++) {
  192. int byte_cnt;
  193. char *src;
  194. ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) &&
  195. (map[i].br_startblock != HOLESTARTBLOCK));
  196. dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock);
  197. blkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
  198. error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
  199. dblkno, blkcnt, 0, &bp,
  200. &xfs_attr3_rmt_buf_ops);
  201. if (error)
  202. return error;
  203. byte_cnt = min_t(int, valuelen, BBTOB(bp->b_length));
  204. byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, byte_cnt);
  205. src = bp->b_addr;
  206. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  207. if (!xfs_attr3_rmt_hdr_ok(mp, args->dp->i_ino,
  208. offset, byte_cnt, bp)) {
  209. xfs_alert(mp,
  210. "remote attribute header does not match required off/len/owner (0x%x/Ox%x,0x%llx)",
  211. offset, byte_cnt, args->dp->i_ino);
  212. xfs_buf_relse(bp);
  213. return EFSCORRUPTED;
  214. }
  215. src += sizeof(struct xfs_attr3_rmt_hdr);
  216. }
  217. memcpy(dst, src, byte_cnt);
  218. xfs_buf_relse(bp);
  219. offset += byte_cnt;
  220. dst += byte_cnt;
  221. valuelen -= byte_cnt;
  222. lblkno += map[i].br_blockcount;
  223. }
  224. }
  225. ASSERT(valuelen == 0);
  226. return 0;
  227. }
  228. /*
  229. * Write the value associated with an attribute into the out-of-line buffer
  230. * that we have defined for it.
  231. */
  232. int
  233. xfs_attr_rmtval_set(
  234. struct xfs_da_args *args)
  235. {
  236. struct xfs_inode *dp = args->dp;
  237. struct xfs_mount *mp = dp->i_mount;
  238. struct xfs_bmbt_irec map;
  239. struct xfs_buf *bp;
  240. xfs_daddr_t dblkno;
  241. xfs_dablk_t lblkno;
  242. xfs_fileoff_t lfileoff = 0;
  243. void *src = args->value;
  244. int blkcnt;
  245. int valuelen;
  246. int nmap;
  247. int error;
  248. int hdrcnt = 0;
  249. bool crcs = xfs_sb_version_hascrc(&mp->m_sb);
  250. int offset = 0;
  251. trace_xfs_attr_rmtval_set(args);
  252. /*
  253. * Find a "hole" in the attribute address space large enough for
  254. * us to drop the new attribute's value into. Because CRC enable
  255. * attributes have headers, we can't just do a straight byte to FSB
  256. * conversion. We calculate the worst case block count in this case
  257. * and we may not need that many, so we have to handle this when
  258. * allocating the blocks below.
  259. */
  260. if (!crcs)
  261. blkcnt = XFS_B_TO_FSB(mp, args->valuelen);
  262. else
  263. blkcnt = xfs_attr3_rmt_blocks(mp, args->valuelen);
  264. error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
  265. XFS_ATTR_FORK);
  266. if (error)
  267. return error;
  268. /* Start with the attribute data. We'll allocate the rest afterwards. */
  269. if (crcs)
  270. blkcnt = XFS_B_TO_FSB(mp, args->valuelen);
  271. args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff;
  272. args->rmtblkcnt = blkcnt;
  273. /*
  274. * Roll through the "value", allocating blocks on disk as required.
  275. */
  276. while (blkcnt > 0) {
  277. int committed;
  278. /*
  279. * Allocate a single extent, up to the size of the value.
  280. */
  281. xfs_bmap_init(args->flist, args->firstblock);
  282. nmap = 1;
  283. error = xfs_bmapi_write(args->trans, dp, (xfs_fileoff_t)lblkno,
  284. blkcnt,
  285. XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
  286. args->firstblock, args->total, &map, &nmap,
  287. args->flist);
  288. if (!error) {
  289. error = xfs_bmap_finish(&args->trans, args->flist,
  290. &committed);
  291. }
  292. if (error) {
  293. ASSERT(committed);
  294. args->trans = NULL;
  295. xfs_bmap_cancel(args->flist);
  296. return(error);
  297. }
  298. /*
  299. * bmap_finish() may have committed the last trans and started
  300. * a new one. We need the inode to be in all transactions.
  301. */
  302. if (committed)
  303. xfs_trans_ijoin(args->trans, dp, 0);
  304. ASSERT(nmap == 1);
  305. ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
  306. (map.br_startblock != HOLESTARTBLOCK));
  307. lblkno += map.br_blockcount;
  308. blkcnt -= map.br_blockcount;
  309. hdrcnt++;
  310. /*
  311. * If we have enough blocks for the attribute data, calculate
  312. * how many extra blocks we need for headers. We might run
  313. * through this multiple times in the case that the additional
  314. * headers in the blocks needed for the data fragments spills
  315. * into requiring more blocks. e.g. for 512 byte blocks, we'll
  316. * spill for another block every 9 headers we require in this
  317. * loop.
  318. *
  319. * Note that this can result in contiguous allocation of blocks,
  320. * so we don't use all the space we allocate for headers as we
  321. * have one less header for each contiguous allocation that
  322. * occurs in the map/write loop below.
  323. */
  324. if (crcs && blkcnt == 0) {
  325. int total_len;
  326. total_len = args->valuelen +
  327. hdrcnt * sizeof(struct xfs_attr3_rmt_hdr);
  328. blkcnt = XFS_B_TO_FSB(mp, total_len);
  329. blkcnt -= args->rmtblkcnt;
  330. args->rmtblkcnt += blkcnt;
  331. }
  332. /*
  333. * Start the next trans in the chain.
  334. */
  335. error = xfs_trans_roll(&args->trans, dp);
  336. if (error)
  337. return (error);
  338. }
  339. /*
  340. * Roll through the "value", copying the attribute value to the
  341. * already-allocated blocks. Blocks are written synchronously
  342. * so that we can know they are all on disk before we turn off
  343. * the INCOMPLETE flag.
  344. */
  345. lblkno = args->rmtblkno;
  346. valuelen = args->valuelen;
  347. while (valuelen > 0) {
  348. int byte_cnt;
  349. char *buf;
  350. /*
  351. * Try to remember where we decided to put the value.
  352. */
  353. xfs_bmap_init(args->flist, args->firstblock);
  354. nmap = 1;
  355. error = xfs_bmapi_read(dp, (xfs_fileoff_t)lblkno,
  356. args->rmtblkcnt, &map, &nmap,
  357. XFS_BMAPI_ATTRFORK);
  358. if (error)
  359. return(error);
  360. ASSERT(nmap == 1);
  361. ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
  362. (map.br_startblock != HOLESTARTBLOCK));
  363. dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
  364. blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
  365. bp = xfs_buf_get(mp->m_ddev_targp, dblkno, blkcnt, 0);
  366. if (!bp)
  367. return ENOMEM;
  368. bp->b_ops = &xfs_attr3_rmt_buf_ops;
  369. byte_cnt = BBTOB(bp->b_length);
  370. byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, byte_cnt);
  371. if (valuelen < byte_cnt)
  372. byte_cnt = valuelen;
  373. buf = bp->b_addr;
  374. buf += xfs_attr3_rmt_hdr_set(mp, dp->i_ino, offset,
  375. byte_cnt, bp);
  376. memcpy(buf, src, byte_cnt);
  377. if (byte_cnt < BBTOB(bp->b_length))
  378. xfs_buf_zero(bp, byte_cnt,
  379. BBTOB(bp->b_length) - byte_cnt);
  380. error = xfs_bwrite(bp); /* GROT: NOTE: synchronous write */
  381. xfs_buf_relse(bp);
  382. if (error)
  383. return error;
  384. src += byte_cnt;
  385. valuelen -= byte_cnt;
  386. offset += byte_cnt;
  387. hdrcnt--;
  388. lblkno += map.br_blockcount;
  389. }
  390. ASSERT(valuelen == 0);
  391. return 0;
  392. }
  393. /*
  394. * Remove the value associated with an attribute by deleting the
  395. * out-of-line buffer that it is stored on.
  396. */
  397. int
  398. xfs_attr_rmtval_remove(xfs_da_args_t *args)
  399. {
  400. xfs_mount_t *mp;
  401. xfs_bmbt_irec_t map;
  402. xfs_buf_t *bp;
  403. xfs_daddr_t dblkno;
  404. xfs_dablk_t lblkno;
  405. int valuelen, blkcnt, nmap, error, done, committed;
  406. trace_xfs_attr_rmtval_remove(args);
  407. mp = args->dp->i_mount;
  408. /*
  409. * Roll through the "value", invalidating the attribute value's
  410. * blocks.
  411. */
  412. lblkno = args->rmtblkno;
  413. valuelen = args->rmtblkcnt;
  414. while (valuelen > 0) {
  415. /*
  416. * Try to remember where we decided to put the value.
  417. */
  418. nmap = 1;
  419. error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
  420. args->rmtblkcnt, &map, &nmap,
  421. XFS_BMAPI_ATTRFORK);
  422. if (error)
  423. return(error);
  424. ASSERT(nmap == 1);
  425. ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
  426. (map.br_startblock != HOLESTARTBLOCK));
  427. dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
  428. blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
  429. /*
  430. * If the "remote" value is in the cache, remove it.
  431. */
  432. bp = xfs_incore(mp->m_ddev_targp, dblkno, blkcnt, XBF_TRYLOCK);
  433. if (bp) {
  434. xfs_buf_stale(bp);
  435. xfs_buf_relse(bp);
  436. bp = NULL;
  437. }
  438. valuelen -= map.br_blockcount;
  439. lblkno += map.br_blockcount;
  440. }
  441. /*
  442. * Keep de-allocating extents until the remote-value region is gone.
  443. */
  444. lblkno = args->rmtblkno;
  445. blkcnt = args->rmtblkcnt;
  446. done = 0;
  447. while (!done) {
  448. xfs_bmap_init(args->flist, args->firstblock);
  449. error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
  450. XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
  451. 1, args->firstblock, args->flist,
  452. &done);
  453. if (!error) {
  454. error = xfs_bmap_finish(&args->trans, args->flist,
  455. &committed);
  456. }
  457. if (error) {
  458. ASSERT(committed);
  459. args->trans = NULL;
  460. xfs_bmap_cancel(args->flist);
  461. return error;
  462. }
  463. /*
  464. * bmap_finish() may have committed the last trans and started
  465. * a new one. We need the inode to be in all transactions.
  466. */
  467. if (committed)
  468. xfs_trans_ijoin(args->trans, args->dp, 0);
  469. /*
  470. * Close out trans and start the next one in the chain.
  471. */
  472. error = xfs_trans_roll(&args->trans, args->dp);
  473. if (error)
  474. return (error);
  475. }
  476. return(0);
  477. }