xfs_attr_remote.c 15 KB

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