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