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