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