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"
19 #include "xfs_symlink_remote.h"
22 #include "xfs_health.h"
25 * Each contiguous block has a header, so it is not just a simple pathlen
33 int buflen = XFS_SYMLINK_BUF_SPACE(mp, mp->m_sb.sb_blocksize);
35 return (pathlen + buflen - 1) / buflen;
46 struct xfs_dsymlink_hdr *dsl = bp->b_addr;
51 memset(dsl, 0, sizeof(struct xfs_dsymlink_hdr));
52 dsl->sl_magic = cpu_to_be32(XFS_SYMLINK_MAGIC);
53 dsl->sl_offset = cpu_to_be32(offset);
54 dsl->sl_bytes = cpu_to_be32(size);
55 uuid_copy(&dsl->sl_uuid, &mp->m_sb.sb_meta_uuid);
56 dsl->sl_owner = cpu_to_be64(ino);
57 dsl->sl_blkno = cpu_to_be64(xfs_buf_daddr(bp));
58 bp->b_ops = &xfs_symlink_buf_ops;
60 return sizeof(struct xfs_dsymlink_hdr);
64 * Checking of the symlink header is split into two parts. the verifier does
65 * CRC, location and bounds checking, the unpacking function checks the path
66 * parameters and owner.
75 struct xfs_dsymlink_hdr *dsl = bp->b_addr;
77 if (offset != be32_to_cpu(dsl->sl_offset))
79 if (size != be32_to_cpu(dsl->sl_bytes))
81 if (ino != be64_to_cpu(dsl->sl_owner))
92 struct xfs_mount *mp = bp->b_mount;
93 struct xfs_dsymlink_hdr *dsl = bp->b_addr;
96 return __this_address;
97 if (!xfs_verify_magic(bp, dsl->sl_magic))
98 return __this_address;
99 if (!uuid_equal(&dsl->sl_uuid, &mp->m_sb.sb_meta_uuid))
100 return __this_address;
101 if (xfs_buf_daddr(bp) != be64_to_cpu(dsl->sl_blkno))
102 return __this_address;
103 if (be32_to_cpu(dsl->sl_offset) +
104 be32_to_cpu(dsl->sl_bytes) >= XFS_SYMLINK_MAXLEN)
105 return __this_address;
106 if (dsl->sl_owner == 0)
107 return __this_address;
108 if (!xfs_log_check_lsn(mp, be64_to_cpu(dsl->sl_lsn)))
109 return __this_address;
115 xfs_symlink_read_verify(
118 struct xfs_mount *mp = bp->b_mount;
121 /* no verification of non-crc buffers */
122 if (!xfs_has_crc(mp))
125 if (!xfs_buf_verify_cksum(bp, XFS_SYMLINK_CRC_OFF))
126 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
128 fa = xfs_symlink_verify(bp);
130 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
135 xfs_symlink_write_verify(
138 struct xfs_mount *mp = bp->b_mount;
139 struct xfs_buf_log_item *bip = bp->b_log_item;
142 /* no verification of non-crc buffers */
143 if (!xfs_has_crc(mp))
146 fa = xfs_symlink_verify(bp);
148 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
153 struct xfs_dsymlink_hdr *dsl = bp->b_addr;
154 dsl->sl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
156 xfs_buf_update_cksum(bp, XFS_SYMLINK_CRC_OFF);
159 const struct xfs_buf_ops xfs_symlink_buf_ops = {
160 .name = "xfs_symlink",
161 .magic = { 0, cpu_to_be32(XFS_SYMLINK_MAGIC) },
162 .verify_read = xfs_symlink_read_verify,
163 .verify_write = xfs_symlink_write_verify,
164 .verify_struct = xfs_symlink_verify,
168 xfs_symlink_local_to_remote(
169 struct xfs_trans *tp,
171 struct xfs_inode *ip,
172 struct xfs_ifork *ifp)
174 struct xfs_mount *mp = ip->i_mount;
177 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SYMLINK_BUF);
179 if (!xfs_has_crc(mp)) {
181 memcpy(bp->b_addr, ifp->if_data, ifp->if_bytes);
182 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
187 * As this symlink fits in an inode literal area, it must also fit in
188 * the smallest buffer the filesystem supports.
190 ASSERT(BBTOB(bp->b_length) >=
191 ifp->if_bytes + sizeof(struct xfs_dsymlink_hdr));
193 bp->b_ops = &xfs_symlink_buf_ops;
196 buf += xfs_symlink_hdr_set(mp, ip->i_ino, 0, ifp->if_bytes, bp);
197 memcpy(buf, ifp->if_data, ifp->if_bytes);
198 xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsymlink_hdr) +
203 * Verify the in-memory consistency of an inline symlink data fork. This
204 * does not do on-disk format checks.
207 xfs_symlink_shortform_verify(
211 char *endp = sfp + size;
214 * Zero length symlinks should never occur in memory as they are
215 * never allowed to exist on disk.
218 return __this_address;
220 /* No negative sizes or overly long symlink targets. */
221 if (size < 0 || size > XFS_SYMLINK_MAXLEN)
222 return __this_address;
224 /* No NULLs in the target either. */
225 if (memchr(sfp, 0, size - 1))
226 return __this_address;
228 /* We /did/ null-terminate the buffer, right? */
230 return __this_address;
234 /* Read a remote symlink target into the buffer. */
236 xfs_symlink_remote_read(
237 struct xfs_inode *ip,
240 struct xfs_mount *mp = ip->i_mount;
241 struct xfs_bmbt_irec mval[XFS_SYMLINK_MAPS];
245 int pathlen = ip->i_disk_size;
246 int nmaps = XFS_SYMLINK_MAPS;
253 xfs_assert_ilocked(ip, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL);
255 fsblocks = xfs_symlink_blocks(mp, pathlen);
256 error = xfs_bmapi_read(ip, 0, fsblocks, mval, &nmaps, 0);
261 for (n = 0; n < nmaps; n++) {
262 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
263 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
265 error = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0,
266 &bp, &xfs_symlink_buf_ops);
267 if (xfs_metadata_is_sick(error))
268 xfs_inode_mark_sick(ip, XFS_SICK_INO_SYMLINK);
271 byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt);
272 if (pathlen < byte_cnt)
275 cur_chunk = bp->b_addr;
276 if (xfs_has_crc(mp)) {
277 if (!xfs_symlink_hdr_ok(ip->i_ino, offset,
279 xfs_inode_mark_sick(ip, XFS_SICK_INO_SYMLINK);
280 error = -EFSCORRUPTED;
282 "symlink header does not match required off/len/owner (0x%x/0x%x,0x%llx)",
283 offset, byte_cnt, ip->i_ino);
289 cur_chunk += sizeof(struct xfs_dsymlink_hdr);
292 memcpy(link + offset, cur_chunk, byte_cnt);
299 ASSERT(pathlen == 0);
301 link[ip->i_disk_size] = '\0';
308 /* Write the symlink target into the inode. */
310 xfs_symlink_write_target(
311 struct xfs_trans *tp,
312 struct xfs_inode *ip,
313 const char *target_path,
315 xfs_fsblock_t fs_blocks,
318 struct xfs_bmbt_irec mval[XFS_SYMLINK_MAPS];
319 struct xfs_mount *mp = tp->t_mountp;
320 const char *cur_chunk;
330 * If the symlink will fit into the inode, write it inline.
332 if (pathlen <= xfs_inode_data_fork_size(ip)) {
333 xfs_init_local_fork(ip, XFS_DATA_FORK, target_path, pathlen);
335 ip->i_disk_size = pathlen;
336 ip->i_df.if_format = XFS_DINODE_FMT_LOCAL;
337 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
341 nmaps = XFS_SYMLINK_MAPS;
342 error = xfs_bmapi_write(tp, ip, 0, fs_blocks, XFS_BMAPI_METADATA,
343 resblks, mval, &nmaps);
347 ip->i_disk_size = pathlen;
348 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
350 cur_chunk = target_path;
352 for (n = 0; n < nmaps; n++) {
355 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
356 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
357 error = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
358 BTOBB(byte_cnt), 0, &bp);
361 bp->b_ops = &xfs_symlink_buf_ops;
363 byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt);
364 byte_cnt = min(byte_cnt, pathlen);
367 buf += xfs_symlink_hdr_set(mp, ip->i_ino, offset, byte_cnt,
370 memcpy(buf, cur_chunk, byte_cnt);
372 cur_chunk += byte_cnt;
376 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SYMLINK_BUF);
377 xfs_trans_log_buf(tp, bp, 0, (buf + byte_cnt - 1) -
380 ASSERT(pathlen == 0);