1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) International Business Machines Corp., 2000-2004
7 * jfs_imap.c: inode allocation map manager
10 * Each AG has a simple lock which is used to control the serialization of
11 * the AG level lists. This lock should be taken first whenever an AG
12 * level list will be modified or accessed.
14 * Each IAG is locked by obtaining the buffer for the IAG page.
16 * There is also a inode lock for the inode map inode. A read lock needs to
17 * be taken whenever an IAG is read from the map or the global level
18 * information is read. A write lock needs to be taken whenever the global
19 * level information is modified or an atomic operation needs to be used.
21 * If more than one IAG is read at one time, the read lock may not
22 * be given up until all of the IAG's are read. Otherwise, a deadlock
23 * may occur when trying to obtain the read lock while another thread
24 * holding the read lock is waiting on the IAG already being held.
26 * The control page of the inode map is read into memory by diMount().
27 * Thereafter it should only be modified in memory and then it will be
28 * written out when the filesystem is unmounted by diUnmount().
32 #include <linux/buffer_head.h>
33 #include <linux/pagemap.h>
34 #include <linux/quotaops.h>
35 #include <linux/slab.h>
37 #include "jfs_incore.h"
38 #include "jfs_inode.h"
39 #include "jfs_filsys.h"
40 #include "jfs_dinode.h"
43 #include "jfs_metapage.h"
44 #include "jfs_superblock.h"
45 #include "jfs_debug.h"
50 /* iag free list lock */
51 #define IAGFREE_LOCK_INIT(imap) mutex_init(&imap->im_freelock)
52 #define IAGFREE_LOCK(imap) mutex_lock(&imap->im_freelock)
53 #define IAGFREE_UNLOCK(imap) mutex_unlock(&imap->im_freelock)
55 /* per ag iag list locks */
56 #define AG_LOCK_INIT(imap,index) mutex_init(&(imap->im_aglock[index]))
57 #define AG_LOCK(imap,agno) mutex_lock(&imap->im_aglock[agno])
58 #define AG_UNLOCK(imap,agno) mutex_unlock(&imap->im_aglock[agno])
63 static int diAllocAG(struct inomap *, int, bool, struct inode *);
64 static int diAllocAny(struct inomap *, int, bool, struct inode *);
65 static int diAllocBit(struct inomap *, struct iag *, int);
66 static int diAllocExt(struct inomap *, int, struct inode *);
67 static int diAllocIno(struct inomap *, int, struct inode *);
68 static int diFindFree(u32, int);
69 static int diNewExt(struct inomap *, struct iag *, int);
70 static int diNewIAG(struct inomap *, int *, int, struct metapage **);
71 static void duplicateIXtree(struct super_block *, s64, int, s64 *);
73 static int diIAGRead(struct inomap * imap, int, struct metapage **);
74 static int copy_from_dinode(struct dinode *, struct inode *);
75 static void copy_to_dinode(struct dinode *, struct inode *);
80 * FUNCTION: initialize the incore inode map control structures for
81 * a fileset or aggregate init time.
83 * the inode map's control structure (dinomap) is
84 * brought in from disk and placed in virtual memory.
87 * ipimap - pointer to inode map inode for the aggregate or fileset.
91 * -ENOMEM - insufficient free virtual memory.
94 int diMount(struct inode *ipimap)
99 struct dinomap_disk *dinom_le;
102 * allocate/initialize the in-memory inode map control structure
104 /* allocate the in-memory inode map control structure. */
105 imap = kmalloc(sizeof(struct inomap), GFP_KERNEL);
109 /* read the on-disk inode map control structure. */
111 mp = read_metapage(ipimap,
112 IMAPBLKNO << JFS_SBI(ipimap->i_sb)->l2nbperpage,
119 /* copy the on-disk version to the in-memory version. */
120 dinom_le = (struct dinomap_disk *) mp->data;
121 imap->im_freeiag = le32_to_cpu(dinom_le->in_freeiag);
122 imap->im_nextiag = le32_to_cpu(dinom_le->in_nextiag);
123 atomic_set(&imap->im_numinos, le32_to_cpu(dinom_le->in_numinos));
124 atomic_set(&imap->im_numfree, le32_to_cpu(dinom_le->in_numfree));
125 imap->im_nbperiext = le32_to_cpu(dinom_le->in_nbperiext);
126 imap->im_l2nbperiext = le32_to_cpu(dinom_le->in_l2nbperiext);
127 for (index = 0; index < MAXAG; index++) {
128 imap->im_agctl[index].inofree =
129 le32_to_cpu(dinom_le->in_agctl[index].inofree);
130 imap->im_agctl[index].extfree =
131 le32_to_cpu(dinom_le->in_agctl[index].extfree);
132 imap->im_agctl[index].numinos =
133 le32_to_cpu(dinom_le->in_agctl[index].numinos);
134 imap->im_agctl[index].numfree =
135 le32_to_cpu(dinom_le->in_agctl[index].numfree);
138 /* release the buffer. */
139 release_metapage(mp);
142 * allocate/initialize inode allocation map locks
144 /* allocate and init iag free list lock */
145 IAGFREE_LOCK_INIT(imap);
147 /* allocate and init ag list locks */
148 for (index = 0; index < MAXAG; index++) {
149 AG_LOCK_INIT(imap, index);
152 /* bind the inode map inode and inode map control structure
155 imap->im_ipimap = ipimap;
156 JFS_IP(ipimap)->i_imap = imap;
165 * FUNCTION: write to disk the incore inode map control structures for
166 * a fileset or aggregate at unmount time.
169 * ipimap - pointer to inode map inode for the aggregate or fileset.
173 * -ENOMEM - insufficient free virtual memory.
176 int diUnmount(struct inode *ipimap, int mounterror)
178 struct inomap *imap = JFS_IP(ipimap)->i_imap;
181 * update the on-disk inode map control structure
184 if (!(mounterror || isReadOnly(ipimap)))
188 * Invalidate the page cache buffers
190 truncate_inode_pages(ipimap->i_mapping, 0);
193 * free in-memory control structure
196 JFS_IP(ipimap)->i_imap = NULL;
205 int diSync(struct inode *ipimap)
207 struct dinomap_disk *dinom_le;
208 struct inomap *imp = JFS_IP(ipimap)->i_imap;
213 * write imap global conrol page
215 /* read the on-disk inode map control structure */
216 mp = get_metapage(ipimap,
217 IMAPBLKNO << JFS_SBI(ipimap->i_sb)->l2nbperpage,
220 jfs_err("diSync: get_metapage failed!");
224 /* copy the in-memory version to the on-disk version */
225 dinom_le = (struct dinomap_disk *) mp->data;
226 dinom_le->in_freeiag = cpu_to_le32(imp->im_freeiag);
227 dinom_le->in_nextiag = cpu_to_le32(imp->im_nextiag);
228 dinom_le->in_numinos = cpu_to_le32(atomic_read(&imp->im_numinos));
229 dinom_le->in_numfree = cpu_to_le32(atomic_read(&imp->im_numfree));
230 dinom_le->in_nbperiext = cpu_to_le32(imp->im_nbperiext);
231 dinom_le->in_l2nbperiext = cpu_to_le32(imp->im_l2nbperiext);
232 for (index = 0; index < MAXAG; index++) {
233 dinom_le->in_agctl[index].inofree =
234 cpu_to_le32(imp->im_agctl[index].inofree);
235 dinom_le->in_agctl[index].extfree =
236 cpu_to_le32(imp->im_agctl[index].extfree);
237 dinom_le->in_agctl[index].numinos =
238 cpu_to_le32(imp->im_agctl[index].numinos);
239 dinom_le->in_agctl[index].numfree =
240 cpu_to_le32(imp->im_agctl[index].numfree);
243 /* write out the control structure */
247 * write out dirty pages of imap
249 filemap_write_and_wait(ipimap->i_mapping);
251 diWriteSpecial(ipimap, 0);
260 * FUNCTION: initialize an incore inode from disk.
262 * on entry, the specifed incore inode should itself
263 * specify the disk inode number corresponding to the
264 * incore inode (i.e. i_number should be initialized).
266 * this routine handles incore inode initialization for
267 * both "special" and "regular" inodes. special inodes
268 * are those required early in the mount process and
269 * require special handling since much of the file system
270 * is not yet initialized. these "special" inodes are
271 * identified by a NULL inode map inode pointer and are
272 * actually initialized by a call to diReadSpecial().
274 * for regular inodes, the iag describing the disk inode
275 * is read from disk to determine the inode extent address
276 * for the disk inode. with the inode extent address in
277 * hand, the page of the extent that contains the disk
278 * inode is read and the disk inode is copied to the
282 * ip - pointer to incore inode to be initialized from disk.
287 * -ENOMEM - insufficient memory
290 int diRead(struct inode *ip)
292 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
293 int iagno, ino, extno, rc;
294 struct inode *ipimap;
302 unsigned long pageno;
305 jfs_info("diRead: ino = %ld", ip->i_ino);
307 ipimap = sbi->ipimap;
308 JFS_IP(ip)->ipimap = ipimap;
310 /* determine the iag number for this inode (number) */
311 iagno = INOTOIAG(ip->i_ino);
314 IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
315 imap = JFS_IP(ipimap)->i_imap;
316 rc = diIAGRead(imap, iagno, &mp);
317 IREAD_UNLOCK(ipimap);
319 jfs_err("diRead: diIAGRead returned %d", rc);
323 iagp = (struct iag *) mp->data;
325 /* determine inode extent that holds the disk inode */
326 ino = ip->i_ino & (INOSPERIAG - 1);
327 extno = ino >> L2INOSPEREXT;
329 if ((lengthPXD(&iagp->inoext[extno]) != imap->im_nbperiext) ||
330 (addressPXD(&iagp->inoext[extno]) == 0)) {
331 release_metapage(mp);
335 /* get disk block number of the page within the inode extent
336 * that holds the disk inode.
338 blkno = INOPBLK(&iagp->inoext[extno], ino, sbi->l2nbperpage);
340 /* get the ag for the iag */
341 agstart = le64_to_cpu(iagp->agstart);
343 release_metapage(mp);
345 rel_inode = (ino & (INOSPERPAGE - 1));
346 pageno = blkno >> sbi->l2nbperpage;
348 if ((block_offset = ((u32) blkno & (sbi->nbperpage - 1)))) {
350 * OS/2 didn't always align inode extents on page boundaries
353 (sbi->nbperpage - block_offset) << sbi->l2niperblk;
355 if (rel_inode < inodes_left)
356 rel_inode += block_offset << sbi->l2niperblk;
359 rel_inode -= inodes_left;
363 /* read the page of disk inode */
364 mp = read_metapage(ipimap, pageno << sbi->l2nbperpage, PSIZE, 1);
366 jfs_err("diRead: read_metapage failed");
370 /* locate the disk inode requested */
371 dp = (struct dinode *) mp->data;
374 if (ip->i_ino != le32_to_cpu(dp->di_number)) {
375 jfs_error(ip->i_sb, "i_ino != di_number\n");
377 } else if (le32_to_cpu(dp->di_nlink) == 0)
380 /* copy the disk inode to the in-memory inode */
381 rc = copy_from_dinode(dp, ip);
383 release_metapage(mp);
385 /* set the ag for the inode */
386 JFS_IP(ip)->agstart = agstart;
387 JFS_IP(ip)->active_ag = -1;
394 * NAME: diReadSpecial()
396 * FUNCTION: initialize a 'special' inode from disk.
398 * this routines handles aggregate level inodes. The
399 * inode cache cannot differentiate between the
400 * aggregate inodes and the filesystem inodes, so we
401 * handle these here. We don't actually use the aggregate
402 * inode map, since these inodes are at a fixed location
403 * and in some cases the aggregate inode map isn't initialized
407 * sb - filesystem superblock
408 * inum - aggregate inode number
409 * secondary - 1 if secondary aggregate inode table
412 * new inode - success
415 struct inode *diReadSpecial(struct super_block *sb, ino_t inum, int secondary)
417 struct jfs_sb_info *sbi = JFS_SBI(sb);
425 jfs_err("diReadSpecial: new_inode returned NULL!");
430 address = addressPXD(&sbi->ait2) >> sbi->l2nbperpage;
431 JFS_IP(ip)->ipimap = sbi->ipaimap2;
433 address = AITBL_OFF >> L2PSIZE;
434 JFS_IP(ip)->ipimap = sbi->ipaimap;
437 ASSERT(inum < INOSPEREXT);
441 address += inum >> 3; /* 8 inodes per 4K page */
443 /* read the page of fixed disk inode (AIT) in raw mode */
444 mp = read_metapage(ip, address << sbi->l2nbperpage, PSIZE, 1);
446 set_nlink(ip, 1); /* Don't want iput() deleting it */
451 /* get the pointer to the disk inode of interest */
452 dp = (struct dinode *) (mp->data);
453 dp += inum % 8; /* 8 inodes per 4K page */
455 /* copy on-disk inode to in-memory inode */
456 if ((copy_from_dinode(dp, ip)) != 0) {
457 /* handle bad return by returning NULL for ip */
458 set_nlink(ip, 1); /* Don't want iput() deleting it */
460 /* release the page */
461 release_metapage(mp);
466 ip->i_mapping->a_ops = &jfs_metapage_aops;
467 mapping_set_gfp_mask(ip->i_mapping, GFP_NOFS);
469 /* Allocations to metadata inodes should not affect quotas */
470 ip->i_flags |= S_NOQUOTA;
472 if ((inum == FILESYSTEM_I) && (JFS_IP(ip)->ipimap == sbi->ipaimap)) {
473 sbi->gengen = le32_to_cpu(dp->di_gengen);
474 sbi->inostamp = le32_to_cpu(dp->di_inostamp);
477 /* release the page */
478 release_metapage(mp);
486 * NAME: diWriteSpecial()
488 * FUNCTION: Write the special inode to disk
492 * secondary - 1 if secondary aggregate inode table
494 * RETURN VALUES: none
497 void diWriteSpecial(struct inode *ip, int secondary)
499 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
502 ino_t inum = ip->i_ino;
506 address = addressPXD(&sbi->ait2) >> sbi->l2nbperpage;
508 address = AITBL_OFF >> L2PSIZE;
510 ASSERT(inum < INOSPEREXT);
512 address += inum >> 3; /* 8 inodes per 4K page */
514 /* read the page of fixed disk inode (AIT) in raw mode */
515 mp = read_metapage(ip, address << sbi->l2nbperpage, PSIZE, 1);
517 jfs_err("diWriteSpecial: failed to read aggregate inode extent!");
521 /* get the pointer to the disk inode of interest */
522 dp = (struct dinode *) (mp->data);
523 dp += inum % 8; /* 8 inodes per 4K page */
525 /* copy on-disk inode to in-memory inode */
526 copy_to_dinode(dp, ip);
527 memcpy(&dp->di_xtroot, &JFS_IP(ip)->i_xtroot, 288);
529 if (inum == FILESYSTEM_I)
530 dp->di_gengen = cpu_to_le32(sbi->gengen);
537 * NAME: diFreeSpecial()
539 * FUNCTION: Free allocated space for special inode
541 void diFreeSpecial(struct inode *ip)
544 jfs_err("diFreeSpecial called with NULL ip!");
547 filemap_write_and_wait(ip->i_mapping);
548 truncate_inode_pages(ip->i_mapping, 0);
557 * FUNCTION: write the on-disk inode portion of the in-memory inode
558 * to its corresponding on-disk inode.
560 * on entry, the specifed incore inode should itself
561 * specify the disk inode number corresponding to the
562 * incore inode (i.e. i_number should be initialized).
564 * the inode contains the inode extent address for the disk
565 * inode. with the inode extent address in hand, the
566 * page of the extent that contains the disk inode is
567 * read and the disk inode portion of the incore inode
568 * is copied to the disk inode.
571 * tid - transacation id
572 * ip - pointer to incore inode to be written to the inode extent.
578 int diWrite(tid_t tid, struct inode *ip)
580 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
581 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
589 unsigned long pageno;
592 struct inode *ipimap;
595 struct tlock *ditlck, *tlck;
596 struct linelock *dilinelock, *ilinelock;
600 ipimap = jfs_ip->ipimap;
602 ino = ip->i_ino & (INOSPERIAG - 1);
604 if (!addressPXD(&(jfs_ip->ixpxd)) ||
605 (lengthPXD(&(jfs_ip->ixpxd)) !=
606 JFS_IP(ipimap)->i_imap->im_nbperiext)) {
607 jfs_error(ip->i_sb, "ixpxd invalid\n");
612 * read the page of disk inode containing the specified inode:
614 /* compute the block address of the page */
615 blkno = INOPBLK(&(jfs_ip->ixpxd), ino, sbi->l2nbperpage);
617 rel_inode = (ino & (INOSPERPAGE - 1));
618 pageno = blkno >> sbi->l2nbperpage;
620 if ((block_offset = ((u32) blkno & (sbi->nbperpage - 1)))) {
622 * OS/2 didn't always align inode extents on page boundaries
625 (sbi->nbperpage - block_offset) << sbi->l2niperblk;
627 if (rel_inode < inodes_left)
628 rel_inode += block_offset << sbi->l2niperblk;
631 rel_inode -= inodes_left;
634 /* read the page of disk inode */
636 mp = read_metapage(ipimap, pageno << sbi->l2nbperpage, PSIZE, 1);
640 /* get the pointer to the disk inode */
641 dp = (struct dinode *) mp->data;
644 dioffset = (ino & (INOSPERPAGE - 1)) << L2DISIZE;
647 * acquire transaction lock on the on-disk inode;
648 * N.B. tlock is acquired on ipimap not ip;
651 txLock(tid, ipimap, mp, tlckINODE | tlckENTRY)) == NULL)
653 dilinelock = (struct linelock *) & ditlck->lock;
656 * copy btree root from in-memory inode to on-disk inode
658 * (tlock is taken from inline B+-tree root in in-memory
659 * inode when the B+-tree root is updated, which is pointed
660 * by jfs_ip->blid as well as being on tx tlock list)
662 * further processing of btree root is based on the copy
663 * in in-memory inode, where txLog() will log from, and,
664 * for xtree root, txUpdateMap() will update map and reset
668 if (S_ISDIR(ip->i_mode) && (lid = jfs_ip->xtlid)) {
670 * This is the special xtree inside the directory for storing
671 * the directory table
677 tlck = lid_to_tlock(lid);
678 assert(tlck->type & tlckXTREE);
679 tlck->type |= tlckBTROOT;
681 ilinelock = (struct linelock *) & tlck->lock;
684 * copy xtree root from inode to dinode:
686 p = &jfs_ip->i_xtroot;
687 xp = (xtpage_t *) &dp->di_dirtable;
689 for (n = 0; n < ilinelock->index; n++, lv++) {
690 memcpy(&xp->xad[lv->offset], &p->xad[lv->offset],
691 lv->length << L2XTSLOTSIZE);
694 /* reset on-disk (metadata page) xtree XAD_NEW bit */
695 xad = &xp->xad[XTENTRYSTART];
696 for (n = XTENTRYSTART;
697 n < le16_to_cpu(xp->header.nextindex); n++, xad++)
698 if (xad->flag & (XAD_NEW | XAD_EXTENDED))
699 xad->flag &= ~(XAD_NEW | XAD_EXTENDED);
702 if ((lid = jfs_ip->blid) == 0)
706 tlck = lid_to_tlock(lid);
708 tlck->type |= tlckBTROOT;
710 ilinelock = (struct linelock *) & tlck->lock;
713 * regular file: 16 byte (XAD slot) granularity
715 if (type & tlckXTREE) {
720 * copy xtree root from inode to dinode:
722 p = &jfs_ip->i_xtroot;
725 for (n = 0; n < ilinelock->index; n++, lv++) {
726 memcpy(&xp->xad[lv->offset], &p->xad[lv->offset],
727 lv->length << L2XTSLOTSIZE);
730 /* reset on-disk (metadata page) xtree XAD_NEW bit */
731 xad = &xp->xad[XTENTRYSTART];
732 for (n = XTENTRYSTART;
733 n < le16_to_cpu(xp->header.nextindex); n++, xad++)
734 if (xad->flag & (XAD_NEW | XAD_EXTENDED))
735 xad->flag &= ~(XAD_NEW | XAD_EXTENDED);
738 * directory: 32 byte (directory entry slot) granularity
740 else if (type & tlckDTREE) {
744 * copy dtree root from inode to dinode:
746 p = (dtpage_t *) &jfs_ip->i_dtroot;
747 xp = (dtpage_t *) & dp->di_dtroot;
749 for (n = 0; n < ilinelock->index; n++, lv++) {
750 memcpy(&xp->slot[lv->offset], &p->slot[lv->offset],
751 lv->length << L2DTSLOTSIZE);
754 jfs_err("diWrite: UFO tlock");
759 * copy inline symlink from in-memory inode to on-disk inode
761 if (S_ISLNK(ip->i_mode) && ip->i_size < IDATASIZE) {
762 lv = & dilinelock->lv[dilinelock->index];
763 lv->offset = (dioffset + 2 * 128) >> L2INODESLOTSIZE;
765 memcpy(&dp->di_inline_all, jfs_ip->i_inline_all, IDATASIZE);
769 * copy inline data from in-memory inode to on-disk inode:
770 * 128 byte slot granularity
772 if (test_cflag(COMMIT_Inlineea, ip)) {
773 lv = & dilinelock->lv[dilinelock->index];
774 lv->offset = (dioffset + 3 * 128) >> L2INODESLOTSIZE;
776 memcpy(&dp->di_inlineea, jfs_ip->i_inline_ea, INODESLOTSIZE);
779 clear_cflag(COMMIT_Inlineea, ip);
783 * lock/copy inode base: 128 byte slot granularity
785 lv = & dilinelock->lv[dilinelock->index];
786 lv->offset = dioffset >> L2INODESLOTSIZE;
787 copy_to_dinode(dp, ip);
788 if (test_and_clear_cflag(COMMIT_Dirtable, ip)) {
790 memcpy(&dp->di_dirtable, &jfs_ip->i_dirtable, 96);
795 /* release the buffer holding the updated on-disk inode.
796 * the buffer will be later written by commit processing.
807 * FUNCTION: free a specified inode from the inode working map
808 * for a fileset or aggregate.
810 * if the inode to be freed represents the first (only)
811 * free inode within the iag, the iag will be placed on
812 * the ag free inode list.
814 * freeing the inode will cause the inode extent to be
815 * freed if the inode is the only allocated inode within
816 * the extent. in this case all the disk resource backing
817 * up the inode extent will be freed. in addition, the iag
818 * will be placed on the ag extent free list if the extent
819 * is the first free extent in the iag. if freeing the
820 * extent also means that no free inodes will exist for
821 * the iag, the iag will also be removed from the ag free
824 * the iag describing the inode will be freed if the extent
825 * is to be freed and it is the only backed extent within
826 * the iag. in this case, the iag will be removed from the
827 * ag free extent list and ag free inode list and placed on
828 * the inode map's free iag list.
830 * a careful update approach is used to provide consistency
831 * in the face of updates to multiple buffers. under this
832 * approach, all required buffers are obtained before making
833 * any updates and are held until all updates are complete.
836 * ip - inode to be freed.
842 int diFree(struct inode *ip)
845 ino_t inum = ip->i_ino;
846 struct iag *iagp, *aiagp, *biagp, *ciagp, *diagp;
847 struct metapage *mp, *amp, *bmp, *cmp, *dmp;
848 int iagno, ino, extno, bitno, sword, agno;
851 struct inode *ipimap = JFS_SBI(ip->i_sb)->ipimap;
852 struct inomap *imap = JFS_IP(ipimap)->i_imap;
855 struct inode *iplist[3];
857 struct pxd_lock *pxdlock;
860 * This is just to suppress compiler warnings. The same logic that
861 * references these variables is used to initialize them.
863 aiagp = biagp = ciagp = diagp = NULL;
865 /* get the iag number containing the inode.
867 iagno = INOTOIAG(inum);
869 /* make sure that the iag is contained within
872 if (iagno >= imap->im_nextiag) {
873 print_hex_dump(KERN_ERR, "imap: ", DUMP_PREFIX_ADDRESS, 16, 4,
875 jfs_error(ip->i_sb, "inum = %d, iagno = %d, nextiag = %d\n",
876 (uint) inum, iagno, imap->im_nextiag);
880 /* get the allocation group for this ino.
882 agno = BLKTOAG(JFS_IP(ip)->agstart, JFS_SBI(ip->i_sb));
884 /* Lock the AG specific inode map information
888 /* Obtain read lock in imap inode. Don't release it until we have
889 * read all of the IAG's that we are going to.
891 IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
895 if ((rc = diIAGRead(imap, iagno, &mp))) {
896 IREAD_UNLOCK(ipimap);
897 AG_UNLOCK(imap, agno);
900 iagp = (struct iag *) mp->data;
902 /* get the inode number and extent number of the inode within
903 * the iag and the inode number within the extent.
905 ino = inum & (INOSPERIAG - 1);
906 extno = ino >> L2INOSPEREXT;
907 bitno = ino & (INOSPEREXT - 1);
908 mask = HIGHORDER >> bitno;
910 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
911 jfs_error(ip->i_sb, "wmap shows inode already free\n");
914 if (!addressPXD(&iagp->inoext[extno])) {
915 release_metapage(mp);
916 IREAD_UNLOCK(ipimap);
917 AG_UNLOCK(imap, agno);
918 jfs_error(ip->i_sb, "invalid inoext\n");
922 /* compute the bitmap for the extent reflecting the freed inode.
924 bitmap = le32_to_cpu(iagp->wmap[extno]) & ~mask;
926 if (imap->im_agctl[agno].numfree > imap->im_agctl[agno].numinos) {
927 release_metapage(mp);
928 IREAD_UNLOCK(ipimap);
929 AG_UNLOCK(imap, agno);
930 jfs_error(ip->i_sb, "numfree > numinos\n");
934 * inode extent still has some inodes or below low water mark:
935 * keep the inode extent;
938 imap->im_agctl[agno].numfree < 96 ||
939 (imap->im_agctl[agno].numfree < 288 &&
940 (((imap->im_agctl[agno].numfree * 100) /
941 imap->im_agctl[agno].numinos) <= 25))) {
942 /* if the iag currently has no free inodes (i.e.,
943 * the inode being freed is the first free inode of iag),
944 * insert the iag at head of the inode free list for the ag.
946 if (iagp->nfreeinos == 0) {
947 /* check if there are any iags on the ag inode
948 * free list. if so, read the first one so that
949 * we can link the current iag onto the list at
952 if ((fwd = imap->im_agctl[agno].inofree) >= 0) {
953 /* read the iag that currently is the head
956 if ((rc = diIAGRead(imap, fwd, &))) {
957 IREAD_UNLOCK(ipimap);
958 AG_UNLOCK(imap, agno);
959 release_metapage(mp);
962 aiagp = (struct iag *) amp->data;
964 /* make current head point back to the iag.
966 aiagp->inofreeback = cpu_to_le32(iagno);
971 /* iag points forward to current head and iag
972 * becomes the new head of the list.
975 cpu_to_le32(imap->im_agctl[agno].inofree);
976 iagp->inofreeback = cpu_to_le32(-1);
977 imap->im_agctl[agno].inofree = iagno;
979 IREAD_UNLOCK(ipimap);
981 /* update the free inode summary map for the extent if
982 * freeing the inode means the extent will now have free
983 * inodes (i.e., the inode being freed is the first free
986 if (iagp->wmap[extno] == cpu_to_le32(ONES)) {
987 sword = extno >> L2EXTSPERSUM;
988 bitno = extno & (EXTSPERSUM - 1);
989 iagp->inosmap[sword] &=
990 cpu_to_le32(~(HIGHORDER >> bitno));
993 /* update the bitmap.
995 iagp->wmap[extno] = cpu_to_le32(bitmap);
997 /* update the free inode counts at the iag, ag and
1000 le32_add_cpu(&iagp->nfreeinos, 1);
1001 imap->im_agctl[agno].numfree += 1;
1002 atomic_inc(&imap->im_numfree);
1004 /* release the AG inode map lock
1006 AG_UNLOCK(imap, agno);
1016 * inode extent has become free and above low water mark:
1017 * free the inode extent;
1021 * prepare to update iag list(s) (careful update step 1)
1023 amp = bmp = cmp = dmp = NULL;
1026 /* check if the iag currently has no free extents. if so,
1027 * it will be placed on the head of the ag extent free list.
1029 if (iagp->nfreeexts == 0) {
1030 /* check if the ag extent free list has any iags.
1031 * if so, read the iag at the head of the list now.
1032 * this (head) iag will be updated later to reflect
1033 * the addition of the current iag at the head of
1036 if ((fwd = imap->im_agctl[agno].extfree) >= 0) {
1037 if ((rc = diIAGRead(imap, fwd, &)))
1039 aiagp = (struct iag *) amp->data;
1042 /* iag has free extents. check if the addition of a free
1043 * extent will cause all extents to be free within this
1044 * iag. if so, the iag will be removed from the ag extent
1045 * free list and placed on the inode map's free iag list.
1047 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG - 1)) {
1048 /* in preparation for removing the iag from the
1049 * ag extent free list, read the iags preceding
1050 * and following the iag on the ag extent free
1053 if ((fwd = le32_to_cpu(iagp->extfreefwd)) >= 0) {
1054 if ((rc = diIAGRead(imap, fwd, &)))
1056 aiagp = (struct iag *) amp->data;
1059 if ((back = le32_to_cpu(iagp->extfreeback)) >= 0) {
1060 if ((rc = diIAGRead(imap, back, &bmp)))
1062 biagp = (struct iag *) bmp->data;
1067 /* remove the iag from the ag inode free list if freeing
1068 * this extent cause the iag to have no free inodes.
1070 if (iagp->nfreeinos == cpu_to_le32(INOSPEREXT - 1)) {
1071 int inofreeback = le32_to_cpu(iagp->inofreeback);
1072 int inofreefwd = le32_to_cpu(iagp->inofreefwd);
1074 /* in preparation for removing the iag from the
1075 * ag inode free list, read the iags preceding
1076 * and following the iag on the ag inode free
1077 * list. before reading these iags, we must make
1078 * sure that we already don't have them in hand
1079 * from up above, since re-reading an iag (buffer)
1080 * we are currently holding would cause a deadlock.
1082 if (inofreefwd >= 0) {
1084 if (inofreefwd == fwd)
1085 ciagp = (struct iag *) amp->data;
1086 else if (inofreefwd == back)
1087 ciagp = (struct iag *) bmp->data;
1090 diIAGRead(imap, inofreefwd, &cmp)))
1092 ciagp = (struct iag *) cmp->data;
1094 assert(ciagp != NULL);
1097 if (inofreeback >= 0) {
1098 if (inofreeback == fwd)
1099 diagp = (struct iag *) amp->data;
1100 else if (inofreeback == back)
1101 diagp = (struct iag *) bmp->data;
1104 diIAGRead(imap, inofreeback, &dmp)))
1106 diagp = (struct iag *) dmp->data;
1108 assert(diagp != NULL);
1112 IREAD_UNLOCK(ipimap);
1115 * invalidate any page of the inode extent freed from buffer cache;
1117 freepxd = iagp->inoext[extno];
1118 invalidate_pxd_metapages(ip, freepxd);
1121 * update iag list(s) (careful update step 2)
1123 /* add the iag to the ag extent free list if this is the
1124 * first free extent for the iag.
1126 if (iagp->nfreeexts == 0) {
1128 aiagp->extfreeback = cpu_to_le32(iagno);
1131 cpu_to_le32(imap->im_agctl[agno].extfree);
1132 iagp->extfreeback = cpu_to_le32(-1);
1133 imap->im_agctl[agno].extfree = iagno;
1135 /* remove the iag from the ag extent list if all extents
1136 * are now free and place it on the inode map iag free list.
1138 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG - 1)) {
1140 aiagp->extfreeback = iagp->extfreeback;
1143 biagp->extfreefwd = iagp->extfreefwd;
1145 imap->im_agctl[agno].extfree =
1146 le32_to_cpu(iagp->extfreefwd);
1148 iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
1151 iagp->iagfree = cpu_to_le32(imap->im_freeiag);
1152 imap->im_freeiag = iagno;
1153 IAGFREE_UNLOCK(imap);
1157 /* remove the iag from the ag inode free list if freeing
1158 * this extent causes the iag to have no free inodes.
1160 if (iagp->nfreeinos == cpu_to_le32(INOSPEREXT - 1)) {
1161 if ((int) le32_to_cpu(iagp->inofreefwd) >= 0)
1162 ciagp->inofreeback = iagp->inofreeback;
1164 if ((int) le32_to_cpu(iagp->inofreeback) >= 0)
1165 diagp->inofreefwd = iagp->inofreefwd;
1167 imap->im_agctl[agno].inofree =
1168 le32_to_cpu(iagp->inofreefwd);
1170 iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
1173 /* update the inode extent address and working map
1174 * to reflect the free extent.
1175 * the permanent map should have been updated already
1176 * for the inode being freed.
1178 if (iagp->pmap[extno] != 0) {
1179 jfs_error(ip->i_sb, "the pmap does not show inode free\n");
1181 iagp->wmap[extno] = 0;
1182 PXDlength(&iagp->inoext[extno], 0);
1183 PXDaddress(&iagp->inoext[extno], 0);
1185 /* update the free extent and free inode summary maps
1186 * to reflect the freed extent.
1187 * the inode summary map is marked to indicate no inodes
1188 * available for the freed extent.
1190 sword = extno >> L2EXTSPERSUM;
1191 bitno = extno & (EXTSPERSUM - 1);
1192 mask = HIGHORDER >> bitno;
1193 iagp->inosmap[sword] |= cpu_to_le32(mask);
1194 iagp->extsmap[sword] &= cpu_to_le32(~mask);
1196 /* update the number of free inodes and number of free extents
1199 le32_add_cpu(&iagp->nfreeinos, -(INOSPEREXT - 1));
1200 le32_add_cpu(&iagp->nfreeexts, 1);
1202 /* update the number of free inodes and backed inodes
1203 * at the ag and inode map level.
1205 imap->im_agctl[agno].numfree -= (INOSPEREXT - 1);
1206 imap->im_agctl[agno].numinos -= INOSPEREXT;
1207 atomic_sub(INOSPEREXT - 1, &imap->im_numfree);
1208 atomic_sub(INOSPEREXT, &imap->im_numinos);
1211 write_metapage(amp);
1213 write_metapage(bmp);
1215 write_metapage(cmp);
1217 write_metapage(dmp);
1220 * start transaction to update block allocation map
1221 * for the inode extent freed;
1223 * N.B. AG_LOCK is released and iag will be released below, and
1224 * other thread may allocate inode from/reusing the ixad freed
1225 * BUT with new/different backing inode extent from the extent
1226 * to be freed by the transaction;
1228 tid = txBegin(ipimap->i_sb, COMMIT_FORCE);
1229 mutex_lock(&JFS_IP(ipimap)->commit_mutex);
1231 /* acquire tlock of the iag page of the freed ixad
1232 * to force the page NOHOMEOK (even though no data is
1233 * logged from the iag page) until NOREDOPAGE|FREEXTENT log
1234 * for the free of the extent is committed;
1235 * write FREEXTENT|NOREDOPAGE log record
1236 * N.B. linelock is overlaid as freed extent descriptor;
1238 tlck = txLock(tid, ipimap, mp, tlckINODE | tlckFREE);
1239 pxdlock = (struct pxd_lock *) & tlck->lock;
1240 pxdlock->flag = mlckFREEPXD;
1241 pxdlock->pxd = freepxd;
1249 * logredo needs the IAG number and IAG extent index in order
1250 * to ensure that the IMap is consistent. The least disruptive
1251 * way to pass these values through to the transaction manager
1252 * is in the iplist array.
1254 * It's not pretty, but it works.
1256 iplist[1] = (struct inode *) (size_t)iagno;
1257 iplist[2] = (struct inode *) (size_t)extno;
1259 rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
1262 mutex_unlock(&JFS_IP(ipimap)->commit_mutex);
1264 /* unlock the AG inode map information */
1265 AG_UNLOCK(imap, agno);
1270 IREAD_UNLOCK(ipimap);
1273 release_metapage(amp);
1275 release_metapage(bmp);
1277 release_metapage(cmp);
1279 release_metapage(dmp);
1281 AG_UNLOCK(imap, agno);
1283 release_metapage(mp);
1289 * There are several places in the diAlloc* routines where we initialize
1293 diInitInode(struct inode *ip, int iagno, int ino, int extno, struct iag * iagp)
1295 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
1297 ip->i_ino = (iagno << L2INOSPERIAG) + ino;
1298 jfs_ip->ixpxd = iagp->inoext[extno];
1299 jfs_ip->agstart = le64_to_cpu(iagp->agstart);
1300 jfs_ip->active_ag = -1;
1305 * NAME: diAlloc(pip,dir,ip)
1307 * FUNCTION: allocate a disk inode from the inode working map
1308 * for a fileset or aggregate.
1311 * pip - pointer to incore inode for the parent inode.
1312 * dir - 'true' if the new disk inode is for a directory.
1313 * ip - pointer to a new inode
1317 * -ENOSPC - insufficient disk resources.
1320 int diAlloc(struct inode *pip, bool dir, struct inode *ip)
1322 int rc, ino, iagno, addext, extno, bitno, sword;
1323 int nwords, rem, i, agno;
1324 u32 mask, inosmap, extsmap;
1325 struct inode *ipimap;
1326 struct metapage *mp;
1329 struct inomap *imap;
1331 /* get the pointers to the inode map inode and the
1332 * corresponding imap control structure.
1334 ipimap = JFS_SBI(pip->i_sb)->ipimap;
1335 imap = JFS_IP(ipimap)->i_imap;
1336 JFS_IP(ip)->ipimap = ipimap;
1337 JFS_IP(ip)->fileset = FILESYSTEM_I;
1339 /* for a directory, the allocation policy is to start
1340 * at the ag level using the preferred ag.
1343 agno = dbNextAG(JFS_SBI(pip->i_sb)->ipbmap);
1344 AG_LOCK(imap, agno);
1348 /* for files, the policy starts off by trying to allocate from
1349 * the same iag containing the parent disk inode:
1350 * try to allocate the new disk inode close to the parent disk
1351 * inode, using parent disk inode number + 1 as the allocation
1352 * hint. (we use a left-to-right policy to attempt to avoid
1353 * moving backward on the disk.) compute the hint within the
1354 * file system and the iag.
1357 /* get the ag number of this iag */
1358 agno = BLKTOAG(JFS_IP(pip)->agstart, JFS_SBI(pip->i_sb));
1360 if (atomic_read(&JFS_SBI(pip->i_sb)->bmap->db_active[agno])) {
1362 * There is an open file actively growing. We want to
1363 * allocate new inodes from a different ag to avoid
1364 * fragmentation problems.
1366 agno = dbNextAG(JFS_SBI(pip->i_sb)->ipbmap);
1367 AG_LOCK(imap, agno);
1371 inum = pip->i_ino + 1;
1372 ino = inum & (INOSPERIAG - 1);
1374 /* back off the hint if it is outside of the iag */
1378 /* lock the AG inode map information */
1379 AG_LOCK(imap, agno);
1381 /* Get read lock on imap inode */
1382 IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
1384 /* get the iag number and read the iag */
1385 iagno = INOTOIAG(inum);
1386 if ((rc = diIAGRead(imap, iagno, &mp))) {
1387 IREAD_UNLOCK(ipimap);
1388 AG_UNLOCK(imap, agno);
1391 iagp = (struct iag *) mp->data;
1393 /* determine if new inode extent is allowed to be added to the iag.
1394 * new inode extent can be added to the iag if the ag
1395 * has less than 32 free disk inodes and the iag has free extents.
1397 addext = (imap->im_agctl[agno].numfree < 32 && iagp->nfreeexts);
1400 * try to allocate from the IAG
1402 /* check if the inode may be allocated from the iag
1403 * (i.e. the inode has free inodes or new extent can be added).
1405 if (iagp->nfreeinos || addext) {
1406 /* determine the extent number of the hint.
1408 extno = ino >> L2INOSPEREXT;
1410 /* check if the extent containing the hint has backed
1411 * inodes. if so, try to allocate within this extent.
1413 if (addressPXD(&iagp->inoext[extno])) {
1414 bitno = ino & (INOSPEREXT - 1);
1416 diFindFree(le32_to_cpu(iagp->wmap[extno]),
1419 ino = (extno << L2INOSPEREXT) + bitno;
1421 /* a free inode (bit) was found within this
1422 * extent, so allocate it.
1424 rc = diAllocBit(imap, iagp, ino);
1425 IREAD_UNLOCK(ipimap);
1429 /* set the results of the allocation
1430 * and write the iag.
1432 diInitInode(ip, iagno, ino, extno,
1434 mark_metapage_dirty(mp);
1436 release_metapage(mp);
1438 /* free the AG lock and return.
1440 AG_UNLOCK(imap, agno);
1447 EXTSPERIAG - 1) ? 0 : extno + 1;
1451 * no free inodes within the extent containing the hint.
1453 * try to allocate from the backed extents following
1454 * hint or, if appropriate (i.e. addext is true), allocate
1455 * an extent of free inodes at or following the extent
1456 * containing the hint.
1458 * the free inode and free extent summary maps are used
1459 * here, so determine the starting summary map position
1460 * and the number of words we'll have to examine. again,
1461 * the approach is to allocate following the hint, so we
1462 * might have to initially ignore prior bits of the summary
1463 * map that represent extents prior to the extent containing
1464 * the hint and later revisit these bits.
1466 bitno = extno & (EXTSPERSUM - 1);
1467 nwords = (bitno == 0) ? SMAPSZ : SMAPSZ + 1;
1468 sword = extno >> L2EXTSPERSUM;
1470 /* mask any prior bits for the starting words of the
1473 mask = (bitno == 0) ? 0 : (ONES << (EXTSPERSUM - bitno));
1474 inosmap = le32_to_cpu(iagp->inosmap[sword]) | mask;
1475 extsmap = le32_to_cpu(iagp->extsmap[sword]) | mask;
1477 /* scan the free inode and free extent summary maps for
1480 for (i = 0; i < nwords; i++) {
1481 /* check if this word of the free inode summary
1482 * map describes an extent with free inodes.
1485 /* an extent with free inodes has been
1486 * found. determine the extent number
1487 * and the inode number within the extent.
1489 rem = diFindFree(inosmap, 0);
1490 extno = (sword << L2EXTSPERSUM) + rem;
1491 rem = diFindFree(le32_to_cpu(iagp->wmap[extno]),
1493 if (rem >= INOSPEREXT) {
1494 IREAD_UNLOCK(ipimap);
1495 release_metapage(mp);
1496 AG_UNLOCK(imap, agno);
1498 "can't find free bit in wmap\n");
1502 /* determine the inode number within the
1503 * iag and allocate the inode from the
1506 ino = (extno << L2INOSPEREXT) + rem;
1507 rc = diAllocBit(imap, iagp, ino);
1508 IREAD_UNLOCK(ipimap);
1512 /* set the results of the allocation
1513 * and write the iag.
1515 diInitInode(ip, iagno, ino, extno,
1517 mark_metapage_dirty(mp);
1519 release_metapage(mp);
1521 /* free the AG lock and return.
1523 AG_UNLOCK(imap, agno);
1528 /* check if we may allocate an extent of free
1529 * inodes and whether this word of the free
1530 * extents summary map describes a free extent.
1532 if (addext && ~extsmap) {
1533 /* a free extent has been found. determine
1534 * the extent number.
1536 rem = diFindFree(extsmap, 0);
1537 extno = (sword << L2EXTSPERSUM) + rem;
1539 /* allocate an extent of free inodes.
1541 if ((rc = diNewExt(imap, iagp, extno))) {
1542 /* if there is no disk space for a
1543 * new extent, try to allocate the
1544 * disk inode from somewhere else.
1551 /* set the results of the allocation
1552 * and write the iag.
1554 diInitInode(ip, iagno,
1555 extno << L2INOSPEREXT,
1557 mark_metapage_dirty(mp);
1559 release_metapage(mp);
1560 /* free the imap inode & the AG lock & return.
1562 IREAD_UNLOCK(ipimap);
1563 AG_UNLOCK(imap, agno);
1567 /* move on to the next set of summary map words.
1569 sword = (sword == SMAPSZ - 1) ? 0 : sword + 1;
1570 inosmap = le32_to_cpu(iagp->inosmap[sword]);
1571 extsmap = le32_to_cpu(iagp->extsmap[sword]);
1574 /* unlock imap inode */
1575 IREAD_UNLOCK(ipimap);
1577 /* nothing doing in this iag, so release it. */
1578 release_metapage(mp);
1582 * try to allocate anywhere within the same AG as the parent inode.
1584 rc = diAllocAG(imap, agno, dir, ip);
1586 AG_UNLOCK(imap, agno);
1592 * try to allocate in any AG.
1594 return (diAllocAny(imap, agno, dir, ip));
1599 * NAME: diAllocAG(imap,agno,dir,ip)
1601 * FUNCTION: allocate a disk inode from the allocation group.
1603 * this routine first determines if a new extent of free
1604 * inodes should be added for the allocation group, with
1605 * the current request satisfied from this extent. if this
1606 * is the case, an attempt will be made to do just that. if
1607 * this attempt fails or it has been determined that a new
1608 * extent should not be added, an attempt is made to satisfy
1609 * the request by allocating an existing (backed) free inode
1610 * from the allocation group.
1612 * PRE CONDITION: Already have the AG lock for this AG.
1615 * imap - pointer to inode map control structure.
1616 * agno - allocation group to allocate from.
1617 * dir - 'true' if the new disk inode is for a directory.
1618 * ip - pointer to the new inode to be filled in on successful return
1619 * with the disk inode number allocated, its extent address
1620 * and the start of the ag.
1624 * -ENOSPC - insufficient disk resources.
1628 diAllocAG(struct inomap * imap, int agno, bool dir, struct inode *ip)
1630 int rc, addext, numfree, numinos;
1632 /* get the number of free and the number of backed disk
1633 * inodes currently within the ag.
1635 numfree = imap->im_agctl[agno].numfree;
1636 numinos = imap->im_agctl[agno].numinos;
1638 if (numfree > numinos) {
1639 jfs_error(ip->i_sb, "numfree > numinos\n");
1643 /* determine if we should allocate a new extent of free inodes
1644 * within the ag: for directory inodes, add a new extent
1645 * if there are a small number of free inodes or number of free
1646 * inodes is a small percentage of the number of backed inodes.
1649 addext = (numfree < 64 ||
1651 && ((numfree * 100) / numinos) <= 20));
1653 addext = (numfree == 0);
1656 * try to allocate a new extent of free inodes.
1659 /* if free space is not available for this new extent, try
1660 * below to allocate a free and existing (already backed)
1661 * inode from the ag.
1663 if ((rc = diAllocExt(imap, agno, ip)) != -ENOSPC)
1668 * try to allocate an existing free inode from the ag.
1670 return (diAllocIno(imap, agno, ip));
1675 * NAME: diAllocAny(imap,agno,dir,iap)
1677 * FUNCTION: allocate a disk inode from any other allocation group.
1679 * this routine is called when an allocation attempt within
1680 * the primary allocation group has failed. if attempts to
1681 * allocate an inode from any allocation group other than the
1682 * specified primary group.
1685 * imap - pointer to inode map control structure.
1686 * agno - primary allocation group (to avoid).
1687 * dir - 'true' if the new disk inode is for a directory.
1688 * ip - pointer to a new inode to be filled in on successful return
1689 * with the disk inode number allocated, its extent address
1690 * and the start of the ag.
1694 * -ENOSPC - insufficient disk resources.
1698 diAllocAny(struct inomap * imap, int agno, bool dir, struct inode *ip)
1701 int maxag = JFS_SBI(imap->im_ipimap->i_sb)->bmap->db_maxag;
1704 /* try to allocate from the ags following agno up to
1705 * the maximum ag number.
1707 for (ag = agno + 1; ag <= maxag; ag++) {
1710 rc = diAllocAG(imap, ag, dir, ip);
1712 AG_UNLOCK(imap, ag);
1718 /* try to allocate from the ags in front of agno.
1720 for (ag = 0; ag < agno; ag++) {
1723 rc = diAllocAG(imap, ag, dir, ip);
1725 AG_UNLOCK(imap, ag);
1731 /* no free disk inodes.
1738 * NAME: diAllocIno(imap,agno,ip)
1740 * FUNCTION: allocate a disk inode from the allocation group's free
1741 * inode list, returning an error if this free list is
1742 * empty (i.e. no iags on the list).
1744 * allocation occurs from the first iag on the list using
1745 * the iag's free inode summary map to find the leftmost
1746 * free inode in the iag.
1748 * PRE CONDITION: Already have AG lock for this AG.
1751 * imap - pointer to inode map control structure.
1752 * agno - allocation group.
1753 * ip - pointer to new inode to be filled in on successful return
1754 * with the disk inode number allocated, its extent address
1755 * and the start of the ag.
1759 * -ENOSPC - insufficient disk resources.
1762 static int diAllocIno(struct inomap * imap, int agno, struct inode *ip)
1764 int iagno, ino, rc, rem, extno, sword;
1765 struct metapage *mp;
1768 /* check if there are iags on the ag's free inode list.
1770 if ((iagno = imap->im_agctl[agno].inofree) < 0)
1773 /* obtain read lock on imap inode */
1774 IREAD_LOCK(imap->im_ipimap, RDWRLOCK_IMAP);
1776 /* read the iag at the head of the list.
1778 if ((rc = diIAGRead(imap, iagno, &mp))) {
1779 IREAD_UNLOCK(imap->im_ipimap);
1782 iagp = (struct iag *) mp->data;
1784 /* better be free inodes in this iag if it is on the
1787 if (!iagp->nfreeinos) {
1788 IREAD_UNLOCK(imap->im_ipimap);
1789 release_metapage(mp);
1790 jfs_error(ip->i_sb, "nfreeinos = 0, but iag on freelist\n");
1794 /* scan the free inode summary map to find an extent
1797 for (sword = 0;; sword++) {
1798 if (sword >= SMAPSZ) {
1799 IREAD_UNLOCK(imap->im_ipimap);
1800 release_metapage(mp);
1802 "free inode not found in summary map\n");
1806 if (~iagp->inosmap[sword])
1810 /* found a extent with free inodes. determine
1811 * the extent number.
1813 rem = diFindFree(le32_to_cpu(iagp->inosmap[sword]), 0);
1814 if (rem >= EXTSPERSUM) {
1815 IREAD_UNLOCK(imap->im_ipimap);
1816 release_metapage(mp);
1817 jfs_error(ip->i_sb, "no free extent found\n");
1820 extno = (sword << L2EXTSPERSUM) + rem;
1822 /* find the first free inode in the extent.
1824 rem = diFindFree(le32_to_cpu(iagp->wmap[extno]), 0);
1825 if (rem >= INOSPEREXT) {
1826 IREAD_UNLOCK(imap->im_ipimap);
1827 release_metapage(mp);
1828 jfs_error(ip->i_sb, "free inode not found\n");
1832 /* compute the inode number within the iag.
1834 ino = (extno << L2INOSPEREXT) + rem;
1836 /* allocate the inode.
1838 rc = diAllocBit(imap, iagp, ino);
1839 IREAD_UNLOCK(imap->im_ipimap);
1841 release_metapage(mp);
1845 /* set the results of the allocation and write the iag.
1847 diInitInode(ip, iagno, ino, extno, iagp);
1855 * NAME: diAllocExt(imap,agno,ip)
1857 * FUNCTION: add a new extent of free inodes to an iag, allocating
1858 * an inode from this extent to satisfy the current allocation
1861 * this routine first tries to find an existing iag with free
1862 * extents through the ag free extent list. if list is not
1863 * empty, the head of the list will be selected as the home
1864 * of the new extent of free inodes. otherwise (the list is
1865 * empty), a new iag will be allocated for the ag to contain
1868 * once an iag has been selected, the free extent summary map
1869 * is used to locate a free extent within the iag and diNewExt()
1870 * is called to initialize the extent, with initialization
1871 * including the allocation of the first inode of the extent
1872 * for the purpose of satisfying this request.
1875 * imap - pointer to inode map control structure.
1876 * agno - allocation group number.
1877 * ip - pointer to new inode to be filled in on successful return
1878 * with the disk inode number allocated, its extent address
1879 * and the start of the ag.
1883 * -ENOSPC - insufficient disk resources.
1886 static int diAllocExt(struct inomap * imap, int agno, struct inode *ip)
1888 int rem, iagno, sword, extno, rc;
1889 struct metapage *mp;
1892 /* check if the ag has any iags with free extents. if not,
1893 * allocate a new iag for the ag.
1895 if ((iagno = imap->im_agctl[agno].extfree) < 0) {
1896 /* If successful, diNewIAG will obtain the read lock on the
1899 if ((rc = diNewIAG(imap, &iagno, agno, &mp))) {
1902 iagp = (struct iag *) mp->data;
1904 /* set the ag number if this a brand new iag
1907 cpu_to_le64(AGTOBLK(agno, imap->im_ipimap));
1911 IREAD_LOCK(imap->im_ipimap, RDWRLOCK_IMAP);
1912 if ((rc = diIAGRead(imap, iagno, &mp))) {
1913 IREAD_UNLOCK(imap->im_ipimap);
1914 jfs_error(ip->i_sb, "error reading iag\n");
1917 iagp = (struct iag *) mp->data;
1920 /* using the free extent summary map, find a free extent.
1922 for (sword = 0;; sword++) {
1923 if (sword >= SMAPSZ) {
1924 release_metapage(mp);
1925 IREAD_UNLOCK(imap->im_ipimap);
1926 jfs_error(ip->i_sb, "free ext summary map not found\n");
1929 if (~iagp->extsmap[sword])
1933 /* determine the extent number of the free extent.
1935 rem = diFindFree(le32_to_cpu(iagp->extsmap[sword]), 0);
1936 if (rem >= EXTSPERSUM) {
1937 release_metapage(mp);
1938 IREAD_UNLOCK(imap->im_ipimap);
1939 jfs_error(ip->i_sb, "free extent not found\n");
1942 extno = (sword << L2EXTSPERSUM) + rem;
1944 /* initialize the new extent.
1946 rc = diNewExt(imap, iagp, extno);
1947 IREAD_UNLOCK(imap->im_ipimap);
1949 /* something bad happened. if a new iag was allocated,
1950 * place it back on the inode map's iag free list, and
1951 * clear the ag number information.
1953 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
1955 iagp->iagfree = cpu_to_le32(imap->im_freeiag);
1956 imap->im_freeiag = iagno;
1957 IAGFREE_UNLOCK(imap);
1963 /* set the results of the allocation and write the iag.
1965 diInitInode(ip, iagno, extno << L2INOSPEREXT, extno, iagp);
1974 * NAME: diAllocBit(imap,iagp,ino)
1976 * FUNCTION: allocate a backed inode from an iag.
1978 * this routine performs the mechanics of allocating a
1979 * specified inode from a backed extent.
1981 * if the inode to be allocated represents the last free
1982 * inode within the iag, the iag will be removed from the
1983 * ag free inode list.
1985 * a careful update approach is used to provide consistency
1986 * in the face of updates to multiple buffers. under this
1987 * approach, all required buffers are obtained before making
1988 * any updates and are held all are updates are complete.
1990 * PRE CONDITION: Already have buffer lock on iagp. Already have AG lock on
1991 * this AG. Must have read lock on imap inode.
1994 * imap - pointer to inode map control structure.
1995 * iagp - pointer to iag.
1996 * ino - inode number to be allocated within the iag.
2000 * -ENOSPC - insufficient disk resources.
2003 static int diAllocBit(struct inomap * imap, struct iag * iagp, int ino)
2005 int extno, bitno, agno, sword, rc;
2006 struct metapage *amp = NULL, *bmp = NULL;
2007 struct iag *aiagp = NULL, *biagp = NULL;
2010 /* check if this is the last free inode within the iag.
2011 * if so, it will have to be removed from the ag free
2012 * inode list, so get the iags preceding and following
2015 if (iagp->nfreeinos == cpu_to_le32(1)) {
2016 if ((int) le32_to_cpu(iagp->inofreefwd) >= 0) {
2018 diIAGRead(imap, le32_to_cpu(iagp->inofreefwd),
2021 aiagp = (struct iag *) amp->data;
2024 if ((int) le32_to_cpu(iagp->inofreeback) >= 0) {
2027 le32_to_cpu(iagp->inofreeback),
2030 release_metapage(amp);
2033 biagp = (struct iag *) bmp->data;
2037 /* get the ag number, extent number, inode number within
2040 agno = BLKTOAG(le64_to_cpu(iagp->agstart), JFS_SBI(imap->im_ipimap->i_sb));
2041 extno = ino >> L2INOSPEREXT;
2042 bitno = ino & (INOSPEREXT - 1);
2044 /* compute the mask for setting the map.
2046 mask = HIGHORDER >> bitno;
2048 /* the inode should be free and backed.
2050 if (((le32_to_cpu(iagp->pmap[extno]) & mask) != 0) ||
2051 ((le32_to_cpu(iagp->wmap[extno]) & mask) != 0) ||
2052 (addressPXD(&iagp->inoext[extno]) == 0)) {
2054 release_metapage(amp);
2056 release_metapage(bmp);
2058 jfs_error(imap->im_ipimap->i_sb, "iag inconsistent\n");
2062 /* mark the inode as allocated in the working map.
2064 iagp->wmap[extno] |= cpu_to_le32(mask);
2066 /* check if all inodes within the extent are now
2067 * allocated. if so, update the free inode summary
2068 * map to reflect this.
2070 if (iagp->wmap[extno] == cpu_to_le32(ONES)) {
2071 sword = extno >> L2EXTSPERSUM;
2072 bitno = extno & (EXTSPERSUM - 1);
2073 iagp->inosmap[sword] |= cpu_to_le32(HIGHORDER >> bitno);
2076 /* if this was the last free inode in the iag, remove the
2077 * iag from the ag free inode list.
2079 if (iagp->nfreeinos == cpu_to_le32(1)) {
2081 aiagp->inofreeback = iagp->inofreeback;
2082 write_metapage(amp);
2086 biagp->inofreefwd = iagp->inofreefwd;
2087 write_metapage(bmp);
2089 imap->im_agctl[agno].inofree =
2090 le32_to_cpu(iagp->inofreefwd);
2092 iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
2095 /* update the free inode count at the iag, ag, inode
2098 le32_add_cpu(&iagp->nfreeinos, -1);
2099 imap->im_agctl[agno].numfree -= 1;
2100 atomic_dec(&imap->im_numfree);
2107 * NAME: diNewExt(imap,iagp,extno)
2109 * FUNCTION: initialize a new extent of inodes for an iag, allocating
2110 * the first inode of the extent for use for the current
2111 * allocation request.
2113 * disk resources are allocated for the new extent of inodes
2114 * and the inodes themselves are initialized to reflect their
2115 * existence within the extent (i.e. their inode numbers and
2116 * inode extent addresses are set) and their initial state
2117 * (mode and link count are set to zero).
2119 * if the iag is new, it is not yet on an ag extent free list
2120 * but will now be placed on this list.
2122 * if the allocation of the new extent causes the iag to
2123 * have no free extent, the iag will be removed from the
2124 * ag extent free list.
2126 * if the iag has no free backed inodes, it will be placed
2127 * on the ag free inode list, since the addition of the new
2128 * extent will now cause it to have free inodes.
2130 * a careful update approach is used to provide consistency
2131 * (i.e. list consistency) in the face of updates to multiple
2132 * buffers. under this approach, all required buffers are
2133 * obtained before making any updates and are held until all
2134 * updates are complete.
2136 * PRE CONDITION: Already have buffer lock on iagp. Already have AG lock on
2137 * this AG. Must have read lock on imap inode.
2140 * imap - pointer to inode map control structure.
2141 * iagp - pointer to iag.
2142 * extno - extent number.
2146 * -ENOSPC - insufficient disk resources.
2149 static int diNewExt(struct inomap * imap, struct iag * iagp, int extno)
2151 int agno, iagno, fwd, back, freei = 0, sword, rc;
2152 struct iag *aiagp = NULL, *biagp = NULL, *ciagp = NULL;
2153 struct metapage *amp, *bmp, *cmp, *dmp;
2154 struct inode *ipimap;
2160 struct jfs_sb_info *sbi;
2162 /* better have free extents.
2164 if (!iagp->nfreeexts) {
2165 jfs_error(imap->im_ipimap->i_sb, "no free extents\n");
2169 /* get the inode map inode.
2171 ipimap = imap->im_ipimap;
2172 sbi = JFS_SBI(ipimap->i_sb);
2174 amp = bmp = cmp = NULL;
2176 /* get the ag and iag numbers for this iag.
2178 agno = BLKTOAG(le64_to_cpu(iagp->agstart), sbi);
2179 iagno = le32_to_cpu(iagp->iagnum);
2181 /* check if this is the last free extent within the
2182 * iag. if so, the iag must be removed from the ag
2183 * free extent list, so get the iags preceding and
2184 * following the iag on this list.
2186 if (iagp->nfreeexts == cpu_to_le32(1)) {
2187 if ((fwd = le32_to_cpu(iagp->extfreefwd)) >= 0) {
2188 if ((rc = diIAGRead(imap, fwd, &)))
2190 aiagp = (struct iag *) amp->data;
2193 if ((back = le32_to_cpu(iagp->extfreeback)) >= 0) {
2194 if ((rc = diIAGRead(imap, back, &bmp)))
2196 biagp = (struct iag *) bmp->data;
2199 /* the iag has free extents. if all extents are free
2200 * (as is the case for a newly allocated iag), the iag
2201 * must be added to the ag free extent list, so get
2202 * the iag at the head of the list in preparation for
2203 * adding this iag to this list.
2206 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2207 if ((fwd = imap->im_agctl[agno].extfree) >= 0) {
2208 if ((rc = diIAGRead(imap, fwd, &)))
2210 aiagp = (struct iag *) amp->data;
2215 /* check if the iag has no free inodes. if so, the iag
2216 * will have to be added to the ag free inode list, so get
2217 * the iag at the head of the list in preparation for
2218 * adding this iag to this list. in doing this, we must
2219 * check if we already have the iag at the head of
2222 if (iagp->nfreeinos == 0) {
2223 freei = imap->im_agctl[agno].inofree;
2228 } else if (freei == back) {
2231 if ((rc = diIAGRead(imap, freei, &cmp)))
2233 ciagp = (struct iag *) cmp->data;
2235 if (ciagp == NULL) {
2236 jfs_error(imap->im_ipimap->i_sb,
2244 /* allocate disk space for the inode extent.
2246 if ((extno == 0) || (addressPXD(&iagp->inoext[extno - 1]) == 0))
2247 hint = ((s64) agno << sbi->bmap->db_agl2size) - 1;
2249 hint = addressPXD(&iagp->inoext[extno - 1]) +
2250 lengthPXD(&iagp->inoext[extno - 1]) - 1;
2252 if ((rc = dbAlloc(ipimap, hint, (s64) imap->im_nbperiext, &blkno)))
2255 /* compute the inode number of the first inode within the
2258 ino = (iagno << L2INOSPERIAG) + (extno << L2INOSPEREXT);
2260 /* initialize the inodes within the newly allocated extent a
2263 for (i = 0; i < imap->im_nbperiext; i += sbi->nbperpage) {
2264 /* get a buffer for this page of disk inodes.
2266 dmp = get_metapage(ipimap, blkno + i, PSIZE, 1);
2271 dp = (struct dinode *) dmp->data;
2273 /* initialize the inode number, mode, link count and
2274 * inode extent address.
2276 for (j = 0; j < INOSPERPAGE; j++, dp++, ino++) {
2277 dp->di_inostamp = cpu_to_le32(sbi->inostamp);
2278 dp->di_number = cpu_to_le32(ino);
2279 dp->di_fileset = cpu_to_le32(FILESYSTEM_I);
2282 PXDaddress(&(dp->di_ixpxd), blkno);
2283 PXDlength(&(dp->di_ixpxd), imap->im_nbperiext);
2285 write_metapage(dmp);
2288 /* if this is the last free extent within the iag, remove the
2289 * iag from the ag free extent list.
2291 if (iagp->nfreeexts == cpu_to_le32(1)) {
2293 aiagp->extfreeback = iagp->extfreeback;
2296 biagp->extfreefwd = iagp->extfreefwd;
2298 imap->im_agctl[agno].extfree =
2299 le32_to_cpu(iagp->extfreefwd);
2301 iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
2303 /* if the iag has all free extents (newly allocated iag),
2304 * add the iag to the ag free extent list.
2306 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2308 aiagp->extfreeback = cpu_to_le32(iagno);
2310 iagp->extfreefwd = cpu_to_le32(fwd);
2311 iagp->extfreeback = cpu_to_le32(-1);
2312 imap->im_agctl[agno].extfree = iagno;
2316 /* if the iag has no free inodes, add the iag to the
2317 * ag free inode list.
2319 if (iagp->nfreeinos == 0) {
2321 ciagp->inofreeback = cpu_to_le32(iagno);
2324 cpu_to_le32(imap->im_agctl[agno].inofree);
2325 iagp->inofreeback = cpu_to_le32(-1);
2326 imap->im_agctl[agno].inofree = iagno;
2329 /* initialize the extent descriptor of the extent. */
2330 PXDlength(&iagp->inoext[extno], imap->im_nbperiext);
2331 PXDaddress(&iagp->inoext[extno], blkno);
2333 /* initialize the working and persistent map of the extent.
2334 * the working map will be initialized such that
2335 * it indicates the first inode of the extent is allocated.
2337 iagp->wmap[extno] = cpu_to_le32(HIGHORDER);
2338 iagp->pmap[extno] = 0;
2340 /* update the free inode and free extent summary maps
2341 * for the extent to indicate the extent has free inodes
2342 * and no longer represents a free extent.
2344 sword = extno >> L2EXTSPERSUM;
2345 mask = HIGHORDER >> (extno & (EXTSPERSUM - 1));
2346 iagp->extsmap[sword] |= cpu_to_le32(mask);
2347 iagp->inosmap[sword] &= cpu_to_le32(~mask);
2349 /* update the free inode and free extent counts for the
2352 le32_add_cpu(&iagp->nfreeinos, (INOSPEREXT - 1));
2353 le32_add_cpu(&iagp->nfreeexts, -1);
2355 /* update the free and backed inode counts for the ag.
2357 imap->im_agctl[agno].numfree += (INOSPEREXT - 1);
2358 imap->im_agctl[agno].numinos += INOSPEREXT;
2360 /* update the free and backed inode counts for the inode map.
2362 atomic_add(INOSPEREXT - 1, &imap->im_numfree);
2363 atomic_add(INOSPEREXT, &imap->im_numinos);
2368 write_metapage(amp);
2370 write_metapage(bmp);
2372 write_metapage(cmp);
2378 /* release the iags.
2381 release_metapage(amp);
2383 release_metapage(bmp);
2385 release_metapage(cmp);
2392 * NAME: diNewIAG(imap,iagnop,agno)
2394 * FUNCTION: allocate a new iag for an allocation group.
2396 * first tries to allocate the iag from the inode map
2398 * if the list has free iags, the head of the list is removed
2399 * and returned to satisfy the request.
2400 * if the inode map's iag free list is empty, the inode map
2401 * is extended to hold a new iag. this new iag is initialized
2402 * and returned to satisfy the request.
2405 * imap - pointer to inode map control structure.
2406 * iagnop - pointer to an iag number set with the number of the
2407 * newly allocated iag upon successful return.
2408 * agno - allocation group number.
2409 * bpp - Buffer pointer to be filled in with new IAG's buffer
2413 * -ENOSPC - insufficient disk resources.
2417 * AG lock held on entry/exit;
2418 * write lock on the map is held inside;
2419 * read lock on the map is held on successful completion;
2421 * note: new iag transaction:
2422 * . synchronously write iag;
2423 * . write log of xtree and inode of imap;
2425 * . synchronous write of xtree (right to left, bottom to top);
2426 * . at start of logredo(): init in-memory imap with one additional iag page;
2427 * . at end of logredo(): re-read imap inode to determine
2431 diNewIAG(struct inomap * imap, int *iagnop, int agno, struct metapage ** mpp)
2435 struct inode *ipimap;
2436 struct super_block *sb;
2437 struct jfs_sb_info *sbi;
2438 struct metapage *mp;
2443 struct inode *iplist[1];
2445 /* pick up pointers to the inode map and mount inodes */
2446 ipimap = imap->im_ipimap;
2450 /* acquire the free iag lock */
2453 /* if there are any iags on the inode map free iag list,
2454 * allocate the iag from the head of the list.
2456 if (imap->im_freeiag >= 0) {
2457 /* pick up the iag number at the head of the list */
2458 iagno = imap->im_freeiag;
2460 /* determine the logical block number of the iag */
2461 blkno = IAGTOLBLK(iagno, sbi->l2nbperpage);
2463 /* no free iags. the inode map will have to be extented
2464 * to include a new iag.
2467 /* acquire inode map lock */
2468 IWRITE_LOCK(ipimap, RDWRLOCK_IMAP);
2470 if (ipimap->i_size >> L2PSIZE != imap->im_nextiag + 1) {
2471 IWRITE_UNLOCK(ipimap);
2472 IAGFREE_UNLOCK(imap);
2473 jfs_error(imap->im_ipimap->i_sb,
2474 "ipimap->i_size is wrong\n");
2479 /* get the next available iag number */
2480 iagno = imap->im_nextiag;
2482 /* make sure that we have not exceeded the maximum inode
2485 if (iagno > (MAXIAGS - 1)) {
2486 /* release the inode map lock */
2487 IWRITE_UNLOCK(ipimap);
2494 * synchronously append new iag page.
2496 /* determine the logical address of iag page to append */
2497 blkno = IAGTOLBLK(iagno, sbi->l2nbperpage);
2499 /* Allocate extent for new iag page */
2500 xlen = sbi->nbperpage;
2501 if ((rc = dbAlloc(ipimap, 0, (s64) xlen, &xaddr))) {
2502 /* release the inode map lock */
2503 IWRITE_UNLOCK(ipimap);
2509 * start transaction of update of the inode map
2510 * addressing structure pointing to the new iag page;
2512 tid = txBegin(sb, COMMIT_FORCE);
2513 mutex_lock(&JFS_IP(ipimap)->commit_mutex);
2515 /* update the inode map addressing structure to point to it */
2517 xtInsert(tid, ipimap, 0, blkno, xlen, &xaddr, 0))) {
2519 mutex_unlock(&JFS_IP(ipimap)->commit_mutex);
2520 /* Free the blocks allocated for the iag since it was
2521 * not successfully added to the inode map
2523 dbFree(ipimap, xaddr, (s64) xlen);
2525 /* release the inode map lock */
2526 IWRITE_UNLOCK(ipimap);
2531 /* update the inode map's inode to reflect the extension */
2532 ipimap->i_size += PSIZE;
2533 inode_add_bytes(ipimap, PSIZE);
2535 /* assign a buffer for the page */
2536 mp = get_metapage(ipimap, blkno, PSIZE, 0);
2539 * This is very unlikely since we just created the
2540 * extent, but let's try to handle it correctly
2542 xtTruncate(tid, ipimap, ipimap->i_size - PSIZE,
2547 mutex_unlock(&JFS_IP(ipimap)->commit_mutex);
2549 /* release the inode map lock */
2550 IWRITE_UNLOCK(ipimap);
2555 iagp = (struct iag *) mp->data;
2558 memset(iagp, 0, sizeof(struct iag));
2559 iagp->iagnum = cpu_to_le32(iagno);
2560 iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
2561 iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
2562 iagp->iagfree = cpu_to_le32(-1);
2563 iagp->nfreeinos = 0;
2564 iagp->nfreeexts = cpu_to_le32(EXTSPERIAG);
2566 /* initialize the free inode summary map (free extent
2567 * summary map initialization handled by bzero).
2569 for (i = 0; i < SMAPSZ; i++)
2570 iagp->inosmap[i] = cpu_to_le32(ONES);
2573 * Write and sync the metapage
2578 * txCommit(COMMIT_FORCE) will synchronously write address
2579 * index pages and inode after commit in careful update order
2580 * of address index pages (right to left, bottom up);
2583 rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
2586 mutex_unlock(&JFS_IP(ipimap)->commit_mutex);
2588 duplicateIXtree(sb, blkno, xlen, &xaddr);
2590 /* update the next available iag number */
2591 imap->im_nextiag += 1;
2593 /* Add the iag to the iag free list so we don't lose the iag
2594 * if a failure happens now.
2596 imap->im_freeiag = iagno;
2598 /* Until we have logredo working, we want the imap inode &
2599 * control page to be up to date.
2603 /* release the inode map lock */
2604 IWRITE_UNLOCK(ipimap);
2607 /* obtain read lock on map */
2608 IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
2611 if ((rc = diIAGRead(imap, iagno, &mp))) {
2612 IREAD_UNLOCK(ipimap);
2616 iagp = (struct iag *) mp->data;
2618 /* remove the iag from the iag free list */
2619 imap->im_freeiag = le32_to_cpu(iagp->iagfree);
2620 iagp->iagfree = cpu_to_le32(-1);
2622 /* set the return iag number and buffer pointer */
2627 /* release the iag free lock */
2628 IAGFREE_UNLOCK(imap);
2636 * FUNCTION: get the buffer for the specified iag within a fileset
2637 * or aggregate inode map.
2640 * imap - pointer to inode map control structure.
2641 * iagno - iag number.
2642 * bpp - point to buffer pointer to be filled in on successful
2646 * must have read lock on imap inode
2647 * (When called by diExtendFS, the filesystem is quiesced, therefore
2648 * the read lock is unnecessary.)
2654 static int diIAGRead(struct inomap * imap, int iagno, struct metapage ** mpp)
2656 struct inode *ipimap = imap->im_ipimap;
2659 /* compute the logical block number of the iag. */
2660 blkno = IAGTOLBLK(iagno, JFS_SBI(ipimap->i_sb)->l2nbperpage);
2663 *mpp = read_metapage(ipimap, blkno, PSIZE, 0);
2672 * NAME: diFindFree()
2674 * FUNCTION: find the first free bit in a word starting at
2675 * the specified bit position.
2678 * word - word to be examined.
2679 * start - starting bit position.
2682 * bit position of first free bit in the word or 32 if
2683 * no free bits were found.
2685 static int diFindFree(u32 word, int start)
2689 /* scan the word for the first free bit. */
2690 for (word <<= start, bitno = start; bitno < 32;
2691 bitno++, word <<= 1) {
2692 if ((word & HIGHORDER) == 0)
2699 * NAME: diUpdatePMap()
2701 * FUNCTION: Update the persistent map in an IAG for the allocation or
2702 * freeing of the specified inode.
2704 * PRE CONDITIONS: Working map has already been updated for allocate.
2707 * ipimap - Incore inode map inode
2708 * inum - Number of inode to mark in permanent map
2709 * is_free - If 'true' indicates inode should be marked freed, otherwise
2710 * indicates inode should be marked allocated.
2716 diUpdatePMap(struct inode *ipimap,
2717 unsigned long inum, bool is_free, struct tblock * tblk)
2721 struct metapage *mp;
2722 int iagno, ino, extno, bitno;
2723 struct inomap *imap;
2725 struct jfs_log *log;
2726 int lsn, difft, diffp;
2727 unsigned long flags;
2729 imap = JFS_IP(ipimap)->i_imap;
2730 /* get the iag number containing the inode */
2731 iagno = INOTOIAG(inum);
2732 /* make sure that the iag is contained within the map */
2733 if (iagno >= imap->im_nextiag) {
2734 jfs_error(ipimap->i_sb, "the iag is outside the map\n");
2738 IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
2739 rc = diIAGRead(imap, iagno, &mp);
2740 IREAD_UNLOCK(ipimap);
2743 metapage_wait_for_io(mp);
2744 iagp = (struct iag *) mp->data;
2745 /* get the inode number and extent number of the inode within
2746 * the iag and the inode number within the extent.
2748 ino = inum & (INOSPERIAG - 1);
2749 extno = ino >> L2INOSPEREXT;
2750 bitno = ino & (INOSPEREXT - 1);
2751 mask = HIGHORDER >> bitno;
2753 * mark the inode free in persistent map:
2756 /* The inode should have been allocated both in working
2757 * map and in persistent map;
2758 * the inode will be freed from working map at the release
2759 * of last reference release;
2761 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
2762 jfs_error(ipimap->i_sb,
2763 "inode %ld not marked as allocated in wmap!\n",
2766 if (!(le32_to_cpu(iagp->pmap[extno]) & mask)) {
2767 jfs_error(ipimap->i_sb,
2768 "inode %ld not marked as allocated in pmap!\n",
2771 /* update the bitmap for the extent of the freed inode */
2772 iagp->pmap[extno] &= cpu_to_le32(~mask);
2775 * mark the inode allocated in persistent map:
2778 /* The inode should be already allocated in the working map
2779 * and should be free in persistent map;
2781 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
2782 release_metapage(mp);
2783 jfs_error(ipimap->i_sb,
2784 "the inode is not allocated in the working map\n");
2787 if ((le32_to_cpu(iagp->pmap[extno]) & mask) != 0) {
2788 release_metapage(mp);
2789 jfs_error(ipimap->i_sb,
2790 "the inode is not free in the persistent map\n");
2793 /* update the bitmap for the extent of the allocated inode */
2794 iagp->pmap[extno] |= cpu_to_le32(mask);
2800 log = JFS_SBI(tblk->sb)->log;
2801 LOGSYNC_LOCK(log, flags);
2803 /* inherit older/smaller lsn */
2804 logdiff(difft, lsn, log);
2805 logdiff(diffp, mp->lsn, log);
2806 if (difft < diffp) {
2808 /* move mp after tblock in logsync list */
2809 list_move(&mp->synclist, &tblk->synclist);
2811 /* inherit younger/larger clsn */
2813 logdiff(difft, tblk->clsn, log);
2814 logdiff(diffp, mp->clsn, log);
2816 mp->clsn = tblk->clsn;
2820 /* insert mp after tblock in logsync list */
2822 list_add(&mp->synclist, &tblk->synclist);
2823 mp->clsn = tblk->clsn;
2825 LOGSYNC_UNLOCK(log, flags);
2833 * function: update imap for extendfs();
2835 * note: AG size has been increased s.t. each k old contiguous AGs are
2836 * coalesced into a new AG;
2838 int diExtendFS(struct inode *ipimap, struct inode *ipbmap)
2841 struct inomap *imap = JFS_IP(ipimap)->i_imap;
2842 struct iag *iagp = NULL, *hiagp = NULL;
2843 struct bmap *mp = JFS_SBI(ipbmap->i_sb)->bmap;
2844 struct metapage *bp, *hbp;
2846 int numinos, xnuminos = 0, xnumfree = 0;
2849 jfs_info("diExtendFS: nextiag:%d numinos:%d numfree:%d",
2850 imap->im_nextiag, atomic_read(&imap->im_numinos),
2851 atomic_read(&imap->im_numfree));
2856 * coalesce contiguous k (newAGSize/oldAGSize) AGs;
2857 * i.e., (AGi, ..., AGj) where i = k*n and j = k*(n+1) - 1 to AGn;
2858 * note: new AG size = old AG size * (2**x).
2861 /* init per AG control information im_agctl[] */
2862 for (i = 0; i < MAXAG; i++) {
2863 imap->im_agctl[i].inofree = -1;
2864 imap->im_agctl[i].extfree = -1;
2865 imap->im_agctl[i].numinos = 0; /* number of backed inodes */
2866 imap->im_agctl[i].numfree = 0; /* number of free backed inodes */
2870 * process each iag page of the map.
2872 * rebuild AG Free Inode List, AG Free Inode Extent List;
2874 for (i = 0; i < imap->im_nextiag; i++) {
2875 if ((rc = diIAGRead(imap, i, &bp))) {
2879 iagp = (struct iag *) bp->data;
2880 if (le32_to_cpu(iagp->iagnum) != i) {
2881 release_metapage(bp);
2882 jfs_error(ipimap->i_sb, "unexpected value of iagnum\n");
2886 /* leave free iag in the free iag list */
2887 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2888 release_metapage(bp);
2892 agstart = le64_to_cpu(iagp->agstart);
2893 n = agstart >> mp->db_agl2size;
2894 iagp->agstart = cpu_to_le64((s64)n << mp->db_agl2size);
2896 /* compute backed inodes */
2897 numinos = (EXTSPERIAG - le32_to_cpu(iagp->nfreeexts))
2900 /* merge AG backed inodes */
2901 imap->im_agctl[n].numinos += numinos;
2902 xnuminos += numinos;
2905 /* if any backed free inodes, insert at AG free inode list */
2906 if ((int) le32_to_cpu(iagp->nfreeinos) > 0) {
2907 if ((head = imap->im_agctl[n].inofree) == -1) {
2908 iagp->inofreefwd = cpu_to_le32(-1);
2909 iagp->inofreeback = cpu_to_le32(-1);
2911 if ((rc = diIAGRead(imap, head, &hbp))) {
2915 hiagp = (struct iag *) hbp->data;
2916 hiagp->inofreeback = iagp->iagnum;
2917 iagp->inofreefwd = cpu_to_le32(head);
2918 iagp->inofreeback = cpu_to_le32(-1);
2919 write_metapage(hbp);
2922 imap->im_agctl[n].inofree =
2923 le32_to_cpu(iagp->iagnum);
2925 /* merge AG backed free inodes */
2926 imap->im_agctl[n].numfree +=
2927 le32_to_cpu(iagp->nfreeinos);
2928 xnumfree += le32_to_cpu(iagp->nfreeinos);
2931 /* if any free extents, insert at AG free extent list */
2932 if (le32_to_cpu(iagp->nfreeexts) > 0) {
2933 if ((head = imap->im_agctl[n].extfree) == -1) {
2934 iagp->extfreefwd = cpu_to_le32(-1);
2935 iagp->extfreeback = cpu_to_le32(-1);
2937 if ((rc = diIAGRead(imap, head, &hbp))) {
2941 hiagp = (struct iag *) hbp->data;
2942 hiagp->extfreeback = iagp->iagnum;
2943 iagp->extfreefwd = cpu_to_le32(head);
2944 iagp->extfreeback = cpu_to_le32(-1);
2945 write_metapage(hbp);
2948 imap->im_agctl[n].extfree =
2949 le32_to_cpu(iagp->iagnum);
2956 if (xnuminos != atomic_read(&imap->im_numinos) ||
2957 xnumfree != atomic_read(&imap->im_numfree)) {
2958 jfs_error(ipimap->i_sb, "numinos or numfree incorrect\n");
2969 * serialization: IWRITE_LOCK held on entry/exit
2971 * note: shadow page with regular inode (rel.2);
2973 static void duplicateIXtree(struct super_block *sb, s64 blkno,
2974 int xlen, s64 *xaddr)
2976 struct jfs_superblock *j_sb;
2977 struct buffer_head *bh;
2981 /* if AIT2 ipmap2 is bad, do not try to update it */
2982 if (JFS_SBI(sb)->mntflag & JFS_BAD_SAIT) /* s_flag */
2984 ip = diReadSpecial(sb, FILESYSTEM_I, 1);
2986 JFS_SBI(sb)->mntflag |= JFS_BAD_SAIT;
2987 if (readSuper(sb, &bh))
2989 j_sb = (struct jfs_superblock *)bh->b_data;
2990 j_sb->s_flag |= cpu_to_le32(JFS_BAD_SAIT);
2992 mark_buffer_dirty(bh);
2993 sync_dirty_buffer(bh);
2998 /* start transaction */
2999 tid = txBegin(sb, COMMIT_FORCE);
3000 /* update the inode map addressing structure to point to it */
3001 if (xtInsert(tid, ip, 0, blkno, xlen, xaddr, 0)) {
3002 JFS_SBI(sb)->mntflag |= JFS_BAD_SAIT;
3007 /* update the inode map's inode to reflect the extension */
3008 ip->i_size += PSIZE;
3009 inode_add_bytes(ip, PSIZE);
3010 txCommit(tid, 1, &ip, COMMIT_FORCE);
3017 * NAME: copy_from_dinode()
3019 * FUNCTION: Copies inode info from disk inode to in-memory inode
3023 * -ENOMEM - insufficient memory
3025 static int copy_from_dinode(struct dinode * dip, struct inode *ip)
3027 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
3028 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
3030 jfs_ip->fileset = le32_to_cpu(dip->di_fileset);
3031 jfs_ip->mode2 = le32_to_cpu(dip->di_mode);
3032 jfs_set_inode_flags(ip);
3034 ip->i_mode = le32_to_cpu(dip->di_mode) & 0xffff;
3035 if (sbi->umask != -1) {
3036 ip->i_mode = (ip->i_mode & ~0777) | (0777 & ~sbi->umask);
3037 /* For directories, add x permission if r is allowed by umask */
3038 if (S_ISDIR(ip->i_mode)) {
3039 if (ip->i_mode & 0400)
3041 if (ip->i_mode & 0040)
3043 if (ip->i_mode & 0004)
3047 set_nlink(ip, le32_to_cpu(dip->di_nlink));
3049 jfs_ip->saved_uid = make_kuid(&init_user_ns, le32_to_cpu(dip->di_uid));
3050 if (!uid_valid(sbi->uid))
3051 ip->i_uid = jfs_ip->saved_uid;
3053 ip->i_uid = sbi->uid;
3056 jfs_ip->saved_gid = make_kgid(&init_user_ns, le32_to_cpu(dip->di_gid));
3057 if (!gid_valid(sbi->gid))
3058 ip->i_gid = jfs_ip->saved_gid;
3060 ip->i_gid = sbi->gid;
3063 ip->i_size = le64_to_cpu(dip->di_size);
3064 ip->i_atime.tv_sec = le32_to_cpu(dip->di_atime.tv_sec);
3065 ip->i_atime.tv_nsec = le32_to_cpu(dip->di_atime.tv_nsec);
3066 ip->i_mtime.tv_sec = le32_to_cpu(dip->di_mtime.tv_sec);
3067 ip->i_mtime.tv_nsec = le32_to_cpu(dip->di_mtime.tv_nsec);
3068 inode_set_ctime(ip, le32_to_cpu(dip->di_ctime.tv_sec),
3069 le32_to_cpu(dip->di_ctime.tv_nsec));
3070 ip->i_blocks = LBLK2PBLK(ip->i_sb, le64_to_cpu(dip->di_nblocks));
3071 ip->i_generation = le32_to_cpu(dip->di_gen);
3073 jfs_ip->ixpxd = dip->di_ixpxd; /* in-memory pxd's are little-endian */
3074 jfs_ip->acl = dip->di_acl; /* as are dxd's */
3075 jfs_ip->ea = dip->di_ea;
3076 jfs_ip->next_index = le32_to_cpu(dip->di_next_index);
3077 jfs_ip->otime = le32_to_cpu(dip->di_otime.tv_sec);
3078 jfs_ip->acltype = le32_to_cpu(dip->di_acltype);
3080 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode)) {
3081 jfs_ip->dev = le32_to_cpu(dip->di_rdev);
3082 ip->i_rdev = new_decode_dev(jfs_ip->dev);
3085 if (S_ISDIR(ip->i_mode)) {
3086 memcpy(&jfs_ip->u.dir, &dip->u._dir, 384);
3087 } else if (S_ISREG(ip->i_mode) || S_ISLNK(ip->i_mode)) {
3088 memcpy(&jfs_ip->i_xtroot, &dip->di_xtroot, 288);
3090 memcpy(&jfs_ip->i_inline_ea, &dip->di_inlineea, 128);
3092 /* Zero the in-memory-only stuff */
3094 jfs_ip->btindex = 0;
3095 jfs_ip->btorder = 0;
3098 jfs_ip->atlhead = 0;
3099 jfs_ip->atltail = 0;
3105 * NAME: copy_to_dinode()
3107 * FUNCTION: Copies inode info from in-memory inode to disk inode
3109 static void copy_to_dinode(struct dinode * dip, struct inode *ip)
3111 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
3112 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
3114 dip->di_fileset = cpu_to_le32(jfs_ip->fileset);
3115 dip->di_inostamp = cpu_to_le32(sbi->inostamp);
3116 dip->di_number = cpu_to_le32(ip->i_ino);
3117 dip->di_gen = cpu_to_le32(ip->i_generation);
3118 dip->di_size = cpu_to_le64(ip->i_size);
3119 dip->di_nblocks = cpu_to_le64(PBLK2LBLK(ip->i_sb, ip->i_blocks));
3120 dip->di_nlink = cpu_to_le32(ip->i_nlink);
3121 if (!uid_valid(sbi->uid))
3122 dip->di_uid = cpu_to_le32(i_uid_read(ip));
3124 dip->di_uid =cpu_to_le32(from_kuid(&init_user_ns,
3125 jfs_ip->saved_uid));
3126 if (!gid_valid(sbi->gid))
3127 dip->di_gid = cpu_to_le32(i_gid_read(ip));
3129 dip->di_gid = cpu_to_le32(from_kgid(&init_user_ns,
3130 jfs_ip->saved_gid));
3132 * mode2 is only needed for storing the higher order bits.
3133 * Trust i_mode for the lower order ones
3135 if (sbi->umask == -1)
3136 dip->di_mode = cpu_to_le32((jfs_ip->mode2 & 0xffff0000) |
3138 else /* Leave the original permissions alone */
3139 dip->di_mode = cpu_to_le32(jfs_ip->mode2);
3141 dip->di_atime.tv_sec = cpu_to_le32(ip->i_atime.tv_sec);
3142 dip->di_atime.tv_nsec = cpu_to_le32(ip->i_atime.tv_nsec);
3143 dip->di_ctime.tv_sec = cpu_to_le32(inode_get_ctime(ip).tv_sec);
3144 dip->di_ctime.tv_nsec = cpu_to_le32(inode_get_ctime(ip).tv_nsec);
3145 dip->di_mtime.tv_sec = cpu_to_le32(ip->i_mtime.tv_sec);
3146 dip->di_mtime.tv_nsec = cpu_to_le32(ip->i_mtime.tv_nsec);
3147 dip->di_ixpxd = jfs_ip->ixpxd; /* in-memory pxd's are little-endian */
3148 dip->di_acl = jfs_ip->acl; /* as are dxd's */
3149 dip->di_ea = jfs_ip->ea;
3150 dip->di_next_index = cpu_to_le32(jfs_ip->next_index);
3151 dip->di_otime.tv_sec = cpu_to_le32(jfs_ip->otime);
3152 dip->di_otime.tv_nsec = 0;
3153 dip->di_acltype = cpu_to_le32(jfs_ip->acltype);
3154 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode))
3155 dip->di_rdev = cpu_to_le32(jfs_ip->dev);