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