xfs: only look at the fork format in xfs_idestroy_fork
[linux-2.6-microblaze.git] / fs / xfs / 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 "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
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                 real_size = roundup(mem_size, 4);
54                 ifp->if_u1.if_data = kmem_alloc(real_size, KM_NOFS);
55                 memcpy(ifp->if_u1.if_data, data, size);
56                 if (zero_terminate)
57                         ifp->if_u1.if_data[size] = '\0';
58         } else {
59                 ifp->if_u1.if_data = NULL;
60         }
61
62         ifp->if_bytes = size;
63         ifp->if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
64         ifp->if_flags |= XFS_IFINLINE;
65 }
66
67 /*
68  * The file is in-lined in the on-disk inode.
69  */
70 STATIC int
71 xfs_iformat_local(
72         xfs_inode_t     *ip,
73         xfs_dinode_t    *dip,
74         int             whichfork,
75         int             size)
76 {
77         /*
78          * If the size is unreasonable, then something
79          * is wrong and we just bail out rather than crash in
80          * kmem_alloc() or memcpy() below.
81          */
82         if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
83                 xfs_warn(ip->i_mount,
84         "corrupt inode %Lu (bad size %d for local fork, size = %zd).",
85                         (unsigned long long) ip->i_ino, size,
86                         XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
87                 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
88                                 "xfs_iformat_local", dip, sizeof(*dip),
89                                 __this_address);
90                 return -EFSCORRUPTED;
91         }
92
93         xfs_init_local_fork(ip, whichfork, XFS_DFORK_PTR(dip, whichfork), size);
94         return 0;
95 }
96
97 /*
98  * The file consists of a set of extents all of which fit into the on-disk
99  * inode.
100  */
101 STATIC int
102 xfs_iformat_extents(
103         struct xfs_inode        *ip,
104         struct xfs_dinode       *dip,
105         int                     whichfork)
106 {
107         struct xfs_mount        *mp = ip->i_mount;
108         struct xfs_ifork        *ifp = XFS_IFORK_PTR(ip, whichfork);
109         int                     state = xfs_bmap_fork_to_state(whichfork);
110         int                     nex = XFS_DFORK_NEXTENTS(dip, whichfork);
111         int                     size = nex * sizeof(xfs_bmbt_rec_t);
112         struct xfs_iext_cursor  icur;
113         struct xfs_bmbt_rec     *dp;
114         struct xfs_bmbt_irec    new;
115         int                     i;
116
117         /*
118          * If the number of extents is unreasonable, then something is wrong and
119          * we just bail out rather than crash in kmem_alloc() or memcpy() below.
120          */
121         if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, mp, whichfork))) {
122                 xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
123                         (unsigned long long) ip->i_ino, nex);
124                 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
125                                 "xfs_iformat_extents(1)", dip, sizeof(*dip),
126                                 __this_address);
127                 return -EFSCORRUPTED;
128         }
129
130         ifp->if_bytes = 0;
131         ifp->if_u1.if_root = NULL;
132         ifp->if_height = 0;
133         if (size) {
134                 dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
135
136                 xfs_iext_first(ifp, &icur);
137                 for (i = 0; i < nex; i++, dp++) {
138                         xfs_failaddr_t  fa;
139
140                         xfs_bmbt_disk_get_all(dp, &new);
141                         fa = xfs_bmap_validate_extent(ip, whichfork, &new);
142                         if (fa) {
143                                 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
144                                                 "xfs_iformat_extents(2)",
145                                                 dp, sizeof(*dp), fa);
146                                 return -EFSCORRUPTED;
147                         }
148
149                         xfs_iext_insert(ip, &icur, &new, state);
150                         trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
151                         xfs_iext_next(ifp, &icur);
152                 }
153         }
154         ifp->if_flags |= XFS_IFEXTENTS;
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         xfs_inode_t             *ip,
169         xfs_dinode_t            *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         ifp->if_flags &= ~XFS_IFEXTENTS;
217         ifp->if_flags |= XFS_IFBROOT;
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
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
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 *
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
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                 kmem_cache_free(xfs_ifork_zone, ip->i_afp);
334                 ip->i_afp = NULL;
335         }
336         return error;
337 }
338
339 /*
340  * Reallocate the space for if_broot based on the number of records
341  * being added or deleted as indicated in rec_diff.  Move the records
342  * and pointers in if_broot to fit the new size.  When shrinking this
343  * will eliminate holes between the records and pointers created by
344  * the caller.  When growing this will create holes to be filled in
345  * by the caller.
346  *
347  * The caller must not request to add more records than would fit in
348  * the on-disk inode root.  If the if_broot is currently NULL, then
349  * if we are adding records, one will be allocated.  The caller must also
350  * not request that the number of records go below zero, although
351  * it can go to zero.
352  *
353  * ip -- the inode whose if_broot area is changing
354  * ext_diff -- the change in the number of records, positive or negative,
355  *       requested for the if_broot array.
356  */
357 void
358 xfs_iroot_realloc(
359         xfs_inode_t             *ip,
360         int                     rec_diff,
361         int                     whichfork)
362 {
363         struct xfs_mount        *mp = ip->i_mount;
364         int                     cur_max;
365         struct xfs_ifork        *ifp;
366         struct xfs_btree_block  *new_broot;
367         int                     new_max;
368         size_t                  new_size;
369         char                    *np;
370         char                    *op;
371
372         /*
373          * Handle the degenerate case quietly.
374          */
375         if (rec_diff == 0) {
376                 return;
377         }
378
379         ifp = XFS_IFORK_PTR(ip, whichfork);
380         if (rec_diff > 0) {
381                 /*
382                  * If there wasn't any memory allocated before, just
383                  * allocate it now and get out.
384                  */
385                 if (ifp->if_broot_bytes == 0) {
386                         new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
387                         ifp->if_broot = kmem_alloc(new_size, KM_NOFS);
388                         ifp->if_broot_bytes = (int)new_size;
389                         return;
390                 }
391
392                 /*
393                  * If there is already an existing if_broot, then we need
394                  * to realloc() it and shift the pointers to their new
395                  * location.  The records don't change location because
396                  * they are kept butted up against the btree block header.
397                  */
398                 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
399                 new_max = cur_max + rec_diff;
400                 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
401                 ifp->if_broot = krealloc(ifp->if_broot, new_size,
402                                          GFP_NOFS | __GFP_NOFAIL);
403                 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
404                                                      ifp->if_broot_bytes);
405                 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
406                                                      (int)new_size);
407                 ifp->if_broot_bytes = (int)new_size;
408                 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
409                         XFS_IFORK_SIZE(ip, whichfork));
410                 memmove(np, op, cur_max * (uint)sizeof(xfs_fsblock_t));
411                 return;
412         }
413
414         /*
415          * rec_diff is less than 0.  In this case, we are shrinking the
416          * if_broot buffer.  It must already exist.  If we go to zero
417          * records, just get rid of the root and clear the status bit.
418          */
419         ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
420         cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
421         new_max = cur_max + rec_diff;
422         ASSERT(new_max >= 0);
423         if (new_max > 0)
424                 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
425         else
426                 new_size = 0;
427         if (new_size > 0) {
428                 new_broot = kmem_alloc(new_size, KM_NOFS);
429                 /*
430                  * First copy over the btree block header.
431                  */
432                 memcpy(new_broot, ifp->if_broot,
433                         XFS_BMBT_BLOCK_LEN(ip->i_mount));
434         } else {
435                 new_broot = NULL;
436                 ifp->if_flags &= ~XFS_IFBROOT;
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
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.
510          */
511         ifp->if_u1.if_data = krealloc(ifp->if_u1.if_data, roundup(new_size, 4),
512                                       GFP_NOFS | __GFP_NOFAIL);
513         ifp->if_bytes = new_size;
514 }
515
516 void
517 xfs_idestroy_fork(
518         struct xfs_ifork        *ifp)
519 {
520         if (ifp->if_broot != NULL) {
521                 kmem_free(ifp->if_broot);
522                 ifp->if_broot = NULL;
523         }
524
525         switch (ifp->if_format) {
526         case XFS_DINODE_FMT_LOCAL:
527                 kmem_free(ifp->if_u1.if_data);
528                 ifp->if_u1.if_data = NULL;
529                 break;
530         case XFS_DINODE_FMT_EXTENTS:
531         case XFS_DINODE_FMT_BTREE:
532                 if (ifp->if_height)
533                         xfs_iext_destroy(ifp);
534                 break;
535         }
536 }
537
538 /*
539  * Convert in-core extents to on-disk form
540  *
541  * In the case of the data fork, the in-core and on-disk fork sizes can be
542  * different due to delayed allocation extents. We only copy on-disk extents
543  * here, so callers must always use the physical fork size to determine the
544  * size of the buffer passed to this routine.  We will return the size actually
545  * used.
546  */
547 int
548 xfs_iextents_copy(
549         struct xfs_inode        *ip,
550         struct xfs_bmbt_rec     *dp,
551         int                     whichfork)
552 {
553         int                     state = xfs_bmap_fork_to_state(whichfork);
554         struct xfs_ifork        *ifp = XFS_IFORK_PTR(ip, whichfork);
555         struct xfs_iext_cursor  icur;
556         struct xfs_bmbt_irec    rec;
557         int64_t                 copied = 0;
558
559         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED));
560         ASSERT(ifp->if_bytes > 0);
561
562         for_each_xfs_iext(ifp, &icur, &rec) {
563                 if (isnullstartblock(rec.br_startblock))
564                         continue;
565                 ASSERT(xfs_bmap_validate_extent(ip, whichfork, &rec) == NULL);
566                 xfs_bmbt_disk_set_all(dp, &rec);
567                 trace_xfs_write_extent(ip, &icur, state, _RET_IP_);
568                 copied += sizeof(struct xfs_bmbt_rec);
569                 dp++;
570         }
571
572         ASSERT(copied > 0);
573         ASSERT(copied <= ifp->if_bytes);
574         return copied;
575 }
576
577 /*
578  * Each of the following cases stores data into the same region
579  * of the on-disk inode, so only one of them can be valid at
580  * any given time. While it is possible to have conflicting formats
581  * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
582  * in EXTENTS format, this can only happen when the fork has
583  * changed formats after being modified but before being flushed.
584  * In these cases, the format always takes precedence, because the
585  * format indicates the current state of the fork.
586  */
587 void
588 xfs_iflush_fork(
589         xfs_inode_t             *ip,
590         xfs_dinode_t            *dip,
591         struct xfs_inode_log_item *iip,
592         int                     whichfork)
593 {
594         char                    *cp;
595         struct xfs_ifork        *ifp;
596         xfs_mount_t             *mp;
597         static const short      brootflag[2] =
598                 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
599         static const short      dataflag[2] =
600                 { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
601         static const short      extflag[2] =
602                 { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
603
604         if (!iip)
605                 return;
606         ifp = XFS_IFORK_PTR(ip, whichfork);
607         /*
608          * This can happen if we gave up in iformat in an error path,
609          * for the attribute fork.
610          */
611         if (!ifp) {
612                 ASSERT(whichfork == XFS_ATTR_FORK);
613                 return;
614         }
615         cp = XFS_DFORK_PTR(dip, whichfork);
616         mp = ip->i_mount;
617         switch (ifp->if_format) {
618         case XFS_DINODE_FMT_LOCAL:
619                 if ((iip->ili_fields & dataflag[whichfork]) &&
620                     (ifp->if_bytes > 0)) {
621                         ASSERT(ifp->if_u1.if_data != NULL);
622                         ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
623                         memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
624                 }
625                 break;
626
627         case XFS_DINODE_FMT_EXTENTS:
628                 ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
629                        !(iip->ili_fields & extflag[whichfork]));
630                 if ((iip->ili_fields & extflag[whichfork]) &&
631                     (ifp->if_bytes > 0)) {
632                         ASSERT(ifp->if_nextents > 0);
633                         (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
634                                 whichfork);
635                 }
636                 break;
637
638         case XFS_DINODE_FMT_BTREE:
639                 if ((iip->ili_fields & brootflag[whichfork]) &&
640                     (ifp->if_broot_bytes > 0)) {
641                         ASSERT(ifp->if_broot != NULL);
642                         ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
643                                 XFS_IFORK_SIZE(ip, whichfork));
644                         xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
645                                 (xfs_bmdr_block_t *)cp,
646                                 XFS_DFORK_SIZE(dip, mp, whichfork));
647                 }
648                 break;
649
650         case XFS_DINODE_FMT_DEV:
651                 if (iip->ili_fields & XFS_ILOG_DEV) {
652                         ASSERT(whichfork == XFS_DATA_FORK);
653                         xfs_dinode_put_rdev(dip,
654                                         linux_to_xfs_dev_t(VFS_I(ip)->i_rdev));
655                 }
656                 break;
657
658         default:
659                 ASSERT(0);
660                 break;
661         }
662 }
663
664 /* Convert bmap state flags to an inode fork. */
665 struct xfs_ifork *
666 xfs_iext_state_to_fork(
667         struct xfs_inode        *ip,
668         int                     state)
669 {
670         if (state & BMAP_COWFORK)
671                 return ip->i_cowfp;
672         else if (state & BMAP_ATTRFORK)
673                 return ip->i_afp;
674         return &ip->i_df;
675 }
676
677 /*
678  * Initialize an inode's copy-on-write fork.
679  */
680 void
681 xfs_ifork_init_cow(
682         struct xfs_inode        *ip)
683 {
684         if (ip->i_cowfp)
685                 return;
686
687         ip->i_cowfp = kmem_cache_zalloc(xfs_ifork_zone,
688                                        GFP_NOFS | __GFP_NOFAIL);
689         ip->i_cowfp->if_flags = XFS_IFEXTENTS;
690         ip->i_cowfp->if_format = XFS_DINODE_FMT_EXTENTS;
691 }
692
693 /* Verify the inline contents of the data fork of an inode. */
694 int
695 xfs_ifork_verify_local_data(
696         struct xfs_inode        *ip)
697 {
698         xfs_failaddr_t          fa = NULL;
699
700         switch (VFS_I(ip)->i_mode & S_IFMT) {
701         case S_IFDIR:
702                 fa = xfs_dir2_sf_verify(ip);
703                 break;
704         case S_IFLNK:
705                 fa = xfs_symlink_shortform_verify(ip);
706                 break;
707         default:
708                 break;
709         }
710
711         if (fa) {
712                 xfs_inode_verifier_error(ip, -EFSCORRUPTED, "data fork",
713                                 ip->i_df.if_u1.if_data, ip->i_df.if_bytes, fa);
714                 return -EFSCORRUPTED;
715         }
716
717         return 0;
718 }
719
720 /* Verify the inline contents of the attr fork of an inode. */
721 int
722 xfs_ifork_verify_local_attr(
723         struct xfs_inode        *ip)
724 {
725         struct xfs_ifork        *ifp = ip->i_afp;
726         xfs_failaddr_t          fa;
727
728         if (!ifp)
729                 fa = __this_address;
730         else
731                 fa = xfs_attr_shortform_verify(ip);
732
733         if (fa) {
734                 xfs_inode_verifier_error(ip, -EFSCORRUPTED, "attr fork",
735                                 ifp ? ifp->if_u1.if_data : NULL,
736                                 ifp ? ifp->if_bytes : 0, fa);
737                 return -EFSCORRUPTED;
738         }
739
740         return 0;
741 }
742
743 int
744 xfs_iext_count_may_overflow(
745         struct xfs_inode        *ip,
746         int                     whichfork,
747         int                     nr_to_add)
748 {
749         struct xfs_ifork        *ifp = XFS_IFORK_PTR(ip, whichfork);
750         uint64_t                max_exts;
751         uint64_t                nr_exts;
752
753         if (whichfork == XFS_COW_FORK)
754                 return 0;
755
756         max_exts = (whichfork == XFS_ATTR_FORK) ? MAXAEXTNUM : MAXEXTNUM;
757
758         if (XFS_TEST_ERROR(false, ip->i_mount, XFS_ERRTAG_REDUCE_MAX_IEXTENTS))
759                 max_exts = 10;
760
761         nr_exts = ifp->if_nextents + nr_to_add;
762         if (nr_exts < ifp->if_nextents || nr_exts > max_exts)
763                 return -EFBIG;
764
765         return 0;
766 }