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