1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
4 * Copyright (c) 2012-2013 Red Hat, Inc.
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_inode.h"
15 #include "xfs_error.h"
16 #include "xfs_trans.h"
17 #include "xfs_buf_item.h"
22 * Each contiguous block has a header, so it is not just a simple pathlen
30 int buflen = XFS_SYMLINK_BUF_SPACE(mp, mp->m_sb.sb_blocksize);
32 return (pathlen + buflen - 1) / buflen;
43 struct xfs_dsymlink_hdr *dsl = bp->b_addr;
48 memset(dsl, 0, sizeof(struct xfs_dsymlink_hdr));
49 dsl->sl_magic = cpu_to_be32(XFS_SYMLINK_MAGIC);
50 dsl->sl_offset = cpu_to_be32(offset);
51 dsl->sl_bytes = cpu_to_be32(size);
52 uuid_copy(&dsl->sl_uuid, &mp->m_sb.sb_meta_uuid);
53 dsl->sl_owner = cpu_to_be64(ino);
54 dsl->sl_blkno = cpu_to_be64(xfs_buf_daddr(bp));
55 bp->b_ops = &xfs_symlink_buf_ops;
57 return sizeof(struct xfs_dsymlink_hdr);
61 * Checking of the symlink header is split into two parts. the verifier does
62 * CRC, location and bounds checking, the unpacking function checks the path
63 * parameters and owner.
72 struct xfs_dsymlink_hdr *dsl = bp->b_addr;
74 if (offset != be32_to_cpu(dsl->sl_offset))
76 if (size != be32_to_cpu(dsl->sl_bytes))
78 if (ino != be64_to_cpu(dsl->sl_owner))
89 struct xfs_mount *mp = bp->b_mount;
90 struct xfs_dsymlink_hdr *dsl = bp->b_addr;
93 return __this_address;
94 if (!xfs_verify_magic(bp, dsl->sl_magic))
95 return __this_address;
96 if (!uuid_equal(&dsl->sl_uuid, &mp->m_sb.sb_meta_uuid))
97 return __this_address;
98 if (xfs_buf_daddr(bp) != be64_to_cpu(dsl->sl_blkno))
99 return __this_address;
100 if (be32_to_cpu(dsl->sl_offset) +
101 be32_to_cpu(dsl->sl_bytes) >= XFS_SYMLINK_MAXLEN)
102 return __this_address;
103 if (dsl->sl_owner == 0)
104 return __this_address;
105 if (!xfs_log_check_lsn(mp, be64_to_cpu(dsl->sl_lsn)))
106 return __this_address;
112 xfs_symlink_read_verify(
115 struct xfs_mount *mp = bp->b_mount;
118 /* no verification of non-crc buffers */
119 if (!xfs_has_crc(mp))
122 if (!xfs_buf_verify_cksum(bp, XFS_SYMLINK_CRC_OFF))
123 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
125 fa = xfs_symlink_verify(bp);
127 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
132 xfs_symlink_write_verify(
135 struct xfs_mount *mp = bp->b_mount;
136 struct xfs_buf_log_item *bip = bp->b_log_item;
139 /* no verification of non-crc buffers */
140 if (!xfs_has_crc(mp))
143 fa = xfs_symlink_verify(bp);
145 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
150 struct xfs_dsymlink_hdr *dsl = bp->b_addr;
151 dsl->sl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
153 xfs_buf_update_cksum(bp, XFS_SYMLINK_CRC_OFF);
156 const struct xfs_buf_ops xfs_symlink_buf_ops = {
157 .name = "xfs_symlink",
158 .magic = { 0, cpu_to_be32(XFS_SYMLINK_MAGIC) },
159 .verify_read = xfs_symlink_read_verify,
160 .verify_write = xfs_symlink_write_verify,
161 .verify_struct = xfs_symlink_verify,
165 xfs_symlink_local_to_remote(
166 struct xfs_trans *tp,
168 struct xfs_inode *ip,
169 struct xfs_ifork *ifp)
171 struct xfs_mount *mp = ip->i_mount;
174 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SYMLINK_BUF);
176 if (!xfs_has_crc(mp)) {
178 memcpy(bp->b_addr, ifp->if_u1.if_data, ifp->if_bytes);
179 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
184 * As this symlink fits in an inode literal area, it must also fit in
185 * the smallest buffer the filesystem supports.
187 ASSERT(BBTOB(bp->b_length) >=
188 ifp->if_bytes + sizeof(struct xfs_dsymlink_hdr));
190 bp->b_ops = &xfs_symlink_buf_ops;
193 buf += xfs_symlink_hdr_set(mp, ip->i_ino, 0, ifp->if_bytes, bp);
194 memcpy(buf, ifp->if_u1.if_data, ifp->if_bytes);
195 xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsymlink_hdr) +
200 * Verify the in-memory consistency of an inline symlink data fork. This
201 * does not do on-disk format checks.
204 xfs_symlink_shortform_verify(
205 struct xfs_inode *ip)
207 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, XFS_DATA_FORK);
208 char *sfp = (char *)ifp->if_u1.if_data;
209 int size = ifp->if_bytes;
210 char *endp = sfp + size;
212 ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
215 * Zero length symlinks should never occur in memory as they are
216 * never allowed to exist on disk.
219 return __this_address;
221 /* No negative sizes or overly long symlink targets. */
222 if (size < 0 || size > XFS_SYMLINK_MAXLEN)
223 return __this_address;
225 /* No NULLs in the target either. */
226 if (memchr(sfp, 0, size - 1))
227 return __this_address;
229 /* We /did/ null-terminate the buffer, right? */
231 return __this_address;