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