patch-2.6.6-vs1.9.0
[linux-2.6.git] / fs / jfs / jfs_imap.c
1 /*
2  *   Copyright (C) International Business Machines Corp., 2000-2003
3  *
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.
13  *
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
17  */
18
19 /*
20  *      jfs_imap.c: inode allocation map manager
21  *
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.
26  *
27  *   Each IAG is locked by obtaining the buffer for the IAG page.
28  *
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.
33  *
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.
38  *
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().
42  */
43
44 #include <linux/fs.h>
45 #include <linux/buffer_head.h>
46 #include <linux/pagemap.h>
47 #include <linux/vserver/xid.h>
48
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"
57
58 /*
59  * imap locks
60  */
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)
65
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])
70
71 /*
72  * external references
73  */
74 extern struct address_space_operations jfs_aops;
75
76 /*
77  * forward references
78  */
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 *);
88
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 *);
92
93 /*
94  *      debug code for double-checking inode map
95  */
96 /* #define      _JFS_DEBUG_IMAP 1 */
97
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)
102
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 */
111
112 /*
113  * NAME:        diMount()
114  *
115  * FUNCTION:    initialize the incore inode map control structures for
116  *              a fileset or aggregate init time.
117  *
118  *              the inode map's control structure (dinomap) is 
119  *              brought in from disk and placed in virtual memory.
120  *
121  * PARAMETERS:
122  *      ipimap  - pointer to inode map inode for the aggregate or fileset.
123  *
124  * RETURN VALUES:
125  *      0       - success
126  *      -ENOMEM  - insufficient free virtual memory.
127  *      -EIO    - i/o error.
128  */
129 int diMount(struct inode *ipimap)
130 {
131         struct inomap *imap;
132         struct metapage *mp;
133         int index;
134         struct dinomap *dinom_le;
135
136         /*
137          * allocate/initialize the in-memory inode map control structure
138          */
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;
144         }
145
146         /* read the on-disk inode map control structure. */
147
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;
154         }
155
156         /* copy the on-disk version to the in-memory version. */
157         dinom_le = (struct dinomap *) mp->data;
158         imap->im_freeiag = le32_to_cpu(dinom_le->in_freeiag);
159         imap->im_nextiag = le32_to_cpu(dinom_le->in_nextiag);
160         atomic_set(&imap->im_numinos, le32_to_cpu(dinom_le->in_numinos));
161         atomic_set(&imap->im_numfree, le32_to_cpu(dinom_le->in_numfree));
162         imap->im_nbperiext = le32_to_cpu(dinom_le->in_nbperiext);
163         imap->im_l2nbperiext = le32_to_cpu(dinom_le->in_l2nbperiext);
164         for (index = 0; index < MAXAG; index++) {
165                 imap->im_agctl[index].inofree =
166                     le32_to_cpu(dinom_le->in_agctl[index].inofree);
167                 imap->im_agctl[index].extfree =
168                     le32_to_cpu(dinom_le->in_agctl[index].extfree);
169                 imap->im_agctl[index].numinos =
170                     le32_to_cpu(dinom_le->in_agctl[index].numinos);
171                 imap->im_agctl[index].numfree =
172                     le32_to_cpu(dinom_le->in_agctl[index].numfree);
173         }
174
175         /* release the buffer. */
176         release_metapage(mp);
177
178         /*
179          * allocate/initialize inode allocation map locks
180          */
181         /* allocate and init iag free list lock */
182         IAGFREE_LOCK_INIT(imap);
183
184         /* allocate and init ag list locks */
185         for (index = 0; index < MAXAG; index++) {
186                 AG_LOCK_INIT(imap, index);
187         }
188
189         /* bind the inode map inode and inode map control structure
190          * to each other.
191          */
192         imap->im_ipimap = ipimap;
193         JFS_IP(ipimap)->i_imap = imap;
194
195 //      DBG_DIINIT(imap);
196
197         return (0);
198 }
199
200
201 /*
202  * NAME:        diUnmount()
203  *
204  * FUNCTION:    write to disk the incore inode map control structures for
205  *              a fileset or aggregate at unmount time.
206  *
207  * PARAMETERS:
208  *      ipimap  - pointer to inode map inode for the aggregate or fileset.
209  *
210  * RETURN VALUES:
211  *      0       - success
212  *      -ENOMEM  - insufficient free virtual memory.
213  *      -EIO    - i/o error.
214  */
215 int diUnmount(struct inode *ipimap, int mounterror)
216 {
217         struct inomap *imap = JFS_IP(ipimap)->i_imap;
218
219         /*
220          * update the on-disk inode map control structure
221          */
222
223         if (!(mounterror || isReadOnly(ipimap)))
224                 diSync(ipimap);
225
226         /*
227          * Invalidate the page cache buffers
228          */
229         truncate_inode_pages(ipimap->i_mapping, 0);
230
231         /*
232          * free in-memory control structure
233          */
234         kfree(imap);
235
236         return (0);
237 }
238
239
240 /*
241  *      diSync()
242  */
243 int diSync(struct inode *ipimap)
244 {
245         struct dinomap *dinom_le;
246         struct inomap *imp = JFS_IP(ipimap)->i_imap;
247         struct metapage *mp;
248         int index;
249
250         /*
251          * write imap global conrol page
252          */
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;
260         }
261
262         /* copy the in-memory version to the on-disk version */
263         dinom_le = (struct dinomap *) mp->data;
264         dinom_le->in_freeiag = cpu_to_le32(imp->im_freeiag);
265         dinom_le->in_nextiag = cpu_to_le32(imp->im_nextiag);
266         dinom_le->in_numinos = cpu_to_le32(atomic_read(&imp->im_numinos));
267         dinom_le->in_numfree = cpu_to_le32(atomic_read(&imp->im_numfree));
268         dinom_le->in_nbperiext = cpu_to_le32(imp->im_nbperiext);
269         dinom_le->in_l2nbperiext = cpu_to_le32(imp->im_l2nbperiext);
270         for (index = 0; index < MAXAG; index++) {
271                 dinom_le->in_agctl[index].inofree =
272                     cpu_to_le32(imp->im_agctl[index].inofree);
273                 dinom_le->in_agctl[index].extfree =
274                     cpu_to_le32(imp->im_agctl[index].extfree);
275                 dinom_le->in_agctl[index].numinos =
276                     cpu_to_le32(imp->im_agctl[index].numinos);
277                 dinom_le->in_agctl[index].numfree =
278                     cpu_to_le32(imp->im_agctl[index].numfree);
279         }
280
281         /* write out the control structure */
282         write_metapage(mp);
283
284         /*
285          * write out dirty pages of imap
286          */
287         filemap_fdatawrite(ipimap->i_mapping);
288         filemap_fdatawait(ipimap->i_mapping);
289
290         diWriteSpecial(ipimap, 0);
291
292         return (0);
293 }
294
295
296 /*
297  * NAME:        diRead()
298  *
299  * FUNCTION:    initialize an incore inode from disk.
300  *
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).
304  *              
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().
312  *              
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.
319  *
320  * PARAMETERS:
321  *      ip  -  pointer to incore inode to be initialized from disk.
322  *
323  * RETURN VALUES:
324  *      0       - success
325  *      -EIO    - i/o error.
326  *      -ENOMEM - insufficient memory
327  *      
328  */
329 int diRead(struct inode *ip)
330 {
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;
343
344         jfs_info("diRead: ino = %ld", ip->i_ino);
345
346         ipimap = sbi->ipimap;
347         JFS_IP(ip)->ipimap = ipimap;
348
349         /* determine the iag number for this inode (number) */
350         iagno = INOTOIAG(ip->i_ino);
351
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);
360         }
361
362         iagp = (struct iag *) mp->data;
363
364         /* determine inode extent that holds the disk inode */
365         ino = ip->i_ino & (INOSPERIAG - 1);
366         extno = ino >> L2INOSPEREXT;
367
368         if ((lengthPXD(&iagp->inoext[extno]) != imap->im_nbperiext) ||
369             (addressPXD(&iagp->inoext[extno]) == 0)) {
370                 release_metapage(mp);
371                 return -ESTALE;
372         }
373
374         /* get disk block number of the page within the inode extent
375          * that holds the disk inode.
376          */
377         blkno = INOPBLK(&iagp->inoext[extno], ino, sbi->l2nbperpage);
378
379         /* get the ag for the iag */
380         agstart = le64_to_cpu(iagp->agstart);
381
382         release_metapage(mp);
383
384         rel_inode = (ino & (INOSPERPAGE - 1));
385         pageno = blkno >> sbi->l2nbperpage;
386
387         if ((block_offset = ((u32) blkno & (sbi->nbperpage - 1)))) {
388                 /*
389                  * OS/2 didn't always align inode extents on page boundaries
390                  */
391                 inodes_left =
392                      (sbi->nbperpage - block_offset) << sbi->l2niperblk;
393
394                 if (rel_inode < inodes_left)
395                         rel_inode += block_offset << sbi->l2niperblk;
396                 else {
397                         pageno += 1;
398                         rel_inode -= inodes_left;
399                 }
400         }
401
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;
407         }
408
409         /* locate the the disk inode requested */
410         dp = (struct dinode *) mp->data;
411         dp += rel_inode;
412
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);
421
422         release_metapage(mp);
423
424         /* set the ag for the inode */
425         JFS_IP(ip)->agno = BLKTOAG(agstart, sbi);
426         JFS_IP(ip)->active_ag = -1;
427
428         return (rc);
429 }
430
431
432 /*
433  * NAME:        diReadSpecial()
434  *
435  * FUNCTION:    initialize a 'special' inode from disk.
436  *
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.
444  *
445  * PARAMETERS:
446  *      sb - filesystem superblock
447  *      inum - aggregate inode number
448  *      secondary - 1 if secondary aggregate inode table
449  *
450  * RETURN VALUES:
451  *      new inode       - success
452  *      NULL            - i/o error.
453  */
454 struct inode *diReadSpecial(struct super_block *sb, ino_t inum, int secondary)
455 {
456         struct jfs_sb_info *sbi = JFS_SBI(sb);
457         uint address;
458         struct dinode *dp;
459         struct inode *ip;
460         struct metapage *mp;
461
462         ip = new_inode(sb);
463         if (ip == NULL) {
464                 jfs_err("diReadSpecial: new_inode returned NULL!");
465                 return ip;
466         }
467
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;
474         }
475
476         ASSERT(inum < INOSPEREXT);
477
478         ip->i_ino = inum;
479
480         address += inum >> 3;   /* 8 inodes per 4K page */
481
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);
488         }
489
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 */
493
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);
502
503         }
504
505         ip->i_mapping->a_ops = &jfs_aops;
506         mapping_set_gfp_mask(ip->i_mapping, GFP_NOFS);
507
508         if ((inum == FILESYSTEM_I) && (JFS_IP(ip)->ipimap == sbi->ipaimap)) {
509                 sbi->gengen = le32_to_cpu(dp->di_gengen);
510                 sbi->inostamp = le32_to_cpu(dp->di_inostamp);
511         }
512
513         /* release the page */
514         release_metapage(mp);
515
516         return (ip);
517 }
518
519 /*
520  * NAME:        diWriteSpecial()
521  *
522  * FUNCTION:    Write the special inode to disk
523  *
524  * PARAMETERS:
525  *      ip - special inode
526  *      secondary - 1 if secondary aggregate inode table
527  *
528  * RETURN VALUES: none
529  */
530
531 void diWriteSpecial(struct inode *ip, int secondary)
532 {
533         struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
534         uint address;
535         struct dinode *dp;
536         ino_t inum = ip->i_ino;
537         struct metapage *mp;
538
539         ip->i_state &= ~I_DIRTY;
540
541         if (secondary)
542                 address = addressPXD(&sbi->ait2) >> sbi->l2nbperpage;
543         else
544                 address = AITBL_OFF >> L2PSIZE;
545
546         ASSERT(inum < INOSPEREXT);
547
548         address += inum >> 3;   /* 8 inodes per 4K page */
549
550         /* read the page of fixed disk inode (AIT) in raw mode */
551         mp = read_metapage(ip, address << sbi->l2nbperpage, PSIZE, 1);
552         if (mp == NULL) {
553                 jfs_err("diWriteSpecial: failed to read aggregate inode "
554                         "extent!");
555                 return;
556         }
557
558         /* get the pointer to the disk inode of interest */
559         dp = (struct dinode *) (mp->data);
560         dp += inum % 8;         /* 8 inodes per 4K page */
561
562         /* copy on-disk inode to in-memory inode */
563         copy_to_dinode(dp, ip);
564         memcpy(&dp->di_xtroot, &JFS_IP(ip)->i_xtroot, 288);
565
566         if (inum == FILESYSTEM_I)
567                 dp->di_gengen = cpu_to_le32(sbi->gengen);
568
569         /* write the page */
570         write_metapage(mp);
571 }
572
573 /*
574  * NAME:        diFreeSpecial()
575  *
576  * FUNCTION:    Free allocated space for special inode
577  */
578 void diFreeSpecial(struct inode *ip)
579 {
580         if (ip == NULL) {
581                 jfs_err("diFreeSpecial called with NULL ip!");
582                 return;
583         }
584         filemap_fdatawrite(ip->i_mapping);
585         filemap_fdatawait(ip->i_mapping);
586         truncate_inode_pages(ip->i_mapping, 0);
587         iput(ip);
588 }
589
590
591
592 /*
593  * NAME:        diWrite()
594  *
595  * FUNCTION:    write the on-disk inode portion of the in-memory inode
596  *              to its corresponding on-disk inode.
597  *
598  *              on entry, the specifed incore inode should itself
599  *              specify the disk inode number corresponding to the
600  *              incore inode (i.e. i_number should be initialized).
601  *
602  *              the inode contains the inode extent address for the disk
603  *              inode.  with the inode extent address in hand, the
604  *              page of the extent that contains the disk inode is
605  *              read and the disk inode portion of the incore inode
606  *              is copied to the disk inode.
607  *              
608  * PARAMETERS:
609  *      tid -  transacation id
610  *      ip  -  pointer to incore inode to be written to the inode extent.
611  *
612  * RETURN VALUES:
613  *      0       - success
614  *      -EIO    - i/o error.
615  */
616 int diWrite(tid_t tid, struct inode *ip)
617 {
618         struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
619         struct jfs_inode_info *jfs_ip = JFS_IP(ip);
620         int rc = 0;
621         s32 ino;
622         struct dinode *dp;
623         s64 blkno;
624         int block_offset;
625         int inodes_left;
626         struct metapage *mp;
627         uint pageno;
628         int rel_inode;
629         int dioffset;
630         struct inode *ipimap;
631         uint type;
632         lid_t lid;
633         struct tlock *ditlck, *tlck;
634         struct linelock *dilinelock, *ilinelock;
635         struct lv *lv;
636         int n;
637
638         ipimap = jfs_ip->ipimap;
639
640         ino = ip->i_ino & (INOSPERIAG - 1);
641
642         if (!addressPXD(&(jfs_ip->ixpxd)) ||
643             (lengthPXD(&(jfs_ip->ixpxd)) !=
644              JFS_IP(ipimap)->i_imap->im_nbperiext)) {
645                 jfs_error(ip->i_sb, "diWrite: ixpxd invalid");
646                 return -EIO;
647         }
648
649         /*
650          * read the page of disk inode containing the specified inode:
651          */
652         /* compute the block address of the page */
653         blkno = INOPBLK(&(jfs_ip->ixpxd), ino, sbi->l2nbperpage);
654
655         rel_inode = (ino & (INOSPERPAGE - 1));
656         pageno = blkno >> sbi->l2nbperpage;
657
658         if ((block_offset = ((u32) blkno & (sbi->nbperpage - 1)))) {
659                 /*
660                  * OS/2 didn't always align inode extents on page boundaries
661                  */
662                 inodes_left =
663                     (sbi->nbperpage - block_offset) << sbi->l2niperblk;
664
665                 if (rel_inode < inodes_left)
666                         rel_inode += block_offset << sbi->l2niperblk;
667                 else {
668                         pageno += 1;
669                         rel_inode -= inodes_left;
670                 }
671         }
672         /* read the page of disk inode */
673       retry:
674         mp = read_metapage(ipimap, pageno << sbi->l2nbperpage, PSIZE, 1);
675         if (mp == 0)
676                 return -EIO;
677
678         /* get the pointer to the disk inode */
679         dp = (struct dinode *) mp->data;
680         dp += rel_inode;
681
682         dioffset = (ino & (INOSPERPAGE - 1)) << L2DISIZE;
683
684         /*
685          * acquire transaction lock on the on-disk inode;
686          * N.B. tlock is acquired on ipimap not ip;
687          */
688         if ((ditlck =
689              txLock(tid, ipimap, mp, tlckINODE | tlckENTRY)) == NULL)
690                 goto retry;
691         dilinelock = (struct linelock *) & ditlck->lock;
692
693         /*
694          * copy btree root from in-memory inode to on-disk inode
695          *
696          * (tlock is taken from inline B+-tree root in in-memory
697          * inode when the B+-tree root is updated, which is pointed 
698          * by jfs_ip->blid as well as being on tx tlock list)
699          *
700          * further processing of btree root is based on the copy 
701          * in in-memory inode, where txLog() will log from, and, 
702          * for xtree root, txUpdateMap() will update map and reset
703          * XAD_NEW bit;
704          */
705
706         if (S_ISDIR(ip->i_mode) && (lid = jfs_ip->xtlid)) {
707                 /*
708                  * This is the special xtree inside the directory for storing
709                  * the directory table
710                  */
711                 xtpage_t *p, *xp;
712                 xad_t *xad;
713
714                 jfs_ip->xtlid = 0;
715                 tlck = lid_to_tlock(lid);
716                 assert(tlck->type & tlckXTREE);
717                 tlck->type |= tlckBTROOT;
718                 tlck->mp = mp;
719                 ilinelock = (struct linelock *) & tlck->lock;
720
721                 /*
722                  * copy xtree root from inode to dinode:
723                  */
724                 p = &jfs_ip->i_xtroot;
725                 xp = (xtpage_t *) &dp->di_dirtable;
726                 lv = ilinelock->lv;
727                 for (n = 0; n < ilinelock->index; n++, lv++) {
728                         memcpy(&xp->xad[lv->offset], &p->xad[lv->offset],
729                                lv->length << L2XTSLOTSIZE);
730                 }
731
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);
738         }
739
740         if ((lid = jfs_ip->blid) == 0)
741                 goto inlineData;
742         jfs_ip->blid = 0;
743
744         tlck = lid_to_tlock(lid);
745         type = tlck->type;
746         tlck->type |= tlckBTROOT;
747         tlck->mp = mp;
748         ilinelock = (struct linelock *) & tlck->lock;
749
750         /*
751          *      regular file: 16 byte (XAD slot) granularity
752          */
753         if (type & tlckXTREE) {
754                 xtpage_t *p, *xp;
755                 xad_t *xad;
756
757                 /*
758                  * copy xtree root from inode to dinode:
759                  */
760                 p = &jfs_ip->i_xtroot;
761                 xp = &dp->di_xtroot;
762                 lv = ilinelock->lv;
763                 for (n = 0; n < ilinelock->index; n++, lv++) {
764                         memcpy(&xp->xad[lv->offset], &p->xad[lv->offset],
765                                lv->length << L2XTSLOTSIZE);
766                 }
767
768                 /* reset on-disk (metadata page) xtree XAD_NEW bit */
769                 xad = &xp->xad[XTENTRYSTART];
770                 for (n = XTENTRYSTART;
771                      n < le16_to_cpu(xp->header.nextindex); n++, xad++)
772                         if (xad->flag & (XAD_NEW | XAD_EXTENDED))
773                                 xad->flag &= ~(XAD_NEW | XAD_EXTENDED);
774         }
775         /*
776          *      directory: 32 byte (directory entry slot) granularity
777          */
778         else if (type & tlckDTREE) {
779                 dtpage_t *p, *xp;
780
781                 /*
782                  * copy dtree root from inode to dinode:
783                  */
784                 p = (dtpage_t *) &jfs_ip->i_dtroot;
785                 xp = (dtpage_t *) & dp->di_dtroot;
786                 lv = ilinelock->lv;
787                 for (n = 0; n < ilinelock->index; n++, lv++) {
788                         memcpy(&xp->slot[lv->offset], &p->slot[lv->offset],
789                                lv->length << L2DTSLOTSIZE);
790                 }
791         } else {
792                 jfs_err("diWrite: UFO tlock");
793         }
794
795       inlineData:
796         /*
797          * copy inline symlink from in-memory inode to on-disk inode
798          */
799         if (S_ISLNK(ip->i_mode) && ip->i_size < IDATASIZE) {
800                 lv = & dilinelock->lv[dilinelock->index];
801                 lv->offset = (dioffset + 2 * 128) >> L2INODESLOTSIZE;
802                 lv->length = 2;
803                 memcpy(&dp->di_fastsymlink, jfs_ip->i_inline, IDATASIZE);
804                 dilinelock->index++;
805         }
806         /*
807          * copy inline data from in-memory inode to on-disk inode:
808          * 128 byte slot granularity
809          */
810         if (test_cflag(COMMIT_Inlineea, ip)) {
811                 lv = & dilinelock->lv[dilinelock->index];
812                 lv->offset = (dioffset + 3 * 128) >> L2INODESLOTSIZE;
813                 lv->length = 1;
814                 memcpy(&dp->di_inlineea, jfs_ip->i_inline_ea, INODESLOTSIZE);
815                 dilinelock->index++;
816
817                 clear_cflag(COMMIT_Inlineea, ip);
818         }
819
820         /*
821          *      lock/copy inode base: 128 byte slot granularity
822          */
823 // baseDinode:
824         lv = & dilinelock->lv[dilinelock->index];
825         lv->offset = dioffset >> L2INODESLOTSIZE;
826         copy_to_dinode(dp, ip);
827         if (test_and_clear_cflag(COMMIT_Dirtable, ip)) {
828                 lv->length = 2;
829                 memcpy(&dp->di_dirtable, &jfs_ip->i_dirtable, 96);
830         } else
831                 lv->length = 1;
832         dilinelock->index++;
833
834 #ifdef _JFS_FASTDASD
835         /*
836          * We aren't logging changes to the DASD used in directory inodes,
837          * but we need to write them to disk.  If we don't unmount cleanly,
838          * mount will recalculate the DASD used.
839          */
840         if (S_ISDIR(ip->i_mode)
841             && (ip->i_ipmnt->i_mntflag & JFS_DASD_ENABLED))
842                 memcpy(&dp->di_DASD, &ip->i_DASD, sizeof(struct dasd));
843 #endif                          /*  _JFS_FASTDASD */
844
845         /* release the buffer holding the updated on-disk inode. 
846          * the buffer will be later written by commit processing.
847          */
848         write_metapage(mp);
849
850         return (rc);
851 }
852
853
854 /*
855  * NAME:        diFree(ip)
856  *
857  * FUNCTION:    free a specified inode from the inode working map
858  *              for a fileset or aggregate.
859  *
860  *              if the inode to be freed represents the first (only)
861  *              free inode within the iag, the iag will be placed on
862  *              the ag free inode list.
863  *      
864  *              freeing the inode will cause the inode extent to be
865  *              freed if the inode is the only allocated inode within
866  *              the extent.  in this case all the disk resource backing
867  *              up the inode extent will be freed. in addition, the iag
868  *              will be placed on the ag extent free list if the extent
869  *              is the first free extent in the iag.  if freeing the
870  *              extent also means that no free inodes will exist for
871  *              the iag, the iag will also be removed from the ag free
872  *              inode list.
873  *
874  *              the iag describing the inode will be freed if the extent
875  *              is to be freed and it is the only backed extent within
876  *              the iag.  in this case, the iag will be removed from the
877  *              ag free extent list and ag free inode list and placed on
878  *              the inode map's free iag list.
879  *
880  *              a careful update approach is used to provide consistency
881  *              in the face of updates to multiple buffers.  under this
882  *              approach, all required buffers are obtained before making
883  *              any updates and are held until all updates are complete.
884  *
885  * PARAMETERS:
886  *      ip      - inode to be freed.
887  *
888  * RETURN VALUES:
889  *      0       - success
890  *      -EIO    - i/o error.
891  */
892 int diFree(struct inode *ip)
893 {
894         int rc;
895         ino_t inum = ip->i_ino;
896         struct iag *iagp, *aiagp, *biagp, *ciagp, *diagp;
897         struct metapage *mp, *amp, *bmp, *cmp, *dmp;
898         int iagno, ino, extno, bitno, sword, agno;
899         int back, fwd;
900         u32 bitmap, mask;
901         struct inode *ipimap = JFS_SBI(ip->i_sb)->ipimap;
902         struct inomap *imap = JFS_IP(ipimap)->i_imap;
903         pxd_t freepxd;
904         tid_t tid;
905         struct inode *iplist[3];
906         struct tlock *tlck;
907         struct pxd_lock *pxdlock;
908
909         /*
910          * This is just to suppress compiler warnings.  The same logic that
911          * references these variables is used to initialize them.
912          */
913         aiagp = biagp = ciagp = diagp = NULL;
914
915         /* get the iag number containing the inode.
916          */
917         iagno = INOTOIAG(inum);
918
919         /* make sure that the iag is contained within 
920          * the map.
921          */
922         if (iagno >= imap->im_nextiag) {
923                 dump_mem("imap", imap, 32);
924                 jfs_error(ip->i_sb,
925                           "diFree: inum = %d, iagno = %d, nextiag = %d",
926                           (uint) inum, iagno, imap->im_nextiag);
927                 return -EIO;
928         }
929
930         /* get the allocation group for this ino.
931          */
932         agno = JFS_IP(ip)->agno;
933
934         /* Lock the AG specific inode map information
935          */
936         AG_LOCK(imap, agno);
937
938         /* Obtain read lock in imap inode.  Don't release it until we have
939          * read all of the IAG's that we are going to.
940          */
941         IREAD_LOCK(ipimap);
942
943         /* read the iag.
944          */
945         if ((rc = diIAGRead(imap, iagno, &mp))) {
946                 IREAD_UNLOCK(ipimap);
947                 AG_UNLOCK(imap, agno);
948                 return (rc);
949         }
950         iagp = (struct iag *) mp->data;
951
952         /* get the inode number and extent number of the inode within
953          * the iag and the inode number within the extent.
954          */
955         ino = inum & (INOSPERIAG - 1);
956         extno = ino >> L2INOSPEREXT;
957         bitno = ino & (INOSPEREXT - 1);
958         mask = HIGHORDER >> bitno;
959
960         if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
961                 jfs_error(ip->i_sb,
962                           "diFree: wmap shows inode already free");
963         }
964
965         if (!addressPXD(&iagp->inoext[extno])) {
966                 release_metapage(mp);
967                 IREAD_UNLOCK(ipimap);
968                 AG_UNLOCK(imap, agno);
969                 jfs_error(ip->i_sb, "diFree: invalid inoext");
970                 return -EIO;
971         }
972
973         /* compute the bitmap for the extent reflecting the freed inode.
974          */
975         bitmap = le32_to_cpu(iagp->wmap[extno]) & ~mask;
976
977         if (imap->im_agctl[agno].numfree > imap->im_agctl[agno].numinos) {
978                 release_metapage(mp);
979                 IREAD_UNLOCK(ipimap);
980                 AG_UNLOCK(imap, agno);
981                 jfs_error(ip->i_sb, "diFree: numfree > numinos");
982                 return -EIO;
983         }
984         /*
985          *      inode extent still has some inodes or below low water mark:
986          *      keep the inode extent;
987          */
988         if (bitmap ||
989             imap->im_agctl[agno].numfree < 96 ||
990             (imap->im_agctl[agno].numfree < 288 &&
991              (((imap->im_agctl[agno].numfree * 100) /
992                imap->im_agctl[agno].numinos) <= 25))) {
993                 /* if the iag currently has no free inodes (i.e.,
994                  * the inode being freed is the first free inode of iag),
995                  * insert the iag at head of the inode free list for the ag.
996                  */
997                 if (iagp->nfreeinos == 0) {
998                         /* check if there are any iags on the ag inode
999                          * free list.  if so, read the first one so that
1000                          * we can link the current iag onto the list at
1001                          * the head.
1002                          */
1003                         if ((fwd = imap->im_agctl[agno].inofree) >= 0) {
1004                                 /* read the iag that currently is the head
1005                                  * of the list.
1006                                  */
1007                                 if ((rc = diIAGRead(imap, fwd, &amp))) {
1008                                         IREAD_UNLOCK(ipimap);
1009                                         AG_UNLOCK(imap, agno);
1010                                         release_metapage(mp);
1011                                         return (rc);
1012                                 }
1013                                 aiagp = (struct iag *) amp->data;
1014
1015                                 /* make current head point back to the iag.
1016                                  */
1017                                 aiagp->inofreeback = cpu_to_le32(iagno);
1018
1019                                 write_metapage(amp);
1020                         }
1021
1022                         /* iag points forward to current head and iag
1023                          * becomes the new head of the list.
1024                          */
1025                         iagp->inofreefwd =
1026                             cpu_to_le32(imap->im_agctl[agno].inofree);
1027                         iagp->inofreeback = -1;
1028                         imap->im_agctl[agno].inofree = iagno;
1029                 }
1030                 IREAD_UNLOCK(ipimap);
1031
1032                 /* update the free inode summary map for the extent if
1033                  * freeing the inode means the extent will now have free
1034                  * inodes (i.e., the inode being freed is the first free 
1035                  * inode of extent),
1036                  */
1037                 if (iagp->wmap[extno] == ONES) {
1038                         sword = extno >> L2EXTSPERSUM;
1039                         bitno = extno & (EXTSPERSUM - 1);
1040                         iagp->inosmap[sword] &=
1041                             cpu_to_le32(~(HIGHORDER >> bitno));
1042                 }
1043
1044                 /* update the bitmap.
1045                  */
1046                 iagp->wmap[extno] = cpu_to_le32(bitmap);
1047                 DBG_DIFREE(imap, inum);
1048
1049                 /* update the free inode counts at the iag, ag and
1050                  * map level.
1051                  */
1052                 iagp->nfreeinos =
1053                     cpu_to_le32(le32_to_cpu(iagp->nfreeinos) + 1);
1054                 imap->im_agctl[agno].numfree += 1;
1055                 atomic_inc(&imap->im_numfree);
1056
1057                 /* release the AG inode map lock
1058                  */
1059                 AG_UNLOCK(imap, agno);
1060
1061                 /* write the iag */
1062                 write_metapage(mp);
1063
1064                 return (0);
1065         }
1066
1067
1068         /*
1069          *      inode extent has become free and above low water mark:
1070          *      free the inode extent;
1071          */
1072
1073         /*
1074          *      prepare to update iag list(s) (careful update step 1)
1075          */
1076         amp = bmp = cmp = dmp = NULL;
1077         fwd = back = -1;
1078
1079         /* check if the iag currently has no free extents.  if so,
1080          * it will be placed on the head of the ag extent free list.
1081          */
1082         if (iagp->nfreeexts == 0) {
1083                 /* check if the ag extent free list has any iags.
1084                  * if so, read the iag at the head of the list now.
1085                  * this (head) iag will be updated later to reflect
1086                  * the addition of the current iag at the head of
1087                  * the list.
1088                  */
1089                 if ((fwd = imap->im_agctl[agno].extfree) >= 0) {
1090                         if ((rc = diIAGRead(imap, fwd, &amp)))
1091                                 goto error_out;
1092                         aiagp = (struct iag *) amp->data;
1093                 }
1094         } else {
1095                 /* iag has free extents. check if the addition of a free
1096                  * extent will cause all extents to be free within this
1097                  * iag.  if so, the iag will be removed from the ag extent
1098                  * free list and placed on the inode map's free iag list.
1099                  */
1100                 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG - 1)) {
1101                         /* in preparation for removing the iag from the
1102                          * ag extent free list, read the iags preceeding
1103                          * and following the iag on the ag extent free
1104                          * list.
1105                          */
1106                         if ((fwd = le32_to_cpu(iagp->extfreefwd)) >= 0) {
1107                                 if ((rc = diIAGRead(imap, fwd, &amp)))
1108                                         goto error_out;
1109                                 aiagp = (struct iag *) amp->data;
1110                         }
1111
1112                         if ((back = le32_to_cpu(iagp->extfreeback)) >= 0) {
1113                                 if ((rc = diIAGRead(imap, back, &bmp)))
1114                                         goto error_out;
1115                                 biagp = (struct iag *) bmp->data;
1116                         }
1117                 }
1118         }
1119
1120         /* remove the iag from the ag inode free list if freeing
1121          * this extent cause the iag to have no free inodes.
1122          */
1123         if (iagp->nfreeinos == cpu_to_le32(INOSPEREXT - 1)) {
1124                 int inofreeback = le32_to_cpu(iagp->inofreeback);
1125                 int inofreefwd = le32_to_cpu(iagp->inofreefwd);
1126
1127                 /* in preparation for removing the iag from the
1128                  * ag inode free list, read the iags preceeding
1129                  * and following the iag on the ag inode free
1130                  * list.  before reading these iags, we must make
1131                  * sure that we already don't have them in hand
1132                  * from up above, since re-reading an iag (buffer)
1133                  * we are currently holding would cause a deadlock.
1134                  */
1135                 if (inofreefwd >= 0) {
1136
1137                         if (inofreefwd == fwd)
1138                                 ciagp = (struct iag *) amp->data;
1139                         else if (inofreefwd == back)
1140                                 ciagp = (struct iag *) bmp->data;
1141                         else {
1142                                 if ((rc =
1143                                      diIAGRead(imap, inofreefwd, &cmp)))
1144                                         goto error_out;
1145                                 ciagp = (struct iag *) cmp->data;
1146                         }
1147                         assert(ciagp != NULL);
1148                 }
1149
1150                 if (inofreeback >= 0) {
1151                         if (inofreeback == fwd)
1152                                 diagp = (struct iag *) amp->data;
1153                         else if (inofreeback == back)
1154                                 diagp = (struct iag *) bmp->data;
1155                         else {
1156                                 if ((rc =
1157                                      diIAGRead(imap, inofreeback, &dmp)))
1158                                         goto error_out;
1159                                 diagp = (struct iag *) dmp->data;
1160                         }
1161                         assert(diagp != NULL);
1162                 }
1163         }
1164
1165         IREAD_UNLOCK(ipimap);
1166
1167         /*
1168          * invalidate any page of the inode extent freed from buffer cache;
1169          */
1170         freepxd = iagp->inoext[extno];
1171         invalidate_pxd_metapages(ip, freepxd);
1172
1173         /*
1174          *      update iag list(s) (careful update step 2)
1175          */
1176         /* add the iag to the ag extent free list if this is the
1177          * first free extent for the iag.
1178          */
1179         if (iagp->nfreeexts == 0) {
1180                 if (fwd >= 0)
1181                         aiagp->extfreeback = cpu_to_le32(iagno);
1182
1183                 iagp->extfreefwd =
1184                     cpu_to_le32(imap->im_agctl[agno].extfree);
1185                 iagp->extfreeback = -1;
1186                 imap->im_agctl[agno].extfree = iagno;
1187         } else {
1188                 /* remove the iag from the ag extent list if all extents
1189                  * are now free and place it on the inode map iag free list.
1190                  */
1191                 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG - 1)) {
1192                         if (fwd >= 0)
1193                                 aiagp->extfreeback = iagp->extfreeback;
1194
1195                         if (back >= 0)
1196                                 biagp->extfreefwd = iagp->extfreefwd;
1197                         else
1198                                 imap->im_agctl[agno].extfree =
1199                                     le32_to_cpu(iagp->extfreefwd);
1200
1201                         iagp->extfreefwd = iagp->extfreeback = -1;
1202
1203                         IAGFREE_LOCK(imap);
1204                         iagp->iagfree = cpu_to_le32(imap->im_freeiag);
1205                         imap->im_freeiag = iagno;
1206                         IAGFREE_UNLOCK(imap);
1207                 }
1208         }
1209
1210         /* remove the iag from the ag inode free list if freeing
1211          * this extent causes the iag to have no free inodes.
1212          */
1213         if (iagp->nfreeinos == cpu_to_le32(INOSPEREXT - 1)) {
1214                 if ((int) le32_to_cpu(iagp->inofreefwd) >= 0)
1215                         ciagp->inofreeback = iagp->inofreeback;
1216
1217                 if ((int) le32_to_cpu(iagp->inofreeback) >= 0)
1218                         diagp->inofreefwd = iagp->inofreefwd;
1219                 else
1220                         imap->im_agctl[agno].inofree =
1221                             le32_to_cpu(iagp->inofreefwd);
1222
1223                 iagp->inofreefwd = iagp->inofreeback = -1;
1224         }
1225
1226         /* update the inode extent address and working map 
1227          * to reflect the free extent.
1228          * the permanent map should have been updated already 
1229          * for the inode being freed.
1230          */
1231         if (iagp->pmap[extno] != 0) {
1232                 jfs_error(ip->i_sb, "diFree: the pmap does not show inode free");
1233         }
1234         iagp->wmap[extno] = 0;
1235         DBG_DIFREE(imap, inum);
1236         PXDlength(&iagp->inoext[extno], 0);
1237         PXDaddress(&iagp->inoext[extno], 0);
1238
1239         /* update the free extent and free inode summary maps
1240          * to reflect the freed extent.
1241          * the inode summary map is marked to indicate no inodes 
1242          * available for the freed extent.
1243          */
1244         sword = extno >> L2EXTSPERSUM;
1245         bitno = extno & (EXTSPERSUM - 1);
1246         mask = HIGHORDER >> bitno;
1247         iagp->inosmap[sword] |= cpu_to_le32(mask);
1248         iagp->extsmap[sword] &= cpu_to_le32(~mask);
1249
1250         /* update the number of free inodes and number of free extents
1251          * for the iag.
1252          */
1253         iagp->nfreeinos = cpu_to_le32(le32_to_cpu(iagp->nfreeinos) -
1254                                       (INOSPEREXT - 1));
1255         iagp->nfreeexts = cpu_to_le32(le32_to_cpu(iagp->nfreeexts) + 1);
1256
1257         /* update the number of free inodes and backed inodes
1258          * at the ag and inode map level.
1259          */
1260         imap->im_agctl[agno].numfree -= (INOSPEREXT - 1);
1261         imap->im_agctl[agno].numinos -= INOSPEREXT;
1262         atomic_sub(INOSPEREXT - 1, &imap->im_numfree);
1263         atomic_sub(INOSPEREXT, &imap->im_numinos);
1264
1265         if (amp)
1266                 write_metapage(amp);
1267         if (bmp)
1268                 write_metapage(bmp);
1269         if (cmp)
1270                 write_metapage(cmp);
1271         if (dmp)
1272                 write_metapage(dmp);
1273
1274         /*
1275          * start transaction to update block allocation map
1276          * for the inode extent freed;
1277          *
1278          * N.B. AG_LOCK is released and iag will be released below, and 
1279          * other thread may allocate inode from/reusing the ixad freed
1280          * BUT with new/different backing inode extent from the extent 
1281          * to be freed by the transaction;  
1282          */
1283         tid = txBegin(ipimap->i_sb, COMMIT_FORCE);
1284
1285         /* acquire tlock of the iag page of the freed ixad 
1286          * to force the page NOHOMEOK (even though no data is
1287          * logged from the iag page) until NOREDOPAGE|FREEXTENT log 
1288          * for the free of the extent is committed;
1289          * write FREEXTENT|NOREDOPAGE log record
1290          * N.B. linelock is overlaid as freed extent descriptor;
1291          */
1292         tlck = txLock(tid, ipimap, mp, tlckINODE | tlckFREE);
1293         pxdlock = (struct pxd_lock *) & tlck->lock;
1294         pxdlock->flag = mlckFREEPXD;
1295         pxdlock->pxd = freepxd;
1296         pxdlock->index = 1;
1297
1298         write_metapage(mp);
1299
1300         iplist[0] = ipimap;
1301
1302         /*
1303          * logredo needs the IAG number and IAG extent index in order
1304          * to ensure that the IMap is consistent.  The least disruptive
1305          * way to pass these values through  to the transaction manager
1306          * is in the iplist array.  
1307          * 
1308          * It's not pretty, but it works.
1309          */
1310         iplist[1] = (struct inode *) (size_t)iagno;
1311         iplist[2] = (struct inode *) (size_t)extno;
1312
1313         rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
1314
1315         txEnd(tid);
1316
1317         /* unlock the AG inode map information */
1318         AG_UNLOCK(imap, agno);
1319
1320         return (0);
1321
1322       error_out:
1323         IREAD_UNLOCK(ipimap);
1324
1325         if (amp)
1326                 release_metapage(amp);
1327         if (bmp)
1328                 release_metapage(bmp);
1329         if (cmp)
1330                 release_metapage(cmp);
1331         if (dmp)
1332                 release_metapage(dmp);
1333
1334         AG_UNLOCK(imap, agno);
1335
1336         release_metapage(mp);
1337
1338         return (rc);
1339 }
1340
1341 /*
1342  * There are several places in the diAlloc* routines where we initialize
1343  * the inode.
1344  */
1345 static inline void
1346 diInitInode(struct inode *ip, int iagno, int ino, int extno, struct iag * iagp)
1347 {
1348         struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
1349         struct jfs_inode_info *jfs_ip = JFS_IP(ip);
1350
1351         ip->i_ino = (iagno << L2INOSPERIAG) + ino;
1352         DBG_DIALLOC(JFS_IP(ipimap)->i_imap, ip->i_ino);
1353         jfs_ip->ixpxd = iagp->inoext[extno];
1354         jfs_ip->agno = BLKTOAG(le64_to_cpu(iagp->agstart), sbi);
1355         jfs_ip->active_ag = -1;
1356 }
1357
1358
1359 /*
1360  * NAME:        diAlloc(pip,dir,ip)
1361  *
1362  * FUNCTION:    allocate a disk inode from the inode working map 
1363  *              for a fileset or aggregate.
1364  *
1365  * PARAMETERS:
1366  *      pip     - pointer to incore inode for the parent inode.
1367  *      dir     - TRUE if the new disk inode is for a directory.
1368  *      ip      - pointer to a new inode
1369  *
1370  * RETURN VALUES:
1371  *      0       - success.
1372  *      -ENOSPC - insufficient disk resources.
1373  *      -EIO    - i/o error.
1374  */
1375 int diAlloc(struct inode *pip, boolean_t dir, struct inode *ip)
1376 {
1377         int rc, ino, iagno, addext, extno, bitno, sword;
1378         int nwords, rem, i, agno;
1379         u32 mask, inosmap, extsmap;
1380         struct inode *ipimap;
1381         struct metapage *mp;
1382         ino_t inum;
1383         struct iag *iagp;
1384         struct inomap *imap;
1385
1386         /* get the pointers to the inode map inode and the
1387          * corresponding imap control structure.
1388          */
1389         ipimap = JFS_SBI(pip->i_sb)->ipimap;
1390         imap = JFS_IP(ipimap)->i_imap;
1391         JFS_IP(ip)->ipimap = ipimap;
1392         JFS_IP(ip)->fileset = FILESYSTEM_I;
1393
1394         /* for a directory, the allocation policy is to start 
1395          * at the ag level using the preferred ag.
1396          */
1397         if (dir == TRUE) {
1398                 agno = dbNextAG(JFS_SBI(pip->i_sb)->ipbmap);
1399                 AG_LOCK(imap, agno);
1400                 goto tryag;
1401         }
1402
1403         /* for files, the policy starts off by trying to allocate from
1404          * the same iag containing the parent disk inode:
1405          * try to allocate the new disk inode close to the parent disk
1406          * inode, using parent disk inode number + 1 as the allocation
1407          * hint.  (we use a left-to-right policy to attempt to avoid
1408          * moving backward on the disk.)  compute the hint within the
1409          * file system and the iag.
1410          */
1411
1412         /* get the ag number of this iag */
1413         agno = JFS_IP(pip)->agno;
1414
1415         if (atomic_read(&JFS_SBI(pip->i_sb)->bmap->db_active[agno])) {
1416                 /*
1417                  * There is an open file actively growing.  We want to
1418                  * allocate new inodes from a different ag to avoid
1419                  * fragmentation problems.
1420                  */
1421                 agno = dbNextAG(JFS_SBI(pip->i_sb)->ipbmap);
1422                 AG_LOCK(imap, agno);
1423                 goto tryag;
1424         }
1425
1426         inum = pip->i_ino + 1;
1427         ino = inum & (INOSPERIAG - 1);
1428
1429         /* back off the the hint if it is outside of the iag */
1430         if (ino == 0)
1431                 inum = pip->i_ino;
1432
1433         /* lock the AG inode map information */
1434         AG_LOCK(imap, agno);
1435
1436         /* Get read lock on imap inode */
1437         IREAD_LOCK(ipimap);
1438
1439         /* get the iag number and read the iag */
1440         iagno = INOTOIAG(inum);
1441         if ((rc = diIAGRead(imap, iagno, &mp))) {
1442                 IREAD_UNLOCK(ipimap);
1443                 AG_UNLOCK(imap, agno);
1444                 return (rc);
1445         }
1446         iagp = (struct iag *) mp->data;
1447
1448         /* determine if new inode extent is allowed to be added to the iag.
1449          * new inode extent can be added to the iag if the ag
1450          * has less than 32 free disk inodes and the iag has free extents.
1451          */
1452         addext = (imap->im_agctl[agno].numfree < 32 && iagp->nfreeexts);
1453
1454         /*
1455          *      try to allocate from the IAG
1456          */
1457         /* check if the inode may be allocated from the iag 
1458          * (i.e. the inode has free inodes or new extent can be added).
1459          */
1460         if (iagp->nfreeinos || addext) {
1461                 /* determine the extent number of the hint.
1462                  */
1463                 extno = ino >> L2INOSPEREXT;
1464
1465                 /* check if the extent containing the hint has backed
1466                  * inodes.  if so, try to allocate within this extent.
1467                  */
1468                 if (addressPXD(&iagp->inoext[extno])) {
1469                         bitno = ino & (INOSPEREXT - 1);
1470                         if ((bitno =
1471                              diFindFree(le32_to_cpu(iagp->wmap[extno]),
1472                                         bitno))
1473                             < INOSPEREXT) {
1474                                 ino = (extno << L2INOSPEREXT) + bitno;
1475
1476                                 /* a free inode (bit) was found within this
1477                                  * extent, so allocate it.
1478                                  */
1479                                 rc = diAllocBit(imap, iagp, ino);
1480                                 IREAD_UNLOCK(ipimap);
1481                                 if (rc) {
1482                                         assert(rc == -EIO);
1483                                 } else {
1484                                         /* set the results of the allocation
1485                                          * and write the iag.
1486                                          */
1487                                         diInitInode(ip, iagno, ino, extno,
1488                                                     iagp);
1489                                         mark_metapage_dirty(mp);
1490                                 }
1491                                 release_metapage(mp);
1492
1493                                 /* free the AG lock and return.
1494                                  */
1495                                 AG_UNLOCK(imap, agno);
1496                                 return (rc);
1497                         }
1498
1499                         if (!addext)
1500                                 extno =
1501                                     (extno ==
1502                                      EXTSPERIAG - 1) ? 0 : extno + 1;
1503                 }
1504
1505                 /*
1506                  * no free inodes within the extent containing the hint.
1507                  *
1508                  * try to allocate from the backed extents following
1509                  * hint or, if appropriate (i.e. addext is true), allocate
1510                  * an extent of free inodes at or following the extent
1511                  * containing the hint.
1512                  * 
1513                  * the free inode and free extent summary maps are used
1514                  * here, so determine the starting summary map position
1515                  * and the number of words we'll have to examine.  again,
1516                  * the approach is to allocate following the hint, so we
1517                  * might have to initially ignore prior bits of the summary
1518                  * map that represent extents prior to the extent containing
1519                  * the hint and later revisit these bits.
1520                  */
1521                 bitno = extno & (EXTSPERSUM - 1);
1522                 nwords = (bitno == 0) ? SMAPSZ : SMAPSZ + 1;
1523                 sword = extno >> L2EXTSPERSUM;
1524
1525                 /* mask any prior bits for the starting words of the
1526                  * summary map.
1527                  */
1528                 mask = ONES << (EXTSPERSUM - bitno);
1529                 inosmap = le32_to_cpu(iagp->inosmap[sword]) | mask;
1530                 extsmap = le32_to_cpu(iagp->extsmap[sword]) | mask;
1531
1532                 /* scan the free inode and free extent summary maps for
1533                  * free resources.
1534                  */
1535                 for (i = 0; i < nwords; i++) {
1536                         /* check if this word of the free inode summary
1537                          * map describes an extent with free inodes.
1538                          */
1539                         if (~inosmap) {
1540                                 /* an extent with free inodes has been
1541                                  * found. determine the extent number
1542                                  * and the inode number within the extent.
1543                                  */
1544                                 rem = diFindFree(inosmap, 0);
1545                                 extno = (sword << L2EXTSPERSUM) + rem;
1546                                 rem = diFindFree(le32_to_cpu(iagp->wmap[extno]),
1547                                                  0);
1548                                 if (rem >= INOSPEREXT) {
1549                                         IREAD_UNLOCK(ipimap);
1550                                         release_metapage(mp);
1551                                         AG_UNLOCK(imap, agno);
1552                                         jfs_error(ip->i_sb,
1553                                                   "diAlloc: can't find free bit "
1554                                                   "in wmap");
1555                                         return EIO;
1556                                 }
1557
1558                                 /* determine the inode number within the
1559                                  * iag and allocate the inode from the
1560                                  * map.
1561                                  */
1562                                 ino = (extno << L2INOSPEREXT) + rem;
1563                                 rc = diAllocBit(imap, iagp, ino);
1564                                 IREAD_UNLOCK(ipimap);
1565                                 if (rc)
1566                                         assert(rc == -EIO);
1567                                 else {
1568                                         /* set the results of the allocation
1569                                          * and write the iag.
1570                                          */
1571                                         diInitInode(ip, iagno, ino, extno,
1572                                                     iagp);
1573                                         mark_metapage_dirty(mp);
1574                                 }
1575                                 release_metapage(mp);
1576
1577                                 /* free the AG lock and return.
1578                                  */
1579                                 AG_UNLOCK(imap, agno);
1580                                 return (rc);
1581
1582                         }
1583
1584                         /* check if we may allocate an extent of free
1585                          * inodes and whether this word of the free
1586                          * extents summary map describes a free extent.
1587                          */
1588                         if (addext && ~extsmap) {
1589                                 /* a free extent has been found.  determine
1590                                  * the extent number.
1591                                  */
1592                                 rem = diFindFree(extsmap, 0);
1593                                 extno = (sword << L2EXTSPERSUM) + rem;
1594
1595                                 /* allocate an extent of free inodes.
1596                                  */
1597                                 if ((rc = diNewExt(imap, iagp, extno))) {
1598                                         /* if there is no disk space for a
1599                                          * new extent, try to allocate the
1600                                          * disk inode from somewhere else.
1601                                          */
1602                                         if (rc == -ENOSPC)
1603                                                 break;
1604
1605                                         assert(rc == -EIO);
1606                                 } else {
1607                                         /* set the results of the allocation
1608                                          * and write the iag.
1609                                          */
1610                                         diInitInode(ip, iagno,
1611                                                     extno << L2INOSPEREXT,
1612                                                     extno, iagp);
1613                                         mark_metapage_dirty(mp);
1614                                 }
1615                                 release_metapage(mp);
1616                                 /* free the imap inode & the AG lock & return.
1617                                  */
1618                                 IREAD_UNLOCK(ipimap);
1619                                 AG_UNLOCK(imap, agno);
1620                                 return (rc);
1621                         }
1622
1623                         /* move on to the next set of summary map words.
1624                          */
1625                         sword = (sword == SMAPSZ - 1) ? 0 : sword + 1;
1626                         inosmap = le32_to_cpu(iagp->inosmap[sword]);
1627                         extsmap = le32_to_cpu(iagp->extsmap[sword]);
1628                 }
1629         }
1630         /* unlock imap inode */
1631         IREAD_UNLOCK(ipimap);
1632
1633         /* nothing doing in this iag, so release it. */
1634         release_metapage(mp);
1635
1636       tryag:
1637         /*
1638          * try to allocate anywhere within the same AG as the parent inode.
1639          */
1640         rc = diAllocAG(imap, agno, dir, ip);
1641
1642         AG_UNLOCK(imap, agno);
1643
1644         if (rc != -ENOSPC)
1645                 return (rc);
1646
1647         /*
1648          * try to allocate in any AG.
1649          */
1650         return (diAllocAny(imap, agno, dir, ip));
1651 }
1652
1653
1654 /*
1655  * NAME:        diAllocAG(imap,agno,dir,ip)
1656  *
1657  * FUNCTION:    allocate a disk inode from the allocation group.
1658  *
1659  *              this routine first determines if a new extent of free
1660  *              inodes should be added for the allocation group, with
1661  *              the current request satisfied from this extent. if this
1662  *              is the case, an attempt will be made to do just that.  if
1663  *              this attempt fails or it has been determined that a new 
1664  *              extent should not be added, an attempt is made to satisfy
1665  *              the request by allocating an existing (backed) free inode
1666  *              from the allocation group.
1667  *
1668  * PRE CONDITION: Already have the AG lock for this AG.
1669  *
1670  * PARAMETERS:
1671  *      imap    - pointer to inode map control structure.
1672  *      agno    - allocation group to allocate from.
1673  *      dir     - TRUE if the new disk inode is for a directory.
1674  *      ip      - pointer to the new inode to be filled in on successful return
1675  *                with the disk inode number allocated, its extent address
1676  *                and the start of the ag.
1677  *
1678  * RETURN VALUES:
1679  *      0       - success.
1680  *      -ENOSPC - insufficient disk resources.
1681  *      -EIO    - i/o error.
1682  */
1683 static int
1684 diAllocAG(struct inomap * imap, int agno, boolean_t dir, struct inode *ip)
1685 {
1686         int rc, addext, numfree, numinos;
1687
1688         /* get the number of free and the number of backed disk 
1689          * inodes currently within the ag.
1690          */
1691         numfree = imap->im_agctl[agno].numfree;
1692         numinos = imap->im_agctl[agno].numinos;
1693
1694         if (numfree > numinos) {
1695                 jfs_error(ip->i_sb, "diAllocAG: numfree > numinos");
1696                 return -EIO;
1697         }
1698
1699         /* determine if we should allocate a new extent of free inodes
1700          * within the ag: for directory inodes, add a new extent
1701          * if there are a small number of free inodes or number of free
1702          * inodes is a small percentage of the number of backed inodes.
1703          */
1704         if (dir == TRUE)
1705                 addext = (numfree < 64 ||
1706                           (numfree < 256
1707                            && ((numfree * 100) / numinos) <= 20));
1708         else
1709                 addext = (numfree == 0);
1710
1711         /*
1712          * try to allocate a new extent of free inodes.
1713          */
1714         if (addext) {
1715                 /* if free space is not avaliable for this new extent, try
1716                  * below to allocate a free and existing (already backed)
1717                  * inode from the ag.
1718                  */
1719                 if ((rc = diAllocExt(imap, agno, ip)) != -ENOSPC)
1720                         return (rc);
1721         }
1722
1723         /*
1724          * try to allocate an existing free inode from the ag.
1725          */
1726         return (diAllocIno(imap, agno, ip));
1727 }
1728
1729
1730 /*
1731  * NAME:        diAllocAny(imap,agno,dir,iap)
1732  *
1733  * FUNCTION:    allocate a disk inode from any other allocation group.
1734  *
1735  *              this routine is called when an allocation attempt within
1736  *              the primary allocation group has failed. if attempts to
1737  *              allocate an inode from any allocation group other than the
1738  *              specified primary group.
1739  *
1740  * PARAMETERS:
1741  *      imap    - pointer to inode map control structure.
1742  *      agno    - primary allocation group (to avoid).
1743  *      dir     - TRUE if the new disk inode is for a directory.
1744  *      ip      - pointer to a new inode to be filled in on successful return
1745  *                with the disk inode number allocated, its extent address
1746  *                and the start of the ag.
1747  *
1748  * RETURN VALUES:
1749  *      0       - success.
1750  *      -ENOSPC - insufficient disk resources.
1751  *      -EIO    - i/o error.
1752  */
1753 static int
1754 diAllocAny(struct inomap * imap, int agno, boolean_t dir, struct inode *ip)
1755 {
1756         int ag, rc;
1757         int maxag = JFS_SBI(imap->im_ipimap->i_sb)->bmap->db_maxag;
1758
1759
1760         /* try to allocate from the ags following agno up to 
1761          * the maximum ag number.
1762          */
1763         for (ag = agno + 1; ag <= maxag; ag++) {
1764                 AG_LOCK(imap, ag);
1765
1766                 rc = diAllocAG(imap, ag, dir, ip);
1767
1768                 AG_UNLOCK(imap, ag);
1769
1770                 if (rc != -ENOSPC)
1771                         return (rc);
1772         }
1773
1774         /* try to allocate from the ags in front of agno.
1775          */
1776         for (ag = 0; ag < agno; ag++) {
1777                 AG_LOCK(imap, ag);
1778
1779                 rc = diAllocAG(imap, ag, dir, ip);
1780
1781                 AG_UNLOCK(imap, ag);
1782
1783                 if (rc != -ENOSPC)
1784                         return (rc);
1785         }
1786
1787         /* no free disk inodes.
1788          */
1789         return -ENOSPC;
1790 }
1791
1792
1793 /*
1794  * NAME:        diAllocIno(imap,agno,ip)
1795  *
1796  * FUNCTION:    allocate a disk inode from the allocation group's free
1797  *              inode list, returning an error if this free list is
1798  *              empty (i.e. no iags on the list).
1799  *
1800  *              allocation occurs from the first iag on the list using
1801  *              the iag's free inode summary map to find the leftmost
1802  *              free inode in the iag. 
1803  *              
1804  * PRE CONDITION: Already have AG lock for this AG.
1805  *              
1806  * PARAMETERS:
1807  *      imap    - pointer to inode map control structure.
1808  *      agno    - allocation group.
1809  *      ip      - pointer to new inode to be filled in on successful return
1810  *                with the disk inode number allocated, its extent address
1811  *                and the start of the ag.
1812  *
1813  * RETURN VALUES:
1814  *      0       - success.
1815  *      -ENOSPC - insufficient disk resources.
1816  *      -EIO    - i/o error.
1817  */
1818 static int diAllocIno(struct inomap * imap, int agno, struct inode *ip)
1819 {
1820         int iagno, ino, rc, rem, extno, sword;
1821         struct metapage *mp;
1822         struct iag *iagp;
1823
1824         /* check if there are iags on the ag's free inode list.
1825          */
1826         if ((iagno = imap->im_agctl[agno].inofree) < 0)
1827                 return -ENOSPC;
1828
1829         /* obtain read lock on imap inode */
1830         IREAD_LOCK(imap->im_ipimap);
1831
1832         /* read the iag at the head of the list.
1833          */
1834         if ((rc = diIAGRead(imap, iagno, &mp))) {
1835                 IREAD_UNLOCK(imap->im_ipimap);
1836                 return (rc);
1837         }
1838         iagp = (struct iag *) mp->data;
1839
1840         /* better be free inodes in this iag if it is on the
1841          * list.
1842          */
1843         if (!iagp->nfreeinos) {
1844                 IREAD_UNLOCK(imap->im_ipimap);
1845                 release_metapage(mp);
1846                 jfs_error(ip->i_sb,
1847                           "diAllocIno: nfreeinos = 0, but iag on freelist");
1848                 return -EIO;
1849         }
1850
1851         /* scan the free inode summary map to find an extent
1852          * with free inodes.
1853          */
1854         for (sword = 0;; sword++) {
1855                 if (sword >= SMAPSZ) {
1856                         IREAD_UNLOCK(imap->im_ipimap);
1857                         release_metapage(mp);
1858                         jfs_error(ip->i_sb,
1859                                   "diAllocIno: free inode not found in summary map");
1860                         return -EIO;
1861                 }
1862
1863                 if (~iagp->inosmap[sword])
1864                         break;
1865         }
1866
1867         /* found a extent with free inodes. determine
1868          * the extent number.
1869          */
1870         rem = diFindFree(le32_to_cpu(iagp->inosmap[sword]), 0);
1871         if (rem >= EXTSPERSUM) {
1872                 IREAD_UNLOCK(imap->im_ipimap);
1873                 release_metapage(mp);
1874                 jfs_error(ip->i_sb, "diAllocIno: no free extent found");
1875                 return -EIO;
1876         }
1877         extno = (sword << L2EXTSPERSUM) + rem;
1878
1879         /* find the first free inode in the extent.
1880          */
1881         rem = diFindFree(le32_to_cpu(iagp->wmap[extno]), 0);
1882         if (rem >= INOSPEREXT) {
1883                 IREAD_UNLOCK(imap->im_ipimap);
1884                 release_metapage(mp);
1885                 jfs_error(ip->i_sb, "diAllocIno: free inode not found");
1886                 return -EIO;
1887         }
1888
1889         /* compute the inode number within the iag. 
1890          */
1891         ino = (extno << L2INOSPEREXT) + rem;
1892
1893         /* allocate the inode.
1894          */
1895         rc = diAllocBit(imap, iagp, ino);
1896         IREAD_UNLOCK(imap->im_ipimap);
1897         if (rc) {
1898                 release_metapage(mp);
1899                 return (rc);
1900         }
1901
1902         /* set the results of the allocation and write the iag.
1903          */
1904         diInitInode(ip, iagno, ino, extno, iagp);
1905         write_metapage(mp);
1906
1907         return (0);
1908 }
1909
1910
1911 /*
1912  * NAME:        diAllocExt(imap,agno,ip)
1913  *
1914  * FUNCTION:    add a new extent of free inodes to an iag, allocating
1915  *              an inode from this extent to satisfy the current allocation
1916  *              request.
1917  *              
1918  *              this routine first tries to find an existing iag with free
1919  *              extents through the ag free extent list.  if list is not
1920  *              empty, the head of the list will be selected as the home
1921  *              of the new extent of free inodes.  otherwise (the list is
1922  *              empty), a new iag will be allocated for the ag to contain
1923  *              the extent.
1924  *              
1925  *              once an iag has been selected, the free extent summary map
1926  *              is used to locate a free extent within the iag and diNewExt()
1927  *              is called to initialize the extent, with initialization
1928  *              including the allocation of the first inode of the extent
1929  *              for the purpose of satisfying this request.
1930  *
1931  * PARAMETERS:
1932  *      imap    - pointer to inode map control structure.
1933  *      agno    - allocation group number.
1934  *      ip      - pointer to new inode to be filled in on successful return
1935  *                with the disk inode number allocated, its extent address
1936  *                and the start of the ag.
1937  *
1938  * RETURN VALUES:
1939  *      0       - success.
1940  *      -ENOSPC - insufficient disk resources.
1941  *      -EIO    - i/o error.
1942  */
1943 static int diAllocExt(struct inomap * imap, int agno, struct inode *ip)
1944 {
1945         int rem, iagno, sword, extno, rc;
1946         struct metapage *mp;
1947         struct iag *iagp;
1948
1949         /* check if the ag has any iags with free extents.  if not,
1950          * allocate a new iag for the ag.
1951          */
1952         if ((iagno = imap->im_agctl[agno].extfree) < 0) {
1953                 /* If successful, diNewIAG will obtain the read lock on the
1954                  * imap inode.
1955                  */
1956                 if ((rc = diNewIAG(imap, &iagno, agno, &mp))) {
1957                         return (rc);
1958                 }
1959                 iagp = (struct iag *) mp->data;
1960
1961                 /* set the ag number if this a brand new iag
1962                  */
1963                 iagp->agstart =
1964                     cpu_to_le64(AGTOBLK(agno, imap->im_ipimap));
1965         } else {
1966                 /* read the iag.
1967                  */
1968                 IREAD_LOCK(imap->im_ipimap);
1969                 if ((rc = diIAGRead(imap, iagno, &mp))) {
1970                         IREAD_UNLOCK(imap->im_ipimap);
1971                         jfs_error(ip->i_sb, "diAllocExt: error reading iag");
1972                         return rc;
1973                 }
1974                 iagp = (struct iag *) mp->data;
1975         }
1976
1977         /* using the free extent summary map, find a free extent.
1978          */
1979         for (sword = 0;; sword++) {
1980                 if (sword >= SMAPSZ) {
1981                         release_metapage(mp);
1982                         IREAD_UNLOCK(imap->im_ipimap);
1983                         jfs_error(ip->i_sb,
1984                                   "diAllocExt: free ext summary map not found");
1985                         return -EIO;
1986                 }
1987                 if (~iagp->extsmap[sword])
1988                         break;
1989         }
1990
1991         /* determine the extent number of the free extent.
1992          */
1993         rem = diFindFree(le32_to_cpu(iagp->extsmap[sword]), 0);
1994         if (rem >= EXTSPERSUM) {
1995                 release_metapage(mp);
1996                 IREAD_UNLOCK(imap->im_ipimap);
1997                 jfs_error(ip->i_sb, "diAllocExt: free extent not found");
1998                 return -EIO;
1999         }
2000         extno = (sword << L2EXTSPERSUM) + rem;
2001
2002         /* initialize the new extent.
2003          */
2004         rc = diNewExt(imap, iagp, extno);
2005         IREAD_UNLOCK(imap->im_ipimap);
2006         if (rc) {
2007                 /* something bad happened.  if a new iag was allocated,
2008                  * place it back on the inode map's iag free list, and
2009                  * clear the ag number information.
2010                  */
2011                 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2012                         IAGFREE_LOCK(imap);
2013                         iagp->iagfree = cpu_to_le32(imap->im_freeiag);
2014                         imap->im_freeiag = iagno;
2015                         IAGFREE_UNLOCK(imap);
2016                 }
2017                 write_metapage(mp);
2018                 return (rc);
2019         }
2020
2021         /* set the results of the allocation and write the iag.
2022          */
2023         diInitInode(ip, iagno, extno << L2INOSPEREXT, extno, iagp);
2024
2025         write_metapage(mp);
2026
2027         return (0);
2028 }
2029
2030
2031 /*
2032  * NAME:        diAllocBit(imap,iagp,ino)
2033  *
2034  * FUNCTION:    allocate a backed inode from an iag.
2035  *
2036  *              this routine performs the mechanics of allocating a
2037  *              specified inode from a backed extent.
2038  *
2039  *              if the inode to be allocated represents the last free
2040  *              inode within the iag, the iag will be removed from the
2041  *              ag free inode list.
2042  *
2043  *              a careful update approach is used to provide consistency
2044  *              in the face of updates to multiple buffers.  under this
2045  *              approach, all required buffers are obtained before making
2046  *              any updates and are held all are updates are complete.
2047  *              
2048  * PRE CONDITION: Already have buffer lock on iagp.  Already have AG lock on
2049  *      this AG.  Must have read lock on imap inode.
2050  *
2051  * PARAMETERS:
2052  *      imap    - pointer to inode map control structure.
2053  *      iagp    - pointer to iag. 
2054  *      ino     - inode number to be allocated within the iag.
2055  *
2056  * RETURN VALUES:
2057  *      0       - success.
2058  *      -ENOSPC - insufficient disk resources.
2059  *      -EIO    - i/o error.
2060  */
2061 static int diAllocBit(struct inomap * imap, struct iag * iagp, int ino)
2062 {
2063         int extno, bitno, agno, sword, rc;
2064         struct metapage *amp = NULL, *bmp = NULL;
2065         struct iag *aiagp = 0, *biagp = 0;
2066         u32 mask;
2067
2068         /* check if this is the last free inode within the iag.
2069          * if so, it will have to be removed from the ag free
2070          * inode list, so get the iags preceeding and following
2071          * it on the list.
2072          */
2073         if (iagp->nfreeinos == cpu_to_le32(1)) {
2074                 if ((int) le32_to_cpu(iagp->inofreefwd) >= 0) {
2075                         if ((rc =
2076                              diIAGRead(imap, le32_to_cpu(iagp->inofreefwd),
2077                                        &amp)))
2078                                 return (rc);
2079                         aiagp = (struct iag *) amp->data;
2080                 }
2081
2082                 if ((int) le32_to_cpu(iagp->inofreeback) >= 0) {
2083                         if ((rc =
2084                              diIAGRead(imap,
2085                                        le32_to_cpu(iagp->inofreeback),
2086                                        &bmp))) {
2087                                 if (amp)
2088                                         release_metapage(amp);
2089                                 return (rc);
2090                         }
2091                         biagp = (struct iag *) bmp->data;
2092                 }
2093         }
2094
2095         /* get the ag number, extent number, inode number within
2096          * the extent.
2097          */
2098         agno = BLKTOAG(le64_to_cpu(iagp->agstart), JFS_SBI(imap->im_ipimap->i_sb));
2099         extno = ino >> L2INOSPEREXT;
2100         bitno = ino & (INOSPEREXT - 1);
2101
2102         /* compute the mask for setting the map.
2103          */
2104         mask = HIGHORDER >> bitno;
2105
2106         /* the inode should be free and backed.
2107          */
2108         if (((le32_to_cpu(iagp->pmap[extno]) & mask) != 0) ||
2109             ((le32_to_cpu(iagp->wmap[extno]) & mask) != 0) ||
2110             (addressPXD(&iagp->inoext[extno]) == 0)) {
2111                 if (amp)
2112                         release_metapage(amp);
2113                 if (bmp)
2114                         release_metapage(bmp);
2115
2116                 jfs_error(imap->im_ipimap->i_sb,
2117                           "diAllocBit: iag inconsistent");
2118                 return -EIO;
2119         }
2120
2121         /* mark the inode as allocated in the working map.
2122          */
2123         iagp->wmap[extno] |= cpu_to_le32(mask);
2124
2125         /* check if all inodes within the extent are now
2126          * allocated.  if so, update the free inode summary
2127          * map to reflect this.
2128          */
2129         if (iagp->wmap[extno] == ONES) {
2130                 sword = extno >> L2EXTSPERSUM;
2131                 bitno = extno & (EXTSPERSUM - 1);
2132                 iagp->inosmap[sword] |= cpu_to_le32(HIGHORDER >> bitno);
2133         }
2134
2135         /* if this was the last free inode in the iag, remove the
2136          * iag from the ag free inode list.
2137          */
2138         if (iagp->nfreeinos == cpu_to_le32(1)) {
2139                 if (amp) {
2140                         aiagp->inofreeback = iagp->inofreeback;
2141                         write_metapage(amp);
2142                 }
2143
2144                 if (bmp) {
2145                         biagp->inofreefwd = iagp->inofreefwd;
2146                         write_metapage(bmp);
2147                 } else {
2148                         imap->im_agctl[agno].inofree =
2149                             le32_to_cpu(iagp->inofreefwd);
2150                 }
2151                 iagp->inofreefwd = iagp->inofreeback = -1;
2152         }
2153
2154         /* update the free inode count at the iag, ag, inode
2155          * map levels.
2156          */
2157         iagp->nfreeinos = cpu_to_le32(le32_to_cpu(iagp->nfreeinos) - 1);
2158         imap->im_agctl[agno].numfree -= 1;
2159         atomic_dec(&imap->im_numfree);
2160
2161         return (0);
2162 }
2163
2164
2165 /*
2166  * NAME:        diNewExt(imap,iagp,extno)
2167  *
2168  * FUNCTION:    initialize a new extent of inodes for an iag, allocating
2169  *              the first inode of the extent for use for the current
2170  *              allocation request.
2171  *
2172  *              disk resources are allocated for the new extent of inodes
2173  *              and the inodes themselves are initialized to reflect their
2174  *              existence within the extent (i.e. their inode numbers and
2175  *              inode extent addresses are set) and their initial state
2176  *              (mode and link count are set to zero).
2177  *
2178  *              if the iag is new, it is not yet on an ag extent free list
2179  *              but will now be placed on this list.
2180  *
2181  *              if the allocation of the new extent causes the iag to
2182  *              have no free extent, the iag will be removed from the
2183  *              ag extent free list.
2184  *
2185  *              if the iag has no free backed inodes, it will be placed
2186  *              on the ag free inode list, since the addition of the new
2187  *              extent will now cause it to have free inodes.
2188  *
2189  *              a careful update approach is used to provide consistency
2190  *              (i.e. list consistency) in the face of updates to multiple
2191  *              buffers.  under this approach, all required buffers are
2192  *              obtained before making any updates and are held until all
2193  *              updates are complete.
2194  *              
2195  * PRE CONDITION: Already have buffer lock on iagp.  Already have AG lock on
2196  *      this AG.  Must have read lock on imap inode.
2197  *
2198  * PARAMETERS:
2199  *      imap    - pointer to inode map control structure.
2200  *      iagp    - pointer to iag. 
2201  *      extno   - extent number.
2202  *
2203  * RETURN VALUES:
2204  *      0       - success.
2205  *      -ENOSPC - insufficient disk resources.
2206  *      -EIO    - i/o error.
2207  */
2208 static int diNewExt(struct inomap * imap, struct iag * iagp, int extno)
2209 {
2210         int agno, iagno, fwd, back, freei = 0, sword, rc;
2211         struct iag *aiagp = 0, *biagp = 0, *ciagp = 0;
2212         struct metapage *amp, *bmp, *cmp, *dmp;
2213         struct inode *ipimap;
2214         s64 blkno, hint;
2215         int i, j;
2216         u32 mask;
2217         ino_t ino;
2218         struct dinode *dp;
2219         struct jfs_sb_info *sbi;
2220
2221         /* better have free extents.
2222          */
2223         if (!iagp->nfreeexts) {
2224                 jfs_error(imap->im_ipimap->i_sb, "diNewExt: no free extents");
2225                 return -EIO;
2226         }
2227
2228         /* get the inode map inode.
2229          */
2230         ipimap = imap->im_ipimap;
2231         sbi = JFS_SBI(ipimap->i_sb);
2232
2233         amp = bmp = cmp = NULL;
2234
2235         /* get the ag and iag numbers for this iag.
2236          */
2237         agno = BLKTOAG(le64_to_cpu(iagp->agstart), sbi);
2238         iagno = le32_to_cpu(iagp->iagnum);
2239
2240         /* check if this is the last free extent within the
2241          * iag.  if so, the iag must be removed from the ag
2242          * free extent list, so get the iags preceeding and
2243          * following the iag on this list.
2244          */
2245         if (iagp->nfreeexts == cpu_to_le32(1)) {
2246                 if ((fwd = le32_to_cpu(iagp->extfreefwd)) >= 0) {
2247                         if ((rc = diIAGRead(imap, fwd, &amp)))
2248                                 return (rc);
2249                         aiagp = (struct iag *) amp->data;
2250                 }
2251
2252                 if ((back = le32_to_cpu(iagp->extfreeback)) >= 0) {
2253                         if ((rc = diIAGRead(imap, back, &bmp)))
2254                                 goto error_out;
2255                         biagp = (struct iag *) bmp->data;
2256                 }
2257         } else {
2258                 /* the iag has free extents.  if all extents are free
2259                  * (as is the case for a newly allocated iag), the iag
2260                  * must be added to the ag free extent list, so get
2261                  * the iag at the head of the list in preparation for
2262                  * adding this iag to this list.
2263                  */
2264                 fwd = back = -1;
2265                 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2266                         if ((fwd = imap->im_agctl[agno].extfree) >= 0) {
2267                                 if ((rc = diIAGRead(imap, fwd, &amp)))
2268                                         goto error_out;
2269                                 aiagp = (struct iag *) amp->data;
2270                         }
2271                 }
2272         }
2273
2274         /* check if the iag has no free inodes.  if so, the iag
2275          * will have to be added to the ag free inode list, so get
2276          * the iag at the head of the list in preparation for
2277          * adding this iag to this list.  in doing this, we must
2278          * check if we already have the iag at the head of
2279          * the list in hand.
2280          */
2281         if (iagp->nfreeinos == 0) {
2282                 freei = imap->im_agctl[agno].inofree;
2283
2284                 if (freei >= 0) {
2285                         if (freei == fwd) {
2286                                 ciagp = aiagp;
2287                         } else if (freei == back) {
2288                                 ciagp = biagp;
2289                         } else {
2290                                 if ((rc = diIAGRead(imap, freei, &cmp)))
2291                                         goto error_out;
2292                                 ciagp = (struct iag *) cmp->data;
2293                         }
2294                         if (ciagp == NULL) {
2295                                 jfs_error(imap->im_ipimap->i_sb,
2296                                           "diNewExt: ciagp == NULL");
2297                                 rc = -EIO;
2298                                 goto error_out;
2299                         }
2300                 }
2301         }
2302
2303         /* allocate disk space for the inode extent.
2304          */
2305         if ((extno == 0) || (addressPXD(&iagp->inoext[extno - 1]) == 0))
2306                 hint = ((s64) agno << sbi->bmap->db_agl2size) - 1;
2307         else
2308                 hint = addressPXD(&iagp->inoext[extno - 1]) +
2309                     lengthPXD(&iagp->inoext[extno - 1]) - 1;
2310
2311         if ((rc = dbAlloc(ipimap, hint, (s64) imap->im_nbperiext, &blkno)))
2312                 goto error_out;
2313
2314         /* compute the inode number of the first inode within the
2315          * extent.
2316          */
2317         ino = (iagno << L2INOSPERIAG) + (extno << L2INOSPEREXT);
2318
2319         /* initialize the inodes within the newly allocated extent a
2320          * page at a time.
2321          */
2322         for (i = 0; i < imap->im_nbperiext; i += sbi->nbperpage) {
2323                 /* get a buffer for this page of disk inodes.
2324                  */
2325                 dmp = get_metapage(ipimap, blkno + i, PSIZE, 1);
2326                 if (dmp == NULL) {
2327                         rc = -EIO;
2328                         goto error_out;
2329                 }
2330                 dp = (struct dinode *) dmp->data;
2331
2332                 /* initialize the inode number, mode, link count and
2333                  * inode extent address.
2334                  */
2335                 for (j = 0; j < INOSPERPAGE; j++, dp++, ino++) {
2336                         dp->di_inostamp = cpu_to_le32(sbi->inostamp);
2337                         dp->di_number = cpu_to_le32(ino);
2338                         dp->di_fileset = cpu_to_le32(FILESYSTEM_I);
2339                         dp->di_mode = 0;
2340                         dp->di_nlink = 0;
2341                         PXDaddress(&(dp->di_ixpxd), blkno);
2342                         PXDlength(&(dp->di_ixpxd), imap->im_nbperiext);
2343                 }
2344                 write_metapage(dmp);
2345         }
2346
2347         /* if this is the last free extent within the iag, remove the
2348          * iag from the ag free extent list.
2349          */
2350         if (iagp->nfreeexts == cpu_to_le32(1)) {
2351                 if (fwd >= 0)
2352                         aiagp->extfreeback = iagp->extfreeback;
2353
2354                 if (back >= 0)
2355                         biagp->extfreefwd = iagp->extfreefwd;
2356                 else
2357                         imap->im_agctl[agno].extfree =
2358                             le32_to_cpu(iagp->extfreefwd);
2359
2360                 iagp->extfreefwd = iagp->extfreeback = -1;
2361         } else {
2362                 /* if the iag has all free extents (newly allocated iag),
2363                  * add the iag to the ag free extent list.
2364                  */
2365                 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2366                         if (fwd >= 0)
2367                                 aiagp->extfreeback = cpu_to_le32(iagno);
2368
2369                         iagp->extfreefwd = cpu_to_le32(fwd);
2370                         iagp->extfreeback = -1;
2371                         imap->im_agctl[agno].extfree = iagno;
2372                 }
2373         }
2374
2375         /* if the iag has no free inodes, add the iag to the
2376          * ag free inode list.
2377          */
2378         if (iagp->nfreeinos == 0) {
2379                 if (freei >= 0)
2380                         ciagp->inofreeback = cpu_to_le32(iagno);
2381
2382                 iagp->inofreefwd =
2383                     cpu_to_le32(imap->im_agctl[agno].inofree);
2384                 iagp->inofreeback = -1;
2385                 imap->im_agctl[agno].inofree = iagno;
2386         }
2387
2388         /* initialize the extent descriptor of the extent. */
2389         PXDlength(&iagp->inoext[extno], imap->im_nbperiext);
2390         PXDaddress(&iagp->inoext[extno], blkno);
2391
2392         /* initialize the working and persistent map of the extent.
2393          * the working map will be initialized such that
2394          * it indicates the first inode of the extent is allocated.
2395          */
2396         iagp->wmap[extno] = cpu_to_le32(HIGHORDER);
2397         iagp->pmap[extno] = 0;
2398
2399         /* update the free inode and free extent summary maps
2400          * for the extent to indicate the extent has free inodes
2401          * and no longer represents a free extent.
2402          */
2403         sword = extno >> L2EXTSPERSUM;
2404         mask = HIGHORDER >> (extno & (EXTSPERSUM - 1));
2405         iagp->extsmap[sword] |= cpu_to_le32(mask);
2406         iagp->inosmap[sword] &= cpu_to_le32(~mask);
2407
2408         /* update the free inode and free extent counts for the
2409          * iag.
2410          */
2411         iagp->nfreeinos = cpu_to_le32(le32_to_cpu(iagp->nfreeinos) +
2412                                       (INOSPEREXT - 1));
2413         iagp->nfreeexts = cpu_to_le32(le32_to_cpu(iagp->nfreeexts) - 1);
2414
2415         /* update the free and backed inode counts for the ag.
2416          */
2417         imap->im_agctl[agno].numfree += (INOSPEREXT - 1);
2418         imap->im_agctl[agno].numinos += INOSPEREXT;
2419
2420         /* update the free and backed inode counts for the inode map.
2421          */
2422         atomic_add(INOSPEREXT - 1, &imap->im_numfree);
2423         atomic_add(INOSPEREXT, &imap->im_numinos);
2424
2425         /* write the iags.
2426          */
2427         if (amp)
2428                 write_metapage(amp);
2429         if (bmp)
2430                 write_metapage(bmp);
2431         if (cmp)
2432                 write_metapage(cmp);
2433
2434         return (0);
2435
2436       error_out:
2437
2438         /* release the iags.
2439          */
2440         if (amp)
2441                 release_metapage(amp);
2442         if (bmp)
2443                 release_metapage(bmp);
2444         if (cmp)
2445                 release_metapage(cmp);
2446
2447         return (rc);
2448 }
2449
2450
2451 /*
2452  * NAME:        diNewIAG(imap,iagnop,agno)
2453  *
2454  * FUNCTION:    allocate a new iag for an allocation group.
2455  *              
2456  *              first tries to allocate the iag from the inode map 
2457  *              iagfree list:  
2458  *              if the list has free iags, the head of the list is removed 
2459  *              and returned to satisfy the request.
2460  *              if the inode map's iag free list is empty, the inode map
2461  *              is extended to hold a new iag. this new iag is initialized
2462  *              and returned to satisfy the request.
2463  *
2464  * PARAMETERS:
2465  *      imap    - pointer to inode map control structure.
2466  *      iagnop  - pointer to an iag number set with the number of the
2467  *                newly allocated iag upon successful return.
2468  *      agno    - allocation group number.
2469  *      bpp     - Buffer pointer to be filled in with new IAG's buffer
2470  *
2471  * RETURN VALUES:
2472  *      0       - success.
2473  *      -ENOSPC - insufficient disk resources.
2474  *      -EIO    - i/o error.
2475  *
2476  * serialization: 
2477  *      AG lock held on entry/exit;
2478  *      write lock on the map is held inside;
2479  *      read lock on the map is held on successful completion;
2480  *
2481  * note: new iag transaction: 
2482  * . synchronously write iag;
2483  * . write log of xtree and inode  of imap;
2484  * . commit;
2485  * . synchronous write of xtree (right to left, bottom to top);
2486  * . at start of logredo(): init in-memory imap with one additional iag page;
2487  * . at end of logredo(): re-read imap inode to determine
2488  *   new imap size;
2489  */
2490 static int
2491 diNewIAG(struct inomap * imap, int *iagnop, int agno, struct metapage ** mpp)
2492 {
2493         int rc;
2494         int iagno, i, xlen;
2495         struct inode *ipimap;
2496         struct super_block *sb;
2497         struct jfs_sb_info *sbi;
2498         struct metapage *mp;
2499         struct iag *iagp;
2500         s64 xaddr = 0;
2501         s64 blkno;
2502         tid_t tid;
2503 #ifdef _STILL_TO_PORT
2504         xad_t xad;
2505 #endif                          /*  _STILL_TO_PORT */
2506         struct inode *iplist[1];
2507
2508         /* pick up pointers to the inode map and mount inodes */
2509         ipimap = imap->im_ipimap;
2510         sb = ipimap->i_sb;
2511         sbi = JFS_SBI(sb);
2512
2513         /* acquire the free iag lock */
2514         IAGFREE_LOCK(imap);
2515
2516         /* if there are any iags on the inode map free iag list, 
2517          * allocate the iag from the head of the list.
2518          */
2519         if (imap->im_freeiag >= 0) {
2520                 /* pick up the iag number at the head of the list */
2521                 iagno = imap->im_freeiag;
2522
2523                 /* determine the logical block number of the iag */
2524                 blkno = IAGTOLBLK(iagno, sbi->l2nbperpage);
2525         } else {
2526                 /* no free iags. the inode map will have to be extented
2527                  * to include a new iag.
2528                  */
2529
2530                 /* acquire inode map lock */
2531                 IWRITE_LOCK(ipimap);
2532
2533                 if (ipimap->i_size >> L2PSIZE != imap->im_nextiag + 1) {
2534                         IWRITE_UNLOCK(ipimap);
2535                         IAGFREE_UNLOCK(imap);
2536                         jfs_error(imap->im_ipimap->i_sb,
2537                                   "diNewIAG: ipimap->i_size is wrong");
2538                         return -EIO;
2539                 }
2540
2541
2542                 /* get the next avaliable iag number */
2543                 iagno = imap->im_nextiag;
2544
2545                 /* make sure that we have not exceeded the maximum inode
2546                  * number limit.
2547                  */
2548                 if (iagno > (MAXIAGS - 1)) {
2549                         /* release the inode map lock */
2550                         IWRITE_UNLOCK(ipimap);
2551
2552                         rc = -ENOSPC;
2553                         goto out;
2554                 }
2555
2556                 /*
2557                  * synchronously append new iag page.
2558                  */
2559                 /* determine the logical address of iag page to append */
2560                 blkno = IAGTOLBLK(iagno, sbi->l2nbperpage);
2561
2562                 /* Allocate extent for new iag page */
2563                 xlen = sbi->nbperpage;
2564                 if ((rc = dbAlloc(ipimap, 0, (s64) xlen, &xaddr))) {
2565                         /* release the inode map lock */
2566                         IWRITE_UNLOCK(ipimap);
2567
2568                         goto out;
2569                 }
2570
2571                 /* assign a buffer for the page */
2572                 mp = get_metapage(ipimap, xaddr, PSIZE, 1);
2573                 if (!mp) {
2574                         /* Free the blocks allocated for the iag since it was
2575                          * not successfully added to the inode map
2576                          */
2577                         dbFree(ipimap, xaddr, (s64) xlen);
2578
2579                         /* release the inode map lock */
2580                         IWRITE_UNLOCK(ipimap);
2581
2582                         rc = -EIO;
2583                         goto out;
2584                 }
2585                 iagp = (struct iag *) mp->data;
2586
2587                 /* init the iag */
2588                 memset(iagp, 0, sizeof(struct iag));
2589                 iagp->iagnum = cpu_to_le32(iagno);
2590                 iagp->inofreefwd = iagp->inofreeback = -1;
2591                 iagp->extfreefwd = iagp->extfreeback = -1;
2592                 iagp->iagfree = -1;
2593                 iagp->nfreeinos = 0;
2594                 iagp->nfreeexts = cpu_to_le32(EXTSPERIAG);
2595
2596                 /* initialize the free inode summary map (free extent
2597                  * summary map initialization handled by bzero).
2598                  */
2599                 for (i = 0; i < SMAPSZ; i++)
2600                         iagp->inosmap[i] = ONES;
2601
2602                 flush_metapage(mp);
2603 #ifdef _STILL_TO_PORT
2604                 /* synchronously write the iag page */
2605                 if (bmWrite(bp)) {
2606                         /* Free the blocks allocated for the iag since it was
2607                          * not successfully added to the inode map
2608                          */
2609                         dbFree(ipimap, xaddr, (s64) xlen);
2610
2611                         /* release the inode map lock */
2612                         IWRITE_UNLOCK(ipimap);
2613
2614                         rc = -EIO;
2615                         goto out;
2616                 }
2617
2618                 /* Now the iag is on disk */
2619
2620                 /*
2621                  * start tyransaction of update of the inode map
2622                  * addressing structure pointing to the new iag page;
2623                  */
2624 #endif                          /*  _STILL_TO_PORT */
2625                 tid = txBegin(sb, COMMIT_FORCE);
2626
2627                 /* update the inode map addressing structure to point to it */
2628                 if ((rc =
2629                      xtInsert(tid, ipimap, 0, blkno, xlen, &xaddr, 0))) {
2630                         /* Free the blocks allocated for the iag since it was
2631                          * not successfully added to the inode map
2632                          */
2633                         dbFree(ipimap, xaddr, (s64) xlen);
2634
2635                         /* release the inode map lock */
2636                         IWRITE_UNLOCK(ipimap);
2637
2638                         goto out;
2639                 }
2640
2641                 /* update the inode map's inode to reflect the extension */
2642                 ipimap->i_size += PSIZE;
2643                 ipimap->i_blocks += LBLK2PBLK(sb, xlen);
2644
2645                 /*
2646                  * txCommit(COMMIT_FORCE) will synchronously write address 
2647                  * index pages and inode after commit in careful update order 
2648                  * of address index pages (right to left, bottom up);
2649                  */
2650                 iplist[0] = ipimap;
2651                 rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
2652
2653                 txEnd(tid);
2654
2655                 duplicateIXtree(sb, blkno, xlen, &xaddr);
2656
2657                 /* update the next avaliable iag number */
2658                 imap->im_nextiag += 1;
2659
2660                 /* Add the iag to the iag free list so we don't lose the iag
2661                  * if a failure happens now.
2662                  */
2663                 imap->im_freeiag = iagno;
2664
2665                 /* Until we have logredo working, we want the imap inode &
2666                  * control page to be up to date.
2667                  */
2668                 diSync(ipimap);
2669
2670                 /* release the inode map lock */
2671                 IWRITE_UNLOCK(ipimap);
2672         }
2673
2674         /* obtain read lock on map */
2675         IREAD_LOCK(ipimap);
2676
2677         /* read the iag */
2678         if ((rc = diIAGRead(imap, iagno, &mp))) {
2679                 IREAD_UNLOCK(ipimap);
2680                 rc = -EIO;
2681                 goto out;
2682         }
2683         iagp = (struct iag *) mp->data;
2684
2685         /* remove the iag from the iag free list */
2686         imap->im_freeiag = le32_to_cpu(iagp->iagfree);
2687         iagp->iagfree = -1;
2688
2689         /* set the return iag number and buffer pointer */
2690         *iagnop = iagno;
2691         *mpp = mp;
2692
2693       out:
2694         /* release the iag free lock */
2695         IAGFREE_UNLOCK(imap);
2696
2697         return (rc);
2698 }
2699
2700 /*
2701  * NAME:        diIAGRead()
2702  *
2703  * FUNCTION:    get the buffer for the specified iag within a fileset
2704  *              or aggregate inode map.
2705  *              
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.
2711  *
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.)
2716  *
2717  * RETURN VALUES:
2718  *      0       - success.
2719  *      -EIO    - i/o error.
2720  */
2721 static int diIAGRead(struct inomap * imap, int iagno, struct metapage ** mpp)
2722 {
2723         struct inode *ipimap = imap->im_ipimap;
2724         s64 blkno;
2725
2726         /* compute the logical block number of the iag. */
2727         blkno = IAGTOLBLK(iagno, JFS_SBI(ipimap->i_sb)->l2nbperpage);
2728
2729         /* read the iag. */
2730         *mpp = read_metapage(ipimap, blkno, PSIZE, 0);
2731         if (*mpp == NULL) {
2732                 return -EIO;
2733         }
2734
2735         return (0);
2736 }
2737
2738 /*
2739  * NAME:        diFindFree()
2740  *
2741  * FUNCTION:    find the first free bit in a word starting at
2742  *              the specified bit position.
2743  *
2744  * PARAMETERS:
2745  *      word    - word to be examined.
2746  *      start   - starting bit position.
2747  *
2748  * RETURN VALUES:
2749  *      bit position of first free bit in the word or 32 if
2750  *      no free bits were found.
2751  */
2752 static int diFindFree(u32 word, int start)
2753 {
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;
2761         }
2762         return (bitno);
2763 }
2764
2765 /*
2766  * NAME:        diUpdatePMap()
2767  *                                                                    
2768  * FUNCTION: Update the persistent map in an IAG for the allocation or 
2769  *      freeing of the specified inode.
2770  *                                                                    
2771  * PRE CONDITIONS: Working map has already been updated for allocate.
2772  *
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.
2778  *
2779  * RETURN VALUES: 
2780  *              0 for success
2781  */
2782 int
2783 diUpdatePMap(struct inode *ipimap,
2784              unsigned long inum, boolean_t is_free, struct tblock * tblk)
2785 {
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
2795         imap = JFS_IP(ipimap)->i_imap;
2796         /* get the iag number containing the inode */
2797         iagno = INOTOIAG(inum);
2798         /* make sure that the iag is contained within the map */
2799         if (iagno >= imap->im_nextiag) {
2800                 jfs_error(ipimap->i_sb,
2801                           "diUpdatePMap: the iag is outside the map");
2802                 return -EIO;
2803         }
2804         /* read the iag */
2805         IREAD_LOCK(ipimap);
2806         rc = diIAGRead(imap, iagno, &mp);
2807         IREAD_UNLOCK(ipimap);
2808         if (rc)
2809                 return (rc);
2810         iagp = (struct iag *) mp->data;
2811         /* get the inode number and extent number of the inode within
2812          * the iag and the inode number within the extent.
2813          */
2814         ino = inum & (INOSPERIAG - 1);
2815         extno = ino >> L2INOSPEREXT;
2816         bitno = ino & (INOSPEREXT - 1);
2817         mask = HIGHORDER >> bitno;
2818         /* 
2819          * mark the inode free in persistent map:
2820          */
2821         if (is_free == TRUE) {
2822                 /* The inode should have been allocated both in working
2823                  * map and in persistent map;
2824                  * the inode will be freed from working map at the release
2825                  * of last reference release;
2826                  */
2827                 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
2828                         jfs_error(ipimap->i_sb, 
2829                                   "diUpdatePMap: inode %ld not marked as "
2830                                   "allocated in wmap!", inum);
2831                 }
2832                 if (!(le32_to_cpu(iagp->pmap[extno]) & mask)) {
2833                         jfs_error(ipimap->i_sb,
2834                                   "diUpdatePMap: inode %ld not marked as "
2835                                   "allocated in pmap!", inum);
2836                 }
2837                 /* update the bitmap for the extent of the freed inode */
2838                 iagp->pmap[extno] &= cpu_to_le32(~mask);
2839         }
2840         /*
2841          * mark the inode allocated in persistent map:
2842          */
2843         else {
2844                 /* The inode should be already allocated in the working map
2845                  * and should be free in persistent map;
2846                  */
2847                 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
2848                         release_metapage(mp);
2849                         jfs_error(ipimap->i_sb,
2850                                   "diUpdatePMap: the inode is not allocated in "
2851                                   "the working map");
2852                         return -EIO;
2853                 }
2854                 if ((le32_to_cpu(iagp->pmap[extno]) & mask) != 0) {
2855                         release_metapage(mp);
2856                         jfs_error(ipimap->i_sb,
2857                                   "diUpdatePMap: the inode is not free in the "
2858                                   "persistent map");
2859                         return -EIO;
2860                 }
2861                 /* update the bitmap for the extent of the allocated inode */
2862                 iagp->pmap[extno] |= cpu_to_le32(mask);
2863         }
2864         /*
2865          * update iag lsn
2866          */
2867         lsn = tblk->lsn;
2868         log = JFS_SBI(tblk->sb)->log;
2869         if (mp->lsn != 0) {
2870                 /* inherit older/smaller lsn */
2871                 logdiff(difft, lsn, log);
2872                 logdiff(diffp, mp->lsn, log);
2873                 if (difft < diffp) {
2874                         mp->lsn = lsn;
2875                         /* move mp after tblock in logsync list */
2876                         LOGSYNC_LOCK(log);
2877                         list_move(&mp->synclist, &tblk->synclist);
2878                         LOGSYNC_UNLOCK(log);
2879                 }
2880                 /* inherit younger/larger clsn */
2881                 LOGSYNC_LOCK(log);
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);
2888         } else {
2889                 mp->log = log;
2890                 mp->lsn = lsn;
2891                 /* insert mp after tblock in logsync list */
2892                 LOGSYNC_LOCK(log);
2893                 log->count++;
2894                 list_add(&mp->synclist, &tblk->synclist);
2895                 mp->clsn = tblk->clsn;
2896                 LOGSYNC_UNLOCK(log);
2897         }
2898         write_metapage(mp);
2899         return (0);
2900 }
2901
2902 /*
2903  *      diExtendFS()
2904  *
2905  * function: update imap for extendfs();
2906  * 
2907  * note: AG size has been increased s.t. each k old contiguous AGs are 
2908  * coalesced into a new AG;
2909  */
2910 int diExtendFS(struct inode *ipimap, struct inode *ipbmap)
2911 {
2912         int rc, rcx = 0;
2913         struct inomap *imap = JFS_IP(ipimap)->i_imap;
2914         struct iag *iagp = 0, *hiagp = 0;
2915         struct bmap *mp = JFS_SBI(ipbmap->i_sb)->bmap;
2916         struct metapage *bp, *hbp;
2917         int i, n, head;
2918         int numinos, xnuminos = 0, xnumfree = 0;
2919         s64 agstart;
2920
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));
2924
2925         /*
2926          *      reconstruct imap 
2927          *
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).
2931          */
2932
2933         /* init per AG control information im_agctl[] */
2934         for (i = 0; i < MAXAG; i++) {
2935                 imap->im_agctl[i].inofree = -1; /* free inode list */
2936                 imap->im_agctl[i].extfree = -1; /* free extent list */
2937                 imap->im_agctl[i].numinos = 0;  /* number of backed inodes */
2938                 imap->im_agctl[i].numfree = 0;  /* number of free backed inodes */
2939         }
2940
2941         /*
2942          *      process each iag page of the map.
2943          *
2944          * rebuild AG Free Inode List, AG Free Inode Extent List;
2945          */
2946         for (i = 0; i < imap->im_nextiag; i++) {
2947                 if ((rc = diIAGRead(imap, i, &bp))) {
2948                         rcx = rc;
2949                         continue;
2950                 }
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;
2957                 }
2958
2959                 /* leave free iag in the free iag list */
2960                 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {  
2961                         release_metapage(bp);
2962                         continue;
2963                 }
2964
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;
2969
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;
2977                 }
2978
2979                 /* if any backed free inodes, insert at AG free inode list */
2980                 if ((int) le32_to_cpu(iagp->nfreeinos) > 0) {
2981                         if ((head = imap->im_agctl[n].inofree) == -1)
2982                                 iagp->inofreefwd = iagp->inofreeback = -1;
2983                         else {
2984                                 if ((rc = diIAGRead(imap, head, &hbp))) {
2985                                         rcx = rc;
2986                                         goto nextiag;
2987                                 }
2988                                 hiagp = (struct iag *) hbp->data;
2989                                 hiagp->inofreeback =
2990                                     le32_to_cpu(iagp->iagnum);
2991                                 iagp->inofreefwd = cpu_to_le32(head);
2992                                 iagp->inofreeback = -1;
2993                                 write_metapage(hbp);
2994                         }
2995
2996                         imap->im_agctl[n].inofree =
2997                             le32_to_cpu(iagp->iagnum);
2998
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);
3003                 }
3004
3005                 /* if any free extents, insert at AG free extent list */
3006                 if (le32_to_cpu(iagp->nfreeexts) > 0) {
3007                         if ((head = imap->im_agctl[n].extfree) == -1)
3008                                 iagp->extfreefwd = iagp->extfreeback = -1;
3009                         else {
3010                                 if ((rc = diIAGRead(imap, head, &hbp))) {
3011                                         rcx = rc;
3012                                         goto nextiag;
3013                                 }
3014                                 hiagp = (struct iag *) hbp->data;
3015                                 hiagp->extfreeback = iagp->iagnum;
3016                                 iagp->extfreefwd = cpu_to_le32(head);
3017                                 iagp->extfreeback = -1;
3018                                 write_metapage(hbp);
3019                         }
3020
3021                         imap->im_agctl[n].extfree =
3022                             le32_to_cpu(iagp->iagnum);
3023                 }
3024
3025               nextiag:
3026                 write_metapage(bp);
3027         }
3028
3029         if (xnuminos != atomic_read(&imap->im_numinos) ||
3030             xnumfree != atomic_read(&imap->im_numfree)) {
3031                 jfs_error(ipimap->i_sb,
3032                           "diExtendFs: numinos or numfree incorrect");
3033                 return -EIO;
3034         }
3035
3036         return rcx;
3037 }
3038
3039
3040 /*
3041  *      duplicateIXtree()
3042  *
3043  * serialization: IWRITE_LOCK held on entry/exit
3044  *
3045  * note: shadow page with regular inode (rel.2);
3046  */
3047 static void duplicateIXtree(struct super_block *sb, s64 blkno,
3048                             int xlen, s64 *xaddr)
3049 {
3050         struct jfs_superblock *j_sb;
3051         struct buffer_head *bh;
3052         struct inode *ip;
3053         tid_t tid;
3054
3055         /* if AIT2 ipmap2 is bad, do not try to update it */
3056         if (JFS_SBI(sb)->mntflag & JFS_BAD_SAIT)        /* s_flag */
3057                 return;
3058         ip = diReadSpecial(sb, FILESYSTEM_I, 1);
3059         if (ip == NULL) {
3060                 JFS_SBI(sb)->mntflag |= JFS_BAD_SAIT;
3061                 if (readSuper(sb, &bh))
3062                         return;
3063                 j_sb = (struct jfs_superblock *)bh->b_data;
3064                 j_sb->s_flag |= JFS_BAD_SAIT;
3065
3066                 mark_buffer_dirty(bh);
3067                 sync_dirty_buffer(bh);
3068                 brelse(bh);
3069                 return;
3070         }
3071
3072         /* start transaction */
3073         tid = txBegin(sb, COMMIT_FORCE);
3074         /* update the inode map addressing structure to point to it */
3075         if (xtInsert(tid, ip, 0, blkno, xlen, xaddr, 0)) {
3076                 JFS_SBI(sb)->mntflag |= JFS_BAD_SAIT;
3077                 txAbort(tid, 1);
3078                 goto cleanup;
3079
3080         }
3081         /* update the inode map's inode to reflect the extension */
3082         ip->i_size += PSIZE;
3083         ip->i_blocks += LBLK2PBLK(sb, xlen);
3084         txCommit(tid, 1, &ip, COMMIT_FORCE);
3085       cleanup:
3086         txEnd(tid);
3087         diFreeSpecial(ip);
3088 }
3089
3090 /*
3091  * NAME:        copy_from_dinode()
3092  *
3093  * FUNCTION:    Copies inode info from disk inode to in-memory inode
3094  *
3095  * RETURN VALUES:
3096  *      0       - success
3097  *      -ENOMEM - insufficient memory
3098  */
3099 static int copy_from_dinode(struct dinode * dip, struct inode *ip)
3100 {
3101         struct jfs_inode_info *jfs_ip = JFS_IP(ip);
3102         uid_t uid;
3103         gid_t gid;
3104
3105         jfs_ip->fileset = le32_to_cpu(dip->di_fileset);
3106         jfs_ip->mode2 = le32_to_cpu(dip->di_mode);
3107
3108         ip->i_mode = le32_to_cpu(dip->di_mode) & 0xffff;
3109         ip->i_nlink = le32_to_cpu(dip->di_nlink);
3110
3111         uid = le32_to_cpu(dip->di_uid);
3112         gid = le32_to_cpu(dip->di_gid);
3113         ip->i_uid = INOXID_UID(uid, gid);
3114         ip->i_gid = INOXID_GID(uid, gid);
3115         ip->i_xid = INOXID_XID(uid, gid, 0);
3116         
3117         ip->i_size = le64_to_cpu(dip->di_size);
3118         ip->i_atime.tv_sec = le32_to_cpu(dip->di_atime.tv_sec);
3119         ip->i_atime.tv_nsec = le32_to_cpu(dip->di_atime.tv_nsec);
3120         ip->i_mtime.tv_sec = le32_to_cpu(dip->di_mtime.tv_sec);
3121         ip->i_mtime.tv_nsec = le32_to_cpu(dip->di_mtime.tv_nsec);
3122         ip->i_ctime.tv_sec = le32_to_cpu(dip->di_ctime.tv_sec);
3123         ip->i_ctime.tv_nsec = le32_to_cpu(dip->di_ctime.tv_nsec);
3124         ip->i_blksize = ip->i_sb->s_blocksize;
3125         ip->i_blocks = LBLK2PBLK(ip->i_sb, le64_to_cpu(dip->di_nblocks));
3126         ip->i_generation = le32_to_cpu(dip->di_gen);
3127
3128         jfs_ip->ixpxd = dip->di_ixpxd;  /* in-memory pxd's are little-endian */
3129         jfs_ip->acl = dip->di_acl;      /* as are dxd's */
3130         jfs_ip->ea = dip->di_ea;
3131         jfs_ip->next_index = le32_to_cpu(dip->di_next_index);
3132         jfs_ip->otime = le32_to_cpu(dip->di_otime.tv_sec);
3133         jfs_ip->acltype = le32_to_cpu(dip->di_acltype);
3134
3135         if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode)) {
3136                 jfs_ip->dev = le32_to_cpu(dip->di_rdev);
3137                 ip->i_rdev = new_decode_dev(jfs_ip->dev);
3138         }
3139
3140         if (S_ISDIR(ip->i_mode)) {
3141                 memcpy(&jfs_ip->i_dirtable, &dip->di_dirtable, 384);
3142         } else if (S_ISREG(ip->i_mode) || S_ISLNK(ip->i_mode)) {
3143                 memcpy(&jfs_ip->i_xtroot, &dip->di_xtroot, 288);
3144         } else
3145                 memcpy(&jfs_ip->i_inline_ea, &dip->di_inlineea, 128);
3146
3147         /* Zero the in-memory-only stuff */
3148         jfs_ip->cflag = 0;
3149         jfs_ip->btindex = 0;
3150         jfs_ip->btorder = 0;
3151         jfs_ip->bxflag = 0;
3152         jfs_ip->blid = 0;
3153         jfs_ip->atlhead = 0;
3154         jfs_ip->atltail = 0;
3155         jfs_ip->xtlid = 0;
3156         return (0);
3157 }
3158
3159 /*
3160  * NAME:        copy_to_dinode()
3161  *
3162  * FUNCTION:    Copies inode info from in-memory inode to disk inode
3163  */
3164 static void copy_to_dinode(struct dinode * dip, struct inode *ip)
3165 {
3166         struct jfs_inode_info *jfs_ip = JFS_IP(ip);
3167         uid_t uid;
3168         gid_t gid;
3169
3170         dip->di_fileset = cpu_to_le32(jfs_ip->fileset);
3171         dip->di_inostamp = cpu_to_le32(JFS_SBI(ip->i_sb)->inostamp);
3172         dip->di_number = cpu_to_le32(ip->i_ino);
3173         dip->di_gen = cpu_to_le32(ip->i_generation);
3174         dip->di_size = cpu_to_le64(ip->i_size);
3175         dip->di_nblocks = cpu_to_le64(PBLK2LBLK(ip->i_sb, ip->i_blocks));
3176         dip->di_nlink = cpu_to_le32(ip->i_nlink);
3177
3178         uid = XIDINO_UID(ip->i_uid, ip->i_xid);
3179         gid = XIDINO_GID(ip->i_gid, ip->i_xid);
3180         dip->di_uid = cpu_to_le32(uid);
3181         dip->di_gid = cpu_to_le32(gid);
3182         /*
3183          * mode2 is only needed for storing the higher order bits.
3184          * Trust i_mode for the lower order ones
3185          */
3186         dip->di_mode = cpu_to_le32((jfs_ip->mode2 & 0xffff0000) | ip->i_mode);
3187         dip->di_atime.tv_sec = cpu_to_le32(ip->i_atime.tv_sec);
3188         dip->di_atime.tv_nsec = cpu_to_le32(ip->i_atime.tv_nsec);
3189         dip->di_ctime.tv_sec = cpu_to_le32(ip->i_ctime.tv_sec);
3190         dip->di_ctime.tv_nsec = cpu_to_le32(ip->i_ctime.tv_nsec);
3191         dip->di_mtime.tv_sec = cpu_to_le32(ip->i_mtime.tv_sec);
3192         dip->di_mtime.tv_nsec = cpu_to_le32(ip->i_mtime.tv_nsec);
3193         dip->di_ixpxd = jfs_ip->ixpxd;  /* in-memory pxd's are little-endian */
3194         dip->di_acl = jfs_ip->acl;      /* as are dxd's */
3195         dip->di_ea = jfs_ip->ea;
3196         dip->di_next_index = cpu_to_le32(jfs_ip->next_index);
3197         dip->di_otime.tv_sec = cpu_to_le32(jfs_ip->otime);
3198         dip->di_otime.tv_nsec = 0;
3199         dip->di_acltype = cpu_to_le32(jfs_ip->acltype);
3200         if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode))
3201                 dip->di_rdev = cpu_to_le32(jfs_ip->dev);
3202 }
3203
3204 #ifdef  _JFS_DEBUG_IMAP
3205 /*
3206  *      DBGdiInit()
3207  */
3208 static void *DBGdiInit(struct inomap * imap)
3209 {
3210         u32 *dimap;
3211         int size;
3212         size = 64 * 1024;
3213         if ((dimap = (u32 *) xmalloc(size, L2PSIZE, kernel_heap)) == NULL)
3214                 assert(0);
3215         bzero((void *) dimap, size);
3216         imap->im_DBGdimap = dimap;
3217 }
3218
3219 /*
3220  *      DBGdiAlloc()
3221  */
3222 static void DBGdiAlloc(struct inomap * imap, ino_t ino)
3223 {
3224         u32 *dimap = imap->im_DBGdimap;
3225         int w, b;
3226         u32 m;
3227         w = ino >> 5;
3228         b = ino & 31;
3229         m = 0x80000000 >> b;
3230         assert(w < 64 * 256);
3231         if (dimap[w] & m) {
3232                 printk("DEBUG diAlloc: duplicate alloc ino:0x%x\n", ino);
3233         }
3234         dimap[w] |= m;
3235 }
3236
3237 /*
3238  *      DBGdiFree()
3239  */
3240 static void DBGdiFree(struct inomap * imap, ino_t ino)
3241 {
3242         u32 *dimap = imap->im_DBGdimap;
3243         int w, b;
3244         u32 m;
3245         w = ino >> 5;
3246         b = ino & 31;
3247         m = 0x80000000 >> b;
3248         assert(w < 64 * 256);
3249         if ((dimap[w] & m) == 0) {
3250                 printk("DEBUG diFree: duplicate free ino:0x%x\n", ino);
3251         }
3252         dimap[w] &= ~m;
3253 }
3254
3255 static void dump_cp(struct inomap * ipimap, char *function, int line)
3256 {
3257         printk("\n* ********* *\nControl Page %s %d\n", function, line);
3258         printk("FreeIAG %d\tNextIAG %d\n", ipimap->im_freeiag,
3259                ipimap->im_nextiag);
3260         printk("NumInos %d\tNumFree %d\n",
3261                atomic_read(&ipimap->im_numinos),
3262                atomic_read(&ipimap->im_numfree));
3263         printk("AG InoFree %d\tAG ExtFree %d\n",
3264                ipimap->im_agctl[0].inofree, ipimap->im_agctl[0].extfree);
3265         printk("AG NumInos %d\tAG NumFree %d\n",
3266                ipimap->im_agctl[0].numinos, ipimap->im_agctl[0].numfree);
3267 }
3268
3269 static void dump_iag(struct iag * iag, char *function, int line)
3270 {
3271         printk("\n* ********* *\nIAG %s %d\n", function, line);
3272         printk("IagNum %d\tIAG Free %d\n", le32_to_cpu(iag->iagnum),
3273                le32_to_cpu(iag->iagfree));
3274         printk("InoFreeFwd %d\tInoFreeBack %d\n",
3275                le32_to_cpu(iag->inofreefwd),
3276                le32_to_cpu(iag->inofreeback));
3277         printk("ExtFreeFwd %d\tExtFreeBack %d\n",
3278                le32_to_cpu(iag->extfreefwd),
3279                le32_to_cpu(iag->extfreeback));
3280         printk("NFreeInos %d\tNFreeExts %d\n", le32_to_cpu(iag->nfreeinos),
3281                le32_to_cpu(iag->nfreeexts));
3282 }
3283 #endif                          /* _JFS_DEBUG_IMAP */