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