1
2
3
4
5
6#include <linux/log2.h>
7
8#include "xfs.h"
9#include "xfs_fs.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_attr_sf.h"
23#include "xfs_da_format.h"
24#include "xfs_da_btree.h"
25#include "xfs_dir2_priv.h"
26#include "xfs_attr_leaf.h"
27#include "xfs_shared.h"
28
29kmem_zone_t *xfs_ifork_zone;
30
31STATIC int xfs_iformat_local(xfs_inode_t *, xfs_dinode_t *, int, int);
32STATIC int xfs_iformat_extents(xfs_inode_t *, xfs_dinode_t *, int);
33STATIC int xfs_iformat_btree(xfs_inode_t *, xfs_dinode_t *, int);
34
35
36
37
38
39
40
41
42
43
44int
45xfs_iformat_fork(
46 struct xfs_inode *ip,
47 struct xfs_dinode *dip)
48{
49 struct inode *inode = VFS_I(ip);
50 struct xfs_attr_shortform *atp;
51 int size;
52 int error = 0;
53 xfs_fsize_t di_size;
54
55 switch (inode->i_mode & S_IFMT) {
56 case S_IFIFO:
57 case S_IFCHR:
58 case S_IFBLK:
59 case S_IFSOCK:
60 ip->i_d.di_size = 0;
61 inode->i_rdev = xfs_to_linux_dev_t(xfs_dinode_get_rdev(dip));
62 break;
63
64 case S_IFREG:
65 case S_IFLNK:
66 case S_IFDIR:
67 switch (dip->di_format) {
68 case XFS_DINODE_FMT_LOCAL:
69 di_size = be64_to_cpu(dip->di_size);
70 size = (int)di_size;
71 error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
72 break;
73 case XFS_DINODE_FMT_EXTENTS:
74 error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
75 break;
76 case XFS_DINODE_FMT_BTREE:
77 error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
78 break;
79 default:
80 return -EFSCORRUPTED;
81 }
82 break;
83
84 default:
85 return -EFSCORRUPTED;
86 }
87 if (error)
88 return error;
89
90 if (xfs_is_reflink_inode(ip)) {
91 ASSERT(ip->i_cowfp == NULL);
92 xfs_ifork_init_cow(ip);
93 }
94
95 if (!XFS_DFORK_Q(dip))
96 return 0;
97
98 ASSERT(ip->i_afp == NULL);
99 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP | KM_NOFS);
100
101 switch (dip->di_aformat) {
102 case XFS_DINODE_FMT_LOCAL:
103 atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
104 size = be16_to_cpu(atp->hdr.totsize);
105
106 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
107 break;
108 case XFS_DINODE_FMT_EXTENTS:
109 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
110 break;
111 case XFS_DINODE_FMT_BTREE:
112 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
113 break;
114 default:
115 error = -EFSCORRUPTED;
116 break;
117 }
118 if (error) {
119 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
120 ip->i_afp = NULL;
121 if (ip->i_cowfp)
122 kmem_zone_free(xfs_ifork_zone, ip->i_cowfp);
123 ip->i_cowfp = NULL;
124 xfs_idestroy_fork(ip, XFS_DATA_FORK);
125 }
126 return error;
127}
128
129void
130xfs_init_local_fork(
131 struct xfs_inode *ip,
132 int whichfork,
133 const void *data,
134 int size)
135{
136 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
137 int mem_size = size, real_size = 0;
138 bool zero_terminate;
139
140
141
142
143
144
145
146 zero_terminate = S_ISLNK(VFS_I(ip)->i_mode);
147 if (zero_terminate)
148 mem_size++;
149
150 if (size) {
151 real_size = roundup(mem_size, 4);
152 ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP | KM_NOFS);
153 memcpy(ifp->if_u1.if_data, data, size);
154 if (zero_terminate)
155 ifp->if_u1.if_data[size] = '\0';
156 } else {
157 ifp->if_u1.if_data = NULL;
158 }
159
160 ifp->if_bytes = size;
161 ifp->if_real_bytes = real_size;
162 ifp->if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
163 ifp->if_flags |= XFS_IFINLINE;
164}
165
166
167
168
169STATIC int
170xfs_iformat_local(
171 xfs_inode_t *ip,
172 xfs_dinode_t *dip,
173 int whichfork,
174 int size)
175{
176
177
178
179
180
181 if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
182 xfs_warn(ip->i_mount,
183 "corrupt inode %Lu (bad size %d for local fork, size = %d).",
184 (unsigned long long) ip->i_ino, size,
185 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
186 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
187 "xfs_iformat_local", dip, sizeof(*dip),
188 __this_address);
189 return -EFSCORRUPTED;
190 }
191
192 xfs_init_local_fork(ip, whichfork, XFS_DFORK_PTR(dip, whichfork), size);
193 return 0;
194}
195
196
197
198
199
200STATIC int
201xfs_iformat_extents(
202 struct xfs_inode *ip,
203 struct xfs_dinode *dip,
204 int whichfork)
205{
206 struct xfs_mount *mp = ip->i_mount;
207 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
208 int state = xfs_bmap_fork_to_state(whichfork);
209 int nex = XFS_DFORK_NEXTENTS(dip, whichfork);
210 int size = nex * sizeof(xfs_bmbt_rec_t);
211 struct xfs_iext_cursor icur;
212 struct xfs_bmbt_rec *dp;
213 struct xfs_bmbt_irec new;
214 int i;
215
216
217
218
219
220 if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, mp, whichfork))) {
221 xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
222 (unsigned long long) ip->i_ino, nex);
223 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
224 "xfs_iformat_extents(1)", dip, sizeof(*dip),
225 __this_address);
226 return -EFSCORRUPTED;
227 }
228
229 ifp->if_real_bytes = 0;
230 ifp->if_bytes = 0;
231 ifp->if_u1.if_root = NULL;
232 ifp->if_height = 0;
233 if (size) {
234 dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
235
236 xfs_iext_first(ifp, &icur);
237 for (i = 0; i < nex; i++, dp++) {
238 xfs_failaddr_t fa;
239
240 xfs_bmbt_disk_get_all(dp, &new);
241 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
242 if (fa) {
243 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
244 "xfs_iformat_extents(2)",
245 dp, sizeof(*dp), fa);
246 return -EFSCORRUPTED;
247 }
248
249 xfs_iext_insert(ip, &icur, &new, state);
250 trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
251 xfs_iext_next(ifp, &icur);
252 }
253 }
254 ifp->if_flags |= XFS_IFEXTENTS;
255 return 0;
256}
257
258
259
260
261
262
263
264
265
266STATIC int
267xfs_iformat_btree(
268 xfs_inode_t *ip,
269 xfs_dinode_t *dip,
270 int whichfork)
271{
272 struct xfs_mount *mp = ip->i_mount;
273 xfs_bmdr_block_t *dfp;
274 xfs_ifork_t *ifp;
275
276 int nrecs;
277 int size;
278 int level;
279
280 ifp = XFS_IFORK_PTR(ip, whichfork);
281 dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
282 size = XFS_BMAP_BROOT_SPACE(mp, dfp);
283 nrecs = be16_to_cpu(dfp->bb_numrecs);
284 level = be16_to_cpu(dfp->bb_level);
285
286
287
288
289
290
291
292
293 if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <=
294 XFS_IFORK_MAXEXT(ip, whichfork) ||
295 nrecs == 0 ||
296 XFS_BMDR_SPACE_CALC(nrecs) >
297 XFS_DFORK_SIZE(dip, mp, whichfork) ||
298 XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks) ||
299 level == 0 || level > XFS_BTREE_MAXLEVELS) {
300 xfs_warn(mp, "corrupt inode %Lu (btree).",
301 (unsigned long long) ip->i_ino);
302 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
303 "xfs_iformat_btree", dfp, size,
304 __this_address);
305 return -EFSCORRUPTED;
306 }
307
308 ifp->if_broot_bytes = size;
309 ifp->if_broot = kmem_alloc(size, KM_SLEEP | KM_NOFS);
310 ASSERT(ifp->if_broot != NULL);
311
312
313
314
315 xfs_bmdr_to_bmbt(ip, dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
316 ifp->if_broot, size);
317 ifp->if_flags &= ~XFS_IFEXTENTS;
318 ifp->if_flags |= XFS_IFBROOT;
319
320 ifp->if_real_bytes = 0;
321 ifp->if_bytes = 0;
322 ifp->if_u1.if_root = NULL;
323 ifp->if_height = 0;
324 return 0;
325}
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345void
346xfs_iroot_realloc(
347 xfs_inode_t *ip,
348 int rec_diff,
349 int whichfork)
350{
351 struct xfs_mount *mp = ip->i_mount;
352 int cur_max;
353 xfs_ifork_t *ifp;
354 struct xfs_btree_block *new_broot;
355 int new_max;
356 size_t new_size;
357 char *np;
358 char *op;
359
360
361
362
363 if (rec_diff == 0) {
364 return;
365 }
366
367 ifp = XFS_IFORK_PTR(ip, whichfork);
368 if (rec_diff > 0) {
369
370
371
372
373 if (ifp->if_broot_bytes == 0) {
374 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
375 ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
376 ifp->if_broot_bytes = (int)new_size;
377 return;
378 }
379
380
381
382
383
384
385
386 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
387 new_max = cur_max + rec_diff;
388 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
389 ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
390 KM_SLEEP | KM_NOFS);
391 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
392 ifp->if_broot_bytes);
393 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
394 (int)new_size);
395 ifp->if_broot_bytes = (int)new_size;
396 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
397 XFS_IFORK_SIZE(ip, whichfork));
398 memmove(np, op, cur_max * (uint)sizeof(xfs_fsblock_t));
399 return;
400 }
401
402
403
404
405
406
407 ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
408 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
409 new_max = cur_max + rec_diff;
410 ASSERT(new_max >= 0);
411 if (new_max > 0)
412 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
413 else
414 new_size = 0;
415 if (new_size > 0) {
416 new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
417
418
419
420 memcpy(new_broot, ifp->if_broot,
421 XFS_BMBT_BLOCK_LEN(ip->i_mount));
422 } else {
423 new_broot = NULL;
424 ifp->if_flags &= ~XFS_IFBROOT;
425 }
426
427
428
429
430 if (new_max > 0) {
431
432
433
434 op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
435 np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
436 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
437
438
439
440
441 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
442 ifp->if_broot_bytes);
443 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
444 (int)new_size);
445 memcpy(np, op, new_max * (uint)sizeof(xfs_fsblock_t));
446 }
447 kmem_free(ifp->if_broot);
448 ifp->if_broot = new_broot;
449 ifp->if_broot_bytes = (int)new_size;
450 if (ifp->if_broot)
451 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
452 XFS_IFORK_SIZE(ip, whichfork));
453 return;
454}
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472void
473xfs_idata_realloc(
474 xfs_inode_t *ip,
475 int byte_diff,
476 int whichfork)
477{
478 xfs_ifork_t *ifp;
479 int new_size;
480 int real_size;
481
482 if (byte_diff == 0) {
483 return;
484 }
485
486 ifp = XFS_IFORK_PTR(ip, whichfork);
487 new_size = (int)ifp->if_bytes + byte_diff;
488 ASSERT(new_size >= 0);
489
490 if (new_size == 0) {
491 kmem_free(ifp->if_u1.if_data);
492 ifp->if_u1.if_data = NULL;
493 real_size = 0;
494 } else {
495
496
497
498
499
500
501
502 real_size = roundup(new_size, 4);
503 if (ifp->if_u1.if_data == NULL) {
504 ASSERT(ifp->if_real_bytes == 0);
505 ifp->if_u1.if_data = kmem_alloc(real_size,
506 KM_SLEEP | KM_NOFS);
507 } else {
508
509
510
511
512 if (ifp->if_real_bytes != real_size) {
513 ifp->if_u1.if_data =
514 kmem_realloc(ifp->if_u1.if_data,
515 real_size,
516 KM_SLEEP | KM_NOFS);
517 }
518 }
519 }
520 ifp->if_real_bytes = real_size;
521 ifp->if_bytes = new_size;
522 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
523}
524
525void
526xfs_idestroy_fork(
527 xfs_inode_t *ip,
528 int whichfork)
529{
530 xfs_ifork_t *ifp;
531
532 ifp = XFS_IFORK_PTR(ip, whichfork);
533 if (ifp->if_broot != NULL) {
534 kmem_free(ifp->if_broot);
535 ifp->if_broot = NULL;
536 }
537
538
539
540
541
542
543
544 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
545 if (ifp->if_u1.if_data != NULL) {
546 ASSERT(ifp->if_real_bytes != 0);
547 kmem_free(ifp->if_u1.if_data);
548 ifp->if_u1.if_data = NULL;
549 ifp->if_real_bytes = 0;
550 }
551 } else if ((ifp->if_flags & XFS_IFEXTENTS) && ifp->if_height) {
552 xfs_iext_destroy(ifp);
553 }
554
555 ASSERT(ifp->if_real_bytes == 0);
556
557 if (whichfork == XFS_ATTR_FORK) {
558 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
559 ip->i_afp = NULL;
560 } else if (whichfork == XFS_COW_FORK) {
561 kmem_zone_free(xfs_ifork_zone, ip->i_cowfp);
562 ip->i_cowfp = NULL;
563 }
564}
565
566
567
568
569
570
571
572
573
574
575int
576xfs_iextents_copy(
577 struct xfs_inode *ip,
578 struct xfs_bmbt_rec *dp,
579 int whichfork)
580{
581 int state = xfs_bmap_fork_to_state(whichfork);
582 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
583 struct xfs_iext_cursor icur;
584 struct xfs_bmbt_irec rec;
585 int copied = 0;
586
587 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED));
588 ASSERT(ifp->if_bytes > 0);
589
590 for_each_xfs_iext(ifp, &icur, &rec) {
591 if (isnullstartblock(rec.br_startblock))
592 continue;
593 ASSERT(xfs_bmap_validate_extent(ip, whichfork, &rec) == NULL);
594 xfs_bmbt_disk_set_all(dp, &rec);
595 trace_xfs_write_extent(ip, &icur, state, _RET_IP_);
596 copied += sizeof(struct xfs_bmbt_rec);
597 dp++;
598 }
599
600 ASSERT(copied > 0);
601 ASSERT(copied <= ifp->if_bytes);
602 return copied;
603}
604
605
606
607
608
609
610
611
612
613
614
615void
616xfs_iflush_fork(
617 xfs_inode_t *ip,
618 xfs_dinode_t *dip,
619 xfs_inode_log_item_t *iip,
620 int whichfork)
621{
622 char *cp;
623 xfs_ifork_t *ifp;
624 xfs_mount_t *mp;
625 static const short brootflag[2] =
626 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
627 static const short dataflag[2] =
628 { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
629 static const short extflag[2] =
630 { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
631
632 if (!iip)
633 return;
634 ifp = XFS_IFORK_PTR(ip, whichfork);
635
636
637
638
639 if (!ifp) {
640 ASSERT(whichfork == XFS_ATTR_FORK);
641 return;
642 }
643 cp = XFS_DFORK_PTR(dip, whichfork);
644 mp = ip->i_mount;
645 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
646 case XFS_DINODE_FMT_LOCAL:
647 if ((iip->ili_fields & dataflag[whichfork]) &&
648 (ifp->if_bytes > 0)) {
649 ASSERT(ifp->if_u1.if_data != NULL);
650 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
651 memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
652 }
653 break;
654
655 case XFS_DINODE_FMT_EXTENTS:
656 ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
657 !(iip->ili_fields & extflag[whichfork]));
658 if ((iip->ili_fields & extflag[whichfork]) &&
659 (ifp->if_bytes > 0)) {
660 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
661 (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
662 whichfork);
663 }
664 break;
665
666 case XFS_DINODE_FMT_BTREE:
667 if ((iip->ili_fields & brootflag[whichfork]) &&
668 (ifp->if_broot_bytes > 0)) {
669 ASSERT(ifp->if_broot != NULL);
670 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
671 XFS_IFORK_SIZE(ip, whichfork));
672 xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
673 (xfs_bmdr_block_t *)cp,
674 XFS_DFORK_SIZE(dip, mp, whichfork));
675 }
676 break;
677
678 case XFS_DINODE_FMT_DEV:
679 if (iip->ili_fields & XFS_ILOG_DEV) {
680 ASSERT(whichfork == XFS_DATA_FORK);
681 xfs_dinode_put_rdev(dip,
682 linux_to_xfs_dev_t(VFS_I(ip)->i_rdev));
683 }
684 break;
685
686 default:
687 ASSERT(0);
688 break;
689 }
690}
691
692
693struct xfs_ifork *
694xfs_iext_state_to_fork(
695 struct xfs_inode *ip,
696 int state)
697{
698 if (state & BMAP_COWFORK)
699 return ip->i_cowfp;
700 else if (state & BMAP_ATTRFORK)
701 return ip->i_afp;
702 return &ip->i_df;
703}
704
705
706
707
708void
709xfs_ifork_init_cow(
710 struct xfs_inode *ip)
711{
712 if (ip->i_cowfp)
713 return;
714
715 ip->i_cowfp = kmem_zone_zalloc(xfs_ifork_zone,
716 KM_SLEEP | KM_NOFS);
717 ip->i_cowfp->if_flags = XFS_IFEXTENTS;
718 ip->i_cformat = XFS_DINODE_FMT_EXTENTS;
719 ip->i_cnextents = 0;
720}
721
722
723struct xfs_ifork_ops xfs_default_ifork_ops = {
724 .verify_attr = xfs_attr_shortform_verify,
725 .verify_dir = xfs_dir2_sf_verify,
726 .verify_symlink = xfs_symlink_shortform_verify,
727};
728
729
730xfs_failaddr_t
731xfs_ifork_verify_data(
732 struct xfs_inode *ip,
733 struct xfs_ifork_ops *ops)
734{
735
736 if (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
737 return NULL;
738
739
740 switch (VFS_I(ip)->i_mode & S_IFMT) {
741 case S_IFDIR:
742 return ops->verify_dir(ip);
743 case S_IFLNK:
744 return ops->verify_symlink(ip);
745 default:
746 return NULL;
747 }
748}
749
750
751xfs_failaddr_t
752xfs_ifork_verify_attr(
753 struct xfs_inode *ip,
754 struct xfs_ifork_ops *ops)
755{
756
757 if (ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
758 return NULL;
759 if (!XFS_IFORK_PTR(ip, XFS_ATTR_FORK))
760 return __this_address;
761 return ops->verify_attr(ip);
762}
763