]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/xfs_inode_buf.c
xfs: call xfs_iformat_fork from xfs_inode_from_disk
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_inode_buf.c
CommitLineData
37b3b4d6 1// SPDX-License-Identifier: GPL-2.0
2bd0ea18 2/*
5e656dbb 3 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
da23017d 4 * All Rights Reserved.
2bd0ea18 5 */
9c799827 6#include "libxfs_priv.h"
b626fb59
DC
7#include "xfs_fs.h"
8#include "xfs_shared.h"
9#include "xfs_format.h"
10#include "xfs_log_format.h"
11#include "xfs_trans_resv.h"
12#include "xfs_mount.h"
13#include "xfs_inode.h"
2cf10e4c 14#include "xfs_errortag.h"
b626fb59
DC
15#include "xfs_trans.h"
16#include "xfs_ialloc.h"
0962b5b3 17#include "xfs_dir2.h"
2bd0ea18 18
998aed52 19
5e656dbb
BN
20/*
21 * Check that none of the inode's in the buffer have a next
22 * unlinked field of 0.
23 */
24#if defined(DEBUG)
2bd0ea18
NS
25void
26xfs_inobp_check(
27 xfs_mount_t *mp,
28 xfs_buf_t *bp)
29{
30 int i;
2bd0ea18
NS
31 xfs_dinode_t *dip;
32
4b0fd0f4 33 for (i = 0; i < M_IGEO(mp)->inodes_per_cluster; i++) {
92acb899 34 dip = xfs_buf_offset(bp, i * mp->m_sb.sb_inodesize);
46eca962 35 if (!dip->di_next_unlinked) {
a2ceac1f 36 xfs_alert(mp,
ea6a00d4
DC
37 "Detected bogus zero next_unlinked field in inode %d buffer 0x%llx.",
38 i, (long long)bp->b_bn);
2bd0ea18
NS
39 }
40 }
41}
42#endif
43
f85fc622
DC
44/*
45 * If we are doing readahead on an inode buffer, we might be in log recovery
46 * reading an inode allocation buffer that hasn't yet been replayed, and hence
47 * has not had the inode cores stamped into it. Hence for readahead, the buffer
48 * may be potentially invalid.
49 *
736e1cf6
DC
50 * If the readahead buffer is invalid, we need to mark it with an error and
51 * clear the DONE status of the buffer so that a followup read will re-read it
52 * from disk. We don't report the error otherwise to avoid warnings during log
53 * recovery and we don't get unnecssary panics on debug kernels. We use EIO here
54 * because all we want to do is say readahead failed; there is no-one to report
55 * the error to, so this will distinguish it from a non-ra verifier failure.
6daba42a
DC
56 * Changes to this readahead error behavour also need to be reflected in
57 * xfs_dquot_buf_readahead_verify().
f85fc622 58 */
a2ceac1f
DC
59static void
60xfs_inode_buf_verify(
f85fc622
DC
61 struct xfs_buf *bp,
62 bool readahead)
5e656dbb 63{
7861ef77 64 struct xfs_mount *mp = bp->b_mount;
2949b467 65 xfs_agnumber_t agno;
5e656dbb
BN
66 int i;
67 int ni;
5e656dbb
BN
68
69 /*
70 * Validate the magic number and version of every inode in the buffer
5e656dbb 71 */
2949b467 72 agno = xfs_daddr_to_agno(mp, XFS_BUF_ADDR(bp));
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;
2949b467 77 xfs_agino_t unlinked_ino;
5e656dbb 78
92acb899 79 dip = xfs_buf_offset(bp, (i << mp->m_sb.sb_inodelog));
2949b467 80 unlinked_ino = be32_to_cpu(dip->di_next_unlinked);
9e26de8f 81 di_ok = xfs_verify_magic16(bp, dip->di_magic) &&
db84c7e8 82 xfs_dinode_good_version(&mp->m_sb, dip->di_version) &&
7a425ea8 83 xfs_verify_agino_or_null(mp, agno, unlinked_ino);
5e656dbb 84 if (unlikely(XFS_TEST_ERROR(!di_ok, mp,
e2a190dd 85 XFS_ERRTAG_ITOBP_INOTOBP))) {
f85fc622
DC
86 if (readahead) {
87 bp->b_flags &= ~XBF_DONE;
736e1cf6 88 xfs_buf_ioerror(bp, -EIO);
f85fc622
DC
89 return;
90 }
91
5e656dbb 92#ifdef DEBUG
ea6a00d4 93 xfs_alert(mp,
a2ceac1f
DC
94 "bad inode magic/vsn daddr %lld #%d (magic=%x)",
95 (unsigned long long)bp->b_bn, i,
56b2de80 96 be16_to_cpu(dip->di_magic));
5e656dbb 97#endif
43f7ceb5
DW
98 xfs_buf_verifier_error(bp, -EFSCORRUPTED,
99 __func__, dip, sizeof(*dip),
100 NULL);
2949b467 101 return;
5e656dbb
BN
102 }
103 }
a2ceac1f 104}
5e656dbb 105
e6d77a21 106
a2ceac1f
DC
107static void
108xfs_inode_buf_read_verify(
109 struct xfs_buf *bp)
110{
f85fc622
DC
111 xfs_inode_buf_verify(bp, false);
112}
113
114static void
115xfs_inode_buf_readahead_verify(
116 struct xfs_buf *bp)
117{
118 xfs_inode_buf_verify(bp, true);
5e656dbb 119}
2bd0ea18 120
a2ceac1f
DC
121static void
122xfs_inode_buf_write_verify(
123 struct xfs_buf *bp)
56b2de80 124{
f85fc622 125 xfs_inode_buf_verify(bp, false);
56b2de80
DC
126}
127
a2ceac1f 128const struct xfs_buf_ops xfs_inode_buf_ops = {
a3fac935 129 .name = "xfs_inode",
9e26de8f
DW
130 .magic16 = { cpu_to_be16(XFS_DINODE_MAGIC),
131 cpu_to_be16(XFS_DINODE_MAGIC) },
a2ceac1f
DC
132 .verify_read = xfs_inode_buf_read_verify,
133 .verify_write = xfs_inode_buf_write_verify,
134};
135
f85fc622 136const struct xfs_buf_ops xfs_inode_buf_ra_ops = {
7563fc9c 137 .name = "xfs_inode_ra",
9e26de8f
DW
138 .magic16 = { cpu_to_be16(XFS_DINODE_MAGIC),
139 cpu_to_be16(XFS_DINODE_MAGIC) },
f85fc622
DC
140 .verify_read = xfs_inode_buf_readahead_verify,
141 .verify_write = xfs_inode_buf_write_verify,
142};
143
144
2bd0ea18 145/*
a2ceac1f
DC
146 * This routine is called to map an inode to the buffer containing the on-disk
147 * version of the inode. It returns a pointer to the buffer containing the
148 * on-disk inode in the bpp parameter, and in the dipp parameter it returns a
149 * pointer to the on-disk inode within that buffer.
2bd0ea18 150 *
a2ceac1f
DC
151 * If a non-zero error is returned, then the contents of bpp and dipp are
152 * undefined.
2bd0ea18
NS
153 */
154int
a2ceac1f
DC
155xfs_imap_to_bp(
156 struct xfs_mount *mp,
157 struct xfs_trans *tp,
158 struct xfs_imap *imap,
159 struct xfs_dinode **dipp,
160 struct xfs_buf **bpp,
0cd4b54a 161 uint buf_flags)
2bd0ea18 162{
a2ceac1f
DC
163 struct xfs_buf *bp;
164 int error;
2bd0ea18 165
a2ceac1f
DC
166 buf_flags |= XBF_UNMAPPED;
167 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, imap->im_blkno,
168 (int)imap->im_len, buf_flags, &bp,
169 &xfs_inode_buf_ops);
170 if (error) {
820c6548 171 ASSERT(error != -EAGAIN || (buf_flags & XBF_TRYLOCK));
a2ceac1f 172 return error;
2bd0ea18 173 }
2bd0ea18 174
2bd0ea18 175 *bpp = bp;
92acb899 176 *dipp = xfs_buf_offset(bp, imap->im_boffset);
2bd0ea18
NS
177 return 0;
178}
179
3eb685ae 180int
1bc6cbe3
DC
181xfs_inode_from_disk(
182 struct xfs_inode *ip,
138659f1 183 struct xfs_dinode *from)
2bd0ea18 184{
1bc6cbe3
DC
185 struct xfs_icdinode *to = &ip->i_d;
186 struct inode *inode = VFS_I(ip);
187
f089fc42
DC
188 /*
189 * Convert v1 inodes immediately to v2 inode format as this is the
190 * minimum inode version format we support in the rest of the code.
fd82a87e 191 * They will also be unconditionally written back to disk as v2 inodes.
f089fc42 192 */
fd82a87e 193 if (unlikely(from->di_version == 1)) {
bcbe04c1 194 set_nlink(inode, be16_to_cpu(from->di_onlink));
e6bd76f0 195 to->di_projid = 0;
f089fc42 196 } else {
bcbe04c1 197 set_nlink(inode, be32_to_cpu(from->di_nlink));
e6bd76f0
CH
198 to->di_projid = (prid_t)be16_to_cpu(from->di_projid_hi) << 16 |
199 be16_to_cpu(from->di_projid_lo);
f089fc42
DC
200 }
201
5e656dbb 202 to->di_format = from->di_format;
100eb9e9
CH
203 i_uid_write(inode, be32_to_cpu(from->di_uid));
204 i_gid_write(inode, be32_to_cpu(from->di_gid));
5e656dbb 205 to->di_flushiter = be16_to_cpu(from->di_flushiter);
1bc6cbe3
DC
206
207 /*
208 * Time is signed, so need to convert to signed 32 bit before
209 * storing in inode timestamp which may be 64 bit. Otherwise
210 * a time before epoch is converted to a time long after epoch
211 * on 64 bit systems.
212 */
213 inode->i_atime.tv_sec = (int)be32_to_cpu(from->di_atime.t_sec);
214 inode->i_atime.tv_nsec = (int)be32_to_cpu(from->di_atime.t_nsec);
215 inode->i_mtime.tv_sec = (int)be32_to_cpu(from->di_mtime.t_sec);
216 inode->i_mtime.tv_nsec = (int)be32_to_cpu(from->di_mtime.t_nsec);
217 inode->i_ctime.tv_sec = (int)be32_to_cpu(from->di_ctime.t_sec);
218 inode->i_ctime.tv_nsec = (int)be32_to_cpu(from->di_ctime.t_nsec);
6652c253 219 inode->i_generation = be32_to_cpu(from->di_gen);
e37bf53c 220 inode->i_mode = be16_to_cpu(from->di_mode);
1bc6cbe3 221
5e656dbb
BN
222 to->di_size = be64_to_cpu(from->di_size);
223 to->di_nblocks = be64_to_cpu(from->di_nblocks);
224 to->di_extsize = be32_to_cpu(from->di_extsize);
225 to->di_nextents = be32_to_cpu(from->di_nextents);
226 to->di_anextents = be16_to_cpu(from->di_anextents);
227 to->di_forkoff = from->di_forkoff;
228 to->di_aformat = from->di_aformat;
229 to->di_dmevmask = be32_to_cpu(from->di_dmevmask);
230 to->di_dmstate = be16_to_cpu(from->di_dmstate);
231 to->di_flags = be16_to_cpu(from->di_flags);
41ce5f36 232
fd82a87e 233 if (xfs_sb_version_has_v3inode(&ip->i_mount->m_sb)) {
d5105a89
JL
234 inode_set_iversion_queried(inode,
235 be64_to_cpu(from->di_changecount));
cb49e9a4
CH
236 to->di_crtime.tv_sec = be32_to_cpu(from->di_crtime.t_sec);
237 to->di_crtime.tv_nsec = be32_to_cpu(from->di_crtime.t_nsec);
41ce5f36 238 to->di_flags2 = be64_to_cpu(from->di_flags2);
10460994 239 to->di_cowextsize = be32_to_cpu(from->di_cowextsize);
41ce5f36 240 }
3eb685ae
CH
241
242 return xfs_iformat_fork(ip, from);
5e656dbb
BN
243}
244
245void
1bc6cbe3
DC
246xfs_inode_to_disk(
247 struct xfs_inode *ip,
db17aebe
DC
248 struct xfs_dinode *to,
249 xfs_lsn_t lsn)
1bc6cbe3
DC
250{
251 struct xfs_icdinode *from = &ip->i_d;
252 struct inode *inode = VFS_I(ip);
253
db17aebe 254 to->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
f089fc42 255 to->di_onlink = 0;
db17aebe 256
1bc6cbe3 257 to->di_format = from->di_format;
100eb9e9
CH
258 to->di_uid = cpu_to_be32(i_uid_read(inode));
259 to->di_gid = cpu_to_be32(i_gid_read(inode));
e6bd76f0
CH
260 to->di_projid_lo = cpu_to_be16(from->di_projid & 0xffff);
261 to->di_projid_hi = cpu_to_be16(from->di_projid >> 16);
1bc6cbe3 262
db17aebe 263 memset(to->di_pad, 0, sizeof(to->di_pad));
1bc6cbe3
DC
264 to->di_atime.t_sec = cpu_to_be32(inode->i_atime.tv_sec);
265 to->di_atime.t_nsec = cpu_to_be32(inode->i_atime.tv_nsec);
266 to->di_mtime.t_sec = cpu_to_be32(inode->i_mtime.tv_sec);
267 to->di_mtime.t_nsec = cpu_to_be32(inode->i_mtime.tv_nsec);
268 to->di_ctime.t_sec = cpu_to_be32(inode->i_ctime.tv_sec);
269 to->di_ctime.t_nsec = cpu_to_be32(inode->i_ctime.tv_nsec);
bcbe04c1 270 to->di_nlink = cpu_to_be32(inode->i_nlink);
6652c253 271 to->di_gen = cpu_to_be32(inode->i_generation);
e37bf53c 272 to->di_mode = cpu_to_be16(inode->i_mode);
1bc6cbe3
DC
273
274 to->di_size = cpu_to_be64(from->di_size);
275 to->di_nblocks = cpu_to_be64(from->di_nblocks);
276 to->di_extsize = cpu_to_be32(from->di_extsize);
277 to->di_nextents = cpu_to_be32(from->di_nextents);
278 to->di_anextents = cpu_to_be16(from->di_anextents);
279 to->di_forkoff = from->di_forkoff;
280 to->di_aformat = from->di_aformat;
281 to->di_dmevmask = cpu_to_be32(from->di_dmevmask);
282 to->di_dmstate = cpu_to_be16(from->di_dmstate);
283 to->di_flags = cpu_to_be16(from->di_flags);
1bc6cbe3 284
fd82a87e
CH
285 if (xfs_sb_version_has_v3inode(&ip->i_mount->m_sb)) {
286 to->di_version = 3;
d5105a89 287 to->di_changecount = cpu_to_be64(inode_peek_iversion(inode));
cb49e9a4
CH
288 to->di_crtime.t_sec = cpu_to_be32(from->di_crtime.tv_sec);
289 to->di_crtime.t_nsec = cpu_to_be32(from->di_crtime.tv_nsec);
1bc6cbe3 290 to->di_flags2 = cpu_to_be64(from->di_flags2);
10460994 291 to->di_cowextsize = cpu_to_be32(from->di_cowextsize);
db17aebe
DC
292 to->di_ino = cpu_to_be64(ip->i_ino);
293 to->di_lsn = cpu_to_be64(lsn);
294 memset(to->di_pad2, 0, sizeof(to->di_pad2));
295 uuid_copy(&to->di_uuid, &ip->i_mount->m_sb.sb_meta_uuid);
1bc6cbe3
DC
296 to->di_flushiter = 0;
297 } else {
fd82a87e 298 to->di_version = 2;
1bc6cbe3
DC
299 to->di_flushiter = cpu_to_be16(from->di_flushiter);
300 }
301}
302
303void
304xfs_log_dinode_to_disk(
305 struct xfs_log_dinode *from,
306 struct xfs_dinode *to)
5e656dbb
BN
307{
308 to->di_magic = cpu_to_be16(from->di_magic);
309 to->di_mode = cpu_to_be16(from->di_mode);
f089fc42 310 to->di_version = from->di_version;
5e656dbb 311 to->di_format = from->di_format;
f089fc42 312 to->di_onlink = 0;
5e656dbb
BN
313 to->di_uid = cpu_to_be32(from->di_uid);
314 to->di_gid = cpu_to_be32(from->di_gid);
315 to->di_nlink = cpu_to_be32(from->di_nlink);
22bc10ed
AM
316 to->di_projid_lo = cpu_to_be16(from->di_projid_lo);
317 to->di_projid_hi = cpu_to_be16(from->di_projid_hi);
5e656dbb 318 memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
1bc6cbe3 319
5e656dbb
BN
320 to->di_atime.t_sec = cpu_to_be32(from->di_atime.t_sec);
321 to->di_atime.t_nsec = cpu_to_be32(from->di_atime.t_nsec);
322 to->di_mtime.t_sec = cpu_to_be32(from->di_mtime.t_sec);
323 to->di_mtime.t_nsec = cpu_to_be32(from->di_mtime.t_nsec);
324 to->di_ctime.t_sec = cpu_to_be32(from->di_ctime.t_sec);
325 to->di_ctime.t_nsec = cpu_to_be32(from->di_ctime.t_nsec);
1bc6cbe3 326
5e656dbb
BN
327 to->di_size = cpu_to_be64(from->di_size);
328 to->di_nblocks = cpu_to_be64(from->di_nblocks);
329 to->di_extsize = cpu_to_be32(from->di_extsize);
330 to->di_nextents = cpu_to_be32(from->di_nextents);
331 to->di_anextents = cpu_to_be16(from->di_anextents);
332 to->di_forkoff = from->di_forkoff;
333 to->di_aformat = from->di_aformat;
334 to->di_dmevmask = cpu_to_be32(from->di_dmevmask);
335 to->di_dmstate = cpu_to_be16(from->di_dmstate);
336 to->di_flags = cpu_to_be16(from->di_flags);
337 to->di_gen = cpu_to_be32(from->di_gen);
41ce5f36
DC
338
339 if (from->di_version == 3) {
340 to->di_changecount = cpu_to_be64(from->di_changecount);
341 to->di_crtime.t_sec = cpu_to_be32(from->di_crtime.t_sec);
342 to->di_crtime.t_nsec = cpu_to_be32(from->di_crtime.t_nsec);
343 to->di_flags2 = cpu_to_be64(from->di_flags2);
10460994 344 to->di_cowextsize = cpu_to_be32(from->di_cowextsize);
41ce5f36
DC
345 to->di_ino = cpu_to_be64(from->di_ino);
346 to->di_lsn = cpu_to_be64(from->di_lsn);
347 memcpy(to->di_pad2, from->di_pad2, sizeof(to->di_pad2));
f85fc622 348 uuid_copy(&to->di_uuid, &from->di_uuid);
fde71651
DC
349 to->di_flushiter = 0;
350 } else {
351 to->di_flushiter = cpu_to_be16(from->di_flushiter);
41ce5f36
DC
352 }
353}
354
8ac50baf
DC
355static xfs_failaddr_t
356xfs_dinode_verify_fork(
357 struct xfs_dinode *dip,
358 struct xfs_mount *mp,
359 int whichfork)
360{
361 uint32_t di_nextents = XFS_DFORK_NEXTENTS(dip, whichfork);
362
363 switch (XFS_DFORK_FORMAT(dip, whichfork)) {
364 case XFS_DINODE_FMT_LOCAL:
365 /*
366 * no local regular files yet
367 */
368 if (whichfork == XFS_DATA_FORK) {
369 if (S_ISREG(be16_to_cpu(dip->di_mode)))
370 return __this_address;
371 if (be64_to_cpu(dip->di_size) >
372 XFS_DFORK_SIZE(dip, mp, whichfork))
373 return __this_address;
374 }
375 if (di_nextents)
376 return __this_address;
377 break;
378 case XFS_DINODE_FMT_EXTENTS:
379 if (di_nextents > XFS_DFORK_MAXEXT(dip, mp, whichfork))
380 return __this_address;
381 break;
382 case XFS_DINODE_FMT_BTREE:
383 if (whichfork == XFS_ATTR_FORK) {
384 if (di_nextents > MAXAEXTNUM)
385 return __this_address;
386 } else if (di_nextents > MAXEXTNUM) {
387 return __this_address;
388 }
389 break;
390 default:
391 return __this_address;
392 }
393 return NULL;
394}
395
91b32a08
ES
396static xfs_failaddr_t
397xfs_dinode_verify_forkoff(
398 struct xfs_dinode *dip,
399 struct xfs_mount *mp)
400{
401 if (!XFS_DFORK_Q(dip))
402 return NULL;
403
404 switch (dip->di_format) {
405 case XFS_DINODE_FMT_DEV:
406 if (dip->di_forkoff != (roundup(sizeof(xfs_dev_t), 8) >> 3))
407 return __this_address;
408 break;
409 case XFS_DINODE_FMT_LOCAL: /* fall through ... */
410 case XFS_DINODE_FMT_EXTENTS: /* fall through ... */
411 case XFS_DINODE_FMT_BTREE:
4de63245 412 if (dip->di_forkoff >= (XFS_LITINO(mp) >> 3))
91b32a08
ES
413 return __this_address;
414 break;
415 default:
416 return __this_address;
417 }
418 return NULL;
419}
420
bc01119d 421xfs_failaddr_t
41ce5f36
DC
422xfs_dinode_verify(
423 struct xfs_mount *mp,
e515cca1 424 xfs_ino_t ino,
41ce5f36
DC
425 struct xfs_dinode *dip)
426{
316d5a9f 427 xfs_failaddr_t fa;
3cfabff6 428 uint16_t mode;
183537ed
DW
429 uint16_t flags;
430 uint64_t flags2;
ad42e5a1 431 uint64_t di_size;
183537ed 432
41ce5f36 433 if (dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC))
bc01119d 434 return __this_address;
41ce5f36 435
98703400
DW
436 /* Verify v3 integrity information first */
437 if (dip->di_version >= 3) {
db84c7e8 438 if (!xfs_sb_version_has_v3inode(&mp->m_sb))
98703400
DW
439 return __this_address;
440 if (!xfs_verify_cksum((char *)dip, mp->m_sb.sb_inodesize,
441 XFS_DINODE_CRC_OFF))
442 return __this_address;
443 if (be64_to_cpu(dip->di_ino) != ino)
444 return __this_address;
445 if (!uuid_equal(&dip->di_uuid, &mp->m_sb.sb_meta_uuid))
446 return __this_address;
447 }
448
9d85eb44 449 /* don't allow invalid i_size */
ad42e5a1
DW
450 di_size = be64_to_cpu(dip->di_size);
451 if (di_size & (1ULL << 63))
bc01119d 452 return __this_address;
9d85eb44 453
3cfabff6 454 mode = be16_to_cpu(dip->di_mode);
0962b5b3 455 if (mode && xfs_mode_to_ftype(mode) == XFS_DIR3_FT_UNKNOWN)
bc01119d 456 return __this_address;
3cfabff6
AG
457
458 /* No zero-length symlinks/dirs. */
ad42e5a1 459 if ((S_ISLNK(mode) || S_ISDIR(mode)) && di_size == 0)
bc01119d 460 return __this_address;
9d85eb44 461
ad42e5a1
DW
462 /* Fork checks carried over from xfs_iformat_fork */
463 if (mode &&
464 be32_to_cpu(dip->di_nextents) + be16_to_cpu(dip->di_anextents) >
465 be64_to_cpu(dip->di_nblocks))
466 return __this_address;
467
468 if (mode && XFS_DFORK_BOFF(dip) > mp->m_sb.sb_inodesize)
469 return __this_address;
470
471 flags = be16_to_cpu(dip->di_flags);
472
473 if (mode && (flags & XFS_DIFLAG_REALTIME) && !mp->m_rtdev_targp)
474 return __this_address;
475
91b32a08
ES
476 /* check for illegal values of forkoff */
477 fa = xfs_dinode_verify_forkoff(dip, mp);
478 if (fa)
479 return fa;
480
ad42e5a1
DW
481 /* Do we have appropriate data fork formats for the mode? */
482 switch (mode & S_IFMT) {
483 case S_IFIFO:
484 case S_IFCHR:
485 case S_IFBLK:
486 case S_IFSOCK:
487 if (dip->di_format != XFS_DINODE_FMT_DEV)
488 return __this_address;
489 break;
490 case S_IFREG:
491 case S_IFLNK:
492 case S_IFDIR:
8ac50baf
DC
493 fa = xfs_dinode_verify_fork(dip, mp, XFS_DATA_FORK);
494 if (fa)
495 return fa;
ad42e5a1
DW
496 break;
497 case 0:
498 /* Uninitialized inode ok. */
499 break;
500 default:
501 return __this_address;
502 }
503
504 if (XFS_DFORK_Q(dip)) {
8ac50baf
DC
505 fa = xfs_dinode_verify_fork(dip, mp, XFS_ATTR_FORK);
506 if (fa)
507 return fa;
3060f26a
ES
508 } else {
509 /*
510 * If there is no fork offset, this may be a freshly-made inode
511 * in a new disk cluster, in which case di_aformat is zeroed.
512 * Otherwise, such an inode must be in EXTENTS format; this goes
513 * for freed inodes as well.
514 */
515 switch (dip->di_aformat) {
516 case 0:
517 case XFS_DINODE_FMT_EXTENTS:
518 break;
519 default:
520 return __this_address;
521 }
522 if (dip->di_anextents)
523 return __this_address;
ad42e5a1
DW
524 }
525
316d5a9f
DC
526 /* extent size hint validation */
527 fa = xfs_inode_validate_extsize(mp, be32_to_cpu(dip->di_extsize),
528 mode, flags);
529 if (fa)
530 return fa;
531
41ce5f36
DC
532 /* only version 3 or greater inodes are extensively verified here */
533 if (dip->di_version < 3)
bc01119d 534 return NULL;
41ce5f36 535
183537ed
DW
536 flags2 = be64_to_cpu(dip->di_flags2);
537
538 /* don't allow reflink/cowextsize if we don't have reflink */
539 if ((flags2 & (XFS_DIFLAG2_REFLINK | XFS_DIFLAG2_COWEXTSIZE)) &&
316d5a9f 540 !xfs_sb_version_hasreflink(&mp->m_sb))
bc01119d 541 return __this_address;
183537ed 542
ad42e5a1
DW
543 /* only regular files get reflink */
544 if ((flags2 & XFS_DIFLAG2_REFLINK) && (mode & S_IFMT) != S_IFREG)
545 return __this_address;
546
183537ed
DW
547 /* don't let reflink and realtime mix */
548 if ((flags2 & XFS_DIFLAG2_REFLINK) && (flags & XFS_DIFLAG_REALTIME))
bc01119d 549 return __this_address;
183537ed 550
378ed4e7
DW
551 /* don't let reflink and dax mix */
552 if ((flags2 & XFS_DIFLAG2_REFLINK) && (flags2 & XFS_DIFLAG2_DAX))
bc01119d 553 return __this_address;
378ed4e7 554
77e17d58
DC
555 /* COW extent size hint validation */
556 fa = xfs_inode_validate_cowextsize(mp, be32_to_cpu(dip->di_cowextsize),
557 mode, flags, flags2);
558 if (fa)
559 return fa;
560
bc01119d 561 return NULL;
41ce5f36 562}
e6d77a21 563
41ce5f36
DC
564void
565xfs_dinode_calc_crc(
566 struct xfs_mount *mp,
567 struct xfs_dinode *dip)
568{
4a492e72 569 uint32_t crc;
41ce5f36
DC
570
571 if (dip->di_version < 3)
572 return;
573
574 ASSERT(xfs_sb_version_hascrc(&mp->m_sb));
0bb90214 575 crc = xfs_start_cksum_update((char *)dip, mp->m_sb.sb_inodesize,
3faddc54 576 XFS_DINODE_CRC_OFF);
41ce5f36 577 dip->di_crc = xfs_end_cksum(crc);
2bd0ea18
NS
578}
579
a2ceac1f
DC
580/*
581 * Read the disk inode attributes into the in-core inode structure.
ff105f75
DC
582 *
583 * For version 5 superblocks, if we are initialising a new inode and we are not
584 * utilising the XFS_MOUNT_IKEEP inode cluster mode, we can simple build the new
585 * inode core with a random generation number. If we are keeping inodes around,
586 * we need to read the inode cluster to get the existing generation number off
587 * disk. Further, if we are using version 4 superblocks (i.e. v1/v2 inode
588 * format) then log recovery is dependent on the di_flushiter field being
589 * initialised from the current on-disk value and hence we must also read the
590 * inode off disk.
a2ceac1f
DC
591 */
592int
593xfs_iread(
594 xfs_mount_t *mp,
595 xfs_trans_t *tp,
596 xfs_inode_t *ip,
597 uint iget_flags)
598{
599 xfs_buf_t *bp;
600 xfs_dinode_t *dip;
bc01119d 601 xfs_failaddr_t fa;
a2ceac1f
DC
602 int error;
603
604 /*
605 * Fill in the location information in the in-core inode.
606 */
607 error = xfs_imap(mp, tp, ip->i_ino, &ip->i_imap, iget_flags);
608 if (error)
609 return error;
610
ff105f75
DC
611 /* shortcut IO on inode allocation if possible */
612 if ((iget_flags & XFS_IGET_CREATE) &&
db84c7e8 613 xfs_sb_version_has_v3inode(&mp->m_sb) &&
ff105f75 614 !(mp->m_flags & XFS_MOUNT_IKEEP)) {
6652c253 615 VFS_I(ip)->i_generation = prandom_u32();
ff105f75
DC
616 return 0;
617 }
618
a2ceac1f
DC
619 /*
620 * Get pointers to the on-disk inode and the buffer containing it.
621 */
0cd4b54a 622 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &bp, 0);
a2ceac1f
DC
623 if (error)
624 return error;
625
41ce5f36 626 /* even unallocated inodes are verified */
bc01119d
DW
627 fa = xfs_dinode_verify(mp, ip->i_ino, dip);
628 if (fa) {
1d3bac1f
DW
629 xfs_inode_verifier_error(ip, -EFSCORRUPTED, "dinode", dip,
630 sizeof(*dip), fa);
12b53197 631 error = -EFSCORRUPTED;
a2ceac1f
DC
632 goto out_brelse;
633 }
634
635 /*
636 * If the on-disk inode is already linked to a directory
637 * entry, copy all of the inode into the in-core inode.
5d90ab5a 638 * xfs_iformat_fork() handles copying in the inode format
a2ceac1f
DC
639 * specific information.
640 * Otherwise, just get the truly permanent information.
641 */
642 if (dip->di_mode) {
3eb685ae 643 error = xfs_inode_from_disk(ip, dip);
a2ceac1f
DC
644 if (error) {
645#ifdef DEBUG
646 xfs_alert(mp, "%s: xfs_iformat() returned error %d",
647 __func__, error);
648#endif /* DEBUG */
649 goto out_brelse;
650 }
651 } else {
41ce5f36
DC
652 /*
653 * Partial initialisation of the in-core inode. Just the bits
654 * that xfs_ialloc won't overwrite or relies on being correct.
655 */
6652c253 656 VFS_I(ip)->i_generation = be32_to_cpu(dip->di_gen);
a2ceac1f 657 ip->i_d.di_flushiter = be16_to_cpu(dip->di_flushiter);
41ce5f36 658
a2ceac1f
DC
659 /*
660 * Make sure to pull in the mode here as well in
661 * case the inode is released without being used.
662 * This ensures that xfs_inactive() will see that
663 * the inode is already free and not try to mess
664 * with the uninitialized part of it.
665 */
e37bf53c 666 VFS_I(ip)->i_mode = 0;
a2ceac1f
DC
667 }
668
a2ceac1f
DC
669 ip->i_delayed_blks = 0;
670
671 /*
672 * Mark the buffer containing the inode as something to keep
673 * around for a while. This helps to keep recently accessed
674 * meta-data in-core longer.
675 */
676 xfs_buf_set_ref(bp, XFS_INO_REF);
677
678 /*
1d519883
DC
679 * Use xfs_trans_brelse() to release the buffer containing the on-disk
680 * inode, because it was acquired with xfs_trans_read_buf() in
681 * xfs_imap_to_bp() above. If tp is NULL, this is just a normal
a2ceac1f
DC
682 * brelse(). If we're within a transaction, then xfs_trans_brelse()
683 * will only release the buffer if it is not dirty within the
684 * transaction. It will be OK to release the buffer in this case,
1d519883
DC
685 * because inodes on disk are never destroyed and we will be locking the
686 * new in-core inode before putting it in the cache where other
687 * processes can find it. Thus we don't have to worry about the inode
688 * being changed just because we released the buffer.
a2ceac1f
DC
689 */
690 out_brelse:
691 xfs_trans_brelse(tp, bp);
692 return error;
693}
fd0c360d
DW
694
695/*
696 * Validate di_extsize hint.
697 *
698 * The rules are documented at xfs_ioctl_setattr_check_extsize().
699 * These functions must be kept in sync with each other.
700 */
701xfs_failaddr_t
702xfs_inode_validate_extsize(
703 struct xfs_mount *mp,
704 uint32_t extsize,
705 uint16_t mode,
706 uint16_t flags)
707{
708 bool rt_flag;
709 bool hint_flag;
710 bool inherit_flag;
711 uint32_t extsize_bytes;
712 uint32_t blocksize_bytes;
713
714 rt_flag = (flags & XFS_DIFLAG_REALTIME);
715 hint_flag = (flags & XFS_DIFLAG_EXTSIZE);
716 inherit_flag = (flags & XFS_DIFLAG_EXTSZINHERIT);
717 extsize_bytes = XFS_FSB_TO_B(mp, extsize);
718
719 if (rt_flag)
720 blocksize_bytes = mp->m_sb.sb_rextsize << mp->m_sb.sb_blocklog;
721 else
722 blocksize_bytes = mp->m_sb.sb_blocksize;
723
724 if ((hint_flag || inherit_flag) && !(S_ISDIR(mode) || S_ISREG(mode)))
725 return __this_address;
726
727 if (hint_flag && !S_ISREG(mode))
728 return __this_address;
729
730 if (inherit_flag && !S_ISDIR(mode))
731 return __this_address;
732
733 if ((hint_flag || inherit_flag) && extsize == 0)
734 return __this_address;
735
206bef93
ES
736 /* free inodes get flags set to zero but extsize remains */
737 if (mode && !(hint_flag || inherit_flag) && extsize != 0)
fd0c360d
DW
738 return __this_address;
739
740 if (extsize_bytes % blocksize_bytes)
741 return __this_address;
742
743 if (extsize > MAXEXTLEN)
744 return __this_address;
745
746 if (!rt_flag && extsize > mp->m_sb.sb_agblocks / 2)
747 return __this_address;
748
749 return NULL;
750}
751
752/*
753 * Validate di_cowextsize hint.
754 *
755 * The rules are documented at xfs_ioctl_setattr_check_cowextsize().
756 * These functions must be kept in sync with each other.
757 */
758xfs_failaddr_t
759xfs_inode_validate_cowextsize(
760 struct xfs_mount *mp,
761 uint32_t cowextsize,
762 uint16_t mode,
763 uint16_t flags,
764 uint64_t flags2)
765{
766 bool rt_flag;
767 bool hint_flag;
768 uint32_t cowextsize_bytes;
769
770 rt_flag = (flags & XFS_DIFLAG_REALTIME);
771 hint_flag = (flags2 & XFS_DIFLAG2_COWEXTSIZE);
772 cowextsize_bytes = XFS_FSB_TO_B(mp, cowextsize);
773
774 if (hint_flag && !xfs_sb_version_hasreflink(&mp->m_sb))
775 return __this_address;
776
777 if (hint_flag && !(S_ISDIR(mode) || S_ISREG(mode)))
778 return __this_address;
779
780 if (hint_flag && cowextsize == 0)
781 return __this_address;
782
206bef93
ES
783 /* free inodes get flags set to zero but cowextsize remains */
784 if (mode && !hint_flag && cowextsize != 0)
fd0c360d
DW
785 return __this_address;
786
787 if (hint_flag && rt_flag)
788 return __this_address;
789
790 if (cowextsize_bytes % mp->m_sb.sb_blocksize)
791 return __this_address;
792
793 if (cowextsize > MAXEXTLEN)
794 return __this_address;
795
796 if (cowextsize > mp->m_sb.sb_agblocks / 2)
797 return __this_address;
798
799 return NULL;
800}