2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
3 * Copyright (c) 2012-2013 Red Hat, Inc.
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
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_error.h"
29 #include "xfs_trace.h"
30 #include "xfs_symlink.h"
31 #include "xfs_cksum.h"
32 #include "xfs_trans.h"
33 #include "xfs_buf_item.h"
38 * Each contiguous block has a header, so it is not just a simple pathlen
46 int buflen
= XFS_SYMLINK_BUF_SPACE(mp
, mp
->m_sb
.sb_blocksize
);
48 return (pathlen
+ buflen
- 1) / buflen
;
59 struct xfs_dsymlink_hdr
*dsl
= bp
->b_addr
;
61 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
64 memset(dsl
, 0, sizeof(struct xfs_dsymlink_hdr
));
65 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
);
68 uuid_copy(&dsl
->sl_uuid
, &mp
->m_sb
.sb_meta_uuid
);
69 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
;
73 return sizeof(struct xfs_dsymlink_hdr
);
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.
88 struct xfs_dsymlink_hdr
*dsl
= bp
->b_addr
;
90 if (offset
!= be32_to_cpu(dsl
->sl_offset
))
92 if (size
!= be32_to_cpu(dsl
->sl_bytes
))
94 if (ino
!= be64_to_cpu(dsl
->sl_owner
))
101 static xfs_failaddr_t
105 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
106 struct xfs_dsymlink_hdr
*dsl
= bp
->b_addr
;
108 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
109 return __this_address
;
110 if (dsl
->sl_magic
!= cpu_to_be32(XFS_SYMLINK_MAGIC
))
111 return __this_address
;
112 if (!uuid_equal(&dsl
->sl_uuid
, &mp
->m_sb
.sb_meta_uuid
))
113 return __this_address
;
114 if (bp
->b_bn
!= be64_to_cpu(dsl
->sl_blkno
))
115 return __this_address
;
116 if (be32_to_cpu(dsl
->sl_offset
) +
117 be32_to_cpu(dsl
->sl_bytes
) >= XFS_SYMLINK_MAXLEN
)
118 return __this_address
;
119 if (dsl
->sl_owner
== 0)
120 return __this_address
;
121 if (!xfs_log_check_lsn(mp
, be64_to_cpu(dsl
->sl_lsn
)))
122 return __this_address
;
128 xfs_symlink_read_verify(
131 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
134 /* no verification of non-crc buffers */
135 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
138 if (!xfs_buf_verify_cksum(bp
, XFS_SYMLINK_CRC_OFF
))
139 xfs_verifier_error(bp
, -EFSBADCRC
, __this_address
);
141 fa
= xfs_symlink_verify(bp
);
143 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
148 xfs_symlink_write_verify(
151 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
152 struct xfs_buf_log_item
*bip
= bp
->b_log_item
;
155 /* no verification of non-crc buffers */
156 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
159 fa
= xfs_symlink_verify(bp
);
161 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
166 struct xfs_dsymlink_hdr
*dsl
= bp
->b_addr
;
167 dsl
->sl_lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
169 xfs_buf_update_cksum(bp
, XFS_SYMLINK_CRC_OFF
);
172 const struct xfs_buf_ops xfs_symlink_buf_ops
= {
173 .name
= "xfs_symlink",
174 .verify_read
= xfs_symlink_read_verify
,
175 .verify_write
= xfs_symlink_write_verify
,
176 .verify_struct
= xfs_symlink_verify
,
180 xfs_symlink_local_to_remote(
181 struct xfs_trans
*tp
,
183 struct xfs_inode
*ip
,
184 struct xfs_ifork
*ifp
)
186 struct xfs_mount
*mp
= ip
->i_mount
;
189 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_SYMLINK_BUF
);
191 if (!xfs_sb_version_hascrc(&mp
->m_sb
)) {
193 memcpy(bp
->b_addr
, ifp
->if_u1
.if_data
, ifp
->if_bytes
);
194 xfs_trans_log_buf(tp
, bp
, 0, ifp
->if_bytes
- 1);
199 * As this symlink fits in an inode literal area, it must also fit in
200 * the smallest buffer the filesystem supports.
202 ASSERT(BBTOB(bp
->b_length
) >=
203 ifp
->if_bytes
+ sizeof(struct xfs_dsymlink_hdr
));
205 bp
->b_ops
= &xfs_symlink_buf_ops
;
208 buf
+= xfs_symlink_hdr_set(mp
, ip
->i_ino
, 0, ifp
->if_bytes
, bp
);
209 memcpy(buf
, ifp
->if_u1
.if_data
, ifp
->if_bytes
);
210 xfs_trans_log_buf(tp
, bp
, 0, sizeof(struct xfs_dsymlink_hdr
) +
214 /* Verify the consistency of an inline symlink. */
216 xfs_symlink_shortform_verify(
217 struct xfs_inode
*ip
)
221 struct xfs_ifork
*ifp
;
224 ASSERT(ip
->i_d
.di_format
== XFS_DINODE_FMT_LOCAL
);
225 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
226 sfp
= (char *)ifp
->if_u1
.if_data
;
227 size
= ifp
->if_bytes
;
230 /* Zero length symlinks can exist while we're deleting a remote one. */
234 /* No negative sizes or overly long symlink targets. */
235 if (size
< 0 || size
> XFS_SYMLINK_MAXLEN
)
236 return __this_address
;
238 /* No NULLs in the target either. */
239 if (memchr(sfp
, 0, size
- 1))
240 return __this_address
;
242 /* We /did/ null-terminate the buffer, right? */
244 return __this_address
;