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