Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0
2 : /*
3 : * Copyright (c) 2000-2006 Silicon Graphics, Inc.
4 : * All Rights Reserved.
5 : */
6 : #include "xfs.h"
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_ag.h"
14 : #include "xfs_inode.h"
15 : #include "xfs_errortag.h"
16 : #include "xfs_error.h"
17 : #include "xfs_icache.h"
18 : #include "xfs_trans.h"
19 : #include "xfs_ialloc.h"
20 : #include "xfs_dir2.h"
21 : #include "xfs_health.h"
22 :
23 : #include <linux/iversion.h>
24 :
25 : /*
26 : * If we are doing readahead on an inode buffer, we might be in log recovery
27 : * reading an inode allocation buffer that hasn't yet been replayed, and hence
28 : * has not had the inode cores stamped into it. Hence for readahead, the buffer
29 : * may be potentially invalid.
30 : *
31 : * If the readahead buffer is invalid, we need to mark it with an error and
32 : * clear the DONE status of the buffer so that a followup read will re-read it
33 : * from disk. We don't report the error otherwise to avoid warnings during log
34 : * recovery and we don't get unnecessary panics on debug kernels. We use EIO here
35 : * because all we want to do is say readahead failed; there is no-one to report
36 : * the error to, so this will distinguish it from a non-ra verifier failure.
37 : * Changes to this readahead error behaviour also need to be reflected in
38 : * xfs_dquot_buf_readahead_verify().
39 : */
40 : static void
41 46116901 : xfs_inode_buf_verify(
42 : struct xfs_buf *bp,
43 : bool readahead)
44 : {
45 46116901 : struct xfs_mount *mp = bp->b_mount;
46 46116901 : int i;
47 46116901 : int ni;
48 :
49 : /*
50 : * Validate the magic number and version of every inode in the buffer
51 : */
52 46116901 : ni = XFS_BB_TO_FSB(mp, bp->b_length) * mp->m_sb.sb_inopblock;
53 1521706318 : for (i = 0; i < ni; i++) {
54 1475591815 : struct xfs_dinode *dip;
55 1475591815 : xfs_agino_t unlinked_ino;
56 1475591815 : int di_ok;
57 :
58 1475591815 : dip = xfs_buf_offset(bp, (i << mp->m_sb.sb_inodelog));
59 1475476848 : unlinked_ino = be32_to_cpu(dip->di_next_unlinked);
60 2951060713 : di_ok = xfs_verify_magic16(bp, dip->di_magic) &&
61 1475586431 : xfs_dinode_good_version(mp, dip->di_version) &&
62 1475584480 : xfs_verify_agino_or_null(bp->b_pag, unlinked_ino);
63 1475586431 : if (unlikely(XFS_TEST_ERROR(!di_ok, mp,
64 : XFS_ERRTAG_ITOBP_INOTOBP))) {
65 2649 : if (readahead) {
66 2255 : bp->b_flags &= ~XBF_DONE;
67 2255 : xfs_buf_ioerror(bp, -EIO);
68 2255 : return;
69 : }
70 :
71 : #ifdef DEBUG
72 394 : xfs_alert(mp,
73 : "bad inode magic/vsn daddr %lld #%d (magic=%x)",
74 : (unsigned long long)xfs_buf_daddr(bp), i,
75 : be16_to_cpu(dip->di_magic));
76 : #endif
77 394 : xfs_buf_verifier_error(bp, -EFSCORRUPTED,
78 : __func__, dip, sizeof(*dip),
79 : NULL);
80 394 : return;
81 : }
82 : }
83 : }
84 :
85 :
86 : static void
87 11545959 : xfs_inode_buf_read_verify(
88 : struct xfs_buf *bp)
89 : {
90 11545959 : xfs_inode_buf_verify(bp, false);
91 11545960 : }
92 :
93 : static void
94 271807 : xfs_inode_buf_readahead_verify(
95 : struct xfs_buf *bp)
96 : {
97 271807 : xfs_inode_buf_verify(bp, true);
98 271807 : }
99 :
100 : static void
101 34299612 : xfs_inode_buf_write_verify(
102 : struct xfs_buf *bp)
103 : {
104 34299612 : xfs_inode_buf_verify(bp, false);
105 34299612 : }
106 :
107 : const struct xfs_buf_ops xfs_inode_buf_ops = {
108 : .name = "xfs_inode",
109 : .magic16 = { cpu_to_be16(XFS_DINODE_MAGIC),
110 : cpu_to_be16(XFS_DINODE_MAGIC) },
111 : .verify_read = xfs_inode_buf_read_verify,
112 : .verify_write = xfs_inode_buf_write_verify,
113 : };
114 :
115 : const struct xfs_buf_ops xfs_inode_buf_ra_ops = {
116 : .name = "xfs_inode_ra",
117 : .magic16 = { cpu_to_be16(XFS_DINODE_MAGIC),
118 : cpu_to_be16(XFS_DINODE_MAGIC) },
119 : .verify_read = xfs_inode_buf_readahead_verify,
120 : .verify_write = xfs_inode_buf_write_verify,
121 : };
122 :
123 :
124 : /*
125 : * This routine is called to map an inode to the buffer containing the on-disk
126 : * version of the inode. It returns a pointer to the buffer containing the
127 : * on-disk inode in the bpp parameter.
128 : */
129 : int
130 744945270 : xfs_imap_to_bp(
131 : struct xfs_mount *mp,
132 : struct xfs_trans *tp,
133 : struct xfs_imap *imap,
134 : struct xfs_buf **bpp)
135 : {
136 744945270 : int error;
137 :
138 744945270 : error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, imap->im_blkno,
139 744945270 : imap->im_len, XBF_UNMAPPED, bpp, &xfs_inode_buf_ops);
140 745012030 : if (xfs_metadata_is_sick(error))
141 384 : xfs_agno_mark_sick(mp, xfs_daddr_to_agno(mp, imap->im_blkno),
142 : XFS_SICK_AG_INODES);
143 745012030 : return error;
144 : }
145 :
146 : static inline struct timespec64 xfs_inode_decode_bigtime(uint64_t ts)
147 : {
148 2134732158 : struct timespec64 tv;
149 2134732158 : uint32_t n;
150 :
151 2134732158 : tv.tv_sec = xfs_bigtime_to_unix(div_u64_rem(ts, NSEC_PER_SEC, &n));
152 2134732158 : tv.tv_nsec = n;
153 :
154 2134732158 : return tv;
155 : }
156 :
157 : /* Convert an ondisk timestamp to an incore timestamp. */
158 : struct timespec64
159 2134808707 : xfs_inode_from_disk_ts(
160 : struct xfs_dinode *dip,
161 : const xfs_timestamp_t ts)
162 : {
163 2134808707 : struct timespec64 tv;
164 2134808707 : struct xfs_legacy_timestamp *lts;
165 :
166 4269617414 : if (xfs_dinode_has_bigtime(dip))
167 2134732158 : return xfs_inode_decode_bigtime(be64_to_cpu(ts));
168 :
169 76549 : lts = (struct xfs_legacy_timestamp *)&ts;
170 76549 : tv.tv_sec = (int)be32_to_cpu(lts->t_sec);
171 76549 : tv.tv_nsec = (int)be32_to_cpu(lts->t_nsec);
172 :
173 76549 : return tv;
174 : }
175 :
176 : int
177 430496323 : xfs_inode_from_disk(
178 : struct xfs_inode *ip,
179 : struct xfs_dinode *from)
180 : {
181 430496323 : struct inode *inode = VFS_I(ip);
182 430496323 : int error;
183 430496323 : xfs_failaddr_t fa;
184 :
185 430496323 : ASSERT(ip->i_cowfp == NULL);
186 :
187 430496323 : fa = xfs_dinode_verify(ip->i_mount, ip->i_ino, from);
188 430484550 : if (fa) {
189 198 : xfs_inode_verifier_error(ip, -EFSCORRUPTED, "dinode", from,
190 : sizeof(*from), fa);
191 198 : return -EFSCORRUPTED;
192 : }
193 :
194 : /*
195 : * First get the permanent information that is needed to allocate an
196 : * inode. If the inode is unused, mode is zero and we shouldn't mess
197 : * with the uninitialized part of it.
198 : */
199 430484352 : if (!xfs_has_v3inodes(ip->i_mount))
200 832 : ip->i_flushiter = be16_to_cpu(from->di_flushiter);
201 430484352 : inode->i_generation = be32_to_cpu(from->di_gen);
202 430484352 : inode->i_mode = be16_to_cpu(from->di_mode);
203 430484352 : if (!inode->i_mode)
204 : return 0;
205 :
206 : /*
207 : * Convert v1 inodes immediately to v2 inode format as this is the
208 : * minimum inode version format we support in the rest of the code.
209 : * They will also be unconditionally written back to disk as v2 inodes.
210 : */
211 430488625 : if (unlikely(from->di_version == 1)) {
212 0 : set_nlink(inode, be16_to_cpu(from->di_onlink));
213 0 : ip->i_projid = 0;
214 : } else {
215 860977250 : set_nlink(inode, be32_to_cpu(from->di_nlink));
216 860982434 : ip->i_projid = (prid_t)be16_to_cpu(from->di_projid_hi) << 16 |
217 430491217 : be16_to_cpu(from->di_projid_lo);
218 : }
219 :
220 430491217 : i_uid_write(inode, be32_to_cpu(from->di_uid));
221 430498568 : i_gid_write(inode, be32_to_cpu(from->di_gid));
222 :
223 : /*
224 : * Time is signed, so need to convert to signed 32 bit before
225 : * storing in inode timestamp which may be 64 bit. Otherwise
226 : * a time before epoch is converted to a time long after epoch
227 : * on 64 bit systems.
228 : */
229 430478224 : inode->i_atime = xfs_inode_from_disk_ts(from, from->di_atime);
230 430478224 : inode->i_mtime = xfs_inode_from_disk_ts(from, from->di_mtime);
231 430478224 : inode->i_ctime = xfs_inode_from_disk_ts(from, from->di_ctime);
232 :
233 430478224 : ip->i_disk_size = be64_to_cpu(from->di_size);
234 430478224 : ip->i_nblocks = be64_to_cpu(from->di_nblocks);
235 430478224 : ip->i_extsize = be32_to_cpu(from->di_extsize);
236 430478224 : ip->i_forkoff = from->di_forkoff;
237 430478224 : ip->i_diflags = be16_to_cpu(from->di_flags);
238 430478224 : ip->i_next_unlinked = be32_to_cpu(from->di_next_unlinked);
239 :
240 430478224 : if (from->di_dmevmask || from->di_dmstate)
241 0 : xfs_iflags_set(ip, XFS_IPRESERVE_DM_FIELDS);
242 :
243 430483899 : if (xfs_has_v3inodes(ip->i_mount)) {
244 430483743 : inode_set_iversion_queried(inode,
245 430483743 : be64_to_cpu(from->di_changecount));
246 430483743 : ip->i_crtime = xfs_inode_from_disk_ts(from, from->di_crtime);
247 430483743 : ip->i_diflags2 = be64_to_cpu(from->di_flags2);
248 860967486 : ip->i_cowextsize = be32_to_cpu(from->di_cowextsize);
249 : }
250 :
251 430483899 : error = xfs_iformat_data_fork(ip, from);
252 430486196 : if (error)
253 : return error;
254 430486192 : if (from->di_forkoff) {
255 430377455 : error = xfs_iformat_attr_fork(ip, from);
256 430374050 : if (error)
257 0 : goto out_destroy_data_fork;
258 : }
259 430482787 : if (xfs_is_reflink_inode(ip))
260 94736031 : xfs_ifork_init_cow(ip);
261 : return 0;
262 :
263 : out_destroy_data_fork:
264 0 : xfs_idestroy_fork(&ip->i_df);
265 0 : return error;
266 : }
267 :
268 : /* Convert an incore timestamp to an ondisk timestamp. */
269 : static inline xfs_timestamp_t
270 1154725036 : xfs_inode_to_disk_ts(
271 : struct xfs_inode *ip,
272 : const struct timespec64 tv)
273 : {
274 1154725036 : struct xfs_legacy_timestamp *lts;
275 1154725036 : xfs_timestamp_t ts;
276 :
277 1154725036 : if (xfs_inode_has_bigtime(ip))
278 1154650867 : return cpu_to_be64(xfs_inode_encode_bigtime(tv));
279 :
280 74169 : lts = (struct xfs_legacy_timestamp *)&ts;
281 74169 : lts->t_sec = cpu_to_be32(tv.tv_sec);
282 74169 : lts->t_nsec = cpu_to_be32(tv.tv_nsec);
283 :
284 74169 : return ts;
285 : }
286 :
287 : static inline void
288 288676344 : xfs_inode_to_disk_iext_counters(
289 : struct xfs_inode *ip,
290 : struct xfs_dinode *to)
291 : {
292 288676344 : if (xfs_inode_has_large_extent_counts(ip)) {
293 288676016 : to->di_big_nextents = cpu_to_be64(xfs_ifork_nextents(&ip->i_df));
294 577352032 : to->di_big_anextents = cpu_to_be32(xfs_ifork_nextents(&ip->i_af));
295 : /*
296 : * We might be upgrading the inode to use larger extent counters
297 : * than was previously used. Hence zero the unused field.
298 : */
299 288676016 : to->di_nrext64_pad = cpu_to_be16(0);
300 : } else {
301 656 : to->di_nextents = cpu_to_be32(xfs_ifork_nextents(&ip->i_df));
302 656 : to->di_anextents = cpu_to_be16(xfs_ifork_nextents(&ip->i_af));
303 : }
304 288676344 : }
305 :
306 : void
307 288673957 : xfs_inode_to_disk(
308 : struct xfs_inode *ip,
309 : struct xfs_dinode *to,
310 : xfs_lsn_t lsn)
311 : {
312 288673957 : struct inode *inode = VFS_I(ip);
313 :
314 288673957 : to->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
315 288673957 : to->di_onlink = 0;
316 :
317 288673957 : to->di_format = xfs_ifork_format(&ip->i_df);
318 288673957 : to->di_uid = cpu_to_be32(i_uid_read(inode));
319 288692472 : to->di_gid = cpu_to_be32(i_gid_read(inode));
320 288683724 : to->di_projid_lo = cpu_to_be16(ip->i_projid & 0xffff);
321 288683724 : to->di_projid_hi = cpu_to_be16(ip->i_projid >> 16);
322 :
323 288683724 : to->di_atime = xfs_inode_to_disk_ts(ip, inode->i_atime);
324 288683724 : to->di_mtime = xfs_inode_to_disk_ts(ip, inode->i_mtime);
325 288683724 : to->di_ctime = xfs_inode_to_disk_ts(ip, inode->i_ctime);
326 288683724 : to->di_nlink = cpu_to_be32(inode->i_nlink);
327 288683724 : to->di_gen = cpu_to_be32(inode->i_generation);
328 288683724 : to->di_mode = cpu_to_be16(inode->i_mode);
329 :
330 288683724 : to->di_size = cpu_to_be64(ip->i_disk_size);
331 288683724 : to->di_nblocks = cpu_to_be64(ip->i_nblocks);
332 288683724 : to->di_extsize = cpu_to_be32(ip->i_extsize);
333 288683724 : to->di_forkoff = ip->i_forkoff;
334 288683724 : to->di_aformat = xfs_ifork_format(&ip->i_af);
335 288683724 : to->di_flags = cpu_to_be16(ip->i_diflags);
336 :
337 288683724 : if (xfs_has_v3inodes(ip->i_mount)) {
338 288683466 : to->di_version = 3;
339 288683466 : to->di_changecount = cpu_to_be64(inode_peek_iversion(inode));
340 288683466 : to->di_crtime = xfs_inode_to_disk_ts(ip, ip->i_crtime);
341 288683466 : to->di_flags2 = cpu_to_be64(ip->i_diflags2);
342 288683466 : to->di_cowextsize = cpu_to_be32(ip->i_cowextsize);
343 288683466 : to->di_ino = cpu_to_be64(ip->i_ino);
344 288683466 : to->di_lsn = cpu_to_be64(lsn);
345 288683466 : memset(to->di_pad2, 0, sizeof(to->di_pad2));
346 288683466 : uuid_copy(&to->di_uuid, &ip->i_mount->m_sb.sb_meta_uuid);
347 288685415 : to->di_v3_pad = 0;
348 : } else {
349 258 : to->di_version = 2;
350 258 : to->di_flushiter = cpu_to_be16(ip->i_flushiter);
351 516 : memset(to->di_v2_pad, 0, sizeof(to->di_v2_pad));
352 : }
353 :
354 288685673 : xfs_inode_to_disk_iext_counters(ip, to);
355 288676542 : }
356 :
357 : static xfs_failaddr_t
358 679624771 : xfs_dinode_verify_fork(
359 : struct xfs_dinode *dip,
360 : struct xfs_mount *mp,
361 : int whichfork)
362 : {
363 679624771 : xfs_extnum_t di_nextents;
364 679624771 : xfs_extnum_t max_extents;
365 679624771 : mode_t mode = be16_to_cpu(dip->di_mode);
366 679624929 : uint32_t fork_size = XFS_DFORK_SIZE(dip, mp, whichfork);
367 679624771 : uint32_t fork_format = XFS_DFORK_FORMAT(dip, whichfork);
368 :
369 679624771 : di_nextents = xfs_dfork_nextents(dip, whichfork);
370 :
371 : /*
372 : * For fork types that can contain local data, check that the fork
373 : * format matches the size of local data contained within the fork.
374 : *
375 : * For all types, check that when the size says the should be in extent
376 : * or btree format, the inode isn't claiming it is in local format.
377 : */
378 679636090 : if (whichfork == XFS_DATA_FORK) {
379 249272586 : if (S_ISDIR(mode) || S_ISLNK(mode)) {
380 93612705 : if (be64_to_cpu(dip->di_size) <= fork_size &&
381 : fork_format != XFS_DINODE_FMT_LOCAL)
382 6 : return __this_address;
383 : }
384 :
385 249272580 : if (be64_to_cpu(dip->di_size) > fork_size &&
386 : fork_format == XFS_DINODE_FMT_LOCAL)
387 0 : return __this_address;
388 : }
389 :
390 679636084 : switch (fork_format) {
391 499032743 : case XFS_DINODE_FMT_LOCAL:
392 : /*
393 : * No local regular files yet.
394 : */
395 499032743 : if (S_ISREG(mode) && whichfork == XFS_DATA_FORK)
396 4 : return __this_address;
397 499032739 : if (di_nextents)
398 0 : return __this_address;
399 : break;
400 169597344 : case XFS_DINODE_FMT_EXTENTS:
401 169597454 : if (di_nextents > XFS_DFORK_MAXEXT(dip, mp, whichfork))
402 0 : return __this_address;
403 : break;
404 : case XFS_DINODE_FMT_BTREE:
405 22011978 : max_extents = xfs_iext_max_nextents(
406 : xfs_dinode_has_large_extent_counts(dip),
407 : whichfork);
408 11005996 : if (di_nextents > max_extents)
409 0 : return __this_address;
410 : break;
411 : default:
412 8 : return __this_address;
413 : }
414 : return NULL;
415 : }
416 :
417 : static xfs_failaddr_t
418 430493993 : xfs_dinode_verify_forkoff(
419 : struct xfs_dinode *dip,
420 : struct xfs_mount *mp)
421 : {
422 430493993 : if (!dip->di_forkoff)
423 : return NULL;
424 :
425 430381633 : switch (dip->di_format) {
426 181216803 : case XFS_DINODE_FMT_DEV:
427 181216803 : if (dip->di_forkoff != (roundup(sizeof(xfs_dev_t), 8) >> 3))
428 0 : return __this_address;
429 : break;
430 249164830 : case XFS_DINODE_FMT_LOCAL: /* fall through ... */
431 : case XFS_DINODE_FMT_EXTENTS: /* fall through ... */
432 : case XFS_DINODE_FMT_BTREE:
433 249164832 : if (dip->di_forkoff >= (XFS_LITINO(mp) >> 3))
434 0 : return __this_address;
435 : break;
436 : default:
437 0 : return __this_address;
438 : }
439 : return NULL;
440 : }
441 :
442 : static xfs_failaddr_t
443 430480201 : xfs_dinode_verify_nrext64(
444 : struct xfs_mount *mp,
445 : struct xfs_dinode *dip)
446 : {
447 860960402 : if (xfs_dinode_has_large_extent_counts(dip)) {
448 430479721 : if (!xfs_has_large_extent_counts(mp))
449 0 : return __this_address;
450 430479721 : if (dip->di_nrext64_pad != 0)
451 0 : return __this_address;
452 480 : } else if (dip->di_version >= 3) {
453 62 : if (dip->di_v3_pad != 0)
454 0 : return __this_address;
455 : }
456 :
457 : return NULL;
458 : }
459 :
460 : xfs_failaddr_t
461 430475825 : xfs_dinode_verify(
462 : struct xfs_mount *mp,
463 : xfs_ino_t ino,
464 : struct xfs_dinode *dip)
465 : {
466 430475825 : xfs_failaddr_t fa;
467 430475825 : uint16_t mode;
468 430475825 : uint16_t flags;
469 430475825 : uint64_t flags2;
470 430475825 : uint64_t di_size;
471 430475825 : xfs_extnum_t nextents;
472 430475825 : xfs_extnum_t naextents;
473 430475825 : xfs_filblks_t nblocks;
474 :
475 430475825 : if (dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC))
476 0 : return __this_address;
477 :
478 : /* Verify v3 integrity information first */
479 430475825 : if (dip->di_version >= 3) {
480 430478195 : if (!xfs_has_v3inodes(mp))
481 0 : return __this_address;
482 430478195 : if (!xfs_verify_cksum((char *)dip, mp->m_sb.sb_inodesize,
483 : XFS_DINODE_CRC_OFF))
484 6 : return __this_address;
485 430469365 : if (be64_to_cpu(dip->di_ino) != ino)
486 0 : return __this_address;
487 430485655 : if (!uuid_equal(&dip->di_uuid, &mp->m_sb.sb_meta_uuid))
488 0 : return __this_address;
489 : }
490 :
491 430486897 : if (dip->di_version > 1) {
492 430486897 : if (dip->di_onlink)
493 0 : return __this_address;
494 : } else {
495 0 : if (dip->di_nlink)
496 0 : return __this_address;
497 : }
498 :
499 : /* don't allow invalid i_size */
500 430486897 : di_size = be64_to_cpu(dip->di_size);
501 430486897 : if (di_size & (1ULL << 63))
502 0 : return __this_address;
503 :
504 430486897 : mode = be16_to_cpu(dip->di_mode);
505 430486897 : if (mode && xfs_mode_to_ftype(mode) == XFS_DIR3_FT_UNKNOWN)
506 126 : return __this_address;
507 :
508 : /* No zero-length symlinks/dirs. */
509 430484470 : if ((S_ISLNK(mode) || S_ISDIR(mode)) && di_size == 0)
510 16 : return __this_address;
511 :
512 430484454 : fa = xfs_dinode_verify_nrext64(mp, dip);
513 430486752 : if (fa)
514 : return fa;
515 :
516 430481880 : nextents = xfs_dfork_data_extents(dip);
517 430481880 : naextents = xfs_dfork_attr_extents(dip);
518 430481880 : nblocks = be64_to_cpu(dip->di_nblocks);
519 :
520 : /* Fork checks carried over from xfs_iformat_fork */
521 430481880 : if (mode && nextents + naextents > nblocks)
522 0 : return __this_address;
523 :
524 430481880 : if (S_ISDIR(mode) && nextents > mp->m_dir_geo->max_extents)
525 0 : return __this_address;
526 :
527 430481880 : if (mode && XFS_DFORK_BOFF(dip) > mp->m_sb.sb_inodesize)
528 0 : return __this_address;
529 :
530 430481880 : flags = be16_to_cpu(dip->di_flags);
531 :
532 430481880 : if (mode && (flags & XFS_DIFLAG_REALTIME) && !mp->m_rtdev_targp)
533 0 : return __this_address;
534 :
535 : /* check for illegal values of forkoff */
536 430481880 : fa = xfs_dinode_verify_forkoff(dip, mp);
537 430468975 : if (fa)
538 : return fa;
539 :
540 : /* Do we have appropriate data fork formats for the mode? */
541 430464583 : switch (mode & S_IFMT) {
542 181222064 : case S_IFIFO:
543 : case S_IFCHR:
544 : case S_IFBLK:
545 : case S_IFSOCK:
546 181222064 : if (dip->di_format != XFS_DINODE_FMT_DEV)
547 32 : return __this_address;
548 : break;
549 249272625 : case S_IFREG:
550 : case S_IFLNK:
551 : case S_IFDIR:
552 249272625 : fa = xfs_dinode_verify_fork(dip, mp, XFS_DATA_FORK);
553 249272431 : if (fa)
554 : return fa;
555 : break;
556 : case 0:
557 : /* Uninitialized inode ok. */
558 : break;
559 : default:
560 0 : return __this_address;
561 : }
562 :
563 430463333 : if (dip->di_forkoff) {
564 430371428 : fa = xfs_dinode_verify_fork(dip, mp, XFS_ATTR_FORK);
565 430381142 : if (fa)
566 : return fa;
567 : } else {
568 : /*
569 : * If there is no fork offset, this may be a freshly-made inode
570 : * in a new disk cluster, in which case di_aformat is zeroed.
571 : * Otherwise, such an inode must be in EXTENTS format; this goes
572 : * for freed inodes as well.
573 : */
574 112551 : switch (dip->di_aformat) {
575 : case 0:
576 : case XFS_DINODE_FMT_EXTENTS:
577 112551 : break;
578 : default:
579 0 : return __this_address;
580 : }
581 112551 : if (naextents)
582 0 : return __this_address;
583 : }
584 :
585 : /* extent size hint validation */
586 860940380 : fa = xfs_inode_validate_extsize(mp, be32_to_cpu(dip->di_extsize),
587 : mode, flags);
588 430490330 : if (fa)
589 : return fa;
590 :
591 : /* only version 3 or greater inodes are extensively verified here */
592 430474158 : if (dip->di_version < 3)
593 : return NULL;
594 :
595 430473742 : flags2 = be64_to_cpu(dip->di_flags2);
596 :
597 : /* don't allow reflink/cowextsize if we don't have reflink */
598 430473742 : if ((flags2 & (XFS_DIFLAG2_REFLINK | XFS_DIFLAG2_COWEXTSIZE)) &&
599 : !xfs_has_reflink(mp))
600 0 : return __this_address;
601 :
602 : /* only regular files get reflink */
603 430473742 : if ((flags2 & XFS_DIFLAG2_REFLINK) && (mode & S_IFMT) != S_IFREG)
604 0 : return __this_address;
605 :
606 : /* don't let reflink and realtime mix */
607 430473742 : if ((flags2 & XFS_DIFLAG2_REFLINK) && (flags & XFS_DIFLAG_REALTIME))
608 0 : return __this_address;
609 :
610 : /* COW extent size hint validation */
611 860947484 : fa = xfs_inode_validate_cowextsize(mp, be32_to_cpu(dip->di_cowextsize),
612 : mode, flags, flags2);
613 430490678 : if (fa)
614 : return fa;
615 :
616 : /* bigtime iflag can only happen on bigtime filesystems */
617 860976642 : if (xfs_dinode_has_bigtime(dip) &&
618 : !xfs_has_bigtime(mp))
619 0 : return __this_address;
620 :
621 : return NULL;
622 : }
623 :
624 : void
625 241038864 : xfs_dinode_calc_crc(
626 : struct xfs_mount *mp,
627 : struct xfs_dinode *dip)
628 : {
629 241038864 : uint32_t crc;
630 :
631 241038864 : if (dip->di_version < 3)
632 : return;
633 :
634 241038622 : ASSERT(xfs_has_crc(mp));
635 241038622 : crc = xfs_start_cksum_update((char *)dip, mp->m_sb.sb_inodesize,
636 : XFS_DINODE_CRC_OFF);
637 241037961 : dip->di_crc = xfs_end_cksum(crc);
638 : }
639 :
640 : /*
641 : * Validate di_extsize hint.
642 : *
643 : * 1. Extent size hint is only valid for directories and regular files.
644 : * 2. FS_XFLAG_EXTSIZE is only valid for regular files.
645 : * 3. FS_XFLAG_EXTSZINHERIT is only valid for directories.
646 : * 4. Hint cannot be larger than MAXTEXTLEN.
647 : * 5. Can be changed on directories at any time.
648 : * 6. Hint value of 0 turns off hints, clears inode flags.
649 : * 7. Extent size must be a multiple of the appropriate block size.
650 : * For realtime files, this is the rt extent size.
651 : * 8. For non-realtime files, the extent size hint must be limited
652 : * to half the AG size to avoid alignment extending the extent beyond the
653 : * limits of the AG.
654 : */
655 : xfs_failaddr_t
656 546532560 : xfs_inode_validate_extsize(
657 : struct xfs_mount *mp,
658 : uint32_t extsize,
659 : uint16_t mode,
660 : uint16_t flags)
661 : {
662 546532560 : bool rt_flag;
663 546532560 : bool hint_flag;
664 546532560 : bool inherit_flag;
665 546532560 : uint32_t extsize_bytes;
666 546532560 : uint32_t blocksize_bytes;
667 :
668 546532560 : rt_flag = (flags & XFS_DIFLAG_REALTIME);
669 546532560 : hint_flag = (flags & XFS_DIFLAG_EXTSIZE);
670 546532560 : inherit_flag = (flags & XFS_DIFLAG_EXTSZINHERIT);
671 546532560 : extsize_bytes = XFS_FSB_TO_B(mp, extsize);
672 :
673 : /*
674 : * This comment describes a historic gap in this verifier function.
675 : *
676 : * For a directory with both RTINHERIT and EXTSZINHERIT flags set, this
677 : * function has never checked that the extent size hint is an integer
678 : * multiple of the realtime extent size. Since we allow users to set
679 : * this combination on non-rt filesystems /and/ to change the rt
680 : * extent size when adding a rt device to a filesystem, the net effect
681 : * is that users can configure a filesystem anticipating one rt
682 : * geometry and change their minds later. Directories do not use the
683 : * extent size hint, so this is harmless for them.
684 : *
685 : * If a directory with a misaligned extent size hint is allowed to
686 : * propagate that hint into a new regular realtime file, the result
687 : * is that the inode cluster buffer verifier will trigger a corruption
688 : * shutdown the next time it is run, because the verifier has always
689 : * enforced the alignment rule for regular files.
690 : *
691 : * Because we allow administrators to set a new rt extent size when
692 : * adding a rt section, we cannot add a check to this verifier because
693 : * that will result a new source of directory corruption errors when
694 : * reading an existing filesystem. Instead, we rely on callers to
695 : * decide when alignment checks are appropriate, and fix things up as
696 : * needed.
697 : */
698 :
699 546532560 : if (rt_flag)
700 29018675 : blocksize_bytes = XFS_FSB_TO_B(mp, mp->m_sb.sb_rextsize);
701 : else
702 517513885 : blocksize_bytes = mp->m_sb.sb_blocksize;
703 :
704 546532560 : if ((hint_flag || inherit_flag) && !(S_ISDIR(mode) || S_ISREG(mode)))
705 0 : return __this_address;
706 :
707 546532560 : if (hint_flag && !S_ISREG(mode))
708 0 : return __this_address;
709 :
710 546532560 : if (inherit_flag && !S_ISDIR(mode))
711 0 : return __this_address;
712 :
713 546532560 : if ((hint_flag || inherit_flag) && extsize == 0)
714 0 : return __this_address;
715 :
716 : /* free inodes get flags set to zero but extsize remains */
717 546532560 : if (mode && !(hint_flag || inherit_flag) && extsize != 0)
718 0 : return __this_address;
719 :
720 546532560 : if (extsize_bytes % blocksize_bytes)
721 0 : return __this_address;
722 :
723 546532560 : if (extsize > XFS_MAX_BMBT_EXTLEN)
724 0 : return __this_address;
725 :
726 546532560 : if (!rt_flag && extsize > mp->m_sb.sb_agblocks / 2)
727 0 : return __this_address;
728 :
729 : return NULL;
730 : }
731 :
732 : /*
733 : * Validate di_cowextsize hint.
734 : *
735 : * 1. CoW extent size hint can only be set if reflink is enabled on the fs.
736 : * The inode does not have to have any shared blocks, but it must be a v3.
737 : * 2. FS_XFLAG_COWEXTSIZE is only valid for directories and regular files;
738 : * for a directory, the hint is propagated to new files.
739 : * 3. Can be changed on files & directories at any time.
740 : * 4. Hint value of 0 turns off hints, clears inode flags.
741 : * 5. Extent size must be a multiple of the appropriate block size.
742 : * 6. The extent size hint must be limited to half the AG size to avoid
743 : * alignment extending the extent beyond the limits of the AG.
744 : */
745 : xfs_failaddr_t
746 563972541 : xfs_inode_validate_cowextsize(
747 : struct xfs_mount *mp,
748 : uint32_t cowextsize,
749 : uint16_t mode,
750 : uint16_t flags,
751 : uint64_t flags2)
752 : {
753 563972541 : bool rt_flag;
754 563972541 : bool hint_flag;
755 563972541 : uint32_t cowextsize_bytes;
756 :
757 563972541 : rt_flag = (flags & XFS_DIFLAG_REALTIME);
758 563972541 : hint_flag = (flags2 & XFS_DIFLAG2_COWEXTSIZE);
759 563972541 : cowextsize_bytes = XFS_FSB_TO_B(mp, cowextsize);
760 :
761 563972541 : if (hint_flag && !xfs_has_reflink(mp))
762 0 : return __this_address;
763 :
764 563972541 : if (hint_flag && !(S_ISDIR(mode) || S_ISREG(mode)))
765 0 : return __this_address;
766 :
767 563972541 : if (hint_flag && cowextsize == 0)
768 0 : return __this_address;
769 :
770 : /* free inodes get flags set to zero but cowextsize remains */
771 563972541 : if (mode && !hint_flag && cowextsize != 0)
772 0 : return __this_address;
773 :
774 563972541 : if (hint_flag && rt_flag)
775 0 : return __this_address;
776 :
777 563972541 : if (cowextsize_bytes % mp->m_sb.sb_blocksize)
778 0 : return __this_address;
779 :
780 563972541 : if (cowextsize > XFS_MAX_BMBT_EXTLEN)
781 0 : return __this_address;
782 :
783 563972541 : if (cowextsize > mp->m_sb.sb_agblocks / 2)
784 0 : return __this_address;
785 :
786 : return NULL;
787 : }
|