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