xfs_symlink_remote.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  4. * Copyright (c) 2012-2013 Red Hat, Inc.
  5. * All rights reserved.
  6. */
  7. #include "xfs.h"
  8. #include "xfs_fs.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_shared.h"
  12. #include "xfs_trans_resv.h"
  13. #include "xfs_mount.h"
  14. #include "xfs_bmap_btree.h"
  15. #include "xfs_inode.h"
  16. #include "xfs_error.h"
  17. #include "xfs_trace.h"
  18. #include "xfs_symlink.h"
  19. #include "xfs_cksum.h"
  20. #include "xfs_trans.h"
  21. #include "xfs_buf_item.h"
  22. #include "xfs_log.h"
  23. /*
  24. * Each contiguous block has a header, so it is not just a simple pathlen
  25. * to FSB conversion.
  26. */
  27. int
  28. xfs_symlink_blocks(
  29. struct xfs_mount *mp,
  30. int pathlen)
  31. {
  32. int buflen = XFS_SYMLINK_BUF_SPACE(mp, mp->m_sb.sb_blocksize);
  33. return (pathlen + buflen - 1) / buflen;
  34. }
  35. int
  36. xfs_symlink_hdr_set(
  37. struct xfs_mount *mp,
  38. xfs_ino_t ino,
  39. uint32_t offset,
  40. uint32_t size,
  41. struct xfs_buf *bp)
  42. {
  43. struct xfs_dsymlink_hdr *dsl = bp->b_addr;
  44. if (!xfs_sb_version_hascrc(&mp->m_sb))
  45. return 0;
  46. memset(dsl, 0, sizeof(struct xfs_dsymlink_hdr));
  47. dsl->sl_magic = cpu_to_be32(XFS_SYMLINK_MAGIC);
  48. dsl->sl_offset = cpu_to_be32(offset);
  49. dsl->sl_bytes = cpu_to_be32(size);
  50. uuid_copy(&dsl->sl_uuid, &mp->m_sb.sb_meta_uuid);
  51. dsl->sl_owner = cpu_to_be64(ino);
  52. dsl->sl_blkno = cpu_to_be64(bp->b_bn);
  53. bp->b_ops = &xfs_symlink_buf_ops;
  54. return sizeof(struct xfs_dsymlink_hdr);
  55. }
  56. /*
  57. * Checking of the symlink header is split into two parts. the verifier does
  58. * CRC, location and bounds checking, the unpacking function checks the path
  59. * parameters and owner.
  60. */
  61. bool
  62. xfs_symlink_hdr_ok(
  63. xfs_ino_t ino,
  64. uint32_t offset,
  65. uint32_t size,
  66. struct xfs_buf *bp)
  67. {
  68. struct xfs_dsymlink_hdr *dsl = bp->b_addr;
  69. if (offset != be32_to_cpu(dsl->sl_offset))
  70. return false;
  71. if (size != be32_to_cpu(dsl->sl_bytes))
  72. return false;
  73. if (ino != be64_to_cpu(dsl->sl_owner))
  74. return false;
  75. /* ok */
  76. return true;
  77. }
  78. static xfs_failaddr_t
  79. xfs_symlink_verify(
  80. struct xfs_buf *bp)
  81. {
  82. struct xfs_mount *mp = bp->b_target->bt_mount;
  83. struct xfs_dsymlink_hdr *dsl = bp->b_addr;
  84. if (!xfs_sb_version_hascrc(&mp->m_sb))
  85. return __this_address;
  86. if (dsl->sl_magic != cpu_to_be32(XFS_SYMLINK_MAGIC))
  87. return __this_address;
  88. if (!uuid_equal(&dsl->sl_uuid, &mp->m_sb.sb_meta_uuid))
  89. return __this_address;
  90. if (bp->b_bn != be64_to_cpu(dsl->sl_blkno))
  91. return __this_address;
  92. if (be32_to_cpu(dsl->sl_offset) +
  93. be32_to_cpu(dsl->sl_bytes) >= XFS_SYMLINK_MAXLEN)
  94. return __this_address;
  95. if (dsl->sl_owner == 0)
  96. return __this_address;
  97. if (!xfs_log_check_lsn(mp, be64_to_cpu(dsl->sl_lsn)))
  98. return __this_address;
  99. return NULL;
  100. }
  101. static void
  102. xfs_symlink_read_verify(
  103. struct xfs_buf *bp)
  104. {
  105. struct xfs_mount *mp = bp->b_target->bt_mount;
  106. xfs_failaddr_t fa;
  107. /* no verification of non-crc buffers */
  108. if (!xfs_sb_version_hascrc(&mp->m_sb))
  109. return;
  110. if (!xfs_buf_verify_cksum(bp, XFS_SYMLINK_CRC_OFF))
  111. xfs_verifier_error(bp, -EFSBADCRC, __this_address);
  112. else {
  113. fa = xfs_symlink_verify(bp);
  114. if (fa)
  115. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  116. }
  117. }
  118. static void
  119. xfs_symlink_write_verify(
  120. struct xfs_buf *bp)
  121. {
  122. struct xfs_mount *mp = bp->b_target->bt_mount;
  123. struct xfs_buf_log_item *bip = bp->b_log_item;
  124. xfs_failaddr_t fa;
  125. /* no verification of non-crc buffers */
  126. if (!xfs_sb_version_hascrc(&mp->m_sb))
  127. return;
  128. fa = xfs_symlink_verify(bp);
  129. if (fa) {
  130. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  131. return;
  132. }
  133. if (bip) {
  134. struct xfs_dsymlink_hdr *dsl = bp->b_addr;
  135. dsl->sl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
  136. }
  137. xfs_buf_update_cksum(bp, XFS_SYMLINK_CRC_OFF);
  138. }
  139. const struct xfs_buf_ops xfs_symlink_buf_ops = {
  140. .name = "xfs_symlink",
  141. .verify_read = xfs_symlink_read_verify,
  142. .verify_write = xfs_symlink_write_verify,
  143. .verify_struct = xfs_symlink_verify,
  144. };
  145. void
  146. xfs_symlink_local_to_remote(
  147. struct xfs_trans *tp,
  148. struct xfs_buf *bp,
  149. struct xfs_inode *ip,
  150. struct xfs_ifork *ifp)
  151. {
  152. struct xfs_mount *mp = ip->i_mount;
  153. char *buf;
  154. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SYMLINK_BUF);
  155. if (!xfs_sb_version_hascrc(&mp->m_sb)) {
  156. bp->b_ops = NULL;
  157. memcpy(bp->b_addr, ifp->if_u1.if_data, ifp->if_bytes);
  158. xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
  159. return;
  160. }
  161. /*
  162. * As this symlink fits in an inode literal area, it must also fit in
  163. * the smallest buffer the filesystem supports.
  164. */
  165. ASSERT(BBTOB(bp->b_length) >=
  166. ifp->if_bytes + sizeof(struct xfs_dsymlink_hdr));
  167. bp->b_ops = &xfs_symlink_buf_ops;
  168. buf = bp->b_addr;
  169. buf += xfs_symlink_hdr_set(mp, ip->i_ino, 0, ifp->if_bytes, bp);
  170. memcpy(buf, ifp->if_u1.if_data, ifp->if_bytes);
  171. xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsymlink_hdr) +
  172. ifp->if_bytes - 1);
  173. }
  174. /*
  175. * Verify the in-memory consistency of an inline symlink data fork. This
  176. * does not do on-disk format checks.
  177. */
  178. xfs_failaddr_t
  179. xfs_symlink_shortform_verify(
  180. struct xfs_inode *ip)
  181. {
  182. char *sfp;
  183. char *endp;
  184. struct xfs_ifork *ifp;
  185. int size;
  186. ASSERT(ip->i_d.di_format == XFS_DINODE_FMT_LOCAL);
  187. ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
  188. sfp = (char *)ifp->if_u1.if_data;
  189. size = ifp->if_bytes;
  190. endp = sfp + size;
  191. /*
  192. * Zero length symlinks should never occur in memory as they are
  193. * never alllowed to exist on disk.
  194. */
  195. if (!size)
  196. return __this_address;
  197. /* No negative sizes or overly long symlink targets. */
  198. if (size < 0 || size > XFS_SYMLINK_MAXLEN)
  199. return __this_address;
  200. /* No NULLs in the target either. */
  201. if (memchr(sfp, 0, size - 1))
  202. return __this_address;
  203. /* We /did/ null-terminate the buffer, right? */
  204. if (*endp != 0)
  205. return __this_address;
  206. return NULL;
  207. }