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 */
28 int
xfs_symlink_blocks(struct xfs_mount * mp,int pathlen)29 xfs_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
38 int
xfs_symlink_hdr_set(struct xfs_mount * mp,xfs_ino_t ino,uint32_t offset,uint32_t size,struct xfs_buf * bp)39 xfs_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 */
68 bool
xfs_symlink_hdr_ok(xfs_ino_t ino,uint32_t offset,uint32_t size,struct xfs_buf * bp)69 xfs_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
88 static xfs_failaddr_t
xfs_symlink_verify(struct xfs_buf * bp)89 xfs_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 /* no verification of non-crc buffers */
96 if (!xfs_has_crc(mp))
97 return NULL;
98
99 if (!xfs_verify_magic(bp, dsl->sl_magic))
100 return __this_address;
101 if (!uuid_equal(&dsl->sl_uuid, &mp->m_sb.sb_meta_uuid))
102 return __this_address;
103 if (xfs_buf_daddr(bp) != be64_to_cpu(dsl->sl_blkno))
104 return __this_address;
105 if (be32_to_cpu(dsl->sl_offset) +
106 be32_to_cpu(dsl->sl_bytes) >= XFS_SYMLINK_MAXLEN)
107 return __this_address;
108 if (dsl->sl_owner == 0)
109 return __this_address;
110 if (!xfs_log_check_lsn(mp, be64_to_cpu(dsl->sl_lsn)))
111 return __this_address;
112
113 return NULL;
114 }
115
116 static void
xfs_symlink_read_verify(struct xfs_buf * bp)117 xfs_symlink_read_verify(
118 struct xfs_buf *bp)
119 {
120 struct xfs_mount *mp = bp->b_mount;
121 xfs_failaddr_t fa;
122
123 /* no verification of non-crc buffers */
124 if (!xfs_has_crc(mp))
125 return;
126
127 if (!xfs_buf_verify_cksum(bp, XFS_SYMLINK_CRC_OFF))
128 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
129 else {
130 fa = xfs_symlink_verify(bp);
131 if (fa)
132 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
133 }
134 }
135
136 static void
xfs_symlink_write_verify(struct xfs_buf * bp)137 xfs_symlink_write_verify(
138 struct xfs_buf *bp)
139 {
140 struct xfs_mount *mp = bp->b_mount;
141 struct xfs_buf_log_item *bip = bp->b_log_item;
142 xfs_failaddr_t fa;
143
144 /* no verification of non-crc buffers */
145 if (!xfs_has_crc(mp))
146 return;
147
148 fa = xfs_symlink_verify(bp);
149 if (fa) {
150 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
151 return;
152 }
153
154 if (bip) {
155 struct xfs_dsymlink_hdr *dsl = bp->b_addr;
156 dsl->sl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
157 }
158 xfs_buf_update_cksum(bp, XFS_SYMLINK_CRC_OFF);
159 }
160
161 const struct xfs_buf_ops xfs_symlink_buf_ops = {
162 .name = "xfs_symlink",
163 .magic = { 0, cpu_to_be32(XFS_SYMLINK_MAGIC) },
164 .verify_read = xfs_symlink_read_verify,
165 .verify_write = xfs_symlink_write_verify,
166 .verify_struct = xfs_symlink_verify,
167 };
168
169 void
xfs_symlink_local_to_remote(struct xfs_trans * tp,struct xfs_buf * bp,struct xfs_inode * ip,struct xfs_ifork * ifp,void * priv)170 xfs_symlink_local_to_remote(
171 struct xfs_trans *tp,
172 struct xfs_buf *bp,
173 struct xfs_inode *ip,
174 struct xfs_ifork *ifp,
175 void *priv)
176 {
177 struct xfs_mount *mp = ip->i_mount;
178 char *buf;
179
180 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SYMLINK_BUF);
181
182 if (!xfs_has_crc(mp)) {
183 bp->b_ops = NULL;
184 memcpy(bp->b_addr, ifp->if_data, ifp->if_bytes);
185 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
186 return;
187 }
188
189 /*
190 * As this symlink fits in an inode literal area, it must also fit in
191 * the smallest buffer the filesystem supports.
192 */
193 ASSERT(BBTOB(bp->b_length) >=
194 ifp->if_bytes + sizeof(struct xfs_dsymlink_hdr));
195
196 bp->b_ops = &xfs_symlink_buf_ops;
197
198 buf = bp->b_addr;
199 buf += xfs_symlink_hdr_set(mp, ip->i_ino, 0, ifp->if_bytes, bp);
200 memcpy(buf, ifp->if_data, ifp->if_bytes);
201 xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsymlink_hdr) +
202 ifp->if_bytes - 1);
203 }
204
205 /*
206 * Verify the in-memory consistency of an inline symlink data fork. This
207 * does not do on-disk format checks.
208 */
209 xfs_failaddr_t
xfs_symlink_shortform_verify(void * sfp,int64_t size)210 xfs_symlink_shortform_verify(
211 void *sfp,
212 int64_t size)
213 {
214 char *endp = sfp + size;
215
216 /*
217 * Zero length symlinks should never occur in memory as they are
218 * never allowed to exist on disk.
219 */
220 if (!size)
221 return __this_address;
222
223 /* No negative sizes or overly long symlink targets. */
224 if (size < 0 || size > XFS_SYMLINK_MAXLEN)
225 return __this_address;
226
227 /* No NULLs in the target either. */
228 if (memchr(sfp, 0, size - 1))
229 return __this_address;
230
231 /* We /did/ null-terminate the buffer, right? */
232 if (*endp != 0)
233 return __this_address;
234 return NULL;
235 }
236
237 /* Read a remote symlink target into the buffer. */
238 int
xfs_symlink_remote_read(struct xfs_inode * ip,char * link)239 xfs_symlink_remote_read(
240 struct xfs_inode *ip,
241 char *link)
242 {
243 struct xfs_mount *mp = ip->i_mount;
244 struct xfs_bmbt_irec mval[XFS_SYMLINK_MAPS];
245 struct xfs_buf *bp;
246 xfs_daddr_t d;
247 char *cur_chunk;
248 int pathlen = ip->i_disk_size;
249 int nmaps = XFS_SYMLINK_MAPS;
250 int byte_cnt;
251 int n;
252 int error = 0;
253 int fsblocks = 0;
254 int offset;
255
256 xfs_assert_ilocked(ip, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL);
257
258 fsblocks = xfs_symlink_blocks(mp, pathlen);
259 error = xfs_bmapi_read(ip, 0, fsblocks, mval, &nmaps, 0);
260 if (error)
261 goto out;
262
263 offset = 0;
264 for (n = 0; n < nmaps; n++) {
265 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
266 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
267
268 error = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0,
269 &bp, &xfs_symlink_buf_ops);
270 if (xfs_metadata_is_sick(error))
271 xfs_inode_mark_sick(ip, XFS_SICK_INO_SYMLINK);
272 if (error)
273 return error;
274 byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt);
275 if (pathlen < byte_cnt)
276 byte_cnt = pathlen;
277
278 cur_chunk = bp->b_addr;
279 if (xfs_has_crc(mp)) {
280 if (!xfs_symlink_hdr_ok(ip->i_ino, offset,
281 byte_cnt, bp)) {
282 xfs_inode_mark_sick(ip, XFS_SICK_INO_SYMLINK);
283 error = -EFSCORRUPTED;
284 xfs_alert(mp,
285 "symlink header does not match required off/len/owner (0x%x/0x%x,0x%llx)",
286 offset, byte_cnt, ip->i_ino);
287 xfs_buf_relse(bp);
288 goto out;
289
290 }
291
292 cur_chunk += sizeof(struct xfs_dsymlink_hdr);
293 }
294
295 memcpy(link + offset, cur_chunk, byte_cnt);
296
297 pathlen -= byte_cnt;
298 offset += byte_cnt;
299
300 xfs_buf_relse(bp);
301 }
302 ASSERT(pathlen == 0);
303
304 link[ip->i_disk_size] = '\0';
305 error = 0;
306
307 out:
308 return error;
309 }
310
311 /* Write the symlink target into the inode. */
312 int
xfs_symlink_write_target(struct xfs_trans * tp,struct xfs_inode * ip,xfs_ino_t owner,const char * target_path,int pathlen,xfs_fsblock_t fs_blocks,uint resblks)313 xfs_symlink_write_target(
314 struct xfs_trans *tp,
315 struct xfs_inode *ip,
316 xfs_ino_t owner,
317 const char *target_path,
318 int pathlen,
319 xfs_fsblock_t fs_blocks,
320 uint resblks)
321 {
322 struct xfs_bmbt_irec mval[XFS_SYMLINK_MAPS];
323 struct xfs_mount *mp = tp->t_mountp;
324 const char *cur_chunk;
325 struct xfs_buf *bp;
326 xfs_daddr_t d;
327 int byte_cnt;
328 int nmaps;
329 int offset = 0;
330 int n;
331 int error;
332
333 /*
334 * If the symlink will fit into the inode, write it inline.
335 */
336 if (pathlen <= xfs_inode_data_fork_size(ip)) {
337 xfs_init_local_fork(ip, XFS_DATA_FORK, target_path, pathlen);
338
339 ip->i_disk_size = pathlen;
340 ip->i_df.if_format = XFS_DINODE_FMT_LOCAL;
341 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
342 return 0;
343 }
344
345 nmaps = XFS_SYMLINK_MAPS;
346 error = xfs_bmapi_write(tp, ip, 0, fs_blocks, XFS_BMAPI_METADATA,
347 resblks, mval, &nmaps);
348 if (error)
349 return error;
350
351 ip->i_disk_size = pathlen;
352 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
353
354 cur_chunk = target_path;
355 offset = 0;
356 for (n = 0; n < nmaps; n++) {
357 char *buf;
358
359 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
360 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
361 error = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
362 BTOBB(byte_cnt), 0, &bp);
363 if (error)
364 return error;
365 bp->b_ops = &xfs_symlink_buf_ops;
366
367 byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt);
368 byte_cnt = min(byte_cnt, pathlen);
369
370 buf = bp->b_addr;
371 buf += xfs_symlink_hdr_set(mp, owner, offset, byte_cnt, bp);
372
373 memcpy(buf, cur_chunk, byte_cnt);
374
375 cur_chunk += byte_cnt;
376 pathlen -= byte_cnt;
377 offset += byte_cnt;
378
379 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SYMLINK_BUF);
380 xfs_trans_log_buf(tp, bp, 0, (buf + byte_cnt - 1) -
381 (char *)bp->b_addr);
382 }
383 ASSERT(pathlen == 0);
384 return 0;
385 }
386
387 /* Remove all the blocks from a symlink and invalidate buffers. */
388 int
xfs_symlink_remote_truncate(struct xfs_trans * tp,struct xfs_inode * ip)389 xfs_symlink_remote_truncate(
390 struct xfs_trans *tp,
391 struct xfs_inode *ip)
392 {
393 struct xfs_bmbt_irec mval[XFS_SYMLINK_MAPS];
394 struct xfs_mount *mp = tp->t_mountp;
395 struct xfs_buf *bp;
396 int nmaps = XFS_SYMLINK_MAPS;
397 int done = 0;
398 int i;
399 int error;
400
401 /* Read mappings and invalidate buffers. */
402 error = xfs_bmapi_read(ip, 0, XFS_MAX_FILEOFF, mval, &nmaps, 0);
403 if (error)
404 return error;
405
406 for (i = 0; i < nmaps; i++) {
407 if (!xfs_bmap_is_real_extent(&mval[i]))
408 break;
409
410 error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
411 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
412 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0,
413 &bp);
414 if (error)
415 return error;
416
417 xfs_trans_binval(tp, bp);
418 }
419
420 /* Unmap the remote blocks. */
421 error = xfs_bunmapi(tp, ip, 0, XFS_MAX_FILEOFF, 0, nmaps, &done);
422 if (error)
423 return error;
424 if (!done) {
425 ASSERT(done);
426 xfs_inode_mark_sick(ip, XFS_SICK_INO_SYMLINK);
427 return -EFSCORRUPTED;
428 }
429
430 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
431 return 0;
432 }
433