]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/xfs_inode_buf.c
libxfs: skip pointless CRC updates after verifier failures
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_inode_buf.c
CommitLineData
2bd0ea18 1/*
5e656dbb 2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
da23017d 3 * All Rights Reserved.
5000d01d 4 *
da23017d
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
2bd0ea18 7 * published by the Free Software Foundation.
5000d01d 8 *
da23017d
NS
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
5000d01d 13 *
da23017d
NS
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2bd0ea18
NS
17 */
18
19#include <xfs.h>
20
5e656dbb
BN
21/*
22 * Check that none of the inode's in the buffer have a next
23 * unlinked field of 0.
24 */
25#if defined(DEBUG)
2bd0ea18
NS
26void
27xfs_inobp_check(
28 xfs_mount_t *mp,
29 xfs_buf_t *bp)
30{
31 int i;
32 int j;
33 xfs_dinode_t *dip;
34
35 j = mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog;
36
37 for (i = 0; i < j; i++) {
38 dip = (xfs_dinode_t *)xfs_buf_offset(bp,
39 i * mp->m_sb.sb_inodesize);
46eca962 40 if (!dip->di_next_unlinked) {
a2ceac1f 41 xfs_alert(mp,
ea6a00d4
DC
42 "Detected bogus zero next_unlinked field in inode %d buffer 0x%llx.",
43 i, (long long)bp->b_bn);
2bd0ea18
NS
44 }
45 }
46}
47#endif
48
f85fc622
DC
49/*
50 * If we are doing readahead on an inode buffer, we might be in log recovery
51 * reading an inode allocation buffer that hasn't yet been replayed, and hence
52 * has not had the inode cores stamped into it. Hence for readahead, the buffer
53 * may be potentially invalid.
54 *
55 * If the readahead buffer is invalid, we don't want to mark it with an error,
56 * but we do want to clear the DONE status of the buffer so that a followup read
57 * will re-read it from disk. This will ensure that we don't get an unnecessary
58 * warnings during log recovery and we don't get unnecessary panics on debug
59 * kernels.
60 */
a2ceac1f
DC
61static void
62xfs_inode_buf_verify(
f85fc622
DC
63 struct xfs_buf *bp,
64 bool readahead)
5e656dbb 65{
a2ceac1f 66 struct xfs_mount *mp = bp->b_target->bt_mount;
5e656dbb
BN
67 int i;
68 int ni;
5e656dbb
BN
69
70 /*
71 * Validate the magic number and version of every inode in the buffer
5e656dbb 72 */
a2ceac1f 73 ni = XFS_BB_TO_FSB(mp, bp->b_length) * mp->m_sb.sb_inopblock;
5e656dbb
BN
74 for (i = 0; i < ni; i++) {
75 int di_ok;
76 xfs_dinode_t *dip;
77
a2ceac1f 78 dip = (struct xfs_dinode *)xfs_buf_offset(bp,
5e656dbb 79 (i << mp->m_sb.sb_inodelog));
a2ceac1f 80 di_ok = dip->di_magic == cpu_to_be16(XFS_DINODE_MAGIC) &&
56b2de80 81 XFS_DINODE_GOOD_VERSION(dip->di_version);
5e656dbb
BN
82 if (unlikely(XFS_TEST_ERROR(!di_ok, mp,
83 XFS_ERRTAG_ITOBP_INOTOBP,
84 XFS_RANDOM_ITOBP_INOTOBP))) {
f85fc622
DC
85 if (readahead) {
86 bp->b_flags &= ~XBF_DONE;
87 return;
88 }
89
a2ceac1f
DC
90 xfs_buf_ioerror(bp, EFSCORRUPTED);
91 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_HIGH,
92 mp, dip);
5e656dbb 93#ifdef DEBUG
ea6a00d4 94 xfs_alert(mp,
a2ceac1f
DC
95 "bad inode magic/vsn daddr %lld #%d (magic=%x)",
96 (unsigned long long)bp->b_bn, i,
56b2de80 97 be16_to_cpu(dip->di_magic));
5e656dbb 98#endif
5e656dbb
BN
99 }
100 }
5e656dbb 101 xfs_inobp_check(mp, bp);
a2ceac1f 102}
5e656dbb 103
e6d77a21 104
a2ceac1f
DC
105static void
106xfs_inode_buf_read_verify(
107 struct xfs_buf *bp)
108{
f85fc622
DC
109 xfs_inode_buf_verify(bp, false);
110}
111
112static void
113xfs_inode_buf_readahead_verify(
114 struct xfs_buf *bp)
115{
116 xfs_inode_buf_verify(bp, true);
5e656dbb 117}
2bd0ea18 118
a2ceac1f
DC
119static void
120xfs_inode_buf_write_verify(
121 struct xfs_buf *bp)
56b2de80 122{
f85fc622 123 xfs_inode_buf_verify(bp, false);
56b2de80
DC
124}
125
a2ceac1f
DC
126const struct xfs_buf_ops xfs_inode_buf_ops = {
127 .verify_read = xfs_inode_buf_read_verify,
128 .verify_write = xfs_inode_buf_write_verify,
129};
130
f85fc622
DC
131const struct xfs_buf_ops xfs_inode_buf_ra_ops = {
132 .verify_read = xfs_inode_buf_readahead_verify,
133 .verify_write = xfs_inode_buf_write_verify,
134};
135
136
2bd0ea18 137/*
a2ceac1f
DC
138 * This routine is called to map an inode to the buffer containing the on-disk
139 * version of the inode. It returns a pointer to the buffer containing the
140 * on-disk inode in the bpp parameter, and in the dipp parameter it returns a
141 * pointer to the on-disk inode within that buffer.
2bd0ea18 142 *
a2ceac1f
DC
143 * If a non-zero error is returned, then the contents of bpp and dipp are
144 * undefined.
2bd0ea18
NS
145 */
146int
a2ceac1f
DC
147xfs_imap_to_bp(
148 struct xfs_mount *mp,
149 struct xfs_trans *tp,
150 struct xfs_imap *imap,
151 struct xfs_dinode **dipp,
152 struct xfs_buf **bpp,
153 uint buf_flags,
154 uint iget_flags)
2bd0ea18 155{
a2ceac1f
DC
156 struct xfs_buf *bp;
157 int error;
2bd0ea18 158
a2ceac1f
DC
159 buf_flags |= XBF_UNMAPPED;
160 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, imap->im_blkno,
161 (int)imap->im_len, buf_flags, &bp,
162 &xfs_inode_buf_ops);
163 if (error) {
164 if (error == EAGAIN) {
165 ASSERT(buf_flags & XBF_TRYLOCK);
166 return error;
167 }
2bd0ea18 168
a2ceac1f
DC
169 if (error == EFSCORRUPTED &&
170 (iget_flags & XFS_IGET_UNTRUSTED))
171 return XFS_ERROR(EINVAL);
2bd0ea18 172
a2ceac1f
DC
173 xfs_warn(mp, "%s: xfs_trans_read_buf() returned error %d.",
174 __func__, error);
175 return error;
2bd0ea18 176 }
2bd0ea18 177
2bd0ea18 178 *bpp = bp;
a2ceac1f 179 *dipp = (struct xfs_dinode *)xfs_buf_offset(bp, imap->im_boffset);
2bd0ea18
NS
180 return 0;
181}
182
5000d01d 183void
5e656dbb
BN
184xfs_dinode_from_disk(
185 xfs_icdinode_t *to,
56b2de80 186 xfs_dinode_t *from)
2bd0ea18 187{
5e656dbb
BN
188 to->di_magic = be16_to_cpu(from->di_magic);
189 to->di_mode = be16_to_cpu(from->di_mode);
190 to->di_version = from ->di_version;
191 to->di_format = from->di_format;
192 to->di_onlink = be16_to_cpu(from->di_onlink);
193 to->di_uid = be32_to_cpu(from->di_uid);
194 to->di_gid = be32_to_cpu(from->di_gid);
195 to->di_nlink = be32_to_cpu(from->di_nlink);
22bc10ed
AM
196 to->di_projid_lo = be16_to_cpu(from->di_projid_lo);
197 to->di_projid_hi = be16_to_cpu(from->di_projid_hi);
5e656dbb
BN
198 memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
199 to->di_flushiter = be16_to_cpu(from->di_flushiter);
200 to->di_atime.t_sec = be32_to_cpu(from->di_atime.t_sec);
201 to->di_atime.t_nsec = be32_to_cpu(from->di_atime.t_nsec);
202 to->di_mtime.t_sec = be32_to_cpu(from->di_mtime.t_sec);
203 to->di_mtime.t_nsec = be32_to_cpu(from->di_mtime.t_nsec);
204 to->di_ctime.t_sec = be32_to_cpu(from->di_ctime.t_sec);
205 to->di_ctime.t_nsec = be32_to_cpu(from->di_ctime.t_nsec);
206 to->di_size = be64_to_cpu(from->di_size);
207 to->di_nblocks = be64_to_cpu(from->di_nblocks);
208 to->di_extsize = be32_to_cpu(from->di_extsize);
209 to->di_nextents = be32_to_cpu(from->di_nextents);
210 to->di_anextents = be16_to_cpu(from->di_anextents);
211 to->di_forkoff = from->di_forkoff;
212 to->di_aformat = from->di_aformat;
213 to->di_dmevmask = be32_to_cpu(from->di_dmevmask);
214 to->di_dmstate = be16_to_cpu(from->di_dmstate);
215 to->di_flags = be16_to_cpu(from->di_flags);
216 to->di_gen = be32_to_cpu(from->di_gen);
41ce5f36
DC
217
218 if (to->di_version == 3) {
219 to->di_changecount = be64_to_cpu(from->di_changecount);
220 to->di_crtime.t_sec = be32_to_cpu(from->di_crtime.t_sec);
221 to->di_crtime.t_nsec = be32_to_cpu(from->di_crtime.t_nsec);
222 to->di_flags2 = be64_to_cpu(from->di_flags2);
223 to->di_ino = be64_to_cpu(from->di_ino);
224 to->di_lsn = be64_to_cpu(from->di_lsn);
225 memcpy(to->di_pad2, from->di_pad2, sizeof(to->di_pad2));
f85fc622 226 uuid_copy(&to->di_uuid, &from->di_uuid);
41ce5f36 227 }
5e656dbb
BN
228}
229
230void
231xfs_dinode_to_disk(
56b2de80 232 xfs_dinode_t *to,
5e656dbb
BN
233 xfs_icdinode_t *from)
234{
235 to->di_magic = cpu_to_be16(from->di_magic);
236 to->di_mode = cpu_to_be16(from->di_mode);
237 to->di_version = from ->di_version;
238 to->di_format = from->di_format;
239 to->di_onlink = cpu_to_be16(from->di_onlink);
240 to->di_uid = cpu_to_be32(from->di_uid);
241 to->di_gid = cpu_to_be32(from->di_gid);
242 to->di_nlink = cpu_to_be32(from->di_nlink);
22bc10ed
AM
243 to->di_projid_lo = cpu_to_be16(from->di_projid_lo);
244 to->di_projid_hi = cpu_to_be16(from->di_projid_hi);
5e656dbb 245 memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
5e656dbb
BN
246 to->di_atime.t_sec = cpu_to_be32(from->di_atime.t_sec);
247 to->di_atime.t_nsec = cpu_to_be32(from->di_atime.t_nsec);
248 to->di_mtime.t_sec = cpu_to_be32(from->di_mtime.t_sec);
249 to->di_mtime.t_nsec = cpu_to_be32(from->di_mtime.t_nsec);
250 to->di_ctime.t_sec = cpu_to_be32(from->di_ctime.t_sec);
251 to->di_ctime.t_nsec = cpu_to_be32(from->di_ctime.t_nsec);
252 to->di_size = cpu_to_be64(from->di_size);
253 to->di_nblocks = cpu_to_be64(from->di_nblocks);
254 to->di_extsize = cpu_to_be32(from->di_extsize);
255 to->di_nextents = cpu_to_be32(from->di_nextents);
256 to->di_anextents = cpu_to_be16(from->di_anextents);
257 to->di_forkoff = from->di_forkoff;
258 to->di_aformat = from->di_aformat;
259 to->di_dmevmask = cpu_to_be32(from->di_dmevmask);
260 to->di_dmstate = cpu_to_be16(from->di_dmstate);
261 to->di_flags = cpu_to_be16(from->di_flags);
262 to->di_gen = cpu_to_be32(from->di_gen);
41ce5f36
DC
263
264 if (from->di_version == 3) {
265 to->di_changecount = cpu_to_be64(from->di_changecount);
266 to->di_crtime.t_sec = cpu_to_be32(from->di_crtime.t_sec);
267 to->di_crtime.t_nsec = cpu_to_be32(from->di_crtime.t_nsec);
268 to->di_flags2 = cpu_to_be64(from->di_flags2);
269 to->di_ino = cpu_to_be64(from->di_ino);
270 to->di_lsn = cpu_to_be64(from->di_lsn);
271 memcpy(to->di_pad2, from->di_pad2, sizeof(to->di_pad2));
f85fc622 272 uuid_copy(&to->di_uuid, &from->di_uuid);
fde71651
DC
273 to->di_flushiter = 0;
274 } else {
275 to->di_flushiter = cpu_to_be16(from->di_flushiter);
41ce5f36
DC
276 }
277}
278
a73b88f2 279bool
41ce5f36
DC
280xfs_dinode_verify(
281 struct xfs_mount *mp,
a73b88f2 282 xfs_ino_t ino,
41ce5f36
DC
283 struct xfs_dinode *dip)
284{
285 if (dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC))
286 return false;
287
288 /* only version 3 or greater inodes are extensively verified here */
289 if (dip->di_version < 3)
290 return true;
291
292 if (!xfs_sb_version_hascrc(&mp->m_sb))
293 return false;
294 if (!xfs_verify_cksum((char *)dip, mp->m_sb.sb_inodesize,
295 offsetof(struct xfs_dinode, di_crc)))
296 return false;
a73b88f2 297 if (be64_to_cpu(dip->di_ino) != ino)
41ce5f36
DC
298 return false;
299 if (!uuid_equal(&dip->di_uuid, &mp->m_sb.sb_uuid))
300 return false;
301 return true;
302}
e6d77a21 303
41ce5f36
DC
304void
305xfs_dinode_calc_crc(
306 struct xfs_mount *mp,
307 struct xfs_dinode *dip)
308{
309 __uint32_t crc;
310
311 if (dip->di_version < 3)
312 return;
313
314 ASSERT(xfs_sb_version_hascrc(&mp->m_sb));
315 crc = xfs_start_cksum((char *)dip, mp->m_sb.sb_inodesize,
316 offsetof(struct xfs_dinode, di_crc));
317 dip->di_crc = xfs_end_cksum(crc);
2bd0ea18
NS
318}
319
a2ceac1f
DC
320/*
321 * Read the disk inode attributes into the in-core inode structure.
322 */
323int
324xfs_iread(
325 xfs_mount_t *mp,
326 xfs_trans_t *tp,
327 xfs_inode_t *ip,
328 uint iget_flags)
329{
330 xfs_buf_t *bp;
331 xfs_dinode_t *dip;
332 int error;
333
334 /*
335 * Fill in the location information in the in-core inode.
336 */
337 error = xfs_imap(mp, tp, ip->i_ino, &ip->i_imap, iget_flags);
338 if (error)
339 return error;
340
341 /*
342 * Get pointers to the on-disk inode and the buffer containing it.
343 */
344 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &bp, 0, iget_flags);
345 if (error)
346 return error;
347
41ce5f36 348 /* even unallocated inodes are verified */
a73b88f2 349 if (!xfs_dinode_verify(mp, ip->i_ino, dip)) {
41ce5f36
DC
350 xfs_alert(mp, "%s: validation failed for inode %lld failed",
351 __func__, ip->i_ino);
352
353 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, dip);
354 error = XFS_ERROR(EFSCORRUPTED);
a2ceac1f
DC
355 goto out_brelse;
356 }
357
358 /*
359 * If the on-disk inode is already linked to a directory
360 * entry, copy all of the inode into the in-core inode.
5d90ab5a 361 * xfs_iformat_fork() handles copying in the inode format
a2ceac1f
DC
362 * specific information.
363 * Otherwise, just get the truly permanent information.
364 */
365 if (dip->di_mode) {
366 xfs_dinode_from_disk(&ip->i_d, dip);
5d90ab5a 367 error = xfs_iformat_fork(ip, dip);
a2ceac1f
DC
368 if (error) {
369#ifdef DEBUG
370 xfs_alert(mp, "%s: xfs_iformat() returned error %d",
371 __func__, error);
372#endif /* DEBUG */
373 goto out_brelse;
374 }
375 } else {
41ce5f36
DC
376 /*
377 * Partial initialisation of the in-core inode. Just the bits
378 * that xfs_ialloc won't overwrite or relies on being correct.
379 */
a2ceac1f
DC
380 ip->i_d.di_magic = be16_to_cpu(dip->di_magic);
381 ip->i_d.di_version = dip->di_version;
382 ip->i_d.di_gen = be32_to_cpu(dip->di_gen);
383 ip->i_d.di_flushiter = be16_to_cpu(dip->di_flushiter);
41ce5f36
DC
384
385 if (dip->di_version == 3) {
386 ip->i_d.di_ino = be64_to_cpu(dip->di_ino);
387 uuid_copy(&ip->i_d.di_uuid, &dip->di_uuid);
388 }
389
a2ceac1f
DC
390 /*
391 * Make sure to pull in the mode here as well in
392 * case the inode is released without being used.
393 * This ensures that xfs_inactive() will see that
394 * the inode is already free and not try to mess
395 * with the uninitialized part of it.
396 */
397 ip->i_d.di_mode = 0;
398 }
399
400 /*
401 * The inode format changed when we moved the link count and
402 * made it 32 bits long. If this is an old format inode,
403 * convert it in memory to look like a new one. If it gets
404 * flushed to disk we will convert back before flushing or
405 * logging it. We zero out the new projid field and the old link
406 * count field. We'll handle clearing the pad field (the remains
407 * of the old uuid field) when we actually convert the inode to
408 * the new format. We don't change the version number so that we
409 * can distinguish this from a real new format inode.
410 */
411 if (ip->i_d.di_version == 1) {
412 ip->i_d.di_nlink = ip->i_d.di_onlink;
413 ip->i_d.di_onlink = 0;
414 xfs_set_projid(&ip->i_d, 0);
415 }
416
417 ip->i_delayed_blks = 0;
418
419 /*
420 * Mark the buffer containing the inode as something to keep
421 * around for a while. This helps to keep recently accessed
422 * meta-data in-core longer.
423 */
424 xfs_buf_set_ref(bp, XFS_INO_REF);
425
426 /*
1d519883
DC
427 * Use xfs_trans_brelse() to release the buffer containing the on-disk
428 * inode, because it was acquired with xfs_trans_read_buf() in
429 * xfs_imap_to_bp() above. If tp is NULL, this is just a normal
a2ceac1f
DC
430 * brelse(). If we're within a transaction, then xfs_trans_brelse()
431 * will only release the buffer if it is not dirty within the
432 * transaction. It will be OK to release the buffer in this case,
1d519883
DC
433 * because inodes on disk are never destroyed and we will be locking the
434 * new in-core inode before putting it in the cache where other
435 * processes can find it. Thus we don't have to worry about the inode
436 * being changed just because we released the buffer.
a2ceac1f
DC
437 */
438 out_brelse:
439 xfs_trans_brelse(tp, bp);
440 return error;
441}