]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/xfs_symlink_remote.c
Merge branch 'progs-misc-fixes-2' into for-next
[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 "libxfs_priv.h"
20 #include "xfs_fs.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_shared.h"
24 #include "xfs_trans_resv.h"
25 #include "xfs_mount.h"
26 #include "xfs_bmap_btree.h"
27 #include "xfs_inode.h"
28 #include "xfs_trace.h"
29 #include "xfs_cksum.h"
30 #include "xfs_trans.h"
31
32
33 /*
34 * Each contiguous block has a header, so it is not just a simple pathlen
35 * to FSB conversion.
36 */
37 int
38 xfs_symlink_blocks(
39 struct xfs_mount *mp,
40 int pathlen)
41 {
42 int buflen = XFS_SYMLINK_BUF_SPACE(mp, mp->m_sb.sb_blocksize);
43
44 return (pathlen + buflen - 1) / buflen;
45 }
46
47 int
48 xfs_symlink_hdr_set(
49 struct xfs_mount *mp,
50 xfs_ino_t ino,
51 uint32_t offset,
52 uint32_t size,
53 struct xfs_buf *bp)
54 {
55 struct xfs_dsymlink_hdr *dsl = bp->b_addr;
56
57 if (!xfs_sb_version_hascrc(&mp->m_sb))
58 return 0;
59
60 dsl->sl_magic = cpu_to_be32(XFS_SYMLINK_MAGIC);
61 dsl->sl_offset = cpu_to_be32(offset);
62 dsl->sl_bytes = cpu_to_be32(size);
63 uuid_copy(&dsl->sl_uuid, &mp->m_sb.sb_meta_uuid);
64 dsl->sl_owner = cpu_to_be64(ino);
65 dsl->sl_blkno = cpu_to_be64(bp->b_bn);
66 bp->b_ops = &xfs_symlink_buf_ops;
67
68 return sizeof(struct xfs_dsymlink_hdr);
69 }
70
71 /*
72 * Checking of the symlink header is split into two parts. the verifier does
73 * CRC, location and bounds checking, the unpacking function checks the path
74 * parameters and owner.
75 */
76 bool
77 xfs_symlink_hdr_ok(
78 xfs_ino_t ino,
79 uint32_t offset,
80 uint32_t size,
81 struct xfs_buf *bp)
82 {
83 struct xfs_dsymlink_hdr *dsl = bp->b_addr;
84
85 if (offset != be32_to_cpu(dsl->sl_offset))
86 return false;
87 if (size != be32_to_cpu(dsl->sl_bytes))
88 return false;
89 if (ino != be64_to_cpu(dsl->sl_owner))
90 return false;
91
92 /* ok */
93 return true;
94 }
95
96 static bool
97 xfs_symlink_verify(
98 struct xfs_buf *bp)
99 {
100 struct xfs_mount *mp = bp->b_target->bt_mount;
101 struct xfs_dsymlink_hdr *dsl = bp->b_addr;
102
103 if (!xfs_sb_version_hascrc(&mp->m_sb))
104 return false;
105 if (dsl->sl_magic != cpu_to_be32(XFS_SYMLINK_MAGIC))
106 return false;
107 if (!uuid_equal(&dsl->sl_uuid, &mp->m_sb.sb_meta_uuid))
108 return false;
109 if (bp->b_bn != be64_to_cpu(dsl->sl_blkno))
110 return false;
111 if (be32_to_cpu(dsl->sl_offset) +
112 be32_to_cpu(dsl->sl_bytes) >= MAXPATHLEN)
113 return false;
114 if (dsl->sl_owner == 0)
115 return false;
116
117 return true;
118 }
119
120 static void
121 xfs_symlink_read_verify(
122 struct xfs_buf *bp)
123 {
124 struct xfs_mount *mp = bp->b_target->bt_mount;
125
126 /* no verification of non-crc buffers */
127 if (!xfs_sb_version_hascrc(&mp->m_sb))
128 return;
129
130 if (!xfs_buf_verify_cksum(bp, XFS_SYMLINK_CRC_OFF))
131 xfs_buf_ioerror(bp, -EFSBADCRC);
132 else if (!xfs_symlink_verify(bp))
133 xfs_buf_ioerror(bp, -EFSCORRUPTED);
134
135 if (bp->b_error)
136 xfs_verifier_error(bp);
137 }
138
139 static void
140 xfs_symlink_write_verify(
141 struct xfs_buf *bp)
142 {
143 struct xfs_mount *mp = bp->b_target->bt_mount;
144 struct xfs_buf_log_item *bip = bp->b_fspriv;
145
146 /* no verification of non-crc buffers */
147 if (!xfs_sb_version_hascrc(&mp->m_sb))
148 return;
149
150 if (!xfs_symlink_verify(bp)) {
151 xfs_buf_ioerror(bp, -EFSCORRUPTED);
152 xfs_verifier_error(bp);
153 return;
154 }
155
156 if (bip) {
157 struct xfs_dsymlink_hdr *dsl = bp->b_addr;
158 dsl->sl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
159 }
160 xfs_buf_update_cksum(bp, XFS_SYMLINK_CRC_OFF);
161 }
162
163 const struct xfs_buf_ops xfs_symlink_buf_ops = {
164 .verify_read = xfs_symlink_read_verify,
165 .verify_write = xfs_symlink_write_verify,
166 };
167
168 void
169 xfs_symlink_local_to_remote(
170 struct xfs_trans *tp,
171 struct xfs_buf *bp,
172 struct xfs_inode *ip,
173 struct xfs_ifork *ifp)
174 {
175 struct xfs_mount *mp = ip->i_mount;
176 char *buf;
177
178 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SYMLINK_BUF);
179
180 if (!xfs_sb_version_hascrc(&mp->m_sb)) {
181 bp->b_ops = NULL;
182 memcpy(bp->b_addr, ifp->if_u1.if_data, ifp->if_bytes);
183 return;
184 }
185
186 /*
187 * As this symlink fits in an inode literal area, it must also fit in
188 * the smallest buffer the filesystem supports.
189 */
190 ASSERT(BBTOB(bp->b_length) >=
191 ifp->if_bytes + sizeof(struct xfs_dsymlink_hdr));
192
193 bp->b_ops = &xfs_symlink_buf_ops;
194
195 buf = bp->b_addr;
196 buf += xfs_symlink_hdr_set(mp, ip->i_ino, 0, ifp->if_bytes, bp);
197 memcpy(buf, ifp->if_u1.if_data, ifp->if_bytes);
198 }