2 * Copyright (C) International Business Machines Corp., 2000-2004
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * jfs_imap.c: inode allocation map manager
23 * Each AG has a simple lock which is used to control the serialization of
24 * the AG level lists. This lock should be taken first whenever an AG
25 * level list will be modified or accessed.
27 * Each IAG is locked by obtaining the buffer for the IAG page.
29 * There is also a inode lock for the inode map inode. A read lock needs to
30 * be taken whenever an IAG is read from the map or the global level
31 * information is read. A write lock needs to be taken whenever the global
32 * level information is modified or an atomic operation needs to be used.
34 * If more than one IAG is read at one time, the read lock may not
35 * be given up until all of the IAG's are read. Otherwise, a deadlock
36 * may occur when trying to obtain the read lock while another thread
37 * holding the read lock is waiting on the IAG already being held.
39 * The control page of the inode map is read into memory by diMount().
40 * Thereafter it should only be modified in memory and then it will be
41 * written out when the filesystem is unmounted by diUnmount().
45 #include <linux/buffer_head.h>
46 #include <linux/pagemap.h>
47 #include <linux/quotaops.h>
48 #include <linux/vserver/xid.h>
50 #include "jfs_incore.h"
51 #include "jfs_filsys.h"
52 #include "jfs_dinode.h"
55 #include "jfs_metapage.h"
56 #include "jfs_superblock.h"
57 #include "jfs_debug.h"
62 /* iag free list lock */
63 #define IAGFREE_LOCK_INIT(imap) init_MUTEX(&imap->im_freelock)
64 #define IAGFREE_LOCK(imap) down(&imap->im_freelock)
65 #define IAGFREE_UNLOCK(imap) up(&imap->im_freelock)
67 /* per ag iag list locks */
68 #define AG_LOCK_INIT(imap,index) init_MUTEX(&(imap->im_aglock[index]))
69 #define AG_LOCK(imap,agno) down(&imap->im_aglock[agno])
70 #define AG_UNLOCK(imap,agno) up(&imap->im_aglock[agno])
75 extern struct address_space_operations jfs_aops;
80 static int diAllocAG(struct inomap *, int, boolean_t, struct inode *);
81 static int diAllocAny(struct inomap *, int, boolean_t, struct inode *);
82 static int diAllocBit(struct inomap *, struct iag *, int);
83 static int diAllocExt(struct inomap *, int, struct inode *);
84 static int diAllocIno(struct inomap *, int, struct inode *);
85 static int diFindFree(u32, int);
86 static int diNewExt(struct inomap *, struct iag *, int);
87 static int diNewIAG(struct inomap *, int *, int, struct metapage **);
88 static void duplicateIXtree(struct super_block *, s64, int, s64 *);
90 static int diIAGRead(struct inomap * imap, int, struct metapage **);
91 static int copy_from_dinode(struct dinode *, struct inode *);
92 static void copy_to_dinode(struct dinode *, struct inode *);
95 * debug code for double-checking inode map
97 /* #define _JFS_DEBUG_IMAP 1 */
99 #ifdef _JFS_DEBUG_IMAP
100 #define DBG_DIINIT(imap) DBGdiInit(imap)
101 #define DBG_DIALLOC(imap, ino) DBGdiAlloc(imap, ino)
102 #define DBG_DIFREE(imap, ino) DBGdiFree(imap, ino)
104 static void *DBGdiInit(struct inomap * imap);
105 static void DBGdiAlloc(struct inomap * imap, ino_t ino);
106 static void DBGdiFree(struct inomap * imap, ino_t ino);
108 #define DBG_DIINIT(imap)
109 #define DBG_DIALLOC(imap, ino)
110 #define DBG_DIFREE(imap, ino)
111 #endif /* _JFS_DEBUG_IMAP */
116 * FUNCTION: initialize the incore inode map control structures for
117 * a fileset or aggregate init time.
119 * the inode map's control structure (dinomap) is
120 * brought in from disk and placed in virtual memory.
123 * ipimap - pointer to inode map inode for the aggregate or fileset.
127 * -ENOMEM - insufficient free virtual memory.
130 int diMount(struct inode *ipimap)
135 struct dinomap_disk *dinom_le;
138 * allocate/initialize the in-memory inode map control structure
140 /* allocate the in-memory inode map control structure. */
141 imap = (struct inomap *) kmalloc(sizeof(struct inomap), GFP_KERNEL);
143 jfs_err("diMount: kmalloc returned NULL!");
147 /* read the on-disk inode map control structure. */
149 mp = read_metapage(ipimap,
150 IMAPBLKNO << JFS_SBI(ipimap->i_sb)->l2nbperpage,
157 /* copy the on-disk version to the in-memory version. */
158 dinom_le = (struct dinomap_disk *) mp->data;
159 imap->im_freeiag = le32_to_cpu(dinom_le->in_freeiag);
160 imap->im_nextiag = le32_to_cpu(dinom_le->in_nextiag);
161 atomic_set(&imap->im_numinos, le32_to_cpu(dinom_le->in_numinos));
162 atomic_set(&imap->im_numfree, le32_to_cpu(dinom_le->in_numfree));
163 imap->im_nbperiext = le32_to_cpu(dinom_le->in_nbperiext);
164 imap->im_l2nbperiext = le32_to_cpu(dinom_le->in_l2nbperiext);
165 for (index = 0; index < MAXAG; index++) {
166 imap->im_agctl[index].inofree =
167 le32_to_cpu(dinom_le->in_agctl[index].inofree);
168 imap->im_agctl[index].extfree =
169 le32_to_cpu(dinom_le->in_agctl[index].extfree);
170 imap->im_agctl[index].numinos =
171 le32_to_cpu(dinom_le->in_agctl[index].numinos);
172 imap->im_agctl[index].numfree =
173 le32_to_cpu(dinom_le->in_agctl[index].numfree);
176 /* release the buffer. */
177 release_metapage(mp);
180 * allocate/initialize inode allocation map locks
182 /* allocate and init iag free list lock */
183 IAGFREE_LOCK_INIT(imap);
185 /* allocate and init ag list locks */
186 for (index = 0; index < MAXAG; index++) {
187 AG_LOCK_INIT(imap, index);
190 /* bind the inode map inode and inode map control structure
193 imap->im_ipimap = ipimap;
194 JFS_IP(ipimap)->i_imap = imap;
205 * FUNCTION: write to disk the incore inode map control structures for
206 * a fileset or aggregate at unmount time.
209 * ipimap - pointer to inode map inode for the aggregate or fileset.
213 * -ENOMEM - insufficient free virtual memory.
216 int diUnmount(struct inode *ipimap, int mounterror)
218 struct inomap *imap = JFS_IP(ipimap)->i_imap;
221 * update the on-disk inode map control structure
224 if (!(mounterror || isReadOnly(ipimap)))
228 * Invalidate the page cache buffers
230 truncate_inode_pages(ipimap->i_mapping, 0);
233 * free in-memory control structure
244 int diSync(struct inode *ipimap)
246 struct dinomap_disk *dinom_le;
247 struct inomap *imp = JFS_IP(ipimap)->i_imap;
252 * write imap global conrol page
254 /* read the on-disk inode map control structure */
255 mp = get_metapage(ipimap,
256 IMAPBLKNO << JFS_SBI(ipimap->i_sb)->l2nbperpage,
259 jfs_err("diSync: get_metapage failed!");
263 /* copy the in-memory version to the on-disk version */
264 dinom_le = (struct dinomap_disk *) mp->data;
265 dinom_le->in_freeiag = cpu_to_le32(imp->im_freeiag);
266 dinom_le->in_nextiag = cpu_to_le32(imp->im_nextiag);
267 dinom_le->in_numinos = cpu_to_le32(atomic_read(&imp->im_numinos));
268 dinom_le->in_numfree = cpu_to_le32(atomic_read(&imp->im_numfree));
269 dinom_le->in_nbperiext = cpu_to_le32(imp->im_nbperiext);
270 dinom_le->in_l2nbperiext = cpu_to_le32(imp->im_l2nbperiext);
271 for (index = 0; index < MAXAG; index++) {
272 dinom_le->in_agctl[index].inofree =
273 cpu_to_le32(imp->im_agctl[index].inofree);
274 dinom_le->in_agctl[index].extfree =
275 cpu_to_le32(imp->im_agctl[index].extfree);
276 dinom_le->in_agctl[index].numinos =
277 cpu_to_le32(imp->im_agctl[index].numinos);
278 dinom_le->in_agctl[index].numfree =
279 cpu_to_le32(imp->im_agctl[index].numfree);
282 /* write out the control structure */
286 * write out dirty pages of imap
288 filemap_fdatawrite(ipimap->i_mapping);
289 filemap_fdatawait(ipimap->i_mapping);
291 diWriteSpecial(ipimap, 0);
300 * FUNCTION: initialize an incore inode from disk.
302 * on entry, the specifed incore inode should itself
303 * specify the disk inode number corresponding to the
304 * incore inode (i.e. i_number should be initialized).
306 * this routine handles incore inode initialization for
307 * both "special" and "regular" inodes. special inodes
308 * are those required early in the mount process and
309 * require special handling since much of the file system
310 * is not yet initialized. these "special" inodes are
311 * identified by a NULL inode map inode pointer and are
312 * actually initialized by a call to diReadSpecial().
314 * for regular inodes, the iag describing the disk inode
315 * is read from disk to determine the inode extent address
316 * for the disk inode. with the inode extent address in
317 * hand, the page of the extent that contains the disk
318 * inode is read and the disk inode is copied to the
322 * ip - pointer to incore inode to be initialized from disk.
327 * -ENOMEM - insufficient memory
330 int diRead(struct inode *ip)
332 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
333 int iagno, ino, extno, rc;
334 struct inode *ipimap;
345 jfs_info("diRead: ino = %ld", ip->i_ino);
347 ipimap = sbi->ipimap;
348 JFS_IP(ip)->ipimap = ipimap;
350 /* determine the iag number for this inode (number) */
351 iagno = INOTOIAG(ip->i_ino);
354 imap = JFS_IP(ipimap)->i_imap;
356 rc = diIAGRead(imap, iagno, &mp);
357 IREAD_UNLOCK(ipimap);
359 jfs_err("diRead: diIAGRead returned %d", rc);
363 iagp = (struct iag *) mp->data;
365 /* determine inode extent that holds the disk inode */
366 ino = ip->i_ino & (INOSPERIAG - 1);
367 extno = ino >> L2INOSPEREXT;
369 if ((lengthPXD(&iagp->inoext[extno]) != imap->im_nbperiext) ||
370 (addressPXD(&iagp->inoext[extno]) == 0)) {
371 release_metapage(mp);
375 /* get disk block number of the page within the inode extent
376 * that holds the disk inode.
378 blkno = INOPBLK(&iagp->inoext[extno], ino, sbi->l2nbperpage);
380 /* get the ag for the iag */
381 agstart = le64_to_cpu(iagp->agstart);
383 release_metapage(mp);
385 rel_inode = (ino & (INOSPERPAGE - 1));
386 pageno = blkno >> sbi->l2nbperpage;
388 if ((block_offset = ((u32) blkno & (sbi->nbperpage - 1)))) {
390 * OS/2 didn't always align inode extents on page boundaries
393 (sbi->nbperpage - block_offset) << sbi->l2niperblk;
395 if (rel_inode < inodes_left)
396 rel_inode += block_offset << sbi->l2niperblk;
399 rel_inode -= inodes_left;
403 /* read the page of disk inode */
404 mp = read_metapage(ipimap, pageno << sbi->l2nbperpage, PSIZE, 1);
406 jfs_err("diRead: read_metapage failed");
410 /* locate the the disk inode requested */
411 dp = (struct dinode *) mp->data;
414 if (ip->i_ino != le32_to_cpu(dp->di_number)) {
415 jfs_error(ip->i_sb, "diRead: i_ino != di_number");
417 } else if (le32_to_cpu(dp->di_nlink) == 0)
420 /* copy the disk inode to the in-memory inode */
421 rc = copy_from_dinode(dp, ip);
423 release_metapage(mp);
425 /* set the ag for the inode */
426 JFS_IP(ip)->agno = BLKTOAG(agstart, sbi);
427 JFS_IP(ip)->active_ag = -1;
434 * NAME: diReadSpecial()
436 * FUNCTION: initialize a 'special' inode from disk.
438 * this routines handles aggregate level inodes. The
439 * inode cache cannot differentiate between the
440 * aggregate inodes and the filesystem inodes, so we
441 * handle these here. We don't actually use the aggregate
442 * inode map, since these inodes are at a fixed location
443 * and in some cases the aggregate inode map isn't initialized
447 * sb - filesystem superblock
448 * inum - aggregate inode number
449 * secondary - 1 if secondary aggregate inode table
452 * new inode - success
455 struct inode *diReadSpecial(struct super_block *sb, ino_t inum, int secondary)
457 struct jfs_sb_info *sbi = JFS_SBI(sb);
465 jfs_err("diReadSpecial: new_inode returned NULL!");
470 address = addressPXD(&sbi->ait2) >> sbi->l2nbperpage;
471 JFS_IP(ip)->ipimap = sbi->ipaimap2;
473 address = AITBL_OFF >> L2PSIZE;
474 JFS_IP(ip)->ipimap = sbi->ipaimap;
477 ASSERT(inum < INOSPEREXT);
481 address += inum >> 3; /* 8 inodes per 4K page */
483 /* read the page of fixed disk inode (AIT) in raw mode */
484 mp = read_metapage(ip, address << sbi->l2nbperpage, PSIZE, 1);
486 ip->i_nlink = 1; /* Don't want iput() deleting it */
491 /* get the pointer to the disk inode of interest */
492 dp = (struct dinode *) (mp->data);
493 dp += inum % 8; /* 8 inodes per 4K page */
495 /* copy on-disk inode to in-memory inode */
496 if ((copy_from_dinode(dp, ip)) != 0) {
497 /* handle bad return by returning NULL for ip */
498 ip->i_nlink = 1; /* Don't want iput() deleting it */
500 /* release the page */
501 release_metapage(mp);
506 ip->i_mapping->a_ops = &jfs_metapage_aops;
507 mapping_set_gfp_mask(ip->i_mapping, GFP_NOFS);
509 /* Allocations to metadata inodes should not affect quotas */
510 ip->i_flags |= S_NOQUOTA;
512 if ((inum == FILESYSTEM_I) && (JFS_IP(ip)->ipimap == sbi->ipaimap)) {
513 sbi->gengen = le32_to_cpu(dp->di_gengen);
514 sbi->inostamp = le32_to_cpu(dp->di_inostamp);
517 /* release the page */
518 release_metapage(mp);
524 * NAME: diWriteSpecial()
526 * FUNCTION: Write the special inode to disk
530 * secondary - 1 if secondary aggregate inode table
532 * RETURN VALUES: none
535 void diWriteSpecial(struct inode *ip, int secondary)
537 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
540 ino_t inum = ip->i_ino;
543 ip->i_state &= ~I_DIRTY;
546 address = addressPXD(&sbi->ait2) >> sbi->l2nbperpage;
548 address = AITBL_OFF >> L2PSIZE;
550 ASSERT(inum < INOSPEREXT);
552 address += inum >> 3; /* 8 inodes per 4K page */
554 /* read the page of fixed disk inode (AIT) in raw mode */
555 mp = read_metapage(ip, address << sbi->l2nbperpage, PSIZE, 1);
557 jfs_err("diWriteSpecial: failed to read aggregate inode "
562 /* get the pointer to the disk inode of interest */
563 dp = (struct dinode *) (mp->data);
564 dp += inum % 8; /* 8 inodes per 4K page */
566 /* copy on-disk inode to in-memory inode */
567 copy_to_dinode(dp, ip);
568 memcpy(&dp->di_xtroot, &JFS_IP(ip)->i_xtroot, 288);
570 if (inum == FILESYSTEM_I)
571 dp->di_gengen = cpu_to_le32(sbi->gengen);
578 * NAME: diFreeSpecial()
580 * FUNCTION: Free allocated space for special inode
582 void diFreeSpecial(struct inode *ip)
585 jfs_err("diFreeSpecial called with NULL ip!");
588 filemap_fdatawrite(ip->i_mapping);
589 filemap_fdatawait(ip->i_mapping);
590 truncate_inode_pages(ip->i_mapping, 0);
599 * FUNCTION: write the on-disk inode portion of the in-memory inode
600 * to its corresponding on-disk inode.
602 * on entry, the specifed incore inode should itself
603 * specify the disk inode number corresponding to the
604 * incore inode (i.e. i_number should be initialized).
606 * the inode contains the inode extent address for the disk
607 * inode. with the inode extent address in hand, the
608 * page of the extent that contains the disk inode is
609 * read and the disk inode portion of the incore inode
610 * is copied to the disk inode.
613 * tid - transacation id
614 * ip - pointer to incore inode to be written to the inode extent.
620 int diWrite(tid_t tid, struct inode *ip)
622 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
623 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
634 struct inode *ipimap;
637 struct tlock *ditlck, *tlck;
638 struct linelock *dilinelock, *ilinelock;
642 ipimap = jfs_ip->ipimap;
644 ino = ip->i_ino & (INOSPERIAG - 1);
646 if (!addressPXD(&(jfs_ip->ixpxd)) ||
647 (lengthPXD(&(jfs_ip->ixpxd)) !=
648 JFS_IP(ipimap)->i_imap->im_nbperiext)) {
649 jfs_error(ip->i_sb, "diWrite: ixpxd invalid");
654 * read the page of disk inode containing the specified inode:
656 /* compute the block address of the page */
657 blkno = INOPBLK(&(jfs_ip->ixpxd), ino, sbi->l2nbperpage);
659 rel_inode = (ino & (INOSPERPAGE - 1));
660 pageno = blkno >> sbi->l2nbperpage;
662 if ((block_offset = ((u32) blkno & (sbi->nbperpage - 1)))) {
664 * OS/2 didn't always align inode extents on page boundaries
667 (sbi->nbperpage - block_offset) << sbi->l2niperblk;
669 if (rel_inode < inodes_left)
670 rel_inode += block_offset << sbi->l2niperblk;
673 rel_inode -= inodes_left;
676 /* read the page of disk inode */
678 mp = read_metapage(ipimap, pageno << sbi->l2nbperpage, PSIZE, 1);
682 /* get the pointer to the disk inode */
683 dp = (struct dinode *) mp->data;
686 dioffset = (ino & (INOSPERPAGE - 1)) << L2DISIZE;
689 * acquire transaction lock on the on-disk inode;
690 * N.B. tlock is acquired on ipimap not ip;
693 txLock(tid, ipimap, mp, tlckINODE | tlckENTRY)) == NULL)
695 dilinelock = (struct linelock *) & ditlck->lock;
698 * copy btree root from in-memory inode to on-disk inode
700 * (tlock is taken from inline B+-tree root in in-memory
701 * inode when the B+-tree root is updated, which is pointed
702 * by jfs_ip->blid as well as being on tx tlock list)
704 * further processing of btree root is based on the copy
705 * in in-memory inode, where txLog() will log from, and,
706 * for xtree root, txUpdateMap() will update map and reset
710 if (S_ISDIR(ip->i_mode) && (lid = jfs_ip->xtlid)) {
712 * This is the special xtree inside the directory for storing
713 * the directory table
719 tlck = lid_to_tlock(lid);
720 assert(tlck->type & tlckXTREE);
721 tlck->type |= tlckBTROOT;
723 ilinelock = (struct linelock *) & tlck->lock;
726 * copy xtree root from inode to dinode:
728 p = &jfs_ip->i_xtroot;
729 xp = (xtpage_t *) &dp->di_dirtable;
731 for (n = 0; n < ilinelock->index; n++, lv++) {
732 memcpy(&xp->xad[lv->offset], &p->xad[lv->offset],
733 lv->length << L2XTSLOTSIZE);
736 /* reset on-disk (metadata page) xtree XAD_NEW bit */
737 xad = &xp->xad[XTENTRYSTART];
738 for (n = XTENTRYSTART;
739 n < le16_to_cpu(xp->header.nextindex); n++, xad++)
740 if (xad->flag & (XAD_NEW | XAD_EXTENDED))
741 xad->flag &= ~(XAD_NEW | XAD_EXTENDED);
744 if ((lid = jfs_ip->blid) == 0)
748 tlck = lid_to_tlock(lid);
750 tlck->type |= tlckBTROOT;
752 ilinelock = (struct linelock *) & tlck->lock;
755 * regular file: 16 byte (XAD slot) granularity
757 if (type & tlckXTREE) {
762 * copy xtree root from inode to dinode:
764 p = &jfs_ip->i_xtroot;
767 for (n = 0; n < ilinelock->index; n++, lv++) {
768 memcpy(&xp->xad[lv->offset], &p->xad[lv->offset],
769 lv->length << L2XTSLOTSIZE);
772 /* reset on-disk (metadata page) xtree XAD_NEW bit */
773 xad = &xp->xad[XTENTRYSTART];
774 for (n = XTENTRYSTART;
775 n < le16_to_cpu(xp->header.nextindex); n++, xad++)
776 if (xad->flag & (XAD_NEW | XAD_EXTENDED))
777 xad->flag &= ~(XAD_NEW | XAD_EXTENDED);
780 * directory: 32 byte (directory entry slot) granularity
782 else if (type & tlckDTREE) {
786 * copy dtree root from inode to dinode:
788 p = (dtpage_t *) &jfs_ip->i_dtroot;
789 xp = (dtpage_t *) & dp->di_dtroot;
791 for (n = 0; n < ilinelock->index; n++, lv++) {
792 memcpy(&xp->slot[lv->offset], &p->slot[lv->offset],
793 lv->length << L2DTSLOTSIZE);
796 jfs_err("diWrite: UFO tlock");
801 * copy inline symlink from in-memory inode to on-disk inode
803 if (S_ISLNK(ip->i_mode) && ip->i_size < IDATASIZE) {
804 lv = & dilinelock->lv[dilinelock->index];
805 lv->offset = (dioffset + 2 * 128) >> L2INODESLOTSIZE;
807 memcpy(&dp->di_fastsymlink, jfs_ip->i_inline, IDATASIZE);
811 * copy inline data from in-memory inode to on-disk inode:
812 * 128 byte slot granularity
814 if (test_cflag(COMMIT_Inlineea, ip)) {
815 lv = & dilinelock->lv[dilinelock->index];
816 lv->offset = (dioffset + 3 * 128) >> L2INODESLOTSIZE;
818 memcpy(&dp->di_inlineea, jfs_ip->i_inline_ea, INODESLOTSIZE);
821 clear_cflag(COMMIT_Inlineea, ip);
825 * lock/copy inode base: 128 byte slot granularity
828 lv = & dilinelock->lv[dilinelock->index];
829 lv->offset = dioffset >> L2INODESLOTSIZE;
830 copy_to_dinode(dp, ip);
831 if (test_and_clear_cflag(COMMIT_Dirtable, ip)) {
833 memcpy(&dp->di_dirtable, &jfs_ip->i_dirtable, 96);
840 * We aren't logging changes to the DASD used in directory inodes,
841 * but we need to write them to disk. If we don't unmount cleanly,
842 * mount will recalculate the DASD used.
844 if (S_ISDIR(ip->i_mode)
845 && (ip->i_ipmnt->i_mntflag & JFS_DASD_ENABLED))
846 memcpy(&dp->di_DASD, &ip->i_DASD, sizeof(struct dasd));
847 #endif /* _JFS_FASTDASD */
849 /* release the buffer holding the updated on-disk inode.
850 * the buffer will be later written by commit processing.
861 * FUNCTION: free a specified inode from the inode working map
862 * for a fileset or aggregate.
864 * if the inode to be freed represents the first (only)
865 * free inode within the iag, the iag will be placed on
866 * the ag free inode list.
868 * freeing the inode will cause the inode extent to be
869 * freed if the inode is the only allocated inode within
870 * the extent. in this case all the disk resource backing
871 * up the inode extent will be freed. in addition, the iag
872 * will be placed on the ag extent free list if the extent
873 * is the first free extent in the iag. if freeing the
874 * extent also means that no free inodes will exist for
875 * the iag, the iag will also be removed from the ag free
878 * the iag describing the inode will be freed if the extent
879 * is to be freed and it is the only backed extent within
880 * the iag. in this case, the iag will be removed from the
881 * ag free extent list and ag free inode list and placed on
882 * the inode map's free iag list.
884 * a careful update approach is used to provide consistency
885 * in the face of updates to multiple buffers. under this
886 * approach, all required buffers are obtained before making
887 * any updates and are held until all updates are complete.
890 * ip - inode to be freed.
896 int diFree(struct inode *ip)
899 ino_t inum = ip->i_ino;
900 struct iag *iagp, *aiagp, *biagp, *ciagp, *diagp;
901 struct metapage *mp, *amp, *bmp, *cmp, *dmp;
902 int iagno, ino, extno, bitno, sword, agno;
905 struct inode *ipimap = JFS_SBI(ip->i_sb)->ipimap;
906 struct inomap *imap = JFS_IP(ipimap)->i_imap;
909 struct inode *iplist[3];
911 struct pxd_lock *pxdlock;
914 * This is just to suppress compiler warnings. The same logic that
915 * references these variables is used to initialize them.
917 aiagp = biagp = ciagp = diagp = NULL;
919 /* get the iag number containing the inode.
921 iagno = INOTOIAG(inum);
923 /* make sure that the iag is contained within
926 if (iagno >= imap->im_nextiag) {
927 dump_mem("imap", imap, 32);
929 "diFree: inum = %d, iagno = %d, nextiag = %d",
930 (uint) inum, iagno, imap->im_nextiag);
934 /* get the allocation group for this ino.
936 agno = JFS_IP(ip)->agno;
938 /* Lock the AG specific inode map information
942 /* Obtain read lock in imap inode. Don't release it until we have
943 * read all of the IAG's that we are going to.
949 if ((rc = diIAGRead(imap, iagno, &mp))) {
950 IREAD_UNLOCK(ipimap);
951 AG_UNLOCK(imap, agno);
954 iagp = (struct iag *) mp->data;
956 /* get the inode number and extent number of the inode within
957 * the iag and the inode number within the extent.
959 ino = inum & (INOSPERIAG - 1);
960 extno = ino >> L2INOSPEREXT;
961 bitno = ino & (INOSPEREXT - 1);
962 mask = HIGHORDER >> bitno;
964 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
966 "diFree: wmap shows inode already free");
969 if (!addressPXD(&iagp->inoext[extno])) {
970 release_metapage(mp);
971 IREAD_UNLOCK(ipimap);
972 AG_UNLOCK(imap, agno);
973 jfs_error(ip->i_sb, "diFree: invalid inoext");
977 /* compute the bitmap for the extent reflecting the freed inode.
979 bitmap = le32_to_cpu(iagp->wmap[extno]) & ~mask;
981 if (imap->im_agctl[agno].numfree > imap->im_agctl[agno].numinos) {
982 release_metapage(mp);
983 IREAD_UNLOCK(ipimap);
984 AG_UNLOCK(imap, agno);
985 jfs_error(ip->i_sb, "diFree: numfree > numinos");
989 * inode extent still has some inodes or below low water mark:
990 * keep the inode extent;
993 imap->im_agctl[agno].numfree < 96 ||
994 (imap->im_agctl[agno].numfree < 288 &&
995 (((imap->im_agctl[agno].numfree * 100) /
996 imap->im_agctl[agno].numinos) <= 25))) {
997 /* if the iag currently has no free inodes (i.e.,
998 * the inode being freed is the first free inode of iag),
999 * insert the iag at head of the inode free list for the ag.
1001 if (iagp->nfreeinos == 0) {
1002 /* check if there are any iags on the ag inode
1003 * free list. if so, read the first one so that
1004 * we can link the current iag onto the list at
1007 if ((fwd = imap->im_agctl[agno].inofree) >= 0) {
1008 /* read the iag that currently is the head
1011 if ((rc = diIAGRead(imap, fwd, &))) {
1012 IREAD_UNLOCK(ipimap);
1013 AG_UNLOCK(imap, agno);
1014 release_metapage(mp);
1017 aiagp = (struct iag *) amp->data;
1019 /* make current head point back to the iag.
1021 aiagp->inofreeback = cpu_to_le32(iagno);
1023 write_metapage(amp);
1026 /* iag points forward to current head and iag
1027 * becomes the new head of the list.
1030 cpu_to_le32(imap->im_agctl[agno].inofree);
1031 iagp->inofreeback = cpu_to_le32(-1);
1032 imap->im_agctl[agno].inofree = iagno;
1034 IREAD_UNLOCK(ipimap);
1036 /* update the free inode summary map for the extent if
1037 * freeing the inode means the extent will now have free
1038 * inodes (i.e., the inode being freed is the first free
1041 if (iagp->wmap[extno] == cpu_to_le32(ONES)) {
1042 sword = extno >> L2EXTSPERSUM;
1043 bitno = extno & (EXTSPERSUM - 1);
1044 iagp->inosmap[sword] &=
1045 cpu_to_le32(~(HIGHORDER >> bitno));
1048 /* update the bitmap.
1050 iagp->wmap[extno] = cpu_to_le32(bitmap);
1051 DBG_DIFREE(imap, inum);
1053 /* update the free inode counts at the iag, ag and
1057 cpu_to_le32(le32_to_cpu(iagp->nfreeinos) + 1);
1058 imap->im_agctl[agno].numfree += 1;
1059 atomic_inc(&imap->im_numfree);
1061 /* release the AG inode map lock
1063 AG_UNLOCK(imap, agno);
1073 * inode extent has become free and above low water mark:
1074 * free the inode extent;
1078 * prepare to update iag list(s) (careful update step 1)
1080 amp = bmp = cmp = dmp = NULL;
1083 /* check if the iag currently has no free extents. if so,
1084 * it will be placed on the head of the ag extent free list.
1086 if (iagp->nfreeexts == 0) {
1087 /* check if the ag extent free list has any iags.
1088 * if so, read the iag at the head of the list now.
1089 * this (head) iag will be updated later to reflect
1090 * the addition of the current iag at the head of
1093 if ((fwd = imap->im_agctl[agno].extfree) >= 0) {
1094 if ((rc = diIAGRead(imap, fwd, &)))
1096 aiagp = (struct iag *) amp->data;
1099 /* iag has free extents. check if the addition of a free
1100 * extent will cause all extents to be free within this
1101 * iag. if so, the iag will be removed from the ag extent
1102 * free list and placed on the inode map's free iag list.
1104 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG - 1)) {
1105 /* in preparation for removing the iag from the
1106 * ag extent free list, read the iags preceeding
1107 * and following the iag on the ag extent free
1110 if ((fwd = le32_to_cpu(iagp->extfreefwd)) >= 0) {
1111 if ((rc = diIAGRead(imap, fwd, &)))
1113 aiagp = (struct iag *) amp->data;
1116 if ((back = le32_to_cpu(iagp->extfreeback)) >= 0) {
1117 if ((rc = diIAGRead(imap, back, &bmp)))
1119 biagp = (struct iag *) bmp->data;
1124 /* remove the iag from the ag inode free list if freeing
1125 * this extent cause the iag to have no free inodes.
1127 if (iagp->nfreeinos == cpu_to_le32(INOSPEREXT - 1)) {
1128 int inofreeback = le32_to_cpu(iagp->inofreeback);
1129 int inofreefwd = le32_to_cpu(iagp->inofreefwd);
1131 /* in preparation for removing the iag from the
1132 * ag inode free list, read the iags preceeding
1133 * and following the iag on the ag inode free
1134 * list. before reading these iags, we must make
1135 * sure that we already don't have them in hand
1136 * from up above, since re-reading an iag (buffer)
1137 * we are currently holding would cause a deadlock.
1139 if (inofreefwd >= 0) {
1141 if (inofreefwd == fwd)
1142 ciagp = (struct iag *) amp->data;
1143 else if (inofreefwd == back)
1144 ciagp = (struct iag *) bmp->data;
1147 diIAGRead(imap, inofreefwd, &cmp)))
1149 ciagp = (struct iag *) cmp->data;
1151 assert(ciagp != NULL);
1154 if (inofreeback >= 0) {
1155 if (inofreeback == fwd)
1156 diagp = (struct iag *) amp->data;
1157 else if (inofreeback == back)
1158 diagp = (struct iag *) bmp->data;
1161 diIAGRead(imap, inofreeback, &dmp)))
1163 diagp = (struct iag *) dmp->data;
1165 assert(diagp != NULL);
1169 IREAD_UNLOCK(ipimap);
1172 * invalidate any page of the inode extent freed from buffer cache;
1174 freepxd = iagp->inoext[extno];
1175 invalidate_pxd_metapages(ip, freepxd);
1178 * update iag list(s) (careful update step 2)
1180 /* add the iag to the ag extent free list if this is the
1181 * first free extent for the iag.
1183 if (iagp->nfreeexts == 0) {
1185 aiagp->extfreeback = cpu_to_le32(iagno);
1188 cpu_to_le32(imap->im_agctl[agno].extfree);
1189 iagp->extfreeback = cpu_to_le32(-1);
1190 imap->im_agctl[agno].extfree = iagno;
1192 /* remove the iag from the ag extent list if all extents
1193 * are now free and place it on the inode map iag free list.
1195 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG - 1)) {
1197 aiagp->extfreeback = iagp->extfreeback;
1200 biagp->extfreefwd = iagp->extfreefwd;
1202 imap->im_agctl[agno].extfree =
1203 le32_to_cpu(iagp->extfreefwd);
1205 iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
1208 iagp->iagfree = cpu_to_le32(imap->im_freeiag);
1209 imap->im_freeiag = iagno;
1210 IAGFREE_UNLOCK(imap);
1214 /* remove the iag from the ag inode free list if freeing
1215 * this extent causes the iag to have no free inodes.
1217 if (iagp->nfreeinos == cpu_to_le32(INOSPEREXT - 1)) {
1218 if ((int) le32_to_cpu(iagp->inofreefwd) >= 0)
1219 ciagp->inofreeback = iagp->inofreeback;
1221 if ((int) le32_to_cpu(iagp->inofreeback) >= 0)
1222 diagp->inofreefwd = iagp->inofreefwd;
1224 imap->im_agctl[agno].inofree =
1225 le32_to_cpu(iagp->inofreefwd);
1227 iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
1230 /* update the inode extent address and working map
1231 * to reflect the free extent.
1232 * the permanent map should have been updated already
1233 * for the inode being freed.
1235 if (iagp->pmap[extno] != 0) {
1236 jfs_error(ip->i_sb, "diFree: the pmap does not show inode free");
1238 iagp->wmap[extno] = 0;
1239 DBG_DIFREE(imap, inum);
1240 PXDlength(&iagp->inoext[extno], 0);
1241 PXDaddress(&iagp->inoext[extno], 0);
1243 /* update the free extent and free inode summary maps
1244 * to reflect the freed extent.
1245 * the inode summary map is marked to indicate no inodes
1246 * available for the freed extent.
1248 sword = extno >> L2EXTSPERSUM;
1249 bitno = extno & (EXTSPERSUM - 1);
1250 mask = HIGHORDER >> bitno;
1251 iagp->inosmap[sword] |= cpu_to_le32(mask);
1252 iagp->extsmap[sword] &= cpu_to_le32(~mask);
1254 /* update the number of free inodes and number of free extents
1257 iagp->nfreeinos = cpu_to_le32(le32_to_cpu(iagp->nfreeinos) -
1259 iagp->nfreeexts = cpu_to_le32(le32_to_cpu(iagp->nfreeexts) + 1);
1261 /* update the number of free inodes and backed inodes
1262 * at the ag and inode map level.
1264 imap->im_agctl[agno].numfree -= (INOSPEREXT - 1);
1265 imap->im_agctl[agno].numinos -= INOSPEREXT;
1266 atomic_sub(INOSPEREXT - 1, &imap->im_numfree);
1267 atomic_sub(INOSPEREXT, &imap->im_numinos);
1270 write_metapage(amp);
1272 write_metapage(bmp);
1274 write_metapage(cmp);
1276 write_metapage(dmp);
1279 * start transaction to update block allocation map
1280 * for the inode extent freed;
1282 * N.B. AG_LOCK is released and iag will be released below, and
1283 * other thread may allocate inode from/reusing the ixad freed
1284 * BUT with new/different backing inode extent from the extent
1285 * to be freed by the transaction;
1287 tid = txBegin(ipimap->i_sb, COMMIT_FORCE);
1288 down(&JFS_IP(ipimap)->commit_sem);
1290 /* acquire tlock of the iag page of the freed ixad
1291 * to force the page NOHOMEOK (even though no data is
1292 * logged from the iag page) until NOREDOPAGE|FREEXTENT log
1293 * for the free of the extent is committed;
1294 * write FREEXTENT|NOREDOPAGE log record
1295 * N.B. linelock is overlaid as freed extent descriptor;
1297 tlck = txLock(tid, ipimap, mp, tlckINODE | tlckFREE);
1298 pxdlock = (struct pxd_lock *) & tlck->lock;
1299 pxdlock->flag = mlckFREEPXD;
1300 pxdlock->pxd = freepxd;
1308 * logredo needs the IAG number and IAG extent index in order
1309 * to ensure that the IMap is consistent. The least disruptive
1310 * way to pass these values through to the transaction manager
1311 * is in the iplist array.
1313 * It's not pretty, but it works.
1315 iplist[1] = (struct inode *) (size_t)iagno;
1316 iplist[2] = (struct inode *) (size_t)extno;
1318 rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
1321 up(&JFS_IP(ipimap)->commit_sem);
1323 /* unlock the AG inode map information */
1324 AG_UNLOCK(imap, agno);
1329 IREAD_UNLOCK(ipimap);
1332 release_metapage(amp);
1334 release_metapage(bmp);
1336 release_metapage(cmp);
1338 release_metapage(dmp);
1340 AG_UNLOCK(imap, agno);
1342 release_metapage(mp);
1348 * There are several places in the diAlloc* routines where we initialize
1352 diInitInode(struct inode *ip, int iagno, int ino, int extno, struct iag * iagp)
1354 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
1355 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
1357 ip->i_ino = (iagno << L2INOSPERIAG) + ino;
1358 DBG_DIALLOC(JFS_IP(ipimap)->i_imap, ip->i_ino);
1359 jfs_ip->ixpxd = iagp->inoext[extno];
1360 jfs_ip->agno = BLKTOAG(le64_to_cpu(iagp->agstart), sbi);
1361 jfs_ip->active_ag = -1;
1366 * NAME: diAlloc(pip,dir,ip)
1368 * FUNCTION: allocate a disk inode from the inode working map
1369 * for a fileset or aggregate.
1372 * pip - pointer to incore inode for the parent inode.
1373 * dir - TRUE if the new disk inode is for a directory.
1374 * ip - pointer to a new inode
1378 * -ENOSPC - insufficient disk resources.
1381 int diAlloc(struct inode *pip, boolean_t dir, struct inode *ip)
1383 int rc, ino, iagno, addext, extno, bitno, sword;
1384 int nwords, rem, i, agno;
1385 u32 mask, inosmap, extsmap;
1386 struct inode *ipimap;
1387 struct metapage *mp;
1390 struct inomap *imap;
1392 /* get the pointers to the inode map inode and the
1393 * corresponding imap control structure.
1395 ipimap = JFS_SBI(pip->i_sb)->ipimap;
1396 imap = JFS_IP(ipimap)->i_imap;
1397 JFS_IP(ip)->ipimap = ipimap;
1398 JFS_IP(ip)->fileset = FILESYSTEM_I;
1400 /* for a directory, the allocation policy is to start
1401 * at the ag level using the preferred ag.
1404 agno = dbNextAG(JFS_SBI(pip->i_sb)->ipbmap);
1405 AG_LOCK(imap, agno);
1409 /* for files, the policy starts off by trying to allocate from
1410 * the same iag containing the parent disk inode:
1411 * try to allocate the new disk inode close to the parent disk
1412 * inode, using parent disk inode number + 1 as the allocation
1413 * hint. (we use a left-to-right policy to attempt to avoid
1414 * moving backward on the disk.) compute the hint within the
1415 * file system and the iag.
1418 /* get the ag number of this iag */
1419 agno = JFS_IP(pip)->agno;
1421 if (atomic_read(&JFS_SBI(pip->i_sb)->bmap->db_active[agno])) {
1423 * There is an open file actively growing. We want to
1424 * allocate new inodes from a different ag to avoid
1425 * fragmentation problems.
1427 agno = dbNextAG(JFS_SBI(pip->i_sb)->ipbmap);
1428 AG_LOCK(imap, agno);
1432 inum = pip->i_ino + 1;
1433 ino = inum & (INOSPERIAG - 1);
1435 /* back off the the hint if it is outside of the iag */
1439 /* lock the AG inode map information */
1440 AG_LOCK(imap, agno);
1442 /* Get read lock on imap inode */
1445 /* get the iag number and read the iag */
1446 iagno = INOTOIAG(inum);
1447 if ((rc = diIAGRead(imap, iagno, &mp))) {
1448 IREAD_UNLOCK(ipimap);
1449 AG_UNLOCK(imap, agno);
1452 iagp = (struct iag *) mp->data;
1454 /* determine if new inode extent is allowed to be added to the iag.
1455 * new inode extent can be added to the iag if the ag
1456 * has less than 32 free disk inodes and the iag has free extents.
1458 addext = (imap->im_agctl[agno].numfree < 32 && iagp->nfreeexts);
1461 * try to allocate from the IAG
1463 /* check if the inode may be allocated from the iag
1464 * (i.e. the inode has free inodes or new extent can be added).
1466 if (iagp->nfreeinos || addext) {
1467 /* determine the extent number of the hint.
1469 extno = ino >> L2INOSPEREXT;
1471 /* check if the extent containing the hint has backed
1472 * inodes. if so, try to allocate within this extent.
1474 if (addressPXD(&iagp->inoext[extno])) {
1475 bitno = ino & (INOSPEREXT - 1);
1477 diFindFree(le32_to_cpu(iagp->wmap[extno]),
1480 ino = (extno << L2INOSPEREXT) + bitno;
1482 /* a free inode (bit) was found within this
1483 * extent, so allocate it.
1485 rc = diAllocBit(imap, iagp, ino);
1486 IREAD_UNLOCK(ipimap);
1490 /* set the results of the allocation
1491 * and write the iag.
1493 diInitInode(ip, iagno, ino, extno,
1495 mark_metapage_dirty(mp);
1497 release_metapage(mp);
1499 /* free the AG lock and return.
1501 AG_UNLOCK(imap, agno);
1508 EXTSPERIAG - 1) ? 0 : extno + 1;
1512 * no free inodes within the extent containing the hint.
1514 * try to allocate from the backed extents following
1515 * hint or, if appropriate (i.e. addext is true), allocate
1516 * an extent of free inodes at or following the extent
1517 * containing the hint.
1519 * the free inode and free extent summary maps are used
1520 * here, so determine the starting summary map position
1521 * and the number of words we'll have to examine. again,
1522 * the approach is to allocate following the hint, so we
1523 * might have to initially ignore prior bits of the summary
1524 * map that represent extents prior to the extent containing
1525 * the hint and later revisit these bits.
1527 bitno = extno & (EXTSPERSUM - 1);
1528 nwords = (bitno == 0) ? SMAPSZ : SMAPSZ + 1;
1529 sword = extno >> L2EXTSPERSUM;
1531 /* mask any prior bits for the starting words of the
1534 mask = ONES << (EXTSPERSUM - bitno);
1535 inosmap = le32_to_cpu(iagp->inosmap[sword]) | mask;
1536 extsmap = le32_to_cpu(iagp->extsmap[sword]) | mask;
1538 /* scan the free inode and free extent summary maps for
1541 for (i = 0; i < nwords; i++) {
1542 /* check if this word of the free inode summary
1543 * map describes an extent with free inodes.
1546 /* an extent with free inodes has been
1547 * found. determine the extent number
1548 * and the inode number within the extent.
1550 rem = diFindFree(inosmap, 0);
1551 extno = (sword << L2EXTSPERSUM) + rem;
1552 rem = diFindFree(le32_to_cpu(iagp->wmap[extno]),
1554 if (rem >= INOSPEREXT) {
1555 IREAD_UNLOCK(ipimap);
1556 release_metapage(mp);
1557 AG_UNLOCK(imap, agno);
1559 "diAlloc: can't find free bit "
1564 /* determine the inode number within the
1565 * iag and allocate the inode from the
1568 ino = (extno << L2INOSPEREXT) + rem;
1569 rc = diAllocBit(imap, iagp, ino);
1570 IREAD_UNLOCK(ipimap);
1574 /* set the results of the allocation
1575 * and write the iag.
1577 diInitInode(ip, iagno, ino, extno,
1579 mark_metapage_dirty(mp);
1581 release_metapage(mp);
1583 /* free the AG lock and return.
1585 AG_UNLOCK(imap, agno);
1590 /* check if we may allocate an extent of free
1591 * inodes and whether this word of the free
1592 * extents summary map describes a free extent.
1594 if (addext && ~extsmap) {
1595 /* a free extent has been found. determine
1596 * the extent number.
1598 rem = diFindFree(extsmap, 0);
1599 extno = (sword << L2EXTSPERSUM) + rem;
1601 /* allocate an extent of free inodes.
1603 if ((rc = diNewExt(imap, iagp, extno))) {
1604 /* if there is no disk space for a
1605 * new extent, try to allocate the
1606 * disk inode from somewhere else.
1613 /* set the results of the allocation
1614 * and write the iag.
1616 diInitInode(ip, iagno,
1617 extno << L2INOSPEREXT,
1619 mark_metapage_dirty(mp);
1621 release_metapage(mp);
1622 /* free the imap inode & the AG lock & return.
1624 IREAD_UNLOCK(ipimap);
1625 AG_UNLOCK(imap, agno);
1629 /* move on to the next set of summary map words.
1631 sword = (sword == SMAPSZ - 1) ? 0 : sword + 1;
1632 inosmap = le32_to_cpu(iagp->inosmap[sword]);
1633 extsmap = le32_to_cpu(iagp->extsmap[sword]);
1636 /* unlock imap inode */
1637 IREAD_UNLOCK(ipimap);
1639 /* nothing doing in this iag, so release it. */
1640 release_metapage(mp);
1644 * try to allocate anywhere within the same AG as the parent inode.
1646 rc = diAllocAG(imap, agno, dir, ip);
1648 AG_UNLOCK(imap, agno);
1654 * try to allocate in any AG.
1656 return (diAllocAny(imap, agno, dir, ip));
1661 * NAME: diAllocAG(imap,agno,dir,ip)
1663 * FUNCTION: allocate a disk inode from the allocation group.
1665 * this routine first determines if a new extent of free
1666 * inodes should be added for the allocation group, with
1667 * the current request satisfied from this extent. if this
1668 * is the case, an attempt will be made to do just that. if
1669 * this attempt fails or it has been determined that a new
1670 * extent should not be added, an attempt is made to satisfy
1671 * the request by allocating an existing (backed) free inode
1672 * from the allocation group.
1674 * PRE CONDITION: Already have the AG lock for this AG.
1677 * imap - pointer to inode map control structure.
1678 * agno - allocation group to allocate from.
1679 * dir - TRUE if the new disk inode is for a directory.
1680 * ip - pointer to the new inode to be filled in on successful return
1681 * with the disk inode number allocated, its extent address
1682 * and the start of the ag.
1686 * -ENOSPC - insufficient disk resources.
1690 diAllocAG(struct inomap * imap, int agno, boolean_t dir, struct inode *ip)
1692 int rc, addext, numfree, numinos;
1694 /* get the number of free and the number of backed disk
1695 * inodes currently within the ag.
1697 numfree = imap->im_agctl[agno].numfree;
1698 numinos = imap->im_agctl[agno].numinos;
1700 if (numfree > numinos) {
1701 jfs_error(ip->i_sb, "diAllocAG: numfree > numinos");
1705 /* determine if we should allocate a new extent of free inodes
1706 * within the ag: for directory inodes, add a new extent
1707 * if there are a small number of free inodes or number of free
1708 * inodes is a small percentage of the number of backed inodes.
1711 addext = (numfree < 64 ||
1713 && ((numfree * 100) / numinos) <= 20));
1715 addext = (numfree == 0);
1718 * try to allocate a new extent of free inodes.
1721 /* if free space is not avaliable for this new extent, try
1722 * below to allocate a free and existing (already backed)
1723 * inode from the ag.
1725 if ((rc = diAllocExt(imap, agno, ip)) != -ENOSPC)
1730 * try to allocate an existing free inode from the ag.
1732 return (diAllocIno(imap, agno, ip));
1737 * NAME: diAllocAny(imap,agno,dir,iap)
1739 * FUNCTION: allocate a disk inode from any other allocation group.
1741 * this routine is called when an allocation attempt within
1742 * the primary allocation group has failed. if attempts to
1743 * allocate an inode from any allocation group other than the
1744 * specified primary group.
1747 * imap - pointer to inode map control structure.
1748 * agno - primary allocation group (to avoid).
1749 * dir - TRUE if the new disk inode is for a directory.
1750 * ip - pointer to a new inode to be filled in on successful return
1751 * with the disk inode number allocated, its extent address
1752 * and the start of the ag.
1756 * -ENOSPC - insufficient disk resources.
1760 diAllocAny(struct inomap * imap, int agno, boolean_t dir, struct inode *ip)
1763 int maxag = JFS_SBI(imap->im_ipimap->i_sb)->bmap->db_maxag;
1766 /* try to allocate from the ags following agno up to
1767 * the maximum ag number.
1769 for (ag = agno + 1; ag <= maxag; ag++) {
1772 rc = diAllocAG(imap, ag, dir, ip);
1774 AG_UNLOCK(imap, ag);
1780 /* try to allocate from the ags in front of agno.
1782 for (ag = 0; ag < agno; ag++) {
1785 rc = diAllocAG(imap, ag, dir, ip);
1787 AG_UNLOCK(imap, ag);
1793 /* no free disk inodes.
1800 * NAME: diAllocIno(imap,agno,ip)
1802 * FUNCTION: allocate a disk inode from the allocation group's free
1803 * inode list, returning an error if this free list is
1804 * empty (i.e. no iags on the list).
1806 * allocation occurs from the first iag on the list using
1807 * the iag's free inode summary map to find the leftmost
1808 * free inode in the iag.
1810 * PRE CONDITION: Already have AG lock for this AG.
1813 * imap - pointer to inode map control structure.
1814 * agno - allocation group.
1815 * ip - pointer to new inode to be filled in on successful return
1816 * with the disk inode number allocated, its extent address
1817 * and the start of the ag.
1821 * -ENOSPC - insufficient disk resources.
1824 static int diAllocIno(struct inomap * imap, int agno, struct inode *ip)
1826 int iagno, ino, rc, rem, extno, sword;
1827 struct metapage *mp;
1830 /* check if there are iags on the ag's free inode list.
1832 if ((iagno = imap->im_agctl[agno].inofree) < 0)
1835 /* obtain read lock on imap inode */
1836 IREAD_LOCK(imap->im_ipimap);
1838 /* read the iag at the head of the list.
1840 if ((rc = diIAGRead(imap, iagno, &mp))) {
1841 IREAD_UNLOCK(imap->im_ipimap);
1844 iagp = (struct iag *) mp->data;
1846 /* better be free inodes in this iag if it is on the
1849 if (!iagp->nfreeinos) {
1850 IREAD_UNLOCK(imap->im_ipimap);
1851 release_metapage(mp);
1853 "diAllocIno: nfreeinos = 0, but iag on freelist");
1857 /* scan the free inode summary map to find an extent
1860 for (sword = 0;; sword++) {
1861 if (sword >= SMAPSZ) {
1862 IREAD_UNLOCK(imap->im_ipimap);
1863 release_metapage(mp);
1865 "diAllocIno: free inode not found in summary map");
1869 if (~iagp->inosmap[sword])
1873 /* found a extent with free inodes. determine
1874 * the extent number.
1876 rem = diFindFree(le32_to_cpu(iagp->inosmap[sword]), 0);
1877 if (rem >= EXTSPERSUM) {
1878 IREAD_UNLOCK(imap->im_ipimap);
1879 release_metapage(mp);
1880 jfs_error(ip->i_sb, "diAllocIno: no free extent found");
1883 extno = (sword << L2EXTSPERSUM) + rem;
1885 /* find the first free inode in the extent.
1887 rem = diFindFree(le32_to_cpu(iagp->wmap[extno]), 0);
1888 if (rem >= INOSPEREXT) {
1889 IREAD_UNLOCK(imap->im_ipimap);
1890 release_metapage(mp);
1891 jfs_error(ip->i_sb, "diAllocIno: free inode not found");
1895 /* compute the inode number within the iag.
1897 ino = (extno << L2INOSPEREXT) + rem;
1899 /* allocate the inode.
1901 rc = diAllocBit(imap, iagp, ino);
1902 IREAD_UNLOCK(imap->im_ipimap);
1904 release_metapage(mp);
1908 /* set the results of the allocation and write the iag.
1910 diInitInode(ip, iagno, ino, extno, iagp);
1918 * NAME: diAllocExt(imap,agno,ip)
1920 * FUNCTION: add a new extent of free inodes to an iag, allocating
1921 * an inode from this extent to satisfy the current allocation
1924 * this routine first tries to find an existing iag with free
1925 * extents through the ag free extent list. if list is not
1926 * empty, the head of the list will be selected as the home
1927 * of the new extent of free inodes. otherwise (the list is
1928 * empty), a new iag will be allocated for the ag to contain
1931 * once an iag has been selected, the free extent summary map
1932 * is used to locate a free extent within the iag and diNewExt()
1933 * is called to initialize the extent, with initialization
1934 * including the allocation of the first inode of the extent
1935 * for the purpose of satisfying this request.
1938 * imap - pointer to inode map control structure.
1939 * agno - allocation group number.
1940 * ip - pointer to new inode to be filled in on successful return
1941 * with the disk inode number allocated, its extent address
1942 * and the start of the ag.
1946 * -ENOSPC - insufficient disk resources.
1949 static int diAllocExt(struct inomap * imap, int agno, struct inode *ip)
1951 int rem, iagno, sword, extno, rc;
1952 struct metapage *mp;
1955 /* check if the ag has any iags with free extents. if not,
1956 * allocate a new iag for the ag.
1958 if ((iagno = imap->im_agctl[agno].extfree) < 0) {
1959 /* If successful, diNewIAG will obtain the read lock on the
1962 if ((rc = diNewIAG(imap, &iagno, agno, &mp))) {
1965 iagp = (struct iag *) mp->data;
1967 /* set the ag number if this a brand new iag
1970 cpu_to_le64(AGTOBLK(agno, imap->im_ipimap));
1974 IREAD_LOCK(imap->im_ipimap);
1975 if ((rc = diIAGRead(imap, iagno, &mp))) {
1976 IREAD_UNLOCK(imap->im_ipimap);
1977 jfs_error(ip->i_sb, "diAllocExt: error reading iag");
1980 iagp = (struct iag *) mp->data;
1983 /* using the free extent summary map, find a free extent.
1985 for (sword = 0;; sword++) {
1986 if (sword >= SMAPSZ) {
1987 release_metapage(mp);
1988 IREAD_UNLOCK(imap->im_ipimap);
1990 "diAllocExt: free ext summary map not found");
1993 if (~iagp->extsmap[sword])
1997 /* determine the extent number of the free extent.
1999 rem = diFindFree(le32_to_cpu(iagp->extsmap[sword]), 0);
2000 if (rem >= EXTSPERSUM) {
2001 release_metapage(mp);
2002 IREAD_UNLOCK(imap->im_ipimap);
2003 jfs_error(ip->i_sb, "diAllocExt: free extent not found");
2006 extno = (sword << L2EXTSPERSUM) + rem;
2008 /* initialize the new extent.
2010 rc = diNewExt(imap, iagp, extno);
2011 IREAD_UNLOCK(imap->im_ipimap);
2013 /* something bad happened. if a new iag was allocated,
2014 * place it back on the inode map's iag free list, and
2015 * clear the ag number information.
2017 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2019 iagp->iagfree = cpu_to_le32(imap->im_freeiag);
2020 imap->im_freeiag = iagno;
2021 IAGFREE_UNLOCK(imap);
2027 /* set the results of the allocation and write the iag.
2029 diInitInode(ip, iagno, extno << L2INOSPEREXT, extno, iagp);
2038 * NAME: diAllocBit(imap,iagp,ino)
2040 * FUNCTION: allocate a backed inode from an iag.
2042 * this routine performs the mechanics of allocating a
2043 * specified inode from a backed extent.
2045 * if the inode to be allocated represents the last free
2046 * inode within the iag, the iag will be removed from the
2047 * ag free inode list.
2049 * a careful update approach is used to provide consistency
2050 * in the face of updates to multiple buffers. under this
2051 * approach, all required buffers are obtained before making
2052 * any updates and are held all are updates are complete.
2054 * PRE CONDITION: Already have buffer lock on iagp. Already have AG lock on
2055 * this AG. Must have read lock on imap inode.
2058 * imap - pointer to inode map control structure.
2059 * iagp - pointer to iag.
2060 * ino - inode number to be allocated within the iag.
2064 * -ENOSPC - insufficient disk resources.
2067 static int diAllocBit(struct inomap * imap, struct iag * iagp, int ino)
2069 int extno, bitno, agno, sword, rc;
2070 struct metapage *amp = NULL, *bmp = NULL;
2071 struct iag *aiagp = NULL, *biagp = NULL;
2074 /* check if this is the last free inode within the iag.
2075 * if so, it will have to be removed from the ag free
2076 * inode list, so get the iags preceeding and following
2079 if (iagp->nfreeinos == cpu_to_le32(1)) {
2080 if ((int) le32_to_cpu(iagp->inofreefwd) >= 0) {
2082 diIAGRead(imap, le32_to_cpu(iagp->inofreefwd),
2085 aiagp = (struct iag *) amp->data;
2088 if ((int) le32_to_cpu(iagp->inofreeback) >= 0) {
2091 le32_to_cpu(iagp->inofreeback),
2094 release_metapage(amp);
2097 biagp = (struct iag *) bmp->data;
2101 /* get the ag number, extent number, inode number within
2104 agno = BLKTOAG(le64_to_cpu(iagp->agstart), JFS_SBI(imap->im_ipimap->i_sb));
2105 extno = ino >> L2INOSPEREXT;
2106 bitno = ino & (INOSPEREXT - 1);
2108 /* compute the mask for setting the map.
2110 mask = HIGHORDER >> bitno;
2112 /* the inode should be free and backed.
2114 if (((le32_to_cpu(iagp->pmap[extno]) & mask) != 0) ||
2115 ((le32_to_cpu(iagp->wmap[extno]) & mask) != 0) ||
2116 (addressPXD(&iagp->inoext[extno]) == 0)) {
2118 release_metapage(amp);
2120 release_metapage(bmp);
2122 jfs_error(imap->im_ipimap->i_sb,
2123 "diAllocBit: iag inconsistent");
2127 /* mark the inode as allocated in the working map.
2129 iagp->wmap[extno] |= cpu_to_le32(mask);
2131 /* check if all inodes within the extent are now
2132 * allocated. if so, update the free inode summary
2133 * map to reflect this.
2135 if (iagp->wmap[extno] == cpu_to_le32(ONES)) {
2136 sword = extno >> L2EXTSPERSUM;
2137 bitno = extno & (EXTSPERSUM - 1);
2138 iagp->inosmap[sword] |= cpu_to_le32(HIGHORDER >> bitno);
2141 /* if this was the last free inode in the iag, remove the
2142 * iag from the ag free inode list.
2144 if (iagp->nfreeinos == cpu_to_le32(1)) {
2146 aiagp->inofreeback = iagp->inofreeback;
2147 write_metapage(amp);
2151 biagp->inofreefwd = iagp->inofreefwd;
2152 write_metapage(bmp);
2154 imap->im_agctl[agno].inofree =
2155 le32_to_cpu(iagp->inofreefwd);
2157 iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
2160 /* update the free inode count at the iag, ag, inode
2163 iagp->nfreeinos = cpu_to_le32(le32_to_cpu(iagp->nfreeinos) - 1);
2164 imap->im_agctl[agno].numfree -= 1;
2165 atomic_dec(&imap->im_numfree);
2172 * NAME: diNewExt(imap,iagp,extno)
2174 * FUNCTION: initialize a new extent of inodes for an iag, allocating
2175 * the first inode of the extent for use for the current
2176 * allocation request.
2178 * disk resources are allocated for the new extent of inodes
2179 * and the inodes themselves are initialized to reflect their
2180 * existence within the extent (i.e. their inode numbers and
2181 * inode extent addresses are set) and their initial state
2182 * (mode and link count are set to zero).
2184 * if the iag is new, it is not yet on an ag extent free list
2185 * but will now be placed on this list.
2187 * if the allocation of the new extent causes the iag to
2188 * have no free extent, the iag will be removed from the
2189 * ag extent free list.
2191 * if the iag has no free backed inodes, it will be placed
2192 * on the ag free inode list, since the addition of the new
2193 * extent will now cause it to have free inodes.
2195 * a careful update approach is used to provide consistency
2196 * (i.e. list consistency) in the face of updates to multiple
2197 * buffers. under this approach, all required buffers are
2198 * obtained before making any updates and are held until all
2199 * updates are complete.
2201 * PRE CONDITION: Already have buffer lock on iagp. Already have AG lock on
2202 * this AG. Must have read lock on imap inode.
2205 * imap - pointer to inode map control structure.
2206 * iagp - pointer to iag.
2207 * extno - extent number.
2211 * -ENOSPC - insufficient disk resources.
2214 static int diNewExt(struct inomap * imap, struct iag * iagp, int extno)
2216 int agno, iagno, fwd, back, freei = 0, sword, rc;
2217 struct iag *aiagp = NULL, *biagp = NULL, *ciagp = NULL;
2218 struct metapage *amp, *bmp, *cmp, *dmp;
2219 struct inode *ipimap;
2225 struct jfs_sb_info *sbi;
2227 /* better have free extents.
2229 if (!iagp->nfreeexts) {
2230 jfs_error(imap->im_ipimap->i_sb, "diNewExt: no free extents");
2234 /* get the inode map inode.
2236 ipimap = imap->im_ipimap;
2237 sbi = JFS_SBI(ipimap->i_sb);
2239 amp = bmp = cmp = NULL;
2241 /* get the ag and iag numbers for this iag.
2243 agno = BLKTOAG(le64_to_cpu(iagp->agstart), sbi);
2244 iagno = le32_to_cpu(iagp->iagnum);
2246 /* check if this is the last free extent within the
2247 * iag. if so, the iag must be removed from the ag
2248 * free extent list, so get the iags preceeding and
2249 * following the iag on this list.
2251 if (iagp->nfreeexts == cpu_to_le32(1)) {
2252 if ((fwd = le32_to_cpu(iagp->extfreefwd)) >= 0) {
2253 if ((rc = diIAGRead(imap, fwd, &)))
2255 aiagp = (struct iag *) amp->data;
2258 if ((back = le32_to_cpu(iagp->extfreeback)) >= 0) {
2259 if ((rc = diIAGRead(imap, back, &bmp)))
2261 biagp = (struct iag *) bmp->data;
2264 /* the iag has free extents. if all extents are free
2265 * (as is the case for a newly allocated iag), the iag
2266 * must be added to the ag free extent list, so get
2267 * the iag at the head of the list in preparation for
2268 * adding this iag to this list.
2271 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2272 if ((fwd = imap->im_agctl[agno].extfree) >= 0) {
2273 if ((rc = diIAGRead(imap, fwd, &)))
2275 aiagp = (struct iag *) amp->data;
2280 /* check if the iag has no free inodes. if so, the iag
2281 * will have to be added to the ag free inode list, so get
2282 * the iag at the head of the list in preparation for
2283 * adding this iag to this list. in doing this, we must
2284 * check if we already have the iag at the head of
2287 if (iagp->nfreeinos == 0) {
2288 freei = imap->im_agctl[agno].inofree;
2293 } else if (freei == back) {
2296 if ((rc = diIAGRead(imap, freei, &cmp)))
2298 ciagp = (struct iag *) cmp->data;
2300 if (ciagp == NULL) {
2301 jfs_error(imap->im_ipimap->i_sb,
2302 "diNewExt: ciagp == NULL");
2309 /* allocate disk space for the inode extent.
2311 if ((extno == 0) || (addressPXD(&iagp->inoext[extno - 1]) == 0))
2312 hint = ((s64) agno << sbi->bmap->db_agl2size) - 1;
2314 hint = addressPXD(&iagp->inoext[extno - 1]) +
2315 lengthPXD(&iagp->inoext[extno - 1]) - 1;
2317 if ((rc = dbAlloc(ipimap, hint, (s64) imap->im_nbperiext, &blkno)))
2320 /* compute the inode number of the first inode within the
2323 ino = (iagno << L2INOSPERIAG) + (extno << L2INOSPEREXT);
2325 /* initialize the inodes within the newly allocated extent a
2328 for (i = 0; i < imap->im_nbperiext; i += sbi->nbperpage) {
2329 /* get a buffer for this page of disk inodes.
2331 dmp = get_metapage(ipimap, blkno + i, PSIZE, 1);
2336 dp = (struct dinode *) dmp->data;
2338 /* initialize the inode number, mode, link count and
2339 * inode extent address.
2341 for (j = 0; j < INOSPERPAGE; j++, dp++, ino++) {
2342 dp->di_inostamp = cpu_to_le32(sbi->inostamp);
2343 dp->di_number = cpu_to_le32(ino);
2344 dp->di_fileset = cpu_to_le32(FILESYSTEM_I);
2347 PXDaddress(&(dp->di_ixpxd), blkno);
2348 PXDlength(&(dp->di_ixpxd), imap->im_nbperiext);
2350 write_metapage(dmp);
2353 /* if this is the last free extent within the iag, remove the
2354 * iag from the ag free extent list.
2356 if (iagp->nfreeexts == cpu_to_le32(1)) {
2358 aiagp->extfreeback = iagp->extfreeback;
2361 biagp->extfreefwd = iagp->extfreefwd;
2363 imap->im_agctl[agno].extfree =
2364 le32_to_cpu(iagp->extfreefwd);
2366 iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
2368 /* if the iag has all free extents (newly allocated iag),
2369 * add the iag to the ag free extent list.
2371 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2373 aiagp->extfreeback = cpu_to_le32(iagno);
2375 iagp->extfreefwd = cpu_to_le32(fwd);
2376 iagp->extfreeback = cpu_to_le32(-1);
2377 imap->im_agctl[agno].extfree = iagno;
2381 /* if the iag has no free inodes, add the iag to the
2382 * ag free inode list.
2384 if (iagp->nfreeinos == 0) {
2386 ciagp->inofreeback = cpu_to_le32(iagno);
2389 cpu_to_le32(imap->im_agctl[agno].inofree);
2390 iagp->inofreeback = cpu_to_le32(-1);
2391 imap->im_agctl[agno].inofree = iagno;
2394 /* initialize the extent descriptor of the extent. */
2395 PXDlength(&iagp->inoext[extno], imap->im_nbperiext);
2396 PXDaddress(&iagp->inoext[extno], blkno);
2398 /* initialize the working and persistent map of the extent.
2399 * the working map will be initialized such that
2400 * it indicates the first inode of the extent is allocated.
2402 iagp->wmap[extno] = cpu_to_le32(HIGHORDER);
2403 iagp->pmap[extno] = 0;
2405 /* update the free inode and free extent summary maps
2406 * for the extent to indicate the extent has free inodes
2407 * and no longer represents a free extent.
2409 sword = extno >> L2EXTSPERSUM;
2410 mask = HIGHORDER >> (extno & (EXTSPERSUM - 1));
2411 iagp->extsmap[sword] |= cpu_to_le32(mask);
2412 iagp->inosmap[sword] &= cpu_to_le32(~mask);
2414 /* update the free inode and free extent counts for the
2417 iagp->nfreeinos = cpu_to_le32(le32_to_cpu(iagp->nfreeinos) +
2419 iagp->nfreeexts = cpu_to_le32(le32_to_cpu(iagp->nfreeexts) - 1);
2421 /* update the free and backed inode counts for the ag.
2423 imap->im_agctl[agno].numfree += (INOSPEREXT - 1);
2424 imap->im_agctl[agno].numinos += INOSPEREXT;
2426 /* update the free and backed inode counts for the inode map.
2428 atomic_add(INOSPEREXT - 1, &imap->im_numfree);
2429 atomic_add(INOSPEREXT, &imap->im_numinos);
2434 write_metapage(amp);
2436 write_metapage(bmp);
2438 write_metapage(cmp);
2444 /* release the iags.
2447 release_metapage(amp);
2449 release_metapage(bmp);
2451 release_metapage(cmp);
2458 * NAME: diNewIAG(imap,iagnop,agno)
2460 * FUNCTION: allocate a new iag for an allocation group.
2462 * first tries to allocate the iag from the inode map
2464 * if the list has free iags, the head of the list is removed
2465 * and returned to satisfy the request.
2466 * if the inode map's iag free list is empty, the inode map
2467 * is extended to hold a new iag. this new iag is initialized
2468 * and returned to satisfy the request.
2471 * imap - pointer to inode map control structure.
2472 * iagnop - pointer to an iag number set with the number of the
2473 * newly allocated iag upon successful return.
2474 * agno - allocation group number.
2475 * bpp - Buffer pointer to be filled in with new IAG's buffer
2479 * -ENOSPC - insufficient disk resources.
2483 * AG lock held on entry/exit;
2484 * write lock on the map is held inside;
2485 * read lock on the map is held on successful completion;
2487 * note: new iag transaction:
2488 * . synchronously write iag;
2489 * . write log of xtree and inode of imap;
2491 * . synchronous write of xtree (right to left, bottom to top);
2492 * . at start of logredo(): init in-memory imap with one additional iag page;
2493 * . at end of logredo(): re-read imap inode to determine
2497 diNewIAG(struct inomap * imap, int *iagnop, int agno, struct metapage ** mpp)
2501 struct inode *ipimap;
2502 struct super_block *sb;
2503 struct jfs_sb_info *sbi;
2504 struct metapage *mp;
2509 #ifdef _STILL_TO_PORT
2511 #endif /* _STILL_TO_PORT */
2512 struct inode *iplist[1];
2514 /* pick up pointers to the inode map and mount inodes */
2515 ipimap = imap->im_ipimap;
2519 /* acquire the free iag lock */
2522 /* if there are any iags on the inode map free iag list,
2523 * allocate the iag from the head of the list.
2525 if (imap->im_freeiag >= 0) {
2526 /* pick up the iag number at the head of the list */
2527 iagno = imap->im_freeiag;
2529 /* determine the logical block number of the iag */
2530 blkno = IAGTOLBLK(iagno, sbi->l2nbperpage);
2532 /* no free iags. the inode map will have to be extented
2533 * to include a new iag.
2536 /* acquire inode map lock */
2537 IWRITE_LOCK(ipimap);
2539 if (ipimap->i_size >> L2PSIZE != imap->im_nextiag + 1) {
2540 IWRITE_UNLOCK(ipimap);
2541 IAGFREE_UNLOCK(imap);
2542 jfs_error(imap->im_ipimap->i_sb,
2543 "diNewIAG: ipimap->i_size is wrong");
2548 /* get the next avaliable iag number */
2549 iagno = imap->im_nextiag;
2551 /* make sure that we have not exceeded the maximum inode
2554 if (iagno > (MAXIAGS - 1)) {
2555 /* release the inode map lock */
2556 IWRITE_UNLOCK(ipimap);
2563 * synchronously append new iag page.
2565 /* determine the logical address of iag page to append */
2566 blkno = IAGTOLBLK(iagno, sbi->l2nbperpage);
2568 /* Allocate extent for new iag page */
2569 xlen = sbi->nbperpage;
2570 if ((rc = dbAlloc(ipimap, 0, (s64) xlen, &xaddr))) {
2571 /* release the inode map lock */
2572 IWRITE_UNLOCK(ipimap);
2578 * start transaction of update of the inode map
2579 * addressing structure pointing to the new iag page;
2581 tid = txBegin(sb, COMMIT_FORCE);
2582 down(&JFS_IP(ipimap)->commit_sem);
2584 /* update the inode map addressing structure to point to it */
2586 xtInsert(tid, ipimap, 0, blkno, xlen, &xaddr, 0))) {
2588 up(&JFS_IP(ipimap)->commit_sem);
2589 /* Free the blocks allocated for the iag since it was
2590 * not successfully added to the inode map
2592 dbFree(ipimap, xaddr, (s64) xlen);
2594 /* release the inode map lock */
2595 IWRITE_UNLOCK(ipimap);
2600 /* update the inode map's inode to reflect the extension */
2601 ipimap->i_size += PSIZE;
2602 inode_add_bytes(ipimap, PSIZE);
2604 /* assign a buffer for the page */
2605 mp = get_metapage(ipimap, blkno, PSIZE, 0);
2608 * This is very unlikely since we just created the
2609 * extent, but let's try to handle it correctly
2611 xtTruncate(tid, ipimap, ipimap->i_size - PSIZE,
2617 /* release the inode map lock */
2618 IWRITE_UNLOCK(ipimap);
2623 iagp = (struct iag *) mp->data;
2626 memset(iagp, 0, sizeof(struct iag));
2627 iagp->iagnum = cpu_to_le32(iagno);
2628 iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
2629 iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
2630 iagp->iagfree = cpu_to_le32(-1);
2631 iagp->nfreeinos = 0;
2632 iagp->nfreeexts = cpu_to_le32(EXTSPERIAG);
2634 /* initialize the free inode summary map (free extent
2635 * summary map initialization handled by bzero).
2637 for (i = 0; i < SMAPSZ; i++)
2638 iagp->inosmap[i] = cpu_to_le32(ONES);
2641 * Write and sync the metapage
2646 * txCommit(COMMIT_FORCE) will synchronously write address
2647 * index pages and inode after commit in careful update order
2648 * of address index pages (right to left, bottom up);
2651 rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
2654 up(&JFS_IP(ipimap)->commit_sem);
2656 duplicateIXtree(sb, blkno, xlen, &xaddr);
2658 /* update the next avaliable iag number */
2659 imap->im_nextiag += 1;
2661 /* Add the iag to the iag free list so we don't lose the iag
2662 * if a failure happens now.
2664 imap->im_freeiag = iagno;
2666 /* Until we have logredo working, we want the imap inode &
2667 * control page to be up to date.
2671 /* release the inode map lock */
2672 IWRITE_UNLOCK(ipimap);
2675 /* obtain read lock on map */
2679 if ((rc = diIAGRead(imap, iagno, &mp))) {
2680 IREAD_UNLOCK(ipimap);
2684 iagp = (struct iag *) mp->data;
2686 /* remove the iag from the iag free list */
2687 imap->im_freeiag = le32_to_cpu(iagp->iagfree);
2688 iagp->iagfree = cpu_to_le32(-1);
2690 /* set the return iag number and buffer pointer */
2695 /* release the iag free lock */
2696 IAGFREE_UNLOCK(imap);
2704 * FUNCTION: get the buffer for the specified iag within a fileset
2705 * or aggregate inode map.
2708 * imap - pointer to inode map control structure.
2709 * iagno - iag number.
2710 * bpp - point to buffer pointer to be filled in on successful
2714 * must have read lock on imap inode
2715 * (When called by diExtendFS, the filesystem is quiesced, therefore
2716 * the read lock is unnecessary.)
2722 static int diIAGRead(struct inomap * imap, int iagno, struct metapage ** mpp)
2724 struct inode *ipimap = imap->im_ipimap;
2727 /* compute the logical block number of the iag. */
2728 blkno = IAGTOLBLK(iagno, JFS_SBI(ipimap->i_sb)->l2nbperpage);
2731 *mpp = read_metapage(ipimap, blkno, PSIZE, 0);
2740 * NAME: diFindFree()
2742 * FUNCTION: find the first free bit in a word starting at
2743 * the specified bit position.
2746 * word - word to be examined.
2747 * start - starting bit position.
2750 * bit position of first free bit in the word or 32 if
2751 * no free bits were found.
2753 static int diFindFree(u32 word, int start)
2757 /* scan the word for the first free bit. */
2758 for (word <<= start, bitno = start; bitno < 32;
2759 bitno++, word <<= 1) {
2760 if ((word & HIGHORDER) == 0)
2767 * NAME: diUpdatePMap()
2769 * FUNCTION: Update the persistent map in an IAG for the allocation or
2770 * freeing of the specified inode.
2772 * PRE CONDITIONS: Working map has already been updated for allocate.
2775 * ipimap - Incore inode map inode
2776 * inum - Number of inode to mark in permanent map
2777 * is_free - If TRUE indicates inode should be marked freed, otherwise
2778 * indicates inode should be marked allocated.
2784 diUpdatePMap(struct inode *ipimap,
2785 unsigned long inum, boolean_t is_free, struct tblock * tblk)
2789 struct metapage *mp;
2790 int iagno, ino, extno, bitno;
2791 struct inomap *imap;
2793 struct jfs_log *log;
2794 int lsn, difft, diffp;
2795 unsigned long flags;
2797 imap = JFS_IP(ipimap)->i_imap;
2798 /* get the iag number containing the inode */
2799 iagno = INOTOIAG(inum);
2800 /* make sure that the iag is contained within the map */
2801 if (iagno >= imap->im_nextiag) {
2802 jfs_error(ipimap->i_sb,
2803 "diUpdatePMap: the iag is outside the map");
2808 rc = diIAGRead(imap, iagno, &mp);
2809 IREAD_UNLOCK(ipimap);
2812 metapage_wait_for_io(mp);
2813 iagp = (struct iag *) mp->data;
2814 /* get the inode number and extent number of the inode within
2815 * the iag and the inode number within the extent.
2817 ino = inum & (INOSPERIAG - 1);
2818 extno = ino >> L2INOSPEREXT;
2819 bitno = ino & (INOSPEREXT - 1);
2820 mask = HIGHORDER >> bitno;
2822 * mark the inode free in persistent map:
2824 if (is_free == TRUE) {
2825 /* The inode should have been allocated both in working
2826 * map and in persistent map;
2827 * the inode will be freed from working map at the release
2828 * of last reference release;
2830 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
2831 jfs_error(ipimap->i_sb,
2832 "diUpdatePMap: inode %ld not marked as "
2833 "allocated in wmap!", inum);
2835 if (!(le32_to_cpu(iagp->pmap[extno]) & mask)) {
2836 jfs_error(ipimap->i_sb,
2837 "diUpdatePMap: inode %ld not marked as "
2838 "allocated in pmap!", inum);
2840 /* update the bitmap for the extent of the freed inode */
2841 iagp->pmap[extno] &= cpu_to_le32(~mask);
2844 * mark the inode allocated in persistent map:
2847 /* The inode should be already allocated in the working map
2848 * and should be free in persistent map;
2850 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
2851 release_metapage(mp);
2852 jfs_error(ipimap->i_sb,
2853 "diUpdatePMap: the inode is not allocated in "
2857 if ((le32_to_cpu(iagp->pmap[extno]) & mask) != 0) {
2858 release_metapage(mp);
2859 jfs_error(ipimap->i_sb,
2860 "diUpdatePMap: the inode is not free in the "
2864 /* update the bitmap for the extent of the allocated inode */
2865 iagp->pmap[extno] |= cpu_to_le32(mask);
2871 log = JFS_SBI(tblk->sb)->log;
2873 /* inherit older/smaller lsn */
2874 logdiff(difft, lsn, log);
2875 logdiff(diffp, mp->lsn, log);
2876 LOGSYNC_LOCK(log, flags);
2877 if (difft < diffp) {
2879 /* move mp after tblock in logsync list */
2880 list_move(&mp->synclist, &tblk->synclist);
2882 /* inherit younger/larger clsn */
2884 logdiff(difft, tblk->clsn, log);
2885 logdiff(diffp, mp->clsn, log);
2887 mp->clsn = tblk->clsn;
2888 LOGSYNC_UNLOCK(log, flags);
2892 /* insert mp after tblock in logsync list */
2893 LOGSYNC_LOCK(log, flags);
2895 list_add(&mp->synclist, &tblk->synclist);
2896 mp->clsn = tblk->clsn;
2897 LOGSYNC_UNLOCK(log, flags);
2906 * function: update imap for extendfs();
2908 * note: AG size has been increased s.t. each k old contiguous AGs are
2909 * coalesced into a new AG;
2911 int diExtendFS(struct inode *ipimap, struct inode *ipbmap)
2914 struct inomap *imap = JFS_IP(ipimap)->i_imap;
2915 struct iag *iagp = NULL, *hiagp = NULL;
2916 struct bmap *mp = JFS_SBI(ipbmap->i_sb)->bmap;
2917 struct metapage *bp, *hbp;
2919 int numinos, xnuminos = 0, xnumfree = 0;
2922 jfs_info("diExtendFS: nextiag:%d numinos:%d numfree:%d",
2923 imap->im_nextiag, atomic_read(&imap->im_numinos),
2924 atomic_read(&imap->im_numfree));
2929 * coalesce contiguous k (newAGSize/oldAGSize) AGs;
2930 * i.e., (AGi, ..., AGj) where i = k*n and j = k*(n+1) - 1 to AGn;
2931 * note: new AG size = old AG size * (2**x).
2934 /* init per AG control information im_agctl[] */
2935 for (i = 0; i < MAXAG; i++) {
2936 imap->im_agctl[i].inofree = -1;
2937 imap->im_agctl[i].extfree = -1;
2938 imap->im_agctl[i].numinos = 0; /* number of backed inodes */
2939 imap->im_agctl[i].numfree = 0; /* number of free backed inodes */
2943 * process each iag page of the map.
2945 * rebuild AG Free Inode List, AG Free Inode Extent List;
2947 for (i = 0; i < imap->im_nextiag; i++) {
2948 if ((rc = diIAGRead(imap, i, &bp))) {
2952 iagp = (struct iag *) bp->data;
2953 if (le32_to_cpu(iagp->iagnum) != i) {
2954 release_metapage(bp);
2955 jfs_error(ipimap->i_sb,
2956 "diExtendFs: unexpected value of iagnum");
2960 /* leave free iag in the free iag list */
2961 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2962 release_metapage(bp);
2966 /* agstart that computes to the same ag is treated as same; */
2967 agstart = le64_to_cpu(iagp->agstart);
2968 /* iagp->agstart = agstart & ~(mp->db_agsize - 1); */
2969 n = agstart >> mp->db_agl2size;
2971 /* compute backed inodes */
2972 numinos = (EXTSPERIAG - le32_to_cpu(iagp->nfreeexts))
2975 /* merge AG backed inodes */
2976 imap->im_agctl[n].numinos += numinos;
2977 xnuminos += numinos;
2980 /* if any backed free inodes, insert at AG free inode list */
2981 if ((int) le32_to_cpu(iagp->nfreeinos) > 0) {
2982 if ((head = imap->im_agctl[n].inofree) == -1) {
2983 iagp->inofreefwd = cpu_to_le32(-1);
2984 iagp->inofreeback = cpu_to_le32(-1);
2986 if ((rc = diIAGRead(imap, head, &hbp))) {
2990 hiagp = (struct iag *) hbp->data;
2991 hiagp->inofreeback = iagp->iagnum;
2992 iagp->inofreefwd = cpu_to_le32(head);
2993 iagp->inofreeback = cpu_to_le32(-1);
2994 write_metapage(hbp);
2997 imap->im_agctl[n].inofree =
2998 le32_to_cpu(iagp->iagnum);
3000 /* merge AG backed free inodes */
3001 imap->im_agctl[n].numfree +=
3002 le32_to_cpu(iagp->nfreeinos);
3003 xnumfree += le32_to_cpu(iagp->nfreeinos);
3006 /* if any free extents, insert at AG free extent list */
3007 if (le32_to_cpu(iagp->nfreeexts) > 0) {
3008 if ((head = imap->im_agctl[n].extfree) == -1) {
3009 iagp->extfreefwd = cpu_to_le32(-1);
3010 iagp->extfreeback = cpu_to_le32(-1);
3012 if ((rc = diIAGRead(imap, head, &hbp))) {
3016 hiagp = (struct iag *) hbp->data;
3017 hiagp->extfreeback = iagp->iagnum;
3018 iagp->extfreefwd = cpu_to_le32(head);
3019 iagp->extfreeback = cpu_to_le32(-1);
3020 write_metapage(hbp);
3023 imap->im_agctl[n].extfree =
3024 le32_to_cpu(iagp->iagnum);
3031 if (xnuminos != atomic_read(&imap->im_numinos) ||
3032 xnumfree != atomic_read(&imap->im_numfree)) {
3033 jfs_error(ipimap->i_sb,
3034 "diExtendFs: numinos or numfree incorrect");
3045 * serialization: IWRITE_LOCK held on entry/exit
3047 * note: shadow page with regular inode (rel.2);
3049 static void duplicateIXtree(struct super_block *sb, s64 blkno,
3050 int xlen, s64 *xaddr)
3052 struct jfs_superblock *j_sb;
3053 struct buffer_head *bh;
3057 /* if AIT2 ipmap2 is bad, do not try to update it */
3058 if (JFS_SBI(sb)->mntflag & JFS_BAD_SAIT) /* s_flag */
3060 ip = diReadSpecial(sb, FILESYSTEM_I, 1);
3062 JFS_SBI(sb)->mntflag |= JFS_BAD_SAIT;
3063 if (readSuper(sb, &bh))
3065 j_sb = (struct jfs_superblock *)bh->b_data;
3066 j_sb->s_flag |= cpu_to_le32(JFS_BAD_SAIT);
3068 mark_buffer_dirty(bh);
3069 sync_dirty_buffer(bh);
3074 /* start transaction */
3075 tid = txBegin(sb, COMMIT_FORCE);
3076 /* update the inode map addressing structure to point to it */
3077 if (xtInsert(tid, ip, 0, blkno, xlen, xaddr, 0)) {
3078 JFS_SBI(sb)->mntflag |= JFS_BAD_SAIT;
3083 /* update the inode map's inode to reflect the extension */
3084 ip->i_size += PSIZE;
3085 inode_add_bytes(ip, PSIZE);
3086 txCommit(tid, 1, &ip, COMMIT_FORCE);
3093 * NAME: copy_from_dinode()
3095 * FUNCTION: Copies inode info from disk inode to in-memory inode
3099 * -ENOMEM - insufficient memory
3101 static int copy_from_dinode(struct dinode * dip, struct inode *ip)
3103 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
3107 jfs_ip->fileset = le32_to_cpu(dip->di_fileset);
3108 jfs_ip->mode2 = le32_to_cpu(dip->di_mode);
3110 ip->i_mode = le32_to_cpu(dip->di_mode) & 0xffff;
3111 ip->i_nlink = le32_to_cpu(dip->di_nlink);
3113 uid = le32_to_cpu(dip->di_uid);
3114 gid = le32_to_cpu(dip->di_gid);
3115 ip->i_uid = INOXID_UID(XID_TAG(ip), uid, gid);
3116 ip->i_gid = INOXID_GID(XID_TAG(ip), uid, gid);
3117 ip->i_xid = INOXID_XID(XID_TAG(ip), uid, gid, 0);
3119 ip->i_size = le64_to_cpu(dip->di_size);
3120 ip->i_atime.tv_sec = le32_to_cpu(dip->di_atime.tv_sec);
3121 ip->i_atime.tv_nsec = le32_to_cpu(dip->di_atime.tv_nsec);
3122 ip->i_mtime.tv_sec = le32_to_cpu(dip->di_mtime.tv_sec);
3123 ip->i_mtime.tv_nsec = le32_to_cpu(dip->di_mtime.tv_nsec);
3124 ip->i_ctime.tv_sec = le32_to_cpu(dip->di_ctime.tv_sec);
3125 ip->i_ctime.tv_nsec = le32_to_cpu(dip->di_ctime.tv_nsec);
3126 ip->i_blksize = ip->i_sb->s_blocksize;
3127 ip->i_blocks = LBLK2PBLK(ip->i_sb, le64_to_cpu(dip->di_nblocks));
3128 ip->i_generation = le32_to_cpu(dip->di_gen);
3130 jfs_ip->ixpxd = dip->di_ixpxd; /* in-memory pxd's are little-endian */
3131 jfs_ip->acl = dip->di_acl; /* as are dxd's */
3132 jfs_ip->ea = dip->di_ea;
3133 jfs_ip->next_index = le32_to_cpu(dip->di_next_index);
3134 jfs_ip->otime = le32_to_cpu(dip->di_otime.tv_sec);
3135 jfs_ip->acltype = le32_to_cpu(dip->di_acltype);
3137 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode)) {
3138 jfs_ip->dev = le32_to_cpu(dip->di_rdev);
3139 ip->i_rdev = new_decode_dev(jfs_ip->dev);
3142 if (S_ISDIR(ip->i_mode)) {
3143 memcpy(&jfs_ip->i_dirtable, &dip->di_dirtable, 384);
3144 } else if (S_ISREG(ip->i_mode) || S_ISLNK(ip->i_mode)) {
3145 memcpy(&jfs_ip->i_xtroot, &dip->di_xtroot, 288);
3147 memcpy(&jfs_ip->i_inline_ea, &dip->di_inlineea, 128);
3149 /* Zero the in-memory-only stuff */
3151 jfs_ip->btindex = 0;
3152 jfs_ip->btorder = 0;
3155 jfs_ip->atlhead = 0;
3156 jfs_ip->atltail = 0;
3162 * NAME: copy_to_dinode()
3164 * FUNCTION: Copies inode info from in-memory inode to disk inode
3166 static void copy_to_dinode(struct dinode * dip, struct inode *ip)
3168 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
3172 dip->di_fileset = cpu_to_le32(jfs_ip->fileset);
3173 dip->di_inostamp = cpu_to_le32(JFS_SBI(ip->i_sb)->inostamp);
3174 dip->di_number = cpu_to_le32(ip->i_ino);
3175 dip->di_gen = cpu_to_le32(ip->i_generation);
3176 dip->di_size = cpu_to_le64(ip->i_size);
3177 dip->di_nblocks = cpu_to_le64(PBLK2LBLK(ip->i_sb, ip->i_blocks));
3178 dip->di_nlink = cpu_to_le32(ip->i_nlink);
3180 uid = XIDINO_UID(XID_TAG(ip), ip->i_uid, ip->i_xid);
3181 gid = XIDINO_GID(XID_TAG(ip), ip->i_gid, ip->i_xid);
3182 dip->di_uid = cpu_to_le32(uid);
3183 dip->di_gid = cpu_to_le32(gid);
3185 * mode2 is only needed for storing the higher order bits.
3186 * Trust i_mode for the lower order ones
3188 dip->di_mode = cpu_to_le32((jfs_ip->mode2 & 0xffff0000) | ip->i_mode);
3189 dip->di_atime.tv_sec = cpu_to_le32(ip->i_atime.tv_sec);
3190 dip->di_atime.tv_nsec = cpu_to_le32(ip->i_atime.tv_nsec);
3191 dip->di_ctime.tv_sec = cpu_to_le32(ip->i_ctime.tv_sec);
3192 dip->di_ctime.tv_nsec = cpu_to_le32(ip->i_ctime.tv_nsec);
3193 dip->di_mtime.tv_sec = cpu_to_le32(ip->i_mtime.tv_sec);
3194 dip->di_mtime.tv_nsec = cpu_to_le32(ip->i_mtime.tv_nsec);
3195 dip->di_ixpxd = jfs_ip->ixpxd; /* in-memory pxd's are little-endian */
3196 dip->di_acl = jfs_ip->acl; /* as are dxd's */
3197 dip->di_ea = jfs_ip->ea;
3198 dip->di_next_index = cpu_to_le32(jfs_ip->next_index);
3199 dip->di_otime.tv_sec = cpu_to_le32(jfs_ip->otime);
3200 dip->di_otime.tv_nsec = 0;
3201 dip->di_acltype = cpu_to_le32(jfs_ip->acltype);
3202 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode))
3203 dip->di_rdev = cpu_to_le32(jfs_ip->dev);
3206 #ifdef _JFS_DEBUG_IMAP
3210 static void *DBGdiInit(struct inomap * imap)
3215 if ((dimap = (u32 *) xmalloc(size, L2PSIZE, kernel_heap)) == NULL)
3217 bzero((void *) dimap, size);
3218 imap->im_DBGdimap = dimap;
3224 static void DBGdiAlloc(struct inomap * imap, ino_t ino)
3226 u32 *dimap = imap->im_DBGdimap;
3231 m = 0x80000000 >> b;
3232 assert(w < 64 * 256);
3234 printk("DEBUG diAlloc: duplicate alloc ino:0x%x\n", ino);
3242 static void DBGdiFree(struct inomap * imap, ino_t ino)
3244 u32 *dimap = imap->im_DBGdimap;
3249 m = 0x80000000 >> b;
3250 assert(w < 64 * 256);
3251 if ((dimap[w] & m) == 0) {
3252 printk("DEBUG diFree: duplicate free ino:0x%x\n", ino);
3257 static void dump_cp(struct inomap * ipimap, char *function, int line)
3259 printk("\n* ********* *\nControl Page %s %d\n", function, line);
3260 printk("FreeIAG %d\tNextIAG %d\n", ipimap->im_freeiag,
3261 ipimap->im_nextiag);
3262 printk("NumInos %d\tNumFree %d\n",
3263 atomic_read(&ipimap->im_numinos),
3264 atomic_read(&ipimap->im_numfree));
3265 printk("AG InoFree %d\tAG ExtFree %d\n",
3266 ipimap->im_agctl[0].inofree, ipimap->im_agctl[0].extfree);
3267 printk("AG NumInos %d\tAG NumFree %d\n",
3268 ipimap->im_agctl[0].numinos, ipimap->im_agctl[0].numfree);
3271 static void dump_iag(struct iag * iag, char *function, int line)
3273 printk("\n* ********* *\nIAG %s %d\n", function, line);
3274 printk("IagNum %d\tIAG Free %d\n", le32_to_cpu(iag->iagnum),
3275 le32_to_cpu(iag->iagfree));
3276 printk("InoFreeFwd %d\tInoFreeBack %d\n",
3277 le32_to_cpu(iag->inofreefwd),
3278 le32_to_cpu(iag->inofreeback));
3279 printk("ExtFreeFwd %d\tExtFreeBack %d\n",
3280 le32_to_cpu(iag->extfreefwd),
3281 le32_to_cpu(iag->extfreeback));
3282 printk("NFreeInos %d\tNFreeExts %d\n", le32_to_cpu(iag->nfreeinos),
3283 le32_to_cpu(iag->nfreeexts));
3285 #endif /* _JFS_DEBUG_IMAP */