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);
1285 /* acquire tlock of the iag page of the freed ixad
1286 * to force the page NOHOMEOK (even though no data is
1287 * logged from the iag page) until NOREDOPAGE|FREEXTENT log
1288 * for the free of the extent is committed;
1289 * write FREEXTENT|NOREDOPAGE log record
1290 * N.B. linelock is overlaid as freed extent descriptor;
1292 tlck = txLock(tid, ipimap, mp, tlckINODE | tlckFREE);
1293 pxdlock = (struct pxd_lock *) & tlck->lock;
1294 pxdlock->flag = mlckFREEPXD;
1295 pxdlock->pxd = freepxd;
1303 * logredo needs the IAG number and IAG extent index in order
1304 * to ensure that the IMap is consistent. The least disruptive
1305 * way to pass these values through to the transaction manager
1306 * is in the iplist array.
1308 * It's not pretty, but it works.
1310 iplist[1] = (struct inode *) (size_t)iagno;
1311 iplist[2] = (struct inode *) (size_t)extno;
1313 rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
1317 /* unlock the AG inode map information */
1318 AG_UNLOCK(imap, agno);
1323 IREAD_UNLOCK(ipimap);
1326 release_metapage(amp);
1328 release_metapage(bmp);
1330 release_metapage(cmp);
1332 release_metapage(dmp);
1334 AG_UNLOCK(imap, agno);
1336 release_metapage(mp);
1342 * There are several places in the diAlloc* routines where we initialize
1346 diInitInode(struct inode *ip, int iagno, int ino, int extno, struct iag * iagp)
1348 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
1349 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
1351 ip->i_ino = (iagno << L2INOSPERIAG) + ino;
1352 DBG_DIALLOC(JFS_IP(ipimap)->i_imap, ip->i_ino);
1353 jfs_ip->ixpxd = iagp->inoext[extno];
1354 jfs_ip->agno = BLKTOAG(le64_to_cpu(iagp->agstart), sbi);
1355 jfs_ip->active_ag = -1;
1360 * NAME: diAlloc(pip,dir,ip)
1362 * FUNCTION: allocate a disk inode from the inode working map
1363 * for a fileset or aggregate.
1366 * pip - pointer to incore inode for the parent inode.
1367 * dir - TRUE if the new disk inode is for a directory.
1368 * ip - pointer to a new inode
1372 * -ENOSPC - insufficient disk resources.
1375 int diAlloc(struct inode *pip, boolean_t dir, struct inode *ip)
1377 int rc, ino, iagno, addext, extno, bitno, sword;
1378 int nwords, rem, i, agno;
1379 u32 mask, inosmap, extsmap;
1380 struct inode *ipimap;
1381 struct metapage *mp;
1384 struct inomap *imap;
1386 /* get the pointers to the inode map inode and the
1387 * corresponding imap control structure.
1389 ipimap = JFS_SBI(pip->i_sb)->ipimap;
1390 imap = JFS_IP(ipimap)->i_imap;
1391 JFS_IP(ip)->ipimap = ipimap;
1392 JFS_IP(ip)->fileset = FILESYSTEM_I;
1394 /* for a directory, the allocation policy is to start
1395 * at the ag level using the preferred ag.
1398 agno = dbNextAG(JFS_SBI(pip->i_sb)->ipbmap);
1399 AG_LOCK(imap, agno);
1403 /* for files, the policy starts off by trying to allocate from
1404 * the same iag containing the parent disk inode:
1405 * try to allocate the new disk inode close to the parent disk
1406 * inode, using parent disk inode number + 1 as the allocation
1407 * hint. (we use a left-to-right policy to attempt to avoid
1408 * moving backward on the disk.) compute the hint within the
1409 * file system and the iag.
1412 /* get the ag number of this iag */
1413 agno = JFS_IP(pip)->agno;
1415 if (atomic_read(&JFS_SBI(pip->i_sb)->bmap->db_active[agno])) {
1417 * There is an open file actively growing. We want to
1418 * allocate new inodes from a different ag to avoid
1419 * fragmentation problems.
1421 agno = dbNextAG(JFS_SBI(pip->i_sb)->ipbmap);
1422 AG_LOCK(imap, agno);
1426 inum = pip->i_ino + 1;
1427 ino = inum & (INOSPERIAG - 1);
1429 /* back off the the hint if it is outside of the iag */
1433 /* lock the AG inode map information */
1434 AG_LOCK(imap, agno);
1436 /* Get read lock on imap inode */
1439 /* get the iag number and read the iag */
1440 iagno = INOTOIAG(inum);
1441 if ((rc = diIAGRead(imap, iagno, &mp))) {
1442 IREAD_UNLOCK(ipimap);
1443 AG_UNLOCK(imap, agno);
1446 iagp = (struct iag *) mp->data;
1448 /* determine if new inode extent is allowed to be added to the iag.
1449 * new inode extent can be added to the iag if the ag
1450 * has less than 32 free disk inodes and the iag has free extents.
1452 addext = (imap->im_agctl[agno].numfree < 32 && iagp->nfreeexts);
1455 * try to allocate from the IAG
1457 /* check if the inode may be allocated from the iag
1458 * (i.e. the inode has free inodes or new extent can be added).
1460 if (iagp->nfreeinos || addext) {
1461 /* determine the extent number of the hint.
1463 extno = ino >> L2INOSPEREXT;
1465 /* check if the extent containing the hint has backed
1466 * inodes. if so, try to allocate within this extent.
1468 if (addressPXD(&iagp->inoext[extno])) {
1469 bitno = ino & (INOSPEREXT - 1);
1471 diFindFree(le32_to_cpu(iagp->wmap[extno]),
1474 ino = (extno << L2INOSPEREXT) + bitno;
1476 /* a free inode (bit) was found within this
1477 * extent, so allocate it.
1479 rc = diAllocBit(imap, iagp, ino);
1480 IREAD_UNLOCK(ipimap);
1484 /* set the results of the allocation
1485 * and write the iag.
1487 diInitInode(ip, iagno, ino, extno,
1489 mark_metapage_dirty(mp);
1491 release_metapage(mp);
1493 /* free the AG lock and return.
1495 AG_UNLOCK(imap, agno);
1502 EXTSPERIAG - 1) ? 0 : extno + 1;
1506 * no free inodes within the extent containing the hint.
1508 * try to allocate from the backed extents following
1509 * hint or, if appropriate (i.e. addext is true), allocate
1510 * an extent of free inodes at or following the extent
1511 * containing the hint.
1513 * the free inode and free extent summary maps are used
1514 * here, so determine the starting summary map position
1515 * and the number of words we'll have to examine. again,
1516 * the approach is to allocate following the hint, so we
1517 * might have to initially ignore prior bits of the summary
1518 * map that represent extents prior to the extent containing
1519 * the hint and later revisit these bits.
1521 bitno = extno & (EXTSPERSUM - 1);
1522 nwords = (bitno == 0) ? SMAPSZ : SMAPSZ + 1;
1523 sword = extno >> L2EXTSPERSUM;
1525 /* mask any prior bits for the starting words of the
1528 mask = ONES << (EXTSPERSUM - bitno);
1529 inosmap = le32_to_cpu(iagp->inosmap[sword]) | mask;
1530 extsmap = le32_to_cpu(iagp->extsmap[sword]) | mask;
1532 /* scan the free inode and free extent summary maps for
1535 for (i = 0; i < nwords; i++) {
1536 /* check if this word of the free inode summary
1537 * map describes an extent with free inodes.
1540 /* an extent with free inodes has been
1541 * found. determine the extent number
1542 * and the inode number within the extent.
1544 rem = diFindFree(inosmap, 0);
1545 extno = (sword << L2EXTSPERSUM) + rem;
1546 rem = diFindFree(le32_to_cpu(iagp->wmap[extno]),
1548 if (rem >= INOSPEREXT) {
1549 IREAD_UNLOCK(ipimap);
1550 release_metapage(mp);
1551 AG_UNLOCK(imap, agno);
1553 "diAlloc: can't find free bit "
1558 /* determine the inode number within the
1559 * iag and allocate the inode from the
1562 ino = (extno << L2INOSPEREXT) + rem;
1563 rc = diAllocBit(imap, iagp, ino);
1564 IREAD_UNLOCK(ipimap);
1568 /* set the results of the allocation
1569 * and write the iag.
1571 diInitInode(ip, iagno, ino, extno,
1573 mark_metapage_dirty(mp);
1575 release_metapage(mp);
1577 /* free the AG lock and return.
1579 AG_UNLOCK(imap, agno);
1584 /* check if we may allocate an extent of free
1585 * inodes and whether this word of the free
1586 * extents summary map describes a free extent.
1588 if (addext && ~extsmap) {
1589 /* a free extent has been found. determine
1590 * the extent number.
1592 rem = diFindFree(extsmap, 0);
1593 extno = (sword << L2EXTSPERSUM) + rem;
1595 /* allocate an extent of free inodes.
1597 if ((rc = diNewExt(imap, iagp, extno))) {
1598 /* if there is no disk space for a
1599 * new extent, try to allocate the
1600 * disk inode from somewhere else.
1607 /* set the results of the allocation
1608 * and write the iag.
1610 diInitInode(ip, iagno,
1611 extno << L2INOSPEREXT,
1613 mark_metapage_dirty(mp);
1615 release_metapage(mp);
1616 /* free the imap inode & the AG lock & return.
1618 IREAD_UNLOCK(ipimap);
1619 AG_UNLOCK(imap, agno);
1623 /* move on to the next set of summary map words.
1625 sword = (sword == SMAPSZ - 1) ? 0 : sword + 1;
1626 inosmap = le32_to_cpu(iagp->inosmap[sword]);
1627 extsmap = le32_to_cpu(iagp->extsmap[sword]);
1630 /* unlock imap inode */
1631 IREAD_UNLOCK(ipimap);
1633 /* nothing doing in this iag, so release it. */
1634 release_metapage(mp);
1638 * try to allocate anywhere within the same AG as the parent inode.
1640 rc = diAllocAG(imap, agno, dir, ip);
1642 AG_UNLOCK(imap, agno);
1648 * try to allocate in any AG.
1650 return (diAllocAny(imap, agno, dir, ip));
1655 * NAME: diAllocAG(imap,agno,dir,ip)
1657 * FUNCTION: allocate a disk inode from the allocation group.
1659 * this routine first determines if a new extent of free
1660 * inodes should be added for the allocation group, with
1661 * the current request satisfied from this extent. if this
1662 * is the case, an attempt will be made to do just that. if
1663 * this attempt fails or it has been determined that a new
1664 * extent should not be added, an attempt is made to satisfy
1665 * the request by allocating an existing (backed) free inode
1666 * from the allocation group.
1668 * PRE CONDITION: Already have the AG lock for this AG.
1671 * imap - pointer to inode map control structure.
1672 * agno - allocation group to allocate from.
1673 * dir - TRUE if the new disk inode is for a directory.
1674 * ip - pointer to the new inode to be filled in on successful return
1675 * with the disk inode number allocated, its extent address
1676 * and the start of the ag.
1680 * -ENOSPC - insufficient disk resources.
1684 diAllocAG(struct inomap * imap, int agno, boolean_t dir, struct inode *ip)
1686 int rc, addext, numfree, numinos;
1688 /* get the number of free and the number of backed disk
1689 * inodes currently within the ag.
1691 numfree = imap->im_agctl[agno].numfree;
1692 numinos = imap->im_agctl[agno].numinos;
1694 if (numfree > numinos) {
1695 jfs_error(ip->i_sb, "diAllocAG: numfree > numinos");
1699 /* determine if we should allocate a new extent of free inodes
1700 * within the ag: for directory inodes, add a new extent
1701 * if there are a small number of free inodes or number of free
1702 * inodes is a small percentage of the number of backed inodes.
1705 addext = (numfree < 64 ||
1707 && ((numfree * 100) / numinos) <= 20));
1709 addext = (numfree == 0);
1712 * try to allocate a new extent of free inodes.
1715 /* if free space is not avaliable for this new extent, try
1716 * below to allocate a free and existing (already backed)
1717 * inode from the ag.
1719 if ((rc = diAllocExt(imap, agno, ip)) != -ENOSPC)
1724 * try to allocate an existing free inode from the ag.
1726 return (diAllocIno(imap, agno, ip));
1731 * NAME: diAllocAny(imap,agno,dir,iap)
1733 * FUNCTION: allocate a disk inode from any other allocation group.
1735 * this routine is called when an allocation attempt within
1736 * the primary allocation group has failed. if attempts to
1737 * allocate an inode from any allocation group other than the
1738 * specified primary group.
1741 * imap - pointer to inode map control structure.
1742 * agno - primary allocation group (to avoid).
1743 * dir - TRUE if the new disk inode is for a directory.
1744 * ip - pointer to a new inode to be filled in on successful return
1745 * with the disk inode number allocated, its extent address
1746 * and the start of the ag.
1750 * -ENOSPC - insufficient disk resources.
1754 diAllocAny(struct inomap * imap, int agno, boolean_t dir, struct inode *ip)
1757 int maxag = JFS_SBI(imap->im_ipimap->i_sb)->bmap->db_maxag;
1760 /* try to allocate from the ags following agno up to
1761 * the maximum ag number.
1763 for (ag = agno + 1; ag <= maxag; ag++) {
1766 rc = diAllocAG(imap, ag, dir, ip);
1768 AG_UNLOCK(imap, ag);
1774 /* try to allocate from the ags in front of agno.
1776 for (ag = 0; ag < agno; ag++) {
1779 rc = diAllocAG(imap, ag, dir, ip);
1781 AG_UNLOCK(imap, ag);
1787 /* no free disk inodes.
1794 * NAME: diAllocIno(imap,agno,ip)
1796 * FUNCTION: allocate a disk inode from the allocation group's free
1797 * inode list, returning an error if this free list is
1798 * empty (i.e. no iags on the list).
1800 * allocation occurs from the first iag on the list using
1801 * the iag's free inode summary map to find the leftmost
1802 * free inode in the iag.
1804 * PRE CONDITION: Already have AG lock for this AG.
1807 * imap - pointer to inode map control structure.
1808 * agno - allocation group.
1809 * ip - pointer to new inode to be filled in on successful return
1810 * with the disk inode number allocated, its extent address
1811 * and the start of the ag.
1815 * -ENOSPC - insufficient disk resources.
1818 static int diAllocIno(struct inomap * imap, int agno, struct inode *ip)
1820 int iagno, ino, rc, rem, extno, sword;
1821 struct metapage *mp;
1824 /* check if there are iags on the ag's free inode list.
1826 if ((iagno = imap->im_agctl[agno].inofree) < 0)
1829 /* obtain read lock on imap inode */
1830 IREAD_LOCK(imap->im_ipimap);
1832 /* read the iag at the head of the list.
1834 if ((rc = diIAGRead(imap, iagno, &mp))) {
1835 IREAD_UNLOCK(imap->im_ipimap);
1838 iagp = (struct iag *) mp->data;
1840 /* better be free inodes in this iag if it is on the
1843 if (!iagp->nfreeinos) {
1844 IREAD_UNLOCK(imap->im_ipimap);
1845 release_metapage(mp);
1847 "diAllocIno: nfreeinos = 0, but iag on freelist");
1851 /* scan the free inode summary map to find an extent
1854 for (sword = 0;; sword++) {
1855 if (sword >= SMAPSZ) {
1856 IREAD_UNLOCK(imap->im_ipimap);
1857 release_metapage(mp);
1859 "diAllocIno: free inode not found in summary map");
1863 if (~iagp->inosmap[sword])
1867 /* found a extent with free inodes. determine
1868 * the extent number.
1870 rem = diFindFree(le32_to_cpu(iagp->inosmap[sword]), 0);
1871 if (rem >= EXTSPERSUM) {
1872 IREAD_UNLOCK(imap->im_ipimap);
1873 release_metapage(mp);
1874 jfs_error(ip->i_sb, "diAllocIno: no free extent found");
1877 extno = (sword << L2EXTSPERSUM) + rem;
1879 /* find the first free inode in the extent.
1881 rem = diFindFree(le32_to_cpu(iagp->wmap[extno]), 0);
1882 if (rem >= INOSPEREXT) {
1883 IREAD_UNLOCK(imap->im_ipimap);
1884 release_metapage(mp);
1885 jfs_error(ip->i_sb, "diAllocIno: free inode not found");
1889 /* compute the inode number within the iag.
1891 ino = (extno << L2INOSPEREXT) + rem;
1893 /* allocate the inode.
1895 rc = diAllocBit(imap, iagp, ino);
1896 IREAD_UNLOCK(imap->im_ipimap);
1898 release_metapage(mp);
1902 /* set the results of the allocation and write the iag.
1904 diInitInode(ip, iagno, ino, extno, iagp);
1912 * NAME: diAllocExt(imap,agno,ip)
1914 * FUNCTION: add a new extent of free inodes to an iag, allocating
1915 * an inode from this extent to satisfy the current allocation
1918 * this routine first tries to find an existing iag with free
1919 * extents through the ag free extent list. if list is not
1920 * empty, the head of the list will be selected as the home
1921 * of the new extent of free inodes. otherwise (the list is
1922 * empty), a new iag will be allocated for the ag to contain
1925 * once an iag has been selected, the free extent summary map
1926 * is used to locate a free extent within the iag and diNewExt()
1927 * is called to initialize the extent, with initialization
1928 * including the allocation of the first inode of the extent
1929 * for the purpose of satisfying this request.
1932 * imap - pointer to inode map control structure.
1933 * agno - allocation group number.
1934 * ip - pointer to new inode to be filled in on successful return
1935 * with the disk inode number allocated, its extent address
1936 * and the start of the ag.
1940 * -ENOSPC - insufficient disk resources.
1943 static int diAllocExt(struct inomap * imap, int agno, struct inode *ip)
1945 int rem, iagno, sword, extno, rc;
1946 struct metapage *mp;
1949 /* check if the ag has any iags with free extents. if not,
1950 * allocate a new iag for the ag.
1952 if ((iagno = imap->im_agctl[agno].extfree) < 0) {
1953 /* If successful, diNewIAG will obtain the read lock on the
1956 if ((rc = diNewIAG(imap, &iagno, agno, &mp))) {
1959 iagp = (struct iag *) mp->data;
1961 /* set the ag number if this a brand new iag
1964 cpu_to_le64(AGTOBLK(agno, imap->im_ipimap));
1968 IREAD_LOCK(imap->im_ipimap);
1969 if ((rc = diIAGRead(imap, iagno, &mp))) {
1970 IREAD_UNLOCK(imap->im_ipimap);
1971 jfs_error(ip->i_sb, "diAllocExt: error reading iag");
1974 iagp = (struct iag *) mp->data;
1977 /* using the free extent summary map, find a free extent.
1979 for (sword = 0;; sword++) {
1980 if (sword >= SMAPSZ) {
1981 release_metapage(mp);
1982 IREAD_UNLOCK(imap->im_ipimap);
1984 "diAllocExt: free ext summary map not found");
1987 if (~iagp->extsmap[sword])
1991 /* determine the extent number of the free extent.
1993 rem = diFindFree(le32_to_cpu(iagp->extsmap[sword]), 0);
1994 if (rem >= EXTSPERSUM) {
1995 release_metapage(mp);
1996 IREAD_UNLOCK(imap->im_ipimap);
1997 jfs_error(ip->i_sb, "diAllocExt: free extent not found");
2000 extno = (sword << L2EXTSPERSUM) + rem;
2002 /* initialize the new extent.
2004 rc = diNewExt(imap, iagp, extno);
2005 IREAD_UNLOCK(imap->im_ipimap);
2007 /* something bad happened. if a new iag was allocated,
2008 * place it back on the inode map's iag free list, and
2009 * clear the ag number information.
2011 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2013 iagp->iagfree = cpu_to_le32(imap->im_freeiag);
2014 imap->im_freeiag = iagno;
2015 IAGFREE_UNLOCK(imap);
2021 /* set the results of the allocation and write the iag.
2023 diInitInode(ip, iagno, extno << L2INOSPEREXT, extno, iagp);
2032 * NAME: diAllocBit(imap,iagp,ino)
2034 * FUNCTION: allocate a backed inode from an iag.
2036 * this routine performs the mechanics of allocating a
2037 * specified inode from a backed extent.
2039 * if the inode to be allocated represents the last free
2040 * inode within the iag, the iag will be removed from the
2041 * ag free inode list.
2043 * a careful update approach is used to provide consistency
2044 * in the face of updates to multiple buffers. under this
2045 * approach, all required buffers are obtained before making
2046 * any updates and are held all are updates are complete.
2048 * PRE CONDITION: Already have buffer lock on iagp. Already have AG lock on
2049 * this AG. Must have read lock on imap inode.
2052 * imap - pointer to inode map control structure.
2053 * iagp - pointer to iag.
2054 * ino - inode number to be allocated within the iag.
2058 * -ENOSPC - insufficient disk resources.
2061 static int diAllocBit(struct inomap * imap, struct iag * iagp, int ino)
2063 int extno, bitno, agno, sword, rc;
2064 struct metapage *amp = NULL, *bmp = NULL;
2065 struct iag *aiagp = 0, *biagp = 0;
2068 /* check if this is the last free inode within the iag.
2069 * if so, it will have to be removed from the ag free
2070 * inode list, so get the iags preceeding and following
2073 if (iagp->nfreeinos == cpu_to_le32(1)) {
2074 if ((int) le32_to_cpu(iagp->inofreefwd) >= 0) {
2076 diIAGRead(imap, le32_to_cpu(iagp->inofreefwd),
2079 aiagp = (struct iag *) amp->data;
2082 if ((int) le32_to_cpu(iagp->inofreeback) >= 0) {
2085 le32_to_cpu(iagp->inofreeback),
2088 release_metapage(amp);
2091 biagp = (struct iag *) bmp->data;
2095 /* get the ag number, extent number, inode number within
2098 agno = BLKTOAG(le64_to_cpu(iagp->agstart), JFS_SBI(imap->im_ipimap->i_sb));
2099 extno = ino >> L2INOSPEREXT;
2100 bitno = ino & (INOSPEREXT - 1);
2102 /* compute the mask for setting the map.
2104 mask = HIGHORDER >> bitno;
2106 /* the inode should be free and backed.
2108 if (((le32_to_cpu(iagp->pmap[extno]) & mask) != 0) ||
2109 ((le32_to_cpu(iagp->wmap[extno]) & mask) != 0) ||
2110 (addressPXD(&iagp->inoext[extno]) == 0)) {
2112 release_metapage(amp);
2114 release_metapage(bmp);
2116 jfs_error(imap->im_ipimap->i_sb,
2117 "diAllocBit: iag inconsistent");
2121 /* mark the inode as allocated in the working map.
2123 iagp->wmap[extno] |= cpu_to_le32(mask);
2125 /* check if all inodes within the extent are now
2126 * allocated. if so, update the free inode summary
2127 * map to reflect this.
2129 if (iagp->wmap[extno] == ONES) {
2130 sword = extno >> L2EXTSPERSUM;
2131 bitno = extno & (EXTSPERSUM - 1);
2132 iagp->inosmap[sword] |= cpu_to_le32(HIGHORDER >> bitno);
2135 /* if this was the last free inode in the iag, remove the
2136 * iag from the ag free inode list.
2138 if (iagp->nfreeinos == cpu_to_le32(1)) {
2140 aiagp->inofreeback = iagp->inofreeback;
2141 write_metapage(amp);
2145 biagp->inofreefwd = iagp->inofreefwd;
2146 write_metapage(bmp);
2148 imap->im_agctl[agno].inofree =
2149 le32_to_cpu(iagp->inofreefwd);
2151 iagp->inofreefwd = iagp->inofreeback = -1;
2154 /* update the free inode count at the iag, ag, inode
2157 iagp->nfreeinos = cpu_to_le32(le32_to_cpu(iagp->nfreeinos) - 1);
2158 imap->im_agctl[agno].numfree -= 1;
2159 atomic_dec(&imap->im_numfree);
2166 * NAME: diNewExt(imap,iagp,extno)
2168 * FUNCTION: initialize a new extent of inodes for an iag, allocating
2169 * the first inode of the extent for use for the current
2170 * allocation request.
2172 * disk resources are allocated for the new extent of inodes
2173 * and the inodes themselves are initialized to reflect their
2174 * existence within the extent (i.e. their inode numbers and
2175 * inode extent addresses are set) and their initial state
2176 * (mode and link count are set to zero).
2178 * if the iag is new, it is not yet on an ag extent free list
2179 * but will now be placed on this list.
2181 * if the allocation of the new extent causes the iag to
2182 * have no free extent, the iag will be removed from the
2183 * ag extent free list.
2185 * if the iag has no free backed inodes, it will be placed
2186 * on the ag free inode list, since the addition of the new
2187 * extent will now cause it to have free inodes.
2189 * a careful update approach is used to provide consistency
2190 * (i.e. list consistency) in the face of updates to multiple
2191 * buffers. under this approach, all required buffers are
2192 * obtained before making any updates and are held until all
2193 * updates are complete.
2195 * PRE CONDITION: Already have buffer lock on iagp. Already have AG lock on
2196 * this AG. Must have read lock on imap inode.
2199 * imap - pointer to inode map control structure.
2200 * iagp - pointer to iag.
2201 * extno - extent number.
2205 * -ENOSPC - insufficient disk resources.
2208 static int diNewExt(struct inomap * imap, struct iag * iagp, int extno)
2210 int agno, iagno, fwd, back, freei = 0, sword, rc;
2211 struct iag *aiagp = 0, *biagp = 0, *ciagp = 0;
2212 struct metapage *amp, *bmp, *cmp, *dmp;
2213 struct inode *ipimap;
2219 struct jfs_sb_info *sbi;
2221 /* better have free extents.
2223 if (!iagp->nfreeexts) {
2224 jfs_error(imap->im_ipimap->i_sb, "diNewExt: no free extents");
2228 /* get the inode map inode.
2230 ipimap = imap->im_ipimap;
2231 sbi = JFS_SBI(ipimap->i_sb);
2233 amp = bmp = cmp = NULL;
2235 /* get the ag and iag numbers for this iag.
2237 agno = BLKTOAG(le64_to_cpu(iagp->agstart), sbi);
2238 iagno = le32_to_cpu(iagp->iagnum);
2240 /* check if this is the last free extent within the
2241 * iag. if so, the iag must be removed from the ag
2242 * free extent list, so get the iags preceeding and
2243 * following the iag on this list.
2245 if (iagp->nfreeexts == cpu_to_le32(1)) {
2246 if ((fwd = le32_to_cpu(iagp->extfreefwd)) >= 0) {
2247 if ((rc = diIAGRead(imap, fwd, &)))
2249 aiagp = (struct iag *) amp->data;
2252 if ((back = le32_to_cpu(iagp->extfreeback)) >= 0) {
2253 if ((rc = diIAGRead(imap, back, &bmp)))
2255 biagp = (struct iag *) bmp->data;
2258 /* the iag has free extents. if all extents are free
2259 * (as is the case for a newly allocated iag), the iag
2260 * must be added to the ag free extent list, so get
2261 * the iag at the head of the list in preparation for
2262 * adding this iag to this list.
2265 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2266 if ((fwd = imap->im_agctl[agno].extfree) >= 0) {
2267 if ((rc = diIAGRead(imap, fwd, &)))
2269 aiagp = (struct iag *) amp->data;
2274 /* check if the iag has no free inodes. if so, the iag
2275 * will have to be added to the ag free inode list, so get
2276 * the iag at the head of the list in preparation for
2277 * adding this iag to this list. in doing this, we must
2278 * check if we already have the iag at the head of
2281 if (iagp->nfreeinos == 0) {
2282 freei = imap->im_agctl[agno].inofree;
2287 } else if (freei == back) {
2290 if ((rc = diIAGRead(imap, freei, &cmp)))
2292 ciagp = (struct iag *) cmp->data;
2294 if (ciagp == NULL) {
2295 jfs_error(imap->im_ipimap->i_sb,
2296 "diNewExt: ciagp == NULL");
2303 /* allocate disk space for the inode extent.
2305 if ((extno == 0) || (addressPXD(&iagp->inoext[extno - 1]) == 0))
2306 hint = ((s64) agno << sbi->bmap->db_agl2size) - 1;
2308 hint = addressPXD(&iagp->inoext[extno - 1]) +
2309 lengthPXD(&iagp->inoext[extno - 1]) - 1;
2311 if ((rc = dbAlloc(ipimap, hint, (s64) imap->im_nbperiext, &blkno)))
2314 /* compute the inode number of the first inode within the
2317 ino = (iagno << L2INOSPERIAG) + (extno << L2INOSPEREXT);
2319 /* initialize the inodes within the newly allocated extent a
2322 for (i = 0; i < imap->im_nbperiext; i += sbi->nbperpage) {
2323 /* get a buffer for this page of disk inodes.
2325 dmp = get_metapage(ipimap, blkno + i, PSIZE, 1);
2330 dp = (struct dinode *) dmp->data;
2332 /* initialize the inode number, mode, link count and
2333 * inode extent address.
2335 for (j = 0; j < INOSPERPAGE; j++, dp++, ino++) {
2336 dp->di_inostamp = cpu_to_le32(sbi->inostamp);
2337 dp->di_number = cpu_to_le32(ino);
2338 dp->di_fileset = cpu_to_le32(FILESYSTEM_I);
2341 PXDaddress(&(dp->di_ixpxd), blkno);
2342 PXDlength(&(dp->di_ixpxd), imap->im_nbperiext);
2344 write_metapage(dmp);
2347 /* if this is the last free extent within the iag, remove the
2348 * iag from the ag free extent list.
2350 if (iagp->nfreeexts == cpu_to_le32(1)) {
2352 aiagp->extfreeback = iagp->extfreeback;
2355 biagp->extfreefwd = iagp->extfreefwd;
2357 imap->im_agctl[agno].extfree =
2358 le32_to_cpu(iagp->extfreefwd);
2360 iagp->extfreefwd = iagp->extfreeback = -1;
2362 /* if the iag has all free extents (newly allocated iag),
2363 * add the iag to the ag free extent list.
2365 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2367 aiagp->extfreeback = cpu_to_le32(iagno);
2369 iagp->extfreefwd = cpu_to_le32(fwd);
2370 iagp->extfreeback = -1;
2371 imap->im_agctl[agno].extfree = iagno;
2375 /* if the iag has no free inodes, add the iag to the
2376 * ag free inode list.
2378 if (iagp->nfreeinos == 0) {
2380 ciagp->inofreeback = cpu_to_le32(iagno);
2383 cpu_to_le32(imap->im_agctl[agno].inofree);
2384 iagp->inofreeback = -1;
2385 imap->im_agctl[agno].inofree = iagno;
2388 /* initialize the extent descriptor of the extent. */
2389 PXDlength(&iagp->inoext[extno], imap->im_nbperiext);
2390 PXDaddress(&iagp->inoext[extno], blkno);
2392 /* initialize the working and persistent map of the extent.
2393 * the working map will be initialized such that
2394 * it indicates the first inode of the extent is allocated.
2396 iagp->wmap[extno] = cpu_to_le32(HIGHORDER);
2397 iagp->pmap[extno] = 0;
2399 /* update the free inode and free extent summary maps
2400 * for the extent to indicate the extent has free inodes
2401 * and no longer represents a free extent.
2403 sword = extno >> L2EXTSPERSUM;
2404 mask = HIGHORDER >> (extno & (EXTSPERSUM - 1));
2405 iagp->extsmap[sword] |= cpu_to_le32(mask);
2406 iagp->inosmap[sword] &= cpu_to_le32(~mask);
2408 /* update the free inode and free extent counts for the
2411 iagp->nfreeinos = cpu_to_le32(le32_to_cpu(iagp->nfreeinos) +
2413 iagp->nfreeexts = cpu_to_le32(le32_to_cpu(iagp->nfreeexts) - 1);
2415 /* update the free and backed inode counts for the ag.
2417 imap->im_agctl[agno].numfree += (INOSPEREXT - 1);
2418 imap->im_agctl[agno].numinos += INOSPEREXT;
2420 /* update the free and backed inode counts for the inode map.
2422 atomic_add(INOSPEREXT - 1, &imap->im_numfree);
2423 atomic_add(INOSPEREXT, &imap->im_numinos);
2428 write_metapage(amp);
2430 write_metapage(bmp);
2432 write_metapage(cmp);
2438 /* release the iags.
2441 release_metapage(amp);
2443 release_metapage(bmp);
2445 release_metapage(cmp);
2452 * NAME: diNewIAG(imap,iagnop,agno)
2454 * FUNCTION: allocate a new iag for an allocation group.
2456 * first tries to allocate the iag from the inode map
2458 * if the list has free iags, the head of the list is removed
2459 * and returned to satisfy the request.
2460 * if the inode map's iag free list is empty, the inode map
2461 * is extended to hold a new iag. this new iag is initialized
2462 * and returned to satisfy the request.
2465 * imap - pointer to inode map control structure.
2466 * iagnop - pointer to an iag number set with the number of the
2467 * newly allocated iag upon successful return.
2468 * agno - allocation group number.
2469 * bpp - Buffer pointer to be filled in with new IAG's buffer
2473 * -ENOSPC - insufficient disk resources.
2477 * AG lock held on entry/exit;
2478 * write lock on the map is held inside;
2479 * read lock on the map is held on successful completion;
2481 * note: new iag transaction:
2482 * . synchronously write iag;
2483 * . write log of xtree and inode of imap;
2485 * . synchronous write of xtree (right to left, bottom to top);
2486 * . at start of logredo(): init in-memory imap with one additional iag page;
2487 * . at end of logredo(): re-read imap inode to determine
2491 diNewIAG(struct inomap * imap, int *iagnop, int agno, struct metapage ** mpp)
2495 struct inode *ipimap;
2496 struct super_block *sb;
2497 struct jfs_sb_info *sbi;
2498 struct metapage *mp;
2503 #ifdef _STILL_TO_PORT
2505 #endif /* _STILL_TO_PORT */
2506 struct inode *iplist[1];
2508 /* pick up pointers to the inode map and mount inodes */
2509 ipimap = imap->im_ipimap;
2513 /* acquire the free iag lock */
2516 /* if there are any iags on the inode map free iag list,
2517 * allocate the iag from the head of the list.
2519 if (imap->im_freeiag >= 0) {
2520 /* pick up the iag number at the head of the list */
2521 iagno = imap->im_freeiag;
2523 /* determine the logical block number of the iag */
2524 blkno = IAGTOLBLK(iagno, sbi->l2nbperpage);
2526 /* no free iags. the inode map will have to be extented
2527 * to include a new iag.
2530 /* acquire inode map lock */
2531 IWRITE_LOCK(ipimap);
2533 if (ipimap->i_size >> L2PSIZE != imap->im_nextiag + 1) {
2534 IWRITE_UNLOCK(ipimap);
2535 IAGFREE_UNLOCK(imap);
2536 jfs_error(imap->im_ipimap->i_sb,
2537 "diNewIAG: ipimap->i_size is wrong");
2542 /* get the next avaliable iag number */
2543 iagno = imap->im_nextiag;
2545 /* make sure that we have not exceeded the maximum inode
2548 if (iagno > (MAXIAGS - 1)) {
2549 /* release the inode map lock */
2550 IWRITE_UNLOCK(ipimap);
2557 * synchronously append new iag page.
2559 /* determine the logical address of iag page to append */
2560 blkno = IAGTOLBLK(iagno, sbi->l2nbperpage);
2562 /* Allocate extent for new iag page */
2563 xlen = sbi->nbperpage;
2564 if ((rc = dbAlloc(ipimap, 0, (s64) xlen, &xaddr))) {
2565 /* release the inode map lock */
2566 IWRITE_UNLOCK(ipimap);
2571 /* assign a buffer for the page */
2572 mp = get_metapage(ipimap, xaddr, PSIZE, 1);
2574 /* Free the blocks allocated for the iag since it was
2575 * not successfully added to the inode map
2577 dbFree(ipimap, xaddr, (s64) xlen);
2579 /* release the inode map lock */
2580 IWRITE_UNLOCK(ipimap);
2585 iagp = (struct iag *) mp->data;
2588 memset(iagp, 0, sizeof(struct iag));
2589 iagp->iagnum = cpu_to_le32(iagno);
2590 iagp->inofreefwd = iagp->inofreeback = -1;
2591 iagp->extfreefwd = iagp->extfreeback = -1;
2593 iagp->nfreeinos = 0;
2594 iagp->nfreeexts = cpu_to_le32(EXTSPERIAG);
2596 /* initialize the free inode summary map (free extent
2597 * summary map initialization handled by bzero).
2599 for (i = 0; i < SMAPSZ; i++)
2600 iagp->inosmap[i] = ONES;
2603 #ifdef _STILL_TO_PORT
2604 /* synchronously write the iag page */
2606 /* Free the blocks allocated for the iag since it was
2607 * not successfully added to the inode map
2609 dbFree(ipimap, xaddr, (s64) xlen);
2611 /* release the inode map lock */
2612 IWRITE_UNLOCK(ipimap);
2618 /* Now the iag is on disk */
2621 * start tyransaction of update of the inode map
2622 * addressing structure pointing to the new iag page;
2624 #endif /* _STILL_TO_PORT */
2625 tid = txBegin(sb, COMMIT_FORCE);
2627 /* update the inode map addressing structure to point to it */
2629 xtInsert(tid, ipimap, 0, blkno, xlen, &xaddr, 0))) {
2630 /* Free the blocks allocated for the iag since it was
2631 * not successfully added to the inode map
2633 dbFree(ipimap, xaddr, (s64) xlen);
2635 /* release the inode map lock */
2636 IWRITE_UNLOCK(ipimap);
2641 /* update the inode map's inode to reflect the extension */
2642 ipimap->i_size += PSIZE;
2643 ipimap->i_blocks += LBLK2PBLK(sb, xlen);
2646 * txCommit(COMMIT_FORCE) will synchronously write address
2647 * index pages and inode after commit in careful update order
2648 * of address index pages (right to left, bottom up);
2651 rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
2655 duplicateIXtree(sb, blkno, xlen, &xaddr);
2657 /* update the next avaliable iag number */
2658 imap->im_nextiag += 1;
2660 /* Add the iag to the iag free list so we don't lose the iag
2661 * if a failure happens now.
2663 imap->im_freeiag = iagno;
2665 /* Until we have logredo working, we want the imap inode &
2666 * control page to be up to date.
2670 /* release the inode map lock */
2671 IWRITE_UNLOCK(ipimap);
2674 /* obtain read lock on map */
2678 if ((rc = diIAGRead(imap, iagno, &mp))) {
2679 IREAD_UNLOCK(ipimap);
2683 iagp = (struct iag *) mp->data;
2685 /* remove the iag from the iag free list */
2686 imap->im_freeiag = le32_to_cpu(iagp->iagfree);
2689 /* set the return iag number and buffer pointer */
2694 /* release the iag free lock */
2695 IAGFREE_UNLOCK(imap);
2703 * FUNCTION: get the buffer for the specified iag within a fileset
2704 * or aggregate inode map.
2707 * imap - pointer to inode map control structure.
2708 * iagno - iag number.
2709 * bpp - point to buffer pointer to be filled in on successful
2713 * must have read lock on imap inode
2714 * (When called by diExtendFS, the filesystem is quiesced, therefore
2715 * the read lock is unnecessary.)
2721 static int diIAGRead(struct inomap * imap, int iagno, struct metapage ** mpp)
2723 struct inode *ipimap = imap->im_ipimap;
2726 /* compute the logical block number of the iag. */
2727 blkno = IAGTOLBLK(iagno, JFS_SBI(ipimap->i_sb)->l2nbperpage);
2730 *mpp = read_metapage(ipimap, blkno, PSIZE, 0);
2739 * NAME: diFindFree()
2741 * FUNCTION: find the first free bit in a word starting at
2742 * the specified bit position.
2745 * word - word to be examined.
2746 * start - starting bit position.
2749 * bit position of first free bit in the word or 32 if
2750 * no free bits were found.
2752 static int diFindFree(u32 word, int start)
2756 /* scan the word for the first free bit. */
2757 for (word <<= start, bitno = start; bitno < 32;
2758 bitno++, word <<= 1) {
2759 if ((word & HIGHORDER) == 0)
2766 * NAME: diUpdatePMap()
2768 * FUNCTION: Update the persistent map in an IAG for the allocation or
2769 * freeing of the specified inode.
2771 * PRE CONDITIONS: Working map has already been updated for allocate.
2774 * ipimap - Incore inode map inode
2775 * inum - Number of inode to mark in permanent map
2776 * is_free - If TRUE indicates inode should be marked freed, otherwise
2777 * indicates inode should be marked allocated.
2783 diUpdatePMap(struct inode *ipimap,
2784 unsigned long inum, boolean_t is_free, struct tblock * tblk)
2788 struct metapage *mp;
2789 int iagno, ino, extno, bitno;
2790 struct inomap *imap;
2792 struct jfs_log *log;
2793 int lsn, difft, diffp;
2795 imap = JFS_IP(ipimap)->i_imap;
2796 /* get the iag number containing the inode */
2797 iagno = INOTOIAG(inum);
2798 /* make sure that the iag is contained within the map */
2799 if (iagno >= imap->im_nextiag) {
2800 jfs_error(ipimap->i_sb,
2801 "diUpdatePMap: the iag is outside the map");
2806 rc = diIAGRead(imap, iagno, &mp);
2807 IREAD_UNLOCK(ipimap);
2810 iagp = (struct iag *) mp->data;
2811 /* get the inode number and extent number of the inode within
2812 * the iag and the inode number within the extent.
2814 ino = inum & (INOSPERIAG - 1);
2815 extno = ino >> L2INOSPEREXT;
2816 bitno = ino & (INOSPEREXT - 1);
2817 mask = HIGHORDER >> bitno;
2819 * mark the inode free in persistent map:
2821 if (is_free == TRUE) {
2822 /* The inode should have been allocated both in working
2823 * map and in persistent map;
2824 * the inode will be freed from working map at the release
2825 * of last reference release;
2827 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
2828 jfs_error(ipimap->i_sb,
2829 "diUpdatePMap: inode %ld not marked as "
2830 "allocated in wmap!", inum);
2832 if (!(le32_to_cpu(iagp->pmap[extno]) & mask)) {
2833 jfs_error(ipimap->i_sb,
2834 "diUpdatePMap: inode %ld not marked as "
2835 "allocated in pmap!", inum);
2837 /* update the bitmap for the extent of the freed inode */
2838 iagp->pmap[extno] &= cpu_to_le32(~mask);
2841 * mark the inode allocated in persistent map:
2844 /* The inode should be already allocated in the working map
2845 * and should be free in persistent map;
2847 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
2848 release_metapage(mp);
2849 jfs_error(ipimap->i_sb,
2850 "diUpdatePMap: the inode is not allocated in "
2854 if ((le32_to_cpu(iagp->pmap[extno]) & mask) != 0) {
2855 release_metapage(mp);
2856 jfs_error(ipimap->i_sb,
2857 "diUpdatePMap: the inode is not free in the "
2861 /* update the bitmap for the extent of the allocated inode */
2862 iagp->pmap[extno] |= cpu_to_le32(mask);
2868 log = JFS_SBI(tblk->sb)->log;
2870 /* inherit older/smaller lsn */
2871 logdiff(difft, lsn, log);
2872 logdiff(diffp, mp->lsn, log);
2873 if (difft < diffp) {
2875 /* move mp after tblock in logsync list */
2877 list_move(&mp->synclist, &tblk->synclist);
2878 LOGSYNC_UNLOCK(log);
2880 /* inherit younger/larger clsn */
2883 logdiff(difft, tblk->clsn, log);
2884 logdiff(diffp, mp->clsn, log);
2886 mp->clsn = tblk->clsn;
2887 LOGSYNC_UNLOCK(log);
2891 /* insert mp after tblock in logsync list */
2894 list_add(&mp->synclist, &tblk->synclist);
2895 mp->clsn = tblk->clsn;
2896 LOGSYNC_UNLOCK(log);
2905 * function: update imap for extendfs();
2907 * note: AG size has been increased s.t. each k old contiguous AGs are
2908 * coalesced into a new AG;
2910 int diExtendFS(struct inode *ipimap, struct inode *ipbmap)
2913 struct inomap *imap = JFS_IP(ipimap)->i_imap;
2914 struct iag *iagp = 0, *hiagp = 0;
2915 struct bmap *mp = JFS_SBI(ipbmap->i_sb)->bmap;
2916 struct metapage *bp, *hbp;
2918 int numinos, xnuminos = 0, xnumfree = 0;
2921 jfs_info("diExtendFS: nextiag:%d numinos:%d numfree:%d",
2922 imap->im_nextiag, atomic_read(&imap->im_numinos),
2923 atomic_read(&imap->im_numfree));
2928 * coalesce contiguous k (newAGSize/oldAGSize) AGs;
2929 * i.e., (AGi, ..., AGj) where i = k*n and j = k*(n+1) - 1 to AGn;
2930 * note: new AG size = old AG size * (2**x).
2933 /* init per AG control information im_agctl[] */
2934 for (i = 0; i < MAXAG; i++) {
2935 imap->im_agctl[i].inofree = -1; /* free inode list */
2936 imap->im_agctl[i].extfree = -1; /* free extent list */
2937 imap->im_agctl[i].numinos = 0; /* number of backed inodes */
2938 imap->im_agctl[i].numfree = 0; /* number of free backed inodes */
2942 * process each iag page of the map.
2944 * rebuild AG Free Inode List, AG Free Inode Extent List;
2946 for (i = 0; i < imap->im_nextiag; i++) {
2947 if ((rc = diIAGRead(imap, i, &bp))) {
2951 iagp = (struct iag *) bp->data;
2952 if (le32_to_cpu(iagp->iagnum) != i) {
2953 release_metapage(bp);
2954 jfs_error(ipimap->i_sb,
2955 "diExtendFs: unexpected value of iagnum");
2959 /* leave free iag in the free iag list */
2960 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2961 release_metapage(bp);
2965 /* agstart that computes to the same ag is treated as same; */
2966 agstart = le64_to_cpu(iagp->agstart);
2967 /* iagp->agstart = agstart & ~(mp->db_agsize - 1); */
2968 n = agstart >> mp->db_agl2size;
2970 /* compute backed inodes */
2971 numinos = (EXTSPERIAG - le32_to_cpu(iagp->nfreeexts))
2974 /* merge AG backed inodes */
2975 imap->im_agctl[n].numinos += numinos;
2976 xnuminos += numinos;
2979 /* if any backed free inodes, insert at AG free inode list */
2980 if ((int) le32_to_cpu(iagp->nfreeinos) > 0) {
2981 if ((head = imap->im_agctl[n].inofree) == -1)
2982 iagp->inofreefwd = iagp->inofreeback = -1;
2984 if ((rc = diIAGRead(imap, head, &hbp))) {
2988 hiagp = (struct iag *) hbp->data;
2989 hiagp->inofreeback =
2990 le32_to_cpu(iagp->iagnum);
2991 iagp->inofreefwd = cpu_to_le32(head);
2992 iagp->inofreeback = -1;
2993 write_metapage(hbp);
2996 imap->im_agctl[n].inofree =
2997 le32_to_cpu(iagp->iagnum);
2999 /* merge AG backed free inodes */
3000 imap->im_agctl[n].numfree +=
3001 le32_to_cpu(iagp->nfreeinos);
3002 xnumfree += le32_to_cpu(iagp->nfreeinos);
3005 /* if any free extents, insert at AG free extent list */
3006 if (le32_to_cpu(iagp->nfreeexts) > 0) {
3007 if ((head = imap->im_agctl[n].extfree) == -1)
3008 iagp->extfreefwd = iagp->extfreeback = -1;
3010 if ((rc = diIAGRead(imap, head, &hbp))) {
3014 hiagp = (struct iag *) hbp->data;
3015 hiagp->extfreeback = iagp->iagnum;
3016 iagp->extfreefwd = cpu_to_le32(head);
3017 iagp->extfreeback = -1;
3018 write_metapage(hbp);
3021 imap->im_agctl[n].extfree =
3022 le32_to_cpu(iagp->iagnum);
3029 if (xnuminos != atomic_read(&imap->im_numinos) ||
3030 xnumfree != atomic_read(&imap->im_numfree)) {
3031 jfs_error(ipimap->i_sb,
3032 "diExtendFs: numinos or numfree incorrect");
3043 * serialization: IWRITE_LOCK held on entry/exit
3045 * note: shadow page with regular inode (rel.2);
3047 static void duplicateIXtree(struct super_block *sb, s64 blkno,
3048 int xlen, s64 *xaddr)
3050 struct jfs_superblock *j_sb;
3051 struct buffer_head *bh;
3055 /* if AIT2 ipmap2 is bad, do not try to update it */
3056 if (JFS_SBI(sb)->mntflag & JFS_BAD_SAIT) /* s_flag */
3058 ip = diReadSpecial(sb, FILESYSTEM_I, 1);
3060 JFS_SBI(sb)->mntflag |= JFS_BAD_SAIT;
3061 if (readSuper(sb, &bh))
3063 j_sb = (struct jfs_superblock *)bh->b_data;
3064 j_sb->s_flag |= JFS_BAD_SAIT;
3066 mark_buffer_dirty(bh);
3067 sync_dirty_buffer(bh);
3072 /* start transaction */
3073 tid = txBegin(sb, COMMIT_FORCE);
3074 /* update the inode map addressing structure to point to it */
3075 if (xtInsert(tid, ip, 0, blkno, xlen, xaddr, 0)) {
3076 JFS_SBI(sb)->mntflag |= JFS_BAD_SAIT;
3081 /* update the inode map's inode to reflect the extension */
3082 ip->i_size += PSIZE;
3083 ip->i_blocks += LBLK2PBLK(sb, xlen);
3084 txCommit(tid, 1, &ip, COMMIT_FORCE);
3091 * NAME: copy_from_dinode()
3093 * FUNCTION: Copies inode info from disk inode to in-memory inode
3097 * -ENOMEM - insufficient memory
3099 static int copy_from_dinode(struct dinode * dip, struct inode *ip)
3101 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
3105 jfs_ip->fileset = le32_to_cpu(dip->di_fileset);
3106 jfs_ip->mode2 = le32_to_cpu(dip->di_mode);
3108 ip->i_mode = le32_to_cpu(dip->di_mode) & 0xffff;
3109 ip->i_nlink = le32_to_cpu(dip->di_nlink);
3111 uid = le32_to_cpu(dip->di_uid);
3112 gid = le32_to_cpu(dip->di_gid);
3113 ip->i_uid = INOXID_UID(uid, gid);
3114 ip->i_gid = INOXID_GID(uid, gid);
3115 ip->i_xid = INOXID_XID(uid, gid, 0);
3117 ip->i_size = le64_to_cpu(dip->di_size);
3118 ip->i_atime.tv_sec = le32_to_cpu(dip->di_atime.tv_sec);
3119 ip->i_atime.tv_nsec = le32_to_cpu(dip->di_atime.tv_nsec);
3120 ip->i_mtime.tv_sec = le32_to_cpu(dip->di_mtime.tv_sec);
3121 ip->i_mtime.tv_nsec = le32_to_cpu(dip->di_mtime.tv_nsec);
3122 ip->i_ctime.tv_sec = le32_to_cpu(dip->di_ctime.tv_sec);
3123 ip->i_ctime.tv_nsec = le32_to_cpu(dip->di_ctime.tv_nsec);
3124 ip->i_blksize = ip->i_sb->s_blocksize;
3125 ip->i_blocks = LBLK2PBLK(ip->i_sb, le64_to_cpu(dip->di_nblocks));
3126 ip->i_generation = le32_to_cpu(dip->di_gen);
3128 jfs_ip->ixpxd = dip->di_ixpxd; /* in-memory pxd's are little-endian */
3129 jfs_ip->acl = dip->di_acl; /* as are dxd's */
3130 jfs_ip->ea = dip->di_ea;
3131 jfs_ip->next_index = le32_to_cpu(dip->di_next_index);
3132 jfs_ip->otime = le32_to_cpu(dip->di_otime.tv_sec);
3133 jfs_ip->acltype = le32_to_cpu(dip->di_acltype);
3135 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode)) {
3136 jfs_ip->dev = le32_to_cpu(dip->di_rdev);
3137 ip->i_rdev = new_decode_dev(jfs_ip->dev);
3140 if (S_ISDIR(ip->i_mode)) {
3141 memcpy(&jfs_ip->i_dirtable, &dip->di_dirtable, 384);
3142 } else if (S_ISREG(ip->i_mode) || S_ISLNK(ip->i_mode)) {
3143 memcpy(&jfs_ip->i_xtroot, &dip->di_xtroot, 288);
3145 memcpy(&jfs_ip->i_inline_ea, &dip->di_inlineea, 128);
3147 /* Zero the in-memory-only stuff */
3149 jfs_ip->btindex = 0;
3150 jfs_ip->btorder = 0;
3153 jfs_ip->atlhead = 0;
3154 jfs_ip->atltail = 0;
3160 * NAME: copy_to_dinode()
3162 * FUNCTION: Copies inode info from in-memory inode to disk inode
3164 static void copy_to_dinode(struct dinode * dip, struct inode *ip)
3166 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
3170 dip->di_fileset = cpu_to_le32(jfs_ip->fileset);
3171 dip->di_inostamp = cpu_to_le32(JFS_SBI(ip->i_sb)->inostamp);
3172 dip->di_number = cpu_to_le32(ip->i_ino);
3173 dip->di_gen = cpu_to_le32(ip->i_generation);
3174 dip->di_size = cpu_to_le64(ip->i_size);
3175 dip->di_nblocks = cpu_to_le64(PBLK2LBLK(ip->i_sb, ip->i_blocks));
3176 dip->di_nlink = cpu_to_le32(ip->i_nlink);
3178 uid = XIDINO_UID(ip->i_uid, ip->i_xid);
3179 gid = XIDINO_GID(ip->i_gid, ip->i_xid);
3180 dip->di_uid = cpu_to_le32(uid);
3181 dip->di_gid = cpu_to_le32(gid);
3183 * mode2 is only needed for storing the higher order bits.
3184 * Trust i_mode for the lower order ones
3186 dip->di_mode = cpu_to_le32((jfs_ip->mode2 & 0xffff0000) | ip->i_mode);
3187 dip->di_atime.tv_sec = cpu_to_le32(ip->i_atime.tv_sec);
3188 dip->di_atime.tv_nsec = cpu_to_le32(ip->i_atime.tv_nsec);
3189 dip->di_ctime.tv_sec = cpu_to_le32(ip->i_ctime.tv_sec);
3190 dip->di_ctime.tv_nsec = cpu_to_le32(ip->i_ctime.tv_nsec);
3191 dip->di_mtime.tv_sec = cpu_to_le32(ip->i_mtime.tv_sec);
3192 dip->di_mtime.tv_nsec = cpu_to_le32(ip->i_mtime.tv_nsec);
3193 dip->di_ixpxd = jfs_ip->ixpxd; /* in-memory pxd's are little-endian */
3194 dip->di_acl = jfs_ip->acl; /* as are dxd's */
3195 dip->di_ea = jfs_ip->ea;
3196 dip->di_next_index = cpu_to_le32(jfs_ip->next_index);
3197 dip->di_otime.tv_sec = cpu_to_le32(jfs_ip->otime);
3198 dip->di_otime.tv_nsec = 0;
3199 dip->di_acltype = cpu_to_le32(jfs_ip->acltype);
3200 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode))
3201 dip->di_rdev = cpu_to_le32(jfs_ip->dev);
3204 #ifdef _JFS_DEBUG_IMAP
3208 static void *DBGdiInit(struct inomap * imap)
3213 if ((dimap = (u32 *) xmalloc(size, L2PSIZE, kernel_heap)) == NULL)
3215 bzero((void *) dimap, size);
3216 imap->im_DBGdimap = dimap;
3222 static void DBGdiAlloc(struct inomap * imap, ino_t ino)
3224 u32 *dimap = imap->im_DBGdimap;
3229 m = 0x80000000 >> b;
3230 assert(w < 64 * 256);
3232 printk("DEBUG diAlloc: duplicate alloc ino:0x%x\n", ino);
3240 static void DBGdiFree(struct inomap * imap, ino_t ino)
3242 u32 *dimap = imap->im_DBGdimap;
3247 m = 0x80000000 >> b;
3248 assert(w < 64 * 256);
3249 if ((dimap[w] & m) == 0) {
3250 printk("DEBUG diFree: duplicate free ino:0x%x\n", ino);
3255 static void dump_cp(struct inomap * ipimap, char *function, int line)
3257 printk("\n* ********* *\nControl Page %s %d\n", function, line);
3258 printk("FreeIAG %d\tNextIAG %d\n", ipimap->im_freeiag,
3259 ipimap->im_nextiag);
3260 printk("NumInos %d\tNumFree %d\n",
3261 atomic_read(&ipimap->im_numinos),
3262 atomic_read(&ipimap->im_numfree));
3263 printk("AG InoFree %d\tAG ExtFree %d\n",
3264 ipimap->im_agctl[0].inofree, ipimap->im_agctl[0].extfree);
3265 printk("AG NumInos %d\tAG NumFree %d\n",
3266 ipimap->im_agctl[0].numinos, ipimap->im_agctl[0].numfree);
3269 static void dump_iag(struct iag * iag, char *function, int line)
3271 printk("\n* ********* *\nIAG %s %d\n", function, line);
3272 printk("IagNum %d\tIAG Free %d\n", le32_to_cpu(iag->iagnum),
3273 le32_to_cpu(iag->iagfree));
3274 printk("InoFreeFwd %d\tInoFreeBack %d\n",
3275 le32_to_cpu(iag->inofreefwd),
3276 le32_to_cpu(iag->inofreeback));
3277 printk("ExtFreeFwd %d\tExtFreeBack %d\n",
3278 le32_to_cpu(iag->extfreefwd),
3279 le32_to_cpu(iag->extfreeback));
3280 printk("NFreeInos %d\tNFreeExts %d\n", le32_to_cpu(iag->nfreeinos),
3281 le32_to_cpu(iag->nfreeexts));
3283 #endif /* _JFS_DEBUG_IMAP */