xref: /openbmc/linux/fs/jfs/jfs_imap.c (revision 6db6b729)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *   Copyright (C) International Business Machines Corp., 2000-2004
4  */
5 
6 /*
7  *	jfs_imap.c: inode allocation map manager
8  *
9  * Serialization:
10  *   Each AG has a simple lock which is used to control the serialization of
11  *	the AG level lists.  This lock should be taken first whenever an AG
12  *	level list will be modified or accessed.
13  *
14  *   Each IAG is locked by obtaining the buffer for the IAG page.
15  *
16  *   There is also a inode lock for the inode map inode.  A read lock needs to
17  *	be taken whenever an IAG is read from the map or the global level
18  *	information is read.  A write lock needs to be taken whenever the global
19  *	level information is modified or an atomic operation needs to be used.
20  *
21  *	If more than one IAG is read at one time, the read lock may not
22  *	be given up until all of the IAG's are read.  Otherwise, a deadlock
23  *	may occur when trying to obtain the read lock while another thread
24  *	holding the read lock is waiting on the IAG already being held.
25  *
26  *   The control page of the inode map is read into memory by diMount().
27  *	Thereafter it should only be modified in memory and then it will be
28  *	written out when the filesystem is unmounted by diUnmount().
29  */
30 
31 #include <linux/fs.h>
32 #include <linux/buffer_head.h>
33 #include <linux/pagemap.h>
34 #include <linux/quotaops.h>
35 #include <linux/slab.h>
36 
37 #include "jfs_incore.h"
38 #include "jfs_inode.h"
39 #include "jfs_filsys.h"
40 #include "jfs_dinode.h"
41 #include "jfs_dmap.h"
42 #include "jfs_imap.h"
43 #include "jfs_metapage.h"
44 #include "jfs_superblock.h"
45 #include "jfs_debug.h"
46 
47 /*
48  * imap locks
49  */
50 /* iag free list lock */
51 #define IAGFREE_LOCK_INIT(imap)		mutex_init(&imap->im_freelock)
52 #define IAGFREE_LOCK(imap)		mutex_lock(&imap->im_freelock)
53 #define IAGFREE_UNLOCK(imap)		mutex_unlock(&imap->im_freelock)
54 
55 /* per ag iag list locks */
56 #define AG_LOCK_INIT(imap,index)	mutex_init(&(imap->im_aglock[index]))
57 #define AG_LOCK(imap,agno)		mutex_lock(&imap->im_aglock[agno])
58 #define AG_UNLOCK(imap,agno)		mutex_unlock(&imap->im_aglock[agno])
59 
60 /*
61  * forward references
62  */
63 static int diAllocAG(struct inomap *, int, bool, struct inode *);
64 static int diAllocAny(struct inomap *, int, bool, struct inode *);
65 static int diAllocBit(struct inomap *, struct iag *, int);
66 static int diAllocExt(struct inomap *, int, struct inode *);
67 static int diAllocIno(struct inomap *, int, struct inode *);
68 static int diFindFree(u32, int);
69 static int diNewExt(struct inomap *, struct iag *, int);
70 static int diNewIAG(struct inomap *, int *, int, struct metapage **);
71 static void duplicateIXtree(struct super_block *, s64, int, s64 *);
72 
73 static int diIAGRead(struct inomap * imap, int, struct metapage **);
74 static int copy_from_dinode(struct dinode *, struct inode *);
75 static void copy_to_dinode(struct dinode *, struct inode *);
76 
77 /*
78  * NAME:	diMount()
79  *
80  * FUNCTION:	initialize the incore inode map control structures for
81  *		a fileset or aggregate init time.
82  *
83  *		the inode map's control structure (dinomap) is
84  *		brought in from disk and placed in virtual memory.
85  *
86  * PARAMETERS:
87  *	ipimap	- pointer to inode map inode for the aggregate or fileset.
88  *
89  * RETURN VALUES:
90  *	0	- success
91  *	-ENOMEM	- insufficient free virtual memory.
92  *	-EIO	- i/o error.
93  */
94 int diMount(struct inode *ipimap)
95 {
96 	struct inomap *imap;
97 	struct metapage *mp;
98 	int index;
99 	struct dinomap_disk *dinom_le;
100 
101 	/*
102 	 * allocate/initialize the in-memory inode map control structure
103 	 */
104 	/* allocate the in-memory inode map control structure. */
105 	imap = kmalloc(sizeof(struct inomap), GFP_KERNEL);
106 	if (imap == NULL)
107 		return -ENOMEM;
108 
109 	/* read the on-disk inode map control structure. */
110 
111 	mp = read_metapage(ipimap,
112 			   IMAPBLKNO << JFS_SBI(ipimap->i_sb)->l2nbperpage,
113 			   PSIZE, 0);
114 	if (mp == NULL) {
115 		kfree(imap);
116 		return -EIO;
117 	}
118 
119 	/* copy the on-disk version to the in-memory version. */
120 	dinom_le = (struct dinomap_disk *) mp->data;
121 	imap->im_freeiag = le32_to_cpu(dinom_le->in_freeiag);
122 	imap->im_nextiag = le32_to_cpu(dinom_le->in_nextiag);
123 	atomic_set(&imap->im_numinos, le32_to_cpu(dinom_le->in_numinos));
124 	atomic_set(&imap->im_numfree, le32_to_cpu(dinom_le->in_numfree));
125 	imap->im_nbperiext = le32_to_cpu(dinom_le->in_nbperiext);
126 	imap->im_l2nbperiext = le32_to_cpu(dinom_le->in_l2nbperiext);
127 	for (index = 0; index < MAXAG; index++) {
128 		imap->im_agctl[index].inofree =
129 		    le32_to_cpu(dinom_le->in_agctl[index].inofree);
130 		imap->im_agctl[index].extfree =
131 		    le32_to_cpu(dinom_le->in_agctl[index].extfree);
132 		imap->im_agctl[index].numinos =
133 		    le32_to_cpu(dinom_le->in_agctl[index].numinos);
134 		imap->im_agctl[index].numfree =
135 		    le32_to_cpu(dinom_le->in_agctl[index].numfree);
136 	}
137 
138 	/* release the buffer. */
139 	release_metapage(mp);
140 
141 	/*
142 	 * allocate/initialize inode allocation map locks
143 	 */
144 	/* allocate and init iag free list lock */
145 	IAGFREE_LOCK_INIT(imap);
146 
147 	/* allocate and init ag list locks */
148 	for (index = 0; index < MAXAG; index++) {
149 		AG_LOCK_INIT(imap, index);
150 	}
151 
152 	/* bind the inode map inode and inode map control structure
153 	 * to each other.
154 	 */
155 	imap->im_ipimap = ipimap;
156 	JFS_IP(ipimap)->i_imap = imap;
157 
158 	return (0);
159 }
160 
161 
162 /*
163  * NAME:	diUnmount()
164  *
165  * FUNCTION:	write to disk the incore inode map control structures for
166  *		a fileset or aggregate at unmount time.
167  *
168  * PARAMETERS:
169  *	ipimap	- pointer to inode map inode for the aggregate or fileset.
170  *
171  * RETURN VALUES:
172  *	0	- success
173  *	-ENOMEM	- insufficient free virtual memory.
174  *	-EIO	- i/o error.
175  */
176 int diUnmount(struct inode *ipimap, int mounterror)
177 {
178 	struct inomap *imap = JFS_IP(ipimap)->i_imap;
179 
180 	/*
181 	 * update the on-disk inode map control structure
182 	 */
183 
184 	if (!(mounterror || isReadOnly(ipimap)))
185 		diSync(ipimap);
186 
187 	/*
188 	 * Invalidate the page cache buffers
189 	 */
190 	truncate_inode_pages(ipimap->i_mapping, 0);
191 
192 	/*
193 	 * free in-memory control structure
194 	 */
195 	kfree(imap);
196 	JFS_IP(ipimap)->i_imap = NULL;
197 
198 	return (0);
199 }
200 
201 
202 /*
203  *	diSync()
204  */
205 int diSync(struct inode *ipimap)
206 {
207 	struct dinomap_disk *dinom_le;
208 	struct inomap *imp = JFS_IP(ipimap)->i_imap;
209 	struct metapage *mp;
210 	int index;
211 
212 	/*
213 	 * write imap global conrol page
214 	 */
215 	/* read the on-disk inode map control structure */
216 	mp = get_metapage(ipimap,
217 			  IMAPBLKNO << JFS_SBI(ipimap->i_sb)->l2nbperpage,
218 			  PSIZE, 0);
219 	if (mp == NULL) {
220 		jfs_err("diSync: get_metapage failed!");
221 		return -EIO;
222 	}
223 
224 	/* copy the in-memory version to the on-disk version */
225 	dinom_le = (struct dinomap_disk *) mp->data;
226 	dinom_le->in_freeiag = cpu_to_le32(imp->im_freeiag);
227 	dinom_le->in_nextiag = cpu_to_le32(imp->im_nextiag);
228 	dinom_le->in_numinos = cpu_to_le32(atomic_read(&imp->im_numinos));
229 	dinom_le->in_numfree = cpu_to_le32(atomic_read(&imp->im_numfree));
230 	dinom_le->in_nbperiext = cpu_to_le32(imp->im_nbperiext);
231 	dinom_le->in_l2nbperiext = cpu_to_le32(imp->im_l2nbperiext);
232 	for (index = 0; index < MAXAG; index++) {
233 		dinom_le->in_agctl[index].inofree =
234 		    cpu_to_le32(imp->im_agctl[index].inofree);
235 		dinom_le->in_agctl[index].extfree =
236 		    cpu_to_le32(imp->im_agctl[index].extfree);
237 		dinom_le->in_agctl[index].numinos =
238 		    cpu_to_le32(imp->im_agctl[index].numinos);
239 		dinom_le->in_agctl[index].numfree =
240 		    cpu_to_le32(imp->im_agctl[index].numfree);
241 	}
242 
243 	/* write out the control structure */
244 	write_metapage(mp);
245 
246 	/*
247 	 * write out dirty pages of imap
248 	 */
249 	filemap_write_and_wait(ipimap->i_mapping);
250 
251 	diWriteSpecial(ipimap, 0);
252 
253 	return (0);
254 }
255 
256 
257 /*
258  * NAME:	diRead()
259  *
260  * FUNCTION:	initialize an incore inode from disk.
261  *
262  *		on entry, the specifed incore inode should itself
263  *		specify the disk inode number corresponding to the
264  *		incore inode (i.e. i_number should be initialized).
265  *
266  *		this routine handles incore inode initialization for
267  *		both "special" and "regular" inodes.  special inodes
268  *		are those required early in the mount process and
269  *		require special handling since much of the file system
270  *		is not yet initialized.  these "special" inodes are
271  *		identified by a NULL inode map inode pointer and are
272  *		actually initialized by a call to diReadSpecial().
273  *
274  *		for regular inodes, the iag describing the disk inode
275  *		is read from disk to determine the inode extent address
276  *		for the disk inode.  with the inode extent address in
277  *		hand, the page of the extent that contains the disk
278  *		inode is read and the disk inode is copied to the
279  *		incore inode.
280  *
281  * PARAMETERS:
282  *	ip	-  pointer to incore inode to be initialized from disk.
283  *
284  * RETURN VALUES:
285  *	0	- success
286  *	-EIO	- i/o error.
287  *	-ENOMEM	- insufficient memory
288  *
289  */
290 int diRead(struct inode *ip)
291 {
292 	struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
293 	int iagno, ino, extno, rc;
294 	struct inode *ipimap;
295 	struct dinode *dp;
296 	struct iag *iagp;
297 	struct metapage *mp;
298 	s64 blkno, agstart;
299 	struct inomap *imap;
300 	int block_offset;
301 	int inodes_left;
302 	unsigned long pageno;
303 	int rel_inode;
304 
305 	jfs_info("diRead: ino = %ld", ip->i_ino);
306 
307 	ipimap = sbi->ipimap;
308 	JFS_IP(ip)->ipimap = ipimap;
309 
310 	/* determine the iag number for this inode (number) */
311 	iagno = INOTOIAG(ip->i_ino);
312 
313 	/* read the iag */
314 	IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
315 	imap = JFS_IP(ipimap)->i_imap;
316 	rc = diIAGRead(imap, iagno, &mp);
317 	IREAD_UNLOCK(ipimap);
318 	if (rc) {
319 		jfs_err("diRead: diIAGRead returned %d", rc);
320 		return (rc);
321 	}
322 
323 	iagp = (struct iag *) mp->data;
324 
325 	/* determine inode extent that holds the disk inode */
326 	ino = ip->i_ino & (INOSPERIAG - 1);
327 	extno = ino >> L2INOSPEREXT;
328 
329 	if ((lengthPXD(&iagp->inoext[extno]) != imap->im_nbperiext) ||
330 	    (addressPXD(&iagp->inoext[extno]) == 0)) {
331 		release_metapage(mp);
332 		return -ESTALE;
333 	}
334 
335 	/* get disk block number of the page within the inode extent
336 	 * that holds the disk inode.
337 	 */
338 	blkno = INOPBLK(&iagp->inoext[extno], ino, sbi->l2nbperpage);
339 
340 	/* get the ag for the iag */
341 	agstart = le64_to_cpu(iagp->agstart);
342 
343 	release_metapage(mp);
344 
345 	rel_inode = (ino & (INOSPERPAGE - 1));
346 	pageno = blkno >> sbi->l2nbperpage;
347 
348 	if ((block_offset = ((u32) blkno & (sbi->nbperpage - 1)))) {
349 		/*
350 		 * OS/2 didn't always align inode extents on page boundaries
351 		 */
352 		inodes_left =
353 		     (sbi->nbperpage - block_offset) << sbi->l2niperblk;
354 
355 		if (rel_inode < inodes_left)
356 			rel_inode += block_offset << sbi->l2niperblk;
357 		else {
358 			pageno += 1;
359 			rel_inode -= inodes_left;
360 		}
361 	}
362 
363 	/* read the page of disk inode */
364 	mp = read_metapage(ipimap, pageno << sbi->l2nbperpage, PSIZE, 1);
365 	if (!mp) {
366 		jfs_err("diRead: read_metapage failed");
367 		return -EIO;
368 	}
369 
370 	/* locate the disk inode requested */
371 	dp = (struct dinode *) mp->data;
372 	dp += rel_inode;
373 
374 	if (ip->i_ino != le32_to_cpu(dp->di_number)) {
375 		jfs_error(ip->i_sb, "i_ino != di_number\n");
376 		rc = -EIO;
377 	} else if (le32_to_cpu(dp->di_nlink) == 0)
378 		rc = -ESTALE;
379 	else
380 		/* copy the disk inode to the in-memory inode */
381 		rc = copy_from_dinode(dp, ip);
382 
383 	release_metapage(mp);
384 
385 	/* set the ag for the inode */
386 	JFS_IP(ip)->agstart = agstart;
387 	JFS_IP(ip)->active_ag = -1;
388 
389 	return (rc);
390 }
391 
392 
393 /*
394  * NAME:	diReadSpecial()
395  *
396  * FUNCTION:	initialize a 'special' inode from disk.
397  *
398  *		this routines handles aggregate level inodes.  The
399  *		inode cache cannot differentiate between the
400  *		aggregate inodes and the filesystem inodes, so we
401  *		handle these here.  We don't actually use the aggregate
402  *		inode map, since these inodes are at a fixed location
403  *		and in some cases the aggregate inode map isn't initialized
404  *		yet.
405  *
406  * PARAMETERS:
407  *	sb - filesystem superblock
408  *	inum - aggregate inode number
409  *	secondary - 1 if secondary aggregate inode table
410  *
411  * RETURN VALUES:
412  *	new inode	- success
413  *	NULL		- i/o error.
414  */
415 struct inode *diReadSpecial(struct super_block *sb, ino_t inum, int secondary)
416 {
417 	struct jfs_sb_info *sbi = JFS_SBI(sb);
418 	uint address;
419 	struct dinode *dp;
420 	struct inode *ip;
421 	struct metapage *mp;
422 
423 	ip = new_inode(sb);
424 	if (ip == NULL) {
425 		jfs_err("diReadSpecial: new_inode returned NULL!");
426 		return ip;
427 	}
428 
429 	if (secondary) {
430 		address = addressPXD(&sbi->ait2) >> sbi->l2nbperpage;
431 		JFS_IP(ip)->ipimap = sbi->ipaimap2;
432 	} else {
433 		address = AITBL_OFF >> L2PSIZE;
434 		JFS_IP(ip)->ipimap = sbi->ipaimap;
435 	}
436 
437 	ASSERT(inum < INOSPEREXT);
438 
439 	ip->i_ino = inum;
440 
441 	address += inum >> 3;	/* 8 inodes per 4K page */
442 
443 	/* read the page of fixed disk inode (AIT) in raw mode */
444 	mp = read_metapage(ip, address << sbi->l2nbperpage, PSIZE, 1);
445 	if (mp == NULL) {
446 		set_nlink(ip, 1);	/* Don't want iput() deleting it */
447 		iput(ip);
448 		return (NULL);
449 	}
450 
451 	/* get the pointer to the disk inode of interest */
452 	dp = (struct dinode *) (mp->data);
453 	dp += inum % 8;		/* 8 inodes per 4K page */
454 
455 	/* copy on-disk inode to in-memory inode */
456 	if ((copy_from_dinode(dp, ip)) != 0) {
457 		/* handle bad return by returning NULL for ip */
458 		set_nlink(ip, 1);	/* Don't want iput() deleting it */
459 		iput(ip);
460 		/* release the page */
461 		release_metapage(mp);
462 		return (NULL);
463 
464 	}
465 
466 	ip->i_mapping->a_ops = &jfs_metapage_aops;
467 	mapping_set_gfp_mask(ip->i_mapping, GFP_NOFS);
468 
469 	/* Allocations to metadata inodes should not affect quotas */
470 	ip->i_flags |= S_NOQUOTA;
471 
472 	if ((inum == FILESYSTEM_I) && (JFS_IP(ip)->ipimap == sbi->ipaimap)) {
473 		sbi->gengen = le32_to_cpu(dp->di_gengen);
474 		sbi->inostamp = le32_to_cpu(dp->di_inostamp);
475 	}
476 
477 	/* release the page */
478 	release_metapage(mp);
479 
480 	inode_fake_hash(ip);
481 
482 	return (ip);
483 }
484 
485 /*
486  * NAME:	diWriteSpecial()
487  *
488  * FUNCTION:	Write the special inode to disk
489  *
490  * PARAMETERS:
491  *	ip - special inode
492  *	secondary - 1 if secondary aggregate inode table
493  *
494  * RETURN VALUES: none
495  */
496 
497 void diWriteSpecial(struct inode *ip, int secondary)
498 {
499 	struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
500 	uint address;
501 	struct dinode *dp;
502 	ino_t inum = ip->i_ino;
503 	struct metapage *mp;
504 
505 	if (secondary)
506 		address = addressPXD(&sbi->ait2) >> sbi->l2nbperpage;
507 	else
508 		address = AITBL_OFF >> L2PSIZE;
509 
510 	ASSERT(inum < INOSPEREXT);
511 
512 	address += inum >> 3;	/* 8 inodes per 4K page */
513 
514 	/* read the page of fixed disk inode (AIT) in raw mode */
515 	mp = read_metapage(ip, address << sbi->l2nbperpage, PSIZE, 1);
516 	if (mp == NULL) {
517 		jfs_err("diWriteSpecial: failed to read aggregate inode extent!");
518 		return;
519 	}
520 
521 	/* get the pointer to the disk inode of interest */
522 	dp = (struct dinode *) (mp->data);
523 	dp += inum % 8;		/* 8 inodes per 4K page */
524 
525 	/* copy on-disk inode to in-memory inode */
526 	copy_to_dinode(dp, ip);
527 	memcpy(&dp->di_xtroot, &JFS_IP(ip)->i_xtroot, 288);
528 
529 	if (inum == FILESYSTEM_I)
530 		dp->di_gengen = cpu_to_le32(sbi->gengen);
531 
532 	/* write the page */
533 	write_metapage(mp);
534 }
535 
536 /*
537  * NAME:	diFreeSpecial()
538  *
539  * FUNCTION:	Free allocated space for special inode
540  */
541 void diFreeSpecial(struct inode *ip)
542 {
543 	if (ip == NULL) {
544 		jfs_err("diFreeSpecial called with NULL ip!");
545 		return;
546 	}
547 	filemap_write_and_wait(ip->i_mapping);
548 	truncate_inode_pages(ip->i_mapping, 0);
549 	iput(ip);
550 }
551 
552 
553 
554 /*
555  * NAME:	diWrite()
556  *
557  * FUNCTION:	write the on-disk inode portion of the in-memory inode
558  *		to its corresponding on-disk inode.
559  *
560  *		on entry, the specifed incore inode should itself
561  *		specify the disk inode number corresponding to the
562  *		incore inode (i.e. i_number should be initialized).
563  *
564  *		the inode contains the inode extent address for the disk
565  *		inode.  with the inode extent address in hand, the
566  *		page of the extent that contains the disk inode is
567  *		read and the disk inode portion of the incore inode
568  *		is copied to the disk inode.
569  *
570  * PARAMETERS:
571  *	tid -  transacation id
572  *	ip  -  pointer to incore inode to be written to the inode extent.
573  *
574  * RETURN VALUES:
575  *	0	- success
576  *	-EIO	- i/o error.
577  */
578 int diWrite(tid_t tid, struct inode *ip)
579 {
580 	struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
581 	struct jfs_inode_info *jfs_ip = JFS_IP(ip);
582 	int rc = 0;
583 	s32 ino;
584 	struct dinode *dp;
585 	s64 blkno;
586 	int block_offset;
587 	int inodes_left;
588 	struct metapage *mp;
589 	unsigned long pageno;
590 	int rel_inode;
591 	int dioffset;
592 	struct inode *ipimap;
593 	uint type;
594 	lid_t lid;
595 	struct tlock *ditlck, *tlck;
596 	struct linelock *dilinelock, *ilinelock;
597 	struct lv *lv;
598 	int n;
599 
600 	ipimap = jfs_ip->ipimap;
601 
602 	ino = ip->i_ino & (INOSPERIAG - 1);
603 
604 	if (!addressPXD(&(jfs_ip->ixpxd)) ||
605 	    (lengthPXD(&(jfs_ip->ixpxd)) !=
606 	     JFS_IP(ipimap)->i_imap->im_nbperiext)) {
607 		jfs_error(ip->i_sb, "ixpxd invalid\n");
608 		return -EIO;
609 	}
610 
611 	/*
612 	 * read the page of disk inode containing the specified inode:
613 	 */
614 	/* compute the block address of the page */
615 	blkno = INOPBLK(&(jfs_ip->ixpxd), ino, sbi->l2nbperpage);
616 
617 	rel_inode = (ino & (INOSPERPAGE - 1));
618 	pageno = blkno >> sbi->l2nbperpage;
619 
620 	if ((block_offset = ((u32) blkno & (sbi->nbperpage - 1)))) {
621 		/*
622 		 * OS/2 didn't always align inode extents on page boundaries
623 		 */
624 		inodes_left =
625 		    (sbi->nbperpage - block_offset) << sbi->l2niperblk;
626 
627 		if (rel_inode < inodes_left)
628 			rel_inode += block_offset << sbi->l2niperblk;
629 		else {
630 			pageno += 1;
631 			rel_inode -= inodes_left;
632 		}
633 	}
634 	/* read the page of disk inode */
635       retry:
636 	mp = read_metapage(ipimap, pageno << sbi->l2nbperpage, PSIZE, 1);
637 	if (!mp)
638 		return -EIO;
639 
640 	/* get the pointer to the disk inode */
641 	dp = (struct dinode *) mp->data;
642 	dp += rel_inode;
643 
644 	dioffset = (ino & (INOSPERPAGE - 1)) << L2DISIZE;
645 
646 	/*
647 	 * acquire transaction lock on the on-disk inode;
648 	 * N.B. tlock is acquired on ipimap not ip;
649 	 */
650 	if ((ditlck =
651 	     txLock(tid, ipimap, mp, tlckINODE | tlckENTRY)) == NULL)
652 		goto retry;
653 	dilinelock = (struct linelock *) & ditlck->lock;
654 
655 	/*
656 	 * copy btree root from in-memory inode to on-disk inode
657 	 *
658 	 * (tlock is taken from inline B+-tree root in in-memory
659 	 * inode when the B+-tree root is updated, which is pointed
660 	 * by jfs_ip->blid as well as being on tx tlock list)
661 	 *
662 	 * further processing of btree root is based on the copy
663 	 * in in-memory inode, where txLog() will log from, and,
664 	 * for xtree root, txUpdateMap() will update map and reset
665 	 * XAD_NEW bit;
666 	 */
667 
668 	if (S_ISDIR(ip->i_mode) && (lid = jfs_ip->xtlid)) {
669 		/*
670 		 * This is the special xtree inside the directory for storing
671 		 * the directory table
672 		 */
673 		xtpage_t *p, *xp;
674 		xad_t *xad;
675 
676 		jfs_ip->xtlid = 0;
677 		tlck = lid_to_tlock(lid);
678 		assert(tlck->type & tlckXTREE);
679 		tlck->type |= tlckBTROOT;
680 		tlck->mp = mp;
681 		ilinelock = (struct linelock *) & tlck->lock;
682 
683 		/*
684 		 * copy xtree root from inode to dinode:
685 		 */
686 		p = &jfs_ip->i_xtroot;
687 		xp = (xtpage_t *) &dp->di_dirtable;
688 		lv = ilinelock->lv;
689 		for (n = 0; n < ilinelock->index; n++, lv++) {
690 			memcpy(&xp->xad[lv->offset], &p->xad[lv->offset],
691 			       lv->length << L2XTSLOTSIZE);
692 		}
693 
694 		/* reset on-disk (metadata page) xtree XAD_NEW bit */
695 		xad = &xp->xad[XTENTRYSTART];
696 		for (n = XTENTRYSTART;
697 		     n < le16_to_cpu(xp->header.nextindex); n++, xad++)
698 			if (xad->flag & (XAD_NEW | XAD_EXTENDED))
699 				xad->flag &= ~(XAD_NEW | XAD_EXTENDED);
700 	}
701 
702 	if ((lid = jfs_ip->blid) == 0)
703 		goto inlineData;
704 	jfs_ip->blid = 0;
705 
706 	tlck = lid_to_tlock(lid);
707 	type = tlck->type;
708 	tlck->type |= tlckBTROOT;
709 	tlck->mp = mp;
710 	ilinelock = (struct linelock *) & tlck->lock;
711 
712 	/*
713 	 *	regular file: 16 byte (XAD slot) granularity
714 	 */
715 	if (type & tlckXTREE) {
716 		xtpage_t *p, *xp;
717 		xad_t *xad;
718 
719 		/*
720 		 * copy xtree root from inode to dinode:
721 		 */
722 		p = &jfs_ip->i_xtroot;
723 		xp = &dp->di_xtroot;
724 		lv = ilinelock->lv;
725 		for (n = 0; n < ilinelock->index; n++, lv++) {
726 			memcpy(&xp->xad[lv->offset], &p->xad[lv->offset],
727 			       lv->length << L2XTSLOTSIZE);
728 		}
729 
730 		/* reset on-disk (metadata page) xtree XAD_NEW bit */
731 		xad = &xp->xad[XTENTRYSTART];
732 		for (n = XTENTRYSTART;
733 		     n < le16_to_cpu(xp->header.nextindex); n++, xad++)
734 			if (xad->flag & (XAD_NEW | XAD_EXTENDED))
735 				xad->flag &= ~(XAD_NEW | XAD_EXTENDED);
736 	}
737 	/*
738 	 *	directory: 32 byte (directory entry slot) granularity
739 	 */
740 	else if (type & tlckDTREE) {
741 		dtpage_t *p, *xp;
742 
743 		/*
744 		 * copy dtree root from inode to dinode:
745 		 */
746 		p = (dtpage_t *) &jfs_ip->i_dtroot;
747 		xp = (dtpage_t *) & dp->di_dtroot;
748 		lv = ilinelock->lv;
749 		for (n = 0; n < ilinelock->index; n++, lv++) {
750 			memcpy(&xp->slot[lv->offset], &p->slot[lv->offset],
751 			       lv->length << L2DTSLOTSIZE);
752 		}
753 	} else {
754 		jfs_err("diWrite: UFO tlock");
755 	}
756 
757       inlineData:
758 	/*
759 	 * copy inline symlink from in-memory inode to on-disk inode
760 	 */
761 	if (S_ISLNK(ip->i_mode) && ip->i_size < IDATASIZE) {
762 		lv = & dilinelock->lv[dilinelock->index];
763 		lv->offset = (dioffset + 2 * 128) >> L2INODESLOTSIZE;
764 		lv->length = 2;
765 		memcpy(&dp->di_inline_all, jfs_ip->i_inline_all, IDATASIZE);
766 		dilinelock->index++;
767 	}
768 	/*
769 	 * copy inline data from in-memory inode to on-disk inode:
770 	 * 128 byte slot granularity
771 	 */
772 	if (test_cflag(COMMIT_Inlineea, ip)) {
773 		lv = & dilinelock->lv[dilinelock->index];
774 		lv->offset = (dioffset + 3 * 128) >> L2INODESLOTSIZE;
775 		lv->length = 1;
776 		memcpy(&dp->di_inlineea, jfs_ip->i_inline_ea, INODESLOTSIZE);
777 		dilinelock->index++;
778 
779 		clear_cflag(COMMIT_Inlineea, ip);
780 	}
781 
782 	/*
783 	 *	lock/copy inode base: 128 byte slot granularity
784 	 */
785 	lv = & dilinelock->lv[dilinelock->index];
786 	lv->offset = dioffset >> L2INODESLOTSIZE;
787 	copy_to_dinode(dp, ip);
788 	if (test_and_clear_cflag(COMMIT_Dirtable, ip)) {
789 		lv->length = 2;
790 		memcpy(&dp->di_dirtable, &jfs_ip->i_dirtable, 96);
791 	} else
792 		lv->length = 1;
793 	dilinelock->index++;
794 
795 	/* release the buffer holding the updated on-disk inode.
796 	 * the buffer will be later written by commit processing.
797 	 */
798 	write_metapage(mp);
799 
800 	return (rc);
801 }
802 
803 
804 /*
805  * NAME:	diFree(ip)
806  *
807  * FUNCTION:	free a specified inode from the inode working map
808  *		for a fileset or aggregate.
809  *
810  *		if the inode to be freed represents the first (only)
811  *		free inode within the iag, the iag will be placed on
812  *		the ag free inode list.
813  *
814  *		freeing the inode will cause the inode extent to be
815  *		freed if the inode is the only allocated inode within
816  *		the extent.  in this case all the disk resource backing
817  *		up the inode extent will be freed. in addition, the iag
818  *		will be placed on the ag extent free list if the extent
819  *		is the first free extent in the iag.  if freeing the
820  *		extent also means that no free inodes will exist for
821  *		the iag, the iag will also be removed from the ag free
822  *		inode list.
823  *
824  *		the iag describing the inode will be freed if the extent
825  *		is to be freed and it is the only backed extent within
826  *		the iag.  in this case, the iag will be removed from the
827  *		ag free extent list and ag free inode list and placed on
828  *		the inode map's free iag list.
829  *
830  *		a careful update approach is used to provide consistency
831  *		in the face of updates to multiple buffers.  under this
832  *		approach, all required buffers are obtained before making
833  *		any updates and are held until all updates are complete.
834  *
835  * PARAMETERS:
836  *	ip	- inode to be freed.
837  *
838  * RETURN VALUES:
839  *	0	- success
840  *	-EIO	- i/o error.
841  */
842 int diFree(struct inode *ip)
843 {
844 	int rc;
845 	ino_t inum = ip->i_ino;
846 	struct iag *iagp, *aiagp, *biagp, *ciagp, *diagp;
847 	struct metapage *mp, *amp, *bmp, *cmp, *dmp;
848 	int iagno, ino, extno, bitno, sword, agno;
849 	int back, fwd;
850 	u32 bitmap, mask;
851 	struct inode *ipimap = JFS_SBI(ip->i_sb)->ipimap;
852 	struct inomap *imap = JFS_IP(ipimap)->i_imap;
853 	pxd_t freepxd;
854 	tid_t tid;
855 	struct inode *iplist[3];
856 	struct tlock *tlck;
857 	struct pxd_lock *pxdlock;
858 
859 	/*
860 	 * This is just to suppress compiler warnings.  The same logic that
861 	 * references these variables is used to initialize them.
862 	 */
863 	aiagp = biagp = ciagp = diagp = NULL;
864 
865 	/* get the iag number containing the inode.
866 	 */
867 	iagno = INOTOIAG(inum);
868 
869 	/* make sure that the iag is contained within
870 	 * the map.
871 	 */
872 	if (iagno >= imap->im_nextiag) {
873 		print_hex_dump(KERN_ERR, "imap: ", DUMP_PREFIX_ADDRESS, 16, 4,
874 			       imap, 32, 0);
875 		jfs_error(ip->i_sb, "inum = %d, iagno = %d, nextiag = %d\n",
876 			  (uint) inum, iagno, imap->im_nextiag);
877 		return -EIO;
878 	}
879 
880 	/* get the allocation group for this ino.
881 	 */
882 	agno = BLKTOAG(JFS_IP(ip)->agstart, JFS_SBI(ip->i_sb));
883 
884 	/* Lock the AG specific inode map information
885 	 */
886 	AG_LOCK(imap, agno);
887 
888 	/* Obtain read lock in imap inode.  Don't release it until we have
889 	 * read all of the IAG's that we are going to.
890 	 */
891 	IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
892 
893 	/* read the iag.
894 	 */
895 	if ((rc = diIAGRead(imap, iagno, &mp))) {
896 		IREAD_UNLOCK(ipimap);
897 		AG_UNLOCK(imap, agno);
898 		return (rc);
899 	}
900 	iagp = (struct iag *) mp->data;
901 
902 	/* get the inode number and extent number of the inode within
903 	 * the iag and the inode number within the extent.
904 	 */
905 	ino = inum & (INOSPERIAG - 1);
906 	extno = ino >> L2INOSPEREXT;
907 	bitno = ino & (INOSPEREXT - 1);
908 	mask = HIGHORDER >> bitno;
909 
910 	if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
911 		jfs_error(ip->i_sb, "wmap shows inode already free\n");
912 	}
913 
914 	if (!addressPXD(&iagp->inoext[extno])) {
915 		release_metapage(mp);
916 		IREAD_UNLOCK(ipimap);
917 		AG_UNLOCK(imap, agno);
918 		jfs_error(ip->i_sb, "invalid inoext\n");
919 		return -EIO;
920 	}
921 
922 	/* compute the bitmap for the extent reflecting the freed inode.
923 	 */
924 	bitmap = le32_to_cpu(iagp->wmap[extno]) & ~mask;
925 
926 	if (imap->im_agctl[agno].numfree > imap->im_agctl[agno].numinos) {
927 		release_metapage(mp);
928 		IREAD_UNLOCK(ipimap);
929 		AG_UNLOCK(imap, agno);
930 		jfs_error(ip->i_sb, "numfree > numinos\n");
931 		return -EIO;
932 	}
933 	/*
934 	 *	inode extent still has some inodes or below low water mark:
935 	 *	keep the inode extent;
936 	 */
937 	if (bitmap ||
938 	    imap->im_agctl[agno].numfree < 96 ||
939 	    (imap->im_agctl[agno].numfree < 288 &&
940 	     (((imap->im_agctl[agno].numfree * 100) /
941 	       imap->im_agctl[agno].numinos) <= 25))) {
942 		/* if the iag currently has no free inodes (i.e.,
943 		 * the inode being freed is the first free inode of iag),
944 		 * insert the iag at head of the inode free list for the ag.
945 		 */
946 		if (iagp->nfreeinos == 0) {
947 			/* check if there are any iags on the ag inode
948 			 * free list.  if so, read the first one so that
949 			 * we can link the current iag onto the list at
950 			 * the head.
951 			 */
952 			if ((fwd = imap->im_agctl[agno].inofree) >= 0) {
953 				/* read the iag that currently is the head
954 				 * of the list.
955 				 */
956 				if ((rc = diIAGRead(imap, fwd, &amp))) {
957 					IREAD_UNLOCK(ipimap);
958 					AG_UNLOCK(imap, agno);
959 					release_metapage(mp);
960 					return (rc);
961 				}
962 				aiagp = (struct iag *) amp->data;
963 
964 				/* make current head point back to the iag.
965 				 */
966 				aiagp->inofreeback = cpu_to_le32(iagno);
967 
968 				write_metapage(amp);
969 			}
970 
971 			/* iag points forward to current head and iag
972 			 * becomes the new head of the list.
973 			 */
974 			iagp->inofreefwd =
975 			    cpu_to_le32(imap->im_agctl[agno].inofree);
976 			iagp->inofreeback = cpu_to_le32(-1);
977 			imap->im_agctl[agno].inofree = iagno;
978 		}
979 		IREAD_UNLOCK(ipimap);
980 
981 		/* update the free inode summary map for the extent if
982 		 * freeing the inode means the extent will now have free
983 		 * inodes (i.e., the inode being freed is the first free
984 		 * inode of extent),
985 		 */
986 		if (iagp->wmap[extno] == cpu_to_le32(ONES)) {
987 			sword = extno >> L2EXTSPERSUM;
988 			bitno = extno & (EXTSPERSUM - 1);
989 			iagp->inosmap[sword] &=
990 			    cpu_to_le32(~(HIGHORDER >> bitno));
991 		}
992 
993 		/* update the bitmap.
994 		 */
995 		iagp->wmap[extno] = cpu_to_le32(bitmap);
996 
997 		/* update the free inode counts at the iag, ag and
998 		 * map level.
999 		 */
1000 		le32_add_cpu(&iagp->nfreeinos, 1);
1001 		imap->im_agctl[agno].numfree += 1;
1002 		atomic_inc(&imap->im_numfree);
1003 
1004 		/* release the AG inode map lock
1005 		 */
1006 		AG_UNLOCK(imap, agno);
1007 
1008 		/* write the iag */
1009 		write_metapage(mp);
1010 
1011 		return (0);
1012 	}
1013 
1014 
1015 	/*
1016 	 *	inode extent has become free and above low water mark:
1017 	 *	free the inode extent;
1018 	 */
1019 
1020 	/*
1021 	 *	prepare to update iag list(s) (careful update step 1)
1022 	 */
1023 	amp = bmp = cmp = dmp = NULL;
1024 	fwd = back = -1;
1025 
1026 	/* check if the iag currently has no free extents.  if so,
1027 	 * it will be placed on the head of the ag extent free list.
1028 	 */
1029 	if (iagp->nfreeexts == 0) {
1030 		/* check if the ag extent free list has any iags.
1031 		 * if so, read the iag at the head of the list now.
1032 		 * this (head) iag will be updated later to reflect
1033 		 * the addition of the current iag at the head of
1034 		 * the list.
1035 		 */
1036 		if ((fwd = imap->im_agctl[agno].extfree) >= 0) {
1037 			if ((rc = diIAGRead(imap, fwd, &amp)))
1038 				goto error_out;
1039 			aiagp = (struct iag *) amp->data;
1040 		}
1041 	} else {
1042 		/* iag has free extents. check if the addition of a free
1043 		 * extent will cause all extents to be free within this
1044 		 * iag.  if so, the iag will be removed from the ag extent
1045 		 * free list and placed on the inode map's free iag list.
1046 		 */
1047 		if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG - 1)) {
1048 			/* in preparation for removing the iag from the
1049 			 * ag extent free list, read the iags preceding
1050 			 * and following the iag on the ag extent free
1051 			 * list.
1052 			 */
1053 			if ((fwd = le32_to_cpu(iagp->extfreefwd)) >= 0) {
1054 				if ((rc = diIAGRead(imap, fwd, &amp)))
1055 					goto error_out;
1056 				aiagp = (struct iag *) amp->data;
1057 			}
1058 
1059 			if ((back = le32_to_cpu(iagp->extfreeback)) >= 0) {
1060 				if ((rc = diIAGRead(imap, back, &bmp)))
1061 					goto error_out;
1062 				biagp = (struct iag *) bmp->data;
1063 			}
1064 		}
1065 	}
1066 
1067 	/* remove the iag from the ag inode free list if freeing
1068 	 * this extent cause the iag to have no free inodes.
1069 	 */
1070 	if (iagp->nfreeinos == cpu_to_le32(INOSPEREXT - 1)) {
1071 		int inofreeback = le32_to_cpu(iagp->inofreeback);
1072 		int inofreefwd = le32_to_cpu(iagp->inofreefwd);
1073 
1074 		/* in preparation for removing the iag from the
1075 		 * ag inode free list, read the iags preceding
1076 		 * and following the iag on the ag inode free
1077 		 * list.  before reading these iags, we must make
1078 		 * sure that we already don't have them in hand
1079 		 * from up above, since re-reading an iag (buffer)
1080 		 * we are currently holding would cause a deadlock.
1081 		 */
1082 		if (inofreefwd >= 0) {
1083 
1084 			if (inofreefwd == fwd)
1085 				ciagp = (struct iag *) amp->data;
1086 			else if (inofreefwd == back)
1087 				ciagp = (struct iag *) bmp->data;
1088 			else {
1089 				if ((rc =
1090 				     diIAGRead(imap, inofreefwd, &cmp)))
1091 					goto error_out;
1092 				ciagp = (struct iag *) cmp->data;
1093 			}
1094 			assert(ciagp != NULL);
1095 		}
1096 
1097 		if (inofreeback >= 0) {
1098 			if (inofreeback == fwd)
1099 				diagp = (struct iag *) amp->data;
1100 			else if (inofreeback == back)
1101 				diagp = (struct iag *) bmp->data;
1102 			else {
1103 				if ((rc =
1104 				     diIAGRead(imap, inofreeback, &dmp)))
1105 					goto error_out;
1106 				diagp = (struct iag *) dmp->data;
1107 			}
1108 			assert(diagp != NULL);
1109 		}
1110 	}
1111 
1112 	IREAD_UNLOCK(ipimap);
1113 
1114 	/*
1115 	 * invalidate any page of the inode extent freed from buffer cache;
1116 	 */
1117 	freepxd = iagp->inoext[extno];
1118 	invalidate_pxd_metapages(ip, freepxd);
1119 
1120 	/*
1121 	 *	update iag list(s) (careful update step 2)
1122 	 */
1123 	/* add the iag to the ag extent free list if this is the
1124 	 * first free extent for the iag.
1125 	 */
1126 	if (iagp->nfreeexts == 0) {
1127 		if (fwd >= 0)
1128 			aiagp->extfreeback = cpu_to_le32(iagno);
1129 
1130 		iagp->extfreefwd =
1131 		    cpu_to_le32(imap->im_agctl[agno].extfree);
1132 		iagp->extfreeback = cpu_to_le32(-1);
1133 		imap->im_agctl[agno].extfree = iagno;
1134 	} else {
1135 		/* remove the iag from the ag extent list if all extents
1136 		 * are now free and place it on the inode map iag free list.
1137 		 */
1138 		if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG - 1)) {
1139 			if (fwd >= 0)
1140 				aiagp->extfreeback = iagp->extfreeback;
1141 
1142 			if (back >= 0)
1143 				biagp->extfreefwd = iagp->extfreefwd;
1144 			else
1145 				imap->im_agctl[agno].extfree =
1146 				    le32_to_cpu(iagp->extfreefwd);
1147 
1148 			iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
1149 
1150 			IAGFREE_LOCK(imap);
1151 			iagp->iagfree = cpu_to_le32(imap->im_freeiag);
1152 			imap->im_freeiag = iagno;
1153 			IAGFREE_UNLOCK(imap);
1154 		}
1155 	}
1156 
1157 	/* remove the iag from the ag inode free list if freeing
1158 	 * this extent causes the iag to have no free inodes.
1159 	 */
1160 	if (iagp->nfreeinos == cpu_to_le32(INOSPEREXT - 1)) {
1161 		if ((int) le32_to_cpu(iagp->inofreefwd) >= 0)
1162 			ciagp->inofreeback = iagp->inofreeback;
1163 
1164 		if ((int) le32_to_cpu(iagp->inofreeback) >= 0)
1165 			diagp->inofreefwd = iagp->inofreefwd;
1166 		else
1167 			imap->im_agctl[agno].inofree =
1168 			    le32_to_cpu(iagp->inofreefwd);
1169 
1170 		iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
1171 	}
1172 
1173 	/* update the inode extent address and working map
1174 	 * to reflect the free extent.
1175 	 * the permanent map should have been updated already
1176 	 * for the inode being freed.
1177 	 */
1178 	if (iagp->pmap[extno] != 0) {
1179 		jfs_error(ip->i_sb, "the pmap does not show inode free\n");
1180 	}
1181 	iagp->wmap[extno] = 0;
1182 	PXDlength(&iagp->inoext[extno], 0);
1183 	PXDaddress(&iagp->inoext[extno], 0);
1184 
1185 	/* update the free extent and free inode summary maps
1186 	 * to reflect the freed extent.
1187 	 * the inode summary map is marked to indicate no inodes
1188 	 * available for the freed extent.
1189 	 */
1190 	sword = extno >> L2EXTSPERSUM;
1191 	bitno = extno & (EXTSPERSUM - 1);
1192 	mask = HIGHORDER >> bitno;
1193 	iagp->inosmap[sword] |= cpu_to_le32(mask);
1194 	iagp->extsmap[sword] &= cpu_to_le32(~mask);
1195 
1196 	/* update the number of free inodes and number of free extents
1197 	 * for the iag.
1198 	 */
1199 	le32_add_cpu(&iagp->nfreeinos, -(INOSPEREXT - 1));
1200 	le32_add_cpu(&iagp->nfreeexts, 1);
1201 
1202 	/* update the number of free inodes and backed inodes
1203 	 * at the ag and inode map level.
1204 	 */
1205 	imap->im_agctl[agno].numfree -= (INOSPEREXT - 1);
1206 	imap->im_agctl[agno].numinos -= INOSPEREXT;
1207 	atomic_sub(INOSPEREXT - 1, &imap->im_numfree);
1208 	atomic_sub(INOSPEREXT, &imap->im_numinos);
1209 
1210 	if (amp)
1211 		write_metapage(amp);
1212 	if (bmp)
1213 		write_metapage(bmp);
1214 	if (cmp)
1215 		write_metapage(cmp);
1216 	if (dmp)
1217 		write_metapage(dmp);
1218 
1219 	/*
1220 	 * start transaction to update block allocation map
1221 	 * for the inode extent freed;
1222 	 *
1223 	 * N.B. AG_LOCK is released and iag will be released below, and
1224 	 * other thread may allocate inode from/reusing the ixad freed
1225 	 * BUT with new/different backing inode extent from the extent
1226 	 * to be freed by the transaction;
1227 	 */
1228 	tid = txBegin(ipimap->i_sb, COMMIT_FORCE);
1229 	mutex_lock(&JFS_IP(ipimap)->commit_mutex);
1230 
1231 	/* acquire tlock of the iag page of the freed ixad
1232 	 * to force the page NOHOMEOK (even though no data is
1233 	 * logged from the iag page) until NOREDOPAGE|FREEXTENT log
1234 	 * for the free of the extent is committed;
1235 	 * write FREEXTENT|NOREDOPAGE log record
1236 	 * N.B. linelock is overlaid as freed extent descriptor;
1237 	 */
1238 	tlck = txLock(tid, ipimap, mp, tlckINODE | tlckFREE);
1239 	pxdlock = (struct pxd_lock *) & tlck->lock;
1240 	pxdlock->flag = mlckFREEPXD;
1241 	pxdlock->pxd = freepxd;
1242 	pxdlock->index = 1;
1243 
1244 	write_metapage(mp);
1245 
1246 	iplist[0] = ipimap;
1247 
1248 	/*
1249 	 * logredo needs the IAG number and IAG extent index in order
1250 	 * to ensure that the IMap is consistent.  The least disruptive
1251 	 * way to pass these values through  to the transaction manager
1252 	 * is in the iplist array.
1253 	 *
1254 	 * It's not pretty, but it works.
1255 	 */
1256 	iplist[1] = (struct inode *) (size_t)iagno;
1257 	iplist[2] = (struct inode *) (size_t)extno;
1258 
1259 	rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
1260 
1261 	txEnd(tid);
1262 	mutex_unlock(&JFS_IP(ipimap)->commit_mutex);
1263 
1264 	/* unlock the AG inode map information */
1265 	AG_UNLOCK(imap, agno);
1266 
1267 	return (0);
1268 
1269       error_out:
1270 	IREAD_UNLOCK(ipimap);
1271 
1272 	if (amp)
1273 		release_metapage(amp);
1274 	if (bmp)
1275 		release_metapage(bmp);
1276 	if (cmp)
1277 		release_metapage(cmp);
1278 	if (dmp)
1279 		release_metapage(dmp);
1280 
1281 	AG_UNLOCK(imap, agno);
1282 
1283 	release_metapage(mp);
1284 
1285 	return (rc);
1286 }
1287 
1288 /*
1289  * There are several places in the diAlloc* routines where we initialize
1290  * the inode.
1291  */
1292 static inline void
1293 diInitInode(struct inode *ip, int iagno, int ino, int extno, struct iag * iagp)
1294 {
1295 	struct jfs_inode_info *jfs_ip = JFS_IP(ip);
1296 
1297 	ip->i_ino = (iagno << L2INOSPERIAG) + ino;
1298 	jfs_ip->ixpxd = iagp->inoext[extno];
1299 	jfs_ip->agstart = le64_to_cpu(iagp->agstart);
1300 	jfs_ip->active_ag = -1;
1301 }
1302 
1303 
1304 /*
1305  * NAME:	diAlloc(pip,dir,ip)
1306  *
1307  * FUNCTION:	allocate a disk inode from the inode working map
1308  *		for a fileset or aggregate.
1309  *
1310  * PARAMETERS:
1311  *	pip	- pointer to incore inode for the parent inode.
1312  *	dir	- 'true' if the new disk inode is for a directory.
1313  *	ip	- pointer to a new inode
1314  *
1315  * RETURN VALUES:
1316  *	0	- success.
1317  *	-ENOSPC	- insufficient disk resources.
1318  *	-EIO	- i/o error.
1319  */
1320 int diAlloc(struct inode *pip, bool dir, struct inode *ip)
1321 {
1322 	int rc, ino, iagno, addext, extno, bitno, sword;
1323 	int nwords, rem, i, agno;
1324 	u32 mask, inosmap, extsmap;
1325 	struct inode *ipimap;
1326 	struct metapage *mp;
1327 	ino_t inum;
1328 	struct iag *iagp;
1329 	struct inomap *imap;
1330 
1331 	/* get the pointers to the inode map inode and the
1332 	 * corresponding imap control structure.
1333 	 */
1334 	ipimap = JFS_SBI(pip->i_sb)->ipimap;
1335 	imap = JFS_IP(ipimap)->i_imap;
1336 	JFS_IP(ip)->ipimap = ipimap;
1337 	JFS_IP(ip)->fileset = FILESYSTEM_I;
1338 
1339 	/* for a directory, the allocation policy is to start
1340 	 * at the ag level using the preferred ag.
1341 	 */
1342 	if (dir) {
1343 		agno = dbNextAG(JFS_SBI(pip->i_sb)->ipbmap);
1344 		AG_LOCK(imap, agno);
1345 		goto tryag;
1346 	}
1347 
1348 	/* for files, the policy starts off by trying to allocate from
1349 	 * the same iag containing the parent disk inode:
1350 	 * try to allocate the new disk inode close to the parent disk
1351 	 * inode, using parent disk inode number + 1 as the allocation
1352 	 * hint.  (we use a left-to-right policy to attempt to avoid
1353 	 * moving backward on the disk.)  compute the hint within the
1354 	 * file system and the iag.
1355 	 */
1356 
1357 	/* get the ag number of this iag */
1358 	agno = BLKTOAG(JFS_IP(pip)->agstart, JFS_SBI(pip->i_sb));
1359 
1360 	if (atomic_read(&JFS_SBI(pip->i_sb)->bmap->db_active[agno])) {
1361 		/*
1362 		 * There is an open file actively growing.  We want to
1363 		 * allocate new inodes from a different ag to avoid
1364 		 * fragmentation problems.
1365 		 */
1366 		agno = dbNextAG(JFS_SBI(pip->i_sb)->ipbmap);
1367 		AG_LOCK(imap, agno);
1368 		goto tryag;
1369 	}
1370 
1371 	inum = pip->i_ino + 1;
1372 	ino = inum & (INOSPERIAG - 1);
1373 
1374 	/* back off the hint if it is outside of the iag */
1375 	if (ino == 0)
1376 		inum = pip->i_ino;
1377 
1378 	/* lock the AG inode map information */
1379 	AG_LOCK(imap, agno);
1380 
1381 	/* Get read lock on imap inode */
1382 	IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
1383 
1384 	/* get the iag number and read the iag */
1385 	iagno = INOTOIAG(inum);
1386 	if ((rc = diIAGRead(imap, iagno, &mp))) {
1387 		IREAD_UNLOCK(ipimap);
1388 		AG_UNLOCK(imap, agno);
1389 		return (rc);
1390 	}
1391 	iagp = (struct iag *) mp->data;
1392 
1393 	/* determine if new inode extent is allowed to be added to the iag.
1394 	 * new inode extent can be added to the iag if the ag
1395 	 * has less than 32 free disk inodes and the iag has free extents.
1396 	 */
1397 	addext = (imap->im_agctl[agno].numfree < 32 && iagp->nfreeexts);
1398 
1399 	/*
1400 	 *	try to allocate from the IAG
1401 	 */
1402 	/* check if the inode may be allocated from the iag
1403 	 * (i.e. the inode has free inodes or new extent can be added).
1404 	 */
1405 	if (iagp->nfreeinos || addext) {
1406 		/* determine the extent number of the hint.
1407 		 */
1408 		extno = ino >> L2INOSPEREXT;
1409 
1410 		/* check if the extent containing the hint has backed
1411 		 * inodes.  if so, try to allocate within this extent.
1412 		 */
1413 		if (addressPXD(&iagp->inoext[extno])) {
1414 			bitno = ino & (INOSPEREXT - 1);
1415 			if ((bitno =
1416 			     diFindFree(le32_to_cpu(iagp->wmap[extno]),
1417 					bitno))
1418 			    < INOSPEREXT) {
1419 				ino = (extno << L2INOSPEREXT) + bitno;
1420 
1421 				/* a free inode (bit) was found within this
1422 				 * extent, so allocate it.
1423 				 */
1424 				rc = diAllocBit(imap, iagp, ino);
1425 				IREAD_UNLOCK(ipimap);
1426 				if (rc) {
1427 					assert(rc == -EIO);
1428 				} else {
1429 					/* set the results of the allocation
1430 					 * and write the iag.
1431 					 */
1432 					diInitInode(ip, iagno, ino, extno,
1433 						    iagp);
1434 					mark_metapage_dirty(mp);
1435 				}
1436 				release_metapage(mp);
1437 
1438 				/* free the AG lock and return.
1439 				 */
1440 				AG_UNLOCK(imap, agno);
1441 				return (rc);
1442 			}
1443 
1444 			if (!addext)
1445 				extno =
1446 				    (extno ==
1447 				     EXTSPERIAG - 1) ? 0 : extno + 1;
1448 		}
1449 
1450 		/*
1451 		 * no free inodes within the extent containing the hint.
1452 		 *
1453 		 * try to allocate from the backed extents following
1454 		 * hint or, if appropriate (i.e. addext is true), allocate
1455 		 * an extent of free inodes at or following the extent
1456 		 * containing the hint.
1457 		 *
1458 		 * the free inode and free extent summary maps are used
1459 		 * here, so determine the starting summary map position
1460 		 * and the number of words we'll have to examine.  again,
1461 		 * the approach is to allocate following the hint, so we
1462 		 * might have to initially ignore prior bits of the summary
1463 		 * map that represent extents prior to the extent containing
1464 		 * the hint and later revisit these bits.
1465 		 */
1466 		bitno = extno & (EXTSPERSUM - 1);
1467 		nwords = (bitno == 0) ? SMAPSZ : SMAPSZ + 1;
1468 		sword = extno >> L2EXTSPERSUM;
1469 
1470 		/* mask any prior bits for the starting words of the
1471 		 * summary map.
1472 		 */
1473 		mask = (bitno == 0) ? 0 : (ONES << (EXTSPERSUM - bitno));
1474 		inosmap = le32_to_cpu(iagp->inosmap[sword]) | mask;
1475 		extsmap = le32_to_cpu(iagp->extsmap[sword]) | mask;
1476 
1477 		/* scan the free inode and free extent summary maps for
1478 		 * free resources.
1479 		 */
1480 		for (i = 0; i < nwords; i++) {
1481 			/* check if this word of the free inode summary
1482 			 * map describes an extent with free inodes.
1483 			 */
1484 			if (~inosmap) {
1485 				/* an extent with free inodes has been
1486 				 * found. determine the extent number
1487 				 * and the inode number within the extent.
1488 				 */
1489 				rem = diFindFree(inosmap, 0);
1490 				extno = (sword << L2EXTSPERSUM) + rem;
1491 				rem = diFindFree(le32_to_cpu(iagp->wmap[extno]),
1492 						 0);
1493 				if (rem >= INOSPEREXT) {
1494 					IREAD_UNLOCK(ipimap);
1495 					release_metapage(mp);
1496 					AG_UNLOCK(imap, agno);
1497 					jfs_error(ip->i_sb,
1498 						  "can't find free bit in wmap\n");
1499 					return -EIO;
1500 				}
1501 
1502 				/* determine the inode number within the
1503 				 * iag and allocate the inode from the
1504 				 * map.
1505 				 */
1506 				ino = (extno << L2INOSPEREXT) + rem;
1507 				rc = diAllocBit(imap, iagp, ino);
1508 				IREAD_UNLOCK(ipimap);
1509 				if (rc)
1510 					assert(rc == -EIO);
1511 				else {
1512 					/* set the results of the allocation
1513 					 * and write the iag.
1514 					 */
1515 					diInitInode(ip, iagno, ino, extno,
1516 						    iagp);
1517 					mark_metapage_dirty(mp);
1518 				}
1519 				release_metapage(mp);
1520 
1521 				/* free the AG lock and return.
1522 				 */
1523 				AG_UNLOCK(imap, agno);
1524 				return (rc);
1525 
1526 			}
1527 
1528 			/* check if we may allocate an extent of free
1529 			 * inodes and whether this word of the free
1530 			 * extents summary map describes a free extent.
1531 			 */
1532 			if (addext && ~extsmap) {
1533 				/* a free extent has been found.  determine
1534 				 * the extent number.
1535 				 */
1536 				rem = diFindFree(extsmap, 0);
1537 				extno = (sword << L2EXTSPERSUM) + rem;
1538 
1539 				/* allocate an extent of free inodes.
1540 				 */
1541 				if ((rc = diNewExt(imap, iagp, extno))) {
1542 					/* if there is no disk space for a
1543 					 * new extent, try to allocate the
1544 					 * disk inode from somewhere else.
1545 					 */
1546 					if (rc == -ENOSPC)
1547 						break;
1548 
1549 					assert(rc == -EIO);
1550 				} else {
1551 					/* set the results of the allocation
1552 					 * and write the iag.
1553 					 */
1554 					diInitInode(ip, iagno,
1555 						    extno << L2INOSPEREXT,
1556 						    extno, iagp);
1557 					mark_metapage_dirty(mp);
1558 				}
1559 				release_metapage(mp);
1560 				/* free the imap inode & the AG lock & return.
1561 				 */
1562 				IREAD_UNLOCK(ipimap);
1563 				AG_UNLOCK(imap, agno);
1564 				return (rc);
1565 			}
1566 
1567 			/* move on to the next set of summary map words.
1568 			 */
1569 			sword = (sword == SMAPSZ - 1) ? 0 : sword + 1;
1570 			inosmap = le32_to_cpu(iagp->inosmap[sword]);
1571 			extsmap = le32_to_cpu(iagp->extsmap[sword]);
1572 		}
1573 	}
1574 	/* unlock imap inode */
1575 	IREAD_UNLOCK(ipimap);
1576 
1577 	/* nothing doing in this iag, so release it. */
1578 	release_metapage(mp);
1579 
1580       tryag:
1581 	/*
1582 	 * try to allocate anywhere within the same AG as the parent inode.
1583 	 */
1584 	rc = diAllocAG(imap, agno, dir, ip);
1585 
1586 	AG_UNLOCK(imap, agno);
1587 
1588 	if (rc != -ENOSPC)
1589 		return (rc);
1590 
1591 	/*
1592 	 * try to allocate in any AG.
1593 	 */
1594 	return (diAllocAny(imap, agno, dir, ip));
1595 }
1596 
1597 
1598 /*
1599  * NAME:	diAllocAG(imap,agno,dir,ip)
1600  *
1601  * FUNCTION:	allocate a disk inode from the allocation group.
1602  *
1603  *		this routine first determines if a new extent of free
1604  *		inodes should be added for the allocation group, with
1605  *		the current request satisfied from this extent. if this
1606  *		is the case, an attempt will be made to do just that.  if
1607  *		this attempt fails or it has been determined that a new
1608  *		extent should not be added, an attempt is made to satisfy
1609  *		the request by allocating an existing (backed) free inode
1610  *		from the allocation group.
1611  *
1612  * PRE CONDITION: Already have the AG lock for this AG.
1613  *
1614  * PARAMETERS:
1615  *	imap	- pointer to inode map control structure.
1616  *	agno	- allocation group to allocate from.
1617  *	dir	- 'true' if the new disk inode is for a directory.
1618  *	ip	- pointer to the new inode to be filled in on successful return
1619  *		  with the disk inode number allocated, its extent address
1620  *		  and the start of the ag.
1621  *
1622  * RETURN VALUES:
1623  *	0	- success.
1624  *	-ENOSPC	- insufficient disk resources.
1625  *	-EIO	- i/o error.
1626  */
1627 static int
1628 diAllocAG(struct inomap * imap, int agno, bool dir, struct inode *ip)
1629 {
1630 	int rc, addext, numfree, numinos;
1631 
1632 	/* get the number of free and the number of backed disk
1633 	 * inodes currently within the ag.
1634 	 */
1635 	numfree = imap->im_agctl[agno].numfree;
1636 	numinos = imap->im_agctl[agno].numinos;
1637 
1638 	if (numfree > numinos) {
1639 		jfs_error(ip->i_sb, "numfree > numinos\n");
1640 		return -EIO;
1641 	}
1642 
1643 	/* determine if we should allocate a new extent of free inodes
1644 	 * within the ag: for directory inodes, add a new extent
1645 	 * if there are a small number of free inodes or number of free
1646 	 * inodes is a small percentage of the number of backed inodes.
1647 	 */
1648 	if (dir)
1649 		addext = (numfree < 64 ||
1650 			  (numfree < 256
1651 			   && ((numfree * 100) / numinos) <= 20));
1652 	else
1653 		addext = (numfree == 0);
1654 
1655 	/*
1656 	 * try to allocate a new extent of free inodes.
1657 	 */
1658 	if (addext) {
1659 		/* if free space is not available for this new extent, try
1660 		 * below to allocate a free and existing (already backed)
1661 		 * inode from the ag.
1662 		 */
1663 		if ((rc = diAllocExt(imap, agno, ip)) != -ENOSPC)
1664 			return (rc);
1665 	}
1666 
1667 	/*
1668 	 * try to allocate an existing free inode from the ag.
1669 	 */
1670 	return (diAllocIno(imap, agno, ip));
1671 }
1672 
1673 
1674 /*
1675  * NAME:	diAllocAny(imap,agno,dir,iap)
1676  *
1677  * FUNCTION:	allocate a disk inode from any other allocation group.
1678  *
1679  *		this routine is called when an allocation attempt within
1680  *		the primary allocation group has failed. if attempts to
1681  *		allocate an inode from any allocation group other than the
1682  *		specified primary group.
1683  *
1684  * PARAMETERS:
1685  *	imap	- pointer to inode map control structure.
1686  *	agno	- primary allocation group (to avoid).
1687  *	dir	- 'true' if the new disk inode is for a directory.
1688  *	ip	- pointer to a new inode to be filled in on successful return
1689  *		  with the disk inode number allocated, its extent address
1690  *		  and the start of the ag.
1691  *
1692  * RETURN VALUES:
1693  *	0	- success.
1694  *	-ENOSPC	- insufficient disk resources.
1695  *	-EIO	- i/o error.
1696  */
1697 static int
1698 diAllocAny(struct inomap * imap, int agno, bool dir, struct inode *ip)
1699 {
1700 	int ag, rc;
1701 	int maxag = JFS_SBI(imap->im_ipimap->i_sb)->bmap->db_maxag;
1702 
1703 
1704 	/* try to allocate from the ags following agno up to
1705 	 * the maximum ag number.
1706 	 */
1707 	for (ag = agno + 1; ag <= maxag; ag++) {
1708 		AG_LOCK(imap, ag);
1709 
1710 		rc = diAllocAG(imap, ag, dir, ip);
1711 
1712 		AG_UNLOCK(imap, ag);
1713 
1714 		if (rc != -ENOSPC)
1715 			return (rc);
1716 	}
1717 
1718 	/* try to allocate from the ags in front of agno.
1719 	 */
1720 	for (ag = 0; ag < agno; ag++) {
1721 		AG_LOCK(imap, ag);
1722 
1723 		rc = diAllocAG(imap, ag, dir, ip);
1724 
1725 		AG_UNLOCK(imap, ag);
1726 
1727 		if (rc != -ENOSPC)
1728 			return (rc);
1729 	}
1730 
1731 	/* no free disk inodes.
1732 	 */
1733 	return -ENOSPC;
1734 }
1735 
1736 
1737 /*
1738  * NAME:	diAllocIno(imap,agno,ip)
1739  *
1740  * FUNCTION:	allocate a disk inode from the allocation group's free
1741  *		inode list, returning an error if this free list is
1742  *		empty (i.e. no iags on the list).
1743  *
1744  *		allocation occurs from the first iag on the list using
1745  *		the iag's free inode summary map to find the leftmost
1746  *		free inode in the iag.
1747  *
1748  * PRE CONDITION: Already have AG lock for this AG.
1749  *
1750  * PARAMETERS:
1751  *	imap	- pointer to inode map control structure.
1752  *	agno	- allocation group.
1753  *	ip	- pointer to new inode to be filled in on successful return
1754  *		  with the disk inode number allocated, its extent address
1755  *		  and the start of the ag.
1756  *
1757  * RETURN VALUES:
1758  *	0	- success.
1759  *	-ENOSPC	- insufficient disk resources.
1760  *	-EIO	- i/o error.
1761  */
1762 static int diAllocIno(struct inomap * imap, int agno, struct inode *ip)
1763 {
1764 	int iagno, ino, rc, rem, extno, sword;
1765 	struct metapage *mp;
1766 	struct iag *iagp;
1767 
1768 	/* check if there are iags on the ag's free inode list.
1769 	 */
1770 	if ((iagno = imap->im_agctl[agno].inofree) < 0)
1771 		return -ENOSPC;
1772 
1773 	/* obtain read lock on imap inode */
1774 	IREAD_LOCK(imap->im_ipimap, RDWRLOCK_IMAP);
1775 
1776 	/* read the iag at the head of the list.
1777 	 */
1778 	if ((rc = diIAGRead(imap, iagno, &mp))) {
1779 		IREAD_UNLOCK(imap->im_ipimap);
1780 		return (rc);
1781 	}
1782 	iagp = (struct iag *) mp->data;
1783 
1784 	/* better be free inodes in this iag if it is on the
1785 	 * list.
1786 	 */
1787 	if (!iagp->nfreeinos) {
1788 		IREAD_UNLOCK(imap->im_ipimap);
1789 		release_metapage(mp);
1790 		jfs_error(ip->i_sb, "nfreeinos = 0, but iag on freelist\n");
1791 		return -EIO;
1792 	}
1793 
1794 	/* scan the free inode summary map to find an extent
1795 	 * with free inodes.
1796 	 */
1797 	for (sword = 0;; sword++) {
1798 		if (sword >= SMAPSZ) {
1799 			IREAD_UNLOCK(imap->im_ipimap);
1800 			release_metapage(mp);
1801 			jfs_error(ip->i_sb,
1802 				  "free inode not found in summary map\n");
1803 			return -EIO;
1804 		}
1805 
1806 		if (~iagp->inosmap[sword])
1807 			break;
1808 	}
1809 
1810 	/* found a extent with free inodes. determine
1811 	 * the extent number.
1812 	 */
1813 	rem = diFindFree(le32_to_cpu(iagp->inosmap[sword]), 0);
1814 	if (rem >= EXTSPERSUM) {
1815 		IREAD_UNLOCK(imap->im_ipimap);
1816 		release_metapage(mp);
1817 		jfs_error(ip->i_sb, "no free extent found\n");
1818 		return -EIO;
1819 	}
1820 	extno = (sword << L2EXTSPERSUM) + rem;
1821 
1822 	/* find the first free inode in the extent.
1823 	 */
1824 	rem = diFindFree(le32_to_cpu(iagp->wmap[extno]), 0);
1825 	if (rem >= INOSPEREXT) {
1826 		IREAD_UNLOCK(imap->im_ipimap);
1827 		release_metapage(mp);
1828 		jfs_error(ip->i_sb, "free inode not found\n");
1829 		return -EIO;
1830 	}
1831 
1832 	/* compute the inode number within the iag.
1833 	 */
1834 	ino = (extno << L2INOSPEREXT) + rem;
1835 
1836 	/* allocate the inode.
1837 	 */
1838 	rc = diAllocBit(imap, iagp, ino);
1839 	IREAD_UNLOCK(imap->im_ipimap);
1840 	if (rc) {
1841 		release_metapage(mp);
1842 		return (rc);
1843 	}
1844 
1845 	/* set the results of the allocation and write the iag.
1846 	 */
1847 	diInitInode(ip, iagno, ino, extno, iagp);
1848 	write_metapage(mp);
1849 
1850 	return (0);
1851 }
1852 
1853 
1854 /*
1855  * NAME:	diAllocExt(imap,agno,ip)
1856  *
1857  * FUNCTION:	add a new extent of free inodes to an iag, allocating
1858  *		an inode from this extent to satisfy the current allocation
1859  *		request.
1860  *
1861  *		this routine first tries to find an existing iag with free
1862  *		extents through the ag free extent list.  if list is not
1863  *		empty, the head of the list will be selected as the home
1864  *		of the new extent of free inodes.  otherwise (the list is
1865  *		empty), a new iag will be allocated for the ag to contain
1866  *		the extent.
1867  *
1868  *		once an iag has been selected, the free extent summary map
1869  *		is used to locate a free extent within the iag and diNewExt()
1870  *		is called to initialize the extent, with initialization
1871  *		including the allocation of the first inode of the extent
1872  *		for the purpose of satisfying this request.
1873  *
1874  * PARAMETERS:
1875  *	imap	- pointer to inode map control structure.
1876  *	agno	- allocation group number.
1877  *	ip	- pointer to new inode to be filled in on successful return
1878  *		  with the disk inode number allocated, its extent address
1879  *		  and the start of the ag.
1880  *
1881  * RETURN VALUES:
1882  *	0	- success.
1883  *	-ENOSPC	- insufficient disk resources.
1884  *	-EIO	- i/o error.
1885  */
1886 static int diAllocExt(struct inomap * imap, int agno, struct inode *ip)
1887 {
1888 	int rem, iagno, sword, extno, rc;
1889 	struct metapage *mp;
1890 	struct iag *iagp;
1891 
1892 	/* check if the ag has any iags with free extents.  if not,
1893 	 * allocate a new iag for the ag.
1894 	 */
1895 	if ((iagno = imap->im_agctl[agno].extfree) < 0) {
1896 		/* If successful, diNewIAG will obtain the read lock on the
1897 		 * imap inode.
1898 		 */
1899 		if ((rc = diNewIAG(imap, &iagno, agno, &mp))) {
1900 			return (rc);
1901 		}
1902 		iagp = (struct iag *) mp->data;
1903 
1904 		/* set the ag number if this a brand new iag
1905 		 */
1906 		iagp->agstart =
1907 		    cpu_to_le64(AGTOBLK(agno, imap->im_ipimap));
1908 	} else {
1909 		/* read the iag.
1910 		 */
1911 		IREAD_LOCK(imap->im_ipimap, RDWRLOCK_IMAP);
1912 		if ((rc = diIAGRead(imap, iagno, &mp))) {
1913 			IREAD_UNLOCK(imap->im_ipimap);
1914 			jfs_error(ip->i_sb, "error reading iag\n");
1915 			return rc;
1916 		}
1917 		iagp = (struct iag *) mp->data;
1918 	}
1919 
1920 	/* using the free extent summary map, find a free extent.
1921 	 */
1922 	for (sword = 0;; sword++) {
1923 		if (sword >= SMAPSZ) {
1924 			release_metapage(mp);
1925 			IREAD_UNLOCK(imap->im_ipimap);
1926 			jfs_error(ip->i_sb, "free ext summary map not found\n");
1927 			return -EIO;
1928 		}
1929 		if (~iagp->extsmap[sword])
1930 			break;
1931 	}
1932 
1933 	/* determine the extent number of the free extent.
1934 	 */
1935 	rem = diFindFree(le32_to_cpu(iagp->extsmap[sword]), 0);
1936 	if (rem >= EXTSPERSUM) {
1937 		release_metapage(mp);
1938 		IREAD_UNLOCK(imap->im_ipimap);
1939 		jfs_error(ip->i_sb, "free extent not found\n");
1940 		return -EIO;
1941 	}
1942 	extno = (sword << L2EXTSPERSUM) + rem;
1943 
1944 	/* initialize the new extent.
1945 	 */
1946 	rc = diNewExt(imap, iagp, extno);
1947 	IREAD_UNLOCK(imap->im_ipimap);
1948 	if (rc) {
1949 		/* something bad happened.  if a new iag was allocated,
1950 		 * place it back on the inode map's iag free list, and
1951 		 * clear the ag number information.
1952 		 */
1953 		if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
1954 			IAGFREE_LOCK(imap);
1955 			iagp->iagfree = cpu_to_le32(imap->im_freeiag);
1956 			imap->im_freeiag = iagno;
1957 			IAGFREE_UNLOCK(imap);
1958 		}
1959 		write_metapage(mp);
1960 		return (rc);
1961 	}
1962 
1963 	/* set the results of the allocation and write the iag.
1964 	 */
1965 	diInitInode(ip, iagno, extno << L2INOSPEREXT, extno, iagp);
1966 
1967 	write_metapage(mp);
1968 
1969 	return (0);
1970 }
1971 
1972 
1973 /*
1974  * NAME:	diAllocBit(imap,iagp,ino)
1975  *
1976  * FUNCTION:	allocate a backed inode from an iag.
1977  *
1978  *		this routine performs the mechanics of allocating a
1979  *		specified inode from a backed extent.
1980  *
1981  *		if the inode to be allocated represents the last free
1982  *		inode within the iag, the iag will be removed from the
1983  *		ag free inode list.
1984  *
1985  *		a careful update approach is used to provide consistency
1986  *		in the face of updates to multiple buffers.  under this
1987  *		approach, all required buffers are obtained before making
1988  *		any updates and are held all are updates are complete.
1989  *
1990  * PRE CONDITION: Already have buffer lock on iagp.  Already have AG lock on
1991  *	this AG.  Must have read lock on imap inode.
1992  *
1993  * PARAMETERS:
1994  *	imap	- pointer to inode map control structure.
1995  *	iagp	- pointer to iag.
1996  *	ino	- inode number to be allocated within the iag.
1997  *
1998  * RETURN VALUES:
1999  *	0	- success.
2000  *	-ENOSPC	- insufficient disk resources.
2001  *	-EIO	- i/o error.
2002  */
2003 static int diAllocBit(struct inomap * imap, struct iag * iagp, int ino)
2004 {
2005 	int extno, bitno, agno, sword, rc;
2006 	struct metapage *amp = NULL, *bmp = NULL;
2007 	struct iag *aiagp = NULL, *biagp = NULL;
2008 	u32 mask;
2009 
2010 	/* check if this is the last free inode within the iag.
2011 	 * if so, it will have to be removed from the ag free
2012 	 * inode list, so get the iags preceding and following
2013 	 * it on the list.
2014 	 */
2015 	if (iagp->nfreeinos == cpu_to_le32(1)) {
2016 		if ((int) le32_to_cpu(iagp->inofreefwd) >= 0) {
2017 			if ((rc =
2018 			     diIAGRead(imap, le32_to_cpu(iagp->inofreefwd),
2019 				       &amp)))
2020 				return (rc);
2021 			aiagp = (struct iag *) amp->data;
2022 		}
2023 
2024 		if ((int) le32_to_cpu(iagp->inofreeback) >= 0) {
2025 			if ((rc =
2026 			     diIAGRead(imap,
2027 				       le32_to_cpu(iagp->inofreeback),
2028 				       &bmp))) {
2029 				if (amp)
2030 					release_metapage(amp);
2031 				return (rc);
2032 			}
2033 			biagp = (struct iag *) bmp->data;
2034 		}
2035 	}
2036 
2037 	/* get the ag number, extent number, inode number within
2038 	 * the extent.
2039 	 */
2040 	agno = BLKTOAG(le64_to_cpu(iagp->agstart), JFS_SBI(imap->im_ipimap->i_sb));
2041 	extno = ino >> L2INOSPEREXT;
2042 	bitno = ino & (INOSPEREXT - 1);
2043 
2044 	/* compute the mask for setting the map.
2045 	 */
2046 	mask = HIGHORDER >> bitno;
2047 
2048 	/* the inode should be free and backed.
2049 	 */
2050 	if (((le32_to_cpu(iagp->pmap[extno]) & mask) != 0) ||
2051 	    ((le32_to_cpu(iagp->wmap[extno]) & mask) != 0) ||
2052 	    (addressPXD(&iagp->inoext[extno]) == 0)) {
2053 		if (amp)
2054 			release_metapage(amp);
2055 		if (bmp)
2056 			release_metapage(bmp);
2057 
2058 		jfs_error(imap->im_ipimap->i_sb, "iag inconsistent\n");
2059 		return -EIO;
2060 	}
2061 
2062 	/* mark the inode as allocated in the working map.
2063 	 */
2064 	iagp->wmap[extno] |= cpu_to_le32(mask);
2065 
2066 	/* check if all inodes within the extent are now
2067 	 * allocated.  if so, update the free inode summary
2068 	 * map to reflect this.
2069 	 */
2070 	if (iagp->wmap[extno] == cpu_to_le32(ONES)) {
2071 		sword = extno >> L2EXTSPERSUM;
2072 		bitno = extno & (EXTSPERSUM - 1);
2073 		iagp->inosmap[sword] |= cpu_to_le32(HIGHORDER >> bitno);
2074 	}
2075 
2076 	/* if this was the last free inode in the iag, remove the
2077 	 * iag from the ag free inode list.
2078 	 */
2079 	if (iagp->nfreeinos == cpu_to_le32(1)) {
2080 		if (amp) {
2081 			aiagp->inofreeback = iagp->inofreeback;
2082 			write_metapage(amp);
2083 		}
2084 
2085 		if (bmp) {
2086 			biagp->inofreefwd = iagp->inofreefwd;
2087 			write_metapage(bmp);
2088 		} else {
2089 			imap->im_agctl[agno].inofree =
2090 			    le32_to_cpu(iagp->inofreefwd);
2091 		}
2092 		iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
2093 	}
2094 
2095 	/* update the free inode count at the iag, ag, inode
2096 	 * map levels.
2097 	 */
2098 	le32_add_cpu(&iagp->nfreeinos, -1);
2099 	imap->im_agctl[agno].numfree -= 1;
2100 	atomic_dec(&imap->im_numfree);
2101 
2102 	return (0);
2103 }
2104 
2105 
2106 /*
2107  * NAME:	diNewExt(imap,iagp,extno)
2108  *
2109  * FUNCTION:	initialize a new extent of inodes for an iag, allocating
2110  *		the first inode of the extent for use for the current
2111  *		allocation request.
2112  *
2113  *		disk resources are allocated for the new extent of inodes
2114  *		and the inodes themselves are initialized to reflect their
2115  *		existence within the extent (i.e. their inode numbers and
2116  *		inode extent addresses are set) and their initial state
2117  *		(mode and link count are set to zero).
2118  *
2119  *		if the iag is new, it is not yet on an ag extent free list
2120  *		but will now be placed on this list.
2121  *
2122  *		if the allocation of the new extent causes the iag to
2123  *		have no free extent, the iag will be removed from the
2124  *		ag extent free list.
2125  *
2126  *		if the iag has no free backed inodes, it will be placed
2127  *		on the ag free inode list, since the addition of the new
2128  *		extent will now cause it to have free inodes.
2129  *
2130  *		a careful update approach is used to provide consistency
2131  *		(i.e. list consistency) in the face of updates to multiple
2132  *		buffers.  under this approach, all required buffers are
2133  *		obtained before making any updates and are held until all
2134  *		updates are complete.
2135  *
2136  * PRE CONDITION: Already have buffer lock on iagp.  Already have AG lock on
2137  *	this AG.  Must have read lock on imap inode.
2138  *
2139  * PARAMETERS:
2140  *	imap	- pointer to inode map control structure.
2141  *	iagp	- pointer to iag.
2142  *	extno	- extent number.
2143  *
2144  * RETURN VALUES:
2145  *	0	- success.
2146  *	-ENOSPC	- insufficient disk resources.
2147  *	-EIO	- i/o error.
2148  */
2149 static int diNewExt(struct inomap * imap, struct iag * iagp, int extno)
2150 {
2151 	int agno, iagno, fwd, back, freei = 0, sword, rc;
2152 	struct iag *aiagp = NULL, *biagp = NULL, *ciagp = NULL;
2153 	struct metapage *amp, *bmp, *cmp, *dmp;
2154 	struct inode *ipimap;
2155 	s64 blkno, hint;
2156 	int i, j;
2157 	u32 mask;
2158 	ino_t ino;
2159 	struct dinode *dp;
2160 	struct jfs_sb_info *sbi;
2161 
2162 	/* better have free extents.
2163 	 */
2164 	if (!iagp->nfreeexts) {
2165 		jfs_error(imap->im_ipimap->i_sb, "no free extents\n");
2166 		return -EIO;
2167 	}
2168 
2169 	/* get the inode map inode.
2170 	 */
2171 	ipimap = imap->im_ipimap;
2172 	sbi = JFS_SBI(ipimap->i_sb);
2173 
2174 	amp = bmp = cmp = NULL;
2175 
2176 	/* get the ag and iag numbers for this iag.
2177 	 */
2178 	agno = BLKTOAG(le64_to_cpu(iagp->agstart), sbi);
2179 	iagno = le32_to_cpu(iagp->iagnum);
2180 
2181 	/* check if this is the last free extent within the
2182 	 * iag.  if so, the iag must be removed from the ag
2183 	 * free extent list, so get the iags preceding and
2184 	 * following the iag on this list.
2185 	 */
2186 	if (iagp->nfreeexts == cpu_to_le32(1)) {
2187 		if ((fwd = le32_to_cpu(iagp->extfreefwd)) >= 0) {
2188 			if ((rc = diIAGRead(imap, fwd, &amp)))
2189 				return (rc);
2190 			aiagp = (struct iag *) amp->data;
2191 		}
2192 
2193 		if ((back = le32_to_cpu(iagp->extfreeback)) >= 0) {
2194 			if ((rc = diIAGRead(imap, back, &bmp)))
2195 				goto error_out;
2196 			biagp = (struct iag *) bmp->data;
2197 		}
2198 	} else {
2199 		/* the iag has free extents.  if all extents are free
2200 		 * (as is the case for a newly allocated iag), the iag
2201 		 * must be added to the ag free extent list, so get
2202 		 * the iag at the head of the list in preparation for
2203 		 * adding this iag to this list.
2204 		 */
2205 		fwd = back = -1;
2206 		if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2207 			if ((fwd = imap->im_agctl[agno].extfree) >= 0) {
2208 				if ((rc = diIAGRead(imap, fwd, &amp)))
2209 					goto error_out;
2210 				aiagp = (struct iag *) amp->data;
2211 			}
2212 		}
2213 	}
2214 
2215 	/* check if the iag has no free inodes.  if so, the iag
2216 	 * will have to be added to the ag free inode list, so get
2217 	 * the iag at the head of the list in preparation for
2218 	 * adding this iag to this list.  in doing this, we must
2219 	 * check if we already have the iag at the head of
2220 	 * the list in hand.
2221 	 */
2222 	if (iagp->nfreeinos == 0) {
2223 		freei = imap->im_agctl[agno].inofree;
2224 
2225 		if (freei >= 0) {
2226 			if (freei == fwd) {
2227 				ciagp = aiagp;
2228 			} else if (freei == back) {
2229 				ciagp = biagp;
2230 			} else {
2231 				if ((rc = diIAGRead(imap, freei, &cmp)))
2232 					goto error_out;
2233 				ciagp = (struct iag *) cmp->data;
2234 			}
2235 			if (ciagp == NULL) {
2236 				jfs_error(imap->im_ipimap->i_sb,
2237 					  "ciagp == NULL\n");
2238 				rc = -EIO;
2239 				goto error_out;
2240 			}
2241 		}
2242 	}
2243 
2244 	/* allocate disk space for the inode extent.
2245 	 */
2246 	if ((extno == 0) || (addressPXD(&iagp->inoext[extno - 1]) == 0))
2247 		hint = ((s64) agno << sbi->bmap->db_agl2size) - 1;
2248 	else
2249 		hint = addressPXD(&iagp->inoext[extno - 1]) +
2250 		    lengthPXD(&iagp->inoext[extno - 1]) - 1;
2251 
2252 	if ((rc = dbAlloc(ipimap, hint, (s64) imap->im_nbperiext, &blkno)))
2253 		goto error_out;
2254 
2255 	/* compute the inode number of the first inode within the
2256 	 * extent.
2257 	 */
2258 	ino = (iagno << L2INOSPERIAG) + (extno << L2INOSPEREXT);
2259 
2260 	/* initialize the inodes within the newly allocated extent a
2261 	 * page at a time.
2262 	 */
2263 	for (i = 0; i < imap->im_nbperiext; i += sbi->nbperpage) {
2264 		/* get a buffer for this page of disk inodes.
2265 		 */
2266 		dmp = get_metapage(ipimap, blkno + i, PSIZE, 1);
2267 		if (dmp == NULL) {
2268 			rc = -EIO;
2269 			goto error_out;
2270 		}
2271 		dp = (struct dinode *) dmp->data;
2272 
2273 		/* initialize the inode number, mode, link count and
2274 		 * inode extent address.
2275 		 */
2276 		for (j = 0; j < INOSPERPAGE; j++, dp++, ino++) {
2277 			dp->di_inostamp = cpu_to_le32(sbi->inostamp);
2278 			dp->di_number = cpu_to_le32(ino);
2279 			dp->di_fileset = cpu_to_le32(FILESYSTEM_I);
2280 			dp->di_mode = 0;
2281 			dp->di_nlink = 0;
2282 			PXDaddress(&(dp->di_ixpxd), blkno);
2283 			PXDlength(&(dp->di_ixpxd), imap->im_nbperiext);
2284 		}
2285 		write_metapage(dmp);
2286 	}
2287 
2288 	/* if this is the last free extent within the iag, remove the
2289 	 * iag from the ag free extent list.
2290 	 */
2291 	if (iagp->nfreeexts == cpu_to_le32(1)) {
2292 		if (fwd >= 0)
2293 			aiagp->extfreeback = iagp->extfreeback;
2294 
2295 		if (back >= 0)
2296 			biagp->extfreefwd = iagp->extfreefwd;
2297 		else
2298 			imap->im_agctl[agno].extfree =
2299 			    le32_to_cpu(iagp->extfreefwd);
2300 
2301 		iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
2302 	} else {
2303 		/* if the iag has all free extents (newly allocated iag),
2304 		 * add the iag to the ag free extent list.
2305 		 */
2306 		if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2307 			if (fwd >= 0)
2308 				aiagp->extfreeback = cpu_to_le32(iagno);
2309 
2310 			iagp->extfreefwd = cpu_to_le32(fwd);
2311 			iagp->extfreeback = cpu_to_le32(-1);
2312 			imap->im_agctl[agno].extfree = iagno;
2313 		}
2314 	}
2315 
2316 	/* if the iag has no free inodes, add the iag to the
2317 	 * ag free inode list.
2318 	 */
2319 	if (iagp->nfreeinos == 0) {
2320 		if (freei >= 0)
2321 			ciagp->inofreeback = cpu_to_le32(iagno);
2322 
2323 		iagp->inofreefwd =
2324 		    cpu_to_le32(imap->im_agctl[agno].inofree);
2325 		iagp->inofreeback = cpu_to_le32(-1);
2326 		imap->im_agctl[agno].inofree = iagno;
2327 	}
2328 
2329 	/* initialize the extent descriptor of the extent. */
2330 	PXDlength(&iagp->inoext[extno], imap->im_nbperiext);
2331 	PXDaddress(&iagp->inoext[extno], blkno);
2332 
2333 	/* initialize the working and persistent map of the extent.
2334 	 * the working map will be initialized such that
2335 	 * it indicates the first inode of the extent is allocated.
2336 	 */
2337 	iagp->wmap[extno] = cpu_to_le32(HIGHORDER);
2338 	iagp->pmap[extno] = 0;
2339 
2340 	/* update the free inode and free extent summary maps
2341 	 * for the extent to indicate the extent has free inodes
2342 	 * and no longer represents a free extent.
2343 	 */
2344 	sword = extno >> L2EXTSPERSUM;
2345 	mask = HIGHORDER >> (extno & (EXTSPERSUM - 1));
2346 	iagp->extsmap[sword] |= cpu_to_le32(mask);
2347 	iagp->inosmap[sword] &= cpu_to_le32(~mask);
2348 
2349 	/* update the free inode and free extent counts for the
2350 	 * iag.
2351 	 */
2352 	le32_add_cpu(&iagp->nfreeinos, (INOSPEREXT - 1));
2353 	le32_add_cpu(&iagp->nfreeexts, -1);
2354 
2355 	/* update the free and backed inode counts for the ag.
2356 	 */
2357 	imap->im_agctl[agno].numfree += (INOSPEREXT - 1);
2358 	imap->im_agctl[agno].numinos += INOSPEREXT;
2359 
2360 	/* update the free and backed inode counts for the inode map.
2361 	 */
2362 	atomic_add(INOSPEREXT - 1, &imap->im_numfree);
2363 	atomic_add(INOSPEREXT, &imap->im_numinos);
2364 
2365 	/* write the iags.
2366 	 */
2367 	if (amp)
2368 		write_metapage(amp);
2369 	if (bmp)
2370 		write_metapage(bmp);
2371 	if (cmp)
2372 		write_metapage(cmp);
2373 
2374 	return (0);
2375 
2376       error_out:
2377 
2378 	/* release the iags.
2379 	 */
2380 	if (amp)
2381 		release_metapage(amp);
2382 	if (bmp)
2383 		release_metapage(bmp);
2384 	if (cmp)
2385 		release_metapage(cmp);
2386 
2387 	return (rc);
2388 }
2389 
2390 
2391 /*
2392  * NAME:	diNewIAG(imap,iagnop,agno)
2393  *
2394  * FUNCTION:	allocate a new iag for an allocation group.
2395  *
2396  *		first tries to allocate the iag from the inode map
2397  *		iagfree list:
2398  *		if the list has free iags, the head of the list is removed
2399  *		and returned to satisfy the request.
2400  *		if the inode map's iag free list is empty, the inode map
2401  *		is extended to hold a new iag. this new iag is initialized
2402  *		and returned to satisfy the request.
2403  *
2404  * PARAMETERS:
2405  *	imap	- pointer to inode map control structure.
2406  *	iagnop	- pointer to an iag number set with the number of the
2407  *		  newly allocated iag upon successful return.
2408  *	agno	- allocation group number.
2409  *	bpp	- Buffer pointer to be filled in with new IAG's buffer
2410  *
2411  * RETURN VALUES:
2412  *	0	- success.
2413  *	-ENOSPC	- insufficient disk resources.
2414  *	-EIO	- i/o error.
2415  *
2416  * serialization:
2417  *	AG lock held on entry/exit;
2418  *	write lock on the map is held inside;
2419  *	read lock on the map is held on successful completion;
2420  *
2421  * note: new iag transaction:
2422  * . synchronously write iag;
2423  * . write log of xtree and inode of imap;
2424  * . commit;
2425  * . synchronous write of xtree (right to left, bottom to top);
2426  * . at start of logredo(): init in-memory imap with one additional iag page;
2427  * . at end of logredo(): re-read imap inode to determine
2428  *   new imap size;
2429  */
2430 static int
2431 diNewIAG(struct inomap * imap, int *iagnop, int agno, struct metapage ** mpp)
2432 {
2433 	int rc;
2434 	int iagno, i, xlen;
2435 	struct inode *ipimap;
2436 	struct super_block *sb;
2437 	struct jfs_sb_info *sbi;
2438 	struct metapage *mp;
2439 	struct iag *iagp;
2440 	s64 xaddr = 0;
2441 	s64 blkno;
2442 	tid_t tid;
2443 	struct inode *iplist[1];
2444 
2445 	/* pick up pointers to the inode map and mount inodes */
2446 	ipimap = imap->im_ipimap;
2447 	sb = ipimap->i_sb;
2448 	sbi = JFS_SBI(sb);
2449 
2450 	/* acquire the free iag lock */
2451 	IAGFREE_LOCK(imap);
2452 
2453 	/* if there are any iags on the inode map free iag list,
2454 	 * allocate the iag from the head of the list.
2455 	 */
2456 	if (imap->im_freeiag >= 0) {
2457 		/* pick up the iag number at the head of the list */
2458 		iagno = imap->im_freeiag;
2459 
2460 		/* determine the logical block number of the iag */
2461 		blkno = IAGTOLBLK(iagno, sbi->l2nbperpage);
2462 	} else {
2463 		/* no free iags. the inode map will have to be extented
2464 		 * to include a new iag.
2465 		 */
2466 
2467 		/* acquire inode map lock */
2468 		IWRITE_LOCK(ipimap, RDWRLOCK_IMAP);
2469 
2470 		if (ipimap->i_size >> L2PSIZE != imap->im_nextiag + 1) {
2471 			IWRITE_UNLOCK(ipimap);
2472 			IAGFREE_UNLOCK(imap);
2473 			jfs_error(imap->im_ipimap->i_sb,
2474 				  "ipimap->i_size is wrong\n");
2475 			return -EIO;
2476 		}
2477 
2478 
2479 		/* get the next available iag number */
2480 		iagno = imap->im_nextiag;
2481 
2482 		/* make sure that we have not exceeded the maximum inode
2483 		 * number limit.
2484 		 */
2485 		if (iagno > (MAXIAGS - 1)) {
2486 			/* release the inode map lock */
2487 			IWRITE_UNLOCK(ipimap);
2488 
2489 			rc = -ENOSPC;
2490 			goto out;
2491 		}
2492 
2493 		/*
2494 		 * synchronously append new iag page.
2495 		 */
2496 		/* determine the logical address of iag page to append */
2497 		blkno = IAGTOLBLK(iagno, sbi->l2nbperpage);
2498 
2499 		/* Allocate extent for new iag page */
2500 		xlen = sbi->nbperpage;
2501 		if ((rc = dbAlloc(ipimap, 0, (s64) xlen, &xaddr))) {
2502 			/* release the inode map lock */
2503 			IWRITE_UNLOCK(ipimap);
2504 
2505 			goto out;
2506 		}
2507 
2508 		/*
2509 		 * start transaction of update of the inode map
2510 		 * addressing structure pointing to the new iag page;
2511 		 */
2512 		tid = txBegin(sb, COMMIT_FORCE);
2513 		mutex_lock(&JFS_IP(ipimap)->commit_mutex);
2514 
2515 		/* update the inode map addressing structure to point to it */
2516 		if ((rc =
2517 		     xtInsert(tid, ipimap, 0, blkno, xlen, &xaddr, 0))) {
2518 			txEnd(tid);
2519 			mutex_unlock(&JFS_IP(ipimap)->commit_mutex);
2520 			/* Free the blocks allocated for the iag since it was
2521 			 * not successfully added to the inode map
2522 			 */
2523 			dbFree(ipimap, xaddr, (s64) xlen);
2524 
2525 			/* release the inode map lock */
2526 			IWRITE_UNLOCK(ipimap);
2527 
2528 			goto out;
2529 		}
2530 
2531 		/* update the inode map's inode to reflect the extension */
2532 		ipimap->i_size += PSIZE;
2533 		inode_add_bytes(ipimap, PSIZE);
2534 
2535 		/* assign a buffer for the page */
2536 		mp = get_metapage(ipimap, blkno, PSIZE, 0);
2537 		if (!mp) {
2538 			/*
2539 			 * This is very unlikely since we just created the
2540 			 * extent, but let's try to handle it correctly
2541 			 */
2542 			xtTruncate(tid, ipimap, ipimap->i_size - PSIZE,
2543 				   COMMIT_PWMAP);
2544 
2545 			txAbort(tid, 0);
2546 			txEnd(tid);
2547 			mutex_unlock(&JFS_IP(ipimap)->commit_mutex);
2548 
2549 			/* release the inode map lock */
2550 			IWRITE_UNLOCK(ipimap);
2551 
2552 			rc = -EIO;
2553 			goto out;
2554 		}
2555 		iagp = (struct iag *) mp->data;
2556 
2557 		/* init the iag */
2558 		memset(iagp, 0, sizeof(struct iag));
2559 		iagp->iagnum = cpu_to_le32(iagno);
2560 		iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
2561 		iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
2562 		iagp->iagfree = cpu_to_le32(-1);
2563 		iagp->nfreeinos = 0;
2564 		iagp->nfreeexts = cpu_to_le32(EXTSPERIAG);
2565 
2566 		/* initialize the free inode summary map (free extent
2567 		 * summary map initialization handled by bzero).
2568 		 */
2569 		for (i = 0; i < SMAPSZ; i++)
2570 			iagp->inosmap[i] = cpu_to_le32(ONES);
2571 
2572 		/*
2573 		 * Write and sync the metapage
2574 		 */
2575 		flush_metapage(mp);
2576 
2577 		/*
2578 		 * txCommit(COMMIT_FORCE) will synchronously write address
2579 		 * index pages and inode after commit in careful update order
2580 		 * of address index pages (right to left, bottom up);
2581 		 */
2582 		iplist[0] = ipimap;
2583 		rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
2584 
2585 		txEnd(tid);
2586 		mutex_unlock(&JFS_IP(ipimap)->commit_mutex);
2587 
2588 		duplicateIXtree(sb, blkno, xlen, &xaddr);
2589 
2590 		/* update the next available iag number */
2591 		imap->im_nextiag += 1;
2592 
2593 		/* Add the iag to the iag free list so we don't lose the iag
2594 		 * if a failure happens now.
2595 		 */
2596 		imap->im_freeiag = iagno;
2597 
2598 		/* Until we have logredo working, we want the imap inode &
2599 		 * control page to be up to date.
2600 		 */
2601 		diSync(ipimap);
2602 
2603 		/* release the inode map lock */
2604 		IWRITE_UNLOCK(ipimap);
2605 	}
2606 
2607 	/* obtain read lock on map */
2608 	IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
2609 
2610 	/* read the iag */
2611 	if ((rc = diIAGRead(imap, iagno, &mp))) {
2612 		IREAD_UNLOCK(ipimap);
2613 		rc = -EIO;
2614 		goto out;
2615 	}
2616 	iagp = (struct iag *) mp->data;
2617 
2618 	/* remove the iag from the iag free list */
2619 	imap->im_freeiag = le32_to_cpu(iagp->iagfree);
2620 	iagp->iagfree = cpu_to_le32(-1);
2621 
2622 	/* set the return iag number and buffer pointer */
2623 	*iagnop = iagno;
2624 	*mpp = mp;
2625 
2626       out:
2627 	/* release the iag free lock */
2628 	IAGFREE_UNLOCK(imap);
2629 
2630 	return (rc);
2631 }
2632 
2633 /*
2634  * NAME:	diIAGRead()
2635  *
2636  * FUNCTION:	get the buffer for the specified iag within a fileset
2637  *		or aggregate inode map.
2638  *
2639  * PARAMETERS:
2640  *	imap	- pointer to inode map control structure.
2641  *	iagno	- iag number.
2642  *	bpp	- point to buffer pointer to be filled in on successful
2643  *		  exit.
2644  *
2645  * SERIALIZATION:
2646  *	must have read lock on imap inode
2647  *	(When called by diExtendFS, the filesystem is quiesced, therefore
2648  *	 the read lock is unnecessary.)
2649  *
2650  * RETURN VALUES:
2651  *	0	- success.
2652  *	-EIO	- i/o error.
2653  */
2654 static int diIAGRead(struct inomap * imap, int iagno, struct metapage ** mpp)
2655 {
2656 	struct inode *ipimap = imap->im_ipimap;
2657 	s64 blkno;
2658 
2659 	/* compute the logical block number of the iag. */
2660 	blkno = IAGTOLBLK(iagno, JFS_SBI(ipimap->i_sb)->l2nbperpage);
2661 
2662 	/* read the iag. */
2663 	*mpp = read_metapage(ipimap, blkno, PSIZE, 0);
2664 	if (*mpp == NULL) {
2665 		return -EIO;
2666 	}
2667 
2668 	return (0);
2669 }
2670 
2671 /*
2672  * NAME:	diFindFree()
2673  *
2674  * FUNCTION:	find the first free bit in a word starting at
2675  *		the specified bit position.
2676  *
2677  * PARAMETERS:
2678  *	word	- word to be examined.
2679  *	start	- starting bit position.
2680  *
2681  * RETURN VALUES:
2682  *	bit position of first free bit in the word or 32 if
2683  *	no free bits were found.
2684  */
2685 static int diFindFree(u32 word, int start)
2686 {
2687 	int bitno;
2688 	assert(start < 32);
2689 	/* scan the word for the first free bit. */
2690 	for (word <<= start, bitno = start; bitno < 32;
2691 	     bitno++, word <<= 1) {
2692 		if ((word & HIGHORDER) == 0)
2693 			break;
2694 	}
2695 	return (bitno);
2696 }
2697 
2698 /*
2699  * NAME:	diUpdatePMap()
2700  *
2701  * FUNCTION: Update the persistent map in an IAG for the allocation or
2702  *	freeing of the specified inode.
2703  *
2704  * PRE CONDITIONS: Working map has already been updated for allocate.
2705  *
2706  * PARAMETERS:
2707  *	ipimap	- Incore inode map inode
2708  *	inum	- Number of inode to mark in permanent map
2709  *	is_free	- If 'true' indicates inode should be marked freed, otherwise
2710  *		  indicates inode should be marked allocated.
2711  *
2712  * RETURN VALUES:
2713  *		0 for success
2714  */
2715 int
2716 diUpdatePMap(struct inode *ipimap,
2717 	     unsigned long inum, bool is_free, struct tblock * tblk)
2718 {
2719 	int rc;
2720 	struct iag *iagp;
2721 	struct metapage *mp;
2722 	int iagno, ino, extno, bitno;
2723 	struct inomap *imap;
2724 	u32 mask;
2725 	struct jfs_log *log;
2726 	int lsn, difft, diffp;
2727 	unsigned long flags;
2728 
2729 	imap = JFS_IP(ipimap)->i_imap;
2730 	/* get the iag number containing the inode */
2731 	iagno = INOTOIAG(inum);
2732 	/* make sure that the iag is contained within the map */
2733 	if (iagno >= imap->im_nextiag) {
2734 		jfs_error(ipimap->i_sb, "the iag is outside the map\n");
2735 		return -EIO;
2736 	}
2737 	/* read the iag */
2738 	IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
2739 	rc = diIAGRead(imap, iagno, &mp);
2740 	IREAD_UNLOCK(ipimap);
2741 	if (rc)
2742 		return (rc);
2743 	metapage_wait_for_io(mp);
2744 	iagp = (struct iag *) mp->data;
2745 	/* get the inode number and extent number of the inode within
2746 	 * the iag and the inode number within the extent.
2747 	 */
2748 	ino = inum & (INOSPERIAG - 1);
2749 	extno = ino >> L2INOSPEREXT;
2750 	bitno = ino & (INOSPEREXT - 1);
2751 	mask = HIGHORDER >> bitno;
2752 	/*
2753 	 * mark the inode free in persistent map:
2754 	 */
2755 	if (is_free) {
2756 		/* The inode should have been allocated both in working
2757 		 * map and in persistent map;
2758 		 * the inode will be freed from working map at the release
2759 		 * of last reference release;
2760 		 */
2761 		if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
2762 			jfs_error(ipimap->i_sb,
2763 				  "inode %ld not marked as allocated in wmap!\n",
2764 				  inum);
2765 		}
2766 		if (!(le32_to_cpu(iagp->pmap[extno]) & mask)) {
2767 			jfs_error(ipimap->i_sb,
2768 				  "inode %ld not marked as allocated in pmap!\n",
2769 				  inum);
2770 		}
2771 		/* update the bitmap for the extent of the freed inode */
2772 		iagp->pmap[extno] &= cpu_to_le32(~mask);
2773 	}
2774 	/*
2775 	 * mark the inode allocated in persistent map:
2776 	 */
2777 	else {
2778 		/* The inode should be already allocated in the working map
2779 		 * and should be free in persistent map;
2780 		 */
2781 		if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
2782 			release_metapage(mp);
2783 			jfs_error(ipimap->i_sb,
2784 				  "the inode is not allocated in the working map\n");
2785 			return -EIO;
2786 		}
2787 		if ((le32_to_cpu(iagp->pmap[extno]) & mask) != 0) {
2788 			release_metapage(mp);
2789 			jfs_error(ipimap->i_sb,
2790 				  "the inode is not free in the persistent map\n");
2791 			return -EIO;
2792 		}
2793 		/* update the bitmap for the extent of the allocated inode */
2794 		iagp->pmap[extno] |= cpu_to_le32(mask);
2795 	}
2796 	/*
2797 	 * update iag lsn
2798 	 */
2799 	lsn = tblk->lsn;
2800 	log = JFS_SBI(tblk->sb)->log;
2801 	LOGSYNC_LOCK(log, flags);
2802 	if (mp->lsn != 0) {
2803 		/* inherit older/smaller lsn */
2804 		logdiff(difft, lsn, log);
2805 		logdiff(diffp, mp->lsn, log);
2806 		if (difft < diffp) {
2807 			mp->lsn = lsn;
2808 			/* move mp after tblock in logsync list */
2809 			list_move(&mp->synclist, &tblk->synclist);
2810 		}
2811 		/* inherit younger/larger clsn */
2812 		assert(mp->clsn);
2813 		logdiff(difft, tblk->clsn, log);
2814 		logdiff(diffp, mp->clsn, log);
2815 		if (difft > diffp)
2816 			mp->clsn = tblk->clsn;
2817 	} else {
2818 		mp->log = log;
2819 		mp->lsn = lsn;
2820 		/* insert mp after tblock in logsync list */
2821 		log->count++;
2822 		list_add(&mp->synclist, &tblk->synclist);
2823 		mp->clsn = tblk->clsn;
2824 	}
2825 	LOGSYNC_UNLOCK(log, flags);
2826 	write_metapage(mp);
2827 	return (0);
2828 }
2829 
2830 /*
2831  *	diExtendFS()
2832  *
2833  * function: update imap for extendfs();
2834  *
2835  * note: AG size has been increased s.t. each k old contiguous AGs are
2836  * coalesced into a new AG;
2837  */
2838 int diExtendFS(struct inode *ipimap, struct inode *ipbmap)
2839 {
2840 	int rc, rcx = 0;
2841 	struct inomap *imap = JFS_IP(ipimap)->i_imap;
2842 	struct iag *iagp = NULL, *hiagp = NULL;
2843 	struct bmap *mp = JFS_SBI(ipbmap->i_sb)->bmap;
2844 	struct metapage *bp, *hbp;
2845 	int i, n, head;
2846 	int numinos, xnuminos = 0, xnumfree = 0;
2847 	s64 agstart;
2848 
2849 	jfs_info("diExtendFS: nextiag:%d numinos:%d numfree:%d",
2850 		   imap->im_nextiag, atomic_read(&imap->im_numinos),
2851 		   atomic_read(&imap->im_numfree));
2852 
2853 	/*
2854 	 *	reconstruct imap
2855 	 *
2856 	 * coalesce contiguous k (newAGSize/oldAGSize) AGs;
2857 	 * i.e., (AGi, ..., AGj) where i = k*n and j = k*(n+1) - 1 to AGn;
2858 	 * note: new AG size = old AG size * (2**x).
2859 	 */
2860 
2861 	/* init per AG control information im_agctl[] */
2862 	for (i = 0; i < MAXAG; i++) {
2863 		imap->im_agctl[i].inofree = -1;
2864 		imap->im_agctl[i].extfree = -1;
2865 		imap->im_agctl[i].numinos = 0;	/* number of backed inodes */
2866 		imap->im_agctl[i].numfree = 0;	/* number of free backed inodes */
2867 	}
2868 
2869 	/*
2870 	 *	process each iag page of the map.
2871 	 *
2872 	 * rebuild AG Free Inode List, AG Free Inode Extent List;
2873 	 */
2874 	for (i = 0; i < imap->im_nextiag; i++) {
2875 		if ((rc = diIAGRead(imap, i, &bp))) {
2876 			rcx = rc;
2877 			continue;
2878 		}
2879 		iagp = (struct iag *) bp->data;
2880 		if (le32_to_cpu(iagp->iagnum) != i) {
2881 			release_metapage(bp);
2882 			jfs_error(ipimap->i_sb, "unexpected value of iagnum\n");
2883 			return -EIO;
2884 		}
2885 
2886 		/* leave free iag in the free iag list */
2887 		if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2888 			release_metapage(bp);
2889 			continue;
2890 		}
2891 
2892 		agstart = le64_to_cpu(iagp->agstart);
2893 		n = agstart >> mp->db_agl2size;
2894 		iagp->agstart = cpu_to_le64((s64)n << mp->db_agl2size);
2895 
2896 		/* compute backed inodes */
2897 		numinos = (EXTSPERIAG - le32_to_cpu(iagp->nfreeexts))
2898 		    << L2INOSPEREXT;
2899 		if (numinos > 0) {
2900 			/* merge AG backed inodes */
2901 			imap->im_agctl[n].numinos += numinos;
2902 			xnuminos += numinos;
2903 		}
2904 
2905 		/* if any backed free inodes, insert at AG free inode list */
2906 		if ((int) le32_to_cpu(iagp->nfreeinos) > 0) {
2907 			if ((head = imap->im_agctl[n].inofree) == -1) {
2908 				iagp->inofreefwd = cpu_to_le32(-1);
2909 				iagp->inofreeback = cpu_to_le32(-1);
2910 			} else {
2911 				if ((rc = diIAGRead(imap, head, &hbp))) {
2912 					rcx = rc;
2913 					goto nextiag;
2914 				}
2915 				hiagp = (struct iag *) hbp->data;
2916 				hiagp->inofreeback = iagp->iagnum;
2917 				iagp->inofreefwd = cpu_to_le32(head);
2918 				iagp->inofreeback = cpu_to_le32(-1);
2919 				write_metapage(hbp);
2920 			}
2921 
2922 			imap->im_agctl[n].inofree =
2923 			    le32_to_cpu(iagp->iagnum);
2924 
2925 			/* merge AG backed free inodes */
2926 			imap->im_agctl[n].numfree +=
2927 			    le32_to_cpu(iagp->nfreeinos);
2928 			xnumfree += le32_to_cpu(iagp->nfreeinos);
2929 		}
2930 
2931 		/* if any free extents, insert at AG free extent list */
2932 		if (le32_to_cpu(iagp->nfreeexts) > 0) {
2933 			if ((head = imap->im_agctl[n].extfree) == -1) {
2934 				iagp->extfreefwd = cpu_to_le32(-1);
2935 				iagp->extfreeback = cpu_to_le32(-1);
2936 			} else {
2937 				if ((rc = diIAGRead(imap, head, &hbp))) {
2938 					rcx = rc;
2939 					goto nextiag;
2940 				}
2941 				hiagp = (struct iag *) hbp->data;
2942 				hiagp->extfreeback = iagp->iagnum;
2943 				iagp->extfreefwd = cpu_to_le32(head);
2944 				iagp->extfreeback = cpu_to_le32(-1);
2945 				write_metapage(hbp);
2946 			}
2947 
2948 			imap->im_agctl[n].extfree =
2949 			    le32_to_cpu(iagp->iagnum);
2950 		}
2951 
2952 	      nextiag:
2953 		write_metapage(bp);
2954 	}
2955 
2956 	if (xnuminos != atomic_read(&imap->im_numinos) ||
2957 	    xnumfree != atomic_read(&imap->im_numfree)) {
2958 		jfs_error(ipimap->i_sb, "numinos or numfree incorrect\n");
2959 		return -EIO;
2960 	}
2961 
2962 	return rcx;
2963 }
2964 
2965 
2966 /*
2967  *	duplicateIXtree()
2968  *
2969  * serialization: IWRITE_LOCK held on entry/exit
2970  *
2971  * note: shadow page with regular inode (rel.2);
2972  */
2973 static void duplicateIXtree(struct super_block *sb, s64 blkno,
2974 			    int xlen, s64 *xaddr)
2975 {
2976 	struct jfs_superblock *j_sb;
2977 	struct buffer_head *bh;
2978 	struct inode *ip;
2979 	tid_t tid;
2980 
2981 	/* if AIT2 ipmap2 is bad, do not try to update it */
2982 	if (JFS_SBI(sb)->mntflag & JFS_BAD_SAIT)	/* s_flag */
2983 		return;
2984 	ip = diReadSpecial(sb, FILESYSTEM_I, 1);
2985 	if (ip == NULL) {
2986 		JFS_SBI(sb)->mntflag |= JFS_BAD_SAIT;
2987 		if (readSuper(sb, &bh))
2988 			return;
2989 		j_sb = (struct jfs_superblock *)bh->b_data;
2990 		j_sb->s_flag |= cpu_to_le32(JFS_BAD_SAIT);
2991 
2992 		mark_buffer_dirty(bh);
2993 		sync_dirty_buffer(bh);
2994 		brelse(bh);
2995 		return;
2996 	}
2997 
2998 	/* start transaction */
2999 	tid = txBegin(sb, COMMIT_FORCE);
3000 	/* update the inode map addressing structure to point to it */
3001 	if (xtInsert(tid, ip, 0, blkno, xlen, xaddr, 0)) {
3002 		JFS_SBI(sb)->mntflag |= JFS_BAD_SAIT;
3003 		txAbort(tid, 1);
3004 		goto cleanup;
3005 
3006 	}
3007 	/* update the inode map's inode to reflect the extension */
3008 	ip->i_size += PSIZE;
3009 	inode_add_bytes(ip, PSIZE);
3010 	txCommit(tid, 1, &ip, COMMIT_FORCE);
3011       cleanup:
3012 	txEnd(tid);
3013 	diFreeSpecial(ip);
3014 }
3015 
3016 /*
3017  * NAME:	copy_from_dinode()
3018  *
3019  * FUNCTION:	Copies inode info from disk inode to in-memory inode
3020  *
3021  * RETURN VALUES:
3022  *	0	- success
3023  *	-ENOMEM	- insufficient memory
3024  */
3025 static int copy_from_dinode(struct dinode * dip, struct inode *ip)
3026 {
3027 	struct jfs_inode_info *jfs_ip = JFS_IP(ip);
3028 	struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
3029 
3030 	jfs_ip->fileset = le32_to_cpu(dip->di_fileset);
3031 	jfs_ip->mode2 = le32_to_cpu(dip->di_mode);
3032 	jfs_set_inode_flags(ip);
3033 
3034 	ip->i_mode = le32_to_cpu(dip->di_mode) & 0xffff;
3035 	if (sbi->umask != -1) {
3036 		ip->i_mode = (ip->i_mode & ~0777) | (0777 & ~sbi->umask);
3037 		/* For directories, add x permission if r is allowed by umask */
3038 		if (S_ISDIR(ip->i_mode)) {
3039 			if (ip->i_mode & 0400)
3040 				ip->i_mode |= 0100;
3041 			if (ip->i_mode & 0040)
3042 				ip->i_mode |= 0010;
3043 			if (ip->i_mode & 0004)
3044 				ip->i_mode |= 0001;
3045 		}
3046 	}
3047 	set_nlink(ip, le32_to_cpu(dip->di_nlink));
3048 
3049 	jfs_ip->saved_uid = make_kuid(&init_user_ns, le32_to_cpu(dip->di_uid));
3050 	if (!uid_valid(sbi->uid))
3051 		ip->i_uid = jfs_ip->saved_uid;
3052 	else {
3053 		ip->i_uid = sbi->uid;
3054 	}
3055 
3056 	jfs_ip->saved_gid = make_kgid(&init_user_ns, le32_to_cpu(dip->di_gid));
3057 	if (!gid_valid(sbi->gid))
3058 		ip->i_gid = jfs_ip->saved_gid;
3059 	else {
3060 		ip->i_gid = sbi->gid;
3061 	}
3062 
3063 	ip->i_size = le64_to_cpu(dip->di_size);
3064 	ip->i_atime.tv_sec = le32_to_cpu(dip->di_atime.tv_sec);
3065 	ip->i_atime.tv_nsec = le32_to_cpu(dip->di_atime.tv_nsec);
3066 	ip->i_mtime.tv_sec = le32_to_cpu(dip->di_mtime.tv_sec);
3067 	ip->i_mtime.tv_nsec = le32_to_cpu(dip->di_mtime.tv_nsec);
3068 	inode_set_ctime(ip, le32_to_cpu(dip->di_ctime.tv_sec),
3069 			le32_to_cpu(dip->di_ctime.tv_nsec));
3070 	ip->i_blocks = LBLK2PBLK(ip->i_sb, le64_to_cpu(dip->di_nblocks));
3071 	ip->i_generation = le32_to_cpu(dip->di_gen);
3072 
3073 	jfs_ip->ixpxd = dip->di_ixpxd;	/* in-memory pxd's are little-endian */
3074 	jfs_ip->acl = dip->di_acl;	/* as are dxd's */
3075 	jfs_ip->ea = dip->di_ea;
3076 	jfs_ip->next_index = le32_to_cpu(dip->di_next_index);
3077 	jfs_ip->otime = le32_to_cpu(dip->di_otime.tv_sec);
3078 	jfs_ip->acltype = le32_to_cpu(dip->di_acltype);
3079 
3080 	if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode)) {
3081 		jfs_ip->dev = le32_to_cpu(dip->di_rdev);
3082 		ip->i_rdev = new_decode_dev(jfs_ip->dev);
3083 	}
3084 
3085 	if (S_ISDIR(ip->i_mode)) {
3086 		memcpy(&jfs_ip->u.dir, &dip->u._dir, 384);
3087 	} else if (S_ISREG(ip->i_mode) || S_ISLNK(ip->i_mode)) {
3088 		memcpy(&jfs_ip->i_xtroot, &dip->di_xtroot, 288);
3089 	} else
3090 		memcpy(&jfs_ip->i_inline_ea, &dip->di_inlineea, 128);
3091 
3092 	/* Zero the in-memory-only stuff */
3093 	jfs_ip->cflag = 0;
3094 	jfs_ip->btindex = 0;
3095 	jfs_ip->btorder = 0;
3096 	jfs_ip->bxflag = 0;
3097 	jfs_ip->blid = 0;
3098 	jfs_ip->atlhead = 0;
3099 	jfs_ip->atltail = 0;
3100 	jfs_ip->xtlid = 0;
3101 	return (0);
3102 }
3103 
3104 /*
3105  * NAME:	copy_to_dinode()
3106  *
3107  * FUNCTION:	Copies inode info from in-memory inode to disk inode
3108  */
3109 static void copy_to_dinode(struct dinode * dip, struct inode *ip)
3110 {
3111 	struct jfs_inode_info *jfs_ip = JFS_IP(ip);
3112 	struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
3113 
3114 	dip->di_fileset = cpu_to_le32(jfs_ip->fileset);
3115 	dip->di_inostamp = cpu_to_le32(sbi->inostamp);
3116 	dip->di_number = cpu_to_le32(ip->i_ino);
3117 	dip->di_gen = cpu_to_le32(ip->i_generation);
3118 	dip->di_size = cpu_to_le64(ip->i_size);
3119 	dip->di_nblocks = cpu_to_le64(PBLK2LBLK(ip->i_sb, ip->i_blocks));
3120 	dip->di_nlink = cpu_to_le32(ip->i_nlink);
3121 	if (!uid_valid(sbi->uid))
3122 		dip->di_uid = cpu_to_le32(i_uid_read(ip));
3123 	else
3124 		dip->di_uid =cpu_to_le32(from_kuid(&init_user_ns,
3125 						   jfs_ip->saved_uid));
3126 	if (!gid_valid(sbi->gid))
3127 		dip->di_gid = cpu_to_le32(i_gid_read(ip));
3128 	else
3129 		dip->di_gid = cpu_to_le32(from_kgid(&init_user_ns,
3130 						    jfs_ip->saved_gid));
3131 	/*
3132 	 * mode2 is only needed for storing the higher order bits.
3133 	 * Trust i_mode for the lower order ones
3134 	 */
3135 	if (sbi->umask == -1)
3136 		dip->di_mode = cpu_to_le32((jfs_ip->mode2 & 0xffff0000) |
3137 					   ip->i_mode);
3138 	else /* Leave the original permissions alone */
3139 		dip->di_mode = cpu_to_le32(jfs_ip->mode2);
3140 
3141 	dip->di_atime.tv_sec = cpu_to_le32(ip->i_atime.tv_sec);
3142 	dip->di_atime.tv_nsec = cpu_to_le32(ip->i_atime.tv_nsec);
3143 	dip->di_ctime.tv_sec = cpu_to_le32(inode_get_ctime(ip).tv_sec);
3144 	dip->di_ctime.tv_nsec = cpu_to_le32(inode_get_ctime(ip).tv_nsec);
3145 	dip->di_mtime.tv_sec = cpu_to_le32(ip->i_mtime.tv_sec);
3146 	dip->di_mtime.tv_nsec = cpu_to_le32(ip->i_mtime.tv_nsec);
3147 	dip->di_ixpxd = jfs_ip->ixpxd;	/* in-memory pxd's are little-endian */
3148 	dip->di_acl = jfs_ip->acl;	/* as are dxd's */
3149 	dip->di_ea = jfs_ip->ea;
3150 	dip->di_next_index = cpu_to_le32(jfs_ip->next_index);
3151 	dip->di_otime.tv_sec = cpu_to_le32(jfs_ip->otime);
3152 	dip->di_otime.tv_nsec = 0;
3153 	dip->di_acltype = cpu_to_le32(jfs_ip->acltype);
3154 	if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode))
3155 		dip->di_rdev = cpu_to_le32(jfs_ip->dev);
3156 }
3157