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