blob: 5497014f52930eafca3abed8144f8654ec6f5642 [file] [log] [blame]
Dave Chinner1fb7e48d2013-08-12 20:49:40 +10001/*
2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
3 * Copyright (c) 2012-2013 Red Hat, Inc.
4 * All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19#include "xfs.h"
20#include "xfs_fs.h"
21#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110022#include "xfs_log_format.h"
Dave Chinner70a98832013-10-23 10:36:05 +110023#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110024#include "xfs_trans_resv.h"
Dave Chinner1fb7e48d2013-08-12 20:49:40 +100025#include "xfs_mount.h"
26#include "xfs_bmap_btree.h"
27#include "xfs_inode.h"
28#include "xfs_error.h"
29#include "xfs_trace.h"
30#include "xfs_symlink.h"
31#include "xfs_cksum.h"
Dave Chinner239880e2013-10-23 10:50:10 +110032#include "xfs_trans.h"
Dave Chinner1fb7e48d2013-08-12 20:49:40 +100033#include "xfs_buf_item.h"
Brian Fostera45086e2015-10-12 15:59:25 +110034#include "xfs_log.h"
Dave Chinner1fb7e48d2013-08-12 20:49:40 +100035
36
37/*
38 * Each contiguous block has a header, so it is not just a simple pathlen
39 * to FSB conversion.
40 */
41int
42xfs_symlink_blocks(
43 struct xfs_mount *mp,
44 int pathlen)
45{
46 int buflen = XFS_SYMLINK_BUF_SPACE(mp, mp->m_sb.sb_blocksize);
47
48 return (pathlen + buflen - 1) / buflen;
49}
50
51int
52xfs_symlink_hdr_set(
53 struct xfs_mount *mp,
54 xfs_ino_t ino,
55 uint32_t offset,
56 uint32_t size,
57 struct xfs_buf *bp)
58{
59 struct xfs_dsymlink_hdr *dsl = bp->b_addr;
60
61 if (!xfs_sb_version_hascrc(&mp->m_sb))
62 return 0;
63
Brian Fostera45086e2015-10-12 15:59:25 +110064 memset(dsl, 0, sizeof(struct xfs_dsymlink_hdr));
Dave Chinner1fb7e48d2013-08-12 20:49:40 +100065 dsl->sl_magic = cpu_to_be32(XFS_SYMLINK_MAGIC);
66 dsl->sl_offset = cpu_to_be32(offset);
67 dsl->sl_bytes = cpu_to_be32(size);
Eric Sandeence748ea2015-07-29 11:53:31 +100068 uuid_copy(&dsl->sl_uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinner1fb7e48d2013-08-12 20:49:40 +100069 dsl->sl_owner = cpu_to_be64(ino);
70 dsl->sl_blkno = cpu_to_be64(bp->b_bn);
71 bp->b_ops = &xfs_symlink_buf_ops;
72
73 return sizeof(struct xfs_dsymlink_hdr);
74}
75
76/*
77 * Checking of the symlink header is split into two parts. the verifier does
78 * CRC, location and bounds checking, the unpacking function checks the path
79 * parameters and owner.
80 */
81bool
82xfs_symlink_hdr_ok(
Dave Chinner1fb7e48d2013-08-12 20:49:40 +100083 xfs_ino_t ino,
84 uint32_t offset,
85 uint32_t size,
86 struct xfs_buf *bp)
87{
88 struct xfs_dsymlink_hdr *dsl = bp->b_addr;
89
90 if (offset != be32_to_cpu(dsl->sl_offset))
91 return false;
92 if (size != be32_to_cpu(dsl->sl_bytes))
93 return false;
94 if (ino != be64_to_cpu(dsl->sl_owner))
95 return false;
96
97 /* ok */
98 return true;
99}
100
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800101static xfs_failaddr_t
Dave Chinner1fb7e48d2013-08-12 20:49:40 +1000102xfs_symlink_verify(
103 struct xfs_buf *bp)
104{
105 struct xfs_mount *mp = bp->b_target->bt_mount;
106 struct xfs_dsymlink_hdr *dsl = bp->b_addr;
107
108 if (!xfs_sb_version_hascrc(&mp->m_sb))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800109 return __this_address;
Dave Chinner1fb7e48d2013-08-12 20:49:40 +1000110 if (dsl->sl_magic != cpu_to_be32(XFS_SYMLINK_MAGIC))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800111 return __this_address;
Eric Sandeence748ea2015-07-29 11:53:31 +1000112 if (!uuid_equal(&dsl->sl_uuid, &mp->m_sb.sb_meta_uuid))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800113 return __this_address;
Dave Chinner1fb7e48d2013-08-12 20:49:40 +1000114 if (bp->b_bn != be64_to_cpu(dsl->sl_blkno))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800115 return __this_address;
Dave Chinner1fb7e48d2013-08-12 20:49:40 +1000116 if (be32_to_cpu(dsl->sl_offset) +
Darrick J. Wong6eb0b8d2017-07-07 08:37:26 -0700117 be32_to_cpu(dsl->sl_bytes) >= XFS_SYMLINK_MAXLEN)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800118 return __this_address;
Dave Chinner1fb7e48d2013-08-12 20:49:40 +1000119 if (dsl->sl_owner == 0)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800120 return __this_address;
Brian Fostera45086e2015-10-12 15:59:25 +1100121 if (!xfs_log_check_lsn(mp, be64_to_cpu(dsl->sl_lsn)))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800122 return __this_address;
Dave Chinner1fb7e48d2013-08-12 20:49:40 +1000123
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800124 return NULL;
Dave Chinner1fb7e48d2013-08-12 20:49:40 +1000125}
126
127static void
128xfs_symlink_read_verify(
129 struct xfs_buf *bp)
130{
131 struct xfs_mount *mp = bp->b_target->bt_mount;
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800132 xfs_failaddr_t fa;
Dave Chinner1fb7e48d2013-08-12 20:49:40 +1000133
134 /* no verification of non-crc buffers */
135 if (!xfs_sb_version_hascrc(&mp->m_sb))
136 return;
137
Eric Sandeence5028c2014-02-27 15:23:10 +1100138 if (!xfs_buf_verify_cksum(bp, XFS_SYMLINK_CRC_OFF))
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800139 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
140 else {
141 fa = xfs_symlink_verify(bp);
142 if (fa)
143 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
144 }
Dave Chinner1fb7e48d2013-08-12 20:49:40 +1000145}
146
147static void
148xfs_symlink_write_verify(
149 struct xfs_buf *bp)
150{
151 struct xfs_mount *mp = bp->b_target->bt_mount;
152 struct xfs_buf_log_item *bip = bp->b_fspriv;
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800153 xfs_failaddr_t fa;
Dave Chinner1fb7e48d2013-08-12 20:49:40 +1000154
155 /* no verification of non-crc buffers */
156 if (!xfs_sb_version_hascrc(&mp->m_sb))
157 return;
158
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800159 fa = xfs_symlink_verify(bp);
160 if (fa) {
161 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
Dave Chinner1fb7e48d2013-08-12 20:49:40 +1000162 return;
163 }
164
165 if (bip) {
166 struct xfs_dsymlink_hdr *dsl = bp->b_addr;
167 dsl->sl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
168 }
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100169 xfs_buf_update_cksum(bp, XFS_SYMLINK_CRC_OFF);
Dave Chinner1fb7e48d2013-08-12 20:49:40 +1000170}
171
172const struct xfs_buf_ops xfs_symlink_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100173 .name = "xfs_symlink",
Dave Chinner1fb7e48d2013-08-12 20:49:40 +1000174 .verify_read = xfs_symlink_read_verify,
175 .verify_write = xfs_symlink_write_verify,
176};
177
178void
179xfs_symlink_local_to_remote(
180 struct xfs_trans *tp,
181 struct xfs_buf *bp,
182 struct xfs_inode *ip,
183 struct xfs_ifork *ifp)
184{
185 struct xfs_mount *mp = ip->i_mount;
186 char *buf;
187
Dave Chinnerfe22d552015-01-22 09:30:06 +1100188 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SYMLINK_BUF);
189
Dave Chinner1fb7e48d2013-08-12 20:49:40 +1000190 if (!xfs_sb_version_hascrc(&mp->m_sb)) {
191 bp->b_ops = NULL;
192 memcpy(bp->b_addr, ifp->if_u1.if_data, ifp->if_bytes);
Brian Fosterb7cdc662015-10-12 15:40:24 +1100193 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
Dave Chinner1fb7e48d2013-08-12 20:49:40 +1000194 return;
195 }
196
197 /*
198 * As this symlink fits in an inode literal area, it must also fit in
199 * the smallest buffer the filesystem supports.
200 */
201 ASSERT(BBTOB(bp->b_length) >=
202 ifp->if_bytes + sizeof(struct xfs_dsymlink_hdr));
203
204 bp->b_ops = &xfs_symlink_buf_ops;
205
206 buf = bp->b_addr;
207 buf += xfs_symlink_hdr_set(mp, ip->i_ino, 0, ifp->if_bytes, bp);
208 memcpy(buf, ifp->if_u1.if_data, ifp->if_bytes);
Brian Fosterb7cdc662015-10-12 15:40:24 +1100209 xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsymlink_hdr) +
210 ifp->if_bytes - 1);
Dave Chinner1fb7e48d2013-08-12 20:49:40 +1000211}