11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * Copyright (C) International Business Machines Corp., 2000-2004
41da177e4SLinus Torvalds */
51da177e4SLinus Torvalds
61da177e4SLinus Torvalds /*
71da177e4SLinus Torvalds * jfs_imap.c: inode allocation map manager
81da177e4SLinus Torvalds *
91da177e4SLinus Torvalds * Serialization:
101da177e4SLinus Torvalds * Each AG has a simple lock which is used to control the serialization of
111da177e4SLinus Torvalds * the AG level lists. This lock should be taken first whenever an AG
121da177e4SLinus Torvalds * level list will be modified or accessed.
131da177e4SLinus Torvalds *
141da177e4SLinus Torvalds * Each IAG is locked by obtaining the buffer for the IAG page.
151da177e4SLinus Torvalds *
161da177e4SLinus Torvalds * There is also a inode lock for the inode map inode. A read lock needs to
171da177e4SLinus Torvalds * be taken whenever an IAG is read from the map or the global level
181da177e4SLinus Torvalds * information is read. A write lock needs to be taken whenever the global
191da177e4SLinus Torvalds * level information is modified or an atomic operation needs to be used.
201da177e4SLinus Torvalds *
211da177e4SLinus Torvalds * If more than one IAG is read at one time, the read lock may not
221da177e4SLinus Torvalds * be given up until all of the IAG's are read. Otherwise, a deadlock
231da177e4SLinus Torvalds * may occur when trying to obtain the read lock while another thread
241da177e4SLinus Torvalds * holding the read lock is waiting on the IAG already being held.
251da177e4SLinus Torvalds *
261da177e4SLinus Torvalds * The control page of the inode map is read into memory by diMount().
271da177e4SLinus Torvalds * Thereafter it should only be modified in memory and then it will be
281da177e4SLinus Torvalds * written out when the filesystem is unmounted by diUnmount().
291da177e4SLinus Torvalds */
301da177e4SLinus Torvalds
311da177e4SLinus Torvalds #include <linux/fs.h>
321da177e4SLinus Torvalds #include <linux/buffer_head.h>
331da177e4SLinus Torvalds #include <linux/pagemap.h>
341da177e4SLinus Torvalds #include <linux/quotaops.h>
355a0e3ad6STejun Heo #include <linux/slab.h>
361da177e4SLinus Torvalds
371da177e4SLinus Torvalds #include "jfs_incore.h"
381868f4aaSDave Kleikamp #include "jfs_inode.h"
391da177e4SLinus Torvalds #include "jfs_filsys.h"
401da177e4SLinus Torvalds #include "jfs_dinode.h"
411da177e4SLinus Torvalds #include "jfs_dmap.h"
421da177e4SLinus Torvalds #include "jfs_imap.h"
431da177e4SLinus Torvalds #include "jfs_metapage.h"
441da177e4SLinus Torvalds #include "jfs_superblock.h"
451da177e4SLinus Torvalds #include "jfs_debug.h"
461da177e4SLinus Torvalds
471da177e4SLinus Torvalds /*
481da177e4SLinus Torvalds * imap locks
491da177e4SLinus Torvalds */
501da177e4SLinus Torvalds /* iag free list lock */
511de87444SIngo Molnar #define IAGFREE_LOCK_INIT(imap) mutex_init(&imap->im_freelock)
521de87444SIngo Molnar #define IAGFREE_LOCK(imap) mutex_lock(&imap->im_freelock)
531de87444SIngo Molnar #define IAGFREE_UNLOCK(imap) mutex_unlock(&imap->im_freelock)
541da177e4SLinus Torvalds
551da177e4SLinus Torvalds /* per ag iag list locks */
561de87444SIngo Molnar #define AG_LOCK_INIT(imap,index) mutex_init(&(imap->im_aglock[index]))
571de87444SIngo Molnar #define AG_LOCK(imap,agno) mutex_lock(&imap->im_aglock[agno])
581de87444SIngo Molnar #define AG_UNLOCK(imap,agno) mutex_unlock(&imap->im_aglock[agno])
591da177e4SLinus Torvalds
601da177e4SLinus Torvalds /*
611da177e4SLinus Torvalds * forward references
621da177e4SLinus Torvalds */
634d81715fSRichard Knutsson static int diAllocAG(struct inomap *, int, bool, struct inode *);
644d81715fSRichard Knutsson static int diAllocAny(struct inomap *, int, bool, struct inode *);
651da177e4SLinus Torvalds static int diAllocBit(struct inomap *, struct iag *, int);
661da177e4SLinus Torvalds static int diAllocExt(struct inomap *, int, struct inode *);
671da177e4SLinus Torvalds static int diAllocIno(struct inomap *, int, struct inode *);
681da177e4SLinus Torvalds static int diFindFree(u32, int);
691da177e4SLinus Torvalds static int diNewExt(struct inomap *, struct iag *, int);
701da177e4SLinus Torvalds static int diNewIAG(struct inomap *, int *, int, struct metapage **);
711da177e4SLinus Torvalds static void duplicateIXtree(struct super_block *, s64, int, s64 *);
721da177e4SLinus Torvalds
731da177e4SLinus Torvalds static int diIAGRead(struct inomap * imap, int, struct metapage **);
741da177e4SLinus Torvalds static int copy_from_dinode(struct dinode *, struct inode *);
751da177e4SLinus Torvalds static void copy_to_dinode(struct dinode *, struct inode *);
761da177e4SLinus Torvalds
771da177e4SLinus Torvalds /*
781da177e4SLinus Torvalds * NAME: diMount()
791da177e4SLinus Torvalds *
801da177e4SLinus Torvalds * FUNCTION: initialize the incore inode map control structures for
811da177e4SLinus Torvalds * a fileset or aggregate init time.
821da177e4SLinus Torvalds *
831da177e4SLinus Torvalds * the inode map's control structure (dinomap) is
841da177e4SLinus Torvalds * brought in from disk and placed in virtual memory.
851da177e4SLinus Torvalds *
861da177e4SLinus Torvalds * PARAMETERS:
871da177e4SLinus Torvalds * ipimap - pointer to inode map inode for the aggregate or fileset.
881da177e4SLinus Torvalds *
891da177e4SLinus Torvalds * RETURN VALUES:
901da177e4SLinus Torvalds * 0 - success
911da177e4SLinus Torvalds * -ENOMEM - insufficient free virtual memory.
921da177e4SLinus Torvalds * -EIO - i/o error.
931da177e4SLinus Torvalds */
diMount(struct inode * ipimap)941da177e4SLinus Torvalds int diMount(struct inode *ipimap)
951da177e4SLinus Torvalds {
961da177e4SLinus Torvalds struct inomap *imap;
971da177e4SLinus Torvalds struct metapage *mp;
981da177e4SLinus Torvalds int index;
991da177e4SLinus Torvalds struct dinomap_disk *dinom_le;
1001da177e4SLinus Torvalds
1011da177e4SLinus Torvalds /*
1021da177e4SLinus Torvalds * allocate/initialize the in-memory inode map control structure
1031da177e4SLinus Torvalds */
1041da177e4SLinus Torvalds /* allocate the in-memory inode map control structure. */
1055cbded58SRobert P. J. Day imap = kmalloc(sizeof(struct inomap), GFP_KERNEL);
106e15a56b7SZhen Lei if (imap == NULL)
1071da177e4SLinus Torvalds return -ENOMEM;
1081da177e4SLinus Torvalds
1091da177e4SLinus Torvalds /* read the on-disk inode map control structure. */
1101da177e4SLinus Torvalds
1111da177e4SLinus Torvalds mp = read_metapage(ipimap,
1121da177e4SLinus Torvalds IMAPBLKNO << JFS_SBI(ipimap->i_sb)->l2nbperpage,
1131da177e4SLinus Torvalds PSIZE, 0);
1141da177e4SLinus Torvalds if (mp == NULL) {
1151da177e4SLinus Torvalds kfree(imap);
1161da177e4SLinus Torvalds return -EIO;
1171da177e4SLinus Torvalds }
1181da177e4SLinus Torvalds
1191da177e4SLinus Torvalds /* copy the on-disk version to the in-memory version. */
1201da177e4SLinus Torvalds dinom_le = (struct dinomap_disk *) mp->data;
1211da177e4SLinus Torvalds imap->im_freeiag = le32_to_cpu(dinom_le->in_freeiag);
1221da177e4SLinus Torvalds imap->im_nextiag = le32_to_cpu(dinom_le->in_nextiag);
1231da177e4SLinus Torvalds atomic_set(&imap->im_numinos, le32_to_cpu(dinom_le->in_numinos));
1241da177e4SLinus Torvalds atomic_set(&imap->im_numfree, le32_to_cpu(dinom_le->in_numfree));
1251da177e4SLinus Torvalds imap->im_nbperiext = le32_to_cpu(dinom_le->in_nbperiext);
1261da177e4SLinus Torvalds imap->im_l2nbperiext = le32_to_cpu(dinom_le->in_l2nbperiext);
1271da177e4SLinus Torvalds for (index = 0; index < MAXAG; index++) {
1281da177e4SLinus Torvalds imap->im_agctl[index].inofree =
1291da177e4SLinus Torvalds le32_to_cpu(dinom_le->in_agctl[index].inofree);
1301da177e4SLinus Torvalds imap->im_agctl[index].extfree =
1311da177e4SLinus Torvalds le32_to_cpu(dinom_le->in_agctl[index].extfree);
1321da177e4SLinus Torvalds imap->im_agctl[index].numinos =
1331da177e4SLinus Torvalds le32_to_cpu(dinom_le->in_agctl[index].numinos);
1341da177e4SLinus Torvalds imap->im_agctl[index].numfree =
1351da177e4SLinus Torvalds le32_to_cpu(dinom_le->in_agctl[index].numfree);
1361da177e4SLinus Torvalds }
1371da177e4SLinus Torvalds
1381da177e4SLinus Torvalds /* release the buffer. */
1391da177e4SLinus Torvalds release_metapage(mp);
1401da177e4SLinus Torvalds
1411da177e4SLinus Torvalds /*
1421da177e4SLinus Torvalds * allocate/initialize inode allocation map locks
1431da177e4SLinus Torvalds */
1441da177e4SLinus Torvalds /* allocate and init iag free list lock */
1451da177e4SLinus Torvalds IAGFREE_LOCK_INIT(imap);
1461da177e4SLinus Torvalds
1471da177e4SLinus Torvalds /* allocate and init ag list locks */
1481da177e4SLinus Torvalds for (index = 0; index < MAXAG; index++) {
1491da177e4SLinus Torvalds AG_LOCK_INIT(imap, index);
1501da177e4SLinus Torvalds }
1511da177e4SLinus Torvalds
1521da177e4SLinus Torvalds /* bind the inode map inode and inode map control structure
1531da177e4SLinus Torvalds * to each other.
1541da177e4SLinus Torvalds */
1551da177e4SLinus Torvalds imap->im_ipimap = ipimap;
1561da177e4SLinus Torvalds JFS_IP(ipimap)->i_imap = imap;
1571da177e4SLinus Torvalds
1581da177e4SLinus Torvalds return (0);
1591da177e4SLinus Torvalds }
1601da177e4SLinus Torvalds
1611da177e4SLinus Torvalds
1621da177e4SLinus Torvalds /*
1631da177e4SLinus Torvalds * NAME: diUnmount()
1641da177e4SLinus Torvalds *
1651da177e4SLinus Torvalds * FUNCTION: write to disk the incore inode map control structures for
1661da177e4SLinus Torvalds * a fileset or aggregate at unmount time.
1671da177e4SLinus Torvalds *
1681da177e4SLinus Torvalds * PARAMETERS:
1691da177e4SLinus Torvalds * ipimap - pointer to inode map inode for the aggregate or fileset.
1701da177e4SLinus Torvalds *
1711da177e4SLinus Torvalds * RETURN VALUES:
1721da177e4SLinus Torvalds * 0 - success
1731da177e4SLinus Torvalds * -ENOMEM - insufficient free virtual memory.
1741da177e4SLinus Torvalds * -EIO - i/o error.
1751da177e4SLinus Torvalds */
diUnmount(struct inode * ipimap,int mounterror)1761da177e4SLinus Torvalds int diUnmount(struct inode *ipimap, int mounterror)
1771da177e4SLinus Torvalds {
1781da177e4SLinus Torvalds struct inomap *imap = JFS_IP(ipimap)->i_imap;
1791da177e4SLinus Torvalds
1801da177e4SLinus Torvalds /*
1811da177e4SLinus Torvalds * update the on-disk inode map control structure
1821da177e4SLinus Torvalds */
1831da177e4SLinus Torvalds
1841da177e4SLinus Torvalds if (!(mounterror || isReadOnly(ipimap)))
1851da177e4SLinus Torvalds diSync(ipimap);
1861da177e4SLinus Torvalds
1871da177e4SLinus Torvalds /*
1881da177e4SLinus Torvalds * Invalidate the page cache buffers
1891da177e4SLinus Torvalds */
1901da177e4SLinus Torvalds truncate_inode_pages(ipimap->i_mapping, 0);
1911da177e4SLinus Torvalds
1921da177e4SLinus Torvalds /*
1931da177e4SLinus Torvalds * free in-memory control structure
1941da177e4SLinus Torvalds */
1951da177e4SLinus Torvalds kfree(imap);
1966e2bda2cSLiu Shixin via Jfs-discussion JFS_IP(ipimap)->i_imap = NULL;
1971da177e4SLinus Torvalds
1981da177e4SLinus Torvalds return (0);
1991da177e4SLinus Torvalds }
2001da177e4SLinus Torvalds
2011da177e4SLinus Torvalds
2021da177e4SLinus Torvalds /*
2031da177e4SLinus Torvalds * diSync()
2041da177e4SLinus Torvalds */
diSync(struct inode * ipimap)2051da177e4SLinus Torvalds int diSync(struct inode *ipimap)
2061da177e4SLinus Torvalds {
2071da177e4SLinus Torvalds struct dinomap_disk *dinom_le;
2081da177e4SLinus Torvalds struct inomap *imp = JFS_IP(ipimap)->i_imap;
2091da177e4SLinus Torvalds struct metapage *mp;
2101da177e4SLinus Torvalds int index;
2111da177e4SLinus Torvalds
2121da177e4SLinus Torvalds /*
2131da177e4SLinus Torvalds * write imap global conrol page
2141da177e4SLinus Torvalds */
2151da177e4SLinus Torvalds /* read the on-disk inode map control structure */
2161da177e4SLinus Torvalds mp = get_metapage(ipimap,
2171da177e4SLinus Torvalds IMAPBLKNO << JFS_SBI(ipimap->i_sb)->l2nbperpage,
2181da177e4SLinus Torvalds PSIZE, 0);
2191da177e4SLinus Torvalds if (mp == NULL) {
2201da177e4SLinus Torvalds jfs_err("diSync: get_metapage failed!");
2211da177e4SLinus Torvalds return -EIO;
2221da177e4SLinus Torvalds }
2231da177e4SLinus Torvalds
2241da177e4SLinus Torvalds /* copy the in-memory version to the on-disk version */
2251da177e4SLinus Torvalds dinom_le = (struct dinomap_disk *) mp->data;
2261da177e4SLinus Torvalds dinom_le->in_freeiag = cpu_to_le32(imp->im_freeiag);
2271da177e4SLinus Torvalds dinom_le->in_nextiag = cpu_to_le32(imp->im_nextiag);
2281da177e4SLinus Torvalds dinom_le->in_numinos = cpu_to_le32(atomic_read(&imp->im_numinos));
2291da177e4SLinus Torvalds dinom_le->in_numfree = cpu_to_le32(atomic_read(&imp->im_numfree));
2301da177e4SLinus Torvalds dinom_le->in_nbperiext = cpu_to_le32(imp->im_nbperiext);
2311da177e4SLinus Torvalds dinom_le->in_l2nbperiext = cpu_to_le32(imp->im_l2nbperiext);
2321da177e4SLinus Torvalds for (index = 0; index < MAXAG; index++) {
2331da177e4SLinus Torvalds dinom_le->in_agctl[index].inofree =
2341da177e4SLinus Torvalds cpu_to_le32(imp->im_agctl[index].inofree);
2351da177e4SLinus Torvalds dinom_le->in_agctl[index].extfree =
2361da177e4SLinus Torvalds cpu_to_le32(imp->im_agctl[index].extfree);
2371da177e4SLinus Torvalds dinom_le->in_agctl[index].numinos =
2381da177e4SLinus Torvalds cpu_to_le32(imp->im_agctl[index].numinos);
2391da177e4SLinus Torvalds dinom_le->in_agctl[index].numfree =
2401da177e4SLinus Torvalds cpu_to_le32(imp->im_agctl[index].numfree);
2411da177e4SLinus Torvalds }
2421da177e4SLinus Torvalds
2431da177e4SLinus Torvalds /* write out the control structure */
2441da177e4SLinus Torvalds write_metapage(mp);
2451da177e4SLinus Torvalds
2461da177e4SLinus Torvalds /*
2471da177e4SLinus Torvalds * write out dirty pages of imap
2481da177e4SLinus Torvalds */
24928fd1298SOGAWA Hirofumi filemap_write_and_wait(ipimap->i_mapping);
2501da177e4SLinus Torvalds
2511da177e4SLinus Torvalds diWriteSpecial(ipimap, 0);
2521da177e4SLinus Torvalds
2531da177e4SLinus Torvalds return (0);
2541da177e4SLinus Torvalds }
2551da177e4SLinus Torvalds
2561da177e4SLinus Torvalds
2571da177e4SLinus Torvalds /*
2581da177e4SLinus Torvalds * NAME: diRead()
2591da177e4SLinus Torvalds *
2601da177e4SLinus Torvalds * FUNCTION: initialize an incore inode from disk.
2611da177e4SLinus Torvalds *
2621da177e4SLinus Torvalds * on entry, the specifed incore inode should itself
2631da177e4SLinus Torvalds * specify the disk inode number corresponding to the
2641da177e4SLinus Torvalds * incore inode (i.e. i_number should be initialized).
2651da177e4SLinus Torvalds *
2661da177e4SLinus Torvalds * this routine handles incore inode initialization for
2671da177e4SLinus Torvalds * both "special" and "regular" inodes. special inodes
2681da177e4SLinus Torvalds * are those required early in the mount process and
2691da177e4SLinus Torvalds * require special handling since much of the file system
2701da177e4SLinus Torvalds * is not yet initialized. these "special" inodes are
2711da177e4SLinus Torvalds * identified by a NULL inode map inode pointer and are
2721da177e4SLinus Torvalds * actually initialized by a call to diReadSpecial().
2731da177e4SLinus Torvalds *
2741da177e4SLinus Torvalds * for regular inodes, the iag describing the disk inode
2751da177e4SLinus Torvalds * is read from disk to determine the inode extent address
2761da177e4SLinus Torvalds * for the disk inode. with the inode extent address in
2771da177e4SLinus Torvalds * hand, the page of the extent that contains the disk
2781da177e4SLinus Torvalds * inode is read and the disk inode is copied to the
2791da177e4SLinus Torvalds * incore inode.
2801da177e4SLinus Torvalds *
2811da177e4SLinus Torvalds * PARAMETERS:
2821da177e4SLinus Torvalds * ip - pointer to incore inode to be initialized from disk.
2831da177e4SLinus Torvalds *
2841da177e4SLinus Torvalds * RETURN VALUES:
2851da177e4SLinus Torvalds * 0 - success
2861da177e4SLinus Torvalds * -EIO - i/o error.
2871da177e4SLinus Torvalds * -ENOMEM - insufficient memory
2881da177e4SLinus Torvalds *
2891da177e4SLinus Torvalds */
diRead(struct inode * ip)2901da177e4SLinus Torvalds int diRead(struct inode *ip)
2911da177e4SLinus Torvalds {
2921da177e4SLinus Torvalds struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
293ff14eadcSJeongjun Park int iagno, ino, extno, rc, agno;
2941da177e4SLinus Torvalds struct inode *ipimap;
2951da177e4SLinus Torvalds struct dinode *dp;
2961da177e4SLinus Torvalds struct iag *iagp;
2971da177e4SLinus Torvalds struct metapage *mp;
2981da177e4SLinus Torvalds s64 blkno, agstart;
2991da177e4SLinus Torvalds struct inomap *imap;
3001da177e4SLinus Torvalds int block_offset;
3011da177e4SLinus Torvalds int inodes_left;
3028f6cff98SDave Kleikamp unsigned long pageno;
3031da177e4SLinus Torvalds int rel_inode;
3041da177e4SLinus Torvalds
3051da177e4SLinus Torvalds jfs_info("diRead: ino = %ld", ip->i_ino);
3061da177e4SLinus Torvalds
3071da177e4SLinus Torvalds ipimap = sbi->ipimap;
3081da177e4SLinus Torvalds JFS_IP(ip)->ipimap = ipimap;
3091da177e4SLinus Torvalds
3101da177e4SLinus Torvalds /* determine the iag number for this inode (number) */
3111da177e4SLinus Torvalds iagno = INOTOIAG(ip->i_ino);
3121da177e4SLinus Torvalds
3131da177e4SLinus Torvalds /* read the iag */
31482d5b9a7SDave Kleikamp IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
315a60dca73SOleg Kanatov imap = JFS_IP(ipimap)->i_imap;
3161da177e4SLinus Torvalds rc = diIAGRead(imap, iagno, &mp);
3171da177e4SLinus Torvalds IREAD_UNLOCK(ipimap);
3181da177e4SLinus Torvalds if (rc) {
3191da177e4SLinus Torvalds jfs_err("diRead: diIAGRead returned %d", rc);
3201da177e4SLinus Torvalds return (rc);
3211da177e4SLinus Torvalds }
3221da177e4SLinus Torvalds
3231da177e4SLinus Torvalds iagp = (struct iag *) mp->data;
3241da177e4SLinus Torvalds
3251da177e4SLinus Torvalds /* determine inode extent that holds the disk inode */
3261da177e4SLinus Torvalds ino = ip->i_ino & (INOSPERIAG - 1);
3271da177e4SLinus Torvalds extno = ino >> L2INOSPEREXT;
3281da177e4SLinus Torvalds
3291da177e4SLinus Torvalds if ((lengthPXD(&iagp->inoext[extno]) != imap->im_nbperiext) ||
3301da177e4SLinus Torvalds (addressPXD(&iagp->inoext[extno]) == 0)) {
3311da177e4SLinus Torvalds release_metapage(mp);
3321da177e4SLinus Torvalds return -ESTALE;
3331da177e4SLinus Torvalds }
3341da177e4SLinus Torvalds
3351da177e4SLinus Torvalds /* get disk block number of the page within the inode extent
3361da177e4SLinus Torvalds * that holds the disk inode.
3371da177e4SLinus Torvalds */
3381da177e4SLinus Torvalds blkno = INOPBLK(&iagp->inoext[extno], ino, sbi->l2nbperpage);
3391da177e4SLinus Torvalds
3401da177e4SLinus Torvalds /* get the ag for the iag */
3411da177e4SLinus Torvalds agstart = le64_to_cpu(iagp->agstart);
342ff14eadcSJeongjun Park agno = BLKTOAG(agstart, JFS_SBI(ip->i_sb));
3431da177e4SLinus Torvalds
3441da177e4SLinus Torvalds release_metapage(mp);
345ff14eadcSJeongjun Park if (agno >= MAXAG || agno < 0)
346ff14eadcSJeongjun Park return -EIO;
3471da177e4SLinus Torvalds
3481da177e4SLinus Torvalds rel_inode = (ino & (INOSPERPAGE - 1));
3491da177e4SLinus Torvalds pageno = blkno >> sbi->l2nbperpage;
3501da177e4SLinus Torvalds
3511da177e4SLinus Torvalds if ((block_offset = ((u32) blkno & (sbi->nbperpage - 1)))) {
3521da177e4SLinus Torvalds /*
3531da177e4SLinus Torvalds * OS/2 didn't always align inode extents on page boundaries
3541da177e4SLinus Torvalds */
3551da177e4SLinus Torvalds inodes_left =
3561da177e4SLinus Torvalds (sbi->nbperpage - block_offset) << sbi->l2niperblk;
3571da177e4SLinus Torvalds
3581da177e4SLinus Torvalds if (rel_inode < inodes_left)
3591da177e4SLinus Torvalds rel_inode += block_offset << sbi->l2niperblk;
3601da177e4SLinus Torvalds else {
3611da177e4SLinus Torvalds pageno += 1;
3621da177e4SLinus Torvalds rel_inode -= inodes_left;
3631da177e4SLinus Torvalds }
3641da177e4SLinus Torvalds }
3651da177e4SLinus Torvalds
3661da177e4SLinus Torvalds /* read the page of disk inode */
3671da177e4SLinus Torvalds mp = read_metapage(ipimap, pageno << sbi->l2nbperpage, PSIZE, 1);
36809aaa749SJoe Perches if (!mp) {
3691da177e4SLinus Torvalds jfs_err("diRead: read_metapage failed");
3701da177e4SLinus Torvalds return -EIO;
3711da177e4SLinus Torvalds }
3721da177e4SLinus Torvalds
37359c51591SMichael Opdenacker /* locate the disk inode requested */
3741da177e4SLinus Torvalds dp = (struct dinode *) mp->data;
3751da177e4SLinus Torvalds dp += rel_inode;
3761da177e4SLinus Torvalds
3771da177e4SLinus Torvalds if (ip->i_ino != le32_to_cpu(dp->di_number)) {
378eb8630d7SJoe Perches jfs_error(ip->i_sb, "i_ino != di_number\n");
3791da177e4SLinus Torvalds rc = -EIO;
3801da177e4SLinus Torvalds } else if (le32_to_cpu(dp->di_nlink) == 0)
3811da177e4SLinus Torvalds rc = -ESTALE;
3821da177e4SLinus Torvalds else
3831da177e4SLinus Torvalds /* copy the disk inode to the in-memory inode */
3841da177e4SLinus Torvalds rc = copy_from_dinode(dp, ip);
3851da177e4SLinus Torvalds
3861da177e4SLinus Torvalds release_metapage(mp);
3871da177e4SLinus Torvalds
3881da177e4SLinus Torvalds /* set the ag for the inode */
389d31b53e3SDave Kleikamp JFS_IP(ip)->agstart = agstart;
3901da177e4SLinus Torvalds JFS_IP(ip)->active_ag = -1;
3911da177e4SLinus Torvalds
3921da177e4SLinus Torvalds return (rc);
3931da177e4SLinus Torvalds }
3941da177e4SLinus Torvalds
3951da177e4SLinus Torvalds
3961da177e4SLinus Torvalds /*
3971da177e4SLinus Torvalds * NAME: diReadSpecial()
3981da177e4SLinus Torvalds *
3991da177e4SLinus Torvalds * FUNCTION: initialize a 'special' inode from disk.
4001da177e4SLinus Torvalds *
4011da177e4SLinus Torvalds * this routines handles aggregate level inodes. The
4021da177e4SLinus Torvalds * inode cache cannot differentiate between the
4031da177e4SLinus Torvalds * aggregate inodes and the filesystem inodes, so we
4041da177e4SLinus Torvalds * handle these here. We don't actually use the aggregate
4051da177e4SLinus Torvalds * inode map, since these inodes are at a fixed location
4061da177e4SLinus Torvalds * and in some cases the aggregate inode map isn't initialized
4071da177e4SLinus Torvalds * yet.
4081da177e4SLinus Torvalds *
4091da177e4SLinus Torvalds * PARAMETERS:
4101da177e4SLinus Torvalds * sb - filesystem superblock
4111da177e4SLinus Torvalds * inum - aggregate inode number
4121da177e4SLinus Torvalds * secondary - 1 if secondary aggregate inode table
4131da177e4SLinus Torvalds *
4141da177e4SLinus Torvalds * RETURN VALUES:
4151da177e4SLinus Torvalds * new inode - success
4161da177e4SLinus Torvalds * NULL - i/o error.
4171da177e4SLinus Torvalds */
diReadSpecial(struct super_block * sb,ino_t inum,int secondary)4181da177e4SLinus Torvalds struct inode *diReadSpecial(struct super_block *sb, ino_t inum, int secondary)
4191da177e4SLinus Torvalds {
4201da177e4SLinus Torvalds struct jfs_sb_info *sbi = JFS_SBI(sb);
4211da177e4SLinus Torvalds uint address;
4221da177e4SLinus Torvalds struct dinode *dp;
4231da177e4SLinus Torvalds struct inode *ip;
4241da177e4SLinus Torvalds struct metapage *mp;
4251da177e4SLinus Torvalds
4261da177e4SLinus Torvalds ip = new_inode(sb);
4271da177e4SLinus Torvalds if (ip == NULL) {
4281da177e4SLinus Torvalds jfs_err("diReadSpecial: new_inode returned NULL!");
4291da177e4SLinus Torvalds return ip;
4301da177e4SLinus Torvalds }
4311da177e4SLinus Torvalds
4321da177e4SLinus Torvalds if (secondary) {
4331da177e4SLinus Torvalds address = addressPXD(&sbi->ait2) >> sbi->l2nbperpage;
4341da177e4SLinus Torvalds JFS_IP(ip)->ipimap = sbi->ipaimap2;
4351da177e4SLinus Torvalds } else {
4361da177e4SLinus Torvalds address = AITBL_OFF >> L2PSIZE;
4371da177e4SLinus Torvalds JFS_IP(ip)->ipimap = sbi->ipaimap;
4381da177e4SLinus Torvalds }
4391da177e4SLinus Torvalds
4401da177e4SLinus Torvalds ASSERT(inum < INOSPEREXT);
4411da177e4SLinus Torvalds
4421da177e4SLinus Torvalds ip->i_ino = inum;
4431da177e4SLinus Torvalds
4441da177e4SLinus Torvalds address += inum >> 3; /* 8 inodes per 4K page */
4451da177e4SLinus Torvalds
4461da177e4SLinus Torvalds /* read the page of fixed disk inode (AIT) in raw mode */
4471da177e4SLinus Torvalds mp = read_metapage(ip, address << sbi->l2nbperpage, PSIZE, 1);
4481da177e4SLinus Torvalds if (mp == NULL) {
449bfe86848SMiklos Szeredi set_nlink(ip, 1); /* Don't want iput() deleting it */
4501da177e4SLinus Torvalds iput(ip);
4511da177e4SLinus Torvalds return (NULL);
4521da177e4SLinus Torvalds }
4531da177e4SLinus Torvalds
4541da177e4SLinus Torvalds /* get the pointer to the disk inode of interest */
4551da177e4SLinus Torvalds dp = (struct dinode *) (mp->data);
4561da177e4SLinus Torvalds dp += inum % 8; /* 8 inodes per 4K page */
4571da177e4SLinus Torvalds
4581da177e4SLinus Torvalds /* copy on-disk inode to in-memory inode */
4591da177e4SLinus Torvalds if ((copy_from_dinode(dp, ip)) != 0) {
4601da177e4SLinus Torvalds /* handle bad return by returning NULL for ip */
461bfe86848SMiklos Szeredi set_nlink(ip, 1); /* Don't want iput() deleting it */
4621da177e4SLinus Torvalds iput(ip);
4631da177e4SLinus Torvalds /* release the page */
4641da177e4SLinus Torvalds release_metapage(mp);
4651da177e4SLinus Torvalds return (NULL);
4661da177e4SLinus Torvalds
4671da177e4SLinus Torvalds }
4681da177e4SLinus Torvalds
4697fab479bSDave Kleikamp ip->i_mapping->a_ops = &jfs_metapage_aops;
4701da177e4SLinus Torvalds mapping_set_gfp_mask(ip->i_mapping, GFP_NOFS);
4711da177e4SLinus Torvalds
4721da177e4SLinus Torvalds /* Allocations to metadata inodes should not affect quotas */
4731da177e4SLinus Torvalds ip->i_flags |= S_NOQUOTA;
4741da177e4SLinus Torvalds
4751da177e4SLinus Torvalds if ((inum == FILESYSTEM_I) && (JFS_IP(ip)->ipimap == sbi->ipaimap)) {
4761da177e4SLinus Torvalds sbi->gengen = le32_to_cpu(dp->di_gengen);
4771da177e4SLinus Torvalds sbi->inostamp = le32_to_cpu(dp->di_inostamp);
4781da177e4SLinus Torvalds }
4791da177e4SLinus Torvalds
4801da177e4SLinus Torvalds /* release the page */
4811da177e4SLinus Torvalds release_metapage(mp);
4821da177e4SLinus Torvalds
4835bef9151SAl Viro inode_fake_hash(ip);
484ac17b8b5SDave Kleikamp
4851da177e4SLinus Torvalds return (ip);
4861da177e4SLinus Torvalds }
4871da177e4SLinus Torvalds
4881da177e4SLinus Torvalds /*
4891da177e4SLinus Torvalds * NAME: diWriteSpecial()
4901da177e4SLinus Torvalds *
4911da177e4SLinus Torvalds * FUNCTION: Write the special inode to disk
4921da177e4SLinus Torvalds *
4931da177e4SLinus Torvalds * PARAMETERS:
4941da177e4SLinus Torvalds * ip - special inode
4951da177e4SLinus Torvalds * secondary - 1 if secondary aggregate inode table
4961da177e4SLinus Torvalds *
4971da177e4SLinus Torvalds * RETURN VALUES: none
4981da177e4SLinus Torvalds */
4991da177e4SLinus Torvalds
diWriteSpecial(struct inode * ip,int secondary)5001da177e4SLinus Torvalds void diWriteSpecial(struct inode *ip, int secondary)
5011da177e4SLinus Torvalds {
5021da177e4SLinus Torvalds struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
5031da177e4SLinus Torvalds uint address;
5041da177e4SLinus Torvalds struct dinode *dp;
5051da177e4SLinus Torvalds ino_t inum = ip->i_ino;
5061da177e4SLinus Torvalds struct metapage *mp;
5071da177e4SLinus Torvalds
5081da177e4SLinus Torvalds if (secondary)
5091da177e4SLinus Torvalds address = addressPXD(&sbi->ait2) >> sbi->l2nbperpage;
5101da177e4SLinus Torvalds else
5111da177e4SLinus Torvalds address = AITBL_OFF >> L2PSIZE;
5121da177e4SLinus Torvalds
5131da177e4SLinus Torvalds ASSERT(inum < INOSPEREXT);
5141da177e4SLinus Torvalds
5151da177e4SLinus Torvalds address += inum >> 3; /* 8 inodes per 4K page */
5161da177e4SLinus Torvalds
5171da177e4SLinus Torvalds /* read the page of fixed disk inode (AIT) in raw mode */
5181da177e4SLinus Torvalds mp = read_metapage(ip, address << sbi->l2nbperpage, PSIZE, 1);
5191da177e4SLinus Torvalds if (mp == NULL) {
5206ed71e98SJoe Perches jfs_err("diWriteSpecial: failed to read aggregate inode extent!");
5211da177e4SLinus Torvalds return;
5221da177e4SLinus Torvalds }
5231da177e4SLinus Torvalds
5241da177e4SLinus Torvalds /* get the pointer to the disk inode of interest */
5251da177e4SLinus Torvalds dp = (struct dinode *) (mp->data);
5261da177e4SLinus Torvalds dp += inum % 8; /* 8 inodes per 4K page */
5271da177e4SLinus Torvalds
5281da177e4SLinus Torvalds /* copy on-disk inode to in-memory inode */
5291da177e4SLinus Torvalds copy_to_dinode(dp, ip);
5301da177e4SLinus Torvalds memcpy(&dp->di_xtroot, &JFS_IP(ip)->i_xtroot, 288);
5311da177e4SLinus Torvalds
5321da177e4SLinus Torvalds if (inum == FILESYSTEM_I)
5331da177e4SLinus Torvalds dp->di_gengen = cpu_to_le32(sbi->gengen);
5341da177e4SLinus Torvalds
5351da177e4SLinus Torvalds /* write the page */
5361da177e4SLinus Torvalds write_metapage(mp);
5371da177e4SLinus Torvalds }
5381da177e4SLinus Torvalds
5391da177e4SLinus Torvalds /*
5401da177e4SLinus Torvalds * NAME: diFreeSpecial()
5411da177e4SLinus Torvalds *
5421da177e4SLinus Torvalds * FUNCTION: Free allocated space for special inode
5431da177e4SLinus Torvalds */
diFreeSpecial(struct inode * ip)5441da177e4SLinus Torvalds void diFreeSpecial(struct inode *ip)
5451da177e4SLinus Torvalds {
5461da177e4SLinus Torvalds if (ip == NULL) {
5471da177e4SLinus Torvalds jfs_err("diFreeSpecial called with NULL ip!");
5481da177e4SLinus Torvalds return;
5491da177e4SLinus Torvalds }
55028fd1298SOGAWA Hirofumi filemap_write_and_wait(ip->i_mapping);
5511da177e4SLinus Torvalds truncate_inode_pages(ip->i_mapping, 0);
5521da177e4SLinus Torvalds iput(ip);
5531da177e4SLinus Torvalds }
5541da177e4SLinus Torvalds
5551da177e4SLinus Torvalds
5561da177e4SLinus Torvalds
5571da177e4SLinus Torvalds /*
5581da177e4SLinus Torvalds * NAME: diWrite()
5591da177e4SLinus Torvalds *
5601da177e4SLinus Torvalds * FUNCTION: write the on-disk inode portion of the in-memory inode
5611da177e4SLinus Torvalds * to its corresponding on-disk inode.
5621da177e4SLinus Torvalds *
5631da177e4SLinus Torvalds * on entry, the specifed incore inode should itself
5641da177e4SLinus Torvalds * specify the disk inode number corresponding to the
5651da177e4SLinus Torvalds * incore inode (i.e. i_number should be initialized).
5661da177e4SLinus Torvalds *
5671da177e4SLinus Torvalds * the inode contains the inode extent address for the disk
5681da177e4SLinus Torvalds * inode. with the inode extent address in hand, the
5691da177e4SLinus Torvalds * page of the extent that contains the disk inode is
5701da177e4SLinus Torvalds * read and the disk inode portion of the incore inode
5711da177e4SLinus Torvalds * is copied to the disk inode.
5721da177e4SLinus Torvalds *
5731da177e4SLinus Torvalds * PARAMETERS:
5741da177e4SLinus Torvalds * tid - transacation id
5751da177e4SLinus Torvalds * ip - pointer to incore inode to be written to the inode extent.
5761da177e4SLinus Torvalds *
5771da177e4SLinus Torvalds * RETURN VALUES:
5781da177e4SLinus Torvalds * 0 - success
5791da177e4SLinus Torvalds * -EIO - i/o error.
5801da177e4SLinus Torvalds */
diWrite(tid_t tid,struct inode * ip)5811da177e4SLinus Torvalds int diWrite(tid_t tid, struct inode *ip)
5821da177e4SLinus Torvalds {
5831da177e4SLinus Torvalds struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
5841da177e4SLinus Torvalds struct jfs_inode_info *jfs_ip = JFS_IP(ip);
5851da177e4SLinus Torvalds int rc = 0;
5861da177e4SLinus Torvalds s32 ino;
5871da177e4SLinus Torvalds struct dinode *dp;
5881da177e4SLinus Torvalds s64 blkno;
5891da177e4SLinus Torvalds int block_offset;
5901da177e4SLinus Torvalds int inodes_left;
5911da177e4SLinus Torvalds struct metapage *mp;
5928f6cff98SDave Kleikamp unsigned long pageno;
5931da177e4SLinus Torvalds int rel_inode;
5941da177e4SLinus Torvalds int dioffset;
5951da177e4SLinus Torvalds struct inode *ipimap;
5961da177e4SLinus Torvalds uint type;
5971da177e4SLinus Torvalds lid_t lid;
5981da177e4SLinus Torvalds struct tlock *ditlck, *tlck;
5991da177e4SLinus Torvalds struct linelock *dilinelock, *ilinelock;
6001da177e4SLinus Torvalds struct lv *lv;
6011da177e4SLinus Torvalds int n;
6021da177e4SLinus Torvalds
6031da177e4SLinus Torvalds ipimap = jfs_ip->ipimap;
6041da177e4SLinus Torvalds
6051da177e4SLinus Torvalds ino = ip->i_ino & (INOSPERIAG - 1);
6061da177e4SLinus Torvalds
6071da177e4SLinus Torvalds if (!addressPXD(&(jfs_ip->ixpxd)) ||
6081da177e4SLinus Torvalds (lengthPXD(&(jfs_ip->ixpxd)) !=
6091da177e4SLinus Torvalds JFS_IP(ipimap)->i_imap->im_nbperiext)) {
610eb8630d7SJoe Perches jfs_error(ip->i_sb, "ixpxd invalid\n");
6111da177e4SLinus Torvalds return -EIO;
6121da177e4SLinus Torvalds }
6131da177e4SLinus Torvalds
6141da177e4SLinus Torvalds /*
6151da177e4SLinus Torvalds * read the page of disk inode containing the specified inode:
6161da177e4SLinus Torvalds */
6171da177e4SLinus Torvalds /* compute the block address of the page */
6181da177e4SLinus Torvalds blkno = INOPBLK(&(jfs_ip->ixpxd), ino, sbi->l2nbperpage);
6191da177e4SLinus Torvalds
6201da177e4SLinus Torvalds rel_inode = (ino & (INOSPERPAGE - 1));
6211da177e4SLinus Torvalds pageno = blkno >> sbi->l2nbperpage;
6221da177e4SLinus Torvalds
6231da177e4SLinus Torvalds if ((block_offset = ((u32) blkno & (sbi->nbperpage - 1)))) {
6241da177e4SLinus Torvalds /*
6251da177e4SLinus Torvalds * OS/2 didn't always align inode extents on page boundaries
6261da177e4SLinus Torvalds */
6271da177e4SLinus Torvalds inodes_left =
6281da177e4SLinus Torvalds (sbi->nbperpage - block_offset) << sbi->l2niperblk;
6291da177e4SLinus Torvalds
6301da177e4SLinus Torvalds if (rel_inode < inodes_left)
6311da177e4SLinus Torvalds rel_inode += block_offset << sbi->l2niperblk;
6321da177e4SLinus Torvalds else {
6331da177e4SLinus Torvalds pageno += 1;
6341da177e4SLinus Torvalds rel_inode -= inodes_left;
6351da177e4SLinus Torvalds }
6361da177e4SLinus Torvalds }
6371da177e4SLinus Torvalds /* read the page of disk inode */
6381da177e4SLinus Torvalds retry:
6391da177e4SLinus Torvalds mp = read_metapage(ipimap, pageno << sbi->l2nbperpage, PSIZE, 1);
64009aaa749SJoe Perches if (!mp)
6411da177e4SLinus Torvalds return -EIO;
6421da177e4SLinus Torvalds
6431da177e4SLinus Torvalds /* get the pointer to the disk inode */
6441da177e4SLinus Torvalds dp = (struct dinode *) mp->data;
6451da177e4SLinus Torvalds dp += rel_inode;
6461da177e4SLinus Torvalds
6471da177e4SLinus Torvalds dioffset = (ino & (INOSPERPAGE - 1)) << L2DISIZE;
6481da177e4SLinus Torvalds
6491da177e4SLinus Torvalds /*
6501da177e4SLinus Torvalds * acquire transaction lock on the on-disk inode;
6511da177e4SLinus Torvalds * N.B. tlock is acquired on ipimap not ip;
6521da177e4SLinus Torvalds */
6531da177e4SLinus Torvalds if ((ditlck =
6541da177e4SLinus Torvalds txLock(tid, ipimap, mp, tlckINODE | tlckENTRY)) == NULL)
6551da177e4SLinus Torvalds goto retry;
6561da177e4SLinus Torvalds dilinelock = (struct linelock *) & ditlck->lock;
6571da177e4SLinus Torvalds
6581da177e4SLinus Torvalds /*
6591da177e4SLinus Torvalds * copy btree root from in-memory inode to on-disk inode
6601da177e4SLinus Torvalds *
6611da177e4SLinus Torvalds * (tlock is taken from inline B+-tree root in in-memory
6621da177e4SLinus Torvalds * inode when the B+-tree root is updated, which is pointed
6631da177e4SLinus Torvalds * by jfs_ip->blid as well as being on tx tlock list)
6641da177e4SLinus Torvalds *
6651da177e4SLinus Torvalds * further processing of btree root is based on the copy
6661da177e4SLinus Torvalds * in in-memory inode, where txLog() will log from, and,
6671da177e4SLinus Torvalds * for xtree root, txUpdateMap() will update map and reset
6681da177e4SLinus Torvalds * XAD_NEW bit;
6691da177e4SLinus Torvalds */
6701da177e4SLinus Torvalds
6711da177e4SLinus Torvalds if (S_ISDIR(ip->i_mode) && (lid = jfs_ip->xtlid)) {
6721da177e4SLinus Torvalds /*
6731da177e4SLinus Torvalds * This is the special xtree inside the directory for storing
6741da177e4SLinus Torvalds * the directory table
6751da177e4SLinus Torvalds */
6762ff51719SDave Kleikamp xtroot_t *p, *xp;
6771da177e4SLinus Torvalds xad_t *xad;
6781da177e4SLinus Torvalds
6791da177e4SLinus Torvalds jfs_ip->xtlid = 0;
6801da177e4SLinus Torvalds tlck = lid_to_tlock(lid);
6811da177e4SLinus Torvalds assert(tlck->type & tlckXTREE);
6821da177e4SLinus Torvalds tlck->type |= tlckBTROOT;
6831da177e4SLinus Torvalds tlck->mp = mp;
6841da177e4SLinus Torvalds ilinelock = (struct linelock *) & tlck->lock;
6851da177e4SLinus Torvalds
6861da177e4SLinus Torvalds /*
6871da177e4SLinus Torvalds * copy xtree root from inode to dinode:
6881da177e4SLinus Torvalds */
6891da177e4SLinus Torvalds p = &jfs_ip->i_xtroot;
6902ff51719SDave Kleikamp xp = (xtroot_t *) &dp->di_dirtable;
6911da177e4SLinus Torvalds lv = ilinelock->lv;
6921da177e4SLinus Torvalds for (n = 0; n < ilinelock->index; n++, lv++) {
6931da177e4SLinus Torvalds memcpy(&xp->xad[lv->offset], &p->xad[lv->offset],
6941da177e4SLinus Torvalds lv->length << L2XTSLOTSIZE);
6951da177e4SLinus Torvalds }
6961da177e4SLinus Torvalds
6971da177e4SLinus Torvalds /* reset on-disk (metadata page) xtree XAD_NEW bit */
6981da177e4SLinus Torvalds xad = &xp->xad[XTENTRYSTART];
6991da177e4SLinus Torvalds for (n = XTENTRYSTART;
7001da177e4SLinus Torvalds n < le16_to_cpu(xp->header.nextindex); n++, xad++)
7011da177e4SLinus Torvalds if (xad->flag & (XAD_NEW | XAD_EXTENDED))
7021da177e4SLinus Torvalds xad->flag &= ~(XAD_NEW | XAD_EXTENDED);
7031da177e4SLinus Torvalds }
7041da177e4SLinus Torvalds
7051da177e4SLinus Torvalds if ((lid = jfs_ip->blid) == 0)
7061da177e4SLinus Torvalds goto inlineData;
7071da177e4SLinus Torvalds jfs_ip->blid = 0;
7081da177e4SLinus Torvalds
7091da177e4SLinus Torvalds tlck = lid_to_tlock(lid);
7101da177e4SLinus Torvalds type = tlck->type;
7111da177e4SLinus Torvalds tlck->type |= tlckBTROOT;
7121da177e4SLinus Torvalds tlck->mp = mp;
7131da177e4SLinus Torvalds ilinelock = (struct linelock *) & tlck->lock;
7141da177e4SLinus Torvalds
7151da177e4SLinus Torvalds /*
7161da177e4SLinus Torvalds * regular file: 16 byte (XAD slot) granularity
7171da177e4SLinus Torvalds */
7181da177e4SLinus Torvalds if (type & tlckXTREE) {
7192ff51719SDave Kleikamp xtroot_t *p, *xp;
7201da177e4SLinus Torvalds xad_t *xad;
7211da177e4SLinus Torvalds
7221da177e4SLinus Torvalds /*
7231da177e4SLinus Torvalds * copy xtree root from inode to dinode:
7241da177e4SLinus Torvalds */
7251da177e4SLinus Torvalds p = &jfs_ip->i_xtroot;
7261da177e4SLinus Torvalds xp = &dp->di_xtroot;
7271da177e4SLinus Torvalds lv = ilinelock->lv;
7281da177e4SLinus Torvalds for (n = 0; n < ilinelock->index; n++, lv++) {
7291da177e4SLinus Torvalds memcpy(&xp->xad[lv->offset], &p->xad[lv->offset],
7301da177e4SLinus Torvalds lv->length << L2XTSLOTSIZE);
7311da177e4SLinus Torvalds }
7321da177e4SLinus Torvalds
7331da177e4SLinus Torvalds /* reset on-disk (metadata page) xtree XAD_NEW bit */
7341da177e4SLinus Torvalds xad = &xp->xad[XTENTRYSTART];
7351da177e4SLinus Torvalds for (n = XTENTRYSTART;
7361da177e4SLinus Torvalds n < le16_to_cpu(xp->header.nextindex); n++, xad++)
7371da177e4SLinus Torvalds if (xad->flag & (XAD_NEW | XAD_EXTENDED))
7381da177e4SLinus Torvalds xad->flag &= ~(XAD_NEW | XAD_EXTENDED);
7391da177e4SLinus Torvalds }
7401da177e4SLinus Torvalds /*
7411da177e4SLinus Torvalds * directory: 32 byte (directory entry slot) granularity
7421da177e4SLinus Torvalds */
7431da177e4SLinus Torvalds else if (type & tlckDTREE) {
7441da177e4SLinus Torvalds dtpage_t *p, *xp;
7451da177e4SLinus Torvalds
7461da177e4SLinus Torvalds /*
7471da177e4SLinus Torvalds * copy dtree root from inode to dinode:
7481da177e4SLinus Torvalds */
7491da177e4SLinus Torvalds p = (dtpage_t *) &jfs_ip->i_dtroot;
7501da177e4SLinus Torvalds xp = (dtpage_t *) & dp->di_dtroot;
7511da177e4SLinus Torvalds lv = ilinelock->lv;
7521da177e4SLinus Torvalds for (n = 0; n < ilinelock->index; n++, lv++) {
7531da177e4SLinus Torvalds memcpy(&xp->slot[lv->offset], &p->slot[lv->offset],
7541da177e4SLinus Torvalds lv->length << L2DTSLOTSIZE);
7551da177e4SLinus Torvalds }
7561da177e4SLinus Torvalds } else {
7571da177e4SLinus Torvalds jfs_err("diWrite: UFO tlock");
7581da177e4SLinus Torvalds }
7591da177e4SLinus Torvalds
7601da177e4SLinus Torvalds inlineData:
7611da177e4SLinus Torvalds /*
7621da177e4SLinus Torvalds * copy inline symlink from in-memory inode to on-disk inode
7631da177e4SLinus Torvalds */
7641da177e4SLinus Torvalds if (S_ISLNK(ip->i_mode) && ip->i_size < IDATASIZE) {
7651da177e4SLinus Torvalds lv = & dilinelock->lv[dilinelock->index];
7661da177e4SLinus Torvalds lv->offset = (dioffset + 2 * 128) >> L2INODESLOTSIZE;
7671da177e4SLinus Torvalds lv->length = 2;
7685d299f44SKees Cook memcpy(&dp->di_inline_all, jfs_ip->i_inline_all, IDATASIZE);
7691da177e4SLinus Torvalds dilinelock->index++;
7701da177e4SLinus Torvalds }
7711da177e4SLinus Torvalds /*
7721da177e4SLinus Torvalds * copy inline data from in-memory inode to on-disk inode:
7731da177e4SLinus Torvalds * 128 byte slot granularity
7741da177e4SLinus Torvalds */
7751da177e4SLinus Torvalds if (test_cflag(COMMIT_Inlineea, ip)) {
7761da177e4SLinus Torvalds lv = & dilinelock->lv[dilinelock->index];
7771da177e4SLinus Torvalds lv->offset = (dioffset + 3 * 128) >> L2INODESLOTSIZE;
7781da177e4SLinus Torvalds lv->length = 1;
7791da177e4SLinus Torvalds memcpy(&dp->di_inlineea, jfs_ip->i_inline_ea, INODESLOTSIZE);
7801da177e4SLinus Torvalds dilinelock->index++;
7811da177e4SLinus Torvalds
7821da177e4SLinus Torvalds clear_cflag(COMMIT_Inlineea, ip);
7831da177e4SLinus Torvalds }
7841da177e4SLinus Torvalds
7851da177e4SLinus Torvalds /*
7861da177e4SLinus Torvalds * lock/copy inode base: 128 byte slot granularity
7871da177e4SLinus Torvalds */
7881da177e4SLinus Torvalds lv = & dilinelock->lv[dilinelock->index];
7891da177e4SLinus Torvalds lv->offset = dioffset >> L2INODESLOTSIZE;
7901da177e4SLinus Torvalds copy_to_dinode(dp, ip);
7911da177e4SLinus Torvalds if (test_and_clear_cflag(COMMIT_Dirtable, ip)) {
7921da177e4SLinus Torvalds lv->length = 2;
7931da177e4SLinus Torvalds memcpy(&dp->di_dirtable, &jfs_ip->i_dirtable, 96);
7941da177e4SLinus Torvalds } else
7951da177e4SLinus Torvalds lv->length = 1;
7961da177e4SLinus Torvalds dilinelock->index++;
7971da177e4SLinus Torvalds
7981da177e4SLinus Torvalds /* release the buffer holding the updated on-disk inode.
7991da177e4SLinus Torvalds * the buffer will be later written by commit processing.
8001da177e4SLinus Torvalds */
8011da177e4SLinus Torvalds write_metapage(mp);
8021da177e4SLinus Torvalds
8031da177e4SLinus Torvalds return (rc);
8041da177e4SLinus Torvalds }
8051da177e4SLinus Torvalds
8061da177e4SLinus Torvalds
8071da177e4SLinus Torvalds /*
8081da177e4SLinus Torvalds * NAME: diFree(ip)
8091da177e4SLinus Torvalds *
8101da177e4SLinus Torvalds * FUNCTION: free a specified inode from the inode working map
8111da177e4SLinus Torvalds * for a fileset or aggregate.
8121da177e4SLinus Torvalds *
8131da177e4SLinus Torvalds * if the inode to be freed represents the first (only)
8141da177e4SLinus Torvalds * free inode within the iag, the iag will be placed on
8151da177e4SLinus Torvalds * the ag free inode list.
8161da177e4SLinus Torvalds *
8171da177e4SLinus Torvalds * freeing the inode will cause the inode extent to be
8181da177e4SLinus Torvalds * freed if the inode is the only allocated inode within
8191da177e4SLinus Torvalds * the extent. in this case all the disk resource backing
8201da177e4SLinus Torvalds * up the inode extent will be freed. in addition, the iag
8211da177e4SLinus Torvalds * will be placed on the ag extent free list if the extent
8221da177e4SLinus Torvalds * is the first free extent in the iag. if freeing the
8231da177e4SLinus Torvalds * extent also means that no free inodes will exist for
8241da177e4SLinus Torvalds * the iag, the iag will also be removed from the ag free
8251da177e4SLinus Torvalds * inode list.
8261da177e4SLinus Torvalds *
8271da177e4SLinus Torvalds * the iag describing the inode will be freed if the extent
8281da177e4SLinus Torvalds * is to be freed and it is the only backed extent within
8291da177e4SLinus Torvalds * the iag. in this case, the iag will be removed from the
8301da177e4SLinus Torvalds * ag free extent list and ag free inode list and placed on
8311da177e4SLinus Torvalds * the inode map's free iag list.
8321da177e4SLinus Torvalds *
8331da177e4SLinus Torvalds * a careful update approach is used to provide consistency
8341da177e4SLinus Torvalds * in the face of updates to multiple buffers. under this
8351da177e4SLinus Torvalds * approach, all required buffers are obtained before making
8361da177e4SLinus Torvalds * any updates and are held until all updates are complete.
8371da177e4SLinus Torvalds *
8381da177e4SLinus Torvalds * PARAMETERS:
8391da177e4SLinus Torvalds * ip - inode to be freed.
8401da177e4SLinus Torvalds *
8411da177e4SLinus Torvalds * RETURN VALUES:
8421da177e4SLinus Torvalds * 0 - success
8431da177e4SLinus Torvalds * -EIO - i/o error.
8441da177e4SLinus Torvalds */
diFree(struct inode * ip)8451da177e4SLinus Torvalds int diFree(struct inode *ip)
8461da177e4SLinus Torvalds {
8471da177e4SLinus Torvalds int rc;
8481da177e4SLinus Torvalds ino_t inum = ip->i_ino;
8491da177e4SLinus Torvalds struct iag *iagp, *aiagp, *biagp, *ciagp, *diagp;
8501da177e4SLinus Torvalds struct metapage *mp, *amp, *bmp, *cmp, *dmp;
8511da177e4SLinus Torvalds int iagno, ino, extno, bitno, sword, agno;
8521da177e4SLinus Torvalds int back, fwd;
8531da177e4SLinus Torvalds u32 bitmap, mask;
8541da177e4SLinus Torvalds struct inode *ipimap = JFS_SBI(ip->i_sb)->ipimap;
8551da177e4SLinus Torvalds struct inomap *imap = JFS_IP(ipimap)->i_imap;
8561da177e4SLinus Torvalds pxd_t freepxd;
8571da177e4SLinus Torvalds tid_t tid;
8581da177e4SLinus Torvalds struct inode *iplist[3];
8591da177e4SLinus Torvalds struct tlock *tlck;
8601da177e4SLinus Torvalds struct pxd_lock *pxdlock;
8611da177e4SLinus Torvalds
8621da177e4SLinus Torvalds /*
8631da177e4SLinus Torvalds * This is just to suppress compiler warnings. The same logic that
8641da177e4SLinus Torvalds * references these variables is used to initialize them.
8651da177e4SLinus Torvalds */
8661da177e4SLinus Torvalds aiagp = biagp = ciagp = diagp = NULL;
8671da177e4SLinus Torvalds
8681da177e4SLinus Torvalds /* get the iag number containing the inode.
8691da177e4SLinus Torvalds */
8701da177e4SLinus Torvalds iagno = INOTOIAG(inum);
8711da177e4SLinus Torvalds
8721da177e4SLinus Torvalds /* make sure that the iag is contained within
8731da177e4SLinus Torvalds * the map.
8741da177e4SLinus Torvalds */
8751da177e4SLinus Torvalds if (iagno >= imap->im_nextiag) {
876288e4d83SDave Kleikamp print_hex_dump(KERN_ERR, "imap: ", DUMP_PREFIX_ADDRESS, 16, 4,
877288e4d83SDave Kleikamp imap, 32, 0);
878eb8630d7SJoe Perches jfs_error(ip->i_sb, "inum = %d, iagno = %d, nextiag = %d\n",
8791da177e4SLinus Torvalds (uint) inum, iagno, imap->im_nextiag);
8801da177e4SLinus Torvalds return -EIO;
8811da177e4SLinus Torvalds }
8821da177e4SLinus Torvalds
8831da177e4SLinus Torvalds /* get the allocation group for this ino.
8841da177e4SLinus Torvalds */
885d31b53e3SDave Kleikamp agno = BLKTOAG(JFS_IP(ip)->agstart, JFS_SBI(ip->i_sb));
8861da177e4SLinus Torvalds
8871da177e4SLinus Torvalds /* Lock the AG specific inode map information
8881da177e4SLinus Torvalds */
8891da177e4SLinus Torvalds AG_LOCK(imap, agno);
8901da177e4SLinus Torvalds
8911da177e4SLinus Torvalds /* Obtain read lock in imap inode. Don't release it until we have
8921da177e4SLinus Torvalds * read all of the IAG's that we are going to.
8931da177e4SLinus Torvalds */
89482d5b9a7SDave Kleikamp IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
8951da177e4SLinus Torvalds
8961da177e4SLinus Torvalds /* read the iag.
8971da177e4SLinus Torvalds */
8981da177e4SLinus Torvalds if ((rc = diIAGRead(imap, iagno, &mp))) {
8991da177e4SLinus Torvalds IREAD_UNLOCK(ipimap);
9001da177e4SLinus Torvalds AG_UNLOCK(imap, agno);
9011da177e4SLinus Torvalds return (rc);
9021da177e4SLinus Torvalds }
9031da177e4SLinus Torvalds iagp = (struct iag *) mp->data;
9041da177e4SLinus Torvalds
9051da177e4SLinus Torvalds /* get the inode number and extent number of the inode within
9061da177e4SLinus Torvalds * the iag and the inode number within the extent.
9071da177e4SLinus Torvalds */
9081da177e4SLinus Torvalds ino = inum & (INOSPERIAG - 1);
9091da177e4SLinus Torvalds extno = ino >> L2INOSPEREXT;
9101da177e4SLinus Torvalds bitno = ino & (INOSPEREXT - 1);
9111da177e4SLinus Torvalds mask = HIGHORDER >> bitno;
9121da177e4SLinus Torvalds
9131da177e4SLinus Torvalds if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
914eb8630d7SJoe Perches jfs_error(ip->i_sb, "wmap shows inode already free\n");
9151da177e4SLinus Torvalds }
9161da177e4SLinus Torvalds
9171da177e4SLinus Torvalds if (!addressPXD(&iagp->inoext[extno])) {
9181da177e4SLinus Torvalds release_metapage(mp);
9191da177e4SLinus Torvalds IREAD_UNLOCK(ipimap);
9201da177e4SLinus Torvalds AG_UNLOCK(imap, agno);
921eb8630d7SJoe Perches jfs_error(ip->i_sb, "invalid inoext\n");
9221da177e4SLinus Torvalds return -EIO;
9231da177e4SLinus Torvalds }
9241da177e4SLinus Torvalds
9251da177e4SLinus Torvalds /* compute the bitmap for the extent reflecting the freed inode.
9261da177e4SLinus Torvalds */
9271da177e4SLinus Torvalds bitmap = le32_to_cpu(iagp->wmap[extno]) & ~mask;
9281da177e4SLinus Torvalds
9291da177e4SLinus Torvalds if (imap->im_agctl[agno].numfree > imap->im_agctl[agno].numinos) {
9301da177e4SLinus Torvalds release_metapage(mp);
9311da177e4SLinus Torvalds IREAD_UNLOCK(ipimap);
9321da177e4SLinus Torvalds AG_UNLOCK(imap, agno);
933eb8630d7SJoe Perches jfs_error(ip->i_sb, "numfree > numinos\n");
9341da177e4SLinus Torvalds return -EIO;
9351da177e4SLinus Torvalds }
9361da177e4SLinus Torvalds /*
9371da177e4SLinus Torvalds * inode extent still has some inodes or below low water mark:
9381da177e4SLinus Torvalds * keep the inode extent;
9391da177e4SLinus Torvalds */
9401da177e4SLinus Torvalds if (bitmap ||
9411da177e4SLinus Torvalds imap->im_agctl[agno].numfree < 96 ||
9421da177e4SLinus Torvalds (imap->im_agctl[agno].numfree < 288 &&
9431da177e4SLinus Torvalds (((imap->im_agctl[agno].numfree * 100) /
9441da177e4SLinus Torvalds imap->im_agctl[agno].numinos) <= 25))) {
9451da177e4SLinus Torvalds /* if the iag currently has no free inodes (i.e.,
9461da177e4SLinus Torvalds * the inode being freed is the first free inode of iag),
9471da177e4SLinus Torvalds * insert the iag at head of the inode free list for the ag.
9481da177e4SLinus Torvalds */
9491da177e4SLinus Torvalds if (iagp->nfreeinos == 0) {
9501da177e4SLinus Torvalds /* check if there are any iags on the ag inode
9511da177e4SLinus Torvalds * free list. if so, read the first one so that
9521da177e4SLinus Torvalds * we can link the current iag onto the list at
9531da177e4SLinus Torvalds * the head.
9541da177e4SLinus Torvalds */
9551da177e4SLinus Torvalds if ((fwd = imap->im_agctl[agno].inofree) >= 0) {
9561da177e4SLinus Torvalds /* read the iag that currently is the head
9571da177e4SLinus Torvalds * of the list.
9581da177e4SLinus Torvalds */
9591da177e4SLinus Torvalds if ((rc = diIAGRead(imap, fwd, &))) {
9601da177e4SLinus Torvalds IREAD_UNLOCK(ipimap);
9611da177e4SLinus Torvalds AG_UNLOCK(imap, agno);
9621da177e4SLinus Torvalds release_metapage(mp);
9631da177e4SLinus Torvalds return (rc);
9641da177e4SLinus Torvalds }
9651da177e4SLinus Torvalds aiagp = (struct iag *) amp->data;
9661da177e4SLinus Torvalds
9671da177e4SLinus Torvalds /* make current head point back to the iag.
9681da177e4SLinus Torvalds */
9691da177e4SLinus Torvalds aiagp->inofreeback = cpu_to_le32(iagno);
9701da177e4SLinus Torvalds
9711da177e4SLinus Torvalds write_metapage(amp);
9721da177e4SLinus Torvalds }
9731da177e4SLinus Torvalds
9741da177e4SLinus Torvalds /* iag points forward to current head and iag
9751da177e4SLinus Torvalds * becomes the new head of the list.
9761da177e4SLinus Torvalds */
9771da177e4SLinus Torvalds iagp->inofreefwd =
9781da177e4SLinus Torvalds cpu_to_le32(imap->im_agctl[agno].inofree);
9791da177e4SLinus Torvalds iagp->inofreeback = cpu_to_le32(-1);
9801da177e4SLinus Torvalds imap->im_agctl[agno].inofree = iagno;
9811da177e4SLinus Torvalds }
9821da177e4SLinus Torvalds IREAD_UNLOCK(ipimap);
9831da177e4SLinus Torvalds
9841da177e4SLinus Torvalds /* update the free inode summary map for the extent if
9851da177e4SLinus Torvalds * freeing the inode means the extent will now have free
9861da177e4SLinus Torvalds * inodes (i.e., the inode being freed is the first free
9871da177e4SLinus Torvalds * inode of extent),
9881da177e4SLinus Torvalds */
9891da177e4SLinus Torvalds if (iagp->wmap[extno] == cpu_to_le32(ONES)) {
9901da177e4SLinus Torvalds sword = extno >> L2EXTSPERSUM;
9911da177e4SLinus Torvalds bitno = extno & (EXTSPERSUM - 1);
9921da177e4SLinus Torvalds iagp->inosmap[sword] &=
9931da177e4SLinus Torvalds cpu_to_le32(~(HIGHORDER >> bitno));
9941da177e4SLinus Torvalds }
9951da177e4SLinus Torvalds
9961da177e4SLinus Torvalds /* update the bitmap.
9971da177e4SLinus Torvalds */
9981da177e4SLinus Torvalds iagp->wmap[extno] = cpu_to_le32(bitmap);
9991da177e4SLinus Torvalds
10001da177e4SLinus Torvalds /* update the free inode counts at the iag, ag and
10011da177e4SLinus Torvalds * map level.
10021da177e4SLinus Torvalds */
100389145622SMarcin Slusarz le32_add_cpu(&iagp->nfreeinos, 1);
10041da177e4SLinus Torvalds imap->im_agctl[agno].numfree += 1;
10051da177e4SLinus Torvalds atomic_inc(&imap->im_numfree);
10061da177e4SLinus Torvalds
10071da177e4SLinus Torvalds /* release the AG inode map lock
10081da177e4SLinus Torvalds */
10091da177e4SLinus Torvalds AG_UNLOCK(imap, agno);
10101da177e4SLinus Torvalds
10111da177e4SLinus Torvalds /* write the iag */
10121da177e4SLinus Torvalds write_metapage(mp);
10131da177e4SLinus Torvalds
10141da177e4SLinus Torvalds return (0);
10151da177e4SLinus Torvalds }
10161da177e4SLinus Torvalds
10171da177e4SLinus Torvalds
10181da177e4SLinus Torvalds /*
10191da177e4SLinus Torvalds * inode extent has become free and above low water mark:
10201da177e4SLinus Torvalds * free the inode extent;
10211da177e4SLinus Torvalds */
10221da177e4SLinus Torvalds
10231da177e4SLinus Torvalds /*
10241da177e4SLinus Torvalds * prepare to update iag list(s) (careful update step 1)
10251da177e4SLinus Torvalds */
10261da177e4SLinus Torvalds amp = bmp = cmp = dmp = NULL;
10271da177e4SLinus Torvalds fwd = back = -1;
10281da177e4SLinus Torvalds
10291da177e4SLinus Torvalds /* check if the iag currently has no free extents. if so,
10301da177e4SLinus Torvalds * it will be placed on the head of the ag extent free list.
10311da177e4SLinus Torvalds */
10321da177e4SLinus Torvalds if (iagp->nfreeexts == 0) {
10331da177e4SLinus Torvalds /* check if the ag extent free list has any iags.
10341da177e4SLinus Torvalds * if so, read the iag at the head of the list now.
10351da177e4SLinus Torvalds * this (head) iag will be updated later to reflect
10361da177e4SLinus Torvalds * the addition of the current iag at the head of
10371da177e4SLinus Torvalds * the list.
10381da177e4SLinus Torvalds */
10391da177e4SLinus Torvalds if ((fwd = imap->im_agctl[agno].extfree) >= 0) {
10401da177e4SLinus Torvalds if ((rc = diIAGRead(imap, fwd, &)))
10411da177e4SLinus Torvalds goto error_out;
10421da177e4SLinus Torvalds aiagp = (struct iag *) amp->data;
10431da177e4SLinus Torvalds }
10441da177e4SLinus Torvalds } else {
10451da177e4SLinus Torvalds /* iag has free extents. check if the addition of a free
10461da177e4SLinus Torvalds * extent will cause all extents to be free within this
10471da177e4SLinus Torvalds * iag. if so, the iag will be removed from the ag extent
10481da177e4SLinus Torvalds * free list and placed on the inode map's free iag list.
10491da177e4SLinus Torvalds */
10501da177e4SLinus Torvalds if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG - 1)) {
10511da177e4SLinus Torvalds /* in preparation for removing the iag from the
105225985edcSLucas De Marchi * ag extent free list, read the iags preceding
10531da177e4SLinus Torvalds * and following the iag on the ag extent free
10541da177e4SLinus Torvalds * list.
10551da177e4SLinus Torvalds */
10561da177e4SLinus Torvalds if ((fwd = le32_to_cpu(iagp->extfreefwd)) >= 0) {
10571da177e4SLinus Torvalds if ((rc = diIAGRead(imap, fwd, &)))
10581da177e4SLinus Torvalds goto error_out;
10591da177e4SLinus Torvalds aiagp = (struct iag *) amp->data;
10601da177e4SLinus Torvalds }
10611da177e4SLinus Torvalds
10621da177e4SLinus Torvalds if ((back = le32_to_cpu(iagp->extfreeback)) >= 0) {
10631da177e4SLinus Torvalds if ((rc = diIAGRead(imap, back, &bmp)))
10641da177e4SLinus Torvalds goto error_out;
10651da177e4SLinus Torvalds biagp = (struct iag *) bmp->data;
10661da177e4SLinus Torvalds }
10671da177e4SLinus Torvalds }
10681da177e4SLinus Torvalds }
10691da177e4SLinus Torvalds
10701da177e4SLinus Torvalds /* remove the iag from the ag inode free list if freeing
10711da177e4SLinus Torvalds * this extent cause the iag to have no free inodes.
10721da177e4SLinus Torvalds */
10731da177e4SLinus Torvalds if (iagp->nfreeinos == cpu_to_le32(INOSPEREXT - 1)) {
10741da177e4SLinus Torvalds int inofreeback = le32_to_cpu(iagp->inofreeback);
10751da177e4SLinus Torvalds int inofreefwd = le32_to_cpu(iagp->inofreefwd);
10761da177e4SLinus Torvalds
10771da177e4SLinus Torvalds /* in preparation for removing the iag from the
107825985edcSLucas De Marchi * ag inode free list, read the iags preceding
10791da177e4SLinus Torvalds * and following the iag on the ag inode free
10801da177e4SLinus Torvalds * list. before reading these iags, we must make
10811da177e4SLinus Torvalds * sure that we already don't have them in hand
10821da177e4SLinus Torvalds * from up above, since re-reading an iag (buffer)
10831da177e4SLinus Torvalds * we are currently holding would cause a deadlock.
10841da177e4SLinus Torvalds */
10851da177e4SLinus Torvalds if (inofreefwd >= 0) {
10861da177e4SLinus Torvalds
10871da177e4SLinus Torvalds if (inofreefwd == fwd)
10881da177e4SLinus Torvalds ciagp = (struct iag *) amp->data;
10891da177e4SLinus Torvalds else if (inofreefwd == back)
10901da177e4SLinus Torvalds ciagp = (struct iag *) bmp->data;
10911da177e4SLinus Torvalds else {
10921da177e4SLinus Torvalds if ((rc =
10931da177e4SLinus Torvalds diIAGRead(imap, inofreefwd, &cmp)))
10941da177e4SLinus Torvalds goto error_out;
10951da177e4SLinus Torvalds ciagp = (struct iag *) cmp->data;
10961da177e4SLinus Torvalds }
10971da177e4SLinus Torvalds assert(ciagp != NULL);
10981da177e4SLinus Torvalds }
10991da177e4SLinus Torvalds
11001da177e4SLinus Torvalds if (inofreeback >= 0) {
11011da177e4SLinus Torvalds if (inofreeback == fwd)
11021da177e4SLinus Torvalds diagp = (struct iag *) amp->data;
11031da177e4SLinus Torvalds else if (inofreeback == back)
11041da177e4SLinus Torvalds diagp = (struct iag *) bmp->data;
11051da177e4SLinus Torvalds else {
11061da177e4SLinus Torvalds if ((rc =
11071da177e4SLinus Torvalds diIAGRead(imap, inofreeback, &dmp)))
11081da177e4SLinus Torvalds goto error_out;
11091da177e4SLinus Torvalds diagp = (struct iag *) dmp->data;
11101da177e4SLinus Torvalds }
11111da177e4SLinus Torvalds assert(diagp != NULL);
11121da177e4SLinus Torvalds }
11131da177e4SLinus Torvalds }
11141da177e4SLinus Torvalds
11151da177e4SLinus Torvalds IREAD_UNLOCK(ipimap);
11161da177e4SLinus Torvalds
11171da177e4SLinus Torvalds /*
11181da177e4SLinus Torvalds * invalidate any page of the inode extent freed from buffer cache;
11191da177e4SLinus Torvalds */
11201da177e4SLinus Torvalds freepxd = iagp->inoext[extno];
11211da177e4SLinus Torvalds invalidate_pxd_metapages(ip, freepxd);
11221da177e4SLinus Torvalds
11231da177e4SLinus Torvalds /*
11241da177e4SLinus Torvalds * update iag list(s) (careful update step 2)
11251da177e4SLinus Torvalds */
11261da177e4SLinus Torvalds /* add the iag to the ag extent free list if this is the
11271da177e4SLinus Torvalds * first free extent for the iag.
11281da177e4SLinus Torvalds */
11291da177e4SLinus Torvalds if (iagp->nfreeexts == 0) {
11301da177e4SLinus Torvalds if (fwd >= 0)
11311da177e4SLinus Torvalds aiagp->extfreeback = cpu_to_le32(iagno);
11321da177e4SLinus Torvalds
11331da177e4SLinus Torvalds iagp->extfreefwd =
11341da177e4SLinus Torvalds cpu_to_le32(imap->im_agctl[agno].extfree);
11351da177e4SLinus Torvalds iagp->extfreeback = cpu_to_le32(-1);
11361da177e4SLinus Torvalds imap->im_agctl[agno].extfree = iagno;
11371da177e4SLinus Torvalds } else {
11381da177e4SLinus Torvalds /* remove the iag from the ag extent list if all extents
11391da177e4SLinus Torvalds * are now free and place it on the inode map iag free list.
11401da177e4SLinus Torvalds */
11411da177e4SLinus Torvalds if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG - 1)) {
11421da177e4SLinus Torvalds if (fwd >= 0)
11431da177e4SLinus Torvalds aiagp->extfreeback = iagp->extfreeback;
11441da177e4SLinus Torvalds
11451da177e4SLinus Torvalds if (back >= 0)
11461da177e4SLinus Torvalds biagp->extfreefwd = iagp->extfreefwd;
11471da177e4SLinus Torvalds else
11481da177e4SLinus Torvalds imap->im_agctl[agno].extfree =
11491da177e4SLinus Torvalds le32_to_cpu(iagp->extfreefwd);
11501da177e4SLinus Torvalds
11511da177e4SLinus Torvalds iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
11521da177e4SLinus Torvalds
11531da177e4SLinus Torvalds IAGFREE_LOCK(imap);
11541da177e4SLinus Torvalds iagp->iagfree = cpu_to_le32(imap->im_freeiag);
11551da177e4SLinus Torvalds imap->im_freeiag = iagno;
11561da177e4SLinus Torvalds IAGFREE_UNLOCK(imap);
11571da177e4SLinus Torvalds }
11581da177e4SLinus Torvalds }
11591da177e4SLinus Torvalds
11601da177e4SLinus Torvalds /* remove the iag from the ag inode free list if freeing
11611da177e4SLinus Torvalds * this extent causes the iag to have no free inodes.
11621da177e4SLinus Torvalds */
11631da177e4SLinus Torvalds if (iagp->nfreeinos == cpu_to_le32(INOSPEREXT - 1)) {
11641da177e4SLinus Torvalds if ((int) le32_to_cpu(iagp->inofreefwd) >= 0)
11651da177e4SLinus Torvalds ciagp->inofreeback = iagp->inofreeback;
11661da177e4SLinus Torvalds
11671da177e4SLinus Torvalds if ((int) le32_to_cpu(iagp->inofreeback) >= 0)
11681da177e4SLinus Torvalds diagp->inofreefwd = iagp->inofreefwd;
11691da177e4SLinus Torvalds else
11701da177e4SLinus Torvalds imap->im_agctl[agno].inofree =
11711da177e4SLinus Torvalds le32_to_cpu(iagp->inofreefwd);
11721da177e4SLinus Torvalds
11731da177e4SLinus Torvalds iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
11741da177e4SLinus Torvalds }
11751da177e4SLinus Torvalds
11761da177e4SLinus Torvalds /* update the inode extent address and working map
11771da177e4SLinus Torvalds * to reflect the free extent.
11781da177e4SLinus Torvalds * the permanent map should have been updated already
11791da177e4SLinus Torvalds * for the inode being freed.
11801da177e4SLinus Torvalds */
11811da177e4SLinus Torvalds if (iagp->pmap[extno] != 0) {
1182eb8630d7SJoe Perches jfs_error(ip->i_sb, "the pmap does not show inode free\n");
11831da177e4SLinus Torvalds }
11841da177e4SLinus Torvalds iagp->wmap[extno] = 0;
11851da177e4SLinus Torvalds PXDlength(&iagp->inoext[extno], 0);
11861da177e4SLinus Torvalds PXDaddress(&iagp->inoext[extno], 0);
11871da177e4SLinus Torvalds
11881da177e4SLinus Torvalds /* update the free extent and free inode summary maps
11891da177e4SLinus Torvalds * to reflect the freed extent.
11901da177e4SLinus Torvalds * the inode summary map is marked to indicate no inodes
11911da177e4SLinus Torvalds * available for the freed extent.
11921da177e4SLinus Torvalds */
11931da177e4SLinus Torvalds sword = extno >> L2EXTSPERSUM;
11941da177e4SLinus Torvalds bitno = extno & (EXTSPERSUM - 1);
11951da177e4SLinus Torvalds mask = HIGHORDER >> bitno;
11961da177e4SLinus Torvalds iagp->inosmap[sword] |= cpu_to_le32(mask);
11971da177e4SLinus Torvalds iagp->extsmap[sword] &= cpu_to_le32(~mask);
11981da177e4SLinus Torvalds
11991da177e4SLinus Torvalds /* update the number of free inodes and number of free extents
12001da177e4SLinus Torvalds * for the iag.
12011da177e4SLinus Torvalds */
120289145622SMarcin Slusarz le32_add_cpu(&iagp->nfreeinos, -(INOSPEREXT - 1));
120389145622SMarcin Slusarz le32_add_cpu(&iagp->nfreeexts, 1);
12041da177e4SLinus Torvalds
12051da177e4SLinus Torvalds /* update the number of free inodes and backed inodes
12061da177e4SLinus Torvalds * at the ag and inode map level.
12071da177e4SLinus Torvalds */
12081da177e4SLinus Torvalds imap->im_agctl[agno].numfree -= (INOSPEREXT - 1);
12091da177e4SLinus Torvalds imap->im_agctl[agno].numinos -= INOSPEREXT;
12101da177e4SLinus Torvalds atomic_sub(INOSPEREXT - 1, &imap->im_numfree);
12111da177e4SLinus Torvalds atomic_sub(INOSPEREXT, &imap->im_numinos);
12121da177e4SLinus Torvalds
12131da177e4SLinus Torvalds if (amp)
12141da177e4SLinus Torvalds write_metapage(amp);
12151da177e4SLinus Torvalds if (bmp)
12161da177e4SLinus Torvalds write_metapage(bmp);
12171da177e4SLinus Torvalds if (cmp)
12181da177e4SLinus Torvalds write_metapage(cmp);
12191da177e4SLinus Torvalds if (dmp)
12201da177e4SLinus Torvalds write_metapage(dmp);
12211da177e4SLinus Torvalds
12221da177e4SLinus Torvalds /*
12231da177e4SLinus Torvalds * start transaction to update block allocation map
12241da177e4SLinus Torvalds * for the inode extent freed;
12251da177e4SLinus Torvalds *
12261da177e4SLinus Torvalds * N.B. AG_LOCK is released and iag will be released below, and
12271da177e4SLinus Torvalds * other thread may allocate inode from/reusing the ixad freed
12281da177e4SLinus Torvalds * BUT with new/different backing inode extent from the extent
12291da177e4SLinus Torvalds * to be freed by the transaction;
12301da177e4SLinus Torvalds */
12311da177e4SLinus Torvalds tid = txBegin(ipimap->i_sb, COMMIT_FORCE);
12321de87444SIngo Molnar mutex_lock(&JFS_IP(ipimap)->commit_mutex);
12331da177e4SLinus Torvalds
12341da177e4SLinus Torvalds /* acquire tlock of the iag page of the freed ixad
12351da177e4SLinus Torvalds * to force the page NOHOMEOK (even though no data is
12361da177e4SLinus Torvalds * logged from the iag page) until NOREDOPAGE|FREEXTENT log
12371da177e4SLinus Torvalds * for the free of the extent is committed;
12381da177e4SLinus Torvalds * write FREEXTENT|NOREDOPAGE log record
12391da177e4SLinus Torvalds * N.B. linelock is overlaid as freed extent descriptor;
12401da177e4SLinus Torvalds */
12411da177e4SLinus Torvalds tlck = txLock(tid, ipimap, mp, tlckINODE | tlckFREE);
12421da177e4SLinus Torvalds pxdlock = (struct pxd_lock *) & tlck->lock;
12431da177e4SLinus Torvalds pxdlock->flag = mlckFREEPXD;
12441da177e4SLinus Torvalds pxdlock->pxd = freepxd;
12451da177e4SLinus Torvalds pxdlock->index = 1;
12461da177e4SLinus Torvalds
12471da177e4SLinus Torvalds write_metapage(mp);
12481da177e4SLinus Torvalds
12491da177e4SLinus Torvalds iplist[0] = ipimap;
12501da177e4SLinus Torvalds
12511da177e4SLinus Torvalds /*
12521da177e4SLinus Torvalds * logredo needs the IAG number and IAG extent index in order
12531da177e4SLinus Torvalds * to ensure that the IMap is consistent. The least disruptive
12541da177e4SLinus Torvalds * way to pass these values through to the transaction manager
12551da177e4SLinus Torvalds * is in the iplist array.
12561da177e4SLinus Torvalds *
12571da177e4SLinus Torvalds * It's not pretty, but it works.
12581da177e4SLinus Torvalds */
12591da177e4SLinus Torvalds iplist[1] = (struct inode *) (size_t)iagno;
12601da177e4SLinus Torvalds iplist[2] = (struct inode *) (size_t)extno;
12611da177e4SLinus Torvalds
12621da177e4SLinus Torvalds rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
12631da177e4SLinus Torvalds
12641da177e4SLinus Torvalds txEnd(tid);
12651de87444SIngo Molnar mutex_unlock(&JFS_IP(ipimap)->commit_mutex);
12661da177e4SLinus Torvalds
12671da177e4SLinus Torvalds /* unlock the AG inode map information */
12681da177e4SLinus Torvalds AG_UNLOCK(imap, agno);
12691da177e4SLinus Torvalds
12701da177e4SLinus Torvalds return (0);
12711da177e4SLinus Torvalds
12721da177e4SLinus Torvalds error_out:
12731da177e4SLinus Torvalds IREAD_UNLOCK(ipimap);
12741da177e4SLinus Torvalds
12751da177e4SLinus Torvalds if (amp)
12761da177e4SLinus Torvalds release_metapage(amp);
12771da177e4SLinus Torvalds if (bmp)
12781da177e4SLinus Torvalds release_metapage(bmp);
12791da177e4SLinus Torvalds if (cmp)
12801da177e4SLinus Torvalds release_metapage(cmp);
12811da177e4SLinus Torvalds if (dmp)
12821da177e4SLinus Torvalds release_metapage(dmp);
12831da177e4SLinus Torvalds
12841da177e4SLinus Torvalds AG_UNLOCK(imap, agno);
12851da177e4SLinus Torvalds
12861da177e4SLinus Torvalds release_metapage(mp);
12871da177e4SLinus Torvalds
12881da177e4SLinus Torvalds return (rc);
12891da177e4SLinus Torvalds }
12901da177e4SLinus Torvalds
12911da177e4SLinus Torvalds /*
12921da177e4SLinus Torvalds * There are several places in the diAlloc* routines where we initialize
12931da177e4SLinus Torvalds * the inode.
12941da177e4SLinus Torvalds */
12951da177e4SLinus Torvalds static inline void
diInitInode(struct inode * ip,int iagno,int ino,int extno,struct iag * iagp)12961da177e4SLinus Torvalds diInitInode(struct inode *ip, int iagno, int ino, int extno, struct iag * iagp)
12971da177e4SLinus Torvalds {
12981da177e4SLinus Torvalds struct jfs_inode_info *jfs_ip = JFS_IP(ip);
12991da177e4SLinus Torvalds
13001da177e4SLinus Torvalds ip->i_ino = (iagno << L2INOSPERIAG) + ino;
13011da177e4SLinus Torvalds jfs_ip->ixpxd = iagp->inoext[extno];
1302d31b53e3SDave Kleikamp jfs_ip->agstart = le64_to_cpu(iagp->agstart);
13031da177e4SLinus Torvalds jfs_ip->active_ag = -1;
13041da177e4SLinus Torvalds }
13051da177e4SLinus Torvalds
13061da177e4SLinus Torvalds
13071da177e4SLinus Torvalds /*
13081da177e4SLinus Torvalds * NAME: diAlloc(pip,dir,ip)
13091da177e4SLinus Torvalds *
13101da177e4SLinus Torvalds * FUNCTION: allocate a disk inode from the inode working map
13111da177e4SLinus Torvalds * for a fileset or aggregate.
13121da177e4SLinus Torvalds *
13131da177e4SLinus Torvalds * PARAMETERS:
13141da177e4SLinus Torvalds * pip - pointer to incore inode for the parent inode.
13154d81715fSRichard Knutsson * dir - 'true' if the new disk inode is for a directory.
13161da177e4SLinus Torvalds * ip - pointer to a new inode
13171da177e4SLinus Torvalds *
13181da177e4SLinus Torvalds * RETURN VALUES:
13191da177e4SLinus Torvalds * 0 - success.
13201da177e4SLinus Torvalds * -ENOSPC - insufficient disk resources.
13211da177e4SLinus Torvalds * -EIO - i/o error.
13221da177e4SLinus Torvalds */
diAlloc(struct inode * pip,bool dir,struct inode * ip)13234d81715fSRichard Knutsson int diAlloc(struct inode *pip, bool dir, struct inode *ip)
13241da177e4SLinus Torvalds {
13251da177e4SLinus Torvalds int rc, ino, iagno, addext, extno, bitno, sword;
13261708d0a9SManas Ghandat int nwords, rem, i, agno, dn_numag;
13271da177e4SLinus Torvalds u32 mask, inosmap, extsmap;
13281da177e4SLinus Torvalds struct inode *ipimap;
13291da177e4SLinus Torvalds struct metapage *mp;
13301da177e4SLinus Torvalds ino_t inum;
13311da177e4SLinus Torvalds struct iag *iagp;
13321da177e4SLinus Torvalds struct inomap *imap;
13331da177e4SLinus Torvalds
13341da177e4SLinus Torvalds /* get the pointers to the inode map inode and the
13351da177e4SLinus Torvalds * corresponding imap control structure.
13361da177e4SLinus Torvalds */
13371da177e4SLinus Torvalds ipimap = JFS_SBI(pip->i_sb)->ipimap;
13381da177e4SLinus Torvalds imap = JFS_IP(ipimap)->i_imap;
13391da177e4SLinus Torvalds JFS_IP(ip)->ipimap = ipimap;
13401da177e4SLinus Torvalds JFS_IP(ip)->fileset = FILESYSTEM_I;
13411da177e4SLinus Torvalds
13421da177e4SLinus Torvalds /* for a directory, the allocation policy is to start
13431da177e4SLinus Torvalds * at the ag level using the preferred ag.
13441da177e4SLinus Torvalds */
13454d81715fSRichard Knutsson if (dir) {
13461da177e4SLinus Torvalds agno = dbNextAG(JFS_SBI(pip->i_sb)->ipbmap);
13471da177e4SLinus Torvalds AG_LOCK(imap, agno);
13481da177e4SLinus Torvalds goto tryag;
13491da177e4SLinus Torvalds }
13501da177e4SLinus Torvalds
13511da177e4SLinus Torvalds /* for files, the policy starts off by trying to allocate from
13521da177e4SLinus Torvalds * the same iag containing the parent disk inode:
13531da177e4SLinus Torvalds * try to allocate the new disk inode close to the parent disk
13541da177e4SLinus Torvalds * inode, using parent disk inode number + 1 as the allocation
13551da177e4SLinus Torvalds * hint. (we use a left-to-right policy to attempt to avoid
13561da177e4SLinus Torvalds * moving backward on the disk.) compute the hint within the
13571da177e4SLinus Torvalds * file system and the iag.
13581da177e4SLinus Torvalds */
13591da177e4SLinus Torvalds
13601da177e4SLinus Torvalds /* get the ag number of this iag */
1361d31b53e3SDave Kleikamp agno = BLKTOAG(JFS_IP(pip)->agstart, JFS_SBI(pip->i_sb));
13621708d0a9SManas Ghandat dn_numag = JFS_SBI(pip->i_sb)->bmap->db_numag;
1363*c1ba4b8cSJeongjun Park if (agno < 0 || agno > dn_numag || agno >= MAXAG)
13641708d0a9SManas Ghandat return -EIO;
13651da177e4SLinus Torvalds
13661da177e4SLinus Torvalds if (atomic_read(&JFS_SBI(pip->i_sb)->bmap->db_active[agno])) {
13671da177e4SLinus Torvalds /*
13681da177e4SLinus Torvalds * There is an open file actively growing. We want to
13691da177e4SLinus Torvalds * allocate new inodes from a different ag to avoid
13701da177e4SLinus Torvalds * fragmentation problems.
13711da177e4SLinus Torvalds */
13721da177e4SLinus Torvalds agno = dbNextAG(JFS_SBI(pip->i_sb)->ipbmap);
13731da177e4SLinus Torvalds AG_LOCK(imap, agno);
13741da177e4SLinus Torvalds goto tryag;
13751da177e4SLinus Torvalds }
13761da177e4SLinus Torvalds
13771da177e4SLinus Torvalds inum = pip->i_ino + 1;
13781da177e4SLinus Torvalds ino = inum & (INOSPERIAG - 1);
13791da177e4SLinus Torvalds
138059c51591SMichael Opdenacker /* back off the hint if it is outside of the iag */
13811da177e4SLinus Torvalds if (ino == 0)
13821da177e4SLinus Torvalds inum = pip->i_ino;
13831da177e4SLinus Torvalds
13841da177e4SLinus Torvalds /* lock the AG inode map information */
13851da177e4SLinus Torvalds AG_LOCK(imap, agno);
13861da177e4SLinus Torvalds
13871da177e4SLinus Torvalds /* Get read lock on imap inode */
138882d5b9a7SDave Kleikamp IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
13891da177e4SLinus Torvalds
13901da177e4SLinus Torvalds /* get the iag number and read the iag */
13911da177e4SLinus Torvalds iagno = INOTOIAG(inum);
13921da177e4SLinus Torvalds if ((rc = diIAGRead(imap, iagno, &mp))) {
13931da177e4SLinus Torvalds IREAD_UNLOCK(ipimap);
13941da177e4SLinus Torvalds AG_UNLOCK(imap, agno);
13951da177e4SLinus Torvalds return (rc);
13961da177e4SLinus Torvalds }
13971da177e4SLinus Torvalds iagp = (struct iag *) mp->data;
13981da177e4SLinus Torvalds
13991da177e4SLinus Torvalds /* determine if new inode extent is allowed to be added to the iag.
14001da177e4SLinus Torvalds * new inode extent can be added to the iag if the ag
14011da177e4SLinus Torvalds * has less than 32 free disk inodes and the iag has free extents.
14021da177e4SLinus Torvalds */
14031da177e4SLinus Torvalds addext = (imap->im_agctl[agno].numfree < 32 && iagp->nfreeexts);
14041da177e4SLinus Torvalds
14051da177e4SLinus Torvalds /*
14061da177e4SLinus Torvalds * try to allocate from the IAG
14071da177e4SLinus Torvalds */
14081da177e4SLinus Torvalds /* check if the inode may be allocated from the iag
14091da177e4SLinus Torvalds * (i.e. the inode has free inodes or new extent can be added).
14101da177e4SLinus Torvalds */
14111da177e4SLinus Torvalds if (iagp->nfreeinos || addext) {
14121da177e4SLinus Torvalds /* determine the extent number of the hint.
14131da177e4SLinus Torvalds */
14141da177e4SLinus Torvalds extno = ino >> L2INOSPEREXT;
14151da177e4SLinus Torvalds
14161da177e4SLinus Torvalds /* check if the extent containing the hint has backed
14171da177e4SLinus Torvalds * inodes. if so, try to allocate within this extent.
14181da177e4SLinus Torvalds */
14191da177e4SLinus Torvalds if (addressPXD(&iagp->inoext[extno])) {
14201da177e4SLinus Torvalds bitno = ino & (INOSPEREXT - 1);
14211da177e4SLinus Torvalds if ((bitno =
14221da177e4SLinus Torvalds diFindFree(le32_to_cpu(iagp->wmap[extno]),
14231da177e4SLinus Torvalds bitno))
14241da177e4SLinus Torvalds < INOSPEREXT) {
14251da177e4SLinus Torvalds ino = (extno << L2INOSPEREXT) + bitno;
14261da177e4SLinus Torvalds
14271da177e4SLinus Torvalds /* a free inode (bit) was found within this
14281da177e4SLinus Torvalds * extent, so allocate it.
14291da177e4SLinus Torvalds */
14301da177e4SLinus Torvalds rc = diAllocBit(imap, iagp, ino);
14311da177e4SLinus Torvalds IREAD_UNLOCK(ipimap);
14321da177e4SLinus Torvalds if (rc) {
14331da177e4SLinus Torvalds assert(rc == -EIO);
14341da177e4SLinus Torvalds } else {
14351da177e4SLinus Torvalds /* set the results of the allocation
14361da177e4SLinus Torvalds * and write the iag.
14371da177e4SLinus Torvalds */
14381da177e4SLinus Torvalds diInitInode(ip, iagno, ino, extno,
14391da177e4SLinus Torvalds iagp);
14401da177e4SLinus Torvalds mark_metapage_dirty(mp);
14411da177e4SLinus Torvalds }
14421da177e4SLinus Torvalds release_metapage(mp);
14431da177e4SLinus Torvalds
14441da177e4SLinus Torvalds /* free the AG lock and return.
14451da177e4SLinus Torvalds */
14461da177e4SLinus Torvalds AG_UNLOCK(imap, agno);
14471da177e4SLinus Torvalds return (rc);
14481da177e4SLinus Torvalds }
14491da177e4SLinus Torvalds
14501da177e4SLinus Torvalds if (!addext)
14511da177e4SLinus Torvalds extno =
14521da177e4SLinus Torvalds (extno ==
14531da177e4SLinus Torvalds EXTSPERIAG - 1) ? 0 : extno + 1;
14541da177e4SLinus Torvalds }
14551da177e4SLinus Torvalds
14561da177e4SLinus Torvalds /*
14571da177e4SLinus Torvalds * no free inodes within the extent containing the hint.
14581da177e4SLinus Torvalds *
14591da177e4SLinus Torvalds * try to allocate from the backed extents following
14601da177e4SLinus Torvalds * hint or, if appropriate (i.e. addext is true), allocate
14611da177e4SLinus Torvalds * an extent of free inodes at or following the extent
14621da177e4SLinus Torvalds * containing the hint.
14631da177e4SLinus Torvalds *
14641da177e4SLinus Torvalds * the free inode and free extent summary maps are used
14651da177e4SLinus Torvalds * here, so determine the starting summary map position
14661da177e4SLinus Torvalds * and the number of words we'll have to examine. again,
14671da177e4SLinus Torvalds * the approach is to allocate following the hint, so we
14681da177e4SLinus Torvalds * might have to initially ignore prior bits of the summary
14691da177e4SLinus Torvalds * map that represent extents prior to the extent containing
14701da177e4SLinus Torvalds * the hint and later revisit these bits.
14711da177e4SLinus Torvalds */
14721da177e4SLinus Torvalds bitno = extno & (EXTSPERSUM - 1);
14731da177e4SLinus Torvalds nwords = (bitno == 0) ? SMAPSZ : SMAPSZ + 1;
14741da177e4SLinus Torvalds sword = extno >> L2EXTSPERSUM;
14751da177e4SLinus Torvalds
14761da177e4SLinus Torvalds /* mask any prior bits for the starting words of the
14771da177e4SLinus Torvalds * summary map.
14781da177e4SLinus Torvalds */
14799d48017bSNickolai Zeldovich mask = (bitno == 0) ? 0 : (ONES << (EXTSPERSUM - bitno));
14801da177e4SLinus Torvalds inosmap = le32_to_cpu(iagp->inosmap[sword]) | mask;
14811da177e4SLinus Torvalds extsmap = le32_to_cpu(iagp->extsmap[sword]) | mask;
14821da177e4SLinus Torvalds
14831da177e4SLinus Torvalds /* scan the free inode and free extent summary maps for
14841da177e4SLinus Torvalds * free resources.
14851da177e4SLinus Torvalds */
14861da177e4SLinus Torvalds for (i = 0; i < nwords; i++) {
14871da177e4SLinus Torvalds /* check if this word of the free inode summary
14881da177e4SLinus Torvalds * map describes an extent with free inodes.
14891da177e4SLinus Torvalds */
14901da177e4SLinus Torvalds if (~inosmap) {
14911da177e4SLinus Torvalds /* an extent with free inodes has been
14921da177e4SLinus Torvalds * found. determine the extent number
14931da177e4SLinus Torvalds * and the inode number within the extent.
14941da177e4SLinus Torvalds */
14951da177e4SLinus Torvalds rem = diFindFree(inosmap, 0);
14961da177e4SLinus Torvalds extno = (sword << L2EXTSPERSUM) + rem;
14971da177e4SLinus Torvalds rem = diFindFree(le32_to_cpu(iagp->wmap[extno]),
14981da177e4SLinus Torvalds 0);
14991da177e4SLinus Torvalds if (rem >= INOSPEREXT) {
15001da177e4SLinus Torvalds IREAD_UNLOCK(ipimap);
15011da177e4SLinus Torvalds release_metapage(mp);
15021da177e4SLinus Torvalds AG_UNLOCK(imap, agno);
15031da177e4SLinus Torvalds jfs_error(ip->i_sb,
1504eb8630d7SJoe Perches "can't find free bit in wmap\n");
15053c65e874SLi Zefan return -EIO;
15061da177e4SLinus Torvalds }
15071da177e4SLinus Torvalds
15081da177e4SLinus Torvalds /* determine the inode number within the
15091da177e4SLinus Torvalds * iag and allocate the inode from the
15101da177e4SLinus Torvalds * map.
15111da177e4SLinus Torvalds */
15121da177e4SLinus Torvalds ino = (extno << L2INOSPEREXT) + rem;
15131da177e4SLinus Torvalds rc = diAllocBit(imap, iagp, ino);
15141da177e4SLinus Torvalds IREAD_UNLOCK(ipimap);
15151da177e4SLinus Torvalds if (rc)
15161da177e4SLinus Torvalds assert(rc == -EIO);
15171da177e4SLinus Torvalds else {
15181da177e4SLinus Torvalds /* set the results of the allocation
15191da177e4SLinus Torvalds * and write the iag.
15201da177e4SLinus Torvalds */
15211da177e4SLinus Torvalds diInitInode(ip, iagno, ino, extno,
15221da177e4SLinus Torvalds iagp);
15231da177e4SLinus Torvalds mark_metapage_dirty(mp);
15241da177e4SLinus Torvalds }
15251da177e4SLinus Torvalds release_metapage(mp);
15261da177e4SLinus Torvalds
15271da177e4SLinus Torvalds /* free the AG lock and return.
15281da177e4SLinus Torvalds */
15291da177e4SLinus Torvalds AG_UNLOCK(imap, agno);
15301da177e4SLinus Torvalds return (rc);
15311da177e4SLinus Torvalds
15321da177e4SLinus Torvalds }
15331da177e4SLinus Torvalds
15341da177e4SLinus Torvalds /* check if we may allocate an extent of free
15351da177e4SLinus Torvalds * inodes and whether this word of the free
15361da177e4SLinus Torvalds * extents summary map describes a free extent.
15371da177e4SLinus Torvalds */
15381da177e4SLinus Torvalds if (addext && ~extsmap) {
15391da177e4SLinus Torvalds /* a free extent has been found. determine
15401da177e4SLinus Torvalds * the extent number.
15411da177e4SLinus Torvalds */
15421da177e4SLinus Torvalds rem = diFindFree(extsmap, 0);
15431da177e4SLinus Torvalds extno = (sword << L2EXTSPERSUM) + rem;
15441da177e4SLinus Torvalds
15451da177e4SLinus Torvalds /* allocate an extent of free inodes.
15461da177e4SLinus Torvalds */
15471da177e4SLinus Torvalds if ((rc = diNewExt(imap, iagp, extno))) {
15481da177e4SLinus Torvalds /* if there is no disk space for a
15491da177e4SLinus Torvalds * new extent, try to allocate the
15501da177e4SLinus Torvalds * disk inode from somewhere else.
15511da177e4SLinus Torvalds */
15521da177e4SLinus Torvalds if (rc == -ENOSPC)
15531da177e4SLinus Torvalds break;
15541da177e4SLinus Torvalds
15551da177e4SLinus Torvalds assert(rc == -EIO);
15561da177e4SLinus Torvalds } else {
15571da177e4SLinus Torvalds /* set the results of the allocation
15581da177e4SLinus Torvalds * and write the iag.
15591da177e4SLinus Torvalds */
15601da177e4SLinus Torvalds diInitInode(ip, iagno,
15611da177e4SLinus Torvalds extno << L2INOSPEREXT,
15621da177e4SLinus Torvalds extno, iagp);
15631da177e4SLinus Torvalds mark_metapage_dirty(mp);
15641da177e4SLinus Torvalds }
15651da177e4SLinus Torvalds release_metapage(mp);
15661da177e4SLinus Torvalds /* free the imap inode & the AG lock & return.
15671da177e4SLinus Torvalds */
15681da177e4SLinus Torvalds IREAD_UNLOCK(ipimap);
15691da177e4SLinus Torvalds AG_UNLOCK(imap, agno);
15701da177e4SLinus Torvalds return (rc);
15711da177e4SLinus Torvalds }
15721da177e4SLinus Torvalds
15731da177e4SLinus Torvalds /* move on to the next set of summary map words.
15741da177e4SLinus Torvalds */
15751da177e4SLinus Torvalds sword = (sword == SMAPSZ - 1) ? 0 : sword + 1;
15761da177e4SLinus Torvalds inosmap = le32_to_cpu(iagp->inosmap[sword]);
15771da177e4SLinus Torvalds extsmap = le32_to_cpu(iagp->extsmap[sword]);
15781da177e4SLinus Torvalds }
15791da177e4SLinus Torvalds }
15801da177e4SLinus Torvalds /* unlock imap inode */
15811da177e4SLinus Torvalds IREAD_UNLOCK(ipimap);
15821da177e4SLinus Torvalds
15831da177e4SLinus Torvalds /* nothing doing in this iag, so release it. */
15841da177e4SLinus Torvalds release_metapage(mp);
15851da177e4SLinus Torvalds
15861da177e4SLinus Torvalds tryag:
15871da177e4SLinus Torvalds /*
15881da177e4SLinus Torvalds * try to allocate anywhere within the same AG as the parent inode.
15891da177e4SLinus Torvalds */
15901da177e4SLinus Torvalds rc = diAllocAG(imap, agno, dir, ip);
15911da177e4SLinus Torvalds
15921da177e4SLinus Torvalds AG_UNLOCK(imap, agno);
15931da177e4SLinus Torvalds
15941da177e4SLinus Torvalds if (rc != -ENOSPC)
15951da177e4SLinus Torvalds return (rc);
15961da177e4SLinus Torvalds
15971da177e4SLinus Torvalds /*
15981da177e4SLinus Torvalds * try to allocate in any AG.
15991da177e4SLinus Torvalds */
16001da177e4SLinus Torvalds return (diAllocAny(imap, agno, dir, ip));
16011da177e4SLinus Torvalds }
16021da177e4SLinus Torvalds
16031da177e4SLinus Torvalds
16041da177e4SLinus Torvalds /*
16051da177e4SLinus Torvalds * NAME: diAllocAG(imap,agno,dir,ip)
16061da177e4SLinus Torvalds *
16071da177e4SLinus Torvalds * FUNCTION: allocate a disk inode from the allocation group.
16081da177e4SLinus Torvalds *
16091da177e4SLinus Torvalds * this routine first determines if a new extent of free
16101da177e4SLinus Torvalds * inodes should be added for the allocation group, with
16111da177e4SLinus Torvalds * the current request satisfied from this extent. if this
16121da177e4SLinus Torvalds * is the case, an attempt will be made to do just that. if
16131da177e4SLinus Torvalds * this attempt fails or it has been determined that a new
16141da177e4SLinus Torvalds * extent should not be added, an attempt is made to satisfy
16151da177e4SLinus Torvalds * the request by allocating an existing (backed) free inode
16161da177e4SLinus Torvalds * from the allocation group.
16171da177e4SLinus Torvalds *
16181da177e4SLinus Torvalds * PRE CONDITION: Already have the AG lock for this AG.
16191da177e4SLinus Torvalds *
16201da177e4SLinus Torvalds * PARAMETERS:
16211da177e4SLinus Torvalds * imap - pointer to inode map control structure.
16221da177e4SLinus Torvalds * agno - allocation group to allocate from.
16234d81715fSRichard Knutsson * dir - 'true' if the new disk inode is for a directory.
16241da177e4SLinus Torvalds * ip - pointer to the new inode to be filled in on successful return
16251da177e4SLinus Torvalds * with the disk inode number allocated, its extent address
16261da177e4SLinus Torvalds * and the start of the ag.
16271da177e4SLinus Torvalds *
16281da177e4SLinus Torvalds * RETURN VALUES:
16291da177e4SLinus Torvalds * 0 - success.
16301da177e4SLinus Torvalds * -ENOSPC - insufficient disk resources.
16311da177e4SLinus Torvalds * -EIO - i/o error.
16321da177e4SLinus Torvalds */
16331da177e4SLinus Torvalds static int
diAllocAG(struct inomap * imap,int agno,bool dir,struct inode * ip)16344d81715fSRichard Knutsson diAllocAG(struct inomap * imap, int agno, bool dir, struct inode *ip)
16351da177e4SLinus Torvalds {
16361da177e4SLinus Torvalds int rc, addext, numfree, numinos;
16371da177e4SLinus Torvalds
16381da177e4SLinus Torvalds /* get the number of free and the number of backed disk
16391da177e4SLinus Torvalds * inodes currently within the ag.
16401da177e4SLinus Torvalds */
16411da177e4SLinus Torvalds numfree = imap->im_agctl[agno].numfree;
16421da177e4SLinus Torvalds numinos = imap->im_agctl[agno].numinos;
16431da177e4SLinus Torvalds
16441da177e4SLinus Torvalds if (numfree > numinos) {
1645eb8630d7SJoe Perches jfs_error(ip->i_sb, "numfree > numinos\n");
16461da177e4SLinus Torvalds return -EIO;
16471da177e4SLinus Torvalds }
16481da177e4SLinus Torvalds
16491da177e4SLinus Torvalds /* determine if we should allocate a new extent of free inodes
16501da177e4SLinus Torvalds * within the ag: for directory inodes, add a new extent
16511da177e4SLinus Torvalds * if there are a small number of free inodes or number of free
16521da177e4SLinus Torvalds * inodes is a small percentage of the number of backed inodes.
16531da177e4SLinus Torvalds */
16544d81715fSRichard Knutsson if (dir)
16551da177e4SLinus Torvalds addext = (numfree < 64 ||
16561da177e4SLinus Torvalds (numfree < 256
16571da177e4SLinus Torvalds && ((numfree * 100) / numinos) <= 20));
16581da177e4SLinus Torvalds else
16591da177e4SLinus Torvalds addext = (numfree == 0);
16601da177e4SLinus Torvalds
16611da177e4SLinus Torvalds /*
16621da177e4SLinus Torvalds * try to allocate a new extent of free inodes.
16631da177e4SLinus Torvalds */
16641da177e4SLinus Torvalds if (addext) {
166525985edcSLucas De Marchi /* if free space is not available for this new extent, try
16661da177e4SLinus Torvalds * below to allocate a free and existing (already backed)
16671da177e4SLinus Torvalds * inode from the ag.
16681da177e4SLinus Torvalds */
16691da177e4SLinus Torvalds if ((rc = diAllocExt(imap, agno, ip)) != -ENOSPC)
16701da177e4SLinus Torvalds return (rc);
16711da177e4SLinus Torvalds }
16721da177e4SLinus Torvalds
16731da177e4SLinus Torvalds /*
16741da177e4SLinus Torvalds * try to allocate an existing free inode from the ag.
16751da177e4SLinus Torvalds */
16761da177e4SLinus Torvalds return (diAllocIno(imap, agno, ip));
16771da177e4SLinus Torvalds }
16781da177e4SLinus Torvalds
16791da177e4SLinus Torvalds
16801da177e4SLinus Torvalds /*
16811da177e4SLinus Torvalds * NAME: diAllocAny(imap,agno,dir,iap)
16821da177e4SLinus Torvalds *
16831da177e4SLinus Torvalds * FUNCTION: allocate a disk inode from any other allocation group.
16841da177e4SLinus Torvalds *
16851da177e4SLinus Torvalds * this routine is called when an allocation attempt within
16861da177e4SLinus Torvalds * the primary allocation group has failed. if attempts to
16871da177e4SLinus Torvalds * allocate an inode from any allocation group other than the
16881da177e4SLinus Torvalds * specified primary group.
16891da177e4SLinus Torvalds *
16901da177e4SLinus Torvalds * PARAMETERS:
16911da177e4SLinus Torvalds * imap - pointer to inode map control structure.
16921da177e4SLinus Torvalds * agno - primary allocation group (to avoid).
16934d81715fSRichard Knutsson * dir - 'true' if the new disk inode is for a directory.
16941da177e4SLinus Torvalds * ip - pointer to a new inode to be filled in on successful return
16951da177e4SLinus Torvalds * with the disk inode number allocated, its extent address
16961da177e4SLinus Torvalds * and the start of the ag.
16971da177e4SLinus Torvalds *
16981da177e4SLinus Torvalds * RETURN VALUES:
16991da177e4SLinus Torvalds * 0 - success.
17001da177e4SLinus Torvalds * -ENOSPC - insufficient disk resources.
17011da177e4SLinus Torvalds * -EIO - i/o error.
17021da177e4SLinus Torvalds */
17031da177e4SLinus Torvalds static int
diAllocAny(struct inomap * imap,int agno,bool dir,struct inode * ip)17044d81715fSRichard Knutsson diAllocAny(struct inomap * imap, int agno, bool dir, struct inode *ip)
17051da177e4SLinus Torvalds {
17061da177e4SLinus Torvalds int ag, rc;
17071da177e4SLinus Torvalds int maxag = JFS_SBI(imap->im_ipimap->i_sb)->bmap->db_maxag;
17081da177e4SLinus Torvalds
17091da177e4SLinus Torvalds
17101da177e4SLinus Torvalds /* try to allocate from the ags following agno up to
17111da177e4SLinus Torvalds * the maximum ag number.
17121da177e4SLinus Torvalds */
17131da177e4SLinus Torvalds for (ag = agno + 1; ag <= maxag; ag++) {
17141da177e4SLinus Torvalds AG_LOCK(imap, ag);
17151da177e4SLinus Torvalds
17161da177e4SLinus Torvalds rc = diAllocAG(imap, ag, dir, ip);
17171da177e4SLinus Torvalds
17181da177e4SLinus Torvalds AG_UNLOCK(imap, ag);
17191da177e4SLinus Torvalds
17201da177e4SLinus Torvalds if (rc != -ENOSPC)
17211da177e4SLinus Torvalds return (rc);
17221da177e4SLinus Torvalds }
17231da177e4SLinus Torvalds
17241da177e4SLinus Torvalds /* try to allocate from the ags in front of agno.
17251da177e4SLinus Torvalds */
17261da177e4SLinus Torvalds for (ag = 0; ag < agno; ag++) {
17271da177e4SLinus Torvalds AG_LOCK(imap, ag);
17281da177e4SLinus Torvalds
17291da177e4SLinus Torvalds rc = diAllocAG(imap, ag, dir, ip);
17301da177e4SLinus Torvalds
17311da177e4SLinus Torvalds AG_UNLOCK(imap, ag);
17321da177e4SLinus Torvalds
17331da177e4SLinus Torvalds if (rc != -ENOSPC)
17341da177e4SLinus Torvalds return (rc);
17351da177e4SLinus Torvalds }
17361da177e4SLinus Torvalds
17371da177e4SLinus Torvalds /* no free disk inodes.
17381da177e4SLinus Torvalds */
17391da177e4SLinus Torvalds return -ENOSPC;
17401da177e4SLinus Torvalds }
17411da177e4SLinus Torvalds
17421da177e4SLinus Torvalds
17431da177e4SLinus Torvalds /*
17441da177e4SLinus Torvalds * NAME: diAllocIno(imap,agno,ip)
17451da177e4SLinus Torvalds *
17461da177e4SLinus Torvalds * FUNCTION: allocate a disk inode from the allocation group's free
17471da177e4SLinus Torvalds * inode list, returning an error if this free list is
17481da177e4SLinus Torvalds * empty (i.e. no iags on the list).
17491da177e4SLinus Torvalds *
17501da177e4SLinus Torvalds * allocation occurs from the first iag on the list using
17511da177e4SLinus Torvalds * the iag's free inode summary map to find the leftmost
17521da177e4SLinus Torvalds * free inode in the iag.
17531da177e4SLinus Torvalds *
17541da177e4SLinus Torvalds * PRE CONDITION: Already have AG lock for this AG.
17551da177e4SLinus Torvalds *
17561da177e4SLinus Torvalds * PARAMETERS:
17571da177e4SLinus Torvalds * imap - pointer to inode map control structure.
17581da177e4SLinus Torvalds * agno - allocation group.
17591da177e4SLinus Torvalds * ip - pointer to new inode to be filled in on successful return
17601da177e4SLinus Torvalds * with the disk inode number allocated, its extent address
17611da177e4SLinus Torvalds * and the start of the ag.
17621da177e4SLinus Torvalds *
17631da177e4SLinus Torvalds * RETURN VALUES:
17641da177e4SLinus Torvalds * 0 - success.
17651da177e4SLinus Torvalds * -ENOSPC - insufficient disk resources.
17661da177e4SLinus Torvalds * -EIO - i/o error.
17671da177e4SLinus Torvalds */
diAllocIno(struct inomap * imap,int agno,struct inode * ip)17681da177e4SLinus Torvalds static int diAllocIno(struct inomap * imap, int agno, struct inode *ip)
17691da177e4SLinus Torvalds {
17701da177e4SLinus Torvalds int iagno, ino, rc, rem, extno, sword;
17711da177e4SLinus Torvalds struct metapage *mp;
17721da177e4SLinus Torvalds struct iag *iagp;
17731da177e4SLinus Torvalds
17741da177e4SLinus Torvalds /* check if there are iags on the ag's free inode list.
17751da177e4SLinus Torvalds */
17761da177e4SLinus Torvalds if ((iagno = imap->im_agctl[agno].inofree) < 0)
17771da177e4SLinus Torvalds return -ENOSPC;
17781da177e4SLinus Torvalds
17791da177e4SLinus Torvalds /* obtain read lock on imap inode */
178082d5b9a7SDave Kleikamp IREAD_LOCK(imap->im_ipimap, RDWRLOCK_IMAP);
17811da177e4SLinus Torvalds
17821da177e4SLinus Torvalds /* read the iag at the head of the list.
17831da177e4SLinus Torvalds */
17841da177e4SLinus Torvalds if ((rc = diIAGRead(imap, iagno, &mp))) {
17851da177e4SLinus Torvalds IREAD_UNLOCK(imap->im_ipimap);
17861da177e4SLinus Torvalds return (rc);
17871da177e4SLinus Torvalds }
17881da177e4SLinus Torvalds iagp = (struct iag *) mp->data;
17891da177e4SLinus Torvalds
17901da177e4SLinus Torvalds /* better be free inodes in this iag if it is on the
17911da177e4SLinus Torvalds * list.
17921da177e4SLinus Torvalds */
17931da177e4SLinus Torvalds if (!iagp->nfreeinos) {
17941da177e4SLinus Torvalds IREAD_UNLOCK(imap->im_ipimap);
17951da177e4SLinus Torvalds release_metapage(mp);
1796eb8630d7SJoe Perches jfs_error(ip->i_sb, "nfreeinos = 0, but iag on freelist\n");
17971da177e4SLinus Torvalds return -EIO;
17981da177e4SLinus Torvalds }
17991da177e4SLinus Torvalds
18001da177e4SLinus Torvalds /* scan the free inode summary map to find an extent
18011da177e4SLinus Torvalds * with free inodes.
18021da177e4SLinus Torvalds */
18031da177e4SLinus Torvalds for (sword = 0;; sword++) {
18041da177e4SLinus Torvalds if (sword >= SMAPSZ) {
18051da177e4SLinus Torvalds IREAD_UNLOCK(imap->im_ipimap);
18061da177e4SLinus Torvalds release_metapage(mp);
18071da177e4SLinus Torvalds jfs_error(ip->i_sb,
1808eb8630d7SJoe Perches "free inode not found in summary map\n");
18091da177e4SLinus Torvalds return -EIO;
18101da177e4SLinus Torvalds }
18111da177e4SLinus Torvalds
18121da177e4SLinus Torvalds if (~iagp->inosmap[sword])
18131da177e4SLinus Torvalds break;
18141da177e4SLinus Torvalds }
18151da177e4SLinus Torvalds
18161da177e4SLinus Torvalds /* found a extent with free inodes. determine
18171da177e4SLinus Torvalds * the extent number.
18181da177e4SLinus Torvalds */
18191da177e4SLinus Torvalds rem = diFindFree(le32_to_cpu(iagp->inosmap[sword]), 0);
18201da177e4SLinus Torvalds if (rem >= EXTSPERSUM) {
18211da177e4SLinus Torvalds IREAD_UNLOCK(imap->im_ipimap);
18221da177e4SLinus Torvalds release_metapage(mp);
1823eb8630d7SJoe Perches jfs_error(ip->i_sb, "no free extent found\n");
18241da177e4SLinus Torvalds return -EIO;
18251da177e4SLinus Torvalds }
18261da177e4SLinus Torvalds extno = (sword << L2EXTSPERSUM) + rem;
18271da177e4SLinus Torvalds
18281da177e4SLinus Torvalds /* find the first free inode in the extent.
18291da177e4SLinus Torvalds */
18301da177e4SLinus Torvalds rem = diFindFree(le32_to_cpu(iagp->wmap[extno]), 0);
18311da177e4SLinus Torvalds if (rem >= INOSPEREXT) {
18321da177e4SLinus Torvalds IREAD_UNLOCK(imap->im_ipimap);
18331da177e4SLinus Torvalds release_metapage(mp);
1834eb8630d7SJoe Perches jfs_error(ip->i_sb, "free inode not found\n");
18351da177e4SLinus Torvalds return -EIO;
18361da177e4SLinus Torvalds }
18371da177e4SLinus Torvalds
18381da177e4SLinus Torvalds /* compute the inode number within the iag.
18391da177e4SLinus Torvalds */
18401da177e4SLinus Torvalds ino = (extno << L2INOSPEREXT) + rem;
18411da177e4SLinus Torvalds
18421da177e4SLinus Torvalds /* allocate the inode.
18431da177e4SLinus Torvalds */
18441da177e4SLinus Torvalds rc = diAllocBit(imap, iagp, ino);
18451da177e4SLinus Torvalds IREAD_UNLOCK(imap->im_ipimap);
18461da177e4SLinus Torvalds if (rc) {
18471da177e4SLinus Torvalds release_metapage(mp);
18481da177e4SLinus Torvalds return (rc);
18491da177e4SLinus Torvalds }
18501da177e4SLinus Torvalds
18511da177e4SLinus Torvalds /* set the results of the allocation and write the iag.
18521da177e4SLinus Torvalds */
18531da177e4SLinus Torvalds diInitInode(ip, iagno, ino, extno, iagp);
18541da177e4SLinus Torvalds write_metapage(mp);
18551da177e4SLinus Torvalds
18561da177e4SLinus Torvalds return (0);
18571da177e4SLinus Torvalds }
18581da177e4SLinus Torvalds
18591da177e4SLinus Torvalds
18601da177e4SLinus Torvalds /*
18611da177e4SLinus Torvalds * NAME: diAllocExt(imap,agno,ip)
18621da177e4SLinus Torvalds *
18631da177e4SLinus Torvalds * FUNCTION: add a new extent of free inodes to an iag, allocating
18641da177e4SLinus Torvalds * an inode from this extent to satisfy the current allocation
18651da177e4SLinus Torvalds * request.
18661da177e4SLinus Torvalds *
18671da177e4SLinus Torvalds * this routine first tries to find an existing iag with free
18681da177e4SLinus Torvalds * extents through the ag free extent list. if list is not
18691da177e4SLinus Torvalds * empty, the head of the list will be selected as the home
18701da177e4SLinus Torvalds * of the new extent of free inodes. otherwise (the list is
18711da177e4SLinus Torvalds * empty), a new iag will be allocated for the ag to contain
18721da177e4SLinus Torvalds * the extent.
18731da177e4SLinus Torvalds *
18741da177e4SLinus Torvalds * once an iag has been selected, the free extent summary map
18751da177e4SLinus Torvalds * is used to locate a free extent within the iag and diNewExt()
18761da177e4SLinus Torvalds * is called to initialize the extent, with initialization
18771da177e4SLinus Torvalds * including the allocation of the first inode of the extent
18781da177e4SLinus Torvalds * for the purpose of satisfying this request.
18791da177e4SLinus Torvalds *
18801da177e4SLinus Torvalds * PARAMETERS:
18811da177e4SLinus Torvalds * imap - pointer to inode map control structure.
18821da177e4SLinus Torvalds * agno - allocation group number.
18831da177e4SLinus Torvalds * ip - pointer to new inode to be filled in on successful return
18841da177e4SLinus Torvalds * with the disk inode number allocated, its extent address
18851da177e4SLinus Torvalds * and the start of the ag.
18861da177e4SLinus Torvalds *
18871da177e4SLinus Torvalds * RETURN VALUES:
18881da177e4SLinus Torvalds * 0 - success.
18891da177e4SLinus Torvalds * -ENOSPC - insufficient disk resources.
18901da177e4SLinus Torvalds * -EIO - i/o error.
18911da177e4SLinus Torvalds */
diAllocExt(struct inomap * imap,int agno,struct inode * ip)18921da177e4SLinus Torvalds static int diAllocExt(struct inomap * imap, int agno, struct inode *ip)
18931da177e4SLinus Torvalds {
18941da177e4SLinus Torvalds int rem, iagno, sword, extno, rc;
18951da177e4SLinus Torvalds struct metapage *mp;
18961da177e4SLinus Torvalds struct iag *iagp;
18971da177e4SLinus Torvalds
18981da177e4SLinus Torvalds /* check if the ag has any iags with free extents. if not,
18991da177e4SLinus Torvalds * allocate a new iag for the ag.
19001da177e4SLinus Torvalds */
19011da177e4SLinus Torvalds if ((iagno = imap->im_agctl[agno].extfree) < 0) {
19021da177e4SLinus Torvalds /* If successful, diNewIAG will obtain the read lock on the
19031da177e4SLinus Torvalds * imap inode.
19041da177e4SLinus Torvalds */
19051da177e4SLinus Torvalds if ((rc = diNewIAG(imap, &iagno, agno, &mp))) {
19061da177e4SLinus Torvalds return (rc);
19071da177e4SLinus Torvalds }
19081da177e4SLinus Torvalds iagp = (struct iag *) mp->data;
19091da177e4SLinus Torvalds
19101da177e4SLinus Torvalds /* set the ag number if this a brand new iag
19111da177e4SLinus Torvalds */
19121da177e4SLinus Torvalds iagp->agstart =
19131da177e4SLinus Torvalds cpu_to_le64(AGTOBLK(agno, imap->im_ipimap));
19141da177e4SLinus Torvalds } else {
19151da177e4SLinus Torvalds /* read the iag.
19161da177e4SLinus Torvalds */
191782d5b9a7SDave Kleikamp IREAD_LOCK(imap->im_ipimap, RDWRLOCK_IMAP);
19181da177e4SLinus Torvalds if ((rc = diIAGRead(imap, iagno, &mp))) {
19191da177e4SLinus Torvalds IREAD_UNLOCK(imap->im_ipimap);
1920eb8630d7SJoe Perches jfs_error(ip->i_sb, "error reading iag\n");
19211da177e4SLinus Torvalds return rc;
19221da177e4SLinus Torvalds }
19231da177e4SLinus Torvalds iagp = (struct iag *) mp->data;
19241da177e4SLinus Torvalds }
19251da177e4SLinus Torvalds
19261da177e4SLinus Torvalds /* using the free extent summary map, find a free extent.
19271da177e4SLinus Torvalds */
19281da177e4SLinus Torvalds for (sword = 0;; sword++) {
19291da177e4SLinus Torvalds if (sword >= SMAPSZ) {
19301da177e4SLinus Torvalds release_metapage(mp);
19311da177e4SLinus Torvalds IREAD_UNLOCK(imap->im_ipimap);
1932eb8630d7SJoe Perches jfs_error(ip->i_sb, "free ext summary map not found\n");
19331da177e4SLinus Torvalds return -EIO;
19341da177e4SLinus Torvalds }
19351da177e4SLinus Torvalds if (~iagp->extsmap[sword])
19361da177e4SLinus Torvalds break;
19371da177e4SLinus Torvalds }
19381da177e4SLinus Torvalds
19391da177e4SLinus Torvalds /* determine the extent number of the free extent.
19401da177e4SLinus Torvalds */
19411da177e4SLinus Torvalds rem = diFindFree(le32_to_cpu(iagp->extsmap[sword]), 0);
19421da177e4SLinus Torvalds if (rem >= EXTSPERSUM) {
19431da177e4SLinus Torvalds release_metapage(mp);
19441da177e4SLinus Torvalds IREAD_UNLOCK(imap->im_ipimap);
1945eb8630d7SJoe Perches jfs_error(ip->i_sb, "free extent not found\n");
19461da177e4SLinus Torvalds return -EIO;
19471da177e4SLinus Torvalds }
19481da177e4SLinus Torvalds extno = (sword << L2EXTSPERSUM) + rem;
19491da177e4SLinus Torvalds
19501da177e4SLinus Torvalds /* initialize the new extent.
19511da177e4SLinus Torvalds */
19521da177e4SLinus Torvalds rc = diNewExt(imap, iagp, extno);
19531da177e4SLinus Torvalds IREAD_UNLOCK(imap->im_ipimap);
19541da177e4SLinus Torvalds if (rc) {
19551da177e4SLinus Torvalds /* something bad happened. if a new iag was allocated,
19561da177e4SLinus Torvalds * place it back on the inode map's iag free list, and
19571da177e4SLinus Torvalds * clear the ag number information.
19581da177e4SLinus Torvalds */
19591da177e4SLinus Torvalds if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
19601da177e4SLinus Torvalds IAGFREE_LOCK(imap);
19611da177e4SLinus Torvalds iagp->iagfree = cpu_to_le32(imap->im_freeiag);
19621da177e4SLinus Torvalds imap->im_freeiag = iagno;
19631da177e4SLinus Torvalds IAGFREE_UNLOCK(imap);
19641da177e4SLinus Torvalds }
19651da177e4SLinus Torvalds write_metapage(mp);
19661da177e4SLinus Torvalds return (rc);
19671da177e4SLinus Torvalds }
19681da177e4SLinus Torvalds
19691da177e4SLinus Torvalds /* set the results of the allocation and write the iag.
19701da177e4SLinus Torvalds */
19711da177e4SLinus Torvalds diInitInode(ip, iagno, extno << L2INOSPEREXT, extno, iagp);
19721da177e4SLinus Torvalds
19731da177e4SLinus Torvalds write_metapage(mp);
19741da177e4SLinus Torvalds
19751da177e4SLinus Torvalds return (0);
19761da177e4SLinus Torvalds }
19771da177e4SLinus Torvalds
19781da177e4SLinus Torvalds
19791da177e4SLinus Torvalds /*
19801da177e4SLinus Torvalds * NAME: diAllocBit(imap,iagp,ino)
19811da177e4SLinus Torvalds *
19821da177e4SLinus Torvalds * FUNCTION: allocate a backed inode from an iag.
19831da177e4SLinus Torvalds *
19841da177e4SLinus Torvalds * this routine performs the mechanics of allocating a
19851da177e4SLinus Torvalds * specified inode from a backed extent.
19861da177e4SLinus Torvalds *
19871da177e4SLinus Torvalds * if the inode to be allocated represents the last free
19881da177e4SLinus Torvalds * inode within the iag, the iag will be removed from the
19891da177e4SLinus Torvalds * ag free inode list.
19901da177e4SLinus Torvalds *
19911da177e4SLinus Torvalds * a careful update approach is used to provide consistency
19921da177e4SLinus Torvalds * in the face of updates to multiple buffers. under this
19931da177e4SLinus Torvalds * approach, all required buffers are obtained before making
19941da177e4SLinus Torvalds * any updates and are held all are updates are complete.
19951da177e4SLinus Torvalds *
19961da177e4SLinus Torvalds * PRE CONDITION: Already have buffer lock on iagp. Already have AG lock on
19971da177e4SLinus Torvalds * this AG. Must have read lock on imap inode.
19981da177e4SLinus Torvalds *
19991da177e4SLinus Torvalds * PARAMETERS:
20001da177e4SLinus Torvalds * imap - pointer to inode map control structure.
20011da177e4SLinus Torvalds * iagp - pointer to iag.
20021da177e4SLinus Torvalds * ino - inode number to be allocated within the iag.
20031da177e4SLinus Torvalds *
20041da177e4SLinus Torvalds * RETURN VALUES:
20051da177e4SLinus Torvalds * 0 - success.
20061da177e4SLinus Torvalds * -ENOSPC - insufficient disk resources.
20071da177e4SLinus Torvalds * -EIO - i/o error.
20081da177e4SLinus Torvalds */
diAllocBit(struct inomap * imap,struct iag * iagp,int ino)20091da177e4SLinus Torvalds static int diAllocBit(struct inomap * imap, struct iag * iagp, int ino)
20101da177e4SLinus Torvalds {
20111da177e4SLinus Torvalds int extno, bitno, agno, sword, rc;
20121da177e4SLinus Torvalds struct metapage *amp = NULL, *bmp = NULL;
20131da177e4SLinus Torvalds struct iag *aiagp = NULL, *biagp = NULL;
20141da177e4SLinus Torvalds u32 mask;
20151da177e4SLinus Torvalds
20161da177e4SLinus Torvalds /* check if this is the last free inode within the iag.
20171da177e4SLinus Torvalds * if so, it will have to be removed from the ag free
201825985edcSLucas De Marchi * inode list, so get the iags preceding and following
20191da177e4SLinus Torvalds * it on the list.
20201da177e4SLinus Torvalds */
20211da177e4SLinus Torvalds if (iagp->nfreeinos == cpu_to_le32(1)) {
20221da177e4SLinus Torvalds if ((int) le32_to_cpu(iagp->inofreefwd) >= 0) {
20231da177e4SLinus Torvalds if ((rc =
20241da177e4SLinus Torvalds diIAGRead(imap, le32_to_cpu(iagp->inofreefwd),
20251da177e4SLinus Torvalds &)))
20261da177e4SLinus Torvalds return (rc);
20271da177e4SLinus Torvalds aiagp = (struct iag *) amp->data;
20281da177e4SLinus Torvalds }
20291da177e4SLinus Torvalds
20301da177e4SLinus Torvalds if ((int) le32_to_cpu(iagp->inofreeback) >= 0) {
20311da177e4SLinus Torvalds if ((rc =
20321da177e4SLinus Torvalds diIAGRead(imap,
20331da177e4SLinus Torvalds le32_to_cpu(iagp->inofreeback),
20341da177e4SLinus Torvalds &bmp))) {
20351da177e4SLinus Torvalds if (amp)
20361da177e4SLinus Torvalds release_metapage(amp);
20371da177e4SLinus Torvalds return (rc);
20381da177e4SLinus Torvalds }
20391da177e4SLinus Torvalds biagp = (struct iag *) bmp->data;
20401da177e4SLinus Torvalds }
20411da177e4SLinus Torvalds }
20421da177e4SLinus Torvalds
20431da177e4SLinus Torvalds /* get the ag number, extent number, inode number within
20441da177e4SLinus Torvalds * the extent.
20451da177e4SLinus Torvalds */
20461da177e4SLinus Torvalds agno = BLKTOAG(le64_to_cpu(iagp->agstart), JFS_SBI(imap->im_ipimap->i_sb));
20471da177e4SLinus Torvalds extno = ino >> L2INOSPEREXT;
20481da177e4SLinus Torvalds bitno = ino & (INOSPEREXT - 1);
20491da177e4SLinus Torvalds
20501da177e4SLinus Torvalds /* compute the mask for setting the map.
20511da177e4SLinus Torvalds */
20521da177e4SLinus Torvalds mask = HIGHORDER >> bitno;
20531da177e4SLinus Torvalds
20541da177e4SLinus Torvalds /* the inode should be free and backed.
20551da177e4SLinus Torvalds */
20561da177e4SLinus Torvalds if (((le32_to_cpu(iagp->pmap[extno]) & mask) != 0) ||
20571da177e4SLinus Torvalds ((le32_to_cpu(iagp->wmap[extno]) & mask) != 0) ||
20581da177e4SLinus Torvalds (addressPXD(&iagp->inoext[extno]) == 0)) {
20591da177e4SLinus Torvalds if (amp)
20601da177e4SLinus Torvalds release_metapage(amp);
20611da177e4SLinus Torvalds if (bmp)
20621da177e4SLinus Torvalds release_metapage(bmp);
20631da177e4SLinus Torvalds
2064eb8630d7SJoe Perches jfs_error(imap->im_ipimap->i_sb, "iag inconsistent\n");
20651da177e4SLinus Torvalds return -EIO;
20661da177e4SLinus Torvalds }
20671da177e4SLinus Torvalds
20681da177e4SLinus Torvalds /* mark the inode as allocated in the working map.
20691da177e4SLinus Torvalds */
20701da177e4SLinus Torvalds iagp->wmap[extno] |= cpu_to_le32(mask);
20711da177e4SLinus Torvalds
20721da177e4SLinus Torvalds /* check if all inodes within the extent are now
20731da177e4SLinus Torvalds * allocated. if so, update the free inode summary
20741da177e4SLinus Torvalds * map to reflect this.
20751da177e4SLinus Torvalds */
20761da177e4SLinus Torvalds if (iagp->wmap[extno] == cpu_to_le32(ONES)) {
20771da177e4SLinus Torvalds sword = extno >> L2EXTSPERSUM;
20781da177e4SLinus Torvalds bitno = extno & (EXTSPERSUM - 1);
20791da177e4SLinus Torvalds iagp->inosmap[sword] |= cpu_to_le32(HIGHORDER >> bitno);
20801da177e4SLinus Torvalds }
20811da177e4SLinus Torvalds
20821da177e4SLinus Torvalds /* if this was the last free inode in the iag, remove the
20831da177e4SLinus Torvalds * iag from the ag free inode list.
20841da177e4SLinus Torvalds */
20851da177e4SLinus Torvalds if (iagp->nfreeinos == cpu_to_le32(1)) {
20861da177e4SLinus Torvalds if (amp) {
20871da177e4SLinus Torvalds aiagp->inofreeback = iagp->inofreeback;
20881da177e4SLinus Torvalds write_metapage(amp);
20891da177e4SLinus Torvalds }
20901da177e4SLinus Torvalds
20911da177e4SLinus Torvalds if (bmp) {
20921da177e4SLinus Torvalds biagp->inofreefwd = iagp->inofreefwd;
20931da177e4SLinus Torvalds write_metapage(bmp);
20941da177e4SLinus Torvalds } else {
20951da177e4SLinus Torvalds imap->im_agctl[agno].inofree =
20961da177e4SLinus Torvalds le32_to_cpu(iagp->inofreefwd);
20971da177e4SLinus Torvalds }
20981da177e4SLinus Torvalds iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
20991da177e4SLinus Torvalds }
21001da177e4SLinus Torvalds
21011da177e4SLinus Torvalds /* update the free inode count at the iag, ag, inode
21021da177e4SLinus Torvalds * map levels.
21031da177e4SLinus Torvalds */
210489145622SMarcin Slusarz le32_add_cpu(&iagp->nfreeinos, -1);
21051da177e4SLinus Torvalds imap->im_agctl[agno].numfree -= 1;
21061da177e4SLinus Torvalds atomic_dec(&imap->im_numfree);
21071da177e4SLinus Torvalds
21081da177e4SLinus Torvalds return (0);
21091da177e4SLinus Torvalds }
21101da177e4SLinus Torvalds
21111da177e4SLinus Torvalds
21121da177e4SLinus Torvalds /*
21131da177e4SLinus Torvalds * NAME: diNewExt(imap,iagp,extno)
21141da177e4SLinus Torvalds *
21151da177e4SLinus Torvalds * FUNCTION: initialize a new extent of inodes for an iag, allocating
21161da177e4SLinus Torvalds * the first inode of the extent for use for the current
21171da177e4SLinus Torvalds * allocation request.
21181da177e4SLinus Torvalds *
21191da177e4SLinus Torvalds * disk resources are allocated for the new extent of inodes
21201da177e4SLinus Torvalds * and the inodes themselves are initialized to reflect their
21211da177e4SLinus Torvalds * existence within the extent (i.e. their inode numbers and
21221da177e4SLinus Torvalds * inode extent addresses are set) and their initial state
21231da177e4SLinus Torvalds * (mode and link count are set to zero).
21241da177e4SLinus Torvalds *
21251da177e4SLinus Torvalds * if the iag is new, it is not yet on an ag extent free list
21261da177e4SLinus Torvalds * but will now be placed on this list.
21271da177e4SLinus Torvalds *
21281da177e4SLinus Torvalds * if the allocation of the new extent causes the iag to
21291da177e4SLinus Torvalds * have no free extent, the iag will be removed from the
21301da177e4SLinus Torvalds * ag extent free list.
21311da177e4SLinus Torvalds *
21321da177e4SLinus Torvalds * if the iag has no free backed inodes, it will be placed
21331da177e4SLinus Torvalds * on the ag free inode list, since the addition of the new
21341da177e4SLinus Torvalds * extent will now cause it to have free inodes.
21351da177e4SLinus Torvalds *
21361da177e4SLinus Torvalds * a careful update approach is used to provide consistency
21371da177e4SLinus Torvalds * (i.e. list consistency) in the face of updates to multiple
21381da177e4SLinus Torvalds * buffers. under this approach, all required buffers are
21391da177e4SLinus Torvalds * obtained before making any updates and are held until all
21401da177e4SLinus Torvalds * updates are complete.
21411da177e4SLinus Torvalds *
21421da177e4SLinus Torvalds * PRE CONDITION: Already have buffer lock on iagp. Already have AG lock on
21431da177e4SLinus Torvalds * this AG. Must have read lock on imap inode.
21441da177e4SLinus Torvalds *
21451da177e4SLinus Torvalds * PARAMETERS:
21461da177e4SLinus Torvalds * imap - pointer to inode map control structure.
21471da177e4SLinus Torvalds * iagp - pointer to iag.
21481da177e4SLinus Torvalds * extno - extent number.
21491da177e4SLinus Torvalds *
21501da177e4SLinus Torvalds * RETURN VALUES:
21511da177e4SLinus Torvalds * 0 - success.
21521da177e4SLinus Torvalds * -ENOSPC - insufficient disk resources.
21531da177e4SLinus Torvalds * -EIO - i/o error.
21541da177e4SLinus Torvalds */
diNewExt(struct inomap * imap,struct iag * iagp,int extno)21551da177e4SLinus Torvalds static int diNewExt(struct inomap * imap, struct iag * iagp, int extno)
21561da177e4SLinus Torvalds {
21571da177e4SLinus Torvalds int agno, iagno, fwd, back, freei = 0, sword, rc;
21581da177e4SLinus Torvalds struct iag *aiagp = NULL, *biagp = NULL, *ciagp = NULL;
21591da177e4SLinus Torvalds struct metapage *amp, *bmp, *cmp, *dmp;
21601da177e4SLinus Torvalds struct inode *ipimap;
21611da177e4SLinus Torvalds s64 blkno, hint;
21621da177e4SLinus Torvalds int i, j;
21631da177e4SLinus Torvalds u32 mask;
21641da177e4SLinus Torvalds ino_t ino;
21651da177e4SLinus Torvalds struct dinode *dp;
21661da177e4SLinus Torvalds struct jfs_sb_info *sbi;
21671da177e4SLinus Torvalds
21681da177e4SLinus Torvalds /* better have free extents.
21691da177e4SLinus Torvalds */
21701da177e4SLinus Torvalds if (!iagp->nfreeexts) {
2171eb8630d7SJoe Perches jfs_error(imap->im_ipimap->i_sb, "no free extents\n");
21721da177e4SLinus Torvalds return -EIO;
21731da177e4SLinus Torvalds }
21741da177e4SLinus Torvalds
21751da177e4SLinus Torvalds /* get the inode map inode.
21761da177e4SLinus Torvalds */
21771da177e4SLinus Torvalds ipimap = imap->im_ipimap;
21781da177e4SLinus Torvalds sbi = JFS_SBI(ipimap->i_sb);
21791da177e4SLinus Torvalds
21801da177e4SLinus Torvalds amp = bmp = cmp = NULL;
21811da177e4SLinus Torvalds
21821da177e4SLinus Torvalds /* get the ag and iag numbers for this iag.
21831da177e4SLinus Torvalds */
21841da177e4SLinus Torvalds agno = BLKTOAG(le64_to_cpu(iagp->agstart), sbi);
21856996d43bSEdward Adam Davis if (agno >= MAXAG || agno < 0)
21866996d43bSEdward Adam Davis return -EIO;
21876996d43bSEdward Adam Davis
21881da177e4SLinus Torvalds iagno = le32_to_cpu(iagp->iagnum);
21891da177e4SLinus Torvalds
21901da177e4SLinus Torvalds /* check if this is the last free extent within the
21911da177e4SLinus Torvalds * iag. if so, the iag must be removed from the ag
219225985edcSLucas De Marchi * free extent list, so get the iags preceding and
21931da177e4SLinus Torvalds * following the iag on this list.
21941da177e4SLinus Torvalds */
21951da177e4SLinus Torvalds if (iagp->nfreeexts == cpu_to_le32(1)) {
21961da177e4SLinus Torvalds if ((fwd = le32_to_cpu(iagp->extfreefwd)) >= 0) {
21971da177e4SLinus Torvalds if ((rc = diIAGRead(imap, fwd, &)))
21981da177e4SLinus Torvalds return (rc);
21991da177e4SLinus Torvalds aiagp = (struct iag *) amp->data;
22001da177e4SLinus Torvalds }
22011da177e4SLinus Torvalds
22021da177e4SLinus Torvalds if ((back = le32_to_cpu(iagp->extfreeback)) >= 0) {
22031da177e4SLinus Torvalds if ((rc = diIAGRead(imap, back, &bmp)))
22041da177e4SLinus Torvalds goto error_out;
22051da177e4SLinus Torvalds biagp = (struct iag *) bmp->data;
22061da177e4SLinus Torvalds }
22071da177e4SLinus Torvalds } else {
22081da177e4SLinus Torvalds /* the iag has free extents. if all extents are free
22091da177e4SLinus Torvalds * (as is the case for a newly allocated iag), the iag
22101da177e4SLinus Torvalds * must be added to the ag free extent list, so get
22111da177e4SLinus Torvalds * the iag at the head of the list in preparation for
22121da177e4SLinus Torvalds * adding this iag to this list.
22131da177e4SLinus Torvalds */
22141da177e4SLinus Torvalds fwd = back = -1;
22151da177e4SLinus Torvalds if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
22161da177e4SLinus Torvalds if ((fwd = imap->im_agctl[agno].extfree) >= 0) {
22171da177e4SLinus Torvalds if ((rc = diIAGRead(imap, fwd, &)))
22181da177e4SLinus Torvalds goto error_out;
22191da177e4SLinus Torvalds aiagp = (struct iag *) amp->data;
22201da177e4SLinus Torvalds }
22211da177e4SLinus Torvalds }
22221da177e4SLinus Torvalds }
22231da177e4SLinus Torvalds
22241da177e4SLinus Torvalds /* check if the iag has no free inodes. if so, the iag
22251da177e4SLinus Torvalds * will have to be added to the ag free inode list, so get
22261da177e4SLinus Torvalds * the iag at the head of the list in preparation for
22271da177e4SLinus Torvalds * adding this iag to this list. in doing this, we must
22281da177e4SLinus Torvalds * check if we already have the iag at the head of
22291da177e4SLinus Torvalds * the list in hand.
22301da177e4SLinus Torvalds */
22311da177e4SLinus Torvalds if (iagp->nfreeinos == 0) {
22321da177e4SLinus Torvalds freei = imap->im_agctl[agno].inofree;
22331da177e4SLinus Torvalds
22341da177e4SLinus Torvalds if (freei >= 0) {
22351da177e4SLinus Torvalds if (freei == fwd) {
22361da177e4SLinus Torvalds ciagp = aiagp;
22371da177e4SLinus Torvalds } else if (freei == back) {
22381da177e4SLinus Torvalds ciagp = biagp;
22391da177e4SLinus Torvalds } else {
22401da177e4SLinus Torvalds if ((rc = diIAGRead(imap, freei, &cmp)))
22411da177e4SLinus Torvalds goto error_out;
22421da177e4SLinus Torvalds ciagp = (struct iag *) cmp->data;
22431da177e4SLinus Torvalds }
22441da177e4SLinus Torvalds if (ciagp == NULL) {
22451da177e4SLinus Torvalds jfs_error(imap->im_ipimap->i_sb,
2246eb8630d7SJoe Perches "ciagp == NULL\n");
22471da177e4SLinus Torvalds rc = -EIO;
22481da177e4SLinus Torvalds goto error_out;
22491da177e4SLinus Torvalds }
22501da177e4SLinus Torvalds }
22511da177e4SLinus Torvalds }
22521da177e4SLinus Torvalds
22531da177e4SLinus Torvalds /* allocate disk space for the inode extent.
22541da177e4SLinus Torvalds */
22551da177e4SLinus Torvalds if ((extno == 0) || (addressPXD(&iagp->inoext[extno - 1]) == 0))
22561da177e4SLinus Torvalds hint = ((s64) agno << sbi->bmap->db_agl2size) - 1;
22571da177e4SLinus Torvalds else
22581da177e4SLinus Torvalds hint = addressPXD(&iagp->inoext[extno - 1]) +
22591da177e4SLinus Torvalds lengthPXD(&iagp->inoext[extno - 1]) - 1;
22601da177e4SLinus Torvalds
22611da177e4SLinus Torvalds if ((rc = dbAlloc(ipimap, hint, (s64) imap->im_nbperiext, &blkno)))
22621da177e4SLinus Torvalds goto error_out;
22631da177e4SLinus Torvalds
22641da177e4SLinus Torvalds /* compute the inode number of the first inode within the
22651da177e4SLinus Torvalds * extent.
22661da177e4SLinus Torvalds */
22671da177e4SLinus Torvalds ino = (iagno << L2INOSPERIAG) + (extno << L2INOSPEREXT);
22681da177e4SLinus Torvalds
22691da177e4SLinus Torvalds /* initialize the inodes within the newly allocated extent a
22701da177e4SLinus Torvalds * page at a time.
22711da177e4SLinus Torvalds */
22721da177e4SLinus Torvalds for (i = 0; i < imap->im_nbperiext; i += sbi->nbperpage) {
22731da177e4SLinus Torvalds /* get a buffer for this page of disk inodes.
22741da177e4SLinus Torvalds */
22751da177e4SLinus Torvalds dmp = get_metapage(ipimap, blkno + i, PSIZE, 1);
22761da177e4SLinus Torvalds if (dmp == NULL) {
22771da177e4SLinus Torvalds rc = -EIO;
22781da177e4SLinus Torvalds goto error_out;
22791da177e4SLinus Torvalds }
22801da177e4SLinus Torvalds dp = (struct dinode *) dmp->data;
22811da177e4SLinus Torvalds
22821da177e4SLinus Torvalds /* initialize the inode number, mode, link count and
22831da177e4SLinus Torvalds * inode extent address.
22841da177e4SLinus Torvalds */
22851da177e4SLinus Torvalds for (j = 0; j < INOSPERPAGE; j++, dp++, ino++) {
22861da177e4SLinus Torvalds dp->di_inostamp = cpu_to_le32(sbi->inostamp);
22871da177e4SLinus Torvalds dp->di_number = cpu_to_le32(ino);
22881da177e4SLinus Torvalds dp->di_fileset = cpu_to_le32(FILESYSTEM_I);
22891da177e4SLinus Torvalds dp->di_mode = 0;
22901da177e4SLinus Torvalds dp->di_nlink = 0;
22911da177e4SLinus Torvalds PXDaddress(&(dp->di_ixpxd), blkno);
22921da177e4SLinus Torvalds PXDlength(&(dp->di_ixpxd), imap->im_nbperiext);
22931da177e4SLinus Torvalds }
22941da177e4SLinus Torvalds write_metapage(dmp);
22951da177e4SLinus Torvalds }
22961da177e4SLinus Torvalds
22971da177e4SLinus Torvalds /* if this is the last free extent within the iag, remove the
22981da177e4SLinus Torvalds * iag from the ag free extent list.
22991da177e4SLinus Torvalds */
23001da177e4SLinus Torvalds if (iagp->nfreeexts == cpu_to_le32(1)) {
23011da177e4SLinus Torvalds if (fwd >= 0)
23021da177e4SLinus Torvalds aiagp->extfreeback = iagp->extfreeback;
23031da177e4SLinus Torvalds
23041da177e4SLinus Torvalds if (back >= 0)
23051da177e4SLinus Torvalds biagp->extfreefwd = iagp->extfreefwd;
23061da177e4SLinus Torvalds else
23071da177e4SLinus Torvalds imap->im_agctl[agno].extfree =
23081da177e4SLinus Torvalds le32_to_cpu(iagp->extfreefwd);
23091da177e4SLinus Torvalds
23101da177e4SLinus Torvalds iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
23111da177e4SLinus Torvalds } else {
23121da177e4SLinus Torvalds /* if the iag has all free extents (newly allocated iag),
23131da177e4SLinus Torvalds * add the iag to the ag free extent list.
23141da177e4SLinus Torvalds */
23151da177e4SLinus Torvalds if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
23161da177e4SLinus Torvalds if (fwd >= 0)
23171da177e4SLinus Torvalds aiagp->extfreeback = cpu_to_le32(iagno);
23181da177e4SLinus Torvalds
23191da177e4SLinus Torvalds iagp->extfreefwd = cpu_to_le32(fwd);
23201da177e4SLinus Torvalds iagp->extfreeback = cpu_to_le32(-1);
23211da177e4SLinus Torvalds imap->im_agctl[agno].extfree = iagno;
23221da177e4SLinus Torvalds }
23231da177e4SLinus Torvalds }
23241da177e4SLinus Torvalds
23251da177e4SLinus Torvalds /* if the iag has no free inodes, add the iag to the
23261da177e4SLinus Torvalds * ag free inode list.
23271da177e4SLinus Torvalds */
23281da177e4SLinus Torvalds if (iagp->nfreeinos == 0) {
23291da177e4SLinus Torvalds if (freei >= 0)
23301da177e4SLinus Torvalds ciagp->inofreeback = cpu_to_le32(iagno);
23311da177e4SLinus Torvalds
23321da177e4SLinus Torvalds iagp->inofreefwd =
23331da177e4SLinus Torvalds cpu_to_le32(imap->im_agctl[agno].inofree);
23341da177e4SLinus Torvalds iagp->inofreeback = cpu_to_le32(-1);
23351da177e4SLinus Torvalds imap->im_agctl[agno].inofree = iagno;
23361da177e4SLinus Torvalds }
23371da177e4SLinus Torvalds
23381da177e4SLinus Torvalds /* initialize the extent descriptor of the extent. */
23391da177e4SLinus Torvalds PXDlength(&iagp->inoext[extno], imap->im_nbperiext);
23401da177e4SLinus Torvalds PXDaddress(&iagp->inoext[extno], blkno);
23411da177e4SLinus Torvalds
23421da177e4SLinus Torvalds /* initialize the working and persistent map of the extent.
23431da177e4SLinus Torvalds * the working map will be initialized such that
23441da177e4SLinus Torvalds * it indicates the first inode of the extent is allocated.
23451da177e4SLinus Torvalds */
23461da177e4SLinus Torvalds iagp->wmap[extno] = cpu_to_le32(HIGHORDER);
23471da177e4SLinus Torvalds iagp->pmap[extno] = 0;
23481da177e4SLinus Torvalds
23491da177e4SLinus Torvalds /* update the free inode and free extent summary maps
23501da177e4SLinus Torvalds * for the extent to indicate the extent has free inodes
23511da177e4SLinus Torvalds * and no longer represents a free extent.
23521da177e4SLinus Torvalds */
23531da177e4SLinus Torvalds sword = extno >> L2EXTSPERSUM;
23541da177e4SLinus Torvalds mask = HIGHORDER >> (extno & (EXTSPERSUM - 1));
23551da177e4SLinus Torvalds iagp->extsmap[sword] |= cpu_to_le32(mask);
23561da177e4SLinus Torvalds iagp->inosmap[sword] &= cpu_to_le32(~mask);
23571da177e4SLinus Torvalds
23581da177e4SLinus Torvalds /* update the free inode and free extent counts for the
23591da177e4SLinus Torvalds * iag.
23601da177e4SLinus Torvalds */
236189145622SMarcin Slusarz le32_add_cpu(&iagp->nfreeinos, (INOSPEREXT - 1));
236289145622SMarcin Slusarz le32_add_cpu(&iagp->nfreeexts, -1);
23631da177e4SLinus Torvalds
23641da177e4SLinus Torvalds /* update the free and backed inode counts for the ag.
23651da177e4SLinus Torvalds */
23661da177e4SLinus Torvalds imap->im_agctl[agno].numfree += (INOSPEREXT - 1);
23671da177e4SLinus Torvalds imap->im_agctl[agno].numinos += INOSPEREXT;
23681da177e4SLinus Torvalds
23691da177e4SLinus Torvalds /* update the free and backed inode counts for the inode map.
23701da177e4SLinus Torvalds */
23711da177e4SLinus Torvalds atomic_add(INOSPEREXT - 1, &imap->im_numfree);
23721da177e4SLinus Torvalds atomic_add(INOSPEREXT, &imap->im_numinos);
23731da177e4SLinus Torvalds
23741da177e4SLinus Torvalds /* write the iags.
23751da177e4SLinus Torvalds */
23761da177e4SLinus Torvalds if (amp)
23771da177e4SLinus Torvalds write_metapage(amp);
23781da177e4SLinus Torvalds if (bmp)
23791da177e4SLinus Torvalds write_metapage(bmp);
23801da177e4SLinus Torvalds if (cmp)
23811da177e4SLinus Torvalds write_metapage(cmp);
23821da177e4SLinus Torvalds
23831da177e4SLinus Torvalds return (0);
23841da177e4SLinus Torvalds
23851da177e4SLinus Torvalds error_out:
23861da177e4SLinus Torvalds
23871da177e4SLinus Torvalds /* release the iags.
23881da177e4SLinus Torvalds */
23891da177e4SLinus Torvalds if (amp)
23901da177e4SLinus Torvalds release_metapage(amp);
23911da177e4SLinus Torvalds if (bmp)
23921da177e4SLinus Torvalds release_metapage(bmp);
23931da177e4SLinus Torvalds if (cmp)
23941da177e4SLinus Torvalds release_metapage(cmp);
23951da177e4SLinus Torvalds
23961da177e4SLinus Torvalds return (rc);
23971da177e4SLinus Torvalds }
23981da177e4SLinus Torvalds
23991da177e4SLinus Torvalds
24001da177e4SLinus Torvalds /*
24011da177e4SLinus Torvalds * NAME: diNewIAG(imap,iagnop,agno)
24021da177e4SLinus Torvalds *
24031da177e4SLinus Torvalds * FUNCTION: allocate a new iag for an allocation group.
24041da177e4SLinus Torvalds *
24051da177e4SLinus Torvalds * first tries to allocate the iag from the inode map
24061da177e4SLinus Torvalds * iagfree list:
24071da177e4SLinus Torvalds * if the list has free iags, the head of the list is removed
24081da177e4SLinus Torvalds * and returned to satisfy the request.
24091da177e4SLinus Torvalds * if the inode map's iag free list is empty, the inode map
24101da177e4SLinus Torvalds * is extended to hold a new iag. this new iag is initialized
24111da177e4SLinus Torvalds * and returned to satisfy the request.
24121da177e4SLinus Torvalds *
24131da177e4SLinus Torvalds * PARAMETERS:
24141da177e4SLinus Torvalds * imap - pointer to inode map control structure.
24151da177e4SLinus Torvalds * iagnop - pointer to an iag number set with the number of the
24161da177e4SLinus Torvalds * newly allocated iag upon successful return.
24171da177e4SLinus Torvalds * agno - allocation group number.
24181da177e4SLinus Torvalds * bpp - Buffer pointer to be filled in with new IAG's buffer
24191da177e4SLinus Torvalds *
24201da177e4SLinus Torvalds * RETURN VALUES:
24211da177e4SLinus Torvalds * 0 - success.
24221da177e4SLinus Torvalds * -ENOSPC - insufficient disk resources.
24231da177e4SLinus Torvalds * -EIO - i/o error.
24241da177e4SLinus Torvalds *
24251da177e4SLinus Torvalds * serialization:
24261da177e4SLinus Torvalds * AG lock held on entry/exit;
24271da177e4SLinus Torvalds * write lock on the map is held inside;
24281da177e4SLinus Torvalds * read lock on the map is held on successful completion;
24291da177e4SLinus Torvalds *
24301da177e4SLinus Torvalds * note: new iag transaction:
24311da177e4SLinus Torvalds * . synchronously write iag;
24321da177e4SLinus Torvalds * . write log of xtree and inode of imap;
24331da177e4SLinus Torvalds * . commit;
24341da177e4SLinus Torvalds * . synchronous write of xtree (right to left, bottom to top);
24351da177e4SLinus Torvalds * . at start of logredo(): init in-memory imap with one additional iag page;
24361da177e4SLinus Torvalds * . at end of logredo(): re-read imap inode to determine
24371da177e4SLinus Torvalds * new imap size;
24381da177e4SLinus Torvalds */
24391da177e4SLinus Torvalds static int
diNewIAG(struct inomap * imap,int * iagnop,int agno,struct metapage ** mpp)24401da177e4SLinus Torvalds diNewIAG(struct inomap * imap, int *iagnop, int agno, struct metapage ** mpp)
24411da177e4SLinus Torvalds {
24421da177e4SLinus Torvalds int rc;
24431da177e4SLinus Torvalds int iagno, i, xlen;
24441da177e4SLinus Torvalds struct inode *ipimap;
24451da177e4SLinus Torvalds struct super_block *sb;
24461da177e4SLinus Torvalds struct jfs_sb_info *sbi;
24471da177e4SLinus Torvalds struct metapage *mp;
24481da177e4SLinus Torvalds struct iag *iagp;
24491da177e4SLinus Torvalds s64 xaddr = 0;
24501da177e4SLinus Torvalds s64 blkno;
24511da177e4SLinus Torvalds tid_t tid;
24521da177e4SLinus Torvalds struct inode *iplist[1];
24531da177e4SLinus Torvalds
24541da177e4SLinus Torvalds /* pick up pointers to the inode map and mount inodes */
24551da177e4SLinus Torvalds ipimap = imap->im_ipimap;
24561da177e4SLinus Torvalds sb = ipimap->i_sb;
24571da177e4SLinus Torvalds sbi = JFS_SBI(sb);
24581da177e4SLinus Torvalds
24591da177e4SLinus Torvalds /* acquire the free iag lock */
24601da177e4SLinus Torvalds IAGFREE_LOCK(imap);
24611da177e4SLinus Torvalds
24621da177e4SLinus Torvalds /* if there are any iags on the inode map free iag list,
24631da177e4SLinus Torvalds * allocate the iag from the head of the list.
24641da177e4SLinus Torvalds */
24651da177e4SLinus Torvalds if (imap->im_freeiag >= 0) {
24661da177e4SLinus Torvalds /* pick up the iag number at the head of the list */
24671da177e4SLinus Torvalds iagno = imap->im_freeiag;
24681da177e4SLinus Torvalds
24691da177e4SLinus Torvalds /* determine the logical block number of the iag */
24701da177e4SLinus Torvalds blkno = IAGTOLBLK(iagno, sbi->l2nbperpage);
24711da177e4SLinus Torvalds } else {
24721da177e4SLinus Torvalds /* no free iags. the inode map will have to be extented
24731da177e4SLinus Torvalds * to include a new iag.
24741da177e4SLinus Torvalds */
24751da177e4SLinus Torvalds
24761da177e4SLinus Torvalds /* acquire inode map lock */
247782d5b9a7SDave Kleikamp IWRITE_LOCK(ipimap, RDWRLOCK_IMAP);
24781da177e4SLinus Torvalds
24791da177e4SLinus Torvalds if (ipimap->i_size >> L2PSIZE != imap->im_nextiag + 1) {
24801da177e4SLinus Torvalds IWRITE_UNLOCK(ipimap);
24811da177e4SLinus Torvalds IAGFREE_UNLOCK(imap);
24821da177e4SLinus Torvalds jfs_error(imap->im_ipimap->i_sb,
2483eb8630d7SJoe Perches "ipimap->i_size is wrong\n");
24841da177e4SLinus Torvalds return -EIO;
24851da177e4SLinus Torvalds }
24861da177e4SLinus Torvalds
24871da177e4SLinus Torvalds
248825985edcSLucas De Marchi /* get the next available iag number */
24891da177e4SLinus Torvalds iagno = imap->im_nextiag;
24901da177e4SLinus Torvalds
24911da177e4SLinus Torvalds /* make sure that we have not exceeded the maximum inode
24921da177e4SLinus Torvalds * number limit.
24931da177e4SLinus Torvalds */
24941da177e4SLinus Torvalds if (iagno > (MAXIAGS - 1)) {
24951da177e4SLinus Torvalds /* release the inode map lock */
24961da177e4SLinus Torvalds IWRITE_UNLOCK(ipimap);
24971da177e4SLinus Torvalds
24981da177e4SLinus Torvalds rc = -ENOSPC;
24991da177e4SLinus Torvalds goto out;
25001da177e4SLinus Torvalds }
25011da177e4SLinus Torvalds
25021da177e4SLinus Torvalds /*
25031da177e4SLinus Torvalds * synchronously append new iag page.
25041da177e4SLinus Torvalds */
25051da177e4SLinus Torvalds /* determine the logical address of iag page to append */
25061da177e4SLinus Torvalds blkno = IAGTOLBLK(iagno, sbi->l2nbperpage);
25071da177e4SLinus Torvalds
25081da177e4SLinus Torvalds /* Allocate extent for new iag page */
25091da177e4SLinus Torvalds xlen = sbi->nbperpage;
25101da177e4SLinus Torvalds if ((rc = dbAlloc(ipimap, 0, (s64) xlen, &xaddr))) {
25111da177e4SLinus Torvalds /* release the inode map lock */
25121da177e4SLinus Torvalds IWRITE_UNLOCK(ipimap);
25131da177e4SLinus Torvalds
25141da177e4SLinus Torvalds goto out;
25151da177e4SLinus Torvalds }
25161da177e4SLinus Torvalds
2517d2e83707SDave Kleikamp /*
2518d2e83707SDave Kleikamp * start transaction of update of the inode map
2519d2e83707SDave Kleikamp * addressing structure pointing to the new iag page;
2520d2e83707SDave Kleikamp */
2521d2e83707SDave Kleikamp tid = txBegin(sb, COMMIT_FORCE);
25221de87444SIngo Molnar mutex_lock(&JFS_IP(ipimap)->commit_mutex);
2523d2e83707SDave Kleikamp
2524d2e83707SDave Kleikamp /* update the inode map addressing structure to point to it */
2525d2e83707SDave Kleikamp if ((rc =
2526d2e83707SDave Kleikamp xtInsert(tid, ipimap, 0, blkno, xlen, &xaddr, 0))) {
2527d2e83707SDave Kleikamp txEnd(tid);
25281de87444SIngo Molnar mutex_unlock(&JFS_IP(ipimap)->commit_mutex);
25291da177e4SLinus Torvalds /* Free the blocks allocated for the iag since it was
25301da177e4SLinus Torvalds * not successfully added to the inode map
25311da177e4SLinus Torvalds */
25321da177e4SLinus Torvalds dbFree(ipimap, xaddr, (s64) xlen);
25331da177e4SLinus Torvalds
25341da177e4SLinus Torvalds /* release the inode map lock */
25351da177e4SLinus Torvalds IWRITE_UNLOCK(ipimap);
25361da177e4SLinus Torvalds
2537d2e83707SDave Kleikamp goto out;
2538d2e83707SDave Kleikamp }
2539d2e83707SDave Kleikamp
2540d2e83707SDave Kleikamp /* update the inode map's inode to reflect the extension */
2541d2e83707SDave Kleikamp ipimap->i_size += PSIZE;
2542d2e83707SDave Kleikamp inode_add_bytes(ipimap, PSIZE);
2543d2e83707SDave Kleikamp
2544d2e83707SDave Kleikamp /* assign a buffer for the page */
2545d2e83707SDave Kleikamp mp = get_metapage(ipimap, blkno, PSIZE, 0);
2546d2e83707SDave Kleikamp if (!mp) {
2547d2e83707SDave Kleikamp /*
2548d2e83707SDave Kleikamp * This is very unlikely since we just created the
2549d2e83707SDave Kleikamp * extent, but let's try to handle it correctly
2550d2e83707SDave Kleikamp */
2551d2e83707SDave Kleikamp xtTruncate(tid, ipimap, ipimap->i_size - PSIZE,
2552d2e83707SDave Kleikamp COMMIT_PWMAP);
2553d2e83707SDave Kleikamp
2554d2e83707SDave Kleikamp txAbort(tid, 0);
2555d2e83707SDave Kleikamp txEnd(tid);
255679f52b77SDave Kleikamp mutex_unlock(&JFS_IP(ipimap)->commit_mutex);
2557d2e83707SDave Kleikamp
2558d2e83707SDave Kleikamp /* release the inode map lock */
2559d2e83707SDave Kleikamp IWRITE_UNLOCK(ipimap);
2560d2e83707SDave Kleikamp
25611da177e4SLinus Torvalds rc = -EIO;
25621da177e4SLinus Torvalds goto out;
25631da177e4SLinus Torvalds }
25641da177e4SLinus Torvalds iagp = (struct iag *) mp->data;
25651da177e4SLinus Torvalds
25661da177e4SLinus Torvalds /* init the iag */
25671da177e4SLinus Torvalds memset(iagp, 0, sizeof(struct iag));
25681da177e4SLinus Torvalds iagp->iagnum = cpu_to_le32(iagno);
25691da177e4SLinus Torvalds iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
25701da177e4SLinus Torvalds iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
25711da177e4SLinus Torvalds iagp->iagfree = cpu_to_le32(-1);
25721da177e4SLinus Torvalds iagp->nfreeinos = 0;
25731da177e4SLinus Torvalds iagp->nfreeexts = cpu_to_le32(EXTSPERIAG);
25741da177e4SLinus Torvalds
25751da177e4SLinus Torvalds /* initialize the free inode summary map (free extent
25761da177e4SLinus Torvalds * summary map initialization handled by bzero).
25771da177e4SLinus Torvalds */
25781da177e4SLinus Torvalds for (i = 0; i < SMAPSZ; i++)
25791da177e4SLinus Torvalds iagp->inosmap[i] = cpu_to_le32(ONES);
25801da177e4SLinus Torvalds
25811da177e4SLinus Torvalds /*
2582d2e83707SDave Kleikamp * Write and sync the metapage
25831da177e4SLinus Torvalds */
25841da177e4SLinus Torvalds flush_metapage(mp);
25851da177e4SLinus Torvalds
25861da177e4SLinus Torvalds /*
25871da177e4SLinus Torvalds * txCommit(COMMIT_FORCE) will synchronously write address
25881da177e4SLinus Torvalds * index pages and inode after commit in careful update order
25891da177e4SLinus Torvalds * of address index pages (right to left, bottom up);
25901da177e4SLinus Torvalds */
25911da177e4SLinus Torvalds iplist[0] = ipimap;
25921da177e4SLinus Torvalds rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
25931da177e4SLinus Torvalds
25941da177e4SLinus Torvalds txEnd(tid);
25951de87444SIngo Molnar mutex_unlock(&JFS_IP(ipimap)->commit_mutex);
25961da177e4SLinus Torvalds
25971da177e4SLinus Torvalds duplicateIXtree(sb, blkno, xlen, &xaddr);
25981da177e4SLinus Torvalds
259925985edcSLucas De Marchi /* update the next available iag number */
26001da177e4SLinus Torvalds imap->im_nextiag += 1;
26011da177e4SLinus Torvalds
26021da177e4SLinus Torvalds /* Add the iag to the iag free list so we don't lose the iag
26031da177e4SLinus Torvalds * if a failure happens now.
26041da177e4SLinus Torvalds */
26051da177e4SLinus Torvalds imap->im_freeiag = iagno;
26061da177e4SLinus Torvalds
26071da177e4SLinus Torvalds /* Until we have logredo working, we want the imap inode &
26081da177e4SLinus Torvalds * control page to be up to date.
26091da177e4SLinus Torvalds */
26101da177e4SLinus Torvalds diSync(ipimap);
26111da177e4SLinus Torvalds
26121da177e4SLinus Torvalds /* release the inode map lock */
26131da177e4SLinus Torvalds IWRITE_UNLOCK(ipimap);
26141da177e4SLinus Torvalds }
26151da177e4SLinus Torvalds
26161da177e4SLinus Torvalds /* obtain read lock on map */
261782d5b9a7SDave Kleikamp IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
26181da177e4SLinus Torvalds
26191da177e4SLinus Torvalds /* read the iag */
26201da177e4SLinus Torvalds if ((rc = diIAGRead(imap, iagno, &mp))) {
26211da177e4SLinus Torvalds IREAD_UNLOCK(ipimap);
26221da177e4SLinus Torvalds rc = -EIO;
26231da177e4SLinus Torvalds goto out;
26241da177e4SLinus Torvalds }
26251da177e4SLinus Torvalds iagp = (struct iag *) mp->data;
26261da177e4SLinus Torvalds
26271da177e4SLinus Torvalds /* remove the iag from the iag free list */
26281da177e4SLinus Torvalds imap->im_freeiag = le32_to_cpu(iagp->iagfree);
26291da177e4SLinus Torvalds iagp->iagfree = cpu_to_le32(-1);
26301da177e4SLinus Torvalds
26311da177e4SLinus Torvalds /* set the return iag number and buffer pointer */
26321da177e4SLinus Torvalds *iagnop = iagno;
26331da177e4SLinus Torvalds *mpp = mp;
26341da177e4SLinus Torvalds
26351da177e4SLinus Torvalds out:
26361da177e4SLinus Torvalds /* release the iag free lock */
26371da177e4SLinus Torvalds IAGFREE_UNLOCK(imap);
26381da177e4SLinus Torvalds
26391da177e4SLinus Torvalds return (rc);
26401da177e4SLinus Torvalds }
26411da177e4SLinus Torvalds
26421da177e4SLinus Torvalds /*
26431da177e4SLinus Torvalds * NAME: diIAGRead()
26441da177e4SLinus Torvalds *
26451da177e4SLinus Torvalds * FUNCTION: get the buffer for the specified iag within a fileset
26461da177e4SLinus Torvalds * or aggregate inode map.
26471da177e4SLinus Torvalds *
26481da177e4SLinus Torvalds * PARAMETERS:
26491da177e4SLinus Torvalds * imap - pointer to inode map control structure.
26501da177e4SLinus Torvalds * iagno - iag number.
26511da177e4SLinus Torvalds * bpp - point to buffer pointer to be filled in on successful
26521da177e4SLinus Torvalds * exit.
26531da177e4SLinus Torvalds *
26541da177e4SLinus Torvalds * SERIALIZATION:
26551da177e4SLinus Torvalds * must have read lock on imap inode
26561da177e4SLinus Torvalds * (When called by diExtendFS, the filesystem is quiesced, therefore
26571da177e4SLinus Torvalds * the read lock is unnecessary.)
26581da177e4SLinus Torvalds *
26591da177e4SLinus Torvalds * RETURN VALUES:
26601da177e4SLinus Torvalds * 0 - success.
26611da177e4SLinus Torvalds * -EIO - i/o error.
26621da177e4SLinus Torvalds */
diIAGRead(struct inomap * imap,int iagno,struct metapage ** mpp)26631da177e4SLinus Torvalds static int diIAGRead(struct inomap * imap, int iagno, struct metapage ** mpp)
26641da177e4SLinus Torvalds {
26651da177e4SLinus Torvalds struct inode *ipimap = imap->im_ipimap;
26661da177e4SLinus Torvalds s64 blkno;
26671da177e4SLinus Torvalds
26681da177e4SLinus Torvalds /* compute the logical block number of the iag. */
26691da177e4SLinus Torvalds blkno = IAGTOLBLK(iagno, JFS_SBI(ipimap->i_sb)->l2nbperpage);
26701da177e4SLinus Torvalds
26711da177e4SLinus Torvalds /* read the iag. */
26721da177e4SLinus Torvalds *mpp = read_metapage(ipimap, blkno, PSIZE, 0);
26731da177e4SLinus Torvalds if (*mpp == NULL) {
26741da177e4SLinus Torvalds return -EIO;
26751da177e4SLinus Torvalds }
26761da177e4SLinus Torvalds
26771da177e4SLinus Torvalds return (0);
26781da177e4SLinus Torvalds }
26791da177e4SLinus Torvalds
26801da177e4SLinus Torvalds /*
26811da177e4SLinus Torvalds * NAME: diFindFree()
26821da177e4SLinus Torvalds *
26831da177e4SLinus Torvalds * FUNCTION: find the first free bit in a word starting at
26841da177e4SLinus Torvalds * the specified bit position.
26851da177e4SLinus Torvalds *
26861da177e4SLinus Torvalds * PARAMETERS:
26871da177e4SLinus Torvalds * word - word to be examined.
26881da177e4SLinus Torvalds * start - starting bit position.
26891da177e4SLinus Torvalds *
26901da177e4SLinus Torvalds * RETURN VALUES:
26911da177e4SLinus Torvalds * bit position of first free bit in the word or 32 if
26921da177e4SLinus Torvalds * no free bits were found.
26931da177e4SLinus Torvalds */
diFindFree(u32 word,int start)26941da177e4SLinus Torvalds static int diFindFree(u32 word, int start)
26951da177e4SLinus Torvalds {
26961da177e4SLinus Torvalds int bitno;
26971da177e4SLinus Torvalds assert(start < 32);
26981da177e4SLinus Torvalds /* scan the word for the first free bit. */
26991da177e4SLinus Torvalds for (word <<= start, bitno = start; bitno < 32;
27001da177e4SLinus Torvalds bitno++, word <<= 1) {
27011da177e4SLinus Torvalds if ((word & HIGHORDER) == 0)
27021da177e4SLinus Torvalds break;
27031da177e4SLinus Torvalds }
27041da177e4SLinus Torvalds return (bitno);
27051da177e4SLinus Torvalds }
27061da177e4SLinus Torvalds
27071da177e4SLinus Torvalds /*
27081da177e4SLinus Torvalds * NAME: diUpdatePMap()
27091da177e4SLinus Torvalds *
27101da177e4SLinus Torvalds * FUNCTION: Update the persistent map in an IAG for the allocation or
27111da177e4SLinus Torvalds * freeing of the specified inode.
27121da177e4SLinus Torvalds *
27131da177e4SLinus Torvalds * PRE CONDITIONS: Working map has already been updated for allocate.
27141da177e4SLinus Torvalds *
27151da177e4SLinus Torvalds * PARAMETERS:
27161da177e4SLinus Torvalds * ipimap - Incore inode map inode
27171da177e4SLinus Torvalds * inum - Number of inode to mark in permanent map
27184d81715fSRichard Knutsson * is_free - If 'true' indicates inode should be marked freed, otherwise
27191da177e4SLinus Torvalds * indicates inode should be marked allocated.
27201da177e4SLinus Torvalds *
27211da177e4SLinus Torvalds * RETURN VALUES:
27221da177e4SLinus Torvalds * 0 for success
27231da177e4SLinus Torvalds */
27241da177e4SLinus Torvalds int
diUpdatePMap(struct inode * ipimap,unsigned long inum,bool is_free,struct tblock * tblk)27251da177e4SLinus Torvalds diUpdatePMap(struct inode *ipimap,
27264d81715fSRichard Knutsson unsigned long inum, bool is_free, struct tblock * tblk)
27271da177e4SLinus Torvalds {
27281da177e4SLinus Torvalds int rc;
27291da177e4SLinus Torvalds struct iag *iagp;
27301da177e4SLinus Torvalds struct metapage *mp;
27311da177e4SLinus Torvalds int iagno, ino, extno, bitno;
27321da177e4SLinus Torvalds struct inomap *imap;
27331da177e4SLinus Torvalds u32 mask;
27341da177e4SLinus Torvalds struct jfs_log *log;
27351da177e4SLinus Torvalds int lsn, difft, diffp;
27367fab479bSDave Kleikamp unsigned long flags;
27371da177e4SLinus Torvalds
27381da177e4SLinus Torvalds imap = JFS_IP(ipimap)->i_imap;
27391da177e4SLinus Torvalds /* get the iag number containing the inode */
27401da177e4SLinus Torvalds iagno = INOTOIAG(inum);
27411da177e4SLinus Torvalds /* make sure that the iag is contained within the map */
27421da177e4SLinus Torvalds if (iagno >= imap->im_nextiag) {
2743eb8630d7SJoe Perches jfs_error(ipimap->i_sb, "the iag is outside the map\n");
27441da177e4SLinus Torvalds return -EIO;
27451da177e4SLinus Torvalds }
27461da177e4SLinus Torvalds /* read the iag */
274782d5b9a7SDave Kleikamp IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
27481da177e4SLinus Torvalds rc = diIAGRead(imap, iagno, &mp);
27491da177e4SLinus Torvalds IREAD_UNLOCK(ipimap);
27501da177e4SLinus Torvalds if (rc)
27511da177e4SLinus Torvalds return (rc);
27527fab479bSDave Kleikamp metapage_wait_for_io(mp);
27531da177e4SLinus Torvalds iagp = (struct iag *) mp->data;
27541da177e4SLinus Torvalds /* get the inode number and extent number of the inode within
27551da177e4SLinus Torvalds * the iag and the inode number within the extent.
27561da177e4SLinus Torvalds */
27571da177e4SLinus Torvalds ino = inum & (INOSPERIAG - 1);
27581da177e4SLinus Torvalds extno = ino >> L2INOSPEREXT;
27591da177e4SLinus Torvalds bitno = ino & (INOSPEREXT - 1);
27601da177e4SLinus Torvalds mask = HIGHORDER >> bitno;
27611da177e4SLinus Torvalds /*
27621da177e4SLinus Torvalds * mark the inode free in persistent map:
27631da177e4SLinus Torvalds */
27644d81715fSRichard Knutsson if (is_free) {
27651da177e4SLinus Torvalds /* The inode should have been allocated both in working
27661da177e4SLinus Torvalds * map and in persistent map;
27671da177e4SLinus Torvalds * the inode will be freed from working map at the release
27681da177e4SLinus Torvalds * of last reference release;
27691da177e4SLinus Torvalds */
27701da177e4SLinus Torvalds if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
27711da177e4SLinus Torvalds jfs_error(ipimap->i_sb,
2772eb8630d7SJoe Perches "inode %ld not marked as allocated in wmap!\n",
2773eb8630d7SJoe Perches inum);
27741da177e4SLinus Torvalds }
27751da177e4SLinus Torvalds if (!(le32_to_cpu(iagp->pmap[extno]) & mask)) {
27761da177e4SLinus Torvalds jfs_error(ipimap->i_sb,
2777eb8630d7SJoe Perches "inode %ld not marked as allocated in pmap!\n",
2778eb8630d7SJoe Perches inum);
27791da177e4SLinus Torvalds }
27801da177e4SLinus Torvalds /* update the bitmap for the extent of the freed inode */
27811da177e4SLinus Torvalds iagp->pmap[extno] &= cpu_to_le32(~mask);
27821da177e4SLinus Torvalds }
27831da177e4SLinus Torvalds /*
27841da177e4SLinus Torvalds * mark the inode allocated in persistent map:
27851da177e4SLinus Torvalds */
27861da177e4SLinus Torvalds else {
27871da177e4SLinus Torvalds /* The inode should be already allocated in the working map
27881da177e4SLinus Torvalds * and should be free in persistent map;
27891da177e4SLinus Torvalds */
27901da177e4SLinus Torvalds if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
27911da177e4SLinus Torvalds release_metapage(mp);
27921da177e4SLinus Torvalds jfs_error(ipimap->i_sb,
2793eb8630d7SJoe Perches "the inode is not allocated in the working map\n");
27941da177e4SLinus Torvalds return -EIO;
27951da177e4SLinus Torvalds }
27961da177e4SLinus Torvalds if ((le32_to_cpu(iagp->pmap[extno]) & mask) != 0) {
27971da177e4SLinus Torvalds release_metapage(mp);
27981da177e4SLinus Torvalds jfs_error(ipimap->i_sb,
2799eb8630d7SJoe Perches "the inode is not free in the persistent map\n");
28001da177e4SLinus Torvalds return -EIO;
28011da177e4SLinus Torvalds }
28021da177e4SLinus Torvalds /* update the bitmap for the extent of the allocated inode */
28031da177e4SLinus Torvalds iagp->pmap[extno] |= cpu_to_le32(mask);
28041da177e4SLinus Torvalds }
28051da177e4SLinus Torvalds /*
28061da177e4SLinus Torvalds * update iag lsn
28071da177e4SLinus Torvalds */
28081da177e4SLinus Torvalds lsn = tblk->lsn;
28091da177e4SLinus Torvalds log = JFS_SBI(tblk->sb)->log;
2810be0bf7daSDave Kleikamp LOGSYNC_LOCK(log, flags);
28111da177e4SLinus Torvalds if (mp->lsn != 0) {
28121da177e4SLinus Torvalds /* inherit older/smaller lsn */
28131da177e4SLinus Torvalds logdiff(difft, lsn, log);
28141da177e4SLinus Torvalds logdiff(diffp, mp->lsn, log);
28151da177e4SLinus Torvalds if (difft < diffp) {
28161da177e4SLinus Torvalds mp->lsn = lsn;
28171da177e4SLinus Torvalds /* move mp after tblock in logsync list */
28181da177e4SLinus Torvalds list_move(&mp->synclist, &tblk->synclist);
28191da177e4SLinus Torvalds }
28201da177e4SLinus Torvalds /* inherit younger/larger clsn */
28211da177e4SLinus Torvalds assert(mp->clsn);
28221da177e4SLinus Torvalds logdiff(difft, tblk->clsn, log);
28231da177e4SLinus Torvalds logdiff(diffp, mp->clsn, log);
28241da177e4SLinus Torvalds if (difft > diffp)
28251da177e4SLinus Torvalds mp->clsn = tblk->clsn;
28261da177e4SLinus Torvalds } else {
28271da177e4SLinus Torvalds mp->log = log;
28281da177e4SLinus Torvalds mp->lsn = lsn;
28291da177e4SLinus Torvalds /* insert mp after tblock in logsync list */
28301da177e4SLinus Torvalds log->count++;
28311da177e4SLinus Torvalds list_add(&mp->synclist, &tblk->synclist);
28321da177e4SLinus Torvalds mp->clsn = tblk->clsn;
28331da177e4SLinus Torvalds }
2834be0bf7daSDave Kleikamp LOGSYNC_UNLOCK(log, flags);
28351da177e4SLinus Torvalds write_metapage(mp);
28361da177e4SLinus Torvalds return (0);
28371da177e4SLinus Torvalds }
28381da177e4SLinus Torvalds
28391da177e4SLinus Torvalds /*
28401da177e4SLinus Torvalds * diExtendFS()
28411da177e4SLinus Torvalds *
28421da177e4SLinus Torvalds * function: update imap for extendfs();
28431da177e4SLinus Torvalds *
28441da177e4SLinus Torvalds * note: AG size has been increased s.t. each k old contiguous AGs are
28451da177e4SLinus Torvalds * coalesced into a new AG;
28461da177e4SLinus Torvalds */
diExtendFS(struct inode * ipimap,struct inode * ipbmap)28471da177e4SLinus Torvalds int diExtendFS(struct inode *ipimap, struct inode *ipbmap)
28481da177e4SLinus Torvalds {
28491da177e4SLinus Torvalds int rc, rcx = 0;
28501da177e4SLinus Torvalds struct inomap *imap = JFS_IP(ipimap)->i_imap;
28511da177e4SLinus Torvalds struct iag *iagp = NULL, *hiagp = NULL;
28521da177e4SLinus Torvalds struct bmap *mp = JFS_SBI(ipbmap->i_sb)->bmap;
28531da177e4SLinus Torvalds struct metapage *bp, *hbp;
28541da177e4SLinus Torvalds int i, n, head;
28551da177e4SLinus Torvalds int numinos, xnuminos = 0, xnumfree = 0;
28561da177e4SLinus Torvalds s64 agstart;
28571da177e4SLinus Torvalds
28581da177e4SLinus Torvalds jfs_info("diExtendFS: nextiag:%d numinos:%d numfree:%d",
28591da177e4SLinus Torvalds imap->im_nextiag, atomic_read(&imap->im_numinos),
28601da177e4SLinus Torvalds atomic_read(&imap->im_numfree));
28611da177e4SLinus Torvalds
28621da177e4SLinus Torvalds /*
28631da177e4SLinus Torvalds * reconstruct imap
28641da177e4SLinus Torvalds *
28651da177e4SLinus Torvalds * coalesce contiguous k (newAGSize/oldAGSize) AGs;
28661da177e4SLinus Torvalds * i.e., (AGi, ..., AGj) where i = k*n and j = k*(n+1) - 1 to AGn;
28671da177e4SLinus Torvalds * note: new AG size = old AG size * (2**x).
28681da177e4SLinus Torvalds */
28691da177e4SLinus Torvalds
28701da177e4SLinus Torvalds /* init per AG control information im_agctl[] */
28711da177e4SLinus Torvalds for (i = 0; i < MAXAG; i++) {
28721da177e4SLinus Torvalds imap->im_agctl[i].inofree = -1;
28731da177e4SLinus Torvalds imap->im_agctl[i].extfree = -1;
28741da177e4SLinus Torvalds imap->im_agctl[i].numinos = 0; /* number of backed inodes */
28751da177e4SLinus Torvalds imap->im_agctl[i].numfree = 0; /* number of free backed inodes */
28761da177e4SLinus Torvalds }
28771da177e4SLinus Torvalds
28781da177e4SLinus Torvalds /*
28791da177e4SLinus Torvalds * process each iag page of the map.
28801da177e4SLinus Torvalds *
28811da177e4SLinus Torvalds * rebuild AG Free Inode List, AG Free Inode Extent List;
28821da177e4SLinus Torvalds */
28831da177e4SLinus Torvalds for (i = 0; i < imap->im_nextiag; i++) {
28841da177e4SLinus Torvalds if ((rc = diIAGRead(imap, i, &bp))) {
28851da177e4SLinus Torvalds rcx = rc;
28861da177e4SLinus Torvalds continue;
28871da177e4SLinus Torvalds }
28881da177e4SLinus Torvalds iagp = (struct iag *) bp->data;
28891da177e4SLinus Torvalds if (le32_to_cpu(iagp->iagnum) != i) {
28901da177e4SLinus Torvalds release_metapage(bp);
2891eb8630d7SJoe Perches jfs_error(ipimap->i_sb, "unexpected value of iagnum\n");
28921da177e4SLinus Torvalds return -EIO;
28931da177e4SLinus Torvalds }
28941da177e4SLinus Torvalds
28951da177e4SLinus Torvalds /* leave free iag in the free iag list */
28961da177e4SLinus Torvalds if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
28971da177e4SLinus Torvalds release_metapage(bp);
28981da177e4SLinus Torvalds continue;
28991da177e4SLinus Torvalds }
29001da177e4SLinus Torvalds
29011da177e4SLinus Torvalds agstart = le64_to_cpu(iagp->agstart);
29021da177e4SLinus Torvalds n = agstart >> mp->db_agl2size;
290328e0fa89SDave Kleikamp iagp->agstart = cpu_to_le64((s64)n << mp->db_agl2size);
29041da177e4SLinus Torvalds
29051da177e4SLinus Torvalds /* compute backed inodes */
29061da177e4SLinus Torvalds numinos = (EXTSPERIAG - le32_to_cpu(iagp->nfreeexts))
29071da177e4SLinus Torvalds << L2INOSPEREXT;
29081da177e4SLinus Torvalds if (numinos > 0) {
29091da177e4SLinus Torvalds /* merge AG backed inodes */
29101da177e4SLinus Torvalds imap->im_agctl[n].numinos += numinos;
29111da177e4SLinus Torvalds xnuminos += numinos;
29121da177e4SLinus Torvalds }
29131da177e4SLinus Torvalds
29141da177e4SLinus Torvalds /* if any backed free inodes, insert at AG free inode list */
29151da177e4SLinus Torvalds if ((int) le32_to_cpu(iagp->nfreeinos) > 0) {
29161da177e4SLinus Torvalds if ((head = imap->im_agctl[n].inofree) == -1) {
29171da177e4SLinus Torvalds iagp->inofreefwd = cpu_to_le32(-1);
29181da177e4SLinus Torvalds iagp->inofreeback = cpu_to_le32(-1);
29191da177e4SLinus Torvalds } else {
29201da177e4SLinus Torvalds if ((rc = diIAGRead(imap, head, &hbp))) {
29211da177e4SLinus Torvalds rcx = rc;
29221da177e4SLinus Torvalds goto nextiag;
29231da177e4SLinus Torvalds }
29241da177e4SLinus Torvalds hiagp = (struct iag *) hbp->data;
29251da177e4SLinus Torvalds hiagp->inofreeback = iagp->iagnum;
29261da177e4SLinus Torvalds iagp->inofreefwd = cpu_to_le32(head);
29271da177e4SLinus Torvalds iagp->inofreeback = cpu_to_le32(-1);
29281da177e4SLinus Torvalds write_metapage(hbp);
29291da177e4SLinus Torvalds }
29301da177e4SLinus Torvalds
29311da177e4SLinus Torvalds imap->im_agctl[n].inofree =
29321da177e4SLinus Torvalds le32_to_cpu(iagp->iagnum);
29331da177e4SLinus Torvalds
29341da177e4SLinus Torvalds /* merge AG backed free inodes */
29351da177e4SLinus Torvalds imap->im_agctl[n].numfree +=
29361da177e4SLinus Torvalds le32_to_cpu(iagp->nfreeinos);
29371da177e4SLinus Torvalds xnumfree += le32_to_cpu(iagp->nfreeinos);
29381da177e4SLinus Torvalds }
29391da177e4SLinus Torvalds
29401da177e4SLinus Torvalds /* if any free extents, insert at AG free extent list */
29411da177e4SLinus Torvalds if (le32_to_cpu(iagp->nfreeexts) > 0) {
29421da177e4SLinus Torvalds if ((head = imap->im_agctl[n].extfree) == -1) {
29431da177e4SLinus Torvalds iagp->extfreefwd = cpu_to_le32(-1);
29441da177e4SLinus Torvalds iagp->extfreeback = cpu_to_le32(-1);
29451da177e4SLinus Torvalds } else {
29461da177e4SLinus Torvalds if ((rc = diIAGRead(imap, head, &hbp))) {
29471da177e4SLinus Torvalds rcx = rc;
29481da177e4SLinus Torvalds goto nextiag;
29491da177e4SLinus Torvalds }
29501da177e4SLinus Torvalds hiagp = (struct iag *) hbp->data;
29511da177e4SLinus Torvalds hiagp->extfreeback = iagp->iagnum;
29521da177e4SLinus Torvalds iagp->extfreefwd = cpu_to_le32(head);
29531da177e4SLinus Torvalds iagp->extfreeback = cpu_to_le32(-1);
29541da177e4SLinus Torvalds write_metapage(hbp);
29551da177e4SLinus Torvalds }
29561da177e4SLinus Torvalds
29571da177e4SLinus Torvalds imap->im_agctl[n].extfree =
29581da177e4SLinus Torvalds le32_to_cpu(iagp->iagnum);
29591da177e4SLinus Torvalds }
29601da177e4SLinus Torvalds
29611da177e4SLinus Torvalds nextiag:
29621da177e4SLinus Torvalds write_metapage(bp);
29631da177e4SLinus Torvalds }
29641da177e4SLinus Torvalds
29651da177e4SLinus Torvalds if (xnuminos != atomic_read(&imap->im_numinos) ||
29661da177e4SLinus Torvalds xnumfree != atomic_read(&imap->im_numfree)) {
2967eb8630d7SJoe Perches jfs_error(ipimap->i_sb, "numinos or numfree incorrect\n");
29681da177e4SLinus Torvalds return -EIO;
29691da177e4SLinus Torvalds }
29701da177e4SLinus Torvalds
29711da177e4SLinus Torvalds return rcx;
29721da177e4SLinus Torvalds }
29731da177e4SLinus Torvalds
29741da177e4SLinus Torvalds
29751da177e4SLinus Torvalds /*
29761da177e4SLinus Torvalds * duplicateIXtree()
29771da177e4SLinus Torvalds *
29781da177e4SLinus Torvalds * serialization: IWRITE_LOCK held on entry/exit
29791da177e4SLinus Torvalds *
29801da177e4SLinus Torvalds * note: shadow page with regular inode (rel.2);
29811da177e4SLinus Torvalds */
duplicateIXtree(struct super_block * sb,s64 blkno,int xlen,s64 * xaddr)29821da177e4SLinus Torvalds static void duplicateIXtree(struct super_block *sb, s64 blkno,
29831da177e4SLinus Torvalds int xlen, s64 *xaddr)
29841da177e4SLinus Torvalds {
29851da177e4SLinus Torvalds struct jfs_superblock *j_sb;
29861da177e4SLinus Torvalds struct buffer_head *bh;
29871da177e4SLinus Torvalds struct inode *ip;
29881da177e4SLinus Torvalds tid_t tid;
29891da177e4SLinus Torvalds
29901da177e4SLinus Torvalds /* if AIT2 ipmap2 is bad, do not try to update it */
29911da177e4SLinus Torvalds if (JFS_SBI(sb)->mntflag & JFS_BAD_SAIT) /* s_flag */
29921da177e4SLinus Torvalds return;
29931da177e4SLinus Torvalds ip = diReadSpecial(sb, FILESYSTEM_I, 1);
29941da177e4SLinus Torvalds if (ip == NULL) {
29951da177e4SLinus Torvalds JFS_SBI(sb)->mntflag |= JFS_BAD_SAIT;
29961da177e4SLinus Torvalds if (readSuper(sb, &bh))
29971da177e4SLinus Torvalds return;
29981da177e4SLinus Torvalds j_sb = (struct jfs_superblock *)bh->b_data;
29991da177e4SLinus Torvalds j_sb->s_flag |= cpu_to_le32(JFS_BAD_SAIT);
30001da177e4SLinus Torvalds
30011da177e4SLinus Torvalds mark_buffer_dirty(bh);
30021da177e4SLinus Torvalds sync_dirty_buffer(bh);
30031da177e4SLinus Torvalds brelse(bh);
30041da177e4SLinus Torvalds return;
30051da177e4SLinus Torvalds }
30061da177e4SLinus Torvalds
30071da177e4SLinus Torvalds /* start transaction */
30081da177e4SLinus Torvalds tid = txBegin(sb, COMMIT_FORCE);
30091da177e4SLinus Torvalds /* update the inode map addressing structure to point to it */
30101da177e4SLinus Torvalds if (xtInsert(tid, ip, 0, blkno, xlen, xaddr, 0)) {
30111da177e4SLinus Torvalds JFS_SBI(sb)->mntflag |= JFS_BAD_SAIT;
30121da177e4SLinus Torvalds txAbort(tid, 1);
30131da177e4SLinus Torvalds goto cleanup;
30141da177e4SLinus Torvalds
30151da177e4SLinus Torvalds }
30161da177e4SLinus Torvalds /* update the inode map's inode to reflect the extension */
30171da177e4SLinus Torvalds ip->i_size += PSIZE;
30181da177e4SLinus Torvalds inode_add_bytes(ip, PSIZE);
30191da177e4SLinus Torvalds txCommit(tid, 1, &ip, COMMIT_FORCE);
30201da177e4SLinus Torvalds cleanup:
30211da177e4SLinus Torvalds txEnd(tid);
30221da177e4SLinus Torvalds diFreeSpecial(ip);
30231da177e4SLinus Torvalds }
30241da177e4SLinus Torvalds
30251da177e4SLinus Torvalds /*
30261da177e4SLinus Torvalds * NAME: copy_from_dinode()
30271da177e4SLinus Torvalds *
30281da177e4SLinus Torvalds * FUNCTION: Copies inode info from disk inode to in-memory inode
30291da177e4SLinus Torvalds *
30301da177e4SLinus Torvalds * RETURN VALUES:
30311da177e4SLinus Torvalds * 0 - success
30321da177e4SLinus Torvalds * -ENOMEM - insufficient memory
30331da177e4SLinus Torvalds */
copy_from_dinode(struct dinode * dip,struct inode * ip)30341da177e4SLinus Torvalds static int copy_from_dinode(struct dinode * dip, struct inode *ip)
30351da177e4SLinus Torvalds {
30361da177e4SLinus Torvalds struct jfs_inode_info *jfs_ip = JFS_IP(ip);
303769eb66d7SDave Kleikamp struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
30381da177e4SLinus Torvalds
30391da177e4SLinus Torvalds jfs_ip->fileset = le32_to_cpu(dip->di_fileset);
30401da177e4SLinus Torvalds jfs_ip->mode2 = le32_to_cpu(dip->di_mode);
30413e2221c7SDave Kleikamp jfs_set_inode_flags(ip);
30421da177e4SLinus Torvalds
30431da177e4SLinus Torvalds ip->i_mode = le32_to_cpu(dip->di_mode) & 0xffff;
304469eb66d7SDave Kleikamp if (sbi->umask != -1) {
304569eb66d7SDave Kleikamp ip->i_mode = (ip->i_mode & ~0777) | (0777 & ~sbi->umask);
304669eb66d7SDave Kleikamp /* For directories, add x permission if r is allowed by umask */
304769eb66d7SDave Kleikamp if (S_ISDIR(ip->i_mode)) {
304869eb66d7SDave Kleikamp if (ip->i_mode & 0400)
304969eb66d7SDave Kleikamp ip->i_mode |= 0100;
305069eb66d7SDave Kleikamp if (ip->i_mode & 0040)
305169eb66d7SDave Kleikamp ip->i_mode |= 0010;
305269eb66d7SDave Kleikamp if (ip->i_mode & 0004)
305369eb66d7SDave Kleikamp ip->i_mode |= 0001;
305469eb66d7SDave Kleikamp }
305569eb66d7SDave Kleikamp }
3056bfe86848SMiklos Szeredi set_nlink(ip, le32_to_cpu(dip->di_nlink));
305769eb66d7SDave Kleikamp
3058c18cdc1aSEric W. Biederman jfs_ip->saved_uid = make_kuid(&init_user_ns, le32_to_cpu(dip->di_uid));
3059c18cdc1aSEric W. Biederman if (!uid_valid(sbi->uid))
306069eb66d7SDave Kleikamp ip->i_uid = jfs_ip->saved_uid;
306169eb66d7SDave Kleikamp else {
306269eb66d7SDave Kleikamp ip->i_uid = sbi->uid;
306369eb66d7SDave Kleikamp }
306469eb66d7SDave Kleikamp
3065c18cdc1aSEric W. Biederman jfs_ip->saved_gid = make_kgid(&init_user_ns, le32_to_cpu(dip->di_gid));
3066c18cdc1aSEric W. Biederman if (!gid_valid(sbi->gid))
306769eb66d7SDave Kleikamp ip->i_gid = jfs_ip->saved_gid;
306869eb66d7SDave Kleikamp else {
306969eb66d7SDave Kleikamp ip->i_gid = sbi->gid;
307069eb66d7SDave Kleikamp }
307169eb66d7SDave Kleikamp
30721da177e4SLinus Torvalds ip->i_size = le64_to_cpu(dip->di_size);
30731da177e4SLinus Torvalds ip->i_atime.tv_sec = le32_to_cpu(dip->di_atime.tv_sec);
30741da177e4SLinus Torvalds ip->i_atime.tv_nsec = le32_to_cpu(dip->di_atime.tv_nsec);
30751da177e4SLinus Torvalds ip->i_mtime.tv_sec = le32_to_cpu(dip->di_mtime.tv_sec);
30761da177e4SLinus Torvalds ip->i_mtime.tv_nsec = le32_to_cpu(dip->di_mtime.tv_nsec);
3077ad9dc5dfSJeff Layton inode_set_ctime(ip, le32_to_cpu(dip->di_ctime.tv_sec),
3078ad9dc5dfSJeff Layton le32_to_cpu(dip->di_ctime.tv_nsec));
30791da177e4SLinus Torvalds ip->i_blocks = LBLK2PBLK(ip->i_sb, le64_to_cpu(dip->di_nblocks));
30801da177e4SLinus Torvalds ip->i_generation = le32_to_cpu(dip->di_gen);
30811da177e4SLinus Torvalds
30821da177e4SLinus Torvalds jfs_ip->ixpxd = dip->di_ixpxd; /* in-memory pxd's are little-endian */
30831da177e4SLinus Torvalds jfs_ip->acl = dip->di_acl; /* as are dxd's */
30841da177e4SLinus Torvalds jfs_ip->ea = dip->di_ea;
30851da177e4SLinus Torvalds jfs_ip->next_index = le32_to_cpu(dip->di_next_index);
30861da177e4SLinus Torvalds jfs_ip->otime = le32_to_cpu(dip->di_otime.tv_sec);
30871da177e4SLinus Torvalds jfs_ip->acltype = le32_to_cpu(dip->di_acltype);
30881da177e4SLinus Torvalds
30891da177e4SLinus Torvalds if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode)) {
30901da177e4SLinus Torvalds jfs_ip->dev = le32_to_cpu(dip->di_rdev);
30911da177e4SLinus Torvalds ip->i_rdev = new_decode_dev(jfs_ip->dev);
30921da177e4SLinus Torvalds }
30931da177e4SLinus Torvalds
30941da177e4SLinus Torvalds if (S_ISDIR(ip->i_mode)) {
30955d299f44SKees Cook memcpy(&jfs_ip->u.dir, &dip->u._dir, 384);
30961da177e4SLinus Torvalds } else if (S_ISREG(ip->i_mode) || S_ISLNK(ip->i_mode)) {
30971da177e4SLinus Torvalds memcpy(&jfs_ip->i_xtroot, &dip->di_xtroot, 288);
30981da177e4SLinus Torvalds } else
30991da177e4SLinus Torvalds memcpy(&jfs_ip->i_inline_ea, &dip->di_inlineea, 128);
31001da177e4SLinus Torvalds
31011da177e4SLinus Torvalds /* Zero the in-memory-only stuff */
31021da177e4SLinus Torvalds jfs_ip->cflag = 0;
31031da177e4SLinus Torvalds jfs_ip->btindex = 0;
31041da177e4SLinus Torvalds jfs_ip->btorder = 0;
31051da177e4SLinus Torvalds jfs_ip->bxflag = 0;
31061da177e4SLinus Torvalds jfs_ip->blid = 0;
31071da177e4SLinus Torvalds jfs_ip->atlhead = 0;
31081da177e4SLinus Torvalds jfs_ip->atltail = 0;
31091da177e4SLinus Torvalds jfs_ip->xtlid = 0;
31101da177e4SLinus Torvalds return (0);
31111da177e4SLinus Torvalds }
31121da177e4SLinus Torvalds
31131da177e4SLinus Torvalds /*
31141da177e4SLinus Torvalds * NAME: copy_to_dinode()
31151da177e4SLinus Torvalds *
31161da177e4SLinus Torvalds * FUNCTION: Copies inode info from in-memory inode to disk inode
31171da177e4SLinus Torvalds */
copy_to_dinode(struct dinode * dip,struct inode * ip)31181da177e4SLinus Torvalds static void copy_to_dinode(struct dinode * dip, struct inode *ip)
31191da177e4SLinus Torvalds {
31201da177e4SLinus Torvalds struct jfs_inode_info *jfs_ip = JFS_IP(ip);
312169eb66d7SDave Kleikamp struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
31221da177e4SLinus Torvalds
31231da177e4SLinus Torvalds dip->di_fileset = cpu_to_le32(jfs_ip->fileset);
312469eb66d7SDave Kleikamp dip->di_inostamp = cpu_to_le32(sbi->inostamp);
31251da177e4SLinus Torvalds dip->di_number = cpu_to_le32(ip->i_ino);
31261da177e4SLinus Torvalds dip->di_gen = cpu_to_le32(ip->i_generation);
31271da177e4SLinus Torvalds dip->di_size = cpu_to_le64(ip->i_size);
31281da177e4SLinus Torvalds dip->di_nblocks = cpu_to_le64(PBLK2LBLK(ip->i_sb, ip->i_blocks));
31291da177e4SLinus Torvalds dip->di_nlink = cpu_to_le32(ip->i_nlink);
3130c18cdc1aSEric W. Biederman if (!uid_valid(sbi->uid))
3131c18cdc1aSEric W. Biederman dip->di_uid = cpu_to_le32(i_uid_read(ip));
313269eb66d7SDave Kleikamp else
3133c18cdc1aSEric W. Biederman dip->di_uid =cpu_to_le32(from_kuid(&init_user_ns,
3134c18cdc1aSEric W. Biederman jfs_ip->saved_uid));
3135c18cdc1aSEric W. Biederman if (!gid_valid(sbi->gid))
3136c18cdc1aSEric W. Biederman dip->di_gid = cpu_to_le32(i_gid_read(ip));
313769eb66d7SDave Kleikamp else
3138c18cdc1aSEric W. Biederman dip->di_gid = cpu_to_le32(from_kgid(&init_user_ns,
3139c18cdc1aSEric W. Biederman jfs_ip->saved_gid));
31401da177e4SLinus Torvalds /*
31411da177e4SLinus Torvalds * mode2 is only needed for storing the higher order bits.
31421da177e4SLinus Torvalds * Trust i_mode for the lower order ones
31431da177e4SLinus Torvalds */
314469eb66d7SDave Kleikamp if (sbi->umask == -1)
314569eb66d7SDave Kleikamp dip->di_mode = cpu_to_le32((jfs_ip->mode2 & 0xffff0000) |
314669eb66d7SDave Kleikamp ip->i_mode);
314769eb66d7SDave Kleikamp else /* Leave the original permissions alone */
314869eb66d7SDave Kleikamp dip->di_mode = cpu_to_le32(jfs_ip->mode2);
314969eb66d7SDave Kleikamp
31501da177e4SLinus Torvalds dip->di_atime.tv_sec = cpu_to_le32(ip->i_atime.tv_sec);
31511da177e4SLinus Torvalds dip->di_atime.tv_nsec = cpu_to_le32(ip->i_atime.tv_nsec);
3152ad9dc5dfSJeff Layton dip->di_ctime.tv_sec = cpu_to_le32(inode_get_ctime(ip).tv_sec);
3153ad9dc5dfSJeff Layton dip->di_ctime.tv_nsec = cpu_to_le32(inode_get_ctime(ip).tv_nsec);
31541da177e4SLinus Torvalds dip->di_mtime.tv_sec = cpu_to_le32(ip->i_mtime.tv_sec);
31551da177e4SLinus Torvalds dip->di_mtime.tv_nsec = cpu_to_le32(ip->i_mtime.tv_nsec);
31561da177e4SLinus Torvalds dip->di_ixpxd = jfs_ip->ixpxd; /* in-memory pxd's are little-endian */
31571da177e4SLinus Torvalds dip->di_acl = jfs_ip->acl; /* as are dxd's */
31581da177e4SLinus Torvalds dip->di_ea = jfs_ip->ea;
31591da177e4SLinus Torvalds dip->di_next_index = cpu_to_le32(jfs_ip->next_index);
31601da177e4SLinus Torvalds dip->di_otime.tv_sec = cpu_to_le32(jfs_ip->otime);
31611da177e4SLinus Torvalds dip->di_otime.tv_nsec = 0;
31621da177e4SLinus Torvalds dip->di_acltype = cpu_to_le32(jfs_ip->acltype);
31631da177e4SLinus Torvalds if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode))
31641da177e4SLinus Torvalds dip->di_rdev = cpu_to_le32(jfs_ip->dev);
31651da177e4SLinus Torvalds }
3166