[IRDA]: Avoid a label defined but not used warning in irda_init()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / jfs / jfs_imap.c
blob3870ba8b9086b8f2c1c481eaf6a1eb2c79497238
1 /*
2 * Copyright (C) International Business Machines Corp., 2000-2004
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * jfs_imap.c: inode allocation map manager
22 * Serialization:
23 * Each AG has a simple lock which is used to control the serialization of
24 * the AG level lists. This lock should be taken first whenever an AG
25 * level list will be modified or accessed.
27 * Each IAG is locked by obtaining the buffer for the IAG page.
29 * There is also a inode lock for the inode map inode. A read lock needs to
30 * be taken whenever an IAG is read from the map or the global level
31 * information is read. A write lock needs to be taken whenever the global
32 * level information is modified or an atomic operation needs to be used.
34 * If more than one IAG is read at one time, the read lock may not
35 * be given up until all of the IAG's are read. Otherwise, a deadlock
36 * may occur when trying to obtain the read lock while another thread
37 * holding the read lock is waiting on the IAG already being held.
39 * The control page of the inode map is read into memory by diMount().
40 * Thereafter it should only be modified in memory and then it will be
41 * written out when the filesystem is unmounted by diUnmount().
44 #include <linux/fs.h>
45 #include <linux/buffer_head.h>
46 #include <linux/pagemap.h>
47 #include <linux/quotaops.h>
49 #include "jfs_incore.h"
50 #include "jfs_inode.h"
51 #include "jfs_filsys.h"
52 #include "jfs_dinode.h"
53 #include "jfs_dmap.h"
54 #include "jfs_imap.h"
55 #include "jfs_metapage.h"
56 #include "jfs_superblock.h"
57 #include "jfs_debug.h"
60 * __mark_inode_dirty expects inodes to be hashed. Since we don't want
61 * special inodes in the fileset inode space, we hash them to a dummy head
63 static HLIST_HEAD(aggregate_hash);
66 * imap locks
68 /* iag free list lock */
69 #define IAGFREE_LOCK_INIT(imap) mutex_init(&imap->im_freelock)
70 #define IAGFREE_LOCK(imap) mutex_lock(&imap->im_freelock)
71 #define IAGFREE_UNLOCK(imap) mutex_unlock(&imap->im_freelock)
73 /* per ag iag list locks */
74 #define AG_LOCK_INIT(imap,index) mutex_init(&(imap->im_aglock[index]))
75 #define AG_LOCK(imap,agno) mutex_lock(&imap->im_aglock[agno])
76 #define AG_UNLOCK(imap,agno) mutex_unlock(&imap->im_aglock[agno])
79 * forward references
81 static int diAllocAG(struct inomap *, int, bool, struct inode *);
82 static int diAllocAny(struct inomap *, int, bool, struct inode *);
83 static int diAllocBit(struct inomap *, struct iag *, int);
84 static int diAllocExt(struct inomap *, int, struct inode *);
85 static int diAllocIno(struct inomap *, int, struct inode *);
86 static int diFindFree(u32, int);
87 static int diNewExt(struct inomap *, struct iag *, int);
88 static int diNewIAG(struct inomap *, int *, int, struct metapage **);
89 static void duplicateIXtree(struct super_block *, s64, int, s64 *);
91 static int diIAGRead(struct inomap * imap, int, struct metapage **);
92 static int copy_from_dinode(struct dinode *, struct inode *);
93 static void copy_to_dinode(struct dinode *, struct inode *);
96 * NAME: diMount()
98 * FUNCTION: initialize the incore inode map control structures for
99 * a fileset or aggregate init time.
101 * the inode map's control structure (dinomap) is
102 * brought in from disk and placed in virtual memory.
104 * PARAMETERS:
105 * ipimap - pointer to inode map inode for the aggregate or fileset.
107 * RETURN VALUES:
108 * 0 - success
109 * -ENOMEM - insufficient free virtual memory.
110 * -EIO - i/o error.
112 int diMount(struct inode *ipimap)
114 struct inomap *imap;
115 struct metapage *mp;
116 int index;
117 struct dinomap_disk *dinom_le;
120 * allocate/initialize the in-memory inode map control structure
122 /* allocate the in-memory inode map control structure. */
123 imap = kmalloc(sizeof(struct inomap), GFP_KERNEL);
124 if (imap == NULL) {
125 jfs_err("diMount: kmalloc returned NULL!");
126 return -ENOMEM;
129 /* read the on-disk inode map control structure. */
131 mp = read_metapage(ipimap,
132 IMAPBLKNO << JFS_SBI(ipimap->i_sb)->l2nbperpage,
133 PSIZE, 0);
134 if (mp == NULL) {
135 kfree(imap);
136 return -EIO;
139 /* copy the on-disk version to the in-memory version. */
140 dinom_le = (struct dinomap_disk *) mp->data;
141 imap->im_freeiag = le32_to_cpu(dinom_le->in_freeiag);
142 imap->im_nextiag = le32_to_cpu(dinom_le->in_nextiag);
143 atomic_set(&imap->im_numinos, le32_to_cpu(dinom_le->in_numinos));
144 atomic_set(&imap->im_numfree, le32_to_cpu(dinom_le->in_numfree));
145 imap->im_nbperiext = le32_to_cpu(dinom_le->in_nbperiext);
146 imap->im_l2nbperiext = le32_to_cpu(dinom_le->in_l2nbperiext);
147 for (index = 0; index < MAXAG; index++) {
148 imap->im_agctl[index].inofree =
149 le32_to_cpu(dinom_le->in_agctl[index].inofree);
150 imap->im_agctl[index].extfree =
151 le32_to_cpu(dinom_le->in_agctl[index].extfree);
152 imap->im_agctl[index].numinos =
153 le32_to_cpu(dinom_le->in_agctl[index].numinos);
154 imap->im_agctl[index].numfree =
155 le32_to_cpu(dinom_le->in_agctl[index].numfree);
158 /* release the buffer. */
159 release_metapage(mp);
162 * allocate/initialize inode allocation map locks
164 /* allocate and init iag free list lock */
165 IAGFREE_LOCK_INIT(imap);
167 /* allocate and init ag list locks */
168 for (index = 0; index < MAXAG; index++) {
169 AG_LOCK_INIT(imap, index);
172 /* bind the inode map inode and inode map control structure
173 * to each other.
175 imap->im_ipimap = ipimap;
176 JFS_IP(ipimap)->i_imap = imap;
178 return (0);
183 * NAME: diUnmount()
185 * FUNCTION: write to disk the incore inode map control structures for
186 * a fileset or aggregate at unmount time.
188 * PARAMETERS:
189 * ipimap - pointer to inode map inode for the aggregate or fileset.
191 * RETURN VALUES:
192 * 0 - success
193 * -ENOMEM - insufficient free virtual memory.
194 * -EIO - i/o error.
196 int diUnmount(struct inode *ipimap, int mounterror)
198 struct inomap *imap = JFS_IP(ipimap)->i_imap;
201 * update the on-disk inode map control structure
204 if (!(mounterror || isReadOnly(ipimap)))
205 diSync(ipimap);
208 * Invalidate the page cache buffers
210 truncate_inode_pages(ipimap->i_mapping, 0);
213 * free in-memory control structure
215 kfree(imap);
217 return (0);
222 * diSync()
224 int diSync(struct inode *ipimap)
226 struct dinomap_disk *dinom_le;
227 struct inomap *imp = JFS_IP(ipimap)->i_imap;
228 struct metapage *mp;
229 int index;
232 * write imap global conrol page
234 /* read the on-disk inode map control structure */
235 mp = get_metapage(ipimap,
236 IMAPBLKNO << JFS_SBI(ipimap->i_sb)->l2nbperpage,
237 PSIZE, 0);
238 if (mp == NULL) {
239 jfs_err("diSync: get_metapage failed!");
240 return -EIO;
243 /* copy the in-memory version to the on-disk version */
244 dinom_le = (struct dinomap_disk *) mp->data;
245 dinom_le->in_freeiag = cpu_to_le32(imp->im_freeiag);
246 dinom_le->in_nextiag = cpu_to_le32(imp->im_nextiag);
247 dinom_le->in_numinos = cpu_to_le32(atomic_read(&imp->im_numinos));
248 dinom_le->in_numfree = cpu_to_le32(atomic_read(&imp->im_numfree));
249 dinom_le->in_nbperiext = cpu_to_le32(imp->im_nbperiext);
250 dinom_le->in_l2nbperiext = cpu_to_le32(imp->im_l2nbperiext);
251 for (index = 0; index < MAXAG; index++) {
252 dinom_le->in_agctl[index].inofree =
253 cpu_to_le32(imp->im_agctl[index].inofree);
254 dinom_le->in_agctl[index].extfree =
255 cpu_to_le32(imp->im_agctl[index].extfree);
256 dinom_le->in_agctl[index].numinos =
257 cpu_to_le32(imp->im_agctl[index].numinos);
258 dinom_le->in_agctl[index].numfree =
259 cpu_to_le32(imp->im_agctl[index].numfree);
262 /* write out the control structure */
263 write_metapage(mp);
266 * write out dirty pages of imap
268 filemap_write_and_wait(ipimap->i_mapping);
270 diWriteSpecial(ipimap, 0);
272 return (0);
277 * NAME: diRead()
279 * FUNCTION: initialize an incore inode from disk.
281 * on entry, the specifed incore inode should itself
282 * specify the disk inode number corresponding to the
283 * incore inode (i.e. i_number should be initialized).
285 * this routine handles incore inode initialization for
286 * both "special" and "regular" inodes. special inodes
287 * are those required early in the mount process and
288 * require special handling since much of the file system
289 * is not yet initialized. these "special" inodes are
290 * identified by a NULL inode map inode pointer and are
291 * actually initialized by a call to diReadSpecial().
293 * for regular inodes, the iag describing the disk inode
294 * is read from disk to determine the inode extent address
295 * for the disk inode. with the inode extent address in
296 * hand, the page of the extent that contains the disk
297 * inode is read and the disk inode is copied to the
298 * incore inode.
300 * PARAMETERS:
301 * ip - pointer to incore inode to be initialized from disk.
303 * RETURN VALUES:
304 * 0 - success
305 * -EIO - i/o error.
306 * -ENOMEM - insufficient memory
309 int diRead(struct inode *ip)
311 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
312 int iagno, ino, extno, rc;
313 struct inode *ipimap;
314 struct dinode *dp;
315 struct iag *iagp;
316 struct metapage *mp;
317 s64 blkno, agstart;
318 struct inomap *imap;
319 int block_offset;
320 int inodes_left;
321 unsigned long pageno;
322 int rel_inode;
324 jfs_info("diRead: ino = %ld", ip->i_ino);
326 ipimap = sbi->ipimap;
327 JFS_IP(ip)->ipimap = ipimap;
329 /* determine the iag number for this inode (number) */
330 iagno = INOTOIAG(ip->i_ino);
332 /* read the iag */
333 imap = JFS_IP(ipimap)->i_imap;
334 IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
335 rc = diIAGRead(imap, iagno, &mp);
336 IREAD_UNLOCK(ipimap);
337 if (rc) {
338 jfs_err("diRead: diIAGRead returned %d", rc);
339 return (rc);
342 iagp = (struct iag *) mp->data;
344 /* determine inode extent that holds the disk inode */
345 ino = ip->i_ino & (INOSPERIAG - 1);
346 extno = ino >> L2INOSPEREXT;
348 if ((lengthPXD(&iagp->inoext[extno]) != imap->im_nbperiext) ||
349 (addressPXD(&iagp->inoext[extno]) == 0)) {
350 release_metapage(mp);
351 return -ESTALE;
354 /* get disk block number of the page within the inode extent
355 * that holds the disk inode.
357 blkno = INOPBLK(&iagp->inoext[extno], ino, sbi->l2nbperpage);
359 /* get the ag for the iag */
360 agstart = le64_to_cpu(iagp->agstart);
362 release_metapage(mp);
364 rel_inode = (ino & (INOSPERPAGE - 1));
365 pageno = blkno >> sbi->l2nbperpage;
367 if ((block_offset = ((u32) blkno & (sbi->nbperpage - 1)))) {
369 * OS/2 didn't always align inode extents on page boundaries
371 inodes_left =
372 (sbi->nbperpage - block_offset) << sbi->l2niperblk;
374 if (rel_inode < inodes_left)
375 rel_inode += block_offset << sbi->l2niperblk;
376 else {
377 pageno += 1;
378 rel_inode -= inodes_left;
382 /* read the page of disk inode */
383 mp = read_metapage(ipimap, pageno << sbi->l2nbperpage, PSIZE, 1);
384 if (mp == 0) {
385 jfs_err("diRead: read_metapage failed");
386 return -EIO;
389 /* locate the disk inode requested */
390 dp = (struct dinode *) mp->data;
391 dp += rel_inode;
393 if (ip->i_ino != le32_to_cpu(dp->di_number)) {
394 jfs_error(ip->i_sb, "diRead: i_ino != di_number");
395 rc = -EIO;
396 } else if (le32_to_cpu(dp->di_nlink) == 0)
397 rc = -ESTALE;
398 else
399 /* copy the disk inode to the in-memory inode */
400 rc = copy_from_dinode(dp, ip);
402 release_metapage(mp);
404 /* set the ag for the inode */
405 JFS_IP(ip)->agno = BLKTOAG(agstart, sbi);
406 JFS_IP(ip)->active_ag = -1;
408 return (rc);
413 * NAME: diReadSpecial()
415 * FUNCTION: initialize a 'special' inode from disk.
417 * this routines handles aggregate level inodes. The
418 * inode cache cannot differentiate between the
419 * aggregate inodes and the filesystem inodes, so we
420 * handle these here. We don't actually use the aggregate
421 * inode map, since these inodes are at a fixed location
422 * and in some cases the aggregate inode map isn't initialized
423 * yet.
425 * PARAMETERS:
426 * sb - filesystem superblock
427 * inum - aggregate inode number
428 * secondary - 1 if secondary aggregate inode table
430 * RETURN VALUES:
431 * new inode - success
432 * NULL - i/o error.
434 struct inode *diReadSpecial(struct super_block *sb, ino_t inum, int secondary)
436 struct jfs_sb_info *sbi = JFS_SBI(sb);
437 uint address;
438 struct dinode *dp;
439 struct inode *ip;
440 struct metapage *mp;
442 ip = new_inode(sb);
443 if (ip == NULL) {
444 jfs_err("diReadSpecial: new_inode returned NULL!");
445 return ip;
448 if (secondary) {
449 address = addressPXD(&sbi->ait2) >> sbi->l2nbperpage;
450 JFS_IP(ip)->ipimap = sbi->ipaimap2;
451 } else {
452 address = AITBL_OFF >> L2PSIZE;
453 JFS_IP(ip)->ipimap = sbi->ipaimap;
456 ASSERT(inum < INOSPEREXT);
458 ip->i_ino = inum;
460 address += inum >> 3; /* 8 inodes per 4K page */
462 /* read the page of fixed disk inode (AIT) in raw mode */
463 mp = read_metapage(ip, address << sbi->l2nbperpage, PSIZE, 1);
464 if (mp == NULL) {
465 ip->i_nlink = 1; /* Don't want iput() deleting it */
466 iput(ip);
467 return (NULL);
470 /* get the pointer to the disk inode of interest */
471 dp = (struct dinode *) (mp->data);
472 dp += inum % 8; /* 8 inodes per 4K page */
474 /* copy on-disk inode to in-memory inode */
475 if ((copy_from_dinode(dp, ip)) != 0) {
476 /* handle bad return by returning NULL for ip */
477 ip->i_nlink = 1; /* Don't want iput() deleting it */
478 iput(ip);
479 /* release the page */
480 release_metapage(mp);
481 return (NULL);
485 ip->i_mapping->a_ops = &jfs_metapage_aops;
486 mapping_set_gfp_mask(ip->i_mapping, GFP_NOFS);
488 /* Allocations to metadata inodes should not affect quotas */
489 ip->i_flags |= S_NOQUOTA;
491 if ((inum == FILESYSTEM_I) && (JFS_IP(ip)->ipimap == sbi->ipaimap)) {
492 sbi->gengen = le32_to_cpu(dp->di_gengen);
493 sbi->inostamp = le32_to_cpu(dp->di_inostamp);
496 /* release the page */
497 release_metapage(mp);
499 hlist_add_head(&ip->i_hash, &aggregate_hash);
501 return (ip);
505 * NAME: diWriteSpecial()
507 * FUNCTION: Write the special inode to disk
509 * PARAMETERS:
510 * ip - special inode
511 * secondary - 1 if secondary aggregate inode table
513 * RETURN VALUES: none
516 void diWriteSpecial(struct inode *ip, int secondary)
518 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
519 uint address;
520 struct dinode *dp;
521 ino_t inum = ip->i_ino;
522 struct metapage *mp;
524 if (secondary)
525 address = addressPXD(&sbi->ait2) >> sbi->l2nbperpage;
526 else
527 address = AITBL_OFF >> L2PSIZE;
529 ASSERT(inum < INOSPEREXT);
531 address += inum >> 3; /* 8 inodes per 4K page */
533 /* read the page of fixed disk inode (AIT) in raw mode */
534 mp = read_metapage(ip, address << sbi->l2nbperpage, PSIZE, 1);
535 if (mp == NULL) {
536 jfs_err("diWriteSpecial: failed to read aggregate inode "
537 "extent!");
538 return;
541 /* get the pointer to the disk inode of interest */
542 dp = (struct dinode *) (mp->data);
543 dp += inum % 8; /* 8 inodes per 4K page */
545 /* copy on-disk inode to in-memory inode */
546 copy_to_dinode(dp, ip);
547 memcpy(&dp->di_xtroot, &JFS_IP(ip)->i_xtroot, 288);
549 if (inum == FILESYSTEM_I)
550 dp->di_gengen = cpu_to_le32(sbi->gengen);
552 /* write the page */
553 write_metapage(mp);
557 * NAME: diFreeSpecial()
559 * FUNCTION: Free allocated space for special inode
561 void diFreeSpecial(struct inode *ip)
563 if (ip == NULL) {
564 jfs_err("diFreeSpecial called with NULL ip!");
565 return;
567 filemap_write_and_wait(ip->i_mapping);
568 truncate_inode_pages(ip->i_mapping, 0);
569 iput(ip);
575 * NAME: diWrite()
577 * FUNCTION: write the on-disk inode portion of the in-memory inode
578 * to its corresponding on-disk inode.
580 * on entry, the specifed incore inode should itself
581 * specify the disk inode number corresponding to the
582 * incore inode (i.e. i_number should be initialized).
584 * the inode contains the inode extent address for the disk
585 * inode. with the inode extent address in hand, the
586 * page of the extent that contains the disk inode is
587 * read and the disk inode portion of the incore inode
588 * is copied to the disk inode.
590 * PARAMETERS:
591 * tid - transacation id
592 * ip - pointer to incore inode to be written to the inode extent.
594 * RETURN VALUES:
595 * 0 - success
596 * -EIO - i/o error.
598 int diWrite(tid_t tid, struct inode *ip)
600 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
601 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
602 int rc = 0;
603 s32 ino;
604 struct dinode *dp;
605 s64 blkno;
606 int block_offset;
607 int inodes_left;
608 struct metapage *mp;
609 unsigned long pageno;
610 int rel_inode;
611 int dioffset;
612 struct inode *ipimap;
613 uint type;
614 lid_t lid;
615 struct tlock *ditlck, *tlck;
616 struct linelock *dilinelock, *ilinelock;
617 struct lv *lv;
618 int n;
620 ipimap = jfs_ip->ipimap;
622 ino = ip->i_ino & (INOSPERIAG - 1);
624 if (!addressPXD(&(jfs_ip->ixpxd)) ||
625 (lengthPXD(&(jfs_ip->ixpxd)) !=
626 JFS_IP(ipimap)->i_imap->im_nbperiext)) {
627 jfs_error(ip->i_sb, "diWrite: ixpxd invalid");
628 return -EIO;
632 * read the page of disk inode containing the specified inode:
634 /* compute the block address of the page */
635 blkno = INOPBLK(&(jfs_ip->ixpxd), ino, sbi->l2nbperpage);
637 rel_inode = (ino & (INOSPERPAGE - 1));
638 pageno = blkno >> sbi->l2nbperpage;
640 if ((block_offset = ((u32) blkno & (sbi->nbperpage - 1)))) {
642 * OS/2 didn't always align inode extents on page boundaries
644 inodes_left =
645 (sbi->nbperpage - block_offset) << sbi->l2niperblk;
647 if (rel_inode < inodes_left)
648 rel_inode += block_offset << sbi->l2niperblk;
649 else {
650 pageno += 1;
651 rel_inode -= inodes_left;
654 /* read the page of disk inode */
655 retry:
656 mp = read_metapage(ipimap, pageno << sbi->l2nbperpage, PSIZE, 1);
657 if (mp == 0)
658 return -EIO;
660 /* get the pointer to the disk inode */
661 dp = (struct dinode *) mp->data;
662 dp += rel_inode;
664 dioffset = (ino & (INOSPERPAGE - 1)) << L2DISIZE;
667 * acquire transaction lock on the on-disk inode;
668 * N.B. tlock is acquired on ipimap not ip;
670 if ((ditlck =
671 txLock(tid, ipimap, mp, tlckINODE | tlckENTRY)) == NULL)
672 goto retry;
673 dilinelock = (struct linelock *) & ditlck->lock;
676 * copy btree root from in-memory inode to on-disk inode
678 * (tlock is taken from inline B+-tree root in in-memory
679 * inode when the B+-tree root is updated, which is pointed
680 * by jfs_ip->blid as well as being on tx tlock list)
682 * further processing of btree root is based on the copy
683 * in in-memory inode, where txLog() will log from, and,
684 * for xtree root, txUpdateMap() will update map and reset
685 * XAD_NEW bit;
688 if (S_ISDIR(ip->i_mode) && (lid = jfs_ip->xtlid)) {
690 * This is the special xtree inside the directory for storing
691 * the directory table
693 xtpage_t *p, *xp;
694 xad_t *xad;
696 jfs_ip->xtlid = 0;
697 tlck = lid_to_tlock(lid);
698 assert(tlck->type & tlckXTREE);
699 tlck->type |= tlckBTROOT;
700 tlck->mp = mp;
701 ilinelock = (struct linelock *) & tlck->lock;
704 * copy xtree root from inode to dinode:
706 p = &jfs_ip->i_xtroot;
707 xp = (xtpage_t *) &dp->di_dirtable;
708 lv = ilinelock->lv;
709 for (n = 0; n < ilinelock->index; n++, lv++) {
710 memcpy(&xp->xad[lv->offset], &p->xad[lv->offset],
711 lv->length << L2XTSLOTSIZE);
714 /* reset on-disk (metadata page) xtree XAD_NEW bit */
715 xad = &xp->xad[XTENTRYSTART];
716 for (n = XTENTRYSTART;
717 n < le16_to_cpu(xp->header.nextindex); n++, xad++)
718 if (xad->flag & (XAD_NEW | XAD_EXTENDED))
719 xad->flag &= ~(XAD_NEW | XAD_EXTENDED);
722 if ((lid = jfs_ip->blid) == 0)
723 goto inlineData;
724 jfs_ip->blid = 0;
726 tlck = lid_to_tlock(lid);
727 type = tlck->type;
728 tlck->type |= tlckBTROOT;
729 tlck->mp = mp;
730 ilinelock = (struct linelock *) & tlck->lock;
733 * regular file: 16 byte (XAD slot) granularity
735 if (type & tlckXTREE) {
736 xtpage_t *p, *xp;
737 xad_t *xad;
740 * copy xtree root from inode to dinode:
742 p = &jfs_ip->i_xtroot;
743 xp = &dp->di_xtroot;
744 lv = ilinelock->lv;
745 for (n = 0; n < ilinelock->index; n++, lv++) {
746 memcpy(&xp->xad[lv->offset], &p->xad[lv->offset],
747 lv->length << L2XTSLOTSIZE);
750 /* reset on-disk (metadata page) xtree XAD_NEW bit */
751 xad = &xp->xad[XTENTRYSTART];
752 for (n = XTENTRYSTART;
753 n < le16_to_cpu(xp->header.nextindex); n++, xad++)
754 if (xad->flag & (XAD_NEW | XAD_EXTENDED))
755 xad->flag &= ~(XAD_NEW | XAD_EXTENDED);
758 * directory: 32 byte (directory entry slot) granularity
760 else if (type & tlckDTREE) {
761 dtpage_t *p, *xp;
764 * copy dtree root from inode to dinode:
766 p = (dtpage_t *) &jfs_ip->i_dtroot;
767 xp = (dtpage_t *) & dp->di_dtroot;
768 lv = ilinelock->lv;
769 for (n = 0; n < ilinelock->index; n++, lv++) {
770 memcpy(&xp->slot[lv->offset], &p->slot[lv->offset],
771 lv->length << L2DTSLOTSIZE);
773 } else {
774 jfs_err("diWrite: UFO tlock");
777 inlineData:
779 * copy inline symlink from in-memory inode to on-disk inode
781 if (S_ISLNK(ip->i_mode) && ip->i_size < IDATASIZE) {
782 lv = & dilinelock->lv[dilinelock->index];
783 lv->offset = (dioffset + 2 * 128) >> L2INODESLOTSIZE;
784 lv->length = 2;
785 memcpy(&dp->di_fastsymlink, jfs_ip->i_inline, IDATASIZE);
786 dilinelock->index++;
789 * copy inline data from in-memory inode to on-disk inode:
790 * 128 byte slot granularity
792 if (test_cflag(COMMIT_Inlineea, ip)) {
793 lv = & dilinelock->lv[dilinelock->index];
794 lv->offset = (dioffset + 3 * 128) >> L2INODESLOTSIZE;
795 lv->length = 1;
796 memcpy(&dp->di_inlineea, jfs_ip->i_inline_ea, INODESLOTSIZE);
797 dilinelock->index++;
799 clear_cflag(COMMIT_Inlineea, ip);
803 * lock/copy inode base: 128 byte slot granularity
805 lv = & dilinelock->lv[dilinelock->index];
806 lv->offset = dioffset >> L2INODESLOTSIZE;
807 copy_to_dinode(dp, ip);
808 if (test_and_clear_cflag(COMMIT_Dirtable, ip)) {
809 lv->length = 2;
810 memcpy(&dp->di_dirtable, &jfs_ip->i_dirtable, 96);
811 } else
812 lv->length = 1;
813 dilinelock->index++;
815 /* release the buffer holding the updated on-disk inode.
816 * the buffer will be later written by commit processing.
818 write_metapage(mp);
820 return (rc);
825 * NAME: diFree(ip)
827 * FUNCTION: free a specified inode from the inode working map
828 * for a fileset or aggregate.
830 * if the inode to be freed represents the first (only)
831 * free inode within the iag, the iag will be placed on
832 * the ag free inode list.
834 * freeing the inode will cause the inode extent to be
835 * freed if the inode is the only allocated inode within
836 * the extent. in this case all the disk resource backing
837 * up the inode extent will be freed. in addition, the iag
838 * will be placed on the ag extent free list if the extent
839 * is the first free extent in the iag. if freeing the
840 * extent also means that no free inodes will exist for
841 * the iag, the iag will also be removed from the ag free
842 * inode list.
844 * the iag describing the inode will be freed if the extent
845 * is to be freed and it is the only backed extent within
846 * the iag. in this case, the iag will be removed from the
847 * ag free extent list and ag free inode list and placed on
848 * the inode map's free iag list.
850 * a careful update approach is used to provide consistency
851 * in the face of updates to multiple buffers. under this
852 * approach, all required buffers are obtained before making
853 * any updates and are held until all updates are complete.
855 * PARAMETERS:
856 * ip - inode to be freed.
858 * RETURN VALUES:
859 * 0 - success
860 * -EIO - i/o error.
862 int diFree(struct inode *ip)
864 int rc;
865 ino_t inum = ip->i_ino;
866 struct iag *iagp, *aiagp, *biagp, *ciagp, *diagp;
867 struct metapage *mp, *amp, *bmp, *cmp, *dmp;
868 int iagno, ino, extno, bitno, sword, agno;
869 int back, fwd;
870 u32 bitmap, mask;
871 struct inode *ipimap = JFS_SBI(ip->i_sb)->ipimap;
872 struct inomap *imap = JFS_IP(ipimap)->i_imap;
873 pxd_t freepxd;
874 tid_t tid;
875 struct inode *iplist[3];
876 struct tlock *tlck;
877 struct pxd_lock *pxdlock;
880 * This is just to suppress compiler warnings. The same logic that
881 * references these variables is used to initialize them.
883 aiagp = biagp = ciagp = diagp = NULL;
885 /* get the iag number containing the inode.
887 iagno = INOTOIAG(inum);
889 /* make sure that the iag is contained within
890 * the map.
892 if (iagno >= imap->im_nextiag) {
893 print_hex_dump(KERN_ERR, "imap: ", DUMP_PREFIX_ADDRESS, 16, 4,
894 imap, 32, 0);
895 jfs_error(ip->i_sb,
896 "diFree: inum = %d, iagno = %d, nextiag = %d",
897 (uint) inum, iagno, imap->im_nextiag);
898 return -EIO;
901 /* get the allocation group for this ino.
903 agno = JFS_IP(ip)->agno;
905 /* Lock the AG specific inode map information
907 AG_LOCK(imap, agno);
909 /* Obtain read lock in imap inode. Don't release it until we have
910 * read all of the IAG's that we are going to.
912 IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
914 /* read the iag.
916 if ((rc = diIAGRead(imap, iagno, &mp))) {
917 IREAD_UNLOCK(ipimap);
918 AG_UNLOCK(imap, agno);
919 return (rc);
921 iagp = (struct iag *) mp->data;
923 /* get the inode number and extent number of the inode within
924 * the iag and the inode number within the extent.
926 ino = inum & (INOSPERIAG - 1);
927 extno = ino >> L2INOSPEREXT;
928 bitno = ino & (INOSPEREXT - 1);
929 mask = HIGHORDER >> bitno;
931 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
932 jfs_error(ip->i_sb,
933 "diFree: wmap shows inode already free");
936 if (!addressPXD(&iagp->inoext[extno])) {
937 release_metapage(mp);
938 IREAD_UNLOCK(ipimap);
939 AG_UNLOCK(imap, agno);
940 jfs_error(ip->i_sb, "diFree: invalid inoext");
941 return -EIO;
944 /* compute the bitmap for the extent reflecting the freed inode.
946 bitmap = le32_to_cpu(iagp->wmap[extno]) & ~mask;
948 if (imap->im_agctl[agno].numfree > imap->im_agctl[agno].numinos) {
949 release_metapage(mp);
950 IREAD_UNLOCK(ipimap);
951 AG_UNLOCK(imap, agno);
952 jfs_error(ip->i_sb, "diFree: numfree > numinos");
953 return -EIO;
956 * inode extent still has some inodes or below low water mark:
957 * keep the inode extent;
959 if (bitmap ||
960 imap->im_agctl[agno].numfree < 96 ||
961 (imap->im_agctl[agno].numfree < 288 &&
962 (((imap->im_agctl[agno].numfree * 100) /
963 imap->im_agctl[agno].numinos) <= 25))) {
964 /* if the iag currently has no free inodes (i.e.,
965 * the inode being freed is the first free inode of iag),
966 * insert the iag at head of the inode free list for the ag.
968 if (iagp->nfreeinos == 0) {
969 /* check if there are any iags on the ag inode
970 * free list. if so, read the first one so that
971 * we can link the current iag onto the list at
972 * the head.
974 if ((fwd = imap->im_agctl[agno].inofree) >= 0) {
975 /* read the iag that currently is the head
976 * of the list.
978 if ((rc = diIAGRead(imap, fwd, &amp))) {
979 IREAD_UNLOCK(ipimap);
980 AG_UNLOCK(imap, agno);
981 release_metapage(mp);
982 return (rc);
984 aiagp = (struct iag *) amp->data;
986 /* make current head point back to the iag.
988 aiagp->inofreeback = cpu_to_le32(iagno);
990 write_metapage(amp);
993 /* iag points forward to current head and iag
994 * becomes the new head of the list.
996 iagp->inofreefwd =
997 cpu_to_le32(imap->im_agctl[agno].inofree);
998 iagp->inofreeback = cpu_to_le32(-1);
999 imap->im_agctl[agno].inofree = iagno;
1001 IREAD_UNLOCK(ipimap);
1003 /* update the free inode summary map for the extent if
1004 * freeing the inode means the extent will now have free
1005 * inodes (i.e., the inode being freed is the first free
1006 * inode of extent),
1008 if (iagp->wmap[extno] == cpu_to_le32(ONES)) {
1009 sword = extno >> L2EXTSPERSUM;
1010 bitno = extno & (EXTSPERSUM - 1);
1011 iagp->inosmap[sword] &=
1012 cpu_to_le32(~(HIGHORDER >> bitno));
1015 /* update the bitmap.
1017 iagp->wmap[extno] = cpu_to_le32(bitmap);
1019 /* update the free inode counts at the iag, ag and
1020 * map level.
1022 iagp->nfreeinos =
1023 cpu_to_le32(le32_to_cpu(iagp->nfreeinos) + 1);
1024 imap->im_agctl[agno].numfree += 1;
1025 atomic_inc(&imap->im_numfree);
1027 /* release the AG inode map lock
1029 AG_UNLOCK(imap, agno);
1031 /* write the iag */
1032 write_metapage(mp);
1034 return (0);
1039 * inode extent has become free and above low water mark:
1040 * free the inode extent;
1044 * prepare to update iag list(s) (careful update step 1)
1046 amp = bmp = cmp = dmp = NULL;
1047 fwd = back = -1;
1049 /* check if the iag currently has no free extents. if so,
1050 * it will be placed on the head of the ag extent free list.
1052 if (iagp->nfreeexts == 0) {
1053 /* check if the ag extent free list has any iags.
1054 * if so, read the iag at the head of the list now.
1055 * this (head) iag will be updated later to reflect
1056 * the addition of the current iag at the head of
1057 * the list.
1059 if ((fwd = imap->im_agctl[agno].extfree) >= 0) {
1060 if ((rc = diIAGRead(imap, fwd, &amp)))
1061 goto error_out;
1062 aiagp = (struct iag *) amp->data;
1064 } else {
1065 /* iag has free extents. check if the addition of a free
1066 * extent will cause all extents to be free within this
1067 * iag. if so, the iag will be removed from the ag extent
1068 * free list and placed on the inode map's free iag list.
1070 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG - 1)) {
1071 /* in preparation for removing the iag from the
1072 * ag extent free list, read the iags preceeding
1073 * and following the iag on the ag extent free
1074 * list.
1076 if ((fwd = le32_to_cpu(iagp->extfreefwd)) >= 0) {
1077 if ((rc = diIAGRead(imap, fwd, &amp)))
1078 goto error_out;
1079 aiagp = (struct iag *) amp->data;
1082 if ((back = le32_to_cpu(iagp->extfreeback)) >= 0) {
1083 if ((rc = diIAGRead(imap, back, &bmp)))
1084 goto error_out;
1085 biagp = (struct iag *) bmp->data;
1090 /* remove the iag from the ag inode free list if freeing
1091 * this extent cause the iag to have no free inodes.
1093 if (iagp->nfreeinos == cpu_to_le32(INOSPEREXT - 1)) {
1094 int inofreeback = le32_to_cpu(iagp->inofreeback);
1095 int inofreefwd = le32_to_cpu(iagp->inofreefwd);
1097 /* in preparation for removing the iag from the
1098 * ag inode free list, read the iags preceeding
1099 * and following the iag on the ag inode free
1100 * list. before reading these iags, we must make
1101 * sure that we already don't have them in hand
1102 * from up above, since re-reading an iag (buffer)
1103 * we are currently holding would cause a deadlock.
1105 if (inofreefwd >= 0) {
1107 if (inofreefwd == fwd)
1108 ciagp = (struct iag *) amp->data;
1109 else if (inofreefwd == back)
1110 ciagp = (struct iag *) bmp->data;
1111 else {
1112 if ((rc =
1113 diIAGRead(imap, inofreefwd, &cmp)))
1114 goto error_out;
1115 ciagp = (struct iag *) cmp->data;
1117 assert(ciagp != NULL);
1120 if (inofreeback >= 0) {
1121 if (inofreeback == fwd)
1122 diagp = (struct iag *) amp->data;
1123 else if (inofreeback == back)
1124 diagp = (struct iag *) bmp->data;
1125 else {
1126 if ((rc =
1127 diIAGRead(imap, inofreeback, &dmp)))
1128 goto error_out;
1129 diagp = (struct iag *) dmp->data;
1131 assert(diagp != NULL);
1135 IREAD_UNLOCK(ipimap);
1138 * invalidate any page of the inode extent freed from buffer cache;
1140 freepxd = iagp->inoext[extno];
1141 invalidate_pxd_metapages(ip, freepxd);
1144 * update iag list(s) (careful update step 2)
1146 /* add the iag to the ag extent free list if this is the
1147 * first free extent for the iag.
1149 if (iagp->nfreeexts == 0) {
1150 if (fwd >= 0)
1151 aiagp->extfreeback = cpu_to_le32(iagno);
1153 iagp->extfreefwd =
1154 cpu_to_le32(imap->im_agctl[agno].extfree);
1155 iagp->extfreeback = cpu_to_le32(-1);
1156 imap->im_agctl[agno].extfree = iagno;
1157 } else {
1158 /* remove the iag from the ag extent list if all extents
1159 * are now free and place it on the inode map iag free list.
1161 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG - 1)) {
1162 if (fwd >= 0)
1163 aiagp->extfreeback = iagp->extfreeback;
1165 if (back >= 0)
1166 biagp->extfreefwd = iagp->extfreefwd;
1167 else
1168 imap->im_agctl[agno].extfree =
1169 le32_to_cpu(iagp->extfreefwd);
1171 iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
1173 IAGFREE_LOCK(imap);
1174 iagp->iagfree = cpu_to_le32(imap->im_freeiag);
1175 imap->im_freeiag = iagno;
1176 IAGFREE_UNLOCK(imap);
1180 /* remove the iag from the ag inode free list if freeing
1181 * this extent causes the iag to have no free inodes.
1183 if (iagp->nfreeinos == cpu_to_le32(INOSPEREXT - 1)) {
1184 if ((int) le32_to_cpu(iagp->inofreefwd) >= 0)
1185 ciagp->inofreeback = iagp->inofreeback;
1187 if ((int) le32_to_cpu(iagp->inofreeback) >= 0)
1188 diagp->inofreefwd = iagp->inofreefwd;
1189 else
1190 imap->im_agctl[agno].inofree =
1191 le32_to_cpu(iagp->inofreefwd);
1193 iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
1196 /* update the inode extent address and working map
1197 * to reflect the free extent.
1198 * the permanent map should have been updated already
1199 * for the inode being freed.
1201 if (iagp->pmap[extno] != 0) {
1202 jfs_error(ip->i_sb, "diFree: the pmap does not show inode free");
1204 iagp->wmap[extno] = 0;
1205 PXDlength(&iagp->inoext[extno], 0);
1206 PXDaddress(&iagp->inoext[extno], 0);
1208 /* update the free extent and free inode summary maps
1209 * to reflect the freed extent.
1210 * the inode summary map is marked to indicate no inodes
1211 * available for the freed extent.
1213 sword = extno >> L2EXTSPERSUM;
1214 bitno = extno & (EXTSPERSUM - 1);
1215 mask = HIGHORDER >> bitno;
1216 iagp->inosmap[sword] |= cpu_to_le32(mask);
1217 iagp->extsmap[sword] &= cpu_to_le32(~mask);
1219 /* update the number of free inodes and number of free extents
1220 * for the iag.
1222 iagp->nfreeinos = cpu_to_le32(le32_to_cpu(iagp->nfreeinos) -
1223 (INOSPEREXT - 1));
1224 iagp->nfreeexts = cpu_to_le32(le32_to_cpu(iagp->nfreeexts) + 1);
1226 /* update the number of free inodes and backed inodes
1227 * at the ag and inode map level.
1229 imap->im_agctl[agno].numfree -= (INOSPEREXT - 1);
1230 imap->im_agctl[agno].numinos -= INOSPEREXT;
1231 atomic_sub(INOSPEREXT - 1, &imap->im_numfree);
1232 atomic_sub(INOSPEREXT, &imap->im_numinos);
1234 if (amp)
1235 write_metapage(amp);
1236 if (bmp)
1237 write_metapage(bmp);
1238 if (cmp)
1239 write_metapage(cmp);
1240 if (dmp)
1241 write_metapage(dmp);
1244 * start transaction to update block allocation map
1245 * for the inode extent freed;
1247 * N.B. AG_LOCK is released and iag will be released below, and
1248 * other thread may allocate inode from/reusing the ixad freed
1249 * BUT with new/different backing inode extent from the extent
1250 * to be freed by the transaction;
1252 tid = txBegin(ipimap->i_sb, COMMIT_FORCE);
1253 mutex_lock(&JFS_IP(ipimap)->commit_mutex);
1255 /* acquire tlock of the iag page of the freed ixad
1256 * to force the page NOHOMEOK (even though no data is
1257 * logged from the iag page) until NOREDOPAGE|FREEXTENT log
1258 * for the free of the extent is committed;
1259 * write FREEXTENT|NOREDOPAGE log record
1260 * N.B. linelock is overlaid as freed extent descriptor;
1262 tlck = txLock(tid, ipimap, mp, tlckINODE | tlckFREE);
1263 pxdlock = (struct pxd_lock *) & tlck->lock;
1264 pxdlock->flag = mlckFREEPXD;
1265 pxdlock->pxd = freepxd;
1266 pxdlock->index = 1;
1268 write_metapage(mp);
1270 iplist[0] = ipimap;
1273 * logredo needs the IAG number and IAG extent index in order
1274 * to ensure that the IMap is consistent. The least disruptive
1275 * way to pass these values through to the transaction manager
1276 * is in the iplist array.
1278 * It's not pretty, but it works.
1280 iplist[1] = (struct inode *) (size_t)iagno;
1281 iplist[2] = (struct inode *) (size_t)extno;
1283 rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
1285 txEnd(tid);
1286 mutex_unlock(&JFS_IP(ipimap)->commit_mutex);
1288 /* unlock the AG inode map information */
1289 AG_UNLOCK(imap, agno);
1291 return (0);
1293 error_out:
1294 IREAD_UNLOCK(ipimap);
1296 if (amp)
1297 release_metapage(amp);
1298 if (bmp)
1299 release_metapage(bmp);
1300 if (cmp)
1301 release_metapage(cmp);
1302 if (dmp)
1303 release_metapage(dmp);
1305 AG_UNLOCK(imap, agno);
1307 release_metapage(mp);
1309 return (rc);
1313 * There are several places in the diAlloc* routines where we initialize
1314 * the inode.
1316 static inline void
1317 diInitInode(struct inode *ip, int iagno, int ino, int extno, struct iag * iagp)
1319 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
1320 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
1322 ip->i_ino = (iagno << L2INOSPERIAG) + ino;
1323 jfs_ip->ixpxd = iagp->inoext[extno];
1324 jfs_ip->agno = BLKTOAG(le64_to_cpu(iagp->agstart), sbi);
1325 jfs_ip->active_ag = -1;
1330 * NAME: diAlloc(pip,dir,ip)
1332 * FUNCTION: allocate a disk inode from the inode working map
1333 * for a fileset or aggregate.
1335 * PARAMETERS:
1336 * pip - pointer to incore inode for the parent inode.
1337 * dir - 'true' if the new disk inode is for a directory.
1338 * ip - pointer to a new inode
1340 * RETURN VALUES:
1341 * 0 - success.
1342 * -ENOSPC - insufficient disk resources.
1343 * -EIO - i/o error.
1345 int diAlloc(struct inode *pip, bool dir, struct inode *ip)
1347 int rc, ino, iagno, addext, extno, bitno, sword;
1348 int nwords, rem, i, agno;
1349 u32 mask, inosmap, extsmap;
1350 struct inode *ipimap;
1351 struct metapage *mp;
1352 ino_t inum;
1353 struct iag *iagp;
1354 struct inomap *imap;
1356 /* get the pointers to the inode map inode and the
1357 * corresponding imap control structure.
1359 ipimap = JFS_SBI(pip->i_sb)->ipimap;
1360 imap = JFS_IP(ipimap)->i_imap;
1361 JFS_IP(ip)->ipimap = ipimap;
1362 JFS_IP(ip)->fileset = FILESYSTEM_I;
1364 /* for a directory, the allocation policy is to start
1365 * at the ag level using the preferred ag.
1367 if (dir) {
1368 agno = dbNextAG(JFS_SBI(pip->i_sb)->ipbmap);
1369 AG_LOCK(imap, agno);
1370 goto tryag;
1373 /* for files, the policy starts off by trying to allocate from
1374 * the same iag containing the parent disk inode:
1375 * try to allocate the new disk inode close to the parent disk
1376 * inode, using parent disk inode number + 1 as the allocation
1377 * hint. (we use a left-to-right policy to attempt to avoid
1378 * moving backward on the disk.) compute the hint within the
1379 * file system and the iag.
1382 /* get the ag number of this iag */
1383 agno = JFS_IP(pip)->agno;
1385 if (atomic_read(&JFS_SBI(pip->i_sb)->bmap->db_active[agno])) {
1387 * There is an open file actively growing. We want to
1388 * allocate new inodes from a different ag to avoid
1389 * fragmentation problems.
1391 agno = dbNextAG(JFS_SBI(pip->i_sb)->ipbmap);
1392 AG_LOCK(imap, agno);
1393 goto tryag;
1396 inum = pip->i_ino + 1;
1397 ino = inum & (INOSPERIAG - 1);
1399 /* back off the hint if it is outside of the iag */
1400 if (ino == 0)
1401 inum = pip->i_ino;
1403 /* lock the AG inode map information */
1404 AG_LOCK(imap, agno);
1406 /* Get read lock on imap inode */
1407 IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
1409 /* get the iag number and read the iag */
1410 iagno = INOTOIAG(inum);
1411 if ((rc = diIAGRead(imap, iagno, &mp))) {
1412 IREAD_UNLOCK(ipimap);
1413 AG_UNLOCK(imap, agno);
1414 return (rc);
1416 iagp = (struct iag *) mp->data;
1418 /* determine if new inode extent is allowed to be added to the iag.
1419 * new inode extent can be added to the iag if the ag
1420 * has less than 32 free disk inodes and the iag has free extents.
1422 addext = (imap->im_agctl[agno].numfree < 32 && iagp->nfreeexts);
1425 * try to allocate from the IAG
1427 /* check if the inode may be allocated from the iag
1428 * (i.e. the inode has free inodes or new extent can be added).
1430 if (iagp->nfreeinos || addext) {
1431 /* determine the extent number of the hint.
1433 extno = ino >> L2INOSPEREXT;
1435 /* check if the extent containing the hint has backed
1436 * inodes. if so, try to allocate within this extent.
1438 if (addressPXD(&iagp->inoext[extno])) {
1439 bitno = ino & (INOSPEREXT - 1);
1440 if ((bitno =
1441 diFindFree(le32_to_cpu(iagp->wmap[extno]),
1442 bitno))
1443 < INOSPEREXT) {
1444 ino = (extno << L2INOSPEREXT) + bitno;
1446 /* a free inode (bit) was found within this
1447 * extent, so allocate it.
1449 rc = diAllocBit(imap, iagp, ino);
1450 IREAD_UNLOCK(ipimap);
1451 if (rc) {
1452 assert(rc == -EIO);
1453 } else {
1454 /* set the results of the allocation
1455 * and write the iag.
1457 diInitInode(ip, iagno, ino, extno,
1458 iagp);
1459 mark_metapage_dirty(mp);
1461 release_metapage(mp);
1463 /* free the AG lock and return.
1465 AG_UNLOCK(imap, agno);
1466 return (rc);
1469 if (!addext)
1470 extno =
1471 (extno ==
1472 EXTSPERIAG - 1) ? 0 : extno + 1;
1476 * no free inodes within the extent containing the hint.
1478 * try to allocate from the backed extents following
1479 * hint or, if appropriate (i.e. addext is true), allocate
1480 * an extent of free inodes at or following the extent
1481 * containing the hint.
1483 * the free inode and free extent summary maps are used
1484 * here, so determine the starting summary map position
1485 * and the number of words we'll have to examine. again,
1486 * the approach is to allocate following the hint, so we
1487 * might have to initially ignore prior bits of the summary
1488 * map that represent extents prior to the extent containing
1489 * the hint and later revisit these bits.
1491 bitno = extno & (EXTSPERSUM - 1);
1492 nwords = (bitno == 0) ? SMAPSZ : SMAPSZ + 1;
1493 sword = extno >> L2EXTSPERSUM;
1495 /* mask any prior bits for the starting words of the
1496 * summary map.
1498 mask = ONES << (EXTSPERSUM - bitno);
1499 inosmap = le32_to_cpu(iagp->inosmap[sword]) | mask;
1500 extsmap = le32_to_cpu(iagp->extsmap[sword]) | mask;
1502 /* scan the free inode and free extent summary maps for
1503 * free resources.
1505 for (i = 0; i < nwords; i++) {
1506 /* check if this word of the free inode summary
1507 * map describes an extent with free inodes.
1509 if (~inosmap) {
1510 /* an extent with free inodes has been
1511 * found. determine the extent number
1512 * and the inode number within the extent.
1514 rem = diFindFree(inosmap, 0);
1515 extno = (sword << L2EXTSPERSUM) + rem;
1516 rem = diFindFree(le32_to_cpu(iagp->wmap[extno]),
1518 if (rem >= INOSPEREXT) {
1519 IREAD_UNLOCK(ipimap);
1520 release_metapage(mp);
1521 AG_UNLOCK(imap, agno);
1522 jfs_error(ip->i_sb,
1523 "diAlloc: can't find free bit "
1524 "in wmap");
1525 return EIO;
1528 /* determine the inode number within the
1529 * iag and allocate the inode from the
1530 * map.
1532 ino = (extno << L2INOSPEREXT) + rem;
1533 rc = diAllocBit(imap, iagp, ino);
1534 IREAD_UNLOCK(ipimap);
1535 if (rc)
1536 assert(rc == -EIO);
1537 else {
1538 /* set the results of the allocation
1539 * and write the iag.
1541 diInitInode(ip, iagno, ino, extno,
1542 iagp);
1543 mark_metapage_dirty(mp);
1545 release_metapage(mp);
1547 /* free the AG lock and return.
1549 AG_UNLOCK(imap, agno);
1550 return (rc);
1554 /* check if we may allocate an extent of free
1555 * inodes and whether this word of the free
1556 * extents summary map describes a free extent.
1558 if (addext && ~extsmap) {
1559 /* a free extent has been found. determine
1560 * the extent number.
1562 rem = diFindFree(extsmap, 0);
1563 extno = (sword << L2EXTSPERSUM) + rem;
1565 /* allocate an extent of free inodes.
1567 if ((rc = diNewExt(imap, iagp, extno))) {
1568 /* if there is no disk space for a
1569 * new extent, try to allocate the
1570 * disk inode from somewhere else.
1572 if (rc == -ENOSPC)
1573 break;
1575 assert(rc == -EIO);
1576 } else {
1577 /* set the results of the allocation
1578 * and write the iag.
1580 diInitInode(ip, iagno,
1581 extno << L2INOSPEREXT,
1582 extno, iagp);
1583 mark_metapage_dirty(mp);
1585 release_metapage(mp);
1586 /* free the imap inode & the AG lock & return.
1588 IREAD_UNLOCK(ipimap);
1589 AG_UNLOCK(imap, agno);
1590 return (rc);
1593 /* move on to the next set of summary map words.
1595 sword = (sword == SMAPSZ - 1) ? 0 : sword + 1;
1596 inosmap = le32_to_cpu(iagp->inosmap[sword]);
1597 extsmap = le32_to_cpu(iagp->extsmap[sword]);
1600 /* unlock imap inode */
1601 IREAD_UNLOCK(ipimap);
1603 /* nothing doing in this iag, so release it. */
1604 release_metapage(mp);
1606 tryag:
1608 * try to allocate anywhere within the same AG as the parent inode.
1610 rc = diAllocAG(imap, agno, dir, ip);
1612 AG_UNLOCK(imap, agno);
1614 if (rc != -ENOSPC)
1615 return (rc);
1618 * try to allocate in any AG.
1620 return (diAllocAny(imap, agno, dir, ip));
1625 * NAME: diAllocAG(imap,agno,dir,ip)
1627 * FUNCTION: allocate a disk inode from the allocation group.
1629 * this routine first determines if a new extent of free
1630 * inodes should be added for the allocation group, with
1631 * the current request satisfied from this extent. if this
1632 * is the case, an attempt will be made to do just that. if
1633 * this attempt fails or it has been determined that a new
1634 * extent should not be added, an attempt is made to satisfy
1635 * the request by allocating an existing (backed) free inode
1636 * from the allocation group.
1638 * PRE CONDITION: Already have the AG lock for this AG.
1640 * PARAMETERS:
1641 * imap - pointer to inode map control structure.
1642 * agno - allocation group to allocate from.
1643 * dir - 'true' if the new disk inode is for a directory.
1644 * ip - pointer to the new inode to be filled in on successful return
1645 * with the disk inode number allocated, its extent address
1646 * and the start of the ag.
1648 * RETURN VALUES:
1649 * 0 - success.
1650 * -ENOSPC - insufficient disk resources.
1651 * -EIO - i/o error.
1653 static int
1654 diAllocAG(struct inomap * imap, int agno, bool dir, struct inode *ip)
1656 int rc, addext, numfree, numinos;
1658 /* get the number of free and the number of backed disk
1659 * inodes currently within the ag.
1661 numfree = imap->im_agctl[agno].numfree;
1662 numinos = imap->im_agctl[agno].numinos;
1664 if (numfree > numinos) {
1665 jfs_error(ip->i_sb, "diAllocAG: numfree > numinos");
1666 return -EIO;
1669 /* determine if we should allocate a new extent of free inodes
1670 * within the ag: for directory inodes, add a new extent
1671 * if there are a small number of free inodes or number of free
1672 * inodes is a small percentage of the number of backed inodes.
1674 if (dir)
1675 addext = (numfree < 64 ||
1676 (numfree < 256
1677 && ((numfree * 100) / numinos) <= 20));
1678 else
1679 addext = (numfree == 0);
1682 * try to allocate a new extent of free inodes.
1684 if (addext) {
1685 /* if free space is not avaliable for this new extent, try
1686 * below to allocate a free and existing (already backed)
1687 * inode from the ag.
1689 if ((rc = diAllocExt(imap, agno, ip)) != -ENOSPC)
1690 return (rc);
1694 * try to allocate an existing free inode from the ag.
1696 return (diAllocIno(imap, agno, ip));
1701 * NAME: diAllocAny(imap,agno,dir,iap)
1703 * FUNCTION: allocate a disk inode from any other allocation group.
1705 * this routine is called when an allocation attempt within
1706 * the primary allocation group has failed. if attempts to
1707 * allocate an inode from any allocation group other than the
1708 * specified primary group.
1710 * PARAMETERS:
1711 * imap - pointer to inode map control structure.
1712 * agno - primary allocation group (to avoid).
1713 * dir - 'true' if the new disk inode is for a directory.
1714 * ip - pointer to a new inode to be filled in on successful return
1715 * with the disk inode number allocated, its extent address
1716 * and the start of the ag.
1718 * RETURN VALUES:
1719 * 0 - success.
1720 * -ENOSPC - insufficient disk resources.
1721 * -EIO - i/o error.
1723 static int
1724 diAllocAny(struct inomap * imap, int agno, bool dir, struct inode *ip)
1726 int ag, rc;
1727 int maxag = JFS_SBI(imap->im_ipimap->i_sb)->bmap->db_maxag;
1730 /* try to allocate from the ags following agno up to
1731 * the maximum ag number.
1733 for (ag = agno + 1; ag <= maxag; ag++) {
1734 AG_LOCK(imap, ag);
1736 rc = diAllocAG(imap, ag, dir, ip);
1738 AG_UNLOCK(imap, ag);
1740 if (rc != -ENOSPC)
1741 return (rc);
1744 /* try to allocate from the ags in front of agno.
1746 for (ag = 0; ag < agno; ag++) {
1747 AG_LOCK(imap, ag);
1749 rc = diAllocAG(imap, ag, dir, ip);
1751 AG_UNLOCK(imap, ag);
1753 if (rc != -ENOSPC)
1754 return (rc);
1757 /* no free disk inodes.
1759 return -ENOSPC;
1764 * NAME: diAllocIno(imap,agno,ip)
1766 * FUNCTION: allocate a disk inode from the allocation group's free
1767 * inode list, returning an error if this free list is
1768 * empty (i.e. no iags on the list).
1770 * allocation occurs from the first iag on the list using
1771 * the iag's free inode summary map to find the leftmost
1772 * free inode in the iag.
1774 * PRE CONDITION: Already have AG lock for this AG.
1776 * PARAMETERS:
1777 * imap - pointer to inode map control structure.
1778 * agno - allocation group.
1779 * ip - pointer to new inode to be filled in on successful return
1780 * with the disk inode number allocated, its extent address
1781 * and the start of the ag.
1783 * RETURN VALUES:
1784 * 0 - success.
1785 * -ENOSPC - insufficient disk resources.
1786 * -EIO - i/o error.
1788 static int diAllocIno(struct inomap * imap, int agno, struct inode *ip)
1790 int iagno, ino, rc, rem, extno, sword;
1791 struct metapage *mp;
1792 struct iag *iagp;
1794 /* check if there are iags on the ag's free inode list.
1796 if ((iagno = imap->im_agctl[agno].inofree) < 0)
1797 return -ENOSPC;
1799 /* obtain read lock on imap inode */
1800 IREAD_LOCK(imap->im_ipimap, RDWRLOCK_IMAP);
1802 /* read the iag at the head of the list.
1804 if ((rc = diIAGRead(imap, iagno, &mp))) {
1805 IREAD_UNLOCK(imap->im_ipimap);
1806 return (rc);
1808 iagp = (struct iag *) mp->data;
1810 /* better be free inodes in this iag if it is on the
1811 * list.
1813 if (!iagp->nfreeinos) {
1814 IREAD_UNLOCK(imap->im_ipimap);
1815 release_metapage(mp);
1816 jfs_error(ip->i_sb,
1817 "diAllocIno: nfreeinos = 0, but iag on freelist");
1818 return -EIO;
1821 /* scan the free inode summary map to find an extent
1822 * with free inodes.
1824 for (sword = 0;; sword++) {
1825 if (sword >= SMAPSZ) {
1826 IREAD_UNLOCK(imap->im_ipimap);
1827 release_metapage(mp);
1828 jfs_error(ip->i_sb,
1829 "diAllocIno: free inode not found in summary map");
1830 return -EIO;
1833 if (~iagp->inosmap[sword])
1834 break;
1837 /* found a extent with free inodes. determine
1838 * the extent number.
1840 rem = diFindFree(le32_to_cpu(iagp->inosmap[sword]), 0);
1841 if (rem >= EXTSPERSUM) {
1842 IREAD_UNLOCK(imap->im_ipimap);
1843 release_metapage(mp);
1844 jfs_error(ip->i_sb, "diAllocIno: no free extent found");
1845 return -EIO;
1847 extno = (sword << L2EXTSPERSUM) + rem;
1849 /* find the first free inode in the extent.
1851 rem = diFindFree(le32_to_cpu(iagp->wmap[extno]), 0);
1852 if (rem >= INOSPEREXT) {
1853 IREAD_UNLOCK(imap->im_ipimap);
1854 release_metapage(mp);
1855 jfs_error(ip->i_sb, "diAllocIno: free inode not found");
1856 return -EIO;
1859 /* compute the inode number within the iag.
1861 ino = (extno << L2INOSPEREXT) + rem;
1863 /* allocate the inode.
1865 rc = diAllocBit(imap, iagp, ino);
1866 IREAD_UNLOCK(imap->im_ipimap);
1867 if (rc) {
1868 release_metapage(mp);
1869 return (rc);
1872 /* set the results of the allocation and write the iag.
1874 diInitInode(ip, iagno, ino, extno, iagp);
1875 write_metapage(mp);
1877 return (0);
1882 * NAME: diAllocExt(imap,agno,ip)
1884 * FUNCTION: add a new extent of free inodes to an iag, allocating
1885 * an inode from this extent to satisfy the current allocation
1886 * request.
1888 * this routine first tries to find an existing iag with free
1889 * extents through the ag free extent list. if list is not
1890 * empty, the head of the list will be selected as the home
1891 * of the new extent of free inodes. otherwise (the list is
1892 * empty), a new iag will be allocated for the ag to contain
1893 * the extent.
1895 * once an iag has been selected, the free extent summary map
1896 * is used to locate a free extent within the iag and diNewExt()
1897 * is called to initialize the extent, with initialization
1898 * including the allocation of the first inode of the extent
1899 * for the purpose of satisfying this request.
1901 * PARAMETERS:
1902 * imap - pointer to inode map control structure.
1903 * agno - allocation group number.
1904 * ip - pointer to new inode to be filled in on successful return
1905 * with the disk inode number allocated, its extent address
1906 * and the start of the ag.
1908 * RETURN VALUES:
1909 * 0 - success.
1910 * -ENOSPC - insufficient disk resources.
1911 * -EIO - i/o error.
1913 static int diAllocExt(struct inomap * imap, int agno, struct inode *ip)
1915 int rem, iagno, sword, extno, rc;
1916 struct metapage *mp;
1917 struct iag *iagp;
1919 /* check if the ag has any iags with free extents. if not,
1920 * allocate a new iag for the ag.
1922 if ((iagno = imap->im_agctl[agno].extfree) < 0) {
1923 /* If successful, diNewIAG will obtain the read lock on the
1924 * imap inode.
1926 if ((rc = diNewIAG(imap, &iagno, agno, &mp))) {
1927 return (rc);
1929 iagp = (struct iag *) mp->data;
1931 /* set the ag number if this a brand new iag
1933 iagp->agstart =
1934 cpu_to_le64(AGTOBLK(agno, imap->im_ipimap));
1935 } else {
1936 /* read the iag.
1938 IREAD_LOCK(imap->im_ipimap, RDWRLOCK_IMAP);
1939 if ((rc = diIAGRead(imap, iagno, &mp))) {
1940 IREAD_UNLOCK(imap->im_ipimap);
1941 jfs_error(ip->i_sb, "diAllocExt: error reading iag");
1942 return rc;
1944 iagp = (struct iag *) mp->data;
1947 /* using the free extent summary map, find a free extent.
1949 for (sword = 0;; sword++) {
1950 if (sword >= SMAPSZ) {
1951 release_metapage(mp);
1952 IREAD_UNLOCK(imap->im_ipimap);
1953 jfs_error(ip->i_sb,
1954 "diAllocExt: free ext summary map not found");
1955 return -EIO;
1957 if (~iagp->extsmap[sword])
1958 break;
1961 /* determine the extent number of the free extent.
1963 rem = diFindFree(le32_to_cpu(iagp->extsmap[sword]), 0);
1964 if (rem >= EXTSPERSUM) {
1965 release_metapage(mp);
1966 IREAD_UNLOCK(imap->im_ipimap);
1967 jfs_error(ip->i_sb, "diAllocExt: free extent not found");
1968 return -EIO;
1970 extno = (sword << L2EXTSPERSUM) + rem;
1972 /* initialize the new extent.
1974 rc = diNewExt(imap, iagp, extno);
1975 IREAD_UNLOCK(imap->im_ipimap);
1976 if (rc) {
1977 /* something bad happened. if a new iag was allocated,
1978 * place it back on the inode map's iag free list, and
1979 * clear the ag number information.
1981 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
1982 IAGFREE_LOCK(imap);
1983 iagp->iagfree = cpu_to_le32(imap->im_freeiag);
1984 imap->im_freeiag = iagno;
1985 IAGFREE_UNLOCK(imap);
1987 write_metapage(mp);
1988 return (rc);
1991 /* set the results of the allocation and write the iag.
1993 diInitInode(ip, iagno, extno << L2INOSPEREXT, extno, iagp);
1995 write_metapage(mp);
1997 return (0);
2002 * NAME: diAllocBit(imap,iagp,ino)
2004 * FUNCTION: allocate a backed inode from an iag.
2006 * this routine performs the mechanics of allocating a
2007 * specified inode from a backed extent.
2009 * if the inode to be allocated represents the last free
2010 * inode within the iag, the iag will be removed from the
2011 * ag free inode list.
2013 * a careful update approach is used to provide consistency
2014 * in the face of updates to multiple buffers. under this
2015 * approach, all required buffers are obtained before making
2016 * any updates and are held all are updates are complete.
2018 * PRE CONDITION: Already have buffer lock on iagp. Already have AG lock on
2019 * this AG. Must have read lock on imap inode.
2021 * PARAMETERS:
2022 * imap - pointer to inode map control structure.
2023 * iagp - pointer to iag.
2024 * ino - inode number to be allocated within the iag.
2026 * RETURN VALUES:
2027 * 0 - success.
2028 * -ENOSPC - insufficient disk resources.
2029 * -EIO - i/o error.
2031 static int diAllocBit(struct inomap * imap, struct iag * iagp, int ino)
2033 int extno, bitno, agno, sword, rc;
2034 struct metapage *amp = NULL, *bmp = NULL;
2035 struct iag *aiagp = NULL, *biagp = NULL;
2036 u32 mask;
2038 /* check if this is the last free inode within the iag.
2039 * if so, it will have to be removed from the ag free
2040 * inode list, so get the iags preceeding and following
2041 * it on the list.
2043 if (iagp->nfreeinos == cpu_to_le32(1)) {
2044 if ((int) le32_to_cpu(iagp->inofreefwd) >= 0) {
2045 if ((rc =
2046 diIAGRead(imap, le32_to_cpu(iagp->inofreefwd),
2047 &amp)))
2048 return (rc);
2049 aiagp = (struct iag *) amp->data;
2052 if ((int) le32_to_cpu(iagp->inofreeback) >= 0) {
2053 if ((rc =
2054 diIAGRead(imap,
2055 le32_to_cpu(iagp->inofreeback),
2056 &bmp))) {
2057 if (amp)
2058 release_metapage(amp);
2059 return (rc);
2061 biagp = (struct iag *) bmp->data;
2065 /* get the ag number, extent number, inode number within
2066 * the extent.
2068 agno = BLKTOAG(le64_to_cpu(iagp->agstart), JFS_SBI(imap->im_ipimap->i_sb));
2069 extno = ino >> L2INOSPEREXT;
2070 bitno = ino & (INOSPEREXT - 1);
2072 /* compute the mask for setting the map.
2074 mask = HIGHORDER >> bitno;
2076 /* the inode should be free and backed.
2078 if (((le32_to_cpu(iagp->pmap[extno]) & mask) != 0) ||
2079 ((le32_to_cpu(iagp->wmap[extno]) & mask) != 0) ||
2080 (addressPXD(&iagp->inoext[extno]) == 0)) {
2081 if (amp)
2082 release_metapage(amp);
2083 if (bmp)
2084 release_metapage(bmp);
2086 jfs_error(imap->im_ipimap->i_sb,
2087 "diAllocBit: iag inconsistent");
2088 return -EIO;
2091 /* mark the inode as allocated in the working map.
2093 iagp->wmap[extno] |= cpu_to_le32(mask);
2095 /* check if all inodes within the extent are now
2096 * allocated. if so, update the free inode summary
2097 * map to reflect this.
2099 if (iagp->wmap[extno] == cpu_to_le32(ONES)) {
2100 sword = extno >> L2EXTSPERSUM;
2101 bitno = extno & (EXTSPERSUM - 1);
2102 iagp->inosmap[sword] |= cpu_to_le32(HIGHORDER >> bitno);
2105 /* if this was the last free inode in the iag, remove the
2106 * iag from the ag free inode list.
2108 if (iagp->nfreeinos == cpu_to_le32(1)) {
2109 if (amp) {
2110 aiagp->inofreeback = iagp->inofreeback;
2111 write_metapage(amp);
2114 if (bmp) {
2115 biagp->inofreefwd = iagp->inofreefwd;
2116 write_metapage(bmp);
2117 } else {
2118 imap->im_agctl[agno].inofree =
2119 le32_to_cpu(iagp->inofreefwd);
2121 iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
2124 /* update the free inode count at the iag, ag, inode
2125 * map levels.
2127 iagp->nfreeinos = cpu_to_le32(le32_to_cpu(iagp->nfreeinos) - 1);
2128 imap->im_agctl[agno].numfree -= 1;
2129 atomic_dec(&imap->im_numfree);
2131 return (0);
2136 * NAME: diNewExt(imap,iagp,extno)
2138 * FUNCTION: initialize a new extent of inodes for an iag, allocating
2139 * the first inode of the extent for use for the current
2140 * allocation request.
2142 * disk resources are allocated for the new extent of inodes
2143 * and the inodes themselves are initialized to reflect their
2144 * existence within the extent (i.e. their inode numbers and
2145 * inode extent addresses are set) and their initial state
2146 * (mode and link count are set to zero).
2148 * if the iag is new, it is not yet on an ag extent free list
2149 * but will now be placed on this list.
2151 * if the allocation of the new extent causes the iag to
2152 * have no free extent, the iag will be removed from the
2153 * ag extent free list.
2155 * if the iag has no free backed inodes, it will be placed
2156 * on the ag free inode list, since the addition of the new
2157 * extent will now cause it to have free inodes.
2159 * a careful update approach is used to provide consistency
2160 * (i.e. list consistency) in the face of updates to multiple
2161 * buffers. under this approach, all required buffers are
2162 * obtained before making any updates and are held until all
2163 * updates are complete.
2165 * PRE CONDITION: Already have buffer lock on iagp. Already have AG lock on
2166 * this AG. Must have read lock on imap inode.
2168 * PARAMETERS:
2169 * imap - pointer to inode map control structure.
2170 * iagp - pointer to iag.
2171 * extno - extent number.
2173 * RETURN VALUES:
2174 * 0 - success.
2175 * -ENOSPC - insufficient disk resources.
2176 * -EIO - i/o error.
2178 static int diNewExt(struct inomap * imap, struct iag * iagp, int extno)
2180 int agno, iagno, fwd, back, freei = 0, sword, rc;
2181 struct iag *aiagp = NULL, *biagp = NULL, *ciagp = NULL;
2182 struct metapage *amp, *bmp, *cmp, *dmp;
2183 struct inode *ipimap;
2184 s64 blkno, hint;
2185 int i, j;
2186 u32 mask;
2187 ino_t ino;
2188 struct dinode *dp;
2189 struct jfs_sb_info *sbi;
2191 /* better have free extents.
2193 if (!iagp->nfreeexts) {
2194 jfs_error(imap->im_ipimap->i_sb, "diNewExt: no free extents");
2195 return -EIO;
2198 /* get the inode map inode.
2200 ipimap = imap->im_ipimap;
2201 sbi = JFS_SBI(ipimap->i_sb);
2203 amp = bmp = cmp = NULL;
2205 /* get the ag and iag numbers for this iag.
2207 agno = BLKTOAG(le64_to_cpu(iagp->agstart), sbi);
2208 iagno = le32_to_cpu(iagp->iagnum);
2210 /* check if this is the last free extent within the
2211 * iag. if so, the iag must be removed from the ag
2212 * free extent list, so get the iags preceeding and
2213 * following the iag on this list.
2215 if (iagp->nfreeexts == cpu_to_le32(1)) {
2216 if ((fwd = le32_to_cpu(iagp->extfreefwd)) >= 0) {
2217 if ((rc = diIAGRead(imap, fwd, &amp)))
2218 return (rc);
2219 aiagp = (struct iag *) amp->data;
2222 if ((back = le32_to_cpu(iagp->extfreeback)) >= 0) {
2223 if ((rc = diIAGRead(imap, back, &bmp)))
2224 goto error_out;
2225 biagp = (struct iag *) bmp->data;
2227 } else {
2228 /* the iag has free extents. if all extents are free
2229 * (as is the case for a newly allocated iag), the iag
2230 * must be added to the ag free extent list, so get
2231 * the iag at the head of the list in preparation for
2232 * adding this iag to this list.
2234 fwd = back = -1;
2235 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2236 if ((fwd = imap->im_agctl[agno].extfree) >= 0) {
2237 if ((rc = diIAGRead(imap, fwd, &amp)))
2238 goto error_out;
2239 aiagp = (struct iag *) amp->data;
2244 /* check if the iag has no free inodes. if so, the iag
2245 * will have to be added to the ag free inode list, so get
2246 * the iag at the head of the list in preparation for
2247 * adding this iag to this list. in doing this, we must
2248 * check if we already have the iag at the head of
2249 * the list in hand.
2251 if (iagp->nfreeinos == 0) {
2252 freei = imap->im_agctl[agno].inofree;
2254 if (freei >= 0) {
2255 if (freei == fwd) {
2256 ciagp = aiagp;
2257 } else if (freei == back) {
2258 ciagp = biagp;
2259 } else {
2260 if ((rc = diIAGRead(imap, freei, &cmp)))
2261 goto error_out;
2262 ciagp = (struct iag *) cmp->data;
2264 if (ciagp == NULL) {
2265 jfs_error(imap->im_ipimap->i_sb,
2266 "diNewExt: ciagp == NULL");
2267 rc = -EIO;
2268 goto error_out;
2273 /* allocate disk space for the inode extent.
2275 if ((extno == 0) || (addressPXD(&iagp->inoext[extno - 1]) == 0))
2276 hint = ((s64) agno << sbi->bmap->db_agl2size) - 1;
2277 else
2278 hint = addressPXD(&iagp->inoext[extno - 1]) +
2279 lengthPXD(&iagp->inoext[extno - 1]) - 1;
2281 if ((rc = dbAlloc(ipimap, hint, (s64) imap->im_nbperiext, &blkno)))
2282 goto error_out;
2284 /* compute the inode number of the first inode within the
2285 * extent.
2287 ino = (iagno << L2INOSPERIAG) + (extno << L2INOSPEREXT);
2289 /* initialize the inodes within the newly allocated extent a
2290 * page at a time.
2292 for (i = 0; i < imap->im_nbperiext; i += sbi->nbperpage) {
2293 /* get a buffer for this page of disk inodes.
2295 dmp = get_metapage(ipimap, blkno + i, PSIZE, 1);
2296 if (dmp == NULL) {
2297 rc = -EIO;
2298 goto error_out;
2300 dp = (struct dinode *) dmp->data;
2302 /* initialize the inode number, mode, link count and
2303 * inode extent address.
2305 for (j = 0; j < INOSPERPAGE; j++, dp++, ino++) {
2306 dp->di_inostamp = cpu_to_le32(sbi->inostamp);
2307 dp->di_number = cpu_to_le32(ino);
2308 dp->di_fileset = cpu_to_le32(FILESYSTEM_I);
2309 dp->di_mode = 0;
2310 dp->di_nlink = 0;
2311 PXDaddress(&(dp->di_ixpxd), blkno);
2312 PXDlength(&(dp->di_ixpxd), imap->im_nbperiext);
2314 write_metapage(dmp);
2317 /* if this is the last free extent within the iag, remove the
2318 * iag from the ag free extent list.
2320 if (iagp->nfreeexts == cpu_to_le32(1)) {
2321 if (fwd >= 0)
2322 aiagp->extfreeback = iagp->extfreeback;
2324 if (back >= 0)
2325 biagp->extfreefwd = iagp->extfreefwd;
2326 else
2327 imap->im_agctl[agno].extfree =
2328 le32_to_cpu(iagp->extfreefwd);
2330 iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
2331 } else {
2332 /* if the iag has all free extents (newly allocated iag),
2333 * add the iag to the ag free extent list.
2335 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2336 if (fwd >= 0)
2337 aiagp->extfreeback = cpu_to_le32(iagno);
2339 iagp->extfreefwd = cpu_to_le32(fwd);
2340 iagp->extfreeback = cpu_to_le32(-1);
2341 imap->im_agctl[agno].extfree = iagno;
2345 /* if the iag has no free inodes, add the iag to the
2346 * ag free inode list.
2348 if (iagp->nfreeinos == 0) {
2349 if (freei >= 0)
2350 ciagp->inofreeback = cpu_to_le32(iagno);
2352 iagp->inofreefwd =
2353 cpu_to_le32(imap->im_agctl[agno].inofree);
2354 iagp->inofreeback = cpu_to_le32(-1);
2355 imap->im_agctl[agno].inofree = iagno;
2358 /* initialize the extent descriptor of the extent. */
2359 PXDlength(&iagp->inoext[extno], imap->im_nbperiext);
2360 PXDaddress(&iagp->inoext[extno], blkno);
2362 /* initialize the working and persistent map of the extent.
2363 * the working map will be initialized such that
2364 * it indicates the first inode of the extent is allocated.
2366 iagp->wmap[extno] = cpu_to_le32(HIGHORDER);
2367 iagp->pmap[extno] = 0;
2369 /* update the free inode and free extent summary maps
2370 * for the extent to indicate the extent has free inodes
2371 * and no longer represents a free extent.
2373 sword = extno >> L2EXTSPERSUM;
2374 mask = HIGHORDER >> (extno & (EXTSPERSUM - 1));
2375 iagp->extsmap[sword] |= cpu_to_le32(mask);
2376 iagp->inosmap[sword] &= cpu_to_le32(~mask);
2378 /* update the free inode and free extent counts for the
2379 * iag.
2381 iagp->nfreeinos = cpu_to_le32(le32_to_cpu(iagp->nfreeinos) +
2382 (INOSPEREXT - 1));
2383 iagp->nfreeexts = cpu_to_le32(le32_to_cpu(iagp->nfreeexts) - 1);
2385 /* update the free and backed inode counts for the ag.
2387 imap->im_agctl[agno].numfree += (INOSPEREXT - 1);
2388 imap->im_agctl[agno].numinos += INOSPEREXT;
2390 /* update the free and backed inode counts for the inode map.
2392 atomic_add(INOSPEREXT - 1, &imap->im_numfree);
2393 atomic_add(INOSPEREXT, &imap->im_numinos);
2395 /* write the iags.
2397 if (amp)
2398 write_metapage(amp);
2399 if (bmp)
2400 write_metapage(bmp);
2401 if (cmp)
2402 write_metapage(cmp);
2404 return (0);
2406 error_out:
2408 /* release the iags.
2410 if (amp)
2411 release_metapage(amp);
2412 if (bmp)
2413 release_metapage(bmp);
2414 if (cmp)
2415 release_metapage(cmp);
2417 return (rc);
2422 * NAME: diNewIAG(imap,iagnop,agno)
2424 * FUNCTION: allocate a new iag for an allocation group.
2426 * first tries to allocate the iag from the inode map
2427 * iagfree list:
2428 * if the list has free iags, the head of the list is removed
2429 * and returned to satisfy the request.
2430 * if the inode map's iag free list is empty, the inode map
2431 * is extended to hold a new iag. this new iag is initialized
2432 * and returned to satisfy the request.
2434 * PARAMETERS:
2435 * imap - pointer to inode map control structure.
2436 * iagnop - pointer to an iag number set with the number of the
2437 * newly allocated iag upon successful return.
2438 * agno - allocation group number.
2439 * bpp - Buffer pointer to be filled in with new IAG's buffer
2441 * RETURN VALUES:
2442 * 0 - success.
2443 * -ENOSPC - insufficient disk resources.
2444 * -EIO - i/o error.
2446 * serialization:
2447 * AG lock held on entry/exit;
2448 * write lock on the map is held inside;
2449 * read lock on the map is held on successful completion;
2451 * note: new iag transaction:
2452 * . synchronously write iag;
2453 * . write log of xtree and inode of imap;
2454 * . commit;
2455 * . synchronous write of xtree (right to left, bottom to top);
2456 * . at start of logredo(): init in-memory imap with one additional iag page;
2457 * . at end of logredo(): re-read imap inode to determine
2458 * new imap size;
2460 static int
2461 diNewIAG(struct inomap * imap, int *iagnop, int agno, struct metapage ** mpp)
2463 int rc;
2464 int iagno, i, xlen;
2465 struct inode *ipimap;
2466 struct super_block *sb;
2467 struct jfs_sb_info *sbi;
2468 struct metapage *mp;
2469 struct iag *iagp;
2470 s64 xaddr = 0;
2471 s64 blkno;
2472 tid_t tid;
2473 struct inode *iplist[1];
2475 /* pick up pointers to the inode map and mount inodes */
2476 ipimap = imap->im_ipimap;
2477 sb = ipimap->i_sb;
2478 sbi = JFS_SBI(sb);
2480 /* acquire the free iag lock */
2481 IAGFREE_LOCK(imap);
2483 /* if there are any iags on the inode map free iag list,
2484 * allocate the iag from the head of the list.
2486 if (imap->im_freeiag >= 0) {
2487 /* pick up the iag number at the head of the list */
2488 iagno = imap->im_freeiag;
2490 /* determine the logical block number of the iag */
2491 blkno = IAGTOLBLK(iagno, sbi->l2nbperpage);
2492 } else {
2493 /* no free iags. the inode map will have to be extented
2494 * to include a new iag.
2497 /* acquire inode map lock */
2498 IWRITE_LOCK(ipimap, RDWRLOCK_IMAP);
2500 if (ipimap->i_size >> L2PSIZE != imap->im_nextiag + 1) {
2501 IWRITE_UNLOCK(ipimap);
2502 IAGFREE_UNLOCK(imap);
2503 jfs_error(imap->im_ipimap->i_sb,
2504 "diNewIAG: ipimap->i_size is wrong");
2505 return -EIO;
2509 /* get the next avaliable iag number */
2510 iagno = imap->im_nextiag;
2512 /* make sure that we have not exceeded the maximum inode
2513 * number limit.
2515 if (iagno > (MAXIAGS - 1)) {
2516 /* release the inode map lock */
2517 IWRITE_UNLOCK(ipimap);
2519 rc = -ENOSPC;
2520 goto out;
2524 * synchronously append new iag page.
2526 /* determine the logical address of iag page to append */
2527 blkno = IAGTOLBLK(iagno, sbi->l2nbperpage);
2529 /* Allocate extent for new iag page */
2530 xlen = sbi->nbperpage;
2531 if ((rc = dbAlloc(ipimap, 0, (s64) xlen, &xaddr))) {
2532 /* release the inode map lock */
2533 IWRITE_UNLOCK(ipimap);
2535 goto out;
2539 * start transaction of update of the inode map
2540 * addressing structure pointing to the new iag page;
2542 tid = txBegin(sb, COMMIT_FORCE);
2543 mutex_lock(&JFS_IP(ipimap)->commit_mutex);
2545 /* update the inode map addressing structure to point to it */
2546 if ((rc =
2547 xtInsert(tid, ipimap, 0, blkno, xlen, &xaddr, 0))) {
2548 txEnd(tid);
2549 mutex_unlock(&JFS_IP(ipimap)->commit_mutex);
2550 /* Free the blocks allocated for the iag since it was
2551 * not successfully added to the inode map
2553 dbFree(ipimap, xaddr, (s64) xlen);
2555 /* release the inode map lock */
2556 IWRITE_UNLOCK(ipimap);
2558 goto out;
2561 /* update the inode map's inode to reflect the extension */
2562 ipimap->i_size += PSIZE;
2563 inode_add_bytes(ipimap, PSIZE);
2565 /* assign a buffer for the page */
2566 mp = get_metapage(ipimap, blkno, PSIZE, 0);
2567 if (!mp) {
2569 * This is very unlikely since we just created the
2570 * extent, but let's try to handle it correctly
2572 xtTruncate(tid, ipimap, ipimap->i_size - PSIZE,
2573 COMMIT_PWMAP);
2575 txAbort(tid, 0);
2576 txEnd(tid);
2578 /* release the inode map lock */
2579 IWRITE_UNLOCK(ipimap);
2581 rc = -EIO;
2582 goto out;
2584 iagp = (struct iag *) mp->data;
2586 /* init the iag */
2587 memset(iagp, 0, sizeof(struct iag));
2588 iagp->iagnum = cpu_to_le32(iagno);
2589 iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
2590 iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
2591 iagp->iagfree = cpu_to_le32(-1);
2592 iagp->nfreeinos = 0;
2593 iagp->nfreeexts = cpu_to_le32(EXTSPERIAG);
2595 /* initialize the free inode summary map (free extent
2596 * summary map initialization handled by bzero).
2598 for (i = 0; i < SMAPSZ; i++)
2599 iagp->inosmap[i] = cpu_to_le32(ONES);
2602 * Write and sync the metapage
2604 flush_metapage(mp);
2607 * txCommit(COMMIT_FORCE) will synchronously write address
2608 * index pages and inode after commit in careful update order
2609 * of address index pages (right to left, bottom up);
2611 iplist[0] = ipimap;
2612 rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
2614 txEnd(tid);
2615 mutex_unlock(&JFS_IP(ipimap)->commit_mutex);
2617 duplicateIXtree(sb, blkno, xlen, &xaddr);
2619 /* update the next avaliable iag number */
2620 imap->im_nextiag += 1;
2622 /* Add the iag to the iag free list so we don't lose the iag
2623 * if a failure happens now.
2625 imap->im_freeiag = iagno;
2627 /* Until we have logredo working, we want the imap inode &
2628 * control page to be up to date.
2630 diSync(ipimap);
2632 /* release the inode map lock */
2633 IWRITE_UNLOCK(ipimap);
2636 /* obtain read lock on map */
2637 IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
2639 /* read the iag */
2640 if ((rc = diIAGRead(imap, iagno, &mp))) {
2641 IREAD_UNLOCK(ipimap);
2642 rc = -EIO;
2643 goto out;
2645 iagp = (struct iag *) mp->data;
2647 /* remove the iag from the iag free list */
2648 imap->im_freeiag = le32_to_cpu(iagp->iagfree);
2649 iagp->iagfree = cpu_to_le32(-1);
2651 /* set the return iag number and buffer pointer */
2652 *iagnop = iagno;
2653 *mpp = mp;
2655 out:
2656 /* release the iag free lock */
2657 IAGFREE_UNLOCK(imap);
2659 return (rc);
2663 * NAME: diIAGRead()
2665 * FUNCTION: get the buffer for the specified iag within a fileset
2666 * or aggregate inode map.
2668 * PARAMETERS:
2669 * imap - pointer to inode map control structure.
2670 * iagno - iag number.
2671 * bpp - point to buffer pointer to be filled in on successful
2672 * exit.
2674 * SERIALIZATION:
2675 * must have read lock on imap inode
2676 * (When called by diExtendFS, the filesystem is quiesced, therefore
2677 * the read lock is unnecessary.)
2679 * RETURN VALUES:
2680 * 0 - success.
2681 * -EIO - i/o error.
2683 static int diIAGRead(struct inomap * imap, int iagno, struct metapage ** mpp)
2685 struct inode *ipimap = imap->im_ipimap;
2686 s64 blkno;
2688 /* compute the logical block number of the iag. */
2689 blkno = IAGTOLBLK(iagno, JFS_SBI(ipimap->i_sb)->l2nbperpage);
2691 /* read the iag. */
2692 *mpp = read_metapage(ipimap, blkno, PSIZE, 0);
2693 if (*mpp == NULL) {
2694 return -EIO;
2697 return (0);
2701 * NAME: diFindFree()
2703 * FUNCTION: find the first free bit in a word starting at
2704 * the specified bit position.
2706 * PARAMETERS:
2707 * word - word to be examined.
2708 * start - starting bit position.
2710 * RETURN VALUES:
2711 * bit position of first free bit in the word or 32 if
2712 * no free bits were found.
2714 static int diFindFree(u32 word, int start)
2716 int bitno;
2717 assert(start < 32);
2718 /* scan the word for the first free bit. */
2719 for (word <<= start, bitno = start; bitno < 32;
2720 bitno++, word <<= 1) {
2721 if ((word & HIGHORDER) == 0)
2722 break;
2724 return (bitno);
2728 * NAME: diUpdatePMap()
2730 * FUNCTION: Update the persistent map in an IAG for the allocation or
2731 * freeing of the specified inode.
2733 * PRE CONDITIONS: Working map has already been updated for allocate.
2735 * PARAMETERS:
2736 * ipimap - Incore inode map inode
2737 * inum - Number of inode to mark in permanent map
2738 * is_free - If 'true' indicates inode should be marked freed, otherwise
2739 * indicates inode should be marked allocated.
2741 * RETURN VALUES:
2742 * 0 for success
2745 diUpdatePMap(struct inode *ipimap,
2746 unsigned long inum, bool is_free, struct tblock * tblk)
2748 int rc;
2749 struct iag *iagp;
2750 struct metapage *mp;
2751 int iagno, ino, extno, bitno;
2752 struct inomap *imap;
2753 u32 mask;
2754 struct jfs_log *log;
2755 int lsn, difft, diffp;
2756 unsigned long flags;
2758 imap = JFS_IP(ipimap)->i_imap;
2759 /* get the iag number containing the inode */
2760 iagno = INOTOIAG(inum);
2761 /* make sure that the iag is contained within the map */
2762 if (iagno >= imap->im_nextiag) {
2763 jfs_error(ipimap->i_sb,
2764 "diUpdatePMap: the iag is outside the map");
2765 return -EIO;
2767 /* read the iag */
2768 IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
2769 rc = diIAGRead(imap, iagno, &mp);
2770 IREAD_UNLOCK(ipimap);
2771 if (rc)
2772 return (rc);
2773 metapage_wait_for_io(mp);
2774 iagp = (struct iag *) mp->data;
2775 /* get the inode number and extent number of the inode within
2776 * the iag and the inode number within the extent.
2778 ino = inum & (INOSPERIAG - 1);
2779 extno = ino >> L2INOSPEREXT;
2780 bitno = ino & (INOSPEREXT - 1);
2781 mask = HIGHORDER >> bitno;
2783 * mark the inode free in persistent map:
2785 if (is_free) {
2786 /* The inode should have been allocated both in working
2787 * map and in persistent map;
2788 * the inode will be freed from working map at the release
2789 * of last reference release;
2791 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
2792 jfs_error(ipimap->i_sb,
2793 "diUpdatePMap: inode %ld not marked as "
2794 "allocated in wmap!", inum);
2796 if (!(le32_to_cpu(iagp->pmap[extno]) & mask)) {
2797 jfs_error(ipimap->i_sb,
2798 "diUpdatePMap: inode %ld not marked as "
2799 "allocated in pmap!", inum);
2801 /* update the bitmap for the extent of the freed inode */
2802 iagp->pmap[extno] &= cpu_to_le32(~mask);
2805 * mark the inode allocated in persistent map:
2807 else {
2808 /* The inode should be already allocated in the working map
2809 * and should be free in persistent map;
2811 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
2812 release_metapage(mp);
2813 jfs_error(ipimap->i_sb,
2814 "diUpdatePMap: the inode is not allocated in "
2815 "the working map");
2816 return -EIO;
2818 if ((le32_to_cpu(iagp->pmap[extno]) & mask) != 0) {
2819 release_metapage(mp);
2820 jfs_error(ipimap->i_sb,
2821 "diUpdatePMap: the inode is not free in the "
2822 "persistent map");
2823 return -EIO;
2825 /* update the bitmap for the extent of the allocated inode */
2826 iagp->pmap[extno] |= cpu_to_le32(mask);
2829 * update iag lsn
2831 lsn = tblk->lsn;
2832 log = JFS_SBI(tblk->sb)->log;
2833 LOGSYNC_LOCK(log, flags);
2834 if (mp->lsn != 0) {
2835 /* inherit older/smaller lsn */
2836 logdiff(difft, lsn, log);
2837 logdiff(diffp, mp->lsn, log);
2838 if (difft < diffp) {
2839 mp->lsn = lsn;
2840 /* move mp after tblock in logsync list */
2841 list_move(&mp->synclist, &tblk->synclist);
2843 /* inherit younger/larger clsn */
2844 assert(mp->clsn);
2845 logdiff(difft, tblk->clsn, log);
2846 logdiff(diffp, mp->clsn, log);
2847 if (difft > diffp)
2848 mp->clsn = tblk->clsn;
2849 } else {
2850 mp->log = log;
2851 mp->lsn = lsn;
2852 /* insert mp after tblock in logsync list */
2853 log->count++;
2854 list_add(&mp->synclist, &tblk->synclist);
2855 mp->clsn = tblk->clsn;
2857 LOGSYNC_UNLOCK(log, flags);
2858 write_metapage(mp);
2859 return (0);
2863 * diExtendFS()
2865 * function: update imap for extendfs();
2867 * note: AG size has been increased s.t. each k old contiguous AGs are
2868 * coalesced into a new AG;
2870 int diExtendFS(struct inode *ipimap, struct inode *ipbmap)
2872 int rc, rcx = 0;
2873 struct inomap *imap = JFS_IP(ipimap)->i_imap;
2874 struct iag *iagp = NULL, *hiagp = NULL;
2875 struct bmap *mp = JFS_SBI(ipbmap->i_sb)->bmap;
2876 struct metapage *bp, *hbp;
2877 int i, n, head;
2878 int numinos, xnuminos = 0, xnumfree = 0;
2879 s64 agstart;
2881 jfs_info("diExtendFS: nextiag:%d numinos:%d numfree:%d",
2882 imap->im_nextiag, atomic_read(&imap->im_numinos),
2883 atomic_read(&imap->im_numfree));
2886 * reconstruct imap
2888 * coalesce contiguous k (newAGSize/oldAGSize) AGs;
2889 * i.e., (AGi, ..., AGj) where i = k*n and j = k*(n+1) - 1 to AGn;
2890 * note: new AG size = old AG size * (2**x).
2893 /* init per AG control information im_agctl[] */
2894 for (i = 0; i < MAXAG; i++) {
2895 imap->im_agctl[i].inofree = -1;
2896 imap->im_agctl[i].extfree = -1;
2897 imap->im_agctl[i].numinos = 0; /* number of backed inodes */
2898 imap->im_agctl[i].numfree = 0; /* number of free backed inodes */
2902 * process each iag page of the map.
2904 * rebuild AG Free Inode List, AG Free Inode Extent List;
2906 for (i = 0; i < imap->im_nextiag; i++) {
2907 if ((rc = diIAGRead(imap, i, &bp))) {
2908 rcx = rc;
2909 continue;
2911 iagp = (struct iag *) bp->data;
2912 if (le32_to_cpu(iagp->iagnum) != i) {
2913 release_metapage(bp);
2914 jfs_error(ipimap->i_sb,
2915 "diExtendFs: unexpected value of iagnum");
2916 return -EIO;
2919 /* leave free iag in the free iag list */
2920 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2921 release_metapage(bp);
2922 continue;
2925 /* agstart that computes to the same ag is treated as same; */
2926 agstart = le64_to_cpu(iagp->agstart);
2927 /* iagp->agstart = agstart & ~(mp->db_agsize - 1); */
2928 n = agstart >> mp->db_agl2size;
2930 /* compute backed inodes */
2931 numinos = (EXTSPERIAG - le32_to_cpu(iagp->nfreeexts))
2932 << L2INOSPEREXT;
2933 if (numinos > 0) {
2934 /* merge AG backed inodes */
2935 imap->im_agctl[n].numinos += numinos;
2936 xnuminos += numinos;
2939 /* if any backed free inodes, insert at AG free inode list */
2940 if ((int) le32_to_cpu(iagp->nfreeinos) > 0) {
2941 if ((head = imap->im_agctl[n].inofree) == -1) {
2942 iagp->inofreefwd = cpu_to_le32(-1);
2943 iagp->inofreeback = cpu_to_le32(-1);
2944 } else {
2945 if ((rc = diIAGRead(imap, head, &hbp))) {
2946 rcx = rc;
2947 goto nextiag;
2949 hiagp = (struct iag *) hbp->data;
2950 hiagp->inofreeback = iagp->iagnum;
2951 iagp->inofreefwd = cpu_to_le32(head);
2952 iagp->inofreeback = cpu_to_le32(-1);
2953 write_metapage(hbp);
2956 imap->im_agctl[n].inofree =
2957 le32_to_cpu(iagp->iagnum);
2959 /* merge AG backed free inodes */
2960 imap->im_agctl[n].numfree +=
2961 le32_to_cpu(iagp->nfreeinos);
2962 xnumfree += le32_to_cpu(iagp->nfreeinos);
2965 /* if any free extents, insert at AG free extent list */
2966 if (le32_to_cpu(iagp->nfreeexts) > 0) {
2967 if ((head = imap->im_agctl[n].extfree) == -1) {
2968 iagp->extfreefwd = cpu_to_le32(-1);
2969 iagp->extfreeback = cpu_to_le32(-1);
2970 } else {
2971 if ((rc = diIAGRead(imap, head, &hbp))) {
2972 rcx = rc;
2973 goto nextiag;
2975 hiagp = (struct iag *) hbp->data;
2976 hiagp->extfreeback = iagp->iagnum;
2977 iagp->extfreefwd = cpu_to_le32(head);
2978 iagp->extfreeback = cpu_to_le32(-1);
2979 write_metapage(hbp);
2982 imap->im_agctl[n].extfree =
2983 le32_to_cpu(iagp->iagnum);
2986 nextiag:
2987 write_metapage(bp);
2990 if (xnuminos != atomic_read(&imap->im_numinos) ||
2991 xnumfree != atomic_read(&imap->im_numfree)) {
2992 jfs_error(ipimap->i_sb,
2993 "diExtendFs: numinos or numfree incorrect");
2994 return -EIO;
2997 return rcx;
3002 * duplicateIXtree()
3004 * serialization: IWRITE_LOCK held on entry/exit
3006 * note: shadow page with regular inode (rel.2);
3008 static void duplicateIXtree(struct super_block *sb, s64 blkno,
3009 int xlen, s64 *xaddr)
3011 struct jfs_superblock *j_sb;
3012 struct buffer_head *bh;
3013 struct inode *ip;
3014 tid_t tid;
3016 /* if AIT2 ipmap2 is bad, do not try to update it */
3017 if (JFS_SBI(sb)->mntflag & JFS_BAD_SAIT) /* s_flag */
3018 return;
3019 ip = diReadSpecial(sb, FILESYSTEM_I, 1);
3020 if (ip == NULL) {
3021 JFS_SBI(sb)->mntflag |= JFS_BAD_SAIT;
3022 if (readSuper(sb, &bh))
3023 return;
3024 j_sb = (struct jfs_superblock *)bh->b_data;
3025 j_sb->s_flag |= cpu_to_le32(JFS_BAD_SAIT);
3027 mark_buffer_dirty(bh);
3028 sync_dirty_buffer(bh);
3029 brelse(bh);
3030 return;
3033 /* start transaction */
3034 tid = txBegin(sb, COMMIT_FORCE);
3035 /* update the inode map addressing structure to point to it */
3036 if (xtInsert(tid, ip, 0, blkno, xlen, xaddr, 0)) {
3037 JFS_SBI(sb)->mntflag |= JFS_BAD_SAIT;
3038 txAbort(tid, 1);
3039 goto cleanup;
3042 /* update the inode map's inode to reflect the extension */
3043 ip->i_size += PSIZE;
3044 inode_add_bytes(ip, PSIZE);
3045 txCommit(tid, 1, &ip, COMMIT_FORCE);
3046 cleanup:
3047 txEnd(tid);
3048 diFreeSpecial(ip);
3052 * NAME: copy_from_dinode()
3054 * FUNCTION: Copies inode info from disk inode to in-memory inode
3056 * RETURN VALUES:
3057 * 0 - success
3058 * -ENOMEM - insufficient memory
3060 static int copy_from_dinode(struct dinode * dip, struct inode *ip)
3062 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
3063 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
3065 jfs_ip->fileset = le32_to_cpu(dip->di_fileset);
3066 jfs_ip->mode2 = le32_to_cpu(dip->di_mode);
3067 jfs_set_inode_flags(ip);
3069 ip->i_mode = le32_to_cpu(dip->di_mode) & 0xffff;
3070 if (sbi->umask != -1) {
3071 ip->i_mode = (ip->i_mode & ~0777) | (0777 & ~sbi->umask);
3072 /* For directories, add x permission if r is allowed by umask */
3073 if (S_ISDIR(ip->i_mode)) {
3074 if (ip->i_mode & 0400)
3075 ip->i_mode |= 0100;
3076 if (ip->i_mode & 0040)
3077 ip->i_mode |= 0010;
3078 if (ip->i_mode & 0004)
3079 ip->i_mode |= 0001;
3082 ip->i_nlink = le32_to_cpu(dip->di_nlink);
3084 jfs_ip->saved_uid = le32_to_cpu(dip->di_uid);
3085 if (sbi->uid == -1)
3086 ip->i_uid = jfs_ip->saved_uid;
3087 else {
3088 ip->i_uid = sbi->uid;
3091 jfs_ip->saved_gid = le32_to_cpu(dip->di_gid);
3092 if (sbi->gid == -1)
3093 ip->i_gid = jfs_ip->saved_gid;
3094 else {
3095 ip->i_gid = sbi->gid;
3098 ip->i_size = le64_to_cpu(dip->di_size);
3099 ip->i_atime.tv_sec = le32_to_cpu(dip->di_atime.tv_sec);
3100 ip->i_atime.tv_nsec = le32_to_cpu(dip->di_atime.tv_nsec);
3101 ip->i_mtime.tv_sec = le32_to_cpu(dip->di_mtime.tv_sec);
3102 ip->i_mtime.tv_nsec = le32_to_cpu(dip->di_mtime.tv_nsec);
3103 ip->i_ctime.tv_sec = le32_to_cpu(dip->di_ctime.tv_sec);
3104 ip->i_ctime.tv_nsec = le32_to_cpu(dip->di_ctime.tv_nsec);
3105 ip->i_blocks = LBLK2PBLK(ip->i_sb, le64_to_cpu(dip->di_nblocks));
3106 ip->i_generation = le32_to_cpu(dip->di_gen);
3108 jfs_ip->ixpxd = dip->di_ixpxd; /* in-memory pxd's are little-endian */
3109 jfs_ip->acl = dip->di_acl; /* as are dxd's */
3110 jfs_ip->ea = dip->di_ea;
3111 jfs_ip->next_index = le32_to_cpu(dip->di_next_index);
3112 jfs_ip->otime = le32_to_cpu(dip->di_otime.tv_sec);
3113 jfs_ip->acltype = le32_to_cpu(dip->di_acltype);
3115 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode)) {
3116 jfs_ip->dev = le32_to_cpu(dip->di_rdev);
3117 ip->i_rdev = new_decode_dev(jfs_ip->dev);
3120 if (S_ISDIR(ip->i_mode)) {
3121 memcpy(&jfs_ip->i_dirtable, &dip->di_dirtable, 384);
3122 } else if (S_ISREG(ip->i_mode) || S_ISLNK(ip->i_mode)) {
3123 memcpy(&jfs_ip->i_xtroot, &dip->di_xtroot, 288);
3124 } else
3125 memcpy(&jfs_ip->i_inline_ea, &dip->di_inlineea, 128);
3127 /* Zero the in-memory-only stuff */
3128 jfs_ip->cflag = 0;
3129 jfs_ip->btindex = 0;
3130 jfs_ip->btorder = 0;
3131 jfs_ip->bxflag = 0;
3132 jfs_ip->blid = 0;
3133 jfs_ip->atlhead = 0;
3134 jfs_ip->atltail = 0;
3135 jfs_ip->xtlid = 0;
3136 return (0);
3140 * NAME: copy_to_dinode()
3142 * FUNCTION: Copies inode info from in-memory inode to disk inode
3144 static void copy_to_dinode(struct dinode * dip, struct inode *ip)
3146 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
3147 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
3149 dip->di_fileset = cpu_to_le32(jfs_ip->fileset);
3150 dip->di_inostamp = cpu_to_le32(sbi->inostamp);
3151 dip->di_number = cpu_to_le32(ip->i_ino);
3152 dip->di_gen = cpu_to_le32(ip->i_generation);
3153 dip->di_size = cpu_to_le64(ip->i_size);
3154 dip->di_nblocks = cpu_to_le64(PBLK2LBLK(ip->i_sb, ip->i_blocks));
3155 dip->di_nlink = cpu_to_le32(ip->i_nlink);
3156 if (sbi->uid == -1)
3157 dip->di_uid = cpu_to_le32(ip->i_uid);
3158 else
3159 dip->di_uid = cpu_to_le32(jfs_ip->saved_uid);
3160 if (sbi->gid == -1)
3161 dip->di_gid = cpu_to_le32(ip->i_gid);
3162 else
3163 dip->di_gid = cpu_to_le32(jfs_ip->saved_gid);
3164 jfs_get_inode_flags(jfs_ip);
3166 * mode2 is only needed for storing the higher order bits.
3167 * Trust i_mode for the lower order ones
3169 if (sbi->umask == -1)
3170 dip->di_mode = cpu_to_le32((jfs_ip->mode2 & 0xffff0000) |
3171 ip->i_mode);
3172 else /* Leave the original permissions alone */
3173 dip->di_mode = cpu_to_le32(jfs_ip->mode2);
3175 dip->di_atime.tv_sec = cpu_to_le32(ip->i_atime.tv_sec);
3176 dip->di_atime.tv_nsec = cpu_to_le32(ip->i_atime.tv_nsec);
3177 dip->di_ctime.tv_sec = cpu_to_le32(ip->i_ctime.tv_sec);
3178 dip->di_ctime.tv_nsec = cpu_to_le32(ip->i_ctime.tv_nsec);
3179 dip->di_mtime.tv_sec = cpu_to_le32(ip->i_mtime.tv_sec);
3180 dip->di_mtime.tv_nsec = cpu_to_le32(ip->i_mtime.tv_nsec);
3181 dip->di_ixpxd = jfs_ip->ixpxd; /* in-memory pxd's are little-endian */
3182 dip->di_acl = jfs_ip->acl; /* as are dxd's */
3183 dip->di_ea = jfs_ip->ea;
3184 dip->di_next_index = cpu_to_le32(jfs_ip->next_index);
3185 dip->di_otime.tv_sec = cpu_to_le32(jfs_ip->otime);
3186 dip->di_otime.tv_nsec = 0;
3187 dip->di_acltype = cpu_to_le32(jfs_ip->acltype);
3188 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode))
3189 dip->di_rdev = cpu_to_le32(jfs_ip->dev);