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