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