xfs_attr_remote.c 15 KB

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