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