xfs_attr_remote.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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. if (crcs && blkcnt == 0) {
  320. int total_len;
  321. total_len = args->valuelen +
  322. hdrcnt * sizeof(struct xfs_attr3_rmt_hdr);
  323. blkcnt = XFS_B_TO_FSB(mp, total_len);
  324. blkcnt -= args->rmtblkcnt;
  325. args->rmtblkcnt += blkcnt;
  326. }
  327. /*
  328. * Start the next trans in the chain.
  329. */
  330. error = xfs_trans_roll(&args->trans, dp);
  331. if (error)
  332. return (error);
  333. }
  334. /*
  335. * Roll through the "value", copying the attribute value to the
  336. * already-allocated blocks. Blocks are written synchronously
  337. * so that we can know they are all on disk before we turn off
  338. * the INCOMPLETE flag.
  339. */
  340. lblkno = args->rmtblkno;
  341. valuelen = args->valuelen;
  342. while (valuelen > 0) {
  343. int byte_cnt;
  344. char *buf;
  345. /*
  346. * Try to remember where we decided to put the value.
  347. */
  348. xfs_bmap_init(args->flist, args->firstblock);
  349. nmap = 1;
  350. error = xfs_bmapi_read(dp, (xfs_fileoff_t)lblkno,
  351. args->rmtblkcnt, &map, &nmap,
  352. XFS_BMAPI_ATTRFORK);
  353. if (error)
  354. return(error);
  355. ASSERT(nmap == 1);
  356. ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
  357. (map.br_startblock != HOLESTARTBLOCK));
  358. dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
  359. blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
  360. bp = xfs_buf_get(mp->m_ddev_targp, dblkno, blkcnt, 0);
  361. if (!bp)
  362. return ENOMEM;
  363. bp->b_ops = &xfs_attr3_rmt_buf_ops;
  364. byte_cnt = BBTOB(bp->b_length);
  365. byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, byte_cnt);
  366. if (valuelen < byte_cnt)
  367. byte_cnt = valuelen;
  368. buf = bp->b_addr;
  369. buf += xfs_attr3_rmt_hdr_set(mp, dp->i_ino, offset,
  370. byte_cnt, bp);
  371. memcpy(buf, src, byte_cnt);
  372. if (byte_cnt < BBTOB(bp->b_length))
  373. xfs_buf_zero(bp, byte_cnt,
  374. BBTOB(bp->b_length) - byte_cnt);
  375. error = xfs_bwrite(bp); /* GROT: NOTE: synchronous write */
  376. xfs_buf_relse(bp);
  377. if (error)
  378. return error;
  379. src += byte_cnt;
  380. valuelen -= byte_cnt;
  381. offset += byte_cnt;
  382. hdrcnt--;
  383. lblkno += map.br_blockcount;
  384. }
  385. ASSERT(valuelen == 0);
  386. ASSERT(hdrcnt == 0);
  387. return 0;
  388. }
  389. /*
  390. * Remove the value associated with an attribute by deleting the
  391. * out-of-line buffer that it is stored on.
  392. */
  393. int
  394. xfs_attr_rmtval_remove(xfs_da_args_t *args)
  395. {
  396. xfs_mount_t *mp;
  397. xfs_bmbt_irec_t map;
  398. xfs_buf_t *bp;
  399. xfs_daddr_t dblkno;
  400. xfs_dablk_t lblkno;
  401. int valuelen, blkcnt, nmap, error, done, committed;
  402. trace_xfs_attr_rmtval_remove(args);
  403. mp = args->dp->i_mount;
  404. /*
  405. * Roll through the "value", invalidating the attribute value's
  406. * blocks.
  407. */
  408. lblkno = args->rmtblkno;
  409. valuelen = args->rmtblkcnt;
  410. while (valuelen > 0) {
  411. /*
  412. * Try to remember where we decided to put the value.
  413. */
  414. nmap = 1;
  415. error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
  416. args->rmtblkcnt, &map, &nmap,
  417. XFS_BMAPI_ATTRFORK);
  418. if (error)
  419. return(error);
  420. ASSERT(nmap == 1);
  421. ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
  422. (map.br_startblock != HOLESTARTBLOCK));
  423. dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
  424. blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
  425. /*
  426. * If the "remote" value is in the cache, remove it.
  427. */
  428. bp = xfs_incore(mp->m_ddev_targp, dblkno, blkcnt, XBF_TRYLOCK);
  429. if (bp) {
  430. xfs_buf_stale(bp);
  431. xfs_buf_relse(bp);
  432. bp = NULL;
  433. }
  434. valuelen -= map.br_blockcount;
  435. lblkno += map.br_blockcount;
  436. }
  437. /*
  438. * Keep de-allocating extents until the remote-value region is gone.
  439. */
  440. lblkno = args->rmtblkno;
  441. blkcnt = args->rmtblkcnt;
  442. done = 0;
  443. while (!done) {
  444. xfs_bmap_init(args->flist, args->firstblock);
  445. error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
  446. XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
  447. 1, args->firstblock, args->flist,
  448. &done);
  449. if (!error) {
  450. error = xfs_bmap_finish(&args->trans, args->flist,
  451. &committed);
  452. }
  453. if (error) {
  454. ASSERT(committed);
  455. args->trans = NULL;
  456. xfs_bmap_cancel(args->flist);
  457. return error;
  458. }
  459. /*
  460. * bmap_finish() may have committed the last trans and started
  461. * a new one. We need the inode to be in all transactions.
  462. */
  463. if (committed)
  464. xfs_trans_ijoin(args->trans, args->dp, 0);
  465. /*
  466. * Close out trans and start the next one in the chain.
  467. */
  468. error = xfs_trans_roll(&args->trans, args->dp);
  469. if (error)
  470. return (error);
  471. }
  472. return(0);
  473. }