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