xfs_attr_remote.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  4. * Copyright (c) 2013 Red Hat, Inc.
  5. * All Rights Reserved.
  6. */
  7. #include "xfs.h"
  8. #include "xfs_fs.h"
  9. #include "xfs_shared.h"
  10. #include "xfs_format.h"
  11. #include "xfs_log_format.h"
  12. #include "xfs_trans_resv.h"
  13. #include "xfs_bit.h"
  14. #include "xfs_mount.h"
  15. #include "xfs_defer.h"
  16. #include "xfs_da_format.h"
  17. #include "xfs_da_btree.h"
  18. #include "xfs_inode.h"
  19. #include "xfs_trans.h"
  20. #include "xfs_bmap.h"
  21. #include "xfs_attr.h"
  22. #include "xfs_attr_remote.h"
  23. #include "xfs_trace.h"
  24. #include "xfs_error.h"
  25. #define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
  26. /*
  27. * Remote Attribute Values
  28. * =======================
  29. *
  30. * Remote extended attribute values are conceptually simple -- they're written
  31. * to data blocks mapped by an inode's attribute fork, and they have an upper
  32. * size limit of 64k. Setting a value does not involve the XFS log.
  33. *
  34. * However, on a v5 filesystem, maximally sized remote attr values require one
  35. * block more than 64k worth of space to hold both the remote attribute value
  36. * header (64 bytes). On a 4k block filesystem this results in a 68k buffer;
  37. * on a 64k block filesystem, this would be a 128k buffer. Note that the log
  38. * format can only handle a dirty buffer of XFS_MAX_BLOCKSIZE length (64k).
  39. * Therefore, we /must/ ensure that remote attribute value buffers never touch
  40. * the logging system and therefore never have a log item.
  41. */
  42. /*
  43. * Each contiguous block has a header, so it is not just a simple attribute
  44. * length to FSB conversion.
  45. */
  46. int
  47. xfs_attr3_rmt_blocks(
  48. struct xfs_mount *mp,
  49. int attrlen)
  50. {
  51. if (xfs_has_crc(mp)) {
  52. int buflen = XFS_ATTR3_RMT_BUF_SPACE(mp, mp->m_sb.sb_blocksize);
  53. return (attrlen + buflen - 1) / buflen;
  54. }
  55. return XFS_B_TO_FSB(mp, attrlen);
  56. }
  57. /*
  58. * Checking of the remote attribute header is split into two parts. The verifier
  59. * does CRC, location and bounds checking, the unpacking function checks the
  60. * attribute parameters and owner.
  61. */
  62. static xfs_failaddr_t
  63. xfs_attr3_rmt_hdr_ok(
  64. void *ptr,
  65. xfs_ino_t ino,
  66. uint32_t offset,
  67. uint32_t size,
  68. xfs_daddr_t bno)
  69. {
  70. struct xfs_attr3_rmt_hdr *rmt = ptr;
  71. if (bno != be64_to_cpu(rmt->rm_blkno))
  72. return __this_address;
  73. if (offset != be32_to_cpu(rmt->rm_offset))
  74. return __this_address;
  75. if (size != be32_to_cpu(rmt->rm_bytes))
  76. return __this_address;
  77. if (ino != be64_to_cpu(rmt->rm_owner))
  78. return __this_address;
  79. /* ok */
  80. return NULL;
  81. }
  82. static xfs_failaddr_t
  83. xfs_attr3_rmt_verify(
  84. struct xfs_mount *mp,
  85. struct xfs_buf *bp,
  86. void *ptr,
  87. int fsbsize,
  88. xfs_daddr_t bno)
  89. {
  90. struct xfs_attr3_rmt_hdr *rmt = ptr;
  91. if (!xfs_verify_magic(bp, rmt->rm_magic))
  92. return __this_address;
  93. if (!uuid_equal(&rmt->rm_uuid, &mp->m_sb.sb_meta_uuid))
  94. return __this_address;
  95. if (be64_to_cpu(rmt->rm_blkno) != bno)
  96. return __this_address;
  97. if (be32_to_cpu(rmt->rm_bytes) > fsbsize - sizeof(*rmt))
  98. return __this_address;
  99. if (be32_to_cpu(rmt->rm_offset) +
  100. be32_to_cpu(rmt->rm_bytes) > XFS_XATTR_SIZE_MAX)
  101. return __this_address;
  102. if (rmt->rm_owner == 0)
  103. return __this_address;
  104. return NULL;
  105. }
  106. static int
  107. __xfs_attr3_rmt_read_verify(
  108. struct xfs_buf *bp,
  109. bool check_crc,
  110. xfs_failaddr_t *failaddr)
  111. {
  112. struct xfs_mount *mp = bp->b_mount;
  113. char *ptr;
  114. int len;
  115. xfs_daddr_t bno;
  116. int blksize = mp->m_attr_geo->blksize;
  117. /* no verification of non-crc buffers */
  118. if (!xfs_has_crc(mp))
  119. return 0;
  120. ptr = bp->b_addr;
  121. bno = xfs_buf_daddr(bp);
  122. len = BBTOB(bp->b_length);
  123. ASSERT(len >= blksize);
  124. while (len > 0) {
  125. if (check_crc &&
  126. !xfs_verify_cksum(ptr, blksize, XFS_ATTR3_RMT_CRC_OFF)) {
  127. *failaddr = __this_address;
  128. return -EFSBADCRC;
  129. }
  130. *failaddr = xfs_attr3_rmt_verify(mp, bp, ptr, blksize, bno);
  131. if (*failaddr)
  132. return -EFSCORRUPTED;
  133. len -= blksize;
  134. ptr += blksize;
  135. bno += BTOBB(blksize);
  136. }
  137. if (len != 0) {
  138. *failaddr = __this_address;
  139. return -EFSCORRUPTED;
  140. }
  141. return 0;
  142. }
  143. static void
  144. xfs_attr3_rmt_read_verify(
  145. struct xfs_buf *bp)
  146. {
  147. xfs_failaddr_t fa;
  148. int error;
  149. error = __xfs_attr3_rmt_read_verify(bp, true, &fa);
  150. if (error)
  151. xfs_verifier_error(bp, error, fa);
  152. }
  153. static xfs_failaddr_t
  154. xfs_attr3_rmt_verify_struct(
  155. struct xfs_buf *bp)
  156. {
  157. xfs_failaddr_t fa;
  158. int error;
  159. error = __xfs_attr3_rmt_read_verify(bp, false, &fa);
  160. return error ? fa : NULL;
  161. }
  162. static void
  163. xfs_attr3_rmt_write_verify(
  164. struct xfs_buf *bp)
  165. {
  166. struct xfs_mount *mp = bp->b_mount;
  167. xfs_failaddr_t fa;
  168. int blksize = mp->m_attr_geo->blksize;
  169. char *ptr;
  170. int len;
  171. xfs_daddr_t bno;
  172. /* no verification of non-crc buffers */
  173. if (!xfs_has_crc(mp))
  174. return;
  175. ptr = bp->b_addr;
  176. bno = xfs_buf_daddr(bp);
  177. len = BBTOB(bp->b_length);
  178. ASSERT(len >= blksize);
  179. while (len > 0) {
  180. struct xfs_attr3_rmt_hdr *rmt = (struct xfs_attr3_rmt_hdr *)ptr;
  181. fa = xfs_attr3_rmt_verify(mp, bp, ptr, blksize, bno);
  182. if (fa) {
  183. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  184. return;
  185. }
  186. /*
  187. * Ensure we aren't writing bogus LSNs to disk. See
  188. * xfs_attr3_rmt_hdr_set() for the explanation.
  189. */
  190. if (rmt->rm_lsn != cpu_to_be64(NULLCOMMITLSN)) {
  191. xfs_verifier_error(bp, -EFSCORRUPTED, __this_address);
  192. return;
  193. }
  194. xfs_update_cksum(ptr, blksize, XFS_ATTR3_RMT_CRC_OFF);
  195. len -= blksize;
  196. ptr += blksize;
  197. bno += BTOBB(blksize);
  198. }
  199. if (len != 0)
  200. xfs_verifier_error(bp, -EFSCORRUPTED, __this_address);
  201. }
  202. const struct xfs_buf_ops xfs_attr3_rmt_buf_ops = {
  203. .name = "xfs_attr3_rmt",
  204. .magic = { 0, cpu_to_be32(XFS_ATTR3_RMT_MAGIC) },
  205. .verify_read = xfs_attr3_rmt_read_verify,
  206. .verify_write = xfs_attr3_rmt_write_verify,
  207. .verify_struct = xfs_attr3_rmt_verify_struct,
  208. };
  209. STATIC int
  210. xfs_attr3_rmt_hdr_set(
  211. struct xfs_mount *mp,
  212. void *ptr,
  213. xfs_ino_t ino,
  214. uint32_t offset,
  215. uint32_t size,
  216. xfs_daddr_t bno)
  217. {
  218. struct xfs_attr3_rmt_hdr *rmt = ptr;
  219. if (!xfs_has_crc(mp))
  220. return 0;
  221. rmt->rm_magic = cpu_to_be32(XFS_ATTR3_RMT_MAGIC);
  222. rmt->rm_offset = cpu_to_be32(offset);
  223. rmt->rm_bytes = cpu_to_be32(size);
  224. uuid_copy(&rmt->rm_uuid, &mp->m_sb.sb_meta_uuid);
  225. rmt->rm_owner = cpu_to_be64(ino);
  226. rmt->rm_blkno = cpu_to_be64(bno);
  227. /*
  228. * Remote attribute blocks are written synchronously, so we don't
  229. * have an LSN that we can stamp in them that makes any sense to log
  230. * recovery. To ensure that log recovery handles overwrites of these
  231. * blocks sanely (i.e. once they've been freed and reallocated as some
  232. * other type of metadata) we need to ensure that the LSN has a value
  233. * that tells log recovery to ignore the LSN and overwrite the buffer
  234. * with whatever is in it's log. To do this, we use the magic
  235. * NULLCOMMITLSN to indicate that the LSN is invalid.
  236. */
  237. rmt->rm_lsn = cpu_to_be64(NULLCOMMITLSN);
  238. return sizeof(struct xfs_attr3_rmt_hdr);
  239. }
  240. /*
  241. * Helper functions to copy attribute data in and out of the one disk extents
  242. */
  243. STATIC int
  244. xfs_attr_rmtval_copyout(
  245. struct xfs_mount *mp,
  246. struct xfs_buf *bp,
  247. xfs_ino_t ino,
  248. int *offset,
  249. int *valuelen,
  250. uint8_t **dst)
  251. {
  252. char *src = bp->b_addr;
  253. xfs_daddr_t bno = xfs_buf_daddr(bp);
  254. int len = BBTOB(bp->b_length);
  255. int blksize = mp->m_attr_geo->blksize;
  256. ASSERT(len >= blksize);
  257. while (len > 0 && *valuelen > 0) {
  258. int hdr_size = 0;
  259. int byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, blksize);
  260. byte_cnt = min(*valuelen, byte_cnt);
  261. if (xfs_has_crc(mp)) {
  262. if (xfs_attr3_rmt_hdr_ok(src, ino, *offset,
  263. byte_cnt, bno)) {
  264. xfs_alert(mp,
  265. "remote attribute header mismatch bno/off/len/owner (0x%llx/0x%x/Ox%x/0x%llx)",
  266. bno, *offset, byte_cnt, ino);
  267. return -EFSCORRUPTED;
  268. }
  269. hdr_size = sizeof(struct xfs_attr3_rmt_hdr);
  270. }
  271. memcpy(*dst, src + hdr_size, byte_cnt);
  272. /* roll buffer forwards */
  273. len -= blksize;
  274. src += blksize;
  275. bno += BTOBB(blksize);
  276. /* roll attribute data forwards */
  277. *valuelen -= byte_cnt;
  278. *dst += byte_cnt;
  279. *offset += byte_cnt;
  280. }
  281. return 0;
  282. }
  283. STATIC void
  284. xfs_attr_rmtval_copyin(
  285. struct xfs_mount *mp,
  286. struct xfs_buf *bp,
  287. xfs_ino_t ino,
  288. int *offset,
  289. int *valuelen,
  290. uint8_t **src)
  291. {
  292. char *dst = bp->b_addr;
  293. xfs_daddr_t bno = xfs_buf_daddr(bp);
  294. int len = BBTOB(bp->b_length);
  295. int blksize = mp->m_attr_geo->blksize;
  296. ASSERT(len >= blksize);
  297. while (len > 0 && *valuelen > 0) {
  298. int hdr_size;
  299. int byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, blksize);
  300. byte_cnt = min(*valuelen, byte_cnt);
  301. hdr_size = xfs_attr3_rmt_hdr_set(mp, dst, ino, *offset,
  302. byte_cnt, bno);
  303. memcpy(dst + hdr_size, *src, byte_cnt);
  304. /*
  305. * If this is the last block, zero the remainder of it.
  306. * Check that we are actually the last block, too.
  307. */
  308. if (byte_cnt + hdr_size < blksize) {
  309. ASSERT(*valuelen - byte_cnt == 0);
  310. ASSERT(len == blksize);
  311. memset(dst + hdr_size + byte_cnt, 0,
  312. blksize - hdr_size - byte_cnt);
  313. }
  314. /* roll buffer forwards */
  315. len -= blksize;
  316. dst += blksize;
  317. bno += BTOBB(blksize);
  318. /* roll attribute data forwards */
  319. *valuelen -= byte_cnt;
  320. *src += byte_cnt;
  321. *offset += byte_cnt;
  322. }
  323. }
  324. /*
  325. * Read the value associated with an attribute from the out-of-line buffer
  326. * that we stored it in.
  327. *
  328. * Returns 0 on successful retrieval, otherwise an error.
  329. */
  330. int
  331. xfs_attr_rmtval_get(
  332. struct xfs_da_args *args)
  333. {
  334. struct xfs_bmbt_irec map[ATTR_RMTVALUE_MAPSIZE];
  335. struct xfs_mount *mp = args->dp->i_mount;
  336. struct xfs_buf *bp;
  337. xfs_dablk_t lblkno = args->rmtblkno;
  338. uint8_t *dst = args->value;
  339. int valuelen;
  340. int nmap;
  341. int error;
  342. int blkcnt = args->rmtblkcnt;
  343. int i;
  344. int offset = 0;
  345. trace_xfs_attr_rmtval_get(args);
  346. ASSERT(args->valuelen != 0);
  347. ASSERT(args->rmtvaluelen == args->valuelen);
  348. valuelen = args->rmtvaluelen;
  349. while (valuelen > 0) {
  350. nmap = ATTR_RMTVALUE_MAPSIZE;
  351. error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
  352. blkcnt, map, &nmap,
  353. XFS_BMAPI_ATTRFORK);
  354. if (error)
  355. return error;
  356. ASSERT(nmap >= 1);
  357. for (i = 0; (i < nmap) && (valuelen > 0); i++) {
  358. xfs_daddr_t dblkno;
  359. int dblkcnt;
  360. ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) &&
  361. (map[i].br_startblock != HOLESTARTBLOCK));
  362. dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock);
  363. dblkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
  364. error = xfs_buf_read(mp->m_ddev_targp, dblkno, dblkcnt,
  365. 0, &bp, &xfs_attr3_rmt_buf_ops);
  366. if (error)
  367. return error;
  368. error = xfs_attr_rmtval_copyout(mp, bp, args->dp->i_ino,
  369. &offset, &valuelen,
  370. &dst);
  371. xfs_buf_relse(bp);
  372. if (error)
  373. return error;
  374. /* roll attribute extent map forwards */
  375. lblkno += map[i].br_blockcount;
  376. blkcnt -= map[i].br_blockcount;
  377. }
  378. }
  379. ASSERT(valuelen == 0);
  380. return 0;
  381. }
  382. /*
  383. * Find a "hole" in the attribute address space large enough for us to drop the
  384. * new attributes value into
  385. */
  386. int
  387. xfs_attr_rmt_find_hole(
  388. struct xfs_da_args *args)
  389. {
  390. struct xfs_inode *dp = args->dp;
  391. struct xfs_mount *mp = dp->i_mount;
  392. int error;
  393. int blkcnt;
  394. xfs_fileoff_t lfileoff = 0;
  395. /*
  396. * Because CRC enable attributes have headers, we can't just do a
  397. * straight byte to FSB conversion and have to take the header space
  398. * into account.
  399. */
  400. blkcnt = xfs_attr3_rmt_blocks(mp, args->rmtvaluelen);
  401. error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
  402. XFS_ATTR_FORK);
  403. if (error)
  404. return error;
  405. args->rmtblkno = (xfs_dablk_t)lfileoff;
  406. args->rmtblkcnt = blkcnt;
  407. return 0;
  408. }
  409. int
  410. xfs_attr_rmtval_set_value(
  411. struct xfs_da_args *args)
  412. {
  413. struct xfs_inode *dp = args->dp;
  414. struct xfs_mount *mp = dp->i_mount;
  415. struct xfs_bmbt_irec map;
  416. xfs_dablk_t lblkno;
  417. uint8_t *src = args->value;
  418. int blkcnt;
  419. int valuelen;
  420. int nmap;
  421. int error;
  422. int offset = 0;
  423. /*
  424. * Roll through the "value", copying the attribute value to the
  425. * already-allocated blocks. Blocks are written synchronously
  426. * so that we can know they are all on disk before we turn off
  427. * the INCOMPLETE flag.
  428. */
  429. lblkno = args->rmtblkno;
  430. blkcnt = args->rmtblkcnt;
  431. valuelen = args->rmtvaluelen;
  432. while (valuelen > 0) {
  433. struct xfs_buf *bp;
  434. xfs_daddr_t dblkno;
  435. int dblkcnt;
  436. ASSERT(blkcnt > 0);
  437. nmap = 1;
  438. error = xfs_bmapi_read(dp, (xfs_fileoff_t)lblkno,
  439. blkcnt, &map, &nmap,
  440. XFS_BMAPI_ATTRFORK);
  441. if (error)
  442. return error;
  443. ASSERT(nmap == 1);
  444. ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
  445. (map.br_startblock != HOLESTARTBLOCK));
  446. dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
  447. dblkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
  448. error = xfs_buf_get(mp->m_ddev_targp, dblkno, dblkcnt, &bp);
  449. if (error)
  450. return error;
  451. bp->b_ops = &xfs_attr3_rmt_buf_ops;
  452. xfs_attr_rmtval_copyin(mp, bp, args->dp->i_ino, &offset,
  453. &valuelen, &src);
  454. error = xfs_bwrite(bp); /* GROT: NOTE: synchronous write */
  455. xfs_buf_relse(bp);
  456. if (error)
  457. return error;
  458. /* roll attribute extent map forwards */
  459. lblkno += map.br_blockcount;
  460. blkcnt -= map.br_blockcount;
  461. }
  462. ASSERT(valuelen == 0);
  463. return 0;
  464. }
  465. /* Mark stale any incore buffers for the remote value. */
  466. int
  467. xfs_attr_rmtval_stale(
  468. struct xfs_inode *ip,
  469. struct xfs_bmbt_irec *map,
  470. xfs_buf_flags_t incore_flags)
  471. {
  472. struct xfs_mount *mp = ip->i_mount;
  473. struct xfs_buf *bp;
  474. int error;
  475. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  476. if (XFS_IS_CORRUPT(mp, map->br_startblock == DELAYSTARTBLOCK) ||
  477. XFS_IS_CORRUPT(mp, map->br_startblock == HOLESTARTBLOCK))
  478. return -EFSCORRUPTED;
  479. error = xfs_buf_incore(mp->m_ddev_targp,
  480. XFS_FSB_TO_DADDR(mp, map->br_startblock),
  481. XFS_FSB_TO_BB(mp, map->br_blockcount),
  482. incore_flags, &bp);
  483. if (error) {
  484. if (error == -ENOENT)
  485. return 0;
  486. return error;
  487. }
  488. xfs_buf_stale(bp);
  489. xfs_buf_relse(bp);
  490. return 0;
  491. }
  492. /*
  493. * Find a hole for the attr and store it in the delayed attr context. This
  494. * initializes the context to roll through allocating an attr extent for a
  495. * delayed attr operation
  496. */
  497. int
  498. xfs_attr_rmtval_find_space(
  499. struct xfs_attr_intent *attr)
  500. {
  501. struct xfs_da_args *args = attr->xattri_da_args;
  502. struct xfs_bmbt_irec *map = &attr->xattri_map;
  503. int error;
  504. attr->xattri_lblkno = 0;
  505. attr->xattri_blkcnt = 0;
  506. args->rmtblkcnt = 0;
  507. args->rmtblkno = 0;
  508. memset(map, 0, sizeof(struct xfs_bmbt_irec));
  509. error = xfs_attr_rmt_find_hole(args);
  510. if (error)
  511. return error;
  512. attr->xattri_blkcnt = args->rmtblkcnt;
  513. attr->xattri_lblkno = args->rmtblkno;
  514. return 0;
  515. }
  516. /*
  517. * Write one block of the value associated with an attribute into the
  518. * out-of-line buffer that we have defined for it. This is similar to a subset
  519. * of xfs_attr_rmtval_set, but records the current block to the delayed attr
  520. * context, and leaves transaction handling to the caller.
  521. */
  522. int
  523. xfs_attr_rmtval_set_blk(
  524. struct xfs_attr_intent *attr)
  525. {
  526. struct xfs_da_args *args = attr->xattri_da_args;
  527. struct xfs_inode *dp = args->dp;
  528. struct xfs_bmbt_irec *map = &attr->xattri_map;
  529. int nmap;
  530. int error;
  531. nmap = 1;
  532. error = xfs_bmapi_write(args->trans, dp,
  533. (xfs_fileoff_t)attr->xattri_lblkno,
  534. attr->xattri_blkcnt, XFS_BMAPI_ATTRFORK, args->total,
  535. map, &nmap);
  536. if (error)
  537. return error;
  538. ASSERT(nmap == 1);
  539. ASSERT((map->br_startblock != DELAYSTARTBLOCK) &&
  540. (map->br_startblock != HOLESTARTBLOCK));
  541. /* roll attribute extent map forwards */
  542. attr->xattri_lblkno += map->br_blockcount;
  543. attr->xattri_blkcnt -= map->br_blockcount;
  544. return 0;
  545. }
  546. /*
  547. * Remove the value associated with an attribute by deleting the
  548. * out-of-line buffer that it is stored on.
  549. */
  550. int
  551. xfs_attr_rmtval_invalidate(
  552. struct xfs_da_args *args)
  553. {
  554. xfs_dablk_t lblkno;
  555. int blkcnt;
  556. int error;
  557. /*
  558. * Roll through the "value", invalidating the attribute value's blocks.
  559. */
  560. lblkno = args->rmtblkno;
  561. blkcnt = args->rmtblkcnt;
  562. while (blkcnt > 0) {
  563. struct xfs_bmbt_irec map;
  564. int nmap;
  565. /*
  566. * Try to remember where we decided to put the value.
  567. */
  568. nmap = 1;
  569. error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
  570. blkcnt, &map, &nmap, XFS_BMAPI_ATTRFORK);
  571. if (error)
  572. return error;
  573. if (XFS_IS_CORRUPT(args->dp->i_mount, nmap != 1))
  574. return -EFSCORRUPTED;
  575. error = xfs_attr_rmtval_stale(args->dp, &map, XBF_TRYLOCK);
  576. if (error)
  577. return error;
  578. lblkno += map.br_blockcount;
  579. blkcnt -= map.br_blockcount;
  580. }
  581. return 0;
  582. }
  583. /*
  584. * Remove the value associated with an attribute by deleting the out-of-line
  585. * buffer that it is stored on. Returns -EAGAIN for the caller to refresh the
  586. * transaction and re-call the function. Callers should keep calling this
  587. * routine until it returns something other than -EAGAIN.
  588. */
  589. int
  590. xfs_attr_rmtval_remove(
  591. struct xfs_attr_intent *attr)
  592. {
  593. struct xfs_da_args *args = attr->xattri_da_args;
  594. int error, done;
  595. /*
  596. * Unmap value blocks for this attr.
  597. */
  598. error = xfs_bunmapi(args->trans, args->dp, args->rmtblkno,
  599. args->rmtblkcnt, XFS_BMAPI_ATTRFORK, 1, &done);
  600. if (error)
  601. return error;
  602. /*
  603. * We don't need an explicit state here to pick up where we left off. We
  604. * can figure it out using the !done return code. The actual value of
  605. * attr->xattri_dela_state may be some value reminiscent of the calling
  606. * function, but it's value is irrelevant with in the context of this
  607. * function. Once we are done here, the next state is set as needed by
  608. * the parent
  609. */
  610. if (!done) {
  611. trace_xfs_attr_rmtval_remove_return(attr->xattri_dela_state,
  612. args->dp);
  613. return -EAGAIN;
  614. }
  615. args->rmtblkno = 0;
  616. args->rmtblkcnt = 0;
  617. return 0;
  618. }