radix-tree-avoid-atomic-allocations-for-preloaded-insertions
[linux-2.6/linux-trees-mm.git] / fs / jfs / jfs_dtree.c
blob97c66f913393d94bfae4a73b5f9ac1c6ba5eee9c
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 "jfs_incore.h"
106 #include "jfs_superblock.h"
107 #include "jfs_filsys.h"
108 #include "jfs_metapage.h"
109 #include "jfs_dmap.h"
110 #include "jfs_unicode.h"
111 #include "jfs_debug.h"
113 /* dtree split parameter */
114 struct dtsplit {
115 struct metapage *mp;
116 s16 index;
117 s16 nslot;
118 struct component_name *key;
119 ddata_t *data;
120 struct pxdlist *pxdlist;
123 #define DT_PAGE(IP, MP) BT_PAGE(IP, MP, dtpage_t, i_dtroot)
125 /* get page buffer for specified block address */
126 #define DT_GETPAGE(IP, BN, MP, SIZE, P, RC)\
128 BT_GETPAGE(IP, BN, MP, dtpage_t, SIZE, P, RC, i_dtroot)\
129 if (!(RC))\
131 if (((P)->header.nextindex > (((BN)==0)?DTROOTMAXSLOT:(P)->header.maxslot)) ||\
132 ((BN) && ((P)->header.maxslot > DTPAGEMAXSLOT)))\
134 BT_PUTPAGE(MP);\
135 jfs_error((IP)->i_sb, "DT_GETPAGE: dtree page corrupt");\
136 MP = NULL;\
137 RC = -EIO;\
142 /* for consistency */
143 #define DT_PUTPAGE(MP) BT_PUTPAGE(MP)
145 #define DT_GETSEARCH(IP, LEAF, BN, MP, P, INDEX) \
146 BT_GETSEARCH(IP, LEAF, BN, MP, dtpage_t, P, INDEX, i_dtroot)
149 * forward references
151 static int dtSplitUp(tid_t tid, struct inode *ip,
152 struct dtsplit * split, struct btstack * btstack);
154 static int dtSplitPage(tid_t tid, struct inode *ip, struct dtsplit * split,
155 struct metapage ** rmpp, dtpage_t ** rpp, pxd_t * rxdp);
157 static int dtExtendPage(tid_t tid, struct inode *ip,
158 struct dtsplit * split, struct btstack * btstack);
160 static int dtSplitRoot(tid_t tid, struct inode *ip,
161 struct dtsplit * split, struct metapage ** rmpp);
163 static int dtDeleteUp(tid_t tid, struct inode *ip, struct metapage * fmp,
164 dtpage_t * fp, struct btstack * btstack);
166 static int dtRelink(tid_t tid, struct inode *ip, dtpage_t * p);
168 static int dtReadFirst(struct inode *ip, struct btstack * btstack);
170 static int dtReadNext(struct inode *ip,
171 loff_t * offset, struct btstack * btstack);
173 static int dtCompare(struct component_name * key, dtpage_t * p, int si);
175 static int ciCompare(struct component_name * key, dtpage_t * p, int si,
176 int flag);
178 static void dtGetKey(dtpage_t * p, int i, struct component_name * key,
179 int flag);
181 static int ciGetLeafPrefixKey(dtpage_t * lp, int li, dtpage_t * rp,
182 int ri, struct component_name * key, int flag);
184 static void dtInsertEntry(dtpage_t * p, int index, struct component_name * key,
185 ddata_t * data, struct dt_lock **);
187 static void dtMoveEntry(dtpage_t * sp, int si, dtpage_t * dp,
188 struct dt_lock ** sdtlock, struct dt_lock ** ddtlock,
189 int do_index);
191 static void dtDeleteEntry(dtpage_t * p, int fi, struct dt_lock ** dtlock);
193 static void dtTruncateEntry(dtpage_t * p, int ti, struct dt_lock ** dtlock);
195 static void dtLinelockFreelist(dtpage_t * p, int m, struct dt_lock ** dtlock);
197 #define ciToUpper(c) UniStrupr((c)->name)
200 * read_index_page()
202 * Reads a page of a directory's index table.
203 * Having metadata mapped into the directory inode's address space
204 * presents a multitude of problems. We avoid this by mapping to
205 * the absolute address space outside of the *_metapage routines
207 static struct metapage *read_index_page(struct inode *inode, s64 blkno)
209 int rc;
210 s64 xaddr;
211 int xflag;
212 s32 xlen;
214 rc = xtLookup(inode, blkno, 1, &xflag, &xaddr, &xlen, 1);
215 if (rc || (xaddr == 0))
216 return NULL;
218 return read_metapage(inode, xaddr, PSIZE, 1);
222 * get_index_page()
224 * Same as get_index_page(), but get's a new page without reading
226 static struct metapage *get_index_page(struct inode *inode, s64 blkno)
228 int rc;
229 s64 xaddr;
230 int xflag;
231 s32 xlen;
233 rc = xtLookup(inode, blkno, 1, &xflag, &xaddr, &xlen, 1);
234 if (rc || (xaddr == 0))
235 return NULL;
237 return get_metapage(inode, xaddr, PSIZE, 1);
241 * find_index()
243 * Returns dtree page containing directory table entry for specified
244 * index and pointer to its entry.
246 * mp must be released by caller.
248 static struct dir_table_slot *find_index(struct inode *ip, u32 index,
249 struct metapage ** mp, s64 *lblock)
251 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
252 s64 blkno;
253 s64 offset;
254 int page_offset;
255 struct dir_table_slot *slot;
256 static int maxWarnings = 10;
258 if (index < 2) {
259 if (maxWarnings) {
260 jfs_warn("find_entry called with index = %d", index);
261 maxWarnings--;
263 return NULL;
266 if (index >= jfs_ip->next_index) {
267 jfs_warn("find_entry called with index >= next_index");
268 return NULL;
271 if (jfs_dirtable_inline(ip)) {
273 * Inline directory table
275 *mp = NULL;
276 slot = &jfs_ip->i_dirtable[index - 2];
277 } else {
278 offset = (index - 2) * sizeof(struct dir_table_slot);
279 page_offset = offset & (PSIZE - 1);
280 blkno = ((offset + 1) >> L2PSIZE) <<
281 JFS_SBI(ip->i_sb)->l2nbperpage;
283 if (*mp && (*lblock != blkno)) {
284 release_metapage(*mp);
285 *mp = NULL;
287 if (*mp == 0) {
288 *lblock = blkno;
289 *mp = read_index_page(ip, blkno);
291 if (*mp == 0) {
292 jfs_err("free_index: error reading directory table");
293 return NULL;
296 slot =
297 (struct dir_table_slot *) ((char *) (*mp)->data +
298 page_offset);
300 return slot;
303 static inline void lock_index(tid_t tid, struct inode *ip, struct metapage * mp,
304 u32 index)
306 struct tlock *tlck;
307 struct linelock *llck;
308 struct lv *lv;
310 tlck = txLock(tid, ip, mp, tlckDATA);
311 llck = (struct linelock *) tlck->lock;
313 if (llck->index >= llck->maxcnt)
314 llck = txLinelock(llck);
315 lv = &llck->lv[llck->index];
318 * Linelock slot size is twice the size of directory table
319 * slot size. 512 entries per page.
321 lv->offset = ((index - 2) & 511) >> 1;
322 lv->length = 1;
323 llck->index++;
327 * add_index()
329 * Adds an entry to the directory index table. This is used to provide
330 * each directory entry with a persistent index in which to resume
331 * directory traversals
333 static u32 add_index(tid_t tid, struct inode *ip, s64 bn, int slot)
335 struct super_block *sb = ip->i_sb;
336 struct jfs_sb_info *sbi = JFS_SBI(sb);
337 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
338 u64 blkno;
339 struct dir_table_slot *dirtab_slot;
340 u32 index;
341 struct linelock *llck;
342 struct lv *lv;
343 struct metapage *mp;
344 s64 offset;
345 uint page_offset;
346 struct tlock *tlck;
347 s64 xaddr;
349 ASSERT(DO_INDEX(ip));
351 if (jfs_ip->next_index < 2) {
352 jfs_warn("add_index: next_index = %d. Resetting!",
353 jfs_ip->next_index);
354 jfs_ip->next_index = 2;
357 index = jfs_ip->next_index++;
359 if (index <= MAX_INLINE_DIRTABLE_ENTRY) {
361 * i_size reflects size of index table, or 8 bytes per entry.
363 ip->i_size = (loff_t) (index - 1) << 3;
366 * dir table fits inline within inode
368 dirtab_slot = &jfs_ip->i_dirtable[index-2];
369 dirtab_slot->flag = DIR_INDEX_VALID;
370 dirtab_slot->slot = slot;
371 DTSaddress(dirtab_slot, bn);
373 set_cflag(COMMIT_Dirtable, ip);
375 return index;
377 if (index == (MAX_INLINE_DIRTABLE_ENTRY + 1)) {
378 struct dir_table_slot temp_table[12];
381 * It's time to move the inline table to an external
382 * page and begin to build the xtree
384 if (DQUOT_ALLOC_BLOCK(ip, sbi->nbperpage))
385 goto clean_up;
386 if (dbAlloc(ip, 0, sbi->nbperpage, &xaddr)) {
387 DQUOT_FREE_BLOCK(ip, sbi->nbperpage);
388 goto clean_up;
392 * Save the table, we're going to overwrite it with the
393 * xtree root
395 memcpy(temp_table, &jfs_ip->i_dirtable, sizeof(temp_table));
398 * Initialize empty x-tree
400 xtInitRoot(tid, ip);
403 * Add the first block to the xtree
405 if (xtInsert(tid, ip, 0, 0, sbi->nbperpage, &xaddr, 0)) {
406 /* This really shouldn't fail */
407 jfs_warn("add_index: xtInsert failed!");
408 memcpy(&jfs_ip->i_dirtable, temp_table,
409 sizeof (temp_table));
410 dbFree(ip, xaddr, sbi->nbperpage);
411 DQUOT_FREE_BLOCK(ip, sbi->nbperpage);
412 goto clean_up;
414 ip->i_size = PSIZE;
416 if ((mp = get_index_page(ip, 0)) == 0) {
417 jfs_err("add_index: get_metapage failed!");
418 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
419 memcpy(&jfs_ip->i_dirtable, temp_table,
420 sizeof (temp_table));
421 goto clean_up;
423 tlck = txLock(tid, ip, mp, tlckDATA);
424 llck = (struct linelock *) & tlck->lock;
425 ASSERT(llck->index == 0);
426 lv = &llck->lv[0];
428 lv->offset = 0;
429 lv->length = 6; /* tlckDATA slot size is 16 bytes */
430 llck->index++;
432 memcpy(mp->data, temp_table, sizeof(temp_table));
434 mark_metapage_dirty(mp);
435 release_metapage(mp);
438 * Logging is now directed by xtree tlocks
440 clear_cflag(COMMIT_Dirtable, ip);
443 offset = (index - 2) * sizeof(struct dir_table_slot);
444 page_offset = offset & (PSIZE - 1);
445 blkno = ((offset + 1) >> L2PSIZE) << sbi->l2nbperpage;
446 if (page_offset == 0) {
448 * This will be the beginning of a new page
450 xaddr = 0;
451 if (xtInsert(tid, ip, 0, blkno, sbi->nbperpage, &xaddr, 0)) {
452 jfs_warn("add_index: xtInsert failed!");
453 goto clean_up;
455 ip->i_size += PSIZE;
457 if ((mp = get_index_page(ip, blkno)))
458 memset(mp->data, 0, PSIZE); /* Just looks better */
459 else
460 xtTruncate(tid, ip, offset, COMMIT_PWMAP);
461 } else
462 mp = read_index_page(ip, blkno);
464 if (mp == 0) {
465 jfs_err("add_index: get/read_metapage failed!");
466 goto clean_up;
469 lock_index(tid, ip, mp, index);
471 dirtab_slot =
472 (struct dir_table_slot *) ((char *) mp->data + page_offset);
473 dirtab_slot->flag = DIR_INDEX_VALID;
474 dirtab_slot->slot = slot;
475 DTSaddress(dirtab_slot, bn);
477 mark_metapage_dirty(mp);
478 release_metapage(mp);
480 return index;
482 clean_up:
484 jfs_ip->next_index--;
486 return 0;
490 * free_index()
492 * Marks an entry to the directory index table as free.
494 static void free_index(tid_t tid, struct inode *ip, u32 index, u32 next)
496 struct dir_table_slot *dirtab_slot;
497 s64 lblock;
498 struct metapage *mp = NULL;
500 dirtab_slot = find_index(ip, index, &mp, &lblock);
502 if (dirtab_slot == 0)
503 return;
505 dirtab_slot->flag = DIR_INDEX_FREE;
506 dirtab_slot->slot = dirtab_slot->addr1 = 0;
507 dirtab_slot->addr2 = cpu_to_le32(next);
509 if (mp) {
510 lock_index(tid, ip, mp, index);
511 mark_metapage_dirty(mp);
512 release_metapage(mp);
513 } else
514 set_cflag(COMMIT_Dirtable, ip);
518 * modify_index()
520 * Changes an entry in the directory index table
522 static void modify_index(tid_t tid, struct inode *ip, u32 index, s64 bn,
523 int slot, struct metapage ** mp, s64 *lblock)
525 struct dir_table_slot *dirtab_slot;
527 dirtab_slot = find_index(ip, index, mp, lblock);
529 if (dirtab_slot == 0)
530 return;
532 DTSaddress(dirtab_slot, bn);
533 dirtab_slot->slot = slot;
535 if (*mp) {
536 lock_index(tid, ip, *mp, index);
537 mark_metapage_dirty(*mp);
538 } else
539 set_cflag(COMMIT_Dirtable, ip);
543 * read_index()
545 * reads a directory table slot
547 static int read_index(struct inode *ip, u32 index,
548 struct dir_table_slot * dirtab_slot)
550 s64 lblock;
551 struct metapage *mp = NULL;
552 struct dir_table_slot *slot;
554 slot = find_index(ip, index, &mp, &lblock);
555 if (slot == 0) {
556 return -EIO;
559 memcpy(dirtab_slot, slot, sizeof(struct dir_table_slot));
561 if (mp)
562 release_metapage(mp);
564 return 0;
568 * dtSearch()
570 * function:
571 * Search for the entry with specified key
573 * parameter:
575 * return: 0 - search result on stack, leaf page pinned;
576 * errno - I/O error
578 int dtSearch(struct inode *ip, struct component_name * key, ino_t * data,
579 struct btstack * btstack, int flag)
581 int rc = 0;
582 int cmp = 1; /* init for empty page */
583 s64 bn;
584 struct metapage *mp;
585 dtpage_t *p;
586 s8 *stbl;
587 int base, index, lim;
588 struct btframe *btsp;
589 pxd_t *pxd;
590 int psize = 288; /* initial in-line directory */
591 ino_t inumber;
592 struct component_name ciKey;
593 struct super_block *sb = ip->i_sb;
595 ciKey.name = kmalloc((JFS_NAME_MAX + 1) * sizeof(wchar_t), GFP_NOFS);
596 if (ciKey.name == 0) {
597 rc = -ENOMEM;
598 goto dtSearch_Exit2;
602 /* uppercase search key for c-i directory */
603 UniStrcpy(ciKey.name, key->name);
604 ciKey.namlen = key->namlen;
606 /* only uppercase if case-insensitive support is on */
607 if ((JFS_SBI(sb)->mntflag & JFS_OS2) == JFS_OS2) {
608 ciToUpper(&ciKey);
610 BT_CLR(btstack); /* reset stack */
612 /* init level count for max pages to split */
613 btstack->nsplit = 1;
616 * search down tree from root:
618 * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
619 * internal page, child page Pi contains entry with k, Ki <= K < Kj.
621 * if entry with search key K is not found
622 * internal page search find the entry with largest key Ki
623 * less than K which point to the child page to search;
624 * leaf page search find the entry with smallest key Kj
625 * greater than K so that the returned index is the position of
626 * the entry to be shifted right for insertion of new entry.
627 * for empty tree, search key is greater than any key of the tree.
629 * by convention, root bn = 0.
631 for (bn = 0;;) {
632 /* get/pin the page to search */
633 DT_GETPAGE(ip, bn, mp, psize, p, rc);
634 if (rc)
635 goto dtSearch_Exit1;
637 /* get sorted entry table of the page */
638 stbl = DT_GETSTBL(p);
641 * binary search with search key K on the current page.
643 for (base = 0, lim = p->header.nextindex; lim; lim >>= 1) {
644 index = base + (lim >> 1);
646 if (p->header.flag & BT_LEAF) {
647 /* uppercase leaf name to compare */
648 cmp =
649 ciCompare(&ciKey, p, stbl[index],
650 JFS_SBI(sb)->mntflag);
651 } else {
652 /* router key is in uppercase */
654 cmp = dtCompare(&ciKey, p, stbl[index]);
658 if (cmp == 0) {
660 * search hit
662 /* search hit - leaf page:
663 * return the entry found
665 if (p->header.flag & BT_LEAF) {
666 inumber = le32_to_cpu(
667 ((struct ldtentry *) & p->slot[stbl[index]])->inumber);
670 * search for JFS_LOOKUP
672 if (flag == JFS_LOOKUP) {
673 *data = inumber;
674 rc = 0;
675 goto out;
679 * search for JFS_CREATE
681 if (flag == JFS_CREATE) {
682 *data = inumber;
683 rc = -EEXIST;
684 goto out;
688 * search for JFS_REMOVE or JFS_RENAME
690 if ((flag == JFS_REMOVE ||
691 flag == JFS_RENAME) &&
692 *data != inumber) {
693 rc = -ESTALE;
694 goto out;
698 * JFS_REMOVE|JFS_FINDDIR|JFS_RENAME
700 /* save search result */
701 *data = inumber;
702 btsp = btstack->top;
703 btsp->bn = bn;
704 btsp->index = index;
705 btsp->mp = mp;
707 rc = 0;
708 goto dtSearch_Exit1;
711 /* search hit - internal page:
712 * descend/search its child page
714 goto getChild;
717 if (cmp > 0) {
718 base = index + 1;
719 --lim;
724 * search miss
726 * base is the smallest index with key (Kj) greater than
727 * search key (K) and may be zero or (maxindex + 1) index.
730 * search miss - leaf page
732 * return location of entry (base) where new entry with
733 * search key K is to be inserted.
735 if (p->header.flag & BT_LEAF) {
737 * search for JFS_LOOKUP, JFS_REMOVE, or JFS_RENAME
739 if (flag == JFS_LOOKUP || flag == JFS_REMOVE ||
740 flag == JFS_RENAME) {
741 rc = -ENOENT;
742 goto out;
746 * search for JFS_CREATE|JFS_FINDDIR:
748 * save search result
750 *data = 0;
751 btsp = btstack->top;
752 btsp->bn = bn;
753 btsp->index = base;
754 btsp->mp = mp;
756 rc = 0;
757 goto dtSearch_Exit1;
761 * search miss - internal page
763 * if base is non-zero, decrement base by one to get the parent
764 * entry of the child page to search.
766 index = base ? base - 1 : base;
769 * go down to child page
771 getChild:
772 /* update max. number of pages to split */
773 if (BT_STACK_FULL(btstack)) {
774 /* Something's corrupted, mark filesystem dirty so
775 * chkdsk will fix it.
777 jfs_error(sb, "stack overrun in dtSearch!");
778 BT_STACK_DUMP(btstack);
779 rc = -EIO;
780 goto out;
782 btstack->nsplit++;
784 /* push (bn, index) of the parent page/entry */
785 BT_PUSH(btstack, bn, index);
787 /* get the child page block number */
788 pxd = (pxd_t *) & p->slot[stbl[index]];
789 bn = addressPXD(pxd);
790 psize = lengthPXD(pxd) << JFS_SBI(ip->i_sb)->l2bsize;
792 /* unpin the parent page */
793 DT_PUTPAGE(mp);
796 out:
797 DT_PUTPAGE(mp);
799 dtSearch_Exit1:
801 kfree(ciKey.name);
803 dtSearch_Exit2:
805 return rc;
810 * dtInsert()
812 * function: insert an entry to directory tree
814 * parameter:
816 * return: 0 - success;
817 * errno - failure;
819 int dtInsert(tid_t tid, struct inode *ip,
820 struct component_name * name, ino_t * fsn, struct btstack * btstack)
822 int rc = 0;
823 struct metapage *mp; /* meta-page buffer */
824 dtpage_t *p; /* base B+-tree index page */
825 s64 bn;
826 int index;
827 struct dtsplit split; /* split information */
828 ddata_t data;
829 struct dt_lock *dtlck;
830 int n;
831 struct tlock *tlck;
832 struct lv *lv;
835 * retrieve search result
837 * dtSearch() returns (leaf page pinned, index at which to insert).
838 * n.b. dtSearch() may return index of (maxindex + 1) of
839 * the full page.
841 DT_GETSEARCH(ip, btstack->top, bn, mp, p, index);
844 * insert entry for new key
846 if (DO_INDEX(ip)) {
847 if (JFS_IP(ip)->next_index == DIREND) {
848 DT_PUTPAGE(mp);
849 return -EMLINK;
851 n = NDTLEAF(name->namlen);
852 data.leaf.tid = tid;
853 data.leaf.ip = ip;
854 } else {
855 n = NDTLEAF_LEGACY(name->namlen);
856 data.leaf.ip = NULL; /* signifies legacy directory format */
858 data.leaf.ino = *fsn;
861 * leaf page does not have enough room for new entry:
863 * extend/split the leaf page;
865 * dtSplitUp() will insert the entry and unpin the leaf page.
867 if (n > p->header.freecnt) {
868 split.mp = mp;
869 split.index = index;
870 split.nslot = n;
871 split.key = name;
872 split.data = &data;
873 rc = dtSplitUp(tid, ip, &split, btstack);
874 return rc;
878 * leaf page does have enough room for new entry:
880 * insert the new data entry into the leaf page;
882 BT_MARK_DIRTY(mp, ip);
884 * acquire a transaction lock on the leaf page
886 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
887 dtlck = (struct dt_lock *) & tlck->lock;
888 ASSERT(dtlck->index == 0);
889 lv = & dtlck->lv[0];
891 /* linelock header */
892 lv->offset = 0;
893 lv->length = 1;
894 dtlck->index++;
896 dtInsertEntry(p, index, name, &data, &dtlck);
898 /* linelock stbl of non-root leaf page */
899 if (!(p->header.flag & BT_ROOT)) {
900 if (dtlck->index >= dtlck->maxcnt)
901 dtlck = (struct dt_lock *) txLinelock(dtlck);
902 lv = & dtlck->lv[dtlck->index];
903 n = index >> L2DTSLOTSIZE;
904 lv->offset = p->header.stblindex + n;
905 lv->length =
906 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) - n + 1;
907 dtlck->index++;
910 /* unpin the leaf page */
911 DT_PUTPAGE(mp);
913 return 0;
918 * dtSplitUp()
920 * function: propagate insertion bottom up;
922 * parameter:
924 * return: 0 - success;
925 * errno - failure;
926 * leaf page unpinned;
928 static int dtSplitUp(tid_t tid,
929 struct inode *ip, struct dtsplit * split, struct btstack * btstack)
931 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
932 int rc = 0;
933 struct metapage *smp;
934 dtpage_t *sp; /* split page */
935 struct metapage *rmp;
936 dtpage_t *rp; /* new right page split from sp */
937 pxd_t rpxd; /* new right page extent descriptor */
938 struct metapage *lmp;
939 dtpage_t *lp; /* left child page */
940 int skip; /* index of entry of insertion */
941 struct btframe *parent; /* parent page entry on traverse stack */
942 s64 xaddr, nxaddr;
943 int xlen, xsize;
944 struct pxdlist pxdlist;
945 pxd_t *pxd;
946 struct component_name key = { 0, NULL };
947 ddata_t *data = split->data;
948 int n;
949 struct dt_lock *dtlck;
950 struct tlock *tlck;
951 struct lv *lv;
952 int quota_allocation = 0;
954 /* get split page */
955 smp = split->mp;
956 sp = DT_PAGE(ip, smp);
958 key.name = kmalloc((JFS_NAME_MAX + 2) * sizeof(wchar_t), GFP_NOFS);
959 if (key.name == 0) {
960 DT_PUTPAGE(smp);
961 rc = -ENOMEM;
962 goto dtSplitUp_Exit;
966 * split leaf page
968 * The split routines insert the new entry, and
969 * acquire txLock as appropriate.
972 * split root leaf page:
974 if (sp->header.flag & BT_ROOT) {
976 * allocate a single extent child page
978 xlen = 1;
979 n = sbi->bsize >> L2DTSLOTSIZE;
980 n -= (n + 31) >> L2DTSLOTSIZE; /* stbl size */
981 n -= DTROOTMAXSLOT - sp->header.freecnt; /* header + entries */
982 if (n <= split->nslot)
983 xlen++;
984 if ((rc = dbAlloc(ip, 0, (s64) xlen, &xaddr))) {
985 DT_PUTPAGE(smp);
986 goto freeKeyName;
989 pxdlist.maxnpxd = 1;
990 pxdlist.npxd = 0;
991 pxd = &pxdlist.pxd[0];
992 PXDaddress(pxd, xaddr);
993 PXDlength(pxd, xlen);
994 split->pxdlist = &pxdlist;
995 rc = dtSplitRoot(tid, ip, split, &rmp);
997 if (rc)
998 dbFree(ip, xaddr, xlen);
999 else
1000 DT_PUTPAGE(rmp);
1002 DT_PUTPAGE(smp);
1004 if (!DO_INDEX(ip))
1005 ip->i_size = xlen << sbi->l2bsize;
1007 goto freeKeyName;
1011 * extend first leaf page
1013 * extend the 1st extent if less than buffer page size
1014 * (dtExtendPage() reurns leaf page unpinned)
1016 pxd = &sp->header.self;
1017 xlen = lengthPXD(pxd);
1018 xsize = xlen << sbi->l2bsize;
1019 if (xsize < PSIZE) {
1020 xaddr = addressPXD(pxd);
1021 n = xsize >> L2DTSLOTSIZE;
1022 n -= (n + 31) >> L2DTSLOTSIZE; /* stbl size */
1023 if ((n + sp->header.freecnt) <= split->nslot)
1024 n = xlen + (xlen << 1);
1025 else
1026 n = xlen;
1028 /* Allocate blocks to quota. */
1029 if (DQUOT_ALLOC_BLOCK(ip, n)) {
1030 rc = -EDQUOT;
1031 goto extendOut;
1033 quota_allocation += n;
1035 if ((rc = dbReAlloc(sbi->ipbmap, xaddr, (s64) xlen,
1036 (s64) n, &nxaddr)))
1037 goto extendOut;
1039 pxdlist.maxnpxd = 1;
1040 pxdlist.npxd = 0;
1041 pxd = &pxdlist.pxd[0];
1042 PXDaddress(pxd, nxaddr)
1043 PXDlength(pxd, xlen + n);
1044 split->pxdlist = &pxdlist;
1045 if ((rc = dtExtendPage(tid, ip, split, btstack))) {
1046 nxaddr = addressPXD(pxd);
1047 if (xaddr != nxaddr) {
1048 /* free relocated extent */
1049 xlen = lengthPXD(pxd);
1050 dbFree(ip, nxaddr, (s64) xlen);
1051 } else {
1052 /* free extended delta */
1053 xlen = lengthPXD(pxd) - n;
1054 xaddr = addressPXD(pxd) + xlen;
1055 dbFree(ip, xaddr, (s64) n);
1057 } else if (!DO_INDEX(ip))
1058 ip->i_size = lengthPXD(pxd) << sbi->l2bsize;
1061 extendOut:
1062 DT_PUTPAGE(smp);
1063 goto freeKeyName;
1067 * split leaf page <sp> into <sp> and a new right page <rp>.
1069 * return <rp> pinned and its extent descriptor <rpxd>
1072 * allocate new directory page extent and
1073 * new index page(s) to cover page split(s)
1075 * allocation hint: ?
1077 n = btstack->nsplit;
1078 pxdlist.maxnpxd = pxdlist.npxd = 0;
1079 xlen = sbi->nbperpage;
1080 for (pxd = pxdlist.pxd; n > 0; n--, pxd++) {
1081 if ((rc = dbAlloc(ip, 0, (s64) xlen, &xaddr)) == 0) {
1082 PXDaddress(pxd, xaddr);
1083 PXDlength(pxd, xlen);
1084 pxdlist.maxnpxd++;
1085 continue;
1088 DT_PUTPAGE(smp);
1090 /* undo allocation */
1091 goto splitOut;
1094 split->pxdlist = &pxdlist;
1095 if ((rc = dtSplitPage(tid, ip, split, &rmp, &rp, &rpxd))) {
1096 DT_PUTPAGE(smp);
1098 /* undo allocation */
1099 goto splitOut;
1102 if (!DO_INDEX(ip))
1103 ip->i_size += PSIZE;
1106 * propagate up the router entry for the leaf page just split
1108 * insert a router entry for the new page into the parent page,
1109 * propagate the insert/split up the tree by walking back the stack
1110 * of (bn of parent page, index of child page entry in parent page)
1111 * that were traversed during the search for the page that split.
1113 * the propagation of insert/split up the tree stops if the root
1114 * splits or the page inserted into doesn't have to split to hold
1115 * the new entry.
1117 * the parent entry for the split page remains the same, and
1118 * a new entry is inserted at its right with the first key and
1119 * block number of the new right page.
1121 * There are a maximum of 4 pages pinned at any time:
1122 * two children, left parent and right parent (when the parent splits).
1123 * keep the child pages pinned while working on the parent.
1124 * make sure that all pins are released at exit.
1126 while ((parent = BT_POP(btstack)) != NULL) {
1127 /* parent page specified by stack frame <parent> */
1129 /* keep current child pages (<lp>, <rp>) pinned */
1130 lmp = smp;
1131 lp = sp;
1134 * insert router entry in parent for new right child page <rp>
1136 /* get the parent page <sp> */
1137 DT_GETPAGE(ip, parent->bn, smp, PSIZE, sp, rc);
1138 if (rc) {
1139 DT_PUTPAGE(lmp);
1140 DT_PUTPAGE(rmp);
1141 goto splitOut;
1145 * The new key entry goes ONE AFTER the index of parent entry,
1146 * because the split was to the right.
1148 skip = parent->index + 1;
1151 * compute the key for the router entry
1153 * key suffix compression:
1154 * for internal pages that have leaf pages as children,
1155 * retain only what's needed to distinguish between
1156 * the new entry and the entry on the page to its left.
1157 * If the keys compare equal, retain the entire key.
1159 * note that compression is performed only at computing
1160 * router key at the lowest internal level.
1161 * further compression of the key between pairs of higher
1162 * level internal pages loses too much information and
1163 * the search may fail.
1164 * (e.g., two adjacent leaf pages of {a, ..., x} {xx, ...,}
1165 * results in two adjacent parent entries (a)(xx).
1166 * if split occurs between these two entries, and
1167 * if compression is applied, the router key of parent entry
1168 * of right page (x) will divert search for x into right
1169 * subtree and miss x in the left subtree.)
1171 * the entire key must be retained for the next-to-leftmost
1172 * internal key at any level of the tree, or search may fail
1173 * (e.g., ?)
1175 switch (rp->header.flag & BT_TYPE) {
1176 case BT_LEAF:
1178 * compute the length of prefix for suffix compression
1179 * between last entry of left page and first entry
1180 * of right page
1182 if ((sp->header.flag & BT_ROOT && skip > 1) ||
1183 sp->header.prev != 0 || skip > 1) {
1184 /* compute uppercase router prefix key */
1185 rc = ciGetLeafPrefixKey(lp,
1186 lp->header.nextindex-1,
1187 rp, 0, &key,
1188 sbi->mntflag);
1189 if (rc) {
1190 DT_PUTPAGE(lmp);
1191 DT_PUTPAGE(rmp);
1192 DT_PUTPAGE(smp);
1193 goto splitOut;
1195 } else {
1196 /* next to leftmost entry of
1197 lowest internal level */
1199 /* compute uppercase router key */
1200 dtGetKey(rp, 0, &key, sbi->mntflag);
1201 key.name[key.namlen] = 0;
1203 if ((sbi->mntflag & JFS_OS2) == JFS_OS2)
1204 ciToUpper(&key);
1207 n = NDTINTERNAL(key.namlen);
1208 break;
1210 case BT_INTERNAL:
1211 dtGetKey(rp, 0, &key, sbi->mntflag);
1212 n = NDTINTERNAL(key.namlen);
1213 break;
1215 default:
1216 jfs_err("dtSplitUp(): UFO!");
1217 break;
1220 /* unpin left child page */
1221 DT_PUTPAGE(lmp);
1224 * compute the data for the router entry
1226 data->xd = rpxd; /* child page xd */
1229 * parent page is full - split the parent page
1231 if (n > sp->header.freecnt) {
1232 /* init for parent page split */
1233 split->mp = smp;
1234 split->index = skip; /* index at insert */
1235 split->nslot = n;
1236 split->key = &key;
1237 /* split->data = data; */
1239 /* unpin right child page */
1240 DT_PUTPAGE(rmp);
1242 /* The split routines insert the new entry,
1243 * acquire txLock as appropriate.
1244 * return <rp> pinned and its block number <rbn>.
1246 rc = (sp->header.flag & BT_ROOT) ?
1247 dtSplitRoot(tid, ip, split, &rmp) :
1248 dtSplitPage(tid, ip, split, &rmp, &rp, &rpxd);
1249 if (rc) {
1250 DT_PUTPAGE(smp);
1251 goto splitOut;
1254 /* smp and rmp are pinned */
1257 * parent page is not full - insert router entry in parent page
1259 else {
1260 BT_MARK_DIRTY(smp, ip);
1262 * acquire a transaction lock on the parent page
1264 tlck = txLock(tid, ip, smp, tlckDTREE | tlckENTRY);
1265 dtlck = (struct dt_lock *) & tlck->lock;
1266 ASSERT(dtlck->index == 0);
1267 lv = & dtlck->lv[0];
1269 /* linelock header */
1270 lv->offset = 0;
1271 lv->length = 1;
1272 dtlck->index++;
1274 /* linelock stbl of non-root parent page */
1275 if (!(sp->header.flag & BT_ROOT)) {
1276 lv++;
1277 n = skip >> L2DTSLOTSIZE;
1278 lv->offset = sp->header.stblindex + n;
1279 lv->length =
1280 ((sp->header.nextindex -
1281 1) >> L2DTSLOTSIZE) - n + 1;
1282 dtlck->index++;
1285 dtInsertEntry(sp, skip, &key, data, &dtlck);
1287 /* exit propagate up */
1288 break;
1292 /* unpin current split and its right page */
1293 DT_PUTPAGE(smp);
1294 DT_PUTPAGE(rmp);
1297 * free remaining extents allocated for split
1299 splitOut:
1300 n = pxdlist.npxd;
1301 pxd = &pxdlist.pxd[n];
1302 for (; n < pxdlist.maxnpxd; n++, pxd++)
1303 dbFree(ip, addressPXD(pxd), (s64) lengthPXD(pxd));
1305 freeKeyName:
1306 kfree(key.name);
1308 /* Rollback quota allocation */
1309 if (rc && quota_allocation)
1310 DQUOT_FREE_BLOCK(ip, quota_allocation);
1312 dtSplitUp_Exit:
1314 return rc;
1319 * dtSplitPage()
1321 * function: Split a non-root page of a btree.
1323 * parameter:
1325 * return: 0 - success;
1326 * errno - failure;
1327 * return split and new page pinned;
1329 static int dtSplitPage(tid_t tid, struct inode *ip, struct dtsplit * split,
1330 struct metapage ** rmpp, dtpage_t ** rpp, pxd_t * rpxdp)
1332 int rc = 0;
1333 struct metapage *smp;
1334 dtpage_t *sp;
1335 struct metapage *rmp;
1336 dtpage_t *rp; /* new right page allocated */
1337 s64 rbn; /* new right page block number */
1338 struct metapage *mp;
1339 dtpage_t *p;
1340 s64 nextbn;
1341 struct pxdlist *pxdlist;
1342 pxd_t *pxd;
1343 int skip, nextindex, half, left, nxt, off, si;
1344 struct ldtentry *ldtentry;
1345 struct idtentry *idtentry;
1346 u8 *stbl;
1347 struct dtslot *f;
1348 int fsi, stblsize;
1349 int n;
1350 struct dt_lock *sdtlck, *rdtlck;
1351 struct tlock *tlck;
1352 struct dt_lock *dtlck;
1353 struct lv *slv, *rlv, *lv;
1355 /* get split page */
1356 smp = split->mp;
1357 sp = DT_PAGE(ip, smp);
1360 * allocate the new right page for the split
1362 pxdlist = split->pxdlist;
1363 pxd = &pxdlist->pxd[pxdlist->npxd];
1364 pxdlist->npxd++;
1365 rbn = addressPXD(pxd);
1366 rmp = get_metapage(ip, rbn, PSIZE, 1);
1367 if (rmp == NULL)
1368 return -EIO;
1370 /* Allocate blocks to quota. */
1371 if (DQUOT_ALLOC_BLOCK(ip, lengthPXD(pxd))) {
1372 release_metapage(rmp);
1373 return -EDQUOT;
1376 jfs_info("dtSplitPage: ip:0x%p smp:0x%p rmp:0x%p", ip, smp, rmp);
1378 BT_MARK_DIRTY(rmp, ip);
1380 * acquire a transaction lock on the new right page
1382 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckNEW);
1383 rdtlck = (struct dt_lock *) & tlck->lock;
1385 rp = (dtpage_t *) rmp->data;
1386 *rpp = rp;
1387 rp->header.self = *pxd;
1389 BT_MARK_DIRTY(smp, ip);
1391 * acquire a transaction lock on the split page
1393 * action:
1395 tlck = txLock(tid, ip, smp, tlckDTREE | tlckENTRY);
1396 sdtlck = (struct dt_lock *) & tlck->lock;
1398 /* linelock header of split page */
1399 ASSERT(sdtlck->index == 0);
1400 slv = & sdtlck->lv[0];
1401 slv->offset = 0;
1402 slv->length = 1;
1403 sdtlck->index++;
1406 * initialize/update sibling pointers between sp and rp
1408 nextbn = le64_to_cpu(sp->header.next);
1409 rp->header.next = cpu_to_le64(nextbn);
1410 rp->header.prev = cpu_to_le64(addressPXD(&sp->header.self));
1411 sp->header.next = cpu_to_le64(rbn);
1414 * initialize new right page
1416 rp->header.flag = sp->header.flag;
1418 /* compute sorted entry table at start of extent data area */
1419 rp->header.nextindex = 0;
1420 rp->header.stblindex = 1;
1422 n = PSIZE >> L2DTSLOTSIZE;
1423 rp->header.maxslot = n;
1424 stblsize = (n + 31) >> L2DTSLOTSIZE; /* in unit of slot */
1426 /* init freelist */
1427 fsi = rp->header.stblindex + stblsize;
1428 rp->header.freelist = fsi;
1429 rp->header.freecnt = rp->header.maxslot - fsi;
1432 * sequential append at tail: append without split
1434 * If splitting the last page on a level because of appending
1435 * a entry to it (skip is maxentry), it's likely that the access is
1436 * sequential. Adding an empty page on the side of the level is less
1437 * work and can push the fill factor much higher than normal.
1438 * If we're wrong it's no big deal, we'll just do the split the right
1439 * way next time.
1440 * (It may look like it's equally easy to do a similar hack for
1441 * reverse sorted data, that is, split the tree left,
1442 * but it's not. Be my guest.)
1444 if (nextbn == 0 && split->index == sp->header.nextindex) {
1445 /* linelock header + stbl (first slot) of new page */
1446 rlv = & rdtlck->lv[rdtlck->index];
1447 rlv->offset = 0;
1448 rlv->length = 2;
1449 rdtlck->index++;
1452 * initialize freelist of new right page
1454 f = &rp->slot[fsi];
1455 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1456 f->next = fsi;
1457 f->next = -1;
1459 /* insert entry at the first entry of the new right page */
1460 dtInsertEntry(rp, 0, split->key, split->data, &rdtlck);
1462 goto out;
1466 * non-sequential insert (at possibly middle page)
1470 * update prev pointer of previous right sibling page;
1472 if (nextbn != 0) {
1473 DT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
1474 if (rc) {
1475 discard_metapage(rmp);
1476 return rc;
1479 BT_MARK_DIRTY(mp, ip);
1481 * acquire a transaction lock on the next page
1483 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
1484 jfs_info("dtSplitPage: tlck = 0x%p, ip = 0x%p, mp=0x%p",
1485 tlck, ip, mp);
1486 dtlck = (struct dt_lock *) & tlck->lock;
1488 /* linelock header of previous right sibling page */
1489 lv = & dtlck->lv[dtlck->index];
1490 lv->offset = 0;
1491 lv->length = 1;
1492 dtlck->index++;
1494 p->header.prev = cpu_to_le64(rbn);
1496 DT_PUTPAGE(mp);
1500 * split the data between the split and right pages.
1502 skip = split->index;
1503 half = (PSIZE >> L2DTSLOTSIZE) >> 1; /* swag */
1504 left = 0;
1507 * compute fill factor for split pages
1509 * <nxt> traces the next entry to move to rp
1510 * <off> traces the next entry to stay in sp
1512 stbl = (u8 *) & sp->slot[sp->header.stblindex];
1513 nextindex = sp->header.nextindex;
1514 for (nxt = off = 0; nxt < nextindex; ++off) {
1515 if (off == skip)
1516 /* check for fill factor with new entry size */
1517 n = split->nslot;
1518 else {
1519 si = stbl[nxt];
1520 switch (sp->header.flag & BT_TYPE) {
1521 case BT_LEAF:
1522 ldtentry = (struct ldtentry *) & sp->slot[si];
1523 if (DO_INDEX(ip))
1524 n = NDTLEAF(ldtentry->namlen);
1525 else
1526 n = NDTLEAF_LEGACY(ldtentry->
1527 namlen);
1528 break;
1530 case BT_INTERNAL:
1531 idtentry = (struct idtentry *) & sp->slot[si];
1532 n = NDTINTERNAL(idtentry->namlen);
1533 break;
1535 default:
1536 break;
1539 ++nxt; /* advance to next entry to move in sp */
1542 left += n;
1543 if (left >= half)
1544 break;
1547 /* <nxt> poins to the 1st entry to move */
1550 * move entries to right page
1552 * dtMoveEntry() initializes rp and reserves entry for insertion
1554 * split page moved out entries are linelocked;
1555 * new/right page moved in entries are linelocked;
1557 /* linelock header + stbl of new right page */
1558 rlv = & rdtlck->lv[rdtlck->index];
1559 rlv->offset = 0;
1560 rlv->length = 5;
1561 rdtlck->index++;
1563 dtMoveEntry(sp, nxt, rp, &sdtlck, &rdtlck, DO_INDEX(ip));
1565 sp->header.nextindex = nxt;
1568 * finalize freelist of new right page
1570 fsi = rp->header.freelist;
1571 f = &rp->slot[fsi];
1572 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1573 f->next = fsi;
1574 f->next = -1;
1577 * Update directory index table for entries now in right page
1579 if ((rp->header.flag & BT_LEAF) && DO_INDEX(ip)) {
1580 s64 lblock;
1582 mp = NULL;
1583 stbl = DT_GETSTBL(rp);
1584 for (n = 0; n < rp->header.nextindex; n++) {
1585 ldtentry = (struct ldtentry *) & rp->slot[stbl[n]];
1586 modify_index(tid, ip, le32_to_cpu(ldtentry->index),
1587 rbn, n, &mp, &lblock);
1589 if (mp)
1590 release_metapage(mp);
1594 * the skipped index was on the left page,
1596 if (skip <= off) {
1597 /* insert the new entry in the split page */
1598 dtInsertEntry(sp, skip, split->key, split->data, &sdtlck);
1600 /* linelock stbl of split page */
1601 if (sdtlck->index >= sdtlck->maxcnt)
1602 sdtlck = (struct dt_lock *) txLinelock(sdtlck);
1603 slv = & sdtlck->lv[sdtlck->index];
1604 n = skip >> L2DTSLOTSIZE;
1605 slv->offset = sp->header.stblindex + n;
1606 slv->length =
1607 ((sp->header.nextindex - 1) >> L2DTSLOTSIZE) - n + 1;
1608 sdtlck->index++;
1611 * the skipped index was on the right page,
1613 else {
1614 /* adjust the skip index to reflect the new position */
1615 skip -= nxt;
1617 /* insert the new entry in the right page */
1618 dtInsertEntry(rp, skip, split->key, split->data, &rdtlck);
1621 out:
1622 *rmpp = rmp;
1623 *rpxdp = *pxd;
1625 return rc;
1630 * dtExtendPage()
1632 * function: extend 1st/only directory leaf page
1634 * parameter:
1636 * return: 0 - success;
1637 * errno - failure;
1638 * return extended page pinned;
1640 static int dtExtendPage(tid_t tid,
1641 struct inode *ip, struct dtsplit * split, struct btstack * btstack)
1643 struct super_block *sb = ip->i_sb;
1644 int rc;
1645 struct metapage *smp, *pmp, *mp;
1646 dtpage_t *sp, *pp;
1647 struct pxdlist *pxdlist;
1648 pxd_t *pxd, *tpxd;
1649 int xlen, xsize;
1650 int newstblindex, newstblsize;
1651 int oldstblindex, oldstblsize;
1652 int fsi, last;
1653 struct dtslot *f;
1654 struct btframe *parent;
1655 int n;
1656 struct dt_lock *dtlck;
1657 s64 xaddr, txaddr;
1658 struct tlock *tlck;
1659 struct pxd_lock *pxdlock;
1660 struct lv *lv;
1661 uint type;
1662 struct ldtentry *ldtentry;
1663 u8 *stbl;
1665 /* get page to extend */
1666 smp = split->mp;
1667 sp = DT_PAGE(ip, smp);
1669 /* get parent/root page */
1670 parent = BT_POP(btstack);
1671 DT_GETPAGE(ip, parent->bn, pmp, PSIZE, pp, rc);
1672 if (rc)
1673 return (rc);
1676 * extend the extent
1678 pxdlist = split->pxdlist;
1679 pxd = &pxdlist->pxd[pxdlist->npxd];
1680 pxdlist->npxd++;
1682 xaddr = addressPXD(pxd);
1683 tpxd = &sp->header.self;
1684 txaddr = addressPXD(tpxd);
1685 /* in-place extension */
1686 if (xaddr == txaddr) {
1687 type = tlckEXTEND;
1689 /* relocation */
1690 else {
1691 type = tlckNEW;
1693 /* save moved extent descriptor for later free */
1694 tlck = txMaplock(tid, ip, tlckDTREE | tlckRELOCATE);
1695 pxdlock = (struct pxd_lock *) & tlck->lock;
1696 pxdlock->flag = mlckFREEPXD;
1697 pxdlock->pxd = sp->header.self;
1698 pxdlock->index = 1;
1701 * Update directory index table to reflect new page address
1703 if (DO_INDEX(ip)) {
1704 s64 lblock;
1706 mp = NULL;
1707 stbl = DT_GETSTBL(sp);
1708 for (n = 0; n < sp->header.nextindex; n++) {
1709 ldtentry =
1710 (struct ldtentry *) & sp->slot[stbl[n]];
1711 modify_index(tid, ip,
1712 le32_to_cpu(ldtentry->index),
1713 xaddr, n, &mp, &lblock);
1715 if (mp)
1716 release_metapage(mp);
1721 * extend the page
1723 sp->header.self = *pxd;
1725 jfs_info("dtExtendPage: ip:0x%p smp:0x%p sp:0x%p", ip, smp, sp);
1727 BT_MARK_DIRTY(smp, ip);
1729 * acquire a transaction lock on the extended/leaf page
1731 tlck = txLock(tid, ip, smp, tlckDTREE | type);
1732 dtlck = (struct dt_lock *) & tlck->lock;
1733 lv = & dtlck->lv[0];
1735 /* update buffer extent descriptor of extended page */
1736 xlen = lengthPXD(pxd);
1737 xsize = xlen << JFS_SBI(sb)->l2bsize;
1740 * copy old stbl to new stbl at start of extended area
1742 oldstblindex = sp->header.stblindex;
1743 oldstblsize = (sp->header.maxslot + 31) >> L2DTSLOTSIZE;
1744 newstblindex = sp->header.maxslot;
1745 n = xsize >> L2DTSLOTSIZE;
1746 newstblsize = (n + 31) >> L2DTSLOTSIZE;
1747 memcpy(&sp->slot[newstblindex], &sp->slot[oldstblindex],
1748 sp->header.nextindex);
1751 * in-line extension: linelock old area of extended page
1753 if (type == tlckEXTEND) {
1754 /* linelock header */
1755 lv->offset = 0;
1756 lv->length = 1;
1757 dtlck->index++;
1758 lv++;
1760 /* linelock new stbl of extended page */
1761 lv->offset = newstblindex;
1762 lv->length = newstblsize;
1765 * relocation: linelock whole relocated area
1767 else {
1768 lv->offset = 0;
1769 lv->length = sp->header.maxslot + newstblsize;
1772 dtlck->index++;
1774 sp->header.maxslot = n;
1775 sp->header.stblindex = newstblindex;
1776 /* sp->header.nextindex remains the same */
1779 * add old stbl region at head of freelist
1781 fsi = oldstblindex;
1782 f = &sp->slot[fsi];
1783 last = sp->header.freelist;
1784 for (n = 0; n < oldstblsize; n++, fsi++, f++) {
1785 f->next = last;
1786 last = fsi;
1788 sp->header.freelist = last;
1789 sp->header.freecnt += oldstblsize;
1792 * append free region of newly extended area at tail of freelist
1794 /* init free region of newly extended area */
1795 fsi = n = newstblindex + newstblsize;
1796 f = &sp->slot[fsi];
1797 for (fsi++; fsi < sp->header.maxslot; f++, fsi++)
1798 f->next = fsi;
1799 f->next = -1;
1801 /* append new free region at tail of old freelist */
1802 fsi = sp->header.freelist;
1803 if (fsi == -1)
1804 sp->header.freelist = n;
1805 else {
1806 do {
1807 f = &sp->slot[fsi];
1808 fsi = f->next;
1809 } while (fsi != -1);
1811 f->next = n;
1814 sp->header.freecnt += sp->header.maxslot - n;
1817 * insert the new entry
1819 dtInsertEntry(sp, split->index, split->key, split->data, &dtlck);
1821 BT_MARK_DIRTY(pmp, ip);
1823 * linelock any freeslots residing in old extent
1825 if (type == tlckEXTEND) {
1826 n = sp->header.maxslot >> 2;
1827 if (sp->header.freelist < n)
1828 dtLinelockFreelist(sp, n, &dtlck);
1832 * update parent entry on the parent/root page
1835 * acquire a transaction lock on the parent/root page
1837 tlck = txLock(tid, ip, pmp, tlckDTREE | tlckENTRY);
1838 dtlck = (struct dt_lock *) & tlck->lock;
1839 lv = & dtlck->lv[dtlck->index];
1841 /* linelock parent entry - 1st slot */
1842 lv->offset = 1;
1843 lv->length = 1;
1844 dtlck->index++;
1846 /* update the parent pxd for page extension */
1847 tpxd = (pxd_t *) & pp->slot[1];
1848 *tpxd = *pxd;
1850 DT_PUTPAGE(pmp);
1851 return 0;
1856 * dtSplitRoot()
1858 * function:
1859 * split the full root page into
1860 * original/root/split page and new right page
1861 * i.e., root remains fixed in tree anchor (inode) and
1862 * the root is copied to a single new right child page
1863 * since root page << non-root page, and
1864 * the split root page contains a single entry for the
1865 * new right child page.
1867 * parameter:
1869 * return: 0 - success;
1870 * errno - failure;
1871 * return new page pinned;
1873 static int dtSplitRoot(tid_t tid,
1874 struct inode *ip, struct dtsplit * split, struct metapage ** rmpp)
1876 struct super_block *sb = ip->i_sb;
1877 struct metapage *smp;
1878 dtroot_t *sp;
1879 struct metapage *rmp;
1880 dtpage_t *rp;
1881 s64 rbn;
1882 int xlen;
1883 int xsize;
1884 struct dtslot *f;
1885 s8 *stbl;
1886 int fsi, stblsize, n;
1887 struct idtentry *s;
1888 pxd_t *ppxd;
1889 struct pxdlist *pxdlist;
1890 pxd_t *pxd;
1891 struct dt_lock *dtlck;
1892 struct tlock *tlck;
1893 struct lv *lv;
1895 /* get split root page */
1896 smp = split->mp;
1897 sp = &JFS_IP(ip)->i_dtroot;
1900 * allocate/initialize a single (right) child page
1902 * N.B. at first split, a one (or two) block to fit new entry
1903 * is allocated; at subsequent split, a full page is allocated;
1905 pxdlist = split->pxdlist;
1906 pxd = &pxdlist->pxd[pxdlist->npxd];
1907 pxdlist->npxd++;
1908 rbn = addressPXD(pxd);
1909 xlen = lengthPXD(pxd);
1910 xsize = xlen << JFS_SBI(sb)->l2bsize;
1911 rmp = get_metapage(ip, rbn, xsize, 1);
1912 if (!rmp)
1913 return -EIO;
1915 rp = rmp->data;
1917 /* Allocate blocks to quota. */
1918 if (DQUOT_ALLOC_BLOCK(ip, lengthPXD(pxd))) {
1919 release_metapage(rmp);
1920 return -EDQUOT;
1923 BT_MARK_DIRTY(rmp, ip);
1925 * acquire a transaction lock on the new right page
1927 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckNEW);
1928 dtlck = (struct dt_lock *) & tlck->lock;
1930 rp->header.flag =
1931 (sp->header.flag & BT_LEAF) ? BT_LEAF : BT_INTERNAL;
1932 rp->header.self = *pxd;
1934 /* initialize sibling pointers */
1935 rp->header.next = 0;
1936 rp->header.prev = 0;
1939 * move in-line root page into new right page extent
1941 /* linelock header + copied entries + new stbl (1st slot) in new page */
1942 ASSERT(dtlck->index == 0);
1943 lv = & dtlck->lv[0];
1944 lv->offset = 0;
1945 lv->length = 10; /* 1 + 8 + 1 */
1946 dtlck->index++;
1948 n = xsize >> L2DTSLOTSIZE;
1949 rp->header.maxslot = n;
1950 stblsize = (n + 31) >> L2DTSLOTSIZE;
1952 /* copy old stbl to new stbl at start of extended area */
1953 rp->header.stblindex = DTROOTMAXSLOT;
1954 stbl = (s8 *) & rp->slot[DTROOTMAXSLOT];
1955 memcpy(stbl, sp->header.stbl, sp->header.nextindex);
1956 rp->header.nextindex = sp->header.nextindex;
1958 /* copy old data area to start of new data area */
1959 memcpy(&rp->slot[1], &sp->slot[1], IDATASIZE);
1962 * append free region of newly extended area at tail of freelist
1964 /* init free region of newly extended area */
1965 fsi = n = DTROOTMAXSLOT + stblsize;
1966 f = &rp->slot[fsi];
1967 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1968 f->next = fsi;
1969 f->next = -1;
1971 /* append new free region at tail of old freelist */
1972 fsi = sp->header.freelist;
1973 if (fsi == -1)
1974 rp->header.freelist = n;
1975 else {
1976 rp->header.freelist = fsi;
1978 do {
1979 f = &rp->slot[fsi];
1980 fsi = f->next;
1981 } while (fsi != -1);
1983 f->next = n;
1986 rp->header.freecnt = sp->header.freecnt + rp->header.maxslot - n;
1989 * Update directory index table for entries now in right page
1991 if ((rp->header.flag & BT_LEAF) && DO_INDEX(ip)) {
1992 s64 lblock;
1993 struct metapage *mp = NULL;
1994 struct ldtentry *ldtentry;
1996 stbl = DT_GETSTBL(rp);
1997 for (n = 0; n < rp->header.nextindex; n++) {
1998 ldtentry = (struct ldtentry *) & rp->slot[stbl[n]];
1999 modify_index(tid, ip, le32_to_cpu(ldtentry->index),
2000 rbn, n, &mp, &lblock);
2002 if (mp)
2003 release_metapage(mp);
2006 * insert the new entry into the new right/child page
2007 * (skip index in the new right page will not change)
2009 dtInsertEntry(rp, split->index, split->key, split->data, &dtlck);
2012 * reset parent/root page
2014 * set the 1st entry offset to 0, which force the left-most key
2015 * at any level of the tree to be less than any search key.
2017 * The btree comparison code guarantees that the left-most key on any
2018 * level of the tree is never used, so it doesn't need to be filled in.
2020 BT_MARK_DIRTY(smp, ip);
2022 * acquire a transaction lock on the root page (in-memory inode)
2024 tlck = txLock(tid, ip, smp, tlckDTREE | tlckNEW | tlckBTROOT);
2025 dtlck = (struct dt_lock *) & tlck->lock;
2027 /* linelock root */
2028 ASSERT(dtlck->index == 0);
2029 lv = & dtlck->lv[0];
2030 lv->offset = 0;
2031 lv->length = DTROOTMAXSLOT;
2032 dtlck->index++;
2034 /* update page header of root */
2035 if (sp->header.flag & BT_LEAF) {
2036 sp->header.flag &= ~BT_LEAF;
2037 sp->header.flag |= BT_INTERNAL;
2040 /* init the first entry */
2041 s = (struct idtentry *) & sp->slot[DTENTRYSTART];
2042 ppxd = (pxd_t *) s;
2043 *ppxd = *pxd;
2044 s->next = -1;
2045 s->namlen = 0;
2047 stbl = sp->header.stbl;
2048 stbl[0] = DTENTRYSTART;
2049 sp->header.nextindex = 1;
2051 /* init freelist */
2052 fsi = DTENTRYSTART + 1;
2053 f = &sp->slot[fsi];
2055 /* init free region of remaining area */
2056 for (fsi++; fsi < DTROOTMAXSLOT; f++, fsi++)
2057 f->next = fsi;
2058 f->next = -1;
2060 sp->header.freelist = DTENTRYSTART + 1;
2061 sp->header.freecnt = DTROOTMAXSLOT - (DTENTRYSTART + 1);
2063 *rmpp = rmp;
2065 return 0;
2070 * dtDelete()
2072 * function: delete the entry(s) referenced by a key.
2074 * parameter:
2076 * return:
2078 int dtDelete(tid_t tid,
2079 struct inode *ip, struct component_name * key, ino_t * ino, int flag)
2081 int rc = 0;
2082 s64 bn;
2083 struct metapage *mp, *imp;
2084 dtpage_t *p;
2085 int index;
2086 struct btstack btstack;
2087 struct dt_lock *dtlck;
2088 struct tlock *tlck;
2089 struct lv *lv;
2090 int i;
2091 struct ldtentry *ldtentry;
2092 u8 *stbl;
2093 u32 table_index, next_index;
2094 struct metapage *nmp;
2095 dtpage_t *np;
2098 * search for the entry to delete:
2100 * dtSearch() returns (leaf page pinned, index at which to delete).
2102 if ((rc = dtSearch(ip, key, ino, &btstack, flag)))
2103 return rc;
2105 /* retrieve search result */
2106 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
2109 * We need to find put the index of the next entry into the
2110 * directory index table in order to resume a readdir from this
2111 * entry.
2113 if (DO_INDEX(ip)) {
2114 stbl = DT_GETSTBL(p);
2115 ldtentry = (struct ldtentry *) & p->slot[stbl[index]];
2116 table_index = le32_to_cpu(ldtentry->index);
2117 if (index == (p->header.nextindex - 1)) {
2119 * Last entry in this leaf page
2121 if ((p->header.flag & BT_ROOT)
2122 || (p->header.next == 0))
2123 next_index = -1;
2124 else {
2125 /* Read next leaf page */
2126 DT_GETPAGE(ip, le64_to_cpu(p->header.next),
2127 nmp, PSIZE, np, rc);
2128 if (rc)
2129 next_index = -1;
2130 else {
2131 stbl = DT_GETSTBL(np);
2132 ldtentry =
2133 (struct ldtentry *) & np->
2134 slot[stbl[0]];
2135 next_index =
2136 le32_to_cpu(ldtentry->index);
2137 DT_PUTPAGE(nmp);
2140 } else {
2141 ldtentry =
2142 (struct ldtentry *) & p->slot[stbl[index + 1]];
2143 next_index = le32_to_cpu(ldtentry->index);
2145 free_index(tid, ip, table_index, next_index);
2148 * the leaf page becomes empty, delete the page
2150 if (p->header.nextindex == 1) {
2151 /* delete empty page */
2152 rc = dtDeleteUp(tid, ip, mp, p, &btstack);
2155 * the leaf page has other entries remaining:
2157 * delete the entry from the leaf page.
2159 else {
2160 BT_MARK_DIRTY(mp, ip);
2162 * acquire a transaction lock on the leaf page
2164 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
2165 dtlck = (struct dt_lock *) & tlck->lock;
2168 * Do not assume that dtlck->index will be zero. During a
2169 * rename within a directory, this transaction may have
2170 * modified this page already when adding the new entry.
2173 /* linelock header */
2174 if (dtlck->index >= dtlck->maxcnt)
2175 dtlck = (struct dt_lock *) txLinelock(dtlck);
2176 lv = & dtlck->lv[dtlck->index];
2177 lv->offset = 0;
2178 lv->length = 1;
2179 dtlck->index++;
2181 /* linelock stbl of non-root leaf page */
2182 if (!(p->header.flag & BT_ROOT)) {
2183 if (dtlck->index >= dtlck->maxcnt)
2184 dtlck = (struct dt_lock *) txLinelock(dtlck);
2185 lv = & dtlck->lv[dtlck->index];
2186 i = index >> L2DTSLOTSIZE;
2187 lv->offset = p->header.stblindex + i;
2188 lv->length =
2189 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) -
2190 i + 1;
2191 dtlck->index++;
2194 /* free the leaf entry */
2195 dtDeleteEntry(p, index, &dtlck);
2198 * Update directory index table for entries moved in stbl
2200 if (DO_INDEX(ip) && index < p->header.nextindex) {
2201 s64 lblock;
2203 imp = NULL;
2204 stbl = DT_GETSTBL(p);
2205 for (i = index; i < p->header.nextindex; i++) {
2206 ldtentry =
2207 (struct ldtentry *) & p->slot[stbl[i]];
2208 modify_index(tid, ip,
2209 le32_to_cpu(ldtentry->index),
2210 bn, i, &imp, &lblock);
2212 if (imp)
2213 release_metapage(imp);
2216 DT_PUTPAGE(mp);
2219 return rc;
2224 * dtDeleteUp()
2226 * function:
2227 * free empty pages as propagating deletion up the tree
2229 * parameter:
2231 * return:
2233 static int dtDeleteUp(tid_t tid, struct inode *ip,
2234 struct metapage * fmp, dtpage_t * fp, struct btstack * btstack)
2236 int rc = 0;
2237 struct metapage *mp;
2238 dtpage_t *p;
2239 int index, nextindex;
2240 int xlen;
2241 struct btframe *parent;
2242 struct dt_lock *dtlck;
2243 struct tlock *tlck;
2244 struct lv *lv;
2245 struct pxd_lock *pxdlock;
2246 int i;
2249 * keep the root leaf page which has become empty
2251 if (BT_IS_ROOT(fmp)) {
2253 * reset the root
2255 * dtInitRoot() acquires txlock on the root
2257 dtInitRoot(tid, ip, PARENT(ip));
2259 DT_PUTPAGE(fmp);
2261 return 0;
2265 * free the non-root leaf page
2268 * acquire a transaction lock on the page
2270 * write FREEXTENT|NOREDOPAGE log record
2271 * N.B. linelock is overlaid as freed extent descriptor, and
2272 * the buffer page is freed;
2274 tlck = txMaplock(tid, ip, tlckDTREE | tlckFREE);
2275 pxdlock = (struct pxd_lock *) & tlck->lock;
2276 pxdlock->flag = mlckFREEPXD;
2277 pxdlock->pxd = fp->header.self;
2278 pxdlock->index = 1;
2280 /* update sibling pointers */
2281 if ((rc = dtRelink(tid, ip, fp))) {
2282 BT_PUTPAGE(fmp);
2283 return rc;
2286 xlen = lengthPXD(&fp->header.self);
2288 /* Free quota allocation. */
2289 DQUOT_FREE_BLOCK(ip, xlen);
2291 /* free/invalidate its buffer page */
2292 discard_metapage(fmp);
2295 * propagate page deletion up the directory tree
2297 * If the delete from the parent page makes it empty,
2298 * continue all the way up the tree.
2299 * stop if the root page is reached (which is never deleted) or
2300 * if the entry deletion does not empty the page.
2302 while ((parent = BT_POP(btstack)) != NULL) {
2303 /* pin the parent page <sp> */
2304 DT_GETPAGE(ip, parent->bn, mp, PSIZE, p, rc);
2305 if (rc)
2306 return rc;
2309 * free the extent of the child page deleted
2311 index = parent->index;
2314 * delete the entry for the child page from parent
2316 nextindex = p->header.nextindex;
2319 * the parent has the single entry being deleted:
2321 * free the parent page which has become empty.
2323 if (nextindex == 1) {
2325 * keep the root internal page which has become empty
2327 if (p->header.flag & BT_ROOT) {
2329 * reset the root
2331 * dtInitRoot() acquires txlock on the root
2333 dtInitRoot(tid, ip, PARENT(ip));
2335 DT_PUTPAGE(mp);
2337 return 0;
2340 * free the parent page
2342 else {
2344 * acquire a transaction lock on the page
2346 * write FREEXTENT|NOREDOPAGE log record
2348 tlck =
2349 txMaplock(tid, ip,
2350 tlckDTREE | tlckFREE);
2351 pxdlock = (struct pxd_lock *) & tlck->lock;
2352 pxdlock->flag = mlckFREEPXD;
2353 pxdlock->pxd = p->header.self;
2354 pxdlock->index = 1;
2356 /* update sibling pointers */
2357 if ((rc = dtRelink(tid, ip, p))) {
2358 DT_PUTPAGE(mp);
2359 return rc;
2362 xlen = lengthPXD(&p->header.self);
2364 /* Free quota allocation */
2365 DQUOT_FREE_BLOCK(ip, xlen);
2367 /* free/invalidate its buffer page */
2368 discard_metapage(mp);
2370 /* propagate up */
2371 continue;
2376 * the parent has other entries remaining:
2378 * delete the router entry from the parent page.
2380 BT_MARK_DIRTY(mp, ip);
2382 * acquire a transaction lock on the page
2384 * action: router entry deletion
2386 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
2387 dtlck = (struct dt_lock *) & tlck->lock;
2389 /* linelock header */
2390 if (dtlck->index >= dtlck->maxcnt)
2391 dtlck = (struct dt_lock *) txLinelock(dtlck);
2392 lv = & dtlck->lv[dtlck->index];
2393 lv->offset = 0;
2394 lv->length = 1;
2395 dtlck->index++;
2397 /* linelock stbl of non-root leaf page */
2398 if (!(p->header.flag & BT_ROOT)) {
2399 if (dtlck->index < dtlck->maxcnt)
2400 lv++;
2401 else {
2402 dtlck = (struct dt_lock *) txLinelock(dtlck);
2403 lv = & dtlck->lv[0];
2405 i = index >> L2DTSLOTSIZE;
2406 lv->offset = p->header.stblindex + i;
2407 lv->length =
2408 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) -
2409 i + 1;
2410 dtlck->index++;
2413 /* free the router entry */
2414 dtDeleteEntry(p, index, &dtlck);
2416 /* reset key of new leftmost entry of level (for consistency) */
2417 if (index == 0 &&
2418 ((p->header.flag & BT_ROOT) || p->header.prev == 0))
2419 dtTruncateEntry(p, 0, &dtlck);
2421 /* unpin the parent page */
2422 DT_PUTPAGE(mp);
2424 /* exit propagation up */
2425 break;
2428 if (!DO_INDEX(ip))
2429 ip->i_size -= PSIZE;
2431 return 0;
2434 #ifdef _NOTYET
2436 * NAME: dtRelocate()
2438 * FUNCTION: relocate dtpage (internal or leaf) of directory;
2439 * This function is mainly used by defragfs utility.
2441 int dtRelocate(tid_t tid, struct inode *ip, s64 lmxaddr, pxd_t * opxd,
2442 s64 nxaddr)
2444 int rc = 0;
2445 struct metapage *mp, *pmp, *lmp, *rmp;
2446 dtpage_t *p, *pp, *rp = 0, *lp= 0;
2447 s64 bn;
2448 int index;
2449 struct btstack btstack;
2450 pxd_t *pxd;
2451 s64 oxaddr, nextbn, prevbn;
2452 int xlen, xsize;
2453 struct tlock *tlck;
2454 struct dt_lock *dtlck;
2455 struct pxd_lock *pxdlock;
2456 s8 *stbl;
2457 struct lv *lv;
2459 oxaddr = addressPXD(opxd);
2460 xlen = lengthPXD(opxd);
2462 jfs_info("dtRelocate: lmxaddr:%Ld xaddr:%Ld:%Ld xlen:%d",
2463 (long long)lmxaddr, (long long)oxaddr, (long long)nxaddr,
2464 xlen);
2467 * 1. get the internal parent dtpage covering
2468 * router entry for the tartget page to be relocated;
2470 rc = dtSearchNode(ip, lmxaddr, opxd, &btstack);
2471 if (rc)
2472 return rc;
2474 /* retrieve search result */
2475 DT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
2476 jfs_info("dtRelocate: parent router entry validated.");
2479 * 2. relocate the target dtpage
2481 /* read in the target page from src extent */
2482 DT_GETPAGE(ip, oxaddr, mp, PSIZE, p, rc);
2483 if (rc) {
2484 /* release the pinned parent page */
2485 DT_PUTPAGE(pmp);
2486 return rc;
2490 * read in sibling pages if any to update sibling pointers;
2492 rmp = NULL;
2493 if (p->header.next) {
2494 nextbn = le64_to_cpu(p->header.next);
2495 DT_GETPAGE(ip, nextbn, rmp, PSIZE, rp, rc);
2496 if (rc) {
2497 DT_PUTPAGE(mp);
2498 DT_PUTPAGE(pmp);
2499 return (rc);
2503 lmp = NULL;
2504 if (p->header.prev) {
2505 prevbn = le64_to_cpu(p->header.prev);
2506 DT_GETPAGE(ip, prevbn, lmp, PSIZE, lp, rc);
2507 if (rc) {
2508 DT_PUTPAGE(mp);
2509 DT_PUTPAGE(pmp);
2510 if (rmp)
2511 DT_PUTPAGE(rmp);
2512 return (rc);
2516 /* at this point, all xtpages to be updated are in memory */
2519 * update sibling pointers of sibling dtpages if any;
2521 if (lmp) {
2522 tlck = txLock(tid, ip, lmp, tlckDTREE | tlckRELINK);
2523 dtlck = (struct dt_lock *) & tlck->lock;
2524 /* linelock header */
2525 ASSERT(dtlck->index == 0);
2526 lv = & dtlck->lv[0];
2527 lv->offset = 0;
2528 lv->length = 1;
2529 dtlck->index++;
2531 lp->header.next = cpu_to_le64(nxaddr);
2532 DT_PUTPAGE(lmp);
2535 if (rmp) {
2536 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckRELINK);
2537 dtlck = (struct dt_lock *) & tlck->lock;
2538 /* linelock header */
2539 ASSERT(dtlck->index == 0);
2540 lv = & dtlck->lv[0];
2541 lv->offset = 0;
2542 lv->length = 1;
2543 dtlck->index++;
2545 rp->header.prev = cpu_to_le64(nxaddr);
2546 DT_PUTPAGE(rmp);
2550 * update the target dtpage to be relocated
2552 * write LOG_REDOPAGE of LOG_NEW type for dst page
2553 * for the whole target page (logredo() will apply
2554 * after image and update bmap for allocation of the
2555 * dst extent), and update bmap for allocation of
2556 * the dst extent;
2558 tlck = txLock(tid, ip, mp, tlckDTREE | tlckNEW);
2559 dtlck = (struct dt_lock *) & tlck->lock;
2560 /* linelock header */
2561 ASSERT(dtlck->index == 0);
2562 lv = & dtlck->lv[0];
2564 /* update the self address in the dtpage header */
2565 pxd = &p->header.self;
2566 PXDaddress(pxd, nxaddr);
2568 /* the dst page is the same as the src page, i.e.,
2569 * linelock for afterimage of the whole page;
2571 lv->offset = 0;
2572 lv->length = p->header.maxslot;
2573 dtlck->index++;
2575 /* update the buffer extent descriptor of the dtpage */
2576 xsize = xlen << JFS_SBI(ip->i_sb)->l2bsize;
2578 /* unpin the relocated page */
2579 DT_PUTPAGE(mp);
2580 jfs_info("dtRelocate: target dtpage relocated.");
2582 /* the moved extent is dtpage, then a LOG_NOREDOPAGE log rec
2583 * needs to be written (in logredo(), the LOG_NOREDOPAGE log rec
2584 * will also force a bmap update ).
2588 * 3. acquire maplock for the source extent to be freed;
2590 /* for dtpage relocation, write a LOG_NOREDOPAGE record
2591 * for the source dtpage (logredo() will init NoRedoPage
2592 * filter and will also update bmap for free of the source
2593 * dtpage), and upadte bmap for free of the source dtpage;
2595 tlck = txMaplock(tid, ip, tlckDTREE | tlckFREE);
2596 pxdlock = (struct pxd_lock *) & tlck->lock;
2597 pxdlock->flag = mlckFREEPXD;
2598 PXDaddress(&pxdlock->pxd, oxaddr);
2599 PXDlength(&pxdlock->pxd, xlen);
2600 pxdlock->index = 1;
2603 * 4. update the parent router entry for relocation;
2605 * acquire tlck for the parent entry covering the target dtpage;
2606 * write LOG_REDOPAGE to apply after image only;
2608 jfs_info("dtRelocate: update parent router entry.");
2609 tlck = txLock(tid, ip, pmp, tlckDTREE | tlckENTRY);
2610 dtlck = (struct dt_lock *) & tlck->lock;
2611 lv = & dtlck->lv[dtlck->index];
2613 /* update the PXD with the new address */
2614 stbl = DT_GETSTBL(pp);
2615 pxd = (pxd_t *) & pp->slot[stbl[index]];
2616 PXDaddress(pxd, nxaddr);
2617 lv->offset = stbl[index];
2618 lv->length = 1;
2619 dtlck->index++;
2621 /* unpin the parent dtpage */
2622 DT_PUTPAGE(pmp);
2624 return rc;
2628 * NAME: dtSearchNode()
2630 * FUNCTION: Search for an dtpage containing a specified address
2631 * This function is mainly used by defragfs utility.
2633 * NOTE: Search result on stack, the found page is pinned at exit.
2634 * The result page must be an internal dtpage.
2635 * lmxaddr give the address of the left most page of the
2636 * dtree level, in which the required dtpage resides.
2638 static int dtSearchNode(struct inode *ip, s64 lmxaddr, pxd_t * kpxd,
2639 struct btstack * btstack)
2641 int rc = 0;
2642 s64 bn;
2643 struct metapage *mp;
2644 dtpage_t *p;
2645 int psize = 288; /* initial in-line directory */
2646 s8 *stbl;
2647 int i;
2648 pxd_t *pxd;
2649 struct btframe *btsp;
2651 BT_CLR(btstack); /* reset stack */
2654 * descend tree to the level with specified leftmost page
2656 * by convention, root bn = 0.
2658 for (bn = 0;;) {
2659 /* get/pin the page to search */
2660 DT_GETPAGE(ip, bn, mp, psize, p, rc);
2661 if (rc)
2662 return rc;
2664 /* does the xaddr of leftmost page of the levevl
2665 * matches levevl search key ?
2667 if (p->header.flag & BT_ROOT) {
2668 if (lmxaddr == 0)
2669 break;
2670 } else if (addressPXD(&p->header.self) == lmxaddr)
2671 break;
2674 * descend down to leftmost child page
2676 if (p->header.flag & BT_LEAF) {
2677 DT_PUTPAGE(mp);
2678 return -ESTALE;
2681 /* get the leftmost entry */
2682 stbl = DT_GETSTBL(p);
2683 pxd = (pxd_t *) & p->slot[stbl[0]];
2685 /* get the child page block address */
2686 bn = addressPXD(pxd);
2687 psize = lengthPXD(pxd) << JFS_SBI(ip->i_sb)->l2bsize;
2688 /* unpin the parent page */
2689 DT_PUTPAGE(mp);
2693 * search each page at the current levevl
2695 loop:
2696 stbl = DT_GETSTBL(p);
2697 for (i = 0; i < p->header.nextindex; i++) {
2698 pxd = (pxd_t *) & p->slot[stbl[i]];
2700 /* found the specified router entry */
2701 if (addressPXD(pxd) == addressPXD(kpxd) &&
2702 lengthPXD(pxd) == lengthPXD(kpxd)) {
2703 btsp = btstack->top;
2704 btsp->bn = bn;
2705 btsp->index = i;
2706 btsp->mp = mp;
2708 return 0;
2712 /* get the right sibling page if any */
2713 if (p->header.next)
2714 bn = le64_to_cpu(p->header.next);
2715 else {
2716 DT_PUTPAGE(mp);
2717 return -ESTALE;
2720 /* unpin current page */
2721 DT_PUTPAGE(mp);
2723 /* get the right sibling page */
2724 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2725 if (rc)
2726 return rc;
2728 goto loop;
2730 #endif /* _NOTYET */
2733 * dtRelink()
2735 * function:
2736 * link around a freed page.
2738 * parameter:
2739 * fp: page to be freed
2741 * return:
2743 static int dtRelink(tid_t tid, struct inode *ip, dtpage_t * p)
2745 int rc;
2746 struct metapage *mp;
2747 s64 nextbn, prevbn;
2748 struct tlock *tlck;
2749 struct dt_lock *dtlck;
2750 struct lv *lv;
2752 nextbn = le64_to_cpu(p->header.next);
2753 prevbn = le64_to_cpu(p->header.prev);
2755 /* update prev pointer of the next page */
2756 if (nextbn != 0) {
2757 DT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
2758 if (rc)
2759 return rc;
2761 BT_MARK_DIRTY(mp, ip);
2763 * acquire a transaction lock on the next page
2765 * action: update prev pointer;
2767 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
2768 jfs_info("dtRelink nextbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2769 tlck, ip, mp);
2770 dtlck = (struct dt_lock *) & tlck->lock;
2772 /* linelock header */
2773 if (dtlck->index >= dtlck->maxcnt)
2774 dtlck = (struct dt_lock *) txLinelock(dtlck);
2775 lv = & dtlck->lv[dtlck->index];
2776 lv->offset = 0;
2777 lv->length = 1;
2778 dtlck->index++;
2780 p->header.prev = cpu_to_le64(prevbn);
2781 DT_PUTPAGE(mp);
2784 /* update next pointer of the previous page */
2785 if (prevbn != 0) {
2786 DT_GETPAGE(ip, prevbn, mp, PSIZE, p, rc);
2787 if (rc)
2788 return rc;
2790 BT_MARK_DIRTY(mp, ip);
2792 * acquire a transaction lock on the prev page
2794 * action: update next pointer;
2796 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
2797 jfs_info("dtRelink prevbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2798 tlck, ip, mp);
2799 dtlck = (struct dt_lock *) & tlck->lock;
2801 /* linelock header */
2802 if (dtlck->index >= dtlck->maxcnt)
2803 dtlck = (struct dt_lock *) txLinelock(dtlck);
2804 lv = & dtlck->lv[dtlck->index];
2805 lv->offset = 0;
2806 lv->length = 1;
2807 dtlck->index++;
2809 p->header.next = cpu_to_le64(nextbn);
2810 DT_PUTPAGE(mp);
2813 return 0;
2818 * dtInitRoot()
2820 * initialize directory root (inline in inode)
2822 void dtInitRoot(tid_t tid, struct inode *ip, u32 idotdot)
2824 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
2825 dtroot_t *p;
2826 int fsi;
2827 struct dtslot *f;
2828 struct tlock *tlck;
2829 struct dt_lock *dtlck;
2830 struct lv *lv;
2831 u16 xflag_save;
2834 * If this was previously an non-empty directory, we need to remove
2835 * the old directory table.
2837 if (DO_INDEX(ip)) {
2838 if (!jfs_dirtable_inline(ip)) {
2839 struct tblock *tblk = tid_to_tblock(tid);
2841 * We're playing games with the tid's xflag. If
2842 * we're removing a regular file, the file's xtree
2843 * is committed with COMMIT_PMAP, but we always
2844 * commit the directories xtree with COMMIT_PWMAP.
2846 xflag_save = tblk->xflag;
2847 tblk->xflag = 0;
2849 * xtTruncate isn't guaranteed to fully truncate
2850 * the xtree. The caller needs to check i_size
2851 * after committing the transaction to see if
2852 * additional truncation is needed. The
2853 * COMMIT_Stale flag tells caller that we
2854 * initiated the truncation.
2856 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
2857 set_cflag(COMMIT_Stale, ip);
2859 tblk->xflag = xflag_save;
2860 } else
2861 ip->i_size = 1;
2863 jfs_ip->next_index = 2;
2864 } else
2865 ip->i_size = IDATASIZE;
2868 * acquire a transaction lock on the root
2870 * action: directory initialization;
2872 tlck = txLock(tid, ip, (struct metapage *) & jfs_ip->bxflag,
2873 tlckDTREE | tlckENTRY | tlckBTROOT);
2874 dtlck = (struct dt_lock *) & tlck->lock;
2876 /* linelock root */
2877 ASSERT(dtlck->index == 0);
2878 lv = & dtlck->lv[0];
2879 lv->offset = 0;
2880 lv->length = DTROOTMAXSLOT;
2881 dtlck->index++;
2883 p = &jfs_ip->i_dtroot;
2885 p->header.flag = DXD_INDEX | BT_ROOT | BT_LEAF;
2887 p->header.nextindex = 0;
2889 /* init freelist */
2890 fsi = 1;
2891 f = &p->slot[fsi];
2893 /* init data area of root */
2894 for (fsi++; fsi < DTROOTMAXSLOT; f++, fsi++)
2895 f->next = fsi;
2896 f->next = -1;
2898 p->header.freelist = 1;
2899 p->header.freecnt = 8;
2901 /* init '..' entry */
2902 p->header.idotdot = cpu_to_le32(idotdot);
2904 return;
2908 * add_missing_indices()
2910 * function: Fix dtree page in which one or more entries has an invalid index.
2911 * fsck.jfs should really fix this, but it currently does not.
2912 * Called from jfs_readdir when bad index is detected.
2914 static void add_missing_indices(struct inode *inode, s64 bn)
2916 struct ldtentry *d;
2917 struct dt_lock *dtlck;
2918 int i;
2919 uint index;
2920 struct lv *lv;
2921 struct metapage *mp;
2922 dtpage_t *p;
2923 int rc;
2924 s8 *stbl;
2925 tid_t tid;
2926 struct tlock *tlck;
2928 tid = txBegin(inode->i_sb, 0);
2930 DT_GETPAGE(inode, bn, mp, PSIZE, p, rc);
2932 if (rc) {
2933 printk(KERN_ERR "DT_GETPAGE failed!\n");
2934 goto end;
2936 BT_MARK_DIRTY(mp, inode);
2938 ASSERT(p->header.flag & BT_LEAF);
2940 tlck = txLock(tid, inode, mp, tlckDTREE | tlckENTRY);
2941 if (BT_IS_ROOT(mp))
2942 tlck->type |= tlckBTROOT;
2944 dtlck = (struct dt_lock *) &tlck->lock;
2946 stbl = DT_GETSTBL(p);
2947 for (i = 0; i < p->header.nextindex; i++) {
2948 d = (struct ldtentry *) &p->slot[stbl[i]];
2949 index = le32_to_cpu(d->index);
2950 if ((index < 2) || (index >= JFS_IP(inode)->next_index)) {
2951 d->index = cpu_to_le32(add_index(tid, inode, bn, i));
2952 if (dtlck->index >= dtlck->maxcnt)
2953 dtlck = (struct dt_lock *) txLinelock(dtlck);
2954 lv = &dtlck->lv[dtlck->index];
2955 lv->offset = stbl[i];
2956 lv->length = 1;
2957 dtlck->index++;
2961 DT_PUTPAGE(mp);
2962 (void) txCommit(tid, 1, &inode, 0);
2963 end:
2964 txEnd(tid);
2968 * Buffer to hold directory entry info while traversing a dtree page
2969 * before being fed to the filldir function
2971 struct jfs_dirent {
2972 loff_t position;
2973 int ino;
2974 u16 name_len;
2975 char name[0];
2979 * function to determine next variable-sized jfs_dirent in buffer
2981 static inline struct jfs_dirent *next_jfs_dirent(struct jfs_dirent *dirent)
2983 return (struct jfs_dirent *)
2984 ((char *)dirent +
2985 ((sizeof (struct jfs_dirent) + dirent->name_len + 1 +
2986 sizeof (loff_t) - 1) &
2987 ~(sizeof (loff_t) - 1)));
2991 * jfs_readdir()
2993 * function: read directory entries sequentially
2994 * from the specified entry offset
2996 * parameter:
2998 * return: offset = (pn, index) of start entry
2999 * of next jfs_readdir()/dtRead()
3001 int jfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
3003 struct inode *ip = filp->f_path.dentry->d_inode;
3004 struct nls_table *codepage = JFS_SBI(ip->i_sb)->nls_tab;
3005 int rc = 0;
3006 loff_t dtpos; /* legacy OS/2 style position */
3007 struct dtoffset {
3008 s16 pn;
3009 s16 index;
3010 s32 unused;
3011 } *dtoffset = (struct dtoffset *) &dtpos;
3012 s64 bn;
3013 struct metapage *mp;
3014 dtpage_t *p;
3015 int index;
3016 s8 *stbl;
3017 struct btstack btstack;
3018 int i, next;
3019 struct ldtentry *d;
3020 struct dtslot *t;
3021 int d_namleft, len, outlen;
3022 unsigned long dirent_buf;
3023 char *name_ptr;
3024 u32 dir_index;
3025 int do_index = 0;
3026 uint loop_count = 0;
3027 struct jfs_dirent *jfs_dirent;
3028 int jfs_dirents;
3029 int overflow, fix_page, page_fixed = 0;
3030 static int unique_pos = 2; /* If we can't fix broken index */
3032 if (filp->f_pos == DIREND)
3033 return 0;
3035 if (DO_INDEX(ip)) {
3037 * persistent index is stored in directory entries.
3038 * Special cases: 0 = .
3039 * 1 = ..
3040 * -1 = End of directory
3042 do_index = 1;
3044 dir_index = (u32) filp->f_pos;
3046 if (dir_index > 1) {
3047 struct dir_table_slot dirtab_slot;
3049 if (dtEmpty(ip) ||
3050 (dir_index >= JFS_IP(ip)->next_index)) {
3051 /* Stale position. Directory has shrunk */
3052 filp->f_pos = DIREND;
3053 return 0;
3055 repeat:
3056 rc = read_index(ip, dir_index, &dirtab_slot);
3057 if (rc) {
3058 filp->f_pos = DIREND;
3059 return rc;
3061 if (dirtab_slot.flag == DIR_INDEX_FREE) {
3062 if (loop_count++ > JFS_IP(ip)->next_index) {
3063 jfs_err("jfs_readdir detected "
3064 "infinite loop!");
3065 filp->f_pos = DIREND;
3066 return 0;
3068 dir_index = le32_to_cpu(dirtab_slot.addr2);
3069 if (dir_index == -1) {
3070 filp->f_pos = DIREND;
3071 return 0;
3073 goto repeat;
3075 bn = addressDTS(&dirtab_slot);
3076 index = dirtab_slot.slot;
3077 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3078 if (rc) {
3079 filp->f_pos = DIREND;
3080 return 0;
3082 if (p->header.flag & BT_INTERNAL) {
3083 jfs_err("jfs_readdir: bad index table");
3084 DT_PUTPAGE(mp);
3085 filp->f_pos = -1;
3086 return 0;
3088 } else {
3089 if (dir_index == 0) {
3091 * self "."
3093 filp->f_pos = 0;
3094 if (filldir(dirent, ".", 1, 0, ip->i_ino,
3095 DT_DIR))
3096 return 0;
3099 * parent ".."
3101 filp->f_pos = 1;
3102 if (filldir(dirent, "..", 2, 1, PARENT(ip), DT_DIR))
3103 return 0;
3106 * Find first entry of left-most leaf
3108 if (dtEmpty(ip)) {
3109 filp->f_pos = DIREND;
3110 return 0;
3113 if ((rc = dtReadFirst(ip, &btstack)))
3114 return rc;
3116 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
3118 } else {
3120 * Legacy filesystem - OS/2 & Linux JFS < 0.3.6
3122 * pn = index = 0: First entry "."
3123 * pn = 0; index = 1: Second entry ".."
3124 * pn > 0: Real entries, pn=1 -> leftmost page
3125 * pn = index = -1: No more entries
3127 dtpos = filp->f_pos;
3128 if (dtpos == 0) {
3129 /* build "." entry */
3131 if (filldir(dirent, ".", 1, filp->f_pos, ip->i_ino,
3132 DT_DIR))
3133 return 0;
3134 dtoffset->index = 1;
3135 filp->f_pos = dtpos;
3138 if (dtoffset->pn == 0) {
3139 if (dtoffset->index == 1) {
3140 /* build ".." entry */
3142 if (filldir(dirent, "..", 2, filp->f_pos,
3143 PARENT(ip), DT_DIR))
3144 return 0;
3145 } else {
3146 jfs_err("jfs_readdir called with "
3147 "invalid offset!");
3149 dtoffset->pn = 1;
3150 dtoffset->index = 0;
3151 filp->f_pos = dtpos;
3154 if (dtEmpty(ip)) {
3155 filp->f_pos = DIREND;
3156 return 0;
3159 if ((rc = dtReadNext(ip, &filp->f_pos, &btstack))) {
3160 jfs_err("jfs_readdir: unexpected rc = %d "
3161 "from dtReadNext", rc);
3162 filp->f_pos = DIREND;
3163 return 0;
3165 /* get start leaf page and index */
3166 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
3168 /* offset beyond directory eof ? */
3169 if (bn < 0) {
3170 filp->f_pos = DIREND;
3171 return 0;
3175 dirent_buf = __get_free_page(GFP_KERNEL);
3176 if (dirent_buf == 0) {
3177 DT_PUTPAGE(mp);
3178 jfs_warn("jfs_readdir: __get_free_page failed!");
3179 filp->f_pos = DIREND;
3180 return -ENOMEM;
3183 while (1) {
3184 jfs_dirent = (struct jfs_dirent *) dirent_buf;
3185 jfs_dirents = 0;
3186 overflow = fix_page = 0;
3188 stbl = DT_GETSTBL(p);
3190 for (i = index; i < p->header.nextindex; i++) {
3191 d = (struct ldtentry *) & p->slot[stbl[i]];
3193 if (((long) jfs_dirent + d->namlen + 1) >
3194 (dirent_buf + PAGE_SIZE)) {
3195 /* DBCS codepages could overrun dirent_buf */
3196 index = i;
3197 overflow = 1;
3198 break;
3201 d_namleft = d->namlen;
3202 name_ptr = jfs_dirent->name;
3203 jfs_dirent->ino = le32_to_cpu(d->inumber);
3205 if (do_index) {
3206 len = min(d_namleft, DTLHDRDATALEN);
3207 jfs_dirent->position = le32_to_cpu(d->index);
3209 * d->index should always be valid, but it
3210 * isn't. fsck.jfs doesn't create the
3211 * directory index for the lost+found
3212 * directory. Rather than let it go,
3213 * we can try to fix it.
3215 if ((jfs_dirent->position < 2) ||
3216 (jfs_dirent->position >=
3217 JFS_IP(ip)->next_index)) {
3218 if (!page_fixed && !isReadOnly(ip)) {
3219 fix_page = 1;
3221 * setting overflow and setting
3222 * index to i will cause the
3223 * same page to be processed
3224 * again starting here
3226 overflow = 1;
3227 index = i;
3228 break;
3230 jfs_dirent->position = unique_pos++;
3232 } else {
3233 jfs_dirent->position = dtpos;
3234 len = min(d_namleft, DTLHDRDATALEN_LEGACY);
3237 /* copy the name of head/only segment */
3238 outlen = jfs_strfromUCS_le(name_ptr, d->name, len,
3239 codepage);
3240 jfs_dirent->name_len = outlen;
3242 /* copy name in the additional segment(s) */
3243 next = d->next;
3244 while (next >= 0) {
3245 t = (struct dtslot *) & p->slot[next];
3246 name_ptr += outlen;
3247 d_namleft -= len;
3248 /* Sanity Check */
3249 if (d_namleft == 0) {
3250 jfs_error(ip->i_sb,
3251 "JFS:Dtree error: ino = "
3252 "%ld, bn=%Ld, index = %d",
3253 (long)ip->i_ino,
3254 (long long)bn,
3256 goto skip_one;
3258 len = min(d_namleft, DTSLOTDATALEN);
3259 outlen = jfs_strfromUCS_le(name_ptr, t->name,
3260 len, codepage);
3261 jfs_dirent->name_len += outlen;
3263 next = t->next;
3266 jfs_dirents++;
3267 jfs_dirent = next_jfs_dirent(jfs_dirent);
3268 skip_one:
3269 if (!do_index)
3270 dtoffset->index++;
3273 if (!overflow) {
3274 /* Point to next leaf page */
3275 if (p->header.flag & BT_ROOT)
3276 bn = 0;
3277 else {
3278 bn = le64_to_cpu(p->header.next);
3279 index = 0;
3280 /* update offset (pn:index) for new page */
3281 if (!do_index) {
3282 dtoffset->pn++;
3283 dtoffset->index = 0;
3286 page_fixed = 0;
3289 /* unpin previous leaf page */
3290 DT_PUTPAGE(mp);
3292 jfs_dirent = (struct jfs_dirent *) dirent_buf;
3293 while (jfs_dirents--) {
3294 filp->f_pos = jfs_dirent->position;
3295 if (filldir(dirent, jfs_dirent->name,
3296 jfs_dirent->name_len, filp->f_pos,
3297 jfs_dirent->ino, DT_UNKNOWN))
3298 goto out;
3299 jfs_dirent = next_jfs_dirent(jfs_dirent);
3302 if (fix_page) {
3303 add_missing_indices(ip, bn);
3304 page_fixed = 1;
3307 if (!overflow && (bn == 0)) {
3308 filp->f_pos = DIREND;
3309 break;
3312 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3313 if (rc) {
3314 free_page(dirent_buf);
3315 return rc;
3319 out:
3320 free_page(dirent_buf);
3322 return rc;
3327 * dtReadFirst()
3329 * function: get the leftmost page of the directory
3331 static int dtReadFirst(struct inode *ip, struct btstack * btstack)
3333 int rc = 0;
3334 s64 bn;
3335 int psize = 288; /* initial in-line directory */
3336 struct metapage *mp;
3337 dtpage_t *p;
3338 s8 *stbl;
3339 struct btframe *btsp;
3340 pxd_t *xd;
3342 BT_CLR(btstack); /* reset stack */
3345 * descend leftmost path of the tree
3347 * by convention, root bn = 0.
3349 for (bn = 0;;) {
3350 DT_GETPAGE(ip, bn, mp, psize, p, rc);
3351 if (rc)
3352 return rc;
3355 * leftmost leaf page
3357 if (p->header.flag & BT_LEAF) {
3358 /* return leftmost entry */
3359 btsp = btstack->top;
3360 btsp->bn = bn;
3361 btsp->index = 0;
3362 btsp->mp = mp;
3364 return 0;
3368 * descend down to leftmost child page
3370 if (BT_STACK_FULL(btstack)) {
3371 DT_PUTPAGE(mp);
3372 jfs_error(ip->i_sb, "dtReadFirst: btstack overrun");
3373 BT_STACK_DUMP(btstack);
3374 return -EIO;
3376 /* push (bn, index) of the parent page/entry */
3377 BT_PUSH(btstack, bn, 0);
3379 /* get the leftmost entry */
3380 stbl = DT_GETSTBL(p);
3381 xd = (pxd_t *) & p->slot[stbl[0]];
3383 /* get the child page block address */
3384 bn = addressPXD(xd);
3385 psize = lengthPXD(xd) << JFS_SBI(ip->i_sb)->l2bsize;
3387 /* unpin the parent page */
3388 DT_PUTPAGE(mp);
3394 * dtReadNext()
3396 * function: get the page of the specified offset (pn:index)
3398 * return: if (offset > eof), bn = -1;
3400 * note: if index > nextindex of the target leaf page,
3401 * start with 1st entry of next leaf page;
3403 static int dtReadNext(struct inode *ip, loff_t * offset,
3404 struct btstack * btstack)
3406 int rc = 0;
3407 struct dtoffset {
3408 s16 pn;
3409 s16 index;
3410 s32 unused;
3411 } *dtoffset = (struct dtoffset *) offset;
3412 s64 bn;
3413 struct metapage *mp;
3414 dtpage_t *p;
3415 int index;
3416 int pn;
3417 s8 *stbl;
3418 struct btframe *btsp, *parent;
3419 pxd_t *xd;
3422 * get leftmost leaf page pinned
3424 if ((rc = dtReadFirst(ip, btstack)))
3425 return rc;
3427 /* get leaf page */
3428 DT_GETSEARCH(ip, btstack->top, bn, mp, p, index);
3430 /* get the start offset (pn:index) */
3431 pn = dtoffset->pn - 1; /* Now pn = 0 represents leftmost leaf */
3432 index = dtoffset->index;
3434 /* start at leftmost page ? */
3435 if (pn == 0) {
3436 /* offset beyond eof ? */
3437 if (index < p->header.nextindex)
3438 goto out;
3440 if (p->header.flag & BT_ROOT) {
3441 bn = -1;
3442 goto out;
3445 /* start with 1st entry of next leaf page */
3446 dtoffset->pn++;
3447 dtoffset->index = index = 0;
3448 goto a;
3451 /* start at non-leftmost page: scan parent pages for large pn */
3452 if (p->header.flag & BT_ROOT) {
3453 bn = -1;
3454 goto out;
3457 /* start after next leaf page ? */
3458 if (pn > 1)
3459 goto b;
3461 /* get leaf page pn = 1 */
3463 bn = le64_to_cpu(p->header.next);
3465 /* unpin leaf page */
3466 DT_PUTPAGE(mp);
3468 /* offset beyond eof ? */
3469 if (bn == 0) {
3470 bn = -1;
3471 goto out;
3474 goto c;
3477 * scan last internal page level to get target leaf page
3480 /* unpin leftmost leaf page */
3481 DT_PUTPAGE(mp);
3483 /* get left most parent page */
3484 btsp = btstack->top;
3485 parent = btsp - 1;
3486 bn = parent->bn;
3487 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3488 if (rc)
3489 return rc;
3491 /* scan parent pages at last internal page level */
3492 while (pn >= p->header.nextindex) {
3493 pn -= p->header.nextindex;
3495 /* get next parent page address */
3496 bn = le64_to_cpu(p->header.next);
3498 /* unpin current parent page */
3499 DT_PUTPAGE(mp);
3501 /* offset beyond eof ? */
3502 if (bn == 0) {
3503 bn = -1;
3504 goto out;
3507 /* get next parent page */
3508 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3509 if (rc)
3510 return rc;
3512 /* update parent page stack frame */
3513 parent->bn = bn;
3516 /* get leaf page address */
3517 stbl = DT_GETSTBL(p);
3518 xd = (pxd_t *) & p->slot[stbl[pn]];
3519 bn = addressPXD(xd);
3521 /* unpin parent page */
3522 DT_PUTPAGE(mp);
3525 * get target leaf page
3528 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3529 if (rc)
3530 return rc;
3533 * leaf page has been completed:
3534 * start with 1st entry of next leaf page
3536 if (index >= p->header.nextindex) {
3537 bn = le64_to_cpu(p->header.next);
3539 /* unpin leaf page */
3540 DT_PUTPAGE(mp);
3542 /* offset beyond eof ? */
3543 if (bn == 0) {
3544 bn = -1;
3545 goto out;
3548 /* get next leaf page */
3549 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3550 if (rc)
3551 return rc;
3553 /* start with 1st entry of next leaf page */
3554 dtoffset->pn++;
3555 dtoffset->index = 0;
3558 out:
3559 /* return target leaf page pinned */
3560 btsp = btstack->top;
3561 btsp->bn = bn;
3562 btsp->index = dtoffset->index;
3563 btsp->mp = mp;
3565 return 0;
3570 * dtCompare()
3572 * function: compare search key with an internal entry
3574 * return:
3575 * < 0 if k is < record
3576 * = 0 if k is = record
3577 * > 0 if k is > record
3579 static int dtCompare(struct component_name * key, /* search key */
3580 dtpage_t * p, /* directory page */
3581 int si)
3582 { /* entry slot index */
3583 wchar_t *kname;
3584 __le16 *name;
3585 int klen, namlen, len, rc;
3586 struct idtentry *ih;
3587 struct dtslot *t;
3590 * force the left-most key on internal pages, at any level of
3591 * the tree, to be less than any search key.
3592 * this obviates having to update the leftmost key on an internal
3593 * page when the user inserts a new key in the tree smaller than
3594 * anything that has been stored.
3596 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3597 * at any internal page at any level of the tree,
3598 * it descends to child of the entry anyway -
3599 * ? make the entry as min size dummy entry)
3601 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3602 * return (1);
3605 kname = key->name;
3606 klen = key->namlen;
3608 ih = (struct idtentry *) & p->slot[si];
3609 si = ih->next;
3610 name = ih->name;
3611 namlen = ih->namlen;
3612 len = min(namlen, DTIHDRDATALEN);
3614 /* compare with head/only segment */
3615 len = min(klen, len);
3616 if ((rc = UniStrncmp_le(kname, name, len)))
3617 return rc;
3619 klen -= len;
3620 namlen -= len;
3622 /* compare with additional segment(s) */
3623 kname += len;
3624 while (klen > 0 && namlen > 0) {
3625 /* compare with next name segment */
3626 t = (struct dtslot *) & p->slot[si];
3627 len = min(namlen, DTSLOTDATALEN);
3628 len = min(klen, len);
3629 name = t->name;
3630 if ((rc = UniStrncmp_le(kname, name, len)))
3631 return rc;
3633 klen -= len;
3634 namlen -= len;
3635 kname += len;
3636 si = t->next;
3639 return (klen - namlen);
3646 * ciCompare()
3648 * function: compare search key with an (leaf/internal) entry
3650 * return:
3651 * < 0 if k is < record
3652 * = 0 if k is = record
3653 * > 0 if k is > record
3655 static int ciCompare(struct component_name * key, /* search key */
3656 dtpage_t * p, /* directory page */
3657 int si, /* entry slot index */
3658 int flag)
3660 wchar_t *kname, x;
3661 __le16 *name;
3662 int klen, namlen, len, rc;
3663 struct ldtentry *lh;
3664 struct idtentry *ih;
3665 struct dtslot *t;
3666 int i;
3669 * force the left-most key on internal pages, at any level of
3670 * the tree, to be less than any search key.
3671 * this obviates having to update the leftmost key on an internal
3672 * page when the user inserts a new key in the tree smaller than
3673 * anything that has been stored.
3675 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3676 * at any internal page at any level of the tree,
3677 * it descends to child of the entry anyway -
3678 * ? make the entry as min size dummy entry)
3680 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3681 * return (1);
3684 kname = key->name;
3685 klen = key->namlen;
3688 * leaf page entry
3690 if (p->header.flag & BT_LEAF) {
3691 lh = (struct ldtentry *) & p->slot[si];
3692 si = lh->next;
3693 name = lh->name;
3694 namlen = lh->namlen;
3695 if (flag & JFS_DIR_INDEX)
3696 len = min(namlen, DTLHDRDATALEN);
3697 else
3698 len = min(namlen, DTLHDRDATALEN_LEGACY);
3701 * internal page entry
3703 else {
3704 ih = (struct idtentry *) & p->slot[si];
3705 si = ih->next;
3706 name = ih->name;
3707 namlen = ih->namlen;
3708 len = min(namlen, DTIHDRDATALEN);
3711 /* compare with head/only segment */
3712 len = min(klen, len);
3713 for (i = 0; i < len; i++, kname++, name++) {
3714 /* only uppercase if case-insensitive support is on */
3715 if ((flag & JFS_OS2) == JFS_OS2)
3716 x = UniToupper(le16_to_cpu(*name));
3717 else
3718 x = le16_to_cpu(*name);
3719 if ((rc = *kname - x))
3720 return rc;
3723 klen -= len;
3724 namlen -= len;
3726 /* compare with additional segment(s) */
3727 while (klen > 0 && namlen > 0) {
3728 /* compare with next name segment */
3729 t = (struct dtslot *) & p->slot[si];
3730 len = min(namlen, DTSLOTDATALEN);
3731 len = min(klen, len);
3732 name = t->name;
3733 for (i = 0; i < len; i++, kname++, name++) {
3734 /* only uppercase if case-insensitive support is on */
3735 if ((flag & JFS_OS2) == JFS_OS2)
3736 x = UniToupper(le16_to_cpu(*name));
3737 else
3738 x = le16_to_cpu(*name);
3740 if ((rc = *kname - x))
3741 return rc;
3744 klen -= len;
3745 namlen -= len;
3746 si = t->next;
3749 return (klen - namlen);
3754 * ciGetLeafPrefixKey()
3756 * function: compute prefix of suffix compression
3757 * from two adjacent leaf entries
3758 * across page boundary
3760 * return: non-zero on error
3763 static int ciGetLeafPrefixKey(dtpage_t * lp, int li, dtpage_t * rp,
3764 int ri, struct component_name * key, int flag)
3766 int klen, namlen;
3767 wchar_t *pl, *pr, *kname;
3768 struct component_name lkey;
3769 struct component_name rkey;
3771 lkey.name = kmalloc((JFS_NAME_MAX + 1) * sizeof(wchar_t),
3772 GFP_KERNEL);
3773 if (lkey.name == NULL)
3774 return -ENOMEM;
3776 rkey.name = kmalloc((JFS_NAME_MAX + 1) * sizeof(wchar_t),
3777 GFP_KERNEL);
3778 if (rkey.name == NULL) {
3779 kfree(lkey.name);
3780 return -ENOMEM;
3783 /* get left and right key */
3784 dtGetKey(lp, li, &lkey, flag);
3785 lkey.name[lkey.namlen] = 0;
3787 if ((flag & JFS_OS2) == JFS_OS2)
3788 ciToUpper(&lkey);
3790 dtGetKey(rp, ri, &rkey, flag);
3791 rkey.name[rkey.namlen] = 0;
3794 if ((flag & JFS_OS2) == JFS_OS2)
3795 ciToUpper(&rkey);
3797 /* compute prefix */
3798 klen = 0;
3799 kname = key->name;
3800 namlen = min(lkey.namlen, rkey.namlen);
3801 for (pl = lkey.name, pr = rkey.name;
3802 namlen; pl++, pr++, namlen--, klen++, kname++) {
3803 *kname = *pr;
3804 if (*pl != *pr) {
3805 key->namlen = klen + 1;
3806 goto free_names;
3810 /* l->namlen <= r->namlen since l <= r */
3811 if (lkey.namlen < rkey.namlen) {
3812 *kname = *pr;
3813 key->namlen = klen + 1;
3814 } else /* l->namelen == r->namelen */
3815 key->namlen = klen;
3817 free_names:
3818 kfree(lkey.name);
3819 kfree(rkey.name);
3820 return 0;
3826 * dtGetKey()
3828 * function: get key of the entry
3830 static void dtGetKey(dtpage_t * p, int i, /* entry index */
3831 struct component_name * key, int flag)
3833 int si;
3834 s8 *stbl;
3835 struct ldtentry *lh;
3836 struct idtentry *ih;
3837 struct dtslot *t;
3838 int namlen, len;
3839 wchar_t *kname;
3840 __le16 *name;
3842 /* get entry */
3843 stbl = DT_GETSTBL(p);
3844 si = stbl[i];
3845 if (p->header.flag & BT_LEAF) {
3846 lh = (struct ldtentry *) & p->slot[si];
3847 si = lh->next;
3848 namlen = lh->namlen;
3849 name = lh->name;
3850 if (flag & JFS_DIR_INDEX)
3851 len = min(namlen, DTLHDRDATALEN);
3852 else
3853 len = min(namlen, DTLHDRDATALEN_LEGACY);
3854 } else {
3855 ih = (struct idtentry *) & p->slot[si];
3856 si = ih->next;
3857 namlen = ih->namlen;
3858 name = ih->name;
3859 len = min(namlen, DTIHDRDATALEN);
3862 key->namlen = namlen;
3863 kname = key->name;
3866 * move head/only segment
3868 UniStrncpy_from_le(kname, name, len);
3871 * move additional segment(s)
3873 while (si >= 0) {
3874 /* get next segment */
3875 t = &p->slot[si];
3876 kname += len;
3877 namlen -= len;
3878 len = min(namlen, DTSLOTDATALEN);
3879 UniStrncpy_from_le(kname, t->name, len);
3881 si = t->next;
3887 * dtInsertEntry()
3889 * function: allocate free slot(s) and
3890 * write a leaf/internal entry
3892 * return: entry slot index
3894 static void dtInsertEntry(dtpage_t * p, int index, struct component_name * key,
3895 ddata_t * data, struct dt_lock ** dtlock)
3897 struct dtslot *h, *t;
3898 struct ldtentry *lh = NULL;
3899 struct idtentry *ih = NULL;
3900 int hsi, fsi, klen, len, nextindex;
3901 wchar_t *kname;
3902 __le16 *name;
3903 s8 *stbl;
3904 pxd_t *xd;
3905 struct dt_lock *dtlck = *dtlock;
3906 struct lv *lv;
3907 int xsi, n;
3908 s64 bn = 0;
3909 struct metapage *mp = NULL;
3911 klen = key->namlen;
3912 kname = key->name;
3914 /* allocate a free slot */
3915 hsi = fsi = p->header.freelist;
3916 h = &p->slot[fsi];
3917 p->header.freelist = h->next;
3918 --p->header.freecnt;
3920 /* open new linelock */
3921 if (dtlck->index >= dtlck->maxcnt)
3922 dtlck = (struct dt_lock *) txLinelock(dtlck);
3924 lv = & dtlck->lv[dtlck->index];
3925 lv->offset = hsi;
3927 /* write head/only segment */
3928 if (p->header.flag & BT_LEAF) {
3929 lh = (struct ldtentry *) h;
3930 lh->next = h->next;
3931 lh->inumber = cpu_to_le32(data->leaf.ino);
3932 lh->namlen = klen;
3933 name = lh->name;
3934 if (data->leaf.ip) {
3935 len = min(klen, DTLHDRDATALEN);
3936 if (!(p->header.flag & BT_ROOT))
3937 bn = addressPXD(&p->header.self);
3938 lh->index = cpu_to_le32(add_index(data->leaf.tid,
3939 data->leaf.ip,
3940 bn, index));
3941 } else
3942 len = min(klen, DTLHDRDATALEN_LEGACY);
3943 } else {
3944 ih = (struct idtentry *) h;
3945 ih->next = h->next;
3946 xd = (pxd_t *) ih;
3947 *xd = data->xd;
3948 ih->namlen = klen;
3949 name = ih->name;
3950 len = min(klen, DTIHDRDATALEN);
3953 UniStrncpy_to_le(name, kname, len);
3955 n = 1;
3956 xsi = hsi;
3958 /* write additional segment(s) */
3959 t = h;
3960 klen -= len;
3961 while (klen) {
3962 /* get free slot */
3963 fsi = p->header.freelist;
3964 t = &p->slot[fsi];
3965 p->header.freelist = t->next;
3966 --p->header.freecnt;
3968 /* is next slot contiguous ? */
3969 if (fsi != xsi + 1) {
3970 /* close current linelock */
3971 lv->length = n;
3972 dtlck->index++;
3974 /* open new linelock */
3975 if (dtlck->index < dtlck->maxcnt)
3976 lv++;
3977 else {
3978 dtlck = (struct dt_lock *) txLinelock(dtlck);
3979 lv = & dtlck->lv[0];
3982 lv->offset = fsi;
3983 n = 0;
3986 kname += len;
3987 len = min(klen, DTSLOTDATALEN);
3988 UniStrncpy_to_le(t->name, kname, len);
3990 n++;
3991 xsi = fsi;
3992 klen -= len;
3995 /* close current linelock */
3996 lv->length = n;
3997 dtlck->index++;
3999 *dtlock = dtlck;
4001 /* terminate last/only segment */
4002 if (h == t) {
4003 /* single segment entry */
4004 if (p->header.flag & BT_LEAF)
4005 lh->next = -1;
4006 else
4007 ih->next = -1;
4008 } else
4009 /* multi-segment entry */
4010 t->next = -1;
4012 /* if insert into middle, shift right succeeding entries in stbl */
4013 stbl = DT_GETSTBL(p);
4014 nextindex = p->header.nextindex;
4015 if (index < nextindex) {
4016 memmove(stbl + index + 1, stbl + index, nextindex - index);
4018 if ((p->header.flag & BT_LEAF) && data->leaf.ip) {
4019 s64 lblock;
4022 * Need to update slot number for entries that moved
4023 * in the stbl
4025 mp = NULL;
4026 for (n = index + 1; n <= nextindex; n++) {
4027 lh = (struct ldtentry *) & (p->slot[stbl[n]]);
4028 modify_index(data->leaf.tid, data->leaf.ip,
4029 le32_to_cpu(lh->index), bn, n,
4030 &mp, &lblock);
4032 if (mp)
4033 release_metapage(mp);
4037 stbl[index] = hsi;
4039 /* advance next available entry index of stbl */
4040 ++p->header.nextindex;
4045 * dtMoveEntry()
4047 * function: move entries from split/left page to new/right page
4049 * nextindex of dst page and freelist/freecnt of both pages
4050 * are updated.
4052 static void dtMoveEntry(dtpage_t * sp, int si, dtpage_t * dp,
4053 struct dt_lock ** sdtlock, struct dt_lock ** ddtlock,
4054 int do_index)
4056 int ssi, next; /* src slot index */
4057 int di; /* dst entry index */
4058 int dsi; /* dst slot index */
4059 s8 *sstbl, *dstbl; /* sorted entry table */
4060 int snamlen, len;
4061 struct ldtentry *slh, *dlh = NULL;
4062 struct idtentry *sih, *dih = NULL;
4063 struct dtslot *h, *s, *d;
4064 struct dt_lock *sdtlck = *sdtlock, *ddtlck = *ddtlock;
4065 struct lv *slv, *dlv;
4066 int xssi, ns, nd;
4067 int sfsi;
4069 sstbl = (s8 *) & sp->slot[sp->header.stblindex];
4070 dstbl = (s8 *) & dp->slot[dp->header.stblindex];
4072 dsi = dp->header.freelist; /* first (whole page) free slot */
4073 sfsi = sp->header.freelist;
4075 /* linelock destination entry slot */
4076 dlv = & ddtlck->lv[ddtlck->index];
4077 dlv->offset = dsi;
4079 /* linelock source entry slot */
4080 slv = & sdtlck->lv[sdtlck->index];
4081 slv->offset = sstbl[si];
4082 xssi = slv->offset - 1;
4085 * move entries
4087 ns = nd = 0;
4088 for (di = 0; si < sp->header.nextindex; si++, di++) {
4089 ssi = sstbl[si];
4090 dstbl[di] = dsi;
4092 /* is next slot contiguous ? */
4093 if (ssi != xssi + 1) {
4094 /* close current linelock */
4095 slv->length = ns;
4096 sdtlck->index++;
4098 /* open new linelock */
4099 if (sdtlck->index < sdtlck->maxcnt)
4100 slv++;
4101 else {
4102 sdtlck = (struct dt_lock *) txLinelock(sdtlck);
4103 slv = & sdtlck->lv[0];
4106 slv->offset = ssi;
4107 ns = 0;
4111 * move head/only segment of an entry
4113 /* get dst slot */
4114 h = d = &dp->slot[dsi];
4116 /* get src slot and move */
4117 s = &sp->slot[ssi];
4118 if (sp->header.flag & BT_LEAF) {
4119 /* get source entry */
4120 slh = (struct ldtentry *) s;
4121 dlh = (struct ldtentry *) h;
4122 snamlen = slh->namlen;
4124 if (do_index) {
4125 len = min(snamlen, DTLHDRDATALEN);
4126 dlh->index = slh->index; /* little-endian */
4127 } else
4128 len = min(snamlen, DTLHDRDATALEN_LEGACY);
4130 memcpy(dlh, slh, 6 + len * 2);
4132 next = slh->next;
4134 /* update dst head/only segment next field */
4135 dsi++;
4136 dlh->next = dsi;
4137 } else {
4138 sih = (struct idtentry *) s;
4139 snamlen = sih->namlen;
4141 len = min(snamlen, DTIHDRDATALEN);
4142 dih = (struct idtentry *) h;
4143 memcpy(dih, sih, 10 + len * 2);
4144 next = sih->next;
4146 dsi++;
4147 dih->next = dsi;
4150 /* free src head/only segment */
4151 s->next = sfsi;
4152 s->cnt = 1;
4153 sfsi = ssi;
4155 ns++;
4156 nd++;
4157 xssi = ssi;
4160 * move additional segment(s) of the entry
4162 snamlen -= len;
4163 while ((ssi = next) >= 0) {
4164 /* is next slot contiguous ? */
4165 if (ssi != xssi + 1) {
4166 /* close current linelock */
4167 slv->length = ns;
4168 sdtlck->index++;
4170 /* open new linelock */
4171 if (sdtlck->index < sdtlck->maxcnt)
4172 slv++;
4173 else {
4174 sdtlck =
4175 (struct dt_lock *)
4176 txLinelock(sdtlck);
4177 slv = & sdtlck->lv[0];
4180 slv->offset = ssi;
4181 ns = 0;
4184 /* get next source segment */
4185 s = &sp->slot[ssi];
4187 /* get next destination free slot */
4188 d++;
4190 len = min(snamlen, DTSLOTDATALEN);
4191 UniStrncpy_le(d->name, s->name, len);
4193 ns++;
4194 nd++;
4195 xssi = ssi;
4197 dsi++;
4198 d->next = dsi;
4200 /* free source segment */
4201 next = s->next;
4202 s->next = sfsi;
4203 s->cnt = 1;
4204 sfsi = ssi;
4206 snamlen -= len;
4207 } /* end while */
4209 /* terminate dst last/only segment */
4210 if (h == d) {
4211 /* single segment entry */
4212 if (dp->header.flag & BT_LEAF)
4213 dlh->next = -1;
4214 else
4215 dih->next = -1;
4216 } else
4217 /* multi-segment entry */
4218 d->next = -1;
4219 } /* end for */
4221 /* close current linelock */
4222 slv->length = ns;
4223 sdtlck->index++;
4224 *sdtlock = sdtlck;
4226 dlv->length = nd;
4227 ddtlck->index++;
4228 *ddtlock = ddtlck;
4230 /* update source header */
4231 sp->header.freelist = sfsi;
4232 sp->header.freecnt += nd;
4234 /* update destination header */
4235 dp->header.nextindex = di;
4237 dp->header.freelist = dsi;
4238 dp->header.freecnt -= nd;
4243 * dtDeleteEntry()
4245 * function: free a (leaf/internal) entry
4247 * log freelist header, stbl, and each segment slot of entry
4248 * (even though last/only segment next field is modified,
4249 * physical image logging requires all segment slots of
4250 * the entry logged to avoid applying previous updates
4251 * to the same slots)
4253 static void dtDeleteEntry(dtpage_t * p, int fi, struct dt_lock ** dtlock)
4255 int fsi; /* free entry slot index */
4256 s8 *stbl;
4257 struct dtslot *t;
4258 int si, freecnt;
4259 struct dt_lock *dtlck = *dtlock;
4260 struct lv *lv;
4261 int xsi, n;
4263 /* get free entry slot index */
4264 stbl = DT_GETSTBL(p);
4265 fsi = stbl[fi];
4267 /* open new linelock */
4268 if (dtlck->index >= dtlck->maxcnt)
4269 dtlck = (struct dt_lock *) txLinelock(dtlck);
4270 lv = & dtlck->lv[dtlck->index];
4272 lv->offset = fsi;
4274 /* get the head/only segment */
4275 t = &p->slot[fsi];
4276 if (p->header.flag & BT_LEAF)
4277 si = ((struct ldtentry *) t)->next;
4278 else
4279 si = ((struct idtentry *) t)->next;
4280 t->next = si;
4281 t->cnt = 1;
4283 n = freecnt = 1;
4284 xsi = fsi;
4286 /* find the last/only segment */
4287 while (si >= 0) {
4288 /* is next slot contiguous ? */
4289 if (si != xsi + 1) {
4290 /* close current linelock */
4291 lv->length = n;
4292 dtlck->index++;
4294 /* open new linelock */
4295 if (dtlck->index < dtlck->maxcnt)
4296 lv++;
4297 else {
4298 dtlck = (struct dt_lock *) txLinelock(dtlck);
4299 lv = & dtlck->lv[0];
4302 lv->offset = si;
4303 n = 0;
4306 n++;
4307 xsi = si;
4308 freecnt++;
4310 t = &p->slot[si];
4311 t->cnt = 1;
4312 si = t->next;
4315 /* close current linelock */
4316 lv->length = n;
4317 dtlck->index++;
4319 *dtlock = dtlck;
4321 /* update freelist */
4322 t->next = p->header.freelist;
4323 p->header.freelist = fsi;
4324 p->header.freecnt += freecnt;
4326 /* if delete from middle,
4327 * shift left the succedding entries in the stbl
4329 si = p->header.nextindex;
4330 if (fi < si - 1)
4331 memmove(&stbl[fi], &stbl[fi + 1], si - fi - 1);
4333 p->header.nextindex--;
4338 * dtTruncateEntry()
4340 * function: truncate a (leaf/internal) entry
4342 * log freelist header, stbl, and each segment slot of entry
4343 * (even though last/only segment next field is modified,
4344 * physical image logging requires all segment slots of
4345 * the entry logged to avoid applying previous updates
4346 * to the same slots)
4348 static void dtTruncateEntry(dtpage_t * p, int ti, struct dt_lock ** dtlock)
4350 int tsi; /* truncate entry slot index */
4351 s8 *stbl;
4352 struct dtslot *t;
4353 int si, freecnt;
4354 struct dt_lock *dtlck = *dtlock;
4355 struct lv *lv;
4356 int fsi, xsi, n;
4358 /* get free entry slot index */
4359 stbl = DT_GETSTBL(p);
4360 tsi = stbl[ti];
4362 /* open new linelock */
4363 if (dtlck->index >= dtlck->maxcnt)
4364 dtlck = (struct dt_lock *) txLinelock(dtlck);
4365 lv = & dtlck->lv[dtlck->index];
4367 lv->offset = tsi;
4369 /* get the head/only segment */
4370 t = &p->slot[tsi];
4371 ASSERT(p->header.flag & BT_INTERNAL);
4372 ((struct idtentry *) t)->namlen = 0;
4373 si = ((struct idtentry *) t)->next;
4374 ((struct idtentry *) t)->next = -1;
4376 n = 1;
4377 freecnt = 0;
4378 fsi = si;
4379 xsi = tsi;
4381 /* find the last/only segment */
4382 while (si >= 0) {
4383 /* is next slot contiguous ? */
4384 if (si != xsi + 1) {
4385 /* close current linelock */
4386 lv->length = n;
4387 dtlck->index++;
4389 /* open new linelock */
4390 if (dtlck->index < dtlck->maxcnt)
4391 lv++;
4392 else {
4393 dtlck = (struct dt_lock *) txLinelock(dtlck);
4394 lv = & dtlck->lv[0];
4397 lv->offset = si;
4398 n = 0;
4401 n++;
4402 xsi = si;
4403 freecnt++;
4405 t = &p->slot[si];
4406 t->cnt = 1;
4407 si = t->next;
4410 /* close current linelock */
4411 lv->length = n;
4412 dtlck->index++;
4414 *dtlock = dtlck;
4416 /* update freelist */
4417 if (freecnt == 0)
4418 return;
4419 t->next = p->header.freelist;
4420 p->header.freelist = fsi;
4421 p->header.freecnt += freecnt;
4426 * dtLinelockFreelist()
4428 static void dtLinelockFreelist(dtpage_t * p, /* directory page */
4429 int m, /* max slot index */
4430 struct dt_lock ** dtlock)
4432 int fsi; /* free entry slot index */
4433 struct dtslot *t;
4434 int si;
4435 struct dt_lock *dtlck = *dtlock;
4436 struct lv *lv;
4437 int xsi, n;
4439 /* get free entry slot index */
4440 fsi = p->header.freelist;
4442 /* open new linelock */
4443 if (dtlck->index >= dtlck->maxcnt)
4444 dtlck = (struct dt_lock *) txLinelock(dtlck);
4445 lv = & dtlck->lv[dtlck->index];
4447 lv->offset = fsi;
4449 n = 1;
4450 xsi = fsi;
4452 t = &p->slot[fsi];
4453 si = t->next;
4455 /* find the last/only segment */
4456 while (si < m && si >= 0) {
4457 /* is next slot contiguous ? */
4458 if (si != xsi + 1) {
4459 /* close current linelock */
4460 lv->length = n;
4461 dtlck->index++;
4463 /* open new linelock */
4464 if (dtlck->index < dtlck->maxcnt)
4465 lv++;
4466 else {
4467 dtlck = (struct dt_lock *) txLinelock(dtlck);
4468 lv = & dtlck->lv[0];
4471 lv->offset = si;
4472 n = 0;
4475 n++;
4476 xsi = si;
4478 t = &p->slot[si];
4479 si = t->next;
4482 /* close current linelock */
4483 lv->length = n;
4484 dtlck->index++;
4486 *dtlock = dtlck;
4491 * NAME: dtModify
4493 * FUNCTION: Modify the inode number part of a directory entry
4495 * PARAMETERS:
4496 * tid - Transaction id
4497 * ip - Inode of parent directory
4498 * key - Name of entry to be modified
4499 * orig_ino - Original inode number expected in entry
4500 * new_ino - New inode number to put into entry
4501 * flag - JFS_RENAME
4503 * RETURNS:
4504 * -ESTALE - If entry found does not match orig_ino passed in
4505 * -ENOENT - If no entry can be found to match key
4506 * 0 - If successfully modified entry
4508 int dtModify(tid_t tid, struct inode *ip,
4509 struct component_name * key, ino_t * orig_ino, ino_t new_ino, int flag)
4511 int rc;
4512 s64 bn;
4513 struct metapage *mp;
4514 dtpage_t *p;
4515 int index;
4516 struct btstack btstack;
4517 struct tlock *tlck;
4518 struct dt_lock *dtlck;
4519 struct lv *lv;
4520 s8 *stbl;
4521 int entry_si; /* entry slot index */
4522 struct ldtentry *entry;
4525 * search for the entry to modify:
4527 * dtSearch() returns (leaf page pinned, index at which to modify).
4529 if ((rc = dtSearch(ip, key, orig_ino, &btstack, flag)))
4530 return rc;
4532 /* retrieve search result */
4533 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
4535 BT_MARK_DIRTY(mp, ip);
4537 * acquire a transaction lock on the leaf page of named entry
4539 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
4540 dtlck = (struct dt_lock *) & tlck->lock;
4542 /* get slot index of the entry */
4543 stbl = DT_GETSTBL(p);
4544 entry_si = stbl[index];
4546 /* linelock entry */
4547 ASSERT(dtlck->index == 0);
4548 lv = & dtlck->lv[0];
4549 lv->offset = entry_si;
4550 lv->length = 1;
4551 dtlck->index++;
4553 /* get the head/only segment */
4554 entry = (struct ldtentry *) & p->slot[entry_si];
4556 /* substitute the inode number of the entry */
4557 entry->inumber = cpu_to_le32(new_ino);
4559 /* unpin the leaf page */
4560 DT_PUTPAGE(mp);
4562 return 0;