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