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_inode.h"
52 #include "jfs_filsys.h"
53 #include "jfs_dinode.h"
56 #include "jfs_metapage.h"
57 #include "jfs_superblock.h"
58 #include "jfs_debug.h"
61 * __mark_inode_dirty expects inodes to be hashed. Since we don't want
62 * special inodes in the fileset inode space, we hash them to a dummy head
64 static HLIST_HEAD(aggregate_hash);
69 /* iag free list lock */
70 #define IAGFREE_LOCK_INIT(imap) init_MUTEX(&imap->im_freelock)
71 #define IAGFREE_LOCK(imap) down(&imap->im_freelock)
72 #define IAGFREE_UNLOCK(imap) up(&imap->im_freelock)
74 /* per ag iag list locks */
75 #define AG_LOCK_INIT(imap,index) init_MUTEX(&(imap->im_aglock[index]))
76 #define AG_LOCK(imap,agno) down(&imap->im_aglock[agno])
77 #define AG_UNLOCK(imap,agno) up(&imap->im_aglock[agno])
82 static int diAllocAG(struct inomap *, int, boolean_t, struct inode *);
83 static int diAllocAny(struct inomap *, int, boolean_t, struct inode *);
84 static int diAllocBit(struct inomap *, struct iag *, int);
85 static int diAllocExt(struct inomap *, int, struct inode *);
86 static int diAllocIno(struct inomap *, int, struct inode *);
87 static int diFindFree(u32, int);
88 static int diNewExt(struct inomap *, struct iag *, int);
89 static int diNewIAG(struct inomap *, int *, int, struct metapage **);
90 static void duplicateIXtree(struct super_block *, s64, int, s64 *);
92 static int diIAGRead(struct inomap * imap, int, struct metapage **);
93 static int copy_from_dinode(struct dinode *, struct inode *);
94 static void copy_to_dinode(struct dinode *, struct inode *);
99 * FUNCTION: initialize the incore inode map control structures for
100 * a fileset or aggregate init time.
102 * the inode map's control structure (dinomap) is
103 * brought in from disk and placed in virtual memory.
106 * ipimap - pointer to inode map inode for the aggregate or fileset.
110 * -ENOMEM - insufficient free virtual memory.
113 int diMount(struct inode *ipimap)
118 struct dinomap_disk *dinom_le;
121 * allocate/initialize the in-memory inode map control structure
123 /* allocate the in-memory inode map control structure. */
124 imap = (struct inomap *) kmalloc(sizeof(struct inomap), GFP_KERNEL);
126 jfs_err("diMount: kmalloc returned NULL!");
130 /* read the on-disk inode map control structure. */
132 mp = read_metapage(ipimap,
133 IMAPBLKNO << JFS_SBI(ipimap->i_sb)->l2nbperpage,
140 /* copy the on-disk version to the in-memory version. */
141 dinom_le = (struct dinomap_disk *) mp->data;
142 imap->im_freeiag = le32_to_cpu(dinom_le->in_freeiag);
143 imap->im_nextiag = le32_to_cpu(dinom_le->in_nextiag);
144 atomic_set(&imap->im_numinos, le32_to_cpu(dinom_le->in_numinos));
145 atomic_set(&imap->im_numfree, le32_to_cpu(dinom_le->in_numfree));
146 imap->im_nbperiext = le32_to_cpu(dinom_le->in_nbperiext);
147 imap->im_l2nbperiext = le32_to_cpu(dinom_le->in_l2nbperiext);
148 for (index = 0; index < MAXAG; index++) {
149 imap->im_agctl[index].inofree =
150 le32_to_cpu(dinom_le->in_agctl[index].inofree);
151 imap->im_agctl[index].extfree =
152 le32_to_cpu(dinom_le->in_agctl[index].extfree);
153 imap->im_agctl[index].numinos =
154 le32_to_cpu(dinom_le->in_agctl[index].numinos);
155 imap->im_agctl[index].numfree =
156 le32_to_cpu(dinom_le->in_agctl[index].numfree);
159 /* release the buffer. */
160 release_metapage(mp);
163 * allocate/initialize inode allocation map locks
165 /* allocate and init iag free list lock */
166 IAGFREE_LOCK_INIT(imap);
168 /* allocate and init ag list locks */
169 for (index = 0; index < MAXAG; index++) {
170 AG_LOCK_INIT(imap, index);
173 /* bind the inode map inode and inode map control structure
176 imap->im_ipimap = ipimap;
177 JFS_IP(ipimap)->i_imap = imap;
186 * FUNCTION: write to disk the incore inode map control structures for
187 * a fileset or aggregate at unmount time.
190 * ipimap - pointer to inode map inode for the aggregate or fileset.
194 * -ENOMEM - insufficient free virtual memory.
197 int diUnmount(struct inode *ipimap, int mounterror)
199 struct inomap *imap = JFS_IP(ipimap)->i_imap;
202 * update the on-disk inode map control structure
205 if (!(mounterror || isReadOnly(ipimap)))
209 * Invalidate the page cache buffers
211 truncate_inode_pages(ipimap->i_mapping, 0);
214 * free in-memory control structure
225 int diSync(struct inode *ipimap)
227 struct dinomap_disk *dinom_le;
228 struct inomap *imp = JFS_IP(ipimap)->i_imap;
233 * write imap global conrol page
235 /* read the on-disk inode map control structure */
236 mp = get_metapage(ipimap,
237 IMAPBLKNO << JFS_SBI(ipimap->i_sb)->l2nbperpage,
240 jfs_err("diSync: get_metapage failed!");
244 /* copy the in-memory version to the on-disk version */
245 dinom_le = (struct dinomap_disk *) mp->data;
246 dinom_le->in_freeiag = cpu_to_le32(imp->im_freeiag);
247 dinom_le->in_nextiag = cpu_to_le32(imp->im_nextiag);
248 dinom_le->in_numinos = cpu_to_le32(atomic_read(&imp->im_numinos));
249 dinom_le->in_numfree = cpu_to_le32(atomic_read(&imp->im_numfree));
250 dinom_le->in_nbperiext = cpu_to_le32(imp->im_nbperiext);
251 dinom_le->in_l2nbperiext = cpu_to_le32(imp->im_l2nbperiext);
252 for (index = 0; index < MAXAG; index++) {
253 dinom_le->in_agctl[index].inofree =
254 cpu_to_le32(imp->im_agctl[index].inofree);
255 dinom_le->in_agctl[index].extfree =
256 cpu_to_le32(imp->im_agctl[index].extfree);
257 dinom_le->in_agctl[index].numinos =
258 cpu_to_le32(imp->im_agctl[index].numinos);
259 dinom_le->in_agctl[index].numfree =
260 cpu_to_le32(imp->im_agctl[index].numfree);
263 /* write out the control structure */
267 * write out dirty pages of imap
269 filemap_write_and_wait(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;
322 unsigned long pageno;
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_write_and_wait(ip->i_mapping);
569 truncate_inode_pages(ip->i_mapping, 0);
578 * FUNCTION: write the on-disk inode portion of the in-memory inode
579 * to its corresponding on-disk inode.
581 * on entry, the specifed incore inode should itself
582 * specify the disk inode number corresponding to the
583 * incore inode (i.e. i_number should be initialized).
585 * the inode contains the inode extent address for the disk
586 * inode. with the inode extent address in hand, the
587 * page of the extent that contains the disk inode is
588 * read and the disk inode portion of the incore inode
589 * is copied to the disk inode.
592 * tid - transacation id
593 * ip - pointer to incore inode to be written to the inode extent.
599 int diWrite(tid_t tid, struct inode *ip)
601 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
602 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
610 unsigned long pageno;
613 struct inode *ipimap;
616 struct tlock *ditlck, *tlck;
617 struct linelock *dilinelock, *ilinelock;
621 ipimap = jfs_ip->ipimap;
623 ino = ip->i_ino & (INOSPERIAG - 1);
625 if (!addressPXD(&(jfs_ip->ixpxd)) ||
626 (lengthPXD(&(jfs_ip->ixpxd)) !=
627 JFS_IP(ipimap)->i_imap->im_nbperiext)) {
628 jfs_error(ip->i_sb, "diWrite: ixpxd invalid");
633 * read the page of disk inode containing the specified inode:
635 /* compute the block address of the page */
636 blkno = INOPBLK(&(jfs_ip->ixpxd), ino, sbi->l2nbperpage);
638 rel_inode = (ino & (INOSPERPAGE - 1));
639 pageno = blkno >> sbi->l2nbperpage;
641 if ((block_offset = ((u32) blkno & (sbi->nbperpage - 1)))) {
643 * OS/2 didn't always align inode extents on page boundaries
646 (sbi->nbperpage - block_offset) << sbi->l2niperblk;
648 if (rel_inode < inodes_left)
649 rel_inode += block_offset << sbi->l2niperblk;
652 rel_inode -= inodes_left;
655 /* read the page of disk inode */
657 mp = read_metapage(ipimap, pageno << sbi->l2nbperpage, PSIZE, 1);
661 /* get the pointer to the disk inode */
662 dp = (struct dinode *) mp->data;
665 dioffset = (ino & (INOSPERPAGE - 1)) << L2DISIZE;
668 * acquire transaction lock on the on-disk inode;
669 * N.B. tlock is acquired on ipimap not ip;
672 txLock(tid, ipimap, mp, tlckINODE | tlckENTRY)) == NULL)
674 dilinelock = (struct linelock *) & ditlck->lock;
677 * copy btree root from in-memory inode to on-disk inode
679 * (tlock is taken from inline B+-tree root in in-memory
680 * inode when the B+-tree root is updated, which is pointed
681 * by jfs_ip->blid as well as being on tx tlock list)
683 * further processing of btree root is based on the copy
684 * in in-memory inode, where txLog() will log from, and,
685 * for xtree root, txUpdateMap() will update map and reset
689 if (S_ISDIR(ip->i_mode) && (lid = jfs_ip->xtlid)) {
691 * This is the special xtree inside the directory for storing
692 * the directory table
698 tlck = lid_to_tlock(lid);
699 assert(tlck->type & tlckXTREE);
700 tlck->type |= tlckBTROOT;
702 ilinelock = (struct linelock *) & tlck->lock;
705 * copy xtree root from inode to dinode:
707 p = &jfs_ip->i_xtroot;
708 xp = (xtpage_t *) &dp->di_dirtable;
710 for (n = 0; n < ilinelock->index; n++, lv++) {
711 memcpy(&xp->xad[lv->offset], &p->xad[lv->offset],
712 lv->length << L2XTSLOTSIZE);
715 /* reset on-disk (metadata page) xtree XAD_NEW bit */
716 xad = &xp->xad[XTENTRYSTART];
717 for (n = XTENTRYSTART;
718 n < le16_to_cpu(xp->header.nextindex); n++, xad++)
719 if (xad->flag & (XAD_NEW | XAD_EXTENDED))
720 xad->flag &= ~(XAD_NEW | XAD_EXTENDED);
723 if ((lid = jfs_ip->blid) == 0)
727 tlck = lid_to_tlock(lid);
729 tlck->type |= tlckBTROOT;
731 ilinelock = (struct linelock *) & tlck->lock;
734 * regular file: 16 byte (XAD slot) granularity
736 if (type & tlckXTREE) {
741 * copy xtree root from inode to dinode:
743 p = &jfs_ip->i_xtroot;
746 for (n = 0; n < ilinelock->index; n++, lv++) {
747 memcpy(&xp->xad[lv->offset], &p->xad[lv->offset],
748 lv->length << L2XTSLOTSIZE);
751 /* reset on-disk (metadata page) xtree XAD_NEW bit */
752 xad = &xp->xad[XTENTRYSTART];
753 for (n = XTENTRYSTART;
754 n < le16_to_cpu(xp->header.nextindex); n++, xad++)
755 if (xad->flag & (XAD_NEW | XAD_EXTENDED))
756 xad->flag &= ~(XAD_NEW | XAD_EXTENDED);
759 * directory: 32 byte (directory entry slot) granularity
761 else if (type & tlckDTREE) {
765 * copy dtree root from inode to dinode:
767 p = (dtpage_t *) &jfs_ip->i_dtroot;
768 xp = (dtpage_t *) & dp->di_dtroot;
770 for (n = 0; n < ilinelock->index; n++, lv++) {
771 memcpy(&xp->slot[lv->offset], &p->slot[lv->offset],
772 lv->length << L2DTSLOTSIZE);
775 jfs_err("diWrite: UFO tlock");
780 * copy inline symlink from in-memory inode to on-disk inode
782 if (S_ISLNK(ip->i_mode) && ip->i_size < IDATASIZE) {
783 lv = & dilinelock->lv[dilinelock->index];
784 lv->offset = (dioffset + 2 * 128) >> L2INODESLOTSIZE;
786 memcpy(&dp->di_fastsymlink, jfs_ip->i_inline, IDATASIZE);
790 * copy inline data from in-memory inode to on-disk inode:
791 * 128 byte slot granularity
793 if (test_cflag(COMMIT_Inlineea, ip)) {
794 lv = & dilinelock->lv[dilinelock->index];
795 lv->offset = (dioffset + 3 * 128) >> L2INODESLOTSIZE;
797 memcpy(&dp->di_inlineea, jfs_ip->i_inline_ea, INODESLOTSIZE);
800 clear_cflag(COMMIT_Inlineea, ip);
804 * lock/copy inode base: 128 byte slot granularity
807 lv = & dilinelock->lv[dilinelock->index];
808 lv->offset = dioffset >> L2INODESLOTSIZE;
809 copy_to_dinode(dp, ip);
810 if (test_and_clear_cflag(COMMIT_Dirtable, ip)) {
812 memcpy(&dp->di_dirtable, &jfs_ip->i_dirtable, 96);
819 * We aren't logging changes to the DASD used in directory inodes,
820 * but we need to write them to disk. If we don't unmount cleanly,
821 * mount will recalculate the DASD used.
823 if (S_ISDIR(ip->i_mode)
824 && (ip->i_ipmnt->i_mntflag & JFS_DASD_ENABLED))
825 memcpy(&dp->di_DASD, &ip->i_DASD, sizeof(struct dasd));
826 #endif /* _JFS_FASTDASD */
828 /* release the buffer holding the updated on-disk inode.
829 * the buffer will be later written by commit processing.
840 * FUNCTION: free a specified inode from the inode working map
841 * for a fileset or aggregate.
843 * if the inode to be freed represents the first (only)
844 * free inode within the iag, the iag will be placed on
845 * the ag free inode list.
847 * freeing the inode will cause the inode extent to be
848 * freed if the inode is the only allocated inode within
849 * the extent. in this case all the disk resource backing
850 * up the inode extent will be freed. in addition, the iag
851 * will be placed on the ag extent free list if the extent
852 * is the first free extent in the iag. if freeing the
853 * extent also means that no free inodes will exist for
854 * the iag, the iag will also be removed from the ag free
857 * the iag describing the inode will be freed if the extent
858 * is to be freed and it is the only backed extent within
859 * the iag. in this case, the iag will be removed from the
860 * ag free extent list and ag free inode list and placed on
861 * the inode map's free iag list.
863 * a careful update approach is used to provide consistency
864 * in the face of updates to multiple buffers. under this
865 * approach, all required buffers are obtained before making
866 * any updates and are held until all updates are complete.
869 * ip - inode to be freed.
875 int diFree(struct inode *ip)
878 ino_t inum = ip->i_ino;
879 struct iag *iagp, *aiagp, *biagp, *ciagp, *diagp;
880 struct metapage *mp, *amp, *bmp, *cmp, *dmp;
881 int iagno, ino, extno, bitno, sword, agno;
884 struct inode *ipimap = JFS_SBI(ip->i_sb)->ipimap;
885 struct inomap *imap = JFS_IP(ipimap)->i_imap;
888 struct inode *iplist[3];
890 struct pxd_lock *pxdlock;
893 * This is just to suppress compiler warnings. The same logic that
894 * references these variables is used to initialize them.
896 aiagp = biagp = ciagp = diagp = NULL;
898 /* get the iag number containing the inode.
900 iagno = INOTOIAG(inum);
902 /* make sure that the iag is contained within
905 if (iagno >= imap->im_nextiag) {
906 dump_mem("imap", imap, 32);
908 "diFree: inum = %d, iagno = %d, nextiag = %d",
909 (uint) inum, iagno, imap->im_nextiag);
913 /* get the allocation group for this ino.
915 agno = JFS_IP(ip)->agno;
917 /* Lock the AG specific inode map information
921 /* Obtain read lock in imap inode. Don't release it until we have
922 * read all of the IAG's that we are going to.
928 if ((rc = diIAGRead(imap, iagno, &mp))) {
929 IREAD_UNLOCK(ipimap);
930 AG_UNLOCK(imap, agno);
933 iagp = (struct iag *) mp->data;
935 /* get the inode number and extent number of the inode within
936 * the iag and the inode number within the extent.
938 ino = inum & (INOSPERIAG - 1);
939 extno = ino >> L2INOSPEREXT;
940 bitno = ino & (INOSPEREXT - 1);
941 mask = HIGHORDER >> bitno;
943 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
945 "diFree: wmap shows inode already free");
948 if (!addressPXD(&iagp->inoext[extno])) {
949 release_metapage(mp);
950 IREAD_UNLOCK(ipimap);
951 AG_UNLOCK(imap, agno);
952 jfs_error(ip->i_sb, "diFree: invalid inoext");
956 /* compute the bitmap for the extent reflecting the freed inode.
958 bitmap = le32_to_cpu(iagp->wmap[extno]) & ~mask;
960 if (imap->im_agctl[agno].numfree > imap->im_agctl[agno].numinos) {
961 release_metapage(mp);
962 IREAD_UNLOCK(ipimap);
963 AG_UNLOCK(imap, agno);
964 jfs_error(ip->i_sb, "diFree: numfree > numinos");
968 * inode extent still has some inodes or below low water mark:
969 * keep the inode extent;
972 imap->im_agctl[agno].numfree < 96 ||
973 (imap->im_agctl[agno].numfree < 288 &&
974 (((imap->im_agctl[agno].numfree * 100) /
975 imap->im_agctl[agno].numinos) <= 25))) {
976 /* if the iag currently has no free inodes (i.e.,
977 * the inode being freed is the first free inode of iag),
978 * insert the iag at head of the inode free list for the ag.
980 if (iagp->nfreeinos == 0) {
981 /* check if there are any iags on the ag inode
982 * free list. if so, read the first one so that
983 * we can link the current iag onto the list at
986 if ((fwd = imap->im_agctl[agno].inofree) >= 0) {
987 /* read the iag that currently is the head
990 if ((rc = diIAGRead(imap, fwd, &))) {
991 IREAD_UNLOCK(ipimap);
992 AG_UNLOCK(imap, agno);
993 release_metapage(mp);
996 aiagp = (struct iag *) amp->data;
998 /* make current head point back to the iag.
1000 aiagp->inofreeback = cpu_to_le32(iagno);
1002 write_metapage(amp);
1005 /* iag points forward to current head and iag
1006 * becomes the new head of the list.
1009 cpu_to_le32(imap->im_agctl[agno].inofree);
1010 iagp->inofreeback = cpu_to_le32(-1);
1011 imap->im_agctl[agno].inofree = iagno;
1013 IREAD_UNLOCK(ipimap);
1015 /* update the free inode summary map for the extent if
1016 * freeing the inode means the extent will now have free
1017 * inodes (i.e., the inode being freed is the first free
1020 if (iagp->wmap[extno] == cpu_to_le32(ONES)) {
1021 sword = extno >> L2EXTSPERSUM;
1022 bitno = extno & (EXTSPERSUM - 1);
1023 iagp->inosmap[sword] &=
1024 cpu_to_le32(~(HIGHORDER >> bitno));
1027 /* update the bitmap.
1029 iagp->wmap[extno] = cpu_to_le32(bitmap);
1031 /* update the free inode counts at the iag, ag and
1035 cpu_to_le32(le32_to_cpu(iagp->nfreeinos) + 1);
1036 imap->im_agctl[agno].numfree += 1;
1037 atomic_inc(&imap->im_numfree);
1039 /* release the AG inode map lock
1041 AG_UNLOCK(imap, agno);
1051 * inode extent has become free and above low water mark:
1052 * free the inode extent;
1056 * prepare to update iag list(s) (careful update step 1)
1058 amp = bmp = cmp = dmp = NULL;
1061 /* check if the iag currently has no free extents. if so,
1062 * it will be placed on the head of the ag extent free list.
1064 if (iagp->nfreeexts == 0) {
1065 /* check if the ag extent free list has any iags.
1066 * if so, read the iag at the head of the list now.
1067 * this (head) iag will be updated later to reflect
1068 * the addition of the current iag at the head of
1071 if ((fwd = imap->im_agctl[agno].extfree) >= 0) {
1072 if ((rc = diIAGRead(imap, fwd, &)))
1074 aiagp = (struct iag *) amp->data;
1077 /* iag has free extents. check if the addition of a free
1078 * extent will cause all extents to be free within this
1079 * iag. if so, the iag will be removed from the ag extent
1080 * free list and placed on the inode map's free iag list.
1082 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG - 1)) {
1083 /* in preparation for removing the iag from the
1084 * ag extent free list, read the iags preceeding
1085 * and following the iag on the ag extent free
1088 if ((fwd = le32_to_cpu(iagp->extfreefwd)) >= 0) {
1089 if ((rc = diIAGRead(imap, fwd, &)))
1091 aiagp = (struct iag *) amp->data;
1094 if ((back = le32_to_cpu(iagp->extfreeback)) >= 0) {
1095 if ((rc = diIAGRead(imap, back, &bmp)))
1097 biagp = (struct iag *) bmp->data;
1102 /* remove the iag from the ag inode free list if freeing
1103 * this extent cause the iag to have no free inodes.
1105 if (iagp->nfreeinos == cpu_to_le32(INOSPEREXT - 1)) {
1106 int inofreeback = le32_to_cpu(iagp->inofreeback);
1107 int inofreefwd = le32_to_cpu(iagp->inofreefwd);
1109 /* in preparation for removing the iag from the
1110 * ag inode free list, read the iags preceeding
1111 * and following the iag on the ag inode free
1112 * list. before reading these iags, we must make
1113 * sure that we already don't have them in hand
1114 * from up above, since re-reading an iag (buffer)
1115 * we are currently holding would cause a deadlock.
1117 if (inofreefwd >= 0) {
1119 if (inofreefwd == fwd)
1120 ciagp = (struct iag *) amp->data;
1121 else if (inofreefwd == back)
1122 ciagp = (struct iag *) bmp->data;
1125 diIAGRead(imap, inofreefwd, &cmp)))
1127 ciagp = (struct iag *) cmp->data;
1129 assert(ciagp != NULL);
1132 if (inofreeback >= 0) {
1133 if (inofreeback == fwd)
1134 diagp = (struct iag *) amp->data;
1135 else if (inofreeback == back)
1136 diagp = (struct iag *) bmp->data;
1139 diIAGRead(imap, inofreeback, &dmp)))
1141 diagp = (struct iag *) dmp->data;
1143 assert(diagp != NULL);
1147 IREAD_UNLOCK(ipimap);
1150 * invalidate any page of the inode extent freed from buffer cache;
1152 freepxd = iagp->inoext[extno];
1153 invalidate_pxd_metapages(ip, freepxd);
1156 * update iag list(s) (careful update step 2)
1158 /* add the iag to the ag extent free list if this is the
1159 * first free extent for the iag.
1161 if (iagp->nfreeexts == 0) {
1163 aiagp->extfreeback = cpu_to_le32(iagno);
1166 cpu_to_le32(imap->im_agctl[agno].extfree);
1167 iagp->extfreeback = cpu_to_le32(-1);
1168 imap->im_agctl[agno].extfree = iagno;
1170 /* remove the iag from the ag extent list if all extents
1171 * are now free and place it on the inode map iag free list.
1173 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG - 1)) {
1175 aiagp->extfreeback = iagp->extfreeback;
1178 biagp->extfreefwd = iagp->extfreefwd;
1180 imap->im_agctl[agno].extfree =
1181 le32_to_cpu(iagp->extfreefwd);
1183 iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
1186 iagp->iagfree = cpu_to_le32(imap->im_freeiag);
1187 imap->im_freeiag = iagno;
1188 IAGFREE_UNLOCK(imap);
1192 /* remove the iag from the ag inode free list if freeing
1193 * this extent causes the iag to have no free inodes.
1195 if (iagp->nfreeinos == cpu_to_le32(INOSPEREXT - 1)) {
1196 if ((int) le32_to_cpu(iagp->inofreefwd) >= 0)
1197 ciagp->inofreeback = iagp->inofreeback;
1199 if ((int) le32_to_cpu(iagp->inofreeback) >= 0)
1200 diagp->inofreefwd = iagp->inofreefwd;
1202 imap->im_agctl[agno].inofree =
1203 le32_to_cpu(iagp->inofreefwd);
1205 iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
1208 /* update the inode extent address and working map
1209 * to reflect the free extent.
1210 * the permanent map should have been updated already
1211 * for the inode being freed.
1213 if (iagp->pmap[extno] != 0) {
1214 jfs_error(ip->i_sb, "diFree: the pmap does not show inode free");
1216 iagp->wmap[extno] = 0;
1217 PXDlength(&iagp->inoext[extno], 0);
1218 PXDaddress(&iagp->inoext[extno], 0);
1220 /* update the free extent and free inode summary maps
1221 * to reflect the freed extent.
1222 * the inode summary map is marked to indicate no inodes
1223 * available for the freed extent.
1225 sword = extno >> L2EXTSPERSUM;
1226 bitno = extno & (EXTSPERSUM - 1);
1227 mask = HIGHORDER >> bitno;
1228 iagp->inosmap[sword] |= cpu_to_le32(mask);
1229 iagp->extsmap[sword] &= cpu_to_le32(~mask);
1231 /* update the number of free inodes and number of free extents
1234 iagp->nfreeinos = cpu_to_le32(le32_to_cpu(iagp->nfreeinos) -
1236 iagp->nfreeexts = cpu_to_le32(le32_to_cpu(iagp->nfreeexts) + 1);
1238 /* update the number of free inodes and backed inodes
1239 * at the ag and inode map level.
1241 imap->im_agctl[agno].numfree -= (INOSPEREXT - 1);
1242 imap->im_agctl[agno].numinos -= INOSPEREXT;
1243 atomic_sub(INOSPEREXT - 1, &imap->im_numfree);
1244 atomic_sub(INOSPEREXT, &imap->im_numinos);
1247 write_metapage(amp);
1249 write_metapage(bmp);
1251 write_metapage(cmp);
1253 write_metapage(dmp);
1256 * start transaction to update block allocation map
1257 * for the inode extent freed;
1259 * N.B. AG_LOCK is released and iag will be released below, and
1260 * other thread may allocate inode from/reusing the ixad freed
1261 * BUT with new/different backing inode extent from the extent
1262 * to be freed by the transaction;
1264 tid = txBegin(ipimap->i_sb, COMMIT_FORCE);
1265 down(&JFS_IP(ipimap)->commit_sem);
1267 /* acquire tlock of the iag page of the freed ixad
1268 * to force the page NOHOMEOK (even though no data is
1269 * logged from the iag page) until NOREDOPAGE|FREEXTENT log
1270 * for the free of the extent is committed;
1271 * write FREEXTENT|NOREDOPAGE log record
1272 * N.B. linelock is overlaid as freed extent descriptor;
1274 tlck = txLock(tid, ipimap, mp, tlckINODE | tlckFREE);
1275 pxdlock = (struct pxd_lock *) & tlck->lock;
1276 pxdlock->flag = mlckFREEPXD;
1277 pxdlock->pxd = freepxd;
1285 * logredo needs the IAG number and IAG extent index in order
1286 * to ensure that the IMap is consistent. The least disruptive
1287 * way to pass these values through to the transaction manager
1288 * is in the iplist array.
1290 * It's not pretty, but it works.
1292 iplist[1] = (struct inode *) (size_t)iagno;
1293 iplist[2] = (struct inode *) (size_t)extno;
1295 rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
1298 up(&JFS_IP(ipimap)->commit_sem);
1300 /* unlock the AG inode map information */
1301 AG_UNLOCK(imap, agno);
1306 IREAD_UNLOCK(ipimap);
1309 release_metapage(amp);
1311 release_metapage(bmp);
1313 release_metapage(cmp);
1315 release_metapage(dmp);
1317 AG_UNLOCK(imap, agno);
1319 release_metapage(mp);
1325 * There are several places in the diAlloc* routines where we initialize
1329 diInitInode(struct inode *ip, int iagno, int ino, int extno, struct iag * iagp)
1331 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
1332 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
1334 ip->i_ino = (iagno << L2INOSPERIAG) + ino;
1335 jfs_ip->ixpxd = iagp->inoext[extno];
1336 jfs_ip->agno = BLKTOAG(le64_to_cpu(iagp->agstart), sbi);
1337 jfs_ip->active_ag = -1;
1342 * NAME: diAlloc(pip,dir,ip)
1344 * FUNCTION: allocate a disk inode from the inode working map
1345 * for a fileset or aggregate.
1348 * pip - pointer to incore inode for the parent inode.
1349 * dir - TRUE if the new disk inode is for a directory.
1350 * ip - pointer to a new inode
1354 * -ENOSPC - insufficient disk resources.
1357 int diAlloc(struct inode *pip, boolean_t dir, struct inode *ip)
1359 int rc, ino, iagno, addext, extno, bitno, sword;
1360 int nwords, rem, i, agno;
1361 u32 mask, inosmap, extsmap;
1362 struct inode *ipimap;
1363 struct metapage *mp;
1366 struct inomap *imap;
1368 /* get the pointers to the inode map inode and the
1369 * corresponding imap control structure.
1371 ipimap = JFS_SBI(pip->i_sb)->ipimap;
1372 imap = JFS_IP(ipimap)->i_imap;
1373 JFS_IP(ip)->ipimap = ipimap;
1374 JFS_IP(ip)->fileset = FILESYSTEM_I;
1376 /* for a directory, the allocation policy is to start
1377 * at the ag level using the preferred ag.
1380 agno = dbNextAG(JFS_SBI(pip->i_sb)->ipbmap);
1381 AG_LOCK(imap, agno);
1385 /* for files, the policy starts off by trying to allocate from
1386 * the same iag containing the parent disk inode:
1387 * try to allocate the new disk inode close to the parent disk
1388 * inode, using parent disk inode number + 1 as the allocation
1389 * hint. (we use a left-to-right policy to attempt to avoid
1390 * moving backward on the disk.) compute the hint within the
1391 * file system and the iag.
1394 /* get the ag number of this iag */
1395 agno = JFS_IP(pip)->agno;
1397 if (atomic_read(&JFS_SBI(pip->i_sb)->bmap->db_active[agno])) {
1399 * There is an open file actively growing. We want to
1400 * allocate new inodes from a different ag to avoid
1401 * fragmentation problems.
1403 agno = dbNextAG(JFS_SBI(pip->i_sb)->ipbmap);
1404 AG_LOCK(imap, agno);
1408 inum = pip->i_ino + 1;
1409 ino = inum & (INOSPERIAG - 1);
1411 /* back off the the hint if it is outside of the iag */
1415 /* lock the AG inode map information */
1416 AG_LOCK(imap, agno);
1418 /* Get read lock on imap inode */
1421 /* get the iag number and read the iag */
1422 iagno = INOTOIAG(inum);
1423 if ((rc = diIAGRead(imap, iagno, &mp))) {
1424 IREAD_UNLOCK(ipimap);
1425 AG_UNLOCK(imap, agno);
1428 iagp = (struct iag *) mp->data;
1430 /* determine if new inode extent is allowed to be added to the iag.
1431 * new inode extent can be added to the iag if the ag
1432 * has less than 32 free disk inodes and the iag has free extents.
1434 addext = (imap->im_agctl[agno].numfree < 32 && iagp->nfreeexts);
1437 * try to allocate from the IAG
1439 /* check if the inode may be allocated from the iag
1440 * (i.e. the inode has free inodes or new extent can be added).
1442 if (iagp->nfreeinos || addext) {
1443 /* determine the extent number of the hint.
1445 extno = ino >> L2INOSPEREXT;
1447 /* check if the extent containing the hint has backed
1448 * inodes. if so, try to allocate within this extent.
1450 if (addressPXD(&iagp->inoext[extno])) {
1451 bitno = ino & (INOSPEREXT - 1);
1453 diFindFree(le32_to_cpu(iagp->wmap[extno]),
1456 ino = (extno << L2INOSPEREXT) + bitno;
1458 /* a free inode (bit) was found within this
1459 * extent, so allocate it.
1461 rc = diAllocBit(imap, iagp, ino);
1462 IREAD_UNLOCK(ipimap);
1466 /* set the results of the allocation
1467 * and write the iag.
1469 diInitInode(ip, iagno, ino, extno,
1471 mark_metapage_dirty(mp);
1473 release_metapage(mp);
1475 /* free the AG lock and return.
1477 AG_UNLOCK(imap, agno);
1484 EXTSPERIAG - 1) ? 0 : extno + 1;
1488 * no free inodes within the extent containing the hint.
1490 * try to allocate from the backed extents following
1491 * hint or, if appropriate (i.e. addext is true), allocate
1492 * an extent of free inodes at or following the extent
1493 * containing the hint.
1495 * the free inode and free extent summary maps are used
1496 * here, so determine the starting summary map position
1497 * and the number of words we'll have to examine. again,
1498 * the approach is to allocate following the hint, so we
1499 * might have to initially ignore prior bits of the summary
1500 * map that represent extents prior to the extent containing
1501 * the hint and later revisit these bits.
1503 bitno = extno & (EXTSPERSUM - 1);
1504 nwords = (bitno == 0) ? SMAPSZ : SMAPSZ + 1;
1505 sword = extno >> L2EXTSPERSUM;
1507 /* mask any prior bits for the starting words of the
1510 mask = ONES << (EXTSPERSUM - bitno);
1511 inosmap = le32_to_cpu(iagp->inosmap[sword]) | mask;
1512 extsmap = le32_to_cpu(iagp->extsmap[sword]) | mask;
1514 /* scan the free inode and free extent summary maps for
1517 for (i = 0; i < nwords; i++) {
1518 /* check if this word of the free inode summary
1519 * map describes an extent with free inodes.
1522 /* an extent with free inodes has been
1523 * found. determine the extent number
1524 * and the inode number within the extent.
1526 rem = diFindFree(inosmap, 0);
1527 extno = (sword << L2EXTSPERSUM) + rem;
1528 rem = diFindFree(le32_to_cpu(iagp->wmap[extno]),
1530 if (rem >= INOSPEREXT) {
1531 IREAD_UNLOCK(ipimap);
1532 release_metapage(mp);
1533 AG_UNLOCK(imap, agno);
1535 "diAlloc: can't find free bit "
1540 /* determine the inode number within the
1541 * iag and allocate the inode from the
1544 ino = (extno << L2INOSPEREXT) + rem;
1545 rc = diAllocBit(imap, iagp, ino);
1546 IREAD_UNLOCK(ipimap);
1550 /* set the results of the allocation
1551 * and write the iag.
1553 diInitInode(ip, iagno, ino, extno,
1555 mark_metapage_dirty(mp);
1557 release_metapage(mp);
1559 /* free the AG lock and return.
1561 AG_UNLOCK(imap, agno);
1566 /* check if we may allocate an extent of free
1567 * inodes and whether this word of the free
1568 * extents summary map describes a free extent.
1570 if (addext && ~extsmap) {
1571 /* a free extent has been found. determine
1572 * the extent number.
1574 rem = diFindFree(extsmap, 0);
1575 extno = (sword << L2EXTSPERSUM) + rem;
1577 /* allocate an extent of free inodes.
1579 if ((rc = diNewExt(imap, iagp, extno))) {
1580 /* if there is no disk space for a
1581 * new extent, try to allocate the
1582 * disk inode from somewhere else.
1589 /* set the results of the allocation
1590 * and write the iag.
1592 diInitInode(ip, iagno,
1593 extno << L2INOSPEREXT,
1595 mark_metapage_dirty(mp);
1597 release_metapage(mp);
1598 /* free the imap inode & the AG lock & return.
1600 IREAD_UNLOCK(ipimap);
1601 AG_UNLOCK(imap, agno);
1605 /* move on to the next set of summary map words.
1607 sword = (sword == SMAPSZ - 1) ? 0 : sword + 1;
1608 inosmap = le32_to_cpu(iagp->inosmap[sword]);
1609 extsmap = le32_to_cpu(iagp->extsmap[sword]);
1612 /* unlock imap inode */
1613 IREAD_UNLOCK(ipimap);
1615 /* nothing doing in this iag, so release it. */
1616 release_metapage(mp);
1620 * try to allocate anywhere within the same AG as the parent inode.
1622 rc = diAllocAG(imap, agno, dir, ip);
1624 AG_UNLOCK(imap, agno);
1630 * try to allocate in any AG.
1632 return (diAllocAny(imap, agno, dir, ip));
1637 * NAME: diAllocAG(imap,agno,dir,ip)
1639 * FUNCTION: allocate a disk inode from the allocation group.
1641 * this routine first determines if a new extent of free
1642 * inodes should be added for the allocation group, with
1643 * the current request satisfied from this extent. if this
1644 * is the case, an attempt will be made to do just that. if
1645 * this attempt fails or it has been determined that a new
1646 * extent should not be added, an attempt is made to satisfy
1647 * the request by allocating an existing (backed) free inode
1648 * from the allocation group.
1650 * PRE CONDITION: Already have the AG lock for this AG.
1653 * imap - pointer to inode map control structure.
1654 * agno - allocation group to allocate from.
1655 * dir - TRUE if the new disk inode is for a directory.
1656 * ip - pointer to the new inode to be filled in on successful return
1657 * with the disk inode number allocated, its extent address
1658 * and the start of the ag.
1662 * -ENOSPC - insufficient disk resources.
1666 diAllocAG(struct inomap * imap, int agno, boolean_t dir, struct inode *ip)
1668 int rc, addext, numfree, numinos;
1670 /* get the number of free and the number of backed disk
1671 * inodes currently within the ag.
1673 numfree = imap->im_agctl[agno].numfree;
1674 numinos = imap->im_agctl[agno].numinos;
1676 if (numfree > numinos) {
1677 jfs_error(ip->i_sb, "diAllocAG: numfree > numinos");
1681 /* determine if we should allocate a new extent of free inodes
1682 * within the ag: for directory inodes, add a new extent
1683 * if there are a small number of free inodes or number of free
1684 * inodes is a small percentage of the number of backed inodes.
1687 addext = (numfree < 64 ||
1689 && ((numfree * 100) / numinos) <= 20));
1691 addext = (numfree == 0);
1694 * try to allocate a new extent of free inodes.
1697 /* if free space is not avaliable for this new extent, try
1698 * below to allocate a free and existing (already backed)
1699 * inode from the ag.
1701 if ((rc = diAllocExt(imap, agno, ip)) != -ENOSPC)
1706 * try to allocate an existing free inode from the ag.
1708 return (diAllocIno(imap, agno, ip));
1713 * NAME: diAllocAny(imap,agno,dir,iap)
1715 * FUNCTION: allocate a disk inode from any other allocation group.
1717 * this routine is called when an allocation attempt within
1718 * the primary allocation group has failed. if attempts to
1719 * allocate an inode from any allocation group other than the
1720 * specified primary group.
1723 * imap - pointer to inode map control structure.
1724 * agno - primary allocation group (to avoid).
1725 * dir - TRUE if the new disk inode is for a directory.
1726 * ip - pointer to a new inode to be filled in on successful return
1727 * with the disk inode number allocated, its extent address
1728 * and the start of the ag.
1732 * -ENOSPC - insufficient disk resources.
1736 diAllocAny(struct inomap * imap, int agno, boolean_t dir, struct inode *ip)
1739 int maxag = JFS_SBI(imap->im_ipimap->i_sb)->bmap->db_maxag;
1742 /* try to allocate from the ags following agno up to
1743 * the maximum ag number.
1745 for (ag = agno + 1; ag <= maxag; ag++) {
1748 rc = diAllocAG(imap, ag, dir, ip);
1750 AG_UNLOCK(imap, ag);
1756 /* try to allocate from the ags in front of agno.
1758 for (ag = 0; ag < agno; ag++) {
1761 rc = diAllocAG(imap, ag, dir, ip);
1763 AG_UNLOCK(imap, ag);
1769 /* no free disk inodes.
1776 * NAME: diAllocIno(imap,agno,ip)
1778 * FUNCTION: allocate a disk inode from the allocation group's free
1779 * inode list, returning an error if this free list is
1780 * empty (i.e. no iags on the list).
1782 * allocation occurs from the first iag on the list using
1783 * the iag's free inode summary map to find the leftmost
1784 * free inode in the iag.
1786 * PRE CONDITION: Already have AG lock for this AG.
1789 * imap - pointer to inode map control structure.
1790 * agno - allocation group.
1791 * ip - pointer to new inode to be filled in on successful return
1792 * with the disk inode number allocated, its extent address
1793 * and the start of the ag.
1797 * -ENOSPC - insufficient disk resources.
1800 static int diAllocIno(struct inomap * imap, int agno, struct inode *ip)
1802 int iagno, ino, rc, rem, extno, sword;
1803 struct metapage *mp;
1806 /* check if there are iags on the ag's free inode list.
1808 if ((iagno = imap->im_agctl[agno].inofree) < 0)
1811 /* obtain read lock on imap inode */
1812 IREAD_LOCK(imap->im_ipimap);
1814 /* read the iag at the head of the list.
1816 if ((rc = diIAGRead(imap, iagno, &mp))) {
1817 IREAD_UNLOCK(imap->im_ipimap);
1820 iagp = (struct iag *) mp->data;
1822 /* better be free inodes in this iag if it is on the
1825 if (!iagp->nfreeinos) {
1826 IREAD_UNLOCK(imap->im_ipimap);
1827 release_metapage(mp);
1829 "diAllocIno: nfreeinos = 0, but iag on freelist");
1833 /* scan the free inode summary map to find an extent
1836 for (sword = 0;; sword++) {
1837 if (sword >= SMAPSZ) {
1838 IREAD_UNLOCK(imap->im_ipimap);
1839 release_metapage(mp);
1841 "diAllocIno: free inode not found in summary map");
1845 if (~iagp->inosmap[sword])
1849 /* found a extent with free inodes. determine
1850 * the extent number.
1852 rem = diFindFree(le32_to_cpu(iagp->inosmap[sword]), 0);
1853 if (rem >= EXTSPERSUM) {
1854 IREAD_UNLOCK(imap->im_ipimap);
1855 release_metapage(mp);
1856 jfs_error(ip->i_sb, "diAllocIno: no free extent found");
1859 extno = (sword << L2EXTSPERSUM) + rem;
1861 /* find the first free inode in the extent.
1863 rem = diFindFree(le32_to_cpu(iagp->wmap[extno]), 0);
1864 if (rem >= INOSPEREXT) {
1865 IREAD_UNLOCK(imap->im_ipimap);
1866 release_metapage(mp);
1867 jfs_error(ip->i_sb, "diAllocIno: free inode not found");
1871 /* compute the inode number within the iag.
1873 ino = (extno << L2INOSPEREXT) + rem;
1875 /* allocate the inode.
1877 rc = diAllocBit(imap, iagp, ino);
1878 IREAD_UNLOCK(imap->im_ipimap);
1880 release_metapage(mp);
1884 /* set the results of the allocation and write the iag.
1886 diInitInode(ip, iagno, ino, extno, iagp);
1894 * NAME: diAllocExt(imap,agno,ip)
1896 * FUNCTION: add a new extent of free inodes to an iag, allocating
1897 * an inode from this extent to satisfy the current allocation
1900 * this routine first tries to find an existing iag with free
1901 * extents through the ag free extent list. if list is not
1902 * empty, the head of the list will be selected as the home
1903 * of the new extent of free inodes. otherwise (the list is
1904 * empty), a new iag will be allocated for the ag to contain
1907 * once an iag has been selected, the free extent summary map
1908 * is used to locate a free extent within the iag and diNewExt()
1909 * is called to initialize the extent, with initialization
1910 * including the allocation of the first inode of the extent
1911 * for the purpose of satisfying this request.
1914 * imap - pointer to inode map control structure.
1915 * agno - allocation group number.
1916 * ip - pointer to new inode to be filled in on successful return
1917 * with the disk inode number allocated, its extent address
1918 * and the start of the ag.
1922 * -ENOSPC - insufficient disk resources.
1925 static int diAllocExt(struct inomap * imap, int agno, struct inode *ip)
1927 int rem, iagno, sword, extno, rc;
1928 struct metapage *mp;
1931 /* check if the ag has any iags with free extents. if not,
1932 * allocate a new iag for the ag.
1934 if ((iagno = imap->im_agctl[agno].extfree) < 0) {
1935 /* If successful, diNewIAG will obtain the read lock on the
1938 if ((rc = diNewIAG(imap, &iagno, agno, &mp))) {
1941 iagp = (struct iag *) mp->data;
1943 /* set the ag number if this a brand new iag
1946 cpu_to_le64(AGTOBLK(agno, imap->im_ipimap));
1950 IREAD_LOCK(imap->im_ipimap);
1951 if ((rc = diIAGRead(imap, iagno, &mp))) {
1952 IREAD_UNLOCK(imap->im_ipimap);
1953 jfs_error(ip->i_sb, "diAllocExt: error reading iag");
1956 iagp = (struct iag *) mp->data;
1959 /* using the free extent summary map, find a free extent.
1961 for (sword = 0;; sword++) {
1962 if (sword >= SMAPSZ) {
1963 release_metapage(mp);
1964 IREAD_UNLOCK(imap->im_ipimap);
1966 "diAllocExt: free ext summary map not found");
1969 if (~iagp->extsmap[sword])
1973 /* determine the extent number of the free extent.
1975 rem = diFindFree(le32_to_cpu(iagp->extsmap[sword]), 0);
1976 if (rem >= EXTSPERSUM) {
1977 release_metapage(mp);
1978 IREAD_UNLOCK(imap->im_ipimap);
1979 jfs_error(ip->i_sb, "diAllocExt: free extent not found");
1982 extno = (sword << L2EXTSPERSUM) + rem;
1984 /* initialize the new extent.
1986 rc = diNewExt(imap, iagp, extno);
1987 IREAD_UNLOCK(imap->im_ipimap);
1989 /* something bad happened. if a new iag was allocated,
1990 * place it back on the inode map's iag free list, and
1991 * clear the ag number information.
1993 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
1995 iagp->iagfree = cpu_to_le32(imap->im_freeiag);
1996 imap->im_freeiag = iagno;
1997 IAGFREE_UNLOCK(imap);
2003 /* set the results of the allocation and write the iag.
2005 diInitInode(ip, iagno, extno << L2INOSPEREXT, extno, iagp);
2014 * NAME: diAllocBit(imap,iagp,ino)
2016 * FUNCTION: allocate a backed inode from an iag.
2018 * this routine performs the mechanics of allocating a
2019 * specified inode from a backed extent.
2021 * if the inode to be allocated represents the last free
2022 * inode within the iag, the iag will be removed from the
2023 * ag free inode list.
2025 * a careful update approach is used to provide consistency
2026 * in the face of updates to multiple buffers. under this
2027 * approach, all required buffers are obtained before making
2028 * any updates and are held all are updates are complete.
2030 * PRE CONDITION: Already have buffer lock on iagp. Already have AG lock on
2031 * this AG. Must have read lock on imap inode.
2034 * imap - pointer to inode map control structure.
2035 * iagp - pointer to iag.
2036 * ino - inode number to be allocated within the iag.
2040 * -ENOSPC - insufficient disk resources.
2043 static int diAllocBit(struct inomap * imap, struct iag * iagp, int ino)
2045 int extno, bitno, agno, sword, rc;
2046 struct metapage *amp = NULL, *bmp = NULL;
2047 struct iag *aiagp = NULL, *biagp = NULL;
2050 /* check if this is the last free inode within the iag.
2051 * if so, it will have to be removed from the ag free
2052 * inode list, so get the iags preceeding and following
2055 if (iagp->nfreeinos == cpu_to_le32(1)) {
2056 if ((int) le32_to_cpu(iagp->inofreefwd) >= 0) {
2058 diIAGRead(imap, le32_to_cpu(iagp->inofreefwd),
2061 aiagp = (struct iag *) amp->data;
2064 if ((int) le32_to_cpu(iagp->inofreeback) >= 0) {
2067 le32_to_cpu(iagp->inofreeback),
2070 release_metapage(amp);
2073 biagp = (struct iag *) bmp->data;
2077 /* get the ag number, extent number, inode number within
2080 agno = BLKTOAG(le64_to_cpu(iagp->agstart), JFS_SBI(imap->im_ipimap->i_sb));
2081 extno = ino >> L2INOSPEREXT;
2082 bitno = ino & (INOSPEREXT - 1);
2084 /* compute the mask for setting the map.
2086 mask = HIGHORDER >> bitno;
2088 /* the inode should be free and backed.
2090 if (((le32_to_cpu(iagp->pmap[extno]) & mask) != 0) ||
2091 ((le32_to_cpu(iagp->wmap[extno]) & mask) != 0) ||
2092 (addressPXD(&iagp->inoext[extno]) == 0)) {
2094 release_metapage(amp);
2096 release_metapage(bmp);
2098 jfs_error(imap->im_ipimap->i_sb,
2099 "diAllocBit: iag inconsistent");
2103 /* mark the inode as allocated in the working map.
2105 iagp->wmap[extno] |= cpu_to_le32(mask);
2107 /* check if all inodes within the extent are now
2108 * allocated. if so, update the free inode summary
2109 * map to reflect this.
2111 if (iagp->wmap[extno] == cpu_to_le32(ONES)) {
2112 sword = extno >> L2EXTSPERSUM;
2113 bitno = extno & (EXTSPERSUM - 1);
2114 iagp->inosmap[sword] |= cpu_to_le32(HIGHORDER >> bitno);
2117 /* if this was the last free inode in the iag, remove the
2118 * iag from the ag free inode list.
2120 if (iagp->nfreeinos == cpu_to_le32(1)) {
2122 aiagp->inofreeback = iagp->inofreeback;
2123 write_metapage(amp);
2127 biagp->inofreefwd = iagp->inofreefwd;
2128 write_metapage(bmp);
2130 imap->im_agctl[agno].inofree =
2131 le32_to_cpu(iagp->inofreefwd);
2133 iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
2136 /* update the free inode count at the iag, ag, inode
2139 iagp->nfreeinos = cpu_to_le32(le32_to_cpu(iagp->nfreeinos) - 1);
2140 imap->im_agctl[agno].numfree -= 1;
2141 atomic_dec(&imap->im_numfree);
2148 * NAME: diNewExt(imap,iagp,extno)
2150 * FUNCTION: initialize a new extent of inodes for an iag, allocating
2151 * the first inode of the extent for use for the current
2152 * allocation request.
2154 * disk resources are allocated for the new extent of inodes
2155 * and the inodes themselves are initialized to reflect their
2156 * existence within the extent (i.e. their inode numbers and
2157 * inode extent addresses are set) and their initial state
2158 * (mode and link count are set to zero).
2160 * if the iag is new, it is not yet on an ag extent free list
2161 * but will now be placed on this list.
2163 * if the allocation of the new extent causes the iag to
2164 * have no free extent, the iag will be removed from the
2165 * ag extent free list.
2167 * if the iag has no free backed inodes, it will be placed
2168 * on the ag free inode list, since the addition of the new
2169 * extent will now cause it to have free inodes.
2171 * a careful update approach is used to provide consistency
2172 * (i.e. list consistency) in the face of updates to multiple
2173 * buffers. under this approach, all required buffers are
2174 * obtained before making any updates and are held until all
2175 * updates are complete.
2177 * PRE CONDITION: Already have buffer lock on iagp. Already have AG lock on
2178 * this AG. Must have read lock on imap inode.
2181 * imap - pointer to inode map control structure.
2182 * iagp - pointer to iag.
2183 * extno - extent number.
2187 * -ENOSPC - insufficient disk resources.
2190 static int diNewExt(struct inomap * imap, struct iag * iagp, int extno)
2192 int agno, iagno, fwd, back, freei = 0, sword, rc;
2193 struct iag *aiagp = NULL, *biagp = NULL, *ciagp = NULL;
2194 struct metapage *amp, *bmp, *cmp, *dmp;
2195 struct inode *ipimap;
2201 struct jfs_sb_info *sbi;
2203 /* better have free extents.
2205 if (!iagp->nfreeexts) {
2206 jfs_error(imap->im_ipimap->i_sb, "diNewExt: no free extents");
2210 /* get the inode map inode.
2212 ipimap = imap->im_ipimap;
2213 sbi = JFS_SBI(ipimap->i_sb);
2215 amp = bmp = cmp = NULL;
2217 /* get the ag and iag numbers for this iag.
2219 agno = BLKTOAG(le64_to_cpu(iagp->agstart), sbi);
2220 iagno = le32_to_cpu(iagp->iagnum);
2222 /* check if this is the last free extent within the
2223 * iag. if so, the iag must be removed from the ag
2224 * free extent list, so get the iags preceeding and
2225 * following the iag on this list.
2227 if (iagp->nfreeexts == cpu_to_le32(1)) {
2228 if ((fwd = le32_to_cpu(iagp->extfreefwd)) >= 0) {
2229 if ((rc = diIAGRead(imap, fwd, &)))
2231 aiagp = (struct iag *) amp->data;
2234 if ((back = le32_to_cpu(iagp->extfreeback)) >= 0) {
2235 if ((rc = diIAGRead(imap, back, &bmp)))
2237 biagp = (struct iag *) bmp->data;
2240 /* the iag has free extents. if all extents are free
2241 * (as is the case for a newly allocated iag), the iag
2242 * must be added to the ag free extent list, so get
2243 * the iag at the head of the list in preparation for
2244 * adding this iag to this list.
2247 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2248 if ((fwd = imap->im_agctl[agno].extfree) >= 0) {
2249 if ((rc = diIAGRead(imap, fwd, &)))
2251 aiagp = (struct iag *) amp->data;
2256 /* check if the iag has no free inodes. if so, the iag
2257 * will have to be added to the ag free inode list, so get
2258 * the iag at the head of the list in preparation for
2259 * adding this iag to this list. in doing this, we must
2260 * check if we already have the iag at the head of
2263 if (iagp->nfreeinos == 0) {
2264 freei = imap->im_agctl[agno].inofree;
2269 } else if (freei == back) {
2272 if ((rc = diIAGRead(imap, freei, &cmp)))
2274 ciagp = (struct iag *) cmp->data;
2276 if (ciagp == NULL) {
2277 jfs_error(imap->im_ipimap->i_sb,
2278 "diNewExt: ciagp == NULL");
2285 /* allocate disk space for the inode extent.
2287 if ((extno == 0) || (addressPXD(&iagp->inoext[extno - 1]) == 0))
2288 hint = ((s64) agno << sbi->bmap->db_agl2size) - 1;
2290 hint = addressPXD(&iagp->inoext[extno - 1]) +
2291 lengthPXD(&iagp->inoext[extno - 1]) - 1;
2293 if ((rc = dbAlloc(ipimap, hint, (s64) imap->im_nbperiext, &blkno)))
2296 /* compute the inode number of the first inode within the
2299 ino = (iagno << L2INOSPERIAG) + (extno << L2INOSPEREXT);
2301 /* initialize the inodes within the newly allocated extent a
2304 for (i = 0; i < imap->im_nbperiext; i += sbi->nbperpage) {
2305 /* get a buffer for this page of disk inodes.
2307 dmp = get_metapage(ipimap, blkno + i, PSIZE, 1);
2312 dp = (struct dinode *) dmp->data;
2314 /* initialize the inode number, mode, link count and
2315 * inode extent address.
2317 for (j = 0; j < INOSPERPAGE; j++, dp++, ino++) {
2318 dp->di_inostamp = cpu_to_le32(sbi->inostamp);
2319 dp->di_number = cpu_to_le32(ino);
2320 dp->di_fileset = cpu_to_le32(FILESYSTEM_I);
2323 PXDaddress(&(dp->di_ixpxd), blkno);
2324 PXDlength(&(dp->di_ixpxd), imap->im_nbperiext);
2326 write_metapage(dmp);
2329 /* if this is the last free extent within the iag, remove the
2330 * iag from the ag free extent list.
2332 if (iagp->nfreeexts == cpu_to_le32(1)) {
2334 aiagp->extfreeback = iagp->extfreeback;
2337 biagp->extfreefwd = iagp->extfreefwd;
2339 imap->im_agctl[agno].extfree =
2340 le32_to_cpu(iagp->extfreefwd);
2342 iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
2344 /* if the iag has all free extents (newly allocated iag),
2345 * add the iag to the ag free extent list.
2347 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2349 aiagp->extfreeback = cpu_to_le32(iagno);
2351 iagp->extfreefwd = cpu_to_le32(fwd);
2352 iagp->extfreeback = cpu_to_le32(-1);
2353 imap->im_agctl[agno].extfree = iagno;
2357 /* if the iag has no free inodes, add the iag to the
2358 * ag free inode list.
2360 if (iagp->nfreeinos == 0) {
2362 ciagp->inofreeback = cpu_to_le32(iagno);
2365 cpu_to_le32(imap->im_agctl[agno].inofree);
2366 iagp->inofreeback = cpu_to_le32(-1);
2367 imap->im_agctl[agno].inofree = iagno;
2370 /* initialize the extent descriptor of the extent. */
2371 PXDlength(&iagp->inoext[extno], imap->im_nbperiext);
2372 PXDaddress(&iagp->inoext[extno], blkno);
2374 /* initialize the working and persistent map of the extent.
2375 * the working map will be initialized such that
2376 * it indicates the first inode of the extent is allocated.
2378 iagp->wmap[extno] = cpu_to_le32(HIGHORDER);
2379 iagp->pmap[extno] = 0;
2381 /* update the free inode and free extent summary maps
2382 * for the extent to indicate the extent has free inodes
2383 * and no longer represents a free extent.
2385 sword = extno >> L2EXTSPERSUM;
2386 mask = HIGHORDER >> (extno & (EXTSPERSUM - 1));
2387 iagp->extsmap[sword] |= cpu_to_le32(mask);
2388 iagp->inosmap[sword] &= cpu_to_le32(~mask);
2390 /* update the free inode and free extent counts for the
2393 iagp->nfreeinos = cpu_to_le32(le32_to_cpu(iagp->nfreeinos) +
2395 iagp->nfreeexts = cpu_to_le32(le32_to_cpu(iagp->nfreeexts) - 1);
2397 /* update the free and backed inode counts for the ag.
2399 imap->im_agctl[agno].numfree += (INOSPEREXT - 1);
2400 imap->im_agctl[agno].numinos += INOSPEREXT;
2402 /* update the free and backed inode counts for the inode map.
2404 atomic_add(INOSPEREXT - 1, &imap->im_numfree);
2405 atomic_add(INOSPEREXT, &imap->im_numinos);
2410 write_metapage(amp);
2412 write_metapage(bmp);
2414 write_metapage(cmp);
2420 /* release the iags.
2423 release_metapage(amp);
2425 release_metapage(bmp);
2427 release_metapage(cmp);
2434 * NAME: diNewIAG(imap,iagnop,agno)
2436 * FUNCTION: allocate a new iag for an allocation group.
2438 * first tries to allocate the iag from the inode map
2440 * if the list has free iags, the head of the list is removed
2441 * and returned to satisfy the request.
2442 * if the inode map's iag free list is empty, the inode map
2443 * is extended to hold a new iag. this new iag is initialized
2444 * and returned to satisfy the request.
2447 * imap - pointer to inode map control structure.
2448 * iagnop - pointer to an iag number set with the number of the
2449 * newly allocated iag upon successful return.
2450 * agno - allocation group number.
2451 * bpp - Buffer pointer to be filled in with new IAG's buffer
2455 * -ENOSPC - insufficient disk resources.
2459 * AG lock held on entry/exit;
2460 * write lock on the map is held inside;
2461 * read lock on the map is held on successful completion;
2463 * note: new iag transaction:
2464 * . synchronously write iag;
2465 * . write log of xtree and inode of imap;
2467 * . synchronous write of xtree (right to left, bottom to top);
2468 * . at start of logredo(): init in-memory imap with one additional iag page;
2469 * . at end of logredo(): re-read imap inode to determine
2473 diNewIAG(struct inomap * imap, int *iagnop, int agno, struct metapage ** mpp)
2477 struct inode *ipimap;
2478 struct super_block *sb;
2479 struct jfs_sb_info *sbi;
2480 struct metapage *mp;
2485 #ifdef _STILL_TO_PORT
2487 #endif /* _STILL_TO_PORT */
2488 struct inode *iplist[1];
2490 /* pick up pointers to the inode map and mount inodes */
2491 ipimap = imap->im_ipimap;
2495 /* acquire the free iag lock */
2498 /* if there are any iags on the inode map free iag list,
2499 * allocate the iag from the head of the list.
2501 if (imap->im_freeiag >= 0) {
2502 /* pick up the iag number at the head of the list */
2503 iagno = imap->im_freeiag;
2505 /* determine the logical block number of the iag */
2506 blkno = IAGTOLBLK(iagno, sbi->l2nbperpage);
2508 /* no free iags. the inode map will have to be extented
2509 * to include a new iag.
2512 /* acquire inode map lock */
2513 IWRITE_LOCK(ipimap);
2515 if (ipimap->i_size >> L2PSIZE != imap->im_nextiag + 1) {
2516 IWRITE_UNLOCK(ipimap);
2517 IAGFREE_UNLOCK(imap);
2518 jfs_error(imap->im_ipimap->i_sb,
2519 "diNewIAG: ipimap->i_size is wrong");
2524 /* get the next avaliable iag number */
2525 iagno = imap->im_nextiag;
2527 /* make sure that we have not exceeded the maximum inode
2530 if (iagno > (MAXIAGS - 1)) {
2531 /* release the inode map lock */
2532 IWRITE_UNLOCK(ipimap);
2539 * synchronously append new iag page.
2541 /* determine the logical address of iag page to append */
2542 blkno = IAGTOLBLK(iagno, sbi->l2nbperpage);
2544 /* Allocate extent for new iag page */
2545 xlen = sbi->nbperpage;
2546 if ((rc = dbAlloc(ipimap, 0, (s64) xlen, &xaddr))) {
2547 /* release the inode map lock */
2548 IWRITE_UNLOCK(ipimap);
2554 * start transaction of update of the inode map
2555 * addressing structure pointing to the new iag page;
2557 tid = txBegin(sb, COMMIT_FORCE);
2558 down(&JFS_IP(ipimap)->commit_sem);
2560 /* update the inode map addressing structure to point to it */
2562 xtInsert(tid, ipimap, 0, blkno, xlen, &xaddr, 0))) {
2564 up(&JFS_IP(ipimap)->commit_sem);
2565 /* Free the blocks allocated for the iag since it was
2566 * not successfully added to the inode map
2568 dbFree(ipimap, xaddr, (s64) xlen);
2570 /* release the inode map lock */
2571 IWRITE_UNLOCK(ipimap);
2576 /* update the inode map's inode to reflect the extension */
2577 ipimap->i_size += PSIZE;
2578 inode_add_bytes(ipimap, PSIZE);
2580 /* assign a buffer for the page */
2581 mp = get_metapage(ipimap, blkno, PSIZE, 0);
2584 * This is very unlikely since we just created the
2585 * extent, but let's try to handle it correctly
2587 xtTruncate(tid, ipimap, ipimap->i_size - PSIZE,
2593 /* release the inode map lock */
2594 IWRITE_UNLOCK(ipimap);
2599 iagp = (struct iag *) mp->data;
2602 memset(iagp, 0, sizeof(struct iag));
2603 iagp->iagnum = cpu_to_le32(iagno);
2604 iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
2605 iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
2606 iagp->iagfree = cpu_to_le32(-1);
2607 iagp->nfreeinos = 0;
2608 iagp->nfreeexts = cpu_to_le32(EXTSPERIAG);
2610 /* initialize the free inode summary map (free extent
2611 * summary map initialization handled by bzero).
2613 for (i = 0; i < SMAPSZ; i++)
2614 iagp->inosmap[i] = cpu_to_le32(ONES);
2617 * Write and sync the metapage
2622 * txCommit(COMMIT_FORCE) will synchronously write address
2623 * index pages and inode after commit in careful update order
2624 * of address index pages (right to left, bottom up);
2627 rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
2630 up(&JFS_IP(ipimap)->commit_sem);
2632 duplicateIXtree(sb, blkno, xlen, &xaddr);
2634 /* update the next avaliable iag number */
2635 imap->im_nextiag += 1;
2637 /* Add the iag to the iag free list so we don't lose the iag
2638 * if a failure happens now.
2640 imap->im_freeiag = iagno;
2642 /* Until we have logredo working, we want the imap inode &
2643 * control page to be up to date.
2647 /* release the inode map lock */
2648 IWRITE_UNLOCK(ipimap);
2651 /* obtain read lock on map */
2655 if ((rc = diIAGRead(imap, iagno, &mp))) {
2656 IREAD_UNLOCK(ipimap);
2660 iagp = (struct iag *) mp->data;
2662 /* remove the iag from the iag free list */
2663 imap->im_freeiag = le32_to_cpu(iagp->iagfree);
2664 iagp->iagfree = cpu_to_le32(-1);
2666 /* set the return iag number and buffer pointer */
2671 /* release the iag free lock */
2672 IAGFREE_UNLOCK(imap);
2680 * FUNCTION: get the buffer for the specified iag within a fileset
2681 * or aggregate inode map.
2684 * imap - pointer to inode map control structure.
2685 * iagno - iag number.
2686 * bpp - point to buffer pointer to be filled in on successful
2690 * must have read lock on imap inode
2691 * (When called by diExtendFS, the filesystem is quiesced, therefore
2692 * the read lock is unnecessary.)
2698 static int diIAGRead(struct inomap * imap, int iagno, struct metapage ** mpp)
2700 struct inode *ipimap = imap->im_ipimap;
2703 /* compute the logical block number of the iag. */
2704 blkno = IAGTOLBLK(iagno, JFS_SBI(ipimap->i_sb)->l2nbperpage);
2707 *mpp = read_metapage(ipimap, blkno, PSIZE, 0);
2716 * NAME: diFindFree()
2718 * FUNCTION: find the first free bit in a word starting at
2719 * the specified bit position.
2722 * word - word to be examined.
2723 * start - starting bit position.
2726 * bit position of first free bit in the word or 32 if
2727 * no free bits were found.
2729 static int diFindFree(u32 word, int start)
2733 /* scan the word for the first free bit. */
2734 for (word <<= start, bitno = start; bitno < 32;
2735 bitno++, word <<= 1) {
2736 if ((word & HIGHORDER) == 0)
2743 * NAME: diUpdatePMap()
2745 * FUNCTION: Update the persistent map in an IAG for the allocation or
2746 * freeing of the specified inode.
2748 * PRE CONDITIONS: Working map has already been updated for allocate.
2751 * ipimap - Incore inode map inode
2752 * inum - Number of inode to mark in permanent map
2753 * is_free - If TRUE indicates inode should be marked freed, otherwise
2754 * indicates inode should be marked allocated.
2760 diUpdatePMap(struct inode *ipimap,
2761 unsigned long inum, boolean_t is_free, struct tblock * tblk)
2765 struct metapage *mp;
2766 int iagno, ino, extno, bitno;
2767 struct inomap *imap;
2769 struct jfs_log *log;
2770 int lsn, difft, diffp;
2771 unsigned long flags;
2773 imap = JFS_IP(ipimap)->i_imap;
2774 /* get the iag number containing the inode */
2775 iagno = INOTOIAG(inum);
2776 /* make sure that the iag is contained within the map */
2777 if (iagno >= imap->im_nextiag) {
2778 jfs_error(ipimap->i_sb,
2779 "diUpdatePMap: the iag is outside the map");
2784 rc = diIAGRead(imap, iagno, &mp);
2785 IREAD_UNLOCK(ipimap);
2788 metapage_wait_for_io(mp);
2789 iagp = (struct iag *) mp->data;
2790 /* get the inode number and extent number of the inode within
2791 * the iag and the inode number within the extent.
2793 ino = inum & (INOSPERIAG - 1);
2794 extno = ino >> L2INOSPEREXT;
2795 bitno = ino & (INOSPEREXT - 1);
2796 mask = HIGHORDER >> bitno;
2798 * mark the inode free in persistent map:
2800 if (is_free == TRUE) {
2801 /* The inode should have been allocated both in working
2802 * map and in persistent map;
2803 * the inode will be freed from working map at the release
2804 * of last reference release;
2806 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
2807 jfs_error(ipimap->i_sb,
2808 "diUpdatePMap: inode %ld not marked as "
2809 "allocated in wmap!", inum);
2811 if (!(le32_to_cpu(iagp->pmap[extno]) & mask)) {
2812 jfs_error(ipimap->i_sb,
2813 "diUpdatePMap: inode %ld not marked as "
2814 "allocated in pmap!", inum);
2816 /* update the bitmap for the extent of the freed inode */
2817 iagp->pmap[extno] &= cpu_to_le32(~mask);
2820 * mark the inode allocated in persistent map:
2823 /* The inode should be already allocated in the working map
2824 * and should be free in persistent map;
2826 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
2827 release_metapage(mp);
2828 jfs_error(ipimap->i_sb,
2829 "diUpdatePMap: the inode is not allocated in "
2833 if ((le32_to_cpu(iagp->pmap[extno]) & mask) != 0) {
2834 release_metapage(mp);
2835 jfs_error(ipimap->i_sb,
2836 "diUpdatePMap: the inode is not free in the "
2840 /* update the bitmap for the extent of the allocated inode */
2841 iagp->pmap[extno] |= cpu_to_le32(mask);
2847 log = JFS_SBI(tblk->sb)->log;
2848 LOGSYNC_LOCK(log, flags);
2850 /* inherit older/smaller lsn */
2851 logdiff(difft, lsn, log);
2852 logdiff(diffp, mp->lsn, log);
2853 if (difft < diffp) {
2855 /* move mp after tblock in logsync list */
2856 list_move(&mp->synclist, &tblk->synclist);
2858 /* inherit younger/larger clsn */
2860 logdiff(difft, tblk->clsn, log);
2861 logdiff(diffp, mp->clsn, log);
2863 mp->clsn = tblk->clsn;
2867 /* insert mp after tblock in logsync list */
2869 list_add(&mp->synclist, &tblk->synclist);
2870 mp->clsn = tblk->clsn;
2872 LOGSYNC_UNLOCK(log, flags);
2880 * function: update imap for extendfs();
2882 * note: AG size has been increased s.t. each k old contiguous AGs are
2883 * coalesced into a new AG;
2885 int diExtendFS(struct inode *ipimap, struct inode *ipbmap)
2888 struct inomap *imap = JFS_IP(ipimap)->i_imap;
2889 struct iag *iagp = NULL, *hiagp = NULL;
2890 struct bmap *mp = JFS_SBI(ipbmap->i_sb)->bmap;
2891 struct metapage *bp, *hbp;
2893 int numinos, xnuminos = 0, xnumfree = 0;
2896 jfs_info("diExtendFS: nextiag:%d numinos:%d numfree:%d",
2897 imap->im_nextiag, atomic_read(&imap->im_numinos),
2898 atomic_read(&imap->im_numfree));
2903 * coalesce contiguous k (newAGSize/oldAGSize) AGs;
2904 * i.e., (AGi, ..., AGj) where i = k*n and j = k*(n+1) - 1 to AGn;
2905 * note: new AG size = old AG size * (2**x).
2908 /* init per AG control information im_agctl[] */
2909 for (i = 0; i < MAXAG; i++) {
2910 imap->im_agctl[i].inofree = -1;
2911 imap->im_agctl[i].extfree = -1;
2912 imap->im_agctl[i].numinos = 0; /* number of backed inodes */
2913 imap->im_agctl[i].numfree = 0; /* number of free backed inodes */
2917 * process each iag page of the map.
2919 * rebuild AG Free Inode List, AG Free Inode Extent List;
2921 for (i = 0; i < imap->im_nextiag; i++) {
2922 if ((rc = diIAGRead(imap, i, &bp))) {
2926 iagp = (struct iag *) bp->data;
2927 if (le32_to_cpu(iagp->iagnum) != i) {
2928 release_metapage(bp);
2929 jfs_error(ipimap->i_sb,
2930 "diExtendFs: unexpected value of iagnum");
2934 /* leave free iag in the free iag list */
2935 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2936 release_metapage(bp);
2940 /* agstart that computes to the same ag is treated as same; */
2941 agstart = le64_to_cpu(iagp->agstart);
2942 /* iagp->agstart = agstart & ~(mp->db_agsize - 1); */
2943 n = agstart >> mp->db_agl2size;
2945 /* compute backed inodes */
2946 numinos = (EXTSPERIAG - le32_to_cpu(iagp->nfreeexts))
2949 /* merge AG backed inodes */
2950 imap->im_agctl[n].numinos += numinos;
2951 xnuminos += numinos;
2954 /* if any backed free inodes, insert at AG free inode list */
2955 if ((int) le32_to_cpu(iagp->nfreeinos) > 0) {
2956 if ((head = imap->im_agctl[n].inofree) == -1) {
2957 iagp->inofreefwd = cpu_to_le32(-1);
2958 iagp->inofreeback = cpu_to_le32(-1);
2960 if ((rc = diIAGRead(imap, head, &hbp))) {
2964 hiagp = (struct iag *) hbp->data;
2965 hiagp->inofreeback = iagp->iagnum;
2966 iagp->inofreefwd = cpu_to_le32(head);
2967 iagp->inofreeback = cpu_to_le32(-1);
2968 write_metapage(hbp);
2971 imap->im_agctl[n].inofree =
2972 le32_to_cpu(iagp->iagnum);
2974 /* merge AG backed free inodes */
2975 imap->im_agctl[n].numfree +=
2976 le32_to_cpu(iagp->nfreeinos);
2977 xnumfree += le32_to_cpu(iagp->nfreeinos);
2980 /* if any free extents, insert at AG free extent list */
2981 if (le32_to_cpu(iagp->nfreeexts) > 0) {
2982 if ((head = imap->im_agctl[n].extfree) == -1) {
2983 iagp->extfreefwd = cpu_to_le32(-1);
2984 iagp->extfreeback = cpu_to_le32(-1);
2986 if ((rc = diIAGRead(imap, head, &hbp))) {
2990 hiagp = (struct iag *) hbp->data;
2991 hiagp->extfreeback = iagp->iagnum;
2992 iagp->extfreefwd = cpu_to_le32(head);
2993 iagp->extfreeback = cpu_to_le32(-1);
2994 write_metapage(hbp);
2997 imap->im_agctl[n].extfree =
2998 le32_to_cpu(iagp->iagnum);
3005 if (xnuminos != atomic_read(&imap->im_numinos) ||
3006 xnumfree != atomic_read(&imap->im_numfree)) {
3007 jfs_error(ipimap->i_sb,
3008 "diExtendFs: numinos or numfree incorrect");
3019 * serialization: IWRITE_LOCK held on entry/exit
3021 * note: shadow page with regular inode (rel.2);
3023 static void duplicateIXtree(struct super_block *sb, s64 blkno,
3024 int xlen, s64 *xaddr)
3026 struct jfs_superblock *j_sb;
3027 struct buffer_head *bh;
3031 /* if AIT2 ipmap2 is bad, do not try to update it */
3032 if (JFS_SBI(sb)->mntflag & JFS_BAD_SAIT) /* s_flag */
3034 ip = diReadSpecial(sb, FILESYSTEM_I, 1);
3036 JFS_SBI(sb)->mntflag |= JFS_BAD_SAIT;
3037 if (readSuper(sb, &bh))
3039 j_sb = (struct jfs_superblock *)bh->b_data;
3040 j_sb->s_flag |= cpu_to_le32(JFS_BAD_SAIT);
3042 mark_buffer_dirty(bh);
3043 sync_dirty_buffer(bh);
3048 /* start transaction */
3049 tid = txBegin(sb, COMMIT_FORCE);
3050 /* update the inode map addressing structure to point to it */
3051 if (xtInsert(tid, ip, 0, blkno, xlen, xaddr, 0)) {
3052 JFS_SBI(sb)->mntflag |= JFS_BAD_SAIT;
3057 /* update the inode map's inode to reflect the extension */
3058 ip->i_size += PSIZE;
3059 inode_add_bytes(ip, PSIZE);
3060 txCommit(tid, 1, &ip, COMMIT_FORCE);
3067 * NAME: copy_from_dinode()
3069 * FUNCTION: Copies inode info from disk inode to in-memory inode
3073 * -ENOMEM - insufficient memory
3075 static int copy_from_dinode(struct dinode * dip, struct inode *ip)
3077 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
3081 jfs_ip->fileset = le32_to_cpu(dip->di_fileset);
3082 jfs_ip->mode2 = le32_to_cpu(dip->di_mode);
3084 ip->i_mode = le32_to_cpu(dip->di_mode) & 0xffff;
3085 ip->i_nlink = le32_to_cpu(dip->di_nlink);
3087 uid = le32_to_cpu(dip->di_uid);
3088 gid = le32_to_cpu(dip->di_gid);
3089 ip->i_uid = INOXID_UID(XID_TAG(ip), uid, gid);
3090 ip->i_gid = INOXID_GID(XID_TAG(ip), uid, gid);
3091 ip->i_xid = INOXID_XID(XID_TAG(ip), uid, gid, 0);
3093 ip->i_size = le64_to_cpu(dip->di_size);
3094 ip->i_atime.tv_sec = le32_to_cpu(dip->di_atime.tv_sec);
3095 ip->i_atime.tv_nsec = le32_to_cpu(dip->di_atime.tv_nsec);
3096 ip->i_mtime.tv_sec = le32_to_cpu(dip->di_mtime.tv_sec);
3097 ip->i_mtime.tv_nsec = le32_to_cpu(dip->di_mtime.tv_nsec);
3098 ip->i_ctime.tv_sec = le32_to_cpu(dip->di_ctime.tv_sec);
3099 ip->i_ctime.tv_nsec = le32_to_cpu(dip->di_ctime.tv_nsec);
3100 ip->i_blksize = ip->i_sb->s_blocksize;
3101 ip->i_blocks = LBLK2PBLK(ip->i_sb, le64_to_cpu(dip->di_nblocks));
3102 ip->i_generation = le32_to_cpu(dip->di_gen);
3104 jfs_ip->ixpxd = dip->di_ixpxd; /* in-memory pxd's are little-endian */
3105 jfs_ip->acl = dip->di_acl; /* as are dxd's */
3106 jfs_ip->ea = dip->di_ea;
3107 jfs_ip->next_index = le32_to_cpu(dip->di_next_index);
3108 jfs_ip->otime = le32_to_cpu(dip->di_otime.tv_sec);
3109 jfs_ip->acltype = le32_to_cpu(dip->di_acltype);
3111 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode)) {
3112 jfs_ip->dev = le32_to_cpu(dip->di_rdev);
3113 ip->i_rdev = new_decode_dev(jfs_ip->dev);
3116 if (S_ISDIR(ip->i_mode)) {
3117 memcpy(&jfs_ip->i_dirtable, &dip->di_dirtable, 384);
3118 } else if (S_ISREG(ip->i_mode) || S_ISLNK(ip->i_mode)) {
3119 memcpy(&jfs_ip->i_xtroot, &dip->di_xtroot, 288);
3121 memcpy(&jfs_ip->i_inline_ea, &dip->di_inlineea, 128);
3123 /* Zero the in-memory-only stuff */
3125 jfs_ip->btindex = 0;
3126 jfs_ip->btorder = 0;
3129 jfs_ip->atlhead = 0;
3130 jfs_ip->atltail = 0;
3136 * NAME: copy_to_dinode()
3138 * FUNCTION: Copies inode info from in-memory inode to disk inode
3140 static void copy_to_dinode(struct dinode * dip, struct inode *ip)
3142 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
3146 dip->di_fileset = cpu_to_le32(jfs_ip->fileset);
3147 dip->di_inostamp = cpu_to_le32(JFS_SBI(ip->i_sb)->inostamp);
3148 dip->di_number = cpu_to_le32(ip->i_ino);
3149 dip->di_gen = cpu_to_le32(ip->i_generation);
3150 dip->di_size = cpu_to_le64(ip->i_size);
3151 dip->di_nblocks = cpu_to_le64(PBLK2LBLK(ip->i_sb, ip->i_blocks));
3152 dip->di_nlink = cpu_to_le32(ip->i_nlink);
3154 uid = XIDINO_UID(XID_TAG(ip), ip->i_uid, ip->i_xid);
3155 gid = XIDINO_GID(XID_TAG(ip), ip->i_gid, ip->i_xid);
3156 dip->di_uid = cpu_to_le32(uid);
3157 dip->di_gid = cpu_to_le32(gid);
3159 * mode2 is only needed for storing the higher order bits.
3160 * Trust i_mode for the lower order ones
3162 dip->di_mode = cpu_to_le32((jfs_ip->mode2 & 0xffff0000) | ip->i_mode);
3163 dip->di_atime.tv_sec = cpu_to_le32(ip->i_atime.tv_sec);
3164 dip->di_atime.tv_nsec = cpu_to_le32(ip->i_atime.tv_nsec);
3165 dip->di_ctime.tv_sec = cpu_to_le32(ip->i_ctime.tv_sec);
3166 dip->di_ctime.tv_nsec = cpu_to_le32(ip->i_ctime.tv_nsec);
3167 dip->di_mtime.tv_sec = cpu_to_le32(ip->i_mtime.tv_sec);
3168 dip->di_mtime.tv_nsec = cpu_to_le32(ip->i_mtime.tv_nsec);
3169 dip->di_ixpxd = jfs_ip->ixpxd; /* in-memory pxd's are little-endian */
3170 dip->di_acl = jfs_ip->acl; /* as are dxd's */
3171 dip->di_ea = jfs_ip->ea;
3172 dip->di_next_index = cpu_to_le32(jfs_ip->next_index);
3173 dip->di_otime.tv_sec = cpu_to_le32(jfs_ip->otime);
3174 dip->di_otime.tv_nsec = 0;
3175 dip->di_acltype = cpu_to_le32(jfs_ip->acltype);
3176 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode))
3177 dip->di_rdev = cpu_to_le32(jfs_ip->dev);