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);
107 jfs_err("diMount: kmalloc returned NULL!");
111 /* read the on-disk inode map control structure. */
113 mp = read_metapage(ipimap,
114 IMAPBLKNO << JFS_SBI(ipimap->i_sb)->l2nbperpage,
121 /* copy the on-disk version to the in-memory version. */
122 dinom_le = (struct dinomap_disk *) mp->data;
123 imap->im_freeiag = le32_to_cpu(dinom_le->in_freeiag);
124 imap->im_nextiag = le32_to_cpu(dinom_le->in_nextiag);
125 atomic_set(&imap->im_numinos, le32_to_cpu(dinom_le->in_numinos));
126 atomic_set(&imap->im_numfree, le32_to_cpu(dinom_le->in_numfree));
127 imap->im_nbperiext = le32_to_cpu(dinom_le->in_nbperiext);
128 imap->im_l2nbperiext = le32_to_cpu(dinom_le->in_l2nbperiext);
129 for (index = 0; index < MAXAG; index++) {
130 imap->im_agctl[index].inofree =
131 le32_to_cpu(dinom_le->in_agctl[index].inofree);
132 imap->im_agctl[index].extfree =
133 le32_to_cpu(dinom_le->in_agctl[index].extfree);
134 imap->im_agctl[index].numinos =
135 le32_to_cpu(dinom_le->in_agctl[index].numinos);
136 imap->im_agctl[index].numfree =
137 le32_to_cpu(dinom_le->in_agctl[index].numfree);
140 /* release the buffer. */
141 release_metapage(mp);
144 * allocate/initialize inode allocation map locks
146 /* allocate and init iag free list lock */
147 IAGFREE_LOCK_INIT(imap);
149 /* allocate and init ag list locks */
150 for (index = 0; index < MAXAG; index++) {
151 AG_LOCK_INIT(imap, index);
154 /* bind the inode map inode and inode map control structure
157 imap->im_ipimap = ipimap;
158 JFS_IP(ipimap)->i_imap = imap;
167 * FUNCTION: write to disk the incore inode map control structures for
168 * a fileset or aggregate at unmount time.
171 * ipimap - pointer to inode map inode for the aggregate or fileset.
175 * -ENOMEM - insufficient free virtual memory.
178 int diUnmount(struct inode *ipimap, int mounterror)
180 struct inomap *imap = JFS_IP(ipimap)->i_imap;
183 * update the on-disk inode map control structure
186 if (!(mounterror || isReadOnly(ipimap)))
190 * Invalidate the page cache buffers
192 truncate_inode_pages(ipimap->i_mapping, 0);
195 * free in-memory control structure
198 JFS_IP(ipimap)->i_imap = NULL;
207 int diSync(struct inode *ipimap)
209 struct dinomap_disk *dinom_le;
210 struct inomap *imp = JFS_IP(ipimap)->i_imap;
215 * write imap global conrol page
217 /* read the on-disk inode map control structure */
218 mp = get_metapage(ipimap,
219 IMAPBLKNO << JFS_SBI(ipimap->i_sb)->l2nbperpage,
222 jfs_err("diSync: get_metapage failed!");
226 /* copy the in-memory version to the on-disk version */
227 dinom_le = (struct dinomap_disk *) mp->data;
228 dinom_le->in_freeiag = cpu_to_le32(imp->im_freeiag);
229 dinom_le->in_nextiag = cpu_to_le32(imp->im_nextiag);
230 dinom_le->in_numinos = cpu_to_le32(atomic_read(&imp->im_numinos));
231 dinom_le->in_numfree = cpu_to_le32(atomic_read(&imp->im_numfree));
232 dinom_le->in_nbperiext = cpu_to_le32(imp->im_nbperiext);
233 dinom_le->in_l2nbperiext = cpu_to_le32(imp->im_l2nbperiext);
234 for (index = 0; index < MAXAG; index++) {
235 dinom_le->in_agctl[index].inofree =
236 cpu_to_le32(imp->im_agctl[index].inofree);
237 dinom_le->in_agctl[index].extfree =
238 cpu_to_le32(imp->im_agctl[index].extfree);
239 dinom_le->in_agctl[index].numinos =
240 cpu_to_le32(imp->im_agctl[index].numinos);
241 dinom_le->in_agctl[index].numfree =
242 cpu_to_le32(imp->im_agctl[index].numfree);
245 /* write out the control structure */
249 * write out dirty pages of imap
251 filemap_write_and_wait(ipimap->i_mapping);
253 diWriteSpecial(ipimap, 0);
262 * FUNCTION: initialize an incore inode from disk.
264 * on entry, the specifed incore inode should itself
265 * specify the disk inode number corresponding to the
266 * incore inode (i.e. i_number should be initialized).
268 * this routine handles incore inode initialization for
269 * both "special" and "regular" inodes. special inodes
270 * are those required early in the mount process and
271 * require special handling since much of the file system
272 * is not yet initialized. these "special" inodes are
273 * identified by a NULL inode map inode pointer and are
274 * actually initialized by a call to diReadSpecial().
276 * for regular inodes, the iag describing the disk inode
277 * is read from disk to determine the inode extent address
278 * for the disk inode. with the inode extent address in
279 * hand, the page of the extent that contains the disk
280 * inode is read and the disk inode is copied to the
284 * ip - pointer to incore inode to be initialized from disk.
289 * -ENOMEM - insufficient memory
292 int diRead(struct inode *ip)
294 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
295 int iagno, ino, extno, rc;
296 struct inode *ipimap;
304 unsigned long pageno;
307 jfs_info("diRead: ino = %ld", ip->i_ino);
309 ipimap = sbi->ipimap;
310 JFS_IP(ip)->ipimap = ipimap;
312 /* determine the iag number for this inode (number) */
313 iagno = INOTOIAG(ip->i_ino);
316 imap = JFS_IP(ipimap)->i_imap;
317 IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
318 rc = diIAGRead(imap, iagno, &mp);
319 IREAD_UNLOCK(ipimap);
321 jfs_err("diRead: diIAGRead returned %d", rc);
325 iagp = (struct iag *) mp->data;
327 /* determine inode extent that holds the disk inode */
328 ino = ip->i_ino & (INOSPERIAG - 1);
329 extno = ino >> L2INOSPEREXT;
331 if ((lengthPXD(&iagp->inoext[extno]) != imap->im_nbperiext) ||
332 (addressPXD(&iagp->inoext[extno]) == 0)) {
333 release_metapage(mp);
337 /* get disk block number of the page within the inode extent
338 * that holds the disk inode.
340 blkno = INOPBLK(&iagp->inoext[extno], ino, sbi->l2nbperpage);
342 /* get the ag for the iag */
343 agstart = le64_to_cpu(iagp->agstart);
345 release_metapage(mp);
347 rel_inode = (ino & (INOSPERPAGE - 1));
348 pageno = blkno >> sbi->l2nbperpage;
350 if ((block_offset = ((u32) blkno & (sbi->nbperpage - 1)))) {
352 * OS/2 didn't always align inode extents on page boundaries
355 (sbi->nbperpage - block_offset) << sbi->l2niperblk;
357 if (rel_inode < inodes_left)
358 rel_inode += block_offset << sbi->l2niperblk;
361 rel_inode -= inodes_left;
365 /* read the page of disk inode */
366 mp = read_metapage(ipimap, pageno << sbi->l2nbperpage, PSIZE, 1);
368 jfs_err("diRead: read_metapage failed");
372 /* locate the disk inode requested */
373 dp = (struct dinode *) mp->data;
376 if (ip->i_ino != le32_to_cpu(dp->di_number)) {
377 jfs_error(ip->i_sb, "i_ino != di_number\n");
379 } else if (le32_to_cpu(dp->di_nlink) == 0)
382 /* copy the disk inode to the in-memory inode */
383 rc = copy_from_dinode(dp, ip);
385 release_metapage(mp);
387 /* set the ag for the inode */
388 JFS_IP(ip)->agstart = agstart;
389 JFS_IP(ip)->active_ag = -1;
396 * NAME: diReadSpecial()
398 * FUNCTION: initialize a 'special' inode from disk.
400 * this routines handles aggregate level inodes. The
401 * inode cache cannot differentiate between the
402 * aggregate inodes and the filesystem inodes, so we
403 * handle these here. We don't actually use the aggregate
404 * inode map, since these inodes are at a fixed location
405 * and in some cases the aggregate inode map isn't initialized
409 * sb - filesystem superblock
410 * inum - aggregate inode number
411 * secondary - 1 if secondary aggregate inode table
414 * new inode - success
417 struct inode *diReadSpecial(struct super_block *sb, ino_t inum, int secondary)
419 struct jfs_sb_info *sbi = JFS_SBI(sb);
427 jfs_err("diReadSpecial: new_inode returned NULL!");
432 address = addressPXD(&sbi->ait2) >> sbi->l2nbperpage;
433 JFS_IP(ip)->ipimap = sbi->ipaimap2;
435 address = AITBL_OFF >> L2PSIZE;
436 JFS_IP(ip)->ipimap = sbi->ipaimap;
439 ASSERT(inum < INOSPEREXT);
443 address += inum >> 3; /* 8 inodes per 4K page */
445 /* read the page of fixed disk inode (AIT) in raw mode */
446 mp = read_metapage(ip, address << sbi->l2nbperpage, PSIZE, 1);
448 set_nlink(ip, 1); /* Don't want iput() deleting it */
453 /* get the pointer to the disk inode of interest */
454 dp = (struct dinode *) (mp->data);
455 dp += inum % 8; /* 8 inodes per 4K page */
457 /* copy on-disk inode to in-memory inode */
458 if ((copy_from_dinode(dp, ip)) != 0) {
459 /* handle bad return by returning NULL for ip */
460 set_nlink(ip, 1); /* Don't want iput() deleting it */
462 /* release the page */
463 release_metapage(mp);
468 ip->i_mapping->a_ops = &jfs_metapage_aops;
469 mapping_set_gfp_mask(ip->i_mapping, GFP_NOFS);
471 /* Allocations to metadata inodes should not affect quotas */
472 ip->i_flags |= S_NOQUOTA;
474 if ((inum == FILESYSTEM_I) && (JFS_IP(ip)->ipimap == sbi->ipaimap)) {
475 sbi->gengen = le32_to_cpu(dp->di_gengen);
476 sbi->inostamp = le32_to_cpu(dp->di_inostamp);
479 /* release the page */
480 release_metapage(mp);
488 * NAME: diWriteSpecial()
490 * FUNCTION: Write the special inode to disk
494 * secondary - 1 if secondary aggregate inode table
496 * RETURN VALUES: none
499 void diWriteSpecial(struct inode *ip, int secondary)
501 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
504 ino_t inum = ip->i_ino;
508 address = addressPXD(&sbi->ait2) >> sbi->l2nbperpage;
510 address = AITBL_OFF >> L2PSIZE;
512 ASSERT(inum < INOSPEREXT);
514 address += inum >> 3; /* 8 inodes per 4K page */
516 /* read the page of fixed disk inode (AIT) in raw mode */
517 mp = read_metapage(ip, address << sbi->l2nbperpage, PSIZE, 1);
519 jfs_err("diWriteSpecial: failed to read aggregate inode extent!");
523 /* get the pointer to the disk inode of interest */
524 dp = (struct dinode *) (mp->data);
525 dp += inum % 8; /* 8 inodes per 4K page */
527 /* copy on-disk inode to in-memory inode */
528 copy_to_dinode(dp, ip);
529 memcpy(&dp->di_xtroot, &JFS_IP(ip)->i_xtroot, 288);
531 if (inum == FILESYSTEM_I)
532 dp->di_gengen = cpu_to_le32(sbi->gengen);
539 * NAME: diFreeSpecial()
541 * FUNCTION: Free allocated space for special inode
543 void diFreeSpecial(struct inode *ip)
546 jfs_err("diFreeSpecial called with NULL ip!");
549 filemap_write_and_wait(ip->i_mapping);
550 truncate_inode_pages(ip->i_mapping, 0);
559 * FUNCTION: write the on-disk inode portion of the in-memory inode
560 * to its corresponding on-disk inode.
562 * on entry, the specifed incore inode should itself
563 * specify the disk inode number corresponding to the
564 * incore inode (i.e. i_number should be initialized).
566 * the inode contains the inode extent address for the disk
567 * inode. with the inode extent address in hand, the
568 * page of the extent that contains the disk inode is
569 * read and the disk inode portion of the incore inode
570 * is copied to the disk inode.
573 * tid - transacation id
574 * ip - pointer to incore inode to be written to the inode extent.
580 int diWrite(tid_t tid, struct inode *ip)
582 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
583 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
591 unsigned long pageno;
594 struct inode *ipimap;
597 struct tlock *ditlck, *tlck;
598 struct linelock *dilinelock, *ilinelock;
602 ipimap = jfs_ip->ipimap;
604 ino = ip->i_ino & (INOSPERIAG - 1);
606 if (!addressPXD(&(jfs_ip->ixpxd)) ||
607 (lengthPXD(&(jfs_ip->ixpxd)) !=
608 JFS_IP(ipimap)->i_imap->im_nbperiext)) {
609 jfs_error(ip->i_sb, "ixpxd invalid\n");
614 * read the page of disk inode containing the specified inode:
616 /* compute the block address of the page */
617 blkno = INOPBLK(&(jfs_ip->ixpxd), ino, sbi->l2nbperpage);
619 rel_inode = (ino & (INOSPERPAGE - 1));
620 pageno = blkno >> sbi->l2nbperpage;
622 if ((block_offset = ((u32) blkno & (sbi->nbperpage - 1)))) {
624 * OS/2 didn't always align inode extents on page boundaries
627 (sbi->nbperpage - block_offset) << sbi->l2niperblk;
629 if (rel_inode < inodes_left)
630 rel_inode += block_offset << sbi->l2niperblk;
633 rel_inode -= inodes_left;
636 /* read the page of disk inode */
638 mp = read_metapage(ipimap, pageno << sbi->l2nbperpage, PSIZE, 1);
642 /* get the pointer to the disk inode */
643 dp = (struct dinode *) mp->data;
646 dioffset = (ino & (INOSPERPAGE - 1)) << L2DISIZE;
649 * acquire transaction lock on the on-disk inode;
650 * N.B. tlock is acquired on ipimap not ip;
653 txLock(tid, ipimap, mp, tlckINODE | tlckENTRY)) == NULL)
655 dilinelock = (struct linelock *) & ditlck->lock;
658 * copy btree root from in-memory inode to on-disk inode
660 * (tlock is taken from inline B+-tree root in in-memory
661 * inode when the B+-tree root is updated, which is pointed
662 * by jfs_ip->blid as well as being on tx tlock list)
664 * further processing of btree root is based on the copy
665 * in in-memory inode, where txLog() will log from, and,
666 * for xtree root, txUpdateMap() will update map and reset
670 if (S_ISDIR(ip->i_mode) && (lid = jfs_ip->xtlid)) {
672 * This is the special xtree inside the directory for storing
673 * the directory table
679 tlck = lid_to_tlock(lid);
680 assert(tlck->type & tlckXTREE);
681 tlck->type |= tlckBTROOT;
683 ilinelock = (struct linelock *) & tlck->lock;
686 * copy xtree root from inode to dinode:
688 p = &jfs_ip->i_xtroot;
689 xp = (xtpage_t *) &dp->di_dirtable;
691 for (n = 0; n < ilinelock->index; n++, lv++) {
692 memcpy(&xp->xad[lv->offset], &p->xad[lv->offset],
693 lv->length << L2XTSLOTSIZE);
696 /* reset on-disk (metadata page) xtree XAD_NEW bit */
697 xad = &xp->xad[XTENTRYSTART];
698 for (n = XTENTRYSTART;
699 n < le16_to_cpu(xp->header.nextindex); n++, xad++)
700 if (xad->flag & (XAD_NEW | XAD_EXTENDED))
701 xad->flag &= ~(XAD_NEW | XAD_EXTENDED);
704 if ((lid = jfs_ip->blid) == 0)
708 tlck = lid_to_tlock(lid);
710 tlck->type |= tlckBTROOT;
712 ilinelock = (struct linelock *) & tlck->lock;
715 * regular file: 16 byte (XAD slot) granularity
717 if (type & tlckXTREE) {
722 * copy xtree root from inode to dinode:
724 p = &jfs_ip->i_xtroot;
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 * directory: 32 byte (directory entry slot) granularity
742 else if (type & tlckDTREE) {
746 * copy dtree root from inode to dinode:
748 p = (dtpage_t *) &jfs_ip->i_dtroot;
749 xp = (dtpage_t *) & dp->di_dtroot;
751 for (n = 0; n < ilinelock->index; n++, lv++) {
752 memcpy(&xp->slot[lv->offset], &p->slot[lv->offset],
753 lv->length << L2DTSLOTSIZE);
756 jfs_err("diWrite: UFO tlock");
761 * copy inline symlink from in-memory inode to on-disk inode
763 if (S_ISLNK(ip->i_mode) && ip->i_size < IDATASIZE) {
764 lv = & dilinelock->lv[dilinelock->index];
765 lv->offset = (dioffset + 2 * 128) >> L2INODESLOTSIZE;
767 memcpy(&dp->di_fastsymlink, jfs_ip->i_inline, IDATASIZE);
771 * copy inline data from in-memory inode to on-disk inode:
772 * 128 byte slot granularity
774 if (test_cflag(COMMIT_Inlineea, ip)) {
775 lv = & dilinelock->lv[dilinelock->index];
776 lv->offset = (dioffset + 3 * 128) >> L2INODESLOTSIZE;
778 memcpy(&dp->di_inlineea, jfs_ip->i_inline_ea, INODESLOTSIZE);
781 clear_cflag(COMMIT_Inlineea, ip);
785 * lock/copy inode base: 128 byte slot granularity
787 lv = & dilinelock->lv[dilinelock->index];
788 lv->offset = dioffset >> L2INODESLOTSIZE;
789 copy_to_dinode(dp, ip);
790 if (test_and_clear_cflag(COMMIT_Dirtable, ip)) {
792 memcpy(&dp->di_dirtable, &jfs_ip->i_dirtable, 96);
797 /* release the buffer holding the updated on-disk inode.
798 * the buffer will be later written by commit processing.
809 * FUNCTION: free a specified inode from the inode working map
810 * for a fileset or aggregate.
812 * if the inode to be freed represents the first (only)
813 * free inode within the iag, the iag will be placed on
814 * the ag free inode list.
816 * freeing the inode will cause the inode extent to be
817 * freed if the inode is the only allocated inode within
818 * the extent. in this case all the disk resource backing
819 * up the inode extent will be freed. in addition, the iag
820 * will be placed on the ag extent free list if the extent
821 * is the first free extent in the iag. if freeing the
822 * extent also means that no free inodes will exist for
823 * the iag, the iag will also be removed from the ag free
826 * the iag describing the inode will be freed if the extent
827 * is to be freed and it is the only backed extent within
828 * the iag. in this case, the iag will be removed from the
829 * ag free extent list and ag free inode list and placed on
830 * the inode map's free iag list.
832 * a careful update approach is used to provide consistency
833 * in the face of updates to multiple buffers. under this
834 * approach, all required buffers are obtained before making
835 * any updates and are held until all updates are complete.
838 * ip - inode to be freed.
844 int diFree(struct inode *ip)
847 ino_t inum = ip->i_ino;
848 struct iag *iagp, *aiagp, *biagp, *ciagp, *diagp;
849 struct metapage *mp, *amp, *bmp, *cmp, *dmp;
850 int iagno, ino, extno, bitno, sword, agno;
853 struct inode *ipimap = JFS_SBI(ip->i_sb)->ipimap;
854 struct inomap *imap = JFS_IP(ipimap)->i_imap;
857 struct inode *iplist[3];
859 struct pxd_lock *pxdlock;
862 * This is just to suppress compiler warnings. The same logic that
863 * references these variables is used to initialize them.
865 aiagp = biagp = ciagp = diagp = NULL;
867 /* get the iag number containing the inode.
869 iagno = INOTOIAG(inum);
871 /* make sure that the iag is contained within
874 if (iagno >= imap->im_nextiag) {
875 print_hex_dump(KERN_ERR, "imap: ", DUMP_PREFIX_ADDRESS, 16, 4,
877 jfs_error(ip->i_sb, "inum = %d, iagno = %d, nextiag = %d\n",
878 (uint) inum, iagno, imap->im_nextiag);
882 /* get the allocation group for this ino.
884 agno = BLKTOAG(JFS_IP(ip)->agstart, JFS_SBI(ip->i_sb));
886 /* Lock the AG specific inode map information
890 /* Obtain read lock in imap inode. Don't release it until we have
891 * read all of the IAG's that we are going to.
893 IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
897 if ((rc = diIAGRead(imap, iagno, &mp))) {
898 IREAD_UNLOCK(ipimap);
899 AG_UNLOCK(imap, agno);
902 iagp = (struct iag *) mp->data;
904 /* get the inode number and extent number of the inode within
905 * the iag and the inode number within the extent.
907 ino = inum & (INOSPERIAG - 1);
908 extno = ino >> L2INOSPEREXT;
909 bitno = ino & (INOSPEREXT - 1);
910 mask = HIGHORDER >> bitno;
912 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
913 jfs_error(ip->i_sb, "wmap shows inode already free\n");
916 if (!addressPXD(&iagp->inoext[extno])) {
917 release_metapage(mp);
918 IREAD_UNLOCK(ipimap);
919 AG_UNLOCK(imap, agno);
920 jfs_error(ip->i_sb, "invalid inoext\n");
924 /* compute the bitmap for the extent reflecting the freed inode.
926 bitmap = le32_to_cpu(iagp->wmap[extno]) & ~mask;
928 if (imap->im_agctl[agno].numfree > imap->im_agctl[agno].numinos) {
929 release_metapage(mp);
930 IREAD_UNLOCK(ipimap);
931 AG_UNLOCK(imap, agno);
932 jfs_error(ip->i_sb, "numfree > numinos\n");
936 * inode extent still has some inodes or below low water mark:
937 * keep the inode extent;
940 imap->im_agctl[agno].numfree < 96 ||
941 (imap->im_agctl[agno].numfree < 288 &&
942 (((imap->im_agctl[agno].numfree * 100) /
943 imap->im_agctl[agno].numinos) <= 25))) {
944 /* if the iag currently has no free inodes (i.e.,
945 * the inode being freed is the first free inode of iag),
946 * insert the iag at head of the inode free list for the ag.
948 if (iagp->nfreeinos == 0) {
949 /* check if there are any iags on the ag inode
950 * free list. if so, read the first one so that
951 * we can link the current iag onto the list at
954 if ((fwd = imap->im_agctl[agno].inofree) >= 0) {
955 /* read the iag that currently is the head
958 if ((rc = diIAGRead(imap, fwd, &))) {
959 IREAD_UNLOCK(ipimap);
960 AG_UNLOCK(imap, agno);
961 release_metapage(mp);
964 aiagp = (struct iag *) amp->data;
966 /* make current head point back to the iag.
968 aiagp->inofreeback = cpu_to_le32(iagno);
973 /* iag points forward to current head and iag
974 * becomes the new head of the list.
977 cpu_to_le32(imap->im_agctl[agno].inofree);
978 iagp->inofreeback = cpu_to_le32(-1);
979 imap->im_agctl[agno].inofree = iagno;
981 IREAD_UNLOCK(ipimap);
983 /* update the free inode summary map for the extent if
984 * freeing the inode means the extent will now have free
985 * inodes (i.e., the inode being freed is the first free
988 if (iagp->wmap[extno] == cpu_to_le32(ONES)) {
989 sword = extno >> L2EXTSPERSUM;
990 bitno = extno & (EXTSPERSUM - 1);
991 iagp->inosmap[sword] &=
992 cpu_to_le32(~(HIGHORDER >> bitno));
995 /* update the bitmap.
997 iagp->wmap[extno] = cpu_to_le32(bitmap);
999 /* update the free inode counts at the iag, ag and
1002 le32_add_cpu(&iagp->nfreeinos, 1);
1003 imap->im_agctl[agno].numfree += 1;
1004 atomic_inc(&imap->im_numfree);
1006 /* release the AG inode map lock
1008 AG_UNLOCK(imap, agno);
1018 * inode extent has become free and above low water mark:
1019 * free the inode extent;
1023 * prepare to update iag list(s) (careful update step 1)
1025 amp = bmp = cmp = dmp = NULL;
1028 /* check if the iag currently has no free extents. if so,
1029 * it will be placed on the head of the ag extent free list.
1031 if (iagp->nfreeexts == 0) {
1032 /* check if the ag extent free list has any iags.
1033 * if so, read the iag at the head of the list now.
1034 * this (head) iag will be updated later to reflect
1035 * the addition of the current iag at the head of
1038 if ((fwd = imap->im_agctl[agno].extfree) >= 0) {
1039 if ((rc = diIAGRead(imap, fwd, &)))
1041 aiagp = (struct iag *) amp->data;
1044 /* iag has free extents. check if the addition of a free
1045 * extent will cause all extents to be free within this
1046 * iag. if so, the iag will be removed from the ag extent
1047 * free list and placed on the inode map's free iag list.
1049 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG - 1)) {
1050 /* in preparation for removing the iag from the
1051 * ag extent free list, read the iags preceding
1052 * and following the iag on the ag extent free
1055 if ((fwd = le32_to_cpu(iagp->extfreefwd)) >= 0) {
1056 if ((rc = diIAGRead(imap, fwd, &)))
1058 aiagp = (struct iag *) amp->data;
1061 if ((back = le32_to_cpu(iagp->extfreeback)) >= 0) {
1062 if ((rc = diIAGRead(imap, back, &bmp)))
1064 biagp = (struct iag *) bmp->data;
1069 /* remove the iag from the ag inode free list if freeing
1070 * this extent cause the iag to have no free inodes.
1072 if (iagp->nfreeinos == cpu_to_le32(INOSPEREXT - 1)) {
1073 int inofreeback = le32_to_cpu(iagp->inofreeback);
1074 int inofreefwd = le32_to_cpu(iagp->inofreefwd);
1076 /* in preparation for removing the iag from the
1077 * ag inode free list, read the iags preceding
1078 * and following the iag on the ag inode free
1079 * list. before reading these iags, we must make
1080 * sure that we already don't have them in hand
1081 * from up above, since re-reading an iag (buffer)
1082 * we are currently holding would cause a deadlock.
1084 if (inofreefwd >= 0) {
1086 if (inofreefwd == fwd)
1087 ciagp = (struct iag *) amp->data;
1088 else if (inofreefwd == back)
1089 ciagp = (struct iag *) bmp->data;
1092 diIAGRead(imap, inofreefwd, &cmp)))
1094 ciagp = (struct iag *) cmp->data;
1096 assert(ciagp != NULL);
1099 if (inofreeback >= 0) {
1100 if (inofreeback == fwd)
1101 diagp = (struct iag *) amp->data;
1102 else if (inofreeback == back)
1103 diagp = (struct iag *) bmp->data;
1106 diIAGRead(imap, inofreeback, &dmp)))
1108 diagp = (struct iag *) dmp->data;
1110 assert(diagp != NULL);
1114 IREAD_UNLOCK(ipimap);
1117 * invalidate any page of the inode extent freed from buffer cache;
1119 freepxd = iagp->inoext[extno];
1120 invalidate_pxd_metapages(ip, freepxd);
1123 * update iag list(s) (careful update step 2)
1125 /* add the iag to the ag extent free list if this is the
1126 * first free extent for the iag.
1128 if (iagp->nfreeexts == 0) {
1130 aiagp->extfreeback = cpu_to_le32(iagno);
1133 cpu_to_le32(imap->im_agctl[agno].extfree);
1134 iagp->extfreeback = cpu_to_le32(-1);
1135 imap->im_agctl[agno].extfree = iagno;
1137 /* remove the iag from the ag extent list if all extents
1138 * are now free and place it on the inode map iag free list.
1140 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG - 1)) {
1142 aiagp->extfreeback = iagp->extfreeback;
1145 biagp->extfreefwd = iagp->extfreefwd;
1147 imap->im_agctl[agno].extfree =
1148 le32_to_cpu(iagp->extfreefwd);
1150 iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
1153 iagp->iagfree = cpu_to_le32(imap->im_freeiag);
1154 imap->im_freeiag = iagno;
1155 IAGFREE_UNLOCK(imap);
1159 /* remove the iag from the ag inode free list if freeing
1160 * this extent causes the iag to have no free inodes.
1162 if (iagp->nfreeinos == cpu_to_le32(INOSPEREXT - 1)) {
1163 if ((int) le32_to_cpu(iagp->inofreefwd) >= 0)
1164 ciagp->inofreeback = iagp->inofreeback;
1166 if ((int) le32_to_cpu(iagp->inofreeback) >= 0)
1167 diagp->inofreefwd = iagp->inofreefwd;
1169 imap->im_agctl[agno].inofree =
1170 le32_to_cpu(iagp->inofreefwd);
1172 iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
1175 /* update the inode extent address and working map
1176 * to reflect the free extent.
1177 * the permanent map should have been updated already
1178 * for the inode being freed.
1180 if (iagp->pmap[extno] != 0) {
1181 jfs_error(ip->i_sb, "the pmap does not show inode free\n");
1183 iagp->wmap[extno] = 0;
1184 PXDlength(&iagp->inoext[extno], 0);
1185 PXDaddress(&iagp->inoext[extno], 0);
1187 /* update the free extent and free inode summary maps
1188 * to reflect the freed extent.
1189 * the inode summary map is marked to indicate no inodes
1190 * available for the freed extent.
1192 sword = extno >> L2EXTSPERSUM;
1193 bitno = extno & (EXTSPERSUM - 1);
1194 mask = HIGHORDER >> bitno;
1195 iagp->inosmap[sword] |= cpu_to_le32(mask);
1196 iagp->extsmap[sword] &= cpu_to_le32(~mask);
1198 /* update the number of free inodes and number of free extents
1201 le32_add_cpu(&iagp->nfreeinos, -(INOSPEREXT - 1));
1202 le32_add_cpu(&iagp->nfreeexts, 1);
1204 /* update the number of free inodes and backed inodes
1205 * at the ag and inode map level.
1207 imap->im_agctl[agno].numfree -= (INOSPEREXT - 1);
1208 imap->im_agctl[agno].numinos -= INOSPEREXT;
1209 atomic_sub(INOSPEREXT - 1, &imap->im_numfree);
1210 atomic_sub(INOSPEREXT, &imap->im_numinos);
1213 write_metapage(amp);
1215 write_metapage(bmp);
1217 write_metapage(cmp);
1219 write_metapage(dmp);
1222 * start transaction to update block allocation map
1223 * for the inode extent freed;
1225 * N.B. AG_LOCK is released and iag will be released below, and
1226 * other thread may allocate inode from/reusing the ixad freed
1227 * BUT with new/different backing inode extent from the extent
1228 * to be freed by the transaction;
1230 tid = txBegin(ipimap->i_sb, COMMIT_FORCE);
1231 mutex_lock(&JFS_IP(ipimap)->commit_mutex);
1233 /* acquire tlock of the iag page of the freed ixad
1234 * to force the page NOHOMEOK (even though no data is
1235 * logged from the iag page) until NOREDOPAGE|FREEXTENT log
1236 * for the free of the extent is committed;
1237 * write FREEXTENT|NOREDOPAGE log record
1238 * N.B. linelock is overlaid as freed extent descriptor;
1240 tlck = txLock(tid, ipimap, mp, tlckINODE | tlckFREE);
1241 pxdlock = (struct pxd_lock *) & tlck->lock;
1242 pxdlock->flag = mlckFREEPXD;
1243 pxdlock->pxd = freepxd;
1251 * logredo needs the IAG number and IAG extent index in order
1252 * to ensure that the IMap is consistent. The least disruptive
1253 * way to pass these values through to the transaction manager
1254 * is in the iplist array.
1256 * It's not pretty, but it works.
1258 iplist[1] = (struct inode *) (size_t)iagno;
1259 iplist[2] = (struct inode *) (size_t)extno;
1261 rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
1264 mutex_unlock(&JFS_IP(ipimap)->commit_mutex);
1266 /* unlock the AG inode map information */
1267 AG_UNLOCK(imap, agno);
1272 IREAD_UNLOCK(ipimap);
1275 release_metapage(amp);
1277 release_metapage(bmp);
1279 release_metapage(cmp);
1281 release_metapage(dmp);
1283 AG_UNLOCK(imap, agno);
1285 release_metapage(mp);
1291 * There are several places in the diAlloc* routines where we initialize
1295 diInitInode(struct inode *ip, int iagno, int ino, int extno, struct iag * iagp)
1297 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
1299 ip->i_ino = (iagno << L2INOSPERIAG) + ino;
1300 jfs_ip->ixpxd = iagp->inoext[extno];
1301 jfs_ip->agstart = le64_to_cpu(iagp->agstart);
1302 jfs_ip->active_ag = -1;
1307 * NAME: diAlloc(pip,dir,ip)
1309 * FUNCTION: allocate a disk inode from the inode working map
1310 * for a fileset or aggregate.
1313 * pip - pointer to incore inode for the parent inode.
1314 * dir - 'true' if the new disk inode is for a directory.
1315 * ip - pointer to a new inode
1319 * -ENOSPC - insufficient disk resources.
1322 int diAlloc(struct inode *pip, bool dir, struct inode *ip)
1324 int rc, ino, iagno, addext, extno, bitno, sword;
1325 int nwords, rem, i, agno;
1326 u32 mask, inosmap, extsmap;
1327 struct inode *ipimap;
1328 struct metapage *mp;
1331 struct inomap *imap;
1333 /* get the pointers to the inode map inode and the
1334 * corresponding imap control structure.
1336 ipimap = JFS_SBI(pip->i_sb)->ipimap;
1337 imap = JFS_IP(ipimap)->i_imap;
1338 JFS_IP(ip)->ipimap = ipimap;
1339 JFS_IP(ip)->fileset = FILESYSTEM_I;
1341 /* for a directory, the allocation policy is to start
1342 * at the ag level using the preferred ag.
1345 agno = dbNextAG(JFS_SBI(pip->i_sb)->ipbmap);
1346 AG_LOCK(imap, agno);
1350 /* for files, the policy starts off by trying to allocate from
1351 * the same iag containing the parent disk inode:
1352 * try to allocate the new disk inode close to the parent disk
1353 * inode, using parent disk inode number + 1 as the allocation
1354 * hint. (we use a left-to-right policy to attempt to avoid
1355 * moving backward on the disk.) compute the hint within the
1356 * file system and the iag.
1359 /* get the ag number of this iag */
1360 agno = BLKTOAG(JFS_IP(pip)->agstart, JFS_SBI(pip->i_sb));
1362 if (atomic_read(&JFS_SBI(pip->i_sb)->bmap->db_active[agno])) {
1364 * There is an open file actively growing. We want to
1365 * allocate new inodes from a different ag to avoid
1366 * fragmentation problems.
1368 agno = dbNextAG(JFS_SBI(pip->i_sb)->ipbmap);
1369 AG_LOCK(imap, agno);
1373 inum = pip->i_ino + 1;
1374 ino = inum & (INOSPERIAG - 1);
1376 /* back off the hint if it is outside of the iag */
1380 /* lock the AG inode map information */
1381 AG_LOCK(imap, agno);
1383 /* Get read lock on imap inode */
1384 IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
1386 /* get the iag number and read the iag */
1387 iagno = INOTOIAG(inum);
1388 if ((rc = diIAGRead(imap, iagno, &mp))) {
1389 IREAD_UNLOCK(ipimap);
1390 AG_UNLOCK(imap, agno);
1393 iagp = (struct iag *) mp->data;
1395 /* determine if new inode extent is allowed to be added to the iag.
1396 * new inode extent can be added to the iag if the ag
1397 * has less than 32 free disk inodes and the iag has free extents.
1399 addext = (imap->im_agctl[agno].numfree < 32 && iagp->nfreeexts);
1402 * try to allocate from the IAG
1404 /* check if the inode may be allocated from the iag
1405 * (i.e. the inode has free inodes or new extent can be added).
1407 if (iagp->nfreeinos || addext) {
1408 /* determine the extent number of the hint.
1410 extno = ino >> L2INOSPEREXT;
1412 /* check if the extent containing the hint has backed
1413 * inodes. if so, try to allocate within this extent.
1415 if (addressPXD(&iagp->inoext[extno])) {
1416 bitno = ino & (INOSPEREXT - 1);
1418 diFindFree(le32_to_cpu(iagp->wmap[extno]),
1421 ino = (extno << L2INOSPEREXT) + bitno;
1423 /* a free inode (bit) was found within this
1424 * extent, so allocate it.
1426 rc = diAllocBit(imap, iagp, ino);
1427 IREAD_UNLOCK(ipimap);
1431 /* set the results of the allocation
1432 * and write the iag.
1434 diInitInode(ip, iagno, ino, extno,
1436 mark_metapage_dirty(mp);
1438 release_metapage(mp);
1440 /* free the AG lock and return.
1442 AG_UNLOCK(imap, agno);
1449 EXTSPERIAG - 1) ? 0 : extno + 1;
1453 * no free inodes within the extent containing the hint.
1455 * try to allocate from the backed extents following
1456 * hint or, if appropriate (i.e. addext is true), allocate
1457 * an extent of free inodes at or following the extent
1458 * containing the hint.
1460 * the free inode and free extent summary maps are used
1461 * here, so determine the starting summary map position
1462 * and the number of words we'll have to examine. again,
1463 * the approach is to allocate following the hint, so we
1464 * might have to initially ignore prior bits of the summary
1465 * map that represent extents prior to the extent containing
1466 * the hint and later revisit these bits.
1468 bitno = extno & (EXTSPERSUM - 1);
1469 nwords = (bitno == 0) ? SMAPSZ : SMAPSZ + 1;
1470 sword = extno >> L2EXTSPERSUM;
1472 /* mask any prior bits for the starting words of the
1475 mask = (bitno == 0) ? 0 : (ONES << (EXTSPERSUM - bitno));
1476 inosmap = le32_to_cpu(iagp->inosmap[sword]) | mask;
1477 extsmap = le32_to_cpu(iagp->extsmap[sword]) | mask;
1479 /* scan the free inode and free extent summary maps for
1482 for (i = 0; i < nwords; i++) {
1483 /* check if this word of the free inode summary
1484 * map describes an extent with free inodes.
1487 /* an extent with free inodes has been
1488 * found. determine the extent number
1489 * and the inode number within the extent.
1491 rem = diFindFree(inosmap, 0);
1492 extno = (sword << L2EXTSPERSUM) + rem;
1493 rem = diFindFree(le32_to_cpu(iagp->wmap[extno]),
1495 if (rem >= INOSPEREXT) {
1496 IREAD_UNLOCK(ipimap);
1497 release_metapage(mp);
1498 AG_UNLOCK(imap, agno);
1500 "can't find free bit in wmap\n");
1504 /* determine the inode number within the
1505 * iag and allocate the inode from the
1508 ino = (extno << L2INOSPEREXT) + rem;
1509 rc = diAllocBit(imap, iagp, ino);
1510 IREAD_UNLOCK(ipimap);
1514 /* set the results of the allocation
1515 * and write the iag.
1517 diInitInode(ip, iagno, ino, extno,
1519 mark_metapage_dirty(mp);
1521 release_metapage(mp);
1523 /* free the AG lock and return.
1525 AG_UNLOCK(imap, agno);
1530 /* check if we may allocate an extent of free
1531 * inodes and whether this word of the free
1532 * extents summary map describes a free extent.
1534 if (addext && ~extsmap) {
1535 /* a free extent has been found. determine
1536 * the extent number.
1538 rem = diFindFree(extsmap, 0);
1539 extno = (sword << L2EXTSPERSUM) + rem;
1541 /* allocate an extent of free inodes.
1543 if ((rc = diNewExt(imap, iagp, extno))) {
1544 /* if there is no disk space for a
1545 * new extent, try to allocate the
1546 * disk inode from somewhere else.
1553 /* set the results of the allocation
1554 * and write the iag.
1556 diInitInode(ip, iagno,
1557 extno << L2INOSPEREXT,
1559 mark_metapage_dirty(mp);
1561 release_metapage(mp);
1562 /* free the imap inode & the AG lock & return.
1564 IREAD_UNLOCK(ipimap);
1565 AG_UNLOCK(imap, agno);
1569 /* move on to the next set of summary map words.
1571 sword = (sword == SMAPSZ - 1) ? 0 : sword + 1;
1572 inosmap = le32_to_cpu(iagp->inosmap[sword]);
1573 extsmap = le32_to_cpu(iagp->extsmap[sword]);
1576 /* unlock imap inode */
1577 IREAD_UNLOCK(ipimap);
1579 /* nothing doing in this iag, so release it. */
1580 release_metapage(mp);
1584 * try to allocate anywhere within the same AG as the parent inode.
1586 rc = diAllocAG(imap, agno, dir, ip);
1588 AG_UNLOCK(imap, agno);
1594 * try to allocate in any AG.
1596 return (diAllocAny(imap, agno, dir, ip));
1601 * NAME: diAllocAG(imap,agno,dir,ip)
1603 * FUNCTION: allocate a disk inode from the allocation group.
1605 * this routine first determines if a new extent of free
1606 * inodes should be added for the allocation group, with
1607 * the current request satisfied from this extent. if this
1608 * is the case, an attempt will be made to do just that. if
1609 * this attempt fails or it has been determined that a new
1610 * extent should not be added, an attempt is made to satisfy
1611 * the request by allocating an existing (backed) free inode
1612 * from the allocation group.
1614 * PRE CONDITION: Already have the AG lock for this AG.
1617 * imap - pointer to inode map control structure.
1618 * agno - allocation group to allocate from.
1619 * dir - 'true' if the new disk inode is for a directory.
1620 * ip - pointer to the new inode to be filled in on successful return
1621 * with the disk inode number allocated, its extent address
1622 * and the start of the ag.
1626 * -ENOSPC - insufficient disk resources.
1630 diAllocAG(struct inomap * imap, int agno, bool dir, struct inode *ip)
1632 int rc, addext, numfree, numinos;
1634 /* get the number of free and the number of backed disk
1635 * inodes currently within the ag.
1637 numfree = imap->im_agctl[agno].numfree;
1638 numinos = imap->im_agctl[agno].numinos;
1640 if (numfree > numinos) {
1641 jfs_error(ip->i_sb, "numfree > numinos\n");
1645 /* determine if we should allocate a new extent of free inodes
1646 * within the ag: for directory inodes, add a new extent
1647 * if there are a small number of free inodes or number of free
1648 * inodes is a small percentage of the number of backed inodes.
1651 addext = (numfree < 64 ||
1653 && ((numfree * 100) / numinos) <= 20));
1655 addext = (numfree == 0);
1658 * try to allocate a new extent of free inodes.
1661 /* if free space is not available for this new extent, try
1662 * below to allocate a free and existing (already backed)
1663 * inode from the ag.
1665 if ((rc = diAllocExt(imap, agno, ip)) != -ENOSPC)
1670 * try to allocate an existing free inode from the ag.
1672 return (diAllocIno(imap, agno, ip));
1677 * NAME: diAllocAny(imap,agno,dir,iap)
1679 * FUNCTION: allocate a disk inode from any other allocation group.
1681 * this routine is called when an allocation attempt within
1682 * the primary allocation group has failed. if attempts to
1683 * allocate an inode from any allocation group other than the
1684 * specified primary group.
1687 * imap - pointer to inode map control structure.
1688 * agno - primary allocation group (to avoid).
1689 * dir - 'true' if the new disk inode is for a directory.
1690 * ip - pointer to a new inode to be filled in on successful return
1691 * with the disk inode number allocated, its extent address
1692 * and the start of the ag.
1696 * -ENOSPC - insufficient disk resources.
1700 diAllocAny(struct inomap * imap, int agno, bool dir, struct inode *ip)
1703 int maxag = JFS_SBI(imap->im_ipimap->i_sb)->bmap->db_maxag;
1706 /* try to allocate from the ags following agno up to
1707 * the maximum ag number.
1709 for (ag = agno + 1; ag <= maxag; ag++) {
1712 rc = diAllocAG(imap, ag, dir, ip);
1714 AG_UNLOCK(imap, ag);
1720 /* try to allocate from the ags in front of agno.
1722 for (ag = 0; ag < agno; ag++) {
1725 rc = diAllocAG(imap, ag, dir, ip);
1727 AG_UNLOCK(imap, ag);
1733 /* no free disk inodes.
1740 * NAME: diAllocIno(imap,agno,ip)
1742 * FUNCTION: allocate a disk inode from the allocation group's free
1743 * inode list, returning an error if this free list is
1744 * empty (i.e. no iags on the list).
1746 * allocation occurs from the first iag on the list using
1747 * the iag's free inode summary map to find the leftmost
1748 * free inode in the iag.
1750 * PRE CONDITION: Already have AG lock for this AG.
1753 * imap - pointer to inode map control structure.
1754 * agno - allocation group.
1755 * ip - pointer to new inode to be filled in on successful return
1756 * with the disk inode number allocated, its extent address
1757 * and the start of the ag.
1761 * -ENOSPC - insufficient disk resources.
1764 static int diAllocIno(struct inomap * imap, int agno, struct inode *ip)
1766 int iagno, ino, rc, rem, extno, sword;
1767 struct metapage *mp;
1770 /* check if there are iags on the ag's free inode list.
1772 if ((iagno = imap->im_agctl[agno].inofree) < 0)
1775 /* obtain read lock on imap inode */
1776 IREAD_LOCK(imap->im_ipimap, RDWRLOCK_IMAP);
1778 /* read the iag at the head of the list.
1780 if ((rc = diIAGRead(imap, iagno, &mp))) {
1781 IREAD_UNLOCK(imap->im_ipimap);
1784 iagp = (struct iag *) mp->data;
1786 /* better be free inodes in this iag if it is on the
1789 if (!iagp->nfreeinos) {
1790 IREAD_UNLOCK(imap->im_ipimap);
1791 release_metapage(mp);
1792 jfs_error(ip->i_sb, "nfreeinos = 0, but iag on freelist\n");
1796 /* scan the free inode summary map to find an extent
1799 for (sword = 0;; sword++) {
1800 if (sword >= SMAPSZ) {
1801 IREAD_UNLOCK(imap->im_ipimap);
1802 release_metapage(mp);
1804 "free inode not found in summary map\n");
1808 if (~iagp->inosmap[sword])
1812 /* found a extent with free inodes. determine
1813 * the extent number.
1815 rem = diFindFree(le32_to_cpu(iagp->inosmap[sword]), 0);
1816 if (rem >= EXTSPERSUM) {
1817 IREAD_UNLOCK(imap->im_ipimap);
1818 release_metapage(mp);
1819 jfs_error(ip->i_sb, "no free extent found\n");
1822 extno = (sword << L2EXTSPERSUM) + rem;
1824 /* find the first free inode in the extent.
1826 rem = diFindFree(le32_to_cpu(iagp->wmap[extno]), 0);
1827 if (rem >= INOSPEREXT) {
1828 IREAD_UNLOCK(imap->im_ipimap);
1829 release_metapage(mp);
1830 jfs_error(ip->i_sb, "free inode not found\n");
1834 /* compute the inode number within the iag.
1836 ino = (extno << L2INOSPEREXT) + rem;
1838 /* allocate the inode.
1840 rc = diAllocBit(imap, iagp, ino);
1841 IREAD_UNLOCK(imap->im_ipimap);
1843 release_metapage(mp);
1847 /* set the results of the allocation and write the iag.
1849 diInitInode(ip, iagno, ino, extno, iagp);
1857 * NAME: diAllocExt(imap,agno,ip)
1859 * FUNCTION: add a new extent of free inodes to an iag, allocating
1860 * an inode from this extent to satisfy the current allocation
1863 * this routine first tries to find an existing iag with free
1864 * extents through the ag free extent list. if list is not
1865 * empty, the head of the list will be selected as the home
1866 * of the new extent of free inodes. otherwise (the list is
1867 * empty), a new iag will be allocated for the ag to contain
1870 * once an iag has been selected, the free extent summary map
1871 * is used to locate a free extent within the iag and diNewExt()
1872 * is called to initialize the extent, with initialization
1873 * including the allocation of the first inode of the extent
1874 * for the purpose of satisfying this request.
1877 * imap - pointer to inode map control structure.
1878 * agno - allocation group number.
1879 * ip - pointer to new inode to be filled in on successful return
1880 * with the disk inode number allocated, its extent address
1881 * and the start of the ag.
1885 * -ENOSPC - insufficient disk resources.
1888 static int diAllocExt(struct inomap * imap, int agno, struct inode *ip)
1890 int rem, iagno, sword, extno, rc;
1891 struct metapage *mp;
1894 /* check if the ag has any iags with free extents. if not,
1895 * allocate a new iag for the ag.
1897 if ((iagno = imap->im_agctl[agno].extfree) < 0) {
1898 /* If successful, diNewIAG will obtain the read lock on the
1901 if ((rc = diNewIAG(imap, &iagno, agno, &mp))) {
1904 iagp = (struct iag *) mp->data;
1906 /* set the ag number if this a brand new iag
1909 cpu_to_le64(AGTOBLK(agno, imap->im_ipimap));
1913 IREAD_LOCK(imap->im_ipimap, RDWRLOCK_IMAP);
1914 if ((rc = diIAGRead(imap, iagno, &mp))) {
1915 IREAD_UNLOCK(imap->im_ipimap);
1916 jfs_error(ip->i_sb, "error reading iag\n");
1919 iagp = (struct iag *) mp->data;
1922 /* using the free extent summary map, find a free extent.
1924 for (sword = 0;; sword++) {
1925 if (sword >= SMAPSZ) {
1926 release_metapage(mp);
1927 IREAD_UNLOCK(imap->im_ipimap);
1928 jfs_error(ip->i_sb, "free ext summary map not found\n");
1931 if (~iagp->extsmap[sword])
1935 /* determine the extent number of the free extent.
1937 rem = diFindFree(le32_to_cpu(iagp->extsmap[sword]), 0);
1938 if (rem >= EXTSPERSUM) {
1939 release_metapage(mp);
1940 IREAD_UNLOCK(imap->im_ipimap);
1941 jfs_error(ip->i_sb, "free extent not found\n");
1944 extno = (sword << L2EXTSPERSUM) + rem;
1946 /* initialize the new extent.
1948 rc = diNewExt(imap, iagp, extno);
1949 IREAD_UNLOCK(imap->im_ipimap);
1951 /* something bad happened. if a new iag was allocated,
1952 * place it back on the inode map's iag free list, and
1953 * clear the ag number information.
1955 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
1957 iagp->iagfree = cpu_to_le32(imap->im_freeiag);
1958 imap->im_freeiag = iagno;
1959 IAGFREE_UNLOCK(imap);
1965 /* set the results of the allocation and write the iag.
1967 diInitInode(ip, iagno, extno << L2INOSPEREXT, extno, iagp);
1976 * NAME: diAllocBit(imap,iagp,ino)
1978 * FUNCTION: allocate a backed inode from an iag.
1980 * this routine performs the mechanics of allocating a
1981 * specified inode from a backed extent.
1983 * if the inode to be allocated represents the last free
1984 * inode within the iag, the iag will be removed from the
1985 * ag free inode list.
1987 * a careful update approach is used to provide consistency
1988 * in the face of updates to multiple buffers. under this
1989 * approach, all required buffers are obtained before making
1990 * any updates and are held all are updates are complete.
1992 * PRE CONDITION: Already have buffer lock on iagp. Already have AG lock on
1993 * this AG. Must have read lock on imap inode.
1996 * imap - pointer to inode map control structure.
1997 * iagp - pointer to iag.
1998 * ino - inode number to be allocated within the iag.
2002 * -ENOSPC - insufficient disk resources.
2005 static int diAllocBit(struct inomap * imap, struct iag * iagp, int ino)
2007 int extno, bitno, agno, sword, rc;
2008 struct metapage *amp = NULL, *bmp = NULL;
2009 struct iag *aiagp = NULL, *biagp = NULL;
2012 /* check if this is the last free inode within the iag.
2013 * if so, it will have to be removed from the ag free
2014 * inode list, so get the iags preceding and following
2017 if (iagp->nfreeinos == cpu_to_le32(1)) {
2018 if ((int) le32_to_cpu(iagp->inofreefwd) >= 0) {
2020 diIAGRead(imap, le32_to_cpu(iagp->inofreefwd),
2023 aiagp = (struct iag *) amp->data;
2026 if ((int) le32_to_cpu(iagp->inofreeback) >= 0) {
2029 le32_to_cpu(iagp->inofreeback),
2032 release_metapage(amp);
2035 biagp = (struct iag *) bmp->data;
2039 /* get the ag number, extent number, inode number within
2042 agno = BLKTOAG(le64_to_cpu(iagp->agstart), JFS_SBI(imap->im_ipimap->i_sb));
2043 extno = ino >> L2INOSPEREXT;
2044 bitno = ino & (INOSPEREXT - 1);
2046 /* compute the mask for setting the map.
2048 mask = HIGHORDER >> bitno;
2050 /* the inode should be free and backed.
2052 if (((le32_to_cpu(iagp->pmap[extno]) & mask) != 0) ||
2053 ((le32_to_cpu(iagp->wmap[extno]) & mask) != 0) ||
2054 (addressPXD(&iagp->inoext[extno]) == 0)) {
2056 release_metapage(amp);
2058 release_metapage(bmp);
2060 jfs_error(imap->im_ipimap->i_sb, "iag inconsistent\n");
2064 /* mark the inode as allocated in the working map.
2066 iagp->wmap[extno] |= cpu_to_le32(mask);
2068 /* check if all inodes within the extent are now
2069 * allocated. if so, update the free inode summary
2070 * map to reflect this.
2072 if (iagp->wmap[extno] == cpu_to_le32(ONES)) {
2073 sword = extno >> L2EXTSPERSUM;
2074 bitno = extno & (EXTSPERSUM - 1);
2075 iagp->inosmap[sword] |= cpu_to_le32(HIGHORDER >> bitno);
2078 /* if this was the last free inode in the iag, remove the
2079 * iag from the ag free inode list.
2081 if (iagp->nfreeinos == cpu_to_le32(1)) {
2083 aiagp->inofreeback = iagp->inofreeback;
2084 write_metapage(amp);
2088 biagp->inofreefwd = iagp->inofreefwd;
2089 write_metapage(bmp);
2091 imap->im_agctl[agno].inofree =
2092 le32_to_cpu(iagp->inofreefwd);
2094 iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
2097 /* update the free inode count at the iag, ag, inode
2100 le32_add_cpu(&iagp->nfreeinos, -1);
2101 imap->im_agctl[agno].numfree -= 1;
2102 atomic_dec(&imap->im_numfree);
2109 * NAME: diNewExt(imap,iagp,extno)
2111 * FUNCTION: initialize a new extent of inodes for an iag, allocating
2112 * the first inode of the extent for use for the current
2113 * allocation request.
2115 * disk resources are allocated for the new extent of inodes
2116 * and the inodes themselves are initialized to reflect their
2117 * existence within the extent (i.e. their inode numbers and
2118 * inode extent addresses are set) and their initial state
2119 * (mode and link count are set to zero).
2121 * if the iag is new, it is not yet on an ag extent free list
2122 * but will now be placed on this list.
2124 * if the allocation of the new extent causes the iag to
2125 * have no free extent, the iag will be removed from the
2126 * ag extent free list.
2128 * if the iag has no free backed inodes, it will be placed
2129 * on the ag free inode list, since the addition of the new
2130 * extent will now cause it to have free inodes.
2132 * a careful update approach is used to provide consistency
2133 * (i.e. list consistency) in the face of updates to multiple
2134 * buffers. under this approach, all required buffers are
2135 * obtained before making any updates and are held until all
2136 * updates are complete.
2138 * PRE CONDITION: Already have buffer lock on iagp. Already have AG lock on
2139 * this AG. Must have read lock on imap inode.
2142 * imap - pointer to inode map control structure.
2143 * iagp - pointer to iag.
2144 * extno - extent number.
2148 * -ENOSPC - insufficient disk resources.
2151 static int diNewExt(struct inomap * imap, struct iag * iagp, int extno)
2153 int agno, iagno, fwd, back, freei = 0, sword, rc;
2154 struct iag *aiagp = NULL, *biagp = NULL, *ciagp = NULL;
2155 struct metapage *amp, *bmp, *cmp, *dmp;
2156 struct inode *ipimap;
2162 struct jfs_sb_info *sbi;
2164 /* better have free extents.
2166 if (!iagp->nfreeexts) {
2167 jfs_error(imap->im_ipimap->i_sb, "no free extents\n");
2171 /* get the inode map inode.
2173 ipimap = imap->im_ipimap;
2174 sbi = JFS_SBI(ipimap->i_sb);
2176 amp = bmp = cmp = NULL;
2178 /* get the ag and iag numbers for this iag.
2180 agno = BLKTOAG(le64_to_cpu(iagp->agstart), sbi);
2181 iagno = le32_to_cpu(iagp->iagnum);
2183 /* check if this is the last free extent within the
2184 * iag. if so, the iag must be removed from the ag
2185 * free extent list, so get the iags preceding and
2186 * following the iag on this list.
2188 if (iagp->nfreeexts == cpu_to_le32(1)) {
2189 if ((fwd = le32_to_cpu(iagp->extfreefwd)) >= 0) {
2190 if ((rc = diIAGRead(imap, fwd, &)))
2192 aiagp = (struct iag *) amp->data;
2195 if ((back = le32_to_cpu(iagp->extfreeback)) >= 0) {
2196 if ((rc = diIAGRead(imap, back, &bmp)))
2198 biagp = (struct iag *) bmp->data;
2201 /* the iag has free extents. if all extents are free
2202 * (as is the case for a newly allocated iag), the iag
2203 * must be added to the ag free extent list, so get
2204 * the iag at the head of the list in preparation for
2205 * adding this iag to this list.
2208 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2209 if ((fwd = imap->im_agctl[agno].extfree) >= 0) {
2210 if ((rc = diIAGRead(imap, fwd, &)))
2212 aiagp = (struct iag *) amp->data;
2217 /* check if the iag has no free inodes. if so, the iag
2218 * will have to be added to the ag free inode list, so get
2219 * the iag at the head of the list in preparation for
2220 * adding this iag to this list. in doing this, we must
2221 * check if we already have the iag at the head of
2224 if (iagp->nfreeinos == 0) {
2225 freei = imap->im_agctl[agno].inofree;
2230 } else if (freei == back) {
2233 if ((rc = diIAGRead(imap, freei, &cmp)))
2235 ciagp = (struct iag *) cmp->data;
2237 if (ciagp == NULL) {
2238 jfs_error(imap->im_ipimap->i_sb,
2246 /* allocate disk space for the inode extent.
2248 if ((extno == 0) || (addressPXD(&iagp->inoext[extno - 1]) == 0))
2249 hint = ((s64) agno << sbi->bmap->db_agl2size) - 1;
2251 hint = addressPXD(&iagp->inoext[extno - 1]) +
2252 lengthPXD(&iagp->inoext[extno - 1]) - 1;
2254 if ((rc = dbAlloc(ipimap, hint, (s64) imap->im_nbperiext, &blkno)))
2257 /* compute the inode number of the first inode within the
2260 ino = (iagno << L2INOSPERIAG) + (extno << L2INOSPEREXT);
2262 /* initialize the inodes within the newly allocated extent a
2265 for (i = 0; i < imap->im_nbperiext; i += sbi->nbperpage) {
2266 /* get a buffer for this page of disk inodes.
2268 dmp = get_metapage(ipimap, blkno + i, PSIZE, 1);
2273 dp = (struct dinode *) dmp->data;
2275 /* initialize the inode number, mode, link count and
2276 * inode extent address.
2278 for (j = 0; j < INOSPERPAGE; j++, dp++, ino++) {
2279 dp->di_inostamp = cpu_to_le32(sbi->inostamp);
2280 dp->di_number = cpu_to_le32(ino);
2281 dp->di_fileset = cpu_to_le32(FILESYSTEM_I);
2284 PXDaddress(&(dp->di_ixpxd), blkno);
2285 PXDlength(&(dp->di_ixpxd), imap->im_nbperiext);
2287 write_metapage(dmp);
2290 /* if this is the last free extent within the iag, remove the
2291 * iag from the ag free extent list.
2293 if (iagp->nfreeexts == cpu_to_le32(1)) {
2295 aiagp->extfreeback = iagp->extfreeback;
2298 biagp->extfreefwd = iagp->extfreefwd;
2300 imap->im_agctl[agno].extfree =
2301 le32_to_cpu(iagp->extfreefwd);
2303 iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
2305 /* if the iag has all free extents (newly allocated iag),
2306 * add the iag to the ag free extent list.
2308 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2310 aiagp->extfreeback = cpu_to_le32(iagno);
2312 iagp->extfreefwd = cpu_to_le32(fwd);
2313 iagp->extfreeback = cpu_to_le32(-1);
2314 imap->im_agctl[agno].extfree = iagno;
2318 /* if the iag has no free inodes, add the iag to the
2319 * ag free inode list.
2321 if (iagp->nfreeinos == 0) {
2323 ciagp->inofreeback = cpu_to_le32(iagno);
2326 cpu_to_le32(imap->im_agctl[agno].inofree);
2327 iagp->inofreeback = cpu_to_le32(-1);
2328 imap->im_agctl[agno].inofree = iagno;
2331 /* initialize the extent descriptor of the extent. */
2332 PXDlength(&iagp->inoext[extno], imap->im_nbperiext);
2333 PXDaddress(&iagp->inoext[extno], blkno);
2335 /* initialize the working and persistent map of the extent.
2336 * the working map will be initialized such that
2337 * it indicates the first inode of the extent is allocated.
2339 iagp->wmap[extno] = cpu_to_le32(HIGHORDER);
2340 iagp->pmap[extno] = 0;
2342 /* update the free inode and free extent summary maps
2343 * for the extent to indicate the extent has free inodes
2344 * and no longer represents a free extent.
2346 sword = extno >> L2EXTSPERSUM;
2347 mask = HIGHORDER >> (extno & (EXTSPERSUM - 1));
2348 iagp->extsmap[sword] |= cpu_to_le32(mask);
2349 iagp->inosmap[sword] &= cpu_to_le32(~mask);
2351 /* update the free inode and free extent counts for the
2354 le32_add_cpu(&iagp->nfreeinos, (INOSPEREXT - 1));
2355 le32_add_cpu(&iagp->nfreeexts, -1);
2357 /* update the free and backed inode counts for the ag.
2359 imap->im_agctl[agno].numfree += (INOSPEREXT - 1);
2360 imap->im_agctl[agno].numinos += INOSPEREXT;
2362 /* update the free and backed inode counts for the inode map.
2364 atomic_add(INOSPEREXT - 1, &imap->im_numfree);
2365 atomic_add(INOSPEREXT, &imap->im_numinos);
2370 write_metapage(amp);
2372 write_metapage(bmp);
2374 write_metapage(cmp);
2380 /* release the iags.
2383 release_metapage(amp);
2385 release_metapage(bmp);
2387 release_metapage(cmp);
2394 * NAME: diNewIAG(imap,iagnop,agno)
2396 * FUNCTION: allocate a new iag for an allocation group.
2398 * first tries to allocate the iag from the inode map
2400 * if the list has free iags, the head of the list is removed
2401 * and returned to satisfy the request.
2402 * if the inode map's iag free list is empty, the inode map
2403 * is extended to hold a new iag. this new iag is initialized
2404 * and returned to satisfy the request.
2407 * imap - pointer to inode map control structure.
2408 * iagnop - pointer to an iag number set with the number of the
2409 * newly allocated iag upon successful return.
2410 * agno - allocation group number.
2411 * bpp - Buffer pointer to be filled in with new IAG's buffer
2415 * -ENOSPC - insufficient disk resources.
2419 * AG lock held on entry/exit;
2420 * write lock on the map is held inside;
2421 * read lock on the map is held on successful completion;
2423 * note: new iag transaction:
2424 * . synchronously write iag;
2425 * . write log of xtree and inode of imap;
2427 * . synchronous write of xtree (right to left, bottom to top);
2428 * . at start of logredo(): init in-memory imap with one additional iag page;
2429 * . at end of logredo(): re-read imap inode to determine
2433 diNewIAG(struct inomap * imap, int *iagnop, int agno, struct metapage ** mpp)
2437 struct inode *ipimap;
2438 struct super_block *sb;
2439 struct jfs_sb_info *sbi;
2440 struct metapage *mp;
2445 struct inode *iplist[1];
2447 /* pick up pointers to the inode map and mount inodes */
2448 ipimap = imap->im_ipimap;
2452 /* acquire the free iag lock */
2455 /* if there are any iags on the inode map free iag list,
2456 * allocate the iag from the head of the list.
2458 if (imap->im_freeiag >= 0) {
2459 /* pick up the iag number at the head of the list */
2460 iagno = imap->im_freeiag;
2462 /* determine the logical block number of the iag */
2463 blkno = IAGTOLBLK(iagno, sbi->l2nbperpage);
2465 /* no free iags. the inode map will have to be extented
2466 * to include a new iag.
2469 /* acquire inode map lock */
2470 IWRITE_LOCK(ipimap, RDWRLOCK_IMAP);
2472 if (ipimap->i_size >> L2PSIZE != imap->im_nextiag + 1) {
2473 IWRITE_UNLOCK(ipimap);
2474 IAGFREE_UNLOCK(imap);
2475 jfs_error(imap->im_ipimap->i_sb,
2476 "ipimap->i_size is wrong\n");
2481 /* get the next available iag number */
2482 iagno = imap->im_nextiag;
2484 /* make sure that we have not exceeded the maximum inode
2487 if (iagno > (MAXIAGS - 1)) {
2488 /* release the inode map lock */
2489 IWRITE_UNLOCK(ipimap);
2496 * synchronously append new iag page.
2498 /* determine the logical address of iag page to append */
2499 blkno = IAGTOLBLK(iagno, sbi->l2nbperpage);
2501 /* Allocate extent for new iag page */
2502 xlen = sbi->nbperpage;
2503 if ((rc = dbAlloc(ipimap, 0, (s64) xlen, &xaddr))) {
2504 /* release the inode map lock */
2505 IWRITE_UNLOCK(ipimap);
2511 * start transaction of update of the inode map
2512 * addressing structure pointing to the new iag page;
2514 tid = txBegin(sb, COMMIT_FORCE);
2515 mutex_lock(&JFS_IP(ipimap)->commit_mutex);
2517 /* update the inode map addressing structure to point to it */
2519 xtInsert(tid, ipimap, 0, blkno, xlen, &xaddr, 0))) {
2521 mutex_unlock(&JFS_IP(ipimap)->commit_mutex);
2522 /* Free the blocks allocated for the iag since it was
2523 * not successfully added to the inode map
2525 dbFree(ipimap, xaddr, (s64) xlen);
2527 /* release the inode map lock */
2528 IWRITE_UNLOCK(ipimap);
2533 /* update the inode map's inode to reflect the extension */
2534 ipimap->i_size += PSIZE;
2535 inode_add_bytes(ipimap, PSIZE);
2537 /* assign a buffer for the page */
2538 mp = get_metapage(ipimap, blkno, PSIZE, 0);
2541 * This is very unlikely since we just created the
2542 * extent, but let's try to handle it correctly
2544 xtTruncate(tid, ipimap, ipimap->i_size - PSIZE,
2549 mutex_unlock(&JFS_IP(ipimap)->commit_mutex);
2551 /* release the inode map lock */
2552 IWRITE_UNLOCK(ipimap);
2557 iagp = (struct iag *) mp->data;
2560 memset(iagp, 0, sizeof(struct iag));
2561 iagp->iagnum = cpu_to_le32(iagno);
2562 iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
2563 iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
2564 iagp->iagfree = cpu_to_le32(-1);
2565 iagp->nfreeinos = 0;
2566 iagp->nfreeexts = cpu_to_le32(EXTSPERIAG);
2568 /* initialize the free inode summary map (free extent
2569 * summary map initialization handled by bzero).
2571 for (i = 0; i < SMAPSZ; i++)
2572 iagp->inosmap[i] = cpu_to_le32(ONES);
2575 * Write and sync the metapage
2580 * txCommit(COMMIT_FORCE) will synchronously write address
2581 * index pages and inode after commit in careful update order
2582 * of address index pages (right to left, bottom up);
2585 rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
2588 mutex_unlock(&JFS_IP(ipimap)->commit_mutex);
2590 duplicateIXtree(sb, blkno, xlen, &xaddr);
2592 /* update the next available iag number */
2593 imap->im_nextiag += 1;
2595 /* Add the iag to the iag free list so we don't lose the iag
2596 * if a failure happens now.
2598 imap->im_freeiag = iagno;
2600 /* Until we have logredo working, we want the imap inode &
2601 * control page to be up to date.
2605 /* release the inode map lock */
2606 IWRITE_UNLOCK(ipimap);
2609 /* obtain read lock on map */
2610 IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
2613 if ((rc = diIAGRead(imap, iagno, &mp))) {
2614 IREAD_UNLOCK(ipimap);
2618 iagp = (struct iag *) mp->data;
2620 /* remove the iag from the iag free list */
2621 imap->im_freeiag = le32_to_cpu(iagp->iagfree);
2622 iagp->iagfree = cpu_to_le32(-1);
2624 /* set the return iag number and buffer pointer */
2629 /* release the iag free lock */
2630 IAGFREE_UNLOCK(imap);
2638 * FUNCTION: get the buffer for the specified iag within a fileset
2639 * or aggregate inode map.
2642 * imap - pointer to inode map control structure.
2643 * iagno - iag number.
2644 * bpp - point to buffer pointer to be filled in on successful
2648 * must have read lock on imap inode
2649 * (When called by diExtendFS, the filesystem is quiesced, therefore
2650 * the read lock is unnecessary.)
2656 static int diIAGRead(struct inomap * imap, int iagno, struct metapage ** mpp)
2658 struct inode *ipimap = imap->im_ipimap;
2661 /* compute the logical block number of the iag. */
2662 blkno = IAGTOLBLK(iagno, JFS_SBI(ipimap->i_sb)->l2nbperpage);
2665 *mpp = read_metapage(ipimap, blkno, PSIZE, 0);
2674 * NAME: diFindFree()
2676 * FUNCTION: find the first free bit in a word starting at
2677 * the specified bit position.
2680 * word - word to be examined.
2681 * start - starting bit position.
2684 * bit position of first free bit in the word or 32 if
2685 * no free bits were found.
2687 static int diFindFree(u32 word, int start)
2691 /* scan the word for the first free bit. */
2692 for (word <<= start, bitno = start; bitno < 32;
2693 bitno++, word <<= 1) {
2694 if ((word & HIGHORDER) == 0)
2701 * NAME: diUpdatePMap()
2703 * FUNCTION: Update the persistent map in an IAG for the allocation or
2704 * freeing of the specified inode.
2706 * PRE CONDITIONS: Working map has already been updated for allocate.
2709 * ipimap - Incore inode map inode
2710 * inum - Number of inode to mark in permanent map
2711 * is_free - If 'true' indicates inode should be marked freed, otherwise
2712 * indicates inode should be marked allocated.
2718 diUpdatePMap(struct inode *ipimap,
2719 unsigned long inum, bool is_free, struct tblock * tblk)
2723 struct metapage *mp;
2724 int iagno, ino, extno, bitno;
2725 struct inomap *imap;
2727 struct jfs_log *log;
2728 int lsn, difft, diffp;
2729 unsigned long flags;
2731 imap = JFS_IP(ipimap)->i_imap;
2732 /* get the iag number containing the inode */
2733 iagno = INOTOIAG(inum);
2734 /* make sure that the iag is contained within the map */
2735 if (iagno >= imap->im_nextiag) {
2736 jfs_error(ipimap->i_sb, "the iag is outside the map\n");
2740 IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
2741 rc = diIAGRead(imap, iagno, &mp);
2742 IREAD_UNLOCK(ipimap);
2745 metapage_wait_for_io(mp);
2746 iagp = (struct iag *) mp->data;
2747 /* get the inode number and extent number of the inode within
2748 * the iag and the inode number within the extent.
2750 ino = inum & (INOSPERIAG - 1);
2751 extno = ino >> L2INOSPEREXT;
2752 bitno = ino & (INOSPEREXT - 1);
2753 mask = HIGHORDER >> bitno;
2755 * mark the inode free in persistent map:
2758 /* The inode should have been allocated both in working
2759 * map and in persistent map;
2760 * the inode will be freed from working map at the release
2761 * of last reference release;
2763 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
2764 jfs_error(ipimap->i_sb,
2765 "inode %ld not marked as allocated in wmap!\n",
2768 if (!(le32_to_cpu(iagp->pmap[extno]) & mask)) {
2769 jfs_error(ipimap->i_sb,
2770 "inode %ld not marked as allocated in pmap!\n",
2773 /* update the bitmap for the extent of the freed inode */
2774 iagp->pmap[extno] &= cpu_to_le32(~mask);
2777 * mark the inode allocated in persistent map:
2780 /* The inode should be already allocated in the working map
2781 * and should be free in persistent map;
2783 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
2784 release_metapage(mp);
2785 jfs_error(ipimap->i_sb,
2786 "the inode is not allocated in the working map\n");
2789 if ((le32_to_cpu(iagp->pmap[extno]) & mask) != 0) {
2790 release_metapage(mp);
2791 jfs_error(ipimap->i_sb,
2792 "the inode is not free in the persistent map\n");
2795 /* update the bitmap for the extent of the allocated inode */
2796 iagp->pmap[extno] |= cpu_to_le32(mask);
2802 log = JFS_SBI(tblk->sb)->log;
2803 LOGSYNC_LOCK(log, flags);
2805 /* inherit older/smaller lsn */
2806 logdiff(difft, lsn, log);
2807 logdiff(diffp, mp->lsn, log);
2808 if (difft < diffp) {
2810 /* move mp after tblock in logsync list */
2811 list_move(&mp->synclist, &tblk->synclist);
2813 /* inherit younger/larger clsn */
2815 logdiff(difft, tblk->clsn, log);
2816 logdiff(diffp, mp->clsn, log);
2818 mp->clsn = tblk->clsn;
2822 /* insert mp after tblock in logsync list */
2824 list_add(&mp->synclist, &tblk->synclist);
2825 mp->clsn = tblk->clsn;
2827 LOGSYNC_UNLOCK(log, flags);
2835 * function: update imap for extendfs();
2837 * note: AG size has been increased s.t. each k old contiguous AGs are
2838 * coalesced into a new AG;
2840 int diExtendFS(struct inode *ipimap, struct inode *ipbmap)
2843 struct inomap *imap = JFS_IP(ipimap)->i_imap;
2844 struct iag *iagp = NULL, *hiagp = NULL;
2845 struct bmap *mp = JFS_SBI(ipbmap->i_sb)->bmap;
2846 struct metapage *bp, *hbp;
2848 int numinos, xnuminos = 0, xnumfree = 0;
2851 jfs_info("diExtendFS: nextiag:%d numinos:%d numfree:%d",
2852 imap->im_nextiag, atomic_read(&imap->im_numinos),
2853 atomic_read(&imap->im_numfree));
2858 * coalesce contiguous k (newAGSize/oldAGSize) AGs;
2859 * i.e., (AGi, ..., AGj) where i = k*n and j = k*(n+1) - 1 to AGn;
2860 * note: new AG size = old AG size * (2**x).
2863 /* init per AG control information im_agctl[] */
2864 for (i = 0; i < MAXAG; i++) {
2865 imap->im_agctl[i].inofree = -1;
2866 imap->im_agctl[i].extfree = -1;
2867 imap->im_agctl[i].numinos = 0; /* number of backed inodes */
2868 imap->im_agctl[i].numfree = 0; /* number of free backed inodes */
2872 * process each iag page of the map.
2874 * rebuild AG Free Inode List, AG Free Inode Extent List;
2876 for (i = 0; i < imap->im_nextiag; i++) {
2877 if ((rc = diIAGRead(imap, i, &bp))) {
2881 iagp = (struct iag *) bp->data;
2882 if (le32_to_cpu(iagp->iagnum) != i) {
2883 release_metapage(bp);
2884 jfs_error(ipimap->i_sb, "unexpected value of iagnum\n");
2888 /* leave free iag in the free iag list */
2889 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2890 release_metapage(bp);
2894 agstart = le64_to_cpu(iagp->agstart);
2895 n = agstart >> mp->db_agl2size;
2896 iagp->agstart = cpu_to_le64((s64)n << mp->db_agl2size);
2898 /* compute backed inodes */
2899 numinos = (EXTSPERIAG - le32_to_cpu(iagp->nfreeexts))
2902 /* merge AG backed inodes */
2903 imap->im_agctl[n].numinos += numinos;
2904 xnuminos += numinos;
2907 /* if any backed free inodes, insert at AG free inode list */
2908 if ((int) le32_to_cpu(iagp->nfreeinos) > 0) {
2909 if ((head = imap->im_agctl[n].inofree) == -1) {
2910 iagp->inofreefwd = cpu_to_le32(-1);
2911 iagp->inofreeback = cpu_to_le32(-1);
2913 if ((rc = diIAGRead(imap, head, &hbp))) {
2917 hiagp = (struct iag *) hbp->data;
2918 hiagp->inofreeback = iagp->iagnum;
2919 iagp->inofreefwd = cpu_to_le32(head);
2920 iagp->inofreeback = cpu_to_le32(-1);
2921 write_metapage(hbp);
2924 imap->im_agctl[n].inofree =
2925 le32_to_cpu(iagp->iagnum);
2927 /* merge AG backed free inodes */
2928 imap->im_agctl[n].numfree +=
2929 le32_to_cpu(iagp->nfreeinos);
2930 xnumfree += le32_to_cpu(iagp->nfreeinos);
2933 /* if any free extents, insert at AG free extent list */
2934 if (le32_to_cpu(iagp->nfreeexts) > 0) {
2935 if ((head = imap->im_agctl[n].extfree) == -1) {
2936 iagp->extfreefwd = cpu_to_le32(-1);
2937 iagp->extfreeback = cpu_to_le32(-1);
2939 if ((rc = diIAGRead(imap, head, &hbp))) {
2943 hiagp = (struct iag *) hbp->data;
2944 hiagp->extfreeback = iagp->iagnum;
2945 iagp->extfreefwd = cpu_to_le32(head);
2946 iagp->extfreeback = cpu_to_le32(-1);
2947 write_metapage(hbp);
2950 imap->im_agctl[n].extfree =
2951 le32_to_cpu(iagp->iagnum);
2958 if (xnuminos != atomic_read(&imap->im_numinos) ||
2959 xnumfree != atomic_read(&imap->im_numfree)) {
2960 jfs_error(ipimap->i_sb, "numinos or numfree incorrect\n");
2971 * serialization: IWRITE_LOCK held on entry/exit
2973 * note: shadow page with regular inode (rel.2);
2975 static void duplicateIXtree(struct super_block *sb, s64 blkno,
2976 int xlen, s64 *xaddr)
2978 struct jfs_superblock *j_sb;
2979 struct buffer_head *bh;
2983 /* if AIT2 ipmap2 is bad, do not try to update it */
2984 if (JFS_SBI(sb)->mntflag & JFS_BAD_SAIT) /* s_flag */
2986 ip = diReadSpecial(sb, FILESYSTEM_I, 1);
2988 JFS_SBI(sb)->mntflag |= JFS_BAD_SAIT;
2989 if (readSuper(sb, &bh))
2991 j_sb = (struct jfs_superblock *)bh->b_data;
2992 j_sb->s_flag |= cpu_to_le32(JFS_BAD_SAIT);
2994 mark_buffer_dirty(bh);
2995 sync_dirty_buffer(bh);
3000 /* start transaction */
3001 tid = txBegin(sb, COMMIT_FORCE);
3002 /* update the inode map addressing structure to point to it */
3003 if (xtInsert(tid, ip, 0, blkno, xlen, xaddr, 0)) {
3004 JFS_SBI(sb)->mntflag |= JFS_BAD_SAIT;
3009 /* update the inode map's inode to reflect the extension */
3010 ip->i_size += PSIZE;
3011 inode_add_bytes(ip, PSIZE);
3012 txCommit(tid, 1, &ip, COMMIT_FORCE);
3019 * NAME: copy_from_dinode()
3021 * FUNCTION: Copies inode info from disk inode to in-memory inode
3025 * -ENOMEM - insufficient memory
3027 static int copy_from_dinode(struct dinode * dip, struct inode *ip)
3029 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
3030 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
3032 jfs_ip->fileset = le32_to_cpu(dip->di_fileset);
3033 jfs_ip->mode2 = le32_to_cpu(dip->di_mode);
3034 jfs_set_inode_flags(ip);
3036 ip->i_mode = le32_to_cpu(dip->di_mode) & 0xffff;
3037 if (sbi->umask != -1) {
3038 ip->i_mode = (ip->i_mode & ~0777) | (0777 & ~sbi->umask);
3039 /* For directories, add x permission if r is allowed by umask */
3040 if (S_ISDIR(ip->i_mode)) {
3041 if (ip->i_mode & 0400)
3043 if (ip->i_mode & 0040)
3045 if (ip->i_mode & 0004)
3049 set_nlink(ip, le32_to_cpu(dip->di_nlink));
3051 jfs_ip->saved_uid = make_kuid(&init_user_ns, le32_to_cpu(dip->di_uid));
3052 if (!uid_valid(sbi->uid))
3053 ip->i_uid = jfs_ip->saved_uid;
3055 ip->i_uid = sbi->uid;
3058 jfs_ip->saved_gid = make_kgid(&init_user_ns, le32_to_cpu(dip->di_gid));
3059 if (!gid_valid(sbi->gid))
3060 ip->i_gid = jfs_ip->saved_gid;
3062 ip->i_gid = sbi->gid;
3065 ip->i_size = le64_to_cpu(dip->di_size);
3066 ip->i_atime.tv_sec = le32_to_cpu(dip->di_atime.tv_sec);
3067 ip->i_atime.tv_nsec = le32_to_cpu(dip->di_atime.tv_nsec);
3068 ip->i_mtime.tv_sec = le32_to_cpu(dip->di_mtime.tv_sec);
3069 ip->i_mtime.tv_nsec = le32_to_cpu(dip->di_mtime.tv_nsec);
3070 ip->i_ctime.tv_sec = le32_to_cpu(dip->di_ctime.tv_sec);
3071 ip->i_ctime.tv_nsec = le32_to_cpu(dip->di_ctime.tv_nsec);
3072 ip->i_blocks = LBLK2PBLK(ip->i_sb, le64_to_cpu(dip->di_nblocks));
3073 ip->i_generation = le32_to_cpu(dip->di_gen);
3075 jfs_ip->ixpxd = dip->di_ixpxd; /* in-memory pxd's are little-endian */
3076 jfs_ip->acl = dip->di_acl; /* as are dxd's */
3077 jfs_ip->ea = dip->di_ea;
3078 jfs_ip->next_index = le32_to_cpu(dip->di_next_index);
3079 jfs_ip->otime = le32_to_cpu(dip->di_otime.tv_sec);
3080 jfs_ip->acltype = le32_to_cpu(dip->di_acltype);
3082 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode)) {
3083 jfs_ip->dev = le32_to_cpu(dip->di_rdev);
3084 ip->i_rdev = new_decode_dev(jfs_ip->dev);
3087 if (S_ISDIR(ip->i_mode)) {
3088 memcpy(&jfs_ip->i_dirtable, &dip->di_dirtable, 384);
3089 } else if (S_ISREG(ip->i_mode) || S_ISLNK(ip->i_mode)) {
3090 memcpy(&jfs_ip->i_xtroot, &dip->di_xtroot, 288);
3092 memcpy(&jfs_ip->i_inline_ea, &dip->di_inlineea, 128);
3094 /* Zero the in-memory-only stuff */
3096 jfs_ip->btindex = 0;
3097 jfs_ip->btorder = 0;
3100 jfs_ip->atlhead = 0;
3101 jfs_ip->atltail = 0;
3107 * NAME: copy_to_dinode()
3109 * FUNCTION: Copies inode info from in-memory inode to disk inode
3111 static void copy_to_dinode(struct dinode * dip, struct inode *ip)
3113 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
3114 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
3116 dip->di_fileset = cpu_to_le32(jfs_ip->fileset);
3117 dip->di_inostamp = cpu_to_le32(sbi->inostamp);
3118 dip->di_number = cpu_to_le32(ip->i_ino);
3119 dip->di_gen = cpu_to_le32(ip->i_generation);
3120 dip->di_size = cpu_to_le64(ip->i_size);
3121 dip->di_nblocks = cpu_to_le64(PBLK2LBLK(ip->i_sb, ip->i_blocks));
3122 dip->di_nlink = cpu_to_le32(ip->i_nlink);
3123 if (!uid_valid(sbi->uid))
3124 dip->di_uid = cpu_to_le32(i_uid_read(ip));
3126 dip->di_uid =cpu_to_le32(from_kuid(&init_user_ns,
3127 jfs_ip->saved_uid));
3128 if (!gid_valid(sbi->gid))
3129 dip->di_gid = cpu_to_le32(i_gid_read(ip));
3131 dip->di_gid = cpu_to_le32(from_kgid(&init_user_ns,
3132 jfs_ip->saved_gid));
3134 * mode2 is only needed for storing the higher order bits.
3135 * Trust i_mode for the lower order ones
3137 if (sbi->umask == -1)
3138 dip->di_mode = cpu_to_le32((jfs_ip->mode2 & 0xffff0000) |
3140 else /* Leave the original permissions alone */
3141 dip->di_mode = cpu_to_le32(jfs_ip->mode2);
3143 dip->di_atime.tv_sec = cpu_to_le32(ip->i_atime.tv_sec);
3144 dip->di_atime.tv_nsec = cpu_to_le32(ip->i_atime.tv_nsec);
3145 dip->di_ctime.tv_sec = cpu_to_le32(ip->i_ctime.tv_sec);
3146 dip->di_ctime.tv_nsec = cpu_to_le32(ip->i_ctime.tv_nsec);
3147 dip->di_mtime.tv_sec = cpu_to_le32(ip->i_mtime.tv_sec);
3148 dip->di_mtime.tv_nsec = cpu_to_le32(ip->i_mtime.tv_nsec);
3149 dip->di_ixpxd = jfs_ip->ixpxd; /* in-memory pxd's are little-endian */
3150 dip->di_acl = jfs_ip->acl; /* as are dxd's */
3151 dip->di_ea = jfs_ip->ea;
3152 dip->di_next_index = cpu_to_le32(jfs_ip->next_index);
3153 dip->di_otime.tv_sec = cpu_to_le32(jfs_ip->otime);
3154 dip->di_otime.tv_nsec = 0;
3155 dip->di_acltype = cpu_to_le32(jfs_ip->acltype);
3156 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode))
3157 dip->di_rdev = cpu_to_le32(jfs_ip->dev);