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_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
162 ifp->if_flags |= XFS_IFINLINE;
163}
164
165
166
167
168STATIC int
169xfs_iformat_local(
170 xfs_inode_t *ip,
171 xfs_dinode_t *dip,
172 int whichfork,
173 int size)
174{
175
176
177
178
179
180 if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
181 xfs_warn(ip->i_mount,
182 "corrupt inode %Lu (bad size %d for local fork, size = %d).",
183 (unsigned long long) ip->i_ino, size,
184 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
185 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
186 "xfs_iformat_local", dip, sizeof(*dip),
187 __this_address);
188 return -EFSCORRUPTED;
189 }
190
191 xfs_init_local_fork(ip, whichfork, XFS_DFORK_PTR(dip, whichfork), size);
192 return 0;
193}
194
195
196
197
198
199STATIC int
200xfs_iformat_extents(
201 struct xfs_inode *ip,
202 struct xfs_dinode *dip,
203 int whichfork)
204{
205 struct xfs_mount *mp = ip->i_mount;
206 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
207 int state = xfs_bmap_fork_to_state(whichfork);
208 int nex = XFS_DFORK_NEXTENTS(dip, whichfork);
209 int size = nex * sizeof(xfs_bmbt_rec_t);
210 struct xfs_iext_cursor icur;
211 struct xfs_bmbt_rec *dp;
212 struct xfs_bmbt_irec new;
213 int i;
214
215
216
217
218
219 if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, mp, whichfork))) {
220 xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
221 (unsigned long long) ip->i_ino, nex);
222 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
223 "xfs_iformat_extents(1)", dip, sizeof(*dip),
224 __this_address);
225 return -EFSCORRUPTED;
226 }
227
228 ifp->if_bytes = 0;
229 ifp->if_u1.if_root = NULL;
230 ifp->if_height = 0;
231 if (size) {
232 dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
233
234 xfs_iext_first(ifp, &icur);
235 for (i = 0; i < nex; i++, dp++) {
236 xfs_failaddr_t fa;
237
238 xfs_bmbt_disk_get_all(dp, &new);
239 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
240 if (fa) {
241 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
242 "xfs_iformat_extents(2)",
243 dp, sizeof(*dp), fa);
244 return -EFSCORRUPTED;
245 }
246
247 xfs_iext_insert(ip, &icur, &new, state);
248 trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
249 xfs_iext_next(ifp, &icur);
250 }
251 }
252 ifp->if_flags |= XFS_IFEXTENTS;
253 return 0;
254}
255
256
257
258
259
260
261
262
263
264STATIC int
265xfs_iformat_btree(
266 xfs_inode_t *ip,
267 xfs_dinode_t *dip,
268 int whichfork)
269{
270 struct xfs_mount *mp = ip->i_mount;
271 xfs_bmdr_block_t *dfp;
272 struct xfs_ifork *ifp;
273
274 int nrecs;
275 int size;
276 int level;
277
278 ifp = XFS_IFORK_PTR(ip, whichfork);
279 dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
280 size = XFS_BMAP_BROOT_SPACE(mp, dfp);
281 nrecs = be16_to_cpu(dfp->bb_numrecs);
282 level = be16_to_cpu(dfp->bb_level);
283
284
285
286
287
288
289
290
291 if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <=
292 XFS_IFORK_MAXEXT(ip, whichfork) ||
293 nrecs == 0 ||
294 XFS_BMDR_SPACE_CALC(nrecs) >
295 XFS_DFORK_SIZE(dip, mp, whichfork) ||
296 XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks) ||
297 level == 0 || level > XFS_BTREE_MAXLEVELS) {
298 xfs_warn(mp, "corrupt inode %Lu (btree).",
299 (unsigned long long) ip->i_ino);
300 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
301 "xfs_iformat_btree", dfp, size,
302 __this_address);
303 return -EFSCORRUPTED;
304 }
305
306 ifp->if_broot_bytes = size;
307 ifp->if_broot = kmem_alloc(size, KM_SLEEP | KM_NOFS);
308 ASSERT(ifp->if_broot != NULL);
309
310
311
312
313 xfs_bmdr_to_bmbt(ip, dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
314 ifp->if_broot, size);
315 ifp->if_flags &= ~XFS_IFEXTENTS;
316 ifp->if_flags |= XFS_IFBROOT;
317
318 ifp->if_bytes = 0;
319 ifp->if_u1.if_root = NULL;
320 ifp->if_height = 0;
321 return 0;
322}
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342void
343xfs_iroot_realloc(
344 xfs_inode_t *ip,
345 int rec_diff,
346 int whichfork)
347{
348 struct xfs_mount *mp = ip->i_mount;
349 int cur_max;
350 struct xfs_ifork *ifp;
351 struct xfs_btree_block *new_broot;
352 int new_max;
353 size_t new_size;
354 char *np;
355 char *op;
356
357
358
359
360 if (rec_diff == 0) {
361 return;
362 }
363
364 ifp = XFS_IFORK_PTR(ip, whichfork);
365 if (rec_diff > 0) {
366
367
368
369
370 if (ifp->if_broot_bytes == 0) {
371 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
372 ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
373 ifp->if_broot_bytes = (int)new_size;
374 return;
375 }
376
377
378
379
380
381
382
383 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
384 new_max = cur_max + rec_diff;
385 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
386 ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
387 KM_SLEEP | KM_NOFS);
388 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
389 ifp->if_broot_bytes);
390 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
391 (int)new_size);
392 ifp->if_broot_bytes = (int)new_size;
393 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
394 XFS_IFORK_SIZE(ip, whichfork));
395 memmove(np, op, cur_max * (uint)sizeof(xfs_fsblock_t));
396 return;
397 }
398
399
400
401
402
403
404 ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
405 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
406 new_max = cur_max + rec_diff;
407 ASSERT(new_max >= 0);
408 if (new_max > 0)
409 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
410 else
411 new_size = 0;
412 if (new_size > 0) {
413 new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
414
415
416
417 memcpy(new_broot, ifp->if_broot,
418 XFS_BMBT_BLOCK_LEN(ip->i_mount));
419 } else {
420 new_broot = NULL;
421 ifp->if_flags &= ~XFS_IFBROOT;
422 }
423
424
425
426
427 if (new_max > 0) {
428
429
430
431 op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
432 np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
433 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
434
435
436
437
438 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
439 ifp->if_broot_bytes);
440 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
441 (int)new_size);
442 memcpy(np, op, new_max * (uint)sizeof(xfs_fsblock_t));
443 }
444 kmem_free(ifp->if_broot);
445 ifp->if_broot = new_broot;
446 ifp->if_broot_bytes = (int)new_size;
447 if (ifp->if_broot)
448 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
449 XFS_IFORK_SIZE(ip, whichfork));
450 return;
451}
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469void
470xfs_idata_realloc(
471 struct xfs_inode *ip,
472 int byte_diff,
473 int whichfork)
474{
475 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
476 int new_size = (int)ifp->if_bytes + byte_diff;
477
478 ASSERT(new_size >= 0);
479 ASSERT(new_size <= XFS_IFORK_SIZE(ip, whichfork));
480
481 if (byte_diff == 0)
482 return;
483
484 if (new_size == 0) {
485 kmem_free(ifp->if_u1.if_data);
486 ifp->if_u1.if_data = NULL;
487 ifp->if_bytes = 0;
488 return;
489 }
490
491
492
493
494
495
496 ifp->if_u1.if_data = kmem_realloc(ifp->if_u1.if_data,
497 roundup(new_size, 4), KM_SLEEP | KM_NOFS);
498 ifp->if_bytes = new_size;
499}
500
501void
502xfs_idestroy_fork(
503 xfs_inode_t *ip,
504 int whichfork)
505{
506 struct xfs_ifork *ifp;
507
508 ifp = XFS_IFORK_PTR(ip, whichfork);
509 if (ifp->if_broot != NULL) {
510 kmem_free(ifp->if_broot);
511 ifp->if_broot = NULL;
512 }
513
514
515
516
517
518
519
520 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
521 if (ifp->if_u1.if_data != NULL) {
522 kmem_free(ifp->if_u1.if_data);
523 ifp->if_u1.if_data = NULL;
524 }
525 } else if ((ifp->if_flags & XFS_IFEXTENTS) && ifp->if_height) {
526 xfs_iext_destroy(ifp);
527 }
528
529 if (whichfork == XFS_ATTR_FORK) {
530 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
531 ip->i_afp = NULL;
532 } else if (whichfork == XFS_COW_FORK) {
533 kmem_zone_free(xfs_ifork_zone, ip->i_cowfp);
534 ip->i_cowfp = NULL;
535 }
536}
537
538
539
540
541
542
543
544
545
546
547int
548xfs_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 int 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
579
580
581
582
583
584
585
586
587void
588xfs_iflush_fork(
589 xfs_inode_t *ip,
590 xfs_dinode_t *dip,
591 xfs_inode_log_item_t *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
609
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 (XFS_IFORK_FORMAT(ip, whichfork)) {
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(XFS_IFORK_NEXTENTS(ip, whichfork) > 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
665struct xfs_ifork *
666xfs_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
679
680void
681xfs_ifork_init_cow(
682 struct xfs_inode *ip)
683{
684 if (ip->i_cowfp)
685 return;
686
687 ip->i_cowfp = kmem_zone_zalloc(xfs_ifork_zone,
688 KM_SLEEP | KM_NOFS);
689 ip->i_cowfp->if_flags = XFS_IFEXTENTS;
690 ip->i_cformat = XFS_DINODE_FMT_EXTENTS;
691 ip->i_cnextents = 0;
692}
693
694
695struct xfs_ifork_ops xfs_default_ifork_ops = {
696 .verify_attr = xfs_attr_shortform_verify,
697 .verify_dir = xfs_dir2_sf_verify,
698 .verify_symlink = xfs_symlink_shortform_verify,
699};
700
701
702xfs_failaddr_t
703xfs_ifork_verify_data(
704 struct xfs_inode *ip,
705 struct xfs_ifork_ops *ops)
706{
707
708 if (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
709 return NULL;
710
711
712 switch (VFS_I(ip)->i_mode & S_IFMT) {
713 case S_IFDIR:
714 return ops->verify_dir(ip);
715 case S_IFLNK:
716 return ops->verify_symlink(ip);
717 default:
718 return NULL;
719 }
720}
721
722
723xfs_failaddr_t
724xfs_ifork_verify_attr(
725 struct xfs_inode *ip,
726 struct xfs_ifork_ops *ops)
727{
728
729 if (ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
730 return NULL;
731 if (!XFS_IFORK_PTR(ip, XFS_ATTR_FORK))
732 return __this_address;
733 return ops->verify_attr(ip);
734}
735