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>
49 #include "jfs_incore.h"
50 #include "jfs_inode.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"
60 * __mark_inode_dirty expects inodes to be hashed. Since we don't want
61 * special inodes in the fileset inode space, we hash them to a dummy head
63 static HLIST_HEAD(aggregate_hash);
68 /* iag free list lock */
69 #define IAGFREE_LOCK_INIT(imap) init_MUTEX(&imap->im_freelock)
70 #define IAGFREE_LOCK(imap) down(&imap->im_freelock)
71 #define IAGFREE_UNLOCK(imap) up(&imap->im_freelock)
73 /* per ag iag list locks */
74 #define AG_LOCK_INIT(imap,index) init_MUTEX(&(imap->im_aglock[index]))
75 #define AG_LOCK(imap,agno) down(&imap->im_aglock[agno])
76 #define AG_UNLOCK(imap,agno) up(&imap->im_aglock[agno])
81 static int diAllocAG(struct inomap *, int, boolean_t, struct inode *);
82 static int diAllocAny(struct inomap *, int, boolean_t, struct inode *);
83 static int diAllocBit(struct inomap *, struct iag *, int);
84 static int diAllocExt(struct inomap *, int, struct inode *);
85 static int diAllocIno(struct inomap *, int, struct inode *);
86 static int diFindFree(u32, int);
87 static int diNewExt(struct inomap *, struct iag *, int);
88 static int diNewIAG(struct inomap *, int *, int, struct metapage **);
89 static void duplicateIXtree(struct super_block *, s64, int, s64 *);
91 static int diIAGRead(struct inomap * imap, int, struct metapage **);
92 static int copy_from_dinode(struct dinode *, struct inode *);
93 static void copy_to_dinode(struct dinode *, struct inode *);
98 * FUNCTION: initialize the incore inode map control structures for
99 * a fileset or aggregate init time.
101 * the inode map's control structure (dinomap) is
102 * brought in from disk and placed in virtual memory.
105 * ipimap - pointer to inode map inode for the aggregate or fileset.
109 * -ENOMEM - insufficient free virtual memory.
112 int diMount(struct inode *ipimap)
117 struct dinomap_disk *dinom_le;
120 * allocate/initialize the in-memory inode map control structure
122 /* allocate the in-memory inode map control structure. */
123 imap = (struct inomap *) kmalloc(sizeof(struct inomap), GFP_KERNEL);
125 jfs_err("diMount: kmalloc returned NULL!");
129 /* read the on-disk inode map control structure. */
131 mp = read_metapage(ipimap,
132 IMAPBLKNO << JFS_SBI(ipimap->i_sb)->l2nbperpage,
139 /* copy the on-disk version to the in-memory version. */
140 dinom_le = (struct dinomap_disk *) mp->data;
141 imap->im_freeiag = le32_to_cpu(dinom_le->in_freeiag);
142 imap->im_nextiag = le32_to_cpu(dinom_le->in_nextiag);
143 atomic_set(&imap->im_numinos, le32_to_cpu(dinom_le->in_numinos));
144 atomic_set(&imap->im_numfree, le32_to_cpu(dinom_le->in_numfree));
145 imap->im_nbperiext = le32_to_cpu(dinom_le->in_nbperiext);
146 imap->im_l2nbperiext = le32_to_cpu(dinom_le->in_l2nbperiext);
147 for (index = 0; index < MAXAG; index++) {
148 imap->im_agctl[index].inofree =
149 le32_to_cpu(dinom_le->in_agctl[index].inofree);
150 imap->im_agctl[index].extfree =
151 le32_to_cpu(dinom_le->in_agctl[index].extfree);
152 imap->im_agctl[index].numinos =
153 le32_to_cpu(dinom_le->in_agctl[index].numinos);
154 imap->im_agctl[index].numfree =
155 le32_to_cpu(dinom_le->in_agctl[index].numfree);
158 /* release the buffer. */
159 release_metapage(mp);
162 * allocate/initialize inode allocation map locks
164 /* allocate and init iag free list lock */
165 IAGFREE_LOCK_INIT(imap);
167 /* allocate and init ag list locks */
168 for (index = 0; index < MAXAG; index++) {
169 AG_LOCK_INIT(imap, index);
172 /* bind the inode map inode and inode map control structure
175 imap->im_ipimap = ipimap;
176 JFS_IP(ipimap)->i_imap = imap;
185 * FUNCTION: write to disk the incore inode map control structures for
186 * a fileset or aggregate at unmount time.
189 * ipimap - pointer to inode map inode for the aggregate or fileset.
193 * -ENOMEM - insufficient free virtual memory.
196 int diUnmount(struct inode *ipimap, int mounterror)
198 struct inomap *imap = JFS_IP(ipimap)->i_imap;
201 * update the on-disk inode map control structure
204 if (!(mounterror || isReadOnly(ipimap)))
208 * Invalidate the page cache buffers
210 truncate_inode_pages(ipimap->i_mapping, 0);
213 * free in-memory control structure
224 int diSync(struct inode *ipimap)
226 struct dinomap_disk *dinom_le;
227 struct inomap *imp = JFS_IP(ipimap)->i_imap;
232 * write imap global conrol page
234 /* read the on-disk inode map control structure */
235 mp = get_metapage(ipimap,
236 IMAPBLKNO << JFS_SBI(ipimap->i_sb)->l2nbperpage,
239 jfs_err("diSync: get_metapage failed!");
243 /* copy the in-memory version to the on-disk version */
244 dinom_le = (struct dinomap_disk *) mp->data;
245 dinom_le->in_freeiag = cpu_to_le32(imp->im_freeiag);
246 dinom_le->in_nextiag = cpu_to_le32(imp->im_nextiag);
247 dinom_le->in_numinos = cpu_to_le32(atomic_read(&imp->im_numinos));
248 dinom_le->in_numfree = cpu_to_le32(atomic_read(&imp->im_numfree));
249 dinom_le->in_nbperiext = cpu_to_le32(imp->im_nbperiext);
250 dinom_le->in_l2nbperiext = cpu_to_le32(imp->im_l2nbperiext);
251 for (index = 0; index < MAXAG; index++) {
252 dinom_le->in_agctl[index].inofree =
253 cpu_to_le32(imp->im_agctl[index].inofree);
254 dinom_le->in_agctl[index].extfree =
255 cpu_to_le32(imp->im_agctl[index].extfree);
256 dinom_le->in_agctl[index].numinos =
257 cpu_to_le32(imp->im_agctl[index].numinos);
258 dinom_le->in_agctl[index].numfree =
259 cpu_to_le32(imp->im_agctl[index].numfree);
262 /* write out the control structure */
266 * write out dirty pages of imap
268 filemap_fdatawrite(ipimap->i_mapping);
269 filemap_fdatawait(ipimap->i_mapping);
271 diWriteSpecial(ipimap, 0);
280 * FUNCTION: initialize an incore inode from disk.
282 * on entry, the specifed incore inode should itself
283 * specify the disk inode number corresponding to the
284 * incore inode (i.e. i_number should be initialized).
286 * this routine handles incore inode initialization for
287 * both "special" and "regular" inodes. special inodes
288 * are those required early in the mount process and
289 * require special handling since much of the file system
290 * is not yet initialized. these "special" inodes are
291 * identified by a NULL inode map inode pointer and are
292 * actually initialized by a call to diReadSpecial().
294 * for regular inodes, the iag describing the disk inode
295 * is read from disk to determine the inode extent address
296 * for the disk inode. with the inode extent address in
297 * hand, the page of the extent that contains the disk
298 * inode is read and the disk inode is copied to the
302 * ip - pointer to incore inode to be initialized from disk.
307 * -ENOMEM - insufficient memory
310 int diRead(struct inode *ip)
312 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
313 int iagno, ino, extno, rc;
314 struct inode *ipimap;
325 jfs_info("diRead: ino = %ld", ip->i_ino);
327 ipimap = sbi->ipimap;
328 JFS_IP(ip)->ipimap = ipimap;
330 /* determine the iag number for this inode (number) */
331 iagno = INOTOIAG(ip->i_ino);
334 imap = JFS_IP(ipimap)->i_imap;
336 rc = diIAGRead(imap, iagno, &mp);
337 IREAD_UNLOCK(ipimap);
339 jfs_err("diRead: diIAGRead returned %d", rc);
343 iagp = (struct iag *) mp->data;
345 /* determine inode extent that holds the disk inode */
346 ino = ip->i_ino & (INOSPERIAG - 1);
347 extno = ino >> L2INOSPEREXT;
349 if ((lengthPXD(&iagp->inoext[extno]) != imap->im_nbperiext) ||
350 (addressPXD(&iagp->inoext[extno]) == 0)) {
351 release_metapage(mp);
355 /* get disk block number of the page within the inode extent
356 * that holds the disk inode.
358 blkno = INOPBLK(&iagp->inoext[extno], ino, sbi->l2nbperpage);
360 /* get the ag for the iag */
361 agstart = le64_to_cpu(iagp->agstart);
363 release_metapage(mp);
365 rel_inode = (ino & (INOSPERPAGE - 1));
366 pageno = blkno >> sbi->l2nbperpage;
368 if ((block_offset = ((u32) blkno & (sbi->nbperpage - 1)))) {
370 * OS/2 didn't always align inode extents on page boundaries
373 (sbi->nbperpage - block_offset) << sbi->l2niperblk;
375 if (rel_inode < inodes_left)
376 rel_inode += block_offset << sbi->l2niperblk;
379 rel_inode -= inodes_left;
383 /* read the page of disk inode */
384 mp = read_metapage(ipimap, pageno << sbi->l2nbperpage, PSIZE, 1);
386 jfs_err("diRead: read_metapage failed");
390 /* locate the the disk inode requested */
391 dp = (struct dinode *) mp->data;
394 if (ip->i_ino != le32_to_cpu(dp->di_number)) {
395 jfs_error(ip->i_sb, "diRead: i_ino != di_number");
397 } else if (le32_to_cpu(dp->di_nlink) == 0)
400 /* copy the disk inode to the in-memory inode */
401 rc = copy_from_dinode(dp, ip);
403 release_metapage(mp);
405 /* set the ag for the inode */
406 JFS_IP(ip)->agno = BLKTOAG(agstart, sbi);
407 JFS_IP(ip)->active_ag = -1;
414 * NAME: diReadSpecial()
416 * FUNCTION: initialize a 'special' inode from disk.
418 * this routines handles aggregate level inodes. The
419 * inode cache cannot differentiate between the
420 * aggregate inodes and the filesystem inodes, so we
421 * handle these here. We don't actually use the aggregate
422 * inode map, since these inodes are at a fixed location
423 * and in some cases the aggregate inode map isn't initialized
427 * sb - filesystem superblock
428 * inum - aggregate inode number
429 * secondary - 1 if secondary aggregate inode table
432 * new inode - success
435 struct inode *diReadSpecial(struct super_block *sb, ino_t inum, int secondary)
437 struct jfs_sb_info *sbi = JFS_SBI(sb);
445 jfs_err("diReadSpecial: new_inode returned NULL!");
450 address = addressPXD(&sbi->ait2) >> sbi->l2nbperpage;
451 JFS_IP(ip)->ipimap = sbi->ipaimap2;
453 address = AITBL_OFF >> L2PSIZE;
454 JFS_IP(ip)->ipimap = sbi->ipaimap;
457 ASSERT(inum < INOSPEREXT);
461 address += inum >> 3; /* 8 inodes per 4K page */
463 /* read the page of fixed disk inode (AIT) in raw mode */
464 mp = read_metapage(ip, address << sbi->l2nbperpage, PSIZE, 1);
466 ip->i_nlink = 1; /* Don't want iput() deleting it */
471 /* get the pointer to the disk inode of interest */
472 dp = (struct dinode *) (mp->data);
473 dp += inum % 8; /* 8 inodes per 4K page */
475 /* copy on-disk inode to in-memory inode */
476 if ((copy_from_dinode(dp, ip)) != 0) {
477 /* handle bad return by returning NULL for ip */
478 ip->i_nlink = 1; /* Don't want iput() deleting it */
480 /* release the page */
481 release_metapage(mp);
486 ip->i_mapping->a_ops = &jfs_metapage_aops;
487 mapping_set_gfp_mask(ip->i_mapping, GFP_NOFS);
489 /* Allocations to metadata inodes should not affect quotas */
490 ip->i_flags |= S_NOQUOTA;
492 if ((inum == FILESYSTEM_I) && (JFS_IP(ip)->ipimap == sbi->ipaimap)) {
493 sbi->gengen = le32_to_cpu(dp->di_gengen);
494 sbi->inostamp = le32_to_cpu(dp->di_inostamp);
497 /* release the page */
498 release_metapage(mp);
500 hlist_add_head(&ip->i_hash, &aggregate_hash);
506 * NAME: diWriteSpecial()
508 * FUNCTION: Write the special inode to disk
512 * secondary - 1 if secondary aggregate inode table
514 * RETURN VALUES: none
517 void diWriteSpecial(struct inode *ip, int secondary)
519 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
522 ino_t inum = ip->i_ino;
526 address = addressPXD(&sbi->ait2) >> sbi->l2nbperpage;
528 address = AITBL_OFF >> L2PSIZE;
530 ASSERT(inum < INOSPEREXT);
532 address += inum >> 3; /* 8 inodes per 4K page */
534 /* read the page of fixed disk inode (AIT) in raw mode */
535 mp = read_metapage(ip, address << sbi->l2nbperpage, PSIZE, 1);
537 jfs_err("diWriteSpecial: failed to read aggregate inode "
542 /* get the pointer to the disk inode of interest */
543 dp = (struct dinode *) (mp->data);
544 dp += inum % 8; /* 8 inodes per 4K page */
546 /* copy on-disk inode to in-memory inode */
547 copy_to_dinode(dp, ip);
548 memcpy(&dp->di_xtroot, &JFS_IP(ip)->i_xtroot, 288);
550 if (inum == FILESYSTEM_I)
551 dp->di_gengen = cpu_to_le32(sbi->gengen);
558 * NAME: diFreeSpecial()
560 * FUNCTION: Free allocated space for special inode
562 void diFreeSpecial(struct inode *ip)
565 jfs_err("diFreeSpecial called with NULL ip!");
568 filemap_fdatawrite(ip->i_mapping);
569 filemap_fdatawait(ip->i_mapping);
570 truncate_inode_pages(ip->i_mapping, 0);
579 * FUNCTION: write the on-disk inode portion of the in-memory inode
580 * to its corresponding on-disk inode.
582 * on entry, the specifed incore inode should itself
583 * specify the disk inode number corresponding to the
584 * incore inode (i.e. i_number should be initialized).
586 * the inode contains the inode extent address for the disk
587 * inode. with the inode extent address in hand, the
588 * page of the extent that contains the disk inode is
589 * read and the disk inode portion of the incore inode
590 * is copied to the disk inode.
593 * tid - transacation id
594 * ip - pointer to incore inode to be written to the inode extent.
600 int diWrite(tid_t tid, struct inode *ip)
602 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
603 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
614 struct inode *ipimap;
617 struct tlock *ditlck, *tlck;
618 struct linelock *dilinelock, *ilinelock;
622 ipimap = jfs_ip->ipimap;
624 ino = ip->i_ino & (INOSPERIAG - 1);
626 if (!addressPXD(&(jfs_ip->ixpxd)) ||
627 (lengthPXD(&(jfs_ip->ixpxd)) !=
628 JFS_IP(ipimap)->i_imap->im_nbperiext)) {
629 jfs_error(ip->i_sb, "diWrite: ixpxd invalid");
634 * read the page of disk inode containing the specified inode:
636 /* compute the block address of the page */
637 blkno = INOPBLK(&(jfs_ip->ixpxd), ino, sbi->l2nbperpage);
639 rel_inode = (ino & (INOSPERPAGE - 1));
640 pageno = blkno >> sbi->l2nbperpage;
642 if ((block_offset = ((u32) blkno & (sbi->nbperpage - 1)))) {
644 * OS/2 didn't always align inode extents on page boundaries
647 (sbi->nbperpage - block_offset) << sbi->l2niperblk;
649 if (rel_inode < inodes_left)
650 rel_inode += block_offset << sbi->l2niperblk;
653 rel_inode -= inodes_left;
656 /* read the page of disk inode */
658 mp = read_metapage(ipimap, pageno << sbi->l2nbperpage, PSIZE, 1);
662 /* get the pointer to the disk inode */
663 dp = (struct dinode *) mp->data;
666 dioffset = (ino & (INOSPERPAGE - 1)) << L2DISIZE;
669 * acquire transaction lock on the on-disk inode;
670 * N.B. tlock is acquired on ipimap not ip;
673 txLock(tid, ipimap, mp, tlckINODE | tlckENTRY)) == NULL)
675 dilinelock = (struct linelock *) & ditlck->lock;
678 * copy btree root from in-memory inode to on-disk inode
680 * (tlock is taken from inline B+-tree root in in-memory
681 * inode when the B+-tree root is updated, which is pointed
682 * by jfs_ip->blid as well as being on tx tlock list)
684 * further processing of btree root is based on the copy
685 * in in-memory inode, where txLog() will log from, and,
686 * for xtree root, txUpdateMap() will update map and reset
690 if (S_ISDIR(ip->i_mode) && (lid = jfs_ip->xtlid)) {
692 * This is the special xtree inside the directory for storing
693 * the directory table
699 tlck = lid_to_tlock(lid);
700 assert(tlck->type & tlckXTREE);
701 tlck->type |= tlckBTROOT;
703 ilinelock = (struct linelock *) & tlck->lock;
706 * copy xtree root from inode to dinode:
708 p = &jfs_ip->i_xtroot;
709 xp = (xtpage_t *) &dp->di_dirtable;
711 for (n = 0; n < ilinelock->index; n++, lv++) {
712 memcpy(&xp->xad[lv->offset], &p->xad[lv->offset],
713 lv->length << L2XTSLOTSIZE);
716 /* reset on-disk (metadata page) xtree XAD_NEW bit */
717 xad = &xp->xad[XTENTRYSTART];
718 for (n = XTENTRYSTART;
719 n < le16_to_cpu(xp->header.nextindex); n++, xad++)
720 if (xad->flag & (XAD_NEW | XAD_EXTENDED))
721 xad->flag &= ~(XAD_NEW | XAD_EXTENDED);
724 if ((lid = jfs_ip->blid) == 0)
728 tlck = lid_to_tlock(lid);
730 tlck->type |= tlckBTROOT;
732 ilinelock = (struct linelock *) & tlck->lock;
735 * regular file: 16 byte (XAD slot) granularity
737 if (type & tlckXTREE) {
742 * copy xtree root from inode to dinode:
744 p = &jfs_ip->i_xtroot;
747 for (n = 0; n < ilinelock->index; n++, lv++) {
748 memcpy(&xp->xad[lv->offset], &p->xad[lv->offset],
749 lv->length << L2XTSLOTSIZE);
752 /* reset on-disk (metadata page) xtree XAD_NEW bit */
753 xad = &xp->xad[XTENTRYSTART];
754 for (n = XTENTRYSTART;
755 n < le16_to_cpu(xp->header.nextindex); n++, xad++)
756 if (xad->flag & (XAD_NEW | XAD_EXTENDED))
757 xad->flag &= ~(XAD_NEW | XAD_EXTENDED);
760 * directory: 32 byte (directory entry slot) granularity
762 else if (type & tlckDTREE) {
766 * copy dtree root from inode to dinode:
768 p = (dtpage_t *) &jfs_ip->i_dtroot;
769 xp = (dtpage_t *) & dp->di_dtroot;
771 for (n = 0; n < ilinelock->index; n++, lv++) {
772 memcpy(&xp->slot[lv->offset], &p->slot[lv->offset],
773 lv->length << L2DTSLOTSIZE);
776 jfs_err("diWrite: UFO tlock");
781 * copy inline symlink from in-memory inode to on-disk inode
783 if (S_ISLNK(ip->i_mode) && ip->i_size < IDATASIZE) {
784 lv = & dilinelock->lv[dilinelock->index];
785 lv->offset = (dioffset + 2 * 128) >> L2INODESLOTSIZE;
787 memcpy(&dp->di_fastsymlink, jfs_ip->i_inline, IDATASIZE);
791 * copy inline data from in-memory inode to on-disk inode:
792 * 128 byte slot granularity
794 if (test_cflag(COMMIT_Inlineea, ip)) {
795 lv = & dilinelock->lv[dilinelock->index];
796 lv->offset = (dioffset + 3 * 128) >> L2INODESLOTSIZE;
798 memcpy(&dp->di_inlineea, jfs_ip->i_inline_ea, INODESLOTSIZE);
801 clear_cflag(COMMIT_Inlineea, ip);
805 * lock/copy inode base: 128 byte slot granularity
808 lv = & dilinelock->lv[dilinelock->index];
809 lv->offset = dioffset >> L2INODESLOTSIZE;
810 copy_to_dinode(dp, ip);
811 if (test_and_clear_cflag(COMMIT_Dirtable, ip)) {
813 memcpy(&dp->di_dirtable, &jfs_ip->i_dirtable, 96);
820 * We aren't logging changes to the DASD used in directory inodes,
821 * but we need to write them to disk. If we don't unmount cleanly,
822 * mount will recalculate the DASD used.
824 if (S_ISDIR(ip->i_mode)
825 && (ip->i_ipmnt->i_mntflag & JFS_DASD_ENABLED))
826 memcpy(&dp->di_DASD, &ip->i_DASD, sizeof(struct dasd));
827 #endif /* _JFS_FASTDASD */
829 /* release the buffer holding the updated on-disk inode.
830 * the buffer will be later written by commit processing.
841 * FUNCTION: free a specified inode from the inode working map
842 * for a fileset or aggregate.
844 * if the inode to be freed represents the first (only)
845 * free inode within the iag, the iag will be placed on
846 * the ag free inode list.
848 * freeing the inode will cause the inode extent to be
849 * freed if the inode is the only allocated inode within
850 * the extent. in this case all the disk resource backing
851 * up the inode extent will be freed. in addition, the iag
852 * will be placed on the ag extent free list if the extent
853 * is the first free extent in the iag. if freeing the
854 * extent also means that no free inodes will exist for
855 * the iag, the iag will also be removed from the ag free
858 * the iag describing the inode will be freed if the extent
859 * is to be freed and it is the only backed extent within
860 * the iag. in this case, the iag will be removed from the
861 * ag free extent list and ag free inode list and placed on
862 * the inode map's free iag list.
864 * a careful update approach is used to provide consistency
865 * in the face of updates to multiple buffers. under this
866 * approach, all required buffers are obtained before making
867 * any updates and are held until all updates are complete.
870 * ip - inode to be freed.
876 int diFree(struct inode *ip)
879 ino_t inum = ip->i_ino;
880 struct iag *iagp, *aiagp, *biagp, *ciagp, *diagp;
881 struct metapage *mp, *amp, *bmp, *cmp, *dmp;
882 int iagno, ino, extno, bitno, sword, agno;
885 struct inode *ipimap = JFS_SBI(ip->i_sb)->ipimap;
886 struct inomap *imap = JFS_IP(ipimap)->i_imap;
889 struct inode *iplist[3];
891 struct pxd_lock *pxdlock;
894 * This is just to suppress compiler warnings. The same logic that
895 * references these variables is used to initialize them.
897 aiagp = biagp = ciagp = diagp = NULL;
899 /* get the iag number containing the inode.
901 iagno = INOTOIAG(inum);
903 /* make sure that the iag is contained within
906 if (iagno >= imap->im_nextiag) {
907 dump_mem("imap", imap, 32);
909 "diFree: inum = %d, iagno = %d, nextiag = %d",
910 (uint) inum, iagno, imap->im_nextiag);
914 /* get the allocation group for this ino.
916 agno = JFS_IP(ip)->agno;
918 /* Lock the AG specific inode map information
922 /* Obtain read lock in imap inode. Don't release it until we have
923 * read all of the IAG's that we are going to.
929 if ((rc = diIAGRead(imap, iagno, &mp))) {
930 IREAD_UNLOCK(ipimap);
931 AG_UNLOCK(imap, agno);
934 iagp = (struct iag *) mp->data;
936 /* get the inode number and extent number of the inode within
937 * the iag and the inode number within the extent.
939 ino = inum & (INOSPERIAG - 1);
940 extno = ino >> L2INOSPEREXT;
941 bitno = ino & (INOSPEREXT - 1);
942 mask = HIGHORDER >> bitno;
944 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
946 "diFree: wmap shows inode already free");
949 if (!addressPXD(&iagp->inoext[extno])) {
950 release_metapage(mp);
951 IREAD_UNLOCK(ipimap);
952 AG_UNLOCK(imap, agno);
953 jfs_error(ip->i_sb, "diFree: invalid inoext");
957 /* compute the bitmap for the extent reflecting the freed inode.
959 bitmap = le32_to_cpu(iagp->wmap[extno]) & ~mask;
961 if (imap->im_agctl[agno].numfree > imap->im_agctl[agno].numinos) {
962 release_metapage(mp);
963 IREAD_UNLOCK(ipimap);
964 AG_UNLOCK(imap, agno);
965 jfs_error(ip->i_sb, "diFree: numfree > numinos");
969 * inode extent still has some inodes or below low water mark:
970 * keep the inode extent;
973 imap->im_agctl[agno].numfree < 96 ||
974 (imap->im_agctl[agno].numfree < 288 &&
975 (((imap->im_agctl[agno].numfree * 100) /
976 imap->im_agctl[agno].numinos) <= 25))) {
977 /* if the iag currently has no free inodes (i.e.,
978 * the inode being freed is the first free inode of iag),
979 * insert the iag at head of the inode free list for the ag.
981 if (iagp->nfreeinos == 0) {
982 /* check if there are any iags on the ag inode
983 * free list. if so, read the first one so that
984 * we can link the current iag onto the list at
987 if ((fwd = imap->im_agctl[agno].inofree) >= 0) {
988 /* read the iag that currently is the head
991 if ((rc = diIAGRead(imap, fwd, &))) {
992 IREAD_UNLOCK(ipimap);
993 AG_UNLOCK(imap, agno);
994 release_metapage(mp);
997 aiagp = (struct iag *) amp->data;
999 /* make current head point back to the iag.
1001 aiagp->inofreeback = cpu_to_le32(iagno);
1003 write_metapage(amp);
1006 /* iag points forward to current head and iag
1007 * becomes the new head of the list.
1010 cpu_to_le32(imap->im_agctl[agno].inofree);
1011 iagp->inofreeback = cpu_to_le32(-1);
1012 imap->im_agctl[agno].inofree = iagno;
1014 IREAD_UNLOCK(ipimap);
1016 /* update the free inode summary map for the extent if
1017 * freeing the inode means the extent will now have free
1018 * inodes (i.e., the inode being freed is the first free
1021 if (iagp->wmap[extno] == cpu_to_le32(ONES)) {
1022 sword = extno >> L2EXTSPERSUM;
1023 bitno = extno & (EXTSPERSUM - 1);
1024 iagp->inosmap[sword] &=
1025 cpu_to_le32(~(HIGHORDER >> bitno));
1028 /* update the bitmap.
1030 iagp->wmap[extno] = cpu_to_le32(bitmap);
1032 /* update the free inode counts at the iag, ag and
1036 cpu_to_le32(le32_to_cpu(iagp->nfreeinos) + 1);
1037 imap->im_agctl[agno].numfree += 1;
1038 atomic_inc(&imap->im_numfree);
1040 /* release the AG inode map lock
1042 AG_UNLOCK(imap, agno);
1052 * inode extent has become free and above low water mark:
1053 * free the inode extent;
1057 * prepare to update iag list(s) (careful update step 1)
1059 amp = bmp = cmp = dmp = NULL;
1062 /* check if the iag currently has no free extents. if so,
1063 * it will be placed on the head of the ag extent free list.
1065 if (iagp->nfreeexts == 0) {
1066 /* check if the ag extent free list has any iags.
1067 * if so, read the iag at the head of the list now.
1068 * this (head) iag will be updated later to reflect
1069 * the addition of the current iag at the head of
1072 if ((fwd = imap->im_agctl[agno].extfree) >= 0) {
1073 if ((rc = diIAGRead(imap, fwd, &)))
1075 aiagp = (struct iag *) amp->data;
1078 /* iag has free extents. check if the addition of a free
1079 * extent will cause all extents to be free within this
1080 * iag. if so, the iag will be removed from the ag extent
1081 * free list and placed on the inode map's free iag list.
1083 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG - 1)) {
1084 /* in preparation for removing the iag from the
1085 * ag extent free list, read the iags preceeding
1086 * and following the iag on the ag extent free
1089 if ((fwd = le32_to_cpu(iagp->extfreefwd)) >= 0) {
1090 if ((rc = diIAGRead(imap, fwd, &)))
1092 aiagp = (struct iag *) amp->data;
1095 if ((back = le32_to_cpu(iagp->extfreeback)) >= 0) {
1096 if ((rc = diIAGRead(imap, back, &bmp)))
1098 biagp = (struct iag *) bmp->data;
1103 /* remove the iag from the ag inode free list if freeing
1104 * this extent cause the iag to have no free inodes.
1106 if (iagp->nfreeinos == cpu_to_le32(INOSPEREXT - 1)) {
1107 int inofreeback = le32_to_cpu(iagp->inofreeback);
1108 int inofreefwd = le32_to_cpu(iagp->inofreefwd);
1110 /* in preparation for removing the iag from the
1111 * ag inode free list, read the iags preceeding
1112 * and following the iag on the ag inode free
1113 * list. before reading these iags, we must make
1114 * sure that we already don't have them in hand
1115 * from up above, since re-reading an iag (buffer)
1116 * we are currently holding would cause a deadlock.
1118 if (inofreefwd >= 0) {
1120 if (inofreefwd == fwd)
1121 ciagp = (struct iag *) amp->data;
1122 else if (inofreefwd == back)
1123 ciagp = (struct iag *) bmp->data;
1126 diIAGRead(imap, inofreefwd, &cmp)))
1128 ciagp = (struct iag *) cmp->data;
1130 assert(ciagp != NULL);
1133 if (inofreeback >= 0) {
1134 if (inofreeback == fwd)
1135 diagp = (struct iag *) amp->data;
1136 else if (inofreeback == back)
1137 diagp = (struct iag *) bmp->data;
1140 diIAGRead(imap, inofreeback, &dmp)))
1142 diagp = (struct iag *) dmp->data;
1144 assert(diagp != NULL);
1148 IREAD_UNLOCK(ipimap);
1151 * invalidate any page of the inode extent freed from buffer cache;
1153 freepxd = iagp->inoext[extno];
1154 invalidate_pxd_metapages(ip, freepxd);
1157 * update iag list(s) (careful update step 2)
1159 /* add the iag to the ag extent free list if this is the
1160 * first free extent for the iag.
1162 if (iagp->nfreeexts == 0) {
1164 aiagp->extfreeback = cpu_to_le32(iagno);
1167 cpu_to_le32(imap->im_agctl[agno].extfree);
1168 iagp->extfreeback = cpu_to_le32(-1);
1169 imap->im_agctl[agno].extfree = iagno;
1171 /* remove the iag from the ag extent list if all extents
1172 * are now free and place it on the inode map iag free list.
1174 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG - 1)) {
1176 aiagp->extfreeback = iagp->extfreeback;
1179 biagp->extfreefwd = iagp->extfreefwd;
1181 imap->im_agctl[agno].extfree =
1182 le32_to_cpu(iagp->extfreefwd);
1184 iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
1187 iagp->iagfree = cpu_to_le32(imap->im_freeiag);
1188 imap->im_freeiag = iagno;
1189 IAGFREE_UNLOCK(imap);
1193 /* remove the iag from the ag inode free list if freeing
1194 * this extent causes the iag to have no free inodes.
1196 if (iagp->nfreeinos == cpu_to_le32(INOSPEREXT - 1)) {
1197 if ((int) le32_to_cpu(iagp->inofreefwd) >= 0)
1198 ciagp->inofreeback = iagp->inofreeback;
1200 if ((int) le32_to_cpu(iagp->inofreeback) >= 0)
1201 diagp->inofreefwd = iagp->inofreefwd;
1203 imap->im_agctl[agno].inofree =
1204 le32_to_cpu(iagp->inofreefwd);
1206 iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
1209 /* update the inode extent address and working map
1210 * to reflect the free extent.
1211 * the permanent map should have been updated already
1212 * for the inode being freed.
1214 if (iagp->pmap[extno] != 0) {
1215 jfs_error(ip->i_sb, "diFree: the pmap does not show inode free");
1217 iagp->wmap[extno] = 0;
1218 PXDlength(&iagp->inoext[extno], 0);
1219 PXDaddress(&iagp->inoext[extno], 0);
1221 /* update the free extent and free inode summary maps
1222 * to reflect the freed extent.
1223 * the inode summary map is marked to indicate no inodes
1224 * available for the freed extent.
1226 sword = extno >> L2EXTSPERSUM;
1227 bitno = extno & (EXTSPERSUM - 1);
1228 mask = HIGHORDER >> bitno;
1229 iagp->inosmap[sword] |= cpu_to_le32(mask);
1230 iagp->extsmap[sword] &= cpu_to_le32(~mask);
1232 /* update the number of free inodes and number of free extents
1235 iagp->nfreeinos = cpu_to_le32(le32_to_cpu(iagp->nfreeinos) -
1237 iagp->nfreeexts = cpu_to_le32(le32_to_cpu(iagp->nfreeexts) + 1);
1239 /* update the number of free inodes and backed inodes
1240 * at the ag and inode map level.
1242 imap->im_agctl[agno].numfree -= (INOSPEREXT - 1);
1243 imap->im_agctl[agno].numinos -= INOSPEREXT;
1244 atomic_sub(INOSPEREXT - 1, &imap->im_numfree);
1245 atomic_sub(INOSPEREXT, &imap->im_numinos);
1248 write_metapage(amp);
1250 write_metapage(bmp);
1252 write_metapage(cmp);
1254 write_metapage(dmp);
1257 * start transaction to update block allocation map
1258 * for the inode extent freed;
1260 * N.B. AG_LOCK is released and iag will be released below, and
1261 * other thread may allocate inode from/reusing the ixad freed
1262 * BUT with new/different backing inode extent from the extent
1263 * to be freed by the transaction;
1265 tid = txBegin(ipimap->i_sb, COMMIT_FORCE);
1266 down(&JFS_IP(ipimap)->commit_sem);
1268 /* acquire tlock of the iag page of the freed ixad
1269 * to force the page NOHOMEOK (even though no data is
1270 * logged from the iag page) until NOREDOPAGE|FREEXTENT log
1271 * for the free of the extent is committed;
1272 * write FREEXTENT|NOREDOPAGE log record
1273 * N.B. linelock is overlaid as freed extent descriptor;
1275 tlck = txLock(tid, ipimap, mp, tlckINODE | tlckFREE);
1276 pxdlock = (struct pxd_lock *) & tlck->lock;
1277 pxdlock->flag = mlckFREEPXD;
1278 pxdlock->pxd = freepxd;
1286 * logredo needs the IAG number and IAG extent index in order
1287 * to ensure that the IMap is consistent. The least disruptive
1288 * way to pass these values through to the transaction manager
1289 * is in the iplist array.
1291 * It's not pretty, but it works.
1293 iplist[1] = (struct inode *) (size_t)iagno;
1294 iplist[2] = (struct inode *) (size_t)extno;
1296 rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
1299 up(&JFS_IP(ipimap)->commit_sem);
1301 /* unlock the AG inode map information */
1302 AG_UNLOCK(imap, agno);
1307 IREAD_UNLOCK(ipimap);
1310 release_metapage(amp);
1312 release_metapage(bmp);
1314 release_metapage(cmp);
1316 release_metapage(dmp);
1318 AG_UNLOCK(imap, agno);
1320 release_metapage(mp);
1326 * There are several places in the diAlloc* routines where we initialize
1330 diInitInode(struct inode *ip, int iagno, int ino, int extno, struct iag * iagp)
1332 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
1333 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
1335 ip->i_ino = (iagno << L2INOSPERIAG) + ino;
1336 jfs_ip->ixpxd = iagp->inoext[extno];
1337 jfs_ip->agno = BLKTOAG(le64_to_cpu(iagp->agstart), sbi);
1338 jfs_ip->active_ag = -1;
1343 * NAME: diAlloc(pip,dir,ip)
1345 * FUNCTION: allocate a disk inode from the inode working map
1346 * for a fileset or aggregate.
1349 * pip - pointer to incore inode for the parent inode.
1350 * dir - TRUE if the new disk inode is for a directory.
1351 * ip - pointer to a new inode
1355 * -ENOSPC - insufficient disk resources.
1358 int diAlloc(struct inode *pip, boolean_t dir, struct inode *ip)
1360 int rc, ino, iagno, addext, extno, bitno, sword;
1361 int nwords, rem, i, agno;
1362 u32 mask, inosmap, extsmap;
1363 struct inode *ipimap;
1364 struct metapage *mp;
1367 struct inomap *imap;
1369 /* get the pointers to the inode map inode and the
1370 * corresponding imap control structure.
1372 ipimap = JFS_SBI(pip->i_sb)->ipimap;
1373 imap = JFS_IP(ipimap)->i_imap;
1374 JFS_IP(ip)->ipimap = ipimap;
1375 JFS_IP(ip)->fileset = FILESYSTEM_I;
1377 /* for a directory, the allocation policy is to start
1378 * at the ag level using the preferred ag.
1381 agno = dbNextAG(JFS_SBI(pip->i_sb)->ipbmap);
1382 AG_LOCK(imap, agno);
1386 /* for files, the policy starts off by trying to allocate from
1387 * the same iag containing the parent disk inode:
1388 * try to allocate the new disk inode close to the parent disk
1389 * inode, using parent disk inode number + 1 as the allocation
1390 * hint. (we use a left-to-right policy to attempt to avoid
1391 * moving backward on the disk.) compute the hint within the
1392 * file system and the iag.
1395 /* get the ag number of this iag */
1396 agno = JFS_IP(pip)->agno;
1398 if (atomic_read(&JFS_SBI(pip->i_sb)->bmap->db_active[agno])) {
1400 * There is an open file actively growing. We want to
1401 * allocate new inodes from a different ag to avoid
1402 * fragmentation problems.
1404 agno = dbNextAG(JFS_SBI(pip->i_sb)->ipbmap);
1405 AG_LOCK(imap, agno);
1409 inum = pip->i_ino + 1;
1410 ino = inum & (INOSPERIAG - 1);
1412 /* back off the the hint if it is outside of the iag */
1416 /* lock the AG inode map information */
1417 AG_LOCK(imap, agno);
1419 /* Get read lock on imap inode */
1422 /* get the iag number and read the iag */
1423 iagno = INOTOIAG(inum);
1424 if ((rc = diIAGRead(imap, iagno, &mp))) {
1425 IREAD_UNLOCK(ipimap);
1426 AG_UNLOCK(imap, agno);
1429 iagp = (struct iag *) mp->data;
1431 /* determine if new inode extent is allowed to be added to the iag.
1432 * new inode extent can be added to the iag if the ag
1433 * has less than 32 free disk inodes and the iag has free extents.
1435 addext = (imap->im_agctl[agno].numfree < 32 && iagp->nfreeexts);
1438 * try to allocate from the IAG
1440 /* check if the inode may be allocated from the iag
1441 * (i.e. the inode has free inodes or new extent can be added).
1443 if (iagp->nfreeinos || addext) {
1444 /* determine the extent number of the hint.
1446 extno = ino >> L2INOSPEREXT;
1448 /* check if the extent containing the hint has backed
1449 * inodes. if so, try to allocate within this extent.
1451 if (addressPXD(&iagp->inoext[extno])) {
1452 bitno = ino & (INOSPEREXT - 1);
1454 diFindFree(le32_to_cpu(iagp->wmap[extno]),
1457 ino = (extno << L2INOSPEREXT) + bitno;
1459 /* a free inode (bit) was found within this
1460 * extent, so allocate it.
1462 rc = diAllocBit(imap, iagp, ino);
1463 IREAD_UNLOCK(ipimap);
1467 /* set the results of the allocation
1468 * and write the iag.
1470 diInitInode(ip, iagno, ino, extno,
1472 mark_metapage_dirty(mp);
1474 release_metapage(mp);
1476 /* free the AG lock and return.
1478 AG_UNLOCK(imap, agno);
1485 EXTSPERIAG - 1) ? 0 : extno + 1;
1489 * no free inodes within the extent containing the hint.
1491 * try to allocate from the backed extents following
1492 * hint or, if appropriate (i.e. addext is true), allocate
1493 * an extent of free inodes at or following the extent
1494 * containing the hint.
1496 * the free inode and free extent summary maps are used
1497 * here, so determine the starting summary map position
1498 * and the number of words we'll have to examine. again,
1499 * the approach is to allocate following the hint, so we
1500 * might have to initially ignore prior bits of the summary
1501 * map that represent extents prior to the extent containing
1502 * the hint and later revisit these bits.
1504 bitno = extno & (EXTSPERSUM - 1);
1505 nwords = (bitno == 0) ? SMAPSZ : SMAPSZ + 1;
1506 sword = extno >> L2EXTSPERSUM;
1508 /* mask any prior bits for the starting words of the
1511 mask = ONES << (EXTSPERSUM - bitno);
1512 inosmap = le32_to_cpu(iagp->inosmap[sword]) | mask;
1513 extsmap = le32_to_cpu(iagp->extsmap[sword]) | mask;
1515 /* scan the free inode and free extent summary maps for
1518 for (i = 0; i < nwords; i++) {
1519 /* check if this word of the free inode summary
1520 * map describes an extent with free inodes.
1523 /* an extent with free inodes has been
1524 * found. determine the extent number
1525 * and the inode number within the extent.
1527 rem = diFindFree(inosmap, 0);
1528 extno = (sword << L2EXTSPERSUM) + rem;
1529 rem = diFindFree(le32_to_cpu(iagp->wmap[extno]),
1531 if (rem >= INOSPEREXT) {
1532 IREAD_UNLOCK(ipimap);
1533 release_metapage(mp);
1534 AG_UNLOCK(imap, agno);
1536 "diAlloc: can't find free bit "
1541 /* determine the inode number within the
1542 * iag and allocate the inode from the
1545 ino = (extno << L2INOSPEREXT) + rem;
1546 rc = diAllocBit(imap, iagp, ino);
1547 IREAD_UNLOCK(ipimap);
1551 /* set the results of the allocation
1552 * and write the iag.
1554 diInitInode(ip, iagno, ino, extno,
1556 mark_metapage_dirty(mp);
1558 release_metapage(mp);
1560 /* free the AG lock and return.
1562 AG_UNLOCK(imap, agno);
1567 /* check if we may allocate an extent of free
1568 * inodes and whether this word of the free
1569 * extents summary map describes a free extent.
1571 if (addext && ~extsmap) {
1572 /* a free extent has been found. determine
1573 * the extent number.
1575 rem = diFindFree(extsmap, 0);
1576 extno = (sword << L2EXTSPERSUM) + rem;
1578 /* allocate an extent of free inodes.
1580 if ((rc = diNewExt(imap, iagp, extno))) {
1581 /* if there is no disk space for a
1582 * new extent, try to allocate the
1583 * disk inode from somewhere else.
1590 /* set the results of the allocation
1591 * and write the iag.
1593 diInitInode(ip, iagno,
1594 extno << L2INOSPEREXT,
1596 mark_metapage_dirty(mp);
1598 release_metapage(mp);
1599 /* free the imap inode & the AG lock & return.
1601 IREAD_UNLOCK(ipimap);
1602 AG_UNLOCK(imap, agno);
1606 /* move on to the next set of summary map words.
1608 sword = (sword == SMAPSZ - 1) ? 0 : sword + 1;
1609 inosmap = le32_to_cpu(iagp->inosmap[sword]);
1610 extsmap = le32_to_cpu(iagp->extsmap[sword]);
1613 /* unlock imap inode */
1614 IREAD_UNLOCK(ipimap);
1616 /* nothing doing in this iag, so release it. */
1617 release_metapage(mp);
1621 * try to allocate anywhere within the same AG as the parent inode.
1623 rc = diAllocAG(imap, agno, dir, ip);
1625 AG_UNLOCK(imap, agno);
1631 * try to allocate in any AG.
1633 return (diAllocAny(imap, agno, dir, ip));
1638 * NAME: diAllocAG(imap,agno,dir,ip)
1640 * FUNCTION: allocate a disk inode from the allocation group.
1642 * this routine first determines if a new extent of free
1643 * inodes should be added for the allocation group, with
1644 * the current request satisfied from this extent. if this
1645 * is the case, an attempt will be made to do just that. if
1646 * this attempt fails or it has been determined that a new
1647 * extent should not be added, an attempt is made to satisfy
1648 * the request by allocating an existing (backed) free inode
1649 * from the allocation group.
1651 * PRE CONDITION: Already have the AG lock for this AG.
1654 * imap - pointer to inode map control structure.
1655 * agno - allocation group to allocate from.
1656 * dir - TRUE if the new disk inode is for a directory.
1657 * ip - pointer to the new inode to be filled in on successful return
1658 * with the disk inode number allocated, its extent address
1659 * and the start of the ag.
1663 * -ENOSPC - insufficient disk resources.
1667 diAllocAG(struct inomap * imap, int agno, boolean_t dir, struct inode *ip)
1669 int rc, addext, numfree, numinos;
1671 /* get the number of free and the number of backed disk
1672 * inodes currently within the ag.
1674 numfree = imap->im_agctl[agno].numfree;
1675 numinos = imap->im_agctl[agno].numinos;
1677 if (numfree > numinos) {
1678 jfs_error(ip->i_sb, "diAllocAG: numfree > numinos");
1682 /* determine if we should allocate a new extent of free inodes
1683 * within the ag: for directory inodes, add a new extent
1684 * if there are a small number of free inodes or number of free
1685 * inodes is a small percentage of the number of backed inodes.
1688 addext = (numfree < 64 ||
1690 && ((numfree * 100) / numinos) <= 20));
1692 addext = (numfree == 0);
1695 * try to allocate a new extent of free inodes.
1698 /* if free space is not avaliable for this new extent, try
1699 * below to allocate a free and existing (already backed)
1700 * inode from the ag.
1702 if ((rc = diAllocExt(imap, agno, ip)) != -ENOSPC)
1707 * try to allocate an existing free inode from the ag.
1709 return (diAllocIno(imap, agno, ip));
1714 * NAME: diAllocAny(imap,agno,dir,iap)
1716 * FUNCTION: allocate a disk inode from any other allocation group.
1718 * this routine is called when an allocation attempt within
1719 * the primary allocation group has failed. if attempts to
1720 * allocate an inode from any allocation group other than the
1721 * specified primary group.
1724 * imap - pointer to inode map control structure.
1725 * agno - primary allocation group (to avoid).
1726 * dir - TRUE if the new disk inode is for a directory.
1727 * ip - pointer to a new inode to be filled in on successful return
1728 * with the disk inode number allocated, its extent address
1729 * and the start of the ag.
1733 * -ENOSPC - insufficient disk resources.
1737 diAllocAny(struct inomap * imap, int agno, boolean_t dir, struct inode *ip)
1740 int maxag = JFS_SBI(imap->im_ipimap->i_sb)->bmap->db_maxag;
1743 /* try to allocate from the ags following agno up to
1744 * the maximum ag number.
1746 for (ag = agno + 1; ag <= maxag; ag++) {
1749 rc = diAllocAG(imap, ag, dir, ip);
1751 AG_UNLOCK(imap, ag);
1757 /* try to allocate from the ags in front of agno.
1759 for (ag = 0; ag < agno; ag++) {
1762 rc = diAllocAG(imap, ag, dir, ip);
1764 AG_UNLOCK(imap, ag);
1770 /* no free disk inodes.
1777 * NAME: diAllocIno(imap,agno,ip)
1779 * FUNCTION: allocate a disk inode from the allocation group's free
1780 * inode list, returning an error if this free list is
1781 * empty (i.e. no iags on the list).
1783 * allocation occurs from the first iag on the list using
1784 * the iag's free inode summary map to find the leftmost
1785 * free inode in the iag.
1787 * PRE CONDITION: Already have AG lock for this AG.
1790 * imap - pointer to inode map control structure.
1791 * agno - allocation group.
1792 * ip - pointer to new inode to be filled in on successful return
1793 * with the disk inode number allocated, its extent address
1794 * and the start of the ag.
1798 * -ENOSPC - insufficient disk resources.
1801 static int diAllocIno(struct inomap * imap, int agno, struct inode *ip)
1803 int iagno, ino, rc, rem, extno, sword;
1804 struct metapage *mp;
1807 /* check if there are iags on the ag's free inode list.
1809 if ((iagno = imap->im_agctl[agno].inofree) < 0)
1812 /* obtain read lock on imap inode */
1813 IREAD_LOCK(imap->im_ipimap);
1815 /* read the iag at the head of the list.
1817 if ((rc = diIAGRead(imap, iagno, &mp))) {
1818 IREAD_UNLOCK(imap->im_ipimap);
1821 iagp = (struct iag *) mp->data;
1823 /* better be free inodes in this iag if it is on the
1826 if (!iagp->nfreeinos) {
1827 IREAD_UNLOCK(imap->im_ipimap);
1828 release_metapage(mp);
1830 "diAllocIno: nfreeinos = 0, but iag on freelist");
1834 /* scan the free inode summary map to find an extent
1837 for (sword = 0;; sword++) {
1838 if (sword >= SMAPSZ) {
1839 IREAD_UNLOCK(imap->im_ipimap);
1840 release_metapage(mp);
1842 "diAllocIno: free inode not found in summary map");
1846 if (~iagp->inosmap[sword])
1850 /* found a extent with free inodes. determine
1851 * the extent number.
1853 rem = diFindFree(le32_to_cpu(iagp->inosmap[sword]), 0);
1854 if (rem >= EXTSPERSUM) {
1855 IREAD_UNLOCK(imap->im_ipimap);
1856 release_metapage(mp);
1857 jfs_error(ip->i_sb, "diAllocIno: no free extent found");
1860 extno = (sword << L2EXTSPERSUM) + rem;
1862 /* find the first free inode in the extent.
1864 rem = diFindFree(le32_to_cpu(iagp->wmap[extno]), 0);
1865 if (rem >= INOSPEREXT) {
1866 IREAD_UNLOCK(imap->im_ipimap);
1867 release_metapage(mp);
1868 jfs_error(ip->i_sb, "diAllocIno: free inode not found");
1872 /* compute the inode number within the iag.
1874 ino = (extno << L2INOSPEREXT) + rem;
1876 /* allocate the inode.
1878 rc = diAllocBit(imap, iagp, ino);
1879 IREAD_UNLOCK(imap->im_ipimap);
1881 release_metapage(mp);
1885 /* set the results of the allocation and write the iag.
1887 diInitInode(ip, iagno, ino, extno, iagp);
1895 * NAME: diAllocExt(imap,agno,ip)
1897 * FUNCTION: add a new extent of free inodes to an iag, allocating
1898 * an inode from this extent to satisfy the current allocation
1901 * this routine first tries to find an existing iag with free
1902 * extents through the ag free extent list. if list is not
1903 * empty, the head of the list will be selected as the home
1904 * of the new extent of free inodes. otherwise (the list is
1905 * empty), a new iag will be allocated for the ag to contain
1908 * once an iag has been selected, the free extent summary map
1909 * is used to locate a free extent within the iag and diNewExt()
1910 * is called to initialize the extent, with initialization
1911 * including the allocation of the first inode of the extent
1912 * for the purpose of satisfying this request.
1915 * imap - pointer to inode map control structure.
1916 * agno - allocation group number.
1917 * ip - pointer to new inode to be filled in on successful return
1918 * with the disk inode number allocated, its extent address
1919 * and the start of the ag.
1923 * -ENOSPC - insufficient disk resources.
1926 static int diAllocExt(struct inomap * imap, int agno, struct inode *ip)
1928 int rem, iagno, sword, extno, rc;
1929 struct metapage *mp;
1932 /* check if the ag has any iags with free extents. if not,
1933 * allocate a new iag for the ag.
1935 if ((iagno = imap->im_agctl[agno].extfree) < 0) {
1936 /* If successful, diNewIAG will obtain the read lock on the
1939 if ((rc = diNewIAG(imap, &iagno, agno, &mp))) {
1942 iagp = (struct iag *) mp->data;
1944 /* set the ag number if this a brand new iag
1947 cpu_to_le64(AGTOBLK(agno, imap->im_ipimap));
1951 IREAD_LOCK(imap->im_ipimap);
1952 if ((rc = diIAGRead(imap, iagno, &mp))) {
1953 IREAD_UNLOCK(imap->im_ipimap);
1954 jfs_error(ip->i_sb, "diAllocExt: error reading iag");
1957 iagp = (struct iag *) mp->data;
1960 /* using the free extent summary map, find a free extent.
1962 for (sword = 0;; sword++) {
1963 if (sword >= SMAPSZ) {
1964 release_metapage(mp);
1965 IREAD_UNLOCK(imap->im_ipimap);
1967 "diAllocExt: free ext summary map not found");
1970 if (~iagp->extsmap[sword])
1974 /* determine the extent number of the free extent.
1976 rem = diFindFree(le32_to_cpu(iagp->extsmap[sword]), 0);
1977 if (rem >= EXTSPERSUM) {
1978 release_metapage(mp);
1979 IREAD_UNLOCK(imap->im_ipimap);
1980 jfs_error(ip->i_sb, "diAllocExt: free extent not found");
1983 extno = (sword << L2EXTSPERSUM) + rem;
1985 /* initialize the new extent.
1987 rc = diNewExt(imap, iagp, extno);
1988 IREAD_UNLOCK(imap->im_ipimap);
1990 /* something bad happened. if a new iag was allocated,
1991 * place it back on the inode map's iag free list, and
1992 * clear the ag number information.
1994 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
1996 iagp->iagfree = cpu_to_le32(imap->im_freeiag);
1997 imap->im_freeiag = iagno;
1998 IAGFREE_UNLOCK(imap);
2004 /* set the results of the allocation and write the iag.
2006 diInitInode(ip, iagno, extno << L2INOSPEREXT, extno, iagp);
2015 * NAME: diAllocBit(imap,iagp,ino)
2017 * FUNCTION: allocate a backed inode from an iag.
2019 * this routine performs the mechanics of allocating a
2020 * specified inode from a backed extent.
2022 * if the inode to be allocated represents the last free
2023 * inode within the iag, the iag will be removed from the
2024 * ag free inode list.
2026 * a careful update approach is used to provide consistency
2027 * in the face of updates to multiple buffers. under this
2028 * approach, all required buffers are obtained before making
2029 * any updates and are held all are updates are complete.
2031 * PRE CONDITION: Already have buffer lock on iagp. Already have AG lock on
2032 * this AG. Must have read lock on imap inode.
2035 * imap - pointer to inode map control structure.
2036 * iagp - pointer to iag.
2037 * ino - inode number to be allocated within the iag.
2041 * -ENOSPC - insufficient disk resources.
2044 static int diAllocBit(struct inomap * imap, struct iag * iagp, int ino)
2046 int extno, bitno, agno, sword, rc;
2047 struct metapage *amp = NULL, *bmp = NULL;
2048 struct iag *aiagp = NULL, *biagp = NULL;
2051 /* check if this is the last free inode within the iag.
2052 * if so, it will have to be removed from the ag free
2053 * inode list, so get the iags preceeding and following
2056 if (iagp->nfreeinos == cpu_to_le32(1)) {
2057 if ((int) le32_to_cpu(iagp->inofreefwd) >= 0) {
2059 diIAGRead(imap, le32_to_cpu(iagp->inofreefwd),
2062 aiagp = (struct iag *) amp->data;
2065 if ((int) le32_to_cpu(iagp->inofreeback) >= 0) {
2068 le32_to_cpu(iagp->inofreeback),
2071 release_metapage(amp);
2074 biagp = (struct iag *) bmp->data;
2078 /* get the ag number, extent number, inode number within
2081 agno = BLKTOAG(le64_to_cpu(iagp->agstart), JFS_SBI(imap->im_ipimap->i_sb));
2082 extno = ino >> L2INOSPEREXT;
2083 bitno = ino & (INOSPEREXT - 1);
2085 /* compute the mask for setting the map.
2087 mask = HIGHORDER >> bitno;
2089 /* the inode should be free and backed.
2091 if (((le32_to_cpu(iagp->pmap[extno]) & mask) != 0) ||
2092 ((le32_to_cpu(iagp->wmap[extno]) & mask) != 0) ||
2093 (addressPXD(&iagp->inoext[extno]) == 0)) {
2095 release_metapage(amp);
2097 release_metapage(bmp);
2099 jfs_error(imap->im_ipimap->i_sb,
2100 "diAllocBit: iag inconsistent");
2104 /* mark the inode as allocated in the working map.
2106 iagp->wmap[extno] |= cpu_to_le32(mask);
2108 /* check if all inodes within the extent are now
2109 * allocated. if so, update the free inode summary
2110 * map to reflect this.
2112 if (iagp->wmap[extno] == cpu_to_le32(ONES)) {
2113 sword = extno >> L2EXTSPERSUM;
2114 bitno = extno & (EXTSPERSUM - 1);
2115 iagp->inosmap[sword] |= cpu_to_le32(HIGHORDER >> bitno);
2118 /* if this was the last free inode in the iag, remove the
2119 * iag from the ag free inode list.
2121 if (iagp->nfreeinos == cpu_to_le32(1)) {
2123 aiagp->inofreeback = iagp->inofreeback;
2124 write_metapage(amp);
2128 biagp->inofreefwd = iagp->inofreefwd;
2129 write_metapage(bmp);
2131 imap->im_agctl[agno].inofree =
2132 le32_to_cpu(iagp->inofreefwd);
2134 iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
2137 /* update the free inode count at the iag, ag, inode
2140 iagp->nfreeinos = cpu_to_le32(le32_to_cpu(iagp->nfreeinos) - 1);
2141 imap->im_agctl[agno].numfree -= 1;
2142 atomic_dec(&imap->im_numfree);
2149 * NAME: diNewExt(imap,iagp,extno)
2151 * FUNCTION: initialize a new extent of inodes for an iag, allocating
2152 * the first inode of the extent for use for the current
2153 * allocation request.
2155 * disk resources are allocated for the new extent of inodes
2156 * and the inodes themselves are initialized to reflect their
2157 * existence within the extent (i.e. their inode numbers and
2158 * inode extent addresses are set) and their initial state
2159 * (mode and link count are set to zero).
2161 * if the iag is new, it is not yet on an ag extent free list
2162 * but will now be placed on this list.
2164 * if the allocation of the new extent causes the iag to
2165 * have no free extent, the iag will be removed from the
2166 * ag extent free list.
2168 * if the iag has no free backed inodes, it will be placed
2169 * on the ag free inode list, since the addition of the new
2170 * extent will now cause it to have free inodes.
2172 * a careful update approach is used to provide consistency
2173 * (i.e. list consistency) in the face of updates to multiple
2174 * buffers. under this approach, all required buffers are
2175 * obtained before making any updates and are held until all
2176 * updates are complete.
2178 * PRE CONDITION: Already have buffer lock on iagp. Already have AG lock on
2179 * this AG. Must have read lock on imap inode.
2182 * imap - pointer to inode map control structure.
2183 * iagp - pointer to iag.
2184 * extno - extent number.
2188 * -ENOSPC - insufficient disk resources.
2191 static int diNewExt(struct inomap * imap, struct iag * iagp, int extno)
2193 int agno, iagno, fwd, back, freei = 0, sword, rc;
2194 struct iag *aiagp = NULL, *biagp = NULL, *ciagp = NULL;
2195 struct metapage *amp, *bmp, *cmp, *dmp;
2196 struct inode *ipimap;
2202 struct jfs_sb_info *sbi;
2204 /* better have free extents.
2206 if (!iagp->nfreeexts) {
2207 jfs_error(imap->im_ipimap->i_sb, "diNewExt: no free extents");
2211 /* get the inode map inode.
2213 ipimap = imap->im_ipimap;
2214 sbi = JFS_SBI(ipimap->i_sb);
2216 amp = bmp = cmp = NULL;
2218 /* get the ag and iag numbers for this iag.
2220 agno = BLKTOAG(le64_to_cpu(iagp->agstart), sbi);
2221 iagno = le32_to_cpu(iagp->iagnum);
2223 /* check if this is the last free extent within the
2224 * iag. if so, the iag must be removed from the ag
2225 * free extent list, so get the iags preceeding and
2226 * following the iag on this list.
2228 if (iagp->nfreeexts == cpu_to_le32(1)) {
2229 if ((fwd = le32_to_cpu(iagp->extfreefwd)) >= 0) {
2230 if ((rc = diIAGRead(imap, fwd, &)))
2232 aiagp = (struct iag *) amp->data;
2235 if ((back = le32_to_cpu(iagp->extfreeback)) >= 0) {
2236 if ((rc = diIAGRead(imap, back, &bmp)))
2238 biagp = (struct iag *) bmp->data;
2241 /* the iag has free extents. if all extents are free
2242 * (as is the case for a newly allocated iag), the iag
2243 * must be added to the ag free extent list, so get
2244 * the iag at the head of the list in preparation for
2245 * adding this iag to this list.
2248 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2249 if ((fwd = imap->im_agctl[agno].extfree) >= 0) {
2250 if ((rc = diIAGRead(imap, fwd, &)))
2252 aiagp = (struct iag *) amp->data;
2257 /* check if the iag has no free inodes. if so, the iag
2258 * will have to be added to the ag free inode list, so get
2259 * the iag at the head of the list in preparation for
2260 * adding this iag to this list. in doing this, we must
2261 * check if we already have the iag at the head of
2264 if (iagp->nfreeinos == 0) {
2265 freei = imap->im_agctl[agno].inofree;
2270 } else if (freei == back) {
2273 if ((rc = diIAGRead(imap, freei, &cmp)))
2275 ciagp = (struct iag *) cmp->data;
2277 if (ciagp == NULL) {
2278 jfs_error(imap->im_ipimap->i_sb,
2279 "diNewExt: ciagp == NULL");
2286 /* allocate disk space for the inode extent.
2288 if ((extno == 0) || (addressPXD(&iagp->inoext[extno - 1]) == 0))
2289 hint = ((s64) agno << sbi->bmap->db_agl2size) - 1;
2291 hint = addressPXD(&iagp->inoext[extno - 1]) +
2292 lengthPXD(&iagp->inoext[extno - 1]) - 1;
2294 if ((rc = dbAlloc(ipimap, hint, (s64) imap->im_nbperiext, &blkno)))
2297 /* compute the inode number of the first inode within the
2300 ino = (iagno << L2INOSPERIAG) + (extno << L2INOSPEREXT);
2302 /* initialize the inodes within the newly allocated extent a
2305 for (i = 0; i < imap->im_nbperiext; i += sbi->nbperpage) {
2306 /* get a buffer for this page of disk inodes.
2308 dmp = get_metapage(ipimap, blkno + i, PSIZE, 1);
2313 dp = (struct dinode *) dmp->data;
2315 /* initialize the inode number, mode, link count and
2316 * inode extent address.
2318 for (j = 0; j < INOSPERPAGE; j++, dp++, ino++) {
2319 dp->di_inostamp = cpu_to_le32(sbi->inostamp);
2320 dp->di_number = cpu_to_le32(ino);
2321 dp->di_fileset = cpu_to_le32(FILESYSTEM_I);
2324 PXDaddress(&(dp->di_ixpxd), blkno);
2325 PXDlength(&(dp->di_ixpxd), imap->im_nbperiext);
2327 write_metapage(dmp);
2330 /* if this is the last free extent within the iag, remove the
2331 * iag from the ag free extent list.
2333 if (iagp->nfreeexts == cpu_to_le32(1)) {
2335 aiagp->extfreeback = iagp->extfreeback;
2338 biagp->extfreefwd = iagp->extfreefwd;
2340 imap->im_agctl[agno].extfree =
2341 le32_to_cpu(iagp->extfreefwd);
2343 iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
2345 /* if the iag has all free extents (newly allocated iag),
2346 * add the iag to the ag free extent list.
2348 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2350 aiagp->extfreeback = cpu_to_le32(iagno);
2352 iagp->extfreefwd = cpu_to_le32(fwd);
2353 iagp->extfreeback = cpu_to_le32(-1);
2354 imap->im_agctl[agno].extfree = iagno;
2358 /* if the iag has no free inodes, add the iag to the
2359 * ag free inode list.
2361 if (iagp->nfreeinos == 0) {
2363 ciagp->inofreeback = cpu_to_le32(iagno);
2366 cpu_to_le32(imap->im_agctl[agno].inofree);
2367 iagp->inofreeback = cpu_to_le32(-1);
2368 imap->im_agctl[agno].inofree = iagno;
2371 /* initialize the extent descriptor of the extent. */
2372 PXDlength(&iagp->inoext[extno], imap->im_nbperiext);
2373 PXDaddress(&iagp->inoext[extno], blkno);
2375 /* initialize the working and persistent map of the extent.
2376 * the working map will be initialized such that
2377 * it indicates the first inode of the extent is allocated.
2379 iagp->wmap[extno] = cpu_to_le32(HIGHORDER);
2380 iagp->pmap[extno] = 0;
2382 /* update the free inode and free extent summary maps
2383 * for the extent to indicate the extent has free inodes
2384 * and no longer represents a free extent.
2386 sword = extno >> L2EXTSPERSUM;
2387 mask = HIGHORDER >> (extno & (EXTSPERSUM - 1));
2388 iagp->extsmap[sword] |= cpu_to_le32(mask);
2389 iagp->inosmap[sword] &= cpu_to_le32(~mask);
2391 /* update the free inode and free extent counts for the
2394 iagp->nfreeinos = cpu_to_le32(le32_to_cpu(iagp->nfreeinos) +
2396 iagp->nfreeexts = cpu_to_le32(le32_to_cpu(iagp->nfreeexts) - 1);
2398 /* update the free and backed inode counts for the ag.
2400 imap->im_agctl[agno].numfree += (INOSPEREXT - 1);
2401 imap->im_agctl[agno].numinos += INOSPEREXT;
2403 /* update the free and backed inode counts for the inode map.
2405 atomic_add(INOSPEREXT - 1, &imap->im_numfree);
2406 atomic_add(INOSPEREXT, &imap->im_numinos);
2411 write_metapage(amp);
2413 write_metapage(bmp);
2415 write_metapage(cmp);
2421 /* release the iags.
2424 release_metapage(amp);
2426 release_metapage(bmp);
2428 release_metapage(cmp);
2435 * NAME: diNewIAG(imap,iagnop,agno)
2437 * FUNCTION: allocate a new iag for an allocation group.
2439 * first tries to allocate the iag from the inode map
2441 * if the list has free iags, the head of the list is removed
2442 * and returned to satisfy the request.
2443 * if the inode map's iag free list is empty, the inode map
2444 * is extended to hold a new iag. this new iag is initialized
2445 * and returned to satisfy the request.
2448 * imap - pointer to inode map control structure.
2449 * iagnop - pointer to an iag number set with the number of the
2450 * newly allocated iag upon successful return.
2451 * agno - allocation group number.
2452 * bpp - Buffer pointer to be filled in with new IAG's buffer
2456 * -ENOSPC - insufficient disk resources.
2460 * AG lock held on entry/exit;
2461 * write lock on the map is held inside;
2462 * read lock on the map is held on successful completion;
2464 * note: new iag transaction:
2465 * . synchronously write iag;
2466 * . write log of xtree and inode of imap;
2468 * . synchronous write of xtree (right to left, bottom to top);
2469 * . at start of logredo(): init in-memory imap with one additional iag page;
2470 * . at end of logredo(): re-read imap inode to determine
2474 diNewIAG(struct inomap * imap, int *iagnop, int agno, struct metapage ** mpp)
2478 struct inode *ipimap;
2479 struct super_block *sb;
2480 struct jfs_sb_info *sbi;
2481 struct metapage *mp;
2486 #ifdef _STILL_TO_PORT
2488 #endif /* _STILL_TO_PORT */
2489 struct inode *iplist[1];
2491 /* pick up pointers to the inode map and mount inodes */
2492 ipimap = imap->im_ipimap;
2496 /* acquire the free iag lock */
2499 /* if there are any iags on the inode map free iag list,
2500 * allocate the iag from the head of the list.
2502 if (imap->im_freeiag >= 0) {
2503 /* pick up the iag number at the head of the list */
2504 iagno = imap->im_freeiag;
2506 /* determine the logical block number of the iag */
2507 blkno = IAGTOLBLK(iagno, sbi->l2nbperpage);
2509 /* no free iags. the inode map will have to be extented
2510 * to include a new iag.
2513 /* acquire inode map lock */
2514 IWRITE_LOCK(ipimap);
2516 if (ipimap->i_size >> L2PSIZE != imap->im_nextiag + 1) {
2517 IWRITE_UNLOCK(ipimap);
2518 IAGFREE_UNLOCK(imap);
2519 jfs_error(imap->im_ipimap->i_sb,
2520 "diNewIAG: ipimap->i_size is wrong");
2525 /* get the next avaliable iag number */
2526 iagno = imap->im_nextiag;
2528 /* make sure that we have not exceeded the maximum inode
2531 if (iagno > (MAXIAGS - 1)) {
2532 /* release the inode map lock */
2533 IWRITE_UNLOCK(ipimap);
2540 * synchronously append new iag page.
2542 /* determine the logical address of iag page to append */
2543 blkno = IAGTOLBLK(iagno, sbi->l2nbperpage);
2545 /* Allocate extent for new iag page */
2546 xlen = sbi->nbperpage;
2547 if ((rc = dbAlloc(ipimap, 0, (s64) xlen, &xaddr))) {
2548 /* release the inode map lock */
2549 IWRITE_UNLOCK(ipimap);
2555 * start transaction of update of the inode map
2556 * addressing structure pointing to the new iag page;
2558 tid = txBegin(sb, COMMIT_FORCE);
2559 down(&JFS_IP(ipimap)->commit_sem);
2561 /* update the inode map addressing structure to point to it */
2563 xtInsert(tid, ipimap, 0, blkno, xlen, &xaddr, 0))) {
2565 up(&JFS_IP(ipimap)->commit_sem);
2566 /* Free the blocks allocated for the iag since it was
2567 * not successfully added to the inode map
2569 dbFree(ipimap, xaddr, (s64) xlen);
2571 /* release the inode map lock */
2572 IWRITE_UNLOCK(ipimap);
2577 /* update the inode map's inode to reflect the extension */
2578 ipimap->i_size += PSIZE;
2579 inode_add_bytes(ipimap, PSIZE);
2581 /* assign a buffer for the page */
2582 mp = get_metapage(ipimap, blkno, PSIZE, 0);
2585 * This is very unlikely since we just created the
2586 * extent, but let's try to handle it correctly
2588 xtTruncate(tid, ipimap, ipimap->i_size - PSIZE,
2594 /* release the inode map lock */
2595 IWRITE_UNLOCK(ipimap);
2600 iagp = (struct iag *) mp->data;
2603 memset(iagp, 0, sizeof(struct iag));
2604 iagp->iagnum = cpu_to_le32(iagno);
2605 iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
2606 iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
2607 iagp->iagfree = cpu_to_le32(-1);
2608 iagp->nfreeinos = 0;
2609 iagp->nfreeexts = cpu_to_le32(EXTSPERIAG);
2611 /* initialize the free inode summary map (free extent
2612 * summary map initialization handled by bzero).
2614 for (i = 0; i < SMAPSZ; i++)
2615 iagp->inosmap[i] = cpu_to_le32(ONES);
2618 * Write and sync the metapage
2623 * txCommit(COMMIT_FORCE) will synchronously write address
2624 * index pages and inode after commit in careful update order
2625 * of address index pages (right to left, bottom up);
2628 rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
2631 up(&JFS_IP(ipimap)->commit_sem);
2633 duplicateIXtree(sb, blkno, xlen, &xaddr);
2635 /* update the next avaliable iag number */
2636 imap->im_nextiag += 1;
2638 /* Add the iag to the iag free list so we don't lose the iag
2639 * if a failure happens now.
2641 imap->im_freeiag = iagno;
2643 /* Until we have logredo working, we want the imap inode &
2644 * control page to be up to date.
2648 /* release the inode map lock */
2649 IWRITE_UNLOCK(ipimap);
2652 /* obtain read lock on map */
2656 if ((rc = diIAGRead(imap, iagno, &mp))) {
2657 IREAD_UNLOCK(ipimap);
2661 iagp = (struct iag *) mp->data;
2663 /* remove the iag from the iag free list */
2664 imap->im_freeiag = le32_to_cpu(iagp->iagfree);
2665 iagp->iagfree = cpu_to_le32(-1);
2667 /* set the return iag number and buffer pointer */
2672 /* release the iag free lock */
2673 IAGFREE_UNLOCK(imap);
2681 * FUNCTION: get the buffer for the specified iag within a fileset
2682 * or aggregate inode map.
2685 * imap - pointer to inode map control structure.
2686 * iagno - iag number.
2687 * bpp - point to buffer pointer to be filled in on successful
2691 * must have read lock on imap inode
2692 * (When called by diExtendFS, the filesystem is quiesced, therefore
2693 * the read lock is unnecessary.)
2699 static int diIAGRead(struct inomap * imap, int iagno, struct metapage ** mpp)
2701 struct inode *ipimap = imap->im_ipimap;
2704 /* compute the logical block number of the iag. */
2705 blkno = IAGTOLBLK(iagno, JFS_SBI(ipimap->i_sb)->l2nbperpage);
2708 *mpp = read_metapage(ipimap, blkno, PSIZE, 0);
2717 * NAME: diFindFree()
2719 * FUNCTION: find the first free bit in a word starting at
2720 * the specified bit position.
2723 * word - word to be examined.
2724 * start - starting bit position.
2727 * bit position of first free bit in the word or 32 if
2728 * no free bits were found.
2730 static int diFindFree(u32 word, int start)
2734 /* scan the word for the first free bit. */
2735 for (word <<= start, bitno = start; bitno < 32;
2736 bitno++, word <<= 1) {
2737 if ((word & HIGHORDER) == 0)
2744 * NAME: diUpdatePMap()
2746 * FUNCTION: Update the persistent map in an IAG for the allocation or
2747 * freeing of the specified inode.
2749 * PRE CONDITIONS: Working map has already been updated for allocate.
2752 * ipimap - Incore inode map inode
2753 * inum - Number of inode to mark in permanent map
2754 * is_free - If TRUE indicates inode should be marked freed, otherwise
2755 * indicates inode should be marked allocated.
2761 diUpdatePMap(struct inode *ipimap,
2762 unsigned long inum, boolean_t is_free, struct tblock * tblk)
2766 struct metapage *mp;
2767 int iagno, ino, extno, bitno;
2768 struct inomap *imap;
2770 struct jfs_log *log;
2771 int lsn, difft, diffp;
2772 unsigned long flags;
2774 imap = JFS_IP(ipimap)->i_imap;
2775 /* get the iag number containing the inode */
2776 iagno = INOTOIAG(inum);
2777 /* make sure that the iag is contained within the map */
2778 if (iagno >= imap->im_nextiag) {
2779 jfs_error(ipimap->i_sb,
2780 "diUpdatePMap: the iag is outside the map");
2785 rc = diIAGRead(imap, iagno, &mp);
2786 IREAD_UNLOCK(ipimap);
2789 metapage_wait_for_io(mp);
2790 iagp = (struct iag *) mp->data;
2791 /* get the inode number and extent number of the inode within
2792 * the iag and the inode number within the extent.
2794 ino = inum & (INOSPERIAG - 1);
2795 extno = ino >> L2INOSPEREXT;
2796 bitno = ino & (INOSPEREXT - 1);
2797 mask = HIGHORDER >> bitno;
2799 * mark the inode free in persistent map:
2801 if (is_free == TRUE) {
2802 /* The inode should have been allocated both in working
2803 * map and in persistent map;
2804 * the inode will be freed from working map at the release
2805 * of last reference release;
2807 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
2808 jfs_error(ipimap->i_sb,
2809 "diUpdatePMap: inode %ld not marked as "
2810 "allocated in wmap!", inum);
2812 if (!(le32_to_cpu(iagp->pmap[extno]) & mask)) {
2813 jfs_error(ipimap->i_sb,
2814 "diUpdatePMap: inode %ld not marked as "
2815 "allocated in pmap!", inum);
2817 /* update the bitmap for the extent of the freed inode */
2818 iagp->pmap[extno] &= cpu_to_le32(~mask);
2821 * mark the inode allocated in persistent map:
2824 /* The inode should be already allocated in the working map
2825 * and should be free in persistent map;
2827 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
2828 release_metapage(mp);
2829 jfs_error(ipimap->i_sb,
2830 "diUpdatePMap: the inode is not allocated in "
2834 if ((le32_to_cpu(iagp->pmap[extno]) & mask) != 0) {
2835 release_metapage(mp);
2836 jfs_error(ipimap->i_sb,
2837 "diUpdatePMap: the inode is not free in the "
2841 /* update the bitmap for the extent of the allocated inode */
2842 iagp->pmap[extno] |= cpu_to_le32(mask);
2848 log = JFS_SBI(tblk->sb)->log;
2850 /* inherit older/smaller lsn */
2851 logdiff(difft, lsn, log);
2852 logdiff(diffp, mp->lsn, log);
2853 LOGSYNC_LOCK(log, flags);
2854 if (difft < diffp) {
2856 /* move mp after tblock in logsync list */
2857 list_move(&mp->synclist, &tblk->synclist);
2859 /* inherit younger/larger clsn */
2861 logdiff(difft, tblk->clsn, log);
2862 logdiff(diffp, mp->clsn, log);
2864 mp->clsn = tblk->clsn;
2865 LOGSYNC_UNLOCK(log, flags);
2869 /* insert mp after tblock in logsync list */
2870 LOGSYNC_LOCK(log, flags);
2872 list_add(&mp->synclist, &tblk->synclist);
2873 mp->clsn = tblk->clsn;
2874 LOGSYNC_UNLOCK(log, flags);
2883 * function: update imap for extendfs();
2885 * note: AG size has been increased s.t. each k old contiguous AGs are
2886 * coalesced into a new AG;
2888 int diExtendFS(struct inode *ipimap, struct inode *ipbmap)
2891 struct inomap *imap = JFS_IP(ipimap)->i_imap;
2892 struct iag *iagp = NULL, *hiagp = NULL;
2893 struct bmap *mp = JFS_SBI(ipbmap->i_sb)->bmap;
2894 struct metapage *bp, *hbp;
2896 int numinos, xnuminos = 0, xnumfree = 0;
2899 jfs_info("diExtendFS: nextiag:%d numinos:%d numfree:%d",
2900 imap->im_nextiag, atomic_read(&imap->im_numinos),
2901 atomic_read(&imap->im_numfree));
2906 * coalesce contiguous k (newAGSize/oldAGSize) AGs;
2907 * i.e., (AGi, ..., AGj) where i = k*n and j = k*(n+1) - 1 to AGn;
2908 * note: new AG size = old AG size * (2**x).
2911 /* init per AG control information im_agctl[] */
2912 for (i = 0; i < MAXAG; i++) {
2913 imap->im_agctl[i].inofree = -1;
2914 imap->im_agctl[i].extfree = -1;
2915 imap->im_agctl[i].numinos = 0; /* number of backed inodes */
2916 imap->im_agctl[i].numfree = 0; /* number of free backed inodes */
2920 * process each iag page of the map.
2922 * rebuild AG Free Inode List, AG Free Inode Extent List;
2924 for (i = 0; i < imap->im_nextiag; i++) {
2925 if ((rc = diIAGRead(imap, i, &bp))) {
2929 iagp = (struct iag *) bp->data;
2930 if (le32_to_cpu(iagp->iagnum) != i) {
2931 release_metapage(bp);
2932 jfs_error(ipimap->i_sb,
2933 "diExtendFs: unexpected value of iagnum");
2937 /* leave free iag in the free iag list */
2938 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2939 release_metapage(bp);
2943 /* agstart that computes to the same ag is treated as same; */
2944 agstart = le64_to_cpu(iagp->agstart);
2945 /* iagp->agstart = agstart & ~(mp->db_agsize - 1); */
2946 n = agstart >> mp->db_agl2size;
2948 /* compute backed inodes */
2949 numinos = (EXTSPERIAG - le32_to_cpu(iagp->nfreeexts))
2952 /* merge AG backed inodes */
2953 imap->im_agctl[n].numinos += numinos;
2954 xnuminos += numinos;
2957 /* if any backed free inodes, insert at AG free inode list */
2958 if ((int) le32_to_cpu(iagp->nfreeinos) > 0) {
2959 if ((head = imap->im_agctl[n].inofree) == -1) {
2960 iagp->inofreefwd = cpu_to_le32(-1);
2961 iagp->inofreeback = cpu_to_le32(-1);
2963 if ((rc = diIAGRead(imap, head, &hbp))) {
2967 hiagp = (struct iag *) hbp->data;
2968 hiagp->inofreeback = iagp->iagnum;
2969 iagp->inofreefwd = cpu_to_le32(head);
2970 iagp->inofreeback = cpu_to_le32(-1);
2971 write_metapage(hbp);
2974 imap->im_agctl[n].inofree =
2975 le32_to_cpu(iagp->iagnum);
2977 /* merge AG backed free inodes */
2978 imap->im_agctl[n].numfree +=
2979 le32_to_cpu(iagp->nfreeinos);
2980 xnumfree += le32_to_cpu(iagp->nfreeinos);
2983 /* if any free extents, insert at AG free extent list */
2984 if (le32_to_cpu(iagp->nfreeexts) > 0) {
2985 if ((head = imap->im_agctl[n].extfree) == -1) {
2986 iagp->extfreefwd = cpu_to_le32(-1);
2987 iagp->extfreeback = cpu_to_le32(-1);
2989 if ((rc = diIAGRead(imap, head, &hbp))) {
2993 hiagp = (struct iag *) hbp->data;
2994 hiagp->extfreeback = iagp->iagnum;
2995 iagp->extfreefwd = cpu_to_le32(head);
2996 iagp->extfreeback = cpu_to_le32(-1);
2997 write_metapage(hbp);
3000 imap->im_agctl[n].extfree =
3001 le32_to_cpu(iagp->iagnum);
3008 if (xnuminos != atomic_read(&imap->im_numinos) ||
3009 xnumfree != atomic_read(&imap->im_numfree)) {
3010 jfs_error(ipimap->i_sb,
3011 "diExtendFs: numinos or numfree incorrect");
3022 * serialization: IWRITE_LOCK held on entry/exit
3024 * note: shadow page with regular inode (rel.2);
3026 static void duplicateIXtree(struct super_block *sb, s64 blkno,
3027 int xlen, s64 *xaddr)
3029 struct jfs_superblock *j_sb;
3030 struct buffer_head *bh;
3034 /* if AIT2 ipmap2 is bad, do not try to update it */
3035 if (JFS_SBI(sb)->mntflag & JFS_BAD_SAIT) /* s_flag */
3037 ip = diReadSpecial(sb, FILESYSTEM_I, 1);
3039 JFS_SBI(sb)->mntflag |= JFS_BAD_SAIT;
3040 if (readSuper(sb, &bh))
3042 j_sb = (struct jfs_superblock *)bh->b_data;
3043 j_sb->s_flag |= cpu_to_le32(JFS_BAD_SAIT);
3045 mark_buffer_dirty(bh);
3046 sync_dirty_buffer(bh);
3051 /* start transaction */
3052 tid = txBegin(sb, COMMIT_FORCE);
3053 /* update the inode map addressing structure to point to it */
3054 if (xtInsert(tid, ip, 0, blkno, xlen, xaddr, 0)) {
3055 JFS_SBI(sb)->mntflag |= JFS_BAD_SAIT;
3060 /* update the inode map's inode to reflect the extension */
3061 ip->i_size += PSIZE;
3062 inode_add_bytes(ip, PSIZE);
3063 txCommit(tid, 1, &ip, COMMIT_FORCE);
3070 * NAME: copy_from_dinode()
3072 * FUNCTION: Copies inode info from disk inode to in-memory inode
3076 * -ENOMEM - insufficient memory
3078 static int copy_from_dinode(struct dinode * dip, struct inode *ip)
3080 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
3082 jfs_ip->fileset = le32_to_cpu(dip->di_fileset);
3083 jfs_ip->mode2 = le32_to_cpu(dip->di_mode);
3085 ip->i_mode = le32_to_cpu(dip->di_mode) & 0xffff;
3086 ip->i_nlink = le32_to_cpu(dip->di_nlink);
3087 ip->i_uid = le32_to_cpu(dip->di_uid);
3088 ip->i_gid = le32_to_cpu(dip->di_gid);
3089 ip->i_size = le64_to_cpu(dip->di_size);
3090 ip->i_atime.tv_sec = le32_to_cpu(dip->di_atime.tv_sec);
3091 ip->i_atime.tv_nsec = le32_to_cpu(dip->di_atime.tv_nsec);
3092 ip->i_mtime.tv_sec = le32_to_cpu(dip->di_mtime.tv_sec);
3093 ip->i_mtime.tv_nsec = le32_to_cpu(dip->di_mtime.tv_nsec);
3094 ip->i_ctime.tv_sec = le32_to_cpu(dip->di_ctime.tv_sec);
3095 ip->i_ctime.tv_nsec = le32_to_cpu(dip->di_ctime.tv_nsec);
3096 ip->i_blksize = ip->i_sb->s_blocksize;
3097 ip->i_blocks = LBLK2PBLK(ip->i_sb, le64_to_cpu(dip->di_nblocks));
3098 ip->i_generation = le32_to_cpu(dip->di_gen);
3100 jfs_ip->ixpxd = dip->di_ixpxd; /* in-memory pxd's are little-endian */
3101 jfs_ip->acl = dip->di_acl; /* as are dxd's */
3102 jfs_ip->ea = dip->di_ea;
3103 jfs_ip->next_index = le32_to_cpu(dip->di_next_index);
3104 jfs_ip->otime = le32_to_cpu(dip->di_otime.tv_sec);
3105 jfs_ip->acltype = le32_to_cpu(dip->di_acltype);
3107 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode)) {
3108 jfs_ip->dev = le32_to_cpu(dip->di_rdev);
3109 ip->i_rdev = new_decode_dev(jfs_ip->dev);
3112 if (S_ISDIR(ip->i_mode)) {
3113 memcpy(&jfs_ip->i_dirtable, &dip->di_dirtable, 384);
3114 } else if (S_ISREG(ip->i_mode) || S_ISLNK(ip->i_mode)) {
3115 memcpy(&jfs_ip->i_xtroot, &dip->di_xtroot, 288);
3117 memcpy(&jfs_ip->i_inline_ea, &dip->di_inlineea, 128);
3119 /* Zero the in-memory-only stuff */
3121 jfs_ip->btindex = 0;
3122 jfs_ip->btorder = 0;
3125 jfs_ip->atlhead = 0;
3126 jfs_ip->atltail = 0;
3132 * NAME: copy_to_dinode()
3134 * FUNCTION: Copies inode info from in-memory inode to disk inode
3136 static void copy_to_dinode(struct dinode * dip, struct inode *ip)
3138 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
3140 dip->di_fileset = cpu_to_le32(jfs_ip->fileset);
3141 dip->di_inostamp = cpu_to_le32(JFS_SBI(ip->i_sb)->inostamp);
3142 dip->di_number = cpu_to_le32(ip->i_ino);
3143 dip->di_gen = cpu_to_le32(ip->i_generation);
3144 dip->di_size = cpu_to_le64(ip->i_size);
3145 dip->di_nblocks = cpu_to_le64(PBLK2LBLK(ip->i_sb, ip->i_blocks));
3146 dip->di_nlink = cpu_to_le32(ip->i_nlink);
3147 dip->di_uid = cpu_to_le32(ip->i_uid);
3148 dip->di_gid = cpu_to_le32(ip->i_gid);
3150 * mode2 is only needed for storing the higher order bits.
3151 * Trust i_mode for the lower order ones
3153 dip->di_mode = cpu_to_le32((jfs_ip->mode2 & 0xffff0000) | ip->i_mode);
3154 dip->di_atime.tv_sec = cpu_to_le32(ip->i_atime.tv_sec);
3155 dip->di_atime.tv_nsec = cpu_to_le32(ip->i_atime.tv_nsec);
3156 dip->di_ctime.tv_sec = cpu_to_le32(ip->i_ctime.tv_sec);
3157 dip->di_ctime.tv_nsec = cpu_to_le32(ip->i_ctime.tv_nsec);
3158 dip->di_mtime.tv_sec = cpu_to_le32(ip->i_mtime.tv_sec);
3159 dip->di_mtime.tv_nsec = cpu_to_le32(ip->i_mtime.tv_nsec);
3160 dip->di_ixpxd = jfs_ip->ixpxd; /* in-memory pxd's are little-endian */
3161 dip->di_acl = jfs_ip->acl; /* as are dxd's */
3162 dip->di_ea = jfs_ip->ea;
3163 dip->di_next_index = cpu_to_le32(jfs_ip->next_index);
3164 dip->di_otime.tv_sec = cpu_to_le32(jfs_ip->otime);
3165 dip->di_otime.tv_nsec = 0;
3166 dip->di_acltype = cpu_to_le32(jfs_ip->acltype);
3167 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode))
3168 dip->di_rdev = cpu_to_le32(jfs_ip->dev);