blob: b9884aab46d32eb2e51c790357ea4a6c4bf6c193 [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"
34
35
36/*
37 * Each contiguous block has a header, so it is not just a simple pathlen
38 * to FSB conversion.
39 */
40int
41xfs_symlink_blocks(
42 struct xfs_mount *mp,
43 int pathlen)
44{
45 int buflen = XFS_SYMLINK_BUF_SPACE(mp, mp->m_sb.sb_blocksize);
46
47 return (pathlen + buflen - 1) / buflen;
48}
49
50int
51xfs_symlink_hdr_set(
52 struct xfs_mount *mp,
53 xfs_ino_t ino,
54 uint32_t offset,
55 uint32_t size,
56 struct xfs_buf *bp)
57{
58 struct xfs_dsymlink_hdr *dsl = bp->b_addr;
59
60 if (!xfs_sb_version_hascrc(&mp->m_sb))
61 return 0;
62
63 dsl->sl_magic = cpu_to_be32(XFS_SYMLINK_MAGIC);
64 dsl->sl_offset = cpu_to_be32(offset);
65 dsl->sl_bytes = cpu_to_be32(size);
Eric Sandeence748ea2015-07-29 11:53:31 +100066 uuid_copy(&dsl->sl_uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinner1fb7e48d2013-08-12 20:49:40 +100067 dsl->sl_owner = cpu_to_be64(ino);
68 dsl->sl_blkno = cpu_to_be64(bp->b_bn);
69 bp->b_ops = &xfs_symlink_buf_ops;
70
71 return sizeof(struct xfs_dsymlink_hdr);
72}
73
74/*
75 * Checking of the symlink header is split into two parts. the verifier does
76 * CRC, location and bounds checking, the unpacking function checks the path
77 * parameters and owner.
78 */
79bool
80xfs_symlink_hdr_ok(
Dave Chinner1fb7e48d2013-08-12 20:49:40 +100081 xfs_ino_t ino,
82 uint32_t offset,
83 uint32_t size,
84 struct xfs_buf *bp)
85{
86 struct xfs_dsymlink_hdr *dsl = bp->b_addr;
87
88 if (offset != be32_to_cpu(dsl->sl_offset))
89 return false;
90 if (size != be32_to_cpu(dsl->sl_bytes))
91 return false;
92 if (ino != be64_to_cpu(dsl->sl_owner))
93 return false;
94
95 /* ok */
96 return true;
97}
98
99static bool
100xfs_symlink_verify(
101 struct xfs_buf *bp)
102{
103 struct xfs_mount *mp = bp->b_target->bt_mount;
104 struct xfs_dsymlink_hdr *dsl = bp->b_addr;
105
106 if (!xfs_sb_version_hascrc(&mp->m_sb))
107 return false;
108 if (dsl->sl_magic != cpu_to_be32(XFS_SYMLINK_MAGIC))
109 return false;
Eric Sandeence748ea2015-07-29 11:53:31 +1000110 if (!uuid_equal(&dsl->sl_uuid, &mp->m_sb.sb_meta_uuid))
Dave Chinner1fb7e48d2013-08-12 20:49:40 +1000111 return false;
112 if (bp->b_bn != be64_to_cpu(dsl->sl_blkno))
113 return false;
114 if (be32_to_cpu(dsl->sl_offset) +
115 be32_to_cpu(dsl->sl_bytes) >= MAXPATHLEN)
116 return false;
117 if (dsl->sl_owner == 0)
118 return false;
119
120 return true;
121}
122
123static void
124xfs_symlink_read_verify(
125 struct xfs_buf *bp)
126{
127 struct xfs_mount *mp = bp->b_target->bt_mount;
128
129 /* no verification of non-crc buffers */
130 if (!xfs_sb_version_hascrc(&mp->m_sb))
131 return;
132
Eric Sandeence5028c2014-02-27 15:23:10 +1100133 if (!xfs_buf_verify_cksum(bp, XFS_SYMLINK_CRC_OFF))
Dave Chinner24513372014-06-25 14:58:08 +1000134 xfs_buf_ioerror(bp, -EFSBADCRC);
Eric Sandeence5028c2014-02-27 15:23:10 +1100135 else if (!xfs_symlink_verify(bp))
Dave Chinner24513372014-06-25 14:58:08 +1000136 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100137
138 if (bp->b_error)
139 xfs_verifier_error(bp);
Dave Chinner1fb7e48d2013-08-12 20:49:40 +1000140}
141
142static void
143xfs_symlink_write_verify(
144 struct xfs_buf *bp)
145{
146 struct xfs_mount *mp = bp->b_target->bt_mount;
147 struct xfs_buf_log_item *bip = bp->b_fspriv;
148
149 /* no verification of non-crc buffers */
150 if (!xfs_sb_version_hascrc(&mp->m_sb))
151 return;
152
153 if (!xfs_symlink_verify(bp)) {
Dave Chinner24513372014-06-25 14:58:08 +1000154 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100155 xfs_verifier_error(bp);
Dave Chinner1fb7e48d2013-08-12 20:49:40 +1000156 return;
157 }
158
159 if (bip) {
160 struct xfs_dsymlink_hdr *dsl = bp->b_addr;
161 dsl->sl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
162 }
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100163 xfs_buf_update_cksum(bp, XFS_SYMLINK_CRC_OFF);
Dave Chinner1fb7e48d2013-08-12 20:49:40 +1000164}
165
166const struct xfs_buf_ops xfs_symlink_buf_ops = {
167 .verify_read = xfs_symlink_read_verify,
168 .verify_write = xfs_symlink_write_verify,
169};
170
171void
172xfs_symlink_local_to_remote(
173 struct xfs_trans *tp,
174 struct xfs_buf *bp,
175 struct xfs_inode *ip,
176 struct xfs_ifork *ifp)
177{
178 struct xfs_mount *mp = ip->i_mount;
179 char *buf;
180
Dave Chinnerfe22d552015-01-22 09:30:06 +1100181 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SYMLINK_BUF);
182
Dave Chinner1fb7e48d2013-08-12 20:49:40 +1000183 if (!xfs_sb_version_hascrc(&mp->m_sb)) {
184 bp->b_ops = NULL;
185 memcpy(bp->b_addr, ifp->if_u1.if_data, ifp->if_bytes);
Brian Fosterb7cdc662015-10-12 15:40:24 +1100186 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
Dave Chinner1fb7e48d2013-08-12 20:49:40 +1000187 return;
188 }
189
190 /*
191 * As this symlink fits in an inode literal area, it must also fit in
192 * the smallest buffer the filesystem supports.
193 */
194 ASSERT(BBTOB(bp->b_length) >=
195 ifp->if_bytes + sizeof(struct xfs_dsymlink_hdr));
196
197 bp->b_ops = &xfs_symlink_buf_ops;
198
199 buf = bp->b_addr;
200 buf += xfs_symlink_hdr_set(mp, ip->i_ino, 0, ifp->if_bytes, bp);
201 memcpy(buf, ifp->if_u1.if_data, ifp->if_bytes);
Brian Fosterb7cdc662015-10-12 15:40:24 +1100202 xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsymlink_hdr) +
203 ifp->if_bytes - 1);
Dave Chinner1fb7e48d2013-08-12 20:49:40 +1000204}