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 int nlists;
1503 int size;
1504
1505 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1506 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1507 size = nlists * sizeof(xfs_ext_irec_t);
1508 ASSERT(ifp->if_real_bytes);
1509 ASSERT((new_size >= 0) && (new_size != size));
1510 if (new_size == 0) {
1511 xfs_iext_destroy(ifp);
1512 } else {
1513 ifp->if_u1.if_ext_irec =
1514 kmem_realloc(ifp->if_u1.if_ext_irec, new_size, KM_NOFS);
1515 }
1516}
1517
1518
1519
1520
1521STATIC void
1522xfs_iext_indirect_to_direct(
1523 xfs_ifork_t *ifp)
1524{
1525 xfs_bmbt_rec_host_t *ep;
1526 xfs_extnum_t nextents;
1527 int size;
1528
1529 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1530 nextents = xfs_iext_count(ifp);
1531 ASSERT(nextents <= XFS_LINEAR_EXTS);
1532 size = nextents * sizeof(xfs_bmbt_rec_t);
1533
1534 xfs_iext_irec_compact_pages(ifp);
1535 ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ);
1536
1537 ep = ifp->if_u1.if_ext_irec->er_extbuf;
1538 kmem_free(ifp->if_u1.if_ext_irec);
1539 ifp->if_flags &= ~XFS_IFEXTIREC;
1540 ifp->if_u1.if_extents = ep;
1541 ifp->if_bytes = size;
1542 if (nextents < XFS_LINEAR_EXTS) {
1543 xfs_iext_realloc_direct(ifp, size);
1544 }
1545}
1546
1547
1548
1549
1550STATIC void
1551xfs_iext_irec_remove_all(
1552 struct xfs_ifork *ifp)
1553{
1554 int nlists;
1555 int i;
1556
1557 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1558 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1559 for (i = 0; i < nlists; i++)
1560 kmem_free(ifp->if_u1.if_ext_irec[i].er_extbuf);
1561 kmem_free(ifp->if_u1.if_ext_irec);
1562 ifp->if_flags &= ~XFS_IFEXTIREC;
1563}
1564
1565
1566
1567
1568void
1569xfs_iext_destroy(
1570 xfs_ifork_t *ifp)
1571{
1572 if (ifp->if_flags & XFS_IFEXTIREC) {
1573 xfs_iext_irec_remove_all(ifp);
1574 } else if (ifp->if_real_bytes) {
1575 kmem_free(ifp->if_u1.if_extents);
1576 } else if (ifp->if_bytes) {
1577 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
1578 sizeof(xfs_bmbt_rec_t));
1579 }
1580 ifp->if_u1.if_extents = NULL;
1581 ifp->if_real_bytes = 0;
1582 ifp->if_bytes = 0;
1583}
1584
1585
1586
1587
1588xfs_bmbt_rec_host_t *
1589xfs_iext_bno_to_ext(
1590 xfs_ifork_t *ifp,
1591 xfs_fileoff_t bno,
1592 xfs_extnum_t *idxp)
1593{
1594 xfs_bmbt_rec_host_t *base;
1595 xfs_filblks_t blockcount = 0;
1596 xfs_bmbt_rec_host_t *ep = NULL;
1597 xfs_ext_irec_t *erp = NULL;
1598 int high;
1599 xfs_extnum_t idx = 0;
1600 int low;
1601 xfs_extnum_t nextents;
1602 xfs_fileoff_t startoff = 0;
1603
1604 nextents = xfs_iext_count(ifp);
1605 if (nextents == 0) {
1606 *idxp = 0;
1607 return NULL;
1608 }
1609 low = 0;
1610 if (ifp->if_flags & XFS_IFEXTIREC) {
1611
1612 int erp_idx = 0;
1613 erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx);
1614 base = erp->er_extbuf;
1615 high = erp->er_extcount - 1;
1616 } else {
1617 base = ifp->if_u1.if_extents;
1618 high = nextents - 1;
1619 }
1620
1621 while (low <= high) {
1622 idx = (low + high) >> 1;
1623 ep = base + idx;
1624 startoff = xfs_bmbt_get_startoff(ep);
1625 blockcount = xfs_bmbt_get_blockcount(ep);
1626 if (bno < startoff) {
1627 high = idx - 1;
1628 } else if (bno >= startoff + blockcount) {
1629 low = idx + 1;
1630 } else {
1631
1632 if (ifp->if_flags & XFS_IFEXTIREC) {
1633 idx += erp->er_extoff;
1634 }
1635 *idxp = idx;
1636 return ep;
1637 }
1638 }
1639
1640 if (ifp->if_flags & XFS_IFEXTIREC) {
1641 idx += erp->er_extoff;
1642 }
1643 if (bno >= startoff + blockcount) {
1644 if (++idx == nextents) {
1645 ep = NULL;
1646 } else {
1647 ep = xfs_iext_get_ext(ifp, idx);
1648 }
1649 }
1650 *idxp = idx;
1651 return ep;
1652}
1653
1654
1655
1656
1657
1658
1659xfs_ext_irec_t *
1660xfs_iext_bno_to_irec(
1661 xfs_ifork_t *ifp,
1662 xfs_fileoff_t bno,
1663 int *erp_idxp)
1664{
1665 xfs_ext_irec_t *erp = NULL;
1666 xfs_ext_irec_t *erp_next;
1667 int erp_idx;
1668 int nlists;
1669 int high;
1670 int low;
1671
1672 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1673 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1674 erp_idx = 0;
1675 low = 0;
1676 high = nlists - 1;
1677 while (low <= high) {
1678 erp_idx = (low + high) >> 1;
1679 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1680 erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL;
1681 if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) {
1682 high = erp_idx - 1;
1683 } else if (erp_next && bno >=
1684 xfs_bmbt_get_startoff(erp_next->er_extbuf)) {
1685 low = erp_idx + 1;
1686 } else {
1687 break;
1688 }
1689 }
1690 *erp_idxp = erp_idx;
1691 return erp;
1692}
1693
1694
1695
1696
1697
1698
1699
1700xfs_ext_irec_t *
1701xfs_iext_idx_to_irec(
1702 xfs_ifork_t *ifp,
1703 xfs_extnum_t *idxp,
1704 int *erp_idxp,
1705 int realloc)
1706{
1707 xfs_ext_irec_t *prev;
1708 xfs_ext_irec_t *erp = NULL;
1709 int erp_idx;
1710 int nlists;
1711 int high;
1712 int low;
1713 xfs_extnum_t page_idx = *idxp;
1714
1715 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1716 ASSERT(page_idx >= 0);
1717 ASSERT(page_idx <= xfs_iext_count(ifp));
1718 ASSERT(page_idx < xfs_iext_count(ifp) || realloc);
1719
1720 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1721 erp_idx = 0;
1722 low = 0;
1723 high = nlists - 1;
1724
1725
1726 while (low <= high) {
1727 erp_idx = (low + high) >> 1;
1728 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1729 prev = erp_idx > 0 ? erp - 1 : NULL;
1730 if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff &&
1731 realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) {
1732 high = erp_idx - 1;
1733 } else if (page_idx > erp->er_extoff + erp->er_extcount ||
1734 (page_idx == erp->er_extoff + erp->er_extcount &&
1735 !realloc)) {
1736 low = erp_idx + 1;
1737 } else if (page_idx == erp->er_extoff + erp->er_extcount &&
1738 erp->er_extcount == XFS_LINEAR_EXTS) {
1739 ASSERT(realloc);
1740 page_idx = 0;
1741 erp_idx++;
1742 erp = erp_idx < nlists ? erp + 1 : NULL;
1743 break;
1744 } else {
1745 page_idx -= erp->er_extoff;
1746 break;
1747 }
1748 }
1749 *idxp = page_idx;
1750 *erp_idxp = erp_idx;
1751 return erp;
1752}
1753
1754
1755
1756
1757
1758void
1759xfs_iext_irec_init(
1760 xfs_ifork_t *ifp)
1761{
1762 xfs_ext_irec_t *erp;
1763 xfs_extnum_t nextents;
1764
1765 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
1766 nextents = xfs_iext_count(ifp);
1767 ASSERT(nextents <= XFS_LINEAR_EXTS);
1768
1769 erp = kmem_alloc(sizeof(xfs_ext_irec_t), KM_NOFS);
1770
1771 if (nextents == 0) {
1772 ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
1773 } else if (!ifp->if_real_bytes) {
1774 xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ);
1775 } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) {
1776 xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ);
1777 }
1778 erp->er_extbuf = ifp->if_u1.if_extents;
1779 erp->er_extcount = nextents;
1780 erp->er_extoff = 0;
1781
1782 ifp->if_flags |= XFS_IFEXTIREC;
1783 ifp->if_real_bytes = XFS_IEXT_BUFSZ;
1784 ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t);
1785 ifp->if_u1.if_ext_irec = erp;
1786
1787 return;
1788}
1789
1790
1791
1792
1793xfs_ext_irec_t *
1794xfs_iext_irec_new(
1795 xfs_ifork_t *ifp,
1796 int erp_idx)
1797{
1798 xfs_ext_irec_t *erp;
1799 int i;
1800 int nlists;
1801
1802 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1803 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1804
1805
1806 xfs_iext_realloc_indirect(ifp, ++nlists *
1807 sizeof(xfs_ext_irec_t));
1808
1809
1810
1811
1812 erp = ifp->if_u1.if_ext_irec;
1813 for (i = nlists - 1; i > erp_idx; i--) {
1814 memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t));
1815 }
1816 ASSERT(i == erp_idx);
1817
1818
1819 erp = ifp->if_u1.if_ext_irec;
1820 erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
1821 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
1822 memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ);
1823 erp[erp_idx].er_extcount = 0;
1824 erp[erp_idx].er_extoff = erp_idx > 0 ?
1825 erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0;
1826 return (&erp[erp_idx]);
1827}
1828
1829
1830
1831
1832void
1833xfs_iext_irec_remove(
1834 xfs_ifork_t *ifp,
1835 int erp_idx)
1836{
1837 xfs_ext_irec_t *erp;
1838 int i;
1839 int nlists;
1840
1841 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1842 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1843 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1844 if (erp->er_extbuf) {
1845 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1,
1846 -erp->er_extcount);
1847 kmem_free(erp->er_extbuf);
1848 }
1849
1850 erp = ifp->if_u1.if_ext_irec;
1851 for (i = erp_idx; i < nlists - 1; i++) {
1852 memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t));
1853 }
1854
1855
1856
1857
1858
1859
1860
1861 if (--nlists) {
1862 xfs_iext_realloc_indirect(ifp,
1863 nlists * sizeof(xfs_ext_irec_t));
1864 } else {
1865 kmem_free(ifp->if_u1.if_ext_irec);
1866 }
1867 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
1868}
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881void
1882xfs_iext_irec_compact(
1883 xfs_ifork_t *ifp)
1884{
1885 xfs_extnum_t nextents;
1886 int nlists;
1887
1888 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1889 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1890 nextents = xfs_iext_count(ifp);
1891
1892 if (nextents == 0) {
1893 xfs_iext_destroy(ifp);
1894 } else if (nextents <= XFS_INLINE_EXTS) {
1895 xfs_iext_indirect_to_direct(ifp);
1896 xfs_iext_direct_to_inline(ifp, nextents);
1897 } else if (nextents <= XFS_LINEAR_EXTS) {
1898 xfs_iext_indirect_to_direct(ifp);
1899 } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) {
1900 xfs_iext_irec_compact_pages(ifp);
1901 }
1902}
1903
1904
1905
1906
1907void
1908xfs_iext_irec_compact_pages(
1909 xfs_ifork_t *ifp)
1910{
1911 xfs_ext_irec_t *erp, *erp_next;
1912 int erp_idx = 0;
1913 int nlists;
1914
1915 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1916 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1917 while (erp_idx < nlists - 1) {
1918 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1919 erp_next = erp + 1;
1920 if (erp_next->er_extcount <=
1921 (XFS_LINEAR_EXTS - erp->er_extcount)) {
1922 memcpy(&erp->er_extbuf[erp->er_extcount],
1923 erp_next->er_extbuf, erp_next->er_extcount *
1924 sizeof(xfs_bmbt_rec_t));
1925 erp->er_extcount += erp_next->er_extcount;
1926
1927
1928
1929
1930
1931 kmem_free(erp_next->er_extbuf);
1932 erp_next->er_extbuf = NULL;
1933 xfs_iext_irec_remove(ifp, erp_idx + 1);
1934 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1935 } else {
1936 erp_idx++;
1937 }
1938 }
1939}
1940
1941
1942
1943
1944
1945
1946
1947
1948void
1949xfs_iext_irec_update_extoffs(
1950 xfs_ifork_t *ifp,
1951 int erp_idx,
1952 int ext_diff)
1953{
1954 int i;
1955 int nlists;
1956
1957 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1958 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1959 for (i = erp_idx; i < nlists; i++) {
1960 ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff;
1961 }
1962}
1963
1964
1965
1966
1967void
1968xfs_ifork_init_cow(
1969 struct xfs_inode *ip)
1970{
1971 if (ip->i_cowfp)
1972 return;
1973
1974 ip->i_cowfp = kmem_zone_zalloc(xfs_ifork_zone,
1975 KM_SLEEP | KM_NOFS);
1976 ip->i_cowfp->if_flags = XFS_IFEXTENTS;
1977 ip->i_cformat = XFS_DINODE_FMT_EXTENTS;
1978 ip->i_cnextents = 0;
1979}
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992bool
1993xfs_iext_lookup_extent(
1994 struct xfs_inode *ip,
1995 struct xfs_ifork *ifp,
1996 xfs_fileoff_t bno,
1997 xfs_extnum_t *idxp,
1998 struct xfs_bmbt_irec *gotp)
1999{
2000 struct xfs_bmbt_rec_host *ep;
2001
2002 XFS_STATS_INC(ip->i_mount, xs_look_exlist);
2003
2004 ep = xfs_iext_bno_to_ext(ifp, bno, idxp);
2005 if (!ep)
2006 return false;
2007 xfs_bmbt_get_all(ep, gotp);
2008 return true;
2009}
2010
2011
2012
2013
2014
2015bool
2016xfs_iext_get_extent(
2017 struct xfs_ifork *ifp,
2018 xfs_extnum_t idx,
2019 struct xfs_bmbt_irec *gotp)
2020{
2021 if (idx < 0 || idx >= xfs_iext_count(ifp))
2022 return false;
2023 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), gotp);
2024 return true;
2025}
2026