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