xfs: Replace xfs_isilocked with xfs_assert_ilocked
[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 struct kmem_cache *xfs_ifork_cache;
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;
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                 char *new_data = kmalloc(mem_size,
54                                 GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL);
55
56                 memcpy(new_data, data, size);
57                 if (zero_terminate)
58                         new_data[size] = '\0';
59
60                 ifp->if_data = new_data;
61         } else {
62                 ifp->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          * kmalloc() 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 %llu (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 kmalloc() 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_data = 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 xfs_bmap_complain_bad_rec(ip, whichfork,
148                                                 fa, &new);
149                         }
150
151                         xfs_iext_insert(ip, &icur, &new, state);
152                         trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
153                         xfs_iext_next(ifp, &icur);
154                 }
155         }
156         return 0;
157 }
158
159 /*
160  * The file has too many extents to fit into
161  * the inode, so they are in B-tree format.
162  * Allocate a buffer for the root of the B-tree
163  * and copy the root into it.  The i_extents
164  * field will remain NULL until all of the
165  * extents are read in (when they are needed).
166  */
167 STATIC int
168 xfs_iformat_btree(
169         struct xfs_inode        *ip,
170         struct xfs_dinode       *dip,
171         int                     whichfork)
172 {
173         struct xfs_mount        *mp = ip->i_mount;
174         xfs_bmdr_block_t        *dfp;
175         struct xfs_ifork        *ifp;
176         /* REFERENCED */
177         int                     nrecs;
178         int                     size;
179         int                     level;
180
181         ifp = xfs_ifork_ptr(ip, whichfork);
182         dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
183         size = XFS_BMAP_BROOT_SPACE(mp, dfp);
184         nrecs = be16_to_cpu(dfp->bb_numrecs);
185         level = be16_to_cpu(dfp->bb_level);
186
187         /*
188          * blow out if -- fork has less extents than can fit in
189          * fork (fork shouldn't be a btree format), root btree
190          * block has more records than can fit into the fork,
191          * or the number of extents is greater than the number of
192          * blocks.
193          */
194         if (unlikely(ifp->if_nextents <= XFS_IFORK_MAXEXT(ip, whichfork) ||
195                      nrecs == 0 ||
196                      XFS_BMDR_SPACE_CALC(nrecs) >
197                                         XFS_DFORK_SIZE(dip, mp, whichfork) ||
198                      ifp->if_nextents > ip->i_nblocks) ||
199                      level == 0 || level > XFS_BM_MAXLEVELS(mp, whichfork)) {
200                 xfs_warn(mp, "corrupt inode %llu (btree).",
201                                         (unsigned long long) ip->i_ino);
202                 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
203                                 "xfs_iformat_btree", dfp, size,
204                                 __this_address);
205                 return -EFSCORRUPTED;
206         }
207
208         ifp->if_broot_bytes = size;
209         ifp->if_broot = kmalloc(size,
210                                 GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL);
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_data = 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.  Use release semantics to set needextents /after/ we
236          * set the format. This ensures that we can use acquire semantics on
237          * needextents in xfs_need_iread_extents() and be guaranteed to see a
238          * valid format value after that load.
239          */
240         ip->i_df.if_format = dip->di_format;
241         ip->i_df.if_nextents = xfs_dfork_data_extents(dip);
242         smp_store_release(&ip->i_df.if_needextents,
243                            ip->i_df.if_format == XFS_DINODE_FMT_BTREE ? 1 : 0);
244
245         switch (inode->i_mode & S_IFMT) {
246         case S_IFIFO:
247         case S_IFCHR:
248         case S_IFBLK:
249         case S_IFSOCK:
250                 ip->i_disk_size = 0;
251                 inode->i_rdev = xfs_to_linux_dev_t(xfs_dinode_get_rdev(dip));
252                 return 0;
253         case S_IFREG:
254         case S_IFLNK:
255         case S_IFDIR:
256                 switch (ip->i_df.if_format) {
257                 case XFS_DINODE_FMT_LOCAL:
258                         error = xfs_iformat_local(ip, dip, XFS_DATA_FORK,
259                                         be64_to_cpu(dip->di_size));
260                         if (!error)
261                                 error = xfs_ifork_verify_local_data(ip);
262                         return error;
263                 case XFS_DINODE_FMT_EXTENTS:
264                         return xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
265                 case XFS_DINODE_FMT_BTREE:
266                         return xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
267                 default:
268                         xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__,
269                                         dip, sizeof(*dip), __this_address);
270                         return -EFSCORRUPTED;
271                 }
272                 break;
273         default:
274                 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, dip,
275                                 sizeof(*dip), __this_address);
276                 return -EFSCORRUPTED;
277         }
278 }
279
280 static uint16_t
281 xfs_dfork_attr_shortform_size(
282         struct xfs_dinode               *dip)
283 {
284         struct xfs_attr_sf_hdr          *sf = XFS_DFORK_APTR(dip);
285
286         return be16_to_cpu(sf->totsize);
287 }
288
289 void
290 xfs_ifork_init_attr(
291         struct xfs_inode        *ip,
292         enum xfs_dinode_fmt     format,
293         xfs_extnum_t            nextents)
294 {
295         /*
296          * Initialize the extent count early, as the per-format routines may
297          * depend on it.  Use release semantics to set needextents /after/ we
298          * set the format. This ensures that we can use acquire semantics on
299          * needextents in xfs_need_iread_extents() and be guaranteed to see a
300          * valid format value after that load.
301          */
302         ip->i_af.if_format = format;
303         ip->i_af.if_nextents = nextents;
304         smp_store_release(&ip->i_af.if_needextents,
305                            ip->i_af.if_format == XFS_DINODE_FMT_BTREE ? 1 : 0);
306 }
307
308 void
309 xfs_ifork_zap_attr(
310         struct xfs_inode        *ip)
311 {
312         xfs_idestroy_fork(&ip->i_af);
313         memset(&ip->i_af, 0, sizeof(struct xfs_ifork));
314         ip->i_af.if_format = XFS_DINODE_FMT_EXTENTS;
315 }
316
317 int
318 xfs_iformat_attr_fork(
319         struct xfs_inode        *ip,
320         struct xfs_dinode       *dip)
321 {
322         xfs_extnum_t            naextents = xfs_dfork_attr_extents(dip);
323         int                     error = 0;
324
325         /*
326          * Initialize the extent count early, as the per-format routines may
327          * depend on it.
328          */
329         xfs_ifork_init_attr(ip, dip->di_aformat, naextents);
330
331         switch (ip->i_af.if_format) {
332         case XFS_DINODE_FMT_LOCAL:
333                 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK,
334                                 xfs_dfork_attr_shortform_size(dip));
335                 if (!error)
336                         error = xfs_ifork_verify_local_attr(ip);
337                 break;
338         case XFS_DINODE_FMT_EXTENTS:
339                 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
340                 break;
341         case XFS_DINODE_FMT_BTREE:
342                 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
343                 break;
344         default:
345                 xfs_inode_verifier_error(ip, error, __func__, dip,
346                                 sizeof(*dip), __this_address);
347                 error = -EFSCORRUPTED;
348                 break;
349         }
350
351         if (error)
352                 xfs_ifork_zap_attr(ip);
353         return error;
354 }
355
356 /*
357  * Reallocate the space for if_broot based on the number of records
358  * being added or deleted as indicated in rec_diff.  Move the records
359  * and pointers in if_broot to fit the new size.  When shrinking this
360  * will eliminate holes between the records and pointers created by
361  * the caller.  When growing this will create holes to be filled in
362  * by the caller.
363  *
364  * The caller must not request to add more records than would fit in
365  * the on-disk inode root.  If the if_broot is currently NULL, then
366  * if we are adding records, one will be allocated.  The caller must also
367  * not request that the number of records go below zero, although
368  * it can go to zero.
369  *
370  * ip -- the inode whose if_broot area is changing
371  * ext_diff -- the change in the number of records, positive or negative,
372  *       requested for the if_broot array.
373  */
374 void
375 xfs_iroot_realloc(
376         xfs_inode_t             *ip,
377         int                     rec_diff,
378         int                     whichfork)
379 {
380         struct xfs_mount        *mp = ip->i_mount;
381         int                     cur_max;
382         struct xfs_ifork        *ifp;
383         struct xfs_btree_block  *new_broot;
384         int                     new_max;
385         size_t                  new_size;
386         char                    *np;
387         char                    *op;
388
389         /*
390          * Handle the degenerate case quietly.
391          */
392         if (rec_diff == 0) {
393                 return;
394         }
395
396         ifp = xfs_ifork_ptr(ip, whichfork);
397         if (rec_diff > 0) {
398                 /*
399                  * If there wasn't any memory allocated before, just
400                  * allocate it now and get out.
401                  */
402                 if (ifp->if_broot_bytes == 0) {
403                         new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
404                         ifp->if_broot = kmalloc(new_size,
405                                                 GFP_KERNEL | __GFP_NOFAIL);
406                         ifp->if_broot_bytes = (int)new_size;
407                         return;
408                 }
409
410                 /*
411                  * If there is already an existing if_broot, then we need
412                  * to realloc() it and shift the pointers to their new
413                  * location.  The records don't change location because
414                  * they are kept butted up against the btree block header.
415                  */
416                 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
417                 new_max = cur_max + rec_diff;
418                 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
419                 ifp->if_broot = krealloc(ifp->if_broot, new_size,
420                                          GFP_KERNEL | __GFP_NOFAIL);
421                 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
422                                                      ifp->if_broot_bytes);
423                 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
424                                                      (int)new_size);
425                 ifp->if_broot_bytes = (int)new_size;
426                 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
427                         xfs_inode_fork_size(ip, whichfork));
428                 memmove(np, op, cur_max * (uint)sizeof(xfs_fsblock_t));
429                 return;
430         }
431
432         /*
433          * rec_diff is less than 0.  In this case, we are shrinking the
434          * if_broot buffer.  It must already exist.  If we go to zero
435          * records, just get rid of the root and clear the status bit.
436          */
437         ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
438         cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
439         new_max = cur_max + rec_diff;
440         ASSERT(new_max >= 0);
441         if (new_max > 0)
442                 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
443         else
444                 new_size = 0;
445         if (new_size > 0) {
446                 new_broot = kmalloc(new_size, GFP_KERNEL | __GFP_NOFAIL);
447                 /*
448                  * First copy over the btree block header.
449                  */
450                 memcpy(new_broot, ifp->if_broot,
451                         XFS_BMBT_BLOCK_LEN(ip->i_mount));
452         } else {
453                 new_broot = NULL;
454         }
455
456         /*
457          * Only copy the records and pointers if there are any.
458          */
459         if (new_max > 0) {
460                 /*
461                  * First copy the records.
462                  */
463                 op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
464                 np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
465                 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
466
467                 /*
468                  * Then copy the pointers.
469                  */
470                 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
471                                                      ifp->if_broot_bytes);
472                 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
473                                                      (int)new_size);
474                 memcpy(np, op, new_max * (uint)sizeof(xfs_fsblock_t));
475         }
476         kfree(ifp->if_broot);
477         ifp->if_broot = new_broot;
478         ifp->if_broot_bytes = (int)new_size;
479         if (ifp->if_broot)
480                 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
481                         xfs_inode_fork_size(ip, whichfork));
482         return;
483 }
484
485
486 /*
487  * This is called when the amount of space needed for if_data
488  * is increased or decreased.  The change in size is indicated by
489  * the number of bytes that need to be added or deleted in the
490  * byte_diff parameter.
491  *
492  * If the amount of space needed has decreased below the size of the
493  * inline buffer, then switch to using the inline buffer.  Otherwise,
494  * use krealloc() or kmalloc() to adjust the size of the buffer
495  * to what is needed.
496  *
497  * ip -- the inode whose if_data area is changing
498  * byte_diff -- the change in the number of bytes, positive or negative,
499  *       requested for the if_data array.
500  */
501 void *
502 xfs_idata_realloc(
503         struct xfs_inode        *ip,
504         int64_t                 byte_diff,
505         int                     whichfork)
506 {
507         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
508         int64_t                 new_size = ifp->if_bytes + byte_diff;
509
510         ASSERT(new_size >= 0);
511         ASSERT(new_size <= xfs_inode_fork_size(ip, whichfork));
512
513         if (byte_diff) {
514                 ifp->if_data = krealloc(ifp->if_data, new_size,
515                                         GFP_KERNEL | __GFP_NOFAIL);
516                 if (new_size == 0)
517                         ifp->if_data = NULL;
518                 ifp->if_bytes = new_size;
519         }
520
521         return ifp->if_data;
522 }
523
524 /* Free all memory and reset a fork back to its initial state. */
525 void
526 xfs_idestroy_fork(
527         struct xfs_ifork        *ifp)
528 {
529         if (ifp->if_broot != NULL) {
530                 kfree(ifp->if_broot);
531                 ifp->if_broot = NULL;
532         }
533
534         switch (ifp->if_format) {
535         case XFS_DINODE_FMT_LOCAL:
536                 kfree(ifp->if_data);
537                 ifp->if_data = NULL;
538                 break;
539         case XFS_DINODE_FMT_EXTENTS:
540         case XFS_DINODE_FMT_BTREE:
541                 if (ifp->if_height)
542                         xfs_iext_destroy(ifp);
543                 break;
544         }
545 }
546
547 /*
548  * Convert in-core extents to on-disk form
549  *
550  * In the case of the data fork, the in-core and on-disk fork sizes can be
551  * different due to delayed allocation extents. We only copy on-disk extents
552  * here, so callers must always use the physical fork size to determine the
553  * size of the buffer passed to this routine.  We will return the size actually
554  * used.
555  */
556 int
557 xfs_iextents_copy(
558         struct xfs_inode        *ip,
559         struct xfs_bmbt_rec     *dp,
560         int                     whichfork)
561 {
562         int                     state = xfs_bmap_fork_to_state(whichfork);
563         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
564         struct xfs_iext_cursor  icur;
565         struct xfs_bmbt_irec    rec;
566         int64_t                 copied = 0;
567
568         xfs_assert_ilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED);
569         ASSERT(ifp->if_bytes > 0);
570
571         for_each_xfs_iext(ifp, &icur, &rec) {
572                 if (isnullstartblock(rec.br_startblock))
573                         continue;
574                 ASSERT(xfs_bmap_validate_extent(ip, whichfork, &rec) == NULL);
575                 xfs_bmbt_disk_set_all(dp, &rec);
576                 trace_xfs_write_extent(ip, &icur, state, _RET_IP_);
577                 copied += sizeof(struct xfs_bmbt_rec);
578                 dp++;
579         }
580
581         ASSERT(copied > 0);
582         ASSERT(copied <= ifp->if_bytes);
583         return copied;
584 }
585
586 /*
587  * Each of the following cases stores data into the same region
588  * of the on-disk inode, so only one of them can be valid at
589  * any given time. While it is possible to have conflicting formats
590  * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
591  * in EXTENTS format, this can only happen when the fork has
592  * changed formats after being modified but before being flushed.
593  * In these cases, the format always takes precedence, because the
594  * format indicates the current state of the fork.
595  */
596 void
597 xfs_iflush_fork(
598         struct xfs_inode        *ip,
599         struct xfs_dinode       *dip,
600         struct xfs_inode_log_item *iip,
601         int                     whichfork)
602 {
603         char                    *cp;
604         struct xfs_ifork        *ifp;
605         xfs_mount_t             *mp;
606         static const short      brootflag[2] =
607                 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
608         static const short      dataflag[2] =
609                 { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
610         static const short      extflag[2] =
611                 { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
612
613         if (!iip)
614                 return;
615         ifp = xfs_ifork_ptr(ip, whichfork);
616         /*
617          * This can happen if we gave up in iformat in an error path,
618          * for the attribute fork.
619          */
620         if (!ifp) {
621                 ASSERT(whichfork == XFS_ATTR_FORK);
622                 return;
623         }
624         cp = XFS_DFORK_PTR(dip, whichfork);
625         mp = ip->i_mount;
626         switch (ifp->if_format) {
627         case XFS_DINODE_FMT_LOCAL:
628                 if ((iip->ili_fields & dataflag[whichfork]) &&
629                     (ifp->if_bytes > 0)) {
630                         ASSERT(ifp->if_data != NULL);
631                         ASSERT(ifp->if_bytes <= xfs_inode_fork_size(ip, whichfork));
632                         memcpy(cp, ifp->if_data, ifp->if_bytes);
633                 }
634                 break;
635
636         case XFS_DINODE_FMT_EXTENTS:
637                 if ((iip->ili_fields & extflag[whichfork]) &&
638                     (ifp->if_bytes > 0)) {
639                         ASSERT(ifp->if_nextents > 0);
640                         (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
641                                 whichfork);
642                 }
643                 break;
644
645         case XFS_DINODE_FMT_BTREE:
646                 if ((iip->ili_fields & brootflag[whichfork]) &&
647                     (ifp->if_broot_bytes > 0)) {
648                         ASSERT(ifp->if_broot != NULL);
649                         ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
650                                 xfs_inode_fork_size(ip, whichfork));
651                         xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
652                                 (xfs_bmdr_block_t *)cp,
653                                 XFS_DFORK_SIZE(dip, mp, whichfork));
654                 }
655                 break;
656
657         case XFS_DINODE_FMT_DEV:
658                 if (iip->ili_fields & XFS_ILOG_DEV) {
659                         ASSERT(whichfork == XFS_DATA_FORK);
660                         xfs_dinode_put_rdev(dip,
661                                         linux_to_xfs_dev_t(VFS_I(ip)->i_rdev));
662                 }
663                 break;
664
665         default:
666                 ASSERT(0);
667                 break;
668         }
669 }
670
671 /* Convert bmap state flags to an inode fork. */
672 struct xfs_ifork *
673 xfs_iext_state_to_fork(
674         struct xfs_inode        *ip,
675         int                     state)
676 {
677         if (state & BMAP_COWFORK)
678                 return ip->i_cowfp;
679         else if (state & BMAP_ATTRFORK)
680                 return &ip->i_af;
681         return &ip->i_df;
682 }
683
684 /*
685  * Initialize an inode's copy-on-write fork.
686  */
687 void
688 xfs_ifork_init_cow(
689         struct xfs_inode        *ip)
690 {
691         if (ip->i_cowfp)
692                 return;
693
694         ip->i_cowfp = kmem_cache_zalloc(xfs_ifork_cache,
695                                 GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL);
696         ip->i_cowfp->if_format = XFS_DINODE_FMT_EXTENTS;
697 }
698
699 /* Verify the inline contents of the data fork of an inode. */
700 int
701 xfs_ifork_verify_local_data(
702         struct xfs_inode        *ip)
703 {
704         xfs_failaddr_t          fa = NULL;
705
706         switch (VFS_I(ip)->i_mode & S_IFMT) {
707         case S_IFDIR: {
708                 struct xfs_mount        *mp = ip->i_mount;
709                 struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, XFS_DATA_FORK);
710                 struct xfs_dir2_sf_hdr  *sfp = ifp->if_data;
711
712                 fa = xfs_dir2_sf_verify(mp, sfp, ifp->if_bytes);
713                 break;
714         }
715         case S_IFLNK: {
716                 struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, XFS_DATA_FORK);
717
718                 fa = xfs_symlink_shortform_verify(ifp->if_data, ifp->if_bytes);
719                 break;
720         }
721         default:
722                 break;
723         }
724
725         if (fa) {
726                 xfs_inode_verifier_error(ip, -EFSCORRUPTED, "data fork",
727                                 ip->i_df.if_data, ip->i_df.if_bytes, fa);
728                 return -EFSCORRUPTED;
729         }
730
731         return 0;
732 }
733
734 /* Verify the inline contents of the attr fork of an inode. */
735 int
736 xfs_ifork_verify_local_attr(
737         struct xfs_inode        *ip)
738 {
739         struct xfs_ifork        *ifp = &ip->i_af;
740         xfs_failaddr_t          fa;
741
742         if (!xfs_inode_has_attr_fork(ip)) {
743                 fa = __this_address;
744         } else {
745                 struct xfs_ifork                *ifp = &ip->i_af;
746
747                 ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
748                 fa = xfs_attr_shortform_verify(ifp->if_data, ifp->if_bytes);
749         }
750         if (fa) {
751                 xfs_inode_verifier_error(ip, -EFSCORRUPTED, "attr fork",
752                                 ifp->if_data, ifp->if_bytes, fa);
753                 return -EFSCORRUPTED;
754         }
755
756         return 0;
757 }
758
759 int
760 xfs_iext_count_may_overflow(
761         struct xfs_inode        *ip,
762         int                     whichfork,
763         int                     nr_to_add)
764 {
765         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
766         uint64_t                max_exts;
767         uint64_t                nr_exts;
768
769         if (whichfork == XFS_COW_FORK)
770                 return 0;
771
772         max_exts = xfs_iext_max_nextents(xfs_inode_has_large_extent_counts(ip),
773                                 whichfork);
774
775         if (XFS_TEST_ERROR(false, ip->i_mount, XFS_ERRTAG_REDUCE_MAX_IEXTENTS))
776                 max_exts = 10;
777
778         nr_exts = ifp->if_nextents + nr_to_add;
779         if (nr_exts < ifp->if_nextents || nr_exts > max_exts)
780                 return -EFBIG;
781
782         return 0;
783 }
784
785 /*
786  * Upgrade this inode's extent counter fields to be able to handle a potential
787  * increase in the extent count by nr_to_add.  Normally this is the same
788  * quantity that caused xfs_iext_count_may_overflow() to return -EFBIG.
789  */
790 int
791 xfs_iext_count_upgrade(
792         struct xfs_trans        *tp,
793         struct xfs_inode        *ip,
794         uint                    nr_to_add)
795 {
796         ASSERT(nr_to_add <= XFS_MAX_EXTCNT_UPGRADE_NR);
797
798         if (!xfs_has_large_extent_counts(ip->i_mount) ||
799             xfs_inode_has_large_extent_counts(ip) ||
800             XFS_TEST_ERROR(false, ip->i_mount, XFS_ERRTAG_REDUCE_MAX_IEXTENTS))
801                 return -EFBIG;
802
803         ip->i_diflags2 |= XFS_DIFLAG2_NREXT64;
804         xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
805
806         return 0;
807 }