PageRenderTime 76ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 1ms

/fs/jfs/jfs_xtree.c

https://bitbucket.org/cyanogenmod/android_kernel_asus_tf300t
C | 3905 lines | 2020 code | 510 blank | 1375 comment | 374 complexity | 4c5575c4be9470acfa2d51e22e5d234c MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0
  1. /*
  2. * Copyright (C) International Business Machines Corp., 2000-2005
  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_xtree.c: extent allocation descriptor B+-tree manager
  20. */
  21. #include <linux/fs.h>
  22. #include <linux/module.h>
  23. #include <linux/quotaops.h>
  24. #include <linux/seq_file.h>
  25. #include "jfs_incore.h"
  26. #include "jfs_filsys.h"
  27. #include "jfs_metapage.h"
  28. #include "jfs_dmap.h"
  29. #include "jfs_dinode.h"
  30. #include "jfs_superblock.h"
  31. #include "jfs_debug.h"
  32. /*
  33. * xtree local flag
  34. */
  35. #define XT_INSERT 0x00000001
  36. /*
  37. * xtree key/entry comparison: extent offset
  38. *
  39. * return:
  40. * -1: k < start of extent
  41. * 0: start_of_extent <= k <= end_of_extent
  42. * 1: k > end_of_extent
  43. */
  44. #define XT_CMP(CMP, K, X, OFFSET64)\
  45. {\
  46. OFFSET64 = offsetXAD(X);\
  47. (CMP) = ((K) >= OFFSET64 + lengthXAD(X)) ? 1 :\
  48. ((K) < OFFSET64) ? -1 : 0;\
  49. }
  50. /* write a xad entry */
  51. #define XT_PUTENTRY(XAD, FLAG, OFF, LEN, ADDR)\
  52. {\
  53. (XAD)->flag = (FLAG);\
  54. XADoffset((XAD), (OFF));\
  55. XADlength((XAD), (LEN));\
  56. XADaddress((XAD), (ADDR));\
  57. }
  58. #define XT_PAGE(IP, MP) BT_PAGE(IP, MP, xtpage_t, i_xtroot)
  59. /* get page buffer for specified block address */
  60. /* ToDo: Replace this ugly macro with a function */
  61. #define XT_GETPAGE(IP, BN, MP, SIZE, P, RC)\
  62. {\
  63. BT_GETPAGE(IP, BN, MP, xtpage_t, SIZE, P, RC, i_xtroot)\
  64. if (!(RC))\
  65. {\
  66. if ((le16_to_cpu((P)->header.nextindex) < XTENTRYSTART) ||\
  67. (le16_to_cpu((P)->header.nextindex) > le16_to_cpu((P)->header.maxentry)) ||\
  68. (le16_to_cpu((P)->header.maxentry) > (((BN)==0)?XTROOTMAXSLOT:PSIZE>>L2XTSLOTSIZE)))\
  69. {\
  70. jfs_error((IP)->i_sb, "XT_GETPAGE: xtree page corrupt");\
  71. BT_PUTPAGE(MP);\
  72. MP = NULL;\
  73. RC = -EIO;\
  74. }\
  75. }\
  76. }
  77. /* for consistency */
  78. #define XT_PUTPAGE(MP) BT_PUTPAGE(MP)
  79. #define XT_GETSEARCH(IP, LEAF, BN, MP, P, INDEX) \
  80. BT_GETSEARCH(IP, LEAF, BN, MP, xtpage_t, P, INDEX, i_xtroot)
  81. /* xtree entry parameter descriptor */
  82. struct xtsplit {
  83. struct metapage *mp;
  84. s16 index;
  85. u8 flag;
  86. s64 off;
  87. s64 addr;
  88. int len;
  89. struct pxdlist *pxdlist;
  90. };
  91. /*
  92. * statistics
  93. */
  94. #ifdef CONFIG_JFS_STATISTICS
  95. static struct {
  96. uint search;
  97. uint fastSearch;
  98. uint split;
  99. } xtStat;
  100. #endif
  101. /*
  102. * forward references
  103. */
  104. static int xtSearch(struct inode *ip, s64 xoff, s64 *next, int *cmpp,
  105. struct btstack * btstack, int flag);
  106. static int xtSplitUp(tid_t tid,
  107. struct inode *ip,
  108. struct xtsplit * split, struct btstack * btstack);
  109. static int xtSplitPage(tid_t tid, struct inode *ip, struct xtsplit * split,
  110. struct metapage ** rmpp, s64 * rbnp);
  111. static int xtSplitRoot(tid_t tid, struct inode *ip,
  112. struct xtsplit * split, struct metapage ** rmpp);
  113. #ifdef _STILL_TO_PORT
  114. static int xtDeleteUp(tid_t tid, struct inode *ip, struct metapage * fmp,
  115. xtpage_t * fp, struct btstack * btstack);
  116. static int xtSearchNode(struct inode *ip,
  117. xad_t * xad,
  118. int *cmpp, struct btstack * btstack, int flag);
  119. static int xtRelink(tid_t tid, struct inode *ip, xtpage_t * fp);
  120. #endif /* _STILL_TO_PORT */
  121. /*
  122. * xtLookup()
  123. *
  124. * function: map a single page into a physical extent;
  125. */
  126. int xtLookup(struct inode *ip, s64 lstart,
  127. s64 llen, int *pflag, s64 * paddr, s32 * plen, int no_check)
  128. {
  129. int rc = 0;
  130. struct btstack btstack;
  131. int cmp;
  132. s64 bn;
  133. struct metapage *mp;
  134. xtpage_t *p;
  135. int index;
  136. xad_t *xad;
  137. s64 next, size, xoff, xend;
  138. int xlen;
  139. s64 xaddr;
  140. *paddr = 0;
  141. *plen = llen;
  142. if (!no_check) {
  143. /* is lookup offset beyond eof ? */
  144. size = ((u64) ip->i_size + (JFS_SBI(ip->i_sb)->bsize - 1)) >>
  145. JFS_SBI(ip->i_sb)->l2bsize;
  146. if (lstart >= size)
  147. return 0;
  148. }
  149. /*
  150. * search for the xad entry covering the logical extent
  151. */
  152. //search:
  153. if ((rc = xtSearch(ip, lstart, &next, &cmp, &btstack, 0))) {
  154. jfs_err("xtLookup: xtSearch returned %d", rc);
  155. return rc;
  156. }
  157. /*
  158. * compute the physical extent covering logical extent
  159. *
  160. * N.B. search may have failed (e.g., hole in sparse file),
  161. * and returned the index of the next entry.
  162. */
  163. /* retrieve search result */
  164. XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
  165. /* is xad found covering start of logical extent ?
  166. * lstart is a page start address,
  167. * i.e., lstart cannot start in a hole;
  168. */
  169. if (cmp) {
  170. if (next)
  171. *plen = min(next - lstart, llen);
  172. goto out;
  173. }
  174. /*
  175. * lxd covered by xad
  176. */
  177. xad = &p->xad[index];
  178. xoff = offsetXAD(xad);
  179. xlen = lengthXAD(xad);
  180. xend = xoff + xlen;
  181. xaddr = addressXAD(xad);
  182. /* initialize new pxd */
  183. *pflag = xad->flag;
  184. *paddr = xaddr + (lstart - xoff);
  185. /* a page must be fully covered by an xad */
  186. *plen = min(xend - lstart, llen);
  187. out:
  188. XT_PUTPAGE(mp);
  189. return rc;
  190. }
  191. /*
  192. * xtSearch()
  193. *
  194. * function: search for the xad entry covering specified offset.
  195. *
  196. * parameters:
  197. * ip - file object;
  198. * xoff - extent offset;
  199. * nextp - address of next extent (if any) for search miss
  200. * cmpp - comparison result:
  201. * btstack - traverse stack;
  202. * flag - search process flag (XT_INSERT);
  203. *
  204. * returns:
  205. * btstack contains (bn, index) of search path traversed to the entry.
  206. * *cmpp is set to result of comparison with the entry returned.
  207. * the page containing the entry is pinned at exit.
  208. */
  209. static int xtSearch(struct inode *ip, s64 xoff, s64 *nextp,
  210. int *cmpp, struct btstack * btstack, int flag)
  211. {
  212. struct jfs_inode_info *jfs_ip = JFS_IP(ip);
  213. int rc = 0;
  214. int cmp = 1; /* init for empty page */
  215. s64 bn; /* block number */
  216. struct metapage *mp; /* page buffer */
  217. xtpage_t *p; /* page */
  218. xad_t *xad;
  219. int base, index, lim, btindex;
  220. struct btframe *btsp;
  221. int nsplit = 0; /* number of pages to split */
  222. s64 t64;
  223. s64 next = 0;
  224. INCREMENT(xtStat.search);
  225. BT_CLR(btstack);
  226. btstack->nsplit = 0;
  227. /*
  228. * search down tree from root:
  229. *
  230. * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
  231. * internal page, child page Pi contains entry with k, Ki <= K < Kj.
  232. *
  233. * if entry with search key K is not found
  234. * internal page search find the entry with largest key Ki
  235. * less than K which point to the child page to search;
  236. * leaf page search find the entry with smallest key Kj
  237. * greater than K so that the returned index is the position of
  238. * the entry to be shifted right for insertion of new entry.
  239. * for empty tree, search key is greater than any key of the tree.
  240. *
  241. * by convention, root bn = 0.
  242. */
  243. for (bn = 0;;) {
  244. /* get/pin the page to search */
  245. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  246. if (rc)
  247. return rc;
  248. /* try sequential access heuristics with the previous
  249. * access entry in target leaf page:
  250. * once search narrowed down into the target leaf,
  251. * key must either match an entry in the leaf or
  252. * key entry does not exist in the tree;
  253. */
  254. //fastSearch:
  255. if ((jfs_ip->btorder & BT_SEQUENTIAL) &&
  256. (p->header.flag & BT_LEAF) &&
  257. (index = jfs_ip->btindex) <
  258. le16_to_cpu(p->header.nextindex)) {
  259. xad = &p->xad[index];
  260. t64 = offsetXAD(xad);
  261. if (xoff < t64 + lengthXAD(xad)) {
  262. if (xoff >= t64) {
  263. *cmpp = 0;
  264. goto out;
  265. }
  266. /* stop sequential access heuristics */
  267. goto binarySearch;
  268. } else { /* (t64 + lengthXAD(xad)) <= xoff */
  269. /* try next sequential entry */
  270. index++;
  271. if (index <
  272. le16_to_cpu(p->header.nextindex)) {
  273. xad++;
  274. t64 = offsetXAD(xad);
  275. if (xoff < t64 + lengthXAD(xad)) {
  276. if (xoff >= t64) {
  277. *cmpp = 0;
  278. goto out;
  279. }
  280. /* miss: key falls between
  281. * previous and this entry
  282. */
  283. *cmpp = 1;
  284. next = t64;
  285. goto out;
  286. }
  287. /* (xoff >= t64 + lengthXAD(xad));
  288. * matching entry may be further out:
  289. * stop heuristic search
  290. */
  291. /* stop sequential access heuristics */
  292. goto binarySearch;
  293. }
  294. /* (index == p->header.nextindex);
  295. * miss: key entry does not exist in
  296. * the target leaf/tree
  297. */
  298. *cmpp = 1;
  299. goto out;
  300. }
  301. /*
  302. * if hit, return index of the entry found, and
  303. * if miss, where new entry with search key is
  304. * to be inserted;
  305. */
  306. out:
  307. /* compute number of pages to split */
  308. if (flag & XT_INSERT) {
  309. if (p->header.nextindex == /* little-endian */
  310. p->header.maxentry)
  311. nsplit++;
  312. else
  313. nsplit = 0;
  314. btstack->nsplit = nsplit;
  315. }
  316. /* save search result */
  317. btsp = btstack->top;
  318. btsp->bn = bn;
  319. btsp->index = index;
  320. btsp->mp = mp;
  321. /* update sequential access heuristics */
  322. jfs_ip->btindex = index;
  323. if (nextp)
  324. *nextp = next;
  325. INCREMENT(xtStat.fastSearch);
  326. return 0;
  327. }
  328. /* well, ... full search now */
  329. binarySearch:
  330. lim = le16_to_cpu(p->header.nextindex) - XTENTRYSTART;
  331. /*
  332. * binary search with search key K on the current page
  333. */
  334. for (base = XTENTRYSTART; lim; lim >>= 1) {
  335. index = base + (lim >> 1);
  336. XT_CMP(cmp, xoff, &p->xad[index], t64);
  337. if (cmp == 0) {
  338. /*
  339. * search hit
  340. */
  341. /* search hit - leaf page:
  342. * return the entry found
  343. */
  344. if (p->header.flag & BT_LEAF) {
  345. *cmpp = cmp;
  346. /* compute number of pages to split */
  347. if (flag & XT_INSERT) {
  348. if (p->header.nextindex ==
  349. p->header.maxentry)
  350. nsplit++;
  351. else
  352. nsplit = 0;
  353. btstack->nsplit = nsplit;
  354. }
  355. /* save search result */
  356. btsp = btstack->top;
  357. btsp->bn = bn;
  358. btsp->index = index;
  359. btsp->mp = mp;
  360. /* init sequential access heuristics */
  361. btindex = jfs_ip->btindex;
  362. if (index == btindex ||
  363. index == btindex + 1)
  364. jfs_ip->btorder = BT_SEQUENTIAL;
  365. else
  366. jfs_ip->btorder = BT_RANDOM;
  367. jfs_ip->btindex = index;
  368. return 0;
  369. }
  370. /* search hit - internal page:
  371. * descend/search its child page
  372. */
  373. if (index < le16_to_cpu(p->header.nextindex)-1)
  374. next = offsetXAD(&p->xad[index + 1]);
  375. goto next;
  376. }
  377. if (cmp > 0) {
  378. base = index + 1;
  379. --lim;
  380. }
  381. }
  382. /*
  383. * search miss
  384. *
  385. * base is the smallest index with key (Kj) greater than
  386. * search key (K) and may be zero or maxentry index.
  387. */
  388. if (base < le16_to_cpu(p->header.nextindex))
  389. next = offsetXAD(&p->xad[base]);
  390. /*
  391. * search miss - leaf page:
  392. *
  393. * return location of entry (base) where new entry with
  394. * search key K is to be inserted.
  395. */
  396. if (p->header.flag & BT_LEAF) {
  397. *cmpp = cmp;
  398. /* compute number of pages to split */
  399. if (flag & XT_INSERT) {
  400. if (p->header.nextindex ==
  401. p->header.maxentry)
  402. nsplit++;
  403. else
  404. nsplit = 0;
  405. btstack->nsplit = nsplit;
  406. }
  407. /* save search result */
  408. btsp = btstack->top;
  409. btsp->bn = bn;
  410. btsp->index = base;
  411. btsp->mp = mp;
  412. /* init sequential access heuristics */
  413. btindex = jfs_ip->btindex;
  414. if (base == btindex || base == btindex + 1)
  415. jfs_ip->btorder = BT_SEQUENTIAL;
  416. else
  417. jfs_ip->btorder = BT_RANDOM;
  418. jfs_ip->btindex = base;
  419. if (nextp)
  420. *nextp = next;
  421. return 0;
  422. }
  423. /*
  424. * search miss - non-leaf page:
  425. *
  426. * if base is non-zero, decrement base by one to get the parent
  427. * entry of the child page to search.
  428. */
  429. index = base ? base - 1 : base;
  430. /*
  431. * go down to child page
  432. */
  433. next:
  434. /* update number of pages to split */
  435. if (p->header.nextindex == p->header.maxentry)
  436. nsplit++;
  437. else
  438. nsplit = 0;
  439. /* push (bn, index) of the parent page/entry */
  440. if (BT_STACK_FULL(btstack)) {
  441. jfs_error(ip->i_sb, "stack overrun in xtSearch!");
  442. XT_PUTPAGE(mp);
  443. return -EIO;
  444. }
  445. BT_PUSH(btstack, bn, index);
  446. /* get the child page block number */
  447. bn = addressXAD(&p->xad[index]);
  448. /* unpin the parent page */
  449. XT_PUTPAGE(mp);
  450. }
  451. }
  452. /*
  453. * xtInsert()
  454. *
  455. * function:
  456. *
  457. * parameter:
  458. * tid - transaction id;
  459. * ip - file object;
  460. * xflag - extent flag (XAD_NOTRECORDED):
  461. * xoff - extent offset;
  462. * xlen - extent length;
  463. * xaddrp - extent address pointer (in/out):
  464. * if (*xaddrp)
  465. * caller allocated data extent at *xaddrp;
  466. * else
  467. * allocate data extent and return its xaddr;
  468. * flag -
  469. *
  470. * return:
  471. */
  472. int xtInsert(tid_t tid, /* transaction id */
  473. struct inode *ip, int xflag, s64 xoff, s32 xlen, s64 * xaddrp,
  474. int flag)
  475. {
  476. int rc = 0;
  477. s64 xaddr, hint;
  478. struct metapage *mp; /* meta-page buffer */
  479. xtpage_t *p; /* base B+-tree index page */
  480. s64 bn;
  481. int index, nextindex;
  482. struct btstack btstack; /* traverse stack */
  483. struct xtsplit split; /* split information */
  484. xad_t *xad;
  485. int cmp;
  486. s64 next;
  487. struct tlock *tlck;
  488. struct xtlock *xtlck;
  489. jfs_info("xtInsert: nxoff:0x%lx nxlen:0x%x", (ulong) xoff, xlen);
  490. /*
  491. * search for the entry location at which to insert:
  492. *
  493. * xtFastSearch() and xtSearch() both returns (leaf page
  494. * pinned, index at which to insert).
  495. * n.b. xtSearch() may return index of maxentry of
  496. * the full page.
  497. */
  498. if ((rc = xtSearch(ip, xoff, &next, &cmp, &btstack, XT_INSERT)))
  499. return rc;
  500. /* retrieve search result */
  501. XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
  502. /* This test must follow XT_GETSEARCH since mp must be valid if
  503. * we branch to out: */
  504. if ((cmp == 0) || (next && (xlen > next - xoff))) {
  505. rc = -EEXIST;
  506. goto out;
  507. }
  508. /*
  509. * allocate data extent requested
  510. *
  511. * allocation hint: last xad
  512. */
  513. if ((xaddr = *xaddrp) == 0) {
  514. if (index > XTENTRYSTART) {
  515. xad = &p->xad[index - 1];
  516. hint = addressXAD(xad) + lengthXAD(xad) - 1;
  517. } else
  518. hint = 0;
  519. if ((rc = dquot_alloc_block(ip, xlen)))
  520. goto out;
  521. if ((rc = dbAlloc(ip, hint, (s64) xlen, &xaddr))) {
  522. dquot_free_block(ip, xlen);
  523. goto out;
  524. }
  525. }
  526. /*
  527. * insert entry for new extent
  528. */
  529. xflag |= XAD_NEW;
  530. /*
  531. * if the leaf page is full, split the page and
  532. * propagate up the router entry for the new page from split
  533. *
  534. * The xtSplitUp() will insert the entry and unpin the leaf page.
  535. */
  536. nextindex = le16_to_cpu(p->header.nextindex);
  537. if (nextindex == le16_to_cpu(p->header.maxentry)) {
  538. split.mp = mp;
  539. split.index = index;
  540. split.flag = xflag;
  541. split.off = xoff;
  542. split.len = xlen;
  543. split.addr = xaddr;
  544. split.pxdlist = NULL;
  545. if ((rc = xtSplitUp(tid, ip, &split, &btstack))) {
  546. /* undo data extent allocation */
  547. if (*xaddrp == 0) {
  548. dbFree(ip, xaddr, (s64) xlen);
  549. dquot_free_block(ip, xlen);
  550. }
  551. return rc;
  552. }
  553. *xaddrp = xaddr;
  554. return 0;
  555. }
  556. /*
  557. * insert the new entry into the leaf page
  558. */
  559. /*
  560. * acquire a transaction lock on the leaf page;
  561. *
  562. * action: xad insertion/extension;
  563. */
  564. BT_MARK_DIRTY(mp, ip);
  565. /* if insert into middle, shift right remaining entries. */
  566. if (index < nextindex)
  567. memmove(&p->xad[index + 1], &p->xad[index],
  568. (nextindex - index) * sizeof(xad_t));
  569. /* insert the new entry: mark the entry NEW */
  570. xad = &p->xad[index];
  571. XT_PUTENTRY(xad, xflag, xoff, xlen, xaddr);
  572. /* advance next available entry index */
  573. le16_add_cpu(&p->header.nextindex, 1);
  574. /* Don't log it if there are no links to the file */
  575. if (!test_cflag(COMMIT_Nolink, ip)) {
  576. tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
  577. xtlck = (struct xtlock *) & tlck->lock;
  578. xtlck->lwm.offset =
  579. (xtlck->lwm.offset) ? min(index,
  580. (int)xtlck->lwm.offset) : index;
  581. xtlck->lwm.length =
  582. le16_to_cpu(p->header.nextindex) - xtlck->lwm.offset;
  583. }
  584. *xaddrp = xaddr;
  585. out:
  586. /* unpin the leaf page */
  587. XT_PUTPAGE(mp);
  588. return rc;
  589. }
  590. /*
  591. * xtSplitUp()
  592. *
  593. * function:
  594. * split full pages as propagating insertion up the tree
  595. *
  596. * parameter:
  597. * tid - transaction id;
  598. * ip - file object;
  599. * split - entry parameter descriptor;
  600. * btstack - traverse stack from xtSearch()
  601. *
  602. * return:
  603. */
  604. static int
  605. xtSplitUp(tid_t tid,
  606. struct inode *ip, struct xtsplit * split, struct btstack * btstack)
  607. {
  608. int rc = 0;
  609. struct metapage *smp;
  610. xtpage_t *sp; /* split page */
  611. struct metapage *rmp;
  612. s64 rbn; /* new right page block number */
  613. struct metapage *rcmp;
  614. xtpage_t *rcp; /* right child page */
  615. s64 rcbn; /* right child page block number */
  616. int skip; /* index of entry of insertion */
  617. int nextindex; /* next available entry index of p */
  618. struct btframe *parent; /* parent page entry on traverse stack */
  619. xad_t *xad;
  620. s64 xaddr;
  621. int xlen;
  622. int nsplit; /* number of pages split */
  623. struct pxdlist pxdlist;
  624. pxd_t *pxd;
  625. struct tlock *tlck;
  626. struct xtlock *xtlck;
  627. smp = split->mp;
  628. sp = XT_PAGE(ip, smp);
  629. /* is inode xtree root extension/inline EA area free ? */
  630. if ((sp->header.flag & BT_ROOT) && (!S_ISDIR(ip->i_mode)) &&
  631. (le16_to_cpu(sp->header.maxentry) < XTROOTMAXSLOT) &&
  632. (JFS_IP(ip)->mode2 & INLINEEA)) {
  633. sp->header.maxentry = cpu_to_le16(XTROOTMAXSLOT);
  634. JFS_IP(ip)->mode2 &= ~INLINEEA;
  635. BT_MARK_DIRTY(smp, ip);
  636. /*
  637. * acquire a transaction lock on the leaf page;
  638. *
  639. * action: xad insertion/extension;
  640. */
  641. /* if insert into middle, shift right remaining entries. */
  642. skip = split->index;
  643. nextindex = le16_to_cpu(sp->header.nextindex);
  644. if (skip < nextindex)
  645. memmove(&sp->xad[skip + 1], &sp->xad[skip],
  646. (nextindex - skip) * sizeof(xad_t));
  647. /* insert the new entry: mark the entry NEW */
  648. xad = &sp->xad[skip];
  649. XT_PUTENTRY(xad, split->flag, split->off, split->len,
  650. split->addr);
  651. /* advance next available entry index */
  652. le16_add_cpu(&sp->header.nextindex, 1);
  653. /* Don't log it if there are no links to the file */
  654. if (!test_cflag(COMMIT_Nolink, ip)) {
  655. tlck = txLock(tid, ip, smp, tlckXTREE | tlckGROW);
  656. xtlck = (struct xtlock *) & tlck->lock;
  657. xtlck->lwm.offset = (xtlck->lwm.offset) ?
  658. min(skip, (int)xtlck->lwm.offset) : skip;
  659. xtlck->lwm.length =
  660. le16_to_cpu(sp->header.nextindex) -
  661. xtlck->lwm.offset;
  662. }
  663. return 0;
  664. }
  665. /*
  666. * allocate new index blocks to cover index page split(s)
  667. *
  668. * allocation hint: ?
  669. */
  670. if (split->pxdlist == NULL) {
  671. nsplit = btstack->nsplit;
  672. split->pxdlist = &pxdlist;
  673. pxdlist.maxnpxd = pxdlist.npxd = 0;
  674. pxd = &pxdlist.pxd[0];
  675. xlen = JFS_SBI(ip->i_sb)->nbperpage;
  676. for (; nsplit > 0; nsplit--, pxd++) {
  677. if ((rc = dbAlloc(ip, (s64) 0, (s64) xlen, &xaddr))
  678. == 0) {
  679. PXDaddress(pxd, xaddr);
  680. PXDlength(pxd, xlen);
  681. pxdlist.maxnpxd++;
  682. continue;
  683. }
  684. /* undo allocation */
  685. XT_PUTPAGE(smp);
  686. return rc;
  687. }
  688. }
  689. /*
  690. * Split leaf page <sp> into <sp> and a new right page <rp>.
  691. *
  692. * The split routines insert the new entry into the leaf page,
  693. * and acquire txLock as appropriate.
  694. * return <rp> pinned and its block number <rpbn>.
  695. */
  696. rc = (sp->header.flag & BT_ROOT) ?
  697. xtSplitRoot(tid, ip, split, &rmp) :
  698. xtSplitPage(tid, ip, split, &rmp, &rbn);
  699. XT_PUTPAGE(smp);
  700. if (rc)
  701. return -EIO;
  702. /*
  703. * propagate up the router entry for the leaf page just split
  704. *
  705. * insert a router entry for the new page into the parent page,
  706. * propagate the insert/split up the tree by walking back the stack
  707. * of (bn of parent page, index of child page entry in parent page)
  708. * that were traversed during the search for the page that split.
  709. *
  710. * the propagation of insert/split up the tree stops if the root
  711. * splits or the page inserted into doesn't have to split to hold
  712. * the new entry.
  713. *
  714. * the parent entry for the split page remains the same, and
  715. * a new entry is inserted at its right with the first key and
  716. * block number of the new right page.
  717. *
  718. * There are a maximum of 3 pages pinned at any time:
  719. * right child, left parent and right parent (when the parent splits)
  720. * to keep the child page pinned while working on the parent.
  721. * make sure that all pins are released at exit.
  722. */
  723. while ((parent = BT_POP(btstack)) != NULL) {
  724. /* parent page specified by stack frame <parent> */
  725. /* keep current child pages <rcp> pinned */
  726. rcmp = rmp;
  727. rcbn = rbn;
  728. rcp = XT_PAGE(ip, rcmp);
  729. /*
  730. * insert router entry in parent for new right child page <rp>
  731. */
  732. /* get/pin the parent page <sp> */
  733. XT_GETPAGE(ip, parent->bn, smp, PSIZE, sp, rc);
  734. if (rc) {
  735. XT_PUTPAGE(rcmp);
  736. return rc;
  737. }
  738. /*
  739. * The new key entry goes ONE AFTER the index of parent entry,
  740. * because the split was to the right.
  741. */
  742. skip = parent->index + 1;
  743. /*
  744. * split or shift right remaining entries of the parent page
  745. */
  746. nextindex = le16_to_cpu(sp->header.nextindex);
  747. /*
  748. * parent page is full - split the parent page
  749. */
  750. if (nextindex == le16_to_cpu(sp->header.maxentry)) {
  751. /* init for parent page split */
  752. split->mp = smp;
  753. split->index = skip; /* index at insert */
  754. split->flag = XAD_NEW;
  755. split->off = offsetXAD(&rcp->xad[XTENTRYSTART]);
  756. split->len = JFS_SBI(ip->i_sb)->nbperpage;
  757. split->addr = rcbn;
  758. /* unpin previous right child page */
  759. XT_PUTPAGE(rcmp);
  760. /* The split routines insert the new entry,
  761. * and acquire txLock as appropriate.
  762. * return <rp> pinned and its block number <rpbn>.
  763. */
  764. rc = (sp->header.flag & BT_ROOT) ?
  765. xtSplitRoot(tid, ip, split, &rmp) :
  766. xtSplitPage(tid, ip, split, &rmp, &rbn);
  767. if (rc) {
  768. XT_PUTPAGE(smp);
  769. return rc;
  770. }
  771. XT_PUTPAGE(smp);
  772. /* keep new child page <rp> pinned */
  773. }
  774. /*
  775. * parent page is not full - insert in parent page
  776. */
  777. else {
  778. /*
  779. * insert router entry in parent for the right child
  780. * page from the first entry of the right child page:
  781. */
  782. /*
  783. * acquire a transaction lock on the parent page;
  784. *
  785. * action: router xad insertion;
  786. */
  787. BT_MARK_DIRTY(smp, ip);
  788. /*
  789. * if insert into middle, shift right remaining entries
  790. */
  791. if (skip < nextindex)
  792. memmove(&sp->xad[skip + 1], &sp->xad[skip],
  793. (nextindex -
  794. skip) << L2XTSLOTSIZE);
  795. /* insert the router entry */
  796. xad = &sp->xad[skip];
  797. XT_PUTENTRY(xad, XAD_NEW,
  798. offsetXAD(&rcp->xad[XTENTRYSTART]),
  799. JFS_SBI(ip->i_sb)->nbperpage, rcbn);
  800. /* advance next available entry index. */
  801. le16_add_cpu(&sp->header.nextindex, 1);
  802. /* Don't log it if there are no links to the file */
  803. if (!test_cflag(COMMIT_Nolink, ip)) {
  804. tlck = txLock(tid, ip, smp,
  805. tlckXTREE | tlckGROW);
  806. xtlck = (struct xtlock *) & tlck->lock;
  807. xtlck->lwm.offset = (xtlck->lwm.offset) ?
  808. min(skip, (int)xtlck->lwm.offset) : skip;
  809. xtlck->lwm.length =
  810. le16_to_cpu(sp->header.nextindex) -
  811. xtlck->lwm.offset;
  812. }
  813. /* unpin parent page */
  814. XT_PUTPAGE(smp);
  815. /* exit propagate up */
  816. break;
  817. }
  818. }
  819. /* unpin current right page */
  820. XT_PUTPAGE(rmp);
  821. return 0;
  822. }
  823. /*
  824. * xtSplitPage()
  825. *
  826. * function:
  827. * split a full non-root page into
  828. * original/split/left page and new right page
  829. * i.e., the original/split page remains as left page.
  830. *
  831. * parameter:
  832. * int tid,
  833. * struct inode *ip,
  834. * struct xtsplit *split,
  835. * struct metapage **rmpp,
  836. * u64 *rbnp,
  837. *
  838. * return:
  839. * Pointer to page in which to insert or NULL on error.
  840. */
  841. static int
  842. xtSplitPage(tid_t tid, struct inode *ip,
  843. struct xtsplit * split, struct metapage ** rmpp, s64 * rbnp)
  844. {
  845. int rc = 0;
  846. struct metapage *smp;
  847. xtpage_t *sp;
  848. struct metapage *rmp;
  849. xtpage_t *rp; /* new right page allocated */
  850. s64 rbn; /* new right page block number */
  851. struct metapage *mp;
  852. xtpage_t *p;
  853. s64 nextbn;
  854. int skip, maxentry, middle, righthalf, n;
  855. xad_t *xad;
  856. struct pxdlist *pxdlist;
  857. pxd_t *pxd;
  858. struct tlock *tlck;
  859. struct xtlock *sxtlck = NULL, *rxtlck = NULL;
  860. int quota_allocation = 0;
  861. smp = split->mp;
  862. sp = XT_PAGE(ip, smp);
  863. INCREMENT(xtStat.split);
  864. pxdlist = split->pxdlist;
  865. pxd = &pxdlist->pxd[pxdlist->npxd];
  866. pxdlist->npxd++;
  867. rbn = addressPXD(pxd);
  868. /* Allocate blocks to quota. */
  869. rc = dquot_alloc_block(ip, lengthPXD(pxd));
  870. if (rc)
  871. goto clean_up;
  872. quota_allocation += lengthPXD(pxd);
  873. /*
  874. * allocate the new right page for the split
  875. */
  876. rmp = get_metapage(ip, rbn, PSIZE, 1);
  877. if (rmp == NULL) {
  878. rc = -EIO;
  879. goto clean_up;
  880. }
  881. jfs_info("xtSplitPage: ip:0x%p smp:0x%p rmp:0x%p", ip, smp, rmp);
  882. BT_MARK_DIRTY(rmp, ip);
  883. /*
  884. * action: new page;
  885. */
  886. rp = (xtpage_t *) rmp->data;
  887. rp->header.self = *pxd;
  888. rp->header.flag = sp->header.flag & BT_TYPE;
  889. rp->header.maxentry = sp->header.maxentry; /* little-endian */
  890. rp->header.nextindex = cpu_to_le16(XTENTRYSTART);
  891. BT_MARK_DIRTY(smp, ip);
  892. /* Don't log it if there are no links to the file */
  893. if (!test_cflag(COMMIT_Nolink, ip)) {
  894. /*
  895. * acquire a transaction lock on the new right page;
  896. */
  897. tlck = txLock(tid, ip, rmp, tlckXTREE | tlckNEW);
  898. rxtlck = (struct xtlock *) & tlck->lock;
  899. rxtlck->lwm.offset = XTENTRYSTART;
  900. /*
  901. * acquire a transaction lock on the split page
  902. */
  903. tlck = txLock(tid, ip, smp, tlckXTREE | tlckGROW);
  904. sxtlck = (struct xtlock *) & tlck->lock;
  905. }
  906. /*
  907. * initialize/update sibling pointers of <sp> and <rp>
  908. */
  909. nextbn = le64_to_cpu(sp->header.next);
  910. rp->header.next = cpu_to_le64(nextbn);
  911. rp->header.prev = cpu_to_le64(addressPXD(&sp->header.self));
  912. sp->header.next = cpu_to_le64(rbn);
  913. skip = split->index;
  914. /*
  915. * sequential append at tail (after last entry of last page)
  916. *
  917. * if splitting the last page on a level because of appending
  918. * a entry to it (skip is maxentry), it's likely that the access is
  919. * sequential. adding an empty page on the side of the level is less
  920. * work and can push the fill factor much higher than normal.
  921. * if we're wrong it's no big deal - we will do the split the right
  922. * way next time.
  923. * (it may look like it's equally easy to do a similar hack for
  924. * reverse sorted data, that is, split the tree left, but it's not.
  925. * Be my guest.)
  926. */
  927. if (nextbn == 0 && skip == le16_to_cpu(sp->header.maxentry)) {
  928. /*
  929. * acquire a transaction lock on the new/right page;
  930. *
  931. * action: xad insertion;
  932. */
  933. /* insert entry at the first entry of the new right page */
  934. xad = &rp->xad[XTENTRYSTART];
  935. XT_PUTENTRY(xad, split->flag, split->off, split->len,
  936. split->addr);
  937. rp->header.nextindex = cpu_to_le16(XTENTRYSTART + 1);
  938. if (!test_cflag(COMMIT_Nolink, ip)) {
  939. /* rxtlck->lwm.offset = XTENTRYSTART; */
  940. rxtlck->lwm.length = 1;
  941. }
  942. *rmpp = rmp;
  943. *rbnp = rbn;
  944. jfs_info("xtSplitPage: sp:0x%p rp:0x%p", sp, rp);
  945. return 0;
  946. }
  947. /*
  948. * non-sequential insert (at possibly middle page)
  949. */
  950. /*
  951. * update previous pointer of old next/right page of <sp>
  952. */
  953. if (nextbn != 0) {
  954. XT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
  955. if (rc) {
  956. XT_PUTPAGE(rmp);
  957. goto clean_up;
  958. }
  959. BT_MARK_DIRTY(mp, ip);
  960. /*
  961. * acquire a transaction lock on the next page;
  962. *
  963. * action:sibling pointer update;
  964. */
  965. if (!test_cflag(COMMIT_Nolink, ip))
  966. tlck = txLock(tid, ip, mp, tlckXTREE | tlckRELINK);
  967. p->header.prev = cpu_to_le64(rbn);
  968. /* sibling page may have been updated previously, or
  969. * it may be updated later;
  970. */
  971. XT_PUTPAGE(mp);
  972. }
  973. /*
  974. * split the data between the split and new/right pages
  975. */
  976. maxentry = le16_to_cpu(sp->header.maxentry);
  977. middle = maxentry >> 1;
  978. righthalf = maxentry - middle;
  979. /*
  980. * skip index in old split/left page - insert into left page:
  981. */
  982. if (skip <= middle) {
  983. /* move right half of split page to the new right page */
  984. memmove(&rp->xad[XTENTRYSTART], &sp->xad[middle],
  985. righthalf << L2XTSLOTSIZE);
  986. /* shift right tail of left half to make room for new entry */
  987. if (skip < middle)
  988. memmove(&sp->xad[skip + 1], &sp->xad[skip],
  989. (middle - skip) << L2XTSLOTSIZE);
  990. /* insert new entry */
  991. xad = &sp->xad[skip];
  992. XT_PUTENTRY(xad, split->flag, split->off, split->len,
  993. split->addr);
  994. /* update page header */
  995. sp->header.nextindex = cpu_to_le16(middle + 1);
  996. if (!test_cflag(COMMIT_Nolink, ip)) {
  997. sxtlck->lwm.offset = (sxtlck->lwm.offset) ?
  998. min(skip, (int)sxtlck->lwm.offset) : skip;
  999. }
  1000. rp->header.nextindex =
  1001. cpu_to_le16(XTENTRYSTART + righthalf);
  1002. }
  1003. /*
  1004. * skip index in new right page - insert into right page:
  1005. */
  1006. else {
  1007. /* move left head of right half to right page */
  1008. n = skip - middle;
  1009. memmove(&rp->xad[XTENTRYSTART], &sp->xad[middle],
  1010. n << L2XTSLOTSIZE);
  1011. /* insert new entry */
  1012. n += XTENTRYSTART;
  1013. xad = &rp->xad[n];
  1014. XT_PUTENTRY(xad, split->flag, split->off, split->len,
  1015. split->addr);
  1016. /* move right tail of right half to right page */
  1017. if (skip < maxentry)
  1018. memmove(&rp->xad[n + 1], &sp->xad[skip],
  1019. (maxentry - skip) << L2XTSLOTSIZE);
  1020. /* update page header */
  1021. sp->header.nextindex = cpu_to_le16(middle);
  1022. if (!test_cflag(COMMIT_Nolink, ip)) {
  1023. sxtlck->lwm.offset = (sxtlck->lwm.offset) ?
  1024. min(middle, (int)sxtlck->lwm.offset) : middle;
  1025. }
  1026. rp->header.nextindex = cpu_to_le16(XTENTRYSTART +
  1027. righthalf + 1);
  1028. }
  1029. if (!test_cflag(COMMIT_Nolink, ip)) {
  1030. sxtlck->lwm.length = le16_to_cpu(sp->header.nextindex) -
  1031. sxtlck->lwm.offset;
  1032. /* rxtlck->lwm.offset = XTENTRYSTART; */
  1033. rxtlck->lwm.length = le16_to_cpu(rp->header.nextindex) -
  1034. XTENTRYSTART;
  1035. }
  1036. *rmpp = rmp;
  1037. *rbnp = rbn;
  1038. jfs_info("xtSplitPage: sp:0x%p rp:0x%p", sp, rp);
  1039. return rc;
  1040. clean_up:
  1041. /* Rollback quota allocation. */
  1042. if (quota_allocation)
  1043. dquot_free_block(ip, quota_allocation);
  1044. return (rc);
  1045. }
  1046. /*
  1047. * xtSplitRoot()
  1048. *
  1049. * function:
  1050. * split the full root page into original/root/split page and new
  1051. * right page
  1052. * i.e., root remains fixed in tree anchor (inode) and the root is
  1053. * copied to a single new right child page since root page <<
  1054. * non-root page, and the split root page contains a single entry
  1055. * for the new right child page.
  1056. *
  1057. * parameter:
  1058. * int tid,
  1059. * struct inode *ip,
  1060. * struct xtsplit *split,
  1061. * struct metapage **rmpp)
  1062. *
  1063. * return:
  1064. * Pointer to page in which to insert or NULL on error.
  1065. */
  1066. static int
  1067. xtSplitRoot(tid_t tid,
  1068. struct inode *ip, struct xtsplit * split, struct metapage ** rmpp)
  1069. {
  1070. xtpage_t *sp;
  1071. struct metapage *rmp;
  1072. xtpage_t *rp;
  1073. s64 rbn;
  1074. int skip, nextindex;
  1075. xad_t *xad;
  1076. pxd_t *pxd;
  1077. struct pxdlist *pxdlist;
  1078. struct tlock *tlck;
  1079. struct xtlock *xtlck;
  1080. int rc;
  1081. sp = &JFS_IP(ip)->i_xtroot;
  1082. INCREMENT(xtStat.split);
  1083. /*
  1084. * allocate a single (right) child page
  1085. */
  1086. pxdlist = split->pxdlist;
  1087. pxd = &pxdlist->pxd[pxdlist->npxd];
  1088. pxdlist->npxd++;
  1089. rbn = addressPXD(pxd);
  1090. rmp = get_metapage(ip, rbn, PSIZE, 1);
  1091. if (rmp == NULL)
  1092. return -EIO;
  1093. /* Allocate blocks to quota. */
  1094. rc = dquot_alloc_block(ip, lengthPXD(pxd));
  1095. if (rc) {
  1096. release_metapage(rmp);
  1097. return rc;
  1098. }
  1099. jfs_info("xtSplitRoot: ip:0x%p rmp:0x%p", ip, rmp);
  1100. /*
  1101. * acquire a transaction lock on the new right page;
  1102. *
  1103. * action: new page;
  1104. */
  1105. BT_MARK_DIRTY(rmp, ip);
  1106. rp = (xtpage_t *) rmp->data;
  1107. rp->header.flag =
  1108. (sp->header.flag & BT_LEAF) ? BT_LEAF : BT_INTERNAL;
  1109. rp->header.self = *pxd;
  1110. rp->header.nextindex = cpu_to_le16(XTENTRYSTART);
  1111. rp->header.maxentry = cpu_to_le16(PSIZE >> L2XTSLOTSIZE);
  1112. /* initialize sibling pointers */
  1113. rp->header.next = 0;
  1114. rp->header.prev = 0;
  1115. /*
  1116. * copy the in-line root page into new right page extent
  1117. */
  1118. nextindex = le16_to_cpu(sp->header.maxentry);
  1119. memmove(&rp->xad[XTENTRYSTART], &sp->xad[XTENTRYSTART],
  1120. (nextindex - XTENTRYSTART) << L2XTSLOTSIZE);
  1121. /*
  1122. * insert the new entry into the new right/child page
  1123. * (skip index in the new right page will not change)
  1124. */
  1125. skip = split->index;
  1126. /* if insert into middle, shift right remaining entries */
  1127. if (skip != nextindex)
  1128. memmove(&rp->xad[skip + 1], &rp->xad[skip],
  1129. (nextindex - skip) * sizeof(xad_t));
  1130. xad = &rp->xad[skip];
  1131. XT_PUTENTRY(xad, split->flag, split->off, split->len, split->addr);
  1132. /* update page header */
  1133. rp->header.nextindex = cpu_to_le16(nextindex + 1);
  1134. if (!test_cflag(COMMIT_Nolink, ip)) {
  1135. tlck = txLock(tid, ip, rmp, tlckXTREE | tlckNEW);
  1136. xtlck = (struct xtlock *) & tlck->lock;
  1137. xtlck->lwm.offset = XTENTRYSTART;
  1138. xtlck->lwm.length = le16_to_cpu(rp->header.nextindex) -
  1139. XTENTRYSTART;
  1140. }
  1141. /*
  1142. * reset the root
  1143. *
  1144. * init root with the single entry for the new right page
  1145. * set the 1st entry offset to 0, which force the left-most key
  1146. * at any level of the tree to be less than any search key.
  1147. */
  1148. /*
  1149. * acquire a transaction lock on the root page (in-memory inode);
  1150. *
  1151. * action: root split;
  1152. */
  1153. BT_MARK_DIRTY(split->mp, ip);
  1154. xad = &sp->xad[XTENTRYSTART];
  1155. XT_PUTENTRY(xad, XAD_NEW, 0, JFS_SBI(ip->i_sb)->nbperpage, rbn);
  1156. /* update page header of root */
  1157. sp->header.flag &= ~BT_LEAF;
  1158. sp->header.flag |= BT_INTERNAL;
  1159. sp->header.nextindex = cpu_to_le16(XTENTRYSTART + 1);
  1160. if (!test_cflag(COMMIT_Nolink, ip)) {
  1161. tlck = txLock(tid, ip, split->mp, tlckXTREE | tlckGROW);
  1162. xtlck = (struct xtlock *) & tlck->lock;
  1163. xtlck->lwm.offset = XTENTRYSTART;
  1164. xtlck->lwm.length = 1;
  1165. }
  1166. *rmpp = rmp;
  1167. jfs_info("xtSplitRoot: sp:0x%p rp:0x%p", sp, rp);
  1168. return 0;
  1169. }
  1170. /*
  1171. * xtExtend()
  1172. *
  1173. * function: extend in-place;
  1174. *
  1175. * note: existing extent may or may not have been committed.
  1176. * caller is responsible for pager buffer cache update, and
  1177. * working block allocation map update;
  1178. * update pmap: alloc whole extended extent;
  1179. */
  1180. int xtExtend(tid_t tid, /* transaction id */
  1181. struct inode *ip, s64 xoff, /* delta extent offset */
  1182. s32 xlen, /* delta extent length */
  1183. int flag)
  1184. {
  1185. int rc = 0;
  1186. int cmp;
  1187. struct metapage *mp; /* meta-page buffer */
  1188. xtpage_t *p; /* base B+-tree index page */
  1189. s64 bn;
  1190. int index, nextindex, len;
  1191. struct btstack btstack; /* traverse stack */
  1192. struct xtsplit split; /* split information */
  1193. xad_t *xad;
  1194. s64 xaddr;
  1195. struct tlock *tlck;
  1196. struct xtlock *xtlck = NULL;
  1197. jfs_info("xtExtend: nxoff:0x%lx nxlen:0x%x", (ulong) xoff, xlen);
  1198. /* there must exist extent to be extended */
  1199. if ((rc = xtSearch(ip, xoff - 1, NULL, &cmp, &btstack, XT_INSERT)))
  1200. return rc;
  1201. /* retrieve search result */
  1202. XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
  1203. if (cmp != 0) {
  1204. XT_PUTPAGE(mp);
  1205. jfs_error(ip->i_sb, "xtExtend: xtSearch did not find extent");
  1206. return -EIO;
  1207. }
  1208. /* extension must be contiguous */
  1209. xad = &p->xad[index];
  1210. if ((offsetXAD(xad) + lengthXAD(xad)) != xoff) {
  1211. XT_PUTPAGE(mp);
  1212. jfs_error(ip->i_sb, "xtExtend: extension is not contiguous");
  1213. return -EIO;
  1214. }
  1215. /*
  1216. * acquire a transaction lock on the leaf page;
  1217. *
  1218. * action: xad insertion/extension;
  1219. */
  1220. BT_MARK_DIRTY(mp, ip);
  1221. if (!test_cflag(COMMIT_Nolink, ip)) {
  1222. tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
  1223. xtlck = (struct xtlock *) & tlck->lock;
  1224. }
  1225. /* extend will overflow extent ? */
  1226. xlen = lengthXAD(xad) + xlen;
  1227. if ((len = xlen - MAXXLEN) <= 0)
  1228. goto extendOld;
  1229. /*
  1230. * extent overflow: insert entry for new extent
  1231. */
  1232. //insertNew:
  1233. xoff = offsetXAD(xad) + MAXXLEN;
  1234. xaddr = addressXAD(xad) + MAXXLEN;
  1235. nextindex = le16_to_cpu(p->header.nextindex);
  1236. /*
  1237. * if the leaf page is full, insert the new entry and
  1238. * propagate up the router entry for the new page from split
  1239. *
  1240. * The xtSplitUp() will insert the entry and unpin the leaf page.
  1241. */
  1242. if (nextindex == le16_to_cpu(p->header.maxentry)) {
  1243. /* xtSpliUp() unpins leaf pages */
  1244. split.mp = mp;
  1245. split.index = index + 1;
  1246. split.flag = XAD_NEW;
  1247. split.off = xoff; /* split offset */
  1248. split.len = len;
  1249. split.addr = xaddr;
  1250. split.pxdlist = NULL;
  1251. if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
  1252. return rc;
  1253. /* get back old page */
  1254. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  1255. if (rc)
  1256. return rc;
  1257. /*
  1258. * if leaf root has been split, original root has been
  1259. * copied to new child page, i.e., original entry now
  1260. * resides on the new child page;
  1261. */
  1262. if (p->header.flag & BT_INTERNAL) {
  1263. ASSERT(p->header.nextindex ==
  1264. cpu_to_le16(XTENTRYSTART + 1));
  1265. xad = &p->xad[XTENTRYSTART];
  1266. bn = addressXAD(xad);
  1267. XT_PUTPAGE(mp);
  1268. /* get new child page */
  1269. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  1270. if (rc)
  1271. return rc;
  1272. BT_MARK_DIRTY(mp, ip);
  1273. if (!test_cflag(COMMIT_Nolink, ip)) {
  1274. tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
  1275. xtlck = (struct xtlock *) & tlck->lock;
  1276. }
  1277. }
  1278. }
  1279. /*
  1280. * insert the new entry into the leaf page
  1281. */
  1282. else {
  1283. /* insert the new entry: mark the entry NEW */
  1284. xad = &p->xad[index + 1];
  1285. XT_PUTENTRY(xad, XAD_NEW, xoff, len, xaddr);
  1286. /* advance next available entry index */
  1287. le16_add_cpu(&p->header.nextindex, 1);
  1288. }
  1289. /* get back old entry */
  1290. xad = &p->xad[index];
  1291. xlen = MAXXLEN;
  1292. /*
  1293. * extend old extent
  1294. */
  1295. extendOld:
  1296. XADlength(xad, xlen);
  1297. if (!(xad->flag & XAD_NEW))
  1298. xad->flag |= XAD_EXTENDED;
  1299. if (!test_cflag(COMMIT_Nolink, ip)) {
  1300. xtlck->lwm.offset =
  1301. (xtlck->lwm.offset) ? min(index,
  1302. (int)xtlck->lwm.offset) : index;
  1303. xtlck->lwm.length =
  1304. le16_to_cpu(p->header.nextindex) - xtlck->lwm.offset;
  1305. }
  1306. /* unpin the leaf page */
  1307. XT_PUTPAGE(mp);
  1308. return rc;
  1309. }
  1310. #ifdef _NOTYET
  1311. /*
  1312. * xtTailgate()
  1313. *
  1314. * function: split existing 'tail' extent
  1315. * (split offset >= start offset of tail extent), and
  1316. * relocate and extend the split tail half;
  1317. *
  1318. * note: existing extent may or may not have been committed.
  1319. * caller is responsible for pager buffer cache update, and
  1320. * working block allocation map update;
  1321. * update pmap: free old split tail extent, alloc new extent;
  1322. */
  1323. int xtTailgate(tid_t tid, /* transaction id */
  1324. struct inode *ip, s64 xoff, /* split/new extent offset */
  1325. s32 xlen, /* new extent length */
  1326. s64 xaddr, /* new extent address */
  1327. int flag)
  1328. {
  1329. int rc = 0;
  1330. int cmp;
  1331. struct metapage *mp; /* meta-page buffer */
  1332. xtpage_t *p; /* base B+-tree index page */
  1333. s64 bn;
  1334. int index, nextindex, llen, rlen;
  1335. struct btstack btstack; /* traverse stack */
  1336. struct xtsplit split; /* split information */
  1337. xad_t *xad;
  1338. struct tlock *tlck;
  1339. struct xtlock *xtlck = 0;
  1340. struct tlock *mtlck;
  1341. struct maplock *pxdlock;
  1342. /*
  1343. printf("xtTailgate: nxoff:0x%lx nxlen:0x%x nxaddr:0x%lx\n",
  1344. (ulong)xoff, xlen, (ulong)xaddr);
  1345. */
  1346. /* there must exist extent to be tailgated */
  1347. if ((rc = xtSearch(ip, xoff, NULL, &cmp, &btstack, XT_INSERT)))
  1348. return rc;
  1349. /* retrieve search result */
  1350. XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
  1351. if (cmp != 0) {
  1352. XT_PUTPAGE(mp);
  1353. jfs_error(ip->i_sb, "xtTailgate: couldn't find extent");
  1354. return -EIO;
  1355. }
  1356. /* entry found must be last entry */
  1357. nextindex = le16_to_cpu(p->header.nextindex);
  1358. if (index != nextindex - 1) {
  1359. XT_PUTPAGE(mp);
  1360. jfs_error(ip->i_sb,
  1361. "xtTailgate: the entry found is not the last entry");
  1362. return -EIO;
  1363. }
  1364. BT_MARK_DIRTY(mp, ip);
  1365. /*
  1366. * acquire tlock of the leaf page containing original entry
  1367. */
  1368. if (!test_cflag(COMMIT_Nolink, ip)) {
  1369. tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
  1370. xtlck = (struct xtlock *) & tlck->lock;
  1371. }
  1372. /* completely replace extent ? */
  1373. xad = &p->xad[index];
  1374. /*
  1375. printf("xtTailgate: xoff:0x%lx xlen:0x%x xaddr:0x%lx\n",
  1376. (ulong)offsetXAD(xad), lengthXAD(xad), (ulong)addressXAD(xad));
  1377. */
  1378. if ((llen = xoff - offsetXAD(xad)) == 0)
  1379. goto updateOld;
  1380. /*
  1381. * partially replace extent: insert entry for new extent
  1382. */
  1383. //insertNew:
  1384. /*
  1385. * if the leaf page is full, insert the new entry and
  1386. * propagate up the router entry for the new page from split
  1387. *
  1388. * The xtSplitUp() will insert the entry and unpin the leaf page.
  1389. */
  1390. if (nextindex == le16_to_cpu(p->header.maxentry)) {
  1391. /* xtSpliUp() unpins leaf pages */
  1392. split.mp = mp;
  1393. split.index = index + 1;
  1394. split.flag = XAD_NEW;
  1395. split.off = xoff; /* split offset */
  1396. split.len = xlen;
  1397. split.addr = xaddr;
  1398. split.pxdlist = NULL;
  1399. if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
  1400. return rc;
  1401. /* get back old page */
  1402. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  1403. if (rc)
  1404. return rc;
  1405. /*
  1406. * if leaf root has been split, original root has been
  1407. * copied to new child page, i.e., original entry now
  1408. * resides on the new child page;
  1409. */
  1410. if (p->header.flag & BT_INTERNAL) {
  1411. ASSERT(p->header.nextindex ==
  1412. cpu_to_le16(XTENTRYSTART + 1));
  1413. xad = &p->xad[XTENTRYSTART];
  1414. bn = addressXAD(xad);
  1415. XT_PUTPAGE(mp);
  1416. /* get new child page */
  1417. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  1418. if (rc)
  1419. return rc;
  1420. BT_MARK_DIRTY(mp, ip);
  1421. if (!test_cflag(COMMIT_Nolink, ip)) {
  1422. tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
  1423. xtlck = (struct xtlock *) & tlck->lock;
  1424. }
  1425. }
  1426. }
  1427. /*
  1428. * insert the new entry into the leaf page
  1429. */
  1430. else {
  1431. /* insert the new entry: mark the entry NEW */
  1432. xad = &p->xad[index + 1];
  1433. XT_PUTENTRY(xad, XAD_NEW, xoff, xlen, xaddr);
  1434. /* advance next available entry index */
  1435. le16_add_cpu(&p->header.nextindex, 1);
  1436. }
  1437. /* get back old XAD */
  1438. xad = &p->xad[index];
  1439. /*
  1440. * truncate/relocate old extent at split offset
  1441. */
  1442. updateOld:
  1443. /* update dmap for old/committed/truncated extent */
  1444. rlen = lengthXAD(xad) - llen;
  1445. if (!(xad->flag & XAD_NEW)) {
  1446. /* free from PWMAP at commit */
  1447. if (!test_cflag(COMMIT_Nolink, ip)) {
  1448. mtlck = txMaplock(tid, ip, tlckMAP);
  1449. pxdlock = (struct maplock *) & mtlck->lock;
  1450. pxdlock->flag = mlckFREEPXD;
  1451. PXDaddress(&pxdlock->pxd, addressXAD(xad) + llen);
  1452. PXDlength(&pxdlock->pxd, rlen);
  1453. pxdlock->index = 1;
  1454. }
  1455. } else
  1456. /* free from WMAP */
  1457. dbFree(ip, addressXAD(xad) + llen, (s64) rlen);
  1458. if (llen)
  1459. /* truncate */
  1460. XADlength(xad, llen);
  1461. else
  1462. /* replace */
  1463. XT_PUTENTRY(xad, XAD_NEW, xoff, xlen, xaddr);
  1464. if (!test_cflag(COMMIT_Nolink, ip)) {
  1465. xtlck->lwm.offset = (xtlck->lwm.offset) ?
  1466. min(index, (int)xtlck->lwm.offset) : index;
  1467. xtlck->lwm.length = le16_to_cpu(p->header.nextindex) -
  1468. xtlck->lwm.offset;
  1469. }
  1470. /* unpin the leaf page */
  1471. XT_PUTPAGE(mp);
  1472. return rc;
  1473. }
  1474. #endif /* _NOTYET */
  1475. /*
  1476. * xtUpdate()
  1477. *
  1478. * function: update XAD;
  1479. *
  1480. * update extent for allocated_but_not_recorded or
  1481. * compressed extent;
  1482. *
  1483. * parameter:
  1484. * nxad - new XAD;
  1485. * logical extent of the specified XAD must be completely
  1486. * contained by an existing XAD;
  1487. */
  1488. int xtUpdate(tid_t tid, struct inode *ip, xad_t * nxad)
  1489. { /* new XAD */
  1490. int rc = 0;
  1491. int cmp;
  1492. struct metapage *mp; /* meta-page buffer */
  1493. xtpage_t *p; /* base B+-tree index page */
  1494. s64 bn;
  1495. int index0, index, newindex, nextindex;
  1496. struct btstack btstack; /* traverse stack */
  1497. struct xtsplit split; /* split information */
  1498. xad_t *xad, *lxad, *rxad;
  1499. int xflag;
  1500. s64 nxoff, xoff;
  1501. int nxlen, xlen, lxlen, rxlen;
  1502. s64 nxaddr, xaddr;
  1503. struct tlock *tlck;
  1504. struct xtlock *xtlck = NULL;
  1505. int newpage = 0;
  1506. /* there must exist extent to be tailgated */
  1507. nxoff = offsetXAD(nxad);
  1508. nxlen = lengthXAD(nxad);
  1509. nxaddr = addressXAD(nxad);
  1510. if ((rc = xtSearch(ip, nxoff, NULL, &cmp, &btstack, XT_INSERT)))
  1511. return rc;
  1512. /* retrieve search result */
  1513. XT_GETSEARCH(ip, btstack.top, bn, mp, p, index0);
  1514. if (cmp != 0) {
  1515. XT_PUTPAGE(mp);
  1516. jfs_error(ip->i_sb, "xtUpdate: Could not find extent");
  1517. return -EIO;
  1518. }
  1519. BT_MARK_DIRTY(mp, ip);
  1520. /*
  1521. * acquire tlock of the leaf page containing original entry
  1522. */
  1523. if (!test_cflag(COMMIT_Nolink, ip)) {
  1524. tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
  1525. xtlck = (struct xtlock *) & tlck->lock;
  1526. }
  1527. xad = &p->xad[index0];
  1528. xflag = xad->flag;
  1529. xoff = offsetXAD(xad);
  1530. xlen = lengthXAD(xad);
  1531. xaddr = addressXAD(xad);
  1532. /* nXAD must be completely contained within XAD */
  1533. if ((xoff > nxoff) ||
  1534. (nxoff + nxlen > xoff + xlen)) {
  1535. XT_PUTPAGE(mp);
  1536. jfs_error(ip->i_sb,
  1537. "xtUpdate: nXAD in not completely contained within XAD");
  1538. return -EIO;
  1539. }
  1540. index = index0;
  1541. newindex = index + 1;
  1542. nextindex = le16_to_cpu(p->header.nextindex);
  1543. #ifdef _JFS_WIP_NOCOALESCE
  1544. if (xoff < nxoff)
  1545. goto updateRight;
  1546. /*
  1547. * replace XAD with nXAD
  1548. */
  1549. replace: /* (nxoff == xoff) */
  1550. if (nxlen == xlen) {
  1551. /* replace XAD with nXAD:recorded */
  1552. *xad = *nxad;
  1553. xad->flag = xflag & ~XAD_NOTRECORDED;
  1554. goto out;
  1555. } else /* (nxlen < xlen) */
  1556. goto updateLeft;
  1557. #endif /* _JFS_WIP_NOCOALESCE */
  1558. /* #ifdef _JFS_WIP_COALESCE */
  1559. if (xoff < nxoff)
  1560. goto coalesceRight;
  1561. /*
  1562. * coalesce with left XAD
  1563. */
  1564. //coalesceLeft: /* (xoff == nxoff) */
  1565. /* is XAD first entry of page ? */
  1566. if (index == XTENTRYSTART)
  1567. goto replace;
  1568. /* is nXAD logically and physically contiguous with lXAD ? */
  1569. lxad = &p->xad[index - 1];
  1570. lxlen = lengthXAD(lxad);
  1571. if (!(lxad->flag & XAD_NOTRECORDED) &&
  1572. (nxoff == offsetXAD(lxad) + lxlen) &&
  1573. (nxaddr == addressXAD(lxad) + lxlen) &&
  1574. (lxlen + nxlen < MAXXLEN)) {
  1575. /* extend right lXAD */
  1576. index0 = index - 1;
  1577. XADlength(lxad, lxlen + nxlen);
  1578. /* If we just merged two extents together, need to make sure the
  1579. * right extent gets logged. If the left one is marked XAD_NEW,
  1580. * then we know it will be logged. Otherwise, mark as
  1581. * XAD_EXTENDED
  1582. */
  1583. if (!(lxad->flag & XAD_NEW))
  1584. lxad->flag |= XAD_EXTENDED;
  1585. if (xlen > nxlen) {
  1586. /* truncate XAD */
  1587. XADoffset(xad, xoff + nxlen);
  1588. XADlength(xad, xlen - nxlen);
  1589. XADaddress(xad, xaddr + nxlen);
  1590. goto out;
  1591. } else { /* (xlen == nxlen) */
  1592. /* remove XAD */
  1593. if (index < nextindex - 1)
  1594. memmove(&p->xad[index], &p->xad[index + 1],
  1595. (nextindex - index -
  1596. 1) << L2XTSLOTSIZE);
  1597. p->header.nextindex =
  1598. cpu_to_le16(le16_to_cpu(p->header.nextindex) -
  1599. 1);
  1600. index = index0;
  1601. newindex = index + 1;
  1602. nextindex = le16_to_cpu(p->header.nextindex);
  1603. xoff = nxoff = offsetXAD(lxad);
  1604. xlen = nxlen = lxlen + nxlen;
  1605. xaddr = nxaddr = addressXAD(lxad);
  1606. goto coalesceRight;
  1607. }
  1608. }
  1609. /*
  1610. * replace XAD with nXAD
  1611. */
  1612. replace: /* (nxoff == xoff) */
  1613. if (nxlen == xlen) {
  1614. /* replace XAD with nXAD:recorded */
  1615. *xad = *nxad;
  1616. xad->flag = xflag & ~XAD_NOTRECORDED;
  1617. goto coalesceRight;
  1618. } else /* (nxlen < xlen) */
  1619. goto updateLeft;
  1620. /*
  1621. * coalesce with right XAD
  1622. */
  1623. coalesceRight: /* (xoff <= nxoff) */
  1624. /* is XAD last entry of page ? */
  1625. if (newindex == nextindex) {
  1626. if (xoff == nxoff)
  1627. goto out;
  1628. goto updateRight;
  1629. }
  1630. /* is nXAD logically and physically contiguous with rXAD ? */
  1631. rxad = &p->xad[index + 1];
  1632. rxlen = lengthXAD(rxad);
  1633. if (!(rxad->flag & XAD_NOTRECORDED) &&
  1634. (nxoff + nxlen == offsetXAD(rxad)) &&
  1635. (nxaddr + nxlen == addressXAD(rxad)) &&
  1636. (rxlen + nxlen < MAXXLEN)) {
  1637. /* extend left rXAD */
  1638. XADoffset(rxad, nxoff);
  1639. XADlength(rxad, rxlen + nxlen);
  1640. XADaddress(rxad, nxaddr);
  1641. /* If we just merged two extents together, need to make sure
  1642. * the left extent gets logged. If the right one is marked
  1643. * XAD_NEW, then we know it will be logged. Otherwise, mark as
  1644. * XAD_EXTENDED
  1645. */
  1646. if (!(rxad->flag & XAD_NEW))
  1647. rxad->flag |= XAD_EXTENDED;
  1648. if (xlen > nxlen)
  1649. /* truncate XAD */
  1650. XADlength(xad, xlen - nxlen);
  1651. else { /* (xlen == nxlen) */
  1652. /* remove XAD */
  1653. memmove(&p->xad[index], &p->xad[index + 1],
  1654. (nextindex - index - 1) << L2XTSLOTSIZE);
  1655. p->header.nextindex =
  1656. cpu_to_le16(le16_to_cpu(p->header.nextindex) -
  1657. 1);
  1658. }
  1659. goto out;
  1660. } else if (xoff == nxoff)
  1661. goto out;
  1662. if (xoff >= nxoff) {
  1663. XT_PUTPAGE(mp);
  1664. jfs_error(ip->i_sb, "xtUpdate: xoff >= nxoff");
  1665. return -EIO;
  1666. }
  1667. /* #endif _JFS_WIP_COALESCE */
  1668. /*
  1669. * split XAD into (lXAD, nXAD):
  1670. *
  1671. * |---nXAD--->
  1672. * --|----------XAD----------|--
  1673. * |-lXAD-|
  1674. */
  1675. updateRight: /* (xoff < nxoff) */
  1676. /* truncate old XAD as lXAD:not_recorded */
  1677. xad = &p->xad[index];
  1678. XADlength(xad, nxoff - xoff);
  1679. /* insert nXAD:recorded */
  1680. if (nextindex == le16_to_cpu(p->header.maxentry)) {
  1681. /* xtSpliUp() unpins leaf pages */
  1682. split.mp = mp;
  1683. split.index = newindex;
  1684. split.flag = xflag & ~XAD_NOTRECORDED;
  1685. split.off = nxoff;
  1686. split.len = nxlen;
  1687. split.addr = nxaddr;
  1688. split.pxdlist = NULL;
  1689. if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
  1690. return rc;
  1691. /* get back old page */
  1692. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  1693. if (rc)
  1694. return rc;
  1695. /*
  1696. * if leaf root has been split, original root has been
  1697. * copied to new child page, i.e., original entry now
  1698. * resides on the new child page;
  1699. */
  1700. if (p->header.flag & BT_INTERNAL) {
  1701. ASSERT(p->header.nextindex ==
  1702. cpu_to_le16(XTENTRYSTART + 1));
  1703. xad = &p->xad[XTENTRYSTART];
  1704. bn = addressXAD(xad);
  1705. XT_PUTPAGE(mp);
  1706. /* get new child page */
  1707. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  1708. if (rc)
  1709. return rc;
  1710. BT_MARK_DIRTY(mp, ip);
  1711. if (!test_cflag(COMMIT_Nolink, ip)) {
  1712. tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
  1713. xtlck = (struct xtlock *) & tlck->lock;
  1714. }
  1715. } else {
  1716. /* is nXAD on new page ? */
  1717. if (newindex >
  1718. (le16_to_cpu(p->header.maxentry) >> 1)) {
  1719. newindex =
  1720. newindex -
  1721. le16_to_cpu(p->header.nextindex) +
  1722. XTENTRYSTART;
  1723. newpage = 1;
  1724. }
  1725. }
  1726. } else {
  1727. /* if insert into middle, shift right remaining entries */
  1728. if (newindex < nextindex)
  1729. memmove(&p->xad[newindex + 1], &p->xad[newindex],
  1730. (nextindex - newindex) << L2XTSLOTSIZE);
  1731. /* insert the entry */
  1732. xad = &p->xad[newindex];
  1733. *xad = *nxad;
  1734. xad->flag = xflag & ~XAD_NOTRECORDED;
  1735. /* advance next available entry index. */
  1736. p->header.nextindex =
  1737. cpu_to_le16(le16_to_cpu(p->header.nextindex) + 1);
  1738. }
  1739. /*
  1740. * does nXAD force 3-way split ?
  1741. *
  1742. * |---nXAD--->|
  1743. * --|----------XAD-------------|--
  1744. * |-lXAD-| |-rXAD -|
  1745. */
  1746. if (nxoff + nxlen == xoff + xlen)
  1747. goto out;
  1748. /* reorient nXAD as XAD for further split XAD into (nXAD, rXAD) */
  1749. if (newpage) {
  1750. /* close out old page */
  1751. if (!test_cflag(COMMIT_Nolink, ip)) {
  1752. xtlck->lwm.offset = (xtlck->lwm.offset) ?
  1753. min(index0, (int)xtlck->lwm.offset) : index0;
  1754. xtlck->lwm.length =
  1755. le16_to_cpu(p->header.nextindex) -
  1756. xtlck->lwm.offset;
  1757. }
  1758. bn = le64_to_cpu(p->header.next);
  1759. XT_PUTPAGE(mp);
  1760. /* get new right page */
  1761. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  1762. if (rc)
  1763. return rc;
  1764. BT_MARK_DIRTY(mp, ip);
  1765. if (!test_cflag(COMMIT_Nolink, ip)) {
  1766. tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
  1767. xtlck = (struct xtlock *) & tlck->lock;
  1768. }
  1769. index0 = index = newindex;
  1770. } else
  1771. index++;
  1772. newindex = index + 1;
  1773. nextindex = le16_to_cpu(p->header.nextindex);
  1774. xlen = xlen - (nxoff - xoff);
  1775. xoff = nxoff;
  1776. xaddr = nxaddr;
  1777. /* recompute split pages */
  1778. if (nextindex == le16_to_cpu(p->header.maxentry)) {
  1779. XT_PUTPAGE(mp);
  1780. if ((rc = xtSearch(ip, nxoff, NULL, &cmp, &btstack, XT_INSERT)))
  1781. return rc;
  1782. /* retrieve search result */
  1783. XT_GETSEARCH(ip, btstack.top, bn, mp, p, index0);
  1784. if (cmp != 0) {
  1785. XT_PUTPAGE(mp);
  1786. jfs_error(ip->i_sb, "xtUpdate: xtSearch failed");
  1787. return -EIO;
  1788. }
  1789. if (index0 != index) {
  1790. XT_PUTPAGE(mp);
  1791. jfs_error(ip->i_sb,
  1792. "xtUpdate: unexpected value of index");
  1793. return -EIO;
  1794. }
  1795. }
  1796. /*
  1797. * split XAD into (nXAD, rXAD)
  1798. *
  1799. * ---nXAD---|
  1800. * --|----------XAD----------|--
  1801. * |-rXAD-|
  1802. */
  1803. updateLeft: /* (nxoff == xoff) && (nxlen < xlen) */
  1804. /* update old XAD with nXAD:recorded */
  1805. xad = &p->xad[index];
  1806. *xad = *nxad;
  1807. xad->flag = xflag & ~XAD_NOTRECORDED;
  1808. /* insert rXAD:not_recorded */
  1809. xoff = xoff + nxlen;
  1810. xlen = xlen - nxlen;
  1811. xaddr = xaddr + nxlen;
  1812. if (nextindex == le16_to_cpu(p->header.maxentry)) {
  1813. /*
  1814. printf("xtUpdate.updateLeft.split p:0x%p\n", p);
  1815. */
  1816. /* xtSpliUp() unpins leaf pages */
  1817. split.mp = mp;
  1818. split.index = newindex;
  1819. split.flag = xflag;
  1820. split.off = xoff;
  1821. split.len = xlen;
  1822. split.addr = xaddr;
  1823. split.pxdlist = NULL;
  1824. if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
  1825. return rc;
  1826. /* get back old page */
  1827. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  1828. if (rc)
  1829. return rc;
  1830. /*
  1831. * if leaf root has been split, original root has been
  1832. * copied to new child page, i.e., original entry now
  1833. * resides on the new child page;
  1834. */
  1835. if (p->header.flag & BT_INTERNAL) {
  1836. ASSERT(p->header.nextindex ==
  1837. cpu_to_le16(XTENTRYSTART + 1));
  1838. xad = &p->xad[XTENTRYSTART];
  1839. bn = addressXAD(xad);
  1840. XT_PUTPAGE(mp);
  1841. /* get new child page */
  1842. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  1843. if (rc)
  1844. return rc;
  1845. BT_MARK_DIRTY(mp, ip);
  1846. if (!test_cflag(COMMIT_Nolink, ip)) {
  1847. tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
  1848. xtlck = (struct xtlock *) & tlck->lock;
  1849. }
  1850. }
  1851. } else {
  1852. /* if insert into middle, shift right remaining entries */
  1853. if (newindex < nextindex)
  1854. memmove(&p->xad[newindex + 1], &p->xad[newindex],
  1855. (nextindex - newindex) << L2XTSLOTSIZE);
  1856. /* insert the entry */
  1857. xad = &p->xad[newindex];
  1858. XT_PUTENTRY(xad, xflag, xoff, xlen, xaddr);
  1859. /* advance next available entry index. */
  1860. p->header.nextindex =
  1861. cpu_to_le16(le16_to_cpu(p->header.nextindex) + 1);
  1862. }
  1863. out:
  1864. if (!test_cflag(COMMIT_Nolink, ip)) {
  1865. xtlck->lwm.offset = (xtlck->lwm.offset) ?
  1866. min(index0, (int)xtlck->lwm.offset) : index0;
  1867. xtlck->lwm.length = le16_to_cpu(p->header.nextindex) -
  1868. xtlck->lwm.offset;
  1869. }
  1870. /* unpin the leaf page */
  1871. XT_PUTPAGE(mp);
  1872. return rc;
  1873. }
  1874. /*
  1875. * xtAppend()
  1876. *
  1877. * function: grow in append mode from contiguous region specified ;
  1878. *
  1879. * parameter:
  1880. * tid - transaction id;
  1881. * ip - file object;
  1882. * xflag - extent flag:
  1883. * xoff - extent offset;
  1884. * maxblocks - max extent length;
  1885. * xlen - extent length (in/out);
  1886. * xaddrp - extent address pointer (in/out):
  1887. * flag -
  1888. *
  1889. * return:
  1890. */
  1891. int xtAppend(tid_t tid, /* transaction id */
  1892. struct inode *ip, int xflag, s64 xoff, s32 maxblocks,
  1893. s32 * xlenp, /* (in/out) */
  1894. s64 * xaddrp, /* (in/out) */
  1895. int flag)
  1896. {
  1897. int rc = 0;
  1898. struct metapage *mp; /* meta-page buffer */
  1899. xtpage_t *p; /* base B+-tree index page */
  1900. s64 bn, xaddr;
  1901. int index, nextindex;
  1902. struct btstack btstack; /* traverse stack */
  1903. struct xtsplit split; /* split information */
  1904. xad_t *xad;
  1905. int cmp;
  1906. struct tlock *tlck;
  1907. struct xtlock *xtlck;
  1908. int nsplit, nblocks, xlen;
  1909. struct pxdlist pxdlist;
  1910. pxd_t *pxd;
  1911. s64 next;
  1912. xaddr = *xaddrp;
  1913. xlen = *xlenp;
  1914. jfs_info("xtAppend: xoff:0x%lx maxblocks:%d xlen:%d xaddr:0x%lx",
  1915. (ulong) xoff, maxblocks, xlen, (ulong) xaddr);
  1916. /*
  1917. * search for the entry location at which to insert:
  1918. *
  1919. * xtFastSearch() and xtSearch() both returns (leaf page
  1920. * pinned, index at which to insert).
  1921. * n.b. xtSearch() may return index of maxentry of
  1922. * the full page.
  1923. */
  1924. if ((rc = xtSearch(ip, xoff, &next, &cmp, &btstack, XT_INSERT)))
  1925. return rc;
  1926. /* retrieve search result */
  1927. XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
  1928. if (cmp == 0) {
  1929. rc = -EEXIST;
  1930. goto out;
  1931. }
  1932. if (next)
  1933. xlen = min(xlen, (int)(next - xoff));
  1934. //insert:
  1935. /*
  1936. * insert entry for new extent
  1937. */
  1938. xflag |= XAD_NEW;
  1939. /*
  1940. * if the leaf page is full, split the page and
  1941. * propagate up the router entry for the new page from split
  1942. *
  1943. * The xtSplitUp() will insert the entry and unpin the leaf page.
  1944. */
  1945. nextindex = le16_to_cpu(p->header.nextindex);
  1946. if (nextindex < le16_to_cpu(p->header.maxentry))
  1947. goto insertLeaf;
  1948. /*
  1949. * allocate new index blocks to cover index page split(s)
  1950. */
  1951. nsplit = btstack.nsplit;
  1952. split.pxdlist = &pxdlist;
  1953. pxdlist.maxnpxd = pxdlist.npxd = 0;
  1954. pxd = &pxdlist.pxd[0];
  1955. nblocks = JFS_SBI(ip->i_sb)->nbperpage;
  1956. for (; nsplit > 0; nsplit--, pxd++, xaddr += nblocks, maxblocks -= nblocks) {
  1957. if ((rc = dbAllocBottomUp(ip, xaddr, (s64) nblocks)) == 0) {
  1958. PXDaddress(pxd, xaddr);
  1959. PXDlength(pxd, nblocks);
  1960. pxdlist.maxnpxd++;
  1961. continue;
  1962. }
  1963. /* undo allocation */
  1964. goto out;
  1965. }
  1966. xlen = min(xlen, maxblocks);
  1967. /*
  1968. * allocate data extent requested
  1969. */
  1970. if ((rc = dbAllocBottomUp(ip, xaddr, (s64) xlen)))
  1971. goto out;
  1972. split.mp = mp;
  1973. split.index = index;
  1974. split.flag = xflag;
  1975. split.off = xoff;
  1976. split.len = xlen;
  1977. split.addr = xaddr;
  1978. if ((rc = xtSplitUp(tid, ip, &split, &btstack))) {
  1979. /* undo data extent allocation */
  1980. dbFree(ip, *xaddrp, (s64) * xlenp);
  1981. return rc;
  1982. }
  1983. *xaddrp = xaddr;
  1984. *xlenp = xlen;
  1985. return 0;
  1986. /*
  1987. * insert the new entry into the leaf page
  1988. */
  1989. insertLeaf:
  1990. /*
  1991. * allocate data extent requested
  1992. */
  1993. if ((rc = dbAllocBottomUp(ip, xaddr, (s64) xlen)))
  1994. goto out;
  1995. BT_MARK_DIRTY(mp, ip);
  1996. /*
  1997. * acquire a transaction lock on the leaf page;
  1998. *
  1999. * action: xad insertion/extension;
  2000. */
  2001. tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
  2002. xtlck = (struct xtlock *) & tlck->lock;
  2003. /* insert the new entry: mark the entry NEW */
  2004. xad = &p->xad[index];
  2005. XT_PUTENTRY(xad, xflag, xoff, xlen, xaddr);
  2006. /* advance next available entry index */
  2007. le16_add_cpu(&p->header.nextindex, 1);
  2008. xtlck->lwm.offset =
  2009. (xtlck->lwm.offset) ? min(index,(int) xtlck->lwm.offset) : index;
  2010. xtlck->lwm.length = le16_to_cpu(p->header.nextindex) -
  2011. xtlck->lwm.offset;
  2012. *xaddrp = xaddr;
  2013. *xlenp = xlen;
  2014. out:
  2015. /* unpin the leaf page */
  2016. XT_PUTPAGE(mp);
  2017. return rc;
  2018. }
  2019. #ifdef _STILL_TO_PORT
  2020. /* - TBD for defragmentaion/reorganization -
  2021. *
  2022. * xtDelete()
  2023. *
  2024. * function:
  2025. * delete the entry with the specified key.
  2026. *
  2027. * N.B.: whole extent of the entry is assumed to be deleted.
  2028. *
  2029. * parameter:
  2030. *
  2031. * return:
  2032. * ENOENT: if the entry is not found.
  2033. *
  2034. * exception:
  2035. */
  2036. int xtDelete(tid_t tid, struct inode *ip, s64 xoff, s32 xlen, int flag)
  2037. {
  2038. int rc = 0;
  2039. struct btstack btstack;
  2040. int cmp;
  2041. s64 bn;
  2042. struct metapage *mp;
  2043. xtpage_t *p;
  2044. int index, nextindex;
  2045. struct tlock *tlck;
  2046. struct xtlock *xtlck;
  2047. /*
  2048. * find the matching entry; xtSearch() pins the page
  2049. */
  2050. if ((rc = xtSearch(ip, xoff, NULL, &cmp, &btstack, 0)))
  2051. return rc;
  2052. XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
  2053. if (cmp) {
  2054. /* unpin the leaf page */
  2055. XT_PUTPAGE(mp);
  2056. return -ENOENT;
  2057. }
  2058. /*
  2059. * delete the entry from the leaf page
  2060. */
  2061. nextindex = le16_to_cpu(p->header.nextindex);
  2062. le16_add_cpu(&p->header.nextindex, -1);
  2063. /*
  2064. * if the leaf page bocome empty, free the page
  2065. */
  2066. if (p->header.nextindex == cpu_to_le16(XTENTRYSTART))
  2067. return (xtDeleteUp(tid, ip, mp, p, &btstack));
  2068. BT_MARK_DIRTY(mp, ip);
  2069. /*
  2070. * acquire a transaction lock on the leaf page;
  2071. *
  2072. * action:xad deletion;
  2073. */
  2074. tlck = txLock(tid, ip, mp, tlckXTREE);
  2075. xtlck = (struct xtlock *) & tlck->lock;
  2076. xtlck->lwm.offset =
  2077. (xtlck->lwm.offset) ? min(index, xtlck->lwm.offset) : index;
  2078. /* if delete from middle, shift left/compact the remaining entries */
  2079. if (index < nextindex - 1)
  2080. memmove(&p->xad[index], &p->xad[index + 1],
  2081. (nextindex - index - 1) * sizeof(xad_t));
  2082. XT_PUTPAGE(mp);
  2083. return 0;
  2084. }
  2085. /* - TBD for defragmentaion/reorganization -
  2086. *
  2087. * xtDeleteUp()
  2088. *
  2089. * function:
  2090. * free empty pages as propagating deletion up the tree
  2091. *
  2092. * parameter:
  2093. *
  2094. * return:
  2095. */
  2096. static int
  2097. xtDeleteUp(tid_t tid, struct inode *ip,
  2098. struct metapage * fmp, xtpage_t * fp, struct btstack * btstack)
  2099. {
  2100. int rc = 0;
  2101. struct metapage *mp;
  2102. xtpage_t *p;
  2103. int index, nextindex;
  2104. s64 xaddr;
  2105. int xlen;
  2106. struct btframe *parent;
  2107. struct tlock *tlck;
  2108. struct xtlock *xtlck;
  2109. /*
  2110. * keep root leaf page which has become empty
  2111. */
  2112. if (fp->header.flag & BT_ROOT) {
  2113. /* keep the root page */
  2114. fp->header.flag &= ~BT_INTERNAL;
  2115. fp->header.flag |= BT_LEAF;
  2116. fp->header.nextindex = cpu_to_le16(XTENTRYSTART);
  2117. /* XT_PUTPAGE(fmp); */
  2118. return 0;
  2119. }
  2120. /*
  2121. * free non-root leaf page
  2122. */
  2123. if ((rc = xtRelink(tid, ip, fp))) {
  2124. XT_PUTPAGE(fmp);
  2125. return rc;
  2126. }
  2127. xaddr = addressPXD(&fp->header.self);
  2128. xlen = lengthPXD(&fp->header.self);
  2129. /* free the page extent */
  2130. dbFree(ip, xaddr, (s64) xlen);
  2131. /* free the buffer page */
  2132. discard_metapage(fmp);
  2133. /*
  2134. * propagate page deletion up the index tree
  2135. *
  2136. * If the delete from the parent page makes it empty,
  2137. * continue all the way up the tree.
  2138. * stop if the root page is reached (which is never deleted) or
  2139. * if the entry deletion does not empty the page.
  2140. */
  2141. while ((parent = BT_POP(btstack)) != NULL) {
  2142. /* get/pin the parent page <sp> */
  2143. XT_GETPAGE(ip, parent->bn, mp, PSIZE, p, rc);
  2144. if (rc)
  2145. return rc;
  2146. index = parent->index;
  2147. /* delete the entry for the freed child page from parent.
  2148. */
  2149. nextindex = le16_to_cpu(p->header.nextindex);
  2150. /*
  2151. * the parent has the single entry being deleted:
  2152. * free the parent page which has become empty.
  2153. */
  2154. if (nextindex == 1) {
  2155. if (p->header.flag & BT_ROOT) {
  2156. /* keep the root page */
  2157. p->header.flag &= ~BT_INTERNAL;
  2158. p->header.flag |= BT_LEAF;
  2159. p->header.nextindex =
  2160. cpu_to_le16(XTENTRYSTART);
  2161. /* XT_PUTPAGE(mp); */
  2162. break;
  2163. } else {
  2164. /* free the parent page */
  2165. if ((rc = xtRelink(tid, ip, p)))
  2166. return rc;
  2167. xaddr = addressPXD(&p->header.self);
  2168. /* free the page extent */
  2169. dbFree(ip, xaddr,
  2170. (s64) JFS_SBI(ip->i_sb)->nbperpage);
  2171. /* unpin/free the buffer page */
  2172. discard_metapage(mp);
  2173. /* propagate up */
  2174. continue;
  2175. }
  2176. }
  2177. /*
  2178. * the parent has other entries remaining:
  2179. * delete the router entry from the parent page.
  2180. */
  2181. else {
  2182. BT_MARK_DIRTY(mp, ip);
  2183. /*
  2184. * acquire a transaction lock on the leaf page;
  2185. *
  2186. * action:xad deletion;
  2187. */
  2188. tlck = txLock(tid, ip, mp, tlckXTREE);
  2189. xtlck = (struct xtlock *) & tlck->lock;
  2190. xtlck->lwm.offset =
  2191. (xtlck->lwm.offset) ? min(index,
  2192. xtlck->lwm.
  2193. offset) : index;
  2194. /* if delete from middle,
  2195. * shift left/compact the remaining entries in the page
  2196. */
  2197. if (index < nextindex - 1)
  2198. memmove(&p->xad[index], &p->xad[index + 1],
  2199. (nextindex - index -
  2200. 1) << L2XTSLOTSIZE);
  2201. le16_add_cpu(&p->header.nextindex, -1);
  2202. jfs_info("xtDeleteUp(entry): 0x%lx[%d]",
  2203. (ulong) parent->bn, index);
  2204. }
  2205. /* unpin the parent page */
  2206. XT_PUTPAGE(mp);
  2207. /* exit propagation up */
  2208. break;
  2209. }
  2210. return 0;
  2211. }
  2212. /*
  2213. * NAME: xtRelocate()
  2214. *
  2215. * FUNCTION: relocate xtpage or data extent of regular file;
  2216. * This function is mainly used by defragfs utility.
  2217. *
  2218. * NOTE: This routine does not have the logic to handle
  2219. * uncommitted allocated extent. The caller should call
  2220. * txCommit() to commit all the allocation before call
  2221. * this routine.
  2222. */
  2223. int
  2224. xtRelocate(tid_t tid, struct inode * ip, xad_t * oxad, /* old XAD */
  2225. s64 nxaddr, /* new xaddr */
  2226. int xtype)
  2227. { /* extent type: XTPAGE or DATAEXT */
  2228. int rc = 0;
  2229. struct tblock *tblk;
  2230. struct tlock *tlck;
  2231. struct xtlock *xtlck;
  2232. struct metapage *mp, *pmp, *lmp, *rmp; /* meta-page buffer */
  2233. xtpage_t *p, *pp, *rp, *lp; /* base B+-tree index page */
  2234. xad_t *xad;
  2235. pxd_t *pxd;
  2236. s64 xoff, xsize;
  2237. int xlen;
  2238. s64 oxaddr, sxaddr, dxaddr, nextbn, prevbn;
  2239. cbuf_t *cp;
  2240. s64 offset, nbytes, nbrd, pno;
  2241. int nb, npages, nblks;
  2242. s64 bn;
  2243. int cmp;
  2244. int index;
  2245. struct pxd_lock *pxdlock;
  2246. struct btstack btstack; /* traverse stack */
  2247. xtype = xtype & EXTENT_TYPE;
  2248. xoff = offsetXAD(oxad);
  2249. oxaddr = addressXAD(oxad);
  2250. xlen = lengthXAD(oxad);
  2251. /* validate extent offset */
  2252. offset = xoff << JFS_SBI(ip->i_sb)->l2bsize;
  2253. if (offset >= ip->i_size)
  2254. return -ESTALE; /* stale extent */
  2255. jfs_info("xtRelocate: xtype:%d xoff:0x%lx xlen:0x%x xaddr:0x%lx:0x%lx",
  2256. xtype, (ulong) xoff, xlen, (ulong) oxaddr, (ulong) nxaddr);
  2257. /*
  2258. * 1. get and validate the parent xtpage/xad entry
  2259. * covering the source extent to be relocated;
  2260. */
  2261. if (xtype == DATAEXT) {
  2262. /* search in leaf entry */
  2263. rc = xtSearch(ip, xoff, NULL, &cmp, &btstack, 0);
  2264. if (rc)
  2265. return rc;
  2266. /* retrieve search result */
  2267. XT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
  2268. if (cmp) {
  2269. XT_PUTPAGE(pmp);
  2270. return -ESTALE;
  2271. }
  2272. /* validate for exact match with a single entry */
  2273. xad = &pp->xad[index];
  2274. if (addressXAD(xad) != oxaddr || lengthXAD(xad) != xlen) {
  2275. XT_PUTPAGE(pmp);
  2276. return -ESTALE;
  2277. }
  2278. } else { /* (xtype == XTPAGE) */
  2279. /* search in internal entry */
  2280. rc = xtSearchNode(ip, oxad, &cmp, &btstack, 0);
  2281. if (rc)
  2282. return rc;
  2283. /* retrieve search result */
  2284. XT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
  2285. if (cmp) {
  2286. XT_PUTPAGE(pmp);
  2287. return -ESTALE;
  2288. }
  2289. /* xtSearchNode() validated for exact match with a single entry
  2290. */
  2291. xad = &pp->xad[index];
  2292. }
  2293. jfs_info("xtRelocate: parent xad entry validated.");
  2294. /*
  2295. * 2. relocate the extent
  2296. */
  2297. if (xtype == DATAEXT) {
  2298. /* if the extent is allocated-but-not-recorded
  2299. * there is no real data to be moved in this extent,
  2300. */
  2301. if (xad->flag & XAD_NOTRECORDED)
  2302. goto out;
  2303. else
  2304. /* release xtpage for cmRead()/xtLookup() */
  2305. XT_PUTPAGE(pmp);
  2306. /*
  2307. * cmRelocate()
  2308. *
  2309. * copy target data pages to be relocated;
  2310. *
  2311. * data extent must start at page boundary and
  2312. * multiple of page size (except the last data extent);
  2313. * read in each page of the source data extent into cbuf,
  2314. * update the cbuf extent descriptor of the page to be
  2315. * homeward bound to new dst data extent
  2316. * copy the data from the old extent to new extent.
  2317. * copy is essential for compressed files to avoid problems
  2318. * that can arise if there was a change in compression
  2319. * algorithms.
  2320. * it is a good strategy because it may disrupt cache
  2321. * policy to keep the pages in memory afterwards.
  2322. */
  2323. offset = xoff << JFS_SBI(ip->i_sb)->l2bsize;
  2324. assert((offset & CM_OFFSET) == 0);
  2325. nbytes = xlen << JFS_SBI(ip->i_sb)->l2bsize;
  2326. pno = offset >> CM_L2BSIZE;
  2327. npages = (nbytes + (CM_BSIZE - 1)) >> CM_L2BSIZE;
  2328. /*
  2329. npages = ((offset + nbytes - 1) >> CM_L2BSIZE) -
  2330. (offset >> CM_L2BSIZE) + 1;
  2331. */
  2332. sxaddr = oxaddr;
  2333. dxaddr = nxaddr;
  2334. /* process the request one cache buffer at a time */
  2335. for (nbrd = 0; nbrd < nbytes; nbrd += nb,
  2336. offset += nb, pno++, npages--) {
  2337. /* compute page size */
  2338. nb = min(nbytes - nbrd, CM_BSIZE);
  2339. /* get the cache buffer of the page */
  2340. if (rc = cmRead(ip, offset, npages, &cp))
  2341. break;
  2342. assert(addressPXD(&cp->cm_pxd) == sxaddr);
  2343. assert(!cp->cm_modified);
  2344. /* bind buffer with the new extent address */
  2345. nblks = nb >> JFS_IP(ip->i_sb)->l2bsize;
  2346. cmSetXD(ip, cp, pno, dxaddr, nblks);
  2347. /* release the cbuf, mark it as modified */
  2348. cmPut(cp, true);
  2349. dxaddr += nblks;
  2350. sxaddr += nblks;
  2351. }
  2352. /* get back parent page */
  2353. if ((rc = xtSearch(ip, xoff, NULL, &cmp, &btstack, 0)))
  2354. return rc;
  2355. XT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
  2356. jfs_info("xtRelocate: target data extent relocated.");
  2357. } else { /* (xtype == XTPAGE) */
  2358. /*
  2359. * read in the target xtpage from the source extent;
  2360. */
  2361. XT_GETPAGE(ip, oxaddr, mp, PSIZE, p, rc);
  2362. if (rc) {
  2363. XT_PUTPAGE(pmp);
  2364. return rc;
  2365. }
  2366. /*
  2367. * read in sibling pages if any to update sibling pointers;
  2368. */
  2369. rmp = NULL;
  2370. if (p->header.next) {
  2371. nextbn = le64_to_cpu(p->header.next);
  2372. XT_GETPAGE(ip, nextbn, rmp, PSIZE, rp, rc);
  2373. if (rc) {
  2374. XT_PUTPAGE(pmp);
  2375. XT_PUTPAGE(mp);
  2376. return (rc);
  2377. }
  2378. }
  2379. lmp = NULL;
  2380. if (p->header.prev) {
  2381. prevbn = le64_to_cpu(p->header.prev);
  2382. XT_GETPAGE(ip, prevbn, lmp, PSIZE, lp, rc);
  2383. if (rc) {
  2384. XT_PUTPAGE(pmp);
  2385. XT_PUTPAGE(mp);
  2386. if (rmp)
  2387. XT_PUTPAGE(rmp);
  2388. return (rc);
  2389. }
  2390. }
  2391. /* at this point, all xtpages to be updated are in memory */
  2392. /*
  2393. * update sibling pointers of sibling xtpages if any;
  2394. */
  2395. if (lmp) {
  2396. BT_MARK_DIRTY(lmp, ip);
  2397. tlck = txLock(tid, ip, lmp, tlckXTREE | tlckRELINK);
  2398. lp->header.next = cpu_to_le64(nxaddr);
  2399. XT_PUTPAGE(lmp);
  2400. }
  2401. if (rmp) {
  2402. BT_MARK_DIRTY(rmp, ip);
  2403. tlck = txLock(tid, ip, rmp, tlckXTREE | tlckRELINK);
  2404. rp->header.prev = cpu_to_le64(nxaddr);
  2405. XT_PUTPAGE(rmp);
  2406. }
  2407. /*
  2408. * update the target xtpage to be relocated
  2409. *
  2410. * update the self address of the target page
  2411. * and write to destination extent;
  2412. * redo image covers the whole xtpage since it is new page
  2413. * to the destination extent;
  2414. * update of bmap for the free of source extent
  2415. * of the target xtpage itself:
  2416. * update of bmap for the allocation of destination extent
  2417. * of the target xtpage itself:
  2418. * update of bmap for the extents covered by xad entries in
  2419. * the target xtpage is not necessary since they are not
  2420. * updated;
  2421. * if not committed before this relocation,
  2422. * target page may contain XAD_NEW entries which must
  2423. * be scanned for bmap update (logredo() always
  2424. * scan xtpage REDOPAGE image for bmap update);
  2425. * if committed before this relocation (tlckRELOCATE),
  2426. * scan may be skipped by commit() and logredo();
  2427. */
  2428. BT_MARK_DIRTY(mp, ip);
  2429. /* tlckNEW init xtlck->lwm.offset = XTENTRYSTART; */
  2430. tlck = txLock(tid, ip, mp, tlckXTREE | tlckNEW);
  2431. xtlck = (struct xtlock *) & tlck->lock;
  2432. /* update the self address in the xtpage header */
  2433. pxd = &p->header.self;
  2434. PXDaddress(pxd, nxaddr);
  2435. /* linelock for the after image of the whole page */
  2436. xtlck->lwm.length =
  2437. le16_to_cpu(p->header.nextindex) - xtlck->lwm.offset;
  2438. /* update the buffer extent descriptor of target xtpage */
  2439. xsize = xlen << JFS_SBI(ip->i_sb)->l2bsize;
  2440. bmSetXD(mp, nxaddr, xsize);
  2441. /* unpin the target page to new homeward bound */
  2442. XT_PUTPAGE(mp);
  2443. jfs_info("xtRelocate: target xtpage relocated.");
  2444. }
  2445. /*
  2446. * 3. acquire maplock for the source extent to be freed;
  2447. *
  2448. * acquire a maplock saving the src relocated extent address;
  2449. * to free of the extent at commit time;
  2450. */
  2451. out:
  2452. /* if DATAEXT relocation, write a LOG_UPDATEMAP record for
  2453. * free PXD of the source data extent (logredo() will update
  2454. * bmap for free of source data extent), and update bmap for
  2455. * free of the source data extent;
  2456. */
  2457. if (xtype == DATAEXT)
  2458. tlck = txMaplock(tid, ip, tlckMAP);
  2459. /* if XTPAGE relocation, write a LOG_NOREDOPAGE record
  2460. * for the source xtpage (logredo() will init NoRedoPage
  2461. * filter and will also update bmap for free of the source
  2462. * xtpage), and update bmap for free of the source xtpage;
  2463. * N.B. We use tlckMAP instead of tlkcXTREE because there
  2464. * is no buffer associated with this lock since the buffer
  2465. * has been redirected to the target location.
  2466. */
  2467. else /* (xtype == XTPAGE) */
  2468. tlck = txMaplock(tid, ip, tlckMAP | tlckRELOCATE);
  2469. pxdlock = (struct pxd_lock *) & tlck->lock;
  2470. pxdlock->flag = mlckFREEPXD;
  2471. PXDaddress(&pxdlock->pxd, oxaddr);
  2472. PXDlength(&pxdlock->pxd, xlen);
  2473. pxdlock->index = 1;
  2474. /*
  2475. * 4. update the parent xad entry for relocation;
  2476. *
  2477. * acquire tlck for the parent entry with XAD_NEW as entry
  2478. * update which will write LOG_REDOPAGE and update bmap for
  2479. * allocation of XAD_NEW destination extent;
  2480. */
  2481. jfs_info("xtRelocate: update parent xad entry.");
  2482. BT_MARK_DIRTY(pmp, ip);
  2483. tlck = txLock(tid, ip, pmp, tlckXTREE | tlckGROW);
  2484. xtlck = (struct xtlock *) & tlck->lock;
  2485. /* update the XAD with the new destination extent; */
  2486. xad = &pp->xad[index];
  2487. xad->flag |= XAD_NEW;
  2488. XADaddress(xad, nxaddr);
  2489. xtlck->lwm.offset = min(index, xtlck->lwm.offset);
  2490. xtlck->lwm.length = le16_to_cpu(pp->header.nextindex) -
  2491. xtlck->lwm.offset;
  2492. /* unpin the parent xtpage */
  2493. XT_PUTPAGE(pmp);
  2494. return rc;
  2495. }
  2496. /*
  2497. * xtSearchNode()
  2498. *
  2499. * function: search for the internal xad entry covering specified extent.
  2500. * This function is mainly used by defragfs utility.
  2501. *
  2502. * parameters:
  2503. * ip - file object;
  2504. * xad - extent to find;
  2505. * cmpp - comparison result:
  2506. * btstack - traverse stack;
  2507. * flag - search process flag;
  2508. *
  2509. * returns:
  2510. * btstack contains (bn, index) of search path traversed to the entry.
  2511. * *cmpp is set to result of comparison with the entry returned.
  2512. * the page containing the entry is pinned at exit.
  2513. */
  2514. static int xtSearchNode(struct inode *ip, xad_t * xad, /* required XAD entry */
  2515. int *cmpp, struct btstack * btstack, int flag)
  2516. {
  2517. int rc = 0;
  2518. s64 xoff, xaddr;
  2519. int xlen;
  2520. int cmp = 1; /* init for empty page */
  2521. s64 bn; /* block number */
  2522. struct metapage *mp; /* meta-page buffer */
  2523. xtpage_t *p; /* page */
  2524. int base, index, lim;
  2525. struct btframe *btsp;
  2526. s64 t64;
  2527. BT_CLR(btstack);
  2528. xoff = offsetXAD(xad);
  2529. xlen = lengthXAD(xad);
  2530. xaddr = addressXAD(xad);
  2531. /*
  2532. * search down tree from root:
  2533. *
  2534. * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
  2535. * internal page, child page Pi contains entry with k, Ki <= K < Kj.
  2536. *
  2537. * if entry with search key K is not found
  2538. * internal page search find the entry with largest key Ki
  2539. * less than K which point to the child page to search;
  2540. * leaf page search find the entry with smallest key Kj
  2541. * greater than K so that the returned index is the position of
  2542. * the entry to be shifted right for insertion of new entry.
  2543. * for empty tree, search key is greater than any key of the tree.
  2544. *
  2545. * by convention, root bn = 0.
  2546. */
  2547. for (bn = 0;;) {
  2548. /* get/pin the page to search */
  2549. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  2550. if (rc)
  2551. return rc;
  2552. if (p->header.flag & BT_LEAF) {
  2553. XT_PUTPAGE(mp);
  2554. return -ESTALE;
  2555. }
  2556. lim = le16_to_cpu(p->header.nextindex) - XTENTRYSTART;
  2557. /*
  2558. * binary search with search key K on the current page
  2559. */
  2560. for (base = XTENTRYSTART; lim; lim >>= 1) {
  2561. index = base + (lim >> 1);
  2562. XT_CMP(cmp, xoff, &p->xad[index], t64);
  2563. if (cmp == 0) {
  2564. /*
  2565. * search hit
  2566. *
  2567. * verify for exact match;
  2568. */
  2569. if (xaddr == addressXAD(&p->xad[index]) &&
  2570. xoff == offsetXAD(&p->xad[index])) {
  2571. *cmpp = cmp;
  2572. /* save search result */
  2573. btsp = btstack->top;
  2574. btsp->bn = bn;
  2575. btsp->index = index;
  2576. btsp->mp = mp;
  2577. return 0;
  2578. }
  2579. /* descend/search its child page */
  2580. goto next;
  2581. }
  2582. if (cmp > 0) {
  2583. base = index + 1;
  2584. --lim;
  2585. }
  2586. }
  2587. /*
  2588. * search miss - non-leaf page:
  2589. *
  2590. * base is the smallest index with key (Kj) greater than
  2591. * search key (K) and may be zero or maxentry index.
  2592. * if base is non-zero, decrement base by one to get the parent
  2593. * entry of the child page to search.
  2594. */
  2595. index = base ? base - 1 : base;
  2596. /*
  2597. * go down to child page
  2598. */
  2599. next:
  2600. /* get the child page block number */
  2601. bn = addressXAD(&p->xad[index]);
  2602. /* unpin the parent page */
  2603. XT_PUTPAGE(mp);
  2604. }
  2605. }
  2606. /*
  2607. * xtRelink()
  2608. *
  2609. * function:
  2610. * link around a freed page.
  2611. *
  2612. * Parameter:
  2613. * int tid,
  2614. * struct inode *ip,
  2615. * xtpage_t *p)
  2616. *
  2617. * returns:
  2618. */
  2619. static int xtRelink(tid_t tid, struct inode *ip, xtpage_t * p)
  2620. {
  2621. int rc = 0;
  2622. struct metapage *mp;
  2623. s64 nextbn, prevbn;
  2624. struct tlock *tlck;
  2625. nextbn = le64_to_cpu(p->header.next);
  2626. prevbn = le64_to_cpu(p->header.prev);
  2627. /* update prev pointer of the next page */
  2628. if (nextbn != 0) {
  2629. XT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
  2630. if (rc)
  2631. return rc;
  2632. /*
  2633. * acquire a transaction lock on the page;
  2634. *
  2635. * action: update prev pointer;
  2636. */
  2637. BT_MARK_DIRTY(mp, ip);
  2638. tlck = txLock(tid, ip, mp, tlckXTREE | tlckRELINK);
  2639. /* the page may already have been tlock'd */
  2640. p->header.prev = cpu_to_le64(prevbn);
  2641. XT_PUTPAGE(mp);
  2642. }
  2643. /* update next pointer of the previous page */
  2644. if (prevbn != 0) {
  2645. XT_GETPAGE(ip, prevbn, mp, PSIZE, p, rc);
  2646. if (rc)
  2647. return rc;
  2648. /*
  2649. * acquire a transaction lock on the page;
  2650. *
  2651. * action: update next pointer;
  2652. */
  2653. BT_MARK_DIRTY(mp, ip);
  2654. tlck = txLock(tid, ip, mp, tlckXTREE | tlckRELINK);
  2655. /* the page may already have been tlock'd */
  2656. p->header.next = le64_to_cpu(nextbn);
  2657. XT_PUTPAGE(mp);
  2658. }
  2659. return 0;
  2660. }
  2661. #endif /* _STILL_TO_PORT */
  2662. /*
  2663. * xtInitRoot()
  2664. *
  2665. * initialize file root (inline in inode)
  2666. */
  2667. void xtInitRoot(tid_t tid, struct inode *ip)
  2668. {
  2669. xtpage_t *p;
  2670. /*
  2671. * acquire a transaction lock on the root
  2672. *
  2673. * action:
  2674. */
  2675. txLock(tid, ip, (struct metapage *) &JFS_IP(ip)->bxflag,
  2676. tlckXTREE | tlckNEW);
  2677. p = &JFS_IP(ip)->i_xtroot;
  2678. p->header.flag = DXD_INDEX | BT_ROOT | BT_LEAF;
  2679. p->header.nextindex = cpu_to_le16(XTENTRYSTART);
  2680. if (S_ISDIR(ip->i_mode))
  2681. p->header.maxentry = cpu_to_le16(XTROOTINITSLOT_DIR);
  2682. else {
  2683. p->header.maxentry = cpu_to_le16(XTROOTINITSLOT);
  2684. ip->i_size = 0;
  2685. }
  2686. return;
  2687. }
  2688. /*
  2689. * We can run into a deadlock truncating a file with a large number of
  2690. * xtree pages (large fragmented file). A robust fix would entail a
  2691. * reservation system where we would reserve a number of metadata pages
  2692. * and tlocks which we would be guaranteed without a deadlock. Without
  2693. * this, a partial fix is to limit number of metadata pages we will lock
  2694. * in a single transaction. Currently we will truncate the file so that
  2695. * no more than 50 leaf pages will be locked. The caller of xtTruncate
  2696. * will be responsible for ensuring that the current transaction gets
  2697. * committed, and that subsequent transactions are created to truncate
  2698. * the file further if needed.
  2699. */
  2700. #define MAX_TRUNCATE_LEAVES 50
  2701. /*
  2702. * xtTruncate()
  2703. *
  2704. * function:
  2705. * traverse for truncation logging backward bottom up;
  2706. * terminate at the last extent entry at the current subtree
  2707. * root page covering new down size.
  2708. * truncation may occur within the last extent entry.
  2709. *
  2710. * parameter:
  2711. * int tid,
  2712. * struct inode *ip,
  2713. * s64 newsize,
  2714. * int type) {PWMAP, PMAP, WMAP; DELETE, TRUNCATE}
  2715. *
  2716. * return:
  2717. *
  2718. * note:
  2719. * PWMAP:
  2720. * 1. truncate (non-COMMIT_NOLINK file)
  2721. * by jfs_truncate() or jfs_open(O_TRUNC):
  2722. * xtree is updated;
  2723. * 2. truncate index table of directory when last entry removed
  2724. * map update via tlock at commit time;
  2725. * PMAP:
  2726. * Call xtTruncate_pmap instead
  2727. * WMAP:
  2728. * 1. remove (free zero link count) on last reference release
  2729. * (pmap has been freed at commit zero link count);
  2730. * 2. truncate (COMMIT_NOLINK file, i.e., tmp file):
  2731. * xtree is updated;
  2732. * map update directly at truncation time;
  2733. *
  2734. * if (DELETE)
  2735. * no LOG_NOREDOPAGE is required (NOREDOFILE is sufficient);
  2736. * else if (TRUNCATE)
  2737. * must write LOG_NOREDOPAGE for deleted index page;
  2738. *
  2739. * pages may already have been tlocked by anonymous transactions
  2740. * during file growth (i.e., write) before truncation;
  2741. *
  2742. * except last truncated entry, deleted entries remains as is
  2743. * in the page (nextindex is updated) for other use
  2744. * (e.g., log/update allocation map): this avoid copying the page
  2745. * info but delay free of pages;
  2746. *
  2747. */
  2748. s64 xtTruncate(tid_t tid, struct inode *ip, s64 newsize, int flag)
  2749. {
  2750. int rc = 0;
  2751. s64 teof;
  2752. struct metapage *mp;
  2753. xtpage_t *p;
  2754. s64 bn;
  2755. int index, nextindex;
  2756. xad_t *xad;
  2757. s64 xoff, xaddr;
  2758. int xlen, len, freexlen;
  2759. struct btstack btstack;
  2760. struct btframe *parent;
  2761. struct tblock *tblk = NULL;
  2762. struct tlock *tlck = NULL;
  2763. struct xtlock *xtlck = NULL;
  2764. struct xdlistlock xadlock; /* maplock for COMMIT_WMAP */
  2765. struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
  2766. s64 nfreed;
  2767. int freed, log;
  2768. int locked_leaves = 0;
  2769. /* save object truncation type */
  2770. if (tid) {
  2771. tblk = tid_to_tblock(tid);
  2772. tblk->xflag |= flag;
  2773. }
  2774. nfreed = 0;
  2775. flag &= COMMIT_MAP;
  2776. assert(flag != COMMIT_PMAP);
  2777. if (flag == COMMIT_PWMAP)
  2778. log = 1;
  2779. else {
  2780. log = 0;
  2781. xadlock.flag = mlckFREEXADLIST;
  2782. xadlock.index = 1;
  2783. }
  2784. /*
  2785. * if the newsize is not an integral number of pages,
  2786. * the file between newsize and next page boundary will
  2787. * be cleared.
  2788. * if truncating into a file hole, it will cause
  2789. * a full block to be allocated for the logical block.
  2790. */
  2791. /*
  2792. * release page blocks of truncated region <teof, eof>
  2793. *
  2794. * free the data blocks from the leaf index blocks.
  2795. * delete the parent index entries corresponding to
  2796. * the freed child data/index blocks.
  2797. * free the index blocks themselves which aren't needed
  2798. * in new sized file.
  2799. *
  2800. * index blocks are updated only if the blocks are to be
  2801. * retained in the new sized file.
  2802. * if type is PMAP, the data and index pages are NOT
  2803. * freed, and the data and index blocks are NOT freed
  2804. * from working map.
  2805. * (this will allow continued access of data/index of
  2806. * temporary file (zerolink count file truncated to zero-length)).
  2807. */
  2808. teof = (newsize + (JFS_SBI(ip->i_sb)->bsize - 1)) >>
  2809. JFS_SBI(ip->i_sb)->l2bsize;
  2810. /* clear stack */
  2811. BT_CLR(&btstack);
  2812. /*
  2813. * start with root
  2814. *
  2815. * root resides in the inode
  2816. */
  2817. bn = 0;
  2818. /*
  2819. * first access of each page:
  2820. */
  2821. getPage:
  2822. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  2823. if (rc)
  2824. return rc;
  2825. /* process entries backward from last index */
  2826. index = le16_to_cpu(p->header.nextindex) - 1;
  2827. /* Since this is the rightmost page at this level, and we may have
  2828. * already freed a page that was formerly to the right, let's make
  2829. * sure that the next pointer is zero.
  2830. */
  2831. if (p->header.next) {
  2832. if (log)
  2833. /*
  2834. * Make sure this change to the header is logged.
  2835. * If we really truncate this leaf, the flag
  2836. * will be changed to tlckTRUNCATE
  2837. */
  2838. tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
  2839. BT_MARK_DIRTY(mp, ip);
  2840. p->header.next = 0;
  2841. }
  2842. if (p->header.flag & BT_INTERNAL)
  2843. goto getChild;
  2844. /*
  2845. * leaf page
  2846. */
  2847. freed = 0;
  2848. /* does region covered by leaf page precede Teof ? */
  2849. xad = &p->xad[index];
  2850. xoff = offsetXAD(xad);
  2851. xlen = lengthXAD(xad);
  2852. if (teof >= xoff + xlen) {
  2853. XT_PUTPAGE(mp);
  2854. goto getParent;
  2855. }
  2856. /* (re)acquire tlock of the leaf page */
  2857. if (log) {
  2858. if (++locked_leaves > MAX_TRUNCATE_LEAVES) {
  2859. /*
  2860. * We need to limit the size of the transaction
  2861. * to avoid exhausting pagecache & tlocks
  2862. */
  2863. XT_PUTPAGE(mp);
  2864. newsize = (xoff + xlen) << JFS_SBI(ip->i_sb)->l2bsize;
  2865. goto getParent;
  2866. }
  2867. tlck = txLock(tid, ip, mp, tlckXTREE);
  2868. tlck->type = tlckXTREE | tlckTRUNCATE;
  2869. xtlck = (struct xtlock *) & tlck->lock;
  2870. xtlck->hwm.offset = le16_to_cpu(p->header.nextindex) - 1;
  2871. }
  2872. BT_MARK_DIRTY(mp, ip);
  2873. /*
  2874. * scan backward leaf page entries
  2875. */
  2876. for (; index >= XTENTRYSTART; index--) {
  2877. xad = &p->xad[index];
  2878. xoff = offsetXAD(xad);
  2879. xlen = lengthXAD(xad);
  2880. xaddr = addressXAD(xad);
  2881. /*
  2882. * The "data" for a directory is indexed by the block
  2883. * device's address space. This metadata must be invalidated
  2884. * here
  2885. */
  2886. if (S_ISDIR(ip->i_mode) && (teof == 0))
  2887. invalidate_xad_metapages(ip, *xad);
  2888. /*
  2889. * entry beyond eof: continue scan of current page
  2890. * xad
  2891. * ---|---=======------->
  2892. * eof
  2893. */
  2894. if (teof < xoff) {
  2895. nfreed += xlen;
  2896. continue;
  2897. }
  2898. /*
  2899. * (xoff <= teof): last entry to be deleted from page;
  2900. * If other entries remain in page: keep and update the page.
  2901. */
  2902. /*
  2903. * eof == entry_start: delete the entry
  2904. * xad
  2905. * -------|=======------->
  2906. * eof
  2907. *
  2908. */
  2909. if (teof == xoff) {
  2910. nfreed += xlen;
  2911. if (index == XTENTRYSTART)
  2912. break;
  2913. nextindex = index;
  2914. }
  2915. /*
  2916. * eof within the entry: truncate the entry.
  2917. * xad
  2918. * -------===|===------->
  2919. * eof
  2920. */
  2921. else if (teof < xoff + xlen) {
  2922. /* update truncated entry */
  2923. len = teof - xoff;
  2924. freexlen = xlen - len;
  2925. XADlength(xad, len);
  2926. /* save pxd of truncated extent in tlck */
  2927. xaddr += len;
  2928. if (log) { /* COMMIT_PWMAP */
  2929. xtlck->lwm.offset = (xtlck->lwm.offset) ?
  2930. min(index, (int)xtlck->lwm.offset) : index;
  2931. xtlck->lwm.length = index + 1 -
  2932. xtlck->lwm.offset;
  2933. xtlck->twm.offset = index;
  2934. pxdlock = (struct pxd_lock *) & xtlck->pxdlock;
  2935. pxdlock->flag = mlckFREEPXD;
  2936. PXDaddress(&pxdlock->pxd, xaddr);
  2937. PXDlength(&pxdlock->pxd, freexlen);
  2938. }
  2939. /* free truncated extent */
  2940. else { /* COMMIT_WMAP */
  2941. pxdlock = (struct pxd_lock *) & xadlock;
  2942. pxdlock->flag = mlckFREEPXD;
  2943. PXDaddress(&pxdlock->pxd, xaddr);
  2944. PXDlength(&pxdlock->pxd, freexlen);
  2945. txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
  2946. /* reset map lock */
  2947. xadlock.flag = mlckFREEXADLIST;
  2948. }
  2949. /* current entry is new last entry; */
  2950. nextindex = index + 1;
  2951. nfreed += freexlen;
  2952. }
  2953. /*
  2954. * eof beyond the entry:
  2955. * xad
  2956. * -------=======---|--->
  2957. * eof
  2958. */
  2959. else { /* (xoff + xlen < teof) */
  2960. nextindex = index + 1;
  2961. }
  2962. if (nextindex < le16_to_cpu(p->header.nextindex)) {
  2963. if (!log) { /* COMMIT_WAMP */
  2964. xadlock.xdlist = &p->xad[nextindex];
  2965. xadlock.count =
  2966. le16_to_cpu(p->header.nextindex) -
  2967. nextindex;
  2968. txFreeMap(ip, (struct maplock *) & xadlock,
  2969. NULL, COMMIT_WMAP);
  2970. }
  2971. p->header.nextindex = cpu_to_le16(nextindex);
  2972. }
  2973. XT_PUTPAGE(mp);
  2974. /* assert(freed == 0); */
  2975. goto getParent;
  2976. } /* end scan of leaf page entries */
  2977. freed = 1;
  2978. /*
  2979. * leaf page become empty: free the page if type != PMAP
  2980. */
  2981. if (log) { /* COMMIT_PWMAP */
  2982. /* txCommit() with tlckFREE:
  2983. * free data extents covered by leaf [XTENTRYSTART:hwm);
  2984. * invalidate leaf if COMMIT_PWMAP;
  2985. * if (TRUNCATE), will write LOG_NOREDOPAGE;
  2986. */
  2987. tlck->type = tlckXTREE | tlckFREE;
  2988. } else { /* COMMIT_WAMP */
  2989. /* free data extents covered by leaf */
  2990. xadlock.xdlist = &p->xad[XTENTRYSTART];
  2991. xadlock.count =
  2992. le16_to_cpu(p->header.nextindex) - XTENTRYSTART;
  2993. txFreeMap(ip, (struct maplock *) & xadlock, NULL, COMMIT_WMAP);
  2994. }
  2995. if (p->header.flag & BT_ROOT) {
  2996. p->header.flag &= ~BT_INTERNAL;
  2997. p->header.flag |= BT_LEAF;
  2998. p->header.nextindex = cpu_to_le16(XTENTRYSTART);
  2999. XT_PUTPAGE(mp); /* debug */
  3000. goto out;
  3001. } else {
  3002. if (log) { /* COMMIT_PWMAP */
  3003. /* page will be invalidated at tx completion
  3004. */
  3005. XT_PUTPAGE(mp);
  3006. } else { /* COMMIT_WMAP */
  3007. if (mp->lid)
  3008. lid_to_tlock(mp->lid)->flag |= tlckFREELOCK;
  3009. /* invalidate empty leaf page */
  3010. discard_metapage(mp);
  3011. }
  3012. }
  3013. /*
  3014. * the leaf page become empty: delete the parent entry
  3015. * for the leaf page if the parent page is to be kept
  3016. * in the new sized file.
  3017. */
  3018. /*
  3019. * go back up to the parent page
  3020. */
  3021. getParent:
  3022. /* pop/restore parent entry for the current child page */
  3023. if ((parent = BT_POP(&btstack)) == NULL)
  3024. /* current page must have been root */
  3025. goto out;
  3026. /* get back the parent page */
  3027. bn = parent->bn;
  3028. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  3029. if (rc)
  3030. return rc;
  3031. index = parent->index;
  3032. /*
  3033. * child page was not empty:
  3034. */
  3035. if (freed == 0) {
  3036. /* has any entry deleted from parent ? */
  3037. if (index < le16_to_cpu(p->header.nextindex) - 1) {
  3038. /* (re)acquire tlock on the parent page */
  3039. if (log) { /* COMMIT_PWMAP */
  3040. /* txCommit() with tlckTRUNCATE:
  3041. * free child extents covered by parent [);
  3042. */
  3043. tlck = txLock(tid, ip, mp, tlckXTREE);
  3044. xtlck = (struct xtlock *) & tlck->lock;
  3045. if (!(tlck->type & tlckTRUNCATE)) {
  3046. xtlck->hwm.offset =
  3047. le16_to_cpu(p->header.
  3048. nextindex) - 1;
  3049. tlck->type =
  3050. tlckXTREE | tlckTRUNCATE;
  3051. }
  3052. } else { /* COMMIT_WMAP */
  3053. /* free child extents covered by parent */
  3054. xadlock.xdlist = &p->xad[index + 1];
  3055. xadlock.count =
  3056. le16_to_cpu(p->header.nextindex) -
  3057. index - 1;
  3058. txFreeMap(ip, (struct maplock *) & xadlock,
  3059. NULL, COMMIT_WMAP);
  3060. }
  3061. BT_MARK_DIRTY(mp, ip);
  3062. p->header.nextindex = cpu_to_le16(index + 1);
  3063. }
  3064. XT_PUTPAGE(mp);
  3065. goto getParent;
  3066. }
  3067. /*
  3068. * child page was empty:
  3069. */
  3070. nfreed += lengthXAD(&p->xad[index]);
  3071. /*
  3072. * During working map update, child page's tlock must be handled
  3073. * before parent's. This is because the parent's tlock will cause
  3074. * the child's disk space to be marked available in the wmap, so
  3075. * it's important that the child page be released by that time.
  3076. *
  3077. * ToDo: tlocks should be on doubly-linked list, so we can
  3078. * quickly remove it and add it to the end.
  3079. */
  3080. /*
  3081. * Move parent page's tlock to the end of the tid's tlock list
  3082. */
  3083. if (log && mp->lid && (tblk->last != mp->lid) &&
  3084. lid_to_tlock(mp->lid)->tid) {
  3085. lid_t lid = mp->lid;
  3086. struct tlock *prev;
  3087. tlck = lid_to_tlock(lid);
  3088. if (tblk->next == lid)
  3089. tblk->next = tlck->next;
  3090. else {
  3091. for (prev = lid_to_tlock(tblk->next);
  3092. prev->next != lid;
  3093. prev = lid_to_tlock(prev->next)) {
  3094. assert(prev->next);
  3095. }
  3096. prev->next = tlck->next;
  3097. }
  3098. lid_to_tlock(tblk->last)->next = lid;
  3099. tlck->next = 0;
  3100. tblk->last = lid;
  3101. }
  3102. /*
  3103. * parent page become empty: free the page
  3104. */
  3105. if (index == XTENTRYSTART) {
  3106. if (log) { /* COMMIT_PWMAP */
  3107. /* txCommit() with tlckFREE:
  3108. * free child extents covered by parent;
  3109. * invalidate parent if COMMIT_PWMAP;
  3110. */
  3111. tlck = txLock(tid, ip, mp, tlckXTREE);
  3112. xtlck = (struct xtlock *) & tlck->lock;
  3113. xtlck->hwm.offset =
  3114. le16_to_cpu(p->header.nextindex) - 1;
  3115. tlck->type = tlckXTREE | tlckFREE;
  3116. } else { /* COMMIT_WMAP */
  3117. /* free child extents covered by parent */
  3118. xadlock.xdlist = &p->xad[XTENTRYSTART];
  3119. xadlock.count =
  3120. le16_to_cpu(p->header.nextindex) -
  3121. XTENTRYSTART;
  3122. txFreeMap(ip, (struct maplock *) & xadlock, NULL,
  3123. COMMIT_WMAP);
  3124. }
  3125. BT_MARK_DIRTY(mp, ip);
  3126. if (p->header.flag & BT_ROOT) {
  3127. p->header.flag &= ~BT_INTERNAL;
  3128. p->header.flag |= BT_LEAF;
  3129. p->header.nextindex = cpu_to_le16(XTENTRYSTART);
  3130. if (le16_to_cpu(p->header.maxentry) == XTROOTMAXSLOT) {
  3131. /*
  3132. * Shrink root down to allow inline
  3133. * EA (otherwise fsck complains)
  3134. */
  3135. p->header.maxentry =
  3136. cpu_to_le16(XTROOTINITSLOT);
  3137. JFS_IP(ip)->mode2 |= INLINEEA;
  3138. }
  3139. XT_PUTPAGE(mp); /* debug */
  3140. goto out;
  3141. } else {
  3142. if (log) { /* COMMIT_PWMAP */
  3143. /* page will be invalidated at tx completion
  3144. */
  3145. XT_PUTPAGE(mp);
  3146. } else { /* COMMIT_WMAP */
  3147. if (mp->lid)
  3148. lid_to_tlock(mp->lid)->flag |=
  3149. tlckFREELOCK;
  3150. /* invalidate parent page */
  3151. discard_metapage(mp);
  3152. }
  3153. /* parent has become empty and freed:
  3154. * go back up to its parent page
  3155. */
  3156. /* freed = 1; */
  3157. goto getParent;
  3158. }
  3159. }
  3160. /*
  3161. * parent page still has entries for front region;
  3162. */
  3163. else {
  3164. /* try truncate region covered by preceding entry
  3165. * (process backward)
  3166. */
  3167. index--;
  3168. /* go back down to the child page corresponding
  3169. * to the entry
  3170. */
  3171. goto getChild;
  3172. }
  3173. /*
  3174. * internal page: go down to child page of current entry
  3175. */
  3176. getChild:
  3177. /* save current parent entry for the child page */
  3178. if (BT_STACK_FULL(&btstack)) {
  3179. jfs_error(ip->i_sb, "stack overrun in xtTruncate!");
  3180. XT_PUTPAGE(mp);
  3181. return -EIO;
  3182. }
  3183. BT_PUSH(&btstack, bn, index);
  3184. /* get child page */
  3185. xad = &p->xad[index];
  3186. bn = addressXAD(xad);
  3187. /*
  3188. * first access of each internal entry:
  3189. */
  3190. /* release parent page */
  3191. XT_PUTPAGE(mp);
  3192. /* process the child page */
  3193. goto getPage;
  3194. out:
  3195. /*
  3196. * update file resource stat
  3197. */
  3198. /* set size
  3199. */
  3200. if (S_ISDIR(ip->i_mode) && !newsize)
  3201. ip->i_size = 1; /* fsck hates zero-length directories */
  3202. else
  3203. ip->i_size = newsize;
  3204. /* update quota allocation to reflect freed blocks */
  3205. dquot_free_block(ip, nfreed);
  3206. /*
  3207. * free tlock of invalidated pages
  3208. */
  3209. if (flag == COMMIT_WMAP)
  3210. txFreelock(ip);
  3211. return newsize;
  3212. }
  3213. /*
  3214. * xtTruncate_pmap()
  3215. *
  3216. * function:
  3217. * Perform truncate to zero length for deleted file, leaving the
  3218. * the xtree and working map untouched. This allows the file to
  3219. * be accessed via open file handles, while the delete of the file
  3220. * is committed to disk.
  3221. *
  3222. * parameter:
  3223. * tid_t tid,
  3224. * struct inode *ip,
  3225. * s64 committed_size)
  3226. *
  3227. * return: new committed size
  3228. *
  3229. * note:
  3230. *
  3231. * To avoid deadlock by holding too many transaction locks, the
  3232. * truncation may be broken up into multiple transactions.
  3233. * The committed_size keeps track of part of the file has been
  3234. * freed from the pmaps.
  3235. */
  3236. s64 xtTruncate_pmap(tid_t tid, struct inode *ip, s64 committed_size)
  3237. {
  3238. s64 bn;
  3239. struct btstack btstack;
  3240. int cmp;
  3241. int index;
  3242. int locked_leaves = 0;
  3243. struct metapage *mp;
  3244. xtpage_t *p;
  3245. struct btframe *parent;
  3246. int rc;
  3247. struct tblock *tblk;
  3248. struct tlock *tlck = NULL;
  3249. xad_t *xad;
  3250. int xlen;
  3251. s64 xoff;
  3252. struct xtlock *xtlck = NULL;
  3253. /* save object truncation type */
  3254. tblk = tid_to_tblock(tid);
  3255. tblk->xflag |= COMMIT_PMAP;
  3256. /* clear stack */
  3257. BT_CLR(&btstack);
  3258. if (committed_size) {
  3259. xoff = (committed_size >> JFS_SBI(ip->i_sb)->l2bsize) - 1;
  3260. rc = xtSearch(ip, xoff, NULL, &cmp, &btstack, 0);
  3261. if (rc)
  3262. return rc;
  3263. XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
  3264. if (cmp != 0) {
  3265. XT_PUTPAGE(mp);
  3266. jfs_error(ip->i_sb,
  3267. "xtTruncate_pmap: did not find extent");
  3268. return -EIO;
  3269. }
  3270. } else {
  3271. /*
  3272. * start with root
  3273. *
  3274. * root resides in the inode
  3275. */
  3276. bn = 0;
  3277. /*
  3278. * first access of each page:
  3279. */
  3280. getPage:
  3281. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  3282. if (rc)
  3283. return rc;
  3284. /* process entries backward from last index */
  3285. index = le16_to_cpu(p->header.nextindex) - 1;
  3286. if (p->header.flag & BT_INTERNAL)
  3287. goto getChild;
  3288. }
  3289. /*
  3290. * leaf page
  3291. */
  3292. if (++locked_leaves > MAX_TRUNCATE_LEAVES) {
  3293. /*
  3294. * We need to limit the size of the transaction
  3295. * to avoid exhausting pagecache & tlocks
  3296. */
  3297. xad = &p->xad[index];
  3298. xoff = offsetXAD(xad);
  3299. xlen = lengthXAD(xad);
  3300. XT_PUTPAGE(mp);
  3301. return (xoff + xlen) << JFS_SBI(ip->i_sb)->l2bsize;
  3302. }
  3303. tlck = txLock(tid, ip, mp, tlckXTREE);
  3304. tlck->type = tlckXTREE | tlckFREE;
  3305. xtlck = (struct xtlock *) & tlck->lock;
  3306. xtlck->hwm.offset = index;
  3307. XT_PUTPAGE(mp);
  3308. /*
  3309. * go back up to the parent page
  3310. */
  3311. getParent:
  3312. /* pop/restore parent entry for the current child page */
  3313. if ((parent = BT_POP(&btstack)) == NULL)
  3314. /* current page must have been root */
  3315. goto out;
  3316. /* get back the parent page */
  3317. bn = parent->bn;
  3318. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  3319. if (rc)
  3320. return rc;
  3321. index = parent->index;
  3322. /*
  3323. * parent page become empty: free the page
  3324. */
  3325. if (index == XTENTRYSTART) {
  3326. /* txCommit() with tlckFREE:
  3327. * free child extents covered by parent;
  3328. * invalidate parent if COMMIT_PWMAP;
  3329. */
  3330. tlck = txLock(tid, ip, mp, tlckXTREE);
  3331. xtlck = (struct xtlock *) & tlck->lock;
  3332. xtlck->hwm.offset = le16_to_cpu(p->header.nextindex) - 1;
  3333. tlck->type = tlckXTREE | tlckFREE;
  3334. XT_PUTPAGE(mp);
  3335. if (p->header.flag & BT_ROOT) {
  3336. goto out;
  3337. } else {
  3338. goto getParent;
  3339. }
  3340. }
  3341. /*
  3342. * parent page still has entries for front region;
  3343. */
  3344. else
  3345. index--;
  3346. /*
  3347. * internal page: go down to child page of current entry
  3348. */
  3349. getChild:
  3350. /* save current parent entry for the child page */
  3351. if (BT_STACK_FULL(&btstack)) {
  3352. jfs_error(ip->i_sb, "stack overrun in xtTruncate_pmap!");
  3353. XT_PUTPAGE(mp);
  3354. return -EIO;
  3355. }
  3356. BT_PUSH(&btstack, bn, index);
  3357. /* get child page */
  3358. xad = &p->xad[index];
  3359. bn = addressXAD(xad);
  3360. /*
  3361. * first access of each internal entry:
  3362. */
  3363. /* release parent page */
  3364. XT_PUTPAGE(mp);
  3365. /* process the child page */
  3366. goto getPage;
  3367. out:
  3368. return 0;
  3369. }
  3370. #ifdef CONFIG_JFS_STATISTICS
  3371. static int jfs_xtstat_proc_show(struct seq_file *m, void *v)
  3372. {
  3373. seq_printf(m,
  3374. "JFS Xtree statistics\n"
  3375. "====================\n"
  3376. "searches = %d\n"
  3377. "fast searches = %d\n"
  3378. "splits = %d\n",
  3379. xtStat.search,
  3380. xtStat.fastSearch,
  3381. xtStat.split);
  3382. return 0;
  3383. }
  3384. static int jfs_xtstat_proc_open(struct inode *inode, struct file *file)
  3385. {
  3386. return single_open(file, jfs_xtstat_proc_show, NULL);
  3387. }
  3388. const struct file_operations jfs_xtstat_proc_fops = {
  3389. .owner = THIS_MODULE,
  3390. .open = jfs_xtstat_proc_open,
  3391. .read = seq_read,
  3392. .llseek = seq_lseek,
  3393. .release = single_release,
  3394. };
  3395. #endif