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