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
29 kmem_zone_t *xfs_ifork_zone;
30
31 void
xfs_init_local_fork(struct xfs_inode * ip,int whichfork,const void * data,int64_t size)32 xfs_init_local_fork(
33 struct xfs_inode *ip,
34 int whichfork,
35 const void *data,
36 int64_t size)
37 {
38 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
39 int mem_size = size, real_size = 0;
40 bool zero_terminate;
41
42 /*
43 * If we are using the local fork to store a symlink body we need to
44 * zero-terminate it so that we can pass it back to the VFS directly.
45 * Overallocate the in-memory fork by one for that and add a zero
46 * to terminate it below.
47 */
48 zero_terminate = S_ISLNK(VFS_I(ip)->i_mode);
49 if (zero_terminate)
50 mem_size++;
51
52 if (size) {
53 /*
54 * As we round up the allocation here, we need to ensure the
55 * bytes we don't copy data into are zeroed because the log
56 * vectors still copy them into the journal.
57 */
58 real_size = roundup(mem_size, 4);
59 ifp->if_u1.if_data = kmem_zalloc(real_size, KM_NOFS);
60 memcpy(ifp->if_u1.if_data, data, size);
61 if (zero_terminate)
62 ifp->if_u1.if_data[size] = '\0';
63 } else {
64 ifp->if_u1.if_data = NULL;
65 }
66
67 ifp->if_bytes = size;
68 }
69
70 /*
71 * The file is in-lined in the on-disk inode.
72 */
73 STATIC int
xfs_iformat_local(xfs_inode_t * ip,xfs_dinode_t * dip,int whichfork,int size)74 xfs_iformat_local(
75 xfs_inode_t *ip,
76 xfs_dinode_t *dip,
77 int whichfork,
78 int size)
79 {
80 /*
81 * If the size is unreasonable, then something
82 * is wrong and we just bail out rather than crash in
83 * kmem_alloc() or memcpy() below.
84 */
85 if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
86 xfs_warn(ip->i_mount,
87 "corrupt inode %Lu (bad size %d for local fork, size = %zd).",
88 (unsigned long long) ip->i_ino, size,
89 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
90 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
91 "xfs_iformat_local", dip, sizeof(*dip),
92 __this_address);
93 return -EFSCORRUPTED;
94 }
95
96 xfs_init_local_fork(ip, whichfork, XFS_DFORK_PTR(dip, whichfork), size);
97 return 0;
98 }
99
100 /*
101 * The file consists of a set of extents all of which fit into the on-disk
102 * inode.
103 */
104 STATIC int
xfs_iformat_extents(struct xfs_inode * ip,struct xfs_dinode * dip,int whichfork)105 xfs_iformat_extents(
106 struct xfs_inode *ip,
107 struct xfs_dinode *dip,
108 int whichfork)
109 {
110 struct xfs_mount *mp = ip->i_mount;
111 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
112 int state = xfs_bmap_fork_to_state(whichfork);
113 int nex = XFS_DFORK_NEXTENTS(dip, whichfork);
114 int size = nex * sizeof(xfs_bmbt_rec_t);
115 struct xfs_iext_cursor icur;
116 struct xfs_bmbt_rec *dp;
117 struct xfs_bmbt_irec new;
118 int i;
119
120 /*
121 * If the number of extents is unreasonable, then something is wrong and
122 * we just bail out rather than crash in kmem_alloc() or memcpy() below.
123 */
124 if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, mp, whichfork))) {
125 xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
126 (unsigned long long) ip->i_ino, nex);
127 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
128 "xfs_iformat_extents(1)", dip, sizeof(*dip),
129 __this_address);
130 return -EFSCORRUPTED;
131 }
132
133 ifp->if_bytes = 0;
134 ifp->if_u1.if_root = NULL;
135 ifp->if_height = 0;
136 if (size) {
137 dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
138
139 xfs_iext_first(ifp, &icur);
140 for (i = 0; i < nex; i++, dp++) {
141 xfs_failaddr_t fa;
142
143 xfs_bmbt_disk_get_all(dp, &new);
144 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
145 if (fa) {
146 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
147 "xfs_iformat_extents(2)",
148 dp, sizeof(*dp), fa);
149 return -EFSCORRUPTED;
150 }
151
152 xfs_iext_insert(ip, &icur, &new, state);
153 trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
154 xfs_iext_next(ifp, &icur);
155 }
156 }
157 return 0;
158 }
159
160 /*
161 * The file has too many extents to fit into
162 * the inode, so they are in B-tree format.
163 * Allocate a buffer for the root of the B-tree
164 * and copy the root into it. The i_extents
165 * field will remain NULL until all of the
166 * extents are read in (when they are needed).
167 */
168 STATIC int
xfs_iformat_btree(xfs_inode_t * ip,xfs_dinode_t * dip,int whichfork)169 xfs_iformat_btree(
170 xfs_inode_t *ip,
171 xfs_dinode_t *dip,
172 int whichfork)
173 {
174 struct xfs_mount *mp = ip->i_mount;
175 xfs_bmdr_block_t *dfp;
176 struct xfs_ifork *ifp;
177 /* REFERENCED */
178 int nrecs;
179 int size;
180 int level;
181
182 ifp = XFS_IFORK_PTR(ip, whichfork);
183 dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
184 size = XFS_BMAP_BROOT_SPACE(mp, dfp);
185 nrecs = be16_to_cpu(dfp->bb_numrecs);
186 level = be16_to_cpu(dfp->bb_level);
187
188 /*
189 * blow out if -- fork has less extents than can fit in
190 * fork (fork shouldn't be a btree format), root btree
191 * block has more records than can fit into the fork,
192 * or the number of extents is greater than the number of
193 * blocks.
194 */
195 if (unlikely(ifp->if_nextents <= XFS_IFORK_MAXEXT(ip, whichfork) ||
196 nrecs == 0 ||
197 XFS_BMDR_SPACE_CALC(nrecs) >
198 XFS_DFORK_SIZE(dip, mp, whichfork) ||
199 ifp->if_nextents > ip->i_nblocks) ||
200 level == 0 || level > XFS_BM_MAXLEVELS(mp, whichfork)) {
201 xfs_warn(mp, "corrupt inode %Lu (btree).",
202 (unsigned long long) ip->i_ino);
203 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
204 "xfs_iformat_btree", dfp, size,
205 __this_address);
206 return -EFSCORRUPTED;
207 }
208
209 ifp->if_broot_bytes = size;
210 ifp->if_broot = kmem_alloc(size, KM_NOFS);
211 ASSERT(ifp->if_broot != NULL);
212 /*
213 * Copy and convert from the on-disk structure
214 * to the in-memory structure.
215 */
216 xfs_bmdr_to_bmbt(ip, dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
217 ifp->if_broot, size);
218
219 ifp->if_bytes = 0;
220 ifp->if_u1.if_root = NULL;
221 ifp->if_height = 0;
222 return 0;
223 }
224
225 int
xfs_iformat_data_fork(struct xfs_inode * ip,struct xfs_dinode * dip)226 xfs_iformat_data_fork(
227 struct xfs_inode *ip,
228 struct xfs_dinode *dip)
229 {
230 struct inode *inode = VFS_I(ip);
231 int error;
232
233 /*
234 * Initialize the extent count early, as the per-format routines may
235 * depend on it.
236 */
237 ip->i_df.if_format = dip->di_format;
238 ip->i_df.if_nextents = be32_to_cpu(dip->di_nextents);
239
240 switch (inode->i_mode & S_IFMT) {
241 case S_IFIFO:
242 case S_IFCHR:
243 case S_IFBLK:
244 case S_IFSOCK:
245 ip->i_disk_size = 0;
246 inode->i_rdev = xfs_to_linux_dev_t(xfs_dinode_get_rdev(dip));
247 return 0;
248 case S_IFREG:
249 case S_IFLNK:
250 case S_IFDIR:
251 switch (ip->i_df.if_format) {
252 case XFS_DINODE_FMT_LOCAL:
253 error = xfs_iformat_local(ip, dip, XFS_DATA_FORK,
254 be64_to_cpu(dip->di_size));
255 if (!error)
256 error = xfs_ifork_verify_local_data(ip);
257 return error;
258 case XFS_DINODE_FMT_EXTENTS:
259 return xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
260 case XFS_DINODE_FMT_BTREE:
261 return xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
262 default:
263 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__,
264 dip, sizeof(*dip), __this_address);
265 return -EFSCORRUPTED;
266 }
267 break;
268 default:
269 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, dip,
270 sizeof(*dip), __this_address);
271 return -EFSCORRUPTED;
272 }
273 }
274
275 static uint16_t
xfs_dfork_attr_shortform_size(struct xfs_dinode * dip)276 xfs_dfork_attr_shortform_size(
277 struct xfs_dinode *dip)
278 {
279 struct xfs_attr_shortform *atp =
280 (struct xfs_attr_shortform *)XFS_DFORK_APTR(dip);
281
282 return be16_to_cpu(atp->hdr.totsize);
283 }
284
285 struct xfs_ifork *
xfs_ifork_alloc(enum xfs_dinode_fmt format,xfs_extnum_t nextents)286 xfs_ifork_alloc(
287 enum xfs_dinode_fmt format,
288 xfs_extnum_t nextents)
289 {
290 struct xfs_ifork *ifp;
291
292 ifp = kmem_cache_zalloc(xfs_ifork_zone, GFP_NOFS | __GFP_NOFAIL);
293 ifp->if_format = format;
294 ifp->if_nextents = nextents;
295 return ifp;
296 }
297
298 int
xfs_iformat_attr_fork(struct xfs_inode * ip,struct xfs_dinode * dip)299 xfs_iformat_attr_fork(
300 struct xfs_inode *ip,
301 struct xfs_dinode *dip)
302 {
303 int error = 0;
304
305 /*
306 * Initialize the extent count early, as the per-format routines may
307 * depend on it.
308 */
309 ip->i_afp = xfs_ifork_alloc(dip->di_aformat,
310 be16_to_cpu(dip->di_anextents));
311
312 switch (ip->i_afp->if_format) {
313 case XFS_DINODE_FMT_LOCAL:
314 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK,
315 xfs_dfork_attr_shortform_size(dip));
316 if (!error)
317 error = xfs_ifork_verify_local_attr(ip);
318 break;
319 case XFS_DINODE_FMT_EXTENTS:
320 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
321 break;
322 case XFS_DINODE_FMT_BTREE:
323 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
324 break;
325 default:
326 xfs_inode_verifier_error(ip, error, __func__, dip,
327 sizeof(*dip), __this_address);
328 error = -EFSCORRUPTED;
329 break;
330 }
331
332 if (error) {
333 xfs_idestroy_fork(ip->i_afp);
334 kmem_cache_free(xfs_ifork_zone, ip->i_afp);
335 ip->i_afp = NULL;
336 }
337 return error;
338 }
339
340 /*
341 * Reallocate the space for if_broot based on the number of records
342 * being added or deleted as indicated in rec_diff. Move the records
343 * and pointers in if_broot to fit the new size. When shrinking this
344 * will eliminate holes between the records and pointers created by
345 * the caller. When growing this will create holes to be filled in
346 * by the caller.
347 *
348 * The caller must not request to add more records than would fit in
349 * the on-disk inode root. If the if_broot is currently NULL, then
350 * if we are adding records, one will be allocated. The caller must also
351 * not request that the number of records go below zero, although
352 * it can go to zero.
353 *
354 * ip -- the inode whose if_broot area is changing
355 * ext_diff -- the change in the number of records, positive or negative,
356 * requested for the if_broot array.
357 */
358 void
xfs_iroot_realloc(xfs_inode_t * ip,int rec_diff,int whichfork)359 xfs_iroot_realloc(
360 xfs_inode_t *ip,
361 int rec_diff,
362 int whichfork)
363 {
364 struct xfs_mount *mp = ip->i_mount;
365 int cur_max;
366 struct xfs_ifork *ifp;
367 struct xfs_btree_block *new_broot;
368 int new_max;
369 size_t new_size;
370 char *np;
371 char *op;
372
373 /*
374 * Handle the degenerate case quietly.
375 */
376 if (rec_diff == 0) {
377 return;
378 }
379
380 ifp = XFS_IFORK_PTR(ip, whichfork);
381 if (rec_diff > 0) {
382 /*
383 * If there wasn't any memory allocated before, just
384 * allocate it now and get out.
385 */
386 if (ifp->if_broot_bytes == 0) {
387 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
388 ifp->if_broot = kmem_alloc(new_size, KM_NOFS);
389 ifp->if_broot_bytes = (int)new_size;
390 return;
391 }
392
393 /*
394 * If there is already an existing if_broot, then we need
395 * to realloc() it and shift the pointers to their new
396 * location. The records don't change location because
397 * they are kept butted up against the btree block header.
398 */
399 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
400 new_max = cur_max + rec_diff;
401 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
402 ifp->if_broot = krealloc(ifp->if_broot, new_size,
403 GFP_NOFS | __GFP_NOFAIL);
404 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
405 ifp->if_broot_bytes);
406 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
407 (int)new_size);
408 ifp->if_broot_bytes = (int)new_size;
409 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
410 XFS_IFORK_SIZE(ip, whichfork));
411 memmove(np, op, cur_max * (uint)sizeof(xfs_fsblock_t));
412 return;
413 }
414
415 /*
416 * rec_diff is less than 0. In this case, we are shrinking the
417 * if_broot buffer. It must already exist. If we go to zero
418 * records, just get rid of the root and clear the status bit.
419 */
420 ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
421 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
422 new_max = cur_max + rec_diff;
423 ASSERT(new_max >= 0);
424 if (new_max > 0)
425 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
426 else
427 new_size = 0;
428 if (new_size > 0) {
429 new_broot = kmem_alloc(new_size, KM_NOFS);
430 /*
431 * First copy over the btree block header.
432 */
433 memcpy(new_broot, ifp->if_broot,
434 XFS_BMBT_BLOCK_LEN(ip->i_mount));
435 } else {
436 new_broot = NULL;
437 }
438
439 /*
440 * Only copy the records and pointers if there are any.
441 */
442 if (new_max > 0) {
443 /*
444 * First copy the records.
445 */
446 op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
447 np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
448 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
449
450 /*
451 * Then copy the pointers.
452 */
453 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
454 ifp->if_broot_bytes);
455 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
456 (int)new_size);
457 memcpy(np, op, new_max * (uint)sizeof(xfs_fsblock_t));
458 }
459 kmem_free(ifp->if_broot);
460 ifp->if_broot = new_broot;
461 ifp->if_broot_bytes = (int)new_size;
462 if (ifp->if_broot)
463 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
464 XFS_IFORK_SIZE(ip, whichfork));
465 return;
466 }
467
468
469 /*
470 * This is called when the amount of space needed for if_data
471 * is increased or decreased. The change in size is indicated by
472 * the number of bytes that need to be added or deleted in the
473 * byte_diff parameter.
474 *
475 * If the amount of space needed has decreased below the size of the
476 * inline buffer, then switch to using the inline buffer. Otherwise,
477 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
478 * to what is needed.
479 *
480 * ip -- the inode whose if_data area is changing
481 * byte_diff -- the change in the number of bytes, positive or negative,
482 * requested for the if_data array.
483 */
484 void
xfs_idata_realloc(struct xfs_inode * ip,int64_t byte_diff,int whichfork)485 xfs_idata_realloc(
486 struct xfs_inode *ip,
487 int64_t byte_diff,
488 int whichfork)
489 {
490 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
491 int64_t new_size = ifp->if_bytes + byte_diff;
492
493 ASSERT(new_size >= 0);
494 ASSERT(new_size <= XFS_IFORK_SIZE(ip, whichfork));
495
496 if (byte_diff == 0)
497 return;
498
499 if (new_size == 0) {
500 kmem_free(ifp->if_u1.if_data);
501 ifp->if_u1.if_data = NULL;
502 ifp->if_bytes = 0;
503 return;
504 }
505
506 /*
507 * For inline data, the underlying buffer must be a multiple of 4 bytes
508 * in size so that it can be logged and stay on word boundaries.
509 * We enforce that here, and use __GFP_ZERO to ensure that size
510 * extensions always zero the unused roundup area.
511 */
512 ifp->if_u1.if_data = krealloc(ifp->if_u1.if_data, roundup(new_size, 4),
513 GFP_NOFS | __GFP_NOFAIL | __GFP_ZERO);
514 ifp->if_bytes = new_size;
515 }
516
517 void
xfs_idestroy_fork(struct xfs_ifork * ifp)518 xfs_idestroy_fork(
519 struct xfs_ifork *ifp)
520 {
521 if (ifp->if_broot != NULL) {
522 kmem_free(ifp->if_broot);
523 ifp->if_broot = NULL;
524 }
525
526 switch (ifp->if_format) {
527 case XFS_DINODE_FMT_LOCAL:
528 kmem_free(ifp->if_u1.if_data);
529 ifp->if_u1.if_data = NULL;
530 break;
531 case XFS_DINODE_FMT_EXTENTS:
532 case XFS_DINODE_FMT_BTREE:
533 if (ifp->if_height)
534 xfs_iext_destroy(ifp);
535 break;
536 }
537 }
538
539 /*
540 * Convert in-core extents to on-disk form
541 *
542 * In the case of the data fork, the in-core and on-disk fork sizes can be
543 * different due to delayed allocation extents. We only copy on-disk extents
544 * here, so callers must always use the physical fork size to determine the
545 * size of the buffer passed to this routine. We will return the size actually
546 * used.
547 */
548 int
xfs_iextents_copy(struct xfs_inode * ip,struct xfs_bmbt_rec * dp,int whichfork)549 xfs_iextents_copy(
550 struct xfs_inode *ip,
551 struct xfs_bmbt_rec *dp,
552 int whichfork)
553 {
554 int state = xfs_bmap_fork_to_state(whichfork);
555 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
556 struct xfs_iext_cursor icur;
557 struct xfs_bmbt_irec rec;
558 int64_t copied = 0;
559
560 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED));
561 ASSERT(ifp->if_bytes > 0);
562
563 for_each_xfs_iext(ifp, &icur, &rec) {
564 if (isnullstartblock(rec.br_startblock))
565 continue;
566 ASSERT(xfs_bmap_validate_extent(ip, whichfork, &rec) == NULL);
567 xfs_bmbt_disk_set_all(dp, &rec);
568 trace_xfs_write_extent(ip, &icur, state, _RET_IP_);
569 copied += sizeof(struct xfs_bmbt_rec);
570 dp++;
571 }
572
573 ASSERT(copied > 0);
574 ASSERT(copied <= ifp->if_bytes);
575 return copied;
576 }
577
578 /*
579 * Each of the following cases stores data into the same region
580 * of the on-disk inode, so only one of them can be valid at
581 * any given time. While it is possible to have conflicting formats
582 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
583 * in EXTENTS format, this can only happen when the fork has
584 * changed formats after being modified but before being flushed.
585 * In these cases, the format always takes precedence, because the
586 * format indicates the current state of the fork.
587 */
588 void
xfs_iflush_fork(xfs_inode_t * ip,xfs_dinode_t * dip,struct xfs_inode_log_item * iip,int whichfork)589 xfs_iflush_fork(
590 xfs_inode_t *ip,
591 xfs_dinode_t *dip,
592 struct xfs_inode_log_item *iip,
593 int whichfork)
594 {
595 char *cp;
596 struct xfs_ifork *ifp;
597 xfs_mount_t *mp;
598 static const short brootflag[2] =
599 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
600 static const short dataflag[2] =
601 { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
602 static const short extflag[2] =
603 { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
604
605 if (!iip)
606 return;
607 ifp = XFS_IFORK_PTR(ip, whichfork);
608 /*
609 * This can happen if we gave up in iformat in an error path,
610 * for the attribute fork.
611 */
612 if (!ifp) {
613 ASSERT(whichfork == XFS_ATTR_FORK);
614 return;
615 }
616 cp = XFS_DFORK_PTR(dip, whichfork);
617 mp = ip->i_mount;
618 switch (ifp->if_format) {
619 case XFS_DINODE_FMT_LOCAL:
620 if ((iip->ili_fields & dataflag[whichfork]) &&
621 (ifp->if_bytes > 0)) {
622 ASSERT(ifp->if_u1.if_data != NULL);
623 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
624 memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
625 }
626 break;
627
628 case XFS_DINODE_FMT_EXTENTS:
629 if ((iip->ili_fields & extflag[whichfork]) &&
630 (ifp->if_bytes > 0)) {
631 ASSERT(ifp->if_nextents > 0);
632 (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
633 whichfork);
634 }
635 break;
636
637 case XFS_DINODE_FMT_BTREE:
638 if ((iip->ili_fields & brootflag[whichfork]) &&
639 (ifp->if_broot_bytes > 0)) {
640 ASSERT(ifp->if_broot != NULL);
641 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
642 XFS_IFORK_SIZE(ip, whichfork));
643 xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
644 (xfs_bmdr_block_t *)cp,
645 XFS_DFORK_SIZE(dip, mp, whichfork));
646 }
647 break;
648
649 case XFS_DINODE_FMT_DEV:
650 if (iip->ili_fields & XFS_ILOG_DEV) {
651 ASSERT(whichfork == XFS_DATA_FORK);
652 xfs_dinode_put_rdev(dip,
653 linux_to_xfs_dev_t(VFS_I(ip)->i_rdev));
654 }
655 break;
656
657 default:
658 ASSERT(0);
659 break;
660 }
661 }
662
663 /* Convert bmap state flags to an inode fork. */
664 struct xfs_ifork *
xfs_iext_state_to_fork(struct xfs_inode * ip,int state)665 xfs_iext_state_to_fork(
666 struct xfs_inode *ip,
667 int state)
668 {
669 if (state & BMAP_COWFORK)
670 return ip->i_cowfp;
671 else if (state & BMAP_ATTRFORK)
672 return ip->i_afp;
673 return &ip->i_df;
674 }
675
676 /*
677 * Initialize an inode's copy-on-write fork.
678 */
679 void
xfs_ifork_init_cow(struct xfs_inode * ip)680 xfs_ifork_init_cow(
681 struct xfs_inode *ip)
682 {
683 if (ip->i_cowfp)
684 return;
685
686 ip->i_cowfp = kmem_cache_zalloc(xfs_ifork_zone,
687 GFP_NOFS | __GFP_NOFAIL);
688 ip->i_cowfp->if_format = XFS_DINODE_FMT_EXTENTS;
689 }
690
691 /* Verify the inline contents of the data fork of an inode. */
692 int
xfs_ifork_verify_local_data(struct xfs_inode * ip)693 xfs_ifork_verify_local_data(
694 struct xfs_inode *ip)
695 {
696 xfs_failaddr_t fa = NULL;
697
698 switch (VFS_I(ip)->i_mode & S_IFMT) {
699 case S_IFDIR:
700 fa = xfs_dir2_sf_verify(ip);
701 break;
702 case S_IFLNK:
703 fa = xfs_symlink_shortform_verify(ip);
704 break;
705 default:
706 break;
707 }
708
709 if (fa) {
710 xfs_inode_verifier_error(ip, -EFSCORRUPTED, "data fork",
711 ip->i_df.if_u1.if_data, ip->i_df.if_bytes, fa);
712 return -EFSCORRUPTED;
713 }
714
715 return 0;
716 }
717
718 /* Verify the inline contents of the attr fork of an inode. */
719 int
xfs_ifork_verify_local_attr(struct xfs_inode * ip)720 xfs_ifork_verify_local_attr(
721 struct xfs_inode *ip)
722 {
723 struct xfs_ifork *ifp = ip->i_afp;
724 xfs_failaddr_t fa;
725
726 if (!ifp)
727 fa = __this_address;
728 else
729 fa = xfs_attr_shortform_verify(ip);
730
731 if (fa) {
732 xfs_inode_verifier_error(ip, -EFSCORRUPTED, "attr fork",
733 ifp ? ifp->if_u1.if_data : NULL,
734 ifp ? ifp->if_bytes : 0, fa);
735 return -EFSCORRUPTED;
736 }
737
738 return 0;
739 }
740
741 int
xfs_iext_count_may_overflow(struct xfs_inode * ip,int whichfork,int nr_to_add)742 xfs_iext_count_may_overflow(
743 struct xfs_inode *ip,
744 int whichfork,
745 int nr_to_add)
746 {
747 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
748 uint64_t max_exts;
749 uint64_t nr_exts;
750
751 if (whichfork == XFS_COW_FORK)
752 return 0;
753
754 max_exts = (whichfork == XFS_ATTR_FORK) ? MAXAEXTNUM : MAXEXTNUM;
755
756 if (XFS_TEST_ERROR(false, ip->i_mount, XFS_ERRTAG_REDUCE_MAX_IEXTENTS))
757 max_exts = 10;
758
759 nr_exts = ifp->if_nextents + nr_to_add;
760 if (nr_exts < ifp->if_nextents || nr_exts > max_exts)
761 return -EFBIG;
762
763 return 0;
764 }
765