2 * Copyright (C) International Business Machines Corp., 2000-2003
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/vserver/xid.h>
49 #include "jfs_incore.h"
50 #include "jfs_filsys.h"
51 #include "jfs_dinode.h"
54 #include "jfs_metapage.h"
55 #include "jfs_superblock.h"
56 #include "jfs_debug.h"
61 /* iag free list lock */
62 #define IAGFREE_LOCK_INIT(imap) init_MUTEX(&imap->im_freelock)
63 #define IAGFREE_LOCK(imap) down(&imap->im_freelock)
64 #define IAGFREE_UNLOCK(imap) up(&imap->im_freelock)
66 /* per ag iag list locks */
67 #define AG_LOCK_INIT(imap,index) init_MUTEX(&(imap->im_aglock[index]))
68 #define AG_LOCK(imap,agno) down(&imap->im_aglock[agno])
69 #define AG_UNLOCK(imap,agno) up(&imap->im_aglock[agno])
74 extern struct address_space_operations jfs_aops;
79 static int diAllocAG(struct inomap *, int, boolean_t, struct inode *);
80 static int diAllocAny(struct inomap *, int, boolean_t, struct inode *);
81 static int diAllocBit(struct inomap *, struct iag *, int);
82 static int diAllocExt(struct inomap *, int, struct inode *);
83 static int diAllocIno(struct inomap *, int, struct inode *);
84 static int diFindFree(u32, int);
85 static int diNewExt(struct inomap *, struct iag *, int);
86 static int diNewIAG(struct inomap *, int *, int, struct metapage **);
87 static void duplicateIXtree(struct super_block *, s64, int, s64 *);
89 static int diIAGRead(struct inomap * imap, int, struct metapage **);
90 static int copy_from_dinode(struct dinode *, struct inode *);
91 static void copy_to_dinode(struct dinode *, struct inode *);
94 * debug code for double-checking inode map
96 /* #define _JFS_DEBUG_IMAP 1 */
98 #ifdef _JFS_DEBUG_IMAP
99 #define DBG_DIINIT(imap) DBGdiInit(imap)
100 #define DBG_DIALLOC(imap, ino) DBGdiAlloc(imap, ino)
101 #define DBG_DIFREE(imap, ino) DBGdiFree(imap, ino)
103 static void *DBGdiInit(struct inomap * imap);
104 static void DBGdiAlloc(struct inomap * imap, ino_t ino);
105 static void DBGdiFree(struct inomap * imap, ino_t ino);
107 #define DBG_DIINIT(imap)
108 #define DBG_DIALLOC(imap, ino)
109 #define DBG_DIFREE(imap, ino)
110 #endif /* _JFS_DEBUG_IMAP */
115 * FUNCTION: initialize the incore inode map control structures for
116 * a fileset or aggregate init time.
118 * the inode map's control structure (dinomap) is
119 * brought in from disk and placed in virtual memory.
122 * ipimap - pointer to inode map inode for the aggregate or fileset.
126 * -ENOMEM - insufficient free virtual memory.
129 int diMount(struct inode *ipimap)
134 struct dinomap *dinom_le;
137 * allocate/initialize the in-memory inode map control structure
139 /* allocate the in-memory inode map control structure. */
140 imap = (struct inomap *) kmalloc(sizeof(struct inomap), GFP_KERNEL);
142 jfs_err("diMount: kmalloc returned NULL!");
146 /* read the on-disk inode map control structure. */
148 mp = read_metapage(ipimap,
149 IMAPBLKNO << JFS_SBI(ipimap->i_sb)->l2nbperpage,
156 /* copy the on-disk version to the in-memory version. */
157 dinom_le = (struct dinomap *) mp->data;
158 imap->im_freeiag = le32_to_cpu(dinom_le->in_freeiag);
159 imap->im_nextiag = le32_to_cpu(dinom_le->in_nextiag);
160 atomic_set(&imap->im_numinos, le32_to_cpu(dinom_le->in_numinos));
161 atomic_set(&imap->im_numfree, le32_to_cpu(dinom_le->in_numfree));
162 imap->im_nbperiext = le32_to_cpu(dinom_le->in_nbperiext);
163 imap->im_l2nbperiext = le32_to_cpu(dinom_le->in_l2nbperiext);
164 for (index = 0; index < MAXAG; index++) {
165 imap->im_agctl[index].inofree =
166 le32_to_cpu(dinom_le->in_agctl[index].inofree);
167 imap->im_agctl[index].extfree =
168 le32_to_cpu(dinom_le->in_agctl[index].extfree);
169 imap->im_agctl[index].numinos =
170 le32_to_cpu(dinom_le->in_agctl[index].numinos);
171 imap->im_agctl[index].numfree =
172 le32_to_cpu(dinom_le->in_agctl[index].numfree);
175 /* release the buffer. */
176 release_metapage(mp);
179 * allocate/initialize inode allocation map locks
181 /* allocate and init iag free list lock */
182 IAGFREE_LOCK_INIT(imap);
184 /* allocate and init ag list locks */
185 for (index = 0; index < MAXAG; index++) {
186 AG_LOCK_INIT(imap, index);
189 /* bind the inode map inode and inode map control structure
192 imap->im_ipimap = ipimap;
193 JFS_IP(ipimap)->i_imap = imap;
204 * FUNCTION: write to disk the incore inode map control structures for
205 * a fileset or aggregate at unmount time.
208 * ipimap - pointer to inode map inode for the aggregate or fileset.
212 * -ENOMEM - insufficient free virtual memory.
215 int diUnmount(struct inode *ipimap, int mounterror)
217 struct inomap *imap = JFS_IP(ipimap)->i_imap;
220 * update the on-disk inode map control structure
223 if (!(mounterror || isReadOnly(ipimap)))
227 * Invalidate the page cache buffers
229 truncate_inode_pages(ipimap->i_mapping, 0);
232 * free in-memory control structure
243 int diSync(struct inode *ipimap)
245 struct dinomap *dinom_le;
246 struct inomap *imp = JFS_IP(ipimap)->i_imap;
251 * write imap global conrol page
253 /* read the on-disk inode map control structure */
254 mp = get_metapage(ipimap,
255 IMAPBLKNO << JFS_SBI(ipimap->i_sb)->l2nbperpage,
258 jfs_err("diSync: get_metapage failed!");
262 /* copy the in-memory version to the on-disk version */
263 dinom_le = (struct dinomap *) mp->data;
264 dinom_le->in_freeiag = cpu_to_le32(imp->im_freeiag);
265 dinom_le->in_nextiag = cpu_to_le32(imp->im_nextiag);
266 dinom_le->in_numinos = cpu_to_le32(atomic_read(&imp->im_numinos));
267 dinom_le->in_numfree = cpu_to_le32(atomic_read(&imp->im_numfree));
268 dinom_le->in_nbperiext = cpu_to_le32(imp->im_nbperiext);
269 dinom_le->in_l2nbperiext = cpu_to_le32(imp->im_l2nbperiext);
270 for (index = 0; index < MAXAG; index++) {
271 dinom_le->in_agctl[index].inofree =
272 cpu_to_le32(imp->im_agctl[index].inofree);
273 dinom_le->in_agctl[index].extfree =
274 cpu_to_le32(imp->im_agctl[index].extfree);
275 dinom_le->in_agctl[index].numinos =
276 cpu_to_le32(imp->im_agctl[index].numinos);
277 dinom_le->in_agctl[index].numfree =
278 cpu_to_le32(imp->im_agctl[index].numfree);
281 /* write out the control structure */
285 * write out dirty pages of imap
287 filemap_fdatawrite(ipimap->i_mapping);
288 filemap_fdatawait(ipimap->i_mapping);
290 diWriteSpecial(ipimap, 0);
299 * FUNCTION: initialize an incore inode from disk.
301 * on entry, the specifed incore inode should itself
302 * specify the disk inode number corresponding to the
303 * incore inode (i.e. i_number should be initialized).
305 * this routine handles incore inode initialization for
306 * both "special" and "regular" inodes. special inodes
307 * are those required early in the mount process and
308 * require special handling since much of the file system
309 * is not yet initialized. these "special" inodes are
310 * identified by a NULL inode map inode pointer and are
311 * actually initialized by a call to diReadSpecial().
313 * for regular inodes, the iag describing the disk inode
314 * is read from disk to determine the inode extent address
315 * for the disk inode. with the inode extent address in
316 * hand, the page of the extent that contains the disk
317 * inode is read and the disk inode is copied to the
321 * ip - pointer to incore inode to be initialized from disk.
326 * -ENOMEM - insufficient memory
329 int diRead(struct inode *ip)
331 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
332 int iagno, ino, extno, rc;
333 struct inode *ipimap;
344 jfs_info("diRead: ino = %ld", ip->i_ino);
346 ipimap = sbi->ipimap;
347 JFS_IP(ip)->ipimap = ipimap;
349 /* determine the iag number for this inode (number) */
350 iagno = INOTOIAG(ip->i_ino);
353 imap = JFS_IP(ipimap)->i_imap;
355 rc = diIAGRead(imap, iagno, &mp);
356 IREAD_UNLOCK(ipimap);
358 jfs_err("diRead: diIAGRead returned %d", rc);
362 iagp = (struct iag *) mp->data;
364 /* determine inode extent that holds the disk inode */
365 ino = ip->i_ino & (INOSPERIAG - 1);
366 extno = ino >> L2INOSPEREXT;
368 if ((lengthPXD(&iagp->inoext[extno]) != imap->im_nbperiext) ||
369 (addressPXD(&iagp->inoext[extno]) == 0)) {
370 release_metapage(mp);
374 /* get disk block number of the page within the inode extent
375 * that holds the disk inode.
377 blkno = INOPBLK(&iagp->inoext[extno], ino, sbi->l2nbperpage);
379 /* get the ag for the iag */
380 agstart = le64_to_cpu(iagp->agstart);
382 release_metapage(mp);
384 rel_inode = (ino & (INOSPERPAGE - 1));
385 pageno = blkno >> sbi->l2nbperpage;
387 if ((block_offset = ((u32) blkno & (sbi->nbperpage - 1)))) {
389 * OS/2 didn't always align inode extents on page boundaries
392 (sbi->nbperpage - block_offset) << sbi->l2niperblk;
394 if (rel_inode < inodes_left)
395 rel_inode += block_offset << sbi->l2niperblk;
398 rel_inode -= inodes_left;
402 /* read the page of disk inode */
403 mp = read_metapage(ipimap, pageno << sbi->l2nbperpage, PSIZE, 1);
405 jfs_err("diRead: read_metapage failed");
409 /* locate the the disk inode requested */
410 dp = (struct dinode *) mp->data;
413 if (ip->i_ino != le32_to_cpu(dp->di_number)) {
414 jfs_error(ip->i_sb, "diRead: i_ino != di_number");
416 } else if (le32_to_cpu(dp->di_nlink) == 0)
419 /* copy the disk inode to the in-memory inode */
420 rc = copy_from_dinode(dp, ip);
422 release_metapage(mp);
424 /* set the ag for the inode */
425 JFS_IP(ip)->agno = BLKTOAG(agstart, sbi);
426 JFS_IP(ip)->active_ag = -1;
433 * NAME: diReadSpecial()
435 * FUNCTION: initialize a 'special' inode from disk.
437 * this routines handles aggregate level inodes. The
438 * inode cache cannot differentiate between the
439 * aggregate inodes and the filesystem inodes, so we
440 * handle these here. We don't actually use the aggregate
441 * inode map, since these inodes are at a fixed location
442 * and in some cases the aggregate inode map isn't initialized
446 * sb - filesystem superblock
447 * inum - aggregate inode number
448 * secondary - 1 if secondary aggregate inode table
451 * new inode - success
454 struct inode *diReadSpecial(struct super_block *sb, ino_t inum, int secondary)
456 struct jfs_sb_info *sbi = JFS_SBI(sb);
464 jfs_err("diReadSpecial: new_inode returned NULL!");
469 address = addressPXD(&sbi->ait2) >> sbi->l2nbperpage;
470 JFS_IP(ip)->ipimap = sbi->ipaimap2;
472 address = AITBL_OFF >> L2PSIZE;
473 JFS_IP(ip)->ipimap = sbi->ipaimap;
476 ASSERT(inum < INOSPEREXT);
480 address += inum >> 3; /* 8 inodes per 4K page */
482 /* read the page of fixed disk inode (AIT) in raw mode */
483 mp = read_metapage(ip, address << sbi->l2nbperpage, PSIZE, 1);
485 ip->i_nlink = 1; /* Don't want iput() deleting it */
490 /* get the pointer to the disk inode of interest */
491 dp = (struct dinode *) (mp->data);
492 dp += inum % 8; /* 8 inodes per 4K page */
494 /* copy on-disk inode to in-memory inode */
495 if ((copy_from_dinode(dp, ip)) != 0) {
496 /* handle bad return by returning NULL for ip */
497 ip->i_nlink = 1; /* Don't want iput() deleting it */
499 /* release the page */
500 release_metapage(mp);
505 ip->i_mapping->a_ops = &jfs_aops;
506 mapping_set_gfp_mask(ip->i_mapping, GFP_NOFS);
508 if ((inum == FILESYSTEM_I) && (JFS_IP(ip)->ipimap == sbi->ipaimap)) {
509 sbi->gengen = le32_to_cpu(dp->di_gengen);
510 sbi->inostamp = le32_to_cpu(dp->di_inostamp);
513 /* release the page */
514 release_metapage(mp);
520 * NAME: diWriteSpecial()
522 * FUNCTION: Write the special inode to disk
526 * secondary - 1 if secondary aggregate inode table
528 * RETURN VALUES: none
531 void diWriteSpecial(struct inode *ip, int secondary)
533 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
536 ino_t inum = ip->i_ino;
539 ip->i_state &= ~I_DIRTY;
542 address = addressPXD(&sbi->ait2) >> sbi->l2nbperpage;
544 address = AITBL_OFF >> L2PSIZE;
546 ASSERT(inum < INOSPEREXT);
548 address += inum >> 3; /* 8 inodes per 4K page */
550 /* read the page of fixed disk inode (AIT) in raw mode */
551 mp = read_metapage(ip, address << sbi->l2nbperpage, PSIZE, 1);
553 jfs_err("diWriteSpecial: failed to read aggregate inode "
558 /* get the pointer to the disk inode of interest */
559 dp = (struct dinode *) (mp->data);
560 dp += inum % 8; /* 8 inodes per 4K page */
562 /* copy on-disk inode to in-memory inode */
563 copy_to_dinode(dp, ip);
564 memcpy(&dp->di_xtroot, &JFS_IP(ip)->i_xtroot, 288);
566 if (inum == FILESYSTEM_I)
567 dp->di_gengen = cpu_to_le32(sbi->gengen);
574 * NAME: diFreeSpecial()
576 * FUNCTION: Free allocated space for special inode
578 void diFreeSpecial(struct inode *ip)
581 jfs_err("diFreeSpecial called with NULL ip!");
584 filemap_fdatawrite(ip->i_mapping);
585 filemap_fdatawait(ip->i_mapping);
586 truncate_inode_pages(ip->i_mapping, 0);
595 * FUNCTION: write the on-disk inode portion of the in-memory inode
596 * to its corresponding on-disk inode.
598 * on entry, the specifed incore inode should itself
599 * specify the disk inode number corresponding to the
600 * incore inode (i.e. i_number should be initialized).
602 * the inode contains the inode extent address for the disk
603 * inode. with the inode extent address in hand, the
604 * page of the extent that contains the disk inode is
605 * read and the disk inode portion of the incore inode
606 * is copied to the disk inode.
609 * tid - transacation id
610 * ip - pointer to incore inode to be written to the inode extent.
616 int diWrite(tid_t tid, struct inode *ip)
618 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
619 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
630 struct inode *ipimap;
633 struct tlock *ditlck, *tlck;
634 struct linelock *dilinelock, *ilinelock;
638 ipimap = jfs_ip->ipimap;
640 ino = ip->i_ino & (INOSPERIAG - 1);
642 if (!addressPXD(&(jfs_ip->ixpxd)) ||
643 (lengthPXD(&(jfs_ip->ixpxd)) !=
644 JFS_IP(ipimap)->i_imap->im_nbperiext)) {
645 jfs_error(ip->i_sb, "diWrite: ixpxd invalid");
650 * read the page of disk inode containing the specified inode:
652 /* compute the block address of the page */
653 blkno = INOPBLK(&(jfs_ip->ixpxd), ino, sbi->l2nbperpage);
655 rel_inode = (ino & (INOSPERPAGE - 1));
656 pageno = blkno >> sbi->l2nbperpage;
658 if ((block_offset = ((u32) blkno & (sbi->nbperpage - 1)))) {
660 * OS/2 didn't always align inode extents on page boundaries
663 (sbi->nbperpage - block_offset) << sbi->l2niperblk;
665 if (rel_inode < inodes_left)
666 rel_inode += block_offset << sbi->l2niperblk;
669 rel_inode -= inodes_left;
672 /* read the page of disk inode */
674 mp = read_metapage(ipimap, pageno << sbi->l2nbperpage, PSIZE, 1);
678 /* get the pointer to the disk inode */
679 dp = (struct dinode *) mp->data;
682 dioffset = (ino & (INOSPERPAGE - 1)) << L2DISIZE;
685 * acquire transaction lock on the on-disk inode;
686 * N.B. tlock is acquired on ipimap not ip;
689 txLock(tid, ipimap, mp, tlckINODE | tlckENTRY)) == NULL)
691 dilinelock = (struct linelock *) & ditlck->lock;
694 * copy btree root from in-memory inode to on-disk inode
696 * (tlock is taken from inline B+-tree root in in-memory
697 * inode when the B+-tree root is updated, which is pointed
698 * by jfs_ip->blid as well as being on tx tlock list)
700 * further processing of btree root is based on the copy
701 * in in-memory inode, where txLog() will log from, and,
702 * for xtree root, txUpdateMap() will update map and reset
706 if (S_ISDIR(ip->i_mode) && (lid = jfs_ip->xtlid)) {
708 * This is the special xtree inside the directory for storing
709 * the directory table
715 tlck = lid_to_tlock(lid);
716 assert(tlck->type & tlckXTREE);
717 tlck->type |= tlckBTROOT;
719 ilinelock = (struct linelock *) & tlck->lock;
722 * copy xtree root from inode to dinode:
724 p = &jfs_ip->i_xtroot;
725 xp = (xtpage_t *) &dp->di_dirtable;
727 for (n = 0; n < ilinelock->index; n++, lv++) {
728 memcpy(&xp->xad[lv->offset], &p->xad[lv->offset],
729 lv->length << L2XTSLOTSIZE);
732 /* reset on-disk (metadata page) xtree XAD_NEW bit */
733 xad = &xp->xad[XTENTRYSTART];
734 for (n = XTENTRYSTART;
735 n < le16_to_cpu(xp->header.nextindex); n++, xad++)
736 if (xad->flag & (XAD_NEW | XAD_EXTENDED))
737 xad->flag &= ~(XAD_NEW | XAD_EXTENDED);
740 if ((lid = jfs_ip->blid) == 0)
744 tlck = lid_to_tlock(lid);
746 tlck->type |= tlckBTROOT;
748 ilinelock = (struct linelock *) & tlck->lock;
751 * regular file: 16 byte (XAD slot) granularity
753 if (type & tlckXTREE) {
758 * copy xtree root from inode to dinode:
760 p = &jfs_ip->i_xtroot;
763 for (n = 0; n < ilinelock->index; n++, lv++) {
764 memcpy(&xp->xad[lv->offset], &p->xad[lv->offset],
765 lv->length << L2XTSLOTSIZE);
768 /* reset on-disk (metadata page) xtree XAD_NEW bit */
769 xad = &xp->xad[XTENTRYSTART];
770 for (n = XTENTRYSTART;
771 n < le16_to_cpu(xp->header.nextindex); n++, xad++)
772 if (xad->flag & (XAD_NEW | XAD_EXTENDED))
773 xad->flag &= ~(XAD_NEW | XAD_EXTENDED);
776 * directory: 32 byte (directory entry slot) granularity
778 else if (type & tlckDTREE) {
782 * copy dtree root from inode to dinode:
784 p = (dtpage_t *) &jfs_ip->i_dtroot;
785 xp = (dtpage_t *) & dp->di_dtroot;
787 for (n = 0; n < ilinelock->index; n++, lv++) {
788 memcpy(&xp->slot[lv->offset], &p->slot[lv->offset],
789 lv->length << L2DTSLOTSIZE);
792 jfs_err("diWrite: UFO tlock");
797 * copy inline symlink from in-memory inode to on-disk inode
799 if (S_ISLNK(ip->i_mode) && ip->i_size < IDATASIZE) {
800 lv = & dilinelock->lv[dilinelock->index];
801 lv->offset = (dioffset + 2 * 128) >> L2INODESLOTSIZE;
803 memcpy(&dp->di_fastsymlink, jfs_ip->i_inline, IDATASIZE);
807 * copy inline data from in-memory inode to on-disk inode:
808 * 128 byte slot granularity
810 if (test_cflag(COMMIT_Inlineea, ip)) {
811 lv = & dilinelock->lv[dilinelock->index];
812 lv->offset = (dioffset + 3 * 128) >> L2INODESLOTSIZE;
814 memcpy(&dp->di_inlineea, jfs_ip->i_inline_ea, INODESLOTSIZE);
817 clear_cflag(COMMIT_Inlineea, ip);
821 * lock/copy inode base: 128 byte slot granularity
824 lv = & dilinelock->lv[dilinelock->index];
825 lv->offset = dioffset >> L2INODESLOTSIZE;
826 copy_to_dinode(dp, ip);
827 if (test_and_clear_cflag(COMMIT_Dirtable, ip)) {
829 memcpy(&dp->di_dirtable, &jfs_ip->i_dirtable, 96);
836 * We aren't logging changes to the DASD used in directory inodes,
837 * but we need to write them to disk. If we don't unmount cleanly,
838 * mount will recalculate the DASD used.
840 if (S_ISDIR(ip->i_mode)
841 && (ip->i_ipmnt->i_mntflag & JFS_DASD_ENABLED))
842 memcpy(&dp->di_DASD, &ip->i_DASD, sizeof(struct dasd));
843 #endif /* _JFS_FASTDASD */
845 /* release the buffer holding the updated on-disk inode.
846 * the buffer will be later written by commit processing.
857 * FUNCTION: free a specified inode from the inode working map
858 * for a fileset or aggregate.
860 * if the inode to be freed represents the first (only)
861 * free inode within the iag, the iag will be placed on
862 * the ag free inode list.
864 * freeing the inode will cause the inode extent to be
865 * freed if the inode is the only allocated inode within
866 * the extent. in this case all the disk resource backing
867 * up the inode extent will be freed. in addition, the iag
868 * will be placed on the ag extent free list if the extent
869 * is the first free extent in the iag. if freeing the
870 * extent also means that no free inodes will exist for
871 * the iag, the iag will also be removed from the ag free
874 * the iag describing the inode will be freed if the extent
875 * is to be freed and it is the only backed extent within
876 * the iag. in this case, the iag will be removed from the
877 * ag free extent list and ag free inode list and placed on
878 * the inode map's free iag list.
880 * a careful update approach is used to provide consistency
881 * in the face of updates to multiple buffers. under this
882 * approach, all required buffers are obtained before making
883 * any updates and are held until all updates are complete.
886 * ip - inode to be freed.
892 int diFree(struct inode *ip)
895 ino_t inum = ip->i_ino;
896 struct iag *iagp, *aiagp, *biagp, *ciagp, *diagp;
897 struct metapage *mp, *amp, *bmp, *cmp, *dmp;
898 int iagno, ino, extno, bitno, sword, agno;
901 struct inode *ipimap = JFS_SBI(ip->i_sb)->ipimap;
902 struct inomap *imap = JFS_IP(ipimap)->i_imap;
905 struct inode *iplist[3];
907 struct pxd_lock *pxdlock;
910 * This is just to suppress compiler warnings. The same logic that
911 * references these variables is used to initialize them.
913 aiagp = biagp = ciagp = diagp = NULL;
915 /* get the iag number containing the inode.
917 iagno = INOTOIAG(inum);
919 /* make sure that the iag is contained within
922 if (iagno >= imap->im_nextiag) {
923 dump_mem("imap", imap, 32);
925 "diFree: inum = %d, iagno = %d, nextiag = %d",
926 (uint) inum, iagno, imap->im_nextiag);
930 /* get the allocation group for this ino.
932 agno = JFS_IP(ip)->agno;
934 /* Lock the AG specific inode map information
938 /* Obtain read lock in imap inode. Don't release it until we have
939 * read all of the IAG's that we are going to.
945 if ((rc = diIAGRead(imap, iagno, &mp))) {
946 IREAD_UNLOCK(ipimap);
947 AG_UNLOCK(imap, agno);
950 iagp = (struct iag *) mp->data;
952 /* get the inode number and extent number of the inode within
953 * the iag and the inode number within the extent.
955 ino = inum & (INOSPERIAG - 1);
956 extno = ino >> L2INOSPEREXT;
957 bitno = ino & (INOSPEREXT - 1);
958 mask = HIGHORDER >> bitno;
960 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
962 "diFree: wmap shows inode already free");
965 if (!addressPXD(&iagp->inoext[extno])) {
966 release_metapage(mp);
967 IREAD_UNLOCK(ipimap);
968 AG_UNLOCK(imap, agno);
969 jfs_error(ip->i_sb, "diFree: invalid inoext");
973 /* compute the bitmap for the extent reflecting the freed inode.
975 bitmap = le32_to_cpu(iagp->wmap[extno]) & ~mask;
977 if (imap->im_agctl[agno].numfree > imap->im_agctl[agno].numinos) {
978 release_metapage(mp);
979 IREAD_UNLOCK(ipimap);
980 AG_UNLOCK(imap, agno);
981 jfs_error(ip->i_sb, "diFree: numfree > numinos");
985 * inode extent still has some inodes or below low water mark:
986 * keep the inode extent;
989 imap->im_agctl[agno].numfree < 96 ||
990 (imap->im_agctl[agno].numfree < 288 &&
991 (((imap->im_agctl[agno].numfree * 100) /
992 imap->im_agctl[agno].numinos) <= 25))) {
993 /* if the iag currently has no free inodes (i.e.,
994 * the inode being freed is the first free inode of iag),
995 * insert the iag at head of the inode free list for the ag.
997 if (iagp->nfreeinos == 0) {
998 /* check if there are any iags on the ag inode
999 * free list. if so, read the first one so that
1000 * we can link the current iag onto the list at
1003 if ((fwd = imap->im_agctl[agno].inofree) >= 0) {
1004 /* read the iag that currently is the head
1007 if ((rc = diIAGRead(imap, fwd, &))) {
1008 IREAD_UNLOCK(ipimap);
1009 AG_UNLOCK(imap, agno);
1010 release_metapage(mp);
1013 aiagp = (struct iag *) amp->data;
1015 /* make current head point back to the iag.
1017 aiagp->inofreeback = cpu_to_le32(iagno);
1019 write_metapage(amp);
1022 /* iag points forward to current head and iag
1023 * becomes the new head of the list.
1026 cpu_to_le32(imap->im_agctl[agno].inofree);
1027 iagp->inofreeback = -1;
1028 imap->im_agctl[agno].inofree = iagno;
1030 IREAD_UNLOCK(ipimap);
1032 /* update the free inode summary map for the extent if
1033 * freeing the inode means the extent will now have free
1034 * inodes (i.e., the inode being freed is the first free
1037 if (iagp->wmap[extno] == ONES) {
1038 sword = extno >> L2EXTSPERSUM;
1039 bitno = extno & (EXTSPERSUM - 1);
1040 iagp->inosmap[sword] &=
1041 cpu_to_le32(~(HIGHORDER >> bitno));
1044 /* update the bitmap.
1046 iagp->wmap[extno] = cpu_to_le32(bitmap);
1047 DBG_DIFREE(imap, inum);
1049 /* update the free inode counts at the iag, ag and
1053 cpu_to_le32(le32_to_cpu(iagp->nfreeinos) + 1);
1054 imap->im_agctl[agno].numfree += 1;
1055 atomic_inc(&imap->im_numfree);
1057 /* release the AG inode map lock
1059 AG_UNLOCK(imap, agno);
1069 * inode extent has become free and above low water mark:
1070 * free the inode extent;
1074 * prepare to update iag list(s) (careful update step 1)
1076 amp = bmp = cmp = dmp = NULL;
1079 /* check if the iag currently has no free extents. if so,
1080 * it will be placed on the head of the ag extent free list.
1082 if (iagp->nfreeexts == 0) {
1083 /* check if the ag extent free list has any iags.
1084 * if so, read the iag at the head of the list now.
1085 * this (head) iag will be updated later to reflect
1086 * the addition of the current iag at the head of
1089 if ((fwd = imap->im_agctl[agno].extfree) >= 0) {
1090 if ((rc = diIAGRead(imap, fwd, &)))
1092 aiagp = (struct iag *) amp->data;
1095 /* iag has free extents. check if the addition of a free
1096 * extent will cause all extents to be free within this
1097 * iag. if so, the iag will be removed from the ag extent
1098 * free list and placed on the inode map's free iag list.
1100 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG - 1)) {
1101 /* in preparation for removing the iag from the
1102 * ag extent free list, read the iags preceeding
1103 * and following the iag on the ag extent free
1106 if ((fwd = le32_to_cpu(iagp->extfreefwd)) >= 0) {
1107 if ((rc = diIAGRead(imap, fwd, &)))
1109 aiagp = (struct iag *) amp->data;
1112 if ((back = le32_to_cpu(iagp->extfreeback)) >= 0) {
1113 if ((rc = diIAGRead(imap, back, &bmp)))
1115 biagp = (struct iag *) bmp->data;
1120 /* remove the iag from the ag inode free list if freeing
1121 * this extent cause the iag to have no free inodes.
1123 if (iagp->nfreeinos == cpu_to_le32(INOSPEREXT - 1)) {
1124 int inofreeback = le32_to_cpu(iagp->inofreeback);
1125 int inofreefwd = le32_to_cpu(iagp->inofreefwd);
1127 /* in preparation for removing the iag from the
1128 * ag inode free list, read the iags preceeding
1129 * and following the iag on the ag inode free
1130 * list. before reading these iags, we must make
1131 * sure that we already don't have them in hand
1132 * from up above, since re-reading an iag (buffer)
1133 * we are currently holding would cause a deadlock.
1135 if (inofreefwd >= 0) {
1137 if (inofreefwd == fwd)
1138 ciagp = (struct iag *) amp->data;
1139 else if (inofreefwd == back)
1140 ciagp = (struct iag *) bmp->data;
1143 diIAGRead(imap, inofreefwd, &cmp)))
1145 ciagp = (struct iag *) cmp->data;
1147 assert(ciagp != NULL);
1150 if (inofreeback >= 0) {
1151 if (inofreeback == fwd)
1152 diagp = (struct iag *) amp->data;
1153 else if (inofreeback == back)
1154 diagp = (struct iag *) bmp->data;
1157 diIAGRead(imap, inofreeback, &dmp)))
1159 diagp = (struct iag *) dmp->data;
1161 assert(diagp != NULL);
1165 IREAD_UNLOCK(ipimap);
1168 * invalidate any page of the inode extent freed from buffer cache;
1170 freepxd = iagp->inoext[extno];
1171 invalidate_pxd_metapages(ip, freepxd);
1174 * update iag list(s) (careful update step 2)
1176 /* add the iag to the ag extent free list if this is the
1177 * first free extent for the iag.
1179 if (iagp->nfreeexts == 0) {
1181 aiagp->extfreeback = cpu_to_le32(iagno);
1184 cpu_to_le32(imap->im_agctl[agno].extfree);
1185 iagp->extfreeback = -1;
1186 imap->im_agctl[agno].extfree = iagno;
1188 /* remove the iag from the ag extent list if all extents
1189 * are now free and place it on the inode map iag free list.
1191 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG - 1)) {
1193 aiagp->extfreeback = iagp->extfreeback;
1196 biagp->extfreefwd = iagp->extfreefwd;
1198 imap->im_agctl[agno].extfree =
1199 le32_to_cpu(iagp->extfreefwd);
1201 iagp->extfreefwd = iagp->extfreeback = -1;
1204 iagp->iagfree = cpu_to_le32(imap->im_freeiag);
1205 imap->im_freeiag = iagno;
1206 IAGFREE_UNLOCK(imap);
1210 /* remove the iag from the ag inode free list if freeing
1211 * this extent causes the iag to have no free inodes.
1213 if (iagp->nfreeinos == cpu_to_le32(INOSPEREXT - 1)) {
1214 if ((int) le32_to_cpu(iagp->inofreefwd) >= 0)
1215 ciagp->inofreeback = iagp->inofreeback;
1217 if ((int) le32_to_cpu(iagp->inofreeback) >= 0)
1218 diagp->inofreefwd = iagp->inofreefwd;
1220 imap->im_agctl[agno].inofree =
1221 le32_to_cpu(iagp->inofreefwd);
1223 iagp->inofreefwd = iagp->inofreeback = -1;
1226 /* update the inode extent address and working map
1227 * to reflect the free extent.
1228 * the permanent map should have been updated already
1229 * for the inode being freed.
1231 if (iagp->pmap[extno] != 0) {
1232 jfs_error(ip->i_sb, "diFree: the pmap does not show inode free");
1234 iagp->wmap[extno] = 0;
1235 DBG_DIFREE(imap, inum);
1236 PXDlength(&iagp->inoext[extno], 0);
1237 PXDaddress(&iagp->inoext[extno], 0);
1239 /* update the free extent and free inode summary maps
1240 * to reflect the freed extent.
1241 * the inode summary map is marked to indicate no inodes
1242 * available for the freed extent.
1244 sword = extno >> L2EXTSPERSUM;
1245 bitno = extno & (EXTSPERSUM - 1);
1246 mask = HIGHORDER >> bitno;
1247 iagp->inosmap[sword] |= cpu_to_le32(mask);
1248 iagp->extsmap[sword] &= cpu_to_le32(~mask);
1250 /* update the number of free inodes and number of free extents
1253 iagp->nfreeinos = cpu_to_le32(le32_to_cpu(iagp->nfreeinos) -
1255 iagp->nfreeexts = cpu_to_le32(le32_to_cpu(iagp->nfreeexts) + 1);
1257 /* update the number of free inodes and backed inodes
1258 * at the ag and inode map level.
1260 imap->im_agctl[agno].numfree -= (INOSPEREXT - 1);
1261 imap->im_agctl[agno].numinos -= INOSPEREXT;
1262 atomic_sub(INOSPEREXT - 1, &imap->im_numfree);
1263 atomic_sub(INOSPEREXT, &imap->im_numinos);
1266 write_metapage(amp);
1268 write_metapage(bmp);
1270 write_metapage(cmp);
1272 write_metapage(dmp);
1275 * start transaction to update block allocation map
1276 * for the inode extent freed;
1278 * N.B. AG_LOCK is released and iag will be released below, and
1279 * other thread may allocate inode from/reusing the ixad freed
1280 * BUT with new/different backing inode extent from the extent
1281 * to be freed by the transaction;
1283 tid = txBegin(ipimap->i_sb, COMMIT_FORCE);
1284 down(&JFS_IP(ipimap)->commit_sem);
1286 /* acquire tlock of the iag page of the freed ixad
1287 * to force the page NOHOMEOK (even though no data is
1288 * logged from the iag page) until NOREDOPAGE|FREEXTENT log
1289 * for the free of the extent is committed;
1290 * write FREEXTENT|NOREDOPAGE log record
1291 * N.B. linelock is overlaid as freed extent descriptor;
1293 tlck = txLock(tid, ipimap, mp, tlckINODE | tlckFREE);
1294 pxdlock = (struct pxd_lock *) & tlck->lock;
1295 pxdlock->flag = mlckFREEPXD;
1296 pxdlock->pxd = freepxd;
1304 * logredo needs the IAG number and IAG extent index in order
1305 * to ensure that the IMap is consistent. The least disruptive
1306 * way to pass these values through to the transaction manager
1307 * is in the iplist array.
1309 * It's not pretty, but it works.
1311 iplist[1] = (struct inode *) (size_t)iagno;
1312 iplist[2] = (struct inode *) (size_t)extno;
1314 rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
1317 up(&JFS_IP(ipimap)->commit_sem);
1319 /* unlock the AG inode map information */
1320 AG_UNLOCK(imap, agno);
1325 IREAD_UNLOCK(ipimap);
1328 release_metapage(amp);
1330 release_metapage(bmp);
1332 release_metapage(cmp);
1334 release_metapage(dmp);
1336 AG_UNLOCK(imap, agno);
1338 release_metapage(mp);
1344 * There are several places in the diAlloc* routines where we initialize
1348 diInitInode(struct inode *ip, int iagno, int ino, int extno, struct iag * iagp)
1350 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
1351 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
1353 ip->i_ino = (iagno << L2INOSPERIAG) + ino;
1354 DBG_DIALLOC(JFS_IP(ipimap)->i_imap, ip->i_ino);
1355 jfs_ip->ixpxd = iagp->inoext[extno];
1356 jfs_ip->agno = BLKTOAG(le64_to_cpu(iagp->agstart), sbi);
1357 jfs_ip->active_ag = -1;
1362 * NAME: diAlloc(pip,dir,ip)
1364 * FUNCTION: allocate a disk inode from the inode working map
1365 * for a fileset or aggregate.
1368 * pip - pointer to incore inode for the parent inode.
1369 * dir - TRUE if the new disk inode is for a directory.
1370 * ip - pointer to a new inode
1374 * -ENOSPC - insufficient disk resources.
1377 int diAlloc(struct inode *pip, boolean_t dir, struct inode *ip)
1379 int rc, ino, iagno, addext, extno, bitno, sword;
1380 int nwords, rem, i, agno;
1381 u32 mask, inosmap, extsmap;
1382 struct inode *ipimap;
1383 struct metapage *mp;
1386 struct inomap *imap;
1388 /* get the pointers to the inode map inode and the
1389 * corresponding imap control structure.
1391 ipimap = JFS_SBI(pip->i_sb)->ipimap;
1392 imap = JFS_IP(ipimap)->i_imap;
1393 JFS_IP(ip)->ipimap = ipimap;
1394 JFS_IP(ip)->fileset = FILESYSTEM_I;
1396 /* for a directory, the allocation policy is to start
1397 * at the ag level using the preferred ag.
1400 agno = dbNextAG(JFS_SBI(pip->i_sb)->ipbmap);
1401 AG_LOCK(imap, agno);
1405 /* for files, the policy starts off by trying to allocate from
1406 * the same iag containing the parent disk inode:
1407 * try to allocate the new disk inode close to the parent disk
1408 * inode, using parent disk inode number + 1 as the allocation
1409 * hint. (we use a left-to-right policy to attempt to avoid
1410 * moving backward on the disk.) compute the hint within the
1411 * file system and the iag.
1414 /* get the ag number of this iag */
1415 agno = JFS_IP(pip)->agno;
1417 if (atomic_read(&JFS_SBI(pip->i_sb)->bmap->db_active[agno])) {
1419 * There is an open file actively growing. We want to
1420 * allocate new inodes from a different ag to avoid
1421 * fragmentation problems.
1423 agno = dbNextAG(JFS_SBI(pip->i_sb)->ipbmap);
1424 AG_LOCK(imap, agno);
1428 inum = pip->i_ino + 1;
1429 ino = inum & (INOSPERIAG - 1);
1431 /* back off the the hint if it is outside of the iag */
1435 /* lock the AG inode map information */
1436 AG_LOCK(imap, agno);
1438 /* Get read lock on imap inode */
1441 /* get the iag number and read the iag */
1442 iagno = INOTOIAG(inum);
1443 if ((rc = diIAGRead(imap, iagno, &mp))) {
1444 IREAD_UNLOCK(ipimap);
1445 AG_UNLOCK(imap, agno);
1448 iagp = (struct iag *) mp->data;
1450 /* determine if new inode extent is allowed to be added to the iag.
1451 * new inode extent can be added to the iag if the ag
1452 * has less than 32 free disk inodes and the iag has free extents.
1454 addext = (imap->im_agctl[agno].numfree < 32 && iagp->nfreeexts);
1457 * try to allocate from the IAG
1459 /* check if the inode may be allocated from the iag
1460 * (i.e. the inode has free inodes or new extent can be added).
1462 if (iagp->nfreeinos || addext) {
1463 /* determine the extent number of the hint.
1465 extno = ino >> L2INOSPEREXT;
1467 /* check if the extent containing the hint has backed
1468 * inodes. if so, try to allocate within this extent.
1470 if (addressPXD(&iagp->inoext[extno])) {
1471 bitno = ino & (INOSPEREXT - 1);
1473 diFindFree(le32_to_cpu(iagp->wmap[extno]),
1476 ino = (extno << L2INOSPEREXT) + bitno;
1478 /* a free inode (bit) was found within this
1479 * extent, so allocate it.
1481 rc = diAllocBit(imap, iagp, ino);
1482 IREAD_UNLOCK(ipimap);
1486 /* set the results of the allocation
1487 * and write the iag.
1489 diInitInode(ip, iagno, ino, extno,
1491 mark_metapage_dirty(mp);
1493 release_metapage(mp);
1495 /* free the AG lock and return.
1497 AG_UNLOCK(imap, agno);
1504 EXTSPERIAG - 1) ? 0 : extno + 1;
1508 * no free inodes within the extent containing the hint.
1510 * try to allocate from the backed extents following
1511 * hint or, if appropriate (i.e. addext is true), allocate
1512 * an extent of free inodes at or following the extent
1513 * containing the hint.
1515 * the free inode and free extent summary maps are used
1516 * here, so determine the starting summary map position
1517 * and the number of words we'll have to examine. again,
1518 * the approach is to allocate following the hint, so we
1519 * might have to initially ignore prior bits of the summary
1520 * map that represent extents prior to the extent containing
1521 * the hint and later revisit these bits.
1523 bitno = extno & (EXTSPERSUM - 1);
1524 nwords = (bitno == 0) ? SMAPSZ : SMAPSZ + 1;
1525 sword = extno >> L2EXTSPERSUM;
1527 /* mask any prior bits for the starting words of the
1530 mask = ONES << (EXTSPERSUM - bitno);
1531 inosmap = le32_to_cpu(iagp->inosmap[sword]) | mask;
1532 extsmap = le32_to_cpu(iagp->extsmap[sword]) | mask;
1534 /* scan the free inode and free extent summary maps for
1537 for (i = 0; i < nwords; i++) {
1538 /* check if this word of the free inode summary
1539 * map describes an extent with free inodes.
1542 /* an extent with free inodes has been
1543 * found. determine the extent number
1544 * and the inode number within the extent.
1546 rem = diFindFree(inosmap, 0);
1547 extno = (sword << L2EXTSPERSUM) + rem;
1548 rem = diFindFree(le32_to_cpu(iagp->wmap[extno]),
1550 if (rem >= INOSPEREXT) {
1551 IREAD_UNLOCK(ipimap);
1552 release_metapage(mp);
1553 AG_UNLOCK(imap, agno);
1555 "diAlloc: can't find free bit "
1560 /* determine the inode number within the
1561 * iag and allocate the inode from the
1564 ino = (extno << L2INOSPEREXT) + rem;
1565 rc = diAllocBit(imap, iagp, ino);
1566 IREAD_UNLOCK(ipimap);
1570 /* set the results of the allocation
1571 * and write the iag.
1573 diInitInode(ip, iagno, ino, extno,
1575 mark_metapage_dirty(mp);
1577 release_metapage(mp);
1579 /* free the AG lock and return.
1581 AG_UNLOCK(imap, agno);
1586 /* check if we may allocate an extent of free
1587 * inodes and whether this word of the free
1588 * extents summary map describes a free extent.
1590 if (addext && ~extsmap) {
1591 /* a free extent has been found. determine
1592 * the extent number.
1594 rem = diFindFree(extsmap, 0);
1595 extno = (sword << L2EXTSPERSUM) + rem;
1597 /* allocate an extent of free inodes.
1599 if ((rc = diNewExt(imap, iagp, extno))) {
1600 /* if there is no disk space for a
1601 * new extent, try to allocate the
1602 * disk inode from somewhere else.
1609 /* set the results of the allocation
1610 * and write the iag.
1612 diInitInode(ip, iagno,
1613 extno << L2INOSPEREXT,
1615 mark_metapage_dirty(mp);
1617 release_metapage(mp);
1618 /* free the imap inode & the AG lock & return.
1620 IREAD_UNLOCK(ipimap);
1621 AG_UNLOCK(imap, agno);
1625 /* move on to the next set of summary map words.
1627 sword = (sword == SMAPSZ - 1) ? 0 : sword + 1;
1628 inosmap = le32_to_cpu(iagp->inosmap[sword]);
1629 extsmap = le32_to_cpu(iagp->extsmap[sword]);
1632 /* unlock imap inode */
1633 IREAD_UNLOCK(ipimap);
1635 /* nothing doing in this iag, so release it. */
1636 release_metapage(mp);
1640 * try to allocate anywhere within the same AG as the parent inode.
1642 rc = diAllocAG(imap, agno, dir, ip);
1644 AG_UNLOCK(imap, agno);
1650 * try to allocate in any AG.
1652 return (diAllocAny(imap, agno, dir, ip));
1657 * NAME: diAllocAG(imap,agno,dir,ip)
1659 * FUNCTION: allocate a disk inode from the allocation group.
1661 * this routine first determines if a new extent of free
1662 * inodes should be added for the allocation group, with
1663 * the current request satisfied from this extent. if this
1664 * is the case, an attempt will be made to do just that. if
1665 * this attempt fails or it has been determined that a new
1666 * extent should not be added, an attempt is made to satisfy
1667 * the request by allocating an existing (backed) free inode
1668 * from the allocation group.
1670 * PRE CONDITION: Already have the AG lock for this AG.
1673 * imap - pointer to inode map control structure.
1674 * agno - allocation group to allocate from.
1675 * dir - TRUE if the new disk inode is for a directory.
1676 * ip - pointer to the new inode to be filled in on successful return
1677 * with the disk inode number allocated, its extent address
1678 * and the start of the ag.
1682 * -ENOSPC - insufficient disk resources.
1686 diAllocAG(struct inomap * imap, int agno, boolean_t dir, struct inode *ip)
1688 int rc, addext, numfree, numinos;
1690 /* get the number of free and the number of backed disk
1691 * inodes currently within the ag.
1693 numfree = imap->im_agctl[agno].numfree;
1694 numinos = imap->im_agctl[agno].numinos;
1696 if (numfree > numinos) {
1697 jfs_error(ip->i_sb, "diAllocAG: numfree > numinos");
1701 /* determine if we should allocate a new extent of free inodes
1702 * within the ag: for directory inodes, add a new extent
1703 * if there are a small number of free inodes or number of free
1704 * inodes is a small percentage of the number of backed inodes.
1707 addext = (numfree < 64 ||
1709 && ((numfree * 100) / numinos) <= 20));
1711 addext = (numfree == 0);
1714 * try to allocate a new extent of free inodes.
1717 /* if free space is not avaliable for this new extent, try
1718 * below to allocate a free and existing (already backed)
1719 * inode from the ag.
1721 if ((rc = diAllocExt(imap, agno, ip)) != -ENOSPC)
1726 * try to allocate an existing free inode from the ag.
1728 return (diAllocIno(imap, agno, ip));
1733 * NAME: diAllocAny(imap,agno,dir,iap)
1735 * FUNCTION: allocate a disk inode from any other allocation group.
1737 * this routine is called when an allocation attempt within
1738 * the primary allocation group has failed. if attempts to
1739 * allocate an inode from any allocation group other than the
1740 * specified primary group.
1743 * imap - pointer to inode map control structure.
1744 * agno - primary allocation group (to avoid).
1745 * dir - TRUE if the new disk inode is for a directory.
1746 * ip - pointer to a new inode to be filled in on successful return
1747 * with the disk inode number allocated, its extent address
1748 * and the start of the ag.
1752 * -ENOSPC - insufficient disk resources.
1756 diAllocAny(struct inomap * imap, int agno, boolean_t dir, struct inode *ip)
1759 int maxag = JFS_SBI(imap->im_ipimap->i_sb)->bmap->db_maxag;
1762 /* try to allocate from the ags following agno up to
1763 * the maximum ag number.
1765 for (ag = agno + 1; ag <= maxag; ag++) {
1768 rc = diAllocAG(imap, ag, dir, ip);
1770 AG_UNLOCK(imap, ag);
1776 /* try to allocate from the ags in front of agno.
1778 for (ag = 0; ag < agno; ag++) {
1781 rc = diAllocAG(imap, ag, dir, ip);
1783 AG_UNLOCK(imap, ag);
1789 /* no free disk inodes.
1796 * NAME: diAllocIno(imap,agno,ip)
1798 * FUNCTION: allocate a disk inode from the allocation group's free
1799 * inode list, returning an error if this free list is
1800 * empty (i.e. no iags on the list).
1802 * allocation occurs from the first iag on the list using
1803 * the iag's free inode summary map to find the leftmost
1804 * free inode in the iag.
1806 * PRE CONDITION: Already have AG lock for this AG.
1809 * imap - pointer to inode map control structure.
1810 * agno - allocation group.
1811 * ip - pointer to new inode to be filled in on successful return
1812 * with the disk inode number allocated, its extent address
1813 * and the start of the ag.
1817 * -ENOSPC - insufficient disk resources.
1820 static int diAllocIno(struct inomap * imap, int agno, struct inode *ip)
1822 int iagno, ino, rc, rem, extno, sword;
1823 struct metapage *mp;
1826 /* check if there are iags on the ag's free inode list.
1828 if ((iagno = imap->im_agctl[agno].inofree) < 0)
1831 /* obtain read lock on imap inode */
1832 IREAD_LOCK(imap->im_ipimap);
1834 /* read the iag at the head of the list.
1836 if ((rc = diIAGRead(imap, iagno, &mp))) {
1837 IREAD_UNLOCK(imap->im_ipimap);
1840 iagp = (struct iag *) mp->data;
1842 /* better be free inodes in this iag if it is on the
1845 if (!iagp->nfreeinos) {
1846 IREAD_UNLOCK(imap->im_ipimap);
1847 release_metapage(mp);
1849 "diAllocIno: nfreeinos = 0, but iag on freelist");
1853 /* scan the free inode summary map to find an extent
1856 for (sword = 0;; sword++) {
1857 if (sword >= SMAPSZ) {
1858 IREAD_UNLOCK(imap->im_ipimap);
1859 release_metapage(mp);
1861 "diAllocIno: free inode not found in summary map");
1865 if (~iagp->inosmap[sword])
1869 /* found a extent with free inodes. determine
1870 * the extent number.
1872 rem = diFindFree(le32_to_cpu(iagp->inosmap[sword]), 0);
1873 if (rem >= EXTSPERSUM) {
1874 IREAD_UNLOCK(imap->im_ipimap);
1875 release_metapage(mp);
1876 jfs_error(ip->i_sb, "diAllocIno: no free extent found");
1879 extno = (sword << L2EXTSPERSUM) + rem;
1881 /* find the first free inode in the extent.
1883 rem = diFindFree(le32_to_cpu(iagp->wmap[extno]), 0);
1884 if (rem >= INOSPEREXT) {
1885 IREAD_UNLOCK(imap->im_ipimap);
1886 release_metapage(mp);
1887 jfs_error(ip->i_sb, "diAllocIno: free inode not found");
1891 /* compute the inode number within the iag.
1893 ino = (extno << L2INOSPEREXT) + rem;
1895 /* allocate the inode.
1897 rc = diAllocBit(imap, iagp, ino);
1898 IREAD_UNLOCK(imap->im_ipimap);
1900 release_metapage(mp);
1904 /* set the results of the allocation and write the iag.
1906 diInitInode(ip, iagno, ino, extno, iagp);
1914 * NAME: diAllocExt(imap,agno,ip)
1916 * FUNCTION: add a new extent of free inodes to an iag, allocating
1917 * an inode from this extent to satisfy the current allocation
1920 * this routine first tries to find an existing iag with free
1921 * extents through the ag free extent list. if list is not
1922 * empty, the head of the list will be selected as the home
1923 * of the new extent of free inodes. otherwise (the list is
1924 * empty), a new iag will be allocated for the ag to contain
1927 * once an iag has been selected, the free extent summary map
1928 * is used to locate a free extent within the iag and diNewExt()
1929 * is called to initialize the extent, with initialization
1930 * including the allocation of the first inode of the extent
1931 * for the purpose of satisfying this request.
1934 * imap - pointer to inode map control structure.
1935 * agno - allocation group number.
1936 * ip - pointer to new inode to be filled in on successful return
1937 * with the disk inode number allocated, its extent address
1938 * and the start of the ag.
1942 * -ENOSPC - insufficient disk resources.
1945 static int diAllocExt(struct inomap * imap, int agno, struct inode *ip)
1947 int rem, iagno, sword, extno, rc;
1948 struct metapage *mp;
1951 /* check if the ag has any iags with free extents. if not,
1952 * allocate a new iag for the ag.
1954 if ((iagno = imap->im_agctl[agno].extfree) < 0) {
1955 /* If successful, diNewIAG will obtain the read lock on the
1958 if ((rc = diNewIAG(imap, &iagno, agno, &mp))) {
1961 iagp = (struct iag *) mp->data;
1963 /* set the ag number if this a brand new iag
1966 cpu_to_le64(AGTOBLK(agno, imap->im_ipimap));
1970 IREAD_LOCK(imap->im_ipimap);
1971 if ((rc = diIAGRead(imap, iagno, &mp))) {
1972 IREAD_UNLOCK(imap->im_ipimap);
1973 jfs_error(ip->i_sb, "diAllocExt: error reading iag");
1976 iagp = (struct iag *) mp->data;
1979 /* using the free extent summary map, find a free extent.
1981 for (sword = 0;; sword++) {
1982 if (sword >= SMAPSZ) {
1983 release_metapage(mp);
1984 IREAD_UNLOCK(imap->im_ipimap);
1986 "diAllocExt: free ext summary map not found");
1989 if (~iagp->extsmap[sword])
1993 /* determine the extent number of the free extent.
1995 rem = diFindFree(le32_to_cpu(iagp->extsmap[sword]), 0);
1996 if (rem >= EXTSPERSUM) {
1997 release_metapage(mp);
1998 IREAD_UNLOCK(imap->im_ipimap);
1999 jfs_error(ip->i_sb, "diAllocExt: free extent not found");
2002 extno = (sword << L2EXTSPERSUM) + rem;
2004 /* initialize the new extent.
2006 rc = diNewExt(imap, iagp, extno);
2007 IREAD_UNLOCK(imap->im_ipimap);
2009 /* something bad happened. if a new iag was allocated,
2010 * place it back on the inode map's iag free list, and
2011 * clear the ag number information.
2013 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2015 iagp->iagfree = cpu_to_le32(imap->im_freeiag);
2016 imap->im_freeiag = iagno;
2017 IAGFREE_UNLOCK(imap);
2023 /* set the results of the allocation and write the iag.
2025 diInitInode(ip, iagno, extno << L2INOSPEREXT, extno, iagp);
2034 * NAME: diAllocBit(imap,iagp,ino)
2036 * FUNCTION: allocate a backed inode from an iag.
2038 * this routine performs the mechanics of allocating a
2039 * specified inode from a backed extent.
2041 * if the inode to be allocated represents the last free
2042 * inode within the iag, the iag will be removed from the
2043 * ag free inode list.
2045 * a careful update approach is used to provide consistency
2046 * in the face of updates to multiple buffers. under this
2047 * approach, all required buffers are obtained before making
2048 * any updates and are held all are updates are complete.
2050 * PRE CONDITION: Already have buffer lock on iagp. Already have AG lock on
2051 * this AG. Must have read lock on imap inode.
2054 * imap - pointer to inode map control structure.
2055 * iagp - pointer to iag.
2056 * ino - inode number to be allocated within the iag.
2060 * -ENOSPC - insufficient disk resources.
2063 static int diAllocBit(struct inomap * imap, struct iag * iagp, int ino)
2065 int extno, bitno, agno, sword, rc;
2066 struct metapage *amp = NULL, *bmp = NULL;
2067 struct iag *aiagp = NULL, *biagp = NULL;
2070 /* check if this is the last free inode within the iag.
2071 * if so, it will have to be removed from the ag free
2072 * inode list, so get the iags preceeding and following
2075 if (iagp->nfreeinos == cpu_to_le32(1)) {
2076 if ((int) le32_to_cpu(iagp->inofreefwd) >= 0) {
2078 diIAGRead(imap, le32_to_cpu(iagp->inofreefwd),
2081 aiagp = (struct iag *) amp->data;
2084 if ((int) le32_to_cpu(iagp->inofreeback) >= 0) {
2087 le32_to_cpu(iagp->inofreeback),
2090 release_metapage(amp);
2093 biagp = (struct iag *) bmp->data;
2097 /* get the ag number, extent number, inode number within
2100 agno = BLKTOAG(le64_to_cpu(iagp->agstart), JFS_SBI(imap->im_ipimap->i_sb));
2101 extno = ino >> L2INOSPEREXT;
2102 bitno = ino & (INOSPEREXT - 1);
2104 /* compute the mask for setting the map.
2106 mask = HIGHORDER >> bitno;
2108 /* the inode should be free and backed.
2110 if (((le32_to_cpu(iagp->pmap[extno]) & mask) != 0) ||
2111 ((le32_to_cpu(iagp->wmap[extno]) & mask) != 0) ||
2112 (addressPXD(&iagp->inoext[extno]) == 0)) {
2114 release_metapage(amp);
2116 release_metapage(bmp);
2118 jfs_error(imap->im_ipimap->i_sb,
2119 "diAllocBit: iag inconsistent");
2123 /* mark the inode as allocated in the working map.
2125 iagp->wmap[extno] |= cpu_to_le32(mask);
2127 /* check if all inodes within the extent are now
2128 * allocated. if so, update the free inode summary
2129 * map to reflect this.
2131 if (iagp->wmap[extno] == ONES) {
2132 sword = extno >> L2EXTSPERSUM;
2133 bitno = extno & (EXTSPERSUM - 1);
2134 iagp->inosmap[sword] |= cpu_to_le32(HIGHORDER >> bitno);
2137 /* if this was the last free inode in the iag, remove the
2138 * iag from the ag free inode list.
2140 if (iagp->nfreeinos == cpu_to_le32(1)) {
2142 aiagp->inofreeback = iagp->inofreeback;
2143 write_metapage(amp);
2147 biagp->inofreefwd = iagp->inofreefwd;
2148 write_metapage(bmp);
2150 imap->im_agctl[agno].inofree =
2151 le32_to_cpu(iagp->inofreefwd);
2153 iagp->inofreefwd = iagp->inofreeback = -1;
2156 /* update the free inode count at the iag, ag, inode
2159 iagp->nfreeinos = cpu_to_le32(le32_to_cpu(iagp->nfreeinos) - 1);
2160 imap->im_agctl[agno].numfree -= 1;
2161 atomic_dec(&imap->im_numfree);
2168 * NAME: diNewExt(imap,iagp,extno)
2170 * FUNCTION: initialize a new extent of inodes for an iag, allocating
2171 * the first inode of the extent for use for the current
2172 * allocation request.
2174 * disk resources are allocated for the new extent of inodes
2175 * and the inodes themselves are initialized to reflect their
2176 * existence within the extent (i.e. their inode numbers and
2177 * inode extent addresses are set) and their initial state
2178 * (mode and link count are set to zero).
2180 * if the iag is new, it is not yet on an ag extent free list
2181 * but will now be placed on this list.
2183 * if the allocation of the new extent causes the iag to
2184 * have no free extent, the iag will be removed from the
2185 * ag extent free list.
2187 * if the iag has no free backed inodes, it will be placed
2188 * on the ag free inode list, since the addition of the new
2189 * extent will now cause it to have free inodes.
2191 * a careful update approach is used to provide consistency
2192 * (i.e. list consistency) in the face of updates to multiple
2193 * buffers. under this approach, all required buffers are
2194 * obtained before making any updates and are held until all
2195 * updates are complete.
2197 * PRE CONDITION: Already have buffer lock on iagp. Already have AG lock on
2198 * this AG. Must have read lock on imap inode.
2201 * imap - pointer to inode map control structure.
2202 * iagp - pointer to iag.
2203 * extno - extent number.
2207 * -ENOSPC - insufficient disk resources.
2210 static int diNewExt(struct inomap * imap, struct iag * iagp, int extno)
2212 int agno, iagno, fwd, back, freei = 0, sword, rc;
2213 struct iag *aiagp = NULL, *biagp = NULL, *ciagp = NULL;
2214 struct metapage *amp, *bmp, *cmp, *dmp;
2215 struct inode *ipimap;
2221 struct jfs_sb_info *sbi;
2223 /* better have free extents.
2225 if (!iagp->nfreeexts) {
2226 jfs_error(imap->im_ipimap->i_sb, "diNewExt: no free extents");
2230 /* get the inode map inode.
2232 ipimap = imap->im_ipimap;
2233 sbi = JFS_SBI(ipimap->i_sb);
2235 amp = bmp = cmp = NULL;
2237 /* get the ag and iag numbers for this iag.
2239 agno = BLKTOAG(le64_to_cpu(iagp->agstart), sbi);
2240 iagno = le32_to_cpu(iagp->iagnum);
2242 /* check if this is the last free extent within the
2243 * iag. if so, the iag must be removed from the ag
2244 * free extent list, so get the iags preceeding and
2245 * following the iag on this list.
2247 if (iagp->nfreeexts == cpu_to_le32(1)) {
2248 if ((fwd = le32_to_cpu(iagp->extfreefwd)) >= 0) {
2249 if ((rc = diIAGRead(imap, fwd, &)))
2251 aiagp = (struct iag *) amp->data;
2254 if ((back = le32_to_cpu(iagp->extfreeback)) >= 0) {
2255 if ((rc = diIAGRead(imap, back, &bmp)))
2257 biagp = (struct iag *) bmp->data;
2260 /* the iag has free extents. if all extents are free
2261 * (as is the case for a newly allocated iag), the iag
2262 * must be added to the ag free extent list, so get
2263 * the iag at the head of the list in preparation for
2264 * adding this iag to this list.
2267 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2268 if ((fwd = imap->im_agctl[agno].extfree) >= 0) {
2269 if ((rc = diIAGRead(imap, fwd, &)))
2271 aiagp = (struct iag *) amp->data;
2276 /* check if the iag has no free inodes. if so, the iag
2277 * will have to be added to the ag free inode list, so get
2278 * the iag at the head of the list in preparation for
2279 * adding this iag to this list. in doing this, we must
2280 * check if we already have the iag at the head of
2283 if (iagp->nfreeinos == 0) {
2284 freei = imap->im_agctl[agno].inofree;
2289 } else if (freei == back) {
2292 if ((rc = diIAGRead(imap, freei, &cmp)))
2294 ciagp = (struct iag *) cmp->data;
2296 if (ciagp == NULL) {
2297 jfs_error(imap->im_ipimap->i_sb,
2298 "diNewExt: ciagp == NULL");
2305 /* allocate disk space for the inode extent.
2307 if ((extno == 0) || (addressPXD(&iagp->inoext[extno - 1]) == 0))
2308 hint = ((s64) agno << sbi->bmap->db_agl2size) - 1;
2310 hint = addressPXD(&iagp->inoext[extno - 1]) +
2311 lengthPXD(&iagp->inoext[extno - 1]) - 1;
2313 if ((rc = dbAlloc(ipimap, hint, (s64) imap->im_nbperiext, &blkno)))
2316 /* compute the inode number of the first inode within the
2319 ino = (iagno << L2INOSPERIAG) + (extno << L2INOSPEREXT);
2321 /* initialize the inodes within the newly allocated extent a
2324 for (i = 0; i < imap->im_nbperiext; i += sbi->nbperpage) {
2325 /* get a buffer for this page of disk inodes.
2327 dmp = get_metapage(ipimap, blkno + i, PSIZE, 1);
2332 dp = (struct dinode *) dmp->data;
2334 /* initialize the inode number, mode, link count and
2335 * inode extent address.
2337 for (j = 0; j < INOSPERPAGE; j++, dp++, ino++) {
2338 dp->di_inostamp = cpu_to_le32(sbi->inostamp);
2339 dp->di_number = cpu_to_le32(ino);
2340 dp->di_fileset = cpu_to_le32(FILESYSTEM_I);
2343 PXDaddress(&(dp->di_ixpxd), blkno);
2344 PXDlength(&(dp->di_ixpxd), imap->im_nbperiext);
2346 write_metapage(dmp);
2349 /* if this is the last free extent within the iag, remove the
2350 * iag from the ag free extent list.
2352 if (iagp->nfreeexts == cpu_to_le32(1)) {
2354 aiagp->extfreeback = iagp->extfreeback;
2357 biagp->extfreefwd = iagp->extfreefwd;
2359 imap->im_agctl[agno].extfree =
2360 le32_to_cpu(iagp->extfreefwd);
2362 iagp->extfreefwd = iagp->extfreeback = -1;
2364 /* if the iag has all free extents (newly allocated iag),
2365 * add the iag to the ag free extent list.
2367 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2369 aiagp->extfreeback = cpu_to_le32(iagno);
2371 iagp->extfreefwd = cpu_to_le32(fwd);
2372 iagp->extfreeback = -1;
2373 imap->im_agctl[agno].extfree = iagno;
2377 /* if the iag has no free inodes, add the iag to the
2378 * ag free inode list.
2380 if (iagp->nfreeinos == 0) {
2382 ciagp->inofreeback = cpu_to_le32(iagno);
2385 cpu_to_le32(imap->im_agctl[agno].inofree);
2386 iagp->inofreeback = -1;
2387 imap->im_agctl[agno].inofree = iagno;
2390 /* initialize the extent descriptor of the extent. */
2391 PXDlength(&iagp->inoext[extno], imap->im_nbperiext);
2392 PXDaddress(&iagp->inoext[extno], blkno);
2394 /* initialize the working and persistent map of the extent.
2395 * the working map will be initialized such that
2396 * it indicates the first inode of the extent is allocated.
2398 iagp->wmap[extno] = cpu_to_le32(HIGHORDER);
2399 iagp->pmap[extno] = 0;
2401 /* update the free inode and free extent summary maps
2402 * for the extent to indicate the extent has free inodes
2403 * and no longer represents a free extent.
2405 sword = extno >> L2EXTSPERSUM;
2406 mask = HIGHORDER >> (extno & (EXTSPERSUM - 1));
2407 iagp->extsmap[sword] |= cpu_to_le32(mask);
2408 iagp->inosmap[sword] &= cpu_to_le32(~mask);
2410 /* update the free inode and free extent counts for the
2413 iagp->nfreeinos = cpu_to_le32(le32_to_cpu(iagp->nfreeinos) +
2415 iagp->nfreeexts = cpu_to_le32(le32_to_cpu(iagp->nfreeexts) - 1);
2417 /* update the free and backed inode counts for the ag.
2419 imap->im_agctl[agno].numfree += (INOSPEREXT - 1);
2420 imap->im_agctl[agno].numinos += INOSPEREXT;
2422 /* update the free and backed inode counts for the inode map.
2424 atomic_add(INOSPEREXT - 1, &imap->im_numfree);
2425 atomic_add(INOSPEREXT, &imap->im_numinos);
2430 write_metapage(amp);
2432 write_metapage(bmp);
2434 write_metapage(cmp);
2440 /* release the iags.
2443 release_metapage(amp);
2445 release_metapage(bmp);
2447 release_metapage(cmp);
2454 * NAME: diNewIAG(imap,iagnop,agno)
2456 * FUNCTION: allocate a new iag for an allocation group.
2458 * first tries to allocate the iag from the inode map
2460 * if the list has free iags, the head of the list is removed
2461 * and returned to satisfy the request.
2462 * if the inode map's iag free list is empty, the inode map
2463 * is extended to hold a new iag. this new iag is initialized
2464 * and returned to satisfy the request.
2467 * imap - pointer to inode map control structure.
2468 * iagnop - pointer to an iag number set with the number of the
2469 * newly allocated iag upon successful return.
2470 * agno - allocation group number.
2471 * bpp - Buffer pointer to be filled in with new IAG's buffer
2475 * -ENOSPC - insufficient disk resources.
2479 * AG lock held on entry/exit;
2480 * write lock on the map is held inside;
2481 * read lock on the map is held on successful completion;
2483 * note: new iag transaction:
2484 * . synchronously write iag;
2485 * . write log of xtree and inode of imap;
2487 * . synchronous write of xtree (right to left, bottom to top);
2488 * . at start of logredo(): init in-memory imap with one additional iag page;
2489 * . at end of logredo(): re-read imap inode to determine
2493 diNewIAG(struct inomap * imap, int *iagnop, int agno, struct metapage ** mpp)
2497 struct inode *ipimap;
2498 struct super_block *sb;
2499 struct jfs_sb_info *sbi;
2500 struct metapage *mp;
2505 #ifdef _STILL_TO_PORT
2507 #endif /* _STILL_TO_PORT */
2508 struct inode *iplist[1];
2510 /* pick up pointers to the inode map and mount inodes */
2511 ipimap = imap->im_ipimap;
2515 /* acquire the free iag lock */
2518 /* if there are any iags on the inode map free iag list,
2519 * allocate the iag from the head of the list.
2521 if (imap->im_freeiag >= 0) {
2522 /* pick up the iag number at the head of the list */
2523 iagno = imap->im_freeiag;
2525 /* determine the logical block number of the iag */
2526 blkno = IAGTOLBLK(iagno, sbi->l2nbperpage);
2528 /* no free iags. the inode map will have to be extented
2529 * to include a new iag.
2532 /* acquire inode map lock */
2533 IWRITE_LOCK(ipimap);
2535 if (ipimap->i_size >> L2PSIZE != imap->im_nextiag + 1) {
2536 IWRITE_UNLOCK(ipimap);
2537 IAGFREE_UNLOCK(imap);
2538 jfs_error(imap->im_ipimap->i_sb,
2539 "diNewIAG: ipimap->i_size is wrong");
2544 /* get the next avaliable iag number */
2545 iagno = imap->im_nextiag;
2547 /* make sure that we have not exceeded the maximum inode
2550 if (iagno > (MAXIAGS - 1)) {
2551 /* release the inode map lock */
2552 IWRITE_UNLOCK(ipimap);
2559 * synchronously append new iag page.
2561 /* determine the logical address of iag page to append */
2562 blkno = IAGTOLBLK(iagno, sbi->l2nbperpage);
2564 /* Allocate extent for new iag page */
2565 xlen = sbi->nbperpage;
2566 if ((rc = dbAlloc(ipimap, 0, (s64) xlen, &xaddr))) {
2567 /* release the inode map lock */
2568 IWRITE_UNLOCK(ipimap);
2573 /* assign a buffer for the page */
2574 mp = get_metapage(ipimap, xaddr, PSIZE, 1);
2576 /* Free the blocks allocated for the iag since it was
2577 * not successfully added to the inode map
2579 dbFree(ipimap, xaddr, (s64) xlen);
2581 /* release the inode map lock */
2582 IWRITE_UNLOCK(ipimap);
2587 iagp = (struct iag *) mp->data;
2590 memset(iagp, 0, sizeof(struct iag));
2591 iagp->iagnum = cpu_to_le32(iagno);
2592 iagp->inofreefwd = iagp->inofreeback = -1;
2593 iagp->extfreefwd = iagp->extfreeback = -1;
2595 iagp->nfreeinos = 0;
2596 iagp->nfreeexts = cpu_to_le32(EXTSPERIAG);
2598 /* initialize the free inode summary map (free extent
2599 * summary map initialization handled by bzero).
2601 for (i = 0; i < SMAPSZ; i++)
2602 iagp->inosmap[i] = ONES;
2605 #ifdef _STILL_TO_PORT
2606 /* synchronously write the iag page */
2608 /* Free the blocks allocated for the iag since it was
2609 * not successfully added to the inode map
2611 dbFree(ipimap, xaddr, (s64) xlen);
2613 /* release the inode map lock */
2614 IWRITE_UNLOCK(ipimap);
2620 /* Now the iag is on disk */
2623 * start tyransaction of update of the inode map
2624 * addressing structure pointing to the new iag page;
2626 #endif /* _STILL_TO_PORT */
2627 tid = txBegin(sb, COMMIT_FORCE);
2628 down(&JFS_IP(ipimap)->commit_sem);
2630 /* update the inode map addressing structure to point to it */
2632 xtInsert(tid, ipimap, 0, blkno, xlen, &xaddr, 0))) {
2634 up(&JFS_IP(ipimap)->commit_sem);
2635 /* Free the blocks allocated for the iag since it was
2636 * not successfully added to the inode map
2638 dbFree(ipimap, xaddr, (s64) xlen);
2640 /* release the inode map lock */
2641 IWRITE_UNLOCK(ipimap);
2646 /* update the inode map's inode to reflect the extension */
2647 ipimap->i_size += PSIZE;
2648 ipimap->i_blocks += LBLK2PBLK(sb, xlen);
2651 * txCommit(COMMIT_FORCE) will synchronously write address
2652 * index pages and inode after commit in careful update order
2653 * of address index pages (right to left, bottom up);
2656 rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
2659 up(&JFS_IP(ipimap)->commit_sem);
2661 duplicateIXtree(sb, blkno, xlen, &xaddr);
2663 /* update the next avaliable iag number */
2664 imap->im_nextiag += 1;
2666 /* Add the iag to the iag free list so we don't lose the iag
2667 * if a failure happens now.
2669 imap->im_freeiag = iagno;
2671 /* Until we have logredo working, we want the imap inode &
2672 * control page to be up to date.
2676 /* release the inode map lock */
2677 IWRITE_UNLOCK(ipimap);
2680 /* obtain read lock on map */
2684 if ((rc = diIAGRead(imap, iagno, &mp))) {
2685 IREAD_UNLOCK(ipimap);
2689 iagp = (struct iag *) mp->data;
2691 /* remove the iag from the iag free list */
2692 imap->im_freeiag = le32_to_cpu(iagp->iagfree);
2695 /* set the return iag number and buffer pointer */
2700 /* release the iag free lock */
2701 IAGFREE_UNLOCK(imap);
2709 * FUNCTION: get the buffer for the specified iag within a fileset
2710 * or aggregate inode map.
2713 * imap - pointer to inode map control structure.
2714 * iagno - iag number.
2715 * bpp - point to buffer pointer to be filled in on successful
2719 * must have read lock on imap inode
2720 * (When called by diExtendFS, the filesystem is quiesced, therefore
2721 * the read lock is unnecessary.)
2727 static int diIAGRead(struct inomap * imap, int iagno, struct metapage ** mpp)
2729 struct inode *ipimap = imap->im_ipimap;
2732 /* compute the logical block number of the iag. */
2733 blkno = IAGTOLBLK(iagno, JFS_SBI(ipimap->i_sb)->l2nbperpage);
2736 *mpp = read_metapage(ipimap, blkno, PSIZE, 0);
2745 * NAME: diFindFree()
2747 * FUNCTION: find the first free bit in a word starting at
2748 * the specified bit position.
2751 * word - word to be examined.
2752 * start - starting bit position.
2755 * bit position of first free bit in the word or 32 if
2756 * no free bits were found.
2758 static int diFindFree(u32 word, int start)
2762 /* scan the word for the first free bit. */
2763 for (word <<= start, bitno = start; bitno < 32;
2764 bitno++, word <<= 1) {
2765 if ((word & HIGHORDER) == 0)
2772 * NAME: diUpdatePMap()
2774 * FUNCTION: Update the persistent map in an IAG for the allocation or
2775 * freeing of the specified inode.
2777 * PRE CONDITIONS: Working map has already been updated for allocate.
2780 * ipimap - Incore inode map inode
2781 * inum - Number of inode to mark in permanent map
2782 * is_free - If TRUE indicates inode should be marked freed, otherwise
2783 * indicates inode should be marked allocated.
2789 diUpdatePMap(struct inode *ipimap,
2790 unsigned long inum, boolean_t is_free, struct tblock * tblk)
2794 struct metapage *mp;
2795 int iagno, ino, extno, bitno;
2796 struct inomap *imap;
2798 struct jfs_log *log;
2799 int lsn, difft, diffp;
2801 imap = JFS_IP(ipimap)->i_imap;
2802 /* get the iag number containing the inode */
2803 iagno = INOTOIAG(inum);
2804 /* make sure that the iag is contained within the map */
2805 if (iagno >= imap->im_nextiag) {
2806 jfs_error(ipimap->i_sb,
2807 "diUpdatePMap: the iag is outside the map");
2812 rc = diIAGRead(imap, iagno, &mp);
2813 IREAD_UNLOCK(ipimap);
2816 iagp = (struct iag *) mp->data;
2817 /* get the inode number and extent number of the inode within
2818 * the iag and the inode number within the extent.
2820 ino = inum & (INOSPERIAG - 1);
2821 extno = ino >> L2INOSPEREXT;
2822 bitno = ino & (INOSPEREXT - 1);
2823 mask = HIGHORDER >> bitno;
2825 * mark the inode free in persistent map:
2827 if (is_free == TRUE) {
2828 /* The inode should have been allocated both in working
2829 * map and in persistent map;
2830 * the inode will be freed from working map at the release
2831 * of last reference release;
2833 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
2834 jfs_error(ipimap->i_sb,
2835 "diUpdatePMap: inode %ld not marked as "
2836 "allocated in wmap!", inum);
2838 if (!(le32_to_cpu(iagp->pmap[extno]) & mask)) {
2839 jfs_error(ipimap->i_sb,
2840 "diUpdatePMap: inode %ld not marked as "
2841 "allocated in pmap!", inum);
2843 /* update the bitmap for the extent of the freed inode */
2844 iagp->pmap[extno] &= cpu_to_le32(~mask);
2847 * mark the inode allocated in persistent map:
2850 /* The inode should be already allocated in the working map
2851 * and should be free in persistent map;
2853 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
2854 release_metapage(mp);
2855 jfs_error(ipimap->i_sb,
2856 "diUpdatePMap: the inode is not allocated in "
2860 if ((le32_to_cpu(iagp->pmap[extno]) & mask) != 0) {
2861 release_metapage(mp);
2862 jfs_error(ipimap->i_sb,
2863 "diUpdatePMap: the inode is not free in the "
2867 /* update the bitmap for the extent of the allocated inode */
2868 iagp->pmap[extno] |= cpu_to_le32(mask);
2874 log = JFS_SBI(tblk->sb)->log;
2876 /* inherit older/smaller lsn */
2877 logdiff(difft, lsn, log);
2878 logdiff(diffp, mp->lsn, log);
2879 if (difft < diffp) {
2881 /* move mp after tblock in logsync list */
2883 list_move(&mp->synclist, &tblk->synclist);
2884 LOGSYNC_UNLOCK(log);
2886 /* inherit younger/larger clsn */
2889 logdiff(difft, tblk->clsn, log);
2890 logdiff(diffp, mp->clsn, log);
2892 mp->clsn = tblk->clsn;
2893 LOGSYNC_UNLOCK(log);
2897 /* insert mp after tblock in logsync list */
2900 list_add(&mp->synclist, &tblk->synclist);
2901 mp->clsn = tblk->clsn;
2902 LOGSYNC_UNLOCK(log);
2911 * function: update imap for extendfs();
2913 * note: AG size has been increased s.t. each k old contiguous AGs are
2914 * coalesced into a new AG;
2916 int diExtendFS(struct inode *ipimap, struct inode *ipbmap)
2919 struct inomap *imap = JFS_IP(ipimap)->i_imap;
2920 struct iag *iagp = NULL, *hiagp = NULL;
2921 struct bmap *mp = JFS_SBI(ipbmap->i_sb)->bmap;
2922 struct metapage *bp, *hbp;
2924 int numinos, xnuminos = 0, xnumfree = 0;
2927 jfs_info("diExtendFS: nextiag:%d numinos:%d numfree:%d",
2928 imap->im_nextiag, atomic_read(&imap->im_numinos),
2929 atomic_read(&imap->im_numfree));
2934 * coalesce contiguous k (newAGSize/oldAGSize) AGs;
2935 * i.e., (AGi, ..., AGj) where i = k*n and j = k*(n+1) - 1 to AGn;
2936 * note: new AG size = old AG size * (2**x).
2939 /* init per AG control information im_agctl[] */
2940 for (i = 0; i < MAXAG; i++) {
2941 imap->im_agctl[i].inofree = -1; /* free inode list */
2942 imap->im_agctl[i].extfree = -1; /* free extent list */
2943 imap->im_agctl[i].numinos = 0; /* number of backed inodes */
2944 imap->im_agctl[i].numfree = 0; /* number of free backed inodes */
2948 * process each iag page of the map.
2950 * rebuild AG Free Inode List, AG Free Inode Extent List;
2952 for (i = 0; i < imap->im_nextiag; i++) {
2953 if ((rc = diIAGRead(imap, i, &bp))) {
2957 iagp = (struct iag *) bp->data;
2958 if (le32_to_cpu(iagp->iagnum) != i) {
2959 release_metapage(bp);
2960 jfs_error(ipimap->i_sb,
2961 "diExtendFs: unexpected value of iagnum");
2965 /* leave free iag in the free iag list */
2966 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2967 release_metapage(bp);
2971 /* agstart that computes to the same ag is treated as same; */
2972 agstart = le64_to_cpu(iagp->agstart);
2973 /* iagp->agstart = agstart & ~(mp->db_agsize - 1); */
2974 n = agstart >> mp->db_agl2size;
2976 /* compute backed inodes */
2977 numinos = (EXTSPERIAG - le32_to_cpu(iagp->nfreeexts))
2980 /* merge AG backed inodes */
2981 imap->im_agctl[n].numinos += numinos;
2982 xnuminos += numinos;
2985 /* if any backed free inodes, insert at AG free inode list */
2986 if ((int) le32_to_cpu(iagp->nfreeinos) > 0) {
2987 if ((head = imap->im_agctl[n].inofree) == -1)
2988 iagp->inofreefwd = iagp->inofreeback = -1;
2990 if ((rc = diIAGRead(imap, head, &hbp))) {
2994 hiagp = (struct iag *) hbp->data;
2995 hiagp->inofreeback =
2996 le32_to_cpu(iagp->iagnum);
2997 iagp->inofreefwd = cpu_to_le32(head);
2998 iagp->inofreeback = -1;
2999 write_metapage(hbp);
3002 imap->im_agctl[n].inofree =
3003 le32_to_cpu(iagp->iagnum);
3005 /* merge AG backed free inodes */
3006 imap->im_agctl[n].numfree +=
3007 le32_to_cpu(iagp->nfreeinos);
3008 xnumfree += le32_to_cpu(iagp->nfreeinos);
3011 /* if any free extents, insert at AG free extent list */
3012 if (le32_to_cpu(iagp->nfreeexts) > 0) {
3013 if ((head = imap->im_agctl[n].extfree) == -1)
3014 iagp->extfreefwd = iagp->extfreeback = -1;
3016 if ((rc = diIAGRead(imap, head, &hbp))) {
3020 hiagp = (struct iag *) hbp->data;
3021 hiagp->extfreeback = iagp->iagnum;
3022 iagp->extfreefwd = cpu_to_le32(head);
3023 iagp->extfreeback = -1;
3024 write_metapage(hbp);
3027 imap->im_agctl[n].extfree =
3028 le32_to_cpu(iagp->iagnum);
3035 if (xnuminos != atomic_read(&imap->im_numinos) ||
3036 xnumfree != atomic_read(&imap->im_numfree)) {
3037 jfs_error(ipimap->i_sb,
3038 "diExtendFs: numinos or numfree incorrect");
3049 * serialization: IWRITE_LOCK held on entry/exit
3051 * note: shadow page with regular inode (rel.2);
3053 static void duplicateIXtree(struct super_block *sb, s64 blkno,
3054 int xlen, s64 *xaddr)
3056 struct jfs_superblock *j_sb;
3057 struct buffer_head *bh;
3061 /* if AIT2 ipmap2 is bad, do not try to update it */
3062 if (JFS_SBI(sb)->mntflag & JFS_BAD_SAIT) /* s_flag */
3064 ip = diReadSpecial(sb, FILESYSTEM_I, 1);
3066 JFS_SBI(sb)->mntflag |= JFS_BAD_SAIT;
3067 if (readSuper(sb, &bh))
3069 j_sb = (struct jfs_superblock *)bh->b_data;
3070 j_sb->s_flag |= JFS_BAD_SAIT;
3072 mark_buffer_dirty(bh);
3073 sync_dirty_buffer(bh);
3078 /* start transaction */
3079 tid = txBegin(sb, COMMIT_FORCE);
3080 /* update the inode map addressing structure to point to it */
3081 if (xtInsert(tid, ip, 0, blkno, xlen, xaddr, 0)) {
3082 JFS_SBI(sb)->mntflag |= JFS_BAD_SAIT;
3087 /* update the inode map's inode to reflect the extension */
3088 ip->i_size += PSIZE;
3089 ip->i_blocks += LBLK2PBLK(sb, xlen);
3090 txCommit(tid, 1, &ip, COMMIT_FORCE);
3097 * NAME: copy_from_dinode()
3099 * FUNCTION: Copies inode info from disk inode to in-memory inode
3103 * -ENOMEM - insufficient memory
3105 static int copy_from_dinode(struct dinode * dip, struct inode *ip)
3107 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
3111 jfs_ip->fileset = le32_to_cpu(dip->di_fileset);
3112 jfs_ip->mode2 = le32_to_cpu(dip->di_mode);
3114 ip->i_mode = le32_to_cpu(dip->di_mode) & 0xffff;
3115 ip->i_nlink = le32_to_cpu(dip->di_nlink);
3117 uid = le32_to_cpu(dip->di_uid);
3118 gid = le32_to_cpu(dip->di_gid);
3119 ip->i_uid = INOXID_UID(XID_TAG(ip), uid, gid);
3120 ip->i_gid = INOXID_GID(XID_TAG(ip), uid, gid);
3121 ip->i_xid = INOXID_XID(XID_TAG(ip), uid, gid, 0);
3123 ip->i_size = le64_to_cpu(dip->di_size);
3124 ip->i_atime.tv_sec = le32_to_cpu(dip->di_atime.tv_sec);
3125 ip->i_atime.tv_nsec = le32_to_cpu(dip->di_atime.tv_nsec);
3126 ip->i_mtime.tv_sec = le32_to_cpu(dip->di_mtime.tv_sec);
3127 ip->i_mtime.tv_nsec = le32_to_cpu(dip->di_mtime.tv_nsec);
3128 ip->i_ctime.tv_sec = le32_to_cpu(dip->di_ctime.tv_sec);
3129 ip->i_ctime.tv_nsec = le32_to_cpu(dip->di_ctime.tv_nsec);
3130 ip->i_blksize = ip->i_sb->s_blocksize;
3131 ip->i_blocks = LBLK2PBLK(ip->i_sb, le64_to_cpu(dip->di_nblocks));
3132 ip->i_generation = le32_to_cpu(dip->di_gen);
3134 jfs_ip->ixpxd = dip->di_ixpxd; /* in-memory pxd's are little-endian */
3135 jfs_ip->acl = dip->di_acl; /* as are dxd's */
3136 jfs_ip->ea = dip->di_ea;
3137 jfs_ip->next_index = le32_to_cpu(dip->di_next_index);
3138 jfs_ip->otime = le32_to_cpu(dip->di_otime.tv_sec);
3139 jfs_ip->acltype = le32_to_cpu(dip->di_acltype);
3141 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode)) {
3142 jfs_ip->dev = le32_to_cpu(dip->di_rdev);
3143 ip->i_rdev = new_decode_dev(jfs_ip->dev);
3146 if (S_ISDIR(ip->i_mode)) {
3147 memcpy(&jfs_ip->i_dirtable, &dip->di_dirtable, 384);
3148 } else if (S_ISREG(ip->i_mode) || S_ISLNK(ip->i_mode)) {
3149 memcpy(&jfs_ip->i_xtroot, &dip->di_xtroot, 288);
3151 memcpy(&jfs_ip->i_inline_ea, &dip->di_inlineea, 128);
3153 /* Zero the in-memory-only stuff */
3155 jfs_ip->btindex = 0;
3156 jfs_ip->btorder = 0;
3159 jfs_ip->atlhead = 0;
3160 jfs_ip->atltail = 0;
3166 * NAME: copy_to_dinode()
3168 * FUNCTION: Copies inode info from in-memory inode to disk inode
3170 static void copy_to_dinode(struct dinode * dip, struct inode *ip)
3172 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
3176 dip->di_fileset = cpu_to_le32(jfs_ip->fileset);
3177 dip->di_inostamp = cpu_to_le32(JFS_SBI(ip->i_sb)->inostamp);
3178 dip->di_number = cpu_to_le32(ip->i_ino);
3179 dip->di_gen = cpu_to_le32(ip->i_generation);
3180 dip->di_size = cpu_to_le64(ip->i_size);
3181 dip->di_nblocks = cpu_to_le64(PBLK2LBLK(ip->i_sb, ip->i_blocks));
3182 dip->di_nlink = cpu_to_le32(ip->i_nlink);
3184 uid = XIDINO_UID(XID_TAG(ip), ip->i_uid, ip->i_xid);
3185 gid = XIDINO_GID(XID_TAG(ip), ip->i_gid, ip->i_xid);
3186 dip->di_uid = cpu_to_le32(uid);
3187 dip->di_gid = cpu_to_le32(gid);
3189 * mode2 is only needed for storing the higher order bits.
3190 * Trust i_mode for the lower order ones
3192 dip->di_mode = cpu_to_le32((jfs_ip->mode2 & 0xffff0000) | ip->i_mode);
3193 dip->di_atime.tv_sec = cpu_to_le32(ip->i_atime.tv_sec);
3194 dip->di_atime.tv_nsec = cpu_to_le32(ip->i_atime.tv_nsec);
3195 dip->di_ctime.tv_sec = cpu_to_le32(ip->i_ctime.tv_sec);
3196 dip->di_ctime.tv_nsec = cpu_to_le32(ip->i_ctime.tv_nsec);
3197 dip->di_mtime.tv_sec = cpu_to_le32(ip->i_mtime.tv_sec);
3198 dip->di_mtime.tv_nsec = cpu_to_le32(ip->i_mtime.tv_nsec);
3199 dip->di_ixpxd = jfs_ip->ixpxd; /* in-memory pxd's are little-endian */
3200 dip->di_acl = jfs_ip->acl; /* as are dxd's */
3201 dip->di_ea = jfs_ip->ea;
3202 dip->di_next_index = cpu_to_le32(jfs_ip->next_index);
3203 dip->di_otime.tv_sec = cpu_to_le32(jfs_ip->otime);
3204 dip->di_otime.tv_nsec = 0;
3205 dip->di_acltype = cpu_to_le32(jfs_ip->acltype);
3206 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode))
3207 dip->di_rdev = cpu_to_le32(jfs_ip->dev);
3210 #ifdef _JFS_DEBUG_IMAP
3214 static void *DBGdiInit(struct inomap * imap)
3219 if ((dimap = (u32 *) xmalloc(size, L2PSIZE, kernel_heap)) == NULL)
3221 bzero((void *) dimap, size);
3222 imap->im_DBGdimap = dimap;
3228 static void DBGdiAlloc(struct inomap * imap, ino_t ino)
3230 u32 *dimap = imap->im_DBGdimap;
3235 m = 0x80000000 >> b;
3236 assert(w < 64 * 256);
3238 printk("DEBUG diAlloc: duplicate alloc ino:0x%x\n", ino);
3246 static void DBGdiFree(struct inomap * imap, ino_t ino)
3248 u32 *dimap = imap->im_DBGdimap;
3253 m = 0x80000000 >> b;
3254 assert(w < 64 * 256);
3255 if ((dimap[w] & m) == 0) {
3256 printk("DEBUG diFree: duplicate free ino:0x%x\n", ino);
3261 static void dump_cp(struct inomap * ipimap, char *function, int line)
3263 printk("\n* ********* *\nControl Page %s %d\n", function, line);
3264 printk("FreeIAG %d\tNextIAG %d\n", ipimap->im_freeiag,
3265 ipimap->im_nextiag);
3266 printk("NumInos %d\tNumFree %d\n",
3267 atomic_read(&ipimap->im_numinos),
3268 atomic_read(&ipimap->im_numfree));
3269 printk("AG InoFree %d\tAG ExtFree %d\n",
3270 ipimap->im_agctl[0].inofree, ipimap->im_agctl[0].extfree);
3271 printk("AG NumInos %d\tAG NumFree %d\n",
3272 ipimap->im_agctl[0].numinos, ipimap->im_agctl[0].numfree);
3275 static void dump_iag(struct iag * iag, char *function, int line)
3277 printk("\n* ********* *\nIAG %s %d\n", function, line);
3278 printk("IagNum %d\tIAG Free %d\n", le32_to_cpu(iag->iagnum),
3279 le32_to_cpu(iag->iagfree));
3280 printk("InoFreeFwd %d\tInoFreeBack %d\n",
3281 le32_to_cpu(iag->inofreefwd),
3282 le32_to_cpu(iag->inofreeback));
3283 printk("ExtFreeFwd %d\tExtFreeBack %d\n",
3284 le32_to_cpu(iag->extfreefwd),
3285 le32_to_cpu(iag->extfreeback));
3286 printk("NFreeInos %d\tNFreeExts %d\n", le32_to_cpu(iag->nfreeinos),
3287 le32_to_cpu(iag->nfreeexts));
3289 #endif /* _JFS_DEBUG_IMAP */