1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <linux/log2.h>
19
20#include "xfs.h"
21#include "xfs_fs.h"
22#include "xfs_format.h"
23#include "xfs_log_format.h"
24#include "xfs_trans_resv.h"
25#include "xfs_mount.h"
26#include "xfs_inode.h"
27#include "xfs_trans.h"
28#include "xfs_inode_item.h"
29#include "xfs_btree.h"
30#include "xfs_bmap_btree.h"
31#include "xfs_bmap.h"
32#include "xfs_error.h"
33#include "xfs_trace.h"
34#include "xfs_attr_sf.h"
35#include "xfs_da_format.h"
36#include "xfs_da_btree.h"
37#include "xfs_dir2_priv.h"
38
39kmem_zone_t *xfs_ifork_zone;
40
41STATIC int xfs_iformat_local(xfs_inode_t *, xfs_dinode_t *, int, int);
42STATIC int xfs_iformat_extents(xfs_inode_t *, xfs_dinode_t *, int);
43STATIC int xfs_iformat_btree(xfs_inode_t *, xfs_dinode_t *, int);
44
45
46
47
48
49
50
51
52
53
54int
55xfs_iformat_fork(
56 xfs_inode_t *ip,
57 xfs_dinode_t *dip)
58{
59 xfs_attr_shortform_t *atp;
60 int size;
61 int error = 0;
62 xfs_fsize_t di_size;
63
64 if (unlikely(be32_to_cpu(dip->di_nextents) +
65 be16_to_cpu(dip->di_anextents) >
66 be64_to_cpu(dip->di_nblocks))) {
67 xfs_warn(ip->i_mount,
68 "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.",
69 (unsigned long long)ip->i_ino,
70 (int)(be32_to_cpu(dip->di_nextents) +
71 be16_to_cpu(dip->di_anextents)),
72 (unsigned long long)
73 be64_to_cpu(dip->di_nblocks));
74 XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW,
75 ip->i_mount, dip);
76 return -EFSCORRUPTED;
77 }
78
79 if (unlikely(dip->di_forkoff > ip->i_mount->m_sb.sb_inodesize)) {
80 xfs_warn(ip->i_mount, "corrupt dinode %Lu, forkoff = 0x%x.",
81 (unsigned long long)ip->i_ino,
82 dip->di_forkoff);
83 XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW,
84 ip->i_mount, dip);
85 return -EFSCORRUPTED;
86 }
87
88 if (unlikely((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) &&
89 !ip->i_mount->m_rtdev_targp)) {
90 xfs_warn(ip->i_mount,
91 "corrupt dinode %Lu, has realtime flag set.",
92 ip->i_ino);
93 XFS_CORRUPTION_ERROR("xfs_iformat(realtime)",
94 XFS_ERRLEVEL_LOW, ip->i_mount, dip);
95 return -EFSCORRUPTED;
96 }
97
98 if (unlikely(xfs_is_reflink_inode(ip) &&
99 (VFS_I(ip)->i_mode & S_IFMT) != S_IFREG)) {
100 xfs_warn(ip->i_mount,
101 "corrupt dinode %llu, wrong file type for reflink.",
102 ip->i_ino);
103 XFS_CORRUPTION_ERROR("xfs_iformat(reflink)",
104 XFS_ERRLEVEL_LOW, ip->i_mount, dip);
105 return -EFSCORRUPTED;
106 }
107
108 if (unlikely(xfs_is_reflink_inode(ip) &&
109 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME))) {
110 xfs_warn(ip->i_mount,
111 "corrupt dinode %llu, has reflink+realtime flag set.",
112 ip->i_ino);
113 XFS_CORRUPTION_ERROR("xfs_iformat(reflink)",
114 XFS_ERRLEVEL_LOW, ip->i_mount, dip);
115 return -EFSCORRUPTED;
116 }
117
118 switch (VFS_I(ip)->i_mode & S_IFMT) {
119 case S_IFIFO:
120 case S_IFCHR:
121 case S_IFBLK:
122 case S_IFSOCK:
123 if (unlikely(dip->di_format != XFS_DINODE_FMT_DEV)) {
124 XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW,
125 ip->i_mount, dip);
126 return -EFSCORRUPTED;
127 }
128 ip->i_d.di_size = 0;
129 ip->i_df.if_u2.if_rdev = xfs_dinode_get_rdev(dip);
130 break;
131
132 case S_IFREG:
133 case S_IFLNK:
134 case S_IFDIR:
135 switch (dip->di_format) {
136 case XFS_DINODE_FMT_LOCAL:
137
138
139
140 if (unlikely(S_ISREG(be16_to_cpu(dip->di_mode)))) {
141 xfs_warn(ip->i_mount,
142 "corrupt inode %Lu (local format for regular file).",
143 (unsigned long long) ip->i_ino);
144 XFS_CORRUPTION_ERROR("xfs_iformat(4)",
145 XFS_ERRLEVEL_LOW,
146 ip->i_mount, dip);
147 return -EFSCORRUPTED;
148 }
149
150 di_size = be64_to_cpu(dip->di_size);
151 if (unlikely(di_size < 0 ||
152 di_size > XFS_DFORK_DSIZE(dip, ip->i_mount))) {
153 xfs_warn(ip->i_mount,
154 "corrupt inode %Lu (bad size %Ld for local inode).",
155 (unsigned long long) ip->i_ino,
156 (long long) di_size);
157 XFS_CORRUPTION_ERROR("xfs_iformat(5)",
158 XFS_ERRLEVEL_LOW,
159 ip->i_mount, dip);
160 return -EFSCORRUPTED;
161 }
162
163 size = (int)di_size;
164 error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
165 break;
166 case XFS_DINODE_FMT_EXTENTS:
167 error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
168 break;
169 case XFS_DINODE_FMT_BTREE:
170 error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
171 break;
172 default:
173 XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW,
174 ip->i_mount);
175 return -EFSCORRUPTED;
176 }
177 break;
178
179 default:
180 XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW, ip->i_mount);
181 return -EFSCORRUPTED;
182 }
183 if (error)
184 return error;
185
186
187 if (S_ISDIR(VFS_I(ip)->i_mode) &&
188 dip->di_format == XFS_DINODE_FMT_LOCAL) {
189 error = xfs_dir2_sf_verify(ip);
190 if (error) {
191 xfs_idestroy_fork(ip, XFS_DATA_FORK);
192 return error;
193 }
194 }
195
196 if (xfs_is_reflink_inode(ip)) {
197 ASSERT(ip->i_cowfp == NULL);
198 xfs_ifork_init_cow(ip);
199 }
200
201 if (!XFS_DFORK_Q(dip))
202 return 0;
203
204 ASSERT(ip->i_afp == NULL);
205 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP | KM_NOFS);
206
207 switch (dip->di_aformat) {
208 case XFS_DINODE_FMT_LOCAL:
209 atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
210 size = be16_to_cpu(atp->hdr.totsize);
211
212 if (unlikely(size < sizeof(struct xfs_attr_sf_hdr))) {
213 xfs_warn(ip->i_mount,
214 "corrupt inode %Lu (bad attr fork size %Ld).",
215 (unsigned long long) ip->i_ino,
216 (long long) size);
217 XFS_CORRUPTION_ERROR("xfs_iformat(8)",
218 XFS_ERRLEVEL_LOW,
219 ip->i_mount, dip);
220 error = -EFSCORRUPTED;
221 break;
222 }
223
224 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
225 break;
226 case XFS_DINODE_FMT_EXTENTS:
227 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
228 break;
229 case XFS_DINODE_FMT_BTREE:
230 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
231 break;
232 default:
233 error = -EFSCORRUPTED;
234 break;
235 }
236 if (error) {
237 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
238 ip->i_afp = NULL;
239 if (ip->i_cowfp)
240 kmem_zone_free(xfs_ifork_zone, ip->i_cowfp);
241 ip->i_cowfp = NULL;
242 xfs_idestroy_fork(ip, XFS_DATA_FORK);
243 }
244 return error;
245}
246
247void
248xfs_init_local_fork(
249 struct xfs_inode *ip,
250 int whichfork,
251 const void *data,
252 int size)
253{
254 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
255 int mem_size = size, real_size = 0;
256 bool zero_terminate;
257
258
259
260
261
262
263
264 zero_terminate = S_ISLNK(VFS_I(ip)->i_mode);
265 if (zero_terminate)
266 mem_size++;
267
268 if (size == 0)
269 ifp->if_u1.if_data = NULL;
270 else if (mem_size <= sizeof(ifp->if_u2.if_inline_data))
271 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
272 else {
273 real_size = roundup(mem_size, 4);
274 ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP | KM_NOFS);
275 }
276
277 if (size) {
278 memcpy(ifp->if_u1.if_data, data, size);
279 if (zero_terminate)
280 ifp->if_u1.if_data[size] = '\0';
281 }
282
283 ifp->if_bytes = size;
284 ifp->if_real_bytes = real_size;
285 ifp->if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
286 ifp->if_flags |= XFS_IFINLINE;
287}
288
289
290
291
292
293
294
295
296
297
298
299STATIC int
300xfs_iformat_local(
301 xfs_inode_t *ip,
302 xfs_dinode_t *dip,
303 int whichfork,
304 int size)
305{
306
307
308
309
310
311 if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
312 xfs_warn(ip->i_mount,
313 "corrupt inode %Lu (bad size %d for local fork, size = %d).",
314 (unsigned long long) ip->i_ino, size,
315 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
316 XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW,
317 ip->i_mount, dip);
318 return -EFSCORRUPTED;
319 }
320
321 xfs_init_local_fork(ip, whichfork, XFS_DFORK_PTR(dip, whichfork), size);
322 return 0;
323}
324
325
326
327
328
329
330
331STATIC int
332xfs_iformat_extents(
333 struct xfs_inode *ip,
334 struct xfs_dinode *dip,
335 int whichfork)
336{
337 struct xfs_mount *mp = ip->i_mount;
338 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
339 int nex = XFS_DFORK_NEXTENTS(dip, whichfork);
340 int size = nex * sizeof(xfs_bmbt_rec_t);
341 struct xfs_bmbt_rec *dp;
342 int i;
343
344
345
346
347
348 if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, mp, whichfork))) {
349 xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
350 (unsigned long long) ip->i_ino, nex);
351 XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW,
352 mp, dip);
353 return -EFSCORRUPTED;
354 }
355
356 ifp->if_real_bytes = 0;
357 if (nex == 0)
358 ifp->if_u1.if_extents = NULL;
359 else if (nex <= XFS_INLINE_EXTS)
360 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
361 else
362 xfs_iext_add(ifp, 0, nex);
363
364 ifp->if_bytes = size;
365 if (size) {
366 dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
367 for (i = 0; i < nex; i++, dp++) {
368 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
369 ep->l0 = get_unaligned_be64(&dp->l0);
370 ep->l1 = get_unaligned_be64(&dp->l1);
371 if (!xfs_bmbt_validate_extent(mp, whichfork, ep)) {
372 XFS_ERROR_REPORT("xfs_iformat_extents(2)",
373 XFS_ERRLEVEL_LOW, mp);
374 return -EFSCORRUPTED;
375 }
376 }
377 XFS_BMAP_TRACE_EXLIST(ip, nex, whichfork);
378 }
379 ifp->if_flags |= XFS_IFEXTENTS;
380 return 0;
381}
382
383
384
385
386
387
388
389
390
391STATIC int
392xfs_iformat_btree(
393 xfs_inode_t *ip,
394 xfs_dinode_t *dip,
395 int whichfork)
396{
397 struct xfs_mount *mp = ip->i_mount;
398 xfs_bmdr_block_t *dfp;
399 xfs_ifork_t *ifp;
400
401 int nrecs;
402 int size;
403 int level;
404
405 ifp = XFS_IFORK_PTR(ip, whichfork);
406 dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
407 size = XFS_BMAP_BROOT_SPACE(mp, dfp);
408 nrecs = be16_to_cpu(dfp->bb_numrecs);
409 level = be16_to_cpu(dfp->bb_level);
410
411
412
413
414
415
416
417
418 if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <=
419 XFS_IFORK_MAXEXT(ip, whichfork) ||
420 XFS_BMDR_SPACE_CALC(nrecs) >
421 XFS_DFORK_SIZE(dip, mp, whichfork) ||
422 XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks) ||
423 level == 0 || level > XFS_BTREE_MAXLEVELS) {
424 xfs_warn(mp, "corrupt inode %Lu (btree).",
425 (unsigned long long) ip->i_ino);
426 XFS_CORRUPTION_ERROR("xfs_iformat_btree", XFS_ERRLEVEL_LOW,
427 mp, dip);
428 return -EFSCORRUPTED;
429 }
430
431 ifp->if_broot_bytes = size;
432 ifp->if_broot = kmem_alloc(size, KM_SLEEP | KM_NOFS);
433 ASSERT(ifp->if_broot != NULL);
434
435
436
437
438 xfs_bmdr_to_bmbt(ip, dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
439 ifp->if_broot, size);
440 ifp->if_flags &= ~XFS_IFEXTENTS;
441 ifp->if_flags |= XFS_IFBROOT;
442
443 return 0;
444}
445
446
447
448
449
450int
451xfs_iread_extents(
452 xfs_trans_t *tp,
453 xfs_inode_t *ip,
454 int whichfork)
455{
456 int error;
457 xfs_ifork_t *ifp;
458 xfs_extnum_t nextents;
459
460 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
461
462 if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
463 XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW,
464 ip->i_mount);
465 return -EFSCORRUPTED;
466 }
467 nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
468 ifp = XFS_IFORK_PTR(ip, whichfork);
469
470
471
472
473 ifp->if_bytes = ifp->if_real_bytes = 0;
474 xfs_iext_add(ifp, 0, nextents);
475 error = xfs_bmap_read_extents(tp, ip, whichfork);
476 if (error) {
477 xfs_iext_destroy(ifp);
478 return error;
479 }
480 ifp->if_flags |= XFS_IFEXTENTS;
481 return 0;
482}
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501void
502xfs_iroot_realloc(
503 xfs_inode_t *ip,
504 int rec_diff,
505 int whichfork)
506{
507 struct xfs_mount *mp = ip->i_mount;
508 int cur_max;
509 xfs_ifork_t *ifp;
510 struct xfs_btree_block *new_broot;
511 int new_max;
512 size_t new_size;
513 char *np;
514 char *op;
515
516
517
518
519 if (rec_diff == 0) {
520 return;
521 }
522
523 ifp = XFS_IFORK_PTR(ip, whichfork);
524 if (rec_diff > 0) {
525
526
527
528
529 if (ifp->if_broot_bytes == 0) {
530 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
531 ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
532 ifp->if_broot_bytes = (int)new_size;
533 return;
534 }
535
536
537
538
539
540
541
542 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
543 new_max = cur_max + rec_diff;
544 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
545 ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
546 KM_SLEEP | KM_NOFS);
547 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
548 ifp->if_broot_bytes);
549 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
550 (int)new_size);
551 ifp->if_broot_bytes = (int)new_size;
552 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
553 XFS_IFORK_SIZE(ip, whichfork));
554 memmove(np, op, cur_max * (uint)sizeof(xfs_fsblock_t));
555 return;
556 }
557
558
559
560
561
562
563 ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
564 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
565 new_max = cur_max + rec_diff;
566 ASSERT(new_max >= 0);
567 if (new_max > 0)
568 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
569 else
570 new_size = 0;
571 if (new_size > 0) {
572 new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
573
574
575
576 memcpy(new_broot, ifp->if_broot,
577 XFS_BMBT_BLOCK_LEN(ip->i_mount));
578 } else {
579 new_broot = NULL;
580 ifp->if_flags &= ~XFS_IFBROOT;
581 }
582
583
584
585
586 if (new_max > 0) {
587
588
589
590 op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
591 np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
592 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
593
594
595
596
597 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
598 ifp->if_broot_bytes);
599 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
600 (int)new_size);
601 memcpy(np, op, new_max * (uint)sizeof(xfs_fsblock_t));
602 }
603 kmem_free(ifp->if_broot);
604 ifp->if_broot = new_broot;
605 ifp->if_broot_bytes = (int)new_size;
606 if (ifp->if_broot)
607 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
608 XFS_IFORK_SIZE(ip, whichfork));
609 return;
610}
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628void
629xfs_idata_realloc(
630 xfs_inode_t *ip,
631 int byte_diff,
632 int whichfork)
633{
634 xfs_ifork_t *ifp;
635 int new_size;
636 int real_size;
637
638 if (byte_diff == 0) {
639 return;
640 }
641
642 ifp = XFS_IFORK_PTR(ip, whichfork);
643 new_size = (int)ifp->if_bytes + byte_diff;
644 ASSERT(new_size >= 0);
645
646 if (new_size == 0) {
647 if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
648 kmem_free(ifp->if_u1.if_data);
649 }
650 ifp->if_u1.if_data = NULL;
651 real_size = 0;
652 } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) {
653
654
655
656
657 if (ifp->if_u1.if_data == NULL) {
658 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
659 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
660 ASSERT(ifp->if_real_bytes != 0);
661 memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data,
662 new_size);
663 kmem_free(ifp->if_u1.if_data);
664 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
665 }
666 real_size = 0;
667 } else {
668
669
670
671
672
673
674
675 real_size = roundup(new_size, 4);
676 if (ifp->if_u1.if_data == NULL) {
677 ASSERT(ifp->if_real_bytes == 0);
678 ifp->if_u1.if_data = kmem_alloc(real_size,
679 KM_SLEEP | KM_NOFS);
680 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
681
682
683
684
685 if (ifp->if_real_bytes != real_size) {
686 ifp->if_u1.if_data =
687 kmem_realloc(ifp->if_u1.if_data,
688 real_size,
689 KM_SLEEP | KM_NOFS);
690 }
691 } else {
692 ASSERT(ifp->if_real_bytes == 0);
693 ifp->if_u1.if_data = kmem_alloc(real_size,
694 KM_SLEEP | KM_NOFS);
695 memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data,
696 ifp->if_bytes);
697 }
698 }
699 ifp->if_real_bytes = real_size;
700 ifp->if_bytes = new_size;
701 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
702}
703
704void
705xfs_idestroy_fork(
706 xfs_inode_t *ip,
707 int whichfork)
708{
709 xfs_ifork_t *ifp;
710
711 ifp = XFS_IFORK_PTR(ip, whichfork);
712 if (ifp->if_broot != NULL) {
713 kmem_free(ifp->if_broot);
714 ifp->if_broot = NULL;
715 }
716
717
718
719
720
721
722
723 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
724 if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) &&
725 (ifp->if_u1.if_data != NULL)) {
726 ASSERT(ifp->if_real_bytes != 0);
727 kmem_free(ifp->if_u1.if_data);
728 ifp->if_u1.if_data = NULL;
729 ifp->if_real_bytes = 0;
730 }
731 } else if ((ifp->if_flags & XFS_IFEXTENTS) &&
732 ((ifp->if_flags & XFS_IFEXTIREC) ||
733 ((ifp->if_u1.if_extents != NULL) &&
734 (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)))) {
735 ASSERT(ifp->if_real_bytes != 0);
736 xfs_iext_destroy(ifp);
737 }
738 ASSERT(ifp->if_u1.if_extents == NULL ||
739 ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext);
740 ASSERT(ifp->if_real_bytes == 0);
741 if (whichfork == XFS_ATTR_FORK) {
742 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
743 ip->i_afp = NULL;
744 } else if (whichfork == XFS_COW_FORK) {
745 kmem_zone_free(xfs_ifork_zone, ip->i_cowfp);
746 ip->i_cowfp = NULL;
747 }
748}
749
750
751xfs_extnum_t
752xfs_iext_count(struct xfs_ifork *ifp)
753{
754 return ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
755}
756
757
758
759
760
761
762
763
764
765
766
767
768
769int
770xfs_iextents_copy(
771 xfs_inode_t *ip,
772 xfs_bmbt_rec_t *dp,
773 int whichfork)
774{
775 int copied;
776 int i;
777 xfs_ifork_t *ifp;
778 int nrecs;
779 xfs_fsblock_t start_block;
780
781 ifp = XFS_IFORK_PTR(ip, whichfork);
782 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
783 ASSERT(ifp->if_bytes > 0);
784
785 nrecs = xfs_iext_count(ifp);
786 XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork);
787 ASSERT(nrecs > 0);
788
789
790
791
792
793
794
795 copied = 0;
796 for (i = 0; i < nrecs; i++) {
797 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
798
799 ASSERT(xfs_bmbt_validate_extent(ip->i_mount, whichfork, ep));
800
801 start_block = xfs_bmbt_get_startblock(ep);
802 if (isnullstartblock(start_block)) {
803
804
805
806 continue;
807 }
808
809
810 put_unaligned_be64(ep->l0, &dp->l0);
811 put_unaligned_be64(ep->l1, &dp->l1);
812 dp++;
813 copied++;
814 }
815 ASSERT(copied != 0);
816
817 return (copied * (uint)sizeof(xfs_bmbt_rec_t));
818}
819
820
821
822
823
824
825
826
827
828
829
830void
831xfs_iflush_fork(
832 xfs_inode_t *ip,
833 xfs_dinode_t *dip,
834 xfs_inode_log_item_t *iip,
835 int whichfork)
836{
837 char *cp;
838 xfs_ifork_t *ifp;
839 xfs_mount_t *mp;
840 static const short brootflag[2] =
841 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
842 static const short dataflag[2] =
843 { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
844 static const short extflag[2] =
845 { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
846
847 if (!iip)
848 return;
849 ifp = XFS_IFORK_PTR(ip, whichfork);
850
851
852
853
854 if (!ifp) {
855 ASSERT(whichfork == XFS_ATTR_FORK);
856 return;
857 }
858 cp = XFS_DFORK_PTR(dip, whichfork);
859 mp = ip->i_mount;
860 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
861 case XFS_DINODE_FMT_LOCAL:
862 if ((iip->ili_fields & dataflag[whichfork]) &&
863 (ifp->if_bytes > 0)) {
864 ASSERT(ifp->if_u1.if_data != NULL);
865 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
866 memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
867 }
868 break;
869
870 case XFS_DINODE_FMT_EXTENTS:
871 ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
872 !(iip->ili_fields & extflag[whichfork]));
873 if ((iip->ili_fields & extflag[whichfork]) &&
874 (ifp->if_bytes > 0)) {
875 ASSERT(xfs_iext_get_ext(ifp, 0));
876 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
877 (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
878 whichfork);
879 }
880 break;
881
882 case XFS_DINODE_FMT_BTREE:
883 if ((iip->ili_fields & brootflag[whichfork]) &&
884 (ifp->if_broot_bytes > 0)) {
885 ASSERT(ifp->if_broot != NULL);
886 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
887 XFS_IFORK_SIZE(ip, whichfork));
888 xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
889 (xfs_bmdr_block_t *)cp,
890 XFS_DFORK_SIZE(dip, mp, whichfork));
891 }
892 break;
893
894 case XFS_DINODE_FMT_DEV:
895 if (iip->ili_fields & XFS_ILOG_DEV) {
896 ASSERT(whichfork == XFS_DATA_FORK);
897 xfs_dinode_put_rdev(dip, ip->i_df.if_u2.if_rdev);
898 }
899 break;
900
901 case XFS_DINODE_FMT_UUID:
902 if (iip->ili_fields & XFS_ILOG_UUID) {
903 ASSERT(whichfork == XFS_DATA_FORK);
904 memcpy(XFS_DFORK_DPTR(dip),
905 &ip->i_df.if_u2.if_uuid,
906 sizeof(uuid_t));
907 }
908 break;
909
910 default:
911 ASSERT(0);
912 break;
913 }
914}
915
916
917
918
919xfs_bmbt_rec_host_t *
920xfs_iext_get_ext(
921 xfs_ifork_t *ifp,
922 xfs_extnum_t idx)
923{
924 ASSERT(idx >= 0);
925 ASSERT(idx < xfs_iext_count(ifp));
926
927 if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) {
928 return ifp->if_u1.if_ext_irec->er_extbuf;
929 } else if (ifp->if_flags & XFS_IFEXTIREC) {
930 xfs_ext_irec_t *erp;
931 int erp_idx = 0;
932 xfs_extnum_t page_idx = idx;
933
934 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
935 return &erp->er_extbuf[page_idx];
936 } else if (ifp->if_bytes) {
937 return &ifp->if_u1.if_extents[idx];
938 } else {
939 return NULL;
940 }
941}
942
943
944struct xfs_ifork *
945xfs_iext_state_to_fork(
946 struct xfs_inode *ip,
947 int state)
948{
949 if (state & BMAP_COWFORK)
950 return ip->i_cowfp;
951 else if (state & BMAP_ATTRFORK)
952 return ip->i_afp;
953 return &ip->i_df;
954}
955
956
957
958
959
960void
961xfs_iext_insert(
962 xfs_inode_t *ip,
963 xfs_extnum_t idx,
964 xfs_extnum_t count,
965 xfs_bmbt_irec_t *new,
966 int state)
967{
968 xfs_ifork_t *ifp = xfs_iext_state_to_fork(ip, state);
969 xfs_extnum_t i;
970
971 trace_xfs_iext_insert(ip, idx, new, state, _RET_IP_);
972
973 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
974 xfs_iext_add(ifp, idx, count);
975 for (i = idx; i < idx + count; i++, new++)
976 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, i), new);
977}
978
979
980
981
982
983
984
985
986
987
988
989
990
991void
992xfs_iext_add(
993 xfs_ifork_t *ifp,
994 xfs_extnum_t idx,
995 int ext_diff)
996{
997 int byte_diff;
998 int new_size;
999 xfs_extnum_t nextents;
1000
1001 nextents = xfs_iext_count(ifp);
1002 ASSERT((idx >= 0) && (idx <= nextents));
1003 byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t);
1004 new_size = ifp->if_bytes + byte_diff;
1005
1006
1007
1008
1009
1010 if (nextents + ext_diff <= XFS_INLINE_EXTS) {
1011 if (idx < nextents) {
1012 memmove(&ifp->if_u2.if_inline_ext[idx + ext_diff],
1013 &ifp->if_u2.if_inline_ext[idx],
1014 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
1015 memset(&ifp->if_u2.if_inline_ext[idx], 0, byte_diff);
1016 }
1017 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
1018 ifp->if_real_bytes = 0;
1019 }
1020
1021
1022
1023
1024
1025
1026 else if (nextents + ext_diff <= XFS_LINEAR_EXTS) {
1027 xfs_iext_realloc_direct(ifp, new_size);
1028 if (idx < nextents) {
1029 memmove(&ifp->if_u1.if_extents[idx + ext_diff],
1030 &ifp->if_u1.if_extents[idx],
1031 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
1032 memset(&ifp->if_u1.if_extents[idx], 0, byte_diff);
1033 }
1034 }
1035
1036 else {
1037 xfs_ext_irec_t *erp;
1038 int erp_idx = 0;
1039 int page_idx = idx;
1040
1041 ASSERT(nextents + ext_diff > XFS_LINEAR_EXTS);
1042 if (ifp->if_flags & XFS_IFEXTIREC) {
1043 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 1);
1044 } else {
1045 xfs_iext_irec_init(ifp);
1046 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1047 erp = ifp->if_u1.if_ext_irec;
1048 }
1049
1050 if (erp && erp->er_extcount + ext_diff <= XFS_LINEAR_EXTS) {
1051 if (page_idx < erp->er_extcount) {
1052 memmove(&erp->er_extbuf[page_idx + ext_diff],
1053 &erp->er_extbuf[page_idx],
1054 (erp->er_extcount - page_idx) *
1055 sizeof(xfs_bmbt_rec_t));
1056 memset(&erp->er_extbuf[page_idx], 0, byte_diff);
1057 }
1058 erp->er_extcount += ext_diff;
1059 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
1060 }
1061
1062 else if (erp) {
1063 xfs_iext_add_indirect_multi(ifp,
1064 erp_idx, page_idx, ext_diff);
1065 }
1066
1067
1068
1069
1070
1071
1072 else {
1073 uint count = ext_diff;
1074
1075 while (count) {
1076 erp = xfs_iext_irec_new(ifp, erp_idx);
1077 erp->er_extcount = min(count, XFS_LINEAR_EXTS);
1078 count -= erp->er_extcount;
1079 if (count)
1080 erp_idx++;
1081 }
1082 }
1083 }
1084 ifp->if_bytes = new_size;
1085}
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103void
1104xfs_iext_add_indirect_multi(
1105 xfs_ifork_t *ifp,
1106 int erp_idx,
1107 xfs_extnum_t idx,
1108 int count)
1109{
1110 int byte_diff;
1111 xfs_ext_irec_t *erp;
1112 xfs_extnum_t ext_diff;
1113 xfs_extnum_t ext_cnt;
1114 xfs_extnum_t nex2;
1115 xfs_bmbt_rec_t *nex2_ep = NULL;
1116 int nlists;
1117
1118 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1119 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1120 nex2 = erp->er_extcount - idx;
1121 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1122
1123
1124
1125
1126 if (nex2) {
1127 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
1128 nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_NOFS);
1129 memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff);
1130 erp->er_extcount -= nex2;
1131 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2);
1132 memset(&erp->er_extbuf[idx], 0, byte_diff);
1133 }
1134
1135
1136
1137
1138
1139
1140
1141 ext_cnt = count;
1142 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount);
1143 if (ext_diff) {
1144 erp->er_extcount += ext_diff;
1145 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
1146 ext_cnt -= ext_diff;
1147 }
1148 while (ext_cnt) {
1149 erp_idx++;
1150 erp = xfs_iext_irec_new(ifp, erp_idx);
1151 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS);
1152 erp->er_extcount = ext_diff;
1153 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
1154 ext_cnt -= ext_diff;
1155 }
1156
1157
1158 if (nex2) {
1159 xfs_extnum_t ext_avail;
1160 int i;
1161
1162 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
1163 ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
1164 i = 0;
1165
1166
1167
1168
1169 if (nex2 <= ext_avail) {
1170 i = erp->er_extcount;
1171 }
1172
1173
1174
1175
1176 else if ((erp_idx < nlists - 1) &&
1177 (nex2 <= (ext_avail = XFS_LINEAR_EXTS -
1178 ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) {
1179 erp_idx++;
1180 erp++;
1181
1182 memmove(&erp->er_extbuf[nex2], erp->er_extbuf,
1183 erp->er_extcount * sizeof(xfs_bmbt_rec_t));
1184 }
1185
1186
1187
1188
1189 else {
1190 erp_idx++;
1191 erp = xfs_iext_irec_new(ifp, erp_idx);
1192 }
1193 memmove(&erp->er_extbuf[i], nex2_ep, byte_diff);
1194 kmem_free(nex2_ep);
1195 erp->er_extcount += nex2;
1196 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2);
1197 }
1198}
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211void
1212xfs_iext_remove(
1213 xfs_inode_t *ip,
1214 xfs_extnum_t idx,
1215 int ext_diff,
1216 int state)
1217{
1218 xfs_ifork_t *ifp = xfs_iext_state_to_fork(ip, state);
1219 xfs_extnum_t nextents;
1220 int new_size;
1221
1222 trace_xfs_iext_remove(ip, idx, state, _RET_IP_);
1223
1224 ASSERT(ext_diff > 0);
1225 nextents = xfs_iext_count(ifp);
1226 new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t);
1227
1228 if (new_size == 0) {
1229 xfs_iext_destroy(ifp);
1230 } else if (ifp->if_flags & XFS_IFEXTIREC) {
1231 xfs_iext_remove_indirect(ifp, idx, ext_diff);
1232 } else if (ifp->if_real_bytes) {
1233 xfs_iext_remove_direct(ifp, idx, ext_diff);
1234 } else {
1235 xfs_iext_remove_inline(ifp, idx, ext_diff);
1236 }
1237 ifp->if_bytes = new_size;
1238}
1239
1240
1241
1242
1243
1244void
1245xfs_iext_remove_inline(
1246 xfs_ifork_t *ifp,
1247 xfs_extnum_t idx,
1248 int ext_diff)
1249{
1250 int nextents;
1251
1252 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
1253 ASSERT(idx < XFS_INLINE_EXTS);
1254 nextents = xfs_iext_count(ifp);
1255 ASSERT(((nextents - ext_diff) > 0) &&
1256 (nextents - ext_diff) < XFS_INLINE_EXTS);
1257
1258 if (idx + ext_diff < nextents) {
1259 memmove(&ifp->if_u2.if_inline_ext[idx],
1260 &ifp->if_u2.if_inline_ext[idx + ext_diff],
1261 (nextents - (idx + ext_diff)) *
1262 sizeof(xfs_bmbt_rec_t));
1263 memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff],
1264 0, ext_diff * sizeof(xfs_bmbt_rec_t));
1265 } else {
1266 memset(&ifp->if_u2.if_inline_ext[idx], 0,
1267 ext_diff * sizeof(xfs_bmbt_rec_t));
1268 }
1269}
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281void
1282xfs_iext_remove_direct(
1283 xfs_ifork_t *ifp,
1284 xfs_extnum_t idx,
1285 int ext_diff)
1286{
1287 xfs_extnum_t nextents;
1288 int new_size;
1289
1290 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
1291 new_size = ifp->if_bytes -
1292 (ext_diff * sizeof(xfs_bmbt_rec_t));
1293 nextents = xfs_iext_count(ifp);
1294
1295 if (new_size == 0) {
1296 xfs_iext_destroy(ifp);
1297 return;
1298 }
1299
1300 if (idx + ext_diff < nextents) {
1301 memmove(&ifp->if_u1.if_extents[idx],
1302 &ifp->if_u1.if_extents[idx + ext_diff],
1303 (nextents - (idx + ext_diff)) *
1304 sizeof(xfs_bmbt_rec_t));
1305 }
1306 memset(&ifp->if_u1.if_extents[nextents - ext_diff],
1307 0, ext_diff * sizeof(xfs_bmbt_rec_t));
1308
1309
1310
1311
1312
1313
1314 xfs_iext_realloc_direct(ifp, new_size);
1315 ifp->if_bytes = new_size;
1316}
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333void
1334xfs_iext_remove_indirect(
1335 xfs_ifork_t *ifp,
1336 xfs_extnum_t idx,
1337 int count)
1338{
1339 xfs_ext_irec_t *erp;
1340 int erp_idx = 0;
1341 xfs_extnum_t ext_cnt;
1342 xfs_extnum_t ext_diff;
1343 xfs_extnum_t nex1;
1344 xfs_extnum_t nex2;
1345 int page_idx = idx;
1346
1347 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1348 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
1349 ASSERT(erp != NULL);
1350 nex1 = page_idx;
1351 ext_cnt = count;
1352 while (ext_cnt) {
1353 nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0);
1354 ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1));
1355
1356
1357
1358
1359 if (ext_diff == erp->er_extcount) {
1360 xfs_iext_irec_remove(ifp, erp_idx);
1361 ext_cnt -= ext_diff;
1362 nex1 = 0;
1363 if (ext_cnt) {
1364 ASSERT(erp_idx < ifp->if_real_bytes /
1365 XFS_IEXT_BUFSZ);
1366 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1367 nex1 = 0;
1368 continue;
1369 } else {
1370 break;
1371 }
1372 }
1373
1374 if (nex2) {
1375 memmove(&erp->er_extbuf[nex1],
1376 &erp->er_extbuf[nex1 + ext_diff],
1377 nex2 * sizeof(xfs_bmbt_rec_t));
1378 }
1379
1380 memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ -
1381 ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t))));
1382
1383 erp->er_extcount -= ext_diff;
1384 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff);
1385 ext_cnt -= ext_diff;
1386 nex1 = 0;
1387 erp_idx++;
1388 erp++;
1389 }
1390 ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t);
1391 xfs_iext_irec_compact(ifp);
1392}
1393
1394
1395
1396
1397void
1398xfs_iext_realloc_direct(
1399 xfs_ifork_t *ifp,
1400 int new_size)
1401{
1402 int rnew_size;
1403
1404 rnew_size = new_size;
1405
1406 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) ||
1407 ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) &&
1408 (new_size != ifp->if_real_bytes)));
1409
1410
1411 if (new_size == 0) {
1412 xfs_iext_destroy(ifp);
1413 }
1414
1415 else if (ifp->if_real_bytes) {
1416
1417 if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) {
1418 xfs_iext_direct_to_inline(ifp, new_size /
1419 (uint)sizeof(xfs_bmbt_rec_t));
1420 ifp->if_bytes = new_size;
1421 return;
1422 }
1423 if (!is_power_of_2(new_size)){
1424 rnew_size = roundup_pow_of_two(new_size);
1425 }
1426 if (rnew_size != ifp->if_real_bytes) {
1427 ifp->if_u1.if_extents =
1428 kmem_realloc(ifp->if_u1.if_extents,
1429 rnew_size, KM_NOFS);
1430 }
1431 if (rnew_size > ifp->if_real_bytes) {
1432 memset(&ifp->if_u1.if_extents[ifp->if_bytes /
1433 (uint)sizeof(xfs_bmbt_rec_t)], 0,
1434 rnew_size - ifp->if_real_bytes);
1435 }
1436 }
1437
1438 else {
1439 if (!is_power_of_2(new_size)) {
1440 rnew_size = roundup_pow_of_two(new_size);
1441 }
1442 xfs_iext_inline_to_direct(ifp, rnew_size);
1443 }
1444 ifp->if_real_bytes = rnew_size;
1445 ifp->if_bytes = new_size;
1446}
1447
1448
1449
1450
1451void
1452xfs_iext_direct_to_inline(
1453 xfs_ifork_t *ifp,
1454 xfs_extnum_t nextents)
1455{
1456 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1457 ASSERT(nextents <= XFS_INLINE_EXTS);
1458
1459
1460
1461
1462
1463 memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents,
1464 nextents * sizeof(xfs_bmbt_rec_t));
1465 kmem_free(ifp->if_u1.if_extents);
1466 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
1467 ifp->if_real_bytes = 0;
1468}
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478void
1479xfs_iext_inline_to_direct(
1480 xfs_ifork_t *ifp,
1481 int new_size)
1482{
1483 ifp->if_u1.if_extents = kmem_alloc(new_size, KM_NOFS);
1484 memset(ifp->if_u1.if_extents, 0, new_size);
1485 if (ifp->if_bytes) {
1486 memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext,
1487 ifp->if_bytes);
1488 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
1489 sizeof(xfs_bmbt_rec_t));
1490 }
1491 ifp->if_real_bytes = new_size;
1492}
1493
1494
1495
1496
1497STATIC void
1498xfs_iext_realloc_indirect(
1499 xfs_ifork_t *ifp,
1500 int new_size)
1501{
1502 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1503 ASSERT(ifp->if_real_bytes);
1504 ASSERT((new_size >= 0) &&
1505 (new_size != ((ifp->if_real_bytes / XFS_IEXT_BUFSZ) *
1506 sizeof(xfs_ext_irec_t))));
1507 if (new_size == 0) {
1508 xfs_iext_destroy(ifp);
1509 } else {
1510 ifp->if_u1.if_ext_irec =
1511 kmem_realloc(ifp->if_u1.if_ext_irec, new_size, KM_NOFS);
1512 }
1513}
1514
1515
1516
1517
1518STATIC void
1519xfs_iext_indirect_to_direct(
1520 xfs_ifork_t *ifp)
1521{
1522 xfs_bmbt_rec_host_t *ep;
1523 xfs_extnum_t nextents;
1524 int size;
1525
1526 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1527 nextents = xfs_iext_count(ifp);
1528 ASSERT(nextents <= XFS_LINEAR_EXTS);
1529 size = nextents * sizeof(xfs_bmbt_rec_t);
1530
1531 xfs_iext_irec_compact_pages(ifp);
1532 ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ);
1533
1534 ep = ifp->if_u1.if_ext_irec->er_extbuf;
1535 kmem_free(ifp->if_u1.if_ext_irec);
1536 ifp->if_flags &= ~XFS_IFEXTIREC;
1537 ifp->if_u1.if_extents = ep;
1538 ifp->if_bytes = size;
1539 if (nextents < XFS_LINEAR_EXTS) {
1540 xfs_iext_realloc_direct(ifp, size);
1541 }
1542}
1543
1544
1545
1546
1547STATIC void
1548xfs_iext_irec_remove_all(
1549 struct xfs_ifork *ifp)
1550{
1551 int nlists;
1552 int i;
1553
1554 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1555 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1556 for (i = 0; i < nlists; i++)
1557 kmem_free(ifp->if_u1.if_ext_irec[i].er_extbuf);
1558 kmem_free(ifp->if_u1.if_ext_irec);
1559 ifp->if_flags &= ~XFS_IFEXTIREC;
1560}
1561
1562
1563
1564
1565void
1566xfs_iext_destroy(
1567 xfs_ifork_t *ifp)
1568{
1569 if (ifp->if_flags & XFS_IFEXTIREC) {
1570 xfs_iext_irec_remove_all(ifp);
1571 } else if (ifp->if_real_bytes) {
1572 kmem_free(ifp->if_u1.if_extents);
1573 } else if (ifp->if_bytes) {
1574 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
1575 sizeof(xfs_bmbt_rec_t));
1576 }
1577 ifp->if_u1.if_extents = NULL;
1578 ifp->if_real_bytes = 0;
1579 ifp->if_bytes = 0;
1580}
1581
1582
1583
1584
1585xfs_bmbt_rec_host_t *
1586xfs_iext_bno_to_ext(
1587 xfs_ifork_t *ifp,
1588 xfs_fileoff_t bno,
1589 xfs_extnum_t *idxp)
1590{
1591 xfs_bmbt_rec_host_t *base;
1592 xfs_filblks_t blockcount = 0;
1593 xfs_bmbt_rec_host_t *ep = NULL;
1594 xfs_ext_irec_t *erp = NULL;
1595 int high;
1596 xfs_extnum_t idx = 0;
1597 int low;
1598 xfs_extnum_t nextents;
1599 xfs_fileoff_t startoff = 0;
1600
1601 nextents = xfs_iext_count(ifp);
1602 if (nextents == 0) {
1603 *idxp = 0;
1604 return NULL;
1605 }
1606 low = 0;
1607 if (ifp->if_flags & XFS_IFEXTIREC) {
1608
1609 int erp_idx = 0;
1610 erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx);
1611 base = erp->er_extbuf;
1612 high = erp->er_extcount - 1;
1613 } else {
1614 base = ifp->if_u1.if_extents;
1615 high = nextents - 1;
1616 }
1617
1618 while (low <= high) {
1619 idx = (low + high) >> 1;
1620 ep = base + idx;
1621 startoff = xfs_bmbt_get_startoff(ep);
1622 blockcount = xfs_bmbt_get_blockcount(ep);
1623 if (bno < startoff) {
1624 high = idx - 1;
1625 } else if (bno >= startoff + blockcount) {
1626 low = idx + 1;
1627 } else {
1628
1629 if (ifp->if_flags & XFS_IFEXTIREC) {
1630 idx += erp->er_extoff;
1631 }
1632 *idxp = idx;
1633 return ep;
1634 }
1635 }
1636
1637 if (ifp->if_flags & XFS_IFEXTIREC) {
1638 idx += erp->er_extoff;
1639 }
1640 if (bno >= startoff + blockcount) {
1641 if (++idx == nextents) {
1642 ep = NULL;
1643 } else {
1644 ep = xfs_iext_get_ext(ifp, idx);
1645 }
1646 }
1647 *idxp = idx;
1648 return ep;
1649}
1650
1651
1652
1653
1654
1655
1656xfs_ext_irec_t *
1657xfs_iext_bno_to_irec(
1658 xfs_ifork_t *ifp,
1659 xfs_fileoff_t bno,
1660 int *erp_idxp)
1661{
1662 xfs_ext_irec_t *erp = NULL;
1663 xfs_ext_irec_t *erp_next;
1664 int erp_idx;
1665 int nlists;
1666 int high;
1667 int low;
1668
1669 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1670 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1671 erp_idx = 0;
1672 low = 0;
1673 high = nlists - 1;
1674 while (low <= high) {
1675 erp_idx = (low + high) >> 1;
1676 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1677 erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL;
1678 if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) {
1679 high = erp_idx - 1;
1680 } else if (erp_next && bno >=
1681 xfs_bmbt_get_startoff(erp_next->er_extbuf)) {
1682 low = erp_idx + 1;
1683 } else {
1684 break;
1685 }
1686 }
1687 *erp_idxp = erp_idx;
1688 return erp;
1689}
1690
1691
1692
1693
1694
1695
1696
1697xfs_ext_irec_t *
1698xfs_iext_idx_to_irec(
1699 xfs_ifork_t *ifp,
1700 xfs_extnum_t *idxp,
1701 int *erp_idxp,
1702 int realloc)
1703{
1704 xfs_ext_irec_t *prev;
1705 xfs_ext_irec_t *erp = NULL;
1706 int erp_idx;
1707 int nlists;
1708 int high;
1709 int low;
1710 xfs_extnum_t page_idx = *idxp;
1711
1712 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1713 ASSERT(page_idx >= 0);
1714 ASSERT(page_idx <= xfs_iext_count(ifp));
1715 ASSERT(page_idx < xfs_iext_count(ifp) || realloc);
1716
1717 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1718 erp_idx = 0;
1719 low = 0;
1720 high = nlists - 1;
1721
1722
1723 while (low <= high) {
1724 erp_idx = (low + high) >> 1;
1725 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1726 prev = erp_idx > 0 ? erp - 1 : NULL;
1727 if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff &&
1728 realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) {
1729 high = erp_idx - 1;
1730 } else if (page_idx > erp->er_extoff + erp->er_extcount ||
1731 (page_idx == erp->er_extoff + erp->er_extcount &&
1732 !realloc)) {
1733 low = erp_idx + 1;
1734 } else if (page_idx == erp->er_extoff + erp->er_extcount &&
1735 erp->er_extcount == XFS_LINEAR_EXTS) {
1736 ASSERT(realloc);
1737 page_idx = 0;
1738 erp_idx++;
1739 erp = erp_idx < nlists ? erp + 1 : NULL;
1740 break;
1741 } else {
1742 page_idx -= erp->er_extoff;
1743 break;
1744 }
1745 }
1746 *idxp = page_idx;
1747 *erp_idxp = erp_idx;
1748 return erp;
1749}
1750
1751
1752
1753
1754
1755void
1756xfs_iext_irec_init(
1757 xfs_ifork_t *ifp)
1758{
1759 xfs_ext_irec_t *erp;
1760 xfs_extnum_t nextents;
1761
1762 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
1763 nextents = xfs_iext_count(ifp);
1764 ASSERT(nextents <= XFS_LINEAR_EXTS);
1765
1766 erp = kmem_alloc(sizeof(xfs_ext_irec_t), KM_NOFS);
1767
1768 if (nextents == 0) {
1769 ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
1770 } else if (!ifp->if_real_bytes) {
1771 xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ);
1772 } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) {
1773 xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ);
1774 }
1775 erp->er_extbuf = ifp->if_u1.if_extents;
1776 erp->er_extcount = nextents;
1777 erp->er_extoff = 0;
1778
1779 ifp->if_flags |= XFS_IFEXTIREC;
1780 ifp->if_real_bytes = XFS_IEXT_BUFSZ;
1781 ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t);
1782 ifp->if_u1.if_ext_irec = erp;
1783
1784 return;
1785}
1786
1787
1788
1789
1790xfs_ext_irec_t *
1791xfs_iext_irec_new(
1792 xfs_ifork_t *ifp,
1793 int erp_idx)
1794{
1795 xfs_ext_irec_t *erp;
1796 int i;
1797 int nlists;
1798
1799 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1800 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1801
1802
1803 xfs_iext_realloc_indirect(ifp, ++nlists *
1804 sizeof(xfs_ext_irec_t));
1805
1806
1807
1808
1809 erp = ifp->if_u1.if_ext_irec;
1810 for (i = nlists - 1; i > erp_idx; i--) {
1811 memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t));
1812 }
1813 ASSERT(i == erp_idx);
1814
1815
1816 erp = ifp->if_u1.if_ext_irec;
1817 erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
1818 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
1819 memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ);
1820 erp[erp_idx].er_extcount = 0;
1821 erp[erp_idx].er_extoff = erp_idx > 0 ?
1822 erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0;
1823 return (&erp[erp_idx]);
1824}
1825
1826
1827
1828
1829void
1830xfs_iext_irec_remove(
1831 xfs_ifork_t *ifp,
1832 int erp_idx)
1833{
1834 xfs_ext_irec_t *erp;
1835 int i;
1836 int nlists;
1837
1838 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1839 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1840 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1841 if (erp->er_extbuf) {
1842 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1,
1843 -erp->er_extcount);
1844 kmem_free(erp->er_extbuf);
1845 }
1846
1847 erp = ifp->if_u1.if_ext_irec;
1848 for (i = erp_idx; i < nlists - 1; i++) {
1849 memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t));
1850 }
1851
1852
1853
1854
1855
1856
1857
1858 if (--nlists) {
1859 xfs_iext_realloc_indirect(ifp,
1860 nlists * sizeof(xfs_ext_irec_t));
1861 } else {
1862 kmem_free(ifp->if_u1.if_ext_irec);
1863 }
1864 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
1865}
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878void
1879xfs_iext_irec_compact(
1880 xfs_ifork_t *ifp)
1881{
1882 xfs_extnum_t nextents;
1883 int nlists;
1884
1885 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1886 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1887 nextents = xfs_iext_count(ifp);
1888
1889 if (nextents == 0) {
1890 xfs_iext_destroy(ifp);
1891 } else if (nextents <= XFS_INLINE_EXTS) {
1892 xfs_iext_indirect_to_direct(ifp);
1893 xfs_iext_direct_to_inline(ifp, nextents);
1894 } else if (nextents <= XFS_LINEAR_EXTS) {
1895 xfs_iext_indirect_to_direct(ifp);
1896 } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) {
1897 xfs_iext_irec_compact_pages(ifp);
1898 }
1899}
1900
1901
1902
1903
1904void
1905xfs_iext_irec_compact_pages(
1906 xfs_ifork_t *ifp)
1907{
1908 xfs_ext_irec_t *erp, *erp_next;
1909 int erp_idx = 0;
1910 int nlists;
1911
1912 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1913 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1914 while (erp_idx < nlists - 1) {
1915 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1916 erp_next = erp + 1;
1917 if (erp_next->er_extcount <=
1918 (XFS_LINEAR_EXTS - erp->er_extcount)) {
1919 memcpy(&erp->er_extbuf[erp->er_extcount],
1920 erp_next->er_extbuf, erp_next->er_extcount *
1921 sizeof(xfs_bmbt_rec_t));
1922 erp->er_extcount += erp_next->er_extcount;
1923
1924
1925
1926
1927
1928 kmem_free(erp_next->er_extbuf);
1929 erp_next->er_extbuf = NULL;
1930 xfs_iext_irec_remove(ifp, erp_idx + 1);
1931 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1932 } else {
1933 erp_idx++;
1934 }
1935 }
1936}
1937
1938
1939
1940
1941
1942
1943
1944
1945void
1946xfs_iext_irec_update_extoffs(
1947 xfs_ifork_t *ifp,
1948 int erp_idx,
1949 int ext_diff)
1950{
1951 int i;
1952 int nlists;
1953
1954 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1955 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1956 for (i = erp_idx; i < nlists; i++) {
1957 ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff;
1958 }
1959}
1960
1961
1962
1963
1964void
1965xfs_ifork_init_cow(
1966 struct xfs_inode *ip)
1967{
1968 if (ip->i_cowfp)
1969 return;
1970
1971 ip->i_cowfp = kmem_zone_zalloc(xfs_ifork_zone,
1972 KM_SLEEP | KM_NOFS);
1973 ip->i_cowfp->if_flags = XFS_IFEXTENTS;
1974 ip->i_cformat = XFS_DINODE_FMT_EXTENTS;
1975 ip->i_cnextents = 0;
1976}
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989bool
1990xfs_iext_lookup_extent(
1991 struct xfs_inode *ip,
1992 struct xfs_ifork *ifp,
1993 xfs_fileoff_t bno,
1994 xfs_extnum_t *idxp,
1995 struct xfs_bmbt_irec *gotp)
1996{
1997 struct xfs_bmbt_rec_host *ep;
1998
1999 XFS_STATS_INC(ip->i_mount, xs_look_exlist);
2000
2001 ep = xfs_iext_bno_to_ext(ifp, bno, idxp);
2002 if (!ep)
2003 return false;
2004 xfs_bmbt_get_all(ep, gotp);
2005 return true;
2006}
2007
2008
2009
2010
2011
2012bool
2013xfs_iext_get_extent(
2014 struct xfs_ifork *ifp,
2015 xfs_extnum_t idx,
2016 struct xfs_bmbt_irec *gotp)
2017{
2018 if (idx < 0 || idx >= xfs_iext_count(ifp))
2019 return false;
2020 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), gotp);
2021 return true;
2022}
2023
2024void
2025xfs_iext_update_extent(
2026 struct xfs_ifork *ifp,
2027 xfs_extnum_t idx,
2028 struct xfs_bmbt_irec *gotp)
2029{
2030 ASSERT(idx >= 0);
2031 ASSERT(idx < xfs_iext_count(ifp));
2032
2033 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, idx), gotp);
2034}
2035