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>
49 #include <linux/quotaops.h>
51 #include "jfs_incore.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"
63 /* iag free list lock */
64 #define IAGFREE_LOCK_INIT(imap) init_MUTEX(&imap->im_freelock)
65 #define IAGFREE_LOCK(imap) down(&imap->im_freelock)
66 #define IAGFREE_UNLOCK(imap) up(&imap->im_freelock)
68 /* per ag iag list locks */
69 #define AG_LOCK_INIT(imap,index) init_MUTEX(&(imap->im_aglock[index]))
70 #define AG_LOCK(imap,agno) down(&imap->im_aglock[agno])
71 #define AG_UNLOCK(imap,agno) up(&imap->im_aglock[agno])
76 extern struct address_space_operations jfs_aops;
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 *);
96 * debug code for double-checking inode map
98 /* #define _JFS_DEBUG_IMAP 1 */
100 #ifdef _JFS_DEBUG_IMAP
101 #define DBG_DIINIT(imap) DBGdiInit(imap)
102 #define DBG_DIALLOC(imap, ino) DBGdiAlloc(imap, ino)
103 #define DBG_DIFREE(imap, ino) DBGdiFree(imap, ino)
105 static void *DBGdiInit(struct inomap * imap);
106 static void DBGdiAlloc(struct inomap * imap, ino_t ino);
107 static void DBGdiFree(struct inomap * imap, ino_t ino);
109 #define DBG_DIINIT(imap)
110 #define DBG_DIALLOC(imap, ino)
111 #define DBG_DIFREE(imap, ino)
112 #endif /* _JFS_DEBUG_IMAP */
117 * FUNCTION: initialize the incore inode map control structures for
118 * a fileset or aggregate init time.
120 * the inode map's control structure (dinomap) is
121 * brought in from disk and placed in virtual memory.
124 * ipimap - pointer to inode map inode for the aggregate or fileset.
128 * -ENOMEM - insufficient free virtual memory.
131 int diMount(struct inode *ipimap)
136 struct dinomap_disk *dinom_le;
139 * allocate/initialize the in-memory inode map control structure
141 /* allocate the in-memory inode map control structure. */
142 imap = (struct inomap *) kmalloc(sizeof(struct inomap), GFP_KERNEL);
144 jfs_err("diMount: kmalloc returned NULL!");
148 /* read the on-disk inode map control structure. */
150 mp = read_metapage(ipimap,
151 IMAPBLKNO << JFS_SBI(ipimap->i_sb)->l2nbperpage,
158 /* copy the on-disk version to the in-memory version. */
159 dinom_le = (struct dinomap_disk *) mp->data;
160 imap->im_freeiag = le32_to_cpu(dinom_le->in_freeiag);
161 imap->im_nextiag = le32_to_cpu(dinom_le->in_nextiag);
162 atomic_set(&imap->im_numinos, le32_to_cpu(dinom_le->in_numinos));
163 atomic_set(&imap->im_numfree, le32_to_cpu(dinom_le->in_numfree));
164 imap->im_nbperiext = le32_to_cpu(dinom_le->in_nbperiext);
165 imap->im_l2nbperiext = le32_to_cpu(dinom_le->in_l2nbperiext);
166 for (index = 0; index < MAXAG; index++) {
167 imap->im_agctl[index].inofree =
168 le32_to_cpu(dinom_le->in_agctl[index].inofree);
169 imap->im_agctl[index].extfree =
170 le32_to_cpu(dinom_le->in_agctl[index].extfree);
171 imap->im_agctl[index].numinos =
172 le32_to_cpu(dinom_le->in_agctl[index].numinos);
173 imap->im_agctl[index].numfree =
174 le32_to_cpu(dinom_le->in_agctl[index].numfree);
177 /* release the buffer. */
178 release_metapage(mp);
181 * allocate/initialize inode allocation map locks
183 /* allocate and init iag free list lock */
184 IAGFREE_LOCK_INIT(imap);
186 /* allocate and init ag list locks */
187 for (index = 0; index < MAXAG; index++) {
188 AG_LOCK_INIT(imap, index);
191 /* bind the inode map inode and inode map control structure
194 imap->im_ipimap = ipimap;
195 JFS_IP(ipimap)->i_imap = imap;
206 * FUNCTION: write to disk the incore inode map control structures for
207 * a fileset or aggregate at unmount time.
210 * ipimap - pointer to inode map inode for the aggregate or fileset.
214 * -ENOMEM - insufficient free virtual memory.
217 int diUnmount(struct inode *ipimap, int mounterror)
219 struct inomap *imap = JFS_IP(ipimap)->i_imap;
222 * update the on-disk inode map control structure
225 if (!(mounterror || isReadOnly(ipimap)))
229 * Invalidate the page cache buffers
231 truncate_inode_pages(ipimap->i_mapping, 0);
234 * free in-memory control structure
245 int diSync(struct inode *ipimap)
247 struct dinomap_disk *dinom_le;
248 struct inomap *imp = JFS_IP(ipimap)->i_imap;
253 * write imap global conrol page
255 /* read the on-disk inode map control structure */
256 mp = get_metapage(ipimap,
257 IMAPBLKNO << JFS_SBI(ipimap->i_sb)->l2nbperpage,
260 jfs_err("diSync: get_metapage failed!");
264 /* copy the in-memory version to the on-disk version */
265 dinom_le = (struct dinomap_disk *) mp->data;
266 dinom_le->in_freeiag = cpu_to_le32(imp->im_freeiag);
267 dinom_le->in_nextiag = cpu_to_le32(imp->im_nextiag);
268 dinom_le->in_numinos = cpu_to_le32(atomic_read(&imp->im_numinos));
269 dinom_le->in_numfree = cpu_to_le32(atomic_read(&imp->im_numfree));
270 dinom_le->in_nbperiext = cpu_to_le32(imp->im_nbperiext);
271 dinom_le->in_l2nbperiext = cpu_to_le32(imp->im_l2nbperiext);
272 for (index = 0; index < MAXAG; index++) {
273 dinom_le->in_agctl[index].inofree =
274 cpu_to_le32(imp->im_agctl[index].inofree);
275 dinom_le->in_agctl[index].extfree =
276 cpu_to_le32(imp->im_agctl[index].extfree);
277 dinom_le->in_agctl[index].numinos =
278 cpu_to_le32(imp->im_agctl[index].numinos);
279 dinom_le->in_agctl[index].numfree =
280 cpu_to_le32(imp->im_agctl[index].numfree);
283 /* write out the control structure */
287 * write out dirty pages of imap
289 filemap_fdatawrite(ipimap->i_mapping);
290 filemap_fdatawait(ipimap->i_mapping);
292 diWriteSpecial(ipimap, 0);
301 * FUNCTION: initialize an incore inode from disk.
303 * on entry, the specifed incore inode should itself
304 * specify the disk inode number corresponding to the
305 * incore inode (i.e. i_number should be initialized).
307 * this routine handles incore inode initialization for
308 * both "special" and "regular" inodes. special inodes
309 * are those required early in the mount process and
310 * require special handling since much of the file system
311 * is not yet initialized. these "special" inodes are
312 * identified by a NULL inode map inode pointer and are
313 * actually initialized by a call to diReadSpecial().
315 * for regular inodes, the iag describing the disk inode
316 * is read from disk to determine the inode extent address
317 * for the disk inode. with the inode extent address in
318 * hand, the page of the extent that contains the disk
319 * inode is read and the disk inode is copied to the
323 * ip - pointer to incore inode to be initialized from disk.
328 * -ENOMEM - insufficient memory
331 int diRead(struct inode *ip)
333 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
334 int iagno, ino, extno, rc;
335 struct inode *ipimap;
346 jfs_info("diRead: ino = %ld", ip->i_ino);
348 ipimap = sbi->ipimap;
349 JFS_IP(ip)->ipimap = ipimap;
351 /* determine the iag number for this inode (number) */
352 iagno = INOTOIAG(ip->i_ino);
355 imap = JFS_IP(ipimap)->i_imap;
357 rc = diIAGRead(imap, iagno, &mp);
358 IREAD_UNLOCK(ipimap);
360 jfs_err("diRead: diIAGRead returned %d", rc);
364 iagp = (struct iag *) mp->data;
366 /* determine inode extent that holds the disk inode */
367 ino = ip->i_ino & (INOSPERIAG - 1);
368 extno = ino >> L2INOSPEREXT;
370 if ((lengthPXD(&iagp->inoext[extno]) != imap->im_nbperiext) ||
371 (addressPXD(&iagp->inoext[extno]) == 0)) {
372 release_metapage(mp);
376 /* get disk block number of the page within the inode extent
377 * that holds the disk inode.
379 blkno = INOPBLK(&iagp->inoext[extno], ino, sbi->l2nbperpage);
381 /* get the ag for the iag */
382 agstart = le64_to_cpu(iagp->agstart);
384 release_metapage(mp);
386 rel_inode = (ino & (INOSPERPAGE - 1));
387 pageno = blkno >> sbi->l2nbperpage;
389 if ((block_offset = ((u32) blkno & (sbi->nbperpage - 1)))) {
391 * OS/2 didn't always align inode extents on page boundaries
394 (sbi->nbperpage - block_offset) << sbi->l2niperblk;
396 if (rel_inode < inodes_left)
397 rel_inode += block_offset << sbi->l2niperblk;
400 rel_inode -= inodes_left;
404 /* read the page of disk inode */
405 mp = read_metapage(ipimap, pageno << sbi->l2nbperpage, PSIZE, 1);
407 jfs_err("diRead: read_metapage failed");
411 /* locate the the disk inode requested */
412 dp = (struct dinode *) mp->data;
415 if (ip->i_ino != le32_to_cpu(dp->di_number)) {
416 jfs_error(ip->i_sb, "diRead: i_ino != di_number");
418 } else if (le32_to_cpu(dp->di_nlink) == 0)
421 /* copy the disk inode to the in-memory inode */
422 rc = copy_from_dinode(dp, ip);
424 release_metapage(mp);
426 /* set the ag for the inode */
427 JFS_IP(ip)->agno = BLKTOAG(agstart, sbi);
428 JFS_IP(ip)->active_ag = -1;
435 * NAME: diReadSpecial()
437 * FUNCTION: initialize a 'special' inode from disk.
439 * this routines handles aggregate level inodes. The
440 * inode cache cannot differentiate between the
441 * aggregate inodes and the filesystem inodes, so we
442 * handle these here. We don't actually use the aggregate
443 * inode map, since these inodes are at a fixed location
444 * and in some cases the aggregate inode map isn't initialized
448 * sb - filesystem superblock
449 * inum - aggregate inode number
450 * secondary - 1 if secondary aggregate inode table
453 * new inode - success
456 struct inode *diReadSpecial(struct super_block *sb, ino_t inum, int secondary)
458 struct jfs_sb_info *sbi = JFS_SBI(sb);
466 jfs_err("diReadSpecial: new_inode returned NULL!");
471 address = addressPXD(&sbi->ait2) >> sbi->l2nbperpage;
472 JFS_IP(ip)->ipimap = sbi->ipaimap2;
474 address = AITBL_OFF >> L2PSIZE;
475 JFS_IP(ip)->ipimap = sbi->ipaimap;
478 ASSERT(inum < INOSPEREXT);
482 address += inum >> 3; /* 8 inodes per 4K page */
484 /* read the page of fixed disk inode (AIT) in raw mode */
485 mp = read_metapage(ip, address << sbi->l2nbperpage, PSIZE, 1);
487 ip->i_nlink = 1; /* Don't want iput() deleting it */
492 /* get the pointer to the disk inode of interest */
493 dp = (struct dinode *) (mp->data);
494 dp += inum % 8; /* 8 inodes per 4K page */
496 /* copy on-disk inode to in-memory inode */
497 if ((copy_from_dinode(dp, ip)) != 0) {
498 /* handle bad return by returning NULL for ip */
499 ip->i_nlink = 1; /* Don't want iput() deleting it */
501 /* release the page */
502 release_metapage(mp);
507 ip->i_mapping->a_ops = &jfs_aops;
508 mapping_set_gfp_mask(ip->i_mapping, GFP_NOFS);
510 /* Allocations to metadata inodes should not affect quotas */
511 ip->i_flags |= S_NOQUOTA;
513 if ((inum == FILESYSTEM_I) && (JFS_IP(ip)->ipimap == sbi->ipaimap)) {
514 sbi->gengen = le32_to_cpu(dp->di_gengen);
515 sbi->inostamp = le32_to_cpu(dp->di_inostamp);
518 /* release the page */
519 release_metapage(mp);
525 * NAME: diWriteSpecial()
527 * FUNCTION: Write the special inode to disk
531 * secondary - 1 if secondary aggregate inode table
533 * RETURN VALUES: none
536 void diWriteSpecial(struct inode *ip, int secondary)
538 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
541 ino_t inum = ip->i_ino;
544 ip->i_state &= ~I_DIRTY;
547 address = addressPXD(&sbi->ait2) >> sbi->l2nbperpage;
549 address = AITBL_OFF >> L2PSIZE;
551 ASSERT(inum < INOSPEREXT);
553 address += inum >> 3; /* 8 inodes per 4K page */
555 /* read the page of fixed disk inode (AIT) in raw mode */
556 mp = read_metapage(ip, address << sbi->l2nbperpage, PSIZE, 1);
558 jfs_err("diWriteSpecial: failed to read aggregate inode "
563 /* get the pointer to the disk inode of interest */
564 dp = (struct dinode *) (mp->data);
565 dp += inum % 8; /* 8 inodes per 4K page */
567 /* copy on-disk inode to in-memory inode */
568 copy_to_dinode(dp, ip);
569 memcpy(&dp->di_xtroot, &JFS_IP(ip)->i_xtroot, 288);
571 if (inum == FILESYSTEM_I)
572 dp->di_gengen = cpu_to_le32(sbi->gengen);
579 * NAME: diFreeSpecial()
581 * FUNCTION: Free allocated space for special inode
583 void diFreeSpecial(struct inode *ip)
586 jfs_err("diFreeSpecial called with NULL ip!");
589 filemap_fdatawrite(ip->i_mapping);
590 filemap_fdatawait(ip->i_mapping);
591 truncate_inode_pages(ip->i_mapping, 0);
600 * FUNCTION: write the on-disk inode portion of the in-memory inode
601 * to its corresponding on-disk inode.
603 * on entry, the specifed incore inode should itself
604 * specify the disk inode number corresponding to the
605 * incore inode (i.e. i_number should be initialized).
607 * the inode contains the inode extent address for the disk
608 * inode. with the inode extent address in hand, the
609 * page of the extent that contains the disk inode is
610 * read and the disk inode portion of the incore inode
611 * is copied to the disk inode.
614 * tid - transacation id
615 * ip - pointer to incore inode to be written to the inode extent.
621 int diWrite(tid_t tid, struct inode *ip)
623 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
624 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
635 struct inode *ipimap;
638 struct tlock *ditlck, *tlck;
639 struct linelock *dilinelock, *ilinelock;
643 ipimap = jfs_ip->ipimap;
645 ino = ip->i_ino & (INOSPERIAG - 1);
647 if (!addressPXD(&(jfs_ip->ixpxd)) ||
648 (lengthPXD(&(jfs_ip->ixpxd)) !=
649 JFS_IP(ipimap)->i_imap->im_nbperiext)) {
650 jfs_error(ip->i_sb, "diWrite: ixpxd invalid");
655 * read the page of disk inode containing the specified inode:
657 /* compute the block address of the page */
658 blkno = INOPBLK(&(jfs_ip->ixpxd), ino, sbi->l2nbperpage);
660 rel_inode = (ino & (INOSPERPAGE - 1));
661 pageno = blkno >> sbi->l2nbperpage;
663 if ((block_offset = ((u32) blkno & (sbi->nbperpage - 1)))) {
665 * OS/2 didn't always align inode extents on page boundaries
668 (sbi->nbperpage - block_offset) << sbi->l2niperblk;
670 if (rel_inode < inodes_left)
671 rel_inode += block_offset << sbi->l2niperblk;
674 rel_inode -= inodes_left;
677 /* read the page of disk inode */
679 mp = read_metapage(ipimap, pageno << sbi->l2nbperpage, PSIZE, 1);
683 /* get the pointer to the disk inode */
684 dp = (struct dinode *) mp->data;
687 dioffset = (ino & (INOSPERPAGE - 1)) << L2DISIZE;
690 * acquire transaction lock on the on-disk inode;
691 * N.B. tlock is acquired on ipimap not ip;
694 txLock(tid, ipimap, mp, tlckINODE | tlckENTRY)) == NULL)
696 dilinelock = (struct linelock *) & ditlck->lock;
699 * copy btree root from in-memory inode to on-disk inode
701 * (tlock is taken from inline B+-tree root in in-memory
702 * inode when the B+-tree root is updated, which is pointed
703 * by jfs_ip->blid as well as being on tx tlock list)
705 * further processing of btree root is based on the copy
706 * in in-memory inode, where txLog() will log from, and,
707 * for xtree root, txUpdateMap() will update map and reset
711 if (S_ISDIR(ip->i_mode) && (lid = jfs_ip->xtlid)) {
713 * This is the special xtree inside the directory for storing
714 * the directory table
720 tlck = lid_to_tlock(lid);
721 assert(tlck->type & tlckXTREE);
722 tlck->type |= tlckBTROOT;
724 ilinelock = (struct linelock *) & tlck->lock;
727 * copy xtree root from inode to dinode:
729 p = &jfs_ip->i_xtroot;
730 xp = (xtpage_t *) &dp->di_dirtable;
732 for (n = 0; n < ilinelock->index; n++, lv++) {
733 memcpy(&xp->xad[lv->offset], &p->xad[lv->offset],
734 lv->length << L2XTSLOTSIZE);
737 /* reset on-disk (metadata page) xtree XAD_NEW bit */
738 xad = &xp->xad[XTENTRYSTART];
739 for (n = XTENTRYSTART;
740 n < le16_to_cpu(xp->header.nextindex); n++, xad++)
741 if (xad->flag & (XAD_NEW | XAD_EXTENDED))
742 xad->flag &= ~(XAD_NEW | XAD_EXTENDED);
745 if ((lid = jfs_ip->blid) == 0)
749 tlck = lid_to_tlock(lid);
751 tlck->type |= tlckBTROOT;
753 ilinelock = (struct linelock *) & tlck->lock;
756 * regular file: 16 byte (XAD slot) granularity
758 if (type & tlckXTREE) {
763 * copy xtree root from inode to dinode:
765 p = &jfs_ip->i_xtroot;
768 for (n = 0; n < ilinelock->index; n++, lv++) {
769 memcpy(&xp->xad[lv->offset], &p->xad[lv->offset],
770 lv->length << L2XTSLOTSIZE);
773 /* reset on-disk (metadata page) xtree XAD_NEW bit */
774 xad = &xp->xad[XTENTRYSTART];
775 for (n = XTENTRYSTART;
776 n < le16_to_cpu(xp->header.nextindex); n++, xad++)
777 if (xad->flag & (XAD_NEW | XAD_EXTENDED))
778 xad->flag &= ~(XAD_NEW | XAD_EXTENDED);
781 * directory: 32 byte (directory entry slot) granularity
783 else if (type & tlckDTREE) {
787 * copy dtree root from inode to dinode:
789 p = (dtpage_t *) &jfs_ip->i_dtroot;
790 xp = (dtpage_t *) & dp->di_dtroot;
792 for (n = 0; n < ilinelock->index; n++, lv++) {
793 memcpy(&xp->slot[lv->offset], &p->slot[lv->offset],
794 lv->length << L2DTSLOTSIZE);
797 jfs_err("diWrite: UFO tlock");
802 * copy inline symlink from in-memory inode to on-disk inode
804 if (S_ISLNK(ip->i_mode) && ip->i_size < IDATASIZE) {
805 lv = & dilinelock->lv[dilinelock->index];
806 lv->offset = (dioffset + 2 * 128) >> L2INODESLOTSIZE;
808 memcpy(&dp->di_fastsymlink, jfs_ip->i_inline, IDATASIZE);
812 * copy inline data from in-memory inode to on-disk inode:
813 * 128 byte slot granularity
815 if (test_cflag(COMMIT_Inlineea, ip)) {
816 lv = & dilinelock->lv[dilinelock->index];
817 lv->offset = (dioffset + 3 * 128) >> L2INODESLOTSIZE;
819 memcpy(&dp->di_inlineea, jfs_ip->i_inline_ea, INODESLOTSIZE);
822 clear_cflag(COMMIT_Inlineea, ip);
826 * lock/copy inode base: 128 byte slot granularity
829 lv = & dilinelock->lv[dilinelock->index];
830 lv->offset = dioffset >> L2INODESLOTSIZE;
831 copy_to_dinode(dp, ip);
832 if (test_and_clear_cflag(COMMIT_Dirtable, ip)) {
834 memcpy(&dp->di_dirtable, &jfs_ip->i_dirtable, 96);
841 * We aren't logging changes to the DASD used in directory inodes,
842 * but we need to write them to disk. If we don't unmount cleanly,
843 * mount will recalculate the DASD used.
845 if (S_ISDIR(ip->i_mode)
846 && (ip->i_ipmnt->i_mntflag & JFS_DASD_ENABLED))
847 memcpy(&dp->di_DASD, &ip->i_DASD, sizeof(struct dasd));
848 #endif /* _JFS_FASTDASD */
850 /* release the buffer holding the updated on-disk inode.
851 * the buffer will be later written by commit processing.
862 * FUNCTION: free a specified inode from the inode working map
863 * for a fileset or aggregate.
865 * if the inode to be freed represents the first (only)
866 * free inode within the iag, the iag will be placed on
867 * the ag free inode list.
869 * freeing the inode will cause the inode extent to be
870 * freed if the inode is the only allocated inode within
871 * the extent. in this case all the disk resource backing
872 * up the inode extent will be freed. in addition, the iag
873 * will be placed on the ag extent free list if the extent
874 * is the first free extent in the iag. if freeing the
875 * extent also means that no free inodes will exist for
876 * the iag, the iag will also be removed from the ag free
879 * the iag describing the inode will be freed if the extent
880 * is to be freed and it is the only backed extent within
881 * the iag. in this case, the iag will be removed from the
882 * ag free extent list and ag free inode list and placed on
883 * the inode map's free iag list.
885 * a careful update approach is used to provide consistency
886 * in the face of updates to multiple buffers. under this
887 * approach, all required buffers are obtained before making
888 * any updates and are held until all updates are complete.
891 * ip - inode to be freed.
897 int diFree(struct inode *ip)
900 ino_t inum = ip->i_ino;
901 struct iag *iagp, *aiagp, *biagp, *ciagp, *diagp;
902 struct metapage *mp, *amp, *bmp, *cmp, *dmp;
903 int iagno, ino, extno, bitno, sword, agno;
906 struct inode *ipimap = JFS_SBI(ip->i_sb)->ipimap;
907 struct inomap *imap = JFS_IP(ipimap)->i_imap;
910 struct inode *iplist[3];
912 struct pxd_lock *pxdlock;
915 * This is just to suppress compiler warnings. The same logic that
916 * references these variables is used to initialize them.
918 aiagp = biagp = ciagp = diagp = NULL;
920 /* get the iag number containing the inode.
922 iagno = INOTOIAG(inum);
924 /* make sure that the iag is contained within
927 if (iagno >= imap->im_nextiag) {
928 dump_mem("imap", imap, 32);
930 "diFree: inum = %d, iagno = %d, nextiag = %d",
931 (uint) inum, iagno, imap->im_nextiag);
935 /* get the allocation group for this ino.
937 agno = JFS_IP(ip)->agno;
939 /* Lock the AG specific inode map information
943 /* Obtain read lock in imap inode. Don't release it until we have
944 * read all of the IAG's that we are going to.
950 if ((rc = diIAGRead(imap, iagno, &mp))) {
951 IREAD_UNLOCK(ipimap);
952 AG_UNLOCK(imap, agno);
955 iagp = (struct iag *) mp->data;
957 /* get the inode number and extent number of the inode within
958 * the iag and the inode number within the extent.
960 ino = inum & (INOSPERIAG - 1);
961 extno = ino >> L2INOSPEREXT;
962 bitno = ino & (INOSPEREXT - 1);
963 mask = HIGHORDER >> bitno;
965 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
967 "diFree: wmap shows inode already free");
970 if (!addressPXD(&iagp->inoext[extno])) {
971 release_metapage(mp);
972 IREAD_UNLOCK(ipimap);
973 AG_UNLOCK(imap, agno);
974 jfs_error(ip->i_sb, "diFree: invalid inoext");
978 /* compute the bitmap for the extent reflecting the freed inode.
980 bitmap = le32_to_cpu(iagp->wmap[extno]) & ~mask;
982 if (imap->im_agctl[agno].numfree > imap->im_agctl[agno].numinos) {
983 release_metapage(mp);
984 IREAD_UNLOCK(ipimap);
985 AG_UNLOCK(imap, agno);
986 jfs_error(ip->i_sb, "diFree: numfree > numinos");
990 * inode extent still has some inodes or below low water mark:
991 * keep the inode extent;
994 imap->im_agctl[agno].numfree < 96 ||
995 (imap->im_agctl[agno].numfree < 288 &&
996 (((imap->im_agctl[agno].numfree * 100) /
997 imap->im_agctl[agno].numinos) <= 25))) {
998 /* if the iag currently has no free inodes (i.e.,
999 * the inode being freed is the first free inode of iag),
1000 * insert the iag at head of the inode free list for the ag.
1002 if (iagp->nfreeinos == 0) {
1003 /* check if there are any iags on the ag inode
1004 * free list. if so, read the first one so that
1005 * we can link the current iag onto the list at
1008 if ((fwd = imap->im_agctl[agno].inofree) >= 0) {
1009 /* read the iag that currently is the head
1012 if ((rc = diIAGRead(imap, fwd, &))) {
1013 IREAD_UNLOCK(ipimap);
1014 AG_UNLOCK(imap, agno);
1015 release_metapage(mp);
1018 aiagp = (struct iag *) amp->data;
1020 /* make current head point back to the iag.
1022 aiagp->inofreeback = cpu_to_le32(iagno);
1024 write_metapage(amp);
1027 /* iag points forward to current head and iag
1028 * becomes the new head of the list.
1031 cpu_to_le32(imap->im_agctl[agno].inofree);
1032 iagp->inofreeback = cpu_to_le32(-1);
1033 imap->im_agctl[agno].inofree = iagno;
1035 IREAD_UNLOCK(ipimap);
1037 /* update the free inode summary map for the extent if
1038 * freeing the inode means the extent will now have free
1039 * inodes (i.e., the inode being freed is the first free
1042 if (iagp->wmap[extno] == cpu_to_le32(ONES)) {
1043 sword = extno >> L2EXTSPERSUM;
1044 bitno = extno & (EXTSPERSUM - 1);
1045 iagp->inosmap[sword] &=
1046 cpu_to_le32(~(HIGHORDER >> bitno));
1049 /* update the bitmap.
1051 iagp->wmap[extno] = cpu_to_le32(bitmap);
1052 DBG_DIFREE(imap, inum);
1054 /* update the free inode counts at the iag, ag and
1058 cpu_to_le32(le32_to_cpu(iagp->nfreeinos) + 1);
1059 imap->im_agctl[agno].numfree += 1;
1060 atomic_inc(&imap->im_numfree);
1062 /* release the AG inode map lock
1064 AG_UNLOCK(imap, agno);
1074 * inode extent has become free and above low water mark:
1075 * free the inode extent;
1079 * prepare to update iag list(s) (careful update step 1)
1081 amp = bmp = cmp = dmp = NULL;
1084 /* check if the iag currently has no free extents. if so,
1085 * it will be placed on the head of the ag extent free list.
1087 if (iagp->nfreeexts == 0) {
1088 /* check if the ag extent free list has any iags.
1089 * if so, read the iag at the head of the list now.
1090 * this (head) iag will be updated later to reflect
1091 * the addition of the current iag at the head of
1094 if ((fwd = imap->im_agctl[agno].extfree) >= 0) {
1095 if ((rc = diIAGRead(imap, fwd, &)))
1097 aiagp = (struct iag *) amp->data;
1100 /* iag has free extents. check if the addition of a free
1101 * extent will cause all extents to be free within this
1102 * iag. if so, the iag will be removed from the ag extent
1103 * free list and placed on the inode map's free iag list.
1105 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG - 1)) {
1106 /* in preparation for removing the iag from the
1107 * ag extent free list, read the iags preceeding
1108 * and following the iag on the ag extent free
1111 if ((fwd = le32_to_cpu(iagp->extfreefwd)) >= 0) {
1112 if ((rc = diIAGRead(imap, fwd, &)))
1114 aiagp = (struct iag *) amp->data;
1117 if ((back = le32_to_cpu(iagp->extfreeback)) >= 0) {
1118 if ((rc = diIAGRead(imap, back, &bmp)))
1120 biagp = (struct iag *) bmp->data;
1125 /* remove the iag from the ag inode free list if freeing
1126 * this extent cause the iag to have no free inodes.
1128 if (iagp->nfreeinos == cpu_to_le32(INOSPEREXT - 1)) {
1129 int inofreeback = le32_to_cpu(iagp->inofreeback);
1130 int inofreefwd = le32_to_cpu(iagp->inofreefwd);
1132 /* in preparation for removing the iag from the
1133 * ag inode free list, read the iags preceeding
1134 * and following the iag on the ag inode free
1135 * list. before reading these iags, we must make
1136 * sure that we already don't have them in hand
1137 * from up above, since re-reading an iag (buffer)
1138 * we are currently holding would cause a deadlock.
1140 if (inofreefwd >= 0) {
1142 if (inofreefwd == fwd)
1143 ciagp = (struct iag *) amp->data;
1144 else if (inofreefwd == back)
1145 ciagp = (struct iag *) bmp->data;
1148 diIAGRead(imap, inofreefwd, &cmp)))
1150 ciagp = (struct iag *) cmp->data;
1152 assert(ciagp != NULL);
1155 if (inofreeback >= 0) {
1156 if (inofreeback == fwd)
1157 diagp = (struct iag *) amp->data;
1158 else if (inofreeback == back)
1159 diagp = (struct iag *) bmp->data;
1162 diIAGRead(imap, inofreeback, &dmp)))
1164 diagp = (struct iag *) dmp->data;
1166 assert(diagp != NULL);
1170 IREAD_UNLOCK(ipimap);
1173 * invalidate any page of the inode extent freed from buffer cache;
1175 freepxd = iagp->inoext[extno];
1176 invalidate_pxd_metapages(ip, freepxd);
1179 * update iag list(s) (careful update step 2)
1181 /* add the iag to the ag extent free list if this is the
1182 * first free extent for the iag.
1184 if (iagp->nfreeexts == 0) {
1186 aiagp->extfreeback = cpu_to_le32(iagno);
1189 cpu_to_le32(imap->im_agctl[agno].extfree);
1190 iagp->extfreeback = cpu_to_le32(-1);
1191 imap->im_agctl[agno].extfree = iagno;
1193 /* remove the iag from the ag extent list if all extents
1194 * are now free and place it on the inode map iag free list.
1196 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG - 1)) {
1198 aiagp->extfreeback = iagp->extfreeback;
1201 biagp->extfreefwd = iagp->extfreefwd;
1203 imap->im_agctl[agno].extfree =
1204 le32_to_cpu(iagp->extfreefwd);
1206 iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
1209 iagp->iagfree = cpu_to_le32(imap->im_freeiag);
1210 imap->im_freeiag = iagno;
1211 IAGFREE_UNLOCK(imap);
1215 /* remove the iag from the ag inode free list if freeing
1216 * this extent causes the iag to have no free inodes.
1218 if (iagp->nfreeinos == cpu_to_le32(INOSPEREXT - 1)) {
1219 if ((int) le32_to_cpu(iagp->inofreefwd) >= 0)
1220 ciagp->inofreeback = iagp->inofreeback;
1222 if ((int) le32_to_cpu(iagp->inofreeback) >= 0)
1223 diagp->inofreefwd = iagp->inofreefwd;
1225 imap->im_agctl[agno].inofree =
1226 le32_to_cpu(iagp->inofreefwd);
1228 iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
1231 /* update the inode extent address and working map
1232 * to reflect the free extent.
1233 * the permanent map should have been updated already
1234 * for the inode being freed.
1236 if (iagp->pmap[extno] != 0) {
1237 jfs_error(ip->i_sb, "diFree: the pmap does not show inode free");
1239 iagp->wmap[extno] = 0;
1240 DBG_DIFREE(imap, inum);
1241 PXDlength(&iagp->inoext[extno], 0);
1242 PXDaddress(&iagp->inoext[extno], 0);
1244 /* update the free extent and free inode summary maps
1245 * to reflect the freed extent.
1246 * the inode summary map is marked to indicate no inodes
1247 * available for the freed extent.
1249 sword = extno >> L2EXTSPERSUM;
1250 bitno = extno & (EXTSPERSUM - 1);
1251 mask = HIGHORDER >> bitno;
1252 iagp->inosmap[sword] |= cpu_to_le32(mask);
1253 iagp->extsmap[sword] &= cpu_to_le32(~mask);
1255 /* update the number of free inodes and number of free extents
1258 iagp->nfreeinos = cpu_to_le32(le32_to_cpu(iagp->nfreeinos) -
1260 iagp->nfreeexts = cpu_to_le32(le32_to_cpu(iagp->nfreeexts) + 1);
1262 /* update the number of free inodes and backed inodes
1263 * at the ag and inode map level.
1265 imap->im_agctl[agno].numfree -= (INOSPEREXT - 1);
1266 imap->im_agctl[agno].numinos -= INOSPEREXT;
1267 atomic_sub(INOSPEREXT - 1, &imap->im_numfree);
1268 atomic_sub(INOSPEREXT, &imap->im_numinos);
1271 write_metapage(amp);
1273 write_metapage(bmp);
1275 write_metapage(cmp);
1277 write_metapage(dmp);
1280 * start transaction to update block allocation map
1281 * for the inode extent freed;
1283 * N.B. AG_LOCK is released and iag will be released below, and
1284 * other thread may allocate inode from/reusing the ixad freed
1285 * BUT with new/different backing inode extent from the extent
1286 * to be freed by the transaction;
1288 tid = txBegin(ipimap->i_sb, COMMIT_FORCE);
1289 down(&JFS_IP(ipimap)->commit_sem);
1291 /* acquire tlock of the iag page of the freed ixad
1292 * to force the page NOHOMEOK (even though no data is
1293 * logged from the iag page) until NOREDOPAGE|FREEXTENT log
1294 * for the free of the extent is committed;
1295 * write FREEXTENT|NOREDOPAGE log record
1296 * N.B. linelock is overlaid as freed extent descriptor;
1298 tlck = txLock(tid, ipimap, mp, tlckINODE | tlckFREE);
1299 pxdlock = (struct pxd_lock *) & tlck->lock;
1300 pxdlock->flag = mlckFREEPXD;
1301 pxdlock->pxd = freepxd;
1309 * logredo needs the IAG number and IAG extent index in order
1310 * to ensure that the IMap is consistent. The least disruptive
1311 * way to pass these values through to the transaction manager
1312 * is in the iplist array.
1314 * It's not pretty, but it works.
1316 iplist[1] = (struct inode *) (size_t)iagno;
1317 iplist[2] = (struct inode *) (size_t)extno;
1319 rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
1322 up(&JFS_IP(ipimap)->commit_sem);
1324 /* unlock the AG inode map information */
1325 AG_UNLOCK(imap, agno);
1330 IREAD_UNLOCK(ipimap);
1333 release_metapage(amp);
1335 release_metapage(bmp);
1337 release_metapage(cmp);
1339 release_metapage(dmp);
1341 AG_UNLOCK(imap, agno);
1343 release_metapage(mp);
1349 * There are several places in the diAlloc* routines where we initialize
1353 diInitInode(struct inode *ip, int iagno, int ino, int extno, struct iag * iagp)
1355 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
1356 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
1358 ip->i_ino = (iagno << L2INOSPERIAG) + ino;
1359 DBG_DIALLOC(JFS_IP(ipimap)->i_imap, ip->i_ino);
1360 jfs_ip->ixpxd = iagp->inoext[extno];
1361 jfs_ip->agno = BLKTOAG(le64_to_cpu(iagp->agstart), sbi);
1362 jfs_ip->active_ag = -1;
1367 * NAME: diAlloc(pip,dir,ip)
1369 * FUNCTION: allocate a disk inode from the inode working map
1370 * for a fileset or aggregate.
1373 * pip - pointer to incore inode for the parent inode.
1374 * dir - TRUE if the new disk inode is for a directory.
1375 * ip - pointer to a new inode
1379 * -ENOSPC - insufficient disk resources.
1382 int diAlloc(struct inode *pip, boolean_t dir, struct inode *ip)
1384 int rc, ino, iagno, addext, extno, bitno, sword;
1385 int nwords, rem, i, agno;
1386 u32 mask, inosmap, extsmap;
1387 struct inode *ipimap;
1388 struct metapage *mp;
1391 struct inomap *imap;
1393 /* get the pointers to the inode map inode and the
1394 * corresponding imap control structure.
1396 ipimap = JFS_SBI(pip->i_sb)->ipimap;
1397 imap = JFS_IP(ipimap)->i_imap;
1398 JFS_IP(ip)->ipimap = ipimap;
1399 JFS_IP(ip)->fileset = FILESYSTEM_I;
1401 /* for a directory, the allocation policy is to start
1402 * at the ag level using the preferred ag.
1405 agno = dbNextAG(JFS_SBI(pip->i_sb)->ipbmap);
1406 AG_LOCK(imap, agno);
1410 /* for files, the policy starts off by trying to allocate from
1411 * the same iag containing the parent disk inode:
1412 * try to allocate the new disk inode close to the parent disk
1413 * inode, using parent disk inode number + 1 as the allocation
1414 * hint. (we use a left-to-right policy to attempt to avoid
1415 * moving backward on the disk.) compute the hint within the
1416 * file system and the iag.
1419 /* get the ag number of this iag */
1420 agno = JFS_IP(pip)->agno;
1422 if (atomic_read(&JFS_SBI(pip->i_sb)->bmap->db_active[agno])) {
1424 * There is an open file actively growing. We want to
1425 * allocate new inodes from a different ag to avoid
1426 * fragmentation problems.
1428 agno = dbNextAG(JFS_SBI(pip->i_sb)->ipbmap);
1429 AG_LOCK(imap, agno);
1433 inum = pip->i_ino + 1;
1434 ino = inum & (INOSPERIAG - 1);
1436 /* back off the the hint if it is outside of the iag */
1440 /* lock the AG inode map information */
1441 AG_LOCK(imap, agno);
1443 /* Get read lock on imap inode */
1446 /* get the iag number and read the iag */
1447 iagno = INOTOIAG(inum);
1448 if ((rc = diIAGRead(imap, iagno, &mp))) {
1449 IREAD_UNLOCK(ipimap);
1450 AG_UNLOCK(imap, agno);
1453 iagp = (struct iag *) mp->data;
1455 /* determine if new inode extent is allowed to be added to the iag.
1456 * new inode extent can be added to the iag if the ag
1457 * has less than 32 free disk inodes and the iag has free extents.
1459 addext = (imap->im_agctl[agno].numfree < 32 && iagp->nfreeexts);
1462 * try to allocate from the IAG
1464 /* check if the inode may be allocated from the iag
1465 * (i.e. the inode has free inodes or new extent can be added).
1467 if (iagp->nfreeinos || addext) {
1468 /* determine the extent number of the hint.
1470 extno = ino >> L2INOSPEREXT;
1472 /* check if the extent containing the hint has backed
1473 * inodes. if so, try to allocate within this extent.
1475 if (addressPXD(&iagp->inoext[extno])) {
1476 bitno = ino & (INOSPEREXT - 1);
1478 diFindFree(le32_to_cpu(iagp->wmap[extno]),
1481 ino = (extno << L2INOSPEREXT) + bitno;
1483 /* a free inode (bit) was found within this
1484 * extent, so allocate it.
1486 rc = diAllocBit(imap, iagp, ino);
1487 IREAD_UNLOCK(ipimap);
1491 /* set the results of the allocation
1492 * and write the iag.
1494 diInitInode(ip, iagno, ino, extno,
1496 mark_metapage_dirty(mp);
1498 release_metapage(mp);
1500 /* free the AG lock and return.
1502 AG_UNLOCK(imap, agno);
1509 EXTSPERIAG - 1) ? 0 : extno + 1;
1513 * no free inodes within the extent containing the hint.
1515 * try to allocate from the backed extents following
1516 * hint or, if appropriate (i.e. addext is true), allocate
1517 * an extent of free inodes at or following the extent
1518 * containing the hint.
1520 * the free inode and free extent summary maps are used
1521 * here, so determine the starting summary map position
1522 * and the number of words we'll have to examine. again,
1523 * the approach is to allocate following the hint, so we
1524 * might have to initially ignore prior bits of the summary
1525 * map that represent extents prior to the extent containing
1526 * the hint and later revisit these bits.
1528 bitno = extno & (EXTSPERSUM - 1);
1529 nwords = (bitno == 0) ? SMAPSZ : SMAPSZ + 1;
1530 sword = extno >> L2EXTSPERSUM;
1532 /* mask any prior bits for the starting words of the
1535 mask = ONES << (EXTSPERSUM - bitno);
1536 inosmap = le32_to_cpu(iagp->inosmap[sword]) | mask;
1537 extsmap = le32_to_cpu(iagp->extsmap[sword]) | mask;
1539 /* scan the free inode and free extent summary maps for
1542 for (i = 0; i < nwords; i++) {
1543 /* check if this word of the free inode summary
1544 * map describes an extent with free inodes.
1547 /* an extent with free inodes has been
1548 * found. determine the extent number
1549 * and the inode number within the extent.
1551 rem = diFindFree(inosmap, 0);
1552 extno = (sword << L2EXTSPERSUM) + rem;
1553 rem = diFindFree(le32_to_cpu(iagp->wmap[extno]),
1555 if (rem >= INOSPEREXT) {
1556 IREAD_UNLOCK(ipimap);
1557 release_metapage(mp);
1558 AG_UNLOCK(imap, agno);
1560 "diAlloc: can't find free bit "
1565 /* determine the inode number within the
1566 * iag and allocate the inode from the
1569 ino = (extno << L2INOSPEREXT) + rem;
1570 rc = diAllocBit(imap, iagp, ino);
1571 IREAD_UNLOCK(ipimap);
1575 /* set the results of the allocation
1576 * and write the iag.
1578 diInitInode(ip, iagno, ino, extno,
1580 mark_metapage_dirty(mp);
1582 release_metapage(mp);
1584 /* free the AG lock and return.
1586 AG_UNLOCK(imap, agno);
1591 /* check if we may allocate an extent of free
1592 * inodes and whether this word of the free
1593 * extents summary map describes a free extent.
1595 if (addext && ~extsmap) {
1596 /* a free extent has been found. determine
1597 * the extent number.
1599 rem = diFindFree(extsmap, 0);
1600 extno = (sword << L2EXTSPERSUM) + rem;
1602 /* allocate an extent of free inodes.
1604 if ((rc = diNewExt(imap, iagp, extno))) {
1605 /* if there is no disk space for a
1606 * new extent, try to allocate the
1607 * disk inode from somewhere else.
1614 /* set the results of the allocation
1615 * and write the iag.
1617 diInitInode(ip, iagno,
1618 extno << L2INOSPEREXT,
1620 mark_metapage_dirty(mp);
1622 release_metapage(mp);
1623 /* free the imap inode & the AG lock & return.
1625 IREAD_UNLOCK(ipimap);
1626 AG_UNLOCK(imap, agno);
1630 /* move on to the next set of summary map words.
1632 sword = (sword == SMAPSZ - 1) ? 0 : sword + 1;
1633 inosmap = le32_to_cpu(iagp->inosmap[sword]);
1634 extsmap = le32_to_cpu(iagp->extsmap[sword]);
1637 /* unlock imap inode */
1638 IREAD_UNLOCK(ipimap);
1640 /* nothing doing in this iag, so release it. */
1641 release_metapage(mp);
1645 * try to allocate anywhere within the same AG as the parent inode.
1647 rc = diAllocAG(imap, agno, dir, ip);
1649 AG_UNLOCK(imap, agno);
1655 * try to allocate in any AG.
1657 return (diAllocAny(imap, agno, dir, ip));
1662 * NAME: diAllocAG(imap,agno,dir,ip)
1664 * FUNCTION: allocate a disk inode from the allocation group.
1666 * this routine first determines if a new extent of free
1667 * inodes should be added for the allocation group, with
1668 * the current request satisfied from this extent. if this
1669 * is the case, an attempt will be made to do just that. if
1670 * this attempt fails or it has been determined that a new
1671 * extent should not be added, an attempt is made to satisfy
1672 * the request by allocating an existing (backed) free inode
1673 * from the allocation group.
1675 * PRE CONDITION: Already have the AG lock for this AG.
1678 * imap - pointer to inode map control structure.
1679 * agno - allocation group to allocate from.
1680 * dir - TRUE if the new disk inode is for a directory.
1681 * ip - pointer to the new inode to be filled in on successful return
1682 * with the disk inode number allocated, its extent address
1683 * and the start of the ag.
1687 * -ENOSPC - insufficient disk resources.
1691 diAllocAG(struct inomap * imap, int agno, boolean_t dir, struct inode *ip)
1693 int rc, addext, numfree, numinos;
1695 /* get the number of free and the number of backed disk
1696 * inodes currently within the ag.
1698 numfree = imap->im_agctl[agno].numfree;
1699 numinos = imap->im_agctl[agno].numinos;
1701 if (numfree > numinos) {
1702 jfs_error(ip->i_sb, "diAllocAG: numfree > numinos");
1706 /* determine if we should allocate a new extent of free inodes
1707 * within the ag: for directory inodes, add a new extent
1708 * if there are a small number of free inodes or number of free
1709 * inodes is a small percentage of the number of backed inodes.
1712 addext = (numfree < 64 ||
1714 && ((numfree * 100) / numinos) <= 20));
1716 addext = (numfree == 0);
1719 * try to allocate a new extent of free inodes.
1722 /* if free space is not avaliable for this new extent, try
1723 * below to allocate a free and existing (already backed)
1724 * inode from the ag.
1726 if ((rc = diAllocExt(imap, agno, ip)) != -ENOSPC)
1731 * try to allocate an existing free inode from the ag.
1733 return (diAllocIno(imap, agno, ip));
1738 * NAME: diAllocAny(imap,agno,dir,iap)
1740 * FUNCTION: allocate a disk inode from any other allocation group.
1742 * this routine is called when an allocation attempt within
1743 * the primary allocation group has failed. if attempts to
1744 * allocate an inode from any allocation group other than the
1745 * specified primary group.
1748 * imap - pointer to inode map control structure.
1749 * agno - primary allocation group (to avoid).
1750 * dir - TRUE if the new disk inode is for a directory.
1751 * ip - pointer to a new inode to be filled in on successful return
1752 * with the disk inode number allocated, its extent address
1753 * and the start of the ag.
1757 * -ENOSPC - insufficient disk resources.
1761 diAllocAny(struct inomap * imap, int agno, boolean_t dir, struct inode *ip)
1764 int maxag = JFS_SBI(imap->im_ipimap->i_sb)->bmap->db_maxag;
1767 /* try to allocate from the ags following agno up to
1768 * the maximum ag number.
1770 for (ag = agno + 1; ag <= maxag; ag++) {
1773 rc = diAllocAG(imap, ag, dir, ip);
1775 AG_UNLOCK(imap, ag);
1781 /* try to allocate from the ags in front of agno.
1783 for (ag = 0; ag < agno; ag++) {
1786 rc = diAllocAG(imap, ag, dir, ip);
1788 AG_UNLOCK(imap, ag);
1794 /* no free disk inodes.
1801 * NAME: diAllocIno(imap,agno,ip)
1803 * FUNCTION: allocate a disk inode from the allocation group's free
1804 * inode list, returning an error if this free list is
1805 * empty (i.e. no iags on the list).
1807 * allocation occurs from the first iag on the list using
1808 * the iag's free inode summary map to find the leftmost
1809 * free inode in the iag.
1811 * PRE CONDITION: Already have AG lock for this AG.
1814 * imap - pointer to inode map control structure.
1815 * agno - allocation group.
1816 * ip - pointer to new inode to be filled in on successful return
1817 * with the disk inode number allocated, its extent address
1818 * and the start of the ag.
1822 * -ENOSPC - insufficient disk resources.
1825 static int diAllocIno(struct inomap * imap, int agno, struct inode *ip)
1827 int iagno, ino, rc, rem, extno, sword;
1828 struct metapage *mp;
1831 /* check if there are iags on the ag's free inode list.
1833 if ((iagno = imap->im_agctl[agno].inofree) < 0)
1836 /* obtain read lock on imap inode */
1837 IREAD_LOCK(imap->im_ipimap);
1839 /* read the iag at the head of the list.
1841 if ((rc = diIAGRead(imap, iagno, &mp))) {
1842 IREAD_UNLOCK(imap->im_ipimap);
1845 iagp = (struct iag *) mp->data;
1847 /* better be free inodes in this iag if it is on the
1850 if (!iagp->nfreeinos) {
1851 IREAD_UNLOCK(imap->im_ipimap);
1852 release_metapage(mp);
1854 "diAllocIno: nfreeinos = 0, but iag on freelist");
1858 /* scan the free inode summary map to find an extent
1861 for (sword = 0;; sword++) {
1862 if (sword >= SMAPSZ) {
1863 IREAD_UNLOCK(imap->im_ipimap);
1864 release_metapage(mp);
1866 "diAllocIno: free inode not found in summary map");
1870 if (~iagp->inosmap[sword])
1874 /* found a extent with free inodes. determine
1875 * the extent number.
1877 rem = diFindFree(le32_to_cpu(iagp->inosmap[sword]), 0);
1878 if (rem >= EXTSPERSUM) {
1879 IREAD_UNLOCK(imap->im_ipimap);
1880 release_metapage(mp);
1881 jfs_error(ip->i_sb, "diAllocIno: no free extent found");
1884 extno = (sword << L2EXTSPERSUM) + rem;
1886 /* find the first free inode in the extent.
1888 rem = diFindFree(le32_to_cpu(iagp->wmap[extno]), 0);
1889 if (rem >= INOSPEREXT) {
1890 IREAD_UNLOCK(imap->im_ipimap);
1891 release_metapage(mp);
1892 jfs_error(ip->i_sb, "diAllocIno: free inode not found");
1896 /* compute the inode number within the iag.
1898 ino = (extno << L2INOSPEREXT) + rem;
1900 /* allocate the inode.
1902 rc = diAllocBit(imap, iagp, ino);
1903 IREAD_UNLOCK(imap->im_ipimap);
1905 release_metapage(mp);
1909 /* set the results of the allocation and write the iag.
1911 diInitInode(ip, iagno, ino, extno, iagp);
1919 * NAME: diAllocExt(imap,agno,ip)
1921 * FUNCTION: add a new extent of free inodes to an iag, allocating
1922 * an inode from this extent to satisfy the current allocation
1925 * this routine first tries to find an existing iag with free
1926 * extents through the ag free extent list. if list is not
1927 * empty, the head of the list will be selected as the home
1928 * of the new extent of free inodes. otherwise (the list is
1929 * empty), a new iag will be allocated for the ag to contain
1932 * once an iag has been selected, the free extent summary map
1933 * is used to locate a free extent within the iag and diNewExt()
1934 * is called to initialize the extent, with initialization
1935 * including the allocation of the first inode of the extent
1936 * for the purpose of satisfying this request.
1939 * imap - pointer to inode map control structure.
1940 * agno - allocation group number.
1941 * ip - pointer to new inode to be filled in on successful return
1942 * with the disk inode number allocated, its extent address
1943 * and the start of the ag.
1947 * -ENOSPC - insufficient disk resources.
1950 static int diAllocExt(struct inomap * imap, int agno, struct inode *ip)
1952 int rem, iagno, sword, extno, rc;
1953 struct metapage *mp;
1956 /* check if the ag has any iags with free extents. if not,
1957 * allocate a new iag for the ag.
1959 if ((iagno = imap->im_agctl[agno].extfree) < 0) {
1960 /* If successful, diNewIAG will obtain the read lock on the
1963 if ((rc = diNewIAG(imap, &iagno, agno, &mp))) {
1966 iagp = (struct iag *) mp->data;
1968 /* set the ag number if this a brand new iag
1971 cpu_to_le64(AGTOBLK(agno, imap->im_ipimap));
1975 IREAD_LOCK(imap->im_ipimap);
1976 if ((rc = diIAGRead(imap, iagno, &mp))) {
1977 IREAD_UNLOCK(imap->im_ipimap);
1978 jfs_error(ip->i_sb, "diAllocExt: error reading iag");
1981 iagp = (struct iag *) mp->data;
1984 /* using the free extent summary map, find a free extent.
1986 for (sword = 0;; sword++) {
1987 if (sword >= SMAPSZ) {
1988 release_metapage(mp);
1989 IREAD_UNLOCK(imap->im_ipimap);
1991 "diAllocExt: free ext summary map not found");
1994 if (~iagp->extsmap[sword])
1998 /* determine the extent number of the free extent.
2000 rem = diFindFree(le32_to_cpu(iagp->extsmap[sword]), 0);
2001 if (rem >= EXTSPERSUM) {
2002 release_metapage(mp);
2003 IREAD_UNLOCK(imap->im_ipimap);
2004 jfs_error(ip->i_sb, "diAllocExt: free extent not found");
2007 extno = (sword << L2EXTSPERSUM) + rem;
2009 /* initialize the new extent.
2011 rc = diNewExt(imap, iagp, extno);
2012 IREAD_UNLOCK(imap->im_ipimap);
2014 /* something bad happened. if a new iag was allocated,
2015 * place it back on the inode map's iag free list, and
2016 * clear the ag number information.
2018 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2020 iagp->iagfree = cpu_to_le32(imap->im_freeiag);
2021 imap->im_freeiag = iagno;
2022 IAGFREE_UNLOCK(imap);
2028 /* set the results of the allocation and write the iag.
2030 diInitInode(ip, iagno, extno << L2INOSPEREXT, extno, iagp);
2039 * NAME: diAllocBit(imap,iagp,ino)
2041 * FUNCTION: allocate a backed inode from an iag.
2043 * this routine performs the mechanics of allocating a
2044 * specified inode from a backed extent.
2046 * if the inode to be allocated represents the last free
2047 * inode within the iag, the iag will be removed from the
2048 * ag free inode list.
2050 * a careful update approach is used to provide consistency
2051 * in the face of updates to multiple buffers. under this
2052 * approach, all required buffers are obtained before making
2053 * any updates and are held all are updates are complete.
2055 * PRE CONDITION: Already have buffer lock on iagp. Already have AG lock on
2056 * this AG. Must have read lock on imap inode.
2059 * imap - pointer to inode map control structure.
2060 * iagp - pointer to iag.
2061 * ino - inode number to be allocated within the iag.
2065 * -ENOSPC - insufficient disk resources.
2068 static int diAllocBit(struct inomap * imap, struct iag * iagp, int ino)
2070 int extno, bitno, agno, sword, rc;
2071 struct metapage *amp = NULL, *bmp = NULL;
2072 struct iag *aiagp = NULL, *biagp = NULL;
2075 /* check if this is the last free inode within the iag.
2076 * if so, it will have to be removed from the ag free
2077 * inode list, so get the iags preceeding and following
2080 if (iagp->nfreeinos == cpu_to_le32(1)) {
2081 if ((int) le32_to_cpu(iagp->inofreefwd) >= 0) {
2083 diIAGRead(imap, le32_to_cpu(iagp->inofreefwd),
2086 aiagp = (struct iag *) amp->data;
2089 if ((int) le32_to_cpu(iagp->inofreeback) >= 0) {
2092 le32_to_cpu(iagp->inofreeback),
2095 release_metapage(amp);
2098 biagp = (struct iag *) bmp->data;
2102 /* get the ag number, extent number, inode number within
2105 agno = BLKTOAG(le64_to_cpu(iagp->agstart), JFS_SBI(imap->im_ipimap->i_sb));
2106 extno = ino >> L2INOSPEREXT;
2107 bitno = ino & (INOSPEREXT - 1);
2109 /* compute the mask for setting the map.
2111 mask = HIGHORDER >> bitno;
2113 /* the inode should be free and backed.
2115 if (((le32_to_cpu(iagp->pmap[extno]) & mask) != 0) ||
2116 ((le32_to_cpu(iagp->wmap[extno]) & mask) != 0) ||
2117 (addressPXD(&iagp->inoext[extno]) == 0)) {
2119 release_metapage(amp);
2121 release_metapage(bmp);
2123 jfs_error(imap->im_ipimap->i_sb,
2124 "diAllocBit: iag inconsistent");
2128 /* mark the inode as allocated in the working map.
2130 iagp->wmap[extno] |= cpu_to_le32(mask);
2132 /* check if all inodes within the extent are now
2133 * allocated. if so, update the free inode summary
2134 * map to reflect this.
2136 if (iagp->wmap[extno] == cpu_to_le32(ONES)) {
2137 sword = extno >> L2EXTSPERSUM;
2138 bitno = extno & (EXTSPERSUM - 1);
2139 iagp->inosmap[sword] |= cpu_to_le32(HIGHORDER >> bitno);
2142 /* if this was the last free inode in the iag, remove the
2143 * iag from the ag free inode list.
2145 if (iagp->nfreeinos == cpu_to_le32(1)) {
2147 aiagp->inofreeback = iagp->inofreeback;
2148 write_metapage(amp);
2152 biagp->inofreefwd = iagp->inofreefwd;
2153 write_metapage(bmp);
2155 imap->im_agctl[agno].inofree =
2156 le32_to_cpu(iagp->inofreefwd);
2158 iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
2161 /* update the free inode count at the iag, ag, inode
2164 iagp->nfreeinos = cpu_to_le32(le32_to_cpu(iagp->nfreeinos) - 1);
2165 imap->im_agctl[agno].numfree -= 1;
2166 atomic_dec(&imap->im_numfree);
2173 * NAME: diNewExt(imap,iagp,extno)
2175 * FUNCTION: initialize a new extent of inodes for an iag, allocating
2176 * the first inode of the extent for use for the current
2177 * allocation request.
2179 * disk resources are allocated for the new extent of inodes
2180 * and the inodes themselves are initialized to reflect their
2181 * existence within the extent (i.e. their inode numbers and
2182 * inode extent addresses are set) and their initial state
2183 * (mode and link count are set to zero).
2185 * if the iag is new, it is not yet on an ag extent free list
2186 * but will now be placed on this list.
2188 * if the allocation of the new extent causes the iag to
2189 * have no free extent, the iag will be removed from the
2190 * ag extent free list.
2192 * if the iag has no free backed inodes, it will be placed
2193 * on the ag free inode list, since the addition of the new
2194 * extent will now cause it to have free inodes.
2196 * a careful update approach is used to provide consistency
2197 * (i.e. list consistency) in the face of updates to multiple
2198 * buffers. under this approach, all required buffers are
2199 * obtained before making any updates and are held until all
2200 * updates are complete.
2202 * PRE CONDITION: Already have buffer lock on iagp. Already have AG lock on
2203 * this AG. Must have read lock on imap inode.
2206 * imap - pointer to inode map control structure.
2207 * iagp - pointer to iag.
2208 * extno - extent number.
2212 * -ENOSPC - insufficient disk resources.
2215 static int diNewExt(struct inomap * imap, struct iag * iagp, int extno)
2217 int agno, iagno, fwd, back, freei = 0, sword, rc;
2218 struct iag *aiagp = NULL, *biagp = NULL, *ciagp = NULL;
2219 struct metapage *amp, *bmp, *cmp, *dmp;
2220 struct inode *ipimap;
2226 struct jfs_sb_info *sbi;
2228 /* better have free extents.
2230 if (!iagp->nfreeexts) {
2231 jfs_error(imap->im_ipimap->i_sb, "diNewExt: no free extents");
2235 /* get the inode map inode.
2237 ipimap = imap->im_ipimap;
2238 sbi = JFS_SBI(ipimap->i_sb);
2240 amp = bmp = cmp = NULL;
2242 /* get the ag and iag numbers for this iag.
2244 agno = BLKTOAG(le64_to_cpu(iagp->agstart), sbi);
2245 iagno = le32_to_cpu(iagp->iagnum);
2247 /* check if this is the last free extent within the
2248 * iag. if so, the iag must be removed from the ag
2249 * free extent list, so get the iags preceeding and
2250 * following the iag on this list.
2252 if (iagp->nfreeexts == cpu_to_le32(1)) {
2253 if ((fwd = le32_to_cpu(iagp->extfreefwd)) >= 0) {
2254 if ((rc = diIAGRead(imap, fwd, &)))
2256 aiagp = (struct iag *) amp->data;
2259 if ((back = le32_to_cpu(iagp->extfreeback)) >= 0) {
2260 if ((rc = diIAGRead(imap, back, &bmp)))
2262 biagp = (struct iag *) bmp->data;
2265 /* the iag has free extents. if all extents are free
2266 * (as is the case for a newly allocated iag), the iag
2267 * must be added to the ag free extent list, so get
2268 * the iag at the head of the list in preparation for
2269 * adding this iag to this list.
2272 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2273 if ((fwd = imap->im_agctl[agno].extfree) >= 0) {
2274 if ((rc = diIAGRead(imap, fwd, &)))
2276 aiagp = (struct iag *) amp->data;
2281 /* check if the iag has no free inodes. if so, the iag
2282 * will have to be added to the ag free inode list, so get
2283 * the iag at the head of the list in preparation for
2284 * adding this iag to this list. in doing this, we must
2285 * check if we already have the iag at the head of
2288 if (iagp->nfreeinos == 0) {
2289 freei = imap->im_agctl[agno].inofree;
2294 } else if (freei == back) {
2297 if ((rc = diIAGRead(imap, freei, &cmp)))
2299 ciagp = (struct iag *) cmp->data;
2301 if (ciagp == NULL) {
2302 jfs_error(imap->im_ipimap->i_sb,
2303 "diNewExt: ciagp == NULL");
2310 /* allocate disk space for the inode extent.
2312 if ((extno == 0) || (addressPXD(&iagp->inoext[extno - 1]) == 0))
2313 hint = ((s64) agno << sbi->bmap->db_agl2size) - 1;
2315 hint = addressPXD(&iagp->inoext[extno - 1]) +
2316 lengthPXD(&iagp->inoext[extno - 1]) - 1;
2318 if ((rc = dbAlloc(ipimap, hint, (s64) imap->im_nbperiext, &blkno)))
2321 /* compute the inode number of the first inode within the
2324 ino = (iagno << L2INOSPERIAG) + (extno << L2INOSPEREXT);
2326 /* initialize the inodes within the newly allocated extent a
2329 for (i = 0; i < imap->im_nbperiext; i += sbi->nbperpage) {
2330 /* get a buffer for this page of disk inodes.
2332 dmp = get_metapage(ipimap, blkno + i, PSIZE, 1);
2337 dp = (struct dinode *) dmp->data;
2339 /* initialize the inode number, mode, link count and
2340 * inode extent address.
2342 for (j = 0; j < INOSPERPAGE; j++, dp++, ino++) {
2343 dp->di_inostamp = cpu_to_le32(sbi->inostamp);
2344 dp->di_number = cpu_to_le32(ino);
2345 dp->di_fileset = cpu_to_le32(FILESYSTEM_I);
2348 PXDaddress(&(dp->di_ixpxd), blkno);
2349 PXDlength(&(dp->di_ixpxd), imap->im_nbperiext);
2351 write_metapage(dmp);
2354 /* if this is the last free extent within the iag, remove the
2355 * iag from the ag free extent list.
2357 if (iagp->nfreeexts == cpu_to_le32(1)) {
2359 aiagp->extfreeback = iagp->extfreeback;
2362 biagp->extfreefwd = iagp->extfreefwd;
2364 imap->im_agctl[agno].extfree =
2365 le32_to_cpu(iagp->extfreefwd);
2367 iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
2369 /* if the iag has all free extents (newly allocated iag),
2370 * add the iag to the ag free extent list.
2372 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2374 aiagp->extfreeback = cpu_to_le32(iagno);
2376 iagp->extfreefwd = cpu_to_le32(fwd);
2377 iagp->extfreeback = cpu_to_le32(-1);
2378 imap->im_agctl[agno].extfree = iagno;
2382 /* if the iag has no free inodes, add the iag to the
2383 * ag free inode list.
2385 if (iagp->nfreeinos == 0) {
2387 ciagp->inofreeback = cpu_to_le32(iagno);
2390 cpu_to_le32(imap->im_agctl[agno].inofree);
2391 iagp->inofreeback = cpu_to_le32(-1);
2392 imap->im_agctl[agno].inofree = iagno;
2395 /* initialize the extent descriptor of the extent. */
2396 PXDlength(&iagp->inoext[extno], imap->im_nbperiext);
2397 PXDaddress(&iagp->inoext[extno], blkno);
2399 /* initialize the working and persistent map of the extent.
2400 * the working map will be initialized such that
2401 * it indicates the first inode of the extent is allocated.
2403 iagp->wmap[extno] = cpu_to_le32(HIGHORDER);
2404 iagp->pmap[extno] = 0;
2406 /* update the free inode and free extent summary maps
2407 * for the extent to indicate the extent has free inodes
2408 * and no longer represents a free extent.
2410 sword = extno >> L2EXTSPERSUM;
2411 mask = HIGHORDER >> (extno & (EXTSPERSUM - 1));
2412 iagp->extsmap[sword] |= cpu_to_le32(mask);
2413 iagp->inosmap[sword] &= cpu_to_le32(~mask);
2415 /* update the free inode and free extent counts for the
2418 iagp->nfreeinos = cpu_to_le32(le32_to_cpu(iagp->nfreeinos) +
2420 iagp->nfreeexts = cpu_to_le32(le32_to_cpu(iagp->nfreeexts) - 1);
2422 /* update the free and backed inode counts for the ag.
2424 imap->im_agctl[agno].numfree += (INOSPEREXT - 1);
2425 imap->im_agctl[agno].numinos += INOSPEREXT;
2427 /* update the free and backed inode counts for the inode map.
2429 atomic_add(INOSPEREXT - 1, &imap->im_numfree);
2430 atomic_add(INOSPEREXT, &imap->im_numinos);
2435 write_metapage(amp);
2437 write_metapage(bmp);
2439 write_metapage(cmp);
2445 /* release the iags.
2448 release_metapage(amp);
2450 release_metapage(bmp);
2452 release_metapage(cmp);
2459 * NAME: diNewIAG(imap,iagnop,agno)
2461 * FUNCTION: allocate a new iag for an allocation group.
2463 * first tries to allocate the iag from the inode map
2465 * if the list has free iags, the head of the list is removed
2466 * and returned to satisfy the request.
2467 * if the inode map's iag free list is empty, the inode map
2468 * is extended to hold a new iag. this new iag is initialized
2469 * and returned to satisfy the request.
2472 * imap - pointer to inode map control structure.
2473 * iagnop - pointer to an iag number set with the number of the
2474 * newly allocated iag upon successful return.
2475 * agno - allocation group number.
2476 * bpp - Buffer pointer to be filled in with new IAG's buffer
2480 * -ENOSPC - insufficient disk resources.
2484 * AG lock held on entry/exit;
2485 * write lock on the map is held inside;
2486 * read lock on the map is held on successful completion;
2488 * note: new iag transaction:
2489 * . synchronously write iag;
2490 * . write log of xtree and inode of imap;
2492 * . synchronous write of xtree (right to left, bottom to top);
2493 * . at start of logredo(): init in-memory imap with one additional iag page;
2494 * . at end of logredo(): re-read imap inode to determine
2498 diNewIAG(struct inomap * imap, int *iagnop, int agno, struct metapage ** mpp)
2502 struct inode *ipimap;
2503 struct super_block *sb;
2504 struct jfs_sb_info *sbi;
2505 struct metapage *mp;
2510 #ifdef _STILL_TO_PORT
2512 #endif /* _STILL_TO_PORT */
2513 struct inode *iplist[1];
2515 /* pick up pointers to the inode map and mount inodes */
2516 ipimap = imap->im_ipimap;
2520 /* acquire the free iag lock */
2523 /* if there are any iags on the inode map free iag list,
2524 * allocate the iag from the head of the list.
2526 if (imap->im_freeiag >= 0) {
2527 /* pick up the iag number at the head of the list */
2528 iagno = imap->im_freeiag;
2530 /* determine the logical block number of the iag */
2531 blkno = IAGTOLBLK(iagno, sbi->l2nbperpage);
2533 /* no free iags. the inode map will have to be extented
2534 * to include a new iag.
2537 /* acquire inode map lock */
2538 IWRITE_LOCK(ipimap);
2540 if (ipimap->i_size >> L2PSIZE != imap->im_nextiag + 1) {
2541 IWRITE_UNLOCK(ipimap);
2542 IAGFREE_UNLOCK(imap);
2543 jfs_error(imap->im_ipimap->i_sb,
2544 "diNewIAG: ipimap->i_size is wrong");
2549 /* get the next avaliable iag number */
2550 iagno = imap->im_nextiag;
2552 /* make sure that we have not exceeded the maximum inode
2555 if (iagno > (MAXIAGS - 1)) {
2556 /* release the inode map lock */
2557 IWRITE_UNLOCK(ipimap);
2564 * synchronously append new iag page.
2566 /* determine the logical address of iag page to append */
2567 blkno = IAGTOLBLK(iagno, sbi->l2nbperpage);
2569 /* Allocate extent for new iag page */
2570 xlen = sbi->nbperpage;
2571 if ((rc = dbAlloc(ipimap, 0, (s64) xlen, &xaddr))) {
2572 /* release the inode map lock */
2573 IWRITE_UNLOCK(ipimap);
2578 /* assign a buffer for the page */
2579 mp = get_metapage(ipimap, xaddr, PSIZE, 1);
2581 /* Free the blocks allocated for the iag since it was
2582 * not successfully added to the inode map
2584 dbFree(ipimap, xaddr, (s64) xlen);
2586 /* release the inode map lock */
2587 IWRITE_UNLOCK(ipimap);
2592 iagp = (struct iag *) mp->data;
2595 memset(iagp, 0, sizeof(struct iag));
2596 iagp->iagnum = cpu_to_le32(iagno);
2597 iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
2598 iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
2599 iagp->iagfree = cpu_to_le32(-1);
2600 iagp->nfreeinos = 0;
2601 iagp->nfreeexts = cpu_to_le32(EXTSPERIAG);
2603 /* initialize the free inode summary map (free extent
2604 * summary map initialization handled by bzero).
2606 for (i = 0; i < SMAPSZ; i++)
2607 iagp->inosmap[i] = cpu_to_le32(ONES);
2612 * start tyransaction of update of the inode map
2613 * addressing structure pointing to the new iag page;
2615 tid = txBegin(sb, COMMIT_FORCE);
2616 down(&JFS_IP(ipimap)->commit_sem);
2618 /* update the inode map addressing structure to point to it */
2620 xtInsert(tid, ipimap, 0, blkno, xlen, &xaddr, 0))) {
2622 up(&JFS_IP(ipimap)->commit_sem);
2623 /* Free the blocks allocated for the iag since it was
2624 * not successfully added to the inode map
2626 dbFree(ipimap, xaddr, (s64) xlen);
2628 /* release the inode map lock */
2629 IWRITE_UNLOCK(ipimap);
2634 /* update the inode map's inode to reflect the extension */
2635 ipimap->i_size += PSIZE;
2636 inode_add_bytes(ipimap, PSIZE);
2639 * txCommit(COMMIT_FORCE) will synchronously write address
2640 * index pages and inode after commit in careful update order
2641 * of address index pages (right to left, bottom up);
2644 rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
2647 up(&JFS_IP(ipimap)->commit_sem);
2649 duplicateIXtree(sb, blkno, xlen, &xaddr);
2651 /* update the next avaliable iag number */
2652 imap->im_nextiag += 1;
2654 /* Add the iag to the iag free list so we don't lose the iag
2655 * if a failure happens now.
2657 imap->im_freeiag = iagno;
2659 /* Until we have logredo working, we want the imap inode &
2660 * control page to be up to date.
2664 /* release the inode map lock */
2665 IWRITE_UNLOCK(ipimap);
2668 /* obtain read lock on map */
2672 if ((rc = diIAGRead(imap, iagno, &mp))) {
2673 IREAD_UNLOCK(ipimap);
2677 iagp = (struct iag *) mp->data;
2679 /* remove the iag from the iag free list */
2680 imap->im_freeiag = le32_to_cpu(iagp->iagfree);
2681 iagp->iagfree = cpu_to_le32(-1);
2683 /* set the return iag number and buffer pointer */
2688 /* release the iag free lock */
2689 IAGFREE_UNLOCK(imap);
2697 * FUNCTION: get the buffer for the specified iag within a fileset
2698 * or aggregate inode map.
2701 * imap - pointer to inode map control structure.
2702 * iagno - iag number.
2703 * bpp - point to buffer pointer to be filled in on successful
2707 * must have read lock on imap inode
2708 * (When called by diExtendFS, the filesystem is quiesced, therefore
2709 * the read lock is unnecessary.)
2715 static int diIAGRead(struct inomap * imap, int iagno, struct metapage ** mpp)
2717 struct inode *ipimap = imap->im_ipimap;
2720 /* compute the logical block number of the iag. */
2721 blkno = IAGTOLBLK(iagno, JFS_SBI(ipimap->i_sb)->l2nbperpage);
2724 *mpp = read_metapage(ipimap, blkno, PSIZE, 0);
2733 * NAME: diFindFree()
2735 * FUNCTION: find the first free bit in a word starting at
2736 * the specified bit position.
2739 * word - word to be examined.
2740 * start - starting bit position.
2743 * bit position of first free bit in the word or 32 if
2744 * no free bits were found.
2746 static int diFindFree(u32 word, int start)
2750 /* scan the word for the first free bit. */
2751 for (word <<= start, bitno = start; bitno < 32;
2752 bitno++, word <<= 1) {
2753 if ((word & HIGHORDER) == 0)
2760 * NAME: diUpdatePMap()
2762 * FUNCTION: Update the persistent map in an IAG for the allocation or
2763 * freeing of the specified inode.
2765 * PRE CONDITIONS: Working map has already been updated for allocate.
2768 * ipimap - Incore inode map inode
2769 * inum - Number of inode to mark in permanent map
2770 * is_free - If TRUE indicates inode should be marked freed, otherwise
2771 * indicates inode should be marked allocated.
2777 diUpdatePMap(struct inode *ipimap,
2778 unsigned long inum, boolean_t is_free, struct tblock * tblk)
2782 struct metapage *mp;
2783 int iagno, ino, extno, bitno;
2784 struct inomap *imap;
2786 struct jfs_log *log;
2787 int lsn, difft, diffp;
2789 imap = JFS_IP(ipimap)->i_imap;
2790 /* get the iag number containing the inode */
2791 iagno = INOTOIAG(inum);
2792 /* make sure that the iag is contained within the map */
2793 if (iagno >= imap->im_nextiag) {
2794 jfs_error(ipimap->i_sb,
2795 "diUpdatePMap: the iag is outside the map");
2800 rc = diIAGRead(imap, iagno, &mp);
2801 IREAD_UNLOCK(ipimap);
2804 iagp = (struct iag *) mp->data;
2805 /* get the inode number and extent number of the inode within
2806 * the iag and the inode number within the extent.
2808 ino = inum & (INOSPERIAG - 1);
2809 extno = ino >> L2INOSPEREXT;
2810 bitno = ino & (INOSPEREXT - 1);
2811 mask = HIGHORDER >> bitno;
2813 * mark the inode free in persistent map:
2815 if (is_free == TRUE) {
2816 /* The inode should have been allocated both in working
2817 * map and in persistent map;
2818 * the inode will be freed from working map at the release
2819 * of last reference release;
2821 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
2822 jfs_error(ipimap->i_sb,
2823 "diUpdatePMap: inode %ld not marked as "
2824 "allocated in wmap!", inum);
2826 if (!(le32_to_cpu(iagp->pmap[extno]) & mask)) {
2827 jfs_error(ipimap->i_sb,
2828 "diUpdatePMap: inode %ld not marked as "
2829 "allocated in pmap!", inum);
2831 /* update the bitmap for the extent of the freed inode */
2832 iagp->pmap[extno] &= cpu_to_le32(~mask);
2835 * mark the inode allocated in persistent map:
2838 /* The inode should be already allocated in the working map
2839 * and should be free in persistent map;
2841 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
2842 release_metapage(mp);
2843 jfs_error(ipimap->i_sb,
2844 "diUpdatePMap: the inode is not allocated in "
2848 if ((le32_to_cpu(iagp->pmap[extno]) & mask) != 0) {
2849 release_metapage(mp);
2850 jfs_error(ipimap->i_sb,
2851 "diUpdatePMap: the inode is not free in the "
2855 /* update the bitmap for the extent of the allocated inode */
2856 iagp->pmap[extno] |= cpu_to_le32(mask);
2862 log = JFS_SBI(tblk->sb)->log;
2864 /* inherit older/smaller lsn */
2865 logdiff(difft, lsn, log);
2866 logdiff(diffp, mp->lsn, log);
2867 if (difft < diffp) {
2869 /* move mp after tblock in logsync list */
2871 list_move(&mp->synclist, &tblk->synclist);
2872 LOGSYNC_UNLOCK(log);
2874 /* inherit younger/larger clsn */
2877 logdiff(difft, tblk->clsn, log);
2878 logdiff(diffp, mp->clsn, log);
2880 mp->clsn = tblk->clsn;
2881 LOGSYNC_UNLOCK(log);
2885 /* insert mp after tblock in logsync list */
2888 list_add(&mp->synclist, &tblk->synclist);
2889 mp->clsn = tblk->clsn;
2890 LOGSYNC_UNLOCK(log);
2899 * function: update imap for extendfs();
2901 * note: AG size has been increased s.t. each k old contiguous AGs are
2902 * coalesced into a new AG;
2904 int diExtendFS(struct inode *ipimap, struct inode *ipbmap)
2907 struct inomap *imap = JFS_IP(ipimap)->i_imap;
2908 struct iag *iagp = NULL, *hiagp = NULL;
2909 struct bmap *mp = JFS_SBI(ipbmap->i_sb)->bmap;
2910 struct metapage *bp, *hbp;
2912 int numinos, xnuminos = 0, xnumfree = 0;
2915 jfs_info("diExtendFS: nextiag:%d numinos:%d numfree:%d",
2916 imap->im_nextiag, atomic_read(&imap->im_numinos),
2917 atomic_read(&imap->im_numfree));
2922 * coalesce contiguous k (newAGSize/oldAGSize) AGs;
2923 * i.e., (AGi, ..., AGj) where i = k*n and j = k*(n+1) - 1 to AGn;
2924 * note: new AG size = old AG size * (2**x).
2927 /* init per AG control information im_agctl[] */
2928 for (i = 0; i < MAXAG; i++) {
2929 imap->im_agctl[i].inofree = -1;
2930 imap->im_agctl[i].extfree = -1;
2931 imap->im_agctl[i].numinos = 0; /* number of backed inodes */
2932 imap->im_agctl[i].numfree = 0; /* number of free backed inodes */
2936 * process each iag page of the map.
2938 * rebuild AG Free Inode List, AG Free Inode Extent List;
2940 for (i = 0; i < imap->im_nextiag; i++) {
2941 if ((rc = diIAGRead(imap, i, &bp))) {
2945 iagp = (struct iag *) bp->data;
2946 if (le32_to_cpu(iagp->iagnum) != i) {
2947 release_metapage(bp);
2948 jfs_error(ipimap->i_sb,
2949 "diExtendFs: unexpected value of iagnum");
2953 /* leave free iag in the free iag list */
2954 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2955 release_metapage(bp);
2959 /* agstart that computes to the same ag is treated as same; */
2960 agstart = le64_to_cpu(iagp->agstart);
2961 /* iagp->agstart = agstart & ~(mp->db_agsize - 1); */
2962 n = agstart >> mp->db_agl2size;
2964 /* compute backed inodes */
2965 numinos = (EXTSPERIAG - le32_to_cpu(iagp->nfreeexts))
2968 /* merge AG backed inodes */
2969 imap->im_agctl[n].numinos += numinos;
2970 xnuminos += numinos;
2973 /* if any backed free inodes, insert at AG free inode list */
2974 if ((int) le32_to_cpu(iagp->nfreeinos) > 0) {
2975 if ((head = imap->im_agctl[n].inofree) == -1) {
2976 iagp->inofreefwd = cpu_to_le32(-1);
2977 iagp->inofreeback = cpu_to_le32(-1);
2979 if ((rc = diIAGRead(imap, head, &hbp))) {
2983 hiagp = (struct iag *) hbp->data;
2984 hiagp->inofreeback = iagp->iagnum;
2985 iagp->inofreefwd = cpu_to_le32(head);
2986 iagp->inofreeback = cpu_to_le32(-1);
2987 write_metapage(hbp);
2990 imap->im_agctl[n].inofree =
2991 le32_to_cpu(iagp->iagnum);
2993 /* merge AG backed free inodes */
2994 imap->im_agctl[n].numfree +=
2995 le32_to_cpu(iagp->nfreeinos);
2996 xnumfree += le32_to_cpu(iagp->nfreeinos);
2999 /* if any free extents, insert at AG free extent list */
3000 if (le32_to_cpu(iagp->nfreeexts) > 0) {
3001 if ((head = imap->im_agctl[n].extfree) == -1) {
3002 iagp->extfreefwd = cpu_to_le32(-1);
3003 iagp->extfreeback = cpu_to_le32(-1);
3005 if ((rc = diIAGRead(imap, head, &hbp))) {
3009 hiagp = (struct iag *) hbp->data;
3010 hiagp->extfreeback = iagp->iagnum;
3011 iagp->extfreefwd = cpu_to_le32(head);
3012 iagp->extfreeback = cpu_to_le32(-1);
3013 write_metapage(hbp);
3016 imap->im_agctl[n].extfree =
3017 le32_to_cpu(iagp->iagnum);
3024 if (xnuminos != atomic_read(&imap->im_numinos) ||
3025 xnumfree != atomic_read(&imap->im_numfree)) {
3026 jfs_error(ipimap->i_sb,
3027 "diExtendFs: numinos or numfree incorrect");
3038 * serialization: IWRITE_LOCK held on entry/exit
3040 * note: shadow page with regular inode (rel.2);
3042 static void duplicateIXtree(struct super_block *sb, s64 blkno,
3043 int xlen, s64 *xaddr)
3045 struct jfs_superblock *j_sb;
3046 struct buffer_head *bh;
3050 /* if AIT2 ipmap2 is bad, do not try to update it */
3051 if (JFS_SBI(sb)->mntflag & JFS_BAD_SAIT) /* s_flag */
3053 ip = diReadSpecial(sb, FILESYSTEM_I, 1);
3055 JFS_SBI(sb)->mntflag |= JFS_BAD_SAIT;
3056 if (readSuper(sb, &bh))
3058 j_sb = (struct jfs_superblock *)bh->b_data;
3059 j_sb->s_flag |= cpu_to_le32(JFS_BAD_SAIT);
3061 mark_buffer_dirty(bh);
3062 sync_dirty_buffer(bh);
3067 /* start transaction */
3068 tid = txBegin(sb, COMMIT_FORCE);
3069 /* update the inode map addressing structure to point to it */
3070 if (xtInsert(tid, ip, 0, blkno, xlen, xaddr, 0)) {
3071 JFS_SBI(sb)->mntflag |= JFS_BAD_SAIT;
3076 /* update the inode map's inode to reflect the extension */
3077 ip->i_size += PSIZE;
3078 inode_add_bytes(ip, PSIZE);
3079 txCommit(tid, 1, &ip, COMMIT_FORCE);
3086 * NAME: copy_from_dinode()
3088 * FUNCTION: Copies inode info from disk inode to in-memory inode
3092 * -ENOMEM - insufficient memory
3094 static int copy_from_dinode(struct dinode * dip, struct inode *ip)
3096 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
3100 jfs_ip->fileset = le32_to_cpu(dip->di_fileset);
3101 jfs_ip->mode2 = le32_to_cpu(dip->di_mode);
3103 ip->i_mode = le32_to_cpu(dip->di_mode) & 0xffff;
3104 ip->i_nlink = le32_to_cpu(dip->di_nlink);
3106 uid = le32_to_cpu(dip->di_uid);
3107 gid = le32_to_cpu(dip->di_gid);
3108 ip->i_uid = INOXID_UID(XID_TAG(ip), uid, gid);
3109 ip->i_gid = INOXID_GID(XID_TAG(ip), uid, gid);
3110 ip->i_xid = INOXID_XID(XID_TAG(ip), uid, gid, 0);
3112 ip->i_size = le64_to_cpu(dip->di_size);
3113 ip->i_atime.tv_sec = le32_to_cpu(dip->di_atime.tv_sec);
3114 ip->i_atime.tv_nsec = le32_to_cpu(dip->di_atime.tv_nsec);
3115 ip->i_mtime.tv_sec = le32_to_cpu(dip->di_mtime.tv_sec);
3116 ip->i_mtime.tv_nsec = le32_to_cpu(dip->di_mtime.tv_nsec);
3117 ip->i_ctime.tv_sec = le32_to_cpu(dip->di_ctime.tv_sec);
3118 ip->i_ctime.tv_nsec = le32_to_cpu(dip->di_ctime.tv_nsec);
3119 ip->i_blksize = ip->i_sb->s_blocksize;
3120 ip->i_blocks = LBLK2PBLK(ip->i_sb, le64_to_cpu(dip->di_nblocks));
3121 ip->i_generation = le32_to_cpu(dip->di_gen);
3123 jfs_ip->ixpxd = dip->di_ixpxd; /* in-memory pxd's are little-endian */
3124 jfs_ip->acl = dip->di_acl; /* as are dxd's */
3125 jfs_ip->ea = dip->di_ea;
3126 jfs_ip->next_index = le32_to_cpu(dip->di_next_index);
3127 jfs_ip->otime = le32_to_cpu(dip->di_otime.tv_sec);
3128 jfs_ip->acltype = le32_to_cpu(dip->di_acltype);
3130 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode)) {
3131 jfs_ip->dev = le32_to_cpu(dip->di_rdev);
3132 ip->i_rdev = new_decode_dev(jfs_ip->dev);
3135 if (S_ISDIR(ip->i_mode)) {
3136 memcpy(&jfs_ip->i_dirtable, &dip->di_dirtable, 384);
3137 } else if (S_ISREG(ip->i_mode) || S_ISLNK(ip->i_mode)) {
3138 memcpy(&jfs_ip->i_xtroot, &dip->di_xtroot, 288);
3140 memcpy(&jfs_ip->i_inline_ea, &dip->di_inlineea, 128);
3142 /* Zero the in-memory-only stuff */
3144 jfs_ip->btindex = 0;
3145 jfs_ip->btorder = 0;
3148 jfs_ip->atlhead = 0;
3149 jfs_ip->atltail = 0;
3155 * NAME: copy_to_dinode()
3157 * FUNCTION: Copies inode info from in-memory inode to disk inode
3159 static void copy_to_dinode(struct dinode * dip, struct inode *ip)
3161 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
3165 dip->di_fileset = cpu_to_le32(jfs_ip->fileset);
3166 dip->di_inostamp = cpu_to_le32(JFS_SBI(ip->i_sb)->inostamp);
3167 dip->di_number = cpu_to_le32(ip->i_ino);
3168 dip->di_gen = cpu_to_le32(ip->i_generation);
3169 dip->di_size = cpu_to_le64(ip->i_size);
3170 dip->di_nblocks = cpu_to_le64(PBLK2LBLK(ip->i_sb, ip->i_blocks));
3171 dip->di_nlink = cpu_to_le32(ip->i_nlink);
3173 uid = XIDINO_UID(XID_TAG(ip), ip->i_uid, ip->i_xid);
3174 gid = XIDINO_GID(XID_TAG(ip), ip->i_gid, ip->i_xid);
3175 dip->di_uid = cpu_to_le32(uid);
3176 dip->di_gid = cpu_to_le32(gid);
3178 * mode2 is only needed for storing the higher order bits.
3179 * Trust i_mode for the lower order ones
3181 dip->di_mode = cpu_to_le32((jfs_ip->mode2 & 0xffff0000) | ip->i_mode);
3182 dip->di_atime.tv_sec = cpu_to_le32(ip->i_atime.tv_sec);
3183 dip->di_atime.tv_nsec = cpu_to_le32(ip->i_atime.tv_nsec);
3184 dip->di_ctime.tv_sec = cpu_to_le32(ip->i_ctime.tv_sec);
3185 dip->di_ctime.tv_nsec = cpu_to_le32(ip->i_ctime.tv_nsec);
3186 dip->di_mtime.tv_sec = cpu_to_le32(ip->i_mtime.tv_sec);
3187 dip->di_mtime.tv_nsec = cpu_to_le32(ip->i_mtime.tv_nsec);
3188 dip->di_ixpxd = jfs_ip->ixpxd; /* in-memory pxd's are little-endian */
3189 dip->di_acl = jfs_ip->acl; /* as are dxd's */
3190 dip->di_ea = jfs_ip->ea;
3191 dip->di_next_index = cpu_to_le32(jfs_ip->next_index);
3192 dip->di_otime.tv_sec = cpu_to_le32(jfs_ip->otime);
3193 dip->di_otime.tv_nsec = 0;
3194 dip->di_acltype = cpu_to_le32(jfs_ip->acltype);
3195 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode))
3196 dip->di_rdev = cpu_to_le32(jfs_ip->dev);
3199 #ifdef _JFS_DEBUG_IMAP
3203 static void *DBGdiInit(struct inomap * imap)
3208 if ((dimap = (u32 *) xmalloc(size, L2PSIZE, kernel_heap)) == NULL)
3210 bzero((void *) dimap, size);
3211 imap->im_DBGdimap = dimap;
3217 static void DBGdiAlloc(struct inomap * imap, ino_t ino)
3219 u32 *dimap = imap->im_DBGdimap;
3224 m = 0x80000000 >> b;
3225 assert(w < 64 * 256);
3227 printk("DEBUG diAlloc: duplicate alloc ino:0x%x\n", ino);
3235 static void DBGdiFree(struct inomap * imap, ino_t ino)
3237 u32 *dimap = imap->im_DBGdimap;
3242 m = 0x80000000 >> b;
3243 assert(w < 64 * 256);
3244 if ((dimap[w] & m) == 0) {
3245 printk("DEBUG diFree: duplicate free ino:0x%x\n", ino);
3250 static void dump_cp(struct inomap * ipimap, char *function, int line)
3252 printk("\n* ********* *\nControl Page %s %d\n", function, line);
3253 printk("FreeIAG %d\tNextIAG %d\n", ipimap->im_freeiag,
3254 ipimap->im_nextiag);
3255 printk("NumInos %d\tNumFree %d\n",
3256 atomic_read(&ipimap->im_numinos),
3257 atomic_read(&ipimap->im_numfree));
3258 printk("AG InoFree %d\tAG ExtFree %d\n",
3259 ipimap->im_agctl[0].inofree, ipimap->im_agctl[0].extfree);
3260 printk("AG NumInos %d\tAG NumFree %d\n",
3261 ipimap->im_agctl[0].numinos, ipimap->im_agctl[0].numfree);
3264 static void dump_iag(struct iag * iag, char *function, int line)
3266 printk("\n* ********* *\nIAG %s %d\n", function, line);
3267 printk("IagNum %d\tIAG Free %d\n", le32_to_cpu(iag->iagnum),
3268 le32_to_cpu(iag->iagfree));
3269 printk("InoFreeFwd %d\tInoFreeBack %d\n",
3270 le32_to_cpu(iag->inofreefwd),
3271 le32_to_cpu(iag->inofreeback));
3272 printk("ExtFreeFwd %d\tExtFreeBack %d\n",
3273 le32_to_cpu(iag->extfreefwd),
3274 le32_to_cpu(iag->extfreeback));
3275 printk("NFreeInos %d\tNFreeExts %d\n", le32_to_cpu(iag->nfreeinos),
3276 le32_to_cpu(iag->nfreeexts));
3278 #endif /* _JFS_DEBUG_IMAP */