]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/xfs_symlink_remote.c
xfs: split out the remote symlink handling
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_symlink_remote.c
1 /*
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
21 /*
22 * Each contiguous block has a header, so it is not just a simple pathlen
23 * to FSB conversion.
24 */
25 int
26 xfs_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
35 int
36 xfs_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 dsl->sl_magic = cpu_to_be32(XFS_SYMLINK_MAGIC);
49 dsl->sl_offset = cpu_to_be32(offset);
50 dsl->sl_bytes = cpu_to_be32(size);
51 uuid_copy(&dsl->sl_uuid, &mp->m_sb.sb_uuid);
52 dsl->sl_owner = cpu_to_be64(ino);
53 dsl->sl_blkno = cpu_to_be64(bp->b_bn);
54 bp->b_ops = &xfs_symlink_buf_ops;
55
56 return sizeof(struct xfs_dsymlink_hdr);
57 }
58
59 /*
60 * Checking of the symlink header is split into two parts. the verifier does
61 * CRC, location and bounds checking, the unpacking function checks the path
62 * parameters and owner.
63 */
64 bool
65 xfs_symlink_hdr_ok(
66 struct xfs_mount *mp,
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
85 static bool
86 xfs_symlink_verify(
87 struct xfs_buf *bp)
88 {
89 struct xfs_mount *mp = bp->b_target->bt_mount;
90 struct xfs_dsymlink_hdr *dsl = bp->b_addr;
91
92 if (!xfs_sb_version_hascrc(&mp->m_sb))
93 return false;
94 if (dsl->sl_magic != cpu_to_be32(XFS_SYMLINK_MAGIC))
95 return false;
96 if (!uuid_equal(&dsl->sl_uuid, &mp->m_sb.sb_uuid))
97 return false;
98 if (bp->b_bn != be64_to_cpu(dsl->sl_blkno))
99 return false;
100 if (be32_to_cpu(dsl->sl_offset) +
101 be32_to_cpu(dsl->sl_bytes) >= MAXPATHLEN)
102 return false;
103 if (dsl->sl_owner == 0)
104 return false;
105
106 return true;
107 }
108
109 static void
110 xfs_symlink_read_verify(
111 struct xfs_buf *bp)
112 {
113 struct xfs_mount *mp = bp->b_target->bt_mount;
114
115 /* no verification of non-crc buffers */
116 if (!xfs_sb_version_hascrc(&mp->m_sb))
117 return;
118
119 if (!xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
120 offsetof(struct xfs_dsymlink_hdr, sl_crc)) ||
121 !xfs_symlink_verify(bp)) {
122 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
123 xfs_buf_ioerror(bp, EFSCORRUPTED);
124 }
125 }
126
127 static void
128 xfs_symlink_write_verify(
129 struct xfs_buf *bp)
130 {
131 struct xfs_mount *mp = bp->b_target->bt_mount;
132 struct xfs_buf_log_item *bip = bp->b_fspriv;
133
134 /* no verification of non-crc buffers */
135 if (!xfs_sb_version_hascrc(&mp->m_sb))
136 return;
137
138 if (!xfs_symlink_verify(bp)) {
139 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
140 xfs_buf_ioerror(bp, EFSCORRUPTED);
141 return;
142 }
143
144 if (bip) {
145 struct xfs_dsymlink_hdr *dsl = bp->b_addr;
146 dsl->sl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
147 }
148 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length),
149 offsetof(struct xfs_dsymlink_hdr, sl_crc));
150 }
151
152 const struct xfs_buf_ops xfs_symlink_buf_ops = {
153 .verify_read = xfs_symlink_read_verify,
154 .verify_write = xfs_symlink_write_verify,
155 };
156
157 void
158 xfs_symlink_local_to_remote(
159 struct xfs_trans *tp,
160 struct xfs_buf *bp,
161 struct xfs_inode *ip,
162 struct xfs_ifork *ifp)
163 {
164 struct xfs_mount *mp = ip->i_mount;
165 char *buf;
166
167 if (!xfs_sb_version_hascrc(&mp->m_sb)) {
168 bp->b_ops = NULL;
169 memcpy(bp->b_addr, ifp->if_u1.if_data, ifp->if_bytes);
170 return;
171 }
172
173 /*
174 * As this symlink fits in an inode literal area, it must also fit in
175 * the smallest buffer the filesystem supports.
176 */
177 ASSERT(BBTOB(bp->b_length) >=
178 ifp->if_bytes + sizeof(struct xfs_dsymlink_hdr));
179
180 bp->b_ops = &xfs_symlink_buf_ops;
181
182 buf = bp->b_addr;
183 buf += xfs_symlink_hdr_set(mp, ip->i_ino, 0, ifp->if_bytes, bp);
184 memcpy(buf, ifp->if_u1.if_data, ifp->if_bytes);
185 }