Linux Audio

Check our new training course

Loading...
v6.9.4
  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_inode.h"
 15#include "xfs_error.h"
 16#include "xfs_trans.h"
 17#include "xfs_buf_item.h"
 18#include "xfs_log.h"
 19#include "xfs_symlink_remote.h"
 20#include "xfs_bit.h"
 21#include "xfs_bmap.h"
 22#include "xfs_health.h"
 23
 24/*
 25 * Each contiguous block has a header, so it is not just a simple pathlen
 26 * to FSB conversion.
 27 */
 28int
 29xfs_symlink_blocks(
 30	struct xfs_mount *mp,
 31	int		pathlen)
 32{
 33	int buflen = XFS_SYMLINK_BUF_SPACE(mp, mp->m_sb.sb_blocksize);
 34
 35	return (pathlen + buflen - 1) / buflen;
 36}
 37
 38int
 39xfs_symlink_hdr_set(
 40	struct xfs_mount	*mp,
 41	xfs_ino_t		ino,
 42	uint32_t		offset,
 43	uint32_t		size,
 44	struct xfs_buf		*bp)
 45{
 46	struct xfs_dsymlink_hdr	*dsl = bp->b_addr;
 47
 48	if (!xfs_has_crc(mp))
 49		return 0;
 50
 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;
 59
 60	return sizeof(struct xfs_dsymlink_hdr);
 61}
 62
 63/*
 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.
 67 */
 68bool
 69xfs_symlink_hdr_ok(
 70	xfs_ino_t		ino,
 71	uint32_t		offset,
 72	uint32_t		size,
 73	struct xfs_buf		*bp)
 74{
 75	struct xfs_dsymlink_hdr *dsl = bp->b_addr;
 76
 77	if (offset != be32_to_cpu(dsl->sl_offset))
 78		return false;
 79	if (size != be32_to_cpu(dsl->sl_bytes))
 80		return false;
 81	if (ino != be64_to_cpu(dsl->sl_owner))
 82		return false;
 83
 84	/* ok */
 85	return true;
 86}
 87
 88static xfs_failaddr_t
 89xfs_symlink_verify(
 90	struct xfs_buf		*bp)
 91{
 92	struct xfs_mount	*mp = bp->b_mount;
 93	struct xfs_dsymlink_hdr	*dsl = bp->b_addr;
 94
 95	if (!xfs_has_crc(mp))
 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;
110
111	return NULL;
112}
113
114static void
115xfs_symlink_read_verify(
116	struct xfs_buf	*bp)
117{
118	struct xfs_mount *mp = bp->b_mount;
119	xfs_failaddr_t	fa;
120
121	/* no verification of non-crc buffers */
122	if (!xfs_has_crc(mp))
123		return;
124
125	if (!xfs_buf_verify_cksum(bp, XFS_SYMLINK_CRC_OFF))
126		xfs_verifier_error(bp, -EFSBADCRC, __this_address);
127	else {
128		fa = xfs_symlink_verify(bp);
129		if (fa)
130			xfs_verifier_error(bp, -EFSCORRUPTED, fa);
131	}
132}
133
134static void
135xfs_symlink_write_verify(
136	struct xfs_buf	*bp)
137{
138	struct xfs_mount *mp = bp->b_mount;
139	struct xfs_buf_log_item	*bip = bp->b_log_item;
140	xfs_failaddr_t		fa;
141
142	/* no verification of non-crc buffers */
143	if (!xfs_has_crc(mp))
144		return;
145
146	fa = xfs_symlink_verify(bp);
147	if (fa) {
148		xfs_verifier_error(bp, -EFSCORRUPTED, fa);
149		return;
150	}
151
152	if (bip) {
153		struct xfs_dsymlink_hdr *dsl = bp->b_addr;
154		dsl->sl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
155	}
156	xfs_buf_update_cksum(bp, XFS_SYMLINK_CRC_OFF);
157}
158
159const 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,
165};
166
167void
168xfs_symlink_local_to_remote(
169	struct xfs_trans	*tp,
170	struct xfs_buf		*bp,
171	struct xfs_inode	*ip,
172	struct xfs_ifork	*ifp)
173{
174	struct xfs_mount	*mp = ip->i_mount;
175	char			*buf;
176
177	xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SYMLINK_BUF);
178
179	if (!xfs_has_crc(mp)) {
180		bp->b_ops = NULL;
181		memcpy(bp->b_addr, ifp->if_data, ifp->if_bytes);
182		xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
183		return;
184	}
185
186	/*
187	 * As this symlink fits in an inode literal area, it must also fit in
188	 * the smallest buffer the filesystem supports.
189	 */
190	ASSERT(BBTOB(bp->b_length) >=
191			ifp->if_bytes + sizeof(struct xfs_dsymlink_hdr));
192
193	bp->b_ops = &xfs_symlink_buf_ops;
194
195	buf = bp->b_addr;
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) +
199					ifp->if_bytes - 1);
200}
201
202/*
203 * Verify the in-memory consistency of an inline symlink data fork. This
204 * does not do on-disk format checks.
205 */
206xfs_failaddr_t
207xfs_symlink_shortform_verify(
208	void			*sfp,
209	int64_t			size)
210{
 
 
 
211	char			*endp = sfp + size;
212
 
 
213	/*
214	 * Zero length symlinks should never occur in memory as they are
215	 * never allowed to exist on disk.
216	 */
217	if (!size)
218		return __this_address;
219
220	/* No negative sizes or overly long symlink targets. */
221	if (size < 0 || size > XFS_SYMLINK_MAXLEN)
222		return __this_address;
223
224	/* No NULLs in the target either. */
225	if (memchr(sfp, 0, size - 1))
226		return __this_address;
227
228	/* We /did/ null-terminate the buffer, right? */
229	if (*endp != 0)
230		return __this_address;
231	return NULL;
232}
233
234/* Read a remote symlink target into the buffer. */
235int
236xfs_symlink_remote_read(
237	struct xfs_inode	*ip,
238	char			*link)
239{
240	struct xfs_mount	*mp = ip->i_mount;
241	struct xfs_bmbt_irec	mval[XFS_SYMLINK_MAPS];
242	struct xfs_buf		*bp;
243	xfs_daddr_t		d;
244	char			*cur_chunk;
245	int			pathlen = ip->i_disk_size;
246	int			nmaps = XFS_SYMLINK_MAPS;
247	int			byte_cnt;
248	int			n;
249	int			error = 0;
250	int			fsblocks = 0;
251	int			offset;
252
253	xfs_assert_ilocked(ip, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL);
254
255	fsblocks = xfs_symlink_blocks(mp, pathlen);
256	error = xfs_bmapi_read(ip, 0, fsblocks, mval, &nmaps, 0);
257	if (error)
258		goto out;
259
260	offset = 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);
264
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);
269		if (error)
270			return error;
271		byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt);
272		if (pathlen < byte_cnt)
273			byte_cnt = pathlen;
274
275		cur_chunk = bp->b_addr;
276		if (xfs_has_crc(mp)) {
277			if (!xfs_symlink_hdr_ok(ip->i_ino, offset,
278							byte_cnt, bp)) {
279				xfs_inode_mark_sick(ip, XFS_SICK_INO_SYMLINK);
280				error = -EFSCORRUPTED;
281				xfs_alert(mp,
282"symlink header does not match required off/len/owner (0x%x/0x%x,0x%llx)",
283					offset, byte_cnt, ip->i_ino);
284				xfs_buf_relse(bp);
285				goto out;
286
287			}
288
289			cur_chunk += sizeof(struct xfs_dsymlink_hdr);
290		}
291
292		memcpy(link + offset, cur_chunk, byte_cnt);
293
294		pathlen -= byte_cnt;
295		offset += byte_cnt;
296
297		xfs_buf_relse(bp);
298	}
299	ASSERT(pathlen == 0);
300
301	link[ip->i_disk_size] = '\0';
302	error = 0;
303
304 out:
305	return error;
306}
307
308/* Write the symlink target into the inode. */
309int
310xfs_symlink_write_target(
311	struct xfs_trans	*tp,
312	struct xfs_inode	*ip,
313	const char		*target_path,
314	int			pathlen,
315	xfs_fsblock_t		fs_blocks,
316	uint			resblks)
317{
318	struct xfs_bmbt_irec	mval[XFS_SYMLINK_MAPS];
319	struct xfs_mount	*mp = tp->t_mountp;
320	const char		*cur_chunk;
321	struct xfs_buf		*bp;
322	xfs_daddr_t		d;
323	int			byte_cnt;
324	int			nmaps;
325	int			offset = 0;
326	int			n;
327	int			error;
328
329	/*
330	 * If the symlink will fit into the inode, write it inline.
331	 */
332	if (pathlen <= xfs_inode_data_fork_size(ip)) {
333		xfs_init_local_fork(ip, XFS_DATA_FORK, target_path, pathlen);
334
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);
338		return 0;
339	}
340
341	nmaps = XFS_SYMLINK_MAPS;
342	error = xfs_bmapi_write(tp, ip, 0, fs_blocks, XFS_BMAPI_METADATA,
343			resblks, mval, &nmaps);
344	if (error)
345		return error;
346
347	ip->i_disk_size = pathlen;
348	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
349
350	cur_chunk = target_path;
351	offset = 0;
352	for (n = 0; n < nmaps; n++) {
353		char	*buf;
354
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);
359		if (error)
360			return error;
361		bp->b_ops = &xfs_symlink_buf_ops;
362
363		byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt);
364		byte_cnt = min(byte_cnt, pathlen);
365
366		buf = bp->b_addr;
367		buf += xfs_symlink_hdr_set(mp, ip->i_ino, offset, byte_cnt,
368				bp);
369
370		memcpy(buf, cur_chunk, byte_cnt);
371
372		cur_chunk += byte_cnt;
373		pathlen -= byte_cnt;
374		offset += byte_cnt;
375
376		xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SYMLINK_BUF);
377		xfs_trans_log_buf(tp, bp, 0, (buf + byte_cnt - 1) -
378						(char *)bp->b_addr);
379	}
380	ASSERT(pathlen == 0);
381	return 0;
382}
v5.14.15
  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_inode.h"
 15#include "xfs_error.h"
 16#include "xfs_trans.h"
 17#include "xfs_buf_item.h"
 18#include "xfs_log.h"
 19
 
 
 
 20
 21/*
 22 * Each contiguous block has a header, so it is not just a simple pathlen
 23 * to FSB conversion.
 24 */
 25int
 26xfs_symlink_blocks(
 27	struct xfs_mount *mp,
 28	int		pathlen)
 29{
 30	int buflen = XFS_SYMLINK_BUF_SPACE(mp, mp->m_sb.sb_blocksize);
 31
 32	return (pathlen + buflen - 1) / buflen;
 33}
 34
 35int
 36xfs_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
 45	if (!xfs_sb_version_hascrc(&mp->m_sb))
 46		return 0;
 47
 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(bp->b_bn);
 55	bp->b_ops = &xfs_symlink_buf_ops;
 56
 57	return sizeof(struct xfs_dsymlink_hdr);
 58}
 59
 60/*
 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.
 64 */
 65bool
 66xfs_symlink_hdr_ok(
 67	xfs_ino_t		ino,
 68	uint32_t		offset,
 69	uint32_t		size,
 70	struct xfs_buf		*bp)
 71{
 72	struct xfs_dsymlink_hdr *dsl = bp->b_addr;
 73
 74	if (offset != be32_to_cpu(dsl->sl_offset))
 75		return false;
 76	if (size != be32_to_cpu(dsl->sl_bytes))
 77		return false;
 78	if (ino != be64_to_cpu(dsl->sl_owner))
 79		return false;
 80
 81	/* ok */
 82	return true;
 83}
 84
 85static xfs_failaddr_t
 86xfs_symlink_verify(
 87	struct xfs_buf		*bp)
 88{
 89	struct xfs_mount	*mp = bp->b_mount;
 90	struct xfs_dsymlink_hdr	*dsl = bp->b_addr;
 91
 92	if (!xfs_sb_version_hascrc(&mp->m_sb))
 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 (bp->b_bn != 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;
107
108	return NULL;
109}
110
111static void
112xfs_symlink_read_verify(
113	struct xfs_buf	*bp)
114{
115	struct xfs_mount *mp = bp->b_mount;
116	xfs_failaddr_t	fa;
117
118	/* no verification of non-crc buffers */
119	if (!xfs_sb_version_hascrc(&mp->m_sb))
120		return;
121
122	if (!xfs_buf_verify_cksum(bp, XFS_SYMLINK_CRC_OFF))
123		xfs_verifier_error(bp, -EFSBADCRC, __this_address);
124	else {
125		fa = xfs_symlink_verify(bp);
126		if (fa)
127			xfs_verifier_error(bp, -EFSCORRUPTED, fa);
128	}
129}
130
131static void
132xfs_symlink_write_verify(
133	struct xfs_buf	*bp)
134{
135	struct xfs_mount *mp = bp->b_mount;
136	struct xfs_buf_log_item	*bip = bp->b_log_item;
137	xfs_failaddr_t		fa;
138
139	/* no verification of non-crc buffers */
140	if (!xfs_sb_version_hascrc(&mp->m_sb))
141		return;
142
143	fa = xfs_symlink_verify(bp);
144	if (fa) {
145		xfs_verifier_error(bp, -EFSCORRUPTED, fa);
146		return;
147	}
148
149	if (bip) {
150		struct xfs_dsymlink_hdr *dsl = bp->b_addr;
151		dsl->sl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
152	}
153	xfs_buf_update_cksum(bp, XFS_SYMLINK_CRC_OFF);
154}
155
156const 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,
162};
163
164void
165xfs_symlink_local_to_remote(
166	struct xfs_trans	*tp,
167	struct xfs_buf		*bp,
168	struct xfs_inode	*ip,
169	struct xfs_ifork	*ifp)
170{
171	struct xfs_mount	*mp = ip->i_mount;
172	char			*buf;
173
174	xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SYMLINK_BUF);
175
176	if (!xfs_sb_version_hascrc(&mp->m_sb)) {
177		bp->b_ops = NULL;
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);
180		return;
181	}
182
183	/*
184	 * As this symlink fits in an inode literal area, it must also fit in
185	 * the smallest buffer the filesystem supports.
186	 */
187	ASSERT(BBTOB(bp->b_length) >=
188			ifp->if_bytes + sizeof(struct xfs_dsymlink_hdr));
189
190	bp->b_ops = &xfs_symlink_buf_ops;
191
192	buf = bp->b_addr;
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) +
196					ifp->if_bytes - 1);
197}
198
199/*
200 * Verify the in-memory consistency of an inline symlink data fork. This
201 * does not do on-disk format checks.
202 */
203xfs_failaddr_t
204xfs_symlink_shortform_verify(
205	struct xfs_inode	*ip)
 
206{
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;
211
212	ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
213
214	/*
215	 * Zero length symlinks should never occur in memory as they are
216	 * never alllowed to exist on disk.
217	 */
218	if (!size)
219		return __this_address;
220
221	/* No negative sizes or overly long symlink targets. */
222	if (size < 0 || size > XFS_SYMLINK_MAXLEN)
223		return __this_address;
224
225	/* No NULLs in the target either. */
226	if (memchr(sfp, 0, size - 1))
227		return __this_address;
228
229	/* We /did/ null-terminate the buffer, right? */
230	if (*endp != 0)
231		return __this_address;
232	return NULL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
233}