Linux 3.12.5
[linux/fpc-iii.git] / fs / xfs / xfs_symlink_remote.c
blob01c85e3f64703e6b7a5c170c9e21421fe36d4e4e
1 /*
2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
3 * Copyright (c) 2012-2013 Red Hat, Inc.
4 * All rights reserved.
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.
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.
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
19 #include "xfs.h"
20 #include "xfs_fs.h"
21 #include "xfs_format.h"
22 #include "xfs_log.h"
23 #include "xfs_trans.h"
24 #include "xfs_ag.h"
25 #include "xfs_sb.h"
26 #include "xfs_mount.h"
27 #include "xfs_bmap_btree.h"
28 #include "xfs_inode.h"
29 #include "xfs_error.h"
30 #include "xfs_trace.h"
31 #include "xfs_symlink.h"
32 #include "xfs_cksum.h"
33 #include "xfs_buf_item.h"
37 * Each contiguous block has a header, so it is not just a simple pathlen
38 * to FSB conversion.
40 int
41 xfs_symlink_blocks(
42 struct xfs_mount *mp,
43 int pathlen)
45 int buflen = XFS_SYMLINK_BUF_SPACE(mp, mp->m_sb.sb_blocksize);
47 return (pathlen + buflen - 1) / buflen;
50 int
51 xfs_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)
58 struct xfs_dsymlink_hdr *dsl = bp->b_addr;
60 if (!xfs_sb_version_hascrc(&mp->m_sb))
61 return 0;
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);
66 uuid_copy(&dsl->sl_uuid, &mp->m_sb.sb_uuid);
67 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;
71 return sizeof(struct xfs_dsymlink_hdr);
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.
79 bool
80 xfs_symlink_hdr_ok(
81 struct xfs_mount *mp,
82 xfs_ino_t ino,
83 uint32_t offset,
84 uint32_t size,
85 struct xfs_buf *bp)
87 struct xfs_dsymlink_hdr *dsl = bp->b_addr;
89 if (offset != be32_to_cpu(dsl->sl_offset))
90 return false;
91 if (size != be32_to_cpu(dsl->sl_bytes))
92 return false;
93 if (ino != be64_to_cpu(dsl->sl_owner))
94 return false;
96 /* ok */
97 return true;
100 static bool
101 xfs_symlink_verify(
102 struct xfs_buf *bp)
104 struct xfs_mount *mp = bp->b_target->bt_mount;
105 struct xfs_dsymlink_hdr *dsl = bp->b_addr;
107 if (!xfs_sb_version_hascrc(&mp->m_sb))
108 return false;
109 if (dsl->sl_magic != cpu_to_be32(XFS_SYMLINK_MAGIC))
110 return false;
111 if (!uuid_equal(&dsl->sl_uuid, &mp->m_sb.sb_uuid))
112 return false;
113 if (bp->b_bn != be64_to_cpu(dsl->sl_blkno))
114 return false;
115 if (be32_to_cpu(dsl->sl_offset) +
116 be32_to_cpu(dsl->sl_bytes) >= MAXPATHLEN)
117 return false;
118 if (dsl->sl_owner == 0)
119 return false;
121 return true;
124 static void
125 xfs_symlink_read_verify(
126 struct xfs_buf *bp)
128 struct xfs_mount *mp = bp->b_target->bt_mount;
130 /* no verification of non-crc buffers */
131 if (!xfs_sb_version_hascrc(&mp->m_sb))
132 return;
134 if (!xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
135 offsetof(struct xfs_dsymlink_hdr, sl_crc)) ||
136 !xfs_symlink_verify(bp)) {
137 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
138 xfs_buf_ioerror(bp, EFSCORRUPTED);
142 static void
143 xfs_symlink_write_verify(
144 struct xfs_buf *bp)
146 struct xfs_mount *mp = bp->b_target->bt_mount;
147 struct xfs_buf_log_item *bip = bp->b_fspriv;
149 /* no verification of non-crc buffers */
150 if (!xfs_sb_version_hascrc(&mp->m_sb))
151 return;
153 if (!xfs_symlink_verify(bp)) {
154 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
155 xfs_buf_ioerror(bp, EFSCORRUPTED);
156 return;
159 if (bip) {
160 struct xfs_dsymlink_hdr *dsl = bp->b_addr;
161 dsl->sl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
163 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length),
164 offsetof(struct xfs_dsymlink_hdr, sl_crc));
167 const struct xfs_buf_ops xfs_symlink_buf_ops = {
168 .verify_read = xfs_symlink_read_verify,
169 .verify_write = xfs_symlink_write_verify,
172 void
173 xfs_symlink_local_to_remote(
174 struct xfs_trans *tp,
175 struct xfs_buf *bp,
176 struct xfs_inode *ip,
177 struct xfs_ifork *ifp)
179 struct xfs_mount *mp = ip->i_mount;
180 char *buf;
182 if (!xfs_sb_version_hascrc(&mp->m_sb)) {
183 bp->b_ops = NULL;
184 memcpy(bp->b_addr, ifp->if_u1.if_data, ifp->if_bytes);
185 return;
189 * As this symlink fits in an inode literal area, it must also fit in
190 * the smallest buffer the filesystem supports.
192 ASSERT(BBTOB(bp->b_length) >=
193 ifp->if_bytes + sizeof(struct xfs_dsymlink_hdr));
195 bp->b_ops = &xfs_symlink_buf_ops;
197 buf = bp->b_addr;
198 buf += xfs_symlink_hdr_set(mp, ip->i_ino, 0, ifp->if_bytes, bp);
199 memcpy(buf, ifp->if_u1.if_data, ifp->if_bytes);