MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / fs / jfs / jfs_dtree.c
blob29930b6ccf43ee90b94a2ac25fc087ec8d360252
1 /*
2 * Copyright (C) International Business Machines Corp., 2000-2004
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * jfs_dtree.c: directory B+-tree manager
22 * B+-tree with variable length key directory:
24 * each directory page is structured as an array of 32-byte
25 * directory entry slots initialized as a freelist
26 * to avoid search/compaction of free space at insertion.
27 * when an entry is inserted, a number of slots are allocated
28 * from the freelist as required to store variable length data
29 * of the entry; when the entry is deleted, slots of the entry
30 * are returned to freelist.
32 * leaf entry stores full name as key and file serial number
33 * (aka inode number) as data.
34 * internal/router entry stores sufffix compressed name
35 * as key and simple extent descriptor as data.
37 * each directory page maintains a sorted entry index table
38 * which stores the start slot index of sorted entries
39 * to allow binary search on the table.
41 * directory starts as a root/leaf page in on-disk inode
42 * inline data area.
43 * when it becomes full, it starts a leaf of a external extent
44 * of length of 1 block. each time the first leaf becomes full,
45 * it is extended rather than split (its size is doubled),
46 * until its length becoms 4 KBytes, from then the extent is split
47 * with new 4 Kbyte extent when it becomes full
48 * to reduce external fragmentation of small directories.
50 * blah, blah, blah, for linear scan of directory in pieces by
51 * readdir().
54 * case-insensitive directory file system
56 * names are stored in case-sensitive way in leaf entry.
57 * but stored, searched and compared in case-insensitive (uppercase) order
58 * (i.e., both search key and entry key are folded for search/compare):
59 * (note that case-sensitive order is BROKEN in storage, e.g.,
60 * sensitive: Ad, aB, aC, aD -> insensitive: aB, aC, aD, Ad
62 * entries which folds to the same key makes up a equivalent class
63 * whose members are stored as contiguous cluster (may cross page boundary)
64 * but whose order is arbitrary and acts as duplicate, e.g.,
65 * abc, Abc, aBc, abC)
67 * once match is found at leaf, requires scan forward/backward
68 * either for, in case-insensitive search, duplicate
69 * or for, in case-sensitive search, for exact match
71 * router entry must be created/stored in case-insensitive way
72 * in internal entry:
73 * (right most key of left page and left most key of right page
74 * are folded, and its suffix compression is propagated as router
75 * key in parent)
76 * (e.g., if split occurs <abc> and <aBd>, <ABD> trather than <aB>
77 * should be made the router key for the split)
79 * case-insensitive search:
81 * fold search key;
83 * case-insensitive search of B-tree:
84 * for internal entry, router key is already folded;
85 * for leaf entry, fold the entry key before comparison.
87 * if (leaf entry case-insensitive match found)
88 * if (next entry satisfies case-insensitive match)
89 * return EDUPLICATE;
90 * if (prev entry satisfies case-insensitive match)
91 * return EDUPLICATE;
92 * return match;
93 * else
94 * return no match;
96 * serialization:
97 * target directory inode lock is being held on entry/exit
98 * of all main directory service routines.
100 * log based recovery:
103 #include <linux/fs.h>
104 #include <linux/quotaops.h>
105 #include "jfs_incore.h"
106 #include "jfs_superblock.h"
107 #include "jfs_filsys.h"
108 #include "jfs_metapage.h"
109 #include "jfs_dmap.h"
110 #include "jfs_unicode.h"
111 #include "jfs_debug.h"
113 /* dtree split parameter */
114 struct dtsplit {
115 struct metapage *mp;
116 s16 index;
117 s16 nslot;
118 struct component_name *key;
119 ddata_t *data;
120 struct pxdlist *pxdlist;
123 #define DT_PAGE(IP, MP) BT_PAGE(IP, MP, dtpage_t, i_dtroot)
125 /* get page buffer for specified block address */
126 #define DT_GETPAGE(IP, BN, MP, SIZE, P, RC)\
128 BT_GETPAGE(IP, BN, MP, dtpage_t, SIZE, P, RC, i_dtroot)\
129 if (!(RC))\
131 if (((P)->header.nextindex > (((BN)==0)?DTROOTMAXSLOT:(P)->header.maxslot)) ||\
132 ((BN) && ((P)->header.maxslot > DTPAGEMAXSLOT)))\
134 BT_PUTPAGE(MP);\
135 jfs_error((IP)->i_sb, "DT_GETPAGE: dtree page corrupt");\
136 MP = NULL;\
137 RC = -EIO;\
142 /* for consistency */
143 #define DT_PUTPAGE(MP) BT_PUTPAGE(MP)
145 #define DT_GETSEARCH(IP, LEAF, BN, MP, P, INDEX) \
146 BT_GETSEARCH(IP, LEAF, BN, MP, dtpage_t, P, INDEX, i_dtroot)
149 * forward references
151 static int dtSplitUp(tid_t tid, struct inode *ip,
152 struct dtsplit * split, struct btstack * btstack);
154 static int dtSplitPage(tid_t tid, struct inode *ip, struct dtsplit * split,
155 struct metapage ** rmpp, dtpage_t ** rpp, pxd_t * rxdp);
157 static int dtExtendPage(tid_t tid, struct inode *ip,
158 struct dtsplit * split, struct btstack * btstack);
160 static int dtSplitRoot(tid_t tid, struct inode *ip,
161 struct dtsplit * split, struct metapage ** rmpp);
163 static int dtDeleteUp(tid_t tid, struct inode *ip, struct metapage * fmp,
164 dtpage_t * fp, struct btstack * btstack);
166 static int dtRelink(tid_t tid, struct inode *ip, dtpage_t * p);
168 static int dtReadFirst(struct inode *ip, struct btstack * btstack);
170 static int dtReadNext(struct inode *ip,
171 loff_t * offset, struct btstack * btstack);
173 static int dtCompare(struct component_name * key, dtpage_t * p, int si);
175 static int ciCompare(struct component_name * key, dtpage_t * p, int si,
176 int flag);
178 static void dtGetKey(dtpage_t * p, int i, struct component_name * key,
179 int flag);
181 static int ciGetLeafPrefixKey(dtpage_t * lp, int li, dtpage_t * rp,
182 int ri, struct component_name * key, int flag);
184 static void dtInsertEntry(dtpage_t * p, int index, struct component_name * key,
185 ddata_t * data, struct dt_lock **);
187 static void dtMoveEntry(dtpage_t * sp, int si, dtpage_t * dp,
188 struct dt_lock ** sdtlock, struct dt_lock ** ddtlock,
189 int do_index);
191 static void dtDeleteEntry(dtpage_t * p, int fi, struct dt_lock ** dtlock);
193 static void dtTruncateEntry(dtpage_t * p, int ti, struct dt_lock ** dtlock);
195 static void dtLinelockFreelist(dtpage_t * p, int m, struct dt_lock ** dtlock);
197 #define ciToUpper(c) UniStrupr((c)->name)
200 * read_index_page()
202 * Reads a page of a directory's index table.
203 * Having metadata mapped into the directory inode's address space
204 * presents a multitude of problems. We avoid this by mapping to
205 * the absolute address space outside of the *_metapage routines
207 static struct metapage *read_index_page(struct inode *inode, s64 blkno)
209 int rc;
210 s64 xaddr;
211 int xflag;
212 s32 xlen;
214 rc = xtLookup(inode, blkno, 1, &xflag, &xaddr, &xlen, 1);
215 if (rc || (xlen == 0))
216 return NULL;
218 return read_metapage(inode, xaddr, PSIZE, 1);
222 * get_index_page()
224 * Same as get_index_page(), but get's a new page without reading
226 static struct metapage *get_index_page(struct inode *inode, s64 blkno)
228 int rc;
229 s64 xaddr;
230 int xflag;
231 s32 xlen;
233 rc = xtLookup(inode, blkno, 1, &xflag, &xaddr, &xlen, 1);
234 if (rc || (xlen == 0))
235 return NULL;
237 return get_metapage(inode, xaddr, PSIZE, 1);
241 * find_index()
243 * Returns dtree page containing directory table entry for specified
244 * index and pointer to its entry.
246 * mp must be released by caller.
248 static struct dir_table_slot *find_index(struct inode *ip, u32 index,
249 struct metapage ** mp, s64 *lblock)
251 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
252 s64 blkno;
253 s64 offset;
254 int page_offset;
255 struct dir_table_slot *slot;
256 static int maxWarnings = 10;
258 if (index < 2) {
259 if (maxWarnings) {
260 jfs_warn("find_entry called with index = %d", index);
261 maxWarnings--;
263 return NULL;
266 if (index >= jfs_ip->next_index) {
267 jfs_warn("find_entry called with index >= next_index");
268 return NULL;
271 if (jfs_ip->next_index <= (MAX_INLINE_DIRTABLE_ENTRY + 1)) {
273 * Inline directory table
275 *mp = NULL;
276 slot = &jfs_ip->i_dirtable[index - 2];
277 } else {
278 offset = (index - 2) * sizeof(struct dir_table_slot);
279 page_offset = offset & (PSIZE - 1);
280 blkno = ((offset + 1) >> L2PSIZE) <<
281 JFS_SBI(ip->i_sb)->l2nbperpage;
283 if (*mp && (*lblock != blkno)) {
284 release_metapage(*mp);
285 *mp = NULL;
287 if (*mp == 0) {
288 *lblock = blkno;
289 *mp = read_index_page(ip, blkno);
291 if (*mp == 0) {
292 jfs_err("free_index: error reading directory table");
293 return NULL;
296 slot =
297 (struct dir_table_slot *) ((char *) (*mp)->data +
298 page_offset);
300 return slot;
303 static inline void lock_index(tid_t tid, struct inode *ip, struct metapage * mp,
304 u32 index)
306 struct tlock *tlck;
307 struct linelock *llck;
308 struct lv *lv;
310 tlck = txLock(tid, ip, mp, tlckDATA);
311 llck = (struct linelock *) tlck->lock;
313 if (llck->index >= llck->maxcnt)
314 llck = txLinelock(llck);
315 lv = &llck->lv[llck->index];
318 * Linelock slot size is twice the size of directory table
319 * slot size. 512 entries per page.
321 lv->offset = ((index - 2) & 511) >> 1;
322 lv->length = 1;
323 llck->index++;
327 * add_index()
329 * Adds an entry to the directory index table. This is used to provide
330 * each directory entry with a persistent index in which to resume
331 * directory traversals
333 static u32 add_index(tid_t tid, struct inode *ip, s64 bn, int slot)
335 struct super_block *sb = ip->i_sb;
336 struct jfs_sb_info *sbi = JFS_SBI(sb);
337 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
338 u64 blkno;
339 struct dir_table_slot *dirtab_slot;
340 u32 index;
341 struct linelock *llck;
342 struct lv *lv;
343 struct metapage *mp;
344 s64 offset;
345 uint page_offset;
346 struct tlock *tlck;
347 s64 xaddr;
349 ASSERT(DO_INDEX(ip));
351 if (jfs_ip->next_index < 2) {
352 jfs_warn("add_index: next_index = %d. Resetting!",
353 jfs_ip->next_index);
354 jfs_ip->next_index = 2;
357 index = jfs_ip->next_index++;
359 if (index <= MAX_INLINE_DIRTABLE_ENTRY) {
361 * i_size reflects size of index table, or 8 bytes per entry.
363 ip->i_size = (loff_t) (index - 1) << 3;
366 * dir table fits inline within inode
368 dirtab_slot = &jfs_ip->i_dirtable[index-2];
369 dirtab_slot->flag = DIR_INDEX_VALID;
370 dirtab_slot->slot = slot;
371 DTSaddress(dirtab_slot, bn);
373 set_cflag(COMMIT_Dirtable, ip);
375 return index;
377 if (index == (MAX_INLINE_DIRTABLE_ENTRY + 1)) {
378 struct dir_table_slot temp_table[12];
381 * It's time to move the inline table to an external
382 * page and begin to build the xtree
384 if (DQUOT_ALLOC_BLOCK(ip, sbi->nbperpage) ||
385 dbAlloc(ip, 0, sbi->nbperpage, &xaddr))
386 goto clean_up; /* No space */
389 * Save the table, we're going to overwrite it with the
390 * xtree root
392 memcpy(temp_table, &jfs_ip->i_dirtable, sizeof(temp_table));
395 * Initialize empty x-tree
397 xtInitRoot(tid, ip);
400 * Allocate the first block & add it to the xtree
402 if (xtInsert(tid, ip, 0, 0, sbi->nbperpage, &xaddr, 0)) {
403 /* This really shouldn't fail */
404 jfs_warn("add_index: xtInsert failed!");
405 memcpy(&jfs_ip->i_dirtable, temp_table,
406 sizeof (temp_table));
407 goto clean_up;
409 ip->i_size = PSIZE;
411 if ((mp = get_index_page(ip, 0)) == 0) {
412 jfs_err("add_index: get_metapage failed!");
413 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
414 memcpy(&jfs_ip->i_dirtable, temp_table,
415 sizeof (temp_table));
416 goto clean_up;
418 tlck = txLock(tid, ip, mp, tlckDATA);
419 llck = (struct linelock *) & tlck->lock;
420 ASSERT(llck->index == 0);
421 lv = &llck->lv[0];
423 lv->offset = 0;
424 lv->length = 6; /* tlckDATA slot size is 16 bytes */
425 llck->index++;
427 memcpy(mp->data, temp_table, sizeof(temp_table));
429 mark_metapage_dirty(mp);
430 release_metapage(mp);
433 * Logging is now directed by xtree tlocks
435 clear_cflag(COMMIT_Dirtable, ip);
438 offset = (index - 2) * sizeof(struct dir_table_slot);
439 page_offset = offset & (PSIZE - 1);
440 blkno = ((offset + 1) >> L2PSIZE) << sbi->l2nbperpage;
441 if (page_offset == 0) {
443 * This will be the beginning of a new page
445 xaddr = 0;
446 if (xtInsert(tid, ip, 0, blkno, sbi->nbperpage, &xaddr, 0)) {
447 jfs_warn("add_index: xtInsert failed!");
448 goto clean_up;
450 ip->i_size += PSIZE;
452 if ((mp = get_index_page(ip, blkno)))
453 memset(mp->data, 0, PSIZE); /* Just looks better */
454 else
455 xtTruncate(tid, ip, offset, COMMIT_PWMAP);
456 } else
457 mp = read_index_page(ip, blkno);
459 if (mp == 0) {
460 jfs_err("add_index: get/read_metapage failed!");
461 goto clean_up;
464 lock_index(tid, ip, mp, index);
466 dirtab_slot =
467 (struct dir_table_slot *) ((char *) mp->data + page_offset);
468 dirtab_slot->flag = DIR_INDEX_VALID;
469 dirtab_slot->slot = slot;
470 DTSaddress(dirtab_slot, bn);
472 mark_metapage_dirty(mp);
473 release_metapage(mp);
475 return index;
477 clean_up:
479 jfs_ip->next_index--;
481 return 0;
485 * free_index()
487 * Marks an entry to the directory index table as free.
489 static void free_index(tid_t tid, struct inode *ip, u32 index, u32 next)
491 struct dir_table_slot *dirtab_slot;
492 s64 lblock;
493 struct metapage *mp = NULL;
495 dirtab_slot = find_index(ip, index, &mp, &lblock);
497 if (dirtab_slot == 0)
498 return;
500 dirtab_slot->flag = DIR_INDEX_FREE;
501 dirtab_slot->slot = dirtab_slot->addr1 = 0;
502 dirtab_slot->addr2 = cpu_to_le32(next);
504 if (mp) {
505 lock_index(tid, ip, mp, index);
506 mark_metapage_dirty(mp);
507 release_metapage(mp);
508 } else
509 set_cflag(COMMIT_Dirtable, ip);
513 * modify_index()
515 * Changes an entry in the directory index table
517 static void modify_index(tid_t tid, struct inode *ip, u32 index, s64 bn,
518 int slot, struct metapage ** mp, u64 *lblock)
520 struct dir_table_slot *dirtab_slot;
522 dirtab_slot = find_index(ip, index, mp, lblock);
524 if (dirtab_slot == 0)
525 return;
527 DTSaddress(dirtab_slot, bn);
528 dirtab_slot->slot = slot;
530 if (*mp) {
531 lock_index(tid, ip, *mp, index);
532 mark_metapage_dirty(*mp);
533 } else
534 set_cflag(COMMIT_Dirtable, ip);
538 * read_index()
540 * reads a directory table slot
542 static int read_index(struct inode *ip, u32 index,
543 struct dir_table_slot * dirtab_slot)
545 s64 lblock;
546 struct metapage *mp = NULL;
547 struct dir_table_slot *slot;
549 slot = find_index(ip, index, &mp, &lblock);
550 if (slot == 0) {
551 return -EIO;
554 memcpy(dirtab_slot, slot, sizeof(struct dir_table_slot));
556 if (mp)
557 release_metapage(mp);
559 return 0;
563 * dtSearch()
565 * function:
566 * Search for the entry with specified key
568 * parameter:
570 * return: 0 - search result on stack, leaf page pinned;
571 * errno - I/O error
573 int dtSearch(struct inode *ip, struct component_name * key, ino_t * data,
574 struct btstack * btstack, int flag)
576 int rc = 0;
577 int cmp = 1; /* init for empty page */
578 s64 bn;
579 struct metapage *mp;
580 dtpage_t *p;
581 s8 *stbl;
582 int base, index, lim;
583 struct btframe *btsp;
584 pxd_t *pxd;
585 int psize = 288; /* initial in-line directory */
586 ino_t inumber;
587 struct component_name ciKey;
588 struct super_block *sb = ip->i_sb;
590 ciKey.name =
591 (wchar_t *) kmalloc((JFS_NAME_MAX + 1) * sizeof(wchar_t),
592 GFP_NOFS);
593 if (ciKey.name == 0) {
594 rc = -ENOMEM;
595 goto dtSearch_Exit2;
599 /* uppercase search key for c-i directory */
600 UniStrcpy(ciKey.name, key->name);
601 ciKey.namlen = key->namlen;
603 /* only uppercase if case-insensitive support is on */
604 if ((JFS_SBI(sb)->mntflag & JFS_OS2) == JFS_OS2) {
605 ciToUpper(&ciKey);
607 BT_CLR(btstack); /* reset stack */
609 /* init level count for max pages to split */
610 btstack->nsplit = 1;
613 * search down tree from root:
615 * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
616 * internal page, child page Pi contains entry with k, Ki <= K < Kj.
618 * if entry with search key K is not found
619 * internal page search find the entry with largest key Ki
620 * less than K which point to the child page to search;
621 * leaf page search find the entry with smallest key Kj
622 * greater than K so that the returned index is the position of
623 * the entry to be shifted right for insertion of new entry.
624 * for empty tree, search key is greater than any key of the tree.
626 * by convention, root bn = 0.
628 for (bn = 0;;) {
629 /* get/pin the page to search */
630 DT_GETPAGE(ip, bn, mp, psize, p, rc);
631 if (rc)
632 goto dtSearch_Exit1;
634 /* get sorted entry table of the page */
635 stbl = DT_GETSTBL(p);
638 * binary search with search key K on the current page.
640 for (base = 0, lim = p->header.nextindex; lim; lim >>= 1) {
641 index = base + (lim >> 1);
643 if (p->header.flag & BT_LEAF) {
644 /* uppercase leaf name to compare */
645 cmp =
646 ciCompare(&ciKey, p, stbl[index],
647 JFS_SBI(sb)->mntflag);
648 } else {
649 /* router key is in uppercase */
651 cmp = dtCompare(&ciKey, p, stbl[index]);
655 if (cmp == 0) {
657 * search hit
659 /* search hit - leaf page:
660 * return the entry found
662 if (p->header.flag & BT_LEAF) {
663 inumber = le32_to_cpu(
664 ((struct ldtentry *) & p->slot[stbl[index]])->inumber);
667 * search for JFS_LOOKUP
669 if (flag == JFS_LOOKUP) {
670 *data = inumber;
671 rc = 0;
672 goto out;
676 * search for JFS_CREATE
678 if (flag == JFS_CREATE) {
679 *data = inumber;
680 rc = -EEXIST;
681 goto out;
685 * search for JFS_REMOVE or JFS_RENAME
687 if ((flag == JFS_REMOVE ||
688 flag == JFS_RENAME) &&
689 *data != inumber) {
690 rc = -ESTALE;
691 goto out;
695 * JFS_REMOVE|JFS_FINDDIR|JFS_RENAME
697 /* save search result */
698 *data = inumber;
699 btsp = btstack->top;
700 btsp->bn = bn;
701 btsp->index = index;
702 btsp->mp = mp;
704 rc = 0;
705 goto dtSearch_Exit1;
708 /* search hit - internal page:
709 * descend/search its child page
711 goto getChild;
714 if (cmp > 0) {
715 base = index + 1;
716 --lim;
721 * search miss
723 * base is the smallest index with key (Kj) greater than
724 * search key (K) and may be zero or (maxindex + 1) index.
727 * search miss - leaf page
729 * return location of entry (base) where new entry with
730 * search key K is to be inserted.
732 if (p->header.flag & BT_LEAF) {
734 * search for JFS_LOOKUP, JFS_REMOVE, or JFS_RENAME
736 if (flag == JFS_LOOKUP || flag == JFS_REMOVE ||
737 flag == JFS_RENAME) {
738 rc = -ENOENT;
739 goto out;
743 * search for JFS_CREATE|JFS_FINDDIR:
745 * save search result
747 *data = 0;
748 btsp = btstack->top;
749 btsp->bn = bn;
750 btsp->index = base;
751 btsp->mp = mp;
753 rc = 0;
754 goto dtSearch_Exit1;
758 * search miss - internal page
760 * if base is non-zero, decrement base by one to get the parent
761 * entry of the child page to search.
763 index = base ? base - 1 : base;
766 * go down to child page
768 getChild:
769 /* update max. number of pages to split */
770 if (BT_STACK_FULL(btstack)) {
771 /* Something's corrupted, mark filesytem dirty so
772 * chkdsk will fix it.
774 jfs_error(sb, "stack overrun in dtSearch!");
775 BT_STACK_DUMP(btstack);
776 rc = -EIO;
777 goto out;
779 btstack->nsplit++;
781 /* push (bn, index) of the parent page/entry */
782 BT_PUSH(btstack, bn, index);
784 /* get the child page block number */
785 pxd = (pxd_t *) & p->slot[stbl[index]];
786 bn = addressPXD(pxd);
787 psize = lengthPXD(pxd) << JFS_SBI(ip->i_sb)->l2bsize;
789 /* unpin the parent page */
790 DT_PUTPAGE(mp);
793 out:
794 DT_PUTPAGE(mp);
796 dtSearch_Exit1:
798 kfree(ciKey.name);
800 dtSearch_Exit2:
802 return rc;
807 * dtInsert()
809 * function: insert an entry to directory tree
811 * parameter:
813 * return: 0 - success;
814 * errno - failure;
816 int dtInsert(tid_t tid, struct inode *ip,
817 struct component_name * name, ino_t * fsn, struct btstack * btstack)
819 int rc = 0;
820 struct metapage *mp; /* meta-page buffer */
821 dtpage_t *p; /* base B+-tree index page */
822 s64 bn;
823 int index;
824 struct dtsplit split; /* split information */
825 ddata_t data;
826 struct dt_lock *dtlck;
827 int n;
828 struct tlock *tlck;
829 struct lv *lv;
832 * retrieve search result
834 * dtSearch() returns (leaf page pinned, index at which to insert).
835 * n.b. dtSearch() may return index of (maxindex + 1) of
836 * the full page.
838 DT_GETSEARCH(ip, btstack->top, bn, mp, p, index);
841 * insert entry for new key
843 if (DO_INDEX(ip)) {
844 if (JFS_IP(ip)->next_index == DIREND) {
845 DT_PUTPAGE(mp);
846 return -EMLINK;
848 n = NDTLEAF(name->namlen);
849 data.leaf.tid = tid;
850 data.leaf.ip = ip;
851 } else {
852 n = NDTLEAF_LEGACY(name->namlen);
853 data.leaf.ip = NULL; /* signifies legacy directory format */
855 data.leaf.ino = cpu_to_le32(*fsn);
858 * leaf page does not have enough room for new entry:
860 * extend/split the leaf page;
862 * dtSplitUp() will insert the entry and unpin the leaf page.
864 if (n > p->header.freecnt) {
865 split.mp = mp;
866 split.index = index;
867 split.nslot = n;
868 split.key = name;
869 split.data = &data;
870 rc = dtSplitUp(tid, ip, &split, btstack);
871 return rc;
875 * leaf page does have enough room for new entry:
877 * insert the new data entry into the leaf page;
879 BT_MARK_DIRTY(mp, ip);
881 * acquire a transaction lock on the leaf page
883 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
884 dtlck = (struct dt_lock *) & tlck->lock;
885 ASSERT(dtlck->index == 0);
886 lv = & dtlck->lv[0];
888 /* linelock header */
889 lv->offset = 0;
890 lv->length = 1;
891 dtlck->index++;
893 dtInsertEntry(p, index, name, &data, &dtlck);
895 /* linelock stbl of non-root leaf page */
896 if (!(p->header.flag & BT_ROOT)) {
897 if (dtlck->index >= dtlck->maxcnt)
898 dtlck = (struct dt_lock *) txLinelock(dtlck);
899 lv = & dtlck->lv[dtlck->index];
900 n = index >> L2DTSLOTSIZE;
901 lv->offset = p->header.stblindex + n;
902 lv->length =
903 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) - n + 1;
904 dtlck->index++;
907 /* unpin the leaf page */
908 DT_PUTPAGE(mp);
910 return 0;
915 * dtSplitUp()
917 * function: propagate insertion bottom up;
919 * parameter:
921 * return: 0 - success;
922 * errno - failure;
923 * leaf page unpinned;
925 static int dtSplitUp(tid_t tid,
926 struct inode *ip, struct dtsplit * split, struct btstack * btstack)
928 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
929 int rc = 0;
930 struct metapage *smp;
931 dtpage_t *sp; /* split page */
932 struct metapage *rmp;
933 dtpage_t *rp; /* new right page split from sp */
934 pxd_t rpxd; /* new right page extent descriptor */
935 struct metapage *lmp;
936 dtpage_t *lp; /* left child page */
937 int skip; /* index of entry of insertion */
938 struct btframe *parent; /* parent page entry on traverse stack */
939 s64 xaddr, nxaddr;
940 int xlen, xsize;
941 struct pxdlist pxdlist;
942 pxd_t *pxd;
943 struct component_name key = { 0, NULL };
944 ddata_t *data = split->data;
945 int n;
946 struct dt_lock *dtlck;
947 struct tlock *tlck;
948 struct lv *lv;
949 int quota_allocation = 0;
951 /* get split page */
952 smp = split->mp;
953 sp = DT_PAGE(ip, smp);
955 key.name =
956 (wchar_t *) kmalloc((JFS_NAME_MAX + 2) * sizeof(wchar_t),
957 GFP_NOFS);
958 if (key.name == 0) {
959 DT_PUTPAGE(smp);
960 rc = -ENOMEM;
961 goto dtSplitUp_Exit;
965 * split leaf page
967 * The split routines insert the new entry, and
968 * acquire txLock as appropriate.
971 * split root leaf page:
973 if (sp->header.flag & BT_ROOT) {
975 * allocate a single extent child page
977 xlen = 1;
978 n = sbi->bsize >> L2DTSLOTSIZE;
979 n -= (n + 31) >> L2DTSLOTSIZE; /* stbl size */
980 n -= DTROOTMAXSLOT - sp->header.freecnt; /* header + entries */
981 if (n <= split->nslot)
982 xlen++;
983 if ((rc = dbAlloc(ip, 0, (s64) xlen, &xaddr))) {
984 DT_PUTPAGE(smp);
985 goto freeKeyName;
988 pxdlist.maxnpxd = 1;
989 pxdlist.npxd = 0;
990 pxd = &pxdlist.pxd[0];
991 PXDaddress(pxd, xaddr);
992 PXDlength(pxd, xlen);
993 split->pxdlist = &pxdlist;
994 rc = dtSplitRoot(tid, ip, split, &rmp);
996 if (rc)
997 dbFree(ip, xaddr, xlen);
998 else
999 DT_PUTPAGE(rmp);
1001 DT_PUTPAGE(smp);
1003 goto freeKeyName;
1007 * extend first leaf page
1009 * extend the 1st extent if less than buffer page size
1010 * (dtExtendPage() reurns leaf page unpinned)
1012 pxd = &sp->header.self;
1013 xlen = lengthPXD(pxd);
1014 xsize = xlen << sbi->l2bsize;
1015 if (xsize < PSIZE) {
1016 xaddr = addressPXD(pxd);
1017 n = xsize >> L2DTSLOTSIZE;
1018 n -= (n + 31) >> L2DTSLOTSIZE; /* stbl size */
1019 if ((n + sp->header.freecnt) <= split->nslot)
1020 n = xlen + (xlen << 1);
1021 else
1022 n = xlen;
1024 /* Allocate blocks to quota. */
1025 if (DQUOT_ALLOC_BLOCK(ip, n)) {
1026 rc = -EDQUOT;
1027 goto extendOut;
1029 quota_allocation += n;
1031 if ((rc = dbReAlloc(sbi->ipbmap, xaddr, (s64) xlen,
1032 (s64) n, &nxaddr)))
1033 goto extendOut;
1035 pxdlist.maxnpxd = 1;
1036 pxdlist.npxd = 0;
1037 pxd = &pxdlist.pxd[0];
1038 PXDaddress(pxd, nxaddr)
1039 PXDlength(pxd, xlen + n);
1040 split->pxdlist = &pxdlist;
1041 if ((rc = dtExtendPage(tid, ip, split, btstack))) {
1042 nxaddr = addressPXD(pxd);
1043 if (xaddr != nxaddr) {
1044 /* free relocated extent */
1045 xlen = lengthPXD(pxd);
1046 dbFree(ip, nxaddr, (s64) xlen);
1047 } else {
1048 /* free extended delta */
1049 xlen = lengthPXD(pxd) - n;
1050 xaddr = addressPXD(pxd) + xlen;
1051 dbFree(ip, xaddr, (s64) n);
1055 extendOut:
1056 DT_PUTPAGE(smp);
1057 goto freeKeyName;
1061 * split leaf page <sp> into <sp> and a new right page <rp>.
1063 * return <rp> pinned and its extent descriptor <rpxd>
1066 * allocate new directory page extent and
1067 * new index page(s) to cover page split(s)
1069 * allocation hint: ?
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;
1082 DT_PUTPAGE(smp);
1084 /* undo allocation */
1085 goto splitOut;
1088 split->pxdlist = &pxdlist;
1089 if ((rc = dtSplitPage(tid, ip, split, &rmp, &rp, &rpxd))) {
1090 DT_PUTPAGE(smp);
1092 /* undo allocation */
1093 goto splitOut;
1097 * propagate up the router entry for the leaf page just split
1099 * insert a router entry for the new page into the parent page,
1100 * propagate the insert/split up the tree by walking back the stack
1101 * of (bn of parent page, index of child page entry in parent page)
1102 * that were traversed during the search for the page that split.
1104 * the propagation of insert/split up the tree stops if the root
1105 * splits or the page inserted into doesn't have to split to hold
1106 * the new entry.
1108 * the parent entry for the split page remains the same, and
1109 * a new entry is inserted at its right with the first key and
1110 * block number of the new right page.
1112 * There are a maximum of 4 pages pinned at any time:
1113 * two children, left parent and right parent (when the parent splits).
1114 * keep the child pages pinned while working on the parent.
1115 * make sure that all pins are released at exit.
1117 while ((parent = BT_POP(btstack)) != NULL) {
1118 /* parent page specified by stack frame <parent> */
1120 /* keep current child pages (<lp>, <rp>) pinned */
1121 lmp = smp;
1122 lp = sp;
1125 * insert router entry in parent for new right child page <rp>
1127 /* get the parent page <sp> */
1128 DT_GETPAGE(ip, parent->bn, smp, PSIZE, sp, rc);
1129 if (rc) {
1130 DT_PUTPAGE(lmp);
1131 DT_PUTPAGE(rmp);
1132 goto splitOut;
1136 * The new key entry goes ONE AFTER the index of parent entry,
1137 * because the split was to the right.
1139 skip = parent->index + 1;
1142 * compute the key for the router entry
1144 * key suffix compression:
1145 * for internal pages that have leaf pages as children,
1146 * retain only what's needed to distinguish between
1147 * the new entry and the entry on the page to its left.
1148 * If the keys compare equal, retain the entire key.
1150 * note that compression is performed only at computing
1151 * router key at the lowest internal level.
1152 * further compression of the key between pairs of higher
1153 * level internal pages loses too much information and
1154 * the search may fail.
1155 * (e.g., two adjacent leaf pages of {a, ..., x} {xx, ...,}
1156 * results in two adjacent parent entries (a)(xx).
1157 * if split occurs between these two entries, and
1158 * if compression is applied, the router key of parent entry
1159 * of right page (x) will divert search for x into right
1160 * subtree and miss x in the left subtree.)
1162 * the entire key must be retained for the next-to-leftmost
1163 * internal key at any level of the tree, or search may fail
1164 * (e.g., ?)
1166 switch (rp->header.flag & BT_TYPE) {
1167 case BT_LEAF:
1169 * compute the length of prefix for suffix compression
1170 * between last entry of left page and first entry
1171 * of right page
1173 if ((sp->header.flag & BT_ROOT && skip > 1) ||
1174 sp->header.prev != 0 || skip > 1) {
1175 /* compute uppercase router prefix key */
1176 rc = ciGetLeafPrefixKey(lp,
1177 lp->header.nextindex-1,
1178 rp, 0, &key,
1179 sbi->mntflag);
1180 if (rc) {
1181 DT_PUTPAGE(lmp);
1182 DT_PUTPAGE(rmp);
1183 DT_PUTPAGE(smp);
1184 goto splitOut;
1186 } else {
1187 /* next to leftmost entry of
1188 lowest internal level */
1190 /* compute uppercase router key */
1191 dtGetKey(rp, 0, &key, sbi->mntflag);
1192 key.name[key.namlen] = 0;
1194 if ((sbi->mntflag & JFS_OS2) == JFS_OS2)
1195 ciToUpper(&key);
1198 n = NDTINTERNAL(key.namlen);
1199 break;
1201 case BT_INTERNAL:
1202 dtGetKey(rp, 0, &key, sbi->mntflag);
1203 n = NDTINTERNAL(key.namlen);
1204 break;
1206 default:
1207 jfs_err("dtSplitUp(): UFO!");
1208 break;
1211 /* unpin left child page */
1212 DT_PUTPAGE(lmp);
1215 * compute the data for the router entry
1217 data->xd = rpxd; /* child page xd */
1220 * parent page is full - split the parent page
1222 if (n > sp->header.freecnt) {
1223 /* init for parent page split */
1224 split->mp = smp;
1225 split->index = skip; /* index at insert */
1226 split->nslot = n;
1227 split->key = &key;
1228 /* split->data = data; */
1230 /* unpin right child page */
1231 DT_PUTPAGE(rmp);
1233 /* The split routines insert the new entry,
1234 * acquire txLock as appropriate.
1235 * return <rp> pinned and its block number <rbn>.
1237 rc = (sp->header.flag & BT_ROOT) ?
1238 dtSplitRoot(tid, ip, split, &rmp) :
1239 dtSplitPage(tid, ip, split, &rmp, &rp, &rpxd);
1240 if (rc) {
1241 DT_PUTPAGE(smp);
1242 goto splitOut;
1245 /* smp and rmp are pinned */
1248 * parent page is not full - insert router entry in parent page
1250 else {
1251 BT_MARK_DIRTY(smp, ip);
1253 * acquire a transaction lock on the parent page
1255 tlck = txLock(tid, ip, smp, tlckDTREE | tlckENTRY);
1256 dtlck = (struct dt_lock *) & tlck->lock;
1257 ASSERT(dtlck->index == 0);
1258 lv = & dtlck->lv[0];
1260 /* linelock header */
1261 lv->offset = 0;
1262 lv->length = 1;
1263 dtlck->index++;
1265 /* linelock stbl of non-root parent page */
1266 if (!(sp->header.flag & BT_ROOT)) {
1267 lv++;
1268 n = skip >> L2DTSLOTSIZE;
1269 lv->offset = sp->header.stblindex + n;
1270 lv->length =
1271 ((sp->header.nextindex -
1272 1) >> L2DTSLOTSIZE) - n + 1;
1273 dtlck->index++;
1276 dtInsertEntry(sp, skip, &key, data, &dtlck);
1278 /* exit propagate up */
1279 break;
1283 /* unpin current split and its right page */
1284 DT_PUTPAGE(smp);
1285 DT_PUTPAGE(rmp);
1288 * free remaining extents allocated for split
1290 splitOut:
1291 n = pxdlist.npxd;
1292 pxd = &pxdlist.pxd[n];
1293 for (; n < pxdlist.maxnpxd; n++, pxd++)
1294 dbFree(ip, addressPXD(pxd), (s64) lengthPXD(pxd));
1296 freeKeyName:
1297 kfree(key.name);
1299 /* Rollback quota allocation */
1300 if (rc && quota_allocation)
1301 DQUOT_FREE_BLOCK(ip, quota_allocation);
1303 dtSplitUp_Exit:
1305 return rc;
1310 * dtSplitPage()
1312 * function: Split a non-root page of a btree.
1314 * parameter:
1316 * return: 0 - success;
1317 * errno - failure;
1318 * return split and new page pinned;
1320 static int dtSplitPage(tid_t tid, struct inode *ip, struct dtsplit * split,
1321 struct metapage ** rmpp, dtpage_t ** rpp, pxd_t * rpxdp)
1323 int rc = 0;
1324 struct metapage *smp;
1325 dtpage_t *sp;
1326 struct metapage *rmp;
1327 dtpage_t *rp; /* new right page allocated */
1328 s64 rbn; /* new right page block number */
1329 struct metapage *mp;
1330 dtpage_t *p;
1331 s64 nextbn;
1332 struct pxdlist *pxdlist;
1333 pxd_t *pxd;
1334 int skip, nextindex, half, left, nxt, off, si;
1335 struct ldtentry *ldtentry;
1336 struct idtentry *idtentry;
1337 u8 *stbl;
1338 struct dtslot *f;
1339 int fsi, stblsize;
1340 int n;
1341 struct dt_lock *sdtlck, *rdtlck;
1342 struct tlock *tlck;
1343 struct dt_lock *dtlck;
1344 struct lv *slv, *rlv, *lv;
1346 /* get split page */
1347 smp = split->mp;
1348 sp = DT_PAGE(ip, smp);
1351 * allocate the new right page for the split
1353 pxdlist = split->pxdlist;
1354 pxd = &pxdlist->pxd[pxdlist->npxd];
1355 pxdlist->npxd++;
1356 rbn = addressPXD(pxd);
1357 rmp = get_metapage(ip, rbn, PSIZE, 1);
1358 if (rmp == NULL)
1359 return -EIO;
1361 /* Allocate blocks to quota. */
1362 if (DQUOT_ALLOC_BLOCK(ip, lengthPXD(pxd))) {
1363 release_metapage(rmp);
1364 return -EDQUOT;
1367 jfs_info("dtSplitPage: ip:0x%p smp:0x%p rmp:0x%p", ip, smp, rmp);
1369 BT_MARK_DIRTY(rmp, ip);
1371 * acquire a transaction lock on the new right page
1373 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckNEW);
1374 rdtlck = (struct dt_lock *) & tlck->lock;
1376 rp = (dtpage_t *) rmp->data;
1377 *rpp = rp;
1378 rp->header.self = *pxd;
1380 BT_MARK_DIRTY(smp, ip);
1382 * acquire a transaction lock on the split page
1384 * action:
1386 tlck = txLock(tid, ip, smp, tlckDTREE | tlckENTRY);
1387 sdtlck = (struct dt_lock *) & tlck->lock;
1389 /* linelock header of split page */
1390 ASSERT(sdtlck->index == 0);
1391 slv = & sdtlck->lv[0];
1392 slv->offset = 0;
1393 slv->length = 1;
1394 sdtlck->index++;
1397 * initialize/update sibling pointers between sp and rp
1399 nextbn = le64_to_cpu(sp->header.next);
1400 rp->header.next = cpu_to_le64(nextbn);
1401 rp->header.prev = cpu_to_le64(addressPXD(&sp->header.self));
1402 sp->header.next = cpu_to_le64(rbn);
1405 * initialize new right page
1407 rp->header.flag = sp->header.flag;
1409 /* compute sorted entry table at start of extent data area */
1410 rp->header.nextindex = 0;
1411 rp->header.stblindex = 1;
1413 n = PSIZE >> L2DTSLOTSIZE;
1414 rp->header.maxslot = n;
1415 stblsize = (n + 31) >> L2DTSLOTSIZE; /* in unit of slot */
1417 /* init freelist */
1418 fsi = rp->header.stblindex + stblsize;
1419 rp->header.freelist = fsi;
1420 rp->header.freecnt = rp->header.maxslot - fsi;
1423 * sequential append at tail: append without split
1425 * If splitting the last page on a level because of appending
1426 * a entry to it (skip is maxentry), it's likely that the access is
1427 * sequential. Adding an empty page on the side of the level is less
1428 * work and can push the fill factor much higher than normal.
1429 * If we're wrong it's no big deal, we'll just do the split the right
1430 * way next time.
1431 * (It may look like it's equally easy to do a similar hack for
1432 * reverse sorted data, that is, split the tree left,
1433 * but it's not. Be my guest.)
1435 if (nextbn == 0 && split->index == sp->header.nextindex) {
1436 /* linelock header + stbl (first slot) of new page */
1437 rlv = & rdtlck->lv[rdtlck->index];
1438 rlv->offset = 0;
1439 rlv->length = 2;
1440 rdtlck->index++;
1443 * initialize freelist of new right page
1445 f = &rp->slot[fsi];
1446 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1447 f->next = fsi;
1448 f->next = -1;
1450 /* insert entry at the first entry of the new right page */
1451 dtInsertEntry(rp, 0, split->key, split->data, &rdtlck);
1453 goto out;
1457 * non-sequential insert (at possibly middle page)
1461 * update prev pointer of previous right sibling page;
1463 if (nextbn != 0) {
1464 DT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
1465 if (rc) {
1466 discard_metapage(rmp);
1467 return rc;
1470 BT_MARK_DIRTY(mp, ip);
1472 * acquire a transaction lock on the next page
1474 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
1475 jfs_info("dtSplitPage: tlck = 0x%p, ip = 0x%p, mp=0x%p",
1476 tlck, ip, mp);
1477 dtlck = (struct dt_lock *) & tlck->lock;
1479 /* linelock header of previous right sibling page */
1480 lv = & dtlck->lv[dtlck->index];
1481 lv->offset = 0;
1482 lv->length = 1;
1483 dtlck->index++;
1485 p->header.prev = cpu_to_le64(rbn);
1487 DT_PUTPAGE(mp);
1491 * split the data between the split and right pages.
1493 skip = split->index;
1494 half = (PSIZE >> L2DTSLOTSIZE) >> 1; /* swag */
1495 left = 0;
1498 * compute fill factor for split pages
1500 * <nxt> traces the next entry to move to rp
1501 * <off> traces the next entry to stay in sp
1503 stbl = (u8 *) & sp->slot[sp->header.stblindex];
1504 nextindex = sp->header.nextindex;
1505 for (nxt = off = 0; nxt < nextindex; ++off) {
1506 if (off == skip)
1507 /* check for fill factor with new entry size */
1508 n = split->nslot;
1509 else {
1510 si = stbl[nxt];
1511 switch (sp->header.flag & BT_TYPE) {
1512 case BT_LEAF:
1513 ldtentry = (struct ldtentry *) & sp->slot[si];
1514 if (DO_INDEX(ip))
1515 n = NDTLEAF(ldtentry->namlen);
1516 else
1517 n = NDTLEAF_LEGACY(ldtentry->
1518 namlen);
1519 break;
1521 case BT_INTERNAL:
1522 idtentry = (struct idtentry *) & sp->slot[si];
1523 n = NDTINTERNAL(idtentry->namlen);
1524 break;
1526 default:
1527 break;
1530 ++nxt; /* advance to next entry to move in sp */
1533 left += n;
1534 if (left >= half)
1535 break;
1538 /* <nxt> poins to the 1st entry to move */
1541 * move entries to right page
1543 * dtMoveEntry() initializes rp and reserves entry for insertion
1545 * split page moved out entries are linelocked;
1546 * new/right page moved in entries are linelocked;
1548 /* linelock header + stbl of new right page */
1549 rlv = & rdtlck->lv[rdtlck->index];
1550 rlv->offset = 0;
1551 rlv->length = 5;
1552 rdtlck->index++;
1554 dtMoveEntry(sp, nxt, rp, &sdtlck, &rdtlck, DO_INDEX(ip));
1556 sp->header.nextindex = nxt;
1559 * finalize freelist of new right page
1561 fsi = rp->header.freelist;
1562 f = &rp->slot[fsi];
1563 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1564 f->next = fsi;
1565 f->next = -1;
1568 * Update directory index table for entries now in right page
1570 if ((rp->header.flag & BT_LEAF) && DO_INDEX(ip)) {
1571 s64 lblock;
1573 mp = NULL;
1574 stbl = DT_GETSTBL(rp);
1575 for (n = 0; n < rp->header.nextindex; n++) {
1576 ldtentry = (struct ldtentry *) & rp->slot[stbl[n]];
1577 modify_index(tid, ip, le32_to_cpu(ldtentry->index),
1578 rbn, n, &mp, &lblock);
1580 if (mp)
1581 release_metapage(mp);
1585 * the skipped index was on the left page,
1587 if (skip <= off) {
1588 /* insert the new entry in the split page */
1589 dtInsertEntry(sp, skip, split->key, split->data, &sdtlck);
1591 /* linelock stbl of split page */
1592 if (sdtlck->index >= sdtlck->maxcnt)
1593 sdtlck = (struct dt_lock *) txLinelock(sdtlck);
1594 slv = & sdtlck->lv[sdtlck->index];
1595 n = skip >> L2DTSLOTSIZE;
1596 slv->offset = sp->header.stblindex + n;
1597 slv->length =
1598 ((sp->header.nextindex - 1) >> L2DTSLOTSIZE) - n + 1;
1599 sdtlck->index++;
1602 * the skipped index was on the right page,
1604 else {
1605 /* adjust the skip index to reflect the new position */
1606 skip -= nxt;
1608 /* insert the new entry in the right page */
1609 dtInsertEntry(rp, skip, split->key, split->data, &rdtlck);
1612 out:
1613 *rmpp = rmp;
1614 *rpxdp = *pxd;
1616 return rc;
1621 * dtExtendPage()
1623 * function: extend 1st/only directory leaf page
1625 * parameter:
1627 * return: 0 - success;
1628 * errno - failure;
1629 * return extended page pinned;
1631 static int dtExtendPage(tid_t tid,
1632 struct inode *ip, struct dtsplit * split, struct btstack * btstack)
1634 struct super_block *sb = ip->i_sb;
1635 int rc;
1636 struct metapage *smp, *pmp, *mp;
1637 dtpage_t *sp, *pp;
1638 struct pxdlist *pxdlist;
1639 pxd_t *pxd, *tpxd;
1640 int xlen, xsize;
1641 int newstblindex, newstblsize;
1642 int oldstblindex, oldstblsize;
1643 int fsi, last;
1644 struct dtslot *f;
1645 struct btframe *parent;
1646 int n;
1647 struct dt_lock *dtlck;
1648 s64 xaddr, txaddr;
1649 struct tlock *tlck;
1650 struct pxd_lock *pxdlock;
1651 struct lv *lv;
1652 uint type;
1653 struct ldtentry *ldtentry;
1654 u8 *stbl;
1656 /* get page to extend */
1657 smp = split->mp;
1658 sp = DT_PAGE(ip, smp);
1660 /* get parent/root page */
1661 parent = BT_POP(btstack);
1662 DT_GETPAGE(ip, parent->bn, pmp, PSIZE, pp, rc);
1663 if (rc)
1664 return (rc);
1667 * extend the extent
1669 pxdlist = split->pxdlist;
1670 pxd = &pxdlist->pxd[pxdlist->npxd];
1671 pxdlist->npxd++;
1673 xaddr = addressPXD(pxd);
1674 tpxd = &sp->header.self;
1675 txaddr = addressPXD(tpxd);
1676 /* in-place extension */
1677 if (xaddr == txaddr) {
1678 type = tlckEXTEND;
1680 /* relocation */
1681 else {
1682 type = tlckNEW;
1684 /* save moved extent descriptor for later free */
1685 tlck = txMaplock(tid, ip, tlckDTREE | tlckRELOCATE);
1686 pxdlock = (struct pxd_lock *) & tlck->lock;
1687 pxdlock->flag = mlckFREEPXD;
1688 pxdlock->pxd = sp->header.self;
1689 pxdlock->index = 1;
1692 * Update directory index table to reflect new page address
1694 if (DO_INDEX(ip)) {
1695 s64 lblock;
1697 mp = NULL;
1698 stbl = DT_GETSTBL(sp);
1699 for (n = 0; n < sp->header.nextindex; n++) {
1700 ldtentry =
1701 (struct ldtentry *) & sp->slot[stbl[n]];
1702 modify_index(tid, ip,
1703 le32_to_cpu(ldtentry->index),
1704 xaddr, n, &mp, &lblock);
1706 if (mp)
1707 release_metapage(mp);
1712 * extend the page
1714 sp->header.self = *pxd;
1716 jfs_info("dtExtendPage: ip:0x%p smp:0x%p sp:0x%p", ip, smp, sp);
1718 BT_MARK_DIRTY(smp, ip);
1720 * acquire a transaction lock on the extended/leaf page
1722 tlck = txLock(tid, ip, smp, tlckDTREE | type);
1723 dtlck = (struct dt_lock *) & tlck->lock;
1724 lv = & dtlck->lv[0];
1726 /* update buffer extent descriptor of extended page */
1727 xlen = lengthPXD(pxd);
1728 xsize = xlen << JFS_SBI(sb)->l2bsize;
1729 #ifdef _STILL_TO_PORT
1730 bmSetXD(smp, xaddr, xsize);
1731 #endif /* _STILL_TO_PORT */
1734 * copy old stbl to new stbl at start of extended area
1736 oldstblindex = sp->header.stblindex;
1737 oldstblsize = (sp->header.maxslot + 31) >> L2DTSLOTSIZE;
1738 newstblindex = sp->header.maxslot;
1739 n = xsize >> L2DTSLOTSIZE;
1740 newstblsize = (n + 31) >> L2DTSLOTSIZE;
1741 memcpy(&sp->slot[newstblindex], &sp->slot[oldstblindex],
1742 sp->header.nextindex);
1745 * in-line extension: linelock old area of extended page
1747 if (type == tlckEXTEND) {
1748 /* linelock header */
1749 lv->offset = 0;
1750 lv->length = 1;
1751 dtlck->index++;
1752 lv++;
1754 /* linelock new stbl of extended page */
1755 lv->offset = newstblindex;
1756 lv->length = newstblsize;
1759 * relocation: linelock whole relocated area
1761 else {
1762 lv->offset = 0;
1763 lv->length = sp->header.maxslot + newstblsize;
1766 dtlck->index++;
1768 sp->header.maxslot = n;
1769 sp->header.stblindex = newstblindex;
1770 /* sp->header.nextindex remains the same */
1773 * add old stbl region at head of freelist
1775 fsi = oldstblindex;
1776 f = &sp->slot[fsi];
1777 last = sp->header.freelist;
1778 for (n = 0; n < oldstblsize; n++, fsi++, f++) {
1779 f->next = last;
1780 last = fsi;
1782 sp->header.freelist = last;
1783 sp->header.freecnt += oldstblsize;
1786 * append free region of newly extended area at tail of freelist
1788 /* init free region of newly extended area */
1789 fsi = n = newstblindex + newstblsize;
1790 f = &sp->slot[fsi];
1791 for (fsi++; fsi < sp->header.maxslot; f++, fsi++)
1792 f->next = fsi;
1793 f->next = -1;
1795 /* append new free region at tail of old freelist */
1796 fsi = sp->header.freelist;
1797 if (fsi == -1)
1798 sp->header.freelist = n;
1799 else {
1800 do {
1801 f = &sp->slot[fsi];
1802 fsi = f->next;
1803 } while (fsi != -1);
1805 f->next = n;
1808 sp->header.freecnt += sp->header.maxslot - n;
1811 * insert the new entry
1813 dtInsertEntry(sp, split->index, split->key, split->data, &dtlck);
1815 BT_MARK_DIRTY(pmp, ip);
1817 * linelock any freeslots residing in old extent
1819 if (type == tlckEXTEND) {
1820 n = sp->header.maxslot >> 2;
1821 if (sp->header.freelist < n)
1822 dtLinelockFreelist(sp, n, &dtlck);
1826 * update parent entry on the parent/root page
1829 * acquire a transaction lock on the parent/root page
1831 tlck = txLock(tid, ip, pmp, tlckDTREE | tlckENTRY);
1832 dtlck = (struct dt_lock *) & tlck->lock;
1833 lv = & dtlck->lv[dtlck->index];
1835 /* linelock parent entry - 1st slot */
1836 lv->offset = 1;
1837 lv->length = 1;
1838 dtlck->index++;
1840 /* update the parent pxd for page extension */
1841 tpxd = (pxd_t *) & pp->slot[1];
1842 *tpxd = *pxd;
1844 DT_PUTPAGE(pmp);
1845 return 0;
1850 * dtSplitRoot()
1852 * function:
1853 * split the full root page into
1854 * original/root/split page and new right page
1855 * i.e., root remains fixed in tree anchor (inode) and
1856 * the root is copied to a single new right child page
1857 * since root page << non-root page, and
1858 * the split root page contains a single entry for the
1859 * new right child page.
1861 * parameter:
1863 * return: 0 - success;
1864 * errno - failure;
1865 * return new page pinned;
1867 static int dtSplitRoot(tid_t tid,
1868 struct inode *ip, struct dtsplit * split, struct metapage ** rmpp)
1870 struct super_block *sb = ip->i_sb;
1871 struct metapage *smp;
1872 dtroot_t *sp;
1873 struct metapage *rmp;
1874 dtpage_t *rp;
1875 s64 rbn;
1876 int xlen;
1877 int xsize;
1878 struct dtslot *f;
1879 s8 *stbl;
1880 int fsi, stblsize, n;
1881 struct idtentry *s;
1882 pxd_t *ppxd;
1883 struct pxdlist *pxdlist;
1884 pxd_t *pxd;
1885 struct dt_lock *dtlck;
1886 struct tlock *tlck;
1887 struct lv *lv;
1889 /* get split root page */
1890 smp = split->mp;
1891 sp = &JFS_IP(ip)->i_dtroot;
1894 * allocate/initialize a single (right) child page
1896 * N.B. at first split, a one (or two) block to fit new entry
1897 * is allocated; at subsequent split, a full page is allocated;
1899 pxdlist = split->pxdlist;
1900 pxd = &pxdlist->pxd[pxdlist->npxd];
1901 pxdlist->npxd++;
1902 rbn = addressPXD(pxd);
1903 xlen = lengthPXD(pxd);
1904 xsize = xlen << JFS_SBI(sb)->l2bsize;
1905 rmp = get_metapage(ip, rbn, xsize, 1);
1906 if (!rmp)
1907 return -EIO;
1909 rp = rmp->data;
1911 /* Allocate blocks to quota. */
1912 if (DQUOT_ALLOC_BLOCK(ip, lengthPXD(pxd))) {
1913 release_metapage(rmp);
1914 return -EDQUOT;
1917 BT_MARK_DIRTY(rmp, ip);
1919 * acquire a transaction lock on the new right page
1921 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckNEW);
1922 dtlck = (struct dt_lock *) & tlck->lock;
1924 rp->header.flag =
1925 (sp->header.flag & BT_LEAF) ? BT_LEAF : BT_INTERNAL;
1926 rp->header.self = *pxd;
1928 /* initialize sibling pointers */
1929 rp->header.next = 0;
1930 rp->header.prev = 0;
1933 * move in-line root page into new right page extent
1935 /* linelock header + copied entries + new stbl (1st slot) in new page */
1936 ASSERT(dtlck->index == 0);
1937 lv = & dtlck->lv[0];
1938 lv->offset = 0;
1939 lv->length = 10; /* 1 + 8 + 1 */
1940 dtlck->index++;
1942 n = xsize >> L2DTSLOTSIZE;
1943 rp->header.maxslot = n;
1944 stblsize = (n + 31) >> L2DTSLOTSIZE;
1946 /* copy old stbl to new stbl at start of extended area */
1947 rp->header.stblindex = DTROOTMAXSLOT;
1948 stbl = (s8 *) & rp->slot[DTROOTMAXSLOT];
1949 memcpy(stbl, sp->header.stbl, sp->header.nextindex);
1950 rp->header.nextindex = sp->header.nextindex;
1952 /* copy old data area to start of new data area */
1953 memcpy(&rp->slot[1], &sp->slot[1], IDATASIZE);
1956 * append free region of newly extended area at tail of freelist
1958 /* init free region of newly extended area */
1959 fsi = n = DTROOTMAXSLOT + stblsize;
1960 f = &rp->slot[fsi];
1961 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1962 f->next = fsi;
1963 f->next = -1;
1965 /* append new free region at tail of old freelist */
1966 fsi = sp->header.freelist;
1967 if (fsi == -1)
1968 rp->header.freelist = n;
1969 else {
1970 rp->header.freelist = fsi;
1972 do {
1973 f = &rp->slot[fsi];
1974 fsi = f->next;
1975 } while (fsi != -1);
1977 f->next = n;
1980 rp->header.freecnt = sp->header.freecnt + rp->header.maxslot - n;
1983 * Update directory index table for entries now in right page
1985 if ((rp->header.flag & BT_LEAF) && DO_INDEX(ip)) {
1986 s64 lblock;
1987 struct metapage *mp = NULL;
1988 struct ldtentry *ldtentry;
1990 stbl = DT_GETSTBL(rp);
1991 for (n = 0; n < rp->header.nextindex; n++) {
1992 ldtentry = (struct ldtentry *) & rp->slot[stbl[n]];
1993 modify_index(tid, ip, le32_to_cpu(ldtentry->index),
1994 rbn, n, &mp, &lblock);
1996 if (mp)
1997 release_metapage(mp);
2000 * insert the new entry into the new right/child page
2001 * (skip index in the new right page will not change)
2003 dtInsertEntry(rp, split->index, split->key, split->data, &dtlck);
2006 * reset parent/root page
2008 * set the 1st entry offset to 0, which force the left-most key
2009 * at any level of the tree to be less than any search key.
2011 * The btree comparison code guarantees that the left-most key on any
2012 * level of the tree is never used, so it doesn't need to be filled in.
2014 BT_MARK_DIRTY(smp, ip);
2016 * acquire a transaction lock on the root page (in-memory inode)
2018 tlck = txLock(tid, ip, smp, tlckDTREE | tlckNEW | tlckBTROOT);
2019 dtlck = (struct dt_lock *) & tlck->lock;
2021 /* linelock root */
2022 ASSERT(dtlck->index == 0);
2023 lv = & dtlck->lv[0];
2024 lv->offset = 0;
2025 lv->length = DTROOTMAXSLOT;
2026 dtlck->index++;
2028 /* update page header of root */
2029 if (sp->header.flag & BT_LEAF) {
2030 sp->header.flag &= ~BT_LEAF;
2031 sp->header.flag |= BT_INTERNAL;
2034 /* init the first entry */
2035 s = (struct idtentry *) & sp->slot[DTENTRYSTART];
2036 ppxd = (pxd_t *) s;
2037 *ppxd = *pxd;
2038 s->next = -1;
2039 s->namlen = 0;
2041 stbl = sp->header.stbl;
2042 stbl[0] = DTENTRYSTART;
2043 sp->header.nextindex = 1;
2045 /* init freelist */
2046 fsi = DTENTRYSTART + 1;
2047 f = &sp->slot[fsi];
2049 /* init free region of remaining area */
2050 for (fsi++; fsi < DTROOTMAXSLOT; f++, fsi++)
2051 f->next = fsi;
2052 f->next = -1;
2054 sp->header.freelist = DTENTRYSTART + 1;
2055 sp->header.freecnt = DTROOTMAXSLOT - (DTENTRYSTART + 1);
2057 *rmpp = rmp;
2059 return 0;
2064 * dtDelete()
2066 * function: delete the entry(s) referenced by a key.
2068 * parameter:
2070 * return:
2072 int dtDelete(tid_t tid,
2073 struct inode *ip, struct component_name * key, ino_t * ino, int flag)
2075 int rc = 0;
2076 s64 bn;
2077 struct metapage *mp, *imp;
2078 dtpage_t *p;
2079 int index;
2080 struct btstack btstack;
2081 struct dt_lock *dtlck;
2082 struct tlock *tlck;
2083 struct lv *lv;
2084 int i;
2085 struct ldtentry *ldtentry;
2086 u8 *stbl;
2087 u32 table_index, next_index;
2088 struct metapage *nmp;
2089 dtpage_t *np;
2092 * search for the entry to delete:
2094 * dtSearch() returns (leaf page pinned, index at which to delete).
2096 if ((rc = dtSearch(ip, key, ino, &btstack, flag)))
2097 return rc;
2099 /* retrieve search result */
2100 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
2103 * We need to find put the index of the next entry into the
2104 * directory index table in order to resume a readdir from this
2105 * entry.
2107 if (DO_INDEX(ip)) {
2108 stbl = DT_GETSTBL(p);
2109 ldtentry = (struct ldtentry *) & p->slot[stbl[index]];
2110 table_index = le32_to_cpu(ldtentry->index);
2111 if (index == (p->header.nextindex - 1)) {
2113 * Last entry in this leaf page
2115 if ((p->header.flag & BT_ROOT)
2116 || (p->header.next == 0))
2117 next_index = -1;
2118 else {
2119 /* Read next leaf page */
2120 DT_GETPAGE(ip, le64_to_cpu(p->header.next),
2121 nmp, PSIZE, np, rc);
2122 if (rc)
2123 next_index = -1;
2124 else {
2125 stbl = DT_GETSTBL(np);
2126 ldtentry =
2127 (struct ldtentry *) & np->
2128 slot[stbl[0]];
2129 next_index =
2130 le32_to_cpu(ldtentry->index);
2131 DT_PUTPAGE(nmp);
2134 } else {
2135 ldtentry =
2136 (struct ldtentry *) & p->slot[stbl[index + 1]];
2137 next_index = le32_to_cpu(ldtentry->index);
2139 free_index(tid, ip, table_index, next_index);
2142 * the leaf page becomes empty, delete the page
2144 if (p->header.nextindex == 1) {
2145 /* delete empty page */
2146 rc = dtDeleteUp(tid, ip, mp, p, &btstack);
2149 * the leaf page has other entries remaining:
2151 * delete the entry from the leaf page.
2153 else {
2154 BT_MARK_DIRTY(mp, ip);
2156 * acquire a transaction lock on the leaf page
2158 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
2159 dtlck = (struct dt_lock *) & tlck->lock;
2162 * Do not assume that dtlck->index will be zero. During a
2163 * rename within a directory, this transaction may have
2164 * modified this page already when adding the new entry.
2167 /* linelock header */
2168 if (dtlck->index >= dtlck->maxcnt)
2169 dtlck = (struct dt_lock *) txLinelock(dtlck);
2170 lv = & dtlck->lv[dtlck->index];
2171 lv->offset = 0;
2172 lv->length = 1;
2173 dtlck->index++;
2175 /* linelock stbl of non-root leaf page */
2176 if (!(p->header.flag & BT_ROOT)) {
2177 if (dtlck->index >= dtlck->maxcnt)
2178 dtlck = (struct dt_lock *) txLinelock(dtlck);
2179 lv = & dtlck->lv[dtlck->index];
2180 i = index >> L2DTSLOTSIZE;
2181 lv->offset = p->header.stblindex + i;
2182 lv->length =
2183 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) -
2184 i + 1;
2185 dtlck->index++;
2188 /* free the leaf entry */
2189 dtDeleteEntry(p, index, &dtlck);
2192 * Update directory index table for entries moved in stbl
2194 if (DO_INDEX(ip) && index < p->header.nextindex) {
2195 s64 lblock;
2197 imp = NULL;
2198 stbl = DT_GETSTBL(p);
2199 for (i = index; i < p->header.nextindex; i++) {
2200 ldtentry =
2201 (struct ldtentry *) & p->slot[stbl[i]];
2202 modify_index(tid, ip,
2203 le32_to_cpu(ldtentry->index),
2204 bn, i, &imp, &lblock);
2206 if (imp)
2207 release_metapage(imp);
2210 DT_PUTPAGE(mp);
2213 return rc;
2218 * dtDeleteUp()
2220 * function:
2221 * free empty pages as propagating deletion up the tree
2223 * parameter:
2225 * return:
2227 static int dtDeleteUp(tid_t tid, struct inode *ip,
2228 struct metapage * fmp, dtpage_t * fp, struct btstack * btstack)
2230 int rc = 0;
2231 struct metapage *mp;
2232 dtpage_t *p;
2233 int index, nextindex;
2234 int xlen;
2235 struct btframe *parent;
2236 struct dt_lock *dtlck;
2237 struct tlock *tlck;
2238 struct lv *lv;
2239 struct pxd_lock *pxdlock;
2240 int i;
2243 * keep the root leaf page which has become empty
2245 if (BT_IS_ROOT(fmp)) {
2247 * reset the root
2249 * dtInitRoot() acquires txlock on the root
2251 dtInitRoot(tid, ip, PARENT(ip));
2253 DT_PUTPAGE(fmp);
2255 return 0;
2259 * free the non-root leaf page
2262 * acquire a transaction lock on the page
2264 * write FREEXTENT|NOREDOPAGE log record
2265 * N.B. linelock is overlaid as freed extent descriptor, and
2266 * the buffer page is freed;
2268 tlck = txMaplock(tid, ip, tlckDTREE | tlckFREE);
2269 pxdlock = (struct pxd_lock *) & tlck->lock;
2270 pxdlock->flag = mlckFREEPXD;
2271 pxdlock->pxd = fp->header.self;
2272 pxdlock->index = 1;
2274 /* update sibling pointers */
2275 if ((rc = dtRelink(tid, ip, fp))) {
2276 BT_PUTPAGE(fmp);
2277 return rc;
2280 xlen = lengthPXD(&fp->header.self);
2282 /* Free quota allocation. */
2283 DQUOT_FREE_BLOCK(ip, xlen);
2285 /* free/invalidate its buffer page */
2286 discard_metapage(fmp);
2289 * propagate page deletion up the directory tree
2291 * If the delete from the parent page makes it empty,
2292 * continue all the way up the tree.
2293 * stop if the root page is reached (which is never deleted) or
2294 * if the entry deletion does not empty the page.
2296 while ((parent = BT_POP(btstack)) != NULL) {
2297 /* pin the parent page <sp> */
2298 DT_GETPAGE(ip, parent->bn, mp, PSIZE, p, rc);
2299 if (rc)
2300 return rc;
2303 * free the extent of the child page deleted
2305 index = parent->index;
2308 * delete the entry for the child page from parent
2310 nextindex = p->header.nextindex;
2313 * the parent has the single entry being deleted:
2315 * free the parent page which has become empty.
2317 if (nextindex == 1) {
2319 * keep the root internal page which has become empty
2321 if (p->header.flag & BT_ROOT) {
2323 * reset the root
2325 * dtInitRoot() acquires txlock on the root
2327 dtInitRoot(tid, ip, PARENT(ip));
2329 DT_PUTPAGE(mp);
2331 return 0;
2334 * free the parent page
2336 else {
2338 * acquire a transaction lock on the page
2340 * write FREEXTENT|NOREDOPAGE log record
2342 tlck =
2343 txMaplock(tid, ip,
2344 tlckDTREE | tlckFREE);
2345 pxdlock = (struct pxd_lock *) & tlck->lock;
2346 pxdlock->flag = mlckFREEPXD;
2347 pxdlock->pxd = p->header.self;
2348 pxdlock->index = 1;
2350 /* update sibling pointers */
2351 if ((rc = dtRelink(tid, ip, p))) {
2352 DT_PUTPAGE(mp);
2353 return rc;
2356 xlen = lengthPXD(&p->header.self);
2358 /* Free quota allocation */
2359 DQUOT_FREE_BLOCK(ip, xlen);
2361 /* free/invalidate its buffer page */
2362 discard_metapage(mp);
2364 /* propagate up */
2365 continue;
2370 * the parent has other entries remaining:
2372 * delete the router entry from the parent page.
2374 BT_MARK_DIRTY(mp, ip);
2376 * acquire a transaction lock on the page
2378 * action: router entry deletion
2380 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
2381 dtlck = (struct dt_lock *) & tlck->lock;
2383 /* linelock header */
2384 if (dtlck->index >= dtlck->maxcnt)
2385 dtlck = (struct dt_lock *) txLinelock(dtlck);
2386 lv = & dtlck->lv[dtlck->index];
2387 lv->offset = 0;
2388 lv->length = 1;
2389 dtlck->index++;
2391 /* linelock stbl of non-root leaf page */
2392 if (!(p->header.flag & BT_ROOT)) {
2393 if (dtlck->index < dtlck->maxcnt)
2394 lv++;
2395 else {
2396 dtlck = (struct dt_lock *) txLinelock(dtlck);
2397 lv = & dtlck->lv[0];
2399 i = index >> L2DTSLOTSIZE;
2400 lv->offset = p->header.stblindex + i;
2401 lv->length =
2402 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) -
2403 i + 1;
2404 dtlck->index++;
2407 /* free the router entry */
2408 dtDeleteEntry(p, index, &dtlck);
2410 /* reset key of new leftmost entry of level (for consistency) */
2411 if (index == 0 &&
2412 ((p->header.flag & BT_ROOT) || p->header.prev == 0))
2413 dtTruncateEntry(p, 0, &dtlck);
2415 /* unpin the parent page */
2416 DT_PUTPAGE(mp);
2418 /* exit propagation up */
2419 break;
2422 return 0;
2425 #ifdef _NOTYET
2427 * NAME: dtRelocate()
2429 * FUNCTION: relocate dtpage (internal or leaf) of directory;
2430 * This function is mainly used by defragfs utility.
2432 int dtRelocate(tid_t tid, struct inode *ip, s64 lmxaddr, pxd_t * opxd,
2433 s64 nxaddr)
2435 int rc = 0;
2436 struct metapage *mp, *pmp, *lmp, *rmp;
2437 dtpage_t *p, *pp, *rp = 0, *lp= 0;
2438 s64 bn;
2439 int index;
2440 struct btstack btstack;
2441 pxd_t *pxd;
2442 s64 oxaddr, nextbn, prevbn;
2443 int xlen, xsize;
2444 struct tlock *tlck;
2445 struct dt_lock *dtlck;
2446 struct pxd_lock *pxdlock;
2447 s8 *stbl;
2448 struct lv *lv;
2450 oxaddr = addressPXD(opxd);
2451 xlen = lengthPXD(opxd);
2453 jfs_info("dtRelocate: lmxaddr:%Ld xaddr:%Ld:%Ld xlen:%d",
2454 (long long)lmxaddr, (long long)oxaddr, (long long)nxaddr,
2455 xlen);
2458 * 1. get the internal parent dtpage covering
2459 * router entry for the tartget page to be relocated;
2461 rc = dtSearchNode(ip, lmxaddr, opxd, &btstack);
2462 if (rc)
2463 return rc;
2465 /* retrieve search result */
2466 DT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
2467 jfs_info("dtRelocate: parent router entry validated.");
2470 * 2. relocate the target dtpage
2472 /* read in the target page from src extent */
2473 DT_GETPAGE(ip, oxaddr, mp, PSIZE, p, rc);
2474 if (rc) {
2475 /* release the pinned parent page */
2476 DT_PUTPAGE(pmp);
2477 return rc;
2481 * read in sibling pages if any to update sibling pointers;
2483 rmp = NULL;
2484 if (p->header.next) {
2485 nextbn = le64_to_cpu(p->header.next);
2486 DT_GETPAGE(ip, nextbn, rmp, PSIZE, rp, rc);
2487 if (rc) {
2488 DT_PUTPAGE(mp);
2489 DT_PUTPAGE(pmp);
2490 return (rc);
2494 lmp = NULL;
2495 if (p->header.prev) {
2496 prevbn = le64_to_cpu(p->header.prev);
2497 DT_GETPAGE(ip, prevbn, lmp, PSIZE, lp, rc);
2498 if (rc) {
2499 DT_PUTPAGE(mp);
2500 DT_PUTPAGE(pmp);
2501 if (rmp)
2502 DT_PUTPAGE(rmp);
2503 return (rc);
2507 /* at this point, all xtpages to be updated are in memory */
2510 * update sibling pointers of sibling dtpages if any;
2512 if (lmp) {
2513 tlck = txLock(tid, ip, lmp, tlckDTREE | tlckRELINK);
2514 dtlck = (struct dt_lock *) & tlck->lock;
2515 /* linelock header */
2516 ASSERT(dtlck->index == 0);
2517 lv = & dtlck->lv[0];
2518 lv->offset = 0;
2519 lv->length = 1;
2520 dtlck->index++;
2522 lp->header.next = cpu_to_le64(nxaddr);
2523 DT_PUTPAGE(lmp);
2526 if (rmp) {
2527 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckRELINK);
2528 dtlck = (struct dt_lock *) & tlck->lock;
2529 /* linelock header */
2530 ASSERT(dtlck->index == 0);
2531 lv = & dtlck->lv[0];
2532 lv->offset = 0;
2533 lv->length = 1;
2534 dtlck->index++;
2536 rp->header.prev = cpu_to_le64(nxaddr);
2537 DT_PUTPAGE(rmp);
2541 * update the target dtpage to be relocated
2543 * write LOG_REDOPAGE of LOG_NEW type for dst page
2544 * for the whole target page (logredo() will apply
2545 * after image and update bmap for allocation of the
2546 * dst extent), and update bmap for allocation of
2547 * the dst extent;
2549 tlck = txLock(tid, ip, mp, tlckDTREE | tlckNEW);
2550 dtlck = (struct dt_lock *) & tlck->lock;
2551 /* linelock header */
2552 ASSERT(dtlck->index == 0);
2553 lv = & dtlck->lv[0];
2555 /* update the self address in the dtpage header */
2556 pxd = &p->header.self;
2557 PXDaddress(pxd, nxaddr);
2559 /* the dst page is the same as the src page, i.e.,
2560 * linelock for afterimage of the whole page;
2562 lv->offset = 0;
2563 lv->length = p->header.maxslot;
2564 dtlck->index++;
2566 /* update the buffer extent descriptor of the dtpage */
2567 xsize = xlen << JFS_SBI(ip->i_sb)->l2bsize;
2568 #ifdef _STILL_TO_PORT
2569 bmSetXD(mp, nxaddr, xsize);
2570 #endif /* _STILL_TO_PORT */
2571 /* unpin the relocated page */
2572 DT_PUTPAGE(mp);
2573 jfs_info("dtRelocate: target dtpage relocated.");
2575 /* the moved extent is dtpage, then a LOG_NOREDOPAGE log rec
2576 * needs to be written (in logredo(), the LOG_NOREDOPAGE log rec
2577 * will also force a bmap update ).
2581 * 3. acquire maplock for the source extent to be freed;
2583 /* for dtpage relocation, write a LOG_NOREDOPAGE record
2584 * for the source dtpage (logredo() will init NoRedoPage
2585 * filter and will also update bmap for free of the source
2586 * dtpage), and upadte bmap for free of the source dtpage;
2588 tlck = txMaplock(tid, ip, tlckDTREE | tlckFREE);
2589 pxdlock = (struct pxd_lock *) & tlck->lock;
2590 pxdlock->flag = mlckFREEPXD;
2591 PXDaddress(&pxdlock->pxd, oxaddr);
2592 PXDlength(&pxdlock->pxd, xlen);
2593 pxdlock->index = 1;
2596 * 4. update the parent router entry for relocation;
2598 * acquire tlck for the parent entry covering the target dtpage;
2599 * write LOG_REDOPAGE to apply after image only;
2601 jfs_info("dtRelocate: update parent router entry.");
2602 tlck = txLock(tid, ip, pmp, tlckDTREE | tlckENTRY);
2603 dtlck = (struct dt_lock *) & tlck->lock;
2604 lv = & dtlck->lv[dtlck->index];
2606 /* update the PXD with the new address */
2607 stbl = DT_GETSTBL(pp);
2608 pxd = (pxd_t *) & pp->slot[stbl[index]];
2609 PXDaddress(pxd, nxaddr);
2610 lv->offset = stbl[index];
2611 lv->length = 1;
2612 dtlck->index++;
2614 /* unpin the parent dtpage */
2615 DT_PUTPAGE(pmp);
2617 return rc;
2621 * NAME: dtSearchNode()
2623 * FUNCTION: Search for an dtpage containing a specified address
2624 * This function is mainly used by defragfs utility.
2626 * NOTE: Search result on stack, the found page is pinned at exit.
2627 * The result page must be an internal dtpage.
2628 * lmxaddr give the address of the left most page of the
2629 * dtree level, in which the required dtpage resides.
2631 static int dtSearchNode(struct inode *ip, s64 lmxaddr, pxd_t * kpxd,
2632 struct btstack * btstack)
2634 int rc = 0;
2635 s64 bn;
2636 struct metapage *mp;
2637 dtpage_t *p;
2638 int psize = 288; /* initial in-line directory */
2639 s8 *stbl;
2640 int i;
2641 pxd_t *pxd;
2642 struct btframe *btsp;
2644 BT_CLR(btstack); /* reset stack */
2647 * descend tree to the level with specified leftmost page
2649 * by convention, root bn = 0.
2651 for (bn = 0;;) {
2652 /* get/pin the page to search */
2653 DT_GETPAGE(ip, bn, mp, psize, p, rc);
2654 if (rc)
2655 return rc;
2657 /* does the xaddr of leftmost page of the levevl
2658 * matches levevl search key ?
2660 if (p->header.flag & BT_ROOT) {
2661 if (lmxaddr == 0)
2662 break;
2663 } else if (addressPXD(&p->header.self) == lmxaddr)
2664 break;
2667 * descend down to leftmost child page
2669 if (p->header.flag & BT_LEAF) {
2670 DT_PUTPAGE(mp);
2671 return -ESTALE;
2674 /* get the leftmost entry */
2675 stbl = DT_GETSTBL(p);
2676 pxd = (pxd_t *) & p->slot[stbl[0]];
2678 /* get the child page block address */
2679 bn = addressPXD(pxd);
2680 psize = lengthPXD(pxd) << JFS_SBI(ip->i_sb)->l2bsize;
2681 /* unpin the parent page */
2682 DT_PUTPAGE(mp);
2686 * search each page at the current levevl
2688 loop:
2689 stbl = DT_GETSTBL(p);
2690 for (i = 0; i < p->header.nextindex; i++) {
2691 pxd = (pxd_t *) & p->slot[stbl[i]];
2693 /* found the specified router entry */
2694 if (addressPXD(pxd) == addressPXD(kpxd) &&
2695 lengthPXD(pxd) == lengthPXD(kpxd)) {
2696 btsp = btstack->top;
2697 btsp->bn = bn;
2698 btsp->index = i;
2699 btsp->mp = mp;
2701 return 0;
2705 /* get the right sibling page if any */
2706 if (p->header.next)
2707 bn = le64_to_cpu(p->header.next);
2708 else {
2709 DT_PUTPAGE(mp);
2710 return -ESTALE;
2713 /* unpin current page */
2714 DT_PUTPAGE(mp);
2716 /* get the right sibling page */
2717 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2718 if (rc)
2719 return rc;
2721 goto loop;
2723 #endif /* _NOTYET */
2726 * dtRelink()
2728 * function:
2729 * link around a freed page.
2731 * parameter:
2732 * fp: page to be freed
2734 * return:
2736 static int dtRelink(tid_t tid, struct inode *ip, dtpage_t * p)
2738 int rc;
2739 struct metapage *mp;
2740 s64 nextbn, prevbn;
2741 struct tlock *tlck;
2742 struct dt_lock *dtlck;
2743 struct lv *lv;
2745 nextbn = le64_to_cpu(p->header.next);
2746 prevbn = le64_to_cpu(p->header.prev);
2748 /* update prev pointer of the next page */
2749 if (nextbn != 0) {
2750 DT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
2751 if (rc)
2752 return rc;
2754 BT_MARK_DIRTY(mp, ip);
2756 * acquire a transaction lock on the next page
2758 * action: update prev pointer;
2760 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
2761 jfs_info("dtRelink nextbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2762 tlck, ip, mp);
2763 dtlck = (struct dt_lock *) & tlck->lock;
2765 /* linelock header */
2766 if (dtlck->index >= dtlck->maxcnt)
2767 dtlck = (struct dt_lock *) txLinelock(dtlck);
2768 lv = & dtlck->lv[dtlck->index];
2769 lv->offset = 0;
2770 lv->length = 1;
2771 dtlck->index++;
2773 p->header.prev = cpu_to_le64(prevbn);
2774 DT_PUTPAGE(mp);
2777 /* update next pointer of the previous page */
2778 if (prevbn != 0) {
2779 DT_GETPAGE(ip, prevbn, mp, PSIZE, p, rc);
2780 if (rc)
2781 return rc;
2783 BT_MARK_DIRTY(mp, ip);
2785 * acquire a transaction lock on the prev page
2787 * action: update next pointer;
2789 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
2790 jfs_info("dtRelink prevbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2791 tlck, ip, mp);
2792 dtlck = (struct dt_lock *) & tlck->lock;
2794 /* linelock header */
2795 if (dtlck->index >= dtlck->maxcnt)
2796 dtlck = (struct dt_lock *) txLinelock(dtlck);
2797 lv = & dtlck->lv[dtlck->index];
2798 lv->offset = 0;
2799 lv->length = 1;
2800 dtlck->index++;
2802 p->header.next = cpu_to_le64(nextbn);
2803 DT_PUTPAGE(mp);
2806 return 0;
2811 * dtInitRoot()
2813 * initialize directory root (inline in inode)
2815 void dtInitRoot(tid_t tid, struct inode *ip, u32 idotdot)
2817 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
2818 dtroot_t *p;
2819 int fsi;
2820 struct dtslot *f;
2821 struct tlock *tlck;
2822 struct dt_lock *dtlck;
2823 struct lv *lv;
2824 u16 xflag_save;
2827 * If this was previously an non-empty directory, we need to remove
2828 * the old directory table.
2830 if (DO_INDEX(ip)) {
2831 if (jfs_ip->next_index > (MAX_INLINE_DIRTABLE_ENTRY + 1)) {
2832 struct tblock *tblk = tid_to_tblock(tid);
2834 * We're playing games with the tid's xflag. If
2835 * we're removing a regular file, the file's xtree
2836 * is committed with COMMIT_PMAP, but we always
2837 * commit the directories xtree with COMMIT_PWMAP.
2839 xflag_save = tblk->xflag;
2840 tblk->xflag = 0;
2842 * xtTruncate isn't guaranteed to fully truncate
2843 * the xtree. The caller needs to check i_size
2844 * after committing the transaction to see if
2845 * additional truncation is needed. The
2846 * COMMIT_Stale flag tells caller that we
2847 * initiated the truncation.
2849 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
2850 set_cflag(COMMIT_Stale, ip);
2852 tblk->xflag = xflag_save;
2853 } else
2854 ip->i_size = 1;
2856 jfs_ip->next_index = 2;
2857 } else
2858 ip->i_size = IDATASIZE;
2861 * acquire a transaction lock on the root
2863 * action: directory initialization;
2865 tlck = txLock(tid, ip, (struct metapage *) & jfs_ip->bxflag,
2866 tlckDTREE | tlckENTRY | tlckBTROOT);
2867 dtlck = (struct dt_lock *) & tlck->lock;
2869 /* linelock root */
2870 ASSERT(dtlck->index == 0);
2871 lv = & dtlck->lv[0];
2872 lv->offset = 0;
2873 lv->length = DTROOTMAXSLOT;
2874 dtlck->index++;
2876 p = &jfs_ip->i_dtroot;
2878 p->header.flag = DXD_INDEX | BT_ROOT | BT_LEAF;
2880 p->header.nextindex = 0;
2882 /* init freelist */
2883 fsi = 1;
2884 f = &p->slot[fsi];
2886 /* init data area of root */
2887 for (fsi++; fsi < DTROOTMAXSLOT; f++, fsi++)
2888 f->next = fsi;
2889 f->next = -1;
2891 p->header.freelist = 1;
2892 p->header.freecnt = 8;
2894 /* init '..' entry */
2895 p->header.idotdot = cpu_to_le32(idotdot);
2897 return;
2901 * add_missing_indices()
2903 * function: Fix dtree page in which one or more entries has an invalid index.
2904 * fsck.jfs should really fix this, but it currently does not.
2905 * Called from jfs_readdir when bad index is detected.
2907 static void add_missing_indices(struct inode *inode, s64 bn)
2909 struct ldtentry *d;
2910 struct dt_lock *dtlck;
2911 int i;
2912 uint index;
2913 struct lv *lv;
2914 struct metapage *mp;
2915 dtpage_t *p;
2916 int rc;
2917 s8 *stbl;
2918 tid_t tid;
2919 struct tlock *tlck;
2921 tid = txBegin(inode->i_sb, 0);
2923 DT_GETPAGE(inode, bn, mp, PSIZE, p, rc);
2925 if (rc) {
2926 printk(KERN_ERR "DT_GETPAGE failed!\n");
2927 goto end;
2929 BT_MARK_DIRTY(mp, inode);
2931 ASSERT(p->header.flag & BT_LEAF);
2933 tlck = txLock(tid, inode, mp, tlckDTREE | tlckENTRY);
2934 dtlck = (struct dt_lock *) &tlck->lock;
2936 stbl = DT_GETSTBL(p);
2937 for (i = 0; i < p->header.nextindex; i++) {
2938 d = (struct ldtentry *) &p->slot[stbl[i]];
2939 index = le32_to_cpu(d->index);
2940 if ((index < 2) || (index >= JFS_IP(inode)->next_index)) {
2941 d->index = cpu_to_le32(add_index(tid, inode, bn, i));
2942 if (dtlck->index >= dtlck->maxcnt)
2943 dtlck = (struct dt_lock *) txLinelock(dtlck);
2944 lv = &dtlck->lv[dtlck->index];
2945 lv->offset = stbl[i];
2946 lv->length = 1;
2947 dtlck->index++;
2951 DT_PUTPAGE(mp);
2952 (void) txCommit(tid, 1, &inode, 0);
2953 end:
2954 txEnd(tid);
2958 * Buffer to hold directory entry info while traversing a dtree page
2959 * before being fed to the filldir function
2961 struct jfs_dirent {
2962 loff_t position;
2963 int ino;
2964 u16 name_len;
2965 char name[0];
2969 * function to determine next variable-sized jfs_dirent in buffer
2971 static inline struct jfs_dirent *next_jfs_dirent(struct jfs_dirent *dirent)
2973 return (struct jfs_dirent *)
2974 ((char *)dirent +
2975 ((sizeof (struct jfs_dirent) + dirent->name_len + 1 +
2976 sizeof (loff_t) - 1) &
2977 ~(sizeof (loff_t) - 1)));
2981 * jfs_readdir()
2983 * function: read directory entries sequentially
2984 * from the specified entry offset
2986 * parameter:
2988 * return: offset = (pn, index) of start entry
2989 * of next jfs_readdir()/dtRead()
2991 int jfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
2993 struct inode *ip = filp->f_dentry->d_inode;
2994 struct nls_table *codepage = JFS_SBI(ip->i_sb)->nls_tab;
2995 int rc = 0;
2996 loff_t dtpos; /* legacy OS/2 style position */
2997 struct dtoffset {
2998 s16 pn;
2999 s16 index;
3000 s32 unused;
3001 } *dtoffset = (struct dtoffset *) &dtpos;
3002 s64 bn;
3003 struct metapage *mp;
3004 dtpage_t *p;
3005 int index;
3006 s8 *stbl;
3007 struct btstack btstack;
3008 int i, next;
3009 struct ldtentry *d;
3010 struct dtslot *t;
3011 int d_namleft, len, outlen;
3012 unsigned long dirent_buf;
3013 char *name_ptr;
3014 u32 dir_index;
3015 int do_index = 0;
3016 uint loop_count = 0;
3017 struct jfs_dirent *jfs_dirent;
3018 int jfs_dirents;
3019 int overflow, fix_page, page_fixed = 0;
3020 static int unique_pos = 2; /* If we can't fix broken index */
3022 if (filp->f_pos == DIREND)
3023 return 0;
3025 if (DO_INDEX(ip)) {
3027 * persistent index is stored in directory entries.
3028 * Special cases: 0 = .
3029 * 1 = ..
3030 * -1 = End of directory
3032 do_index = 1;
3034 dir_index = (u32) filp->f_pos;
3036 if (dir_index > 1) {
3037 struct dir_table_slot dirtab_slot;
3039 if (dtEmpty(ip) ||
3040 (dir_index >= JFS_IP(ip)->next_index)) {
3041 /* Stale position. Directory has shrunk */
3042 filp->f_pos = DIREND;
3043 return 0;
3045 repeat:
3046 rc = read_index(ip, dir_index, &dirtab_slot);
3047 if (rc) {
3048 filp->f_pos = DIREND;
3049 return rc;
3051 if (dirtab_slot.flag == DIR_INDEX_FREE) {
3052 if (loop_count++ > JFS_IP(ip)->next_index) {
3053 jfs_err("jfs_readdir detected "
3054 "infinite loop!");
3055 filp->f_pos = DIREND;
3056 return 0;
3058 dir_index = le32_to_cpu(dirtab_slot.addr2);
3059 if (dir_index == -1) {
3060 filp->f_pos = DIREND;
3061 return 0;
3063 goto repeat;
3065 bn = addressDTS(&dirtab_slot);
3066 index = dirtab_slot.slot;
3067 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3068 if (rc) {
3069 filp->f_pos = DIREND;
3070 return 0;
3072 if (p->header.flag & BT_INTERNAL) {
3073 jfs_err("jfs_readdir: bad index table");
3074 DT_PUTPAGE(mp);
3075 filp->f_pos = -1;
3076 return 0;
3078 } else {
3079 if (dir_index == 0) {
3081 * self "."
3083 filp->f_pos = 0;
3084 if (filldir(dirent, ".", 1, 0, ip->i_ino,
3085 DT_DIR))
3086 return 0;
3089 * parent ".."
3091 filp->f_pos = 1;
3092 if (filldir(dirent, "..", 2, 1, PARENT(ip), DT_DIR))
3093 return 0;
3096 * Find first entry of left-most leaf
3098 if (dtEmpty(ip)) {
3099 filp->f_pos = DIREND;
3100 return 0;
3103 if ((rc = dtReadFirst(ip, &btstack)))
3104 return rc;
3106 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
3108 } else {
3110 * Legacy filesystem - OS/2 & Linux JFS < 0.3.6
3112 * pn = index = 0: First entry "."
3113 * pn = 0; index = 1: Second entry ".."
3114 * pn > 0: Real entries, pn=1 -> leftmost page
3115 * pn = index = -1: No more entries
3117 dtpos = filp->f_pos;
3118 if (dtpos == 0) {
3119 /* build "." entry */
3121 if (filldir(dirent, ".", 1, filp->f_pos, ip->i_ino,
3122 DT_DIR))
3123 return 0;
3124 dtoffset->index = 1;
3125 filp->f_pos = dtpos;
3128 if (dtoffset->pn == 0) {
3129 if (dtoffset->index == 1) {
3130 /* build ".." entry */
3132 if (filldir(dirent, "..", 2, filp->f_pos,
3133 PARENT(ip), DT_DIR))
3134 return 0;
3135 } else {
3136 jfs_err("jfs_readdir called with "
3137 "invalid offset!");
3139 dtoffset->pn = 1;
3140 dtoffset->index = 0;
3141 filp->f_pos = dtpos;
3144 if (dtEmpty(ip)) {
3145 filp->f_pos = DIREND;
3146 return 0;
3149 if ((rc = dtReadNext(ip, &filp->f_pos, &btstack))) {
3150 jfs_err("jfs_readdir: unexpected rc = %d "
3151 "from dtReadNext", rc);
3152 filp->f_pos = DIREND;
3153 return 0;
3155 /* get start leaf page and index */
3156 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
3158 /* offset beyond directory eof ? */
3159 if (bn < 0) {
3160 filp->f_pos = DIREND;
3161 return 0;
3165 dirent_buf = __get_free_page(GFP_KERNEL);
3166 if (dirent_buf == 0) {
3167 DT_PUTPAGE(mp);
3168 jfs_warn("jfs_readdir: __get_free_page failed!");
3169 filp->f_pos = DIREND;
3170 return -ENOMEM;
3173 while (1) {
3174 jfs_dirent = (struct jfs_dirent *) dirent_buf;
3175 jfs_dirents = 0;
3176 overflow = fix_page = 0;
3178 stbl = DT_GETSTBL(p);
3180 for (i = index; i < p->header.nextindex; i++) {
3181 d = (struct ldtentry *) & p->slot[stbl[i]];
3183 if (((long) jfs_dirent + d->namlen + 1) >
3184 (dirent_buf + PSIZE)) {
3185 /* DBCS codepages could overrun dirent_buf */
3186 index = i;
3187 overflow = 1;
3188 break;
3191 d_namleft = d->namlen;
3192 name_ptr = jfs_dirent->name;
3193 jfs_dirent->ino = le32_to_cpu(d->inumber);
3195 if (do_index) {
3196 len = min(d_namleft, DTLHDRDATALEN);
3197 jfs_dirent->position = le32_to_cpu(d->index);
3199 * d->index should always be valid, but it
3200 * isn't. fsck.jfs doesn't create the
3201 * directory index for the lost+found
3202 * directory. Rather than let it go,
3203 * we can try to fix it.
3205 if ((jfs_dirent->position < 2) ||
3206 (jfs_dirent->position >=
3207 JFS_IP(ip)->next_index)) {
3208 if (!page_fixed && !isReadOnly(ip)) {
3209 fix_page = 1;
3211 * setting overflow and setting
3212 * index to i will cause the
3213 * same page to be processed
3214 * again starting here
3216 overflow = 1;
3217 index = i;
3218 break;
3220 jfs_dirent->position = unique_pos++;
3222 } else {
3223 jfs_dirent->position = dtpos;
3224 len = min(d_namleft, DTLHDRDATALEN_LEGACY);
3227 /* copy the name of head/only segment */
3228 outlen = jfs_strfromUCS_le(name_ptr, d->name, len,
3229 codepage);
3230 jfs_dirent->name_len = outlen;
3232 /* copy name in the additional segment(s) */
3233 next = d->next;
3234 while (next >= 0) {
3235 t = (struct dtslot *) & p->slot[next];
3236 name_ptr += outlen;
3237 d_namleft -= len;
3238 /* Sanity Check */
3239 if (d_namleft == 0) {
3240 jfs_error(ip->i_sb,
3241 "JFS:Dtree error: ino = "
3242 "%ld, bn=%Ld, index = %d",
3243 (long)ip->i_ino,
3244 (long long)bn,
3246 goto skip_one;
3248 len = min(d_namleft, DTSLOTDATALEN);
3249 outlen = jfs_strfromUCS_le(name_ptr, t->name,
3250 len, codepage);
3251 jfs_dirent->name_len += outlen;
3253 next = t->next;
3256 jfs_dirents++;
3257 jfs_dirent = next_jfs_dirent(jfs_dirent);
3258 skip_one:
3259 if (!do_index)
3260 dtoffset->index++;
3263 if (!overflow) {
3264 /* Point to next leaf page */
3265 if (p->header.flag & BT_ROOT)
3266 bn = 0;
3267 else {
3268 bn = le64_to_cpu(p->header.next);
3269 index = 0;
3270 /* update offset (pn:index) for new page */
3271 if (!do_index) {
3272 dtoffset->pn++;
3273 dtoffset->index = 0;
3276 page_fixed = 0;
3279 /* unpin previous leaf page */
3280 DT_PUTPAGE(mp);
3282 jfs_dirent = (struct jfs_dirent *) dirent_buf;
3283 while (jfs_dirents--) {
3284 filp->f_pos = jfs_dirent->position;
3285 if (filldir(dirent, jfs_dirent->name,
3286 jfs_dirent->name_len, filp->f_pos,
3287 jfs_dirent->ino, DT_UNKNOWN))
3288 goto out;
3289 jfs_dirent = next_jfs_dirent(jfs_dirent);
3292 if (fix_page) {
3293 add_missing_indices(ip, bn);
3294 page_fixed = 1;
3297 if (!overflow && (bn == 0)) {
3298 filp->f_pos = DIREND;
3299 break;
3302 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3303 if (rc) {
3304 free_page(dirent_buf);
3305 return rc;
3309 out:
3310 free_page(dirent_buf);
3312 return rc;
3317 * dtReadFirst()
3319 * function: get the leftmost page of the directory
3321 static int dtReadFirst(struct inode *ip, struct btstack * btstack)
3323 int rc = 0;
3324 s64 bn;
3325 int psize = 288; /* initial in-line directory */
3326 struct metapage *mp;
3327 dtpage_t *p;
3328 s8 *stbl;
3329 struct btframe *btsp;
3330 pxd_t *xd;
3332 BT_CLR(btstack); /* reset stack */
3335 * descend leftmost path of the tree
3337 * by convention, root bn = 0.
3339 for (bn = 0;;) {
3340 DT_GETPAGE(ip, bn, mp, psize, p, rc);
3341 if (rc)
3342 return rc;
3345 * leftmost leaf page
3347 if (p->header.flag & BT_LEAF) {
3348 /* return leftmost entry */
3349 btsp = btstack->top;
3350 btsp->bn = bn;
3351 btsp->index = 0;
3352 btsp->mp = mp;
3354 return 0;
3358 * descend down to leftmost child page
3360 if (BT_STACK_FULL(btstack)) {
3361 DT_PUTPAGE(mp);
3362 jfs_error(ip->i_sb, "dtReadFirst: btstack overrun");
3363 BT_STACK_DUMP(btstack);
3364 return -EIO;
3366 /* push (bn, index) of the parent page/entry */
3367 BT_PUSH(btstack, bn, 0);
3369 /* get the leftmost entry */
3370 stbl = DT_GETSTBL(p);
3371 xd = (pxd_t *) & p->slot[stbl[0]];
3373 /* get the child page block address */
3374 bn = addressPXD(xd);
3375 psize = lengthPXD(xd) << JFS_SBI(ip->i_sb)->l2bsize;
3377 /* unpin the parent page */
3378 DT_PUTPAGE(mp);
3384 * dtReadNext()
3386 * function: get the page of the specified offset (pn:index)
3388 * return: if (offset > eof), bn = -1;
3390 * note: if index > nextindex of the target leaf page,
3391 * start with 1st entry of next leaf page;
3393 static int dtReadNext(struct inode *ip, loff_t * offset,
3394 struct btstack * btstack)
3396 int rc = 0;
3397 struct dtoffset {
3398 s16 pn;
3399 s16 index;
3400 s32 unused;
3401 } *dtoffset = (struct dtoffset *) offset;
3402 s64 bn;
3403 struct metapage *mp;
3404 dtpage_t *p;
3405 int index;
3406 int pn;
3407 s8 *stbl;
3408 struct btframe *btsp, *parent;
3409 pxd_t *xd;
3412 * get leftmost leaf page pinned
3414 if ((rc = dtReadFirst(ip, btstack)))
3415 return rc;
3417 /* get leaf page */
3418 DT_GETSEARCH(ip, btstack->top, bn, mp, p, index);
3420 /* get the start offset (pn:index) */
3421 pn = dtoffset->pn - 1; /* Now pn = 0 represents leftmost leaf */
3422 index = dtoffset->index;
3424 /* start at leftmost page ? */
3425 if (pn == 0) {
3426 /* offset beyond eof ? */
3427 if (index < p->header.nextindex)
3428 goto out;
3430 if (p->header.flag & BT_ROOT) {
3431 bn = -1;
3432 goto out;
3435 /* start with 1st entry of next leaf page */
3436 dtoffset->pn++;
3437 dtoffset->index = index = 0;
3438 goto a;
3441 /* start at non-leftmost page: scan parent pages for large pn */
3442 if (p->header.flag & BT_ROOT) {
3443 bn = -1;
3444 goto out;
3447 /* start after next leaf page ? */
3448 if (pn > 1)
3449 goto b;
3451 /* get leaf page pn = 1 */
3453 bn = le64_to_cpu(p->header.next);
3455 /* unpin leaf page */
3456 DT_PUTPAGE(mp);
3458 /* offset beyond eof ? */
3459 if (bn == 0) {
3460 bn = -1;
3461 goto out;
3464 goto c;
3467 * scan last internal page level to get target leaf page
3470 /* unpin leftmost leaf page */
3471 DT_PUTPAGE(mp);
3473 /* get left most parent page */
3474 btsp = btstack->top;
3475 parent = btsp - 1;
3476 bn = parent->bn;
3477 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3478 if (rc)
3479 return rc;
3481 /* scan parent pages at last internal page level */
3482 while (pn >= p->header.nextindex) {
3483 pn -= p->header.nextindex;
3485 /* get next parent page address */
3486 bn = le64_to_cpu(p->header.next);
3488 /* unpin current parent page */
3489 DT_PUTPAGE(mp);
3491 /* offset beyond eof ? */
3492 if (bn == 0) {
3493 bn = -1;
3494 goto out;
3497 /* get next parent page */
3498 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3499 if (rc)
3500 return rc;
3502 /* update parent page stack frame */
3503 parent->bn = bn;
3506 /* get leaf page address */
3507 stbl = DT_GETSTBL(p);
3508 xd = (pxd_t *) & p->slot[stbl[pn]];
3509 bn = addressPXD(xd);
3511 /* unpin parent page */
3512 DT_PUTPAGE(mp);
3515 * get target leaf page
3518 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3519 if (rc)
3520 return rc;
3523 * leaf page has been completed:
3524 * start with 1st entry of next leaf page
3526 if (index >= p->header.nextindex) {
3527 bn = le64_to_cpu(p->header.next);
3529 /* unpin leaf page */
3530 DT_PUTPAGE(mp);
3532 /* offset beyond eof ? */
3533 if (bn == 0) {
3534 bn = -1;
3535 goto out;
3538 /* get next leaf page */
3539 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3540 if (rc)
3541 return rc;
3543 /* start with 1st entry of next leaf page */
3544 dtoffset->pn++;
3545 dtoffset->index = 0;
3548 out:
3549 /* return target leaf page pinned */
3550 btsp = btstack->top;
3551 btsp->bn = bn;
3552 btsp->index = dtoffset->index;
3553 btsp->mp = mp;
3555 return 0;
3560 * dtCompare()
3562 * function: compare search key with an internal entry
3564 * return:
3565 * < 0 if k is < record
3566 * = 0 if k is = record
3567 * > 0 if k is > record
3569 static int dtCompare(struct component_name * key, /* search key */
3570 dtpage_t * p, /* directory page */
3571 int si)
3572 { /* entry slot index */
3573 wchar_t *kname, *name;
3574 int klen, namlen, len, rc;
3575 struct idtentry *ih;
3576 struct dtslot *t;
3579 * force the left-most key on internal pages, at any level of
3580 * the tree, to be less than any search key.
3581 * this obviates having to update the leftmost key on an internal
3582 * page when the user inserts a new key in the tree smaller than
3583 * anything that has been stored.
3585 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3586 * at any internal page at any level of the tree,
3587 * it descends to child of the entry anyway -
3588 * ? make the entry as min size dummy entry)
3590 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3591 * return (1);
3594 kname = key->name;
3595 klen = key->namlen;
3597 ih = (struct idtentry *) & p->slot[si];
3598 si = ih->next;
3599 name = ih->name;
3600 namlen = ih->namlen;
3601 len = min(namlen, DTIHDRDATALEN);
3603 /* compare with head/only segment */
3604 len = min(klen, len);
3605 if ((rc = UniStrncmp_le(kname, name, len)))
3606 return rc;
3608 klen -= len;
3609 namlen -= len;
3611 /* compare with additional segment(s) */
3612 kname += len;
3613 while (klen > 0 && namlen > 0) {
3614 /* compare with next name segment */
3615 t = (struct dtslot *) & p->slot[si];
3616 len = min(namlen, DTSLOTDATALEN);
3617 len = min(klen, len);
3618 name = t->name;
3619 if ((rc = UniStrncmp_le(kname, name, len)))
3620 return rc;
3622 klen -= len;
3623 namlen -= len;
3624 kname += len;
3625 si = t->next;
3628 return (klen - namlen);
3635 * ciCompare()
3637 * function: compare search key with an (leaf/internal) entry
3639 * return:
3640 * < 0 if k is < record
3641 * = 0 if k is = record
3642 * > 0 if k is > record
3644 static int ciCompare(struct component_name * key, /* search key */
3645 dtpage_t * p, /* directory page */
3646 int si, /* entry slot index */
3647 int flag)
3649 wchar_t *kname, *name, x;
3650 int klen, namlen, len, rc;
3651 struct ldtentry *lh;
3652 struct idtentry *ih;
3653 struct dtslot *t;
3654 int i;
3657 * force the left-most key on internal pages, at any level of
3658 * the tree, to be less than any search key.
3659 * this obviates having to update the leftmost key on an internal
3660 * page when the user inserts a new key in the tree smaller than
3661 * anything that has been stored.
3663 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3664 * at any internal page at any level of the tree,
3665 * it descends to child of the entry anyway -
3666 * ? make the entry as min size dummy entry)
3668 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3669 * return (1);
3672 kname = key->name;
3673 klen = key->namlen;
3676 * leaf page entry
3678 if (p->header.flag & BT_LEAF) {
3679 lh = (struct ldtentry *) & p->slot[si];
3680 si = lh->next;
3681 name = lh->name;
3682 namlen = lh->namlen;
3683 if (flag & JFS_DIR_INDEX)
3684 len = min(namlen, DTLHDRDATALEN);
3685 else
3686 len = min(namlen, DTLHDRDATALEN_LEGACY);
3689 * internal page entry
3691 else {
3692 ih = (struct idtentry *) & p->slot[si];
3693 si = ih->next;
3694 name = ih->name;
3695 namlen = ih->namlen;
3696 len = min(namlen, DTIHDRDATALEN);
3699 /* compare with head/only segment */
3700 len = min(klen, len);
3701 for (i = 0; i < len; i++, kname++, name++) {
3702 /* only uppercase if case-insensitive support is on */
3703 if ((flag & JFS_OS2) == JFS_OS2)
3704 x = UniToupper(le16_to_cpu(*name));
3705 else
3706 x = le16_to_cpu(*name);
3707 if ((rc = *kname - x))
3708 return rc;
3711 klen -= len;
3712 namlen -= len;
3714 /* compare with additional segment(s) */
3715 while (klen > 0 && namlen > 0) {
3716 /* compare with next name segment */
3717 t = (struct dtslot *) & p->slot[si];
3718 len = min(namlen, DTSLOTDATALEN);
3719 len = min(klen, len);
3720 name = t->name;
3721 for (i = 0; i < len; i++, kname++, name++) {
3722 /* only uppercase if case-insensitive support is on */
3723 if ((flag & JFS_OS2) == JFS_OS2)
3724 x = UniToupper(le16_to_cpu(*name));
3725 else
3726 x = le16_to_cpu(*name);
3728 if ((rc = *kname - x))
3729 return rc;
3732 klen -= len;
3733 namlen -= len;
3734 si = t->next;
3737 return (klen - namlen);
3742 * ciGetLeafPrefixKey()
3744 * function: compute prefix of suffix compression
3745 * from two adjacent leaf entries
3746 * across page boundary
3748 * return: non-zero on error
3751 static int ciGetLeafPrefixKey(dtpage_t * lp, int li, dtpage_t * rp,
3752 int ri, struct component_name * key, int flag)
3754 int klen, namlen;
3755 wchar_t *pl, *pr, *kname;
3756 struct component_name lkey;
3757 struct component_name rkey;
3759 lkey.name = (wchar_t *) kmalloc((JFS_NAME_MAX + 1) * sizeof(wchar_t),
3760 GFP_KERNEL);
3761 if (lkey.name == NULL)
3762 return -ENOSPC;
3764 rkey.name = (wchar_t *) kmalloc((JFS_NAME_MAX + 1) * sizeof(wchar_t),
3765 GFP_KERNEL);
3766 if (rkey.name == NULL) {
3767 kfree(lkey.name);
3768 return -ENOSPC;
3771 /* get left and right key */
3772 dtGetKey(lp, li, &lkey, flag);
3773 lkey.name[lkey.namlen] = 0;
3775 if ((flag & JFS_OS2) == JFS_OS2)
3776 ciToUpper(&lkey);
3778 dtGetKey(rp, ri, &rkey, flag);
3779 rkey.name[rkey.namlen] = 0;
3782 if ((flag & JFS_OS2) == JFS_OS2)
3783 ciToUpper(&rkey);
3785 /* compute prefix */
3786 klen = 0;
3787 kname = key->name;
3788 namlen = min(lkey.namlen, rkey.namlen);
3789 for (pl = lkey.name, pr = rkey.name;
3790 namlen; pl++, pr++, namlen--, klen++, kname++) {
3791 *kname = *pr;
3792 if (*pl != *pr) {
3793 key->namlen = klen + 1;
3794 goto free_names;
3798 /* l->namlen <= r->namlen since l <= r */
3799 if (lkey.namlen < rkey.namlen) {
3800 *kname = *pr;
3801 key->namlen = klen + 1;
3802 } else /* l->namelen == r->namelen */
3803 key->namlen = klen;
3805 free_names:
3806 kfree(lkey.name);
3807 kfree(rkey.name);
3808 return 0;
3814 * dtGetKey()
3816 * function: get key of the entry
3818 static void dtGetKey(dtpage_t * p, int i, /* entry index */
3819 struct component_name * key, int flag)
3821 int si;
3822 s8 *stbl;
3823 struct ldtentry *lh;
3824 struct idtentry *ih;
3825 struct dtslot *t;
3826 int namlen, len;
3827 wchar_t *name, *kname;
3829 /* get entry */
3830 stbl = DT_GETSTBL(p);
3831 si = stbl[i];
3832 if (p->header.flag & BT_LEAF) {
3833 lh = (struct ldtentry *) & p->slot[si];
3834 si = lh->next;
3835 namlen = lh->namlen;
3836 name = lh->name;
3837 if (flag & JFS_DIR_INDEX)
3838 len = min(namlen, DTLHDRDATALEN);
3839 else
3840 len = min(namlen, DTLHDRDATALEN_LEGACY);
3841 } else {
3842 ih = (struct idtentry *) & p->slot[si];
3843 si = ih->next;
3844 namlen = ih->namlen;
3845 name = ih->name;
3846 len = min(namlen, DTIHDRDATALEN);
3849 key->namlen = namlen;
3850 kname = key->name;
3853 * move head/only segment
3855 UniStrncpy_le(kname, name, len);
3858 * move additional segment(s)
3860 while (si >= 0) {
3861 /* get next segment */
3862 t = &p->slot[si];
3863 kname += len;
3864 namlen -= len;
3865 len = min(namlen, DTSLOTDATALEN);
3866 UniStrncpy_le(kname, t->name, len);
3868 si = t->next;
3874 * dtInsertEntry()
3876 * function: allocate free slot(s) and
3877 * write a leaf/internal entry
3879 * return: entry slot index
3881 static void dtInsertEntry(dtpage_t * p, int index, struct component_name * key,
3882 ddata_t * data, struct dt_lock ** dtlock)
3884 struct dtslot *h, *t;
3885 struct ldtentry *lh = NULL;
3886 struct idtentry *ih = NULL;
3887 int hsi, fsi, klen, len, nextindex;
3888 wchar_t *kname, *name;
3889 s8 *stbl;
3890 pxd_t *xd;
3891 struct dt_lock *dtlck = *dtlock;
3892 struct lv *lv;
3893 int xsi, n;
3894 s64 bn = 0;
3895 struct metapage *mp = NULL;
3897 klen = key->namlen;
3898 kname = key->name;
3900 /* allocate a free slot */
3901 hsi = fsi = p->header.freelist;
3902 h = &p->slot[fsi];
3903 p->header.freelist = h->next;
3904 --p->header.freecnt;
3906 /* open new linelock */
3907 if (dtlck->index >= dtlck->maxcnt)
3908 dtlck = (struct dt_lock *) txLinelock(dtlck);
3910 lv = & dtlck->lv[dtlck->index];
3911 lv->offset = hsi;
3913 /* write head/only segment */
3914 if (p->header.flag & BT_LEAF) {
3915 lh = (struct ldtentry *) h;
3916 lh->next = h->next;
3917 lh->inumber = data->leaf.ino; /* little-endian */
3918 lh->namlen = klen;
3919 name = lh->name;
3920 if (data->leaf.ip) {
3921 len = min(klen, DTLHDRDATALEN);
3922 if (!(p->header.flag & BT_ROOT))
3923 bn = addressPXD(&p->header.self);
3924 lh->index = cpu_to_le32(add_index(data->leaf.tid,
3925 data->leaf.ip,
3926 bn, index));
3927 } else
3928 len = min(klen, DTLHDRDATALEN_LEGACY);
3929 } else {
3930 ih = (struct idtentry *) h;
3931 ih->next = h->next;
3932 xd = (pxd_t *) ih;
3933 *xd = data->xd;
3934 ih->namlen = klen;
3935 name = ih->name;
3936 len = min(klen, DTIHDRDATALEN);
3939 UniStrncpy_le(name, kname, len);
3941 n = 1;
3942 xsi = hsi;
3944 /* write additional segment(s) */
3945 t = h;
3946 klen -= len;
3947 while (klen) {
3948 /* get free slot */
3949 fsi = p->header.freelist;
3950 t = &p->slot[fsi];
3951 p->header.freelist = t->next;
3952 --p->header.freecnt;
3954 /* is next slot contiguous ? */
3955 if (fsi != xsi + 1) {
3956 /* close current linelock */
3957 lv->length = n;
3958 dtlck->index++;
3960 /* open new linelock */
3961 if (dtlck->index < dtlck->maxcnt)
3962 lv++;
3963 else {
3964 dtlck = (struct dt_lock *) txLinelock(dtlck);
3965 lv = & dtlck->lv[0];
3968 lv->offset = fsi;
3969 n = 0;
3972 kname += len;
3973 len = min(klen, DTSLOTDATALEN);
3974 UniStrncpy_le(t->name, kname, len);
3976 n++;
3977 xsi = fsi;
3978 klen -= len;
3981 /* close current linelock */
3982 lv->length = n;
3983 dtlck->index++;
3985 *dtlock = dtlck;
3987 /* terminate last/only segment */
3988 if (h == t) {
3989 /* single segment entry */
3990 if (p->header.flag & BT_LEAF)
3991 lh->next = -1;
3992 else
3993 ih->next = -1;
3994 } else
3995 /* multi-segment entry */
3996 t->next = -1;
3998 /* if insert into middle, shift right succeeding entries in stbl */
3999 stbl = DT_GETSTBL(p);
4000 nextindex = p->header.nextindex;
4001 if (index < nextindex) {
4002 memmove(stbl + index + 1, stbl + index, nextindex - index);
4004 if ((p->header.flag & BT_LEAF) && data->leaf.ip) {
4005 s64 lblock;
4008 * Need to update slot number for entries that moved
4009 * in the stbl
4011 mp = NULL;
4012 for (n = index + 1; n <= nextindex; n++) {
4013 lh = (struct ldtentry *) & (p->slot[stbl[n]]);
4014 modify_index(data->leaf.tid, data->leaf.ip,
4015 le32_to_cpu(lh->index), bn, n,
4016 &mp, &lblock);
4018 if (mp)
4019 release_metapage(mp);
4023 stbl[index] = hsi;
4025 /* advance next available entry index of stbl */
4026 ++p->header.nextindex;
4031 * dtMoveEntry()
4033 * function: move entries from split/left page to new/right page
4035 * nextindex of dst page and freelist/freecnt of both pages
4036 * are updated.
4038 static void dtMoveEntry(dtpage_t * sp, int si, dtpage_t * dp,
4039 struct dt_lock ** sdtlock, struct dt_lock ** ddtlock,
4040 int do_index)
4042 int ssi, next; /* src slot index */
4043 int di; /* dst entry index */
4044 int dsi; /* dst slot index */
4045 s8 *sstbl, *dstbl; /* sorted entry table */
4046 int snamlen, len;
4047 struct ldtentry *slh, *dlh = NULL;
4048 struct idtentry *sih, *dih = NULL;
4049 struct dtslot *h, *s, *d;
4050 struct dt_lock *sdtlck = *sdtlock, *ddtlck = *ddtlock;
4051 struct lv *slv, *dlv;
4052 int xssi, ns, nd;
4053 int sfsi;
4055 sstbl = (s8 *) & sp->slot[sp->header.stblindex];
4056 dstbl = (s8 *) & dp->slot[dp->header.stblindex];
4058 dsi = dp->header.freelist; /* first (whole page) free slot */
4059 sfsi = sp->header.freelist;
4061 /* linelock destination entry slot */
4062 dlv = & ddtlck->lv[ddtlck->index];
4063 dlv->offset = dsi;
4065 /* linelock source entry slot */
4066 slv = & sdtlck->lv[sdtlck->index];
4067 slv->offset = sstbl[si];
4068 xssi = slv->offset - 1;
4071 * move entries
4073 ns = nd = 0;
4074 for (di = 0; si < sp->header.nextindex; si++, di++) {
4075 ssi = sstbl[si];
4076 dstbl[di] = dsi;
4078 /* is next slot contiguous ? */
4079 if (ssi != xssi + 1) {
4080 /* close current linelock */
4081 slv->length = ns;
4082 sdtlck->index++;
4084 /* open new linelock */
4085 if (sdtlck->index < sdtlck->maxcnt)
4086 slv++;
4087 else {
4088 sdtlck = (struct dt_lock *) txLinelock(sdtlck);
4089 slv = & sdtlck->lv[0];
4092 slv->offset = ssi;
4093 ns = 0;
4097 * move head/only segment of an entry
4099 /* get dst slot */
4100 h = d = &dp->slot[dsi];
4102 /* get src slot and move */
4103 s = &sp->slot[ssi];
4104 if (sp->header.flag & BT_LEAF) {
4105 /* get source entry */
4106 slh = (struct ldtentry *) s;
4107 dlh = (struct ldtentry *) h;
4108 snamlen = slh->namlen;
4110 if (do_index) {
4111 len = min(snamlen, DTLHDRDATALEN);
4112 dlh->index = slh->index; /* little-endian */
4113 } else
4114 len = min(snamlen, DTLHDRDATALEN_LEGACY);
4116 memcpy(dlh, slh, 6 + len * 2);
4118 next = slh->next;
4120 /* update dst head/only segment next field */
4121 dsi++;
4122 dlh->next = dsi;
4123 } else {
4124 sih = (struct idtentry *) s;
4125 snamlen = sih->namlen;
4127 len = min(snamlen, DTIHDRDATALEN);
4128 dih = (struct idtentry *) h;
4129 memcpy(dih, sih, 10 + len * 2);
4130 next = sih->next;
4132 dsi++;
4133 dih->next = dsi;
4136 /* free src head/only segment */
4137 s->next = sfsi;
4138 s->cnt = 1;
4139 sfsi = ssi;
4141 ns++;
4142 nd++;
4143 xssi = ssi;
4146 * move additional segment(s) of the entry
4148 snamlen -= len;
4149 while ((ssi = next) >= 0) {
4150 /* is next slot contiguous ? */
4151 if (ssi != xssi + 1) {
4152 /* close current linelock */
4153 slv->length = ns;
4154 sdtlck->index++;
4156 /* open new linelock */
4157 if (sdtlck->index < sdtlck->maxcnt)
4158 slv++;
4159 else {
4160 sdtlck =
4161 (struct dt_lock *)
4162 txLinelock(sdtlck);
4163 slv = & sdtlck->lv[0];
4166 slv->offset = ssi;
4167 ns = 0;
4170 /* get next source segment */
4171 s = &sp->slot[ssi];
4173 /* get next destination free slot */
4174 d++;
4176 len = min(snamlen, DTSLOTDATALEN);
4177 UniStrncpy(d->name, s->name, len);
4179 ns++;
4180 nd++;
4181 xssi = ssi;
4183 dsi++;
4184 d->next = dsi;
4186 /* free source segment */
4187 next = s->next;
4188 s->next = sfsi;
4189 s->cnt = 1;
4190 sfsi = ssi;
4192 snamlen -= len;
4193 } /* end while */
4195 /* terminate dst last/only segment */
4196 if (h == d) {
4197 /* single segment entry */
4198 if (dp->header.flag & BT_LEAF)
4199 dlh->next = -1;
4200 else
4201 dih->next = -1;
4202 } else
4203 /* multi-segment entry */
4204 d->next = -1;
4205 } /* end for */
4207 /* close current linelock */
4208 slv->length = ns;
4209 sdtlck->index++;
4210 *sdtlock = sdtlck;
4212 dlv->length = nd;
4213 ddtlck->index++;
4214 *ddtlock = ddtlck;
4216 /* update source header */
4217 sp->header.freelist = sfsi;
4218 sp->header.freecnt += nd;
4220 /* update destination header */
4221 dp->header.nextindex = di;
4223 dp->header.freelist = dsi;
4224 dp->header.freecnt -= nd;
4229 * dtDeleteEntry()
4231 * function: free a (leaf/internal) entry
4233 * log freelist header, stbl, and each segment slot of entry
4234 * (even though last/only segment next field is modified,
4235 * physical image logging requires all segment slots of
4236 * the entry logged to avoid applying previous updates
4237 * to the same slots)
4239 static void dtDeleteEntry(dtpage_t * p, int fi, struct dt_lock ** dtlock)
4241 int fsi; /* free entry slot index */
4242 s8 *stbl;
4243 struct dtslot *t;
4244 int si, freecnt;
4245 struct dt_lock *dtlck = *dtlock;
4246 struct lv *lv;
4247 int xsi, n;
4249 /* get free entry slot index */
4250 stbl = DT_GETSTBL(p);
4251 fsi = stbl[fi];
4253 /* open new linelock */
4254 if (dtlck->index >= dtlck->maxcnt)
4255 dtlck = (struct dt_lock *) txLinelock(dtlck);
4256 lv = & dtlck->lv[dtlck->index];
4258 lv->offset = fsi;
4260 /* get the head/only segment */
4261 t = &p->slot[fsi];
4262 if (p->header.flag & BT_LEAF)
4263 si = ((struct ldtentry *) t)->next;
4264 else
4265 si = ((struct idtentry *) t)->next;
4266 t->next = si;
4267 t->cnt = 1;
4269 n = freecnt = 1;
4270 xsi = fsi;
4272 /* find the last/only segment */
4273 while (si >= 0) {
4274 /* is next slot contiguous ? */
4275 if (si != xsi + 1) {
4276 /* close current linelock */
4277 lv->length = n;
4278 dtlck->index++;
4280 /* open new linelock */
4281 if (dtlck->index < dtlck->maxcnt)
4282 lv++;
4283 else {
4284 dtlck = (struct dt_lock *) txLinelock(dtlck);
4285 lv = & dtlck->lv[0];
4288 lv->offset = si;
4289 n = 0;
4292 n++;
4293 xsi = si;
4294 freecnt++;
4296 t = &p->slot[si];
4297 t->cnt = 1;
4298 si = t->next;
4301 /* close current linelock */
4302 lv->length = n;
4303 dtlck->index++;
4305 *dtlock = dtlck;
4307 /* update freelist */
4308 t->next = p->header.freelist;
4309 p->header.freelist = fsi;
4310 p->header.freecnt += freecnt;
4312 /* if delete from middle,
4313 * shift left the succedding entries in the stbl
4315 si = p->header.nextindex;
4316 if (fi < si - 1)
4317 memmove(&stbl[fi], &stbl[fi + 1], si - fi - 1);
4319 p->header.nextindex--;
4324 * dtTruncateEntry()
4326 * function: truncate a (leaf/internal) entry
4328 * log freelist header, stbl, and each segment slot of entry
4329 * (even though last/only segment next field is modified,
4330 * physical image logging requires all segment slots of
4331 * the entry logged to avoid applying previous updates
4332 * to the same slots)
4334 static void dtTruncateEntry(dtpage_t * p, int ti, struct dt_lock ** dtlock)
4336 int tsi; /* truncate entry slot index */
4337 s8 *stbl;
4338 struct dtslot *t;
4339 int si, freecnt;
4340 struct dt_lock *dtlck = *dtlock;
4341 struct lv *lv;
4342 int fsi, xsi, n;
4344 /* get free entry slot index */
4345 stbl = DT_GETSTBL(p);
4346 tsi = stbl[ti];
4348 /* open new linelock */
4349 if (dtlck->index >= dtlck->maxcnt)
4350 dtlck = (struct dt_lock *) txLinelock(dtlck);
4351 lv = & dtlck->lv[dtlck->index];
4353 lv->offset = tsi;
4355 /* get the head/only segment */
4356 t = &p->slot[tsi];
4357 ASSERT(p->header.flag & BT_INTERNAL);
4358 ((struct idtentry *) t)->namlen = 0;
4359 si = ((struct idtentry *) t)->next;
4360 ((struct idtentry *) t)->next = -1;
4362 n = 1;
4363 freecnt = 0;
4364 fsi = si;
4365 xsi = tsi;
4367 /* find the last/only segment */
4368 while (si >= 0) {
4369 /* is next slot contiguous ? */
4370 if (si != xsi + 1) {
4371 /* close current linelock */
4372 lv->length = n;
4373 dtlck->index++;
4375 /* open new linelock */
4376 if (dtlck->index < dtlck->maxcnt)
4377 lv++;
4378 else {
4379 dtlck = (struct dt_lock *) txLinelock(dtlck);
4380 lv = & dtlck->lv[0];
4383 lv->offset = si;
4384 n = 0;
4387 n++;
4388 xsi = si;
4389 freecnt++;
4391 t = &p->slot[si];
4392 t->cnt = 1;
4393 si = t->next;
4396 /* close current linelock */
4397 lv->length = n;
4398 dtlck->index++;
4400 *dtlock = dtlck;
4402 /* update freelist */
4403 if (freecnt == 0)
4404 return;
4405 t->next = p->header.freelist;
4406 p->header.freelist = fsi;
4407 p->header.freecnt += freecnt;
4412 * dtLinelockFreelist()
4414 static void dtLinelockFreelist(dtpage_t * p, /* directory page */
4415 int m, /* max slot index */
4416 struct dt_lock ** dtlock)
4418 int fsi; /* free entry slot index */
4419 struct dtslot *t;
4420 int si;
4421 struct dt_lock *dtlck = *dtlock;
4422 struct lv *lv;
4423 int xsi, n;
4425 /* get free entry slot index */
4426 fsi = p->header.freelist;
4428 /* open new linelock */
4429 if (dtlck->index >= dtlck->maxcnt)
4430 dtlck = (struct dt_lock *) txLinelock(dtlck);
4431 lv = & dtlck->lv[dtlck->index];
4433 lv->offset = fsi;
4435 n = 1;
4436 xsi = fsi;
4438 t = &p->slot[fsi];
4439 si = t->next;
4441 /* find the last/only segment */
4442 while (si < m && si >= 0) {
4443 /* is next slot contiguous ? */
4444 if (si != xsi + 1) {
4445 /* close current linelock */
4446 lv->length = n;
4447 dtlck->index++;
4449 /* open new linelock */
4450 if (dtlck->index < dtlck->maxcnt)
4451 lv++;
4452 else {
4453 dtlck = (struct dt_lock *) txLinelock(dtlck);
4454 lv = & dtlck->lv[0];
4457 lv->offset = si;
4458 n = 0;
4461 n++;
4462 xsi = si;
4464 t = &p->slot[si];
4465 si = t->next;
4468 /* close current linelock */
4469 lv->length = n;
4470 dtlck->index++;
4472 *dtlock = dtlck;
4477 * NAME: dtModify
4479 * FUNCTION: Modify the inode number part of a directory entry
4481 * PARAMETERS:
4482 * tid - Transaction id
4483 * ip - Inode of parent directory
4484 * key - Name of entry to be modified
4485 * orig_ino - Original inode number expected in entry
4486 * new_ino - New inode number to put into entry
4487 * flag - JFS_RENAME
4489 * RETURNS:
4490 * -ESTALE - If entry found does not match orig_ino passed in
4491 * -ENOENT - If no entry can be found to match key
4492 * 0 - If successfully modified entry
4494 int dtModify(tid_t tid, struct inode *ip,
4495 struct component_name * key, ino_t * orig_ino, ino_t new_ino, int flag)
4497 int rc;
4498 s64 bn;
4499 struct metapage *mp;
4500 dtpage_t *p;
4501 int index;
4502 struct btstack btstack;
4503 struct tlock *tlck;
4504 struct dt_lock *dtlck;
4505 struct lv *lv;
4506 s8 *stbl;
4507 int entry_si; /* entry slot index */
4508 struct ldtentry *entry;
4511 * search for the entry to modify:
4513 * dtSearch() returns (leaf page pinned, index at which to modify).
4515 if ((rc = dtSearch(ip, key, orig_ino, &btstack, flag)))
4516 return rc;
4518 /* retrieve search result */
4519 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
4521 BT_MARK_DIRTY(mp, ip);
4523 * acquire a transaction lock on the leaf page of named entry
4525 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
4526 dtlck = (struct dt_lock *) & tlck->lock;
4528 /* get slot index of the entry */
4529 stbl = DT_GETSTBL(p);
4530 entry_si = stbl[index];
4532 /* linelock entry */
4533 ASSERT(dtlck->index == 0);
4534 lv = & dtlck->lv[0];
4535 lv->offset = entry_si;
4536 lv->length = 1;
4537 dtlck->index++;
4539 /* get the head/only segment */
4540 entry = (struct ldtentry *) & p->slot[entry_si];
4542 /* substitute the inode number of the entry */
4543 entry->inumber = cpu_to_le32(new_ino);
4545 /* unpin the leaf page */
4546 DT_PUTPAGE(mp);
4548 return 0;
4551 #ifdef _JFS_DEBUG_DTREE
4553 * dtDisplayTree()
4555 * function: traverse forward
4557 int dtDisplayTree(struct inode *ip)
4559 int rc;
4560 struct metapage *mp;
4561 dtpage_t *p;
4562 s64 bn, pbn;
4563 int index, lastindex, v, h;
4564 pxd_t *xd;
4565 struct btstack btstack;
4566 struct btframe *btsp;
4567 struct btframe *parent;
4568 u8 *stbl;
4569 int psize = 256;
4571 printk("display B+-tree.\n");
4573 /* clear stack */
4574 btsp = btstack.stack;
4577 * start with root
4579 * root resides in the inode
4581 bn = 0;
4582 v = h = 0;
4585 * first access of each page:
4587 newPage:
4588 DT_GETPAGE(ip, bn, mp, psize, p, rc);
4589 if (rc)
4590 return rc;
4592 /* process entries forward from first index */
4593 index = 0;
4594 lastindex = p->header.nextindex - 1;
4596 if (p->header.flag & BT_INTERNAL) {
4598 * first access of each internal page
4600 printf("internal page ");
4601 dtDisplayPage(ip, bn, p);
4603 goto getChild;
4604 } else { /* (p->header.flag & BT_LEAF) */
4607 * first access of each leaf page
4609 printf("leaf page ");
4610 dtDisplayPage(ip, bn, p);
4613 * process leaf page entries
4615 for ( ; index <= lastindex; index++)
4620 /* unpin the leaf page */
4621 DT_PUTPAGE(mp);
4625 * go back up to the parent page
4627 getParent:
4628 /* pop/restore parent entry for the current child page */
4629 if ((parent = (btsp == btstack.stack ? NULL : --btsp)) == NULL)
4630 /* current page must have been root */
4631 return;
4634 * parent page scan completed
4636 if ((index = parent->index) == (lastindex = parent->lastindex)) {
4637 /* go back up to the parent page */
4638 goto getParent;
4642 * parent page has entries remaining
4644 /* get back the parent page */
4645 bn = parent->bn;
4646 /* v = parent->level; */
4647 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
4648 if (rc)
4649 return rc;
4651 /* get next parent entry */
4652 index++;
4655 * internal page: go down to child page of current entry
4657 getChild:
4658 /* push/save current parent entry for the child page */
4659 btsp->bn = pbn = bn;
4660 btsp->index = index;
4661 btsp->lastindex = lastindex;
4662 /* btsp->level = v; */
4663 /* btsp->node = h; */
4664 ++btsp;
4666 /* get current entry for the child page */
4667 stbl = DT_GETSTBL(p);
4668 xd = (pxd_t *) & p->slot[stbl[index]];
4671 * first access of each internal entry:
4674 /* get child page */
4675 bn = addressPXD(xd);
4676 psize = lengthPXD(xd) << ip->i_ipmnt->i_l2bsize;
4678 printk("traverse down 0x%Lx[%d]->0x%Lx\n", pbn, index, bn);
4679 v++;
4680 h = index;
4682 /* release parent page */
4683 DT_PUTPAGE(mp);
4685 /* process the child page */
4686 goto newPage;
4691 * dtDisplayPage()
4693 * function: display page
4695 int dtDisplayPage(struct inode *ip, s64 bn, dtpage_t * p)
4697 int rc;
4698 struct metapage *mp;
4699 struct ldtentry *lh;
4700 struct idtentry *ih;
4701 pxd_t *xd;
4702 int i, j;
4703 u8 *stbl;
4704 wchar_t name[JFS_NAME_MAX + 1];
4705 struct component_name key = { 0, name };
4706 int freepage = 0;
4708 if (p == NULL) {
4709 freepage = 1;
4710 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
4711 if (rc)
4712 return rc;
4715 /* display page control */
4716 printk("bn:0x%Lx flag:0x%08x nextindex:%d\n",
4717 bn, p->header.flag, p->header.nextindex);
4719 /* display entries */
4720 stbl = DT_GETSTBL(p);
4721 for (i = 0, j = 1; i < p->header.nextindex; i++, j++) {
4722 dtGetKey(p, i, &key, JFS_SBI(ip->i_sb)->mntflag);
4723 key.name[key.namlen] = '\0';
4724 if (p->header.flag & BT_LEAF) {
4725 lh = (struct ldtentry *) & p->slot[stbl[i]];
4726 printf("\t[%d] %s:%d", i, key.name,
4727 le32_to_cpu(lh->inumber));
4728 } else {
4729 ih = (struct idtentry *) & p->slot[stbl[i]];
4730 xd = (pxd_t *) ih;
4731 bn = addressPXD(xd);
4732 printf("\t[%d] %s:0x%Lx", i, key.name, bn);
4735 if (j == 4) {
4736 printf("\n");
4737 j = 0;
4741 printf("\n");
4743 if (freepage)
4744 DT_PUTPAGE(mp);
4746 return 0;
4748 #endif /* _JFS_DEBUG_DTREE */