SCSI: fix queue cleanup race before queue initialization is done
[linux/fpc-iii.git] / fs / jfs / jfs_dtree.c
blob52bae3f5c9145df968a3803ee2f7cad0b5fe4bf7
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.
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 <linux/slab.h>
106 #include "jfs_incore.h"
107 #include "jfs_superblock.h"
108 #include "jfs_filsys.h"
109 #include "jfs_metapage.h"
110 #include "jfs_dmap.h"
111 #include "jfs_unicode.h"
112 #include "jfs_debug.h"
114 /* dtree split parameter */
115 struct dtsplit {
116 struct metapage *mp;
117 s16 index;
118 s16 nslot;
119 struct component_name *key;
120 ddata_t *data;
121 struct pxdlist *pxdlist;
124 #define DT_PAGE(IP, MP) BT_PAGE(IP, MP, dtpage_t, i_dtroot)
126 /* get page buffer for specified block address */
127 #define DT_GETPAGE(IP, BN, MP, SIZE, P, RC) \
128 do { \
129 BT_GETPAGE(IP, BN, MP, dtpage_t, SIZE, P, RC, i_dtroot); \
130 if (!(RC)) { \
131 if (((P)->header.nextindex > \
132 (((BN) == 0) ? DTROOTMAXSLOT : (P)->header.maxslot)) || \
133 ((BN) && ((P)->header.maxslot > DTPAGEMAXSLOT))) { \
134 BT_PUTPAGE(MP); \
135 jfs_error((IP)->i_sb, \
136 "DT_GETPAGE: dtree page corrupt\n"); \
137 MP = NULL; \
138 RC = -EIO; \
141 } while (0)
143 /* for consistency */
144 #define DT_PUTPAGE(MP) BT_PUTPAGE(MP)
146 #define DT_GETSEARCH(IP, LEAF, BN, MP, P, INDEX) \
147 BT_GETSEARCH(IP, LEAF, BN, MP, dtpage_t, P, INDEX, i_dtroot)
150 * forward references
152 static int dtSplitUp(tid_t tid, struct inode *ip,
153 struct dtsplit * split, struct btstack * btstack);
155 static int dtSplitPage(tid_t tid, struct inode *ip, struct dtsplit * split,
156 struct metapage ** rmpp, dtpage_t ** rpp, pxd_t * rxdp);
158 static int dtExtendPage(tid_t tid, struct inode *ip,
159 struct dtsplit * split, struct btstack * btstack);
161 static int dtSplitRoot(tid_t tid, struct inode *ip,
162 struct dtsplit * split, struct metapage ** rmpp);
164 static int dtDeleteUp(tid_t tid, struct inode *ip, struct metapage * fmp,
165 dtpage_t * fp, struct btstack * btstack);
167 static int dtRelink(tid_t tid, struct inode *ip, dtpage_t * p);
169 static int dtReadFirst(struct inode *ip, struct btstack * btstack);
171 static int dtReadNext(struct inode *ip,
172 loff_t * offset, struct btstack * btstack);
174 static int dtCompare(struct component_name * key, dtpage_t * p, int si);
176 static int ciCompare(struct component_name * key, dtpage_t * p, int si,
177 int flag);
179 static void dtGetKey(dtpage_t * p, int i, struct component_name * key,
180 int flag);
182 static int ciGetLeafPrefixKey(dtpage_t * lp, int li, dtpage_t * rp,
183 int ri, struct component_name * key, int flag);
185 static void dtInsertEntry(dtpage_t * p, int index, struct component_name * key,
186 ddata_t * data, struct dt_lock **);
188 static void dtMoveEntry(dtpage_t * sp, int si, dtpage_t * dp,
189 struct dt_lock ** sdtlock, struct dt_lock ** ddtlock,
190 int do_index);
192 static void dtDeleteEntry(dtpage_t * p, int fi, struct dt_lock ** dtlock);
194 static void dtTruncateEntry(dtpage_t * p, int ti, struct dt_lock ** dtlock);
196 static void dtLinelockFreelist(dtpage_t * p, int m, struct dt_lock ** dtlock);
198 #define ciToUpper(c) UniStrupr((c)->name)
201 * read_index_page()
203 * Reads a page of a directory's index table.
204 * Having metadata mapped into the directory inode's address space
205 * presents a multitude of problems. We avoid this by mapping to
206 * the absolute address space outside of the *_metapage routines
208 static struct metapage *read_index_page(struct inode *inode, s64 blkno)
210 int rc;
211 s64 xaddr;
212 int xflag;
213 s32 xlen;
215 rc = xtLookup(inode, blkno, 1, &xflag, &xaddr, &xlen, 1);
216 if (rc || (xaddr == 0))
217 return NULL;
219 return read_metapage(inode, xaddr, PSIZE, 1);
223 * get_index_page()
225 * Same as get_index_page(), but get's a new page without reading
227 static struct metapage *get_index_page(struct inode *inode, s64 blkno)
229 int rc;
230 s64 xaddr;
231 int xflag;
232 s32 xlen;
234 rc = xtLookup(inode, blkno, 1, &xflag, &xaddr, &xlen, 1);
235 if (rc || (xaddr == 0))
236 return NULL;
238 return get_metapage(inode, xaddr, PSIZE, 1);
242 * find_index()
244 * Returns dtree page containing directory table entry for specified
245 * index and pointer to its entry.
247 * mp must be released by caller.
249 static struct dir_table_slot *find_index(struct inode *ip, u32 index,
250 struct metapage ** mp, s64 *lblock)
252 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
253 s64 blkno;
254 s64 offset;
255 int page_offset;
256 struct dir_table_slot *slot;
257 static int maxWarnings = 10;
259 if (index < 2) {
260 if (maxWarnings) {
261 jfs_warn("find_entry called with index = %d", index);
262 maxWarnings--;
264 return NULL;
267 if (index >= jfs_ip->next_index) {
268 jfs_warn("find_entry called with index >= next_index");
269 return NULL;
272 if (jfs_dirtable_inline(ip)) {
274 * Inline directory table
276 *mp = NULL;
277 slot = &jfs_ip->i_dirtable[index - 2];
278 } else {
279 offset = (index - 2) * sizeof(struct dir_table_slot);
280 page_offset = offset & (PSIZE - 1);
281 blkno = ((offset + 1) >> L2PSIZE) <<
282 JFS_SBI(ip->i_sb)->l2nbperpage;
284 if (*mp && (*lblock != blkno)) {
285 release_metapage(*mp);
286 *mp = NULL;
288 if (!(*mp)) {
289 *lblock = blkno;
290 *mp = read_index_page(ip, blkno);
292 if (!(*mp)) {
293 jfs_err("free_index: error reading directory table");
294 return NULL;
297 slot =
298 (struct dir_table_slot *) ((char *) (*mp)->data +
299 page_offset);
301 return slot;
304 static inline void lock_index(tid_t tid, struct inode *ip, struct metapage * mp,
305 u32 index)
307 struct tlock *tlck;
308 struct linelock *llck;
309 struct lv *lv;
311 tlck = txLock(tid, ip, mp, tlckDATA);
312 llck = (struct linelock *) tlck->lock;
314 if (llck->index >= llck->maxcnt)
315 llck = txLinelock(llck);
316 lv = &llck->lv[llck->index];
319 * Linelock slot size is twice the size of directory table
320 * slot size. 512 entries per page.
322 lv->offset = ((index - 2) & 511) >> 1;
323 lv->length = 1;
324 llck->index++;
328 * add_index()
330 * Adds an entry to the directory index table. This is used to provide
331 * each directory entry with a persistent index in which to resume
332 * directory traversals
334 static u32 add_index(tid_t tid, struct inode *ip, s64 bn, int slot)
336 struct super_block *sb = ip->i_sb;
337 struct jfs_sb_info *sbi = JFS_SBI(sb);
338 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
339 u64 blkno;
340 struct dir_table_slot *dirtab_slot;
341 u32 index;
342 struct linelock *llck;
343 struct lv *lv;
344 struct metapage *mp;
345 s64 offset;
346 uint page_offset;
347 struct tlock *tlck;
348 s64 xaddr;
350 ASSERT(DO_INDEX(ip));
352 if (jfs_ip->next_index < 2) {
353 jfs_warn("add_index: next_index = %d. Resetting!",
354 jfs_ip->next_index);
355 jfs_ip->next_index = 2;
358 index = jfs_ip->next_index++;
360 if (index <= MAX_INLINE_DIRTABLE_ENTRY) {
362 * i_size reflects size of index table, or 8 bytes per entry.
364 ip->i_size = (loff_t) (index - 1) << 3;
367 * dir table fits inline within inode
369 dirtab_slot = &jfs_ip->i_dirtable[index-2];
370 dirtab_slot->flag = DIR_INDEX_VALID;
371 dirtab_slot->slot = slot;
372 DTSaddress(dirtab_slot, bn);
374 set_cflag(COMMIT_Dirtable, ip);
376 return index;
378 if (index == (MAX_INLINE_DIRTABLE_ENTRY + 1)) {
379 struct dir_table_slot temp_table[12];
382 * It's time to move the inline table to an external
383 * page and begin to build the xtree
385 if (dquot_alloc_block(ip, sbi->nbperpage))
386 goto clean_up;
387 if (dbAlloc(ip, 0, sbi->nbperpage, &xaddr)) {
388 dquot_free_block(ip, sbi->nbperpage);
389 goto clean_up;
393 * Save the table, we're going to overwrite it with the
394 * xtree root
396 memcpy(temp_table, &jfs_ip->i_dirtable, sizeof(temp_table));
399 * Initialize empty x-tree
401 xtInitRoot(tid, ip);
404 * Add the first block to the xtree
406 if (xtInsert(tid, ip, 0, 0, sbi->nbperpage, &xaddr, 0)) {
407 /* This really shouldn't fail */
408 jfs_warn("add_index: xtInsert failed!");
409 memcpy(&jfs_ip->i_dirtable, temp_table,
410 sizeof (temp_table));
411 dbFree(ip, xaddr, sbi->nbperpage);
412 dquot_free_block(ip, sbi->nbperpage);
413 goto clean_up;
415 ip->i_size = PSIZE;
417 mp = get_index_page(ip, 0);
418 if (!mp) {
419 jfs_err("add_index: get_metapage failed!");
420 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
421 memcpy(&jfs_ip->i_dirtable, temp_table,
422 sizeof (temp_table));
423 goto clean_up;
425 tlck = txLock(tid, ip, mp, tlckDATA);
426 llck = (struct linelock *) & tlck->lock;
427 ASSERT(llck->index == 0);
428 lv = &llck->lv[0];
430 lv->offset = 0;
431 lv->length = 6; /* tlckDATA slot size is 16 bytes */
432 llck->index++;
434 memcpy(mp->data, temp_table, sizeof(temp_table));
436 mark_metapage_dirty(mp);
437 release_metapage(mp);
440 * Logging is now directed by xtree tlocks
442 clear_cflag(COMMIT_Dirtable, ip);
445 offset = (index - 2) * sizeof(struct dir_table_slot);
446 page_offset = offset & (PSIZE - 1);
447 blkno = ((offset + 1) >> L2PSIZE) << sbi->l2nbperpage;
448 if (page_offset == 0) {
450 * This will be the beginning of a new page
452 xaddr = 0;
453 if (xtInsert(tid, ip, 0, blkno, sbi->nbperpage, &xaddr, 0)) {
454 jfs_warn("add_index: xtInsert failed!");
455 goto clean_up;
457 ip->i_size += PSIZE;
459 if ((mp = get_index_page(ip, blkno)))
460 memset(mp->data, 0, PSIZE); /* Just looks better */
461 else
462 xtTruncate(tid, ip, offset, COMMIT_PWMAP);
463 } else
464 mp = read_index_page(ip, blkno);
466 if (!mp) {
467 jfs_err("add_index: get/read_metapage failed!");
468 goto clean_up;
471 lock_index(tid, ip, mp, index);
473 dirtab_slot =
474 (struct dir_table_slot *) ((char *) mp->data + page_offset);
475 dirtab_slot->flag = DIR_INDEX_VALID;
476 dirtab_slot->slot = slot;
477 DTSaddress(dirtab_slot, bn);
479 mark_metapage_dirty(mp);
480 release_metapage(mp);
482 return index;
484 clean_up:
486 jfs_ip->next_index--;
488 return 0;
492 * free_index()
494 * Marks an entry to the directory index table as free.
496 static void free_index(tid_t tid, struct inode *ip, u32 index, u32 next)
498 struct dir_table_slot *dirtab_slot;
499 s64 lblock;
500 struct metapage *mp = NULL;
502 dirtab_slot = find_index(ip, index, &mp, &lblock);
504 if (!dirtab_slot)
505 return;
507 dirtab_slot->flag = DIR_INDEX_FREE;
508 dirtab_slot->slot = dirtab_slot->addr1 = 0;
509 dirtab_slot->addr2 = cpu_to_le32(next);
511 if (mp) {
512 lock_index(tid, ip, mp, index);
513 mark_metapage_dirty(mp);
514 release_metapage(mp);
515 } else
516 set_cflag(COMMIT_Dirtable, ip);
520 * modify_index()
522 * Changes an entry in the directory index table
524 static void modify_index(tid_t tid, struct inode *ip, u32 index, s64 bn,
525 int slot, struct metapage ** mp, s64 *lblock)
527 struct dir_table_slot *dirtab_slot;
529 dirtab_slot = find_index(ip, index, mp, lblock);
531 if (!dirtab_slot)
532 return;
534 DTSaddress(dirtab_slot, bn);
535 dirtab_slot->slot = slot;
537 if (*mp) {
538 lock_index(tid, ip, *mp, index);
539 mark_metapage_dirty(*mp);
540 } else
541 set_cflag(COMMIT_Dirtable, ip);
545 * read_index()
547 * reads a directory table slot
549 static int read_index(struct inode *ip, u32 index,
550 struct dir_table_slot * dirtab_slot)
552 s64 lblock;
553 struct metapage *mp = NULL;
554 struct dir_table_slot *slot;
556 slot = find_index(ip, index, &mp, &lblock);
557 if (!slot) {
558 return -EIO;
561 memcpy(dirtab_slot, slot, sizeof(struct dir_table_slot));
563 if (mp)
564 release_metapage(mp);
566 return 0;
570 * dtSearch()
572 * function:
573 * Search for the entry with specified key
575 * parameter:
577 * return: 0 - search result on stack, leaf page pinned;
578 * errno - I/O error
580 int dtSearch(struct inode *ip, struct component_name * key, ino_t * data,
581 struct btstack * btstack, int flag)
583 int rc = 0;
584 int cmp = 1; /* init for empty page */
585 s64 bn;
586 struct metapage *mp;
587 dtpage_t *p;
588 s8 *stbl;
589 int base, index, lim;
590 struct btframe *btsp;
591 pxd_t *pxd;
592 int psize = 288; /* initial in-line directory */
593 ino_t inumber;
594 struct component_name ciKey;
595 struct super_block *sb = ip->i_sb;
597 ciKey.name = kmalloc_array(JFS_NAME_MAX + 1, sizeof(wchar_t),
598 GFP_NOFS);
599 if (!ciKey.name) {
600 rc = -ENOMEM;
601 goto dtSearch_Exit2;
605 /* uppercase search key for c-i directory */
606 UniStrcpy(ciKey.name, key->name);
607 ciKey.namlen = key->namlen;
609 /* only uppercase if case-insensitive support is on */
610 if ((JFS_SBI(sb)->mntflag & JFS_OS2) == JFS_OS2) {
611 ciToUpper(&ciKey);
613 BT_CLR(btstack); /* reset stack */
615 /* init level count for max pages to split */
616 btstack->nsplit = 1;
619 * search down tree from root:
621 * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
622 * internal page, child page Pi contains entry with k, Ki <= K < Kj.
624 * if entry with search key K is not found
625 * internal page search find the entry with largest key Ki
626 * less than K which point to the child page to search;
627 * leaf page search find the entry with smallest key Kj
628 * greater than K so that the returned index is the position of
629 * the entry to be shifted right for insertion of new entry.
630 * for empty tree, search key is greater than any key of the tree.
632 * by convention, root bn = 0.
634 for (bn = 0;;) {
635 /* get/pin the page to search */
636 DT_GETPAGE(ip, bn, mp, psize, p, rc);
637 if (rc)
638 goto dtSearch_Exit1;
640 /* get sorted entry table of the page */
641 stbl = DT_GETSTBL(p);
644 * binary search with search key K on the current page.
646 for (base = 0, lim = p->header.nextindex; lim; lim >>= 1) {
647 index = base + (lim >> 1);
649 if (p->header.flag & BT_LEAF) {
650 /* uppercase leaf name to compare */
651 cmp =
652 ciCompare(&ciKey, p, stbl[index],
653 JFS_SBI(sb)->mntflag);
654 } else {
655 /* router key is in uppercase */
657 cmp = dtCompare(&ciKey, p, stbl[index]);
661 if (cmp == 0) {
663 * search hit
665 /* search hit - leaf page:
666 * return the entry found
668 if (p->header.flag & BT_LEAF) {
669 inumber = le32_to_cpu(
670 ((struct ldtentry *) & p->slot[stbl[index]])->inumber);
673 * search for JFS_LOOKUP
675 if (flag == JFS_LOOKUP) {
676 *data = inumber;
677 rc = 0;
678 goto out;
682 * search for JFS_CREATE
684 if (flag == JFS_CREATE) {
685 *data = inumber;
686 rc = -EEXIST;
687 goto out;
691 * search for JFS_REMOVE or JFS_RENAME
693 if ((flag == JFS_REMOVE ||
694 flag == JFS_RENAME) &&
695 *data != inumber) {
696 rc = -ESTALE;
697 goto out;
701 * JFS_REMOVE|JFS_FINDDIR|JFS_RENAME
703 /* save search result */
704 *data = inumber;
705 btsp = btstack->top;
706 btsp->bn = bn;
707 btsp->index = index;
708 btsp->mp = mp;
710 rc = 0;
711 goto dtSearch_Exit1;
714 /* search hit - internal page:
715 * descend/search its child page
717 goto getChild;
720 if (cmp > 0) {
721 base = index + 1;
722 --lim;
727 * search miss
729 * base is the smallest index with key (Kj) greater than
730 * search key (K) and may be zero or (maxindex + 1) index.
733 * search miss - leaf page
735 * return location of entry (base) where new entry with
736 * search key K is to be inserted.
738 if (p->header.flag & BT_LEAF) {
740 * search for JFS_LOOKUP, JFS_REMOVE, or JFS_RENAME
742 if (flag == JFS_LOOKUP || flag == JFS_REMOVE ||
743 flag == JFS_RENAME) {
744 rc = -ENOENT;
745 goto out;
749 * search for JFS_CREATE|JFS_FINDDIR:
751 * save search result
753 *data = 0;
754 btsp = btstack->top;
755 btsp->bn = bn;
756 btsp->index = base;
757 btsp->mp = mp;
759 rc = 0;
760 goto dtSearch_Exit1;
764 * search miss - internal page
766 * if base is non-zero, decrement base by one to get the parent
767 * entry of the child page to search.
769 index = base ? base - 1 : base;
772 * go down to child page
774 getChild:
775 /* update max. number of pages to split */
776 if (BT_STACK_FULL(btstack)) {
777 /* Something's corrupted, mark filesystem dirty so
778 * chkdsk will fix it.
780 jfs_error(sb, "stack overrun!\n");
781 BT_STACK_DUMP(btstack);
782 rc = -EIO;
783 goto out;
785 btstack->nsplit++;
787 /* push (bn, index) of the parent page/entry */
788 BT_PUSH(btstack, bn, index);
790 /* get the child page block number */
791 pxd = (pxd_t *) & p->slot[stbl[index]];
792 bn = addressPXD(pxd);
793 psize = lengthPXD(pxd) << JFS_SBI(ip->i_sb)->l2bsize;
795 /* unpin the parent page */
796 DT_PUTPAGE(mp);
799 out:
800 DT_PUTPAGE(mp);
802 dtSearch_Exit1:
804 kfree(ciKey.name);
806 dtSearch_Exit2:
808 return rc;
813 * dtInsert()
815 * function: insert an entry to directory tree
817 * parameter:
819 * return: 0 - success;
820 * errno - failure;
822 int dtInsert(tid_t tid, struct inode *ip,
823 struct component_name * name, ino_t * fsn, struct btstack * btstack)
825 int rc = 0;
826 struct metapage *mp; /* meta-page buffer */
827 dtpage_t *p; /* base B+-tree index page */
828 s64 bn;
829 int index;
830 struct dtsplit split; /* split information */
831 ddata_t data;
832 struct dt_lock *dtlck;
833 int n;
834 struct tlock *tlck;
835 struct lv *lv;
838 * retrieve search result
840 * dtSearch() returns (leaf page pinned, index at which to insert).
841 * n.b. dtSearch() may return index of (maxindex + 1) of
842 * the full page.
844 DT_GETSEARCH(ip, btstack->top, bn, mp, p, index);
847 * insert entry for new key
849 if (DO_INDEX(ip)) {
850 if (JFS_IP(ip)->next_index == DIREND) {
851 DT_PUTPAGE(mp);
852 return -EMLINK;
854 n = NDTLEAF(name->namlen);
855 data.leaf.tid = tid;
856 data.leaf.ip = ip;
857 } else {
858 n = NDTLEAF_LEGACY(name->namlen);
859 data.leaf.ip = NULL; /* signifies legacy directory format */
861 data.leaf.ino = *fsn;
864 * leaf page does not have enough room for new entry:
866 * extend/split the leaf page;
868 * dtSplitUp() will insert the entry and unpin the leaf page.
870 if (n > p->header.freecnt) {
871 split.mp = mp;
872 split.index = index;
873 split.nslot = n;
874 split.key = name;
875 split.data = &data;
876 rc = dtSplitUp(tid, ip, &split, btstack);
877 return rc;
881 * leaf page does have enough room for new entry:
883 * insert the new data entry into the leaf page;
885 BT_MARK_DIRTY(mp, ip);
887 * acquire a transaction lock on the leaf page
889 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
890 dtlck = (struct dt_lock *) & tlck->lock;
891 ASSERT(dtlck->index == 0);
892 lv = & dtlck->lv[0];
894 /* linelock header */
895 lv->offset = 0;
896 lv->length = 1;
897 dtlck->index++;
899 dtInsertEntry(p, index, name, &data, &dtlck);
901 /* linelock stbl of non-root leaf page */
902 if (!(p->header.flag & BT_ROOT)) {
903 if (dtlck->index >= dtlck->maxcnt)
904 dtlck = (struct dt_lock *) txLinelock(dtlck);
905 lv = & dtlck->lv[dtlck->index];
906 n = index >> L2DTSLOTSIZE;
907 lv->offset = p->header.stblindex + n;
908 lv->length =
909 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) - n + 1;
910 dtlck->index++;
913 /* unpin the leaf page */
914 DT_PUTPAGE(mp);
916 return 0;
921 * dtSplitUp()
923 * function: propagate insertion bottom up;
925 * parameter:
927 * return: 0 - success;
928 * errno - failure;
929 * leaf page unpinned;
931 static int dtSplitUp(tid_t tid,
932 struct inode *ip, struct dtsplit * split, struct btstack * btstack)
934 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
935 int rc = 0;
936 struct metapage *smp;
937 dtpage_t *sp; /* split page */
938 struct metapage *rmp;
939 dtpage_t *rp; /* new right page split from sp */
940 pxd_t rpxd; /* new right page extent descriptor */
941 struct metapage *lmp;
942 dtpage_t *lp; /* left child page */
943 int skip; /* index of entry of insertion */
944 struct btframe *parent; /* parent page entry on traverse stack */
945 s64 xaddr, nxaddr;
946 int xlen, xsize;
947 struct pxdlist pxdlist;
948 pxd_t *pxd;
949 struct component_name key = { 0, NULL };
950 ddata_t *data = split->data;
951 int n;
952 struct dt_lock *dtlck;
953 struct tlock *tlck;
954 struct lv *lv;
955 int quota_allocation = 0;
957 /* get split page */
958 smp = split->mp;
959 sp = DT_PAGE(ip, smp);
961 key.name = kmalloc_array(JFS_NAME_MAX + 2, sizeof(wchar_t), GFP_NOFS);
962 if (!key.name) {
963 DT_PUTPAGE(smp);
964 rc = -ENOMEM;
965 goto dtSplitUp_Exit;
969 * split leaf page
971 * The split routines insert the new entry, and
972 * acquire txLock as appropriate.
975 * split root leaf page:
977 if (sp->header.flag & BT_ROOT) {
979 * allocate a single extent child page
981 xlen = 1;
982 n = sbi->bsize >> L2DTSLOTSIZE;
983 n -= (n + 31) >> L2DTSLOTSIZE; /* stbl size */
984 n -= DTROOTMAXSLOT - sp->header.freecnt; /* header + entries */
985 if (n <= split->nslot)
986 xlen++;
987 if ((rc = dbAlloc(ip, 0, (s64) xlen, &xaddr))) {
988 DT_PUTPAGE(smp);
989 goto freeKeyName;
992 pxdlist.maxnpxd = 1;
993 pxdlist.npxd = 0;
994 pxd = &pxdlist.pxd[0];
995 PXDaddress(pxd, xaddr);
996 PXDlength(pxd, xlen);
997 split->pxdlist = &pxdlist;
998 rc = dtSplitRoot(tid, ip, split, &rmp);
1000 if (rc)
1001 dbFree(ip, xaddr, xlen);
1002 else
1003 DT_PUTPAGE(rmp);
1005 DT_PUTPAGE(smp);
1007 if (!DO_INDEX(ip))
1008 ip->i_size = xlen << sbi->l2bsize;
1010 goto freeKeyName;
1014 * extend first leaf page
1016 * extend the 1st extent if less than buffer page size
1017 * (dtExtendPage() reurns leaf page unpinned)
1019 pxd = &sp->header.self;
1020 xlen = lengthPXD(pxd);
1021 xsize = xlen << sbi->l2bsize;
1022 if (xsize < PSIZE) {
1023 xaddr = addressPXD(pxd);
1024 n = xsize >> L2DTSLOTSIZE;
1025 n -= (n + 31) >> L2DTSLOTSIZE; /* stbl size */
1026 if ((n + sp->header.freecnt) <= split->nslot)
1027 n = xlen + (xlen << 1);
1028 else
1029 n = xlen;
1031 /* Allocate blocks to quota. */
1032 rc = dquot_alloc_block(ip, n);
1033 if (rc)
1034 goto extendOut;
1035 quota_allocation += n;
1037 if ((rc = dbReAlloc(sbi->ipbmap, xaddr, (s64) xlen,
1038 (s64) n, &nxaddr)))
1039 goto extendOut;
1041 pxdlist.maxnpxd = 1;
1042 pxdlist.npxd = 0;
1043 pxd = &pxdlist.pxd[0];
1044 PXDaddress(pxd, nxaddr);
1045 PXDlength(pxd, xlen + n);
1046 split->pxdlist = &pxdlist;
1047 if ((rc = dtExtendPage(tid, ip, split, btstack))) {
1048 nxaddr = addressPXD(pxd);
1049 if (xaddr != nxaddr) {
1050 /* free relocated extent */
1051 xlen = lengthPXD(pxd);
1052 dbFree(ip, nxaddr, (s64) xlen);
1053 } else {
1054 /* free extended delta */
1055 xlen = lengthPXD(pxd) - n;
1056 xaddr = addressPXD(pxd) + xlen;
1057 dbFree(ip, xaddr, (s64) n);
1059 } else if (!DO_INDEX(ip))
1060 ip->i_size = lengthPXD(pxd) << sbi->l2bsize;
1063 extendOut:
1064 DT_PUTPAGE(smp);
1065 goto freeKeyName;
1069 * split leaf page <sp> into <sp> and a new right page <rp>.
1071 * return <rp> pinned and its extent descriptor <rpxd>
1074 * allocate new directory page extent and
1075 * new index page(s) to cover page split(s)
1077 * allocation hint: ?
1079 n = btstack->nsplit;
1080 pxdlist.maxnpxd = pxdlist.npxd = 0;
1081 xlen = sbi->nbperpage;
1082 for (pxd = pxdlist.pxd; n > 0; n--, pxd++) {
1083 if ((rc = dbAlloc(ip, 0, (s64) xlen, &xaddr)) == 0) {
1084 PXDaddress(pxd, xaddr);
1085 PXDlength(pxd, xlen);
1086 pxdlist.maxnpxd++;
1087 continue;
1090 DT_PUTPAGE(smp);
1092 /* undo allocation */
1093 goto splitOut;
1096 split->pxdlist = &pxdlist;
1097 if ((rc = dtSplitPage(tid, ip, split, &rmp, &rp, &rpxd))) {
1098 DT_PUTPAGE(smp);
1100 /* undo allocation */
1101 goto splitOut;
1104 if (!DO_INDEX(ip))
1105 ip->i_size += PSIZE;
1108 * propagate up the router entry for the leaf page just split
1110 * insert a router entry for the new page into the parent page,
1111 * propagate the insert/split up the tree by walking back the stack
1112 * of (bn of parent page, index of child page entry in parent page)
1113 * that were traversed during the search for the page that split.
1115 * the propagation of insert/split up the tree stops if the root
1116 * splits or the page inserted into doesn't have to split to hold
1117 * the new entry.
1119 * the parent entry for the split page remains the same, and
1120 * a new entry is inserted at its right with the first key and
1121 * block number of the new right page.
1123 * There are a maximum of 4 pages pinned at any time:
1124 * two children, left parent and right parent (when the parent splits).
1125 * keep the child pages pinned while working on the parent.
1126 * make sure that all pins are released at exit.
1128 while ((parent = BT_POP(btstack)) != NULL) {
1129 /* parent page specified by stack frame <parent> */
1131 /* keep current child pages (<lp>, <rp>) pinned */
1132 lmp = smp;
1133 lp = sp;
1136 * insert router entry in parent for new right child page <rp>
1138 /* get the parent page <sp> */
1139 DT_GETPAGE(ip, parent->bn, smp, PSIZE, sp, rc);
1140 if (rc) {
1141 DT_PUTPAGE(lmp);
1142 DT_PUTPAGE(rmp);
1143 goto splitOut;
1147 * The new key entry goes ONE AFTER the index of parent entry,
1148 * because the split was to the right.
1150 skip = parent->index + 1;
1153 * compute the key for the router entry
1155 * key suffix compression:
1156 * for internal pages that have leaf pages as children,
1157 * retain only what's needed to distinguish between
1158 * the new entry and the entry on the page to its left.
1159 * If the keys compare equal, retain the entire key.
1161 * note that compression is performed only at computing
1162 * router key at the lowest internal level.
1163 * further compression of the key between pairs of higher
1164 * level internal pages loses too much information and
1165 * the search may fail.
1166 * (e.g., two adjacent leaf pages of {a, ..., x} {xx, ...,}
1167 * results in two adjacent parent entries (a)(xx).
1168 * if split occurs between these two entries, and
1169 * if compression is applied, the router key of parent entry
1170 * of right page (x) will divert search for x into right
1171 * subtree and miss x in the left subtree.)
1173 * the entire key must be retained for the next-to-leftmost
1174 * internal key at any level of the tree, or search may fail
1175 * (e.g., ?)
1177 switch (rp->header.flag & BT_TYPE) {
1178 case BT_LEAF:
1180 * compute the length of prefix for suffix compression
1181 * between last entry of left page and first entry
1182 * of right page
1184 if ((sp->header.flag & BT_ROOT && skip > 1) ||
1185 sp->header.prev != 0 || skip > 1) {
1186 /* compute uppercase router prefix key */
1187 rc = ciGetLeafPrefixKey(lp,
1188 lp->header.nextindex-1,
1189 rp, 0, &key,
1190 sbi->mntflag);
1191 if (rc) {
1192 DT_PUTPAGE(lmp);
1193 DT_PUTPAGE(rmp);
1194 DT_PUTPAGE(smp);
1195 goto splitOut;
1197 } else {
1198 /* next to leftmost entry of
1199 lowest internal level */
1201 /* compute uppercase router key */
1202 dtGetKey(rp, 0, &key, sbi->mntflag);
1203 key.name[key.namlen] = 0;
1205 if ((sbi->mntflag & JFS_OS2) == JFS_OS2)
1206 ciToUpper(&key);
1209 n = NDTINTERNAL(key.namlen);
1210 break;
1212 case BT_INTERNAL:
1213 dtGetKey(rp, 0, &key, sbi->mntflag);
1214 n = NDTINTERNAL(key.namlen);
1215 break;
1217 default:
1218 jfs_err("dtSplitUp(): UFO!");
1219 break;
1222 /* unpin left child page */
1223 DT_PUTPAGE(lmp);
1226 * compute the data for the router entry
1228 data->xd = rpxd; /* child page xd */
1231 * parent page is full - split the parent page
1233 if (n > sp->header.freecnt) {
1234 /* init for parent page split */
1235 split->mp = smp;
1236 split->index = skip; /* index at insert */
1237 split->nslot = n;
1238 split->key = &key;
1239 /* split->data = data; */
1241 /* unpin right child page */
1242 DT_PUTPAGE(rmp);
1244 /* The split routines insert the new entry,
1245 * acquire txLock as appropriate.
1246 * return <rp> pinned and its block number <rbn>.
1248 rc = (sp->header.flag & BT_ROOT) ?
1249 dtSplitRoot(tid, ip, split, &rmp) :
1250 dtSplitPage(tid, ip, split, &rmp, &rp, &rpxd);
1251 if (rc) {
1252 DT_PUTPAGE(smp);
1253 goto splitOut;
1256 /* smp and rmp are pinned */
1259 * parent page is not full - insert router entry in parent page
1261 else {
1262 BT_MARK_DIRTY(smp, ip);
1264 * acquire a transaction lock on the parent page
1266 tlck = txLock(tid, ip, smp, tlckDTREE | tlckENTRY);
1267 dtlck = (struct dt_lock *) & tlck->lock;
1268 ASSERT(dtlck->index == 0);
1269 lv = & dtlck->lv[0];
1271 /* linelock header */
1272 lv->offset = 0;
1273 lv->length = 1;
1274 dtlck->index++;
1276 /* linelock stbl of non-root parent page */
1277 if (!(sp->header.flag & BT_ROOT)) {
1278 lv++;
1279 n = skip >> L2DTSLOTSIZE;
1280 lv->offset = sp->header.stblindex + n;
1281 lv->length =
1282 ((sp->header.nextindex -
1283 1) >> L2DTSLOTSIZE) - n + 1;
1284 dtlck->index++;
1287 dtInsertEntry(sp, skip, &key, data, &dtlck);
1289 /* exit propagate up */
1290 break;
1294 /* unpin current split and its right page */
1295 DT_PUTPAGE(smp);
1296 DT_PUTPAGE(rmp);
1299 * free remaining extents allocated for split
1301 splitOut:
1302 n = pxdlist.npxd;
1303 pxd = &pxdlist.pxd[n];
1304 for (; n < pxdlist.maxnpxd; n++, pxd++)
1305 dbFree(ip, addressPXD(pxd), (s64) lengthPXD(pxd));
1307 freeKeyName:
1308 kfree(key.name);
1310 /* Rollback quota allocation */
1311 if (rc && quota_allocation)
1312 dquot_free_block(ip, quota_allocation);
1314 dtSplitUp_Exit:
1316 return rc;
1321 * dtSplitPage()
1323 * function: Split a non-root page of a btree.
1325 * parameter:
1327 * return: 0 - success;
1328 * errno - failure;
1329 * return split and new page pinned;
1331 static int dtSplitPage(tid_t tid, struct inode *ip, struct dtsplit * split,
1332 struct metapage ** rmpp, dtpage_t ** rpp, pxd_t * rpxdp)
1334 int rc = 0;
1335 struct metapage *smp;
1336 dtpage_t *sp;
1337 struct metapage *rmp;
1338 dtpage_t *rp; /* new right page allocated */
1339 s64 rbn; /* new right page block number */
1340 struct metapage *mp;
1341 dtpage_t *p;
1342 s64 nextbn;
1343 struct pxdlist *pxdlist;
1344 pxd_t *pxd;
1345 int skip, nextindex, half, left, nxt, off, si;
1346 struct ldtentry *ldtentry;
1347 struct idtentry *idtentry;
1348 u8 *stbl;
1349 struct dtslot *f;
1350 int fsi, stblsize;
1351 int n;
1352 struct dt_lock *sdtlck, *rdtlck;
1353 struct tlock *tlck;
1354 struct dt_lock *dtlck;
1355 struct lv *slv, *rlv, *lv;
1357 /* get split page */
1358 smp = split->mp;
1359 sp = DT_PAGE(ip, smp);
1362 * allocate the new right page for the split
1364 pxdlist = split->pxdlist;
1365 pxd = &pxdlist->pxd[pxdlist->npxd];
1366 pxdlist->npxd++;
1367 rbn = addressPXD(pxd);
1368 rmp = get_metapage(ip, rbn, PSIZE, 1);
1369 if (rmp == NULL)
1370 return -EIO;
1372 /* Allocate blocks to quota. */
1373 rc = dquot_alloc_block(ip, lengthPXD(pxd));
1374 if (rc) {
1375 release_metapage(rmp);
1376 return rc;
1379 jfs_info("dtSplitPage: ip:0x%p smp:0x%p rmp:0x%p", ip, smp, rmp);
1381 BT_MARK_DIRTY(rmp, ip);
1383 * acquire a transaction lock on the new right page
1385 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckNEW);
1386 rdtlck = (struct dt_lock *) & tlck->lock;
1388 rp = (dtpage_t *) rmp->data;
1389 *rpp = rp;
1390 rp->header.self = *pxd;
1392 BT_MARK_DIRTY(smp, ip);
1394 * acquire a transaction lock on the split page
1396 * action:
1398 tlck = txLock(tid, ip, smp, tlckDTREE | tlckENTRY);
1399 sdtlck = (struct dt_lock *) & tlck->lock;
1401 /* linelock header of split page */
1402 ASSERT(sdtlck->index == 0);
1403 slv = & sdtlck->lv[0];
1404 slv->offset = 0;
1405 slv->length = 1;
1406 sdtlck->index++;
1409 * initialize/update sibling pointers between sp and rp
1411 nextbn = le64_to_cpu(sp->header.next);
1412 rp->header.next = cpu_to_le64(nextbn);
1413 rp->header.prev = cpu_to_le64(addressPXD(&sp->header.self));
1414 sp->header.next = cpu_to_le64(rbn);
1417 * initialize new right page
1419 rp->header.flag = sp->header.flag;
1421 /* compute sorted entry table at start of extent data area */
1422 rp->header.nextindex = 0;
1423 rp->header.stblindex = 1;
1425 n = PSIZE >> L2DTSLOTSIZE;
1426 rp->header.maxslot = n;
1427 stblsize = (n + 31) >> L2DTSLOTSIZE; /* in unit of slot */
1429 /* init freelist */
1430 fsi = rp->header.stblindex + stblsize;
1431 rp->header.freelist = fsi;
1432 rp->header.freecnt = rp->header.maxslot - fsi;
1435 * sequential append at tail: append without split
1437 * If splitting the last page on a level because of appending
1438 * a entry to it (skip is maxentry), it's likely that the access is
1439 * sequential. Adding an empty page on the side of the level is less
1440 * work and can push the fill factor much higher than normal.
1441 * If we're wrong it's no big deal, we'll just do the split the right
1442 * way next time.
1443 * (It may look like it's equally easy to do a similar hack for
1444 * reverse sorted data, that is, split the tree left,
1445 * but it's not. Be my guest.)
1447 if (nextbn == 0 && split->index == sp->header.nextindex) {
1448 /* linelock header + stbl (first slot) of new page */
1449 rlv = & rdtlck->lv[rdtlck->index];
1450 rlv->offset = 0;
1451 rlv->length = 2;
1452 rdtlck->index++;
1455 * initialize freelist of new right page
1457 f = &rp->slot[fsi];
1458 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1459 f->next = fsi;
1460 f->next = -1;
1462 /* insert entry at the first entry of the new right page */
1463 dtInsertEntry(rp, 0, split->key, split->data, &rdtlck);
1465 goto out;
1469 * non-sequential insert (at possibly middle page)
1473 * update prev pointer of previous right sibling page;
1475 if (nextbn != 0) {
1476 DT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
1477 if (rc) {
1478 discard_metapage(rmp);
1479 return rc;
1482 BT_MARK_DIRTY(mp, ip);
1484 * acquire a transaction lock on the next page
1486 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
1487 jfs_info("dtSplitPage: tlck = 0x%p, ip = 0x%p, mp=0x%p",
1488 tlck, ip, mp);
1489 dtlck = (struct dt_lock *) & tlck->lock;
1491 /* linelock header of previous right sibling page */
1492 lv = & dtlck->lv[dtlck->index];
1493 lv->offset = 0;
1494 lv->length = 1;
1495 dtlck->index++;
1497 p->header.prev = cpu_to_le64(rbn);
1499 DT_PUTPAGE(mp);
1503 * split the data between the split and right pages.
1505 skip = split->index;
1506 half = (PSIZE >> L2DTSLOTSIZE) >> 1; /* swag */
1507 left = 0;
1510 * compute fill factor for split pages
1512 * <nxt> traces the next entry to move to rp
1513 * <off> traces the next entry to stay in sp
1515 stbl = (u8 *) & sp->slot[sp->header.stblindex];
1516 nextindex = sp->header.nextindex;
1517 for (nxt = off = 0; nxt < nextindex; ++off) {
1518 if (off == skip)
1519 /* check for fill factor with new entry size */
1520 n = split->nslot;
1521 else {
1522 si = stbl[nxt];
1523 switch (sp->header.flag & BT_TYPE) {
1524 case BT_LEAF:
1525 ldtentry = (struct ldtentry *) & sp->slot[si];
1526 if (DO_INDEX(ip))
1527 n = NDTLEAF(ldtentry->namlen);
1528 else
1529 n = NDTLEAF_LEGACY(ldtentry->
1530 namlen);
1531 break;
1533 case BT_INTERNAL:
1534 idtentry = (struct idtentry *) & sp->slot[si];
1535 n = NDTINTERNAL(idtentry->namlen);
1536 break;
1538 default:
1539 break;
1542 ++nxt; /* advance to next entry to move in sp */
1545 left += n;
1546 if (left >= half)
1547 break;
1550 /* <nxt> poins to the 1st entry to move */
1553 * move entries to right page
1555 * dtMoveEntry() initializes rp and reserves entry for insertion
1557 * split page moved out entries are linelocked;
1558 * new/right page moved in entries are linelocked;
1560 /* linelock header + stbl of new right page */
1561 rlv = & rdtlck->lv[rdtlck->index];
1562 rlv->offset = 0;
1563 rlv->length = 5;
1564 rdtlck->index++;
1566 dtMoveEntry(sp, nxt, rp, &sdtlck, &rdtlck, DO_INDEX(ip));
1568 sp->header.nextindex = nxt;
1571 * finalize freelist of new right page
1573 fsi = rp->header.freelist;
1574 f = &rp->slot[fsi];
1575 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1576 f->next = fsi;
1577 f->next = -1;
1580 * Update directory index table for entries now in right page
1582 if ((rp->header.flag & BT_LEAF) && DO_INDEX(ip)) {
1583 s64 lblock;
1585 mp = NULL;
1586 stbl = DT_GETSTBL(rp);
1587 for (n = 0; n < rp->header.nextindex; n++) {
1588 ldtentry = (struct ldtentry *) & rp->slot[stbl[n]];
1589 modify_index(tid, ip, le32_to_cpu(ldtentry->index),
1590 rbn, n, &mp, &lblock);
1592 if (mp)
1593 release_metapage(mp);
1597 * the skipped index was on the left page,
1599 if (skip <= off) {
1600 /* insert the new entry in the split page */
1601 dtInsertEntry(sp, skip, split->key, split->data, &sdtlck);
1603 /* linelock stbl of split page */
1604 if (sdtlck->index >= sdtlck->maxcnt)
1605 sdtlck = (struct dt_lock *) txLinelock(sdtlck);
1606 slv = & sdtlck->lv[sdtlck->index];
1607 n = skip >> L2DTSLOTSIZE;
1608 slv->offset = sp->header.stblindex + n;
1609 slv->length =
1610 ((sp->header.nextindex - 1) >> L2DTSLOTSIZE) - n + 1;
1611 sdtlck->index++;
1614 * the skipped index was on the right page,
1616 else {
1617 /* adjust the skip index to reflect the new position */
1618 skip -= nxt;
1620 /* insert the new entry in the right page */
1621 dtInsertEntry(rp, skip, split->key, split->data, &rdtlck);
1624 out:
1625 *rmpp = rmp;
1626 *rpxdp = *pxd;
1628 return rc;
1633 * dtExtendPage()
1635 * function: extend 1st/only directory leaf page
1637 * parameter:
1639 * return: 0 - success;
1640 * errno - failure;
1641 * return extended page pinned;
1643 static int dtExtendPage(tid_t tid,
1644 struct inode *ip, struct dtsplit * split, struct btstack * btstack)
1646 struct super_block *sb = ip->i_sb;
1647 int rc;
1648 struct metapage *smp, *pmp, *mp;
1649 dtpage_t *sp, *pp;
1650 struct pxdlist *pxdlist;
1651 pxd_t *pxd, *tpxd;
1652 int xlen, xsize;
1653 int newstblindex, newstblsize;
1654 int oldstblindex, oldstblsize;
1655 int fsi, last;
1656 struct dtslot *f;
1657 struct btframe *parent;
1658 int n;
1659 struct dt_lock *dtlck;
1660 s64 xaddr, txaddr;
1661 struct tlock *tlck;
1662 struct pxd_lock *pxdlock;
1663 struct lv *lv;
1664 uint type;
1665 struct ldtentry *ldtentry;
1666 u8 *stbl;
1668 /* get page to extend */
1669 smp = split->mp;
1670 sp = DT_PAGE(ip, smp);
1672 /* get parent/root page */
1673 parent = BT_POP(btstack);
1674 DT_GETPAGE(ip, parent->bn, pmp, PSIZE, pp, rc);
1675 if (rc)
1676 return (rc);
1679 * extend the extent
1681 pxdlist = split->pxdlist;
1682 pxd = &pxdlist->pxd[pxdlist->npxd];
1683 pxdlist->npxd++;
1685 xaddr = addressPXD(pxd);
1686 tpxd = &sp->header.self;
1687 txaddr = addressPXD(tpxd);
1688 /* in-place extension */
1689 if (xaddr == txaddr) {
1690 type = tlckEXTEND;
1692 /* relocation */
1693 else {
1694 type = tlckNEW;
1696 /* save moved extent descriptor for later free */
1697 tlck = txMaplock(tid, ip, tlckDTREE | tlckRELOCATE);
1698 pxdlock = (struct pxd_lock *) & tlck->lock;
1699 pxdlock->flag = mlckFREEPXD;
1700 pxdlock->pxd = sp->header.self;
1701 pxdlock->index = 1;
1704 * Update directory index table to reflect new page address
1706 if (DO_INDEX(ip)) {
1707 s64 lblock;
1709 mp = NULL;
1710 stbl = DT_GETSTBL(sp);
1711 for (n = 0; n < sp->header.nextindex; n++) {
1712 ldtentry =
1713 (struct ldtentry *) & sp->slot[stbl[n]];
1714 modify_index(tid, ip,
1715 le32_to_cpu(ldtentry->index),
1716 xaddr, n, &mp, &lblock);
1718 if (mp)
1719 release_metapage(mp);
1724 * extend the page
1726 sp->header.self = *pxd;
1728 jfs_info("dtExtendPage: ip:0x%p smp:0x%p sp:0x%p", ip, smp, sp);
1730 BT_MARK_DIRTY(smp, ip);
1732 * acquire a transaction lock on the extended/leaf page
1734 tlck = txLock(tid, ip, smp, tlckDTREE | type);
1735 dtlck = (struct dt_lock *) & tlck->lock;
1736 lv = & dtlck->lv[0];
1738 /* update buffer extent descriptor of extended page */
1739 xlen = lengthPXD(pxd);
1740 xsize = xlen << JFS_SBI(sb)->l2bsize;
1743 * copy old stbl to new stbl at start of extended area
1745 oldstblindex = sp->header.stblindex;
1746 oldstblsize = (sp->header.maxslot + 31) >> L2DTSLOTSIZE;
1747 newstblindex = sp->header.maxslot;
1748 n = xsize >> L2DTSLOTSIZE;
1749 newstblsize = (n + 31) >> L2DTSLOTSIZE;
1750 memcpy(&sp->slot[newstblindex], &sp->slot[oldstblindex],
1751 sp->header.nextindex);
1754 * in-line extension: linelock old area of extended page
1756 if (type == tlckEXTEND) {
1757 /* linelock header */
1758 lv->offset = 0;
1759 lv->length = 1;
1760 dtlck->index++;
1761 lv++;
1763 /* linelock new stbl of extended page */
1764 lv->offset = newstblindex;
1765 lv->length = newstblsize;
1768 * relocation: linelock whole relocated area
1770 else {
1771 lv->offset = 0;
1772 lv->length = sp->header.maxslot + newstblsize;
1775 dtlck->index++;
1777 sp->header.maxslot = n;
1778 sp->header.stblindex = newstblindex;
1779 /* sp->header.nextindex remains the same */
1782 * add old stbl region at head of freelist
1784 fsi = oldstblindex;
1785 f = &sp->slot[fsi];
1786 last = sp->header.freelist;
1787 for (n = 0; n < oldstblsize; n++, fsi++, f++) {
1788 f->next = last;
1789 last = fsi;
1791 sp->header.freelist = last;
1792 sp->header.freecnt += oldstblsize;
1795 * append free region of newly extended area at tail of freelist
1797 /* init free region of newly extended area */
1798 fsi = n = newstblindex + newstblsize;
1799 f = &sp->slot[fsi];
1800 for (fsi++; fsi < sp->header.maxslot; f++, fsi++)
1801 f->next = fsi;
1802 f->next = -1;
1804 /* append new free region at tail of old freelist */
1805 fsi = sp->header.freelist;
1806 if (fsi == -1)
1807 sp->header.freelist = n;
1808 else {
1809 do {
1810 f = &sp->slot[fsi];
1811 fsi = f->next;
1812 } while (fsi != -1);
1814 f->next = n;
1817 sp->header.freecnt += sp->header.maxslot - n;
1820 * insert the new entry
1822 dtInsertEntry(sp, split->index, split->key, split->data, &dtlck);
1824 BT_MARK_DIRTY(pmp, ip);
1826 * linelock any freeslots residing in old extent
1828 if (type == tlckEXTEND) {
1829 n = sp->header.maxslot >> 2;
1830 if (sp->header.freelist < n)
1831 dtLinelockFreelist(sp, n, &dtlck);
1835 * update parent entry on the parent/root page
1838 * acquire a transaction lock on the parent/root page
1840 tlck = txLock(tid, ip, pmp, tlckDTREE | tlckENTRY);
1841 dtlck = (struct dt_lock *) & tlck->lock;
1842 lv = & dtlck->lv[dtlck->index];
1844 /* linelock parent entry - 1st slot */
1845 lv->offset = 1;
1846 lv->length = 1;
1847 dtlck->index++;
1849 /* update the parent pxd for page extension */
1850 tpxd = (pxd_t *) & pp->slot[1];
1851 *tpxd = *pxd;
1853 DT_PUTPAGE(pmp);
1854 return 0;
1859 * dtSplitRoot()
1861 * function:
1862 * split the full root page into
1863 * original/root/split page and new right page
1864 * i.e., root remains fixed in tree anchor (inode) and
1865 * the root is copied to a single new right child page
1866 * since root page << non-root page, and
1867 * the split root page contains a single entry for the
1868 * new right child page.
1870 * parameter:
1872 * return: 0 - success;
1873 * errno - failure;
1874 * return new page pinned;
1876 static int dtSplitRoot(tid_t tid,
1877 struct inode *ip, struct dtsplit * split, struct metapage ** rmpp)
1879 struct super_block *sb = ip->i_sb;
1880 struct metapage *smp;
1881 dtroot_t *sp;
1882 struct metapage *rmp;
1883 dtpage_t *rp;
1884 s64 rbn;
1885 int xlen;
1886 int xsize;
1887 struct dtslot *f;
1888 s8 *stbl;
1889 int fsi, stblsize, n;
1890 struct idtentry *s;
1891 pxd_t *ppxd;
1892 struct pxdlist *pxdlist;
1893 pxd_t *pxd;
1894 struct dt_lock *dtlck;
1895 struct tlock *tlck;
1896 struct lv *lv;
1897 int rc;
1899 /* get split root page */
1900 smp = split->mp;
1901 sp = &JFS_IP(ip)->i_dtroot;
1904 * allocate/initialize a single (right) child page
1906 * N.B. at first split, a one (or two) block to fit new entry
1907 * is allocated; at subsequent split, a full page is allocated;
1909 pxdlist = split->pxdlist;
1910 pxd = &pxdlist->pxd[pxdlist->npxd];
1911 pxdlist->npxd++;
1912 rbn = addressPXD(pxd);
1913 xlen = lengthPXD(pxd);
1914 xsize = xlen << JFS_SBI(sb)->l2bsize;
1915 rmp = get_metapage(ip, rbn, xsize, 1);
1916 if (!rmp)
1917 return -EIO;
1919 rp = rmp->data;
1921 /* Allocate blocks to quota. */
1922 rc = dquot_alloc_block(ip, lengthPXD(pxd));
1923 if (rc) {
1924 release_metapage(rmp);
1925 return rc;
1928 BT_MARK_DIRTY(rmp, ip);
1930 * acquire a transaction lock on the new right page
1932 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckNEW);
1933 dtlck = (struct dt_lock *) & tlck->lock;
1935 rp->header.flag =
1936 (sp->header.flag & BT_LEAF) ? BT_LEAF : BT_INTERNAL;
1937 rp->header.self = *pxd;
1939 /* initialize sibling pointers */
1940 rp->header.next = 0;
1941 rp->header.prev = 0;
1944 * move in-line root page into new right page extent
1946 /* linelock header + copied entries + new stbl (1st slot) in new page */
1947 ASSERT(dtlck->index == 0);
1948 lv = & dtlck->lv[0];
1949 lv->offset = 0;
1950 lv->length = 10; /* 1 + 8 + 1 */
1951 dtlck->index++;
1953 n = xsize >> L2DTSLOTSIZE;
1954 rp->header.maxslot = n;
1955 stblsize = (n + 31) >> L2DTSLOTSIZE;
1957 /* copy old stbl to new stbl at start of extended area */
1958 rp->header.stblindex = DTROOTMAXSLOT;
1959 stbl = (s8 *) & rp->slot[DTROOTMAXSLOT];
1960 memcpy(stbl, sp->header.stbl, sp->header.nextindex);
1961 rp->header.nextindex = sp->header.nextindex;
1963 /* copy old data area to start of new data area */
1964 memcpy(&rp->slot[1], &sp->slot[1], IDATASIZE);
1967 * append free region of newly extended area at tail of freelist
1969 /* init free region of newly extended area */
1970 fsi = n = DTROOTMAXSLOT + stblsize;
1971 f = &rp->slot[fsi];
1972 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1973 f->next = fsi;
1974 f->next = -1;
1976 /* append new free region at tail of old freelist */
1977 fsi = sp->header.freelist;
1978 if (fsi == -1)
1979 rp->header.freelist = n;
1980 else {
1981 rp->header.freelist = fsi;
1983 do {
1984 f = &rp->slot[fsi];
1985 fsi = f->next;
1986 } while (fsi != -1);
1988 f->next = n;
1991 rp->header.freecnt = sp->header.freecnt + rp->header.maxslot - n;
1994 * Update directory index table for entries now in right page
1996 if ((rp->header.flag & BT_LEAF) && DO_INDEX(ip)) {
1997 s64 lblock;
1998 struct metapage *mp = NULL;
1999 struct ldtentry *ldtentry;
2001 stbl = DT_GETSTBL(rp);
2002 for (n = 0; n < rp->header.nextindex; n++) {
2003 ldtentry = (struct ldtentry *) & rp->slot[stbl[n]];
2004 modify_index(tid, ip, le32_to_cpu(ldtentry->index),
2005 rbn, n, &mp, &lblock);
2007 if (mp)
2008 release_metapage(mp);
2011 * insert the new entry into the new right/child page
2012 * (skip index in the new right page will not change)
2014 dtInsertEntry(rp, split->index, split->key, split->data, &dtlck);
2017 * reset parent/root page
2019 * set the 1st entry offset to 0, which force the left-most key
2020 * at any level of the tree to be less than any search key.
2022 * The btree comparison code guarantees that the left-most key on any
2023 * level of the tree is never used, so it doesn't need to be filled in.
2025 BT_MARK_DIRTY(smp, ip);
2027 * acquire a transaction lock on the root page (in-memory inode)
2029 tlck = txLock(tid, ip, smp, tlckDTREE | tlckNEW | tlckBTROOT);
2030 dtlck = (struct dt_lock *) & tlck->lock;
2032 /* linelock root */
2033 ASSERT(dtlck->index == 0);
2034 lv = & dtlck->lv[0];
2035 lv->offset = 0;
2036 lv->length = DTROOTMAXSLOT;
2037 dtlck->index++;
2039 /* update page header of root */
2040 if (sp->header.flag & BT_LEAF) {
2041 sp->header.flag &= ~BT_LEAF;
2042 sp->header.flag |= BT_INTERNAL;
2045 /* init the first entry */
2046 s = (struct idtentry *) & sp->slot[DTENTRYSTART];
2047 ppxd = (pxd_t *) s;
2048 *ppxd = *pxd;
2049 s->next = -1;
2050 s->namlen = 0;
2052 stbl = sp->header.stbl;
2053 stbl[0] = DTENTRYSTART;
2054 sp->header.nextindex = 1;
2056 /* init freelist */
2057 fsi = DTENTRYSTART + 1;
2058 f = &sp->slot[fsi];
2060 /* init free region of remaining area */
2061 for (fsi++; fsi < DTROOTMAXSLOT; f++, fsi++)
2062 f->next = fsi;
2063 f->next = -1;
2065 sp->header.freelist = DTENTRYSTART + 1;
2066 sp->header.freecnt = DTROOTMAXSLOT - (DTENTRYSTART + 1);
2068 *rmpp = rmp;
2070 return 0;
2075 * dtDelete()
2077 * function: delete the entry(s) referenced by a key.
2079 * parameter:
2081 * return:
2083 int dtDelete(tid_t tid,
2084 struct inode *ip, struct component_name * key, ino_t * ino, int flag)
2086 int rc = 0;
2087 s64 bn;
2088 struct metapage *mp, *imp;
2089 dtpage_t *p;
2090 int index;
2091 struct btstack btstack;
2092 struct dt_lock *dtlck;
2093 struct tlock *tlck;
2094 struct lv *lv;
2095 int i;
2096 struct ldtentry *ldtentry;
2097 u8 *stbl;
2098 u32 table_index, next_index;
2099 struct metapage *nmp;
2100 dtpage_t *np;
2103 * search for the entry to delete:
2105 * dtSearch() returns (leaf page pinned, index at which to delete).
2107 if ((rc = dtSearch(ip, key, ino, &btstack, flag)))
2108 return rc;
2110 /* retrieve search result */
2111 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
2114 * We need to find put the index of the next entry into the
2115 * directory index table in order to resume a readdir from this
2116 * entry.
2118 if (DO_INDEX(ip)) {
2119 stbl = DT_GETSTBL(p);
2120 ldtentry = (struct ldtentry *) & p->slot[stbl[index]];
2121 table_index = le32_to_cpu(ldtentry->index);
2122 if (index == (p->header.nextindex - 1)) {
2124 * Last entry in this leaf page
2126 if ((p->header.flag & BT_ROOT)
2127 || (p->header.next == 0))
2128 next_index = -1;
2129 else {
2130 /* Read next leaf page */
2131 DT_GETPAGE(ip, le64_to_cpu(p->header.next),
2132 nmp, PSIZE, np, rc);
2133 if (rc)
2134 next_index = -1;
2135 else {
2136 stbl = DT_GETSTBL(np);
2137 ldtentry =
2138 (struct ldtentry *) & np->
2139 slot[stbl[0]];
2140 next_index =
2141 le32_to_cpu(ldtentry->index);
2142 DT_PUTPAGE(nmp);
2145 } else {
2146 ldtentry =
2147 (struct ldtentry *) & p->slot[stbl[index + 1]];
2148 next_index = le32_to_cpu(ldtentry->index);
2150 free_index(tid, ip, table_index, next_index);
2153 * the leaf page becomes empty, delete the page
2155 if (p->header.nextindex == 1) {
2156 /* delete empty page */
2157 rc = dtDeleteUp(tid, ip, mp, p, &btstack);
2160 * the leaf page has other entries remaining:
2162 * delete the entry from the leaf page.
2164 else {
2165 BT_MARK_DIRTY(mp, ip);
2167 * acquire a transaction lock on the leaf page
2169 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
2170 dtlck = (struct dt_lock *) & tlck->lock;
2173 * Do not assume that dtlck->index will be zero. During a
2174 * rename within a directory, this transaction may have
2175 * modified this page already when adding the new entry.
2178 /* linelock header */
2179 if (dtlck->index >= dtlck->maxcnt)
2180 dtlck = (struct dt_lock *) txLinelock(dtlck);
2181 lv = & dtlck->lv[dtlck->index];
2182 lv->offset = 0;
2183 lv->length = 1;
2184 dtlck->index++;
2186 /* linelock stbl of non-root leaf page */
2187 if (!(p->header.flag & BT_ROOT)) {
2188 if (dtlck->index >= dtlck->maxcnt)
2189 dtlck = (struct dt_lock *) txLinelock(dtlck);
2190 lv = & dtlck->lv[dtlck->index];
2191 i = index >> L2DTSLOTSIZE;
2192 lv->offset = p->header.stblindex + i;
2193 lv->length =
2194 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) -
2195 i + 1;
2196 dtlck->index++;
2199 /* free the leaf entry */
2200 dtDeleteEntry(p, index, &dtlck);
2203 * Update directory index table for entries moved in stbl
2205 if (DO_INDEX(ip) && index < p->header.nextindex) {
2206 s64 lblock;
2208 imp = NULL;
2209 stbl = DT_GETSTBL(p);
2210 for (i = index; i < p->header.nextindex; i++) {
2211 ldtentry =
2212 (struct ldtentry *) & p->slot[stbl[i]];
2213 modify_index(tid, ip,
2214 le32_to_cpu(ldtentry->index),
2215 bn, i, &imp, &lblock);
2217 if (imp)
2218 release_metapage(imp);
2221 DT_PUTPAGE(mp);
2224 return rc;
2229 * dtDeleteUp()
2231 * function:
2232 * free empty pages as propagating deletion up the tree
2234 * parameter:
2236 * return:
2238 static int dtDeleteUp(tid_t tid, struct inode *ip,
2239 struct metapage * fmp, dtpage_t * fp, struct btstack * btstack)
2241 int rc = 0;
2242 struct metapage *mp;
2243 dtpage_t *p;
2244 int index, nextindex;
2245 int xlen;
2246 struct btframe *parent;
2247 struct dt_lock *dtlck;
2248 struct tlock *tlck;
2249 struct lv *lv;
2250 struct pxd_lock *pxdlock;
2251 int i;
2254 * keep the root leaf page which has become empty
2256 if (BT_IS_ROOT(fmp)) {
2258 * reset the root
2260 * dtInitRoot() acquires txlock on the root
2262 dtInitRoot(tid, ip, PARENT(ip));
2264 DT_PUTPAGE(fmp);
2266 return 0;
2270 * free the non-root leaf page
2273 * acquire a transaction lock on the page
2275 * write FREEXTENT|NOREDOPAGE log record
2276 * N.B. linelock is overlaid as freed extent descriptor, and
2277 * the buffer page is freed;
2279 tlck = txMaplock(tid, ip, tlckDTREE | tlckFREE);
2280 pxdlock = (struct pxd_lock *) & tlck->lock;
2281 pxdlock->flag = mlckFREEPXD;
2282 pxdlock->pxd = fp->header.self;
2283 pxdlock->index = 1;
2285 /* update sibling pointers */
2286 if ((rc = dtRelink(tid, ip, fp))) {
2287 BT_PUTPAGE(fmp);
2288 return rc;
2291 xlen = lengthPXD(&fp->header.self);
2293 /* Free quota allocation. */
2294 dquot_free_block(ip, xlen);
2296 /* free/invalidate its buffer page */
2297 discard_metapage(fmp);
2300 * propagate page deletion up the directory tree
2302 * If the delete from the parent page makes it empty,
2303 * continue all the way up the tree.
2304 * stop if the root page is reached (which is never deleted) or
2305 * if the entry deletion does not empty the page.
2307 while ((parent = BT_POP(btstack)) != NULL) {
2308 /* pin the parent page <sp> */
2309 DT_GETPAGE(ip, parent->bn, mp, PSIZE, p, rc);
2310 if (rc)
2311 return rc;
2314 * free the extent of the child page deleted
2316 index = parent->index;
2319 * delete the entry for the child page from parent
2321 nextindex = p->header.nextindex;
2324 * the parent has the single entry being deleted:
2326 * free the parent page which has become empty.
2328 if (nextindex == 1) {
2330 * keep the root internal page which has become empty
2332 if (p->header.flag & BT_ROOT) {
2334 * reset the root
2336 * dtInitRoot() acquires txlock on the root
2338 dtInitRoot(tid, ip, PARENT(ip));
2340 DT_PUTPAGE(mp);
2342 return 0;
2345 * free the parent page
2347 else {
2349 * acquire a transaction lock on the page
2351 * write FREEXTENT|NOREDOPAGE log record
2353 tlck =
2354 txMaplock(tid, ip,
2355 tlckDTREE | tlckFREE);
2356 pxdlock = (struct pxd_lock *) & tlck->lock;
2357 pxdlock->flag = mlckFREEPXD;
2358 pxdlock->pxd = p->header.self;
2359 pxdlock->index = 1;
2361 /* update sibling pointers */
2362 if ((rc = dtRelink(tid, ip, p))) {
2363 DT_PUTPAGE(mp);
2364 return rc;
2367 xlen = lengthPXD(&p->header.self);
2369 /* Free quota allocation */
2370 dquot_free_block(ip, xlen);
2372 /* free/invalidate its buffer page */
2373 discard_metapage(mp);
2375 /* propagate up */
2376 continue;
2381 * the parent has other entries remaining:
2383 * delete the router entry from the parent page.
2385 BT_MARK_DIRTY(mp, ip);
2387 * acquire a transaction lock on the page
2389 * action: router entry deletion
2391 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
2392 dtlck = (struct dt_lock *) & tlck->lock;
2394 /* linelock header */
2395 if (dtlck->index >= dtlck->maxcnt)
2396 dtlck = (struct dt_lock *) txLinelock(dtlck);
2397 lv = & dtlck->lv[dtlck->index];
2398 lv->offset = 0;
2399 lv->length = 1;
2400 dtlck->index++;
2402 /* linelock stbl of non-root leaf page */
2403 if (!(p->header.flag & BT_ROOT)) {
2404 if (dtlck->index < dtlck->maxcnt)
2405 lv++;
2406 else {
2407 dtlck = (struct dt_lock *) txLinelock(dtlck);
2408 lv = & dtlck->lv[0];
2410 i = index >> L2DTSLOTSIZE;
2411 lv->offset = p->header.stblindex + i;
2412 lv->length =
2413 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) -
2414 i + 1;
2415 dtlck->index++;
2418 /* free the router entry */
2419 dtDeleteEntry(p, index, &dtlck);
2421 /* reset key of new leftmost entry of level (for consistency) */
2422 if (index == 0 &&
2423 ((p->header.flag & BT_ROOT) || p->header.prev == 0))
2424 dtTruncateEntry(p, 0, &dtlck);
2426 /* unpin the parent page */
2427 DT_PUTPAGE(mp);
2429 /* exit propagation up */
2430 break;
2433 if (!DO_INDEX(ip))
2434 ip->i_size -= PSIZE;
2436 return 0;
2439 #ifdef _NOTYET
2441 * NAME: dtRelocate()
2443 * FUNCTION: relocate dtpage (internal or leaf) of directory;
2444 * This function is mainly used by defragfs utility.
2446 int dtRelocate(tid_t tid, struct inode *ip, s64 lmxaddr, pxd_t * opxd,
2447 s64 nxaddr)
2449 int rc = 0;
2450 struct metapage *mp, *pmp, *lmp, *rmp;
2451 dtpage_t *p, *pp, *rp = 0, *lp= 0;
2452 s64 bn;
2453 int index;
2454 struct btstack btstack;
2455 pxd_t *pxd;
2456 s64 oxaddr, nextbn, prevbn;
2457 int xlen, xsize;
2458 struct tlock *tlck;
2459 struct dt_lock *dtlck;
2460 struct pxd_lock *pxdlock;
2461 s8 *stbl;
2462 struct lv *lv;
2464 oxaddr = addressPXD(opxd);
2465 xlen = lengthPXD(opxd);
2467 jfs_info("dtRelocate: lmxaddr:%Ld xaddr:%Ld:%Ld xlen:%d",
2468 (long long)lmxaddr, (long long)oxaddr, (long long)nxaddr,
2469 xlen);
2472 * 1. get the internal parent dtpage covering
2473 * router entry for the tartget page to be relocated;
2475 rc = dtSearchNode(ip, lmxaddr, opxd, &btstack);
2476 if (rc)
2477 return rc;
2479 /* retrieve search result */
2480 DT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
2481 jfs_info("dtRelocate: parent router entry validated.");
2484 * 2. relocate the target dtpage
2486 /* read in the target page from src extent */
2487 DT_GETPAGE(ip, oxaddr, mp, PSIZE, p, rc);
2488 if (rc) {
2489 /* release the pinned parent page */
2490 DT_PUTPAGE(pmp);
2491 return rc;
2495 * read in sibling pages if any to update sibling pointers;
2497 rmp = NULL;
2498 if (p->header.next) {
2499 nextbn = le64_to_cpu(p->header.next);
2500 DT_GETPAGE(ip, nextbn, rmp, PSIZE, rp, rc);
2501 if (rc) {
2502 DT_PUTPAGE(mp);
2503 DT_PUTPAGE(pmp);
2504 return (rc);
2508 lmp = NULL;
2509 if (p->header.prev) {
2510 prevbn = le64_to_cpu(p->header.prev);
2511 DT_GETPAGE(ip, prevbn, lmp, PSIZE, lp, rc);
2512 if (rc) {
2513 DT_PUTPAGE(mp);
2514 DT_PUTPAGE(pmp);
2515 if (rmp)
2516 DT_PUTPAGE(rmp);
2517 return (rc);
2521 /* at this point, all xtpages to be updated are in memory */
2524 * update sibling pointers of sibling dtpages if any;
2526 if (lmp) {
2527 tlck = txLock(tid, ip, lmp, 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 lp->header.next = cpu_to_le64(nxaddr);
2537 DT_PUTPAGE(lmp);
2540 if (rmp) {
2541 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckRELINK);
2542 dtlck = (struct dt_lock *) & tlck->lock;
2543 /* linelock header */
2544 ASSERT(dtlck->index == 0);
2545 lv = & dtlck->lv[0];
2546 lv->offset = 0;
2547 lv->length = 1;
2548 dtlck->index++;
2550 rp->header.prev = cpu_to_le64(nxaddr);
2551 DT_PUTPAGE(rmp);
2555 * update the target dtpage to be relocated
2557 * write LOG_REDOPAGE of LOG_NEW type for dst page
2558 * for the whole target page (logredo() will apply
2559 * after image and update bmap for allocation of the
2560 * dst extent), and update bmap for allocation of
2561 * the dst extent;
2563 tlck = txLock(tid, ip, mp, tlckDTREE | tlckNEW);
2564 dtlck = (struct dt_lock *) & tlck->lock;
2565 /* linelock header */
2566 ASSERT(dtlck->index == 0);
2567 lv = & dtlck->lv[0];
2569 /* update the self address in the dtpage header */
2570 pxd = &p->header.self;
2571 PXDaddress(pxd, nxaddr);
2573 /* the dst page is the same as the src page, i.e.,
2574 * linelock for afterimage of the whole page;
2576 lv->offset = 0;
2577 lv->length = p->header.maxslot;
2578 dtlck->index++;
2580 /* update the buffer extent descriptor of the dtpage */
2581 xsize = xlen << JFS_SBI(ip->i_sb)->l2bsize;
2583 /* unpin the relocated page */
2584 DT_PUTPAGE(mp);
2585 jfs_info("dtRelocate: target dtpage relocated.");
2587 /* the moved extent is dtpage, then a LOG_NOREDOPAGE log rec
2588 * needs to be written (in logredo(), the LOG_NOREDOPAGE log rec
2589 * will also force a bmap update ).
2593 * 3. acquire maplock for the source extent to be freed;
2595 /* for dtpage relocation, write a LOG_NOREDOPAGE record
2596 * for the source dtpage (logredo() will init NoRedoPage
2597 * filter and will also update bmap for free of the source
2598 * dtpage), and upadte bmap for free of the source dtpage;
2600 tlck = txMaplock(tid, ip, tlckDTREE | tlckFREE);
2601 pxdlock = (struct pxd_lock *) & tlck->lock;
2602 pxdlock->flag = mlckFREEPXD;
2603 PXDaddress(&pxdlock->pxd, oxaddr);
2604 PXDlength(&pxdlock->pxd, xlen);
2605 pxdlock->index = 1;
2608 * 4. update the parent router entry for relocation;
2610 * acquire tlck for the parent entry covering the target dtpage;
2611 * write LOG_REDOPAGE to apply after image only;
2613 jfs_info("dtRelocate: update parent router entry.");
2614 tlck = txLock(tid, ip, pmp, tlckDTREE | tlckENTRY);
2615 dtlck = (struct dt_lock *) & tlck->lock;
2616 lv = & dtlck->lv[dtlck->index];
2618 /* update the PXD with the new address */
2619 stbl = DT_GETSTBL(pp);
2620 pxd = (pxd_t *) & pp->slot[stbl[index]];
2621 PXDaddress(pxd, nxaddr);
2622 lv->offset = stbl[index];
2623 lv->length = 1;
2624 dtlck->index++;
2626 /* unpin the parent dtpage */
2627 DT_PUTPAGE(pmp);
2629 return rc;
2633 * NAME: dtSearchNode()
2635 * FUNCTION: Search for an dtpage containing a specified address
2636 * This function is mainly used by defragfs utility.
2638 * NOTE: Search result on stack, the found page is pinned at exit.
2639 * The result page must be an internal dtpage.
2640 * lmxaddr give the address of the left most page of the
2641 * dtree level, in which the required dtpage resides.
2643 static int dtSearchNode(struct inode *ip, s64 lmxaddr, pxd_t * kpxd,
2644 struct btstack * btstack)
2646 int rc = 0;
2647 s64 bn;
2648 struct metapage *mp;
2649 dtpage_t *p;
2650 int psize = 288; /* initial in-line directory */
2651 s8 *stbl;
2652 int i;
2653 pxd_t *pxd;
2654 struct btframe *btsp;
2656 BT_CLR(btstack); /* reset stack */
2659 * descend tree to the level with specified leftmost page
2661 * by convention, root bn = 0.
2663 for (bn = 0;;) {
2664 /* get/pin the page to search */
2665 DT_GETPAGE(ip, bn, mp, psize, p, rc);
2666 if (rc)
2667 return rc;
2669 /* does the xaddr of leftmost page of the levevl
2670 * matches levevl search key ?
2672 if (p->header.flag & BT_ROOT) {
2673 if (lmxaddr == 0)
2674 break;
2675 } else if (addressPXD(&p->header.self) == lmxaddr)
2676 break;
2679 * descend down to leftmost child page
2681 if (p->header.flag & BT_LEAF) {
2682 DT_PUTPAGE(mp);
2683 return -ESTALE;
2686 /* get the leftmost entry */
2687 stbl = DT_GETSTBL(p);
2688 pxd = (pxd_t *) & p->slot[stbl[0]];
2690 /* get the child page block address */
2691 bn = addressPXD(pxd);
2692 psize = lengthPXD(pxd) << JFS_SBI(ip->i_sb)->l2bsize;
2693 /* unpin the parent page */
2694 DT_PUTPAGE(mp);
2698 * search each page at the current levevl
2700 loop:
2701 stbl = DT_GETSTBL(p);
2702 for (i = 0; i < p->header.nextindex; i++) {
2703 pxd = (pxd_t *) & p->slot[stbl[i]];
2705 /* found the specified router entry */
2706 if (addressPXD(pxd) == addressPXD(kpxd) &&
2707 lengthPXD(pxd) == lengthPXD(kpxd)) {
2708 btsp = btstack->top;
2709 btsp->bn = bn;
2710 btsp->index = i;
2711 btsp->mp = mp;
2713 return 0;
2717 /* get the right sibling page if any */
2718 if (p->header.next)
2719 bn = le64_to_cpu(p->header.next);
2720 else {
2721 DT_PUTPAGE(mp);
2722 return -ESTALE;
2725 /* unpin current page */
2726 DT_PUTPAGE(mp);
2728 /* get the right sibling page */
2729 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2730 if (rc)
2731 return rc;
2733 goto loop;
2735 #endif /* _NOTYET */
2738 * dtRelink()
2740 * function:
2741 * link around a freed page.
2743 * parameter:
2744 * fp: page to be freed
2746 * return:
2748 static int dtRelink(tid_t tid, struct inode *ip, dtpage_t * p)
2750 int rc;
2751 struct metapage *mp;
2752 s64 nextbn, prevbn;
2753 struct tlock *tlck;
2754 struct dt_lock *dtlck;
2755 struct lv *lv;
2757 nextbn = le64_to_cpu(p->header.next);
2758 prevbn = le64_to_cpu(p->header.prev);
2760 /* update prev pointer of the next page */
2761 if (nextbn != 0) {
2762 DT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
2763 if (rc)
2764 return rc;
2766 BT_MARK_DIRTY(mp, ip);
2768 * acquire a transaction lock on the next page
2770 * action: update prev pointer;
2772 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
2773 jfs_info("dtRelink nextbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2774 tlck, ip, mp);
2775 dtlck = (struct dt_lock *) & tlck->lock;
2777 /* linelock header */
2778 if (dtlck->index >= dtlck->maxcnt)
2779 dtlck = (struct dt_lock *) txLinelock(dtlck);
2780 lv = & dtlck->lv[dtlck->index];
2781 lv->offset = 0;
2782 lv->length = 1;
2783 dtlck->index++;
2785 p->header.prev = cpu_to_le64(prevbn);
2786 DT_PUTPAGE(mp);
2789 /* update next pointer of the previous page */
2790 if (prevbn != 0) {
2791 DT_GETPAGE(ip, prevbn, mp, PSIZE, p, rc);
2792 if (rc)
2793 return rc;
2795 BT_MARK_DIRTY(mp, ip);
2797 * acquire a transaction lock on the prev page
2799 * action: update next pointer;
2801 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
2802 jfs_info("dtRelink prevbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2803 tlck, ip, mp);
2804 dtlck = (struct dt_lock *) & tlck->lock;
2806 /* linelock header */
2807 if (dtlck->index >= dtlck->maxcnt)
2808 dtlck = (struct dt_lock *) txLinelock(dtlck);
2809 lv = & dtlck->lv[dtlck->index];
2810 lv->offset = 0;
2811 lv->length = 1;
2812 dtlck->index++;
2814 p->header.next = cpu_to_le64(nextbn);
2815 DT_PUTPAGE(mp);
2818 return 0;
2823 * dtInitRoot()
2825 * initialize directory root (inline in inode)
2827 void dtInitRoot(tid_t tid, struct inode *ip, u32 idotdot)
2829 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
2830 dtroot_t *p;
2831 int fsi;
2832 struct dtslot *f;
2833 struct tlock *tlck;
2834 struct dt_lock *dtlck;
2835 struct lv *lv;
2836 u16 xflag_save;
2839 * If this was previously an non-empty directory, we need to remove
2840 * the old directory table.
2842 if (DO_INDEX(ip)) {
2843 if (!jfs_dirtable_inline(ip)) {
2844 struct tblock *tblk = tid_to_tblock(tid);
2846 * We're playing games with the tid's xflag. If
2847 * we're removing a regular file, the file's xtree
2848 * is committed with COMMIT_PMAP, but we always
2849 * commit the directories xtree with COMMIT_PWMAP.
2851 xflag_save = tblk->xflag;
2852 tblk->xflag = 0;
2854 * xtTruncate isn't guaranteed to fully truncate
2855 * the xtree. The caller needs to check i_size
2856 * after committing the transaction to see if
2857 * additional truncation is needed. The
2858 * COMMIT_Stale flag tells caller that we
2859 * initiated the truncation.
2861 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
2862 set_cflag(COMMIT_Stale, ip);
2864 tblk->xflag = xflag_save;
2865 } else
2866 ip->i_size = 1;
2868 jfs_ip->next_index = 2;
2869 } else
2870 ip->i_size = IDATASIZE;
2873 * acquire a transaction lock on the root
2875 * action: directory initialization;
2877 tlck = txLock(tid, ip, (struct metapage *) & jfs_ip->bxflag,
2878 tlckDTREE | tlckENTRY | tlckBTROOT);
2879 dtlck = (struct dt_lock *) & tlck->lock;
2881 /* linelock root */
2882 ASSERT(dtlck->index == 0);
2883 lv = & dtlck->lv[0];
2884 lv->offset = 0;
2885 lv->length = DTROOTMAXSLOT;
2886 dtlck->index++;
2888 p = &jfs_ip->i_dtroot;
2890 p->header.flag = DXD_INDEX | BT_ROOT | BT_LEAF;
2892 p->header.nextindex = 0;
2894 /* init freelist */
2895 fsi = 1;
2896 f = &p->slot[fsi];
2898 /* init data area of root */
2899 for (fsi++; fsi < DTROOTMAXSLOT; f++, fsi++)
2900 f->next = fsi;
2901 f->next = -1;
2903 p->header.freelist = 1;
2904 p->header.freecnt = 8;
2906 /* init '..' entry */
2907 p->header.idotdot = cpu_to_le32(idotdot);
2909 return;
2913 * add_missing_indices()
2915 * function: Fix dtree page in which one or more entries has an invalid index.
2916 * fsck.jfs should really fix this, but it currently does not.
2917 * Called from jfs_readdir when bad index is detected.
2919 static void add_missing_indices(struct inode *inode, s64 bn)
2921 struct ldtentry *d;
2922 struct dt_lock *dtlck;
2923 int i;
2924 uint index;
2925 struct lv *lv;
2926 struct metapage *mp;
2927 dtpage_t *p;
2928 int rc;
2929 s8 *stbl;
2930 tid_t tid;
2931 struct tlock *tlck;
2933 tid = txBegin(inode->i_sb, 0);
2935 DT_GETPAGE(inode, bn, mp, PSIZE, p, rc);
2937 if (rc) {
2938 printk(KERN_ERR "DT_GETPAGE failed!\n");
2939 goto end;
2941 BT_MARK_DIRTY(mp, inode);
2943 ASSERT(p->header.flag & BT_LEAF);
2945 tlck = txLock(tid, inode, mp, tlckDTREE | tlckENTRY);
2946 if (BT_IS_ROOT(mp))
2947 tlck->type |= tlckBTROOT;
2949 dtlck = (struct dt_lock *) &tlck->lock;
2951 stbl = DT_GETSTBL(p);
2952 for (i = 0; i < p->header.nextindex; i++) {
2953 d = (struct ldtentry *) &p->slot[stbl[i]];
2954 index = le32_to_cpu(d->index);
2955 if ((index < 2) || (index >= JFS_IP(inode)->next_index)) {
2956 d->index = cpu_to_le32(add_index(tid, inode, bn, i));
2957 if (dtlck->index >= dtlck->maxcnt)
2958 dtlck = (struct dt_lock *) txLinelock(dtlck);
2959 lv = &dtlck->lv[dtlck->index];
2960 lv->offset = stbl[i];
2961 lv->length = 1;
2962 dtlck->index++;
2966 DT_PUTPAGE(mp);
2967 (void) txCommit(tid, 1, &inode, 0);
2968 end:
2969 txEnd(tid);
2973 * Buffer to hold directory entry info while traversing a dtree page
2974 * before being fed to the filldir function
2976 struct jfs_dirent {
2977 loff_t position;
2978 int ino;
2979 u16 name_len;
2980 char name[0];
2984 * function to determine next variable-sized jfs_dirent in buffer
2986 static inline struct jfs_dirent *next_jfs_dirent(struct jfs_dirent *dirent)
2988 return (struct jfs_dirent *)
2989 ((char *)dirent +
2990 ((sizeof (struct jfs_dirent) + dirent->name_len + 1 +
2991 sizeof (loff_t) - 1) &
2992 ~(sizeof (loff_t) - 1)));
2996 * jfs_readdir()
2998 * function: read directory entries sequentially
2999 * from the specified entry offset
3001 * parameter:
3003 * return: offset = (pn, index) of start entry
3004 * of next jfs_readdir()/dtRead()
3006 int jfs_readdir(struct file *file, struct dir_context *ctx)
3008 struct inode *ip = file_inode(file);
3009 struct nls_table *codepage = JFS_SBI(ip->i_sb)->nls_tab;
3010 int rc = 0;
3011 loff_t dtpos; /* legacy OS/2 style position */
3012 struct dtoffset {
3013 s16 pn;
3014 s16 index;
3015 s32 unused;
3016 } *dtoffset = (struct dtoffset *) &dtpos;
3017 s64 bn;
3018 struct metapage *mp;
3019 dtpage_t *p;
3020 int index;
3021 s8 *stbl;
3022 struct btstack btstack;
3023 int i, next;
3024 struct ldtentry *d;
3025 struct dtslot *t;
3026 int d_namleft, len, outlen;
3027 unsigned long dirent_buf;
3028 char *name_ptr;
3029 u32 dir_index;
3030 int do_index = 0;
3031 uint loop_count = 0;
3032 struct jfs_dirent *jfs_dirent;
3033 int jfs_dirents;
3034 int overflow, fix_page, page_fixed = 0;
3035 static int unique_pos = 2; /* If we can't fix broken index */
3037 if (ctx->pos == DIREND)
3038 return 0;
3040 if (DO_INDEX(ip)) {
3042 * persistent index is stored in directory entries.
3043 * Special cases: 0 = .
3044 * 1 = ..
3045 * -1 = End of directory
3047 do_index = 1;
3049 dir_index = (u32) ctx->pos;
3052 * NFSv4 reserves cookies 1 and 2 for . and .. so the value
3053 * we return to the vfs is one greater than the one we use
3054 * internally.
3056 if (dir_index)
3057 dir_index--;
3059 if (dir_index > 1) {
3060 struct dir_table_slot dirtab_slot;
3062 if (dtEmpty(ip) ||
3063 (dir_index >= JFS_IP(ip)->next_index)) {
3064 /* Stale position. Directory has shrunk */
3065 ctx->pos = DIREND;
3066 return 0;
3068 repeat:
3069 rc = read_index(ip, dir_index, &dirtab_slot);
3070 if (rc) {
3071 ctx->pos = DIREND;
3072 return rc;
3074 if (dirtab_slot.flag == DIR_INDEX_FREE) {
3075 if (loop_count++ > JFS_IP(ip)->next_index) {
3076 jfs_err("jfs_readdir detected infinite loop!");
3077 ctx->pos = DIREND;
3078 return 0;
3080 dir_index = le32_to_cpu(dirtab_slot.addr2);
3081 if (dir_index == -1) {
3082 ctx->pos = DIREND;
3083 return 0;
3085 goto repeat;
3087 bn = addressDTS(&dirtab_slot);
3088 index = dirtab_slot.slot;
3089 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3090 if (rc) {
3091 ctx->pos = DIREND;
3092 return 0;
3094 if (p->header.flag & BT_INTERNAL) {
3095 jfs_err("jfs_readdir: bad index table");
3096 DT_PUTPAGE(mp);
3097 ctx->pos = DIREND;
3098 return 0;
3100 } else {
3101 if (dir_index == 0) {
3103 * self "."
3105 ctx->pos = 1;
3106 if (!dir_emit(ctx, ".", 1, ip->i_ino, DT_DIR))
3107 return 0;
3110 * parent ".."
3112 ctx->pos = 2;
3113 if (!dir_emit(ctx, "..", 2, PARENT(ip), DT_DIR))
3114 return 0;
3117 * Find first entry of left-most leaf
3119 if (dtEmpty(ip)) {
3120 ctx->pos = DIREND;
3121 return 0;
3124 if ((rc = dtReadFirst(ip, &btstack)))
3125 return rc;
3127 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
3129 } else {
3131 * Legacy filesystem - OS/2 & Linux JFS < 0.3.6
3133 * pn = 0; index = 1: First entry "."
3134 * pn = 0; index = 2: Second entry ".."
3135 * pn > 0: Real entries, pn=1 -> leftmost page
3136 * pn = index = -1: No more entries
3138 dtpos = ctx->pos;
3139 if (dtpos < 2) {
3140 /* build "." entry */
3141 ctx->pos = 1;
3142 if (!dir_emit(ctx, ".", 1, ip->i_ino, DT_DIR))
3143 return 0;
3144 dtoffset->index = 2;
3145 ctx->pos = dtpos;
3148 if (dtoffset->pn == 0) {
3149 if (dtoffset->index == 2) {
3150 /* build ".." entry */
3151 if (!dir_emit(ctx, "..", 2, PARENT(ip), DT_DIR))
3152 return 0;
3153 } else {
3154 jfs_err("jfs_readdir called with invalid offset!");
3156 dtoffset->pn = 1;
3157 dtoffset->index = 0;
3158 ctx->pos = dtpos;
3161 if (dtEmpty(ip)) {
3162 ctx->pos = DIREND;
3163 return 0;
3166 if ((rc = dtReadNext(ip, &ctx->pos, &btstack))) {
3167 jfs_err("jfs_readdir: unexpected rc = %d from dtReadNext",
3168 rc);
3169 ctx->pos = DIREND;
3170 return 0;
3172 /* get start leaf page and index */
3173 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
3175 /* offset beyond directory eof ? */
3176 if (bn < 0) {
3177 ctx->pos = DIREND;
3178 return 0;
3182 dirent_buf = __get_free_page(GFP_KERNEL);
3183 if (dirent_buf == 0) {
3184 DT_PUTPAGE(mp);
3185 jfs_warn("jfs_readdir: __get_free_page failed!");
3186 ctx->pos = DIREND;
3187 return -ENOMEM;
3190 while (1) {
3191 jfs_dirent = (struct jfs_dirent *) dirent_buf;
3192 jfs_dirents = 0;
3193 overflow = fix_page = 0;
3195 stbl = DT_GETSTBL(p);
3197 for (i = index; i < p->header.nextindex; i++) {
3198 d = (struct ldtentry *) & p->slot[stbl[i]];
3200 if (((long) jfs_dirent + d->namlen + 1) >
3201 (dirent_buf + PAGE_SIZE)) {
3202 /* DBCS codepages could overrun dirent_buf */
3203 index = i;
3204 overflow = 1;
3205 break;
3208 d_namleft = d->namlen;
3209 name_ptr = jfs_dirent->name;
3210 jfs_dirent->ino = le32_to_cpu(d->inumber);
3212 if (do_index) {
3213 len = min(d_namleft, DTLHDRDATALEN);
3214 jfs_dirent->position = le32_to_cpu(d->index);
3216 * d->index should always be valid, but it
3217 * isn't. fsck.jfs doesn't create the
3218 * directory index for the lost+found
3219 * directory. Rather than let it go,
3220 * we can try to fix it.
3222 if ((jfs_dirent->position < 2) ||
3223 (jfs_dirent->position >=
3224 JFS_IP(ip)->next_index)) {
3225 if (!page_fixed && !isReadOnly(ip)) {
3226 fix_page = 1;
3228 * setting overflow and setting
3229 * index to i will cause the
3230 * same page to be processed
3231 * again starting here
3233 overflow = 1;
3234 index = i;
3235 break;
3237 jfs_dirent->position = unique_pos++;
3240 * We add 1 to the index because we may
3241 * use a value of 2 internally, and NFSv4
3242 * doesn't like that.
3244 jfs_dirent->position++;
3245 } else {
3246 jfs_dirent->position = dtpos;
3247 len = min(d_namleft, DTLHDRDATALEN_LEGACY);
3250 /* copy the name of head/only segment */
3251 outlen = jfs_strfromUCS_le(name_ptr, d->name, len,
3252 codepage);
3253 jfs_dirent->name_len = outlen;
3255 /* copy name in the additional segment(s) */
3256 next = d->next;
3257 while (next >= 0) {
3258 t = (struct dtslot *) & p->slot[next];
3259 name_ptr += outlen;
3260 d_namleft -= len;
3261 /* Sanity Check */
3262 if (d_namleft == 0) {
3263 jfs_error(ip->i_sb,
3264 "JFS:Dtree error: ino = %ld, bn=%lld, index = %d\n",
3265 (long)ip->i_ino,
3266 (long long)bn,
3268 goto skip_one;
3270 len = min(d_namleft, DTSLOTDATALEN);
3271 outlen = jfs_strfromUCS_le(name_ptr, t->name,
3272 len, codepage);
3273 jfs_dirent->name_len += outlen;
3275 next = t->next;
3278 jfs_dirents++;
3279 jfs_dirent = next_jfs_dirent(jfs_dirent);
3280 skip_one:
3281 if (!do_index)
3282 dtoffset->index++;
3285 if (!overflow) {
3286 /* Point to next leaf page */
3287 if (p->header.flag & BT_ROOT)
3288 bn = 0;
3289 else {
3290 bn = le64_to_cpu(p->header.next);
3291 index = 0;
3292 /* update offset (pn:index) for new page */
3293 if (!do_index) {
3294 dtoffset->pn++;
3295 dtoffset->index = 0;
3298 page_fixed = 0;
3301 /* unpin previous leaf page */
3302 DT_PUTPAGE(mp);
3304 jfs_dirent = (struct jfs_dirent *) dirent_buf;
3305 while (jfs_dirents--) {
3306 ctx->pos = jfs_dirent->position;
3307 if (!dir_emit(ctx, jfs_dirent->name,
3308 jfs_dirent->name_len,
3309 jfs_dirent->ino, DT_UNKNOWN))
3310 goto out;
3311 jfs_dirent = next_jfs_dirent(jfs_dirent);
3314 if (fix_page) {
3315 add_missing_indices(ip, bn);
3316 page_fixed = 1;
3319 if (!overflow && (bn == 0)) {
3320 ctx->pos = DIREND;
3321 break;
3324 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3325 if (rc) {
3326 free_page(dirent_buf);
3327 return rc;
3331 out:
3332 free_page(dirent_buf);
3334 return rc;
3339 * dtReadFirst()
3341 * function: get the leftmost page of the directory
3343 static int dtReadFirst(struct inode *ip, struct btstack * btstack)
3345 int rc = 0;
3346 s64 bn;
3347 int psize = 288; /* initial in-line directory */
3348 struct metapage *mp;
3349 dtpage_t *p;
3350 s8 *stbl;
3351 struct btframe *btsp;
3352 pxd_t *xd;
3354 BT_CLR(btstack); /* reset stack */
3357 * descend leftmost path of the tree
3359 * by convention, root bn = 0.
3361 for (bn = 0;;) {
3362 DT_GETPAGE(ip, bn, mp, psize, p, rc);
3363 if (rc)
3364 return rc;
3367 * leftmost leaf page
3369 if (p->header.flag & BT_LEAF) {
3370 /* return leftmost entry */
3371 btsp = btstack->top;
3372 btsp->bn = bn;
3373 btsp->index = 0;
3374 btsp->mp = mp;
3376 return 0;
3380 * descend down to leftmost child page
3382 if (BT_STACK_FULL(btstack)) {
3383 DT_PUTPAGE(mp);
3384 jfs_error(ip->i_sb, "btstack overrun\n");
3385 BT_STACK_DUMP(btstack);
3386 return -EIO;
3388 /* push (bn, index) of the parent page/entry */
3389 BT_PUSH(btstack, bn, 0);
3391 /* get the leftmost entry */
3392 stbl = DT_GETSTBL(p);
3393 xd = (pxd_t *) & p->slot[stbl[0]];
3395 /* get the child page block address */
3396 bn = addressPXD(xd);
3397 psize = lengthPXD(xd) << JFS_SBI(ip->i_sb)->l2bsize;
3399 /* unpin the parent page */
3400 DT_PUTPAGE(mp);
3406 * dtReadNext()
3408 * function: get the page of the specified offset (pn:index)
3410 * return: if (offset > eof), bn = -1;
3412 * note: if index > nextindex of the target leaf page,
3413 * start with 1st entry of next leaf page;
3415 static int dtReadNext(struct inode *ip, loff_t * offset,
3416 struct btstack * btstack)
3418 int rc = 0;
3419 struct dtoffset {
3420 s16 pn;
3421 s16 index;
3422 s32 unused;
3423 } *dtoffset = (struct dtoffset *) offset;
3424 s64 bn;
3425 struct metapage *mp;
3426 dtpage_t *p;
3427 int index;
3428 int pn;
3429 s8 *stbl;
3430 struct btframe *btsp, *parent;
3431 pxd_t *xd;
3434 * get leftmost leaf page pinned
3436 if ((rc = dtReadFirst(ip, btstack)))
3437 return rc;
3439 /* get leaf page */
3440 DT_GETSEARCH(ip, btstack->top, bn, mp, p, index);
3442 /* get the start offset (pn:index) */
3443 pn = dtoffset->pn - 1; /* Now pn = 0 represents leftmost leaf */
3444 index = dtoffset->index;
3446 /* start at leftmost page ? */
3447 if (pn == 0) {
3448 /* offset beyond eof ? */
3449 if (index < p->header.nextindex)
3450 goto out;
3452 if (p->header.flag & BT_ROOT) {
3453 bn = -1;
3454 goto out;
3457 /* start with 1st entry of next leaf page */
3458 dtoffset->pn++;
3459 dtoffset->index = index = 0;
3460 goto a;
3463 /* start at non-leftmost page: scan parent pages for large pn */
3464 if (p->header.flag & BT_ROOT) {
3465 bn = -1;
3466 goto out;
3469 /* start after next leaf page ? */
3470 if (pn > 1)
3471 goto b;
3473 /* get leaf page pn = 1 */
3475 bn = le64_to_cpu(p->header.next);
3477 /* unpin leaf page */
3478 DT_PUTPAGE(mp);
3480 /* offset beyond eof ? */
3481 if (bn == 0) {
3482 bn = -1;
3483 goto out;
3486 goto c;
3489 * scan last internal page level to get target leaf page
3492 /* unpin leftmost leaf page */
3493 DT_PUTPAGE(mp);
3495 /* get left most parent page */
3496 btsp = btstack->top;
3497 parent = btsp - 1;
3498 bn = parent->bn;
3499 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3500 if (rc)
3501 return rc;
3503 /* scan parent pages at last internal page level */
3504 while (pn >= p->header.nextindex) {
3505 pn -= p->header.nextindex;
3507 /* get next parent page address */
3508 bn = le64_to_cpu(p->header.next);
3510 /* unpin current parent page */
3511 DT_PUTPAGE(mp);
3513 /* offset beyond eof ? */
3514 if (bn == 0) {
3515 bn = -1;
3516 goto out;
3519 /* get next parent page */
3520 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3521 if (rc)
3522 return rc;
3524 /* update parent page stack frame */
3525 parent->bn = bn;
3528 /* get leaf page address */
3529 stbl = DT_GETSTBL(p);
3530 xd = (pxd_t *) & p->slot[stbl[pn]];
3531 bn = addressPXD(xd);
3533 /* unpin parent page */
3534 DT_PUTPAGE(mp);
3537 * get target leaf page
3540 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3541 if (rc)
3542 return rc;
3545 * leaf page has been completed:
3546 * start with 1st entry of next leaf page
3548 if (index >= p->header.nextindex) {
3549 bn = le64_to_cpu(p->header.next);
3551 /* unpin leaf page */
3552 DT_PUTPAGE(mp);
3554 /* offset beyond eof ? */
3555 if (bn == 0) {
3556 bn = -1;
3557 goto out;
3560 /* get next leaf page */
3561 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3562 if (rc)
3563 return rc;
3565 /* start with 1st entry of next leaf page */
3566 dtoffset->pn++;
3567 dtoffset->index = 0;
3570 out:
3571 /* return target leaf page pinned */
3572 btsp = btstack->top;
3573 btsp->bn = bn;
3574 btsp->index = dtoffset->index;
3575 btsp->mp = mp;
3577 return 0;
3582 * dtCompare()
3584 * function: compare search key with an internal entry
3586 * return:
3587 * < 0 if k is < record
3588 * = 0 if k is = record
3589 * > 0 if k is > record
3591 static int dtCompare(struct component_name * key, /* search key */
3592 dtpage_t * p, /* directory page */
3593 int si)
3594 { /* entry slot index */
3595 wchar_t *kname;
3596 __le16 *name;
3597 int klen, namlen, len, rc;
3598 struct idtentry *ih;
3599 struct dtslot *t;
3602 * force the left-most key on internal pages, at any level of
3603 * the tree, to be less than any search key.
3604 * this obviates having to update the leftmost key on an internal
3605 * page when the user inserts a new key in the tree smaller than
3606 * anything that has been stored.
3608 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3609 * at any internal page at any level of the tree,
3610 * it descends to child of the entry anyway -
3611 * ? make the entry as min size dummy entry)
3613 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3614 * return (1);
3617 kname = key->name;
3618 klen = key->namlen;
3620 ih = (struct idtentry *) & p->slot[si];
3621 si = ih->next;
3622 name = ih->name;
3623 namlen = ih->namlen;
3624 len = min(namlen, DTIHDRDATALEN);
3626 /* compare with head/only segment */
3627 len = min(klen, len);
3628 if ((rc = UniStrncmp_le(kname, name, len)))
3629 return rc;
3631 klen -= len;
3632 namlen -= len;
3634 /* compare with additional segment(s) */
3635 kname += len;
3636 while (klen > 0 && namlen > 0) {
3637 /* compare with next name segment */
3638 t = (struct dtslot *) & p->slot[si];
3639 len = min(namlen, DTSLOTDATALEN);
3640 len = min(klen, len);
3641 name = t->name;
3642 if ((rc = UniStrncmp_le(kname, name, len)))
3643 return rc;
3645 klen -= len;
3646 namlen -= len;
3647 kname += len;
3648 si = t->next;
3651 return (klen - namlen);
3658 * ciCompare()
3660 * function: compare search key with an (leaf/internal) entry
3662 * return:
3663 * < 0 if k is < record
3664 * = 0 if k is = record
3665 * > 0 if k is > record
3667 static int ciCompare(struct component_name * key, /* search key */
3668 dtpage_t * p, /* directory page */
3669 int si, /* entry slot index */
3670 int flag)
3672 wchar_t *kname, x;
3673 __le16 *name;
3674 int klen, namlen, len, rc;
3675 struct ldtentry *lh;
3676 struct idtentry *ih;
3677 struct dtslot *t;
3678 int i;
3681 * force the left-most key on internal pages, at any level of
3682 * the tree, to be less than any search key.
3683 * this obviates having to update the leftmost key on an internal
3684 * page when the user inserts a new key in the tree smaller than
3685 * anything that has been stored.
3687 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3688 * at any internal page at any level of the tree,
3689 * it descends to child of the entry anyway -
3690 * ? make the entry as min size dummy entry)
3692 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3693 * return (1);
3696 kname = key->name;
3697 klen = key->namlen;
3700 * leaf page entry
3702 if (p->header.flag & BT_LEAF) {
3703 lh = (struct ldtentry *) & p->slot[si];
3704 si = lh->next;
3705 name = lh->name;
3706 namlen = lh->namlen;
3707 if (flag & JFS_DIR_INDEX)
3708 len = min(namlen, DTLHDRDATALEN);
3709 else
3710 len = min(namlen, DTLHDRDATALEN_LEGACY);
3713 * internal page entry
3715 else {
3716 ih = (struct idtentry *) & p->slot[si];
3717 si = ih->next;
3718 name = ih->name;
3719 namlen = ih->namlen;
3720 len = min(namlen, DTIHDRDATALEN);
3723 /* compare with head/only segment */
3724 len = min(klen, len);
3725 for (i = 0; i < len; i++, kname++, name++) {
3726 /* only uppercase if case-insensitive support is on */
3727 if ((flag & JFS_OS2) == JFS_OS2)
3728 x = UniToupper(le16_to_cpu(*name));
3729 else
3730 x = le16_to_cpu(*name);
3731 if ((rc = *kname - x))
3732 return rc;
3735 klen -= len;
3736 namlen -= len;
3738 /* compare with additional segment(s) */
3739 while (klen > 0 && namlen > 0) {
3740 /* compare with next name segment */
3741 t = (struct dtslot *) & p->slot[si];
3742 len = min(namlen, DTSLOTDATALEN);
3743 len = min(klen, len);
3744 name = t->name;
3745 for (i = 0; i < len; i++, kname++, name++) {
3746 /* only uppercase if case-insensitive support is on */
3747 if ((flag & JFS_OS2) == JFS_OS2)
3748 x = UniToupper(le16_to_cpu(*name));
3749 else
3750 x = le16_to_cpu(*name);
3752 if ((rc = *kname - x))
3753 return rc;
3756 klen -= len;
3757 namlen -= len;
3758 si = t->next;
3761 return (klen - namlen);
3766 * ciGetLeafPrefixKey()
3768 * function: compute prefix of suffix compression
3769 * from two adjacent leaf entries
3770 * across page boundary
3772 * return: non-zero on error
3775 static int ciGetLeafPrefixKey(dtpage_t * lp, int li, dtpage_t * rp,
3776 int ri, struct component_name * key, int flag)
3778 int klen, namlen;
3779 wchar_t *pl, *pr, *kname;
3780 struct component_name lkey;
3781 struct component_name rkey;
3783 lkey.name = kmalloc_array(JFS_NAME_MAX + 1, sizeof(wchar_t),
3784 GFP_KERNEL);
3785 if (lkey.name == NULL)
3786 return -ENOMEM;
3788 rkey.name = kmalloc_array(JFS_NAME_MAX + 1, sizeof(wchar_t),
3789 GFP_KERNEL);
3790 if (rkey.name == NULL) {
3791 kfree(lkey.name);
3792 return -ENOMEM;
3795 /* get left and right key */
3796 dtGetKey(lp, li, &lkey, flag);
3797 lkey.name[lkey.namlen] = 0;
3799 if ((flag & JFS_OS2) == JFS_OS2)
3800 ciToUpper(&lkey);
3802 dtGetKey(rp, ri, &rkey, flag);
3803 rkey.name[rkey.namlen] = 0;
3806 if ((flag & JFS_OS2) == JFS_OS2)
3807 ciToUpper(&rkey);
3809 /* compute prefix */
3810 klen = 0;
3811 kname = key->name;
3812 namlen = min(lkey.namlen, rkey.namlen);
3813 for (pl = lkey.name, pr = rkey.name;
3814 namlen; pl++, pr++, namlen--, klen++, kname++) {
3815 *kname = *pr;
3816 if (*pl != *pr) {
3817 key->namlen = klen + 1;
3818 goto free_names;
3822 /* l->namlen <= r->namlen since l <= r */
3823 if (lkey.namlen < rkey.namlen) {
3824 *kname = *pr;
3825 key->namlen = klen + 1;
3826 } else /* l->namelen == r->namelen */
3827 key->namlen = klen;
3829 free_names:
3830 kfree(lkey.name);
3831 kfree(rkey.name);
3832 return 0;
3838 * dtGetKey()
3840 * function: get key of the entry
3842 static void dtGetKey(dtpage_t * p, int i, /* entry index */
3843 struct component_name * key, int flag)
3845 int si;
3846 s8 *stbl;
3847 struct ldtentry *lh;
3848 struct idtentry *ih;
3849 struct dtslot *t;
3850 int namlen, len;
3851 wchar_t *kname;
3852 __le16 *name;
3854 /* get entry */
3855 stbl = DT_GETSTBL(p);
3856 si = stbl[i];
3857 if (p->header.flag & BT_LEAF) {
3858 lh = (struct ldtentry *) & p->slot[si];
3859 si = lh->next;
3860 namlen = lh->namlen;
3861 name = lh->name;
3862 if (flag & JFS_DIR_INDEX)
3863 len = min(namlen, DTLHDRDATALEN);
3864 else
3865 len = min(namlen, DTLHDRDATALEN_LEGACY);
3866 } else {
3867 ih = (struct idtentry *) & p->slot[si];
3868 si = ih->next;
3869 namlen = ih->namlen;
3870 name = ih->name;
3871 len = min(namlen, DTIHDRDATALEN);
3874 key->namlen = namlen;
3875 kname = key->name;
3878 * move head/only segment
3880 UniStrncpy_from_le(kname, name, len);
3883 * move additional segment(s)
3885 while (si >= 0) {
3886 /* get next segment */
3887 t = &p->slot[si];
3888 kname += len;
3889 namlen -= len;
3890 len = min(namlen, DTSLOTDATALEN);
3891 UniStrncpy_from_le(kname, t->name, len);
3893 si = t->next;
3899 * dtInsertEntry()
3901 * function: allocate free slot(s) and
3902 * write a leaf/internal entry
3904 * return: entry slot index
3906 static void dtInsertEntry(dtpage_t * p, int index, struct component_name * key,
3907 ddata_t * data, struct dt_lock ** dtlock)
3909 struct dtslot *h, *t;
3910 struct ldtentry *lh = NULL;
3911 struct idtentry *ih = NULL;
3912 int hsi, fsi, klen, len, nextindex;
3913 wchar_t *kname;
3914 __le16 *name;
3915 s8 *stbl;
3916 pxd_t *xd;
3917 struct dt_lock *dtlck = *dtlock;
3918 struct lv *lv;
3919 int xsi, n;
3920 s64 bn = 0;
3921 struct metapage *mp = NULL;
3923 klen = key->namlen;
3924 kname = key->name;
3926 /* allocate a free slot */
3927 hsi = fsi = p->header.freelist;
3928 h = &p->slot[fsi];
3929 p->header.freelist = h->next;
3930 --p->header.freecnt;
3932 /* open new linelock */
3933 if (dtlck->index >= dtlck->maxcnt)
3934 dtlck = (struct dt_lock *) txLinelock(dtlck);
3936 lv = & dtlck->lv[dtlck->index];
3937 lv->offset = hsi;
3939 /* write head/only segment */
3940 if (p->header.flag & BT_LEAF) {
3941 lh = (struct ldtentry *) h;
3942 lh->next = h->next;
3943 lh->inumber = cpu_to_le32(data->leaf.ino);
3944 lh->namlen = klen;
3945 name = lh->name;
3946 if (data->leaf.ip) {
3947 len = min(klen, DTLHDRDATALEN);
3948 if (!(p->header.flag & BT_ROOT))
3949 bn = addressPXD(&p->header.self);
3950 lh->index = cpu_to_le32(add_index(data->leaf.tid,
3951 data->leaf.ip,
3952 bn, index));
3953 } else
3954 len = min(klen, DTLHDRDATALEN_LEGACY);
3955 } else {
3956 ih = (struct idtentry *) h;
3957 ih->next = h->next;
3958 xd = (pxd_t *) ih;
3959 *xd = data->xd;
3960 ih->namlen = klen;
3961 name = ih->name;
3962 len = min(klen, DTIHDRDATALEN);
3965 UniStrncpy_to_le(name, kname, len);
3967 n = 1;
3968 xsi = hsi;
3970 /* write additional segment(s) */
3971 t = h;
3972 klen -= len;
3973 while (klen) {
3974 /* get free slot */
3975 fsi = p->header.freelist;
3976 t = &p->slot[fsi];
3977 p->header.freelist = t->next;
3978 --p->header.freecnt;
3980 /* is next slot contiguous ? */
3981 if (fsi != xsi + 1) {
3982 /* close current linelock */
3983 lv->length = n;
3984 dtlck->index++;
3986 /* open new linelock */
3987 if (dtlck->index < dtlck->maxcnt)
3988 lv++;
3989 else {
3990 dtlck = (struct dt_lock *) txLinelock(dtlck);
3991 lv = & dtlck->lv[0];
3994 lv->offset = fsi;
3995 n = 0;
3998 kname += len;
3999 len = min(klen, DTSLOTDATALEN);
4000 UniStrncpy_to_le(t->name, kname, len);
4002 n++;
4003 xsi = fsi;
4004 klen -= len;
4007 /* close current linelock */
4008 lv->length = n;
4009 dtlck->index++;
4011 *dtlock = dtlck;
4013 /* terminate last/only segment */
4014 if (h == t) {
4015 /* single segment entry */
4016 if (p->header.flag & BT_LEAF)
4017 lh->next = -1;
4018 else
4019 ih->next = -1;
4020 } else
4021 /* multi-segment entry */
4022 t->next = -1;
4024 /* if insert into middle, shift right succeeding entries in stbl */
4025 stbl = DT_GETSTBL(p);
4026 nextindex = p->header.nextindex;
4027 if (index < nextindex) {
4028 memmove(stbl + index + 1, stbl + index, nextindex - index);
4030 if ((p->header.flag & BT_LEAF) && data->leaf.ip) {
4031 s64 lblock;
4034 * Need to update slot number for entries that moved
4035 * in the stbl
4037 mp = NULL;
4038 for (n = index + 1; n <= nextindex; n++) {
4039 lh = (struct ldtentry *) & (p->slot[stbl[n]]);
4040 modify_index(data->leaf.tid, data->leaf.ip,
4041 le32_to_cpu(lh->index), bn, n,
4042 &mp, &lblock);
4044 if (mp)
4045 release_metapage(mp);
4049 stbl[index] = hsi;
4051 /* advance next available entry index of stbl */
4052 ++p->header.nextindex;
4057 * dtMoveEntry()
4059 * function: move entries from split/left page to new/right page
4061 * nextindex of dst page and freelist/freecnt of both pages
4062 * are updated.
4064 static void dtMoveEntry(dtpage_t * sp, int si, dtpage_t * dp,
4065 struct dt_lock ** sdtlock, struct dt_lock ** ddtlock,
4066 int do_index)
4068 int ssi, next; /* src slot index */
4069 int di; /* dst entry index */
4070 int dsi; /* dst slot index */
4071 s8 *sstbl, *dstbl; /* sorted entry table */
4072 int snamlen, len;
4073 struct ldtentry *slh, *dlh = NULL;
4074 struct idtentry *sih, *dih = NULL;
4075 struct dtslot *h, *s, *d;
4076 struct dt_lock *sdtlck = *sdtlock, *ddtlck = *ddtlock;
4077 struct lv *slv, *dlv;
4078 int xssi, ns, nd;
4079 int sfsi;
4081 sstbl = (s8 *) & sp->slot[sp->header.stblindex];
4082 dstbl = (s8 *) & dp->slot[dp->header.stblindex];
4084 dsi = dp->header.freelist; /* first (whole page) free slot */
4085 sfsi = sp->header.freelist;
4087 /* linelock destination entry slot */
4088 dlv = & ddtlck->lv[ddtlck->index];
4089 dlv->offset = dsi;
4091 /* linelock source entry slot */
4092 slv = & sdtlck->lv[sdtlck->index];
4093 slv->offset = sstbl[si];
4094 xssi = slv->offset - 1;
4097 * move entries
4099 ns = nd = 0;
4100 for (di = 0; si < sp->header.nextindex; si++, di++) {
4101 ssi = sstbl[si];
4102 dstbl[di] = dsi;
4104 /* is next slot contiguous ? */
4105 if (ssi != xssi + 1) {
4106 /* close current linelock */
4107 slv->length = ns;
4108 sdtlck->index++;
4110 /* open new linelock */
4111 if (sdtlck->index < sdtlck->maxcnt)
4112 slv++;
4113 else {
4114 sdtlck = (struct dt_lock *) txLinelock(sdtlck);
4115 slv = & sdtlck->lv[0];
4118 slv->offset = ssi;
4119 ns = 0;
4123 * move head/only segment of an entry
4125 /* get dst slot */
4126 h = d = &dp->slot[dsi];
4128 /* get src slot and move */
4129 s = &sp->slot[ssi];
4130 if (sp->header.flag & BT_LEAF) {
4131 /* get source entry */
4132 slh = (struct ldtentry *) s;
4133 dlh = (struct ldtentry *) h;
4134 snamlen = slh->namlen;
4136 if (do_index) {
4137 len = min(snamlen, DTLHDRDATALEN);
4138 dlh->index = slh->index; /* little-endian */
4139 } else
4140 len = min(snamlen, DTLHDRDATALEN_LEGACY);
4142 memcpy(dlh, slh, 6 + len * 2);
4144 next = slh->next;
4146 /* update dst head/only segment next field */
4147 dsi++;
4148 dlh->next = dsi;
4149 } else {
4150 sih = (struct idtentry *) s;
4151 snamlen = sih->namlen;
4153 len = min(snamlen, DTIHDRDATALEN);
4154 dih = (struct idtentry *) h;
4155 memcpy(dih, sih, 10 + len * 2);
4156 next = sih->next;
4158 dsi++;
4159 dih->next = dsi;
4162 /* free src head/only segment */
4163 s->next = sfsi;
4164 s->cnt = 1;
4165 sfsi = ssi;
4167 ns++;
4168 nd++;
4169 xssi = ssi;
4172 * move additional segment(s) of the entry
4174 snamlen -= len;
4175 while ((ssi = next) >= 0) {
4176 /* is next slot contiguous ? */
4177 if (ssi != xssi + 1) {
4178 /* close current linelock */
4179 slv->length = ns;
4180 sdtlck->index++;
4182 /* open new linelock */
4183 if (sdtlck->index < sdtlck->maxcnt)
4184 slv++;
4185 else {
4186 sdtlck =
4187 (struct dt_lock *)
4188 txLinelock(sdtlck);
4189 slv = & sdtlck->lv[0];
4192 slv->offset = ssi;
4193 ns = 0;
4196 /* get next source segment */
4197 s = &sp->slot[ssi];
4199 /* get next destination free slot */
4200 d++;
4202 len = min(snamlen, DTSLOTDATALEN);
4203 UniStrncpy_le(d->name, s->name, len);
4205 ns++;
4206 nd++;
4207 xssi = ssi;
4209 dsi++;
4210 d->next = dsi;
4212 /* free source segment */
4213 next = s->next;
4214 s->next = sfsi;
4215 s->cnt = 1;
4216 sfsi = ssi;
4218 snamlen -= len;
4219 } /* end while */
4221 /* terminate dst last/only segment */
4222 if (h == d) {
4223 /* single segment entry */
4224 if (dp->header.flag & BT_LEAF)
4225 dlh->next = -1;
4226 else
4227 dih->next = -1;
4228 } else
4229 /* multi-segment entry */
4230 d->next = -1;
4231 } /* end for */
4233 /* close current linelock */
4234 slv->length = ns;
4235 sdtlck->index++;
4236 *sdtlock = sdtlck;
4238 dlv->length = nd;
4239 ddtlck->index++;
4240 *ddtlock = ddtlck;
4242 /* update source header */
4243 sp->header.freelist = sfsi;
4244 sp->header.freecnt += nd;
4246 /* update destination header */
4247 dp->header.nextindex = di;
4249 dp->header.freelist = dsi;
4250 dp->header.freecnt -= nd;
4255 * dtDeleteEntry()
4257 * function: free a (leaf/internal) entry
4259 * log freelist header, stbl, and each segment slot of entry
4260 * (even though last/only segment next field is modified,
4261 * physical image logging requires all segment slots of
4262 * the entry logged to avoid applying previous updates
4263 * to the same slots)
4265 static void dtDeleteEntry(dtpage_t * p, int fi, struct dt_lock ** dtlock)
4267 int fsi; /* free entry slot index */
4268 s8 *stbl;
4269 struct dtslot *t;
4270 int si, freecnt;
4271 struct dt_lock *dtlck = *dtlock;
4272 struct lv *lv;
4273 int xsi, n;
4275 /* get free entry slot index */
4276 stbl = DT_GETSTBL(p);
4277 fsi = stbl[fi];
4279 /* open new linelock */
4280 if (dtlck->index >= dtlck->maxcnt)
4281 dtlck = (struct dt_lock *) txLinelock(dtlck);
4282 lv = & dtlck->lv[dtlck->index];
4284 lv->offset = fsi;
4286 /* get the head/only segment */
4287 t = &p->slot[fsi];
4288 if (p->header.flag & BT_LEAF)
4289 si = ((struct ldtentry *) t)->next;
4290 else
4291 si = ((struct idtentry *) t)->next;
4292 t->next = si;
4293 t->cnt = 1;
4295 n = freecnt = 1;
4296 xsi = fsi;
4298 /* find the last/only segment */
4299 while (si >= 0) {
4300 /* is next slot contiguous ? */
4301 if (si != xsi + 1) {
4302 /* close current linelock */
4303 lv->length = n;
4304 dtlck->index++;
4306 /* open new linelock */
4307 if (dtlck->index < dtlck->maxcnt)
4308 lv++;
4309 else {
4310 dtlck = (struct dt_lock *) txLinelock(dtlck);
4311 lv = & dtlck->lv[0];
4314 lv->offset = si;
4315 n = 0;
4318 n++;
4319 xsi = si;
4320 freecnt++;
4322 t = &p->slot[si];
4323 t->cnt = 1;
4324 si = t->next;
4327 /* close current linelock */
4328 lv->length = n;
4329 dtlck->index++;
4331 *dtlock = dtlck;
4333 /* update freelist */
4334 t->next = p->header.freelist;
4335 p->header.freelist = fsi;
4336 p->header.freecnt += freecnt;
4338 /* if delete from middle,
4339 * shift left the succedding entries in the stbl
4341 si = p->header.nextindex;
4342 if (fi < si - 1)
4343 memmove(&stbl[fi], &stbl[fi + 1], si - fi - 1);
4345 p->header.nextindex--;
4350 * dtTruncateEntry()
4352 * function: truncate a (leaf/internal) entry
4354 * log freelist header, stbl, and each segment slot of entry
4355 * (even though last/only segment next field is modified,
4356 * physical image logging requires all segment slots of
4357 * the entry logged to avoid applying previous updates
4358 * to the same slots)
4360 static void dtTruncateEntry(dtpage_t * p, int ti, struct dt_lock ** dtlock)
4362 int tsi; /* truncate entry slot index */
4363 s8 *stbl;
4364 struct dtslot *t;
4365 int si, freecnt;
4366 struct dt_lock *dtlck = *dtlock;
4367 struct lv *lv;
4368 int fsi, xsi, n;
4370 /* get free entry slot index */
4371 stbl = DT_GETSTBL(p);
4372 tsi = stbl[ti];
4374 /* open new linelock */
4375 if (dtlck->index >= dtlck->maxcnt)
4376 dtlck = (struct dt_lock *) txLinelock(dtlck);
4377 lv = & dtlck->lv[dtlck->index];
4379 lv->offset = tsi;
4381 /* get the head/only segment */
4382 t = &p->slot[tsi];
4383 ASSERT(p->header.flag & BT_INTERNAL);
4384 ((struct idtentry *) t)->namlen = 0;
4385 si = ((struct idtentry *) t)->next;
4386 ((struct idtentry *) t)->next = -1;
4388 n = 1;
4389 freecnt = 0;
4390 fsi = si;
4391 xsi = tsi;
4393 /* find the last/only segment */
4394 while (si >= 0) {
4395 /* is next slot contiguous ? */
4396 if (si != xsi + 1) {
4397 /* close current linelock */
4398 lv->length = n;
4399 dtlck->index++;
4401 /* open new linelock */
4402 if (dtlck->index < dtlck->maxcnt)
4403 lv++;
4404 else {
4405 dtlck = (struct dt_lock *) txLinelock(dtlck);
4406 lv = & dtlck->lv[0];
4409 lv->offset = si;
4410 n = 0;
4413 n++;
4414 xsi = si;
4415 freecnt++;
4417 t = &p->slot[si];
4418 t->cnt = 1;
4419 si = t->next;
4422 /* close current linelock */
4423 lv->length = n;
4424 dtlck->index++;
4426 *dtlock = dtlck;
4428 /* update freelist */
4429 if (freecnt == 0)
4430 return;
4431 t->next = p->header.freelist;
4432 p->header.freelist = fsi;
4433 p->header.freecnt += freecnt;
4438 * dtLinelockFreelist()
4440 static void dtLinelockFreelist(dtpage_t * p, /* directory page */
4441 int m, /* max slot index */
4442 struct dt_lock ** dtlock)
4444 int fsi; /* free entry slot index */
4445 struct dtslot *t;
4446 int si;
4447 struct dt_lock *dtlck = *dtlock;
4448 struct lv *lv;
4449 int xsi, n;
4451 /* get free entry slot index */
4452 fsi = p->header.freelist;
4454 /* open new linelock */
4455 if (dtlck->index >= dtlck->maxcnt)
4456 dtlck = (struct dt_lock *) txLinelock(dtlck);
4457 lv = & dtlck->lv[dtlck->index];
4459 lv->offset = fsi;
4461 n = 1;
4462 xsi = fsi;
4464 t = &p->slot[fsi];
4465 si = t->next;
4467 /* find the last/only segment */
4468 while (si < m && si >= 0) {
4469 /* is next slot contiguous ? */
4470 if (si != xsi + 1) {
4471 /* close current linelock */
4472 lv->length = n;
4473 dtlck->index++;
4475 /* open new linelock */
4476 if (dtlck->index < dtlck->maxcnt)
4477 lv++;
4478 else {
4479 dtlck = (struct dt_lock *) txLinelock(dtlck);
4480 lv = & dtlck->lv[0];
4483 lv->offset = si;
4484 n = 0;
4487 n++;
4488 xsi = si;
4490 t = &p->slot[si];
4491 si = t->next;
4494 /* close current linelock */
4495 lv->length = n;
4496 dtlck->index++;
4498 *dtlock = dtlck;
4503 * NAME: dtModify
4505 * FUNCTION: Modify the inode number part of a directory entry
4507 * PARAMETERS:
4508 * tid - Transaction id
4509 * ip - Inode of parent directory
4510 * key - Name of entry to be modified
4511 * orig_ino - Original inode number expected in entry
4512 * new_ino - New inode number to put into entry
4513 * flag - JFS_RENAME
4515 * RETURNS:
4516 * -ESTALE - If entry found does not match orig_ino passed in
4517 * -ENOENT - If no entry can be found to match key
4518 * 0 - If successfully modified entry
4520 int dtModify(tid_t tid, struct inode *ip,
4521 struct component_name * key, ino_t * orig_ino, ino_t new_ino, int flag)
4523 int rc;
4524 s64 bn;
4525 struct metapage *mp;
4526 dtpage_t *p;
4527 int index;
4528 struct btstack btstack;
4529 struct tlock *tlck;
4530 struct dt_lock *dtlck;
4531 struct lv *lv;
4532 s8 *stbl;
4533 int entry_si; /* entry slot index */
4534 struct ldtentry *entry;
4537 * search for the entry to modify:
4539 * dtSearch() returns (leaf page pinned, index at which to modify).
4541 if ((rc = dtSearch(ip, key, orig_ino, &btstack, flag)))
4542 return rc;
4544 /* retrieve search result */
4545 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
4547 BT_MARK_DIRTY(mp, ip);
4549 * acquire a transaction lock on the leaf page of named entry
4551 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
4552 dtlck = (struct dt_lock *) & tlck->lock;
4554 /* get slot index of the entry */
4555 stbl = DT_GETSTBL(p);
4556 entry_si = stbl[index];
4558 /* linelock entry */
4559 ASSERT(dtlck->index == 0);
4560 lv = & dtlck->lv[0];
4561 lv->offset = entry_si;
4562 lv->length = 1;
4563 dtlck->index++;
4565 /* get the head/only segment */
4566 entry = (struct ldtentry *) & p->slot[entry_si];
4568 /* substitute the inode number of the entry */
4569 entry->inumber = cpu_to_le32(new_ino);
4571 /* unpin the leaf page */
4572 DT_PUTPAGE(mp);
4574 return 0;