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