Automatic merge of rsync://rsync.kernel.org/pub/scm/linux/kernel/git/gregkh/driver...
[linux-2.6/verdex.git] / fs / jfs / jfs_imap.c
blob7acff2ce3c80f44fee086bef667ae86c12eac463
1 /*
2 * Copyright (C) International Business Machines Corp., 2000-2004
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * jfs_imap.c: inode allocation map manager
22 * Serialization:
23 * Each AG has a simple lock which is used to control the serialization of
24 * the AG level lists. This lock should be taken first whenever an AG
25 * level list will be modified or accessed.
27 * Each IAG is locked by obtaining the buffer for the IAG page.
29 * There is also a inode lock for the inode map inode. A read lock needs to
30 * be taken whenever an IAG is read from the map or the global level
31 * information is read. A write lock needs to be taken whenever the global
32 * level information is modified or an atomic operation needs to be used.
34 * If more than one IAG is read at one time, the read lock may not
35 * be given up until all of the IAG's are read. Otherwise, a deadlock
36 * may occur when trying to obtain the read lock while another thread
37 * holding the read lock is waiting on the IAG already being held.
39 * The control page of the inode map is read into memory by diMount().
40 * Thereafter it should only be modified in memory and then it will be
41 * written out when the filesystem is unmounted by diUnmount().
44 #include <linux/fs.h>
45 #include <linux/buffer_head.h>
46 #include <linux/pagemap.h>
47 #include <linux/quotaops.h>
49 #include "jfs_incore.h"
50 #include "jfs_filsys.h"
51 #include "jfs_dinode.h"
52 #include "jfs_dmap.h"
53 #include "jfs_imap.h"
54 #include "jfs_metapage.h"
55 #include "jfs_superblock.h"
56 #include "jfs_debug.h"
59 * imap locks
61 /* iag free list lock */
62 #define IAGFREE_LOCK_INIT(imap) init_MUTEX(&imap->im_freelock)
63 #define IAGFREE_LOCK(imap) down(&imap->im_freelock)
64 #define IAGFREE_UNLOCK(imap) up(&imap->im_freelock)
66 /* per ag iag list locks */
67 #define AG_LOCK_INIT(imap,index) init_MUTEX(&(imap->im_aglock[index]))
68 #define AG_LOCK(imap,agno) down(&imap->im_aglock[agno])
69 #define AG_UNLOCK(imap,agno) up(&imap->im_aglock[agno])
72 * external references
74 extern struct address_space_operations jfs_aops;
77 * forward references
79 static int diAllocAG(struct inomap *, int, boolean_t, struct inode *);
80 static int diAllocAny(struct inomap *, int, boolean_t, struct inode *);
81 static int diAllocBit(struct inomap *, struct iag *, int);
82 static int diAllocExt(struct inomap *, int, struct inode *);
83 static int diAllocIno(struct inomap *, int, struct inode *);
84 static int diFindFree(u32, int);
85 static int diNewExt(struct inomap *, struct iag *, int);
86 static int diNewIAG(struct inomap *, int *, int, struct metapage **);
87 static void duplicateIXtree(struct super_block *, s64, int, s64 *);
89 static int diIAGRead(struct inomap * imap, int, struct metapage **);
90 static int copy_from_dinode(struct dinode *, struct inode *);
91 static void copy_to_dinode(struct dinode *, struct inode *);
94 * debug code for double-checking inode map
96 /* #define _JFS_DEBUG_IMAP 1 */
98 #ifdef _JFS_DEBUG_IMAP
99 #define DBG_DIINIT(imap) DBGdiInit(imap)
100 #define DBG_DIALLOC(imap, ino) DBGdiAlloc(imap, ino)
101 #define DBG_DIFREE(imap, ino) DBGdiFree(imap, ino)
103 static void *DBGdiInit(struct inomap * imap);
104 static void DBGdiAlloc(struct inomap * imap, ino_t ino);
105 static void DBGdiFree(struct inomap * imap, ino_t ino);
106 #else
107 #define DBG_DIINIT(imap)
108 #define DBG_DIALLOC(imap, ino)
109 #define DBG_DIFREE(imap, ino)
110 #endif /* _JFS_DEBUG_IMAP */
113 * NAME: diMount()
115 * FUNCTION: initialize the incore inode map control structures for
116 * a fileset or aggregate init time.
118 * the inode map's control structure (dinomap) is
119 * brought in from disk and placed in virtual memory.
121 * PARAMETERS:
122 * ipimap - pointer to inode map inode for the aggregate or fileset.
124 * RETURN VALUES:
125 * 0 - success
126 * -ENOMEM - insufficient free virtual memory.
127 * -EIO - i/o error.
129 int diMount(struct inode *ipimap)
131 struct inomap *imap;
132 struct metapage *mp;
133 int index;
134 struct dinomap_disk *dinom_le;
137 * allocate/initialize the in-memory inode map control structure
139 /* allocate the in-memory inode map control structure. */
140 imap = (struct inomap *) kmalloc(sizeof(struct inomap), GFP_KERNEL);
141 if (imap == NULL) {
142 jfs_err("diMount: kmalloc returned NULL!");
143 return -ENOMEM;
146 /* read the on-disk inode map control structure. */
148 mp = read_metapage(ipimap,
149 IMAPBLKNO << JFS_SBI(ipimap->i_sb)->l2nbperpage,
150 PSIZE, 0);
151 if (mp == NULL) {
152 kfree(imap);
153 return -EIO;
156 /* copy the on-disk version to the in-memory version. */
157 dinom_le = (struct dinomap_disk *) mp->data;
158 imap->im_freeiag = le32_to_cpu(dinom_le->in_freeiag);
159 imap->im_nextiag = le32_to_cpu(dinom_le->in_nextiag);
160 atomic_set(&imap->im_numinos, le32_to_cpu(dinom_le->in_numinos));
161 atomic_set(&imap->im_numfree, le32_to_cpu(dinom_le->in_numfree));
162 imap->im_nbperiext = le32_to_cpu(dinom_le->in_nbperiext);
163 imap->im_l2nbperiext = le32_to_cpu(dinom_le->in_l2nbperiext);
164 for (index = 0; index < MAXAG; index++) {
165 imap->im_agctl[index].inofree =
166 le32_to_cpu(dinom_le->in_agctl[index].inofree);
167 imap->im_agctl[index].extfree =
168 le32_to_cpu(dinom_le->in_agctl[index].extfree);
169 imap->im_agctl[index].numinos =
170 le32_to_cpu(dinom_le->in_agctl[index].numinos);
171 imap->im_agctl[index].numfree =
172 le32_to_cpu(dinom_le->in_agctl[index].numfree);
175 /* release the buffer. */
176 release_metapage(mp);
179 * allocate/initialize inode allocation map locks
181 /* allocate and init iag free list lock */
182 IAGFREE_LOCK_INIT(imap);
184 /* allocate and init ag list locks */
185 for (index = 0; index < MAXAG; index++) {
186 AG_LOCK_INIT(imap, index);
189 /* bind the inode map inode and inode map control structure
190 * to each other.
192 imap->im_ipimap = ipimap;
193 JFS_IP(ipimap)->i_imap = imap;
195 // DBG_DIINIT(imap);
197 return (0);
202 * NAME: diUnmount()
204 * FUNCTION: write to disk the incore inode map control structures for
205 * a fileset or aggregate at unmount time.
207 * PARAMETERS:
208 * ipimap - pointer to inode map inode for the aggregate or fileset.
210 * RETURN VALUES:
211 * 0 - success
212 * -ENOMEM - insufficient free virtual memory.
213 * -EIO - i/o error.
215 int diUnmount(struct inode *ipimap, int mounterror)
217 struct inomap *imap = JFS_IP(ipimap)->i_imap;
220 * update the on-disk inode map control structure
223 if (!(mounterror || isReadOnly(ipimap)))
224 diSync(ipimap);
227 * Invalidate the page cache buffers
229 truncate_inode_pages(ipimap->i_mapping, 0);
232 * free in-memory control structure
234 kfree(imap);
236 return (0);
241 * diSync()
243 int diSync(struct inode *ipimap)
245 struct dinomap_disk *dinom_le;
246 struct inomap *imp = JFS_IP(ipimap)->i_imap;
247 struct metapage *mp;
248 int index;
251 * write imap global conrol page
253 /* read the on-disk inode map control structure */
254 mp = get_metapage(ipimap,
255 IMAPBLKNO << JFS_SBI(ipimap->i_sb)->l2nbperpage,
256 PSIZE, 0);
257 if (mp == NULL) {
258 jfs_err("diSync: get_metapage failed!");
259 return -EIO;
262 /* copy the in-memory version to the on-disk version */
263 dinom_le = (struct dinomap_disk *) mp->data;
264 dinom_le->in_freeiag = cpu_to_le32(imp->im_freeiag);
265 dinom_le->in_nextiag = cpu_to_le32(imp->im_nextiag);
266 dinom_le->in_numinos = cpu_to_le32(atomic_read(&imp->im_numinos));
267 dinom_le->in_numfree = cpu_to_le32(atomic_read(&imp->im_numfree));
268 dinom_le->in_nbperiext = cpu_to_le32(imp->im_nbperiext);
269 dinom_le->in_l2nbperiext = cpu_to_le32(imp->im_l2nbperiext);
270 for (index = 0; index < MAXAG; index++) {
271 dinom_le->in_agctl[index].inofree =
272 cpu_to_le32(imp->im_agctl[index].inofree);
273 dinom_le->in_agctl[index].extfree =
274 cpu_to_le32(imp->im_agctl[index].extfree);
275 dinom_le->in_agctl[index].numinos =
276 cpu_to_le32(imp->im_agctl[index].numinos);
277 dinom_le->in_agctl[index].numfree =
278 cpu_to_le32(imp->im_agctl[index].numfree);
281 /* write out the control structure */
282 write_metapage(mp);
285 * write out dirty pages of imap
287 filemap_fdatawrite(ipimap->i_mapping);
288 filemap_fdatawait(ipimap->i_mapping);
290 diWriteSpecial(ipimap, 0);
292 return (0);
297 * NAME: diRead()
299 * FUNCTION: initialize an incore inode from disk.
301 * on entry, the specifed incore inode should itself
302 * specify the disk inode number corresponding to the
303 * incore inode (i.e. i_number should be initialized).
305 * this routine handles incore inode initialization for
306 * both "special" and "regular" inodes. special inodes
307 * are those required early in the mount process and
308 * require special handling since much of the file system
309 * is not yet initialized. these "special" inodes are
310 * identified by a NULL inode map inode pointer and are
311 * actually initialized by a call to diReadSpecial().
313 * for regular inodes, the iag describing the disk inode
314 * is read from disk to determine the inode extent address
315 * for the disk inode. with the inode extent address in
316 * hand, the page of the extent that contains the disk
317 * inode is read and the disk inode is copied to the
318 * incore inode.
320 * PARAMETERS:
321 * ip - pointer to incore inode to be initialized from disk.
323 * RETURN VALUES:
324 * 0 - success
325 * -EIO - i/o error.
326 * -ENOMEM - insufficient memory
329 int diRead(struct inode *ip)
331 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
332 int iagno, ino, extno, rc;
333 struct inode *ipimap;
334 struct dinode *dp;
335 struct iag *iagp;
336 struct metapage *mp;
337 s64 blkno, agstart;
338 struct inomap *imap;
339 int block_offset;
340 int inodes_left;
341 uint pageno;
342 int rel_inode;
344 jfs_info("diRead: ino = %ld", ip->i_ino);
346 ipimap = sbi->ipimap;
347 JFS_IP(ip)->ipimap = ipimap;
349 /* determine the iag number for this inode (number) */
350 iagno = INOTOIAG(ip->i_ino);
352 /* read the iag */
353 imap = JFS_IP(ipimap)->i_imap;
354 IREAD_LOCK(ipimap);
355 rc = diIAGRead(imap, iagno, &mp);
356 IREAD_UNLOCK(ipimap);
357 if (rc) {
358 jfs_err("diRead: diIAGRead returned %d", rc);
359 return (rc);
362 iagp = (struct iag *) mp->data;
364 /* determine inode extent that holds the disk inode */
365 ino = ip->i_ino & (INOSPERIAG - 1);
366 extno = ino >> L2INOSPEREXT;
368 if ((lengthPXD(&iagp->inoext[extno]) != imap->im_nbperiext) ||
369 (addressPXD(&iagp->inoext[extno]) == 0)) {
370 release_metapage(mp);
371 return -ESTALE;
374 /* get disk block number of the page within the inode extent
375 * that holds the disk inode.
377 blkno = INOPBLK(&iagp->inoext[extno], ino, sbi->l2nbperpage);
379 /* get the ag for the iag */
380 agstart = le64_to_cpu(iagp->agstart);
382 release_metapage(mp);
384 rel_inode = (ino & (INOSPERPAGE - 1));
385 pageno = blkno >> sbi->l2nbperpage;
387 if ((block_offset = ((u32) blkno & (sbi->nbperpage - 1)))) {
389 * OS/2 didn't always align inode extents on page boundaries
391 inodes_left =
392 (sbi->nbperpage - block_offset) << sbi->l2niperblk;
394 if (rel_inode < inodes_left)
395 rel_inode += block_offset << sbi->l2niperblk;
396 else {
397 pageno += 1;
398 rel_inode -= inodes_left;
402 /* read the page of disk inode */
403 mp = read_metapage(ipimap, pageno << sbi->l2nbperpage, PSIZE, 1);
404 if (mp == 0) {
405 jfs_err("diRead: read_metapage failed");
406 return -EIO;
409 /* locate the the disk inode requested */
410 dp = (struct dinode *) mp->data;
411 dp += rel_inode;
413 if (ip->i_ino != le32_to_cpu(dp->di_number)) {
414 jfs_error(ip->i_sb, "diRead: i_ino != di_number");
415 rc = -EIO;
416 } else if (le32_to_cpu(dp->di_nlink) == 0)
417 rc = -ESTALE;
418 else
419 /* copy the disk inode to the in-memory inode */
420 rc = copy_from_dinode(dp, ip);
422 release_metapage(mp);
424 /* set the ag for the inode */
425 JFS_IP(ip)->agno = BLKTOAG(agstart, sbi);
426 JFS_IP(ip)->active_ag = -1;
428 return (rc);
433 * NAME: diReadSpecial()
435 * FUNCTION: initialize a 'special' inode from disk.
437 * this routines handles aggregate level inodes. The
438 * inode cache cannot differentiate between the
439 * aggregate inodes and the filesystem inodes, so we
440 * handle these here. We don't actually use the aggregate
441 * inode map, since these inodes are at a fixed location
442 * and in some cases the aggregate inode map isn't initialized
443 * yet.
445 * PARAMETERS:
446 * sb - filesystem superblock
447 * inum - aggregate inode number
448 * secondary - 1 if secondary aggregate inode table
450 * RETURN VALUES:
451 * new inode - success
452 * NULL - i/o error.
454 struct inode *diReadSpecial(struct super_block *sb, ino_t inum, int secondary)
456 struct jfs_sb_info *sbi = JFS_SBI(sb);
457 uint address;
458 struct dinode *dp;
459 struct inode *ip;
460 struct metapage *mp;
462 ip = new_inode(sb);
463 if (ip == NULL) {
464 jfs_err("diReadSpecial: new_inode returned NULL!");
465 return ip;
468 if (secondary) {
469 address = addressPXD(&sbi->ait2) >> sbi->l2nbperpage;
470 JFS_IP(ip)->ipimap = sbi->ipaimap2;
471 } else {
472 address = AITBL_OFF >> L2PSIZE;
473 JFS_IP(ip)->ipimap = sbi->ipaimap;
476 ASSERT(inum < INOSPEREXT);
478 ip->i_ino = inum;
480 address += inum >> 3; /* 8 inodes per 4K page */
482 /* read the page of fixed disk inode (AIT) in raw mode */
483 mp = read_metapage(ip, address << sbi->l2nbperpage, PSIZE, 1);
484 if (mp == NULL) {
485 ip->i_nlink = 1; /* Don't want iput() deleting it */
486 iput(ip);
487 return (NULL);
490 /* get the pointer to the disk inode of interest */
491 dp = (struct dinode *) (mp->data);
492 dp += inum % 8; /* 8 inodes per 4K page */
494 /* copy on-disk inode to in-memory inode */
495 if ((copy_from_dinode(dp, ip)) != 0) {
496 /* handle bad return by returning NULL for ip */
497 ip->i_nlink = 1; /* Don't want iput() deleting it */
498 iput(ip);
499 /* release the page */
500 release_metapage(mp);
501 return (NULL);
505 ip->i_mapping->a_ops = &jfs_metapage_aops;
506 mapping_set_gfp_mask(ip->i_mapping, GFP_NOFS);
508 /* Allocations to metadata inodes should not affect quotas */
509 ip->i_flags |= S_NOQUOTA;
511 if ((inum == FILESYSTEM_I) && (JFS_IP(ip)->ipimap == sbi->ipaimap)) {
512 sbi->gengen = le32_to_cpu(dp->di_gengen);
513 sbi->inostamp = le32_to_cpu(dp->di_inostamp);
516 /* release the page */
517 release_metapage(mp);
519 return (ip);
523 * NAME: diWriteSpecial()
525 * FUNCTION: Write the special inode to disk
527 * PARAMETERS:
528 * ip - special inode
529 * secondary - 1 if secondary aggregate inode table
531 * RETURN VALUES: none
534 void diWriteSpecial(struct inode *ip, int secondary)
536 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
537 uint address;
538 struct dinode *dp;
539 ino_t inum = ip->i_ino;
540 struct metapage *mp;
542 ip->i_state &= ~I_DIRTY;
544 if (secondary)
545 address = addressPXD(&sbi->ait2) >> sbi->l2nbperpage;
546 else
547 address = AITBL_OFF >> L2PSIZE;
549 ASSERT(inum < INOSPEREXT);
551 address += inum >> 3; /* 8 inodes per 4K page */
553 /* read the page of fixed disk inode (AIT) in raw mode */
554 mp = read_metapage(ip, address << sbi->l2nbperpage, PSIZE, 1);
555 if (mp == NULL) {
556 jfs_err("diWriteSpecial: failed to read aggregate inode "
557 "extent!");
558 return;
561 /* get the pointer to the disk inode of interest */
562 dp = (struct dinode *) (mp->data);
563 dp += inum % 8; /* 8 inodes per 4K page */
565 /* copy on-disk inode to in-memory inode */
566 copy_to_dinode(dp, ip);
567 memcpy(&dp->di_xtroot, &JFS_IP(ip)->i_xtroot, 288);
569 if (inum == FILESYSTEM_I)
570 dp->di_gengen = cpu_to_le32(sbi->gengen);
572 /* write the page */
573 write_metapage(mp);
577 * NAME: diFreeSpecial()
579 * FUNCTION: Free allocated space for special inode
581 void diFreeSpecial(struct inode *ip)
583 if (ip == NULL) {
584 jfs_err("diFreeSpecial called with NULL ip!");
585 return;
587 filemap_fdatawrite(ip->i_mapping);
588 filemap_fdatawait(ip->i_mapping);
589 truncate_inode_pages(ip->i_mapping, 0);
590 iput(ip);
596 * NAME: diWrite()
598 * FUNCTION: write the on-disk inode portion of the in-memory inode
599 * to its corresponding on-disk inode.
601 * on entry, the specifed incore inode should itself
602 * specify the disk inode number corresponding to the
603 * incore inode (i.e. i_number should be initialized).
605 * the inode contains the inode extent address for the disk
606 * inode. with the inode extent address in hand, the
607 * page of the extent that contains the disk inode is
608 * read and the disk inode portion of the incore inode
609 * is copied to the disk inode.
611 * PARAMETERS:
612 * tid - transacation id
613 * ip - pointer to incore inode to be written to the inode extent.
615 * RETURN VALUES:
616 * 0 - success
617 * -EIO - i/o error.
619 int diWrite(tid_t tid, struct inode *ip)
621 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
622 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
623 int rc = 0;
624 s32 ino;
625 struct dinode *dp;
626 s64 blkno;
627 int block_offset;
628 int inodes_left;
629 struct metapage *mp;
630 uint pageno;
631 int rel_inode;
632 int dioffset;
633 struct inode *ipimap;
634 uint type;
635 lid_t lid;
636 struct tlock *ditlck, *tlck;
637 struct linelock *dilinelock, *ilinelock;
638 struct lv *lv;
639 int n;
641 ipimap = jfs_ip->ipimap;
643 ino = ip->i_ino & (INOSPERIAG - 1);
645 if (!addressPXD(&(jfs_ip->ixpxd)) ||
646 (lengthPXD(&(jfs_ip->ixpxd)) !=
647 JFS_IP(ipimap)->i_imap->im_nbperiext)) {
648 jfs_error(ip->i_sb, "diWrite: ixpxd invalid");
649 return -EIO;
653 * read the page of disk inode containing the specified inode:
655 /* compute the block address of the page */
656 blkno = INOPBLK(&(jfs_ip->ixpxd), ino, sbi->l2nbperpage);
658 rel_inode = (ino & (INOSPERPAGE - 1));
659 pageno = blkno >> sbi->l2nbperpage;
661 if ((block_offset = ((u32) blkno & (sbi->nbperpage - 1)))) {
663 * OS/2 didn't always align inode extents on page boundaries
665 inodes_left =
666 (sbi->nbperpage - block_offset) << sbi->l2niperblk;
668 if (rel_inode < inodes_left)
669 rel_inode += block_offset << sbi->l2niperblk;
670 else {
671 pageno += 1;
672 rel_inode -= inodes_left;
675 /* read the page of disk inode */
676 retry:
677 mp = read_metapage(ipimap, pageno << sbi->l2nbperpage, PSIZE, 1);
678 if (mp == 0)
679 return -EIO;
681 /* get the pointer to the disk inode */
682 dp = (struct dinode *) mp->data;
683 dp += rel_inode;
685 dioffset = (ino & (INOSPERPAGE - 1)) << L2DISIZE;
688 * acquire transaction lock on the on-disk inode;
689 * N.B. tlock is acquired on ipimap not ip;
691 if ((ditlck =
692 txLock(tid, ipimap, mp, tlckINODE | tlckENTRY)) == NULL)
693 goto retry;
694 dilinelock = (struct linelock *) & ditlck->lock;
697 * copy btree root from in-memory inode to on-disk inode
699 * (tlock is taken from inline B+-tree root in in-memory
700 * inode when the B+-tree root is updated, which is pointed
701 * by jfs_ip->blid as well as being on tx tlock list)
703 * further processing of btree root is based on the copy
704 * in in-memory inode, where txLog() will log from, and,
705 * for xtree root, txUpdateMap() will update map and reset
706 * XAD_NEW bit;
709 if (S_ISDIR(ip->i_mode) && (lid = jfs_ip->xtlid)) {
711 * This is the special xtree inside the directory for storing
712 * the directory table
714 xtpage_t *p, *xp;
715 xad_t *xad;
717 jfs_ip->xtlid = 0;
718 tlck = lid_to_tlock(lid);
719 assert(tlck->type & tlckXTREE);
720 tlck->type |= tlckBTROOT;
721 tlck->mp = mp;
722 ilinelock = (struct linelock *) & tlck->lock;
725 * copy xtree root from inode to dinode:
727 p = &jfs_ip->i_xtroot;
728 xp = (xtpage_t *) &dp->di_dirtable;
729 lv = ilinelock->lv;
730 for (n = 0; n < ilinelock->index; n++, lv++) {
731 memcpy(&xp->xad[lv->offset], &p->xad[lv->offset],
732 lv->length << L2XTSLOTSIZE);
735 /* reset on-disk (metadata page) xtree XAD_NEW bit */
736 xad = &xp->xad[XTENTRYSTART];
737 for (n = XTENTRYSTART;
738 n < le16_to_cpu(xp->header.nextindex); n++, xad++)
739 if (xad->flag & (XAD_NEW | XAD_EXTENDED))
740 xad->flag &= ~(XAD_NEW | XAD_EXTENDED);
743 if ((lid = jfs_ip->blid) == 0)
744 goto inlineData;
745 jfs_ip->blid = 0;
747 tlck = lid_to_tlock(lid);
748 type = tlck->type;
749 tlck->type |= tlckBTROOT;
750 tlck->mp = mp;
751 ilinelock = (struct linelock *) & tlck->lock;
754 * regular file: 16 byte (XAD slot) granularity
756 if (type & tlckXTREE) {
757 xtpage_t *p, *xp;
758 xad_t *xad;
761 * copy xtree root from inode to dinode:
763 p = &jfs_ip->i_xtroot;
764 xp = &dp->di_xtroot;
765 lv = ilinelock->lv;
766 for (n = 0; n < ilinelock->index; n++, lv++) {
767 memcpy(&xp->xad[lv->offset], &p->xad[lv->offset],
768 lv->length << L2XTSLOTSIZE);
771 /* reset on-disk (metadata page) xtree XAD_NEW bit */
772 xad = &xp->xad[XTENTRYSTART];
773 for (n = XTENTRYSTART;
774 n < le16_to_cpu(xp->header.nextindex); n++, xad++)
775 if (xad->flag & (XAD_NEW | XAD_EXTENDED))
776 xad->flag &= ~(XAD_NEW | XAD_EXTENDED);
779 * directory: 32 byte (directory entry slot) granularity
781 else if (type & tlckDTREE) {
782 dtpage_t *p, *xp;
785 * copy dtree root from inode to dinode:
787 p = (dtpage_t *) &jfs_ip->i_dtroot;
788 xp = (dtpage_t *) & dp->di_dtroot;
789 lv = ilinelock->lv;
790 for (n = 0; n < ilinelock->index; n++, lv++) {
791 memcpy(&xp->slot[lv->offset], &p->slot[lv->offset],
792 lv->length << L2DTSLOTSIZE);
794 } else {
795 jfs_err("diWrite: UFO tlock");
798 inlineData:
800 * copy inline symlink from in-memory inode to on-disk inode
802 if (S_ISLNK(ip->i_mode) && ip->i_size < IDATASIZE) {
803 lv = & dilinelock->lv[dilinelock->index];
804 lv->offset = (dioffset + 2 * 128) >> L2INODESLOTSIZE;
805 lv->length = 2;
806 memcpy(&dp->di_fastsymlink, jfs_ip->i_inline, IDATASIZE);
807 dilinelock->index++;
810 * copy inline data from in-memory inode to on-disk inode:
811 * 128 byte slot granularity
813 if (test_cflag(COMMIT_Inlineea, ip)) {
814 lv = & dilinelock->lv[dilinelock->index];
815 lv->offset = (dioffset + 3 * 128) >> L2INODESLOTSIZE;
816 lv->length = 1;
817 memcpy(&dp->di_inlineea, jfs_ip->i_inline_ea, INODESLOTSIZE);
818 dilinelock->index++;
820 clear_cflag(COMMIT_Inlineea, ip);
824 * lock/copy inode base: 128 byte slot granularity
826 // baseDinode:
827 lv = & dilinelock->lv[dilinelock->index];
828 lv->offset = dioffset >> L2INODESLOTSIZE;
829 copy_to_dinode(dp, ip);
830 if (test_and_clear_cflag(COMMIT_Dirtable, ip)) {
831 lv->length = 2;
832 memcpy(&dp->di_dirtable, &jfs_ip->i_dirtable, 96);
833 } else
834 lv->length = 1;
835 dilinelock->index++;
837 #ifdef _JFS_FASTDASD
839 * We aren't logging changes to the DASD used in directory inodes,
840 * but we need to write them to disk. If we don't unmount cleanly,
841 * mount will recalculate the DASD used.
843 if (S_ISDIR(ip->i_mode)
844 && (ip->i_ipmnt->i_mntflag & JFS_DASD_ENABLED))
845 memcpy(&dp->di_DASD, &ip->i_DASD, sizeof(struct dasd));
846 #endif /* _JFS_FASTDASD */
848 /* release the buffer holding the updated on-disk inode.
849 * the buffer will be later written by commit processing.
851 write_metapage(mp);
853 return (rc);
858 * NAME: diFree(ip)
860 * FUNCTION: free a specified inode from the inode working map
861 * for a fileset or aggregate.
863 * if the inode to be freed represents the first (only)
864 * free inode within the iag, the iag will be placed on
865 * the ag free inode list.
867 * freeing the inode will cause the inode extent to be
868 * freed if the inode is the only allocated inode within
869 * the extent. in this case all the disk resource backing
870 * up the inode extent will be freed. in addition, the iag
871 * will be placed on the ag extent free list if the extent
872 * is the first free extent in the iag. if freeing the
873 * extent also means that no free inodes will exist for
874 * the iag, the iag will also be removed from the ag free
875 * inode list.
877 * the iag describing the inode will be freed if the extent
878 * is to be freed and it is the only backed extent within
879 * the iag. in this case, the iag will be removed from the
880 * ag free extent list and ag free inode list and placed on
881 * the inode map's free iag list.
883 * a careful update approach is used to provide consistency
884 * in the face of updates to multiple buffers. under this
885 * approach, all required buffers are obtained before making
886 * any updates and are held until all updates are complete.
888 * PARAMETERS:
889 * ip - inode to be freed.
891 * RETURN VALUES:
892 * 0 - success
893 * -EIO - i/o error.
895 int diFree(struct inode *ip)
897 int rc;
898 ino_t inum = ip->i_ino;
899 struct iag *iagp, *aiagp, *biagp, *ciagp, *diagp;
900 struct metapage *mp, *amp, *bmp, *cmp, *dmp;
901 int iagno, ino, extno, bitno, sword, agno;
902 int back, fwd;
903 u32 bitmap, mask;
904 struct inode *ipimap = JFS_SBI(ip->i_sb)->ipimap;
905 struct inomap *imap = JFS_IP(ipimap)->i_imap;
906 pxd_t freepxd;
907 tid_t tid;
908 struct inode *iplist[3];
909 struct tlock *tlck;
910 struct pxd_lock *pxdlock;
913 * This is just to suppress compiler warnings. The same logic that
914 * references these variables is used to initialize them.
916 aiagp = biagp = ciagp = diagp = NULL;
918 /* get the iag number containing the inode.
920 iagno = INOTOIAG(inum);
922 /* make sure that the iag is contained within
923 * the map.
925 if (iagno >= imap->im_nextiag) {
926 dump_mem("imap", imap, 32);
927 jfs_error(ip->i_sb,
928 "diFree: inum = %d, iagno = %d, nextiag = %d",
929 (uint) inum, iagno, imap->im_nextiag);
930 return -EIO;
933 /* get the allocation group for this ino.
935 agno = JFS_IP(ip)->agno;
937 /* Lock the AG specific inode map information
939 AG_LOCK(imap, agno);
941 /* Obtain read lock in imap inode. Don't release it until we have
942 * read all of the IAG's that we are going to.
944 IREAD_LOCK(ipimap);
946 /* read the iag.
948 if ((rc = diIAGRead(imap, iagno, &mp))) {
949 IREAD_UNLOCK(ipimap);
950 AG_UNLOCK(imap, agno);
951 return (rc);
953 iagp = (struct iag *) mp->data;
955 /* get the inode number and extent number of the inode within
956 * the iag and the inode number within the extent.
958 ino = inum & (INOSPERIAG - 1);
959 extno = ino >> L2INOSPEREXT;
960 bitno = ino & (INOSPEREXT - 1);
961 mask = HIGHORDER >> bitno;
963 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
964 jfs_error(ip->i_sb,
965 "diFree: wmap shows inode already free");
968 if (!addressPXD(&iagp->inoext[extno])) {
969 release_metapage(mp);
970 IREAD_UNLOCK(ipimap);
971 AG_UNLOCK(imap, agno);
972 jfs_error(ip->i_sb, "diFree: invalid inoext");
973 return -EIO;
976 /* compute the bitmap for the extent reflecting the freed inode.
978 bitmap = le32_to_cpu(iagp->wmap[extno]) & ~mask;
980 if (imap->im_agctl[agno].numfree > imap->im_agctl[agno].numinos) {
981 release_metapage(mp);
982 IREAD_UNLOCK(ipimap);
983 AG_UNLOCK(imap, agno);
984 jfs_error(ip->i_sb, "diFree: numfree > numinos");
985 return -EIO;
988 * inode extent still has some inodes or below low water mark:
989 * keep the inode extent;
991 if (bitmap ||
992 imap->im_agctl[agno].numfree < 96 ||
993 (imap->im_agctl[agno].numfree < 288 &&
994 (((imap->im_agctl[agno].numfree * 100) /
995 imap->im_agctl[agno].numinos) <= 25))) {
996 /* if the iag currently has no free inodes (i.e.,
997 * the inode being freed is the first free inode of iag),
998 * insert the iag at head of the inode free list for the ag.
1000 if (iagp->nfreeinos == 0) {
1001 /* check if there are any iags on the ag inode
1002 * free list. if so, read the first one so that
1003 * we can link the current iag onto the list at
1004 * the head.
1006 if ((fwd = imap->im_agctl[agno].inofree) >= 0) {
1007 /* read the iag that currently is the head
1008 * of the list.
1010 if ((rc = diIAGRead(imap, fwd, &amp))) {
1011 IREAD_UNLOCK(ipimap);
1012 AG_UNLOCK(imap, agno);
1013 release_metapage(mp);
1014 return (rc);
1016 aiagp = (struct iag *) amp->data;
1018 /* make current head point back to the iag.
1020 aiagp->inofreeback = cpu_to_le32(iagno);
1022 write_metapage(amp);
1025 /* iag points forward to current head and iag
1026 * becomes the new head of the list.
1028 iagp->inofreefwd =
1029 cpu_to_le32(imap->im_agctl[agno].inofree);
1030 iagp->inofreeback = cpu_to_le32(-1);
1031 imap->im_agctl[agno].inofree = iagno;
1033 IREAD_UNLOCK(ipimap);
1035 /* update the free inode summary map for the extent if
1036 * freeing the inode means the extent will now have free
1037 * inodes (i.e., the inode being freed is the first free
1038 * inode of extent),
1040 if (iagp->wmap[extno] == cpu_to_le32(ONES)) {
1041 sword = extno >> L2EXTSPERSUM;
1042 bitno = extno & (EXTSPERSUM - 1);
1043 iagp->inosmap[sword] &=
1044 cpu_to_le32(~(HIGHORDER >> bitno));
1047 /* update the bitmap.
1049 iagp->wmap[extno] = cpu_to_le32(bitmap);
1050 DBG_DIFREE(imap, inum);
1052 /* update the free inode counts at the iag, ag and
1053 * map level.
1055 iagp->nfreeinos =
1056 cpu_to_le32(le32_to_cpu(iagp->nfreeinos) + 1);
1057 imap->im_agctl[agno].numfree += 1;
1058 atomic_inc(&imap->im_numfree);
1060 /* release the AG inode map lock
1062 AG_UNLOCK(imap, agno);
1064 /* write the iag */
1065 write_metapage(mp);
1067 return (0);
1072 * inode extent has become free and above low water mark:
1073 * free the inode extent;
1077 * prepare to update iag list(s) (careful update step 1)
1079 amp = bmp = cmp = dmp = NULL;
1080 fwd = back = -1;
1082 /* check if the iag currently has no free extents. if so,
1083 * it will be placed on the head of the ag extent free list.
1085 if (iagp->nfreeexts == 0) {
1086 /* check if the ag extent free list has any iags.
1087 * if so, read the iag at the head of the list now.
1088 * this (head) iag will be updated later to reflect
1089 * the addition of the current iag at the head of
1090 * the list.
1092 if ((fwd = imap->im_agctl[agno].extfree) >= 0) {
1093 if ((rc = diIAGRead(imap, fwd, &amp)))
1094 goto error_out;
1095 aiagp = (struct iag *) amp->data;
1097 } else {
1098 /* iag has free extents. check if the addition of a free
1099 * extent will cause all extents to be free within this
1100 * iag. if so, the iag will be removed from the ag extent
1101 * free list and placed on the inode map's free iag list.
1103 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG - 1)) {
1104 /* in preparation for removing the iag from the
1105 * ag extent free list, read the iags preceeding
1106 * and following the iag on the ag extent free
1107 * list.
1109 if ((fwd = le32_to_cpu(iagp->extfreefwd)) >= 0) {
1110 if ((rc = diIAGRead(imap, fwd, &amp)))
1111 goto error_out;
1112 aiagp = (struct iag *) amp->data;
1115 if ((back = le32_to_cpu(iagp->extfreeback)) >= 0) {
1116 if ((rc = diIAGRead(imap, back, &bmp)))
1117 goto error_out;
1118 biagp = (struct iag *) bmp->data;
1123 /* remove the iag from the ag inode free list if freeing
1124 * this extent cause the iag to have no free inodes.
1126 if (iagp->nfreeinos == cpu_to_le32(INOSPEREXT - 1)) {
1127 int inofreeback = le32_to_cpu(iagp->inofreeback);
1128 int inofreefwd = le32_to_cpu(iagp->inofreefwd);
1130 /* in preparation for removing the iag from the
1131 * ag inode free list, read the iags preceeding
1132 * and following the iag on the ag inode free
1133 * list. before reading these iags, we must make
1134 * sure that we already don't have them in hand
1135 * from up above, since re-reading an iag (buffer)
1136 * we are currently holding would cause a deadlock.
1138 if (inofreefwd >= 0) {
1140 if (inofreefwd == fwd)
1141 ciagp = (struct iag *) amp->data;
1142 else if (inofreefwd == back)
1143 ciagp = (struct iag *) bmp->data;
1144 else {
1145 if ((rc =
1146 diIAGRead(imap, inofreefwd, &cmp)))
1147 goto error_out;
1148 ciagp = (struct iag *) cmp->data;
1150 assert(ciagp != NULL);
1153 if (inofreeback >= 0) {
1154 if (inofreeback == fwd)
1155 diagp = (struct iag *) amp->data;
1156 else if (inofreeback == back)
1157 diagp = (struct iag *) bmp->data;
1158 else {
1159 if ((rc =
1160 diIAGRead(imap, inofreeback, &dmp)))
1161 goto error_out;
1162 diagp = (struct iag *) dmp->data;
1164 assert(diagp != NULL);
1168 IREAD_UNLOCK(ipimap);
1171 * invalidate any page of the inode extent freed from buffer cache;
1173 freepxd = iagp->inoext[extno];
1174 invalidate_pxd_metapages(ip, freepxd);
1177 * update iag list(s) (careful update step 2)
1179 /* add the iag to the ag extent free list if this is the
1180 * first free extent for the iag.
1182 if (iagp->nfreeexts == 0) {
1183 if (fwd >= 0)
1184 aiagp->extfreeback = cpu_to_le32(iagno);
1186 iagp->extfreefwd =
1187 cpu_to_le32(imap->im_agctl[agno].extfree);
1188 iagp->extfreeback = cpu_to_le32(-1);
1189 imap->im_agctl[agno].extfree = iagno;
1190 } else {
1191 /* remove the iag from the ag extent list if all extents
1192 * are now free and place it on the inode map iag free list.
1194 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG - 1)) {
1195 if (fwd >= 0)
1196 aiagp->extfreeback = iagp->extfreeback;
1198 if (back >= 0)
1199 biagp->extfreefwd = iagp->extfreefwd;
1200 else
1201 imap->im_agctl[agno].extfree =
1202 le32_to_cpu(iagp->extfreefwd);
1204 iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
1206 IAGFREE_LOCK(imap);
1207 iagp->iagfree = cpu_to_le32(imap->im_freeiag);
1208 imap->im_freeiag = iagno;
1209 IAGFREE_UNLOCK(imap);
1213 /* remove the iag from the ag inode free list if freeing
1214 * this extent causes the iag to have no free inodes.
1216 if (iagp->nfreeinos == cpu_to_le32(INOSPEREXT - 1)) {
1217 if ((int) le32_to_cpu(iagp->inofreefwd) >= 0)
1218 ciagp->inofreeback = iagp->inofreeback;
1220 if ((int) le32_to_cpu(iagp->inofreeback) >= 0)
1221 diagp->inofreefwd = iagp->inofreefwd;
1222 else
1223 imap->im_agctl[agno].inofree =
1224 le32_to_cpu(iagp->inofreefwd);
1226 iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
1229 /* update the inode extent address and working map
1230 * to reflect the free extent.
1231 * the permanent map should have been updated already
1232 * for the inode being freed.
1234 if (iagp->pmap[extno] != 0) {
1235 jfs_error(ip->i_sb, "diFree: the pmap does not show inode free");
1237 iagp->wmap[extno] = 0;
1238 DBG_DIFREE(imap, inum);
1239 PXDlength(&iagp->inoext[extno], 0);
1240 PXDaddress(&iagp->inoext[extno], 0);
1242 /* update the free extent and free inode summary maps
1243 * to reflect the freed extent.
1244 * the inode summary map is marked to indicate no inodes
1245 * available for the freed extent.
1247 sword = extno >> L2EXTSPERSUM;
1248 bitno = extno & (EXTSPERSUM - 1);
1249 mask = HIGHORDER >> bitno;
1250 iagp->inosmap[sword] |= cpu_to_le32(mask);
1251 iagp->extsmap[sword] &= cpu_to_le32(~mask);
1253 /* update the number of free inodes and number of free extents
1254 * for the iag.
1256 iagp->nfreeinos = cpu_to_le32(le32_to_cpu(iagp->nfreeinos) -
1257 (INOSPEREXT - 1));
1258 iagp->nfreeexts = cpu_to_le32(le32_to_cpu(iagp->nfreeexts) + 1);
1260 /* update the number of free inodes and backed inodes
1261 * at the ag and inode map level.
1263 imap->im_agctl[agno].numfree -= (INOSPEREXT - 1);
1264 imap->im_agctl[agno].numinos -= INOSPEREXT;
1265 atomic_sub(INOSPEREXT - 1, &imap->im_numfree);
1266 atomic_sub(INOSPEREXT, &imap->im_numinos);
1268 if (amp)
1269 write_metapage(amp);
1270 if (bmp)
1271 write_metapage(bmp);
1272 if (cmp)
1273 write_metapage(cmp);
1274 if (dmp)
1275 write_metapage(dmp);
1278 * start transaction to update block allocation map
1279 * for the inode extent freed;
1281 * N.B. AG_LOCK is released and iag will be released below, and
1282 * other thread may allocate inode from/reusing the ixad freed
1283 * BUT with new/different backing inode extent from the extent
1284 * to be freed by the transaction;
1286 tid = txBegin(ipimap->i_sb, COMMIT_FORCE);
1287 down(&JFS_IP(ipimap)->commit_sem);
1289 /* acquire tlock of the iag page of the freed ixad
1290 * to force the page NOHOMEOK (even though no data is
1291 * logged from the iag page) until NOREDOPAGE|FREEXTENT log
1292 * for the free of the extent is committed;
1293 * write FREEXTENT|NOREDOPAGE log record
1294 * N.B. linelock is overlaid as freed extent descriptor;
1296 tlck = txLock(tid, ipimap, mp, tlckINODE | tlckFREE);
1297 pxdlock = (struct pxd_lock *) & tlck->lock;
1298 pxdlock->flag = mlckFREEPXD;
1299 pxdlock->pxd = freepxd;
1300 pxdlock->index = 1;
1302 write_metapage(mp);
1304 iplist[0] = ipimap;
1307 * logredo needs the IAG number and IAG extent index in order
1308 * to ensure that the IMap is consistent. The least disruptive
1309 * way to pass these values through to the transaction manager
1310 * is in the iplist array.
1312 * It's not pretty, but it works.
1314 iplist[1] = (struct inode *) (size_t)iagno;
1315 iplist[2] = (struct inode *) (size_t)extno;
1317 rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
1319 txEnd(tid);
1320 up(&JFS_IP(ipimap)->commit_sem);
1322 /* unlock the AG inode map information */
1323 AG_UNLOCK(imap, agno);
1325 return (0);
1327 error_out:
1328 IREAD_UNLOCK(ipimap);
1330 if (amp)
1331 release_metapage(amp);
1332 if (bmp)
1333 release_metapage(bmp);
1334 if (cmp)
1335 release_metapage(cmp);
1336 if (dmp)
1337 release_metapage(dmp);
1339 AG_UNLOCK(imap, agno);
1341 release_metapage(mp);
1343 return (rc);
1347 * There are several places in the diAlloc* routines where we initialize
1348 * the inode.
1350 static inline void
1351 diInitInode(struct inode *ip, int iagno, int ino, int extno, struct iag * iagp)
1353 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
1354 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
1356 ip->i_ino = (iagno << L2INOSPERIAG) + ino;
1357 DBG_DIALLOC(JFS_IP(ipimap)->i_imap, ip->i_ino);
1358 jfs_ip->ixpxd = iagp->inoext[extno];
1359 jfs_ip->agno = BLKTOAG(le64_to_cpu(iagp->agstart), sbi);
1360 jfs_ip->active_ag = -1;
1365 * NAME: diAlloc(pip,dir,ip)
1367 * FUNCTION: allocate a disk inode from the inode working map
1368 * for a fileset or aggregate.
1370 * PARAMETERS:
1371 * pip - pointer to incore inode for the parent inode.
1372 * dir - TRUE if the new disk inode is for a directory.
1373 * ip - pointer to a new inode
1375 * RETURN VALUES:
1376 * 0 - success.
1377 * -ENOSPC - insufficient disk resources.
1378 * -EIO - i/o error.
1380 int diAlloc(struct inode *pip, boolean_t dir, struct inode *ip)
1382 int rc, ino, iagno, addext, extno, bitno, sword;
1383 int nwords, rem, i, agno;
1384 u32 mask, inosmap, extsmap;
1385 struct inode *ipimap;
1386 struct metapage *mp;
1387 ino_t inum;
1388 struct iag *iagp;
1389 struct inomap *imap;
1391 /* get the pointers to the inode map inode and the
1392 * corresponding imap control structure.
1394 ipimap = JFS_SBI(pip->i_sb)->ipimap;
1395 imap = JFS_IP(ipimap)->i_imap;
1396 JFS_IP(ip)->ipimap = ipimap;
1397 JFS_IP(ip)->fileset = FILESYSTEM_I;
1399 /* for a directory, the allocation policy is to start
1400 * at the ag level using the preferred ag.
1402 if (dir == TRUE) {
1403 agno = dbNextAG(JFS_SBI(pip->i_sb)->ipbmap);
1404 AG_LOCK(imap, agno);
1405 goto tryag;
1408 /* for files, the policy starts off by trying to allocate from
1409 * the same iag containing the parent disk inode:
1410 * try to allocate the new disk inode close to the parent disk
1411 * inode, using parent disk inode number + 1 as the allocation
1412 * hint. (we use a left-to-right policy to attempt to avoid
1413 * moving backward on the disk.) compute the hint within the
1414 * file system and the iag.
1417 /* get the ag number of this iag */
1418 agno = JFS_IP(pip)->agno;
1420 if (atomic_read(&JFS_SBI(pip->i_sb)->bmap->db_active[agno])) {
1422 * There is an open file actively growing. We want to
1423 * allocate new inodes from a different ag to avoid
1424 * fragmentation problems.
1426 agno = dbNextAG(JFS_SBI(pip->i_sb)->ipbmap);
1427 AG_LOCK(imap, agno);
1428 goto tryag;
1431 inum = pip->i_ino + 1;
1432 ino = inum & (INOSPERIAG - 1);
1434 /* back off the the hint if it is outside of the iag */
1435 if (ino == 0)
1436 inum = pip->i_ino;
1438 /* lock the AG inode map information */
1439 AG_LOCK(imap, agno);
1441 /* Get read lock on imap inode */
1442 IREAD_LOCK(ipimap);
1444 /* get the iag number and read the iag */
1445 iagno = INOTOIAG(inum);
1446 if ((rc = diIAGRead(imap, iagno, &mp))) {
1447 IREAD_UNLOCK(ipimap);
1448 AG_UNLOCK(imap, agno);
1449 return (rc);
1451 iagp = (struct iag *) mp->data;
1453 /* determine if new inode extent is allowed to be added to the iag.
1454 * new inode extent can be added to the iag if the ag
1455 * has less than 32 free disk inodes and the iag has free extents.
1457 addext = (imap->im_agctl[agno].numfree < 32 && iagp->nfreeexts);
1460 * try to allocate from the IAG
1462 /* check if the inode may be allocated from the iag
1463 * (i.e. the inode has free inodes or new extent can be added).
1465 if (iagp->nfreeinos || addext) {
1466 /* determine the extent number of the hint.
1468 extno = ino >> L2INOSPEREXT;
1470 /* check if the extent containing the hint has backed
1471 * inodes. if so, try to allocate within this extent.
1473 if (addressPXD(&iagp->inoext[extno])) {
1474 bitno = ino & (INOSPEREXT - 1);
1475 if ((bitno =
1476 diFindFree(le32_to_cpu(iagp->wmap[extno]),
1477 bitno))
1478 < INOSPEREXT) {
1479 ino = (extno << L2INOSPEREXT) + bitno;
1481 /* a free inode (bit) was found within this
1482 * extent, so allocate it.
1484 rc = diAllocBit(imap, iagp, ino);
1485 IREAD_UNLOCK(ipimap);
1486 if (rc) {
1487 assert(rc == -EIO);
1488 } else {
1489 /* set the results of the allocation
1490 * and write the iag.
1492 diInitInode(ip, iagno, ino, extno,
1493 iagp);
1494 mark_metapage_dirty(mp);
1496 release_metapage(mp);
1498 /* free the AG lock and return.
1500 AG_UNLOCK(imap, agno);
1501 return (rc);
1504 if (!addext)
1505 extno =
1506 (extno ==
1507 EXTSPERIAG - 1) ? 0 : extno + 1;
1511 * no free inodes within the extent containing the hint.
1513 * try to allocate from the backed extents following
1514 * hint or, if appropriate (i.e. addext is true), allocate
1515 * an extent of free inodes at or following the extent
1516 * containing the hint.
1518 * the free inode and free extent summary maps are used
1519 * here, so determine the starting summary map position
1520 * and the number of words we'll have to examine. again,
1521 * the approach is to allocate following the hint, so we
1522 * might have to initially ignore prior bits of the summary
1523 * map that represent extents prior to the extent containing
1524 * the hint and later revisit these bits.
1526 bitno = extno & (EXTSPERSUM - 1);
1527 nwords = (bitno == 0) ? SMAPSZ : SMAPSZ + 1;
1528 sword = extno >> L2EXTSPERSUM;
1530 /* mask any prior bits for the starting words of the
1531 * summary map.
1533 mask = ONES << (EXTSPERSUM - bitno);
1534 inosmap = le32_to_cpu(iagp->inosmap[sword]) | mask;
1535 extsmap = le32_to_cpu(iagp->extsmap[sword]) | mask;
1537 /* scan the free inode and free extent summary maps for
1538 * free resources.
1540 for (i = 0; i < nwords; i++) {
1541 /* check if this word of the free inode summary
1542 * map describes an extent with free inodes.
1544 if (~inosmap) {
1545 /* an extent with free inodes has been
1546 * found. determine the extent number
1547 * and the inode number within the extent.
1549 rem = diFindFree(inosmap, 0);
1550 extno = (sword << L2EXTSPERSUM) + rem;
1551 rem = diFindFree(le32_to_cpu(iagp->wmap[extno]),
1553 if (rem >= INOSPEREXT) {
1554 IREAD_UNLOCK(ipimap);
1555 release_metapage(mp);
1556 AG_UNLOCK(imap, agno);
1557 jfs_error(ip->i_sb,
1558 "diAlloc: can't find free bit "
1559 "in wmap");
1560 return EIO;
1563 /* determine the inode number within the
1564 * iag and allocate the inode from the
1565 * map.
1567 ino = (extno << L2INOSPEREXT) + rem;
1568 rc = diAllocBit(imap, iagp, ino);
1569 IREAD_UNLOCK(ipimap);
1570 if (rc)
1571 assert(rc == -EIO);
1572 else {
1573 /* set the results of the allocation
1574 * and write the iag.
1576 diInitInode(ip, iagno, ino, extno,
1577 iagp);
1578 mark_metapage_dirty(mp);
1580 release_metapage(mp);
1582 /* free the AG lock and return.
1584 AG_UNLOCK(imap, agno);
1585 return (rc);
1589 /* check if we may allocate an extent of free
1590 * inodes and whether this word of the free
1591 * extents summary map describes a free extent.
1593 if (addext && ~extsmap) {
1594 /* a free extent has been found. determine
1595 * the extent number.
1597 rem = diFindFree(extsmap, 0);
1598 extno = (sword << L2EXTSPERSUM) + rem;
1600 /* allocate an extent of free inodes.
1602 if ((rc = diNewExt(imap, iagp, extno))) {
1603 /* if there is no disk space for a
1604 * new extent, try to allocate the
1605 * disk inode from somewhere else.
1607 if (rc == -ENOSPC)
1608 break;
1610 assert(rc == -EIO);
1611 } else {
1612 /* set the results of the allocation
1613 * and write the iag.
1615 diInitInode(ip, iagno,
1616 extno << L2INOSPEREXT,
1617 extno, iagp);
1618 mark_metapage_dirty(mp);
1620 release_metapage(mp);
1621 /* free the imap inode & the AG lock & return.
1623 IREAD_UNLOCK(ipimap);
1624 AG_UNLOCK(imap, agno);
1625 return (rc);
1628 /* move on to the next set of summary map words.
1630 sword = (sword == SMAPSZ - 1) ? 0 : sword + 1;
1631 inosmap = le32_to_cpu(iagp->inosmap[sword]);
1632 extsmap = le32_to_cpu(iagp->extsmap[sword]);
1635 /* unlock imap inode */
1636 IREAD_UNLOCK(ipimap);
1638 /* nothing doing in this iag, so release it. */
1639 release_metapage(mp);
1641 tryag:
1643 * try to allocate anywhere within the same AG as the parent inode.
1645 rc = diAllocAG(imap, agno, dir, ip);
1647 AG_UNLOCK(imap, agno);
1649 if (rc != -ENOSPC)
1650 return (rc);
1653 * try to allocate in any AG.
1655 return (diAllocAny(imap, agno, dir, ip));
1660 * NAME: diAllocAG(imap,agno,dir,ip)
1662 * FUNCTION: allocate a disk inode from the allocation group.
1664 * this routine first determines if a new extent of free
1665 * inodes should be added for the allocation group, with
1666 * the current request satisfied from this extent. if this
1667 * is the case, an attempt will be made to do just that. if
1668 * this attempt fails or it has been determined that a new
1669 * extent should not be added, an attempt is made to satisfy
1670 * the request by allocating an existing (backed) free inode
1671 * from the allocation group.
1673 * PRE CONDITION: Already have the AG lock for this AG.
1675 * PARAMETERS:
1676 * imap - pointer to inode map control structure.
1677 * agno - allocation group to allocate from.
1678 * dir - TRUE if the new disk inode is for a directory.
1679 * ip - pointer to the new inode to be filled in on successful return
1680 * with the disk inode number allocated, its extent address
1681 * and the start of the ag.
1683 * RETURN VALUES:
1684 * 0 - success.
1685 * -ENOSPC - insufficient disk resources.
1686 * -EIO - i/o error.
1688 static int
1689 diAllocAG(struct inomap * imap, int agno, boolean_t dir, struct inode *ip)
1691 int rc, addext, numfree, numinos;
1693 /* get the number of free and the number of backed disk
1694 * inodes currently within the ag.
1696 numfree = imap->im_agctl[agno].numfree;
1697 numinos = imap->im_agctl[agno].numinos;
1699 if (numfree > numinos) {
1700 jfs_error(ip->i_sb, "diAllocAG: numfree > numinos");
1701 return -EIO;
1704 /* determine if we should allocate a new extent of free inodes
1705 * within the ag: for directory inodes, add a new extent
1706 * if there are a small number of free inodes or number of free
1707 * inodes is a small percentage of the number of backed inodes.
1709 if (dir == TRUE)
1710 addext = (numfree < 64 ||
1711 (numfree < 256
1712 && ((numfree * 100) / numinos) <= 20));
1713 else
1714 addext = (numfree == 0);
1717 * try to allocate a new extent of free inodes.
1719 if (addext) {
1720 /* if free space is not avaliable for this new extent, try
1721 * below to allocate a free and existing (already backed)
1722 * inode from the ag.
1724 if ((rc = diAllocExt(imap, agno, ip)) != -ENOSPC)
1725 return (rc);
1729 * try to allocate an existing free inode from the ag.
1731 return (diAllocIno(imap, agno, ip));
1736 * NAME: diAllocAny(imap,agno,dir,iap)
1738 * FUNCTION: allocate a disk inode from any other allocation group.
1740 * this routine is called when an allocation attempt within
1741 * the primary allocation group has failed. if attempts to
1742 * allocate an inode from any allocation group other than the
1743 * specified primary group.
1745 * PARAMETERS:
1746 * imap - pointer to inode map control structure.
1747 * agno - primary allocation group (to avoid).
1748 * dir - TRUE if the new disk inode is for a directory.
1749 * ip - pointer to a new inode to be filled in on successful return
1750 * with the disk inode number allocated, its extent address
1751 * and the start of the ag.
1753 * RETURN VALUES:
1754 * 0 - success.
1755 * -ENOSPC - insufficient disk resources.
1756 * -EIO - i/o error.
1758 static int
1759 diAllocAny(struct inomap * imap, int agno, boolean_t dir, struct inode *ip)
1761 int ag, rc;
1762 int maxag = JFS_SBI(imap->im_ipimap->i_sb)->bmap->db_maxag;
1765 /* try to allocate from the ags following agno up to
1766 * the maximum ag number.
1768 for (ag = agno + 1; ag <= maxag; ag++) {
1769 AG_LOCK(imap, ag);
1771 rc = diAllocAG(imap, ag, dir, ip);
1773 AG_UNLOCK(imap, ag);
1775 if (rc != -ENOSPC)
1776 return (rc);
1779 /* try to allocate from the ags in front of agno.
1781 for (ag = 0; ag < agno; ag++) {
1782 AG_LOCK(imap, ag);
1784 rc = diAllocAG(imap, ag, dir, ip);
1786 AG_UNLOCK(imap, ag);
1788 if (rc != -ENOSPC)
1789 return (rc);
1792 /* no free disk inodes.
1794 return -ENOSPC;
1799 * NAME: diAllocIno(imap,agno,ip)
1801 * FUNCTION: allocate a disk inode from the allocation group's free
1802 * inode list, returning an error if this free list is
1803 * empty (i.e. no iags on the list).
1805 * allocation occurs from the first iag on the list using
1806 * the iag's free inode summary map to find the leftmost
1807 * free inode in the iag.
1809 * PRE CONDITION: Already have AG lock for this AG.
1811 * PARAMETERS:
1812 * imap - pointer to inode map control structure.
1813 * agno - allocation group.
1814 * ip - pointer to new inode to be filled in on successful return
1815 * with the disk inode number allocated, its extent address
1816 * and the start of the ag.
1818 * RETURN VALUES:
1819 * 0 - success.
1820 * -ENOSPC - insufficient disk resources.
1821 * -EIO - i/o error.
1823 static int diAllocIno(struct inomap * imap, int agno, struct inode *ip)
1825 int iagno, ino, rc, rem, extno, sword;
1826 struct metapage *mp;
1827 struct iag *iagp;
1829 /* check if there are iags on the ag's free inode list.
1831 if ((iagno = imap->im_agctl[agno].inofree) < 0)
1832 return -ENOSPC;
1834 /* obtain read lock on imap inode */
1835 IREAD_LOCK(imap->im_ipimap);
1837 /* read the iag at the head of the list.
1839 if ((rc = diIAGRead(imap, iagno, &mp))) {
1840 IREAD_UNLOCK(imap->im_ipimap);
1841 return (rc);
1843 iagp = (struct iag *) mp->data;
1845 /* better be free inodes in this iag if it is on the
1846 * list.
1848 if (!iagp->nfreeinos) {
1849 IREAD_UNLOCK(imap->im_ipimap);
1850 release_metapage(mp);
1851 jfs_error(ip->i_sb,
1852 "diAllocIno: nfreeinos = 0, but iag on freelist");
1853 return -EIO;
1856 /* scan the free inode summary map to find an extent
1857 * with free inodes.
1859 for (sword = 0;; sword++) {
1860 if (sword >= SMAPSZ) {
1861 IREAD_UNLOCK(imap->im_ipimap);
1862 release_metapage(mp);
1863 jfs_error(ip->i_sb,
1864 "diAllocIno: free inode not found in summary map");
1865 return -EIO;
1868 if (~iagp->inosmap[sword])
1869 break;
1872 /* found a extent with free inodes. determine
1873 * the extent number.
1875 rem = diFindFree(le32_to_cpu(iagp->inosmap[sword]), 0);
1876 if (rem >= EXTSPERSUM) {
1877 IREAD_UNLOCK(imap->im_ipimap);
1878 release_metapage(mp);
1879 jfs_error(ip->i_sb, "diAllocIno: no free extent found");
1880 return -EIO;
1882 extno = (sword << L2EXTSPERSUM) + rem;
1884 /* find the first free inode in the extent.
1886 rem = diFindFree(le32_to_cpu(iagp->wmap[extno]), 0);
1887 if (rem >= INOSPEREXT) {
1888 IREAD_UNLOCK(imap->im_ipimap);
1889 release_metapage(mp);
1890 jfs_error(ip->i_sb, "diAllocIno: free inode not found");
1891 return -EIO;
1894 /* compute the inode number within the iag.
1896 ino = (extno << L2INOSPEREXT) + rem;
1898 /* allocate the inode.
1900 rc = diAllocBit(imap, iagp, ino);
1901 IREAD_UNLOCK(imap->im_ipimap);
1902 if (rc) {
1903 release_metapage(mp);
1904 return (rc);
1907 /* set the results of the allocation and write the iag.
1909 diInitInode(ip, iagno, ino, extno, iagp);
1910 write_metapage(mp);
1912 return (0);
1917 * NAME: diAllocExt(imap,agno,ip)
1919 * FUNCTION: add a new extent of free inodes to an iag, allocating
1920 * an inode from this extent to satisfy the current allocation
1921 * request.
1923 * this routine first tries to find an existing iag with free
1924 * extents through the ag free extent list. if list is not
1925 * empty, the head of the list will be selected as the home
1926 * of the new extent of free inodes. otherwise (the list is
1927 * empty), a new iag will be allocated for the ag to contain
1928 * the extent.
1930 * once an iag has been selected, the free extent summary map
1931 * is used to locate a free extent within the iag and diNewExt()
1932 * is called to initialize the extent, with initialization
1933 * including the allocation of the first inode of the extent
1934 * for the purpose of satisfying this request.
1936 * PARAMETERS:
1937 * imap - pointer to inode map control structure.
1938 * agno - allocation group number.
1939 * ip - pointer to new inode to be filled in on successful return
1940 * with the disk inode number allocated, its extent address
1941 * and the start of the ag.
1943 * RETURN VALUES:
1944 * 0 - success.
1945 * -ENOSPC - insufficient disk resources.
1946 * -EIO - i/o error.
1948 static int diAllocExt(struct inomap * imap, int agno, struct inode *ip)
1950 int rem, iagno, sword, extno, rc;
1951 struct metapage *mp;
1952 struct iag *iagp;
1954 /* check if the ag has any iags with free extents. if not,
1955 * allocate a new iag for the ag.
1957 if ((iagno = imap->im_agctl[agno].extfree) < 0) {
1958 /* If successful, diNewIAG will obtain the read lock on the
1959 * imap inode.
1961 if ((rc = diNewIAG(imap, &iagno, agno, &mp))) {
1962 return (rc);
1964 iagp = (struct iag *) mp->data;
1966 /* set the ag number if this a brand new iag
1968 iagp->agstart =
1969 cpu_to_le64(AGTOBLK(agno, imap->im_ipimap));
1970 } else {
1971 /* read the iag.
1973 IREAD_LOCK(imap->im_ipimap);
1974 if ((rc = diIAGRead(imap, iagno, &mp))) {
1975 IREAD_UNLOCK(imap->im_ipimap);
1976 jfs_error(ip->i_sb, "diAllocExt: error reading iag");
1977 return rc;
1979 iagp = (struct iag *) mp->data;
1982 /* using the free extent summary map, find a free extent.
1984 for (sword = 0;; sword++) {
1985 if (sword >= SMAPSZ) {
1986 release_metapage(mp);
1987 IREAD_UNLOCK(imap->im_ipimap);
1988 jfs_error(ip->i_sb,
1989 "diAllocExt: free ext summary map not found");
1990 return -EIO;
1992 if (~iagp->extsmap[sword])
1993 break;
1996 /* determine the extent number of the free extent.
1998 rem = diFindFree(le32_to_cpu(iagp->extsmap[sword]), 0);
1999 if (rem >= EXTSPERSUM) {
2000 release_metapage(mp);
2001 IREAD_UNLOCK(imap->im_ipimap);
2002 jfs_error(ip->i_sb, "diAllocExt: free extent not found");
2003 return -EIO;
2005 extno = (sword << L2EXTSPERSUM) + rem;
2007 /* initialize the new extent.
2009 rc = diNewExt(imap, iagp, extno);
2010 IREAD_UNLOCK(imap->im_ipimap);
2011 if (rc) {
2012 /* something bad happened. if a new iag was allocated,
2013 * place it back on the inode map's iag free list, and
2014 * clear the ag number information.
2016 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2017 IAGFREE_LOCK(imap);
2018 iagp->iagfree = cpu_to_le32(imap->im_freeiag);
2019 imap->im_freeiag = iagno;
2020 IAGFREE_UNLOCK(imap);
2022 write_metapage(mp);
2023 return (rc);
2026 /* set the results of the allocation and write the iag.
2028 diInitInode(ip, iagno, extno << L2INOSPEREXT, extno, iagp);
2030 write_metapage(mp);
2032 return (0);
2037 * NAME: diAllocBit(imap,iagp,ino)
2039 * FUNCTION: allocate a backed inode from an iag.
2041 * this routine performs the mechanics of allocating a
2042 * specified inode from a backed extent.
2044 * if the inode to be allocated represents the last free
2045 * inode within the iag, the iag will be removed from the
2046 * ag free inode list.
2048 * a careful update approach is used to provide consistency
2049 * in the face of updates to multiple buffers. under this
2050 * approach, all required buffers are obtained before making
2051 * any updates and are held all are updates are complete.
2053 * PRE CONDITION: Already have buffer lock on iagp. Already have AG lock on
2054 * this AG. Must have read lock on imap inode.
2056 * PARAMETERS:
2057 * imap - pointer to inode map control structure.
2058 * iagp - pointer to iag.
2059 * ino - inode number to be allocated within the iag.
2061 * RETURN VALUES:
2062 * 0 - success.
2063 * -ENOSPC - insufficient disk resources.
2064 * -EIO - i/o error.
2066 static int diAllocBit(struct inomap * imap, struct iag * iagp, int ino)
2068 int extno, bitno, agno, sword, rc;
2069 struct metapage *amp = NULL, *bmp = NULL;
2070 struct iag *aiagp = NULL, *biagp = NULL;
2071 u32 mask;
2073 /* check if this is the last free inode within the iag.
2074 * if so, it will have to be removed from the ag free
2075 * inode list, so get the iags preceeding and following
2076 * it on the list.
2078 if (iagp->nfreeinos == cpu_to_le32(1)) {
2079 if ((int) le32_to_cpu(iagp->inofreefwd) >= 0) {
2080 if ((rc =
2081 diIAGRead(imap, le32_to_cpu(iagp->inofreefwd),
2082 &amp)))
2083 return (rc);
2084 aiagp = (struct iag *) amp->data;
2087 if ((int) le32_to_cpu(iagp->inofreeback) >= 0) {
2088 if ((rc =
2089 diIAGRead(imap,
2090 le32_to_cpu(iagp->inofreeback),
2091 &bmp))) {
2092 if (amp)
2093 release_metapage(amp);
2094 return (rc);
2096 biagp = (struct iag *) bmp->data;
2100 /* get the ag number, extent number, inode number within
2101 * the extent.
2103 agno = BLKTOAG(le64_to_cpu(iagp->agstart), JFS_SBI(imap->im_ipimap->i_sb));
2104 extno = ino >> L2INOSPEREXT;
2105 bitno = ino & (INOSPEREXT - 1);
2107 /* compute the mask for setting the map.
2109 mask = HIGHORDER >> bitno;
2111 /* the inode should be free and backed.
2113 if (((le32_to_cpu(iagp->pmap[extno]) & mask) != 0) ||
2114 ((le32_to_cpu(iagp->wmap[extno]) & mask) != 0) ||
2115 (addressPXD(&iagp->inoext[extno]) == 0)) {
2116 if (amp)
2117 release_metapage(amp);
2118 if (bmp)
2119 release_metapage(bmp);
2121 jfs_error(imap->im_ipimap->i_sb,
2122 "diAllocBit: iag inconsistent");
2123 return -EIO;
2126 /* mark the inode as allocated in the working map.
2128 iagp->wmap[extno] |= cpu_to_le32(mask);
2130 /* check if all inodes within the extent are now
2131 * allocated. if so, update the free inode summary
2132 * map to reflect this.
2134 if (iagp->wmap[extno] == cpu_to_le32(ONES)) {
2135 sword = extno >> L2EXTSPERSUM;
2136 bitno = extno & (EXTSPERSUM - 1);
2137 iagp->inosmap[sword] |= cpu_to_le32(HIGHORDER >> bitno);
2140 /* if this was the last free inode in the iag, remove the
2141 * iag from the ag free inode list.
2143 if (iagp->nfreeinos == cpu_to_le32(1)) {
2144 if (amp) {
2145 aiagp->inofreeback = iagp->inofreeback;
2146 write_metapage(amp);
2149 if (bmp) {
2150 biagp->inofreefwd = iagp->inofreefwd;
2151 write_metapage(bmp);
2152 } else {
2153 imap->im_agctl[agno].inofree =
2154 le32_to_cpu(iagp->inofreefwd);
2156 iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
2159 /* update the free inode count at the iag, ag, inode
2160 * map levels.
2162 iagp->nfreeinos = cpu_to_le32(le32_to_cpu(iagp->nfreeinos) - 1);
2163 imap->im_agctl[agno].numfree -= 1;
2164 atomic_dec(&imap->im_numfree);
2166 return (0);
2171 * NAME: diNewExt(imap,iagp,extno)
2173 * FUNCTION: initialize a new extent of inodes for an iag, allocating
2174 * the first inode of the extent for use for the current
2175 * allocation request.
2177 * disk resources are allocated for the new extent of inodes
2178 * and the inodes themselves are initialized to reflect their
2179 * existence within the extent (i.e. their inode numbers and
2180 * inode extent addresses are set) and their initial state
2181 * (mode and link count are set to zero).
2183 * if the iag is new, it is not yet on an ag extent free list
2184 * but will now be placed on this list.
2186 * if the allocation of the new extent causes the iag to
2187 * have no free extent, the iag will be removed from the
2188 * ag extent free list.
2190 * if the iag has no free backed inodes, it will be placed
2191 * on the ag free inode list, since the addition of the new
2192 * extent will now cause it to have free inodes.
2194 * a careful update approach is used to provide consistency
2195 * (i.e. list consistency) in the face of updates to multiple
2196 * buffers. under this approach, all required buffers are
2197 * obtained before making any updates and are held until all
2198 * updates are complete.
2200 * PRE CONDITION: Already have buffer lock on iagp. Already have AG lock on
2201 * this AG. Must have read lock on imap inode.
2203 * PARAMETERS:
2204 * imap - pointer to inode map control structure.
2205 * iagp - pointer to iag.
2206 * extno - extent number.
2208 * RETURN VALUES:
2209 * 0 - success.
2210 * -ENOSPC - insufficient disk resources.
2211 * -EIO - i/o error.
2213 static int diNewExt(struct inomap * imap, struct iag * iagp, int extno)
2215 int agno, iagno, fwd, back, freei = 0, sword, rc;
2216 struct iag *aiagp = NULL, *biagp = NULL, *ciagp = NULL;
2217 struct metapage *amp, *bmp, *cmp, *dmp;
2218 struct inode *ipimap;
2219 s64 blkno, hint;
2220 int i, j;
2221 u32 mask;
2222 ino_t ino;
2223 struct dinode *dp;
2224 struct jfs_sb_info *sbi;
2226 /* better have free extents.
2228 if (!iagp->nfreeexts) {
2229 jfs_error(imap->im_ipimap->i_sb, "diNewExt: no free extents");
2230 return -EIO;
2233 /* get the inode map inode.
2235 ipimap = imap->im_ipimap;
2236 sbi = JFS_SBI(ipimap->i_sb);
2238 amp = bmp = cmp = NULL;
2240 /* get the ag and iag numbers for this iag.
2242 agno = BLKTOAG(le64_to_cpu(iagp->agstart), sbi);
2243 iagno = le32_to_cpu(iagp->iagnum);
2245 /* check if this is the last free extent within the
2246 * iag. if so, the iag must be removed from the ag
2247 * free extent list, so get the iags preceeding and
2248 * following the iag on this list.
2250 if (iagp->nfreeexts == cpu_to_le32(1)) {
2251 if ((fwd = le32_to_cpu(iagp->extfreefwd)) >= 0) {
2252 if ((rc = diIAGRead(imap, fwd, &amp)))
2253 return (rc);
2254 aiagp = (struct iag *) amp->data;
2257 if ((back = le32_to_cpu(iagp->extfreeback)) >= 0) {
2258 if ((rc = diIAGRead(imap, back, &bmp)))
2259 goto error_out;
2260 biagp = (struct iag *) bmp->data;
2262 } else {
2263 /* the iag has free extents. if all extents are free
2264 * (as is the case for a newly allocated iag), the iag
2265 * must be added to the ag free extent list, so get
2266 * the iag at the head of the list in preparation for
2267 * adding this iag to this list.
2269 fwd = back = -1;
2270 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2271 if ((fwd = imap->im_agctl[agno].extfree) >= 0) {
2272 if ((rc = diIAGRead(imap, fwd, &amp)))
2273 goto error_out;
2274 aiagp = (struct iag *) amp->data;
2279 /* check if the iag has no free inodes. if so, the iag
2280 * will have to be added to the ag free inode list, so get
2281 * the iag at the head of the list in preparation for
2282 * adding this iag to this list. in doing this, we must
2283 * check if we already have the iag at the head of
2284 * the list in hand.
2286 if (iagp->nfreeinos == 0) {
2287 freei = imap->im_agctl[agno].inofree;
2289 if (freei >= 0) {
2290 if (freei == fwd) {
2291 ciagp = aiagp;
2292 } else if (freei == back) {
2293 ciagp = biagp;
2294 } else {
2295 if ((rc = diIAGRead(imap, freei, &cmp)))
2296 goto error_out;
2297 ciagp = (struct iag *) cmp->data;
2299 if (ciagp == NULL) {
2300 jfs_error(imap->im_ipimap->i_sb,
2301 "diNewExt: ciagp == NULL");
2302 rc = -EIO;
2303 goto error_out;
2308 /* allocate disk space for the inode extent.
2310 if ((extno == 0) || (addressPXD(&iagp->inoext[extno - 1]) == 0))
2311 hint = ((s64) agno << sbi->bmap->db_agl2size) - 1;
2312 else
2313 hint = addressPXD(&iagp->inoext[extno - 1]) +
2314 lengthPXD(&iagp->inoext[extno - 1]) - 1;
2316 if ((rc = dbAlloc(ipimap, hint, (s64) imap->im_nbperiext, &blkno)))
2317 goto error_out;
2319 /* compute the inode number of the first inode within the
2320 * extent.
2322 ino = (iagno << L2INOSPERIAG) + (extno << L2INOSPEREXT);
2324 /* initialize the inodes within the newly allocated extent a
2325 * page at a time.
2327 for (i = 0; i < imap->im_nbperiext; i += sbi->nbperpage) {
2328 /* get a buffer for this page of disk inodes.
2330 dmp = get_metapage(ipimap, blkno + i, PSIZE, 1);
2331 if (dmp == NULL) {
2332 rc = -EIO;
2333 goto error_out;
2335 dp = (struct dinode *) dmp->data;
2337 /* initialize the inode number, mode, link count and
2338 * inode extent address.
2340 for (j = 0; j < INOSPERPAGE; j++, dp++, ino++) {
2341 dp->di_inostamp = cpu_to_le32(sbi->inostamp);
2342 dp->di_number = cpu_to_le32(ino);
2343 dp->di_fileset = cpu_to_le32(FILESYSTEM_I);
2344 dp->di_mode = 0;
2345 dp->di_nlink = 0;
2346 PXDaddress(&(dp->di_ixpxd), blkno);
2347 PXDlength(&(dp->di_ixpxd), imap->im_nbperiext);
2349 write_metapage(dmp);
2352 /* if this is the last free extent within the iag, remove the
2353 * iag from the ag free extent list.
2355 if (iagp->nfreeexts == cpu_to_le32(1)) {
2356 if (fwd >= 0)
2357 aiagp->extfreeback = iagp->extfreeback;
2359 if (back >= 0)
2360 biagp->extfreefwd = iagp->extfreefwd;
2361 else
2362 imap->im_agctl[agno].extfree =
2363 le32_to_cpu(iagp->extfreefwd);
2365 iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
2366 } else {
2367 /* if the iag has all free extents (newly allocated iag),
2368 * add the iag to the ag free extent list.
2370 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2371 if (fwd >= 0)
2372 aiagp->extfreeback = cpu_to_le32(iagno);
2374 iagp->extfreefwd = cpu_to_le32(fwd);
2375 iagp->extfreeback = cpu_to_le32(-1);
2376 imap->im_agctl[agno].extfree = iagno;
2380 /* if the iag has no free inodes, add the iag to the
2381 * ag free inode list.
2383 if (iagp->nfreeinos == 0) {
2384 if (freei >= 0)
2385 ciagp->inofreeback = cpu_to_le32(iagno);
2387 iagp->inofreefwd =
2388 cpu_to_le32(imap->im_agctl[agno].inofree);
2389 iagp->inofreeback = cpu_to_le32(-1);
2390 imap->im_agctl[agno].inofree = iagno;
2393 /* initialize the extent descriptor of the extent. */
2394 PXDlength(&iagp->inoext[extno], imap->im_nbperiext);
2395 PXDaddress(&iagp->inoext[extno], blkno);
2397 /* initialize the working and persistent map of the extent.
2398 * the working map will be initialized such that
2399 * it indicates the first inode of the extent is allocated.
2401 iagp->wmap[extno] = cpu_to_le32(HIGHORDER);
2402 iagp->pmap[extno] = 0;
2404 /* update the free inode and free extent summary maps
2405 * for the extent to indicate the extent has free inodes
2406 * and no longer represents a free extent.
2408 sword = extno >> L2EXTSPERSUM;
2409 mask = HIGHORDER >> (extno & (EXTSPERSUM - 1));
2410 iagp->extsmap[sword] |= cpu_to_le32(mask);
2411 iagp->inosmap[sword] &= cpu_to_le32(~mask);
2413 /* update the free inode and free extent counts for the
2414 * iag.
2416 iagp->nfreeinos = cpu_to_le32(le32_to_cpu(iagp->nfreeinos) +
2417 (INOSPEREXT - 1));
2418 iagp->nfreeexts = cpu_to_le32(le32_to_cpu(iagp->nfreeexts) - 1);
2420 /* update the free and backed inode counts for the ag.
2422 imap->im_agctl[agno].numfree += (INOSPEREXT - 1);
2423 imap->im_agctl[agno].numinos += INOSPEREXT;
2425 /* update the free and backed inode counts for the inode map.
2427 atomic_add(INOSPEREXT - 1, &imap->im_numfree);
2428 atomic_add(INOSPEREXT, &imap->im_numinos);
2430 /* write the iags.
2432 if (amp)
2433 write_metapage(amp);
2434 if (bmp)
2435 write_metapage(bmp);
2436 if (cmp)
2437 write_metapage(cmp);
2439 return (0);
2441 error_out:
2443 /* release the iags.
2445 if (amp)
2446 release_metapage(amp);
2447 if (bmp)
2448 release_metapage(bmp);
2449 if (cmp)
2450 release_metapage(cmp);
2452 return (rc);
2457 * NAME: diNewIAG(imap,iagnop,agno)
2459 * FUNCTION: allocate a new iag for an allocation group.
2461 * first tries to allocate the iag from the inode map
2462 * iagfree list:
2463 * if the list has free iags, the head of the list is removed
2464 * and returned to satisfy the request.
2465 * if the inode map's iag free list is empty, the inode map
2466 * is extended to hold a new iag. this new iag is initialized
2467 * and returned to satisfy the request.
2469 * PARAMETERS:
2470 * imap - pointer to inode map control structure.
2471 * iagnop - pointer to an iag number set with the number of the
2472 * newly allocated iag upon successful return.
2473 * agno - allocation group number.
2474 * bpp - Buffer pointer to be filled in with new IAG's buffer
2476 * RETURN VALUES:
2477 * 0 - success.
2478 * -ENOSPC - insufficient disk resources.
2479 * -EIO - i/o error.
2481 * serialization:
2482 * AG lock held on entry/exit;
2483 * write lock on the map is held inside;
2484 * read lock on the map is held on successful completion;
2486 * note: new iag transaction:
2487 * . synchronously write iag;
2488 * . write log of xtree and inode of imap;
2489 * . commit;
2490 * . synchronous write of xtree (right to left, bottom to top);
2491 * . at start of logredo(): init in-memory imap with one additional iag page;
2492 * . at end of logredo(): re-read imap inode to determine
2493 * new imap size;
2495 static int
2496 diNewIAG(struct inomap * imap, int *iagnop, int agno, struct metapage ** mpp)
2498 int rc;
2499 int iagno, i, xlen;
2500 struct inode *ipimap;
2501 struct super_block *sb;
2502 struct jfs_sb_info *sbi;
2503 struct metapage *mp;
2504 struct iag *iagp;
2505 s64 xaddr = 0;
2506 s64 blkno;
2507 tid_t tid;
2508 #ifdef _STILL_TO_PORT
2509 xad_t xad;
2510 #endif /* _STILL_TO_PORT */
2511 struct inode *iplist[1];
2513 /* pick up pointers to the inode map and mount inodes */
2514 ipimap = imap->im_ipimap;
2515 sb = ipimap->i_sb;
2516 sbi = JFS_SBI(sb);
2518 /* acquire the free iag lock */
2519 IAGFREE_LOCK(imap);
2521 /* if there are any iags on the inode map free iag list,
2522 * allocate the iag from the head of the list.
2524 if (imap->im_freeiag >= 0) {
2525 /* pick up the iag number at the head of the list */
2526 iagno = imap->im_freeiag;
2528 /* determine the logical block number of the iag */
2529 blkno = IAGTOLBLK(iagno, sbi->l2nbperpage);
2530 } else {
2531 /* no free iags. the inode map will have to be extented
2532 * to include a new iag.
2535 /* acquire inode map lock */
2536 IWRITE_LOCK(ipimap);
2538 if (ipimap->i_size >> L2PSIZE != imap->im_nextiag + 1) {
2539 IWRITE_UNLOCK(ipimap);
2540 IAGFREE_UNLOCK(imap);
2541 jfs_error(imap->im_ipimap->i_sb,
2542 "diNewIAG: ipimap->i_size is wrong");
2543 return -EIO;
2547 /* get the next avaliable iag number */
2548 iagno = imap->im_nextiag;
2550 /* make sure that we have not exceeded the maximum inode
2551 * number limit.
2553 if (iagno > (MAXIAGS - 1)) {
2554 /* release the inode map lock */
2555 IWRITE_UNLOCK(ipimap);
2557 rc = -ENOSPC;
2558 goto out;
2562 * synchronously append new iag page.
2564 /* determine the logical address of iag page to append */
2565 blkno = IAGTOLBLK(iagno, sbi->l2nbperpage);
2567 /* Allocate extent for new iag page */
2568 xlen = sbi->nbperpage;
2569 if ((rc = dbAlloc(ipimap, 0, (s64) xlen, &xaddr))) {
2570 /* release the inode map lock */
2571 IWRITE_UNLOCK(ipimap);
2573 goto out;
2577 * start transaction of update of the inode map
2578 * addressing structure pointing to the new iag page;
2580 tid = txBegin(sb, COMMIT_FORCE);
2581 down(&JFS_IP(ipimap)->commit_sem);
2583 /* update the inode map addressing structure to point to it */
2584 if ((rc =
2585 xtInsert(tid, ipimap, 0, blkno, xlen, &xaddr, 0))) {
2586 txEnd(tid);
2587 up(&JFS_IP(ipimap)->commit_sem);
2588 /* Free the blocks allocated for the iag since it was
2589 * not successfully added to the inode map
2591 dbFree(ipimap, xaddr, (s64) xlen);
2593 /* release the inode map lock */
2594 IWRITE_UNLOCK(ipimap);
2596 goto out;
2599 /* update the inode map's inode to reflect the extension */
2600 ipimap->i_size += PSIZE;
2601 inode_add_bytes(ipimap, PSIZE);
2603 /* assign a buffer for the page */
2604 mp = get_metapage(ipimap, blkno, PSIZE, 0);
2605 if (!mp) {
2607 * This is very unlikely since we just created the
2608 * extent, but let's try to handle it correctly
2610 xtTruncate(tid, ipimap, ipimap->i_size - PSIZE,
2611 COMMIT_PWMAP);
2613 txAbort(tid, 0);
2614 txEnd(tid);
2616 /* release the inode map lock */
2617 IWRITE_UNLOCK(ipimap);
2619 rc = -EIO;
2620 goto out;
2622 iagp = (struct iag *) mp->data;
2624 /* init the iag */
2625 memset(iagp, 0, sizeof(struct iag));
2626 iagp->iagnum = cpu_to_le32(iagno);
2627 iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
2628 iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
2629 iagp->iagfree = cpu_to_le32(-1);
2630 iagp->nfreeinos = 0;
2631 iagp->nfreeexts = cpu_to_le32(EXTSPERIAG);
2633 /* initialize the free inode summary map (free extent
2634 * summary map initialization handled by bzero).
2636 for (i = 0; i < SMAPSZ; i++)
2637 iagp->inosmap[i] = cpu_to_le32(ONES);
2640 * Write and sync the metapage
2642 flush_metapage(mp);
2645 * txCommit(COMMIT_FORCE) will synchronously write address
2646 * index pages and inode after commit in careful update order
2647 * of address index pages (right to left, bottom up);
2649 iplist[0] = ipimap;
2650 rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
2652 txEnd(tid);
2653 up(&JFS_IP(ipimap)->commit_sem);
2655 duplicateIXtree(sb, blkno, xlen, &xaddr);
2657 /* update the next avaliable iag number */
2658 imap->im_nextiag += 1;
2660 /* Add the iag to the iag free list so we don't lose the iag
2661 * if a failure happens now.
2663 imap->im_freeiag = iagno;
2665 /* Until we have logredo working, we want the imap inode &
2666 * control page to be up to date.
2668 diSync(ipimap);
2670 /* release the inode map lock */
2671 IWRITE_UNLOCK(ipimap);
2674 /* obtain read lock on map */
2675 IREAD_LOCK(ipimap);
2677 /* read the iag */
2678 if ((rc = diIAGRead(imap, iagno, &mp))) {
2679 IREAD_UNLOCK(ipimap);
2680 rc = -EIO;
2681 goto out;
2683 iagp = (struct iag *) mp->data;
2685 /* remove the iag from the iag free list */
2686 imap->im_freeiag = le32_to_cpu(iagp->iagfree);
2687 iagp->iagfree = cpu_to_le32(-1);
2689 /* set the return iag number and buffer pointer */
2690 *iagnop = iagno;
2691 *mpp = mp;
2693 out:
2694 /* release the iag free lock */
2695 IAGFREE_UNLOCK(imap);
2697 return (rc);
2701 * NAME: diIAGRead()
2703 * FUNCTION: get the buffer for the specified iag within a fileset
2704 * or aggregate inode map.
2706 * PARAMETERS:
2707 * imap - pointer to inode map control structure.
2708 * iagno - iag number.
2709 * bpp - point to buffer pointer to be filled in on successful
2710 * exit.
2712 * SERIALIZATION:
2713 * must have read lock on imap inode
2714 * (When called by diExtendFS, the filesystem is quiesced, therefore
2715 * the read lock is unnecessary.)
2717 * RETURN VALUES:
2718 * 0 - success.
2719 * -EIO - i/o error.
2721 static int diIAGRead(struct inomap * imap, int iagno, struct metapage ** mpp)
2723 struct inode *ipimap = imap->im_ipimap;
2724 s64 blkno;
2726 /* compute the logical block number of the iag. */
2727 blkno = IAGTOLBLK(iagno, JFS_SBI(ipimap->i_sb)->l2nbperpage);
2729 /* read the iag. */
2730 *mpp = read_metapage(ipimap, blkno, PSIZE, 0);
2731 if (*mpp == NULL) {
2732 return -EIO;
2735 return (0);
2739 * NAME: diFindFree()
2741 * FUNCTION: find the first free bit in a word starting at
2742 * the specified bit position.
2744 * PARAMETERS:
2745 * word - word to be examined.
2746 * start - starting bit position.
2748 * RETURN VALUES:
2749 * bit position of first free bit in the word or 32 if
2750 * no free bits were found.
2752 static int diFindFree(u32 word, int start)
2754 int bitno;
2755 assert(start < 32);
2756 /* scan the word for the first free bit. */
2757 for (word <<= start, bitno = start; bitno < 32;
2758 bitno++, word <<= 1) {
2759 if ((word & HIGHORDER) == 0)
2760 break;
2762 return (bitno);
2766 * NAME: diUpdatePMap()
2768 * FUNCTION: Update the persistent map in an IAG for the allocation or
2769 * freeing of the specified inode.
2771 * PRE CONDITIONS: Working map has already been updated for allocate.
2773 * PARAMETERS:
2774 * ipimap - Incore inode map inode
2775 * inum - Number of inode to mark in permanent map
2776 * is_free - If TRUE indicates inode should be marked freed, otherwise
2777 * indicates inode should be marked allocated.
2779 * RETURN VALUES:
2780 * 0 for success
2783 diUpdatePMap(struct inode *ipimap,
2784 unsigned long inum, boolean_t is_free, struct tblock * tblk)
2786 int rc;
2787 struct iag *iagp;
2788 struct metapage *mp;
2789 int iagno, ino, extno, bitno;
2790 struct inomap *imap;
2791 u32 mask;
2792 struct jfs_log *log;
2793 int lsn, difft, diffp;
2794 unsigned long flags;
2796 imap = JFS_IP(ipimap)->i_imap;
2797 /* get the iag number containing the inode */
2798 iagno = INOTOIAG(inum);
2799 /* make sure that the iag is contained within the map */
2800 if (iagno >= imap->im_nextiag) {
2801 jfs_error(ipimap->i_sb,
2802 "diUpdatePMap: the iag is outside the map");
2803 return -EIO;
2805 /* read the iag */
2806 IREAD_LOCK(ipimap);
2807 rc = diIAGRead(imap, iagno, &mp);
2808 IREAD_UNLOCK(ipimap);
2809 if (rc)
2810 return (rc);
2811 metapage_wait_for_io(mp);
2812 iagp = (struct iag *) mp->data;
2813 /* get the inode number and extent number of the inode within
2814 * the iag and the inode number within the extent.
2816 ino = inum & (INOSPERIAG - 1);
2817 extno = ino >> L2INOSPEREXT;
2818 bitno = ino & (INOSPEREXT - 1);
2819 mask = HIGHORDER >> bitno;
2821 * mark the inode free in persistent map:
2823 if (is_free == TRUE) {
2824 /* The inode should have been allocated both in working
2825 * map and in persistent map;
2826 * the inode will be freed from working map at the release
2827 * of last reference release;
2829 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
2830 jfs_error(ipimap->i_sb,
2831 "diUpdatePMap: inode %ld not marked as "
2832 "allocated in wmap!", inum);
2834 if (!(le32_to_cpu(iagp->pmap[extno]) & mask)) {
2835 jfs_error(ipimap->i_sb,
2836 "diUpdatePMap: inode %ld not marked as "
2837 "allocated in pmap!", inum);
2839 /* update the bitmap for the extent of the freed inode */
2840 iagp->pmap[extno] &= cpu_to_le32(~mask);
2843 * mark the inode allocated in persistent map:
2845 else {
2846 /* The inode should be already allocated in the working map
2847 * and should be free in persistent map;
2849 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
2850 release_metapage(mp);
2851 jfs_error(ipimap->i_sb,
2852 "diUpdatePMap: the inode is not allocated in "
2853 "the working map");
2854 return -EIO;
2856 if ((le32_to_cpu(iagp->pmap[extno]) & mask) != 0) {
2857 release_metapage(mp);
2858 jfs_error(ipimap->i_sb,
2859 "diUpdatePMap: the inode is not free in the "
2860 "persistent map");
2861 return -EIO;
2863 /* update the bitmap for the extent of the allocated inode */
2864 iagp->pmap[extno] |= cpu_to_le32(mask);
2867 * update iag lsn
2869 lsn = tblk->lsn;
2870 log = JFS_SBI(tblk->sb)->log;
2871 if (mp->lsn != 0) {
2872 /* inherit older/smaller lsn */
2873 logdiff(difft, lsn, log);
2874 logdiff(diffp, mp->lsn, log);
2875 LOGSYNC_LOCK(log, flags);
2876 if (difft < diffp) {
2877 mp->lsn = lsn;
2878 /* move mp after tblock in logsync list */
2879 list_move(&mp->synclist, &tblk->synclist);
2881 /* inherit younger/larger clsn */
2882 assert(mp->clsn);
2883 logdiff(difft, tblk->clsn, log);
2884 logdiff(diffp, mp->clsn, log);
2885 if (difft > diffp)
2886 mp->clsn = tblk->clsn;
2887 LOGSYNC_UNLOCK(log, flags);
2888 } else {
2889 mp->log = log;
2890 mp->lsn = lsn;
2891 /* insert mp after tblock in logsync list */
2892 LOGSYNC_LOCK(log, flags);
2893 log->count++;
2894 list_add(&mp->synclist, &tblk->synclist);
2895 mp->clsn = tblk->clsn;
2896 LOGSYNC_UNLOCK(log, flags);
2898 write_metapage(mp);
2899 return (0);
2903 * diExtendFS()
2905 * function: update imap for extendfs();
2907 * note: AG size has been increased s.t. each k old contiguous AGs are
2908 * coalesced into a new AG;
2910 int diExtendFS(struct inode *ipimap, struct inode *ipbmap)
2912 int rc, rcx = 0;
2913 struct inomap *imap = JFS_IP(ipimap)->i_imap;
2914 struct iag *iagp = NULL, *hiagp = NULL;
2915 struct bmap *mp = JFS_SBI(ipbmap->i_sb)->bmap;
2916 struct metapage *bp, *hbp;
2917 int i, n, head;
2918 int numinos, xnuminos = 0, xnumfree = 0;
2919 s64 agstart;
2921 jfs_info("diExtendFS: nextiag:%d numinos:%d numfree:%d",
2922 imap->im_nextiag, atomic_read(&imap->im_numinos),
2923 atomic_read(&imap->im_numfree));
2926 * reconstruct imap
2928 * coalesce contiguous k (newAGSize/oldAGSize) AGs;
2929 * i.e., (AGi, ..., AGj) where i = k*n and j = k*(n+1) - 1 to AGn;
2930 * note: new AG size = old AG size * (2**x).
2933 /* init per AG control information im_agctl[] */
2934 for (i = 0; i < MAXAG; i++) {
2935 imap->im_agctl[i].inofree = -1;
2936 imap->im_agctl[i].extfree = -1;
2937 imap->im_agctl[i].numinos = 0; /* number of backed inodes */
2938 imap->im_agctl[i].numfree = 0; /* number of free backed inodes */
2942 * process each iag page of the map.
2944 * rebuild AG Free Inode List, AG Free Inode Extent List;
2946 for (i = 0; i < imap->im_nextiag; i++) {
2947 if ((rc = diIAGRead(imap, i, &bp))) {
2948 rcx = rc;
2949 continue;
2951 iagp = (struct iag *) bp->data;
2952 if (le32_to_cpu(iagp->iagnum) != i) {
2953 release_metapage(bp);
2954 jfs_error(ipimap->i_sb,
2955 "diExtendFs: unexpected value of iagnum");
2956 return -EIO;
2959 /* leave free iag in the free iag list */
2960 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2961 release_metapage(bp);
2962 continue;
2965 /* agstart that computes to the same ag is treated as same; */
2966 agstart = le64_to_cpu(iagp->agstart);
2967 /* iagp->agstart = agstart & ~(mp->db_agsize - 1); */
2968 n = agstart >> mp->db_agl2size;
2970 /* compute backed inodes */
2971 numinos = (EXTSPERIAG - le32_to_cpu(iagp->nfreeexts))
2972 << L2INOSPEREXT;
2973 if (numinos > 0) {
2974 /* merge AG backed inodes */
2975 imap->im_agctl[n].numinos += numinos;
2976 xnuminos += numinos;
2979 /* if any backed free inodes, insert at AG free inode list */
2980 if ((int) le32_to_cpu(iagp->nfreeinos) > 0) {
2981 if ((head = imap->im_agctl[n].inofree) == -1) {
2982 iagp->inofreefwd = cpu_to_le32(-1);
2983 iagp->inofreeback = cpu_to_le32(-1);
2984 } else {
2985 if ((rc = diIAGRead(imap, head, &hbp))) {
2986 rcx = rc;
2987 goto nextiag;
2989 hiagp = (struct iag *) hbp->data;
2990 hiagp->inofreeback = iagp->iagnum;
2991 iagp->inofreefwd = cpu_to_le32(head);
2992 iagp->inofreeback = cpu_to_le32(-1);
2993 write_metapage(hbp);
2996 imap->im_agctl[n].inofree =
2997 le32_to_cpu(iagp->iagnum);
2999 /* merge AG backed free inodes */
3000 imap->im_agctl[n].numfree +=
3001 le32_to_cpu(iagp->nfreeinos);
3002 xnumfree += le32_to_cpu(iagp->nfreeinos);
3005 /* if any free extents, insert at AG free extent list */
3006 if (le32_to_cpu(iagp->nfreeexts) > 0) {
3007 if ((head = imap->im_agctl[n].extfree) == -1) {
3008 iagp->extfreefwd = cpu_to_le32(-1);
3009 iagp->extfreeback = cpu_to_le32(-1);
3010 } else {
3011 if ((rc = diIAGRead(imap, head, &hbp))) {
3012 rcx = rc;
3013 goto nextiag;
3015 hiagp = (struct iag *) hbp->data;
3016 hiagp->extfreeback = iagp->iagnum;
3017 iagp->extfreefwd = cpu_to_le32(head);
3018 iagp->extfreeback = cpu_to_le32(-1);
3019 write_metapage(hbp);
3022 imap->im_agctl[n].extfree =
3023 le32_to_cpu(iagp->iagnum);
3026 nextiag:
3027 write_metapage(bp);
3030 if (xnuminos != atomic_read(&imap->im_numinos) ||
3031 xnumfree != atomic_read(&imap->im_numfree)) {
3032 jfs_error(ipimap->i_sb,
3033 "diExtendFs: numinos or numfree incorrect");
3034 return -EIO;
3037 return rcx;
3042 * duplicateIXtree()
3044 * serialization: IWRITE_LOCK held on entry/exit
3046 * note: shadow page with regular inode (rel.2);
3048 static void duplicateIXtree(struct super_block *sb, s64 blkno,
3049 int xlen, s64 *xaddr)
3051 struct jfs_superblock *j_sb;
3052 struct buffer_head *bh;
3053 struct inode *ip;
3054 tid_t tid;
3056 /* if AIT2 ipmap2 is bad, do not try to update it */
3057 if (JFS_SBI(sb)->mntflag & JFS_BAD_SAIT) /* s_flag */
3058 return;
3059 ip = diReadSpecial(sb, FILESYSTEM_I, 1);
3060 if (ip == NULL) {
3061 JFS_SBI(sb)->mntflag |= JFS_BAD_SAIT;
3062 if (readSuper(sb, &bh))
3063 return;
3064 j_sb = (struct jfs_superblock *)bh->b_data;
3065 j_sb->s_flag |= cpu_to_le32(JFS_BAD_SAIT);
3067 mark_buffer_dirty(bh);
3068 sync_dirty_buffer(bh);
3069 brelse(bh);
3070 return;
3073 /* start transaction */
3074 tid = txBegin(sb, COMMIT_FORCE);
3075 /* update the inode map addressing structure to point to it */
3076 if (xtInsert(tid, ip, 0, blkno, xlen, xaddr, 0)) {
3077 JFS_SBI(sb)->mntflag |= JFS_BAD_SAIT;
3078 txAbort(tid, 1);
3079 goto cleanup;
3082 /* update the inode map's inode to reflect the extension */
3083 ip->i_size += PSIZE;
3084 inode_add_bytes(ip, PSIZE);
3085 txCommit(tid, 1, &ip, COMMIT_FORCE);
3086 cleanup:
3087 txEnd(tid);
3088 diFreeSpecial(ip);
3092 * NAME: copy_from_dinode()
3094 * FUNCTION: Copies inode info from disk inode to in-memory inode
3096 * RETURN VALUES:
3097 * 0 - success
3098 * -ENOMEM - insufficient memory
3100 static int copy_from_dinode(struct dinode * dip, struct inode *ip)
3102 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
3104 jfs_ip->fileset = le32_to_cpu(dip->di_fileset);
3105 jfs_ip->mode2 = le32_to_cpu(dip->di_mode);
3107 ip->i_mode = le32_to_cpu(dip->di_mode) & 0xffff;
3108 ip->i_nlink = le32_to_cpu(dip->di_nlink);
3109 ip->i_uid = le32_to_cpu(dip->di_uid);
3110 ip->i_gid = le32_to_cpu(dip->di_gid);
3111 ip->i_size = le64_to_cpu(dip->di_size);
3112 ip->i_atime.tv_sec = le32_to_cpu(dip->di_atime.tv_sec);
3113 ip->i_atime.tv_nsec = le32_to_cpu(dip->di_atime.tv_nsec);
3114 ip->i_mtime.tv_sec = le32_to_cpu(dip->di_mtime.tv_sec);
3115 ip->i_mtime.tv_nsec = le32_to_cpu(dip->di_mtime.tv_nsec);
3116 ip->i_ctime.tv_sec = le32_to_cpu(dip->di_ctime.tv_sec);
3117 ip->i_ctime.tv_nsec = le32_to_cpu(dip->di_ctime.tv_nsec);
3118 ip->i_blksize = ip->i_sb->s_blocksize;
3119 ip->i_blocks = LBLK2PBLK(ip->i_sb, le64_to_cpu(dip->di_nblocks));
3120 ip->i_generation = le32_to_cpu(dip->di_gen);
3122 jfs_ip->ixpxd = dip->di_ixpxd; /* in-memory pxd's are little-endian */
3123 jfs_ip->acl = dip->di_acl; /* as are dxd's */
3124 jfs_ip->ea = dip->di_ea;
3125 jfs_ip->next_index = le32_to_cpu(dip->di_next_index);
3126 jfs_ip->otime = le32_to_cpu(dip->di_otime.tv_sec);
3127 jfs_ip->acltype = le32_to_cpu(dip->di_acltype);
3129 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode)) {
3130 jfs_ip->dev = le32_to_cpu(dip->di_rdev);
3131 ip->i_rdev = new_decode_dev(jfs_ip->dev);
3134 if (S_ISDIR(ip->i_mode)) {
3135 memcpy(&jfs_ip->i_dirtable, &dip->di_dirtable, 384);
3136 } else if (S_ISREG(ip->i_mode) || S_ISLNK(ip->i_mode)) {
3137 memcpy(&jfs_ip->i_xtroot, &dip->di_xtroot, 288);
3138 } else
3139 memcpy(&jfs_ip->i_inline_ea, &dip->di_inlineea, 128);
3141 /* Zero the in-memory-only stuff */
3142 jfs_ip->cflag = 0;
3143 jfs_ip->btindex = 0;
3144 jfs_ip->btorder = 0;
3145 jfs_ip->bxflag = 0;
3146 jfs_ip->blid = 0;
3147 jfs_ip->atlhead = 0;
3148 jfs_ip->atltail = 0;
3149 jfs_ip->xtlid = 0;
3150 return (0);
3154 * NAME: copy_to_dinode()
3156 * FUNCTION: Copies inode info from in-memory inode to disk inode
3158 static void copy_to_dinode(struct dinode * dip, struct inode *ip)
3160 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
3162 dip->di_fileset = cpu_to_le32(jfs_ip->fileset);
3163 dip->di_inostamp = cpu_to_le32(JFS_SBI(ip->i_sb)->inostamp);
3164 dip->di_number = cpu_to_le32(ip->i_ino);
3165 dip->di_gen = cpu_to_le32(ip->i_generation);
3166 dip->di_size = cpu_to_le64(ip->i_size);
3167 dip->di_nblocks = cpu_to_le64(PBLK2LBLK(ip->i_sb, ip->i_blocks));
3168 dip->di_nlink = cpu_to_le32(ip->i_nlink);
3169 dip->di_uid = cpu_to_le32(ip->i_uid);
3170 dip->di_gid = cpu_to_le32(ip->i_gid);
3172 * mode2 is only needed for storing the higher order bits.
3173 * Trust i_mode for the lower order ones
3175 dip->di_mode = cpu_to_le32((jfs_ip->mode2 & 0xffff0000) | ip->i_mode);
3176 dip->di_atime.tv_sec = cpu_to_le32(ip->i_atime.tv_sec);
3177 dip->di_atime.tv_nsec = cpu_to_le32(ip->i_atime.tv_nsec);
3178 dip->di_ctime.tv_sec = cpu_to_le32(ip->i_ctime.tv_sec);
3179 dip->di_ctime.tv_nsec = cpu_to_le32(ip->i_ctime.tv_nsec);
3180 dip->di_mtime.tv_sec = cpu_to_le32(ip->i_mtime.tv_sec);
3181 dip->di_mtime.tv_nsec = cpu_to_le32(ip->i_mtime.tv_nsec);
3182 dip->di_ixpxd = jfs_ip->ixpxd; /* in-memory pxd's are little-endian */
3183 dip->di_acl = jfs_ip->acl; /* as are dxd's */
3184 dip->di_ea = jfs_ip->ea;
3185 dip->di_next_index = cpu_to_le32(jfs_ip->next_index);
3186 dip->di_otime.tv_sec = cpu_to_le32(jfs_ip->otime);
3187 dip->di_otime.tv_nsec = 0;
3188 dip->di_acltype = cpu_to_le32(jfs_ip->acltype);
3189 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode))
3190 dip->di_rdev = cpu_to_le32(jfs_ip->dev);
3193 #ifdef _JFS_DEBUG_IMAP
3195 * DBGdiInit()
3197 static void *DBGdiInit(struct inomap * imap)
3199 u32 *dimap;
3200 int size;
3201 size = 64 * 1024;
3202 if ((dimap = (u32 *) xmalloc(size, L2PSIZE, kernel_heap)) == NULL)
3203 assert(0);
3204 bzero((void *) dimap, size);
3205 imap->im_DBGdimap = dimap;
3209 * DBGdiAlloc()
3211 static void DBGdiAlloc(struct inomap * imap, ino_t ino)
3213 u32 *dimap = imap->im_DBGdimap;
3214 int w, b;
3215 u32 m;
3216 w = ino >> 5;
3217 b = ino & 31;
3218 m = 0x80000000 >> b;
3219 assert(w < 64 * 256);
3220 if (dimap[w] & m) {
3221 printk("DEBUG diAlloc: duplicate alloc ino:0x%x\n", ino);
3223 dimap[w] |= m;
3227 * DBGdiFree()
3229 static void DBGdiFree(struct inomap * imap, ino_t ino)
3231 u32 *dimap = imap->im_DBGdimap;
3232 int w, b;
3233 u32 m;
3234 w = ino >> 5;
3235 b = ino & 31;
3236 m = 0x80000000 >> b;
3237 assert(w < 64 * 256);
3238 if ((dimap[w] & m) == 0) {
3239 printk("DEBUG diFree: duplicate free ino:0x%x\n", ino);
3241 dimap[w] &= ~m;
3244 static void dump_cp(struct inomap * ipimap, char *function, int line)
3246 printk("\n* ********* *\nControl Page %s %d\n", function, line);
3247 printk("FreeIAG %d\tNextIAG %d\n", ipimap->im_freeiag,
3248 ipimap->im_nextiag);
3249 printk("NumInos %d\tNumFree %d\n",
3250 atomic_read(&ipimap->im_numinos),
3251 atomic_read(&ipimap->im_numfree));
3252 printk("AG InoFree %d\tAG ExtFree %d\n",
3253 ipimap->im_agctl[0].inofree, ipimap->im_agctl[0].extfree);
3254 printk("AG NumInos %d\tAG NumFree %d\n",
3255 ipimap->im_agctl[0].numinos, ipimap->im_agctl[0].numfree);
3258 static void dump_iag(struct iag * iag, char *function, int line)
3260 printk("\n* ********* *\nIAG %s %d\n", function, line);
3261 printk("IagNum %d\tIAG Free %d\n", le32_to_cpu(iag->iagnum),
3262 le32_to_cpu(iag->iagfree));
3263 printk("InoFreeFwd %d\tInoFreeBack %d\n",
3264 le32_to_cpu(iag->inofreefwd),
3265 le32_to_cpu(iag->inofreeback));
3266 printk("ExtFreeFwd %d\tExtFreeBack %d\n",
3267 le32_to_cpu(iag->extfreefwd),
3268 le32_to_cpu(iag->extfreeback));
3269 printk("NFreeInos %d\tNFreeExts %d\n", le32_to_cpu(iag->nfreeinos),
3270 le32_to_cpu(iag->nfreeexts));
3272 #endif /* _JFS_DEBUG_IMAP */