xfs_attr_remote.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  54. int buflen = XFS_ATTR3_RMT_BUF_SPACE(mp, mp->m_sb.sb_blocksize);
  55. return (attrlen + buflen - 1) / buflen;
  56. }
  57. return XFS_B_TO_FSB(mp, attrlen);
  58. }
  59. static bool
  60. xfs_attr3_rmt_verify(
  61. struct xfs_buf *bp)
  62. {
  63. struct xfs_mount *mp = bp->b_target->bt_mount;
  64. struct xfs_attr3_rmt_hdr *rmt = bp->b_addr;
  65. if (!xfs_sb_version_hascrc(&mp->m_sb))
  66. return false;
  67. if (rmt->rm_magic != cpu_to_be32(XFS_ATTR3_RMT_MAGIC))
  68. return false;
  69. if (!uuid_equal(&rmt->rm_uuid, &mp->m_sb.sb_uuid))
  70. return false;
  71. if (bp->b_bn != be64_to_cpu(rmt->rm_blkno))
  72. return false;
  73. if (be32_to_cpu(rmt->rm_offset) +
  74. be32_to_cpu(rmt->rm_bytes) >= XATTR_SIZE_MAX)
  75. return false;
  76. if (rmt->rm_owner == 0)
  77. return false;
  78. return true;
  79. }
  80. static void
  81. xfs_attr3_rmt_read_verify(
  82. struct xfs_buf *bp)
  83. {
  84. struct xfs_mount *mp = bp->b_target->bt_mount;
  85. /* no verification of non-crc buffers */
  86. if (!xfs_sb_version_hascrc(&mp->m_sb))
  87. return;
  88. if (!xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
  89. XFS_ATTR3_RMT_CRC_OFF) ||
  90. !xfs_attr3_rmt_verify(bp)) {
  91. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
  92. xfs_buf_ioerror(bp, EFSCORRUPTED);
  93. }
  94. }
  95. static void
  96. xfs_attr3_rmt_write_verify(
  97. struct xfs_buf *bp)
  98. {
  99. struct xfs_mount *mp = bp->b_target->bt_mount;
  100. struct xfs_buf_log_item *bip = bp->b_fspriv;
  101. /* no verification of non-crc buffers */
  102. if (!xfs_sb_version_hascrc(&mp->m_sb))
  103. return;
  104. if (!xfs_attr3_rmt_verify(bp)) {
  105. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
  106. xfs_buf_ioerror(bp, EFSCORRUPTED);
  107. return;
  108. }
  109. if (bip) {
  110. struct xfs_attr3_rmt_hdr *rmt = bp->b_addr;
  111. rmt->rm_lsn = cpu_to_be64(bip->bli_item.li_lsn);
  112. }
  113. xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length),
  114. XFS_ATTR3_RMT_CRC_OFF);
  115. }
  116. const struct xfs_buf_ops xfs_attr3_rmt_buf_ops = {
  117. .verify_read = xfs_attr3_rmt_read_verify,
  118. .verify_write = xfs_attr3_rmt_write_verify,
  119. };
  120. static int
  121. xfs_attr3_rmt_hdr_set(
  122. struct xfs_mount *mp,
  123. xfs_ino_t ino,
  124. uint32_t offset,
  125. uint32_t size,
  126. struct xfs_buf *bp)
  127. {
  128. struct xfs_attr3_rmt_hdr *rmt = bp->b_addr;
  129. if (!xfs_sb_version_hascrc(&mp->m_sb))
  130. return 0;
  131. rmt->rm_magic = cpu_to_be32(XFS_ATTR3_RMT_MAGIC);
  132. rmt->rm_offset = cpu_to_be32(offset);
  133. rmt->rm_bytes = cpu_to_be32(size);
  134. uuid_copy(&rmt->rm_uuid, &mp->m_sb.sb_uuid);
  135. rmt->rm_owner = cpu_to_be64(ino);
  136. rmt->rm_blkno = cpu_to_be64(bp->b_bn);
  137. bp->b_ops = &xfs_attr3_rmt_buf_ops;
  138. return sizeof(struct xfs_attr3_rmt_hdr);
  139. }
  140. /*
  141. * Checking of the remote attribute header is split into two parts. the verifier
  142. * does CRC, location and bounds checking, the unpacking function checks the
  143. * attribute parameters and owner.
  144. */
  145. static bool
  146. xfs_attr3_rmt_hdr_ok(
  147. struct xfs_mount *mp,
  148. xfs_ino_t ino,
  149. uint32_t offset,
  150. uint32_t size,
  151. struct xfs_buf *bp)
  152. {
  153. struct xfs_attr3_rmt_hdr *rmt = bp->b_addr;
  154. if (offset != be32_to_cpu(rmt->rm_offset))
  155. return false;
  156. if (size != be32_to_cpu(rmt->rm_bytes))
  157. return false;
  158. if (ino != be64_to_cpu(rmt->rm_owner))
  159. return false;
  160. /* ok */
  161. return true;
  162. }
  163. /*
  164. * Read the value associated with an attribute from the out-of-line buffer
  165. * that we stored it in.
  166. */
  167. int
  168. xfs_attr_rmtval_get(
  169. struct xfs_da_args *args)
  170. {
  171. struct xfs_bmbt_irec map[ATTR_RMTVALUE_MAPSIZE];
  172. struct xfs_mount *mp = args->dp->i_mount;
  173. struct xfs_buf *bp;
  174. xfs_daddr_t dblkno;
  175. xfs_dablk_t lblkno = args->rmtblkno;
  176. void *dst = args->value;
  177. int valuelen = args->valuelen;
  178. int nmap;
  179. int error;
  180. int blkcnt;
  181. int i;
  182. int offset = 0;
  183. trace_xfs_attr_rmtval_get(args);
  184. ASSERT(!(args->flags & ATTR_KERNOVAL));
  185. while (valuelen > 0) {
  186. nmap = ATTR_RMTVALUE_MAPSIZE;
  187. blkcnt = xfs_attr3_rmt_blocks(mp, valuelen);
  188. error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
  189. blkcnt, map, &nmap,
  190. XFS_BMAPI_ATTRFORK);
  191. if (error)
  192. return error;
  193. ASSERT(nmap >= 1);
  194. for (i = 0; (i < nmap) && (valuelen > 0); i++) {
  195. int byte_cnt;
  196. char *src;
  197. ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) &&
  198. (map[i].br_startblock != HOLESTARTBLOCK));
  199. dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock);
  200. blkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
  201. error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
  202. dblkno, blkcnt, 0, &bp,
  203. &xfs_attr3_rmt_buf_ops);
  204. if (error)
  205. return error;
  206. byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, BBTOB(bp->b_length));
  207. byte_cnt = min_t(int, valuelen, byte_cnt);
  208. src = bp->b_addr;
  209. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  210. if (!xfs_attr3_rmt_hdr_ok(mp, args->dp->i_ino,
  211. offset, byte_cnt, bp)) {
  212. xfs_alert(mp,
  213. "remote attribute header does not match required off/len/owner (0x%x/Ox%x,0x%llx)",
  214. offset, byte_cnt, args->dp->i_ino);
  215. xfs_buf_relse(bp);
  216. return EFSCORRUPTED;
  217. }
  218. src += sizeof(struct xfs_attr3_rmt_hdr);
  219. }
  220. memcpy(dst, src, byte_cnt);
  221. xfs_buf_relse(bp);
  222. offset += byte_cnt;
  223. dst += byte_cnt;
  224. valuelen -= byte_cnt;
  225. lblkno += map[i].br_blockcount;
  226. }
  227. }
  228. ASSERT(valuelen == 0);
  229. return 0;
  230. }
  231. /*
  232. * Write the value associated with an attribute into the out-of-line buffer
  233. * that we have defined for it.
  234. */
  235. int
  236. xfs_attr_rmtval_set(
  237. struct xfs_da_args *args)
  238. {
  239. struct xfs_inode *dp = args->dp;
  240. struct xfs_mount *mp = dp->i_mount;
  241. struct xfs_bmbt_irec map;
  242. struct xfs_buf *bp;
  243. xfs_daddr_t dblkno;
  244. xfs_dablk_t lblkno;
  245. xfs_fileoff_t lfileoff = 0;
  246. void *src = args->value;
  247. int blkcnt;
  248. int valuelen;
  249. int nmap;
  250. int error;
  251. int hdrcnt = 0;
  252. bool crcs = xfs_sb_version_hascrc(&mp->m_sb);
  253. int offset = 0;
  254. trace_xfs_attr_rmtval_set(args);
  255. /*
  256. * Find a "hole" in the attribute address space large enough for
  257. * us to drop the new attribute's value into. Because CRC enable
  258. * attributes have headers, we can't just do a straight byte to FSB
  259. * conversion. We calculate the worst case block count in this case
  260. * and we may not need that many, so we have to handle this when
  261. * allocating the blocks below.
  262. */
  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. blkcnt = args->rmtblkcnt;
  348. while (valuelen > 0) {
  349. int byte_cnt;
  350. int hdr_size;
  351. int dblkcnt;
  352. char *buf;
  353. /*
  354. * Try to remember where we decided to put the value.
  355. */
  356. xfs_bmap_init(args->flist, args->firstblock);
  357. nmap = 1;
  358. error = xfs_bmapi_read(dp, (xfs_fileoff_t)lblkno,
  359. blkcnt, &map, &nmap,
  360. XFS_BMAPI_ATTRFORK);
  361. if (error)
  362. return(error);
  363. ASSERT(nmap == 1);
  364. ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
  365. (map.br_startblock != HOLESTARTBLOCK));
  366. dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
  367. dblkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
  368. bp = xfs_buf_get(mp->m_ddev_targp, dblkno, dblkcnt, 0);
  369. if (!bp)
  370. return ENOMEM;
  371. bp->b_ops = &xfs_attr3_rmt_buf_ops;
  372. buf = bp->b_addr;
  373. byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, BBTOB(bp->b_length));
  374. byte_cnt = min_t(int, valuelen, byte_cnt);
  375. hdr_size = xfs_attr3_rmt_hdr_set(mp, dp->i_ino, offset,
  376. byte_cnt, bp);
  377. ASSERT(hdr_size + byte_cnt <= BBTOB(bp->b_length));
  378. memcpy(buf + hdr_size, src, byte_cnt);
  379. if (byte_cnt + hdr_size < BBTOB(bp->b_length))
  380. xfs_buf_zero(bp, byte_cnt + hdr_size,
  381. BBTOB(bp->b_length) - byte_cnt - hdr_size);
  382. error = xfs_bwrite(bp); /* GROT: NOTE: synchronous write */
  383. xfs_buf_relse(bp);
  384. if (error)
  385. return error;
  386. src += byte_cnt;
  387. valuelen -= byte_cnt;
  388. offset += byte_cnt;
  389. lblkno += map.br_blockcount;
  390. blkcnt -= map.br_blockcount;
  391. }
  392. ASSERT(valuelen == 0);
  393. return 0;
  394. }
  395. /*
  396. * Remove the value associated with an attribute by deleting the
  397. * out-of-line buffer that it is stored on.
  398. */
  399. int
  400. xfs_attr_rmtval_remove(xfs_da_args_t *args)
  401. {
  402. xfs_mount_t *mp;
  403. xfs_bmbt_irec_t map;
  404. xfs_buf_t *bp;
  405. xfs_daddr_t dblkno;
  406. xfs_dablk_t lblkno;
  407. int valuelen, blkcnt, nmap, error, done, committed;
  408. trace_xfs_attr_rmtval_remove(args);
  409. mp = args->dp->i_mount;
  410. /*
  411. * Roll through the "value", invalidating the attribute value's
  412. * blocks.
  413. */
  414. lblkno = args->rmtblkno;
  415. valuelen = args->rmtblkcnt;
  416. while (valuelen > 0) {
  417. /*
  418. * Try to remember where we decided to put the value.
  419. */
  420. nmap = 1;
  421. error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
  422. args->rmtblkcnt, &map, &nmap,
  423. XFS_BMAPI_ATTRFORK);
  424. if (error)
  425. return(error);
  426. ASSERT(nmap == 1);
  427. ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
  428. (map.br_startblock != HOLESTARTBLOCK));
  429. dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
  430. blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
  431. /*
  432. * If the "remote" value is in the cache, remove it.
  433. */
  434. bp = xfs_incore(mp->m_ddev_targp, dblkno, blkcnt, XBF_TRYLOCK);
  435. if (bp) {
  436. xfs_buf_stale(bp);
  437. xfs_buf_relse(bp);
  438. bp = NULL;
  439. }
  440. valuelen -= map.br_blockcount;
  441. lblkno += map.br_blockcount;
  442. }
  443. /*
  444. * Keep de-allocating extents until the remote-value region is gone.
  445. */
  446. lblkno = args->rmtblkno;
  447. blkcnt = args->rmtblkcnt;
  448. done = 0;
  449. while (!done) {
  450. xfs_bmap_init(args->flist, args->firstblock);
  451. error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
  452. XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
  453. 1, args->firstblock, args->flist,
  454. &done);
  455. if (!error) {
  456. error = xfs_bmap_finish(&args->trans, args->flist,
  457. &committed);
  458. }
  459. if (error) {
  460. ASSERT(committed);
  461. args->trans = NULL;
  462. xfs_bmap_cancel(args->flist);
  463. return error;
  464. }
  465. /*
  466. * bmap_finish() may have committed the last trans and started
  467. * a new one. We need the inode to be in all transactions.
  468. */
  469. if (committed)
  470. xfs_trans_ijoin(args->trans, args->dp, 0);
  471. /*
  472. * Close out trans and start the next one in the chain.
  473. */
  474. error = xfs_trans_roll(&args->trans, args->dp);
  475. if (error)
  476. return (error);
  477. }
  478. return(0);
  479. }