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