48 #include <linux/slab.h>
64 #define IAGFREE_LOCK_INIT(imap) mutex_init(&imap->im_freelock)
65 #define IAGFREE_LOCK(imap) mutex_lock(&imap->im_freelock)
66 #define IAGFREE_UNLOCK(imap) mutex_unlock(&imap->im_freelock)
69 #define AG_LOCK_INIT(imap,index) mutex_init(&(imap->im_aglock[index]))
70 #define AG_LOCK(imap,agno) mutex_lock(&imap->im_aglock[agno])
71 #define AG_UNLOCK(imap,agno) mutex_unlock(&imap->im_aglock[agno])
76 static int diAllocAG(
struct inomap *,
int,
bool,
struct inode *);
77 static int diAllocAny(
struct inomap *,
int,
bool,
struct inode *);
78 static int diAllocBit(
struct inomap *,
struct iag *,
int);
79 static int diAllocExt(
struct inomap *,
int,
struct inode *);
80 static int diAllocIno(
struct inomap *,
int,
struct inode *);
81 static int diFindFree(
u32,
int);
82 static int diNewExt(
struct inomap *,
struct iag *,
int);
83 static int diNewIAG(
struct inomap *,
int *,
int,
struct metapage **);
86 static int diIAGRead(
struct inomap * imap,
int,
struct metapage **);
87 static int copy_from_dinode(
struct dinode *,
struct inode *);
88 static void copy_to_dinode(
struct dinode *,
struct inode *);
120 jfs_err(
"diMount: kmalloc returned NULL!");
142 for (index = 0; index <
MAXAG; index++) {
143 imap->im_agctl[
index].inofree =
145 imap->im_agctl[
index].extfree =
147 imap->im_agctl[
index].numinos =
149 imap->im_agctl[
index].numfree =
163 for (index = 0; index <
MAXAG; index++) {
171 JFS_IP(ipimap)->i_imap = imap;
193 struct inomap *imap = JFS_IP(ipimap)->i_imap;
199 if (!(mounterror || isReadOnly(ipimap)))
222 struct inomap *imp = JFS_IP(ipimap)->i_imap;
234 jfs_err(
"diSync: get_metapage failed!");
246 for (index = 0; index <
MAXAG; index++) {
307 int iagno,
ino, extno,
rc;
308 struct inode *ipimap;
316 unsigned long pageno;
322 JFS_IP(ip)->ipimap = ipimap;
328 imap = JFS_IP(ipimap)->i_imap;
330 rc = diIAGRead(imap, iagno, &mp);
333 jfs_err(
"diRead: diIAGRead returned %d", rc);
337 iagp = (
struct iag *) mp->
data;
362 if ((block_offset = ((
u32) blkno & (sbi->
nbperpage - 1)))) {
369 if (rel_inode < inodes_left)
373 rel_inode -= inodes_left;
380 jfs_err(
"diRead: read_metapage failed");
395 rc = copy_from_dinode(dp, ip);
400 JFS_IP(ip)->agstart = agstart;
401 JFS_IP(ip)->active_ag = -1;
439 jfs_err(
"diReadSpecial: new_inode returned NULL!");
448 JFS_IP(ip)->ipimap = sbi->
ipaimap;
455 address += inum >> 3;
470 if ((copy_from_dinode(dp, ip)) != 0) {
500 hlist_add_fake(&ip->
i_hash);
532 address += inum >> 3;
537 jfs_err(
"diWriteSpecial: failed to read aggregate inode "
547 copy_to_dinode(dp, ip);
565 jfs_err(
"diFreeSpecial called with NULL ip!");
610 unsigned long pageno;
613 struct inode *ipimap;
616 struct tlock *ditlck, *tlck;
617 struct linelock *dilinelock, *ilinelock;
627 JFS_IP(ipimap)->i_imap->im_nbperiext)) {
641 if ((block_offset = ((
u32) blkno & (sbi->
nbperpage - 1)))) {
648 if (rel_inode < inodes_left)
652 rel_inode -= inodes_left;
707 p = &jfs_ip->i_xtroot;
710 for (n = 0; n < ilinelock->
index; n++, lv++) {
723 if ((lid = jfs_ip->
blid) == 0)
743 p = &jfs_ip->i_xtroot;
746 for (n = 0; n < ilinelock->
index; n++, lv++) {
770 for (n = 0; n < ilinelock->
index; n++, lv++) {
783 lv = & dilinelock->
lv[dilinelock->
index];
794 lv = & dilinelock->
lv[dilinelock->
index];
806 lv = & dilinelock->
lv[dilinelock->
index];
808 copy_to_dinode(dp, ip);
811 memcpy(&dp->di_dirtable, &jfs_ip->i_dirtable, 96);
867 struct iag *iagp, *aiagp, *biagp, *ciagp, *diagp;
869 int iagno,
ino, extno, bitno, sword, agno;
872 struct inode *ipimap = JFS_SBI(ip->
i_sb)->ipimap;
873 struct inomap *imap = JFS_IP(ipimap)->i_imap;
876 struct inode *iplist[3];
884 aiagp = biagp = ciagp = diagp =
NULL;
893 if (iagno >= imap->im_nextiag) {
897 "diFree: inum = %d, iagno = %d, nextiag = %d",
898 (
uint) inum, iagno, imap->im_nextiag);
904 agno =
BLKTOAG(JFS_IP(ip)->agstart, JFS_SBI(ip->
i_sb));
917 if ((rc = diIAGRead(imap, iagno, &mp))) {
922 iagp = (
struct iag *) mp->
data;
934 "diFree: wmap shows inode already free");
949 if (imap->im_agctl[agno].numfree > imap->im_agctl[agno].numinos) {
961 imap->im_agctl[agno].numfree < 96 ||
962 (imap->im_agctl[agno].numfree < 288 &&
963 (((imap->im_agctl[agno].numfree * 100) /
964 imap->im_agctl[agno].numinos) <= 25))) {
975 if ((fwd = imap->im_agctl[agno].inofree) >= 0) {
979 if ((rc = diIAGRead(imap, fwd, &))) {
985 aiagp = (
struct iag *) amp->
data;
1000 imap->im_agctl[agno].inofree = iagno;
1024 imap->im_agctl[agno].numfree += 1;
1046 amp = bmp = cmp = dmp =
NULL;
1059 if ((fwd = imap->im_agctl[agno].extfree) >= 0) {
1060 if ((rc = diIAGRead(imap, fwd, &)))
1062 aiagp = (
struct iag *) amp->
data;
1077 if ((rc = diIAGRead(imap, fwd, &)))
1079 aiagp = (
struct iag *) amp->
data;
1083 if ((rc = diIAGRead(imap, back, &bmp)))
1085 biagp = (
struct iag *) bmp->
data;
1105 if (inofreefwd >= 0) {
1107 if (inofreefwd == fwd)
1108 ciagp = (
struct iag *) amp->
data;
1109 else if (inofreefwd == back)
1110 ciagp = (
struct iag *) bmp->
data;
1113 diIAGRead(imap, inofreefwd, &cmp)))
1115 ciagp = (
struct iag *) cmp->
data;
1122 diagp = (
struct iag *) amp->
data;
1124 diagp = (
struct iag *) bmp->
data;
1129 diagp = (
struct iag *) dmp->
data;
1140 freepxd = iagp->inoext[extno];
1149 if (iagp->nfreeexts == 0) {
1156 imap->im_agctl[agno].extfree = iagno;
1163 aiagp->extfreeback = iagp->extfreeback;
1166 biagp->extfreefwd = iagp->extfreefwd;
1168 imap->im_agctl[agno].extfree =
1171 iagp->extfreefwd = iagp->extfreeback =
cpu_to_le32(-1);
1175 imap->im_freeiag = iagno;
1185 ciagp->inofreeback = iagp->inofreeback;
1188 diagp->inofreefwd = iagp->inofreefwd;
1190 imap->im_agctl[agno].inofree =
1193 iagp->inofreefwd = iagp->inofreeback =
cpu_to_le32(-1);
1201 if (iagp->pmap[extno] != 0) {
1202 jfs_error(
ip->i_sb,
"diFree: the pmap does not show inode free");
1204 iagp->wmap[extno] = 0;
1222 le32_add_cpu(&iagp->nfreeinos, -(
INOSPEREXT - 1));
1223 le32_add_cpu(&iagp->nfreeexts, 1);
1228 imap->im_agctl[agno].numfree -= (
INOSPEREXT - 1);
1234 write_metapage(amp);
1236 write_metapage(bmp);
1238 write_metapage(cmp);
1240 write_metapage(dmp);
1262 pxdlock = (
struct pxd_lock *) & tlck->lock;
1264 pxdlock->pxd = freepxd;
1279 iplist[1] = (
struct inode *) (
size_t)iagno;
1280 iplist[2] = (
struct inode *) (
size_t)extno;
1316 diInitInode(
struct inode *
ip,
int iagno,
int ino,
int extno,
struct iag * iagp)
1345 int rc,
ino, iagno, addext, extno, bitno, sword;
1346 int nwords, rem,
i, agno;
1348 struct inode *ipimap;
1357 ipimap = JFS_SBI(pip->
i_sb)->ipimap;
1358 imap = JFS_IP(ipimap)->i_imap;
1359 JFS_IP(ip)->ipimap = ipimap;
1381 agno =
BLKTOAG(JFS_IP(pip)->agstart, JFS_SBI(pip->
i_sb));
1394 inum = pip->
i_ino + 1;
1409 if ((rc = diIAGRead(imap, iagno, &mp))) {
1414 iagp = (
struct iag *) mp->
data;
1420 addext = (imap->im_agctl[agno].numfree < 32 && iagp->
nfreeexts);
1447 rc = diAllocBit(imap, iagp, ino);
1455 diInitInode(ip, iagno, ino, extno,
1503 for (i = 0; i < nwords; i++) {
1512 rem = diFindFree(inosmap, 0);
1521 "diAlloc: can't find free bit "
1531 rc = diAllocBit(imap, iagp, ino);
1539 diInitInode(ip, iagno, ino, extno,
1556 if (addext && ~extsmap) {
1560 rem = diFindFree(extsmap, 0);
1565 if ((rc = diNewExt(imap, iagp, extno))) {
1578 diInitInode(ip, iagno,
1579 extno << L2INOSPEREXT,
1593 sword = (sword ==
SMAPSZ - 1) ? 0 : sword + 1;
1608 rc = diAllocAG(imap, agno, dir, ip);
1618 return (diAllocAny(imap, agno, dir, ip));
1652 diAllocAG(
struct inomap * imap,
int agno,
bool dir,
struct inode *
ip)
1654 int rc, addext, numfree, numinos;
1659 numfree = imap->im_agctl[agno].numfree;
1660 numinos = imap->im_agctl[agno].numinos;
1662 if (numfree > numinos) {
1673 addext = (numfree < 64 ||
1675 && ((numfree * 100) / numinos) <= 20));
1677 addext = (numfree == 0);
1687 if ((rc = diAllocExt(imap, agno, ip)) != -
ENOSPC)
1694 return (diAllocIno(imap, agno, ip));
1722 diAllocAny(
struct inomap * imap,
int agno,
bool dir,
struct inode *ip)
1725 int maxag = JFS_SBI(imap->
im_ipimap->i_sb)->bmap->db_maxag;
1731 for (ag = agno + 1; ag <= maxag; ag++) {
1734 rc = diAllocAG(imap, ag, dir, ip);
1744 for (ag = 0; ag < agno; ag++) {
1747 rc = diAllocAG(imap, ag, dir, ip);
1786 static int diAllocIno(
struct inomap * imap,
int agno,
struct inode *ip)
1788 int iagno,
ino,
rc, rem, extno, sword;
1794 if ((iagno = imap->im_agctl[agno].inofree) < 0)
1802 if ((rc = diIAGRead(imap, iagno, &mp))) {
1806 iagp = (
struct iag *) mp->
data;
1815 "diAllocIno: nfreeinos = 0, but iag on freelist");
1822 for (sword = 0;; sword++) {
1827 "diAllocIno: free inode not found in summary map");
1863 rc = diAllocBit(imap, iagp, ino);
1872 diInitInode(ip, iagno, ino, extno, iagp);
1911 static int diAllocExt(
struct inomap * imap,
int agno,
struct inode *ip)
1913 int rem, iagno, sword, extno,
rc;
1920 if ((iagno = imap->im_agctl[agno].extfree) < 0) {
1924 if ((rc = diNewIAG(imap, &iagno, agno, &mp))) {
1927 iagp = (
struct iag *) mp->
data;
1937 if ((rc = diIAGRead(imap, iagno, &mp))) {
1942 iagp = (
struct iag *) mp->
data;
1947 for (sword = 0;; sword++) {
1952 "diAllocExt: free ext summary map not found");
1972 rc = diNewExt(imap, iagp, extno);
1982 imap->im_freeiag = iagno;
1991 diInitInode(ip, iagno, extno <<
L2INOSPEREXT, extno, iagp);
2029 static int diAllocBit(
struct inomap * imap,
struct iag * iagp,
int ino)
2031 int extno, bitno, agno, sword,
rc;
2047 aiagp = (
struct iag *) amp->
data;
2059 biagp = (
struct iag *) bmp->data;
2085 "diAllocBit: iag inconsistent");
2109 write_metapage(amp);
2114 write_metapage(bmp);
2116 imap->im_agctl[agno].inofree =
2126 imap->im_agctl[agno].numfree -= 1;
2176 static int diNewExt(
struct inomap * imap,
struct iag * iagp,
int extno)
2178 int agno, iagno, fwd, back, freei = 0, sword,
rc;
2180 struct metapage *amp, *bmp, *cmp, *dmp;
2181 struct inode *ipimap;
2199 sbi = JFS_SBI(ipimap->
i_sb);
2201 amp = bmp = cmp =
NULL;
2215 if ((rc = diIAGRead(imap, fwd, &)))
2217 aiagp = (
struct iag *) amp->
data;
2221 if ((rc = diIAGRead(imap, back, &bmp)))
2223 biagp = (
struct iag *) bmp->
data;
2234 if ((fwd = imap->im_agctl[agno].extfree) >= 0) {
2235 if ((rc = diIAGRead(imap, fwd, &)))
2237 aiagp = (
struct iag *) amp->
data;
2250 freei = imap->im_agctl[agno].inofree;
2255 }
else if (freei == back) {
2258 if ((rc = diIAGRead(imap, freei, &cmp)))
2260 ciagp = (
struct iag *) cmp->
data;
2262 if (ciagp ==
NULL) {
2264 "diNewExt: ciagp == NULL");
2274 hint = ((
s64) agno << sbi->
bmap->db_agl2size) - 1;
2279 if ((rc =
dbAlloc(ipimap, hint, (
s64) imap->im_nbperiext, &blkno)))
2285 ino = (iagno <<
L2INOSPERIAG) + (extno << L2INOSPEREXT);
2290 for (i = 0; i < imap->im_nbperiext; i += sbi->
nbperpage) {
2310 PXDlength(&(dp->di_ixpxd), imap->im_nbperiext);
2312 write_metapage(dmp);
2325 imap->im_agctl[agno].extfree =
2339 imap->im_agctl[agno].extfree = iagno;
2353 imap->im_agctl[agno].inofree = iagno;
2365 iagp->
pmap[extno] = 0;
2384 imap->im_agctl[agno].numfree += (
INOSPEREXT - 1);
2395 write_metapage(amp);
2397 write_metapage(bmp);
2399 write_metapage(cmp);
2458 diNewIAG(
struct inomap * imap,
int *iagnop,
int agno,
struct metapage ** mpp)
2462 struct inode *ipimap;
2470 struct inode *iplist[1];
2483 if (imap->im_freeiag >= 0) {
2485 iagno = imap->im_freeiag;
2501 "diNewIAG: ipimap->i_size is wrong");
2507 iagno = imap->im_nextiag;
2528 if ((rc =
dbAlloc(ipimap, 0, (
s64) xlen, &xaddr))) {
2544 xtInsert(tid, ipimap, 0, blkno, xlen, &xaddr, 0))) {
2582 iagp = (
struct iag *) mp->
data;
2596 for (i = 0; i <
SMAPSZ; i++)
2615 duplicateIXtree(sb, blkno, xlen, &xaddr);
2618 imap->im_nextiag += 1;
2623 imap->im_freeiag = iagno;
2638 if ((rc = diIAGRead(imap, iagno, &mp))) {
2643 iagp = (
struct iag *) mp->
data;
2681 static int diIAGRead(
struct inomap * imap,
int iagno,
struct metapage ** mpp)
2687 blkno =
IAGTOLBLK(iagno, JFS_SBI(ipimap->
i_sb)->l2nbperpage);
2717 for (word <<= start, bitno = start; bitno < 32;
2718 bitno++, word <<= 1) {
2744 unsigned long inum,
bool is_free,
struct tblock * tblk)
2749 int iagno,
ino, extno, bitno;
2753 int lsn, difft, diffp;
2754 unsigned long flags;
2756 imap = JFS_IP(ipimap)->i_imap;
2760 if (iagno >= imap->im_nextiag) {
2762 "diUpdatePMap: the iag is outside the map");
2767 rc = diIAGRead(imap, iagno, &mp);
2771 metapage_wait_for_io(mp);
2772 iagp = (
struct iag *) mp->
data;
2791 "diUpdatePMap: inode %ld not marked as "
2792 "allocated in wmap!", inum);
2796 "diUpdatePMap: inode %ld not marked as "
2797 "allocated in pmap!", inum);
2812 "diUpdatePMap: the inode is not allocated in "
2819 "diUpdatePMap: the inode is not free in the "
2830 log = JFS_SBI(tblk->
sb)->log;
2836 if (difft < diffp) {
2871 struct inomap *imap = JFS_IP(ipimap)->i_imap;
2873 struct bmap *mp = JFS_SBI(ipbmap->
i_sb)->bmap;
2876 int numinos, xnuminos = 0, xnumfree = 0;
2879 jfs_info(
"diExtendFS: nextiag:%d numinos:%d numfree:%d",
2892 for (i = 0; i <
MAXAG; i++) {
2893 imap->im_agctl[
i].inofree = -1;
2894 imap->im_agctl[
i].extfree = -1;
2895 imap->im_agctl[
i].numinos = 0;
2896 imap->im_agctl[
i].numfree = 0;
2904 for (i = 0; i < imap->im_nextiag; i++) {
2905 if ((rc = diIAGRead(imap, i, &bp))) {
2909 iagp = (
struct iag *) bp->
data;
2913 "diExtendFs: unexpected value of iagnum");
2924 n = agstart >> mp->db_agl2size;
2932 imap->im_agctl[
n].numinos += numinos;
2933 xnuminos += numinos;
2938 if ((head = imap->im_agctl[n].inofree) == -1) {
2942 if ((rc = diIAGRead(imap, head, &hbp))) {
2946 hiagp = (
struct iag *) hbp->
data;
2947 hiagp->inofreeback = iagp->
iagnum;
2950 write_metapage(hbp);
2953 imap->im_agctl[
n].inofree =
2957 imap->im_agctl[
n].numfree +=
2964 if ((head = imap->im_agctl[n].extfree) == -1) {
2968 if ((rc = diIAGRead(imap, head, &hbp))) {
2972 hiagp = (
struct iag *) hbp->
data;
2973 hiagp->extfreeback = iagp->
iagnum;
2976 write_metapage(hbp);
2979 imap->im_agctl[
n].extfree =
2990 "diExtendFs: numinos or numfree incorrect");
3006 int xlen,
s64 *xaddr)
3009 struct buffer_head *bh;
3033 if (
xtInsert(tid, ip, 0, blkno, xlen, xaddr, 0)) {
3057 static int copy_from_dinode(
struct dinode * dip,
struct inode *ip)
3067 if (sbi->
umask != -1) {
3082 if (!uid_valid(sbi->
uid))
3089 if (!gid_valid(sbi->
gid))
3114 ip->
i_rdev = new_decode_dev(jfs_ip->
dev);
3118 memcpy(&jfs_ip->i_dirtable, &dip->di_dirtable, 384);
3120 memcpy(&jfs_ip->i_xtroot, &dip->di_xtroot, 288);
3122 memcpy(&jfs_ip->i_inline_ea, &dip->di_inlineea, 128);
3141 static void copy_to_dinode(
struct dinode * dip,
struct inode *ip)
3153 if (!uid_valid(sbi->
uid))
3158 if (!gid_valid(sbi->
gid))
3168 if (sbi->
umask == -1)