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