1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
4 * All Rights Reserved.
5 */
6
7 #include "xfs.h"
8 #include "xfs_fs.h"
9 #include "xfs_shared.h"
10 #include "xfs_format.h"
11 #include "xfs_log_format.h"
12 #include "xfs_trans_resv.h"
13 #include "xfs_mount.h"
14 #include "xfs_inode.h"
15 #include "xfs_trans.h"
16 #include "xfs_inode_item.h"
17 #include "xfs_btree.h"
18 #include "xfs_bmap_btree.h"
19 #include "xfs_bmap.h"
20 #include "xfs_error.h"
21 #include "xfs_trace.h"
22 #include "xfs_da_format.h"
23 #include "xfs_da_btree.h"
24 #include "xfs_dir2_priv.h"
25 #include "xfs_attr_leaf.h"
26 #include "xfs_types.h"
27 #include "xfs_errortag.h"
28 #include "xfs_health.h"
29 #include "xfs_symlink_remote.h"
30 #include "xfs_rtrmap_btree.h"
31 #include "xfs_rtrefcount_btree.h"
32
33 struct kmem_cache *xfs_ifork_cache;
34
35 void
xfs_init_local_fork(struct xfs_inode * ip,int whichfork,const void * data,int64_t size)36 xfs_init_local_fork(
37 struct xfs_inode *ip,
38 int whichfork,
39 const void *data,
40 int64_t size)
41 {
42 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
43 int mem_size = size;
44 bool zero_terminate;
45
46 /*
47 * If we are using the local fork to store a symlink body we need to
48 * zero-terminate it so that we can pass it back to the VFS directly.
49 * Overallocate the in-memory fork by one for that and add a zero
50 * to terminate it below.
51 */
52 zero_terminate = S_ISLNK(VFS_I(ip)->i_mode);
53 if (zero_terminate)
54 mem_size++;
55
56 if (size) {
57 char *new_data = kmalloc(mem_size,
58 GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL);
59
60 memcpy(new_data, data, size);
61 if (zero_terminate)
62 new_data[size] = '\0';
63
64 ifp->if_data = new_data;
65 } else {
66 ifp->if_data = NULL;
67 }
68
69 ifp->if_bytes = size;
70 }
71
72 /*
73 * The file is in-lined in the on-disk inode.
74 */
75 STATIC int
xfs_iformat_local(struct xfs_inode * ip,struct xfs_dinode * dip,int whichfork,int size)76 xfs_iformat_local(
77 struct xfs_inode *ip,
78 struct xfs_dinode *dip,
79 int whichfork,
80 int size)
81 {
82 /*
83 * If the size is unreasonable, then something
84 * is wrong and we just bail out rather than crash in
85 * kmalloc() or memcpy() below.
86 */
87 if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
88 xfs_warn(ip->i_mount,
89 "corrupt inode %llu (bad size %d for local fork, size = %zd).",
90 (unsigned long long) ip->i_ino, size,
91 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
92 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
93 "xfs_iformat_local", dip, sizeof(*dip),
94 __this_address);
95 xfs_inode_mark_sick(ip, XFS_SICK_INO_CORE);
96 return -EFSCORRUPTED;
97 }
98
99 xfs_init_local_fork(ip, whichfork, XFS_DFORK_PTR(dip, whichfork), size);
100 return 0;
101 }
102
103 /*
104 * The file consists of a set of extents all of which fit into the on-disk
105 * inode.
106 */
107 STATIC int
xfs_iformat_extents(struct xfs_inode * ip,struct xfs_dinode * dip,int whichfork)108 xfs_iformat_extents(
109 struct xfs_inode *ip,
110 struct xfs_dinode *dip,
111 int whichfork)
112 {
113 struct xfs_mount *mp = ip->i_mount;
114 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
115 int state = xfs_bmap_fork_to_state(whichfork);
116 xfs_extnum_t nex = xfs_dfork_nextents(dip, whichfork);
117 int size = nex * sizeof(xfs_bmbt_rec_t);
118 struct xfs_iext_cursor icur;
119 struct xfs_bmbt_rec *dp;
120 struct xfs_bmbt_irec new;
121 int i;
122
123 /*
124 * If the number of extents is unreasonable, then something is wrong and
125 * we just bail out rather than crash in kmalloc() or memcpy() below.
126 */
127 if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, mp, whichfork))) {
128 xfs_warn(ip->i_mount, "corrupt inode %llu ((a)extents = %llu).",
129 ip->i_ino, nex);
130 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
131 "xfs_iformat_extents(1)", dip, sizeof(*dip),
132 __this_address);
133 xfs_inode_mark_sick(ip, XFS_SICK_INO_CORE);
134 return -EFSCORRUPTED;
135 }
136
137 ifp->if_bytes = 0;
138 ifp->if_data = NULL;
139 ifp->if_height = 0;
140 if (size) {
141 dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
142
143 xfs_iext_first(ifp, &icur);
144 for (i = 0; i < nex; i++, dp++) {
145 xfs_failaddr_t fa;
146
147 xfs_bmbt_disk_get_all(dp, &new);
148 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
149 if (fa) {
150 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
151 "xfs_iformat_extents(2)",
152 dp, sizeof(*dp), fa);
153 xfs_inode_mark_sick(ip, XFS_SICK_INO_CORE);
154 return xfs_bmap_complain_bad_rec(ip, whichfork,
155 fa, &new);
156 }
157
158 xfs_iext_insert(ip, &icur, &new, state);
159 trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
160 xfs_iext_next(ifp, &icur);
161 }
162 }
163 return 0;
164 }
165
166 /*
167 * The file has too many extents to fit into
168 * the inode, so they are in B-tree format.
169 * Allocate a buffer for the root of the B-tree
170 * and copy the root into it. The i_extents
171 * field will remain NULL until all of the
172 * extents are read in (when they are needed).
173 */
174 STATIC int
xfs_iformat_btree(struct xfs_inode * ip,struct xfs_dinode * dip,int whichfork)175 xfs_iformat_btree(
176 struct xfs_inode *ip,
177 struct xfs_dinode *dip,
178 int whichfork)
179 {
180 struct xfs_mount *mp = ip->i_mount;
181 xfs_bmdr_block_t *dfp;
182 struct xfs_ifork *ifp;
183 struct xfs_btree_block *broot;
184 int nrecs;
185 int size;
186 int level;
187
188 ifp = xfs_ifork_ptr(ip, whichfork);
189 dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
190 size = xfs_bmap_broot_space(mp, dfp);
191 nrecs = be16_to_cpu(dfp->bb_numrecs);
192 level = be16_to_cpu(dfp->bb_level);
193
194 /*
195 * blow out if -- fork has less extents than can fit in
196 * fork (fork shouldn't be a btree format), root btree
197 * block has more records than can fit into the fork,
198 * or the number of extents is greater than the number of
199 * blocks.
200 */
201 if (unlikely(ifp->if_nextents <= XFS_IFORK_MAXEXT(ip, whichfork) ||
202 nrecs == 0 ||
203 xfs_bmdr_space_calc(nrecs) >
204 XFS_DFORK_SIZE(dip, mp, whichfork) ||
205 ifp->if_nextents > ip->i_nblocks) ||
206 level == 0 || level > XFS_BM_MAXLEVELS(mp, whichfork)) {
207 xfs_warn(mp, "corrupt inode %llu (btree).",
208 (unsigned long long) ip->i_ino);
209 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
210 "xfs_iformat_btree", dfp, size,
211 __this_address);
212 xfs_inode_mark_sick(ip, XFS_SICK_INO_CORE);
213 return -EFSCORRUPTED;
214 }
215
216 broot = xfs_broot_alloc(ifp, size);
217 /*
218 * Copy and convert from the on-disk structure
219 * to the in-memory structure.
220 */
221 xfs_bmdr_to_bmbt(ip, dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
222 broot, size);
223
224 ifp->if_bytes = 0;
225 ifp->if_data = NULL;
226 ifp->if_height = 0;
227 return 0;
228 }
229
230 int
xfs_iformat_data_fork(struct xfs_inode * ip,struct xfs_dinode * dip)231 xfs_iformat_data_fork(
232 struct xfs_inode *ip,
233 struct xfs_dinode *dip)
234 {
235 struct inode *inode = VFS_I(ip);
236 int error;
237
238 /*
239 * Initialize the extent count early, as the per-format routines may
240 * depend on it. Use release semantics to set needextents /after/ we
241 * set the format. This ensures that we can use acquire semantics on
242 * needextents in xfs_need_iread_extents() and be guaranteed to see a
243 * valid format value after that load.
244 */
245 ip->i_df.if_format = dip->di_format;
246 ip->i_df.if_nextents = xfs_dfork_data_extents(dip);
247 smp_store_release(&ip->i_df.if_needextents,
248 ip->i_df.if_format == XFS_DINODE_FMT_BTREE ? 1 : 0);
249
250 switch (inode->i_mode & S_IFMT) {
251 case S_IFIFO:
252 case S_IFCHR:
253 case S_IFBLK:
254 case S_IFSOCK:
255 ip->i_disk_size = 0;
256 inode->i_rdev = xfs_to_linux_dev_t(xfs_dinode_get_rdev(dip));
257 return 0;
258 case S_IFREG:
259 case S_IFLNK:
260 case S_IFDIR:
261 switch (ip->i_df.if_format) {
262 case XFS_DINODE_FMT_LOCAL:
263 error = xfs_iformat_local(ip, dip, XFS_DATA_FORK,
264 be64_to_cpu(dip->di_size));
265 if (!error)
266 error = xfs_ifork_verify_local_data(ip);
267 return error;
268 case XFS_DINODE_FMT_EXTENTS:
269 return xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
270 case XFS_DINODE_FMT_BTREE:
271 return xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
272 case XFS_DINODE_FMT_META_BTREE:
273 switch (ip->i_metatype) {
274 case XFS_METAFILE_RTRMAP:
275 return xfs_iformat_rtrmap(ip, dip);
276 case XFS_METAFILE_RTREFCOUNT:
277 return xfs_iformat_rtrefcount(ip, dip);
278 default:
279 break;
280 }
281 fallthrough;
282 default:
283 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__,
284 dip, sizeof(*dip), __this_address);
285 xfs_inode_mark_sick(ip, XFS_SICK_INO_CORE);
286 return -EFSCORRUPTED;
287 }
288 break;
289 default:
290 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, dip,
291 sizeof(*dip), __this_address);
292 xfs_inode_mark_sick(ip, XFS_SICK_INO_CORE);
293 return -EFSCORRUPTED;
294 }
295 }
296
297 static uint16_t
xfs_dfork_attr_shortform_size(struct xfs_dinode * dip)298 xfs_dfork_attr_shortform_size(
299 struct xfs_dinode *dip)
300 {
301 struct xfs_attr_sf_hdr *sf = XFS_DFORK_APTR(dip);
302
303 return be16_to_cpu(sf->totsize);
304 }
305
306 void
xfs_ifork_init_attr(struct xfs_inode * ip,enum xfs_dinode_fmt format,xfs_extnum_t nextents)307 xfs_ifork_init_attr(
308 struct xfs_inode *ip,
309 enum xfs_dinode_fmt format,
310 xfs_extnum_t nextents)
311 {
312 /*
313 * Initialize the extent count early, as the per-format routines may
314 * depend on it. Use release semantics to set needextents /after/ we
315 * set the format. This ensures that we can use acquire semantics on
316 * needextents in xfs_need_iread_extents() and be guaranteed to see a
317 * valid format value after that load.
318 */
319 ip->i_af.if_format = format;
320 ip->i_af.if_nextents = nextents;
321 smp_store_release(&ip->i_af.if_needextents,
322 ip->i_af.if_format == XFS_DINODE_FMT_BTREE ? 1 : 0);
323 }
324
325 void
xfs_ifork_zap_attr(struct xfs_inode * ip)326 xfs_ifork_zap_attr(
327 struct xfs_inode *ip)
328 {
329 xfs_idestroy_fork(&ip->i_af);
330 memset(&ip->i_af, 0, sizeof(struct xfs_ifork));
331 ip->i_af.if_format = XFS_DINODE_FMT_EXTENTS;
332 }
333
334 int
xfs_iformat_attr_fork(struct xfs_inode * ip,struct xfs_dinode * dip)335 xfs_iformat_attr_fork(
336 struct xfs_inode *ip,
337 struct xfs_dinode *dip)
338 {
339 xfs_extnum_t naextents = xfs_dfork_attr_extents(dip);
340 int error = 0;
341
342 /*
343 * Initialize the extent count early, as the per-format routines may
344 * depend on it.
345 */
346 xfs_ifork_init_attr(ip, dip->di_aformat, naextents);
347
348 switch (ip->i_af.if_format) {
349 case XFS_DINODE_FMT_LOCAL:
350 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK,
351 xfs_dfork_attr_shortform_size(dip));
352 if (!error)
353 error = xfs_ifork_verify_local_attr(ip);
354 break;
355 case XFS_DINODE_FMT_EXTENTS:
356 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
357 break;
358 case XFS_DINODE_FMT_BTREE:
359 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
360 break;
361 default:
362 xfs_inode_verifier_error(ip, error, __func__, dip,
363 sizeof(*dip), __this_address);
364 xfs_inode_mark_sick(ip, XFS_SICK_INO_CORE);
365 error = -EFSCORRUPTED;
366 break;
367 }
368
369 if (error)
370 xfs_ifork_zap_attr(ip);
371 return error;
372 }
373
374 /*
375 * Allocate the if_broot component of an inode fork so that it is @new_size
376 * bytes in size, using __GFP_NOLOCKDEP like all the other code that
377 * initializes a broot during inode load. Returns if_broot.
378 */
379 struct xfs_btree_block *
xfs_broot_alloc(struct xfs_ifork * ifp,size_t new_size)380 xfs_broot_alloc(
381 struct xfs_ifork *ifp,
382 size_t new_size)
383 {
384 ASSERT(ifp->if_broot == NULL);
385
386 ifp->if_broot = kmalloc(new_size,
387 GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL);
388 ifp->if_broot_bytes = new_size;
389 return ifp->if_broot;
390 }
391
392 /*
393 * Reallocate the if_broot component of an inode fork so that it is @new_size
394 * bytes in size. Returns if_broot.
395 */
396 struct xfs_btree_block *
xfs_broot_realloc(struct xfs_ifork * ifp,size_t new_size)397 xfs_broot_realloc(
398 struct xfs_ifork *ifp,
399 size_t new_size)
400 {
401 /* No size change? No action needed. */
402 if (new_size == ifp->if_broot_bytes)
403 return ifp->if_broot;
404
405 /* New size is zero, free it. */
406 if (new_size == 0) {
407 ifp->if_broot_bytes = 0;
408 kfree(ifp->if_broot);
409 ifp->if_broot = NULL;
410 return NULL;
411 }
412
413 /*
414 * Shrinking the iroot means we allocate a new smaller object and copy
415 * it. We don't trust krealloc not to nop on realloc-down.
416 */
417 if (ifp->if_broot_bytes > 0 && ifp->if_broot_bytes > new_size) {
418 struct xfs_btree_block *old_broot = ifp->if_broot;
419
420 ifp->if_broot = kmalloc(new_size, GFP_KERNEL | __GFP_NOFAIL);
421 ifp->if_broot_bytes = new_size;
422 memcpy(ifp->if_broot, old_broot, new_size);
423 kfree(old_broot);
424 return ifp->if_broot;
425 }
426
427 /*
428 * Growing the iroot means we can krealloc. This may get us the same
429 * object.
430 */
431 ifp->if_broot = krealloc(ifp->if_broot, new_size,
432 GFP_KERNEL | __GFP_NOFAIL);
433 ifp->if_broot_bytes = new_size;
434 return ifp->if_broot;
435 }
436
437 /*
438 * This is called when the amount of space needed for if_data
439 * is increased or decreased. The change in size is indicated by
440 * the number of bytes that need to be added or deleted in the
441 * byte_diff parameter.
442 *
443 * If the amount of space needed has decreased below the size of the
444 * inline buffer, then switch to using the inline buffer. Otherwise,
445 * use krealloc() or kmalloc() to adjust the size of the buffer
446 * to what is needed.
447 *
448 * ip -- the inode whose if_data area is changing
449 * byte_diff -- the change in the number of bytes, positive or negative,
450 * requested for the if_data array.
451 */
452 void *
xfs_idata_realloc(struct xfs_inode * ip,int64_t byte_diff,int whichfork)453 xfs_idata_realloc(
454 struct xfs_inode *ip,
455 int64_t byte_diff,
456 int whichfork)
457 {
458 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
459 int64_t new_size = ifp->if_bytes + byte_diff;
460
461 ASSERT(new_size >= 0);
462 ASSERT(new_size <= xfs_inode_fork_size(ip, whichfork));
463
464 if (byte_diff) {
465 ifp->if_data = krealloc(ifp->if_data, new_size,
466 GFP_KERNEL | __GFP_NOFAIL);
467 if (new_size == 0)
468 ifp->if_data = NULL;
469 ifp->if_bytes = new_size;
470 }
471
472 return ifp->if_data;
473 }
474
475 /* Free all memory and reset a fork back to its initial state. */
476 void
xfs_idestroy_fork(struct xfs_ifork * ifp)477 xfs_idestroy_fork(
478 struct xfs_ifork *ifp)
479 {
480 if (ifp->if_broot != NULL) {
481 kfree(ifp->if_broot);
482 ifp->if_broot = NULL;
483 }
484
485 switch (ifp->if_format) {
486 case XFS_DINODE_FMT_LOCAL:
487 kfree(ifp->if_data);
488 ifp->if_data = NULL;
489 break;
490 case XFS_DINODE_FMT_EXTENTS:
491 case XFS_DINODE_FMT_BTREE:
492 if (ifp->if_height)
493 xfs_iext_destroy(ifp);
494 break;
495 }
496 }
497
498 /*
499 * Convert in-core extents to on-disk form
500 *
501 * In the case of the data fork, the in-core and on-disk fork sizes can be
502 * different due to delayed allocation extents. We only copy on-disk extents
503 * here, so callers must always use the physical fork size to determine the
504 * size of the buffer passed to this routine. We will return the size actually
505 * used.
506 */
507 int
xfs_iextents_copy(struct xfs_inode * ip,struct xfs_bmbt_rec * dp,int whichfork)508 xfs_iextents_copy(
509 struct xfs_inode *ip,
510 struct xfs_bmbt_rec *dp,
511 int whichfork)
512 {
513 int state = xfs_bmap_fork_to_state(whichfork);
514 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
515 struct xfs_iext_cursor icur;
516 struct xfs_bmbt_irec rec;
517 int64_t copied = 0;
518
519 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED);
520 ASSERT(ifp->if_bytes > 0);
521
522 for_each_xfs_iext(ifp, &icur, &rec) {
523 if (isnullstartblock(rec.br_startblock))
524 continue;
525 ASSERT(xfs_bmap_validate_extent(ip, whichfork, &rec) == NULL);
526 xfs_bmbt_disk_set_all(dp, &rec);
527 trace_xfs_write_extent(ip, &icur, state, _RET_IP_);
528 copied += sizeof(struct xfs_bmbt_rec);
529 dp++;
530 }
531
532 ASSERT(copied > 0);
533 ASSERT(copied <= ifp->if_bytes);
534 return copied;
535 }
536
537 /*
538 * Each of the following cases stores data into the same region
539 * of the on-disk inode, so only one of them can be valid at
540 * any given time. While it is possible to have conflicting formats
541 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
542 * in EXTENTS format, this can only happen when the fork has
543 * changed formats after being modified but before being flushed.
544 * In these cases, the format always takes precedence, because the
545 * format indicates the current state of the fork.
546 */
547 void
xfs_iflush_fork(struct xfs_inode * ip,struct xfs_dinode * dip,struct xfs_inode_log_item * iip,int whichfork)548 xfs_iflush_fork(
549 struct xfs_inode *ip,
550 struct xfs_dinode *dip,
551 struct xfs_inode_log_item *iip,
552 int whichfork)
553 {
554 char *cp;
555 struct xfs_ifork *ifp;
556 xfs_mount_t *mp;
557 static const short brootflag[2] =
558 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
559 static const short dataflag[2] =
560 { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
561 static const short extflag[2] =
562 { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
563
564 if (!iip)
565 return;
566 ifp = xfs_ifork_ptr(ip, whichfork);
567 /*
568 * This can happen if we gave up in iformat in an error path,
569 * for the attribute fork.
570 */
571 if (!ifp) {
572 ASSERT(whichfork == XFS_ATTR_FORK);
573 return;
574 }
575 cp = XFS_DFORK_PTR(dip, whichfork);
576 mp = ip->i_mount;
577 switch (ifp->if_format) {
578 case XFS_DINODE_FMT_LOCAL:
579 if ((iip->ili_fields & dataflag[whichfork]) &&
580 (ifp->if_bytes > 0)) {
581 ASSERT(ifp->if_data != NULL);
582 ASSERT(ifp->if_bytes <= xfs_inode_fork_size(ip, whichfork));
583 memcpy(cp, ifp->if_data, ifp->if_bytes);
584 }
585 break;
586
587 case XFS_DINODE_FMT_EXTENTS:
588 if ((iip->ili_fields & extflag[whichfork]) &&
589 (ifp->if_bytes > 0)) {
590 ASSERT(ifp->if_nextents > 0);
591 (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
592 whichfork);
593 }
594 break;
595
596 case XFS_DINODE_FMT_BTREE:
597 if ((iip->ili_fields & brootflag[whichfork]) &&
598 (ifp->if_broot_bytes > 0)) {
599 ASSERT(ifp->if_broot != NULL);
600 ASSERT(xfs_bmap_bmdr_space(ifp->if_broot) <=
601 xfs_inode_fork_size(ip, whichfork));
602 xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
603 (xfs_bmdr_block_t *)cp,
604 XFS_DFORK_SIZE(dip, mp, whichfork));
605 }
606 break;
607
608 case XFS_DINODE_FMT_DEV:
609 if (iip->ili_fields & XFS_ILOG_DEV) {
610 ASSERT(whichfork == XFS_DATA_FORK);
611 xfs_dinode_put_rdev(dip,
612 linux_to_xfs_dev_t(VFS_I(ip)->i_rdev));
613 }
614 break;
615
616 case XFS_DINODE_FMT_META_BTREE:
617 ASSERT(whichfork == XFS_DATA_FORK);
618
619 if (!(iip->ili_fields & brootflag[whichfork]))
620 break;
621
622 switch (ip->i_metatype) {
623 case XFS_METAFILE_RTRMAP:
624 xfs_iflush_rtrmap(ip, dip);
625 break;
626 case XFS_METAFILE_RTREFCOUNT:
627 xfs_iflush_rtrefcount(ip, dip);
628 break;
629 default:
630 ASSERT(0);
631 break;
632 }
633 break;
634
635 default:
636 ASSERT(0);
637 break;
638 }
639 }
640
641 /* Convert bmap state flags to an inode fork. */
642 struct xfs_ifork *
xfs_iext_state_to_fork(struct xfs_inode * ip,int state)643 xfs_iext_state_to_fork(
644 struct xfs_inode *ip,
645 int state)
646 {
647 if (state & BMAP_COWFORK)
648 return ip->i_cowfp;
649 else if (state & BMAP_ATTRFORK)
650 return &ip->i_af;
651 return &ip->i_df;
652 }
653
654 /*
655 * Initialize an inode's copy-on-write fork.
656 */
657 void
xfs_ifork_init_cow(struct xfs_inode * ip)658 xfs_ifork_init_cow(
659 struct xfs_inode *ip)
660 {
661 if (ip->i_cowfp)
662 return;
663
664 ip->i_cowfp = kmem_cache_zalloc(xfs_ifork_cache,
665 GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL);
666 ip->i_cowfp->if_format = XFS_DINODE_FMT_EXTENTS;
667 }
668
669 /* Verify the inline contents of the data fork of an inode. */
670 int
xfs_ifork_verify_local_data(struct xfs_inode * ip)671 xfs_ifork_verify_local_data(
672 struct xfs_inode *ip)
673 {
674 xfs_failaddr_t fa = NULL;
675
676 switch (VFS_I(ip)->i_mode & S_IFMT) {
677 case S_IFDIR: {
678 struct xfs_mount *mp = ip->i_mount;
679 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, XFS_DATA_FORK);
680 struct xfs_dir2_sf_hdr *sfp = ifp->if_data;
681
682 fa = xfs_dir2_sf_verify(mp, sfp, ifp->if_bytes);
683 break;
684 }
685 case S_IFLNK: {
686 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, XFS_DATA_FORK);
687
688 fa = xfs_symlink_shortform_verify(ifp->if_data, ifp->if_bytes);
689 break;
690 }
691 default:
692 break;
693 }
694
695 if (fa) {
696 xfs_inode_verifier_error(ip, -EFSCORRUPTED, "data fork",
697 ip->i_df.if_data, ip->i_df.if_bytes, fa);
698 return -EFSCORRUPTED;
699 }
700
701 return 0;
702 }
703
704 /* Verify the inline contents of the attr fork of an inode. */
705 int
xfs_ifork_verify_local_attr(struct xfs_inode * ip)706 xfs_ifork_verify_local_attr(
707 struct xfs_inode *ip)
708 {
709 struct xfs_ifork *ifp = &ip->i_af;
710 xfs_failaddr_t fa;
711
712 if (!xfs_inode_has_attr_fork(ip)) {
713 fa = __this_address;
714 } else {
715 struct xfs_ifork *ifp = &ip->i_af;
716
717 ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
718 fa = xfs_attr_shortform_verify(ifp->if_data, ifp->if_bytes);
719 }
720 if (fa) {
721 xfs_inode_verifier_error(ip, -EFSCORRUPTED, "attr fork",
722 ifp->if_data, ifp->if_bytes, fa);
723 return -EFSCORRUPTED;
724 }
725
726 return 0;
727 }
728
729 /*
730 * Check if the inode fork supports adding nr_to_add more extents.
731 *
732 * If it doesn't but we can upgrade it to large extent counters, do the upgrade.
733 * If we can't upgrade or are already using big counters but still can't fit the
734 * additional extents, return -EFBIG.
735 */
736 int
xfs_iext_count_extend(struct xfs_trans * tp,struct xfs_inode * ip,int whichfork,uint nr_to_add)737 xfs_iext_count_extend(
738 struct xfs_trans *tp,
739 struct xfs_inode *ip,
740 int whichfork,
741 uint nr_to_add)
742 {
743 struct xfs_mount *mp = ip->i_mount;
744 bool has_large =
745 xfs_inode_has_large_extent_counts(ip);
746 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
747 uint64_t nr_exts;
748
749 ASSERT(nr_to_add <= XFS_MAX_EXTCNT_UPGRADE_NR);
750
751 if (whichfork == XFS_COW_FORK)
752 return 0;
753
754 /* no point in upgrading if if_nextents overflows */
755 nr_exts = ifp->if_nextents + nr_to_add;
756 if (nr_exts < ifp->if_nextents)
757 return -EFBIG;
758
759 if (XFS_TEST_ERROR(false, mp, XFS_ERRTAG_REDUCE_MAX_IEXTENTS) &&
760 nr_exts > 10)
761 return -EFBIG;
762
763 if (nr_exts > xfs_iext_max_nextents(has_large, whichfork)) {
764 if (has_large || !xfs_has_large_extent_counts(mp))
765 return -EFBIG;
766 ip->i_diflags2 |= XFS_DIFLAG2_NREXT64;
767 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
768 }
769 return 0;
770 }
771
772 /* Decide if a file mapping is on the realtime device or not. */
773 bool
xfs_ifork_is_realtime(struct xfs_inode * ip,int whichfork)774 xfs_ifork_is_realtime(
775 struct xfs_inode *ip,
776 int whichfork)
777 {
778 return XFS_IS_REALTIME_INODE(ip) && whichfork != XFS_ATTR_FORK;
779 }
780