1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103#include <linux/fs.h>
104#include <linux/quotaops.h>
105#include <linux/slab.h>
106#include "jfs_incore.h"
107#include "jfs_superblock.h"
108#include "jfs_filsys.h"
109#include "jfs_metapage.h"
110#include "jfs_dmap.h"
111#include "jfs_unicode.h"
112#include "jfs_debug.h"
113
114
115struct dtsplit {
116 struct metapage *mp;
117 s16 index;
118 s16 nslot;
119 struct component_name *key;
120 ddata_t *data;
121 struct pxdlist *pxdlist;
122};
123
124#define DT_PAGE(IP, MP) BT_PAGE(IP, MP, dtpage_t, i_dtroot)
125
126
127#define DT_GETPAGE(IP, BN, MP, SIZE, P, RC)\
128{\
129 BT_GETPAGE(IP, BN, MP, dtpage_t, SIZE, P, RC, i_dtroot)\
130 if (!(RC))\
131 {\
132 if (((P)->header.nextindex > (((BN)==0)?DTROOTMAXSLOT:(P)->header.maxslot)) ||\
133 ((BN) && ((P)->header.maxslot > DTPAGEMAXSLOT)))\
134 {\
135 BT_PUTPAGE(MP);\
136 jfs_error((IP)->i_sb, "DT_GETPAGE: dtree page corrupt");\
137 MP = NULL;\
138 RC = -EIO;\
139 }\
140 }\
141}
142
143
144#define DT_PUTPAGE(MP) BT_PUTPAGE(MP)
145
146#define DT_GETSEARCH(IP, LEAF, BN, MP, P, INDEX) \
147 BT_GETSEARCH(IP, LEAF, BN, MP, dtpage_t, P, INDEX, i_dtroot)
148
149
150
151
152static int dtSplitUp(tid_t tid, struct inode *ip,
153 struct dtsplit * split, struct btstack * btstack);
154
155static int dtSplitPage(tid_t tid, struct inode *ip, struct dtsplit * split,
156 struct metapage ** rmpp, dtpage_t ** rpp, pxd_t * rxdp);
157
158static int dtExtendPage(tid_t tid, struct inode *ip,
159 struct dtsplit * split, struct btstack * btstack);
160
161static int dtSplitRoot(tid_t tid, struct inode *ip,
162 struct dtsplit * split, struct metapage ** rmpp);
163
164static int dtDeleteUp(tid_t tid, struct inode *ip, struct metapage * fmp,
165 dtpage_t * fp, struct btstack * btstack);
166
167static int dtRelink(tid_t tid, struct inode *ip, dtpage_t * p);
168
169static int dtReadFirst(struct inode *ip, struct btstack * btstack);
170
171static int dtReadNext(struct inode *ip,
172 loff_t * offset, struct btstack * btstack);
173
174static int dtCompare(struct component_name * key, dtpage_t * p, int si);
175
176static int ciCompare(struct component_name * key, dtpage_t * p, int si,
177 int flag);
178
179static void dtGetKey(dtpage_t * p, int i, struct component_name * key,
180 int flag);
181
182static int ciGetLeafPrefixKey(dtpage_t * lp, int li, dtpage_t * rp,
183 int ri, struct component_name * key, int flag);
184
185static void dtInsertEntry(dtpage_t * p, int index, struct component_name * key,
186 ddata_t * data, struct dt_lock **);
187
188static void dtMoveEntry(dtpage_t * sp, int si, dtpage_t * dp,
189 struct dt_lock ** sdtlock, struct dt_lock ** ddtlock,
190 int do_index);
191
192static void dtDeleteEntry(dtpage_t * p, int fi, struct dt_lock ** dtlock);
193
194static void dtTruncateEntry(dtpage_t * p, int ti, struct dt_lock ** dtlock);
195
196static void dtLinelockFreelist(dtpage_t * p, int m, struct dt_lock ** dtlock);
197
198#define ciToUpper(c) UniStrupr((c)->name)
199
200
201
202
203
204
205
206
207
208static struct metapage *read_index_page(struct inode *inode, s64 blkno)
209{
210 int rc;
211 s64 xaddr;
212 int xflag;
213 s32 xlen;
214
215 rc = xtLookup(inode, blkno, 1, &xflag, &xaddr, &xlen, 1);
216 if (rc || (xaddr == 0))
217 return NULL;
218
219 return read_metapage(inode, xaddr, PSIZE, 1);
220}
221
222
223
224
225
226
227static struct metapage *get_index_page(struct inode *inode, s64 blkno)
228{
229 int rc;
230 s64 xaddr;
231 int xflag;
232 s32 xlen;
233
234 rc = xtLookup(inode, blkno, 1, &xflag, &xaddr, &xlen, 1);
235 if (rc || (xaddr == 0))
236 return NULL;
237
238 return get_metapage(inode, xaddr, PSIZE, 1);
239}
240
241
242
243
244
245
246
247
248
249static struct dir_table_slot *find_index(struct inode *ip, u32 index,
250 struct metapage ** mp, s64 *lblock)
251{
252 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
253 s64 blkno;
254 s64 offset;
255 int page_offset;
256 struct dir_table_slot *slot;
257 static int maxWarnings = 10;
258
259 if (index < 2) {
260 if (maxWarnings) {
261 jfs_warn("find_entry called with index = %d", index);
262 maxWarnings--;
263 }
264 return NULL;
265 }
266
267 if (index >= jfs_ip->next_index) {
268 jfs_warn("find_entry called with index >= next_index");
269 return NULL;
270 }
271
272 if (jfs_dirtable_inline(ip)) {
273
274
275
276 *mp = NULL;
277 slot = &jfs_ip->i_dirtable[index - 2];
278 } else {
279 offset = (index - 2) * sizeof(struct dir_table_slot);
280 page_offset = offset & (PSIZE - 1);
281 blkno = ((offset + 1) >> L2PSIZE) <<
282 JFS_SBI(ip->i_sb)->l2nbperpage;
283
284 if (*mp && (*lblock != blkno)) {
285 release_metapage(*mp);
286 *mp = NULL;
287 }
288 if (!(*mp)) {
289 *lblock = blkno;
290 *mp = read_index_page(ip, blkno);
291 }
292 if (!(*mp)) {
293 jfs_err("free_index: error reading directory table");
294 return NULL;
295 }
296
297 slot =
298 (struct dir_table_slot *) ((char *) (*mp)->data +
299 page_offset);
300 }
301 return slot;
302}
303
304static inline void lock_index(tid_t tid, struct inode *ip, struct metapage * mp,
305 u32 index)
306{
307 struct tlock *tlck;
308 struct linelock *llck;
309 struct lv *lv;
310
311 tlck = txLock(tid, ip, mp, tlckDATA);
312 llck = (struct linelock *) tlck->lock;
313
314 if (llck->index >= llck->maxcnt)
315 llck = txLinelock(llck);
316 lv = &llck->lv[llck->index];
317
318
319
320
321
322 lv->offset = ((index - 2) & 511) >> 1;
323 lv->length = 1;
324 llck->index++;
325}
326
327
328
329
330
331
332
333
334static u32 add_index(tid_t tid, struct inode *ip, s64 bn, int slot)
335{
336 struct super_block *sb = ip->i_sb;
337 struct jfs_sb_info *sbi = JFS_SBI(sb);
338 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
339 u64 blkno;
340 struct dir_table_slot *dirtab_slot;
341 u32 index;
342 struct linelock *llck;
343 struct lv *lv;
344 struct metapage *mp;
345 s64 offset;
346 uint page_offset;
347 struct tlock *tlck;
348 s64 xaddr;
349
350 ASSERT(DO_INDEX(ip));
351
352 if (jfs_ip->next_index < 2) {
353 jfs_warn("add_index: next_index = %d. Resetting!",
354 jfs_ip->next_index);
355 jfs_ip->next_index = 2;
356 }
357
358 index = jfs_ip->next_index++;
359
360 if (index <= MAX_INLINE_DIRTABLE_ENTRY) {
361
362
363
364 ip->i_size = (loff_t) (index - 1) << 3;
365
366
367
368
369 dirtab_slot = &jfs_ip->i_dirtable[index-2];
370 dirtab_slot->flag = DIR_INDEX_VALID;
371 dirtab_slot->slot = slot;
372 DTSaddress(dirtab_slot, bn);
373
374 set_cflag(COMMIT_Dirtable, ip);
375
376 return index;
377 }
378 if (index == (MAX_INLINE_DIRTABLE_ENTRY + 1)) {
379 struct dir_table_slot temp_table[12];
380
381
382
383
384
385 if (dquot_alloc_block(ip, sbi->nbperpage))
386 goto clean_up;
387 if (dbAlloc(ip, 0, sbi->nbperpage, &xaddr)) {
388 dquot_free_block(ip, sbi->nbperpage);
389 goto clean_up;
390 }
391
392
393
394
395
396 memcpy(temp_table, &jfs_ip->i_dirtable, sizeof(temp_table));
397
398
399
400
401 xtInitRoot(tid, ip);
402
403
404
405
406 if (xtInsert(tid, ip, 0, 0, sbi->nbperpage, &xaddr, 0)) {
407
408 jfs_warn("add_index: xtInsert failed!");
409 memcpy(&jfs_ip->i_dirtable, temp_table,
410 sizeof (temp_table));
411 dbFree(ip, xaddr, sbi->nbperpage);
412 dquot_free_block(ip, sbi->nbperpage);
413 goto clean_up;
414 }
415 ip->i_size = PSIZE;
416
417 mp = get_index_page(ip, 0);
418 if (!mp) {
419 jfs_err("add_index: get_metapage failed!");
420 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
421 memcpy(&jfs_ip->i_dirtable, temp_table,
422 sizeof (temp_table));
423 goto clean_up;
424 }
425 tlck = txLock(tid, ip, mp, tlckDATA);
426 llck = (struct linelock *) & tlck->lock;
427 ASSERT(llck->index == 0);
428 lv = &llck->lv[0];
429
430 lv->offset = 0;
431 lv->length = 6;
432 llck->index++;
433
434 memcpy(mp->data, temp_table, sizeof(temp_table));
435
436 mark_metapage_dirty(mp);
437 release_metapage(mp);
438
439
440
441
442 clear_cflag(COMMIT_Dirtable, ip);
443 }
444
445 offset = (index - 2) * sizeof(struct dir_table_slot);
446 page_offset = offset & (PSIZE - 1);
447 blkno = ((offset + 1) >> L2PSIZE) << sbi->l2nbperpage;
448 if (page_offset == 0) {
449
450
451
452 xaddr = 0;
453 if (xtInsert(tid, ip, 0, blkno, sbi->nbperpage, &xaddr, 0)) {
454 jfs_warn("add_index: xtInsert failed!");
455 goto clean_up;
456 }
457 ip->i_size += PSIZE;
458
459 if ((mp = get_index_page(ip, blkno)))
460 memset(mp->data, 0, PSIZE);
461 else
462 xtTruncate(tid, ip, offset, COMMIT_PWMAP);
463 } else
464 mp = read_index_page(ip, blkno);
465
466 if (!mp) {
467 jfs_err("add_index: get/read_metapage failed!");
468 goto clean_up;
469 }
470
471 lock_index(tid, ip, mp, index);
472
473 dirtab_slot =
474 (struct dir_table_slot *) ((char *) mp->data + page_offset);
475 dirtab_slot->flag = DIR_INDEX_VALID;
476 dirtab_slot->slot = slot;
477 DTSaddress(dirtab_slot, bn);
478
479 mark_metapage_dirty(mp);
480 release_metapage(mp);
481
482 return index;
483
484 clean_up:
485
486 jfs_ip->next_index--;
487
488 return 0;
489}
490
491
492
493
494
495
496static void free_index(tid_t tid, struct inode *ip, u32 index, u32 next)
497{
498 struct dir_table_slot *dirtab_slot;
499 s64 lblock;
500 struct metapage *mp = NULL;
501
502 dirtab_slot = find_index(ip, index, &mp, &lblock);
503
504 if (!dirtab_slot)
505 return;
506
507 dirtab_slot->flag = DIR_INDEX_FREE;
508 dirtab_slot->slot = dirtab_slot->addr1 = 0;
509 dirtab_slot->addr2 = cpu_to_le32(next);
510
511 if (mp) {
512 lock_index(tid, ip, mp, index);
513 mark_metapage_dirty(mp);
514 release_metapage(mp);
515 } else
516 set_cflag(COMMIT_Dirtable, ip);
517}
518
519
520
521
522
523
524static void modify_index(tid_t tid, struct inode *ip, u32 index, s64 bn,
525 int slot, struct metapage ** mp, s64 *lblock)
526{
527 struct dir_table_slot *dirtab_slot;
528
529 dirtab_slot = find_index(ip, index, mp, lblock);
530
531 if (!dirtab_slot)
532 return;
533
534 DTSaddress(dirtab_slot, bn);
535 dirtab_slot->slot = slot;
536
537 if (*mp) {
538 lock_index(tid, ip, *mp, index);
539 mark_metapage_dirty(*mp);
540 } else
541 set_cflag(COMMIT_Dirtable, ip);
542}
543
544
545
546
547
548
549static int read_index(struct inode *ip, u32 index,
550 struct dir_table_slot * dirtab_slot)
551{
552 s64 lblock;
553 struct metapage *mp = NULL;
554 struct dir_table_slot *slot;
555
556 slot = find_index(ip, index, &mp, &lblock);
557 if (!slot) {
558 return -EIO;
559 }
560
561 memcpy(dirtab_slot, slot, sizeof(struct dir_table_slot));
562
563 if (mp)
564 release_metapage(mp);
565
566 return 0;
567}
568
569
570
571
572
573
574
575
576
577
578
579
580int dtSearch(struct inode *ip, struct component_name * key, ino_t * data,
581 struct btstack * btstack, int flag)
582{
583 int rc = 0;
584 int cmp = 1;
585 s64 bn;
586 struct metapage *mp;
587 dtpage_t *p;
588 s8 *stbl;
589 int base, index, lim;
590 struct btframe *btsp;
591 pxd_t *pxd;
592 int psize = 288;
593 ino_t inumber;
594 struct component_name ciKey;
595 struct super_block *sb = ip->i_sb;
596
597 ciKey.name = kmalloc((JFS_NAME_MAX + 1) * sizeof(wchar_t), GFP_NOFS);
598 if (!ciKey.name) {
599 rc = -ENOMEM;
600 goto dtSearch_Exit2;
601 }
602
603
604
605 UniStrcpy(ciKey.name, key->name);
606 ciKey.namlen = key->namlen;
607
608
609 if ((JFS_SBI(sb)->mntflag & JFS_OS2) == JFS_OS2) {
610 ciToUpper(&ciKey);
611 }
612 BT_CLR(btstack);
613
614
615 btstack->nsplit = 1;
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633 for (bn = 0;;) {
634
635 DT_GETPAGE(ip, bn, mp, psize, p, rc);
636 if (rc)
637 goto dtSearch_Exit1;
638
639
640 stbl = DT_GETSTBL(p);
641
642
643
644
645 for (base = 0, lim = p->header.nextindex; lim; lim >>= 1) {
646 index = base + (lim >> 1);
647
648 if (p->header.flag & BT_LEAF) {
649
650 cmp =
651 ciCompare(&ciKey, p, stbl[index],
652 JFS_SBI(sb)->mntflag);
653 } else {
654
655
656 cmp = dtCompare(&ciKey, p, stbl[index]);
657
658
659 }
660 if (cmp == 0) {
661
662
663
664
665
666
667 if (p->header.flag & BT_LEAF) {
668 inumber = le32_to_cpu(
669 ((struct ldtentry *) & p->slot[stbl[index]])->inumber);
670
671
672
673
674 if (flag == JFS_LOOKUP) {
675 *data = inumber;
676 rc = 0;
677 goto out;
678 }
679
680
681
682
683 if (flag == JFS_CREATE) {
684 *data = inumber;
685 rc = -EEXIST;
686 goto out;
687 }
688
689
690
691
692 if ((flag == JFS_REMOVE ||
693 flag == JFS_RENAME) &&
694 *data != inumber) {
695 rc = -ESTALE;
696 goto out;
697 }
698
699
700
701
702
703 *data = inumber;
704 btsp = btstack->top;
705 btsp->bn = bn;
706 btsp->index = index;
707 btsp->mp = mp;
708
709 rc = 0;
710 goto dtSearch_Exit1;
711 }
712
713
714
715
716 goto getChild;
717 }
718
719 if (cmp > 0) {
720 base = index + 1;
721 --lim;
722 }
723 }
724
725
726
727
728
729
730
731
732
733
734
735
736
737 if (p->header.flag & BT_LEAF) {
738
739
740
741 if (flag == JFS_LOOKUP || flag == JFS_REMOVE ||
742 flag == JFS_RENAME) {
743 rc = -ENOENT;
744 goto out;
745 }
746
747
748
749
750
751
752 *data = 0;
753 btsp = btstack->top;
754 btsp->bn = bn;
755 btsp->index = base;
756 btsp->mp = mp;
757
758 rc = 0;
759 goto dtSearch_Exit1;
760 }
761
762
763
764
765
766
767
768 index = base ? base - 1 : base;
769
770
771
772
773 getChild:
774
775 if (BT_STACK_FULL(btstack)) {
776
777
778
779 jfs_error(sb, "stack overrun in dtSearch!");
780 BT_STACK_DUMP(btstack);
781 rc = -EIO;
782 goto out;
783 }
784 btstack->nsplit++;
785
786
787 BT_PUSH(btstack, bn, index);
788
789
790 pxd = (pxd_t *) & p->slot[stbl[index]];
791 bn = addressPXD(pxd);
792 psize = lengthPXD(pxd) << JFS_SBI(ip->i_sb)->l2bsize;
793
794
795 DT_PUTPAGE(mp);
796 }
797
798 out:
799 DT_PUTPAGE(mp);
800
801 dtSearch_Exit1:
802
803 kfree(ciKey.name);
804
805 dtSearch_Exit2:
806
807 return rc;
808}
809
810
811
812
813
814
815
816
817
818
819
820
821int dtInsert(tid_t tid, struct inode *ip,
822 struct component_name * name, ino_t * fsn, struct btstack * btstack)
823{
824 int rc = 0;
825 struct metapage *mp;
826 dtpage_t *p;
827 s64 bn;
828 int index;
829 struct dtsplit split;
830 ddata_t data;
831 struct dt_lock *dtlck;
832 int n;
833 struct tlock *tlck;
834 struct lv *lv;
835
836
837
838
839
840
841
842
843 DT_GETSEARCH(ip, btstack->top, bn, mp, p, index);
844
845
846
847
848 if (DO_INDEX(ip)) {
849 if (JFS_IP(ip)->next_index == DIREND) {
850 DT_PUTPAGE(mp);
851 return -EMLINK;
852 }
853 n = NDTLEAF(name->namlen);
854 data.leaf.tid = tid;
855 data.leaf.ip = ip;
856 } else {
857 n = NDTLEAF_LEGACY(name->namlen);
858 data.leaf.ip = NULL;
859 }
860 data.leaf.ino = *fsn;
861
862
863
864
865
866
867
868
869 if (n > p->header.freecnt) {
870 split.mp = mp;
871 split.index = index;
872 split.nslot = n;
873 split.key = name;
874 split.data = &data;
875 rc = dtSplitUp(tid, ip, &split, btstack);
876 return rc;
877 }
878
879
880
881
882
883
884 BT_MARK_DIRTY(mp, ip);
885
886
887
888 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
889 dtlck = (struct dt_lock *) & tlck->lock;
890 ASSERT(dtlck->index == 0);
891 lv = & dtlck->lv[0];
892
893
894 lv->offset = 0;
895 lv->length = 1;
896 dtlck->index++;
897
898 dtInsertEntry(p, index, name, &data, &dtlck);
899
900
901 if (!(p->header.flag & BT_ROOT)) {
902 if (dtlck->index >= dtlck->maxcnt)
903 dtlck = (struct dt_lock *) txLinelock(dtlck);
904 lv = & dtlck->lv[dtlck->index];
905 n = index >> L2DTSLOTSIZE;
906 lv->offset = p->header.stblindex + n;
907 lv->length =
908 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) - n + 1;
909 dtlck->index++;
910 }
911
912
913 DT_PUTPAGE(mp);
914
915 return 0;
916}
917
918
919
920
921
922
923
924
925
926
927
928
929
930static int dtSplitUp(tid_t tid,
931 struct inode *ip, struct dtsplit * split, struct btstack * btstack)
932{
933 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
934 int rc = 0;
935 struct metapage *smp;
936 dtpage_t *sp;
937 struct metapage *rmp;
938 dtpage_t *rp;
939 pxd_t rpxd;
940 struct metapage *lmp;
941 dtpage_t *lp;
942 int skip;
943 struct btframe *parent;
944 s64 xaddr, nxaddr;
945 int xlen, xsize;
946 struct pxdlist pxdlist;
947 pxd_t *pxd;
948 struct component_name key = { 0, NULL };
949 ddata_t *data = split->data;
950 int n;
951 struct dt_lock *dtlck;
952 struct tlock *tlck;
953 struct lv *lv;
954 int quota_allocation = 0;
955
956
957 smp = split->mp;
958 sp = DT_PAGE(ip, smp);
959
960 key.name = kmalloc((JFS_NAME_MAX + 2) * sizeof(wchar_t), GFP_NOFS);
961 if (!key.name) {
962 DT_PUTPAGE(smp);
963 rc = -ENOMEM;
964 goto dtSplitUp_Exit;
965 }
966
967
968
969
970
971
972
973
974
975
976 if (sp->header.flag & BT_ROOT) {
977
978
979
980 xlen = 1;
981 n = sbi->bsize >> L2DTSLOTSIZE;
982 n -= (n + 31) >> L2DTSLOTSIZE;
983 n -= DTROOTMAXSLOT - sp->header.freecnt;
984 if (n <= split->nslot)
985 xlen++;
986 if ((rc = dbAlloc(ip, 0, (s64) xlen, &xaddr))) {
987 DT_PUTPAGE(smp);
988 goto freeKeyName;
989 }
990
991 pxdlist.maxnpxd = 1;
992 pxdlist.npxd = 0;
993 pxd = &pxdlist.pxd[0];
994 PXDaddress(pxd, xaddr);
995 PXDlength(pxd, xlen);
996 split->pxdlist = &pxdlist;
997 rc = dtSplitRoot(tid, ip, split, &rmp);
998
999 if (rc)
1000 dbFree(ip, xaddr, xlen);
1001 else
1002 DT_PUTPAGE(rmp);
1003
1004 DT_PUTPAGE(smp);
1005
1006 if (!DO_INDEX(ip))
1007 ip->i_size = xlen << sbi->l2bsize;
1008
1009 goto freeKeyName;
1010 }
1011
1012
1013
1014
1015
1016
1017
1018 pxd = &sp->header.self;
1019 xlen = lengthPXD(pxd);
1020 xsize = xlen << sbi->l2bsize;
1021 if (xsize < PSIZE) {
1022 xaddr = addressPXD(pxd);
1023 n = xsize >> L2DTSLOTSIZE;
1024 n -= (n + 31) >> L2DTSLOTSIZE;
1025 if ((n + sp->header.freecnt) <= split->nslot)
1026 n = xlen + (xlen << 1);
1027 else
1028 n = xlen;
1029
1030
1031 rc = dquot_alloc_block(ip, n);
1032 if (rc)
1033 goto extendOut;
1034 quota_allocation += n;
1035
1036 if ((rc = dbReAlloc(sbi->ipbmap, xaddr, (s64) xlen,
1037 (s64) n, &nxaddr)))
1038 goto extendOut;
1039
1040 pxdlist.maxnpxd = 1;
1041 pxdlist.npxd = 0;
1042 pxd = &pxdlist.pxd[0];
1043 PXDaddress(pxd, nxaddr)
1044 PXDlength(pxd, xlen + n);
1045 split->pxdlist = &pxdlist;
1046 if ((rc = dtExtendPage(tid, ip, split, btstack))) {
1047 nxaddr = addressPXD(pxd);
1048 if (xaddr != nxaddr) {
1049
1050 xlen = lengthPXD(pxd);
1051 dbFree(ip, nxaddr, (s64) xlen);
1052 } else {
1053
1054 xlen = lengthPXD(pxd) - n;
1055 xaddr = addressPXD(pxd) + xlen;
1056 dbFree(ip, xaddr, (s64) n);
1057 }
1058 } else if (!DO_INDEX(ip))
1059 ip->i_size = lengthPXD(pxd) << sbi->l2bsize;
1060
1061
1062 extendOut:
1063 DT_PUTPAGE(smp);
1064 goto freeKeyName;
1065 }
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078 n = btstack->nsplit;
1079 pxdlist.maxnpxd = pxdlist.npxd = 0;
1080 xlen = sbi->nbperpage;
1081 for (pxd = pxdlist.pxd; n > 0; n--, pxd++) {
1082 if ((rc = dbAlloc(ip, 0, (s64) xlen, &xaddr)) == 0) {
1083 PXDaddress(pxd, xaddr);
1084 PXDlength(pxd, xlen);
1085 pxdlist.maxnpxd++;
1086 continue;
1087 }
1088
1089 DT_PUTPAGE(smp);
1090
1091
1092 goto splitOut;
1093 }
1094
1095 split->pxdlist = &pxdlist;
1096 if ((rc = dtSplitPage(tid, ip, split, &rmp, &rp, &rpxd))) {
1097 DT_PUTPAGE(smp);
1098
1099
1100 goto splitOut;
1101 }
1102
1103 if (!DO_INDEX(ip))
1104 ip->i_size += PSIZE;
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127 while ((parent = BT_POP(btstack)) != NULL) {
1128
1129
1130
1131 lmp = smp;
1132 lp = sp;
1133
1134
1135
1136
1137
1138 DT_GETPAGE(ip, parent->bn, smp, PSIZE, sp, rc);
1139 if (rc) {
1140 DT_PUTPAGE(lmp);
1141 DT_PUTPAGE(rmp);
1142 goto splitOut;
1143 }
1144
1145
1146
1147
1148
1149 skip = parent->index + 1;
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176 switch (rp->header.flag & BT_TYPE) {
1177 case BT_LEAF:
1178
1179
1180
1181
1182
1183 if ((sp->header.flag & BT_ROOT && skip > 1) ||
1184 sp->header.prev != 0 || skip > 1) {
1185
1186 rc = ciGetLeafPrefixKey(lp,
1187 lp->header.nextindex-1,
1188 rp, 0, &key,
1189 sbi->mntflag);
1190 if (rc) {
1191 DT_PUTPAGE(lmp);
1192 DT_PUTPAGE(rmp);
1193 DT_PUTPAGE(smp);
1194 goto splitOut;
1195 }
1196 } else {
1197
1198
1199
1200
1201 dtGetKey(rp, 0, &key, sbi->mntflag);
1202 key.name[key.namlen] = 0;
1203
1204 if ((sbi->mntflag & JFS_OS2) == JFS_OS2)
1205 ciToUpper(&key);
1206 }
1207
1208 n = NDTINTERNAL(key.namlen);
1209 break;
1210
1211 case BT_INTERNAL:
1212 dtGetKey(rp, 0, &key, sbi->mntflag);
1213 n = NDTINTERNAL(key.namlen);
1214 break;
1215
1216 default:
1217 jfs_err("dtSplitUp(): UFO!");
1218 break;
1219 }
1220
1221
1222 DT_PUTPAGE(lmp);
1223
1224
1225
1226
1227 data->xd = rpxd;
1228
1229
1230
1231
1232 if (n > sp->header.freecnt) {
1233
1234 split->mp = smp;
1235 split->index = skip;
1236 split->nslot = n;
1237 split->key = &key;
1238
1239
1240
1241 DT_PUTPAGE(rmp);
1242
1243
1244
1245
1246
1247 rc = (sp->header.flag & BT_ROOT) ?
1248 dtSplitRoot(tid, ip, split, &rmp) :
1249 dtSplitPage(tid, ip, split, &rmp, &rp, &rpxd);
1250 if (rc) {
1251 DT_PUTPAGE(smp);
1252 goto splitOut;
1253 }
1254
1255
1256 }
1257
1258
1259
1260 else {
1261 BT_MARK_DIRTY(smp, ip);
1262
1263
1264
1265 tlck = txLock(tid, ip, smp, tlckDTREE | tlckENTRY);
1266 dtlck = (struct dt_lock *) & tlck->lock;
1267 ASSERT(dtlck->index == 0);
1268 lv = & dtlck->lv[0];
1269
1270
1271 lv->offset = 0;
1272 lv->length = 1;
1273 dtlck->index++;
1274
1275
1276 if (!(sp->header.flag & BT_ROOT)) {
1277 lv++;
1278 n = skip >> L2DTSLOTSIZE;
1279 lv->offset = sp->header.stblindex + n;
1280 lv->length =
1281 ((sp->header.nextindex -
1282 1) >> L2DTSLOTSIZE) - n + 1;
1283 dtlck->index++;
1284 }
1285
1286 dtInsertEntry(sp, skip, &key, data, &dtlck);
1287
1288
1289 break;
1290 }
1291 }
1292
1293
1294 DT_PUTPAGE(smp);
1295 DT_PUTPAGE(rmp);
1296
1297
1298
1299
1300 splitOut:
1301 n = pxdlist.npxd;
1302 pxd = &pxdlist.pxd[n];
1303 for (; n < pxdlist.maxnpxd; n++, pxd++)
1304 dbFree(ip, addressPXD(pxd), (s64) lengthPXD(pxd));
1305
1306 freeKeyName:
1307 kfree(key.name);
1308
1309
1310 if (rc && quota_allocation)
1311 dquot_free_block(ip, quota_allocation);
1312
1313 dtSplitUp_Exit:
1314
1315 return rc;
1316}
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330static int dtSplitPage(tid_t tid, struct inode *ip, struct dtsplit * split,
1331 struct metapage ** rmpp, dtpage_t ** rpp, pxd_t * rpxdp)
1332{
1333 int rc = 0;
1334 struct metapage *smp;
1335 dtpage_t *sp;
1336 struct metapage *rmp;
1337 dtpage_t *rp;
1338 s64 rbn;
1339 struct metapage *mp;
1340 dtpage_t *p;
1341 s64 nextbn;
1342 struct pxdlist *pxdlist;
1343 pxd_t *pxd;
1344 int skip, nextindex, half, left, nxt, off, si;
1345 struct ldtentry *ldtentry;
1346 struct idtentry *idtentry;
1347 u8 *stbl;
1348 struct dtslot *f;
1349 int fsi, stblsize;
1350 int n;
1351 struct dt_lock *sdtlck, *rdtlck;
1352 struct tlock *tlck;
1353 struct dt_lock *dtlck;
1354 struct lv *slv, *rlv, *lv;
1355
1356
1357 smp = split->mp;
1358 sp = DT_PAGE(ip, smp);
1359
1360
1361
1362
1363 pxdlist = split->pxdlist;
1364 pxd = &pxdlist->pxd[pxdlist->npxd];
1365 pxdlist->npxd++;
1366 rbn = addressPXD(pxd);
1367 rmp = get_metapage(ip, rbn, PSIZE, 1);
1368 if (rmp == NULL)
1369 return -EIO;
1370
1371
1372 rc = dquot_alloc_block(ip, lengthPXD(pxd));
1373 if (rc) {
1374 release_metapage(rmp);
1375 return rc;
1376 }
1377
1378 jfs_info("dtSplitPage: ip:0x%p smp:0x%p rmp:0x%p", ip, smp, rmp);
1379
1380 BT_MARK_DIRTY(rmp, ip);
1381
1382
1383
1384 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckNEW);
1385 rdtlck = (struct dt_lock *) & tlck->lock;
1386
1387 rp = (dtpage_t *) rmp->data;
1388 *rpp = rp;
1389 rp->header.self = *pxd;
1390
1391 BT_MARK_DIRTY(smp, ip);
1392
1393
1394
1395
1396
1397 tlck = txLock(tid, ip, smp, tlckDTREE | tlckENTRY);
1398 sdtlck = (struct dt_lock *) & tlck->lock;
1399
1400
1401 ASSERT(sdtlck->index == 0);
1402 slv = & sdtlck->lv[0];
1403 slv->offset = 0;
1404 slv->length = 1;
1405 sdtlck->index++;
1406
1407
1408
1409
1410 nextbn = le64_to_cpu(sp->header.next);
1411 rp->header.next = cpu_to_le64(nextbn);
1412 rp->header.prev = cpu_to_le64(addressPXD(&sp->header.self));
1413 sp->header.next = cpu_to_le64(rbn);
1414
1415
1416
1417
1418 rp->header.flag = sp->header.flag;
1419
1420
1421 rp->header.nextindex = 0;
1422 rp->header.stblindex = 1;
1423
1424 n = PSIZE >> L2DTSLOTSIZE;
1425 rp->header.maxslot = n;
1426 stblsize = (n + 31) >> L2DTSLOTSIZE;
1427
1428
1429 fsi = rp->header.stblindex + stblsize;
1430 rp->header.freelist = fsi;
1431 rp->header.freecnt = rp->header.maxslot - fsi;
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446 if (nextbn == 0 && split->index == sp->header.nextindex) {
1447
1448 rlv = & rdtlck->lv[rdtlck->index];
1449 rlv->offset = 0;
1450 rlv->length = 2;
1451 rdtlck->index++;
1452
1453
1454
1455
1456 f = &rp->slot[fsi];
1457 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1458 f->next = fsi;
1459 f->next = -1;
1460
1461
1462 dtInsertEntry(rp, 0, split->key, split->data, &rdtlck);
1463
1464 goto out;
1465 }
1466
1467
1468
1469
1470
1471
1472
1473
1474 if (nextbn != 0) {
1475 DT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
1476 if (rc) {
1477 discard_metapage(rmp);
1478 return rc;
1479 }
1480
1481 BT_MARK_DIRTY(mp, ip);
1482
1483
1484
1485 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
1486 jfs_info("dtSplitPage: tlck = 0x%p, ip = 0x%p, mp=0x%p",
1487 tlck, ip, mp);
1488 dtlck = (struct dt_lock *) & tlck->lock;
1489
1490
1491 lv = & dtlck->lv[dtlck->index];
1492 lv->offset = 0;
1493 lv->length = 1;
1494 dtlck->index++;
1495
1496 p->header.prev = cpu_to_le64(rbn);
1497
1498 DT_PUTPAGE(mp);
1499 }
1500
1501
1502
1503
1504 skip = split->index;
1505 half = (PSIZE >> L2DTSLOTSIZE) >> 1;
1506 left = 0;
1507
1508
1509
1510
1511
1512
1513
1514 stbl = (u8 *) & sp->slot[sp->header.stblindex];
1515 nextindex = sp->header.nextindex;
1516 for (nxt = off = 0; nxt < nextindex; ++off) {
1517 if (off == skip)
1518
1519 n = split->nslot;
1520 else {
1521 si = stbl[nxt];
1522 switch (sp->header.flag & BT_TYPE) {
1523 case BT_LEAF:
1524 ldtentry = (struct ldtentry *) & sp->slot[si];
1525 if (DO_INDEX(ip))
1526 n = NDTLEAF(ldtentry->namlen);
1527 else
1528 n = NDTLEAF_LEGACY(ldtentry->
1529 namlen);
1530 break;
1531
1532 case BT_INTERNAL:
1533 idtentry = (struct idtentry *) & sp->slot[si];
1534 n = NDTINTERNAL(idtentry->namlen);
1535 break;
1536
1537 default:
1538 break;
1539 }
1540
1541 ++nxt;
1542 }
1543
1544 left += n;
1545 if (left >= half)
1546 break;
1547 }
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560 rlv = & rdtlck->lv[rdtlck->index];
1561 rlv->offset = 0;
1562 rlv->length = 5;
1563 rdtlck->index++;
1564
1565 dtMoveEntry(sp, nxt, rp, &sdtlck, &rdtlck, DO_INDEX(ip));
1566
1567 sp->header.nextindex = nxt;
1568
1569
1570
1571
1572 fsi = rp->header.freelist;
1573 f = &rp->slot[fsi];
1574 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1575 f->next = fsi;
1576 f->next = -1;
1577
1578
1579
1580
1581 if ((rp->header.flag & BT_LEAF) && DO_INDEX(ip)) {
1582 s64 lblock;
1583
1584 mp = NULL;
1585 stbl = DT_GETSTBL(rp);
1586 for (n = 0; n < rp->header.nextindex; n++) {
1587 ldtentry = (struct ldtentry *) & rp->slot[stbl[n]];
1588 modify_index(tid, ip, le32_to_cpu(ldtentry->index),
1589 rbn, n, &mp, &lblock);
1590 }
1591 if (mp)
1592 release_metapage(mp);
1593 }
1594
1595
1596
1597
1598 if (skip <= off) {
1599
1600 dtInsertEntry(sp, skip, split->key, split->data, &sdtlck);
1601
1602
1603 if (sdtlck->index >= sdtlck->maxcnt)
1604 sdtlck = (struct dt_lock *) txLinelock(sdtlck);
1605 slv = & sdtlck->lv[sdtlck->index];
1606 n = skip >> L2DTSLOTSIZE;
1607 slv->offset = sp->header.stblindex + n;
1608 slv->length =
1609 ((sp->header.nextindex - 1) >> L2DTSLOTSIZE) - n + 1;
1610 sdtlck->index++;
1611 }
1612
1613
1614
1615 else {
1616
1617 skip -= nxt;
1618
1619
1620 dtInsertEntry(rp, skip, split->key, split->data, &rdtlck);
1621 }
1622
1623 out:
1624 *rmpp = rmp;
1625 *rpxdp = *pxd;
1626
1627 return rc;
1628}
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642static int dtExtendPage(tid_t tid,
1643 struct inode *ip, struct dtsplit * split, struct btstack * btstack)
1644{
1645 struct super_block *sb = ip->i_sb;
1646 int rc;
1647 struct metapage *smp, *pmp, *mp;
1648 dtpage_t *sp, *pp;
1649 struct pxdlist *pxdlist;
1650 pxd_t *pxd, *tpxd;
1651 int xlen, xsize;
1652 int newstblindex, newstblsize;
1653 int oldstblindex, oldstblsize;
1654 int fsi, last;
1655 struct dtslot *f;
1656 struct btframe *parent;
1657 int n;
1658 struct dt_lock *dtlck;
1659 s64 xaddr, txaddr;
1660 struct tlock *tlck;
1661 struct pxd_lock *pxdlock;
1662 struct lv *lv;
1663 uint type;
1664 struct ldtentry *ldtentry;
1665 u8 *stbl;
1666
1667
1668 smp = split->mp;
1669 sp = DT_PAGE(ip, smp);
1670
1671
1672 parent = BT_POP(btstack);
1673 DT_GETPAGE(ip, parent->bn, pmp, PSIZE, pp, rc);
1674 if (rc)
1675 return (rc);
1676
1677
1678
1679
1680 pxdlist = split->pxdlist;
1681 pxd = &pxdlist->pxd[pxdlist->npxd];
1682 pxdlist->npxd++;
1683
1684 xaddr = addressPXD(pxd);
1685 tpxd = &sp->header.self;
1686 txaddr = addressPXD(tpxd);
1687
1688 if (xaddr == txaddr) {
1689 type = tlckEXTEND;
1690 }
1691
1692 else {
1693 type = tlckNEW;
1694
1695
1696 tlck = txMaplock(tid, ip, tlckDTREE | tlckRELOCATE);
1697 pxdlock = (struct pxd_lock *) & tlck->lock;
1698 pxdlock->flag = mlckFREEPXD;
1699 pxdlock->pxd = sp->header.self;
1700 pxdlock->index = 1;
1701
1702
1703
1704
1705 if (DO_INDEX(ip)) {
1706 s64 lblock;
1707
1708 mp = NULL;
1709 stbl = DT_GETSTBL(sp);
1710 for (n = 0; n < sp->header.nextindex; n++) {
1711 ldtentry =
1712 (struct ldtentry *) & sp->slot[stbl[n]];
1713 modify_index(tid, ip,
1714 le32_to_cpu(ldtentry->index),
1715 xaddr, n, &mp, &lblock);
1716 }
1717 if (mp)
1718 release_metapage(mp);
1719 }
1720 }
1721
1722
1723
1724
1725 sp->header.self = *pxd;
1726
1727 jfs_info("dtExtendPage: ip:0x%p smp:0x%p sp:0x%p", ip, smp, sp);
1728
1729 BT_MARK_DIRTY(smp, ip);
1730
1731
1732
1733 tlck = txLock(tid, ip, smp, tlckDTREE | type);
1734 dtlck = (struct dt_lock *) & tlck->lock;
1735 lv = & dtlck->lv[0];
1736
1737
1738 xlen = lengthPXD(pxd);
1739 xsize = xlen << JFS_SBI(sb)->l2bsize;
1740
1741
1742
1743
1744 oldstblindex = sp->header.stblindex;
1745 oldstblsize = (sp->header.maxslot + 31) >> L2DTSLOTSIZE;
1746 newstblindex = sp->header.maxslot;
1747 n = xsize >> L2DTSLOTSIZE;
1748 newstblsize = (n + 31) >> L2DTSLOTSIZE;
1749 memcpy(&sp->slot[newstblindex], &sp->slot[oldstblindex],
1750 sp->header.nextindex);
1751
1752
1753
1754
1755 if (type == tlckEXTEND) {
1756
1757 lv->offset = 0;
1758 lv->length = 1;
1759 dtlck->index++;
1760 lv++;
1761
1762
1763 lv->offset = newstblindex;
1764 lv->length = newstblsize;
1765 }
1766
1767
1768
1769 else {
1770 lv->offset = 0;
1771 lv->length = sp->header.maxslot + newstblsize;
1772 }
1773
1774 dtlck->index++;
1775
1776 sp->header.maxslot = n;
1777 sp->header.stblindex = newstblindex;
1778
1779
1780
1781
1782
1783 fsi = oldstblindex;
1784 f = &sp->slot[fsi];
1785 last = sp->header.freelist;
1786 for (n = 0; n < oldstblsize; n++, fsi++, f++) {
1787 f->next = last;
1788 last = fsi;
1789 }
1790 sp->header.freelist = last;
1791 sp->header.freecnt += oldstblsize;
1792
1793
1794
1795
1796
1797 fsi = n = newstblindex + newstblsize;
1798 f = &sp->slot[fsi];
1799 for (fsi++; fsi < sp->header.maxslot; f++, fsi++)
1800 f->next = fsi;
1801 f->next = -1;
1802
1803
1804 fsi = sp->header.freelist;
1805 if (fsi == -1)
1806 sp->header.freelist = n;
1807 else {
1808 do {
1809 f = &sp->slot[fsi];
1810 fsi = f->next;
1811 } while (fsi != -1);
1812
1813 f->next = n;
1814 }
1815
1816 sp->header.freecnt += sp->header.maxslot - n;
1817
1818
1819
1820
1821 dtInsertEntry(sp, split->index, split->key, split->data, &dtlck);
1822
1823 BT_MARK_DIRTY(pmp, ip);
1824
1825
1826
1827 if (type == tlckEXTEND) {
1828 n = sp->header.maxslot >> 2;
1829 if (sp->header.freelist < n)
1830 dtLinelockFreelist(sp, n, &dtlck);
1831 }
1832
1833
1834
1835
1836
1837
1838
1839 tlck = txLock(tid, ip, pmp, tlckDTREE | tlckENTRY);
1840 dtlck = (struct dt_lock *) & tlck->lock;
1841 lv = & dtlck->lv[dtlck->index];
1842
1843
1844 lv->offset = 1;
1845 lv->length = 1;
1846 dtlck->index++;
1847
1848
1849 tpxd = (pxd_t *) & pp->slot[1];
1850 *tpxd = *pxd;
1851
1852 DT_PUTPAGE(pmp);
1853 return 0;
1854}
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875static int dtSplitRoot(tid_t tid,
1876 struct inode *ip, struct dtsplit * split, struct metapage ** rmpp)
1877{
1878 struct super_block *sb = ip->i_sb;
1879 struct metapage *smp;
1880 dtroot_t *sp;
1881 struct metapage *rmp;
1882 dtpage_t *rp;
1883 s64 rbn;
1884 int xlen;
1885 int xsize;
1886 struct dtslot *f;
1887 s8 *stbl;
1888 int fsi, stblsize, n;
1889 struct idtentry *s;
1890 pxd_t *ppxd;
1891 struct pxdlist *pxdlist;
1892 pxd_t *pxd;
1893 struct dt_lock *dtlck;
1894 struct tlock *tlck;
1895 struct lv *lv;
1896 int rc;
1897
1898
1899 smp = split->mp;
1900 sp = &JFS_IP(ip)->i_dtroot;
1901
1902
1903
1904
1905
1906
1907
1908 pxdlist = split->pxdlist;
1909 pxd = &pxdlist->pxd[pxdlist->npxd];
1910 pxdlist->npxd++;
1911 rbn = addressPXD(pxd);
1912 xlen = lengthPXD(pxd);
1913 xsize = xlen << JFS_SBI(sb)->l2bsize;
1914 rmp = get_metapage(ip, rbn, xsize, 1);
1915 if (!rmp)
1916 return -EIO;
1917
1918 rp = rmp->data;
1919
1920
1921 rc = dquot_alloc_block(ip, lengthPXD(pxd));
1922 if (rc) {
1923 release_metapage(rmp);
1924 return rc;
1925 }
1926
1927 BT_MARK_DIRTY(rmp, ip);
1928
1929
1930
1931 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckNEW);
1932 dtlck = (struct dt_lock *) & tlck->lock;
1933
1934 rp->header.flag =
1935 (sp->header.flag & BT_LEAF) ? BT_LEAF : BT_INTERNAL;
1936 rp->header.self = *pxd;
1937
1938
1939 rp->header.next = 0;
1940 rp->header.prev = 0;
1941
1942
1943
1944
1945
1946 ASSERT(dtlck->index == 0);
1947 lv = & dtlck->lv[0];
1948 lv->offset = 0;
1949 lv->length = 10;
1950 dtlck->index++;
1951
1952 n = xsize >> L2DTSLOTSIZE;
1953 rp->header.maxslot = n;
1954 stblsize = (n + 31) >> L2DTSLOTSIZE;
1955
1956
1957 rp->header.stblindex = DTROOTMAXSLOT;
1958 stbl = (s8 *) & rp->slot[DTROOTMAXSLOT];
1959 memcpy(stbl, sp->header.stbl, sp->header.nextindex);
1960 rp->header.nextindex = sp->header.nextindex;
1961
1962
1963 memcpy(&rp->slot[1], &sp->slot[1], IDATASIZE);
1964
1965
1966
1967
1968
1969 fsi = n = DTROOTMAXSLOT + stblsize;
1970 f = &rp->slot[fsi];
1971 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1972 f->next = fsi;
1973 f->next = -1;
1974
1975
1976 fsi = sp->header.freelist;
1977 if (fsi == -1)
1978 rp->header.freelist = n;
1979 else {
1980 rp->header.freelist = fsi;
1981
1982 do {
1983 f = &rp->slot[fsi];
1984 fsi = f->next;
1985 } while (fsi != -1);
1986
1987 f->next = n;
1988 }
1989
1990 rp->header.freecnt = sp->header.freecnt + rp->header.maxslot - n;
1991
1992
1993
1994
1995 if ((rp->header.flag & BT_LEAF) && DO_INDEX(ip)) {
1996 s64 lblock;
1997 struct metapage *mp = NULL;
1998 struct ldtentry *ldtentry;
1999
2000 stbl = DT_GETSTBL(rp);
2001 for (n = 0; n < rp->header.nextindex; n++) {
2002 ldtentry = (struct ldtentry *) & rp->slot[stbl[n]];
2003 modify_index(tid, ip, le32_to_cpu(ldtentry->index),
2004 rbn, n, &mp, &lblock);
2005 }
2006 if (mp)
2007 release_metapage(mp);
2008 }
2009
2010
2011
2012
2013 dtInsertEntry(rp, split->index, split->key, split->data, &dtlck);
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024 BT_MARK_DIRTY(smp, ip);
2025
2026
2027
2028 tlck = txLock(tid, ip, smp, tlckDTREE | tlckNEW | tlckBTROOT);
2029 dtlck = (struct dt_lock *) & tlck->lock;
2030
2031
2032 ASSERT(dtlck->index == 0);
2033 lv = & dtlck->lv[0];
2034 lv->offset = 0;
2035 lv->length = DTROOTMAXSLOT;
2036 dtlck->index++;
2037
2038
2039 if (sp->header.flag & BT_LEAF) {
2040 sp->header.flag &= ~BT_LEAF;
2041 sp->header.flag |= BT_INTERNAL;
2042 }
2043
2044
2045 s = (struct idtentry *) & sp->slot[DTENTRYSTART];
2046 ppxd = (pxd_t *) s;
2047 *ppxd = *pxd;
2048 s->next = -1;
2049 s->namlen = 0;
2050
2051 stbl = sp->header.stbl;
2052 stbl[0] = DTENTRYSTART;
2053 sp->header.nextindex = 1;
2054
2055
2056 fsi = DTENTRYSTART + 1;
2057 f = &sp->slot[fsi];
2058
2059
2060 for (fsi++; fsi < DTROOTMAXSLOT; f++, fsi++)
2061 f->next = fsi;
2062 f->next = -1;
2063
2064 sp->header.freelist = DTENTRYSTART + 1;
2065 sp->header.freecnt = DTROOTMAXSLOT - (DTENTRYSTART + 1);
2066
2067 *rmpp = rmp;
2068
2069 return 0;
2070}
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082int dtDelete(tid_t tid,
2083 struct inode *ip, struct component_name * key, ino_t * ino, int flag)
2084{
2085 int rc = 0;
2086 s64 bn;
2087 struct metapage *mp, *imp;
2088 dtpage_t *p;
2089 int index;
2090 struct btstack btstack;
2091 struct dt_lock *dtlck;
2092 struct tlock *tlck;
2093 struct lv *lv;
2094 int i;
2095 struct ldtentry *ldtentry;
2096 u8 *stbl;
2097 u32 table_index, next_index;
2098 struct metapage *nmp;
2099 dtpage_t *np;
2100
2101
2102
2103
2104
2105
2106 if ((rc = dtSearch(ip, key, ino, &btstack, flag)))
2107 return rc;
2108
2109
2110 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
2111
2112
2113
2114
2115
2116
2117 if (DO_INDEX(ip)) {
2118 stbl = DT_GETSTBL(p);
2119 ldtentry = (struct ldtentry *) & p->slot[stbl[index]];
2120 table_index = le32_to_cpu(ldtentry->index);
2121 if (index == (p->header.nextindex - 1)) {
2122
2123
2124
2125 if ((p->header.flag & BT_ROOT)
2126 || (p->header.next == 0))
2127 next_index = -1;
2128 else {
2129
2130 DT_GETPAGE(ip, le64_to_cpu(p->header.next),
2131 nmp, PSIZE, np, rc);
2132 if (rc)
2133 next_index = -1;
2134 else {
2135 stbl = DT_GETSTBL(np);
2136 ldtentry =
2137 (struct ldtentry *) & np->
2138 slot[stbl[0]];
2139 next_index =
2140 le32_to_cpu(ldtentry->index);
2141 DT_PUTPAGE(nmp);
2142 }
2143 }
2144 } else {
2145 ldtentry =
2146 (struct ldtentry *) & p->slot[stbl[index + 1]];
2147 next_index = le32_to_cpu(ldtentry->index);
2148 }
2149 free_index(tid, ip, table_index, next_index);
2150 }
2151
2152
2153
2154 if (p->header.nextindex == 1) {
2155
2156 rc = dtDeleteUp(tid, ip, mp, p, &btstack);
2157 }
2158
2159
2160
2161
2162
2163 else {
2164 BT_MARK_DIRTY(mp, ip);
2165
2166
2167
2168 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
2169 dtlck = (struct dt_lock *) & tlck->lock;
2170
2171
2172
2173
2174
2175
2176
2177
2178 if (dtlck->index >= dtlck->maxcnt)
2179 dtlck = (struct dt_lock *) txLinelock(dtlck);
2180 lv = & dtlck->lv[dtlck->index];
2181 lv->offset = 0;
2182 lv->length = 1;
2183 dtlck->index++;
2184
2185
2186 if (!(p->header.flag & BT_ROOT)) {
2187 if (dtlck->index >= dtlck->maxcnt)
2188 dtlck = (struct dt_lock *) txLinelock(dtlck);
2189 lv = & dtlck->lv[dtlck->index];
2190 i = index >> L2DTSLOTSIZE;
2191 lv->offset = p->header.stblindex + i;
2192 lv->length =
2193 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) -
2194 i + 1;
2195 dtlck->index++;
2196 }
2197
2198
2199 dtDeleteEntry(p, index, &dtlck);
2200
2201
2202
2203
2204 if (DO_INDEX(ip) && index < p->header.nextindex) {
2205 s64 lblock;
2206
2207 imp = NULL;
2208 stbl = DT_GETSTBL(p);
2209 for (i = index; i < p->header.nextindex; i++) {
2210 ldtentry =
2211 (struct ldtentry *) & p->slot[stbl[i]];
2212 modify_index(tid, ip,
2213 le32_to_cpu(ldtentry->index),
2214 bn, i, &imp, &lblock);
2215 }
2216 if (imp)
2217 release_metapage(imp);
2218 }
2219
2220 DT_PUTPAGE(mp);
2221 }
2222
2223 return rc;
2224}
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237static int dtDeleteUp(tid_t tid, struct inode *ip,
2238 struct metapage * fmp, dtpage_t * fp, struct btstack * btstack)
2239{
2240 int rc = 0;
2241 struct metapage *mp;
2242 dtpage_t *p;
2243 int index, nextindex;
2244 int xlen;
2245 struct btframe *parent;
2246 struct dt_lock *dtlck;
2247 struct tlock *tlck;
2248 struct lv *lv;
2249 struct pxd_lock *pxdlock;
2250 int i;
2251
2252
2253
2254
2255 if (BT_IS_ROOT(fmp)) {
2256
2257
2258
2259
2260
2261 dtInitRoot(tid, ip, PARENT(ip));
2262
2263 DT_PUTPAGE(fmp);
2264
2265 return 0;
2266 }
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278 tlck = txMaplock(tid, ip, tlckDTREE | tlckFREE);
2279 pxdlock = (struct pxd_lock *) & tlck->lock;
2280 pxdlock->flag = mlckFREEPXD;
2281 pxdlock->pxd = fp->header.self;
2282 pxdlock->index = 1;
2283
2284
2285 if ((rc = dtRelink(tid, ip, fp))) {
2286 BT_PUTPAGE(fmp);
2287 return rc;
2288 }
2289
2290 xlen = lengthPXD(&fp->header.self);
2291
2292
2293 dquot_free_block(ip, xlen);
2294
2295
2296 discard_metapage(fmp);
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306 while ((parent = BT_POP(btstack)) != NULL) {
2307
2308 DT_GETPAGE(ip, parent->bn, mp, PSIZE, p, rc);
2309 if (rc)
2310 return rc;
2311
2312
2313
2314
2315 index = parent->index;
2316
2317
2318
2319
2320 nextindex = p->header.nextindex;
2321
2322
2323
2324
2325
2326
2327 if (nextindex == 1) {
2328
2329
2330
2331 if (p->header.flag & BT_ROOT) {
2332
2333
2334
2335
2336
2337 dtInitRoot(tid, ip, PARENT(ip));
2338
2339 DT_PUTPAGE(mp);
2340
2341 return 0;
2342 }
2343
2344
2345
2346 else {
2347
2348
2349
2350
2351
2352 tlck =
2353 txMaplock(tid, ip,
2354 tlckDTREE | tlckFREE);
2355 pxdlock = (struct pxd_lock *) & tlck->lock;
2356 pxdlock->flag = mlckFREEPXD;
2357 pxdlock->pxd = p->header.self;
2358 pxdlock->index = 1;
2359
2360
2361 if ((rc = dtRelink(tid, ip, p))) {
2362 DT_PUTPAGE(mp);
2363 return rc;
2364 }
2365
2366 xlen = lengthPXD(&p->header.self);
2367
2368
2369 dquot_free_block(ip, xlen);
2370
2371
2372 discard_metapage(mp);
2373
2374
2375 continue;
2376 }
2377 }
2378
2379
2380
2381
2382
2383
2384 BT_MARK_DIRTY(mp, ip);
2385
2386
2387
2388
2389
2390 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
2391 dtlck = (struct dt_lock *) & tlck->lock;
2392
2393
2394 if (dtlck->index >= dtlck->maxcnt)
2395 dtlck = (struct dt_lock *) txLinelock(dtlck);
2396 lv = & dtlck->lv[dtlck->index];
2397 lv->offset = 0;
2398 lv->length = 1;
2399 dtlck->index++;
2400
2401
2402 if (!(p->header.flag & BT_ROOT)) {
2403 if (dtlck->index < dtlck->maxcnt)
2404 lv++;
2405 else {
2406 dtlck = (struct dt_lock *) txLinelock(dtlck);
2407 lv = & dtlck->lv[0];
2408 }
2409 i = index >> L2DTSLOTSIZE;
2410 lv->offset = p->header.stblindex + i;
2411 lv->length =
2412 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) -
2413 i + 1;
2414 dtlck->index++;
2415 }
2416
2417
2418 dtDeleteEntry(p, index, &dtlck);
2419
2420
2421 if (index == 0 &&
2422 ((p->header.flag & BT_ROOT) || p->header.prev == 0))
2423 dtTruncateEntry(p, 0, &dtlck);
2424
2425
2426 DT_PUTPAGE(mp);
2427
2428
2429 break;
2430 }
2431
2432 if (!DO_INDEX(ip))
2433 ip->i_size -= PSIZE;
2434
2435 return 0;
2436}
2437
2438#ifdef _NOTYET
2439
2440
2441
2442
2443
2444
2445int dtRelocate(tid_t tid, struct inode *ip, s64 lmxaddr, pxd_t * opxd,
2446 s64 nxaddr)
2447{
2448 int rc = 0;
2449 struct metapage *mp, *pmp, *lmp, *rmp;
2450 dtpage_t *p, *pp, *rp = 0, *lp= 0;
2451 s64 bn;
2452 int index;
2453 struct btstack btstack;
2454 pxd_t *pxd;
2455 s64 oxaddr, nextbn, prevbn;
2456 int xlen, xsize;
2457 struct tlock *tlck;
2458 struct dt_lock *dtlck;
2459 struct pxd_lock *pxdlock;
2460 s8 *stbl;
2461 struct lv *lv;
2462
2463 oxaddr = addressPXD(opxd);
2464 xlen = lengthPXD(opxd);
2465
2466 jfs_info("dtRelocate: lmxaddr:%Ld xaddr:%Ld:%Ld xlen:%d",
2467 (long long)lmxaddr, (long long)oxaddr, (long long)nxaddr,
2468 xlen);
2469
2470
2471
2472
2473
2474 rc = dtSearchNode(ip, lmxaddr, opxd, &btstack);
2475 if (rc)
2476 return rc;
2477
2478
2479 DT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
2480 jfs_info("dtRelocate: parent router entry validated.");
2481
2482
2483
2484
2485
2486 DT_GETPAGE(ip, oxaddr, mp, PSIZE, p, rc);
2487 if (rc) {
2488
2489 DT_PUTPAGE(pmp);
2490 return rc;
2491 }
2492
2493
2494
2495
2496 rmp = NULL;
2497 if (p->header.next) {
2498 nextbn = le64_to_cpu(p->header.next);
2499 DT_GETPAGE(ip, nextbn, rmp, PSIZE, rp, rc);
2500 if (rc) {
2501 DT_PUTPAGE(mp);
2502 DT_PUTPAGE(pmp);
2503 return (rc);
2504 }
2505 }
2506
2507 lmp = NULL;
2508 if (p->header.prev) {
2509 prevbn = le64_to_cpu(p->header.prev);
2510 DT_GETPAGE(ip, prevbn, lmp, PSIZE, lp, rc);
2511 if (rc) {
2512 DT_PUTPAGE(mp);
2513 DT_PUTPAGE(pmp);
2514 if (rmp)
2515 DT_PUTPAGE(rmp);
2516 return (rc);
2517 }
2518 }
2519
2520
2521
2522
2523
2524
2525 if (lmp) {
2526 tlck = txLock(tid, ip, lmp, tlckDTREE | tlckRELINK);
2527 dtlck = (struct dt_lock *) & tlck->lock;
2528
2529 ASSERT(dtlck->index == 0);
2530 lv = & dtlck->lv[0];
2531 lv->offset = 0;
2532 lv->length = 1;
2533 dtlck->index++;
2534
2535 lp->header.next = cpu_to_le64(nxaddr);
2536 DT_PUTPAGE(lmp);
2537 }
2538
2539 if (rmp) {
2540 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckRELINK);
2541 dtlck = (struct dt_lock *) & tlck->lock;
2542
2543 ASSERT(dtlck->index == 0);
2544 lv = & dtlck->lv[0];
2545 lv->offset = 0;
2546 lv->length = 1;
2547 dtlck->index++;
2548
2549 rp->header.prev = cpu_to_le64(nxaddr);
2550 DT_PUTPAGE(rmp);
2551 }
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562 tlck = txLock(tid, ip, mp, tlckDTREE | tlckNEW);
2563 dtlck = (struct dt_lock *) & tlck->lock;
2564
2565 ASSERT(dtlck->index == 0);
2566 lv = & dtlck->lv[0];
2567
2568
2569 pxd = &p->header.self;
2570 PXDaddress(pxd, nxaddr);
2571
2572
2573
2574
2575 lv->offset = 0;
2576 lv->length = p->header.maxslot;
2577 dtlck->index++;
2578
2579
2580 xsize = xlen << JFS_SBI(ip->i_sb)->l2bsize;
2581
2582
2583 DT_PUTPAGE(mp);
2584 jfs_info("dtRelocate: target dtpage relocated.");
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599 tlck = txMaplock(tid, ip, tlckDTREE | tlckFREE);
2600 pxdlock = (struct pxd_lock *) & tlck->lock;
2601 pxdlock->flag = mlckFREEPXD;
2602 PXDaddress(&pxdlock->pxd, oxaddr);
2603 PXDlength(&pxdlock->pxd, xlen);
2604 pxdlock->index = 1;
2605
2606
2607
2608
2609
2610
2611
2612 jfs_info("dtRelocate: update parent router entry.");
2613 tlck = txLock(tid, ip, pmp, tlckDTREE | tlckENTRY);
2614 dtlck = (struct dt_lock *) & tlck->lock;
2615 lv = & dtlck->lv[dtlck->index];
2616
2617
2618 stbl = DT_GETSTBL(pp);
2619 pxd = (pxd_t *) & pp->slot[stbl[index]];
2620 PXDaddress(pxd, nxaddr);
2621 lv->offset = stbl[index];
2622 lv->length = 1;
2623 dtlck->index++;
2624
2625
2626 DT_PUTPAGE(pmp);
2627
2628 return rc;
2629}
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642static int dtSearchNode(struct inode *ip, s64 lmxaddr, pxd_t * kpxd,
2643 struct btstack * btstack)
2644{
2645 int rc = 0;
2646 s64 bn;
2647 struct metapage *mp;
2648 dtpage_t *p;
2649 int psize = 288;
2650 s8 *stbl;
2651 int i;
2652 pxd_t *pxd;
2653 struct btframe *btsp;
2654
2655 BT_CLR(btstack);
2656
2657
2658
2659
2660
2661
2662 for (bn = 0;;) {
2663
2664 DT_GETPAGE(ip, bn, mp, psize, p, rc);
2665 if (rc)
2666 return rc;
2667
2668
2669
2670
2671 if (p->header.flag & BT_ROOT) {
2672 if (lmxaddr == 0)
2673 break;
2674 } else if (addressPXD(&p->header.self) == lmxaddr)
2675 break;
2676
2677
2678
2679
2680 if (p->header.flag & BT_LEAF) {
2681 DT_PUTPAGE(mp);
2682 return -ESTALE;
2683 }
2684
2685
2686 stbl = DT_GETSTBL(p);
2687 pxd = (pxd_t *) & p->slot[stbl[0]];
2688
2689
2690 bn = addressPXD(pxd);
2691 psize = lengthPXD(pxd) << JFS_SBI(ip->i_sb)->l2bsize;
2692
2693 DT_PUTPAGE(mp);
2694 }
2695
2696
2697
2698
2699 loop:
2700 stbl = DT_GETSTBL(p);
2701 for (i = 0; i < p->header.nextindex; i++) {
2702 pxd = (pxd_t *) & p->slot[stbl[i]];
2703
2704
2705 if (addressPXD(pxd) == addressPXD(kpxd) &&
2706 lengthPXD(pxd) == lengthPXD(kpxd)) {
2707 btsp = btstack->top;
2708 btsp->bn = bn;
2709 btsp->index = i;
2710 btsp->mp = mp;
2711
2712 return 0;
2713 }
2714 }
2715
2716
2717 if (p->header.next)
2718 bn = le64_to_cpu(p->header.next);
2719 else {
2720 DT_PUTPAGE(mp);
2721 return -ESTALE;
2722 }
2723
2724
2725 DT_PUTPAGE(mp);
2726
2727
2728 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2729 if (rc)
2730 return rc;
2731
2732 goto loop;
2733}
2734#endif
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747static int dtRelink(tid_t tid, struct inode *ip, dtpage_t * p)
2748{
2749 int rc;
2750 struct metapage *mp;
2751 s64 nextbn, prevbn;
2752 struct tlock *tlck;
2753 struct dt_lock *dtlck;
2754 struct lv *lv;
2755
2756 nextbn = le64_to_cpu(p->header.next);
2757 prevbn = le64_to_cpu(p->header.prev);
2758
2759
2760 if (nextbn != 0) {
2761 DT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
2762 if (rc)
2763 return rc;
2764
2765 BT_MARK_DIRTY(mp, ip);
2766
2767
2768
2769
2770
2771 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
2772 jfs_info("dtRelink nextbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2773 tlck, ip, mp);
2774 dtlck = (struct dt_lock *) & tlck->lock;
2775
2776
2777 if (dtlck->index >= dtlck->maxcnt)
2778 dtlck = (struct dt_lock *) txLinelock(dtlck);
2779 lv = & dtlck->lv[dtlck->index];
2780 lv->offset = 0;
2781 lv->length = 1;
2782 dtlck->index++;
2783
2784 p->header.prev = cpu_to_le64(prevbn);
2785 DT_PUTPAGE(mp);
2786 }
2787
2788
2789 if (prevbn != 0) {
2790 DT_GETPAGE(ip, prevbn, mp, PSIZE, p, rc);
2791 if (rc)
2792 return rc;
2793
2794 BT_MARK_DIRTY(mp, ip);
2795
2796
2797
2798
2799
2800 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
2801 jfs_info("dtRelink prevbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2802 tlck, ip, mp);
2803 dtlck = (struct dt_lock *) & tlck->lock;
2804
2805
2806 if (dtlck->index >= dtlck->maxcnt)
2807 dtlck = (struct dt_lock *) txLinelock(dtlck);
2808 lv = & dtlck->lv[dtlck->index];
2809 lv->offset = 0;
2810 lv->length = 1;
2811 dtlck->index++;
2812
2813 p->header.next = cpu_to_le64(nextbn);
2814 DT_PUTPAGE(mp);
2815 }
2816
2817 return 0;
2818}
2819
2820
2821
2822
2823
2824
2825
2826void dtInitRoot(tid_t tid, struct inode *ip, u32 idotdot)
2827{
2828 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
2829 dtroot_t *p;
2830 int fsi;
2831 struct dtslot *f;
2832 struct tlock *tlck;
2833 struct dt_lock *dtlck;
2834 struct lv *lv;
2835 u16 xflag_save;
2836
2837
2838
2839
2840
2841 if (DO_INDEX(ip)) {
2842 if (!jfs_dirtable_inline(ip)) {
2843 struct tblock *tblk = tid_to_tblock(tid);
2844
2845
2846
2847
2848
2849
2850 xflag_save = tblk->xflag;
2851 tblk->xflag = 0;
2852
2853
2854
2855
2856
2857
2858
2859
2860 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
2861 set_cflag(COMMIT_Stale, ip);
2862
2863 tblk->xflag = xflag_save;
2864 } else
2865 ip->i_size = 1;
2866
2867 jfs_ip->next_index = 2;
2868 } else
2869 ip->i_size = IDATASIZE;
2870
2871
2872
2873
2874
2875
2876 tlck = txLock(tid, ip, (struct metapage *) & jfs_ip->bxflag,
2877 tlckDTREE | tlckENTRY | tlckBTROOT);
2878 dtlck = (struct dt_lock *) & tlck->lock;
2879
2880
2881 ASSERT(dtlck->index == 0);
2882 lv = & dtlck->lv[0];
2883 lv->offset = 0;
2884 lv->length = DTROOTMAXSLOT;
2885 dtlck->index++;
2886
2887 p = &jfs_ip->i_dtroot;
2888
2889 p->header.flag = DXD_INDEX | BT_ROOT | BT_LEAF;
2890
2891 p->header.nextindex = 0;
2892
2893
2894 fsi = 1;
2895 f = &p->slot[fsi];
2896
2897
2898 for (fsi++; fsi < DTROOTMAXSLOT; f++, fsi++)
2899 f->next = fsi;
2900 f->next = -1;
2901
2902 p->header.freelist = 1;
2903 p->header.freecnt = 8;
2904
2905
2906 p->header.idotdot = cpu_to_le32(idotdot);
2907
2908 return;
2909}
2910
2911
2912
2913
2914
2915
2916
2917
2918static void add_missing_indices(struct inode *inode, s64 bn)
2919{
2920 struct ldtentry *d;
2921 struct dt_lock *dtlck;
2922 int i;
2923 uint index;
2924 struct lv *lv;
2925 struct metapage *mp;
2926 dtpage_t *p;
2927 int rc;
2928 s8 *stbl;
2929 tid_t tid;
2930 struct tlock *tlck;
2931
2932 tid = txBegin(inode->i_sb, 0);
2933
2934 DT_GETPAGE(inode, bn, mp, PSIZE, p, rc);
2935
2936 if (rc) {
2937 printk(KERN_ERR "DT_GETPAGE failed!\n");
2938 goto end;
2939 }
2940 BT_MARK_DIRTY(mp, inode);
2941
2942 ASSERT(p->header.flag & BT_LEAF);
2943
2944 tlck = txLock(tid, inode, mp, tlckDTREE | tlckENTRY);
2945 if (BT_IS_ROOT(mp))
2946 tlck->type |= tlckBTROOT;
2947
2948 dtlck = (struct dt_lock *) &tlck->lock;
2949
2950 stbl = DT_GETSTBL(p);
2951 for (i = 0; i < p->header.nextindex; i++) {
2952 d = (struct ldtentry *) &p->slot[stbl[i]];
2953 index = le32_to_cpu(d->index);
2954 if ((index < 2) || (index >= JFS_IP(inode)->next_index)) {
2955 d->index = cpu_to_le32(add_index(tid, inode, bn, i));
2956 if (dtlck->index >= dtlck->maxcnt)
2957 dtlck = (struct dt_lock *) txLinelock(dtlck);
2958 lv = &dtlck->lv[dtlck->index];
2959 lv->offset = stbl[i];
2960 lv->length = 1;
2961 dtlck->index++;
2962 }
2963 }
2964
2965 DT_PUTPAGE(mp);
2966 (void) txCommit(tid, 1, &inode, 0);
2967end:
2968 txEnd(tid);
2969}
2970
2971
2972
2973
2974
2975struct jfs_dirent {
2976 loff_t position;
2977 int ino;
2978 u16 name_len;
2979 char name[0];
2980};
2981
2982
2983
2984
2985static inline struct jfs_dirent *next_jfs_dirent(struct jfs_dirent *dirent)
2986{
2987 return (struct jfs_dirent *)
2988 ((char *)dirent +
2989 ((sizeof (struct jfs_dirent) + dirent->name_len + 1 +
2990 sizeof (loff_t) - 1) &
2991 ~(sizeof (loff_t) - 1)));
2992}
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005int jfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
3006{
3007 struct inode *ip = file_inode(filp);
3008 struct nls_table *codepage = JFS_SBI(ip->i_sb)->nls_tab;
3009 int rc = 0;
3010 loff_t dtpos;
3011 struct dtoffset {
3012 s16 pn;
3013 s16 index;
3014 s32 unused;
3015 } *dtoffset = (struct dtoffset *) &dtpos;
3016 s64 bn;
3017 struct metapage *mp;
3018 dtpage_t *p;
3019 int index;
3020 s8 *stbl;
3021 struct btstack btstack;
3022 int i, next;
3023 struct ldtentry *d;
3024 struct dtslot *t;
3025 int d_namleft, len, outlen;
3026 unsigned long dirent_buf;
3027 char *name_ptr;
3028 u32 dir_index;
3029 int do_index = 0;
3030 uint loop_count = 0;
3031 struct jfs_dirent *jfs_dirent;
3032 int jfs_dirents;
3033 int overflow, fix_page, page_fixed = 0;
3034 static int unique_pos = 2;
3035
3036 if (filp->f_pos == DIREND)
3037 return 0;
3038
3039 if (DO_INDEX(ip)) {
3040
3041
3042
3043
3044
3045
3046 do_index = 1;
3047
3048 dir_index = (u32) filp->f_pos;
3049
3050
3051
3052
3053
3054
3055 if (dir_index)
3056 dir_index--;
3057
3058 if (dir_index > 1) {
3059 struct dir_table_slot dirtab_slot;
3060
3061 if (dtEmpty(ip) ||
3062 (dir_index >= JFS_IP(ip)->next_index)) {
3063
3064 filp->f_pos = DIREND;
3065 return 0;
3066 }
3067 repeat:
3068 rc = read_index(ip, dir_index, &dirtab_slot);
3069 if (rc) {
3070 filp->f_pos = DIREND;
3071 return rc;
3072 }
3073 if (dirtab_slot.flag == DIR_INDEX_FREE) {
3074 if (loop_count++ > JFS_IP(ip)->next_index) {
3075 jfs_err("jfs_readdir detected "
3076 "infinite loop!");
3077 filp->f_pos = DIREND;
3078 return 0;
3079 }
3080 dir_index = le32_to_cpu(dirtab_slot.addr2);
3081 if (dir_index == -1) {
3082 filp->f_pos = DIREND;
3083 return 0;
3084 }
3085 goto repeat;
3086 }
3087 bn = addressDTS(&dirtab_slot);
3088 index = dirtab_slot.slot;
3089 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3090 if (rc) {
3091 filp->f_pos = DIREND;
3092 return 0;
3093 }
3094 if (p->header.flag & BT_INTERNAL) {
3095 jfs_err("jfs_readdir: bad index table");
3096 DT_PUTPAGE(mp);
3097 filp->f_pos = DIREND;
3098 return 0;
3099 }
3100 } else {
3101 if (dir_index == 0) {
3102
3103
3104
3105 filp->f_pos = 1;
3106 if (filldir(dirent, ".", 1, 0, ip->i_ino,
3107 DT_DIR))
3108 return 0;
3109 }
3110
3111
3112
3113 filp->f_pos = 2;
3114 if (filldir(dirent, "..", 2, 1, PARENT(ip), DT_DIR))
3115 return 0;
3116
3117
3118
3119
3120 if (dtEmpty(ip)) {
3121 filp->f_pos = DIREND;
3122 return 0;
3123 }
3124
3125 if ((rc = dtReadFirst(ip, &btstack)))
3126 return rc;
3127
3128 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
3129 }
3130 } else {
3131
3132
3133
3134
3135
3136
3137
3138
3139 dtpos = filp->f_pos;
3140 if (dtpos < 2) {
3141
3142
3143 filp->f_pos = 1;
3144 if (filldir(dirent, ".", 1, filp->f_pos, ip->i_ino,
3145 DT_DIR))
3146 return 0;
3147 dtoffset->index = 2;
3148 filp->f_pos = dtpos;
3149 }
3150
3151 if (dtoffset->pn == 0) {
3152 if (dtoffset->index == 2) {
3153
3154
3155 if (filldir(dirent, "..", 2, filp->f_pos,
3156 PARENT(ip), DT_DIR))
3157 return 0;
3158 } else {
3159 jfs_err("jfs_readdir called with "
3160 "invalid offset!");
3161 }
3162 dtoffset->pn = 1;
3163 dtoffset->index = 0;
3164 filp->f_pos = dtpos;
3165 }
3166
3167 if (dtEmpty(ip)) {
3168 filp->f_pos = DIREND;
3169 return 0;
3170 }
3171
3172 if ((rc = dtReadNext(ip, &filp->f_pos, &btstack))) {
3173 jfs_err("jfs_readdir: unexpected rc = %d "
3174 "from dtReadNext", rc);
3175 filp->f_pos = DIREND;
3176 return 0;
3177 }
3178
3179 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
3180
3181
3182 if (bn < 0) {
3183 filp->f_pos = DIREND;
3184 return 0;
3185 }
3186 }
3187
3188 dirent_buf = __get_free_page(GFP_KERNEL);
3189 if (dirent_buf == 0) {
3190 DT_PUTPAGE(mp);
3191 jfs_warn("jfs_readdir: __get_free_page failed!");
3192 filp->f_pos = DIREND;
3193 return -ENOMEM;
3194 }
3195
3196 while (1) {
3197 jfs_dirent = (struct jfs_dirent *) dirent_buf;
3198 jfs_dirents = 0;
3199 overflow = fix_page = 0;
3200
3201 stbl = DT_GETSTBL(p);
3202
3203 for (i = index; i < p->header.nextindex; i++) {
3204 d = (struct ldtentry *) & p->slot[stbl[i]];
3205
3206 if (((long) jfs_dirent + d->namlen + 1) >
3207 (dirent_buf + PAGE_SIZE)) {
3208
3209 index = i;
3210 overflow = 1;
3211 break;
3212 }
3213
3214 d_namleft = d->namlen;
3215 name_ptr = jfs_dirent->name;
3216 jfs_dirent->ino = le32_to_cpu(d->inumber);
3217
3218 if (do_index) {
3219 len = min(d_namleft, DTLHDRDATALEN);
3220 jfs_dirent->position = le32_to_cpu(d->index);
3221
3222
3223
3224
3225
3226
3227
3228 if ((jfs_dirent->position < 2) ||
3229 (jfs_dirent->position >=
3230 JFS_IP(ip)->next_index)) {
3231 if (!page_fixed && !isReadOnly(ip)) {
3232 fix_page = 1;
3233
3234
3235
3236
3237
3238
3239 overflow = 1;
3240 index = i;
3241 break;
3242 }
3243 jfs_dirent->position = unique_pos++;
3244 }
3245
3246
3247
3248
3249
3250 jfs_dirent->position++;
3251 } else {
3252 jfs_dirent->position = dtpos;
3253 len = min(d_namleft, DTLHDRDATALEN_LEGACY);
3254 }
3255
3256
3257 outlen = jfs_strfromUCS_le(name_ptr, d->name, len,
3258 codepage);
3259 jfs_dirent->name_len = outlen;
3260
3261
3262 next = d->next;
3263 while (next >= 0) {
3264 t = (struct dtslot *) & p->slot[next];
3265 name_ptr += outlen;
3266 d_namleft -= len;
3267
3268 if (d_namleft == 0) {
3269 jfs_error(ip->i_sb,
3270 "JFS:Dtree error: ino = "
3271 "%ld, bn=%Ld, index = %d",
3272 (long)ip->i_ino,
3273 (long long)bn,
3274 i);
3275 goto skip_one;
3276 }
3277 len = min(d_namleft, DTSLOTDATALEN);
3278 outlen = jfs_strfromUCS_le(name_ptr, t->name,
3279 len, codepage);
3280 jfs_dirent->name_len += outlen;
3281
3282 next = t->next;
3283 }
3284
3285 jfs_dirents++;
3286 jfs_dirent = next_jfs_dirent(jfs_dirent);
3287skip_one:
3288 if (!do_index)
3289 dtoffset->index++;
3290 }
3291
3292 if (!overflow) {
3293
3294 if (p->header.flag & BT_ROOT)
3295 bn = 0;
3296 else {
3297 bn = le64_to_cpu(p->header.next);
3298 index = 0;
3299
3300 if (!do_index) {
3301 dtoffset->pn++;
3302 dtoffset->index = 0;
3303 }
3304 }
3305 page_fixed = 0;
3306 }
3307
3308
3309 DT_PUTPAGE(mp);
3310
3311 jfs_dirent = (struct jfs_dirent *) dirent_buf;
3312 while (jfs_dirents--) {
3313 filp->f_pos = jfs_dirent->position;
3314 if (filldir(dirent, jfs_dirent->name,
3315 jfs_dirent->name_len, filp->f_pos,
3316 jfs_dirent->ino, DT_UNKNOWN))
3317 goto out;
3318 jfs_dirent = next_jfs_dirent(jfs_dirent);
3319 }
3320
3321 if (fix_page) {
3322 add_missing_indices(ip, bn);
3323 page_fixed = 1;
3324 }
3325
3326 if (!overflow && (bn == 0)) {
3327 filp->f_pos = DIREND;
3328 break;
3329 }
3330
3331 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3332 if (rc) {
3333 free_page(dirent_buf);
3334 return rc;
3335 }
3336 }
3337
3338 out:
3339 free_page(dirent_buf);
3340
3341 return rc;
3342}
3343
3344
3345
3346
3347
3348
3349
3350static int dtReadFirst(struct inode *ip, struct btstack * btstack)
3351{
3352 int rc = 0;
3353 s64 bn;
3354 int psize = 288;
3355 struct metapage *mp;
3356 dtpage_t *p;
3357 s8 *stbl;
3358 struct btframe *btsp;
3359 pxd_t *xd;
3360
3361 BT_CLR(btstack);
3362
3363
3364
3365
3366
3367
3368 for (bn = 0;;) {
3369 DT_GETPAGE(ip, bn, mp, psize, p, rc);
3370 if (rc)
3371 return rc;
3372
3373
3374
3375
3376 if (p->header.flag & BT_LEAF) {
3377
3378 btsp = btstack->top;
3379 btsp->bn = bn;
3380 btsp->index = 0;
3381 btsp->mp = mp;
3382
3383 return 0;
3384 }
3385
3386
3387
3388
3389 if (BT_STACK_FULL(btstack)) {
3390 DT_PUTPAGE(mp);
3391 jfs_error(ip->i_sb, "dtReadFirst: btstack overrun");
3392 BT_STACK_DUMP(btstack);
3393 return -EIO;
3394 }
3395
3396 BT_PUSH(btstack, bn, 0);
3397
3398
3399 stbl = DT_GETSTBL(p);
3400 xd = (pxd_t *) & p->slot[stbl[0]];
3401
3402
3403 bn = addressPXD(xd);
3404 psize = lengthPXD(xd) << JFS_SBI(ip->i_sb)->l2bsize;
3405
3406
3407 DT_PUTPAGE(mp);
3408 }
3409}
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422static int dtReadNext(struct inode *ip, loff_t * offset,
3423 struct btstack * btstack)
3424{
3425 int rc = 0;
3426 struct dtoffset {
3427 s16 pn;
3428 s16 index;
3429 s32 unused;
3430 } *dtoffset = (struct dtoffset *) offset;
3431 s64 bn;
3432 struct metapage *mp;
3433 dtpage_t *p;
3434 int index;
3435 int pn;
3436 s8 *stbl;
3437 struct btframe *btsp, *parent;
3438 pxd_t *xd;
3439
3440
3441
3442
3443 if ((rc = dtReadFirst(ip, btstack)))
3444 return rc;
3445
3446
3447 DT_GETSEARCH(ip, btstack->top, bn, mp, p, index);
3448
3449
3450 pn = dtoffset->pn - 1;
3451 index = dtoffset->index;
3452
3453
3454 if (pn == 0) {
3455
3456 if (index < p->header.nextindex)
3457 goto out;
3458
3459 if (p->header.flag & BT_ROOT) {
3460 bn = -1;
3461 goto out;
3462 }
3463
3464
3465 dtoffset->pn++;
3466 dtoffset->index = index = 0;
3467 goto a;
3468 }
3469
3470
3471 if (p->header.flag & BT_ROOT) {
3472 bn = -1;
3473 goto out;
3474 }
3475
3476
3477 if (pn > 1)
3478 goto b;
3479
3480
3481 a:
3482 bn = le64_to_cpu(p->header.next);
3483
3484
3485 DT_PUTPAGE(mp);
3486
3487
3488 if (bn == 0) {
3489 bn = -1;
3490 goto out;
3491 }
3492
3493 goto c;
3494
3495
3496
3497
3498 b:
3499
3500 DT_PUTPAGE(mp);
3501
3502
3503 btsp = btstack->top;
3504 parent = btsp - 1;
3505 bn = parent->bn;
3506 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3507 if (rc)
3508 return rc;
3509
3510
3511 while (pn >= p->header.nextindex) {
3512 pn -= p->header.nextindex;
3513
3514
3515 bn = le64_to_cpu(p->header.next);
3516
3517
3518 DT_PUTPAGE(mp);
3519
3520
3521 if (bn == 0) {
3522 bn = -1;
3523 goto out;
3524 }
3525
3526
3527 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3528 if (rc)
3529 return rc;
3530
3531
3532 parent->bn = bn;
3533 }
3534
3535
3536 stbl = DT_GETSTBL(p);
3537 xd = (pxd_t *) & p->slot[stbl[pn]];
3538 bn = addressPXD(xd);
3539
3540
3541 DT_PUTPAGE(mp);
3542
3543
3544
3545
3546 c:
3547 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3548 if (rc)
3549 return rc;
3550
3551
3552
3553
3554
3555 if (index >= p->header.nextindex) {
3556 bn = le64_to_cpu(p->header.next);
3557
3558
3559 DT_PUTPAGE(mp);
3560
3561
3562 if (bn == 0) {
3563 bn = -1;
3564 goto out;
3565 }
3566
3567
3568 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3569 if (rc)
3570 return rc;
3571
3572
3573 dtoffset->pn++;
3574 dtoffset->index = 0;
3575 }
3576
3577 out:
3578
3579 btsp = btstack->top;
3580 btsp->bn = bn;
3581 btsp->index = dtoffset->index;
3582 btsp->mp = mp;
3583
3584 return 0;
3585}
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598static int dtCompare(struct component_name * key,
3599 dtpage_t * p,
3600 int si)
3601{
3602 wchar_t *kname;
3603 __le16 *name;
3604 int klen, namlen, len, rc;
3605 struct idtentry *ih;
3606 struct dtslot *t;
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624 kname = key->name;
3625 klen = key->namlen;
3626
3627 ih = (struct idtentry *) & p->slot[si];
3628 si = ih->next;
3629 name = ih->name;
3630 namlen = ih->namlen;
3631 len = min(namlen, DTIHDRDATALEN);
3632
3633
3634 len = min(klen, len);
3635 if ((rc = UniStrncmp_le(kname, name, len)))
3636 return rc;
3637
3638 klen -= len;
3639 namlen -= len;
3640
3641
3642 kname += len;
3643 while (klen > 0 && namlen > 0) {
3644
3645 t = (struct dtslot *) & p->slot[si];
3646 len = min(namlen, DTSLOTDATALEN);
3647 len = min(klen, len);
3648 name = t->name;
3649 if ((rc = UniStrncmp_le(kname, name, len)))
3650 return rc;
3651
3652 klen -= len;
3653 namlen -= len;
3654 kname += len;
3655 si = t->next;
3656 }
3657
3658 return (klen - namlen);
3659}
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674static int ciCompare(struct component_name * key,
3675 dtpage_t * p,
3676 int si,
3677 int flag)
3678{
3679 wchar_t *kname, x;
3680 __le16 *name;
3681 int klen, namlen, len, rc;
3682 struct ldtentry *lh;
3683 struct idtentry *ih;
3684 struct dtslot *t;
3685 int i;
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703 kname = key->name;
3704 klen = key->namlen;
3705
3706
3707
3708
3709 if (p->header.flag & BT_LEAF) {
3710 lh = (struct ldtentry *) & p->slot[si];
3711 si = lh->next;
3712 name = lh->name;
3713 namlen = lh->namlen;
3714 if (flag & JFS_DIR_INDEX)
3715 len = min(namlen, DTLHDRDATALEN);
3716 else
3717 len = min(namlen, DTLHDRDATALEN_LEGACY);
3718 }
3719
3720
3721
3722 else {
3723 ih = (struct idtentry *) & p->slot[si];
3724 si = ih->next;
3725 name = ih->name;
3726 namlen = ih->namlen;
3727 len = min(namlen, DTIHDRDATALEN);
3728 }
3729
3730
3731 len = min(klen, len);
3732 for (i = 0; i < len; i++, kname++, name++) {
3733
3734 if ((flag & JFS_OS2) == JFS_OS2)
3735 x = UniToupper(le16_to_cpu(*name));
3736 else
3737 x = le16_to_cpu(*name);
3738 if ((rc = *kname - x))
3739 return rc;
3740 }
3741
3742 klen -= len;
3743 namlen -= len;
3744
3745
3746 while (klen > 0 && namlen > 0) {
3747
3748 t = (struct dtslot *) & p->slot[si];
3749 len = min(namlen, DTSLOTDATALEN);
3750 len = min(klen, len);
3751 name = t->name;
3752 for (i = 0; i < len; i++, kname++, name++) {
3753
3754 if ((flag & JFS_OS2) == JFS_OS2)
3755 x = UniToupper(le16_to_cpu(*name));
3756 else
3757 x = le16_to_cpu(*name);
3758
3759 if ((rc = *kname - x))
3760 return rc;
3761 }
3762
3763 klen -= len;
3764 namlen -= len;
3765 si = t->next;
3766 }
3767
3768 return (klen - namlen);
3769}
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782static int ciGetLeafPrefixKey(dtpage_t * lp, int li, dtpage_t * rp,
3783 int ri, struct component_name * key, int flag)
3784{
3785 int klen, namlen;
3786 wchar_t *pl, *pr, *kname;
3787 struct component_name lkey;
3788 struct component_name rkey;
3789
3790 lkey.name = kmalloc((JFS_NAME_MAX + 1) * sizeof(wchar_t),
3791 GFP_KERNEL);
3792 if (lkey.name == NULL)
3793 return -ENOMEM;
3794
3795 rkey.name = kmalloc((JFS_NAME_MAX + 1) * sizeof(wchar_t),
3796 GFP_KERNEL);
3797 if (rkey.name == NULL) {
3798 kfree(lkey.name);
3799 return -ENOMEM;
3800 }
3801
3802
3803 dtGetKey(lp, li, &lkey, flag);
3804 lkey.name[lkey.namlen] = 0;
3805
3806 if ((flag & JFS_OS2) == JFS_OS2)
3807 ciToUpper(&lkey);
3808
3809 dtGetKey(rp, ri, &rkey, flag);
3810 rkey.name[rkey.namlen] = 0;
3811
3812
3813 if ((flag & JFS_OS2) == JFS_OS2)
3814 ciToUpper(&rkey);
3815
3816
3817 klen = 0;
3818 kname = key->name;
3819 namlen = min(lkey.namlen, rkey.namlen);
3820 for (pl = lkey.name, pr = rkey.name;
3821 namlen; pl++, pr++, namlen--, klen++, kname++) {
3822 *kname = *pr;
3823 if (*pl != *pr) {
3824 key->namlen = klen + 1;
3825 goto free_names;
3826 }
3827 }
3828
3829
3830 if (lkey.namlen < rkey.namlen) {
3831 *kname = *pr;
3832 key->namlen = klen + 1;
3833 } else
3834 key->namlen = klen;
3835
3836free_names:
3837 kfree(lkey.name);
3838 kfree(rkey.name);
3839 return 0;
3840}
3841
3842
3843
3844
3845
3846
3847
3848
3849static void dtGetKey(dtpage_t * p, int i,
3850 struct component_name * key, int flag)
3851{
3852 int si;
3853 s8 *stbl;
3854 struct ldtentry *lh;
3855 struct idtentry *ih;
3856 struct dtslot *t;
3857 int namlen, len;
3858 wchar_t *kname;
3859 __le16 *name;
3860
3861
3862 stbl = DT_GETSTBL(p);
3863 si = stbl[i];
3864 if (p->header.flag & BT_LEAF) {
3865 lh = (struct ldtentry *) & p->slot[si];
3866 si = lh->next;
3867 namlen = lh->namlen;
3868 name = lh->name;
3869 if (flag & JFS_DIR_INDEX)
3870 len = min(namlen, DTLHDRDATALEN);
3871 else
3872 len = min(namlen, DTLHDRDATALEN_LEGACY);
3873 } else {
3874 ih = (struct idtentry *) & p->slot[si];
3875 si = ih->next;
3876 namlen = ih->namlen;
3877 name = ih->name;
3878 len = min(namlen, DTIHDRDATALEN);
3879 }
3880
3881 key->namlen = namlen;
3882 kname = key->name;
3883
3884
3885
3886
3887 UniStrncpy_from_le(kname, name, len);
3888
3889
3890
3891
3892 while (si >= 0) {
3893
3894 t = &p->slot[si];
3895 kname += len;
3896 namlen -= len;
3897 len = min(namlen, DTSLOTDATALEN);
3898 UniStrncpy_from_le(kname, t->name, len);
3899
3900 si = t->next;
3901 }
3902}
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913static void dtInsertEntry(dtpage_t * p, int index, struct component_name * key,
3914 ddata_t * data, struct dt_lock ** dtlock)
3915{
3916 struct dtslot *h, *t;
3917 struct ldtentry *lh = NULL;
3918 struct idtentry *ih = NULL;
3919 int hsi, fsi, klen, len, nextindex;
3920 wchar_t *kname;
3921 __le16 *name;
3922 s8 *stbl;
3923 pxd_t *xd;
3924 struct dt_lock *dtlck = *dtlock;
3925 struct lv *lv;
3926 int xsi, n;
3927 s64 bn = 0;
3928 struct metapage *mp = NULL;
3929
3930 klen = key->namlen;
3931 kname = key->name;
3932
3933
3934 hsi = fsi = p->header.freelist;
3935 h = &p->slot[fsi];
3936 p->header.freelist = h->next;
3937 --p->header.freecnt;
3938
3939
3940 if (dtlck->index >= dtlck->maxcnt)
3941 dtlck = (struct dt_lock *) txLinelock(dtlck);
3942
3943 lv = & dtlck->lv[dtlck->index];
3944 lv->offset = hsi;
3945
3946
3947 if (p->header.flag & BT_LEAF) {
3948 lh = (struct ldtentry *) h;
3949 lh->next = h->next;
3950 lh->inumber = cpu_to_le32(data->leaf.ino);
3951 lh->namlen = klen;
3952 name = lh->name;
3953 if (data->leaf.ip) {
3954 len = min(klen, DTLHDRDATALEN);
3955 if (!(p->header.flag & BT_ROOT))
3956 bn = addressPXD(&p->header.self);
3957 lh->index = cpu_to_le32(add_index(data->leaf.tid,
3958 data->leaf.ip,
3959 bn, index));
3960 } else
3961 len = min(klen, DTLHDRDATALEN_LEGACY);
3962 } else {
3963 ih = (struct idtentry *) h;
3964 ih->next = h->next;
3965 xd = (pxd_t *) ih;
3966 *xd = data->xd;
3967 ih->namlen = klen;
3968 name = ih->name;
3969 len = min(klen, DTIHDRDATALEN);
3970 }
3971
3972 UniStrncpy_to_le(name, kname, len);
3973
3974 n = 1;
3975 xsi = hsi;
3976
3977
3978 t = h;
3979 klen -= len;
3980 while (klen) {
3981
3982 fsi = p->header.freelist;
3983 t = &p->slot[fsi];
3984 p->header.freelist = t->next;
3985 --p->header.freecnt;
3986
3987
3988 if (fsi != xsi + 1) {
3989
3990 lv->length = n;
3991 dtlck->index++;
3992
3993
3994 if (dtlck->index < dtlck->maxcnt)
3995 lv++;
3996 else {
3997 dtlck = (struct dt_lock *) txLinelock(dtlck);
3998 lv = & dtlck->lv[0];
3999 }
4000
4001 lv->offset = fsi;
4002 n = 0;
4003 }
4004
4005 kname += len;
4006 len = min(klen, DTSLOTDATALEN);
4007 UniStrncpy_to_le(t->name, kname, len);
4008
4009 n++;
4010 xsi = fsi;
4011 klen -= len;
4012 }
4013
4014
4015 lv->length = n;
4016 dtlck->index++;
4017
4018 *dtlock = dtlck;
4019
4020
4021 if (h == t) {
4022
4023 if (p->header.flag & BT_LEAF)
4024 lh->next = -1;
4025 else
4026 ih->next = -1;
4027 } else
4028
4029 t->next = -1;
4030
4031
4032 stbl = DT_GETSTBL(p);
4033 nextindex = p->header.nextindex;
4034 if (index < nextindex) {
4035 memmove(stbl + index + 1, stbl + index, nextindex - index);
4036
4037 if ((p->header.flag & BT_LEAF) && data->leaf.ip) {
4038 s64 lblock;
4039
4040
4041
4042
4043
4044 mp = NULL;
4045 for (n = index + 1; n <= nextindex; n++) {
4046 lh = (struct ldtentry *) & (p->slot[stbl[n]]);
4047 modify_index(data->leaf.tid, data->leaf.ip,
4048 le32_to_cpu(lh->index), bn, n,
4049 &mp, &lblock);
4050 }
4051 if (mp)
4052 release_metapage(mp);
4053 }
4054 }
4055
4056 stbl[index] = hsi;
4057
4058
4059 ++p->header.nextindex;
4060}
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071static void dtMoveEntry(dtpage_t * sp, int si, dtpage_t * dp,
4072 struct dt_lock ** sdtlock, struct dt_lock ** ddtlock,
4073 int do_index)
4074{
4075 int ssi, next;
4076 int di;
4077 int dsi;
4078 s8 *sstbl, *dstbl;
4079 int snamlen, len;
4080 struct ldtentry *slh, *dlh = NULL;
4081 struct idtentry *sih, *dih = NULL;
4082 struct dtslot *h, *s, *d;
4083 struct dt_lock *sdtlck = *sdtlock, *ddtlck = *ddtlock;
4084 struct lv *slv, *dlv;
4085 int xssi, ns, nd;
4086 int sfsi;
4087
4088 sstbl = (s8 *) & sp->slot[sp->header.stblindex];
4089 dstbl = (s8 *) & dp->slot[dp->header.stblindex];
4090
4091 dsi = dp->header.freelist;
4092 sfsi = sp->header.freelist;
4093
4094
4095 dlv = & ddtlck->lv[ddtlck->index];
4096 dlv->offset = dsi;
4097
4098
4099 slv = & sdtlck->lv[sdtlck->index];
4100 slv->offset = sstbl[si];
4101 xssi = slv->offset - 1;
4102
4103
4104
4105
4106 ns = nd = 0;
4107 for (di = 0; si < sp->header.nextindex; si++, di++) {
4108 ssi = sstbl[si];
4109 dstbl[di] = dsi;
4110
4111
4112 if (ssi != xssi + 1) {
4113
4114 slv->length = ns;
4115 sdtlck->index++;
4116
4117
4118 if (sdtlck->index < sdtlck->maxcnt)
4119 slv++;
4120 else {
4121 sdtlck = (struct dt_lock *) txLinelock(sdtlck);
4122 slv = & sdtlck->lv[0];
4123 }
4124
4125 slv->offset = ssi;
4126 ns = 0;
4127 }
4128
4129
4130
4131
4132
4133 h = d = &dp->slot[dsi];
4134
4135
4136 s = &sp->slot[ssi];
4137 if (sp->header.flag & BT_LEAF) {
4138
4139 slh = (struct ldtentry *) s;
4140 dlh = (struct ldtentry *) h;
4141 snamlen = slh->namlen;
4142
4143 if (do_index) {
4144 len = min(snamlen, DTLHDRDATALEN);
4145 dlh->index = slh->index;
4146 } else
4147 len = min(snamlen, DTLHDRDATALEN_LEGACY);
4148
4149 memcpy(dlh, slh, 6 + len * 2);
4150
4151 next = slh->next;
4152
4153
4154 dsi++;
4155 dlh->next = dsi;
4156 } else {
4157 sih = (struct idtentry *) s;
4158 snamlen = sih->namlen;
4159
4160 len = min(snamlen, DTIHDRDATALEN);
4161 dih = (struct idtentry *) h;
4162 memcpy(dih, sih, 10 + len * 2);
4163 next = sih->next;
4164
4165 dsi++;
4166 dih->next = dsi;
4167 }
4168
4169
4170 s->next = sfsi;
4171 s->cnt = 1;
4172 sfsi = ssi;
4173
4174 ns++;
4175 nd++;
4176 xssi = ssi;
4177
4178
4179
4180
4181 snamlen -= len;
4182 while ((ssi = next) >= 0) {
4183
4184 if (ssi != xssi + 1) {
4185
4186 slv->length = ns;
4187 sdtlck->index++;
4188
4189
4190 if (sdtlck->index < sdtlck->maxcnt)
4191 slv++;
4192 else {
4193 sdtlck =
4194 (struct dt_lock *)
4195 txLinelock(sdtlck);
4196 slv = & sdtlck->lv[0];
4197 }
4198
4199 slv->offset = ssi;
4200 ns = 0;
4201 }
4202
4203
4204 s = &sp->slot[ssi];
4205
4206
4207 d++;
4208
4209 len = min(snamlen, DTSLOTDATALEN);
4210 UniStrncpy_le(d->name, s->name, len);
4211
4212 ns++;
4213 nd++;
4214 xssi = ssi;
4215
4216 dsi++;
4217 d->next = dsi;
4218
4219
4220 next = s->next;
4221 s->next = sfsi;
4222 s->cnt = 1;
4223 sfsi = ssi;
4224
4225 snamlen -= len;
4226 }
4227
4228
4229 if (h == d) {
4230
4231 if (dp->header.flag & BT_LEAF)
4232 dlh->next = -1;
4233 else
4234 dih->next = -1;
4235 } else
4236
4237 d->next = -1;
4238 }
4239
4240
4241 slv->length = ns;
4242 sdtlck->index++;
4243 *sdtlock = sdtlck;
4244
4245 dlv->length = nd;
4246 ddtlck->index++;
4247 *ddtlock = ddtlck;
4248
4249
4250 sp->header.freelist = sfsi;
4251 sp->header.freecnt += nd;
4252
4253
4254 dp->header.nextindex = di;
4255
4256 dp->header.freelist = dsi;
4257 dp->header.freecnt -= nd;
4258}
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272static void dtDeleteEntry(dtpage_t * p, int fi, struct dt_lock ** dtlock)
4273{
4274 int fsi;
4275 s8 *stbl;
4276 struct dtslot *t;
4277 int si, freecnt;
4278 struct dt_lock *dtlck = *dtlock;
4279 struct lv *lv;
4280 int xsi, n;
4281
4282
4283 stbl = DT_GETSTBL(p);
4284 fsi = stbl[fi];
4285
4286
4287 if (dtlck->index >= dtlck->maxcnt)
4288 dtlck = (struct dt_lock *) txLinelock(dtlck);
4289 lv = & dtlck->lv[dtlck->index];
4290
4291 lv->offset = fsi;
4292
4293
4294 t = &p->slot[fsi];
4295 if (p->header.flag & BT_LEAF)
4296 si = ((struct ldtentry *) t)->next;
4297 else
4298 si = ((struct idtentry *) t)->next;
4299 t->next = si;
4300 t->cnt = 1;
4301
4302 n = freecnt = 1;
4303 xsi = fsi;
4304
4305
4306 while (si >= 0) {
4307
4308 if (si != xsi + 1) {
4309
4310 lv->length = n;
4311 dtlck->index++;
4312
4313
4314 if (dtlck->index < dtlck->maxcnt)
4315 lv++;
4316 else {
4317 dtlck = (struct dt_lock *) txLinelock(dtlck);
4318 lv = & dtlck->lv[0];
4319 }
4320
4321 lv->offset = si;
4322 n = 0;
4323 }
4324
4325 n++;
4326 xsi = si;
4327 freecnt++;
4328
4329 t = &p->slot[si];
4330 t->cnt = 1;
4331 si = t->next;
4332 }
4333
4334
4335 lv->length = n;
4336 dtlck->index++;
4337
4338 *dtlock = dtlck;
4339
4340
4341 t->next = p->header.freelist;
4342 p->header.freelist = fsi;
4343 p->header.freecnt += freecnt;
4344
4345
4346
4347
4348 si = p->header.nextindex;
4349 if (fi < si - 1)
4350 memmove(&stbl[fi], &stbl[fi + 1], si - fi - 1);
4351
4352 p->header.nextindex--;
4353}
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367static void dtTruncateEntry(dtpage_t * p, int ti, struct dt_lock ** dtlock)
4368{
4369 int tsi;
4370 s8 *stbl;
4371 struct dtslot *t;
4372 int si, freecnt;
4373 struct dt_lock *dtlck = *dtlock;
4374 struct lv *lv;
4375 int fsi, xsi, n;
4376
4377
4378 stbl = DT_GETSTBL(p);
4379 tsi = stbl[ti];
4380
4381
4382 if (dtlck->index >= dtlck->maxcnt)
4383 dtlck = (struct dt_lock *) txLinelock(dtlck);
4384 lv = & dtlck->lv[dtlck->index];
4385
4386 lv->offset = tsi;
4387
4388
4389 t = &p->slot[tsi];
4390 ASSERT(p->header.flag & BT_INTERNAL);
4391 ((struct idtentry *) t)->namlen = 0;
4392 si = ((struct idtentry *) t)->next;
4393 ((struct idtentry *) t)->next = -1;
4394
4395 n = 1;
4396 freecnt = 0;
4397 fsi = si;
4398 xsi = tsi;
4399
4400
4401 while (si >= 0) {
4402
4403 if (si != xsi + 1) {
4404
4405 lv->length = n;
4406 dtlck->index++;
4407
4408
4409 if (dtlck->index < dtlck->maxcnt)
4410 lv++;
4411 else {
4412 dtlck = (struct dt_lock *) txLinelock(dtlck);
4413 lv = & dtlck->lv[0];
4414 }
4415
4416 lv->offset = si;
4417 n = 0;
4418 }
4419
4420 n++;
4421 xsi = si;
4422 freecnt++;
4423
4424 t = &p->slot[si];
4425 t->cnt = 1;
4426 si = t->next;
4427 }
4428
4429
4430 lv->length = n;
4431 dtlck->index++;
4432
4433 *dtlock = dtlck;
4434
4435
4436 if (freecnt == 0)
4437 return;
4438 t->next = p->header.freelist;
4439 p->header.freelist = fsi;
4440 p->header.freecnt += freecnt;
4441}
4442
4443
4444
4445
4446
4447static void dtLinelockFreelist(dtpage_t * p,
4448 int m,
4449 struct dt_lock ** dtlock)
4450{
4451 int fsi;
4452 struct dtslot *t;
4453 int si;
4454 struct dt_lock *dtlck = *dtlock;
4455 struct lv *lv;
4456 int xsi, n;
4457
4458
4459 fsi = p->header.freelist;
4460
4461
4462 if (dtlck->index >= dtlck->maxcnt)
4463 dtlck = (struct dt_lock *) txLinelock(dtlck);
4464 lv = & dtlck->lv[dtlck->index];
4465
4466 lv->offset = fsi;
4467
4468 n = 1;
4469 xsi = fsi;
4470
4471 t = &p->slot[fsi];
4472 si = t->next;
4473
4474
4475 while (si < m && si >= 0) {
4476
4477 if (si != xsi + 1) {
4478
4479 lv->length = n;
4480 dtlck->index++;
4481
4482
4483 if (dtlck->index < dtlck->maxcnt)
4484 lv++;
4485 else {
4486 dtlck = (struct dt_lock *) txLinelock(dtlck);
4487 lv = & dtlck->lv[0];
4488 }
4489
4490 lv->offset = si;
4491 n = 0;
4492 }
4493
4494 n++;
4495 xsi = si;
4496
4497 t = &p->slot[si];
4498 si = t->next;
4499 }
4500
4501
4502 lv->length = n;
4503 dtlck->index++;
4504
4505 *dtlock = dtlck;
4506}
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527int dtModify(tid_t tid, struct inode *ip,
4528 struct component_name * key, ino_t * orig_ino, ino_t new_ino, int flag)
4529{
4530 int rc;
4531 s64 bn;
4532 struct metapage *mp;
4533 dtpage_t *p;
4534 int index;
4535 struct btstack btstack;
4536 struct tlock *tlck;
4537 struct dt_lock *dtlck;
4538 struct lv *lv;
4539 s8 *stbl;
4540 int entry_si;
4541 struct ldtentry *entry;
4542
4543
4544
4545
4546
4547
4548 if ((rc = dtSearch(ip, key, orig_ino, &btstack, flag)))
4549 return rc;
4550
4551
4552 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
4553
4554 BT_MARK_DIRTY(mp, ip);
4555
4556
4557
4558 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
4559 dtlck = (struct dt_lock *) & tlck->lock;
4560
4561
4562 stbl = DT_GETSTBL(p);
4563 entry_si = stbl[index];
4564
4565
4566 ASSERT(dtlck->index == 0);
4567 lv = & dtlck->lv[0];
4568 lv->offset = entry_si;
4569 lv->length = 1;
4570 dtlck->index++;
4571
4572
4573 entry = (struct ldtentry *) & p->slot[entry_si];
4574
4575
4576 entry->inumber = cpu_to_le32(new_ino);
4577
4578
4579 DT_PUTPAGE(mp);
4580
4581 return 0;
4582}
4583