xfs_attr_remote.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. if (!crcs)
  264. blkcnt = XFS_B_TO_FSB(mp, args->valuelen);
  265. else
  266. blkcnt = xfs_attr3_rmt_blocks(mp, args->valuelen);
  267. error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
  268. XFS_ATTR_FORK);
  269. if (error)
  270. return error;
  271. /* Start with the attribute data. We'll allocate the rest afterwards. */
  272. if (crcs)
  273. blkcnt = XFS_B_TO_FSB(mp, args->valuelen);
  274. args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff;
  275. args->rmtblkcnt = blkcnt;
  276. /*
  277. * Roll through the "value", allocating blocks on disk as required.
  278. */
  279. while (blkcnt > 0) {
  280. int committed;
  281. /*
  282. * Allocate a single extent, up to the size of the value.
  283. */
  284. xfs_bmap_init(args->flist, args->firstblock);
  285. nmap = 1;
  286. error = xfs_bmapi_write(args->trans, dp, (xfs_fileoff_t)lblkno,
  287. blkcnt,
  288. XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
  289. args->firstblock, args->total, &map, &nmap,
  290. args->flist);
  291. if (!error) {
  292. error = xfs_bmap_finish(&args->trans, args->flist,
  293. &committed);
  294. }
  295. if (error) {
  296. ASSERT(committed);
  297. args->trans = NULL;
  298. xfs_bmap_cancel(args->flist);
  299. return(error);
  300. }
  301. /*
  302. * bmap_finish() may have committed the last trans and started
  303. * a new one. We need the inode to be in all transactions.
  304. */
  305. if (committed)
  306. xfs_trans_ijoin(args->trans, dp, 0);
  307. ASSERT(nmap == 1);
  308. ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
  309. (map.br_startblock != HOLESTARTBLOCK));
  310. lblkno += map.br_blockcount;
  311. blkcnt -= map.br_blockcount;
  312. hdrcnt++;
  313. /*
  314. * If we have enough blocks for the attribute data, calculate
  315. * how many extra blocks we need for headers. We might run
  316. * through this multiple times in the case that the additional
  317. * headers in the blocks needed for the data fragments spills
  318. * into requiring more blocks. e.g. for 512 byte blocks, we'll
  319. * spill for another block every 9 headers we require in this
  320. * loop.
  321. *
  322. * Note that this can result in contiguous allocation of blocks,
  323. * so we don't use all the space we allocate for headers as we
  324. * have one less header for each contiguous allocation that
  325. * occurs in the map/write loop below.
  326. */
  327. if (crcs && blkcnt == 0) {
  328. int total_len;
  329. total_len = args->valuelen +
  330. hdrcnt * sizeof(struct xfs_attr3_rmt_hdr);
  331. blkcnt = XFS_B_TO_FSB(mp, total_len);
  332. blkcnt -= args->rmtblkcnt;
  333. args->rmtblkcnt += blkcnt;
  334. }
  335. /*
  336. * Start the next trans in the chain.
  337. */
  338. error = xfs_trans_roll(&args->trans, dp);
  339. if (error)
  340. return (error);
  341. }
  342. /*
  343. * Roll through the "value", copying the attribute value to the
  344. * already-allocated blocks. Blocks are written synchronously
  345. * so that we can know they are all on disk before we turn off
  346. * the INCOMPLETE flag.
  347. */
  348. lblkno = args->rmtblkno;
  349. valuelen = args->valuelen;
  350. while (valuelen > 0) {
  351. int byte_cnt;
  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. args->rmtblkcnt, &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. blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
  368. bp = xfs_buf_get(mp->m_ddev_targp, dblkno, blkcnt, 0);
  369. if (!bp)
  370. return ENOMEM;
  371. bp->b_ops = &xfs_attr3_rmt_buf_ops;
  372. byte_cnt = BBTOB(bp->b_length);
  373. byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, byte_cnt);
  374. if (valuelen < byte_cnt)
  375. byte_cnt = valuelen;
  376. buf = bp->b_addr;
  377. buf += xfs_attr3_rmt_hdr_set(mp, dp->i_ino, offset,
  378. byte_cnt, bp);
  379. memcpy(buf, src, byte_cnt);
  380. if (byte_cnt < BBTOB(bp->b_length))
  381. xfs_buf_zero(bp, byte_cnt,
  382. BBTOB(bp->b_length) - byte_cnt);
  383. error = xfs_bwrite(bp); /* GROT: NOTE: synchronous write */
  384. xfs_buf_relse(bp);
  385. if (error)
  386. return error;
  387. src += byte_cnt;
  388. valuelen -= byte_cnt;
  389. offset += byte_cnt;
  390. hdrcnt--;
  391. lblkno += map.br_blockcount;
  392. }
  393. ASSERT(valuelen == 0);
  394. return 0;
  395. }
  396. /*
  397. * Remove the value associated with an attribute by deleting the
  398. * out-of-line buffer that it is stored on.
  399. */
  400. int
  401. xfs_attr_rmtval_remove(xfs_da_args_t *args)
  402. {
  403. xfs_mount_t *mp;
  404. xfs_bmbt_irec_t map;
  405. xfs_buf_t *bp;
  406. xfs_daddr_t dblkno;
  407. xfs_dablk_t lblkno;
  408. int valuelen, blkcnt, nmap, error, done, committed;
  409. trace_xfs_attr_rmtval_remove(args);
  410. mp = args->dp->i_mount;
  411. /*
  412. * Roll through the "value", invalidating the attribute value's
  413. * blocks.
  414. */
  415. lblkno = args->rmtblkno;
  416. valuelen = args->rmtblkcnt;
  417. while (valuelen > 0) {
  418. /*
  419. * Try to remember where we decided to put the value.
  420. */
  421. nmap = 1;
  422. error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
  423. args->rmtblkcnt, &map, &nmap,
  424. XFS_BMAPI_ATTRFORK);
  425. if (error)
  426. return(error);
  427. ASSERT(nmap == 1);
  428. ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
  429. (map.br_startblock != HOLESTARTBLOCK));
  430. dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
  431. blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
  432. /*
  433. * If the "remote" value is in the cache, remove it.
  434. */
  435. bp = xfs_incore(mp->m_ddev_targp, dblkno, blkcnt, XBF_TRYLOCK);
  436. if (bp) {
  437. xfs_buf_stale(bp);
  438. xfs_buf_relse(bp);
  439. bp = NULL;
  440. }
  441. valuelen -= map.br_blockcount;
  442. lblkno += map.br_blockcount;
  443. }
  444. /*
  445. * Keep de-allocating extents until the remote-value region is gone.
  446. */
  447. lblkno = args->rmtblkno;
  448. blkcnt = args->rmtblkcnt;
  449. done = 0;
  450. while (!done) {
  451. xfs_bmap_init(args->flist, args->firstblock);
  452. error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
  453. XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
  454. 1, args->firstblock, args->flist,
  455. &done);
  456. if (!error) {
  457. error = xfs_bmap_finish(&args->trans, args->flist,
  458. &committed);
  459. }
  460. if (error) {
  461. ASSERT(committed);
  462. args->trans = NULL;
  463. xfs_bmap_cancel(args->flist);
  464. return error;
  465. }
  466. /*
  467. * bmap_finish() may have committed the last trans and started
  468. * a new one. We need the inode to be in all transactions.
  469. */
  470. if (committed)
  471. xfs_trans_ijoin(args->trans, args->dp, 0);
  472. /*
  473. * Close out trans and start the next one in the chain.
  474. */
  475. error = xfs_trans_roll(&args->trans, args->dp);
  476. if (error)
  477. return (error);
  478. }
  479. return(0);
  480. }