]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/xfs_symlink_remote.c
libxfs: return the opened fd from libxfs_device_open
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_symlink_remote.c
CommitLineData
37b3b4d6 1// SPDX-License-Identifier: GPL-2.0
9f76c5b5 2/*
270b1db1
DC
3 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
4 * Copyright (c) 2012-2013 Red Hat, Inc.
9f76c5b5
DC
5 * All rights reserved.
6 */
9c799827 7#include "libxfs_priv.h"
b626fb59
DC
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"
b626fb59 14#include "xfs_inode.h"
b626fb59
DC
15#include "xfs_trans.h"
16
9f76c5b5
DC
17
18/*
19 * Each contiguous block has a header, so it is not just a simple pathlen
20 * to FSB conversion.
21 */
22int
23xfs_symlink_blocks(
24 struct xfs_mount *mp,
25 int pathlen)
26{
2019931a 27 int buflen = XFS_SYMLINK_BUF_SPACE(mp, mp->m_sb.sb_blocksize);
9f76c5b5 28
2019931a 29 return (pathlen + buflen - 1) / buflen;
9f76c5b5
DC
30}
31
f7b80291 32int
9f76c5b5
DC
33xfs_symlink_hdr_set(
34 struct xfs_mount *mp,
35 xfs_ino_t ino,
36 uint32_t offset,
37 uint32_t size,
38 struct xfs_buf *bp)
39{
40 struct xfs_dsymlink_hdr *dsl = bp->b_addr;
41
b16a427a 42 if (!xfs_has_crc(mp))
9f76c5b5
DC
43 return 0;
44
a65d8d29 45 memset(dsl, 0, sizeof(struct xfs_dsymlink_hdr));
9f76c5b5
DC
46 dsl->sl_magic = cpu_to_be32(XFS_SYMLINK_MAGIC);
47 dsl->sl_offset = cpu_to_be32(offset);
48 dsl->sl_bytes = cpu_to_be32(size);
9c4e12fb 49 uuid_copy(&dsl->sl_uuid, &mp->m_sb.sb_meta_uuid);
9f76c5b5 50 dsl->sl_owner = cpu_to_be64(ino);
f1208396 51 dsl->sl_blkno = cpu_to_be64(xfs_buf_daddr(bp));
9f76c5b5
DC
52 bp->b_ops = &xfs_symlink_buf_ops;
53
54 return sizeof(struct xfs_dsymlink_hdr);
55}
56
57/*
58 * Checking of the symlink header is split into two parts. the verifier does
59 * CRC, location and bounds checking, the unpacking function checks the path
60 * parameters and owner.
61 */
62bool
63xfs_symlink_hdr_ok(
9f76c5b5
DC
64 xfs_ino_t ino,
65 uint32_t offset,
66 uint32_t size,
67 struct xfs_buf *bp)
68{
69 struct xfs_dsymlink_hdr *dsl = bp->b_addr;
70
71 if (offset != be32_to_cpu(dsl->sl_offset))
72 return false;
73 if (size != be32_to_cpu(dsl->sl_bytes))
74 return false;
75 if (ino != be64_to_cpu(dsl->sl_owner))
76 return false;
77
78 /* ok */
79 return true;
9f76c5b5
DC
80}
81
bc01119d 82static xfs_failaddr_t
9f76c5b5
DC
83xfs_symlink_verify(
84 struct xfs_buf *bp)
85{
7861ef77 86 struct xfs_mount *mp = bp->b_mount;
9f76c5b5
DC
87 struct xfs_dsymlink_hdr *dsl = bp->b_addr;
88
b16a427a 89 if (!xfs_has_crc(mp))
bc01119d 90 return __this_address;
68dbe77f 91 if (!xfs_verify_magic(bp, dsl->sl_magic))
bc01119d 92 return __this_address;
9c4e12fb 93 if (!uuid_equal(&dsl->sl_uuid, &mp->m_sb.sb_meta_uuid))
bc01119d 94 return __this_address;
f1208396 95 if (xfs_buf_daddr(bp) != be64_to_cpu(dsl->sl_blkno))
bc01119d 96 return __this_address;
9f76c5b5 97 if (be32_to_cpu(dsl->sl_offset) +
b8fb8c95 98 be32_to_cpu(dsl->sl_bytes) >= XFS_SYMLINK_MAXLEN)
bc01119d 99 return __this_address;
9f76c5b5 100 if (dsl->sl_owner == 0)
bc01119d 101 return __this_address;
a65d8d29 102 if (!xfs_log_check_lsn(mp, be64_to_cpu(dsl->sl_lsn)))
bc01119d 103 return __this_address;
9f76c5b5 104
bc01119d 105 return NULL;
9f76c5b5
DC
106}
107
108static void
109xfs_symlink_read_verify(
110 struct xfs_buf *bp)
111{
7861ef77 112 struct xfs_mount *mp = bp->b_mount;
1e697959 113 xfs_failaddr_t fa;
9f76c5b5
DC
114
115 /* no verification of non-crc buffers */
b16a427a 116 if (!xfs_has_crc(mp))
9f76c5b5
DC
117 return;
118
45922933 119 if (!xfs_buf_verify_cksum(bp, XFS_SYMLINK_CRC_OFF))
1e697959
DW
120 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
121 else {
122 fa = xfs_symlink_verify(bp);
123 if (fa)
124 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
125 }
9f76c5b5
DC
126}
127
128static void
129xfs_symlink_write_verify(
130 struct xfs_buf *bp)
131{
7861ef77 132 struct xfs_mount *mp = bp->b_mount;
37d086ca 133 struct xfs_buf_log_item *bip = bp->b_log_item;
1e697959 134 xfs_failaddr_t fa;
9f76c5b5
DC
135
136 /* no verification of non-crc buffers */
b16a427a 137 if (!xfs_has_crc(mp))
9f76c5b5
DC
138 return;
139
1e697959
DW
140 fa = xfs_symlink_verify(bp);
141 if (fa) {
142 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
9f76c5b5
DC
143 return;
144 }
145
146 if (bip) {
147 struct xfs_dsymlink_hdr *dsl = bp->b_addr;
148 dsl->sl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
149 }
43b5aeed 150 xfs_buf_update_cksum(bp, XFS_SYMLINK_CRC_OFF);
9f76c5b5
DC
151}
152
153const struct xfs_buf_ops xfs_symlink_buf_ops = {
a3fac935 154 .name = "xfs_symlink",
68dbe77f 155 .magic = { 0, cpu_to_be32(XFS_SYMLINK_MAGIC) },
9f76c5b5
DC
156 .verify_read = xfs_symlink_read_verify,
157 .verify_write = xfs_symlink_write_verify,
95d9582b 158 .verify_struct = xfs_symlink_verify,
9f76c5b5
DC
159};
160
88b66843
DC
161void
162xfs_symlink_local_to_remote(
163 struct xfs_trans *tp,
164 struct xfs_buf *bp,
165 struct xfs_inode *ip,
166 struct xfs_ifork *ifp)
167{
168 struct xfs_mount *mp = ip->i_mount;
169 char *buf;
170
19ebedcf
DC
171 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SYMLINK_BUF);
172
b16a427a 173 if (!xfs_has_crc(mp)) {
88b66843
DC
174 bp->b_ops = NULL;
175 memcpy(bp->b_addr, ifp->if_u1.if_data, ifp->if_bytes);
f44fbde0 176 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
88b66843
DC
177 return;
178 }
179
180 /*
181 * As this symlink fits in an inode literal area, it must also fit in
182 * the smallest buffer the filesystem supports.
183 */
184 ASSERT(BBTOB(bp->b_length) >=
185 ifp->if_bytes + sizeof(struct xfs_dsymlink_hdr));
186
187 bp->b_ops = &xfs_symlink_buf_ops;
188
189 buf = bp->b_addr;
190 buf += xfs_symlink_hdr_set(mp, ip->i_ino, 0, ifp->if_bytes, bp);
191 memcpy(buf, ifp->if_u1.if_data, ifp->if_bytes);
f44fbde0
BF
192 xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsymlink_hdr) +
193 ifp->if_bytes - 1);
88b66843 194}
6db3a800 195
bf64a47a
DC
196/*
197 * Verify the in-memory consistency of an inline symlink data fork. This
198 * does not do on-disk format checks.
199 */
6db3a800
DW
200xfs_failaddr_t
201xfs_symlink_shortform_verify(
202 struct xfs_inode *ip)
203{
722e81c1 204 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, XFS_DATA_FORK);
d967a68d
CH
205 char *sfp = (char *)ifp->if_u1.if_data;
206 int size = ifp->if_bytes;
207 char *endp = sfp + size;
208
209 ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
6db3a800 210
bf64a47a
DC
211 /*
212 * Zero length symlinks should never occur in memory as they are
7e5dda22 213 * never allowed to exist on disk.
bf64a47a
DC
214 */
215 if (!size)
216 return __this_address;
6db3a800
DW
217
218 /* No negative sizes or overly long symlink targets. */
219 if (size < 0 || size > XFS_SYMLINK_MAXLEN)
220 return __this_address;
221
222 /* No NULLs in the target either. */
223 if (memchr(sfp, 0, size - 1))
224 return __this_address;
225
226 /* We /did/ null-terminate the buffer, right? */
227 if (*endp != 0)
228 return __this_address;
229 return NULL;
230}