jfs_dtree.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:103k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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