OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [fs/] [jfs/] [jfs_imap.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.