blob: f5c5227b0d1f69081c959fbdee0d646aab0a47ab [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Dave Kleikamp63f83c92006-10-02 09:55:27 -05006 * the Free Software Foundation; either version 2 of the License, or
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * (at your option) any later version.
Dave Kleikamp63f83c92006-10-02 09:55:27 -05008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * 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
Dave Kleikamp63f83c92006-10-02 09:55:27 -050015 * along with this program; if not, write to the Free Software
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * 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"
Dave Kleikamp1868f4a2005-05-04 15:29:35 -050050#include "jfs_inode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#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/*
Dave Kleikampac17b8b2005-10-03 15:32:11 -050060 * __mark_inode_dirty expects inodes to be hashed. Since we don't want
61 * special inodes in the fileset inode space, we hash them to a dummy head
62 */
63static HLIST_HEAD(aggregate_hash);
64
65/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 * imap locks
67 */
68/* iag free list lock */
Ingo Molnar1de87442006-01-24 15:22:50 -060069#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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73/* per ag iag list locks */
Ingo Molnar1de87442006-01-24 15:22:50 -060074#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])
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 * forward references
80 */
Richard Knutsson4d817152006-09-30 23:27:14 -070081static int diAllocAG(struct inomap *, int, bool, struct inode *);
82static int diAllocAny(struct inomap *, int, bool, struct inode *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static int diAllocBit(struct inomap *, struct iag *, int);
84static int diAllocExt(struct inomap *, int, struct inode *);
85static int diAllocIno(struct inomap *, int, struct inode *);
86static int diFindFree(u32, int);
87static int diNewExt(struct inomap *, struct iag *, int);
88static int diNewIAG(struct inomap *, int *, int, struct metapage **);
89static void duplicateIXtree(struct super_block *, s64, int, s64 *);
90
91static int diIAGRead(struct inomap * imap, int, struct metapage **);
92static int copy_from_dinode(struct dinode *, struct inode *);
93static void copy_to_dinode(struct dinode *, struct inode *);
94
95/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -050096 * NAME: diMount()
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -050098 * FUNCTION: initialize the incore inode map control structures for
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 * a fileset or aggregate init time.
100 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500101 * the inode map's control structure (dinomap) is
102 * brought in from disk and placed in virtual memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 *
104 * PARAMETERS:
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500105 * ipimap - pointer to inode map inode for the aggregate or fileset.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 *
107 * RETURN VALUES:
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500108 * 0 - success
109 * -ENOMEM - insufficient free virtual memory.
110 * -EIO - i/o error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 */
112int 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. */
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800123 imap = kmalloc(sizeof(struct inomap), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 return (0);
179}
180
181
182/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500183 * NAME: diUnmount()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500185 * FUNCTION: write to disk the incore inode map control structures for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 * a fileset or aggregate at unmount time.
187 *
188 * PARAMETERS:
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500189 * ipimap - pointer to inode map inode for the aggregate or fileset.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 *
191 * RETURN VALUES:
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500192 * 0 - success
193 * -ENOMEM - insufficient free virtual memory.
194 * -EIO - i/o error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 */
196int 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 */
224int 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 */
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800268 filemap_write_and_wait(ipimap->i_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 diWriteSpecial(ipimap, 0);
271
272 return (0);
273}
274
275
276/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500277 * NAME: diRead()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500279 * FUNCTION: initialize an incore inode from disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 *
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).
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500284 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 * 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
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500288 * require special handling since much of the file system
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 * 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().
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500292 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 * 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:
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500301 * ip - pointer to incore inode to be initialized from disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 *
303 * RETURN VALUES:
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500304 * 0 - success
305 * -EIO - i/o error.
306 * -ENOMEM - insufficient memory
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500307 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 */
309int 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;
Dave Kleikamp8f6cff92006-10-13 12:42:36 -0500321 unsigned long pageno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 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;
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600334 IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 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 == 0) {
385 jfs_err("diRead: read_metapage failed");
386 return -EIO;
387 }
388
Michael Opdenacker59c51592007-05-09 08:57:56 +0200389 /* locate the disk inode requested */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 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/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500413 * NAME: diReadSpecial()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500415 * FUNCTION: initialize a 'special' inode from disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 *
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
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500421 * inode map, since these inodes are at a fixed location
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 * and in some cases the aggregate inode map isn't initialized
423 * yet.
424 *
425 * PARAMETERS:
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500426 * sb - filesystem superblock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 * inum - aggregate inode number
428 * secondary - 1 if secondary aggregate inode table
429 *
430 * RETURN VALUES:
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500431 * new inode - success
432 * NULL - i/o error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 */
434struct 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
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600485 ip->i_mapping->a_ops = &jfs_metapage_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 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
Dave Kleikampac17b8b2005-10-03 15:32:11 -0500499 hlist_add_head(&ip->i_hash, &aggregate_hash);
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return (ip);
502}
503
504/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500505 * NAME: diWriteSpecial()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500507 * FUNCTION: Write the special inode to disk
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 *
509 * PARAMETERS:
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500510 * ip - special inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 * secondary - 1 if secondary aggregate inode table
512 *
513 * RETURN VALUES: none
514 */
515
516void diWriteSpecial(struct inode *ip, int secondary)
517{
518 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
519 uint address;
520 struct dinode *dp;
521 ino_t inum = ip->i_ino;
522 struct metapage *mp;
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (secondary)
525 address = addressPXD(&sbi->ait2) >> sbi->l2nbperpage;
526 else
527 address = AITBL_OFF >> L2PSIZE;
528
529 ASSERT(inum < INOSPEREXT);
530
531 address += inum >> 3; /* 8 inodes per 4K page */
532
533 /* read the page of fixed disk inode (AIT) in raw mode */
534 mp = read_metapage(ip, address << sbi->l2nbperpage, PSIZE, 1);
535 if (mp == NULL) {
536 jfs_err("diWriteSpecial: failed to read aggregate inode "
537 "extent!");
538 return;
539 }
540
541 /* get the pointer to the disk inode of interest */
542 dp = (struct dinode *) (mp->data);
543 dp += inum % 8; /* 8 inodes per 4K page */
544
545 /* copy on-disk inode to in-memory inode */
546 copy_to_dinode(dp, ip);
547 memcpy(&dp->di_xtroot, &JFS_IP(ip)->i_xtroot, 288);
548
549 if (inum == FILESYSTEM_I)
550 dp->di_gengen = cpu_to_le32(sbi->gengen);
551
552 /* write the page */
553 write_metapage(mp);
554}
555
556/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500557 * NAME: diFreeSpecial()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500559 * FUNCTION: Free allocated space for special inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 */
561void diFreeSpecial(struct inode *ip)
562{
563 if (ip == NULL) {
564 jfs_err("diFreeSpecial called with NULL ip!");
565 return;
566 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800567 filemap_write_and_wait(ip->i_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 truncate_inode_pages(ip->i_mapping, 0);
569 iput(ip);
570}
571
572
573
574/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500575 * NAME: diWrite()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500577 * FUNCTION: write the on-disk inode portion of the in-memory inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 * to its corresponding on-disk inode.
579 *
580 * on entry, the specifed incore inode should itself
581 * specify the disk inode number corresponding to the
582 * incore inode (i.e. i_number should be initialized).
583 *
584 * the inode contains the inode extent address for the disk
585 * inode. with the inode extent address in hand, the
586 * page of the extent that contains the disk inode is
587 * read and the disk inode portion of the incore inode
588 * is copied to the disk inode.
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500589 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 * PARAMETERS:
591 * tid - transacation id
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500592 * ip - pointer to incore inode to be written to the inode extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 *
594 * RETURN VALUES:
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500595 * 0 - success
596 * -EIO - i/o error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 */
598int diWrite(tid_t tid, struct inode *ip)
599{
600 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
601 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
602 int rc = 0;
603 s32 ino;
604 struct dinode *dp;
605 s64 blkno;
606 int block_offset;
607 int inodes_left;
608 struct metapage *mp;
Dave Kleikamp8f6cff92006-10-13 12:42:36 -0500609 unsigned long pageno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 int rel_inode;
611 int dioffset;
612 struct inode *ipimap;
613 uint type;
614 lid_t lid;
615 struct tlock *ditlck, *tlck;
616 struct linelock *dilinelock, *ilinelock;
617 struct lv *lv;
618 int n;
619
620 ipimap = jfs_ip->ipimap;
621
622 ino = ip->i_ino & (INOSPERIAG - 1);
623
624 if (!addressPXD(&(jfs_ip->ixpxd)) ||
625 (lengthPXD(&(jfs_ip->ixpxd)) !=
626 JFS_IP(ipimap)->i_imap->im_nbperiext)) {
627 jfs_error(ip->i_sb, "diWrite: ixpxd invalid");
628 return -EIO;
629 }
630
631 /*
632 * read the page of disk inode containing the specified inode:
633 */
634 /* compute the block address of the page */
635 blkno = INOPBLK(&(jfs_ip->ixpxd), ino, sbi->l2nbperpage);
636
637 rel_inode = (ino & (INOSPERPAGE - 1));
638 pageno = blkno >> sbi->l2nbperpage;
639
640 if ((block_offset = ((u32) blkno & (sbi->nbperpage - 1)))) {
641 /*
642 * OS/2 didn't always align inode extents on page boundaries
643 */
644 inodes_left =
645 (sbi->nbperpage - block_offset) << sbi->l2niperblk;
646
647 if (rel_inode < inodes_left)
648 rel_inode += block_offset << sbi->l2niperblk;
649 else {
650 pageno += 1;
651 rel_inode -= inodes_left;
652 }
653 }
654 /* read the page of disk inode */
655 retry:
656 mp = read_metapage(ipimap, pageno << sbi->l2nbperpage, PSIZE, 1);
657 if (mp == 0)
658 return -EIO;
659
660 /* get the pointer to the disk inode */
661 dp = (struct dinode *) mp->data;
662 dp += rel_inode;
663
664 dioffset = (ino & (INOSPERPAGE - 1)) << L2DISIZE;
665
666 /*
667 * acquire transaction lock on the on-disk inode;
668 * N.B. tlock is acquired on ipimap not ip;
669 */
670 if ((ditlck =
671 txLock(tid, ipimap, mp, tlckINODE | tlckENTRY)) == NULL)
672 goto retry;
673 dilinelock = (struct linelock *) & ditlck->lock;
674
675 /*
676 * copy btree root from in-memory inode to on-disk inode
677 *
678 * (tlock is taken from inline B+-tree root in in-memory
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500679 * inode when the B+-tree root is updated, which is pointed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 * by jfs_ip->blid as well as being on tx tlock list)
681 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500682 * further processing of btree root is based on the copy
683 * in in-memory inode, where txLog() will log from, and,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 * for xtree root, txUpdateMap() will update map and reset
685 * XAD_NEW bit;
686 */
687
688 if (S_ISDIR(ip->i_mode) && (lid = jfs_ip->xtlid)) {
689 /*
690 * This is the special xtree inside the directory for storing
691 * the directory table
692 */
693 xtpage_t *p, *xp;
694 xad_t *xad;
695
696 jfs_ip->xtlid = 0;
697 tlck = lid_to_tlock(lid);
698 assert(tlck->type & tlckXTREE);
699 tlck->type |= tlckBTROOT;
700 tlck->mp = mp;
701 ilinelock = (struct linelock *) & tlck->lock;
702
703 /*
704 * copy xtree root from inode to dinode:
705 */
706 p = &jfs_ip->i_xtroot;
707 xp = (xtpage_t *) &dp->di_dirtable;
708 lv = ilinelock->lv;
709 for (n = 0; n < ilinelock->index; n++, lv++) {
710 memcpy(&xp->xad[lv->offset], &p->xad[lv->offset],
711 lv->length << L2XTSLOTSIZE);
712 }
713
714 /* reset on-disk (metadata page) xtree XAD_NEW bit */
715 xad = &xp->xad[XTENTRYSTART];
716 for (n = XTENTRYSTART;
717 n < le16_to_cpu(xp->header.nextindex); n++, xad++)
718 if (xad->flag & (XAD_NEW | XAD_EXTENDED))
719 xad->flag &= ~(XAD_NEW | XAD_EXTENDED);
720 }
721
722 if ((lid = jfs_ip->blid) == 0)
723 goto inlineData;
724 jfs_ip->blid = 0;
725
726 tlck = lid_to_tlock(lid);
727 type = tlck->type;
728 tlck->type |= tlckBTROOT;
729 tlck->mp = mp;
730 ilinelock = (struct linelock *) & tlck->lock;
731
732 /*
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500733 * regular file: 16 byte (XAD slot) granularity
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 */
735 if (type & tlckXTREE) {
736 xtpage_t *p, *xp;
737 xad_t *xad;
738
739 /*
740 * copy xtree root from inode to dinode:
741 */
742 p = &jfs_ip->i_xtroot;
743 xp = &dp->di_xtroot;
744 lv = ilinelock->lv;
745 for (n = 0; n < ilinelock->index; n++, lv++) {
746 memcpy(&xp->xad[lv->offset], &p->xad[lv->offset],
747 lv->length << L2XTSLOTSIZE);
748 }
749
750 /* reset on-disk (metadata page) xtree XAD_NEW bit */
751 xad = &xp->xad[XTENTRYSTART];
752 for (n = XTENTRYSTART;
753 n < le16_to_cpu(xp->header.nextindex); n++, xad++)
754 if (xad->flag & (XAD_NEW | XAD_EXTENDED))
755 xad->flag &= ~(XAD_NEW | XAD_EXTENDED);
756 }
757 /*
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500758 * directory: 32 byte (directory entry slot) granularity
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 */
760 else if (type & tlckDTREE) {
761 dtpage_t *p, *xp;
762
763 /*
764 * copy dtree root from inode to dinode:
765 */
766 p = (dtpage_t *) &jfs_ip->i_dtroot;
767 xp = (dtpage_t *) & dp->di_dtroot;
768 lv = ilinelock->lv;
769 for (n = 0; n < ilinelock->index; n++, lv++) {
770 memcpy(&xp->slot[lv->offset], &p->slot[lv->offset],
771 lv->length << L2DTSLOTSIZE);
772 }
773 } else {
774 jfs_err("diWrite: UFO tlock");
775 }
776
777 inlineData:
778 /*
779 * copy inline symlink from in-memory inode to on-disk inode
780 */
781 if (S_ISLNK(ip->i_mode) && ip->i_size < IDATASIZE) {
782 lv = & dilinelock->lv[dilinelock->index];
783 lv->offset = (dioffset + 2 * 128) >> L2INODESLOTSIZE;
784 lv->length = 2;
785 memcpy(&dp->di_fastsymlink, jfs_ip->i_inline, IDATASIZE);
786 dilinelock->index++;
787 }
788 /*
789 * copy inline data from in-memory inode to on-disk inode:
790 * 128 byte slot granularity
791 */
792 if (test_cflag(COMMIT_Inlineea, ip)) {
793 lv = & dilinelock->lv[dilinelock->index];
794 lv->offset = (dioffset + 3 * 128) >> L2INODESLOTSIZE;
795 lv->length = 1;
796 memcpy(&dp->di_inlineea, jfs_ip->i_inline_ea, INODESLOTSIZE);
797 dilinelock->index++;
798
799 clear_cflag(COMMIT_Inlineea, ip);
800 }
801
802 /*
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500803 * lock/copy inode base: 128 byte slot granularity
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 lv = & dilinelock->lv[dilinelock->index];
806 lv->offset = dioffset >> L2INODESLOTSIZE;
807 copy_to_dinode(dp, ip);
808 if (test_and_clear_cflag(COMMIT_Dirtable, ip)) {
809 lv->length = 2;
810 memcpy(&dp->di_dirtable, &jfs_ip->i_dirtable, 96);
811 } else
812 lv->length = 1;
813 dilinelock->index++;
814
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500815 /* release the buffer holding the updated on-disk inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 * the buffer will be later written by commit processing.
817 */
818 write_metapage(mp);
819
820 return (rc);
821}
822
823
824/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500825 * NAME: diFree(ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500827 * FUNCTION: free a specified inode from the inode working map
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 * for a fileset or aggregate.
829 *
830 * if the inode to be freed represents the first (only)
831 * free inode within the iag, the iag will be placed on
832 * the ag free inode list.
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500833 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 * freeing the inode will cause the inode extent to be
835 * freed if the inode is the only allocated inode within
836 * the extent. in this case all the disk resource backing
837 * up the inode extent will be freed. in addition, the iag
838 * will be placed on the ag extent free list if the extent
839 * is the first free extent in the iag. if freeing the
840 * extent also means that no free inodes will exist for
841 * the iag, the iag will also be removed from the ag free
842 * inode list.
843 *
844 * the iag describing the inode will be freed if the extent
845 * is to be freed and it is the only backed extent within
846 * the iag. in this case, the iag will be removed from the
847 * ag free extent list and ag free inode list and placed on
848 * the inode map's free iag list.
849 *
850 * a careful update approach is used to provide consistency
851 * in the face of updates to multiple buffers. under this
852 * approach, all required buffers are obtained before making
853 * any updates and are held until all updates are complete.
854 *
855 * PARAMETERS:
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500856 * ip - inode to be freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 *
858 * RETURN VALUES:
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500859 * 0 - success
860 * -EIO - i/o error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 */
862int diFree(struct inode *ip)
863{
864 int rc;
865 ino_t inum = ip->i_ino;
866 struct iag *iagp, *aiagp, *biagp, *ciagp, *diagp;
867 struct metapage *mp, *amp, *bmp, *cmp, *dmp;
868 int iagno, ino, extno, bitno, sword, agno;
869 int back, fwd;
870 u32 bitmap, mask;
871 struct inode *ipimap = JFS_SBI(ip->i_sb)->ipimap;
872 struct inomap *imap = JFS_IP(ipimap)->i_imap;
873 pxd_t freepxd;
874 tid_t tid;
875 struct inode *iplist[3];
876 struct tlock *tlck;
877 struct pxd_lock *pxdlock;
878
879 /*
880 * This is just to suppress compiler warnings. The same logic that
881 * references these variables is used to initialize them.
882 */
883 aiagp = biagp = ciagp = diagp = NULL;
884
885 /* get the iag number containing the inode.
886 */
887 iagno = INOTOIAG(inum);
888
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500889 /* make sure that the iag is contained within
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 * the map.
891 */
892 if (iagno >= imap->im_nextiag) {
893 dump_mem("imap", imap, 32);
894 jfs_error(ip->i_sb,
895 "diFree: inum = %d, iagno = %d, nextiag = %d",
896 (uint) inum, iagno, imap->im_nextiag);
897 return -EIO;
898 }
899
900 /* get the allocation group for this ino.
901 */
902 agno = JFS_IP(ip)->agno;
903
904 /* Lock the AG specific inode map information
905 */
906 AG_LOCK(imap, agno);
907
908 /* Obtain read lock in imap inode. Don't release it until we have
909 * read all of the IAG's that we are going to.
910 */
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600911 IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 /* read the iag.
914 */
915 if ((rc = diIAGRead(imap, iagno, &mp))) {
916 IREAD_UNLOCK(ipimap);
917 AG_UNLOCK(imap, agno);
918 return (rc);
919 }
920 iagp = (struct iag *) mp->data;
921
922 /* get the inode number and extent number of the inode within
923 * the iag and the inode number within the extent.
924 */
925 ino = inum & (INOSPERIAG - 1);
926 extno = ino >> L2INOSPEREXT;
927 bitno = ino & (INOSPEREXT - 1);
928 mask = HIGHORDER >> bitno;
929
930 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
931 jfs_error(ip->i_sb,
932 "diFree: wmap shows inode already free");
933 }
934
935 if (!addressPXD(&iagp->inoext[extno])) {
936 release_metapage(mp);
937 IREAD_UNLOCK(ipimap);
938 AG_UNLOCK(imap, agno);
939 jfs_error(ip->i_sb, "diFree: invalid inoext");
940 return -EIO;
941 }
942
943 /* compute the bitmap for the extent reflecting the freed inode.
944 */
945 bitmap = le32_to_cpu(iagp->wmap[extno]) & ~mask;
946
947 if (imap->im_agctl[agno].numfree > imap->im_agctl[agno].numinos) {
948 release_metapage(mp);
949 IREAD_UNLOCK(ipimap);
950 AG_UNLOCK(imap, agno);
951 jfs_error(ip->i_sb, "diFree: numfree > numinos");
952 return -EIO;
953 }
954 /*
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500955 * inode extent still has some inodes or below low water mark:
956 * keep the inode extent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 */
958 if (bitmap ||
959 imap->im_agctl[agno].numfree < 96 ||
960 (imap->im_agctl[agno].numfree < 288 &&
961 (((imap->im_agctl[agno].numfree * 100) /
962 imap->im_agctl[agno].numinos) <= 25))) {
963 /* if the iag currently has no free inodes (i.e.,
964 * the inode being freed is the first free inode of iag),
965 * insert the iag at head of the inode free list for the ag.
966 */
967 if (iagp->nfreeinos == 0) {
968 /* check if there are any iags on the ag inode
969 * free list. if so, read the first one so that
970 * we can link the current iag onto the list at
971 * the head.
972 */
973 if ((fwd = imap->im_agctl[agno].inofree) >= 0) {
974 /* read the iag that currently is the head
975 * of the list.
976 */
977 if ((rc = diIAGRead(imap, fwd, &amp))) {
978 IREAD_UNLOCK(ipimap);
979 AG_UNLOCK(imap, agno);
980 release_metapage(mp);
981 return (rc);
982 }
983 aiagp = (struct iag *) amp->data;
984
985 /* make current head point back to the iag.
986 */
987 aiagp->inofreeback = cpu_to_le32(iagno);
988
989 write_metapage(amp);
990 }
991
992 /* iag points forward to current head and iag
993 * becomes the new head of the list.
994 */
995 iagp->inofreefwd =
996 cpu_to_le32(imap->im_agctl[agno].inofree);
997 iagp->inofreeback = cpu_to_le32(-1);
998 imap->im_agctl[agno].inofree = iagno;
999 }
1000 IREAD_UNLOCK(ipimap);
1001
1002 /* update the free inode summary map for the extent if
1003 * freeing the inode means the extent will now have free
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001004 * inodes (i.e., the inode being freed is the first free
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 * inode of extent),
1006 */
1007 if (iagp->wmap[extno] == cpu_to_le32(ONES)) {
1008 sword = extno >> L2EXTSPERSUM;
1009 bitno = extno & (EXTSPERSUM - 1);
1010 iagp->inosmap[sword] &=
1011 cpu_to_le32(~(HIGHORDER >> bitno));
1012 }
1013
1014 /* update the bitmap.
1015 */
1016 iagp->wmap[extno] = cpu_to_le32(bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
1018 /* update the free inode counts at the iag, ag and
1019 * map level.
1020 */
1021 iagp->nfreeinos =
1022 cpu_to_le32(le32_to_cpu(iagp->nfreeinos) + 1);
1023 imap->im_agctl[agno].numfree += 1;
1024 atomic_inc(&imap->im_numfree);
1025
1026 /* release the AG inode map lock
1027 */
1028 AG_UNLOCK(imap, agno);
1029
1030 /* write the iag */
1031 write_metapage(mp);
1032
1033 return (0);
1034 }
1035
1036
1037 /*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001038 * inode extent has become free and above low water mark:
1039 * free the inode extent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 */
1041
1042 /*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001043 * prepare to update iag list(s) (careful update step 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 */
1045 amp = bmp = cmp = dmp = NULL;
1046 fwd = back = -1;
1047
1048 /* check if the iag currently has no free extents. if so,
1049 * it will be placed on the head of the ag extent free list.
1050 */
1051 if (iagp->nfreeexts == 0) {
1052 /* check if the ag extent free list has any iags.
1053 * if so, read the iag at the head of the list now.
1054 * this (head) iag will be updated later to reflect
1055 * the addition of the current iag at the head of
1056 * the list.
1057 */
1058 if ((fwd = imap->im_agctl[agno].extfree) >= 0) {
1059 if ((rc = diIAGRead(imap, fwd, &amp)))
1060 goto error_out;
1061 aiagp = (struct iag *) amp->data;
1062 }
1063 } else {
1064 /* iag has free extents. check if the addition of a free
1065 * extent will cause all extents to be free within this
1066 * iag. if so, the iag will be removed from the ag extent
1067 * free list and placed on the inode map's free iag list.
1068 */
1069 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG - 1)) {
1070 /* in preparation for removing the iag from the
1071 * ag extent free list, read the iags preceeding
1072 * and following the iag on the ag extent free
1073 * list.
1074 */
1075 if ((fwd = le32_to_cpu(iagp->extfreefwd)) >= 0) {
1076 if ((rc = diIAGRead(imap, fwd, &amp)))
1077 goto error_out;
1078 aiagp = (struct iag *) amp->data;
1079 }
1080
1081 if ((back = le32_to_cpu(iagp->extfreeback)) >= 0) {
1082 if ((rc = diIAGRead(imap, back, &bmp)))
1083 goto error_out;
1084 biagp = (struct iag *) bmp->data;
1085 }
1086 }
1087 }
1088
1089 /* remove the iag from the ag inode free list if freeing
1090 * this extent cause the iag to have no free inodes.
1091 */
1092 if (iagp->nfreeinos == cpu_to_le32(INOSPEREXT - 1)) {
1093 int inofreeback = le32_to_cpu(iagp->inofreeback);
1094 int inofreefwd = le32_to_cpu(iagp->inofreefwd);
1095
1096 /* in preparation for removing the iag from the
1097 * ag inode free list, read the iags preceeding
1098 * and following the iag on the ag inode free
1099 * list. before reading these iags, we must make
1100 * sure that we already don't have them in hand
1101 * from up above, since re-reading an iag (buffer)
1102 * we are currently holding would cause a deadlock.
1103 */
1104 if (inofreefwd >= 0) {
1105
1106 if (inofreefwd == fwd)
1107 ciagp = (struct iag *) amp->data;
1108 else if (inofreefwd == back)
1109 ciagp = (struct iag *) bmp->data;
1110 else {
1111 if ((rc =
1112 diIAGRead(imap, inofreefwd, &cmp)))
1113 goto error_out;
1114 ciagp = (struct iag *) cmp->data;
1115 }
1116 assert(ciagp != NULL);
1117 }
1118
1119 if (inofreeback >= 0) {
1120 if (inofreeback == fwd)
1121 diagp = (struct iag *) amp->data;
1122 else if (inofreeback == back)
1123 diagp = (struct iag *) bmp->data;
1124 else {
1125 if ((rc =
1126 diIAGRead(imap, inofreeback, &dmp)))
1127 goto error_out;
1128 diagp = (struct iag *) dmp->data;
1129 }
1130 assert(diagp != NULL);
1131 }
1132 }
1133
1134 IREAD_UNLOCK(ipimap);
1135
1136 /*
1137 * invalidate any page of the inode extent freed from buffer cache;
1138 */
1139 freepxd = iagp->inoext[extno];
1140 invalidate_pxd_metapages(ip, freepxd);
1141
1142 /*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001143 * update iag list(s) (careful update step 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 */
1145 /* add the iag to the ag extent free list if this is the
1146 * first free extent for the iag.
1147 */
1148 if (iagp->nfreeexts == 0) {
1149 if (fwd >= 0)
1150 aiagp->extfreeback = cpu_to_le32(iagno);
1151
1152 iagp->extfreefwd =
1153 cpu_to_le32(imap->im_agctl[agno].extfree);
1154 iagp->extfreeback = cpu_to_le32(-1);
1155 imap->im_agctl[agno].extfree = iagno;
1156 } else {
1157 /* remove the iag from the ag extent list if all extents
1158 * are now free and place it on the inode map iag free list.
1159 */
1160 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG - 1)) {
1161 if (fwd >= 0)
1162 aiagp->extfreeback = iagp->extfreeback;
1163
1164 if (back >= 0)
1165 biagp->extfreefwd = iagp->extfreefwd;
1166 else
1167 imap->im_agctl[agno].extfree =
1168 le32_to_cpu(iagp->extfreefwd);
1169
1170 iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
1171
1172 IAGFREE_LOCK(imap);
1173 iagp->iagfree = cpu_to_le32(imap->im_freeiag);
1174 imap->im_freeiag = iagno;
1175 IAGFREE_UNLOCK(imap);
1176 }
1177 }
1178
1179 /* remove the iag from the ag inode free list if freeing
1180 * this extent causes the iag to have no free inodes.
1181 */
1182 if (iagp->nfreeinos == cpu_to_le32(INOSPEREXT - 1)) {
1183 if ((int) le32_to_cpu(iagp->inofreefwd) >= 0)
1184 ciagp->inofreeback = iagp->inofreeback;
1185
1186 if ((int) le32_to_cpu(iagp->inofreeback) >= 0)
1187 diagp->inofreefwd = iagp->inofreefwd;
1188 else
1189 imap->im_agctl[agno].inofree =
1190 le32_to_cpu(iagp->inofreefwd);
1191
1192 iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
1193 }
1194
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001195 /* update the inode extent address and working map
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 * to reflect the free extent.
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001197 * the permanent map should have been updated already
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 * for the inode being freed.
1199 */
1200 if (iagp->pmap[extno] != 0) {
1201 jfs_error(ip->i_sb, "diFree: the pmap does not show inode free");
1202 }
1203 iagp->wmap[extno] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 PXDlength(&iagp->inoext[extno], 0);
1205 PXDaddress(&iagp->inoext[extno], 0);
1206
1207 /* update the free extent and free inode summary maps
1208 * to reflect the freed extent.
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001209 * the inode summary map is marked to indicate no inodes
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 * available for the freed extent.
1211 */
1212 sword = extno >> L2EXTSPERSUM;
1213 bitno = extno & (EXTSPERSUM - 1);
1214 mask = HIGHORDER >> bitno;
1215 iagp->inosmap[sword] |= cpu_to_le32(mask);
1216 iagp->extsmap[sword] &= cpu_to_le32(~mask);
1217
1218 /* update the number of free inodes and number of free extents
1219 * for the iag.
1220 */
1221 iagp->nfreeinos = cpu_to_le32(le32_to_cpu(iagp->nfreeinos) -
1222 (INOSPEREXT - 1));
1223 iagp->nfreeexts = cpu_to_le32(le32_to_cpu(iagp->nfreeexts) + 1);
1224
1225 /* update the number of free inodes and backed inodes
1226 * at the ag and inode map level.
1227 */
1228 imap->im_agctl[agno].numfree -= (INOSPEREXT - 1);
1229 imap->im_agctl[agno].numinos -= INOSPEREXT;
1230 atomic_sub(INOSPEREXT - 1, &imap->im_numfree);
1231 atomic_sub(INOSPEREXT, &imap->im_numinos);
1232
1233 if (amp)
1234 write_metapage(amp);
1235 if (bmp)
1236 write_metapage(bmp);
1237 if (cmp)
1238 write_metapage(cmp);
1239 if (dmp)
1240 write_metapage(dmp);
1241
1242 /*
1243 * start transaction to update block allocation map
1244 * for the inode extent freed;
1245 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001246 * N.B. AG_LOCK is released and iag will be released below, and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 * other thread may allocate inode from/reusing the ixad freed
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001248 * BUT with new/different backing inode extent from the extent
1249 * to be freed by the transaction;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 */
1251 tid = txBegin(ipimap->i_sb, COMMIT_FORCE);
Ingo Molnar1de87442006-01-24 15:22:50 -06001252 mutex_lock(&JFS_IP(ipimap)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001254 /* acquire tlock of the iag page of the freed ixad
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 * to force the page NOHOMEOK (even though no data is
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001256 * logged from the iag page) until NOREDOPAGE|FREEXTENT log
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 * for the free of the extent is committed;
1258 * write FREEXTENT|NOREDOPAGE log record
1259 * N.B. linelock is overlaid as freed extent descriptor;
1260 */
1261 tlck = txLock(tid, ipimap, mp, tlckINODE | tlckFREE);
1262 pxdlock = (struct pxd_lock *) & tlck->lock;
1263 pxdlock->flag = mlckFREEPXD;
1264 pxdlock->pxd = freepxd;
1265 pxdlock->index = 1;
1266
1267 write_metapage(mp);
1268
1269 iplist[0] = ipimap;
1270
1271 /*
1272 * logredo needs the IAG number and IAG extent index in order
1273 * to ensure that the IMap is consistent. The least disruptive
1274 * way to pass these values through to the transaction manager
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001275 * is in the iplist array.
1276 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 * It's not pretty, but it works.
1278 */
1279 iplist[1] = (struct inode *) (size_t)iagno;
1280 iplist[2] = (struct inode *) (size_t)extno;
1281
1282 rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
1283
1284 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -06001285 mutex_unlock(&JFS_IP(ipimap)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287 /* unlock the AG inode map information */
1288 AG_UNLOCK(imap, agno);
1289
1290 return (0);
1291
1292 error_out:
1293 IREAD_UNLOCK(ipimap);
1294
1295 if (amp)
1296 release_metapage(amp);
1297 if (bmp)
1298 release_metapage(bmp);
1299 if (cmp)
1300 release_metapage(cmp);
1301 if (dmp)
1302 release_metapage(dmp);
1303
1304 AG_UNLOCK(imap, agno);
1305
1306 release_metapage(mp);
1307
1308 return (rc);
1309}
1310
1311/*
1312 * There are several places in the diAlloc* routines where we initialize
1313 * the inode.
1314 */
1315static inline void
1316diInitInode(struct inode *ip, int iagno, int ino, int extno, struct iag * iagp)
1317{
1318 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
1319 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
1320
1321 ip->i_ino = (iagno << L2INOSPERIAG) + ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 jfs_ip->ixpxd = iagp->inoext[extno];
1323 jfs_ip->agno = BLKTOAG(le64_to_cpu(iagp->agstart), sbi);
1324 jfs_ip->active_ag = -1;
1325}
1326
1327
1328/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001329 * NAME: diAlloc(pip,dir,ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001331 * FUNCTION: allocate a disk inode from the inode working map
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 * for a fileset or aggregate.
1333 *
1334 * PARAMETERS:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001335 * pip - pointer to incore inode for the parent inode.
1336 * dir - 'true' if the new disk inode is for a directory.
1337 * ip - pointer to a new inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 *
1339 * RETURN VALUES:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001340 * 0 - success.
1341 * -ENOSPC - insufficient disk resources.
1342 * -EIO - i/o error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 */
Richard Knutsson4d817152006-09-30 23:27:14 -07001344int diAlloc(struct inode *pip, bool dir, struct inode *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
1346 int rc, ino, iagno, addext, extno, bitno, sword;
1347 int nwords, rem, i, agno;
1348 u32 mask, inosmap, extsmap;
1349 struct inode *ipimap;
1350 struct metapage *mp;
1351 ino_t inum;
1352 struct iag *iagp;
1353 struct inomap *imap;
1354
1355 /* get the pointers to the inode map inode and the
1356 * corresponding imap control structure.
1357 */
1358 ipimap = JFS_SBI(pip->i_sb)->ipimap;
1359 imap = JFS_IP(ipimap)->i_imap;
1360 JFS_IP(ip)->ipimap = ipimap;
1361 JFS_IP(ip)->fileset = FILESYSTEM_I;
1362
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001363 /* for a directory, the allocation policy is to start
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 * at the ag level using the preferred ag.
1365 */
Richard Knutsson4d817152006-09-30 23:27:14 -07001366 if (dir) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 agno = dbNextAG(JFS_SBI(pip->i_sb)->ipbmap);
1368 AG_LOCK(imap, agno);
1369 goto tryag;
1370 }
1371
1372 /* for files, the policy starts off by trying to allocate from
1373 * the same iag containing the parent disk inode:
1374 * try to allocate the new disk inode close to the parent disk
1375 * inode, using parent disk inode number + 1 as the allocation
1376 * hint. (we use a left-to-right policy to attempt to avoid
1377 * moving backward on the disk.) compute the hint within the
1378 * file system and the iag.
1379 */
1380
1381 /* get the ag number of this iag */
1382 agno = JFS_IP(pip)->agno;
1383
1384 if (atomic_read(&JFS_SBI(pip->i_sb)->bmap->db_active[agno])) {
1385 /*
1386 * There is an open file actively growing. We want to
1387 * allocate new inodes from a different ag to avoid
1388 * fragmentation problems.
1389 */
1390 agno = dbNextAG(JFS_SBI(pip->i_sb)->ipbmap);
1391 AG_LOCK(imap, agno);
1392 goto tryag;
1393 }
1394
1395 inum = pip->i_ino + 1;
1396 ino = inum & (INOSPERIAG - 1);
1397
Michael Opdenacker59c51592007-05-09 08:57:56 +02001398 /* back off the hint if it is outside of the iag */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 if (ino == 0)
1400 inum = pip->i_ino;
1401
1402 /* lock the AG inode map information */
1403 AG_LOCK(imap, agno);
1404
1405 /* Get read lock on imap inode */
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001406 IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 /* get the iag number and read the iag */
1409 iagno = INOTOIAG(inum);
1410 if ((rc = diIAGRead(imap, iagno, &mp))) {
1411 IREAD_UNLOCK(ipimap);
1412 AG_UNLOCK(imap, agno);
1413 return (rc);
1414 }
1415 iagp = (struct iag *) mp->data;
1416
1417 /* determine if new inode extent is allowed to be added to the iag.
1418 * new inode extent can be added to the iag if the ag
1419 * has less than 32 free disk inodes and the iag has free extents.
1420 */
1421 addext = (imap->im_agctl[agno].numfree < 32 && iagp->nfreeexts);
1422
1423 /*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001424 * try to allocate from the IAG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 */
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001426 /* check if the inode may be allocated from the iag
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 * (i.e. the inode has free inodes or new extent can be added).
1428 */
1429 if (iagp->nfreeinos || addext) {
1430 /* determine the extent number of the hint.
1431 */
1432 extno = ino >> L2INOSPEREXT;
1433
1434 /* check if the extent containing the hint has backed
1435 * inodes. if so, try to allocate within this extent.
1436 */
1437 if (addressPXD(&iagp->inoext[extno])) {
1438 bitno = ino & (INOSPEREXT - 1);
1439 if ((bitno =
1440 diFindFree(le32_to_cpu(iagp->wmap[extno]),
1441 bitno))
1442 < INOSPEREXT) {
1443 ino = (extno << L2INOSPEREXT) + bitno;
1444
1445 /* a free inode (bit) was found within this
1446 * extent, so allocate it.
1447 */
1448 rc = diAllocBit(imap, iagp, ino);
1449 IREAD_UNLOCK(ipimap);
1450 if (rc) {
1451 assert(rc == -EIO);
1452 } else {
1453 /* set the results of the allocation
1454 * and write the iag.
1455 */
1456 diInitInode(ip, iagno, ino, extno,
1457 iagp);
1458 mark_metapage_dirty(mp);
1459 }
1460 release_metapage(mp);
1461
1462 /* free the AG lock and return.
1463 */
1464 AG_UNLOCK(imap, agno);
1465 return (rc);
1466 }
1467
1468 if (!addext)
1469 extno =
1470 (extno ==
1471 EXTSPERIAG - 1) ? 0 : extno + 1;
1472 }
1473
1474 /*
1475 * no free inodes within the extent containing the hint.
1476 *
1477 * try to allocate from the backed extents following
1478 * hint or, if appropriate (i.e. addext is true), allocate
1479 * an extent of free inodes at or following the extent
1480 * containing the hint.
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001481 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 * the free inode and free extent summary maps are used
1483 * here, so determine the starting summary map position
1484 * and the number of words we'll have to examine. again,
1485 * the approach is to allocate following the hint, so we
1486 * might have to initially ignore prior bits of the summary
1487 * map that represent extents prior to the extent containing
1488 * the hint and later revisit these bits.
1489 */
1490 bitno = extno & (EXTSPERSUM - 1);
1491 nwords = (bitno == 0) ? SMAPSZ : SMAPSZ + 1;
1492 sword = extno >> L2EXTSPERSUM;
1493
1494 /* mask any prior bits for the starting words of the
1495 * summary map.
1496 */
1497 mask = ONES << (EXTSPERSUM - bitno);
1498 inosmap = le32_to_cpu(iagp->inosmap[sword]) | mask;
1499 extsmap = le32_to_cpu(iagp->extsmap[sword]) | mask;
1500
1501 /* scan the free inode and free extent summary maps for
1502 * free resources.
1503 */
1504 for (i = 0; i < nwords; i++) {
1505 /* check if this word of the free inode summary
1506 * map describes an extent with free inodes.
1507 */
1508 if (~inosmap) {
1509 /* an extent with free inodes has been
1510 * found. determine the extent number
1511 * and the inode number within the extent.
1512 */
1513 rem = diFindFree(inosmap, 0);
1514 extno = (sword << L2EXTSPERSUM) + rem;
1515 rem = diFindFree(le32_to_cpu(iagp->wmap[extno]),
1516 0);
1517 if (rem >= INOSPEREXT) {
1518 IREAD_UNLOCK(ipimap);
1519 release_metapage(mp);
1520 AG_UNLOCK(imap, agno);
1521 jfs_error(ip->i_sb,
1522 "diAlloc: can't find free bit "
1523 "in wmap");
1524 return EIO;
1525 }
1526
1527 /* determine the inode number within the
1528 * iag and allocate the inode from the
1529 * map.
1530 */
1531 ino = (extno << L2INOSPEREXT) + rem;
1532 rc = diAllocBit(imap, iagp, ino);
1533 IREAD_UNLOCK(ipimap);
1534 if (rc)
1535 assert(rc == -EIO);
1536 else {
1537 /* set the results of the allocation
1538 * and write the iag.
1539 */
1540 diInitInode(ip, iagno, ino, extno,
1541 iagp);
1542 mark_metapage_dirty(mp);
1543 }
1544 release_metapage(mp);
1545
1546 /* free the AG lock and return.
1547 */
1548 AG_UNLOCK(imap, agno);
1549 return (rc);
1550
1551 }
1552
1553 /* check if we may allocate an extent of free
1554 * inodes and whether this word of the free
1555 * extents summary map describes a free extent.
1556 */
1557 if (addext && ~extsmap) {
1558 /* a free extent has been found. determine
1559 * the extent number.
1560 */
1561 rem = diFindFree(extsmap, 0);
1562 extno = (sword << L2EXTSPERSUM) + rem;
1563
1564 /* allocate an extent of free inodes.
1565 */
1566 if ((rc = diNewExt(imap, iagp, extno))) {
1567 /* if there is no disk space for a
1568 * new extent, try to allocate the
1569 * disk inode from somewhere else.
1570 */
1571 if (rc == -ENOSPC)
1572 break;
1573
1574 assert(rc == -EIO);
1575 } else {
1576 /* set the results of the allocation
1577 * and write the iag.
1578 */
1579 diInitInode(ip, iagno,
1580 extno << L2INOSPEREXT,
1581 extno, iagp);
1582 mark_metapage_dirty(mp);
1583 }
1584 release_metapage(mp);
1585 /* free the imap inode & the AG lock & return.
1586 */
1587 IREAD_UNLOCK(ipimap);
1588 AG_UNLOCK(imap, agno);
1589 return (rc);
1590 }
1591
1592 /* move on to the next set of summary map words.
1593 */
1594 sword = (sword == SMAPSZ - 1) ? 0 : sword + 1;
1595 inosmap = le32_to_cpu(iagp->inosmap[sword]);
1596 extsmap = le32_to_cpu(iagp->extsmap[sword]);
1597 }
1598 }
1599 /* unlock imap inode */
1600 IREAD_UNLOCK(ipimap);
1601
1602 /* nothing doing in this iag, so release it. */
1603 release_metapage(mp);
1604
1605 tryag:
1606 /*
1607 * try to allocate anywhere within the same AG as the parent inode.
1608 */
1609 rc = diAllocAG(imap, agno, dir, ip);
1610
1611 AG_UNLOCK(imap, agno);
1612
1613 if (rc != -ENOSPC)
1614 return (rc);
1615
1616 /*
1617 * try to allocate in any AG.
1618 */
1619 return (diAllocAny(imap, agno, dir, ip));
1620}
1621
1622
1623/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001624 * NAME: diAllocAG(imap,agno,dir,ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001626 * FUNCTION: allocate a disk inode from the allocation group.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 *
1628 * this routine first determines if a new extent of free
1629 * inodes should be added for the allocation group, with
1630 * the current request satisfied from this extent. if this
1631 * is the case, an attempt will be made to do just that. if
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001632 * this attempt fails or it has been determined that a new
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 * extent should not be added, an attempt is made to satisfy
1634 * the request by allocating an existing (backed) free inode
1635 * from the allocation group.
1636 *
1637 * PRE CONDITION: Already have the AG lock for this AG.
1638 *
1639 * PARAMETERS:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001640 * imap - pointer to inode map control structure.
1641 * agno - allocation group to allocate from.
1642 * dir - 'true' if the new disk inode is for a directory.
1643 * ip - pointer to the new inode to be filled in on successful return
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 * with the disk inode number allocated, its extent address
1645 * and the start of the ag.
1646 *
1647 * RETURN VALUES:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001648 * 0 - success.
1649 * -ENOSPC - insufficient disk resources.
1650 * -EIO - i/o error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 */
1652static int
Richard Knutsson4d817152006-09-30 23:27:14 -07001653diAllocAG(struct inomap * imap, int agno, bool dir, struct inode *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
1655 int rc, addext, numfree, numinos;
1656
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001657 /* get the number of free and the number of backed disk
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 * inodes currently within the ag.
1659 */
1660 numfree = imap->im_agctl[agno].numfree;
1661 numinos = imap->im_agctl[agno].numinos;
1662
1663 if (numfree > numinos) {
1664 jfs_error(ip->i_sb, "diAllocAG: numfree > numinos");
1665 return -EIO;
1666 }
1667
1668 /* determine if we should allocate a new extent of free inodes
1669 * within the ag: for directory inodes, add a new extent
1670 * if there are a small number of free inodes or number of free
1671 * inodes is a small percentage of the number of backed inodes.
1672 */
Richard Knutsson4d817152006-09-30 23:27:14 -07001673 if (dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 addext = (numfree < 64 ||
1675 (numfree < 256
1676 && ((numfree * 100) / numinos) <= 20));
1677 else
1678 addext = (numfree == 0);
1679
1680 /*
1681 * try to allocate a new extent of free inodes.
1682 */
1683 if (addext) {
1684 /* if free space is not avaliable for this new extent, try
1685 * below to allocate a free and existing (already backed)
1686 * inode from the ag.
1687 */
1688 if ((rc = diAllocExt(imap, agno, ip)) != -ENOSPC)
1689 return (rc);
1690 }
1691
1692 /*
1693 * try to allocate an existing free inode from the ag.
1694 */
1695 return (diAllocIno(imap, agno, ip));
1696}
1697
1698
1699/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001700 * NAME: diAllocAny(imap,agno,dir,iap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001702 * FUNCTION: allocate a disk inode from any other allocation group.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 *
1704 * this routine is called when an allocation attempt within
1705 * the primary allocation group has failed. if attempts to
1706 * allocate an inode from any allocation group other than the
1707 * specified primary group.
1708 *
1709 * PARAMETERS:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001710 * imap - pointer to inode map control structure.
1711 * agno - primary allocation group (to avoid).
1712 * dir - 'true' if the new disk inode is for a directory.
1713 * ip - pointer to a new inode to be filled in on successful return
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 * with the disk inode number allocated, its extent address
1715 * and the start of the ag.
1716 *
1717 * RETURN VALUES:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001718 * 0 - success.
1719 * -ENOSPC - insufficient disk resources.
1720 * -EIO - i/o error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 */
1722static int
Richard Knutsson4d817152006-09-30 23:27:14 -07001723diAllocAny(struct inomap * imap, int agno, bool dir, struct inode *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724{
1725 int ag, rc;
1726 int maxag = JFS_SBI(imap->im_ipimap->i_sb)->bmap->db_maxag;
1727
1728
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001729 /* try to allocate from the ags following agno up to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 * the maximum ag number.
1731 */
1732 for (ag = agno + 1; ag <= maxag; ag++) {
1733 AG_LOCK(imap, ag);
1734
1735 rc = diAllocAG(imap, ag, dir, ip);
1736
1737 AG_UNLOCK(imap, ag);
1738
1739 if (rc != -ENOSPC)
1740 return (rc);
1741 }
1742
1743 /* try to allocate from the ags in front of agno.
1744 */
1745 for (ag = 0; ag < agno; ag++) {
1746 AG_LOCK(imap, ag);
1747
1748 rc = diAllocAG(imap, ag, dir, ip);
1749
1750 AG_UNLOCK(imap, ag);
1751
1752 if (rc != -ENOSPC)
1753 return (rc);
1754 }
1755
1756 /* no free disk inodes.
1757 */
1758 return -ENOSPC;
1759}
1760
1761
1762/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001763 * NAME: diAllocIno(imap,agno,ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001765 * FUNCTION: allocate a disk inode from the allocation group's free
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 * inode list, returning an error if this free list is
1767 * empty (i.e. no iags on the list).
1768 *
1769 * allocation occurs from the first iag on the list using
1770 * the iag's free inode summary map to find the leftmost
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001771 * free inode in the iag.
1772 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 * PRE CONDITION: Already have AG lock for this AG.
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001774 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 * PARAMETERS:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001776 * imap - pointer to inode map control structure.
1777 * agno - allocation group.
1778 * ip - pointer to new inode to be filled in on successful return
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 * with the disk inode number allocated, its extent address
1780 * and the start of the ag.
1781 *
1782 * RETURN VALUES:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001783 * 0 - success.
1784 * -ENOSPC - insufficient disk resources.
1785 * -EIO - i/o error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 */
1787static int diAllocIno(struct inomap * imap, int agno, struct inode *ip)
1788{
1789 int iagno, ino, rc, rem, extno, sword;
1790 struct metapage *mp;
1791 struct iag *iagp;
1792
1793 /* check if there are iags on the ag's free inode list.
1794 */
1795 if ((iagno = imap->im_agctl[agno].inofree) < 0)
1796 return -ENOSPC;
1797
1798 /* obtain read lock on imap inode */
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001799 IREAD_LOCK(imap->im_ipimap, RDWRLOCK_IMAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
1801 /* read the iag at the head of the list.
1802 */
1803 if ((rc = diIAGRead(imap, iagno, &mp))) {
1804 IREAD_UNLOCK(imap->im_ipimap);
1805 return (rc);
1806 }
1807 iagp = (struct iag *) mp->data;
1808
1809 /* better be free inodes in this iag if it is on the
1810 * list.
1811 */
1812 if (!iagp->nfreeinos) {
1813 IREAD_UNLOCK(imap->im_ipimap);
1814 release_metapage(mp);
1815 jfs_error(ip->i_sb,
1816 "diAllocIno: nfreeinos = 0, but iag on freelist");
1817 return -EIO;
1818 }
1819
1820 /* scan the free inode summary map to find an extent
1821 * with free inodes.
1822 */
1823 for (sword = 0;; sword++) {
1824 if (sword >= SMAPSZ) {
1825 IREAD_UNLOCK(imap->im_ipimap);
1826 release_metapage(mp);
1827 jfs_error(ip->i_sb,
1828 "diAllocIno: free inode not found in summary map");
1829 return -EIO;
1830 }
1831
1832 if (~iagp->inosmap[sword])
1833 break;
1834 }
1835
1836 /* found a extent with free inodes. determine
1837 * the extent number.
1838 */
1839 rem = diFindFree(le32_to_cpu(iagp->inosmap[sword]), 0);
1840 if (rem >= EXTSPERSUM) {
1841 IREAD_UNLOCK(imap->im_ipimap);
1842 release_metapage(mp);
1843 jfs_error(ip->i_sb, "diAllocIno: no free extent found");
1844 return -EIO;
1845 }
1846 extno = (sword << L2EXTSPERSUM) + rem;
1847
1848 /* find the first free inode in the extent.
1849 */
1850 rem = diFindFree(le32_to_cpu(iagp->wmap[extno]), 0);
1851 if (rem >= INOSPEREXT) {
1852 IREAD_UNLOCK(imap->im_ipimap);
1853 release_metapage(mp);
1854 jfs_error(ip->i_sb, "diAllocIno: free inode not found");
1855 return -EIO;
1856 }
1857
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001858 /* compute the inode number within the iag.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 */
1860 ino = (extno << L2INOSPEREXT) + rem;
1861
1862 /* allocate the inode.
1863 */
1864 rc = diAllocBit(imap, iagp, ino);
1865 IREAD_UNLOCK(imap->im_ipimap);
1866 if (rc) {
1867 release_metapage(mp);
1868 return (rc);
1869 }
1870
1871 /* set the results of the allocation and write the iag.
1872 */
1873 diInitInode(ip, iagno, ino, extno, iagp);
1874 write_metapage(mp);
1875
1876 return (0);
1877}
1878
1879
1880/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001881 * NAME: diAllocExt(imap,agno,ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001883 * FUNCTION: add a new extent of free inodes to an iag, allocating
1884 * an inode from this extent to satisfy the current allocation
1885 * request.
1886 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 * this routine first tries to find an existing iag with free
1888 * extents through the ag free extent list. if list is not
1889 * empty, the head of the list will be selected as the home
1890 * of the new extent of free inodes. otherwise (the list is
1891 * empty), a new iag will be allocated for the ag to contain
1892 * the extent.
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001893 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 * once an iag has been selected, the free extent summary map
1895 * is used to locate a free extent within the iag and diNewExt()
1896 * is called to initialize the extent, with initialization
1897 * including the allocation of the first inode of the extent
1898 * for the purpose of satisfying this request.
1899 *
1900 * PARAMETERS:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001901 * imap - pointer to inode map control structure.
1902 * agno - allocation group number.
1903 * ip - pointer to new inode to be filled in on successful return
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 * with the disk inode number allocated, its extent address
1905 * and the start of the ag.
1906 *
1907 * RETURN VALUES:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001908 * 0 - success.
1909 * -ENOSPC - insufficient disk resources.
1910 * -EIO - i/o error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 */
1912static int diAllocExt(struct inomap * imap, int agno, struct inode *ip)
1913{
1914 int rem, iagno, sword, extno, rc;
1915 struct metapage *mp;
1916 struct iag *iagp;
1917
1918 /* check if the ag has any iags with free extents. if not,
1919 * allocate a new iag for the ag.
1920 */
1921 if ((iagno = imap->im_agctl[agno].extfree) < 0) {
1922 /* If successful, diNewIAG will obtain the read lock on the
1923 * imap inode.
1924 */
1925 if ((rc = diNewIAG(imap, &iagno, agno, &mp))) {
1926 return (rc);
1927 }
1928 iagp = (struct iag *) mp->data;
1929
1930 /* set the ag number if this a brand new iag
1931 */
1932 iagp->agstart =
1933 cpu_to_le64(AGTOBLK(agno, imap->im_ipimap));
1934 } else {
1935 /* read the iag.
1936 */
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001937 IREAD_LOCK(imap->im_ipimap, RDWRLOCK_IMAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 if ((rc = diIAGRead(imap, iagno, &mp))) {
1939 IREAD_UNLOCK(imap->im_ipimap);
1940 jfs_error(ip->i_sb, "diAllocExt: error reading iag");
1941 return rc;
1942 }
1943 iagp = (struct iag *) mp->data;
1944 }
1945
1946 /* using the free extent summary map, find a free extent.
1947 */
1948 for (sword = 0;; sword++) {
1949 if (sword >= SMAPSZ) {
1950 release_metapage(mp);
1951 IREAD_UNLOCK(imap->im_ipimap);
1952 jfs_error(ip->i_sb,
1953 "diAllocExt: free ext summary map not found");
1954 return -EIO;
1955 }
1956 if (~iagp->extsmap[sword])
1957 break;
1958 }
1959
1960 /* determine the extent number of the free extent.
1961 */
1962 rem = diFindFree(le32_to_cpu(iagp->extsmap[sword]), 0);
1963 if (rem >= EXTSPERSUM) {
1964 release_metapage(mp);
1965 IREAD_UNLOCK(imap->im_ipimap);
1966 jfs_error(ip->i_sb, "diAllocExt: free extent not found");
1967 return -EIO;
1968 }
1969 extno = (sword << L2EXTSPERSUM) + rem;
1970
1971 /* initialize the new extent.
1972 */
1973 rc = diNewExt(imap, iagp, extno);
1974 IREAD_UNLOCK(imap->im_ipimap);
1975 if (rc) {
1976 /* something bad happened. if a new iag was allocated,
1977 * place it back on the inode map's iag free list, and
1978 * clear the ag number information.
1979 */
1980 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
1981 IAGFREE_LOCK(imap);
1982 iagp->iagfree = cpu_to_le32(imap->im_freeiag);
1983 imap->im_freeiag = iagno;
1984 IAGFREE_UNLOCK(imap);
1985 }
1986 write_metapage(mp);
1987 return (rc);
1988 }
1989
1990 /* set the results of the allocation and write the iag.
1991 */
1992 diInitInode(ip, iagno, extno << L2INOSPEREXT, extno, iagp);
1993
1994 write_metapage(mp);
1995
1996 return (0);
1997}
1998
1999
2000/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002001 * NAME: diAllocBit(imap,iagp,ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -05002003 * FUNCTION: allocate a backed inode from an iag.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 *
2005 * this routine performs the mechanics of allocating a
2006 * specified inode from a backed extent.
2007 *
2008 * if the inode to be allocated represents the last free
2009 * inode within the iag, the iag will be removed from the
2010 * ag free inode list.
2011 *
2012 * a careful update approach is used to provide consistency
2013 * in the face of updates to multiple buffers. under this
2014 * approach, all required buffers are obtained before making
2015 * any updates and are held all are updates are complete.
Dave Kleikamp63f83c92006-10-02 09:55:27 -05002016 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 * PRE CONDITION: Already have buffer lock on iagp. Already have AG lock on
2018 * this AG. Must have read lock on imap inode.
2019 *
2020 * PARAMETERS:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002021 * imap - pointer to inode map control structure.
2022 * iagp - pointer to iag.
2023 * ino - inode number to be allocated within the iag.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 *
2025 * RETURN VALUES:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002026 * 0 - success.
2027 * -ENOSPC - insufficient disk resources.
2028 * -EIO - i/o error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 */
2030static int diAllocBit(struct inomap * imap, struct iag * iagp, int ino)
2031{
2032 int extno, bitno, agno, sword, rc;
2033 struct metapage *amp = NULL, *bmp = NULL;
2034 struct iag *aiagp = NULL, *biagp = NULL;
2035 u32 mask;
2036
2037 /* check if this is the last free inode within the iag.
2038 * if so, it will have to be removed from the ag free
2039 * inode list, so get the iags preceeding and following
2040 * it on the list.
2041 */
2042 if (iagp->nfreeinos == cpu_to_le32(1)) {
2043 if ((int) le32_to_cpu(iagp->inofreefwd) >= 0) {
2044 if ((rc =
2045 diIAGRead(imap, le32_to_cpu(iagp->inofreefwd),
2046 &amp)))
2047 return (rc);
2048 aiagp = (struct iag *) amp->data;
2049 }
2050
2051 if ((int) le32_to_cpu(iagp->inofreeback) >= 0) {
2052 if ((rc =
2053 diIAGRead(imap,
2054 le32_to_cpu(iagp->inofreeback),
2055 &bmp))) {
2056 if (amp)
2057 release_metapage(amp);
2058 return (rc);
2059 }
2060 biagp = (struct iag *) bmp->data;
2061 }
2062 }
2063
2064 /* get the ag number, extent number, inode number within
2065 * the extent.
2066 */
2067 agno = BLKTOAG(le64_to_cpu(iagp->agstart), JFS_SBI(imap->im_ipimap->i_sb));
2068 extno = ino >> L2INOSPEREXT;
2069 bitno = ino & (INOSPEREXT - 1);
2070
2071 /* compute the mask for setting the map.
2072 */
2073 mask = HIGHORDER >> bitno;
2074
2075 /* the inode should be free and backed.
2076 */
2077 if (((le32_to_cpu(iagp->pmap[extno]) & mask) != 0) ||
2078 ((le32_to_cpu(iagp->wmap[extno]) & mask) != 0) ||
2079 (addressPXD(&iagp->inoext[extno]) == 0)) {
2080 if (amp)
2081 release_metapage(amp);
2082 if (bmp)
2083 release_metapage(bmp);
2084
2085 jfs_error(imap->im_ipimap->i_sb,
2086 "diAllocBit: iag inconsistent");
2087 return -EIO;
2088 }
2089
2090 /* mark the inode as allocated in the working map.
2091 */
2092 iagp->wmap[extno] |= cpu_to_le32(mask);
2093
2094 /* check if all inodes within the extent are now
2095 * allocated. if so, update the free inode summary
2096 * map to reflect this.
2097 */
2098 if (iagp->wmap[extno] == cpu_to_le32(ONES)) {
2099 sword = extno >> L2EXTSPERSUM;
2100 bitno = extno & (EXTSPERSUM - 1);
2101 iagp->inosmap[sword] |= cpu_to_le32(HIGHORDER >> bitno);
2102 }
2103
2104 /* if this was the last free inode in the iag, remove the
2105 * iag from the ag free inode list.
2106 */
2107 if (iagp->nfreeinos == cpu_to_le32(1)) {
2108 if (amp) {
2109 aiagp->inofreeback = iagp->inofreeback;
2110 write_metapage(amp);
2111 }
2112
2113 if (bmp) {
2114 biagp->inofreefwd = iagp->inofreefwd;
2115 write_metapage(bmp);
2116 } else {
2117 imap->im_agctl[agno].inofree =
2118 le32_to_cpu(iagp->inofreefwd);
2119 }
2120 iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
2121 }
2122
2123 /* update the free inode count at the iag, ag, inode
2124 * map levels.
2125 */
2126 iagp->nfreeinos = cpu_to_le32(le32_to_cpu(iagp->nfreeinos) - 1);
2127 imap->im_agctl[agno].numfree -= 1;
2128 atomic_dec(&imap->im_numfree);
2129
2130 return (0);
2131}
2132
2133
2134/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002135 * NAME: diNewExt(imap,iagp,extno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002137 * FUNCTION: initialize a new extent of inodes for an iag, allocating
2138 * the first inode of the extent for use for the current
2139 * allocation request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 *
2141 * disk resources are allocated for the new extent of inodes
2142 * and the inodes themselves are initialized to reflect their
2143 * existence within the extent (i.e. their inode numbers and
2144 * inode extent addresses are set) and their initial state
2145 * (mode and link count are set to zero).
2146 *
2147 * if the iag is new, it is not yet on an ag extent free list
2148 * but will now be placed on this list.
2149 *
2150 * if the allocation of the new extent causes the iag to
2151 * have no free extent, the iag will be removed from the
2152 * ag extent free list.
2153 *
2154 * if the iag has no free backed inodes, it will be placed
2155 * on the ag free inode list, since the addition of the new
2156 * extent will now cause it to have free inodes.
2157 *
2158 * a careful update approach is used to provide consistency
2159 * (i.e. list consistency) in the face of updates to multiple
2160 * buffers. under this approach, all required buffers are
2161 * obtained before making any updates and are held until all
2162 * updates are complete.
Dave Kleikamp63f83c92006-10-02 09:55:27 -05002163 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 * PRE CONDITION: Already have buffer lock on iagp. Already have AG lock on
2165 * this AG. Must have read lock on imap inode.
2166 *
2167 * PARAMETERS:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002168 * imap - pointer to inode map control structure.
2169 * iagp - pointer to iag.
2170 * extno - extent number.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 *
2172 * RETURN VALUES:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002173 * 0 - success.
2174 * -ENOSPC - insufficient disk resources.
2175 * -EIO - i/o error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 */
2177static int diNewExt(struct inomap * imap, struct iag * iagp, int extno)
2178{
2179 int agno, iagno, fwd, back, freei = 0, sword, rc;
2180 struct iag *aiagp = NULL, *biagp = NULL, *ciagp = NULL;
2181 struct metapage *amp, *bmp, *cmp, *dmp;
2182 struct inode *ipimap;
2183 s64 blkno, hint;
2184 int i, j;
2185 u32 mask;
2186 ino_t ino;
2187 struct dinode *dp;
2188 struct jfs_sb_info *sbi;
2189
2190 /* better have free extents.
2191 */
2192 if (!iagp->nfreeexts) {
2193 jfs_error(imap->im_ipimap->i_sb, "diNewExt: no free extents");
2194 return -EIO;
2195 }
2196
2197 /* get the inode map inode.
2198 */
2199 ipimap = imap->im_ipimap;
2200 sbi = JFS_SBI(ipimap->i_sb);
2201
2202 amp = bmp = cmp = NULL;
2203
2204 /* get the ag and iag numbers for this iag.
2205 */
2206 agno = BLKTOAG(le64_to_cpu(iagp->agstart), sbi);
2207 iagno = le32_to_cpu(iagp->iagnum);
2208
2209 /* check if this is the last free extent within the
2210 * iag. if so, the iag must be removed from the ag
2211 * free extent list, so get the iags preceeding and
2212 * following the iag on this list.
2213 */
2214 if (iagp->nfreeexts == cpu_to_le32(1)) {
2215 if ((fwd = le32_to_cpu(iagp->extfreefwd)) >= 0) {
2216 if ((rc = diIAGRead(imap, fwd, &amp)))
2217 return (rc);
2218 aiagp = (struct iag *) amp->data;
2219 }
2220
2221 if ((back = le32_to_cpu(iagp->extfreeback)) >= 0) {
2222 if ((rc = diIAGRead(imap, back, &bmp)))
2223 goto error_out;
2224 biagp = (struct iag *) bmp->data;
2225 }
2226 } else {
2227 /* the iag has free extents. if all extents are free
2228 * (as is the case for a newly allocated iag), the iag
2229 * must be added to the ag free extent list, so get
2230 * the iag at the head of the list in preparation for
2231 * adding this iag to this list.
2232 */
2233 fwd = back = -1;
2234 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2235 if ((fwd = imap->im_agctl[agno].extfree) >= 0) {
2236 if ((rc = diIAGRead(imap, fwd, &amp)))
2237 goto error_out;
2238 aiagp = (struct iag *) amp->data;
2239 }
2240 }
2241 }
2242
2243 /* check if the iag has no free inodes. if so, the iag
2244 * will have to be added to the ag free inode list, so get
2245 * the iag at the head of the list in preparation for
2246 * adding this iag to this list. in doing this, we must
2247 * check if we already have the iag at the head of
2248 * the list in hand.
2249 */
2250 if (iagp->nfreeinos == 0) {
2251 freei = imap->im_agctl[agno].inofree;
2252
2253 if (freei >= 0) {
2254 if (freei == fwd) {
2255 ciagp = aiagp;
2256 } else if (freei == back) {
2257 ciagp = biagp;
2258 } else {
2259 if ((rc = diIAGRead(imap, freei, &cmp)))
2260 goto error_out;
2261 ciagp = (struct iag *) cmp->data;
2262 }
2263 if (ciagp == NULL) {
2264 jfs_error(imap->im_ipimap->i_sb,
2265 "diNewExt: ciagp == NULL");
2266 rc = -EIO;
2267 goto error_out;
2268 }
2269 }
2270 }
2271
2272 /* allocate disk space for the inode extent.
2273 */
2274 if ((extno == 0) || (addressPXD(&iagp->inoext[extno - 1]) == 0))
2275 hint = ((s64) agno << sbi->bmap->db_agl2size) - 1;
2276 else
2277 hint = addressPXD(&iagp->inoext[extno - 1]) +
2278 lengthPXD(&iagp->inoext[extno - 1]) - 1;
2279
2280 if ((rc = dbAlloc(ipimap, hint, (s64) imap->im_nbperiext, &blkno)))
2281 goto error_out;
2282
2283 /* compute the inode number of the first inode within the
2284 * extent.
2285 */
2286 ino = (iagno << L2INOSPERIAG) + (extno << L2INOSPEREXT);
2287
2288 /* initialize the inodes within the newly allocated extent a
2289 * page at a time.
2290 */
2291 for (i = 0; i < imap->im_nbperiext; i += sbi->nbperpage) {
2292 /* get a buffer for this page of disk inodes.
2293 */
2294 dmp = get_metapage(ipimap, blkno + i, PSIZE, 1);
2295 if (dmp == NULL) {
2296 rc = -EIO;
2297 goto error_out;
2298 }
2299 dp = (struct dinode *) dmp->data;
2300
2301 /* initialize the inode number, mode, link count and
2302 * inode extent address.
2303 */
2304 for (j = 0; j < INOSPERPAGE; j++, dp++, ino++) {
2305 dp->di_inostamp = cpu_to_le32(sbi->inostamp);
2306 dp->di_number = cpu_to_le32(ino);
2307 dp->di_fileset = cpu_to_le32(FILESYSTEM_I);
2308 dp->di_mode = 0;
2309 dp->di_nlink = 0;
2310 PXDaddress(&(dp->di_ixpxd), blkno);
2311 PXDlength(&(dp->di_ixpxd), imap->im_nbperiext);
2312 }
2313 write_metapage(dmp);
2314 }
2315
2316 /* if this is the last free extent within the iag, remove the
2317 * iag from the ag free extent list.
2318 */
2319 if (iagp->nfreeexts == cpu_to_le32(1)) {
2320 if (fwd >= 0)
2321 aiagp->extfreeback = iagp->extfreeback;
2322
2323 if (back >= 0)
2324 biagp->extfreefwd = iagp->extfreefwd;
2325 else
2326 imap->im_agctl[agno].extfree =
2327 le32_to_cpu(iagp->extfreefwd);
2328
2329 iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
2330 } else {
2331 /* if the iag has all free extents (newly allocated iag),
2332 * add the iag to the ag free extent list.
2333 */
2334 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2335 if (fwd >= 0)
2336 aiagp->extfreeback = cpu_to_le32(iagno);
2337
2338 iagp->extfreefwd = cpu_to_le32(fwd);
2339 iagp->extfreeback = cpu_to_le32(-1);
2340 imap->im_agctl[agno].extfree = iagno;
2341 }
2342 }
2343
2344 /* if the iag has no free inodes, add the iag to the
2345 * ag free inode list.
2346 */
2347 if (iagp->nfreeinos == 0) {
2348 if (freei >= 0)
2349 ciagp->inofreeback = cpu_to_le32(iagno);
2350
2351 iagp->inofreefwd =
2352 cpu_to_le32(imap->im_agctl[agno].inofree);
2353 iagp->inofreeback = cpu_to_le32(-1);
2354 imap->im_agctl[agno].inofree = iagno;
2355 }
2356
2357 /* initialize the extent descriptor of the extent. */
2358 PXDlength(&iagp->inoext[extno], imap->im_nbperiext);
2359 PXDaddress(&iagp->inoext[extno], blkno);
2360
2361 /* initialize the working and persistent map of the extent.
2362 * the working map will be initialized such that
2363 * it indicates the first inode of the extent is allocated.
2364 */
2365 iagp->wmap[extno] = cpu_to_le32(HIGHORDER);
2366 iagp->pmap[extno] = 0;
2367
2368 /* update the free inode and free extent summary maps
2369 * for the extent to indicate the extent has free inodes
2370 * and no longer represents a free extent.
2371 */
2372 sword = extno >> L2EXTSPERSUM;
2373 mask = HIGHORDER >> (extno & (EXTSPERSUM - 1));
2374 iagp->extsmap[sword] |= cpu_to_le32(mask);
2375 iagp->inosmap[sword] &= cpu_to_le32(~mask);
2376
2377 /* update the free inode and free extent counts for the
2378 * iag.
2379 */
2380 iagp->nfreeinos = cpu_to_le32(le32_to_cpu(iagp->nfreeinos) +
2381 (INOSPEREXT - 1));
2382 iagp->nfreeexts = cpu_to_le32(le32_to_cpu(iagp->nfreeexts) - 1);
2383
2384 /* update the free and backed inode counts for the ag.
2385 */
2386 imap->im_agctl[agno].numfree += (INOSPEREXT - 1);
2387 imap->im_agctl[agno].numinos += INOSPEREXT;
2388
2389 /* update the free and backed inode counts for the inode map.
2390 */
2391 atomic_add(INOSPEREXT - 1, &imap->im_numfree);
2392 atomic_add(INOSPEREXT, &imap->im_numinos);
2393
2394 /* write the iags.
2395 */
2396 if (amp)
2397 write_metapage(amp);
2398 if (bmp)
2399 write_metapage(bmp);
2400 if (cmp)
2401 write_metapage(cmp);
2402
2403 return (0);
2404
2405 error_out:
2406
2407 /* release the iags.
2408 */
2409 if (amp)
2410 release_metapage(amp);
2411 if (bmp)
2412 release_metapage(bmp);
2413 if (cmp)
2414 release_metapage(cmp);
2415
2416 return (rc);
2417}
2418
2419
2420/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002421 * NAME: diNewIAG(imap,iagnop,agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -05002423 * FUNCTION: allocate a new iag for an allocation group.
2424 *
2425 * first tries to allocate the iag from the inode map
2426 * iagfree list:
2427 * if the list has free iags, the head of the list is removed
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 * and returned to satisfy the request.
2429 * if the inode map's iag free list is empty, the inode map
2430 * is extended to hold a new iag. this new iag is initialized
2431 * and returned to satisfy the request.
2432 *
2433 * PARAMETERS:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002434 * imap - pointer to inode map control structure.
2435 * iagnop - pointer to an iag number set with the number of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 * newly allocated iag upon successful return.
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002437 * agno - allocation group number.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 * bpp - Buffer pointer to be filled in with new IAG's buffer
2439 *
2440 * RETURN VALUES:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002441 * 0 - success.
2442 * -ENOSPC - insufficient disk resources.
2443 * -EIO - i/o error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -05002445 * serialization:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 * AG lock held on entry/exit;
2447 * write lock on the map is held inside;
2448 * read lock on the map is held on successful completion;
2449 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -05002450 * note: new iag transaction:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 * . synchronously write iag;
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002452 * . write log of xtree and inode of imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 * . commit;
2454 * . synchronous write of xtree (right to left, bottom to top);
2455 * . at start of logredo(): init in-memory imap with one additional iag page;
2456 * . at end of logredo(): re-read imap inode to determine
2457 * new imap size;
2458 */
2459static int
2460diNewIAG(struct inomap * imap, int *iagnop, int agno, struct metapage ** mpp)
2461{
2462 int rc;
2463 int iagno, i, xlen;
2464 struct inode *ipimap;
2465 struct super_block *sb;
2466 struct jfs_sb_info *sbi;
2467 struct metapage *mp;
2468 struct iag *iagp;
2469 s64 xaddr = 0;
2470 s64 blkno;
2471 tid_t tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 struct inode *iplist[1];
2473
2474 /* pick up pointers to the inode map and mount inodes */
2475 ipimap = imap->im_ipimap;
2476 sb = ipimap->i_sb;
2477 sbi = JFS_SBI(sb);
2478
2479 /* acquire the free iag lock */
2480 IAGFREE_LOCK(imap);
2481
Dave Kleikamp63f83c92006-10-02 09:55:27 -05002482 /* if there are any iags on the inode map free iag list,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 * allocate the iag from the head of the list.
2484 */
2485 if (imap->im_freeiag >= 0) {
2486 /* pick up the iag number at the head of the list */
2487 iagno = imap->im_freeiag;
2488
2489 /* determine the logical block number of the iag */
2490 blkno = IAGTOLBLK(iagno, sbi->l2nbperpage);
2491 } else {
2492 /* no free iags. the inode map will have to be extented
2493 * to include a new iag.
2494 */
2495
2496 /* acquire inode map lock */
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06002497 IWRITE_LOCK(ipimap, RDWRLOCK_IMAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
2499 if (ipimap->i_size >> L2PSIZE != imap->im_nextiag + 1) {
2500 IWRITE_UNLOCK(ipimap);
2501 IAGFREE_UNLOCK(imap);
2502 jfs_error(imap->im_ipimap->i_sb,
2503 "diNewIAG: ipimap->i_size is wrong");
2504 return -EIO;
2505 }
2506
2507
2508 /* get the next avaliable iag number */
2509 iagno = imap->im_nextiag;
2510
2511 /* make sure that we have not exceeded the maximum inode
2512 * number limit.
2513 */
2514 if (iagno > (MAXIAGS - 1)) {
2515 /* release the inode map lock */
2516 IWRITE_UNLOCK(ipimap);
2517
2518 rc = -ENOSPC;
2519 goto out;
2520 }
2521
2522 /*
2523 * synchronously append new iag page.
2524 */
2525 /* determine the logical address of iag page to append */
2526 blkno = IAGTOLBLK(iagno, sbi->l2nbperpage);
2527
2528 /* Allocate extent for new iag page */
2529 xlen = sbi->nbperpage;
2530 if ((rc = dbAlloc(ipimap, 0, (s64) xlen, &xaddr))) {
2531 /* release the inode map lock */
2532 IWRITE_UNLOCK(ipimap);
2533
2534 goto out;
2535 }
2536
Dave Kleikampd2e83702005-05-02 12:24:51 -06002537 /*
2538 * start transaction of update of the inode map
2539 * addressing structure pointing to the new iag page;
2540 */
2541 tid = txBegin(sb, COMMIT_FORCE);
Ingo Molnar1de87442006-01-24 15:22:50 -06002542 mutex_lock(&JFS_IP(ipimap)->commit_mutex);
Dave Kleikampd2e83702005-05-02 12:24:51 -06002543
2544 /* update the inode map addressing structure to point to it */
2545 if ((rc =
2546 xtInsert(tid, ipimap, 0, blkno, xlen, &xaddr, 0))) {
2547 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -06002548 mutex_unlock(&JFS_IP(ipimap)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 /* Free the blocks allocated for the iag since it was
2550 * not successfully added to the inode map
2551 */
2552 dbFree(ipimap, xaddr, (s64) xlen);
2553
2554 /* release the inode map lock */
2555 IWRITE_UNLOCK(ipimap);
2556
Dave Kleikampd2e83702005-05-02 12:24:51 -06002557 goto out;
2558 }
2559
2560 /* update the inode map's inode to reflect the extension */
2561 ipimap->i_size += PSIZE;
2562 inode_add_bytes(ipimap, PSIZE);
2563
2564 /* assign a buffer for the page */
2565 mp = get_metapage(ipimap, blkno, PSIZE, 0);
2566 if (!mp) {
2567 /*
2568 * This is very unlikely since we just created the
2569 * extent, but let's try to handle it correctly
2570 */
2571 xtTruncate(tid, ipimap, ipimap->i_size - PSIZE,
2572 COMMIT_PWMAP);
2573
2574 txAbort(tid, 0);
2575 txEnd(tid);
2576
2577 /* release the inode map lock */
2578 IWRITE_UNLOCK(ipimap);
2579
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 rc = -EIO;
2581 goto out;
2582 }
2583 iagp = (struct iag *) mp->data;
2584
2585 /* init the iag */
2586 memset(iagp, 0, sizeof(struct iag));
2587 iagp->iagnum = cpu_to_le32(iagno);
2588 iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
2589 iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
2590 iagp->iagfree = cpu_to_le32(-1);
2591 iagp->nfreeinos = 0;
2592 iagp->nfreeexts = cpu_to_le32(EXTSPERIAG);
2593
2594 /* initialize the free inode summary map (free extent
2595 * summary map initialization handled by bzero).
2596 */
2597 for (i = 0; i < SMAPSZ; i++)
2598 iagp->inosmap[i] = cpu_to_le32(ONES);
2599
2600 /*
Dave Kleikampd2e83702005-05-02 12:24:51 -06002601 * Write and sync the metapage
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 flush_metapage(mp);
2604
2605 /*
Dave Kleikamp63f83c92006-10-02 09:55:27 -05002606 * txCommit(COMMIT_FORCE) will synchronously write address
2607 * index pages and inode after commit in careful update order
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 * of address index pages (right to left, bottom up);
2609 */
2610 iplist[0] = ipimap;
2611 rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
2612
2613 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -06002614 mutex_unlock(&JFS_IP(ipimap)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
2616 duplicateIXtree(sb, blkno, xlen, &xaddr);
2617
2618 /* update the next avaliable iag number */
2619 imap->im_nextiag += 1;
2620
2621 /* Add the iag to the iag free list so we don't lose the iag
2622 * if a failure happens now.
2623 */
2624 imap->im_freeiag = iagno;
2625
2626 /* Until we have logredo working, we want the imap inode &
2627 * control page to be up to date.
2628 */
2629 diSync(ipimap);
2630
2631 /* release the inode map lock */
2632 IWRITE_UNLOCK(ipimap);
2633 }
2634
2635 /* obtain read lock on map */
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06002636 IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
2638 /* read the iag */
2639 if ((rc = diIAGRead(imap, iagno, &mp))) {
2640 IREAD_UNLOCK(ipimap);
2641 rc = -EIO;
2642 goto out;
2643 }
2644 iagp = (struct iag *) mp->data;
2645
2646 /* remove the iag from the iag free list */
2647 imap->im_freeiag = le32_to_cpu(iagp->iagfree);
2648 iagp->iagfree = cpu_to_le32(-1);
2649
2650 /* set the return iag number and buffer pointer */
2651 *iagnop = iagno;
2652 *mpp = mp;
2653
2654 out:
2655 /* release the iag free lock */
2656 IAGFREE_UNLOCK(imap);
2657
2658 return (rc);
2659}
2660
2661/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002662 * NAME: diIAGRead()
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002664 * FUNCTION: get the buffer for the specified iag within a fileset
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 * or aggregate inode map.
Dave Kleikamp63f83c92006-10-02 09:55:27 -05002666 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 * PARAMETERS:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002668 * imap - pointer to inode map control structure.
2669 * iagno - iag number.
2670 * bpp - point to buffer pointer to be filled in on successful
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 * exit.
2672 *
2673 * SERIALIZATION:
2674 * must have read lock on imap inode
2675 * (When called by diExtendFS, the filesystem is quiesced, therefore
2676 * the read lock is unnecessary.)
2677 *
2678 * RETURN VALUES:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002679 * 0 - success.
2680 * -EIO - i/o error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 */
2682static int diIAGRead(struct inomap * imap, int iagno, struct metapage ** mpp)
2683{
2684 struct inode *ipimap = imap->im_ipimap;
2685 s64 blkno;
2686
2687 /* compute the logical block number of the iag. */
2688 blkno = IAGTOLBLK(iagno, JFS_SBI(ipimap->i_sb)->l2nbperpage);
2689
2690 /* read the iag. */
2691 *mpp = read_metapage(ipimap, blkno, PSIZE, 0);
2692 if (*mpp == NULL) {
2693 return -EIO;
2694 }
2695
2696 return (0);
2697}
2698
2699/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002700 * NAME: diFindFree()
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002702 * FUNCTION: find the first free bit in a word starting at
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 * the specified bit position.
2704 *
2705 * PARAMETERS:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002706 * word - word to be examined.
2707 * start - starting bit position.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 *
2709 * RETURN VALUES:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002710 * bit position of first free bit in the word or 32 if
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 * no free bits were found.
2712 */
2713static int diFindFree(u32 word, int start)
2714{
2715 int bitno;
2716 assert(start < 32);
2717 /* scan the word for the first free bit. */
2718 for (word <<= start, bitno = start; bitno < 32;
2719 bitno++, word <<= 1) {
2720 if ((word & HIGHORDER) == 0)
2721 break;
2722 }
2723 return (bitno);
2724}
2725
2726/*
2727 * NAME: diUpdatePMap()
Dave Kleikamp63f83c92006-10-02 09:55:27 -05002728 *
2729 * FUNCTION: Update the persistent map in an IAG for the allocation or
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 * freeing of the specified inode.
Dave Kleikamp63f83c92006-10-02 09:55:27 -05002731 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 * PRE CONDITIONS: Working map has already been updated for allocate.
2733 *
2734 * PARAMETERS:
2735 * ipimap - Incore inode map inode
2736 * inum - Number of inode to mark in permanent map
Richard Knutsson4d817152006-09-30 23:27:14 -07002737 * is_free - If 'true' indicates inode should be marked freed, otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 * indicates inode should be marked allocated.
2739 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -05002740 * RETURN VALUES:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 * 0 for success
2742 */
2743int
2744diUpdatePMap(struct inode *ipimap,
Richard Knutsson4d817152006-09-30 23:27:14 -07002745 unsigned long inum, bool is_free, struct tblock * tblk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746{
2747 int rc;
2748 struct iag *iagp;
2749 struct metapage *mp;
2750 int iagno, ino, extno, bitno;
2751 struct inomap *imap;
2752 u32 mask;
2753 struct jfs_log *log;
2754 int lsn, difft, diffp;
Dave Kleikamp7fab4792005-05-02 12:25:02 -06002755 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
2757 imap = JFS_IP(ipimap)->i_imap;
2758 /* get the iag number containing the inode */
2759 iagno = INOTOIAG(inum);
2760 /* make sure that the iag is contained within the map */
2761 if (iagno >= imap->im_nextiag) {
2762 jfs_error(ipimap->i_sb,
2763 "diUpdatePMap: the iag is outside the map");
2764 return -EIO;
2765 }
2766 /* read the iag */
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06002767 IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 rc = diIAGRead(imap, iagno, &mp);
2769 IREAD_UNLOCK(ipimap);
2770 if (rc)
2771 return (rc);
Dave Kleikamp7fab4792005-05-02 12:25:02 -06002772 metapage_wait_for_io(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 iagp = (struct iag *) mp->data;
2774 /* get the inode number and extent number of the inode within
2775 * the iag and the inode number within the extent.
2776 */
2777 ino = inum & (INOSPERIAG - 1);
2778 extno = ino >> L2INOSPEREXT;
2779 bitno = ino & (INOSPEREXT - 1);
2780 mask = HIGHORDER >> bitno;
Dave Kleikamp63f83c92006-10-02 09:55:27 -05002781 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 * mark the inode free in persistent map:
2783 */
Richard Knutsson4d817152006-09-30 23:27:14 -07002784 if (is_free) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 /* The inode should have been allocated both in working
2786 * map and in persistent map;
2787 * the inode will be freed from working map at the release
2788 * of last reference release;
2789 */
2790 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
Dave Kleikamp63f83c92006-10-02 09:55:27 -05002791 jfs_error(ipimap->i_sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 "diUpdatePMap: inode %ld not marked as "
2793 "allocated in wmap!", inum);
2794 }
2795 if (!(le32_to_cpu(iagp->pmap[extno]) & mask)) {
2796 jfs_error(ipimap->i_sb,
2797 "diUpdatePMap: inode %ld not marked as "
2798 "allocated in pmap!", inum);
2799 }
2800 /* update the bitmap for the extent of the freed inode */
2801 iagp->pmap[extno] &= cpu_to_le32(~mask);
2802 }
2803 /*
2804 * mark the inode allocated in persistent map:
2805 */
2806 else {
2807 /* The inode should be already allocated in the working map
2808 * and should be free in persistent map;
2809 */
2810 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
2811 release_metapage(mp);
2812 jfs_error(ipimap->i_sb,
2813 "diUpdatePMap: the inode is not allocated in "
2814 "the working map");
2815 return -EIO;
2816 }
2817 if ((le32_to_cpu(iagp->pmap[extno]) & mask) != 0) {
2818 release_metapage(mp);
2819 jfs_error(ipimap->i_sb,
2820 "diUpdatePMap: the inode is not free in the "
2821 "persistent map");
2822 return -EIO;
2823 }
2824 /* update the bitmap for the extent of the allocated inode */
2825 iagp->pmap[extno] |= cpu_to_le32(mask);
2826 }
2827 /*
2828 * update iag lsn
2829 */
2830 lsn = tblk->lsn;
2831 log = JFS_SBI(tblk->sb)->log;
Dave Kleikampbe0bf7d2006-03-08 10:59:15 -06002832 LOGSYNC_LOCK(log, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 if (mp->lsn != 0) {
2834 /* inherit older/smaller lsn */
2835 logdiff(difft, lsn, log);
2836 logdiff(diffp, mp->lsn, log);
2837 if (difft < diffp) {
2838 mp->lsn = lsn;
2839 /* move mp after tblock in logsync list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 list_move(&mp->synclist, &tblk->synclist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 }
2842 /* inherit younger/larger clsn */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 assert(mp->clsn);
2844 logdiff(difft, tblk->clsn, log);
2845 logdiff(diffp, mp->clsn, log);
2846 if (difft > diffp)
2847 mp->clsn = tblk->clsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 } else {
2849 mp->log = log;
2850 mp->lsn = lsn;
2851 /* insert mp after tblock in logsync list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 log->count++;
2853 list_add(&mp->synclist, &tblk->synclist);
2854 mp->clsn = tblk->clsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 }
Dave Kleikampbe0bf7d2006-03-08 10:59:15 -06002856 LOGSYNC_UNLOCK(log, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 write_metapage(mp);
2858 return (0);
2859}
2860
2861/*
2862 * diExtendFS()
2863 *
2864 * function: update imap for extendfs();
Dave Kleikamp63f83c92006-10-02 09:55:27 -05002865 *
2866 * note: AG size has been increased s.t. each k old contiguous AGs are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 * coalesced into a new AG;
2868 */
2869int diExtendFS(struct inode *ipimap, struct inode *ipbmap)
2870{
2871 int rc, rcx = 0;
2872 struct inomap *imap = JFS_IP(ipimap)->i_imap;
2873 struct iag *iagp = NULL, *hiagp = NULL;
2874 struct bmap *mp = JFS_SBI(ipbmap->i_sb)->bmap;
2875 struct metapage *bp, *hbp;
2876 int i, n, head;
2877 int numinos, xnuminos = 0, xnumfree = 0;
2878 s64 agstart;
2879
2880 jfs_info("diExtendFS: nextiag:%d numinos:%d numfree:%d",
2881 imap->im_nextiag, atomic_read(&imap->im_numinos),
2882 atomic_read(&imap->im_numfree));
2883
2884 /*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002885 * reconstruct imap
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 *
2887 * coalesce contiguous k (newAGSize/oldAGSize) AGs;
2888 * i.e., (AGi, ..., AGj) where i = k*n and j = k*(n+1) - 1 to AGn;
2889 * note: new AG size = old AG size * (2**x).
2890 */
2891
2892 /* init per AG control information im_agctl[] */
2893 for (i = 0; i < MAXAG; i++) {
2894 imap->im_agctl[i].inofree = -1;
2895 imap->im_agctl[i].extfree = -1;
2896 imap->im_agctl[i].numinos = 0; /* number of backed inodes */
2897 imap->im_agctl[i].numfree = 0; /* number of free backed inodes */
2898 }
2899
2900 /*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002901 * process each iag page of the map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 *
2903 * rebuild AG Free Inode List, AG Free Inode Extent List;
2904 */
2905 for (i = 0; i < imap->im_nextiag; i++) {
2906 if ((rc = diIAGRead(imap, i, &bp))) {
2907 rcx = rc;
2908 continue;
2909 }
2910 iagp = (struct iag *) bp->data;
2911 if (le32_to_cpu(iagp->iagnum) != i) {
2912 release_metapage(bp);
2913 jfs_error(ipimap->i_sb,
2914 "diExtendFs: unexpected value of iagnum");
2915 return -EIO;
2916 }
2917
2918 /* leave free iag in the free iag list */
Dave Kleikamp63f83c92006-10-02 09:55:27 -05002919 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002920 release_metapage(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 continue;
2922 }
2923
2924 /* agstart that computes to the same ag is treated as same; */
2925 agstart = le64_to_cpu(iagp->agstart);
2926 /* iagp->agstart = agstart & ~(mp->db_agsize - 1); */
2927 n = agstart >> mp->db_agl2size;
2928
2929 /* compute backed inodes */
2930 numinos = (EXTSPERIAG - le32_to_cpu(iagp->nfreeexts))
2931 << L2INOSPEREXT;
2932 if (numinos > 0) {
2933 /* merge AG backed inodes */
2934 imap->im_agctl[n].numinos += numinos;
2935 xnuminos += numinos;
2936 }
2937
2938 /* if any backed free inodes, insert at AG free inode list */
2939 if ((int) le32_to_cpu(iagp->nfreeinos) > 0) {
2940 if ((head = imap->im_agctl[n].inofree) == -1) {
2941 iagp->inofreefwd = cpu_to_le32(-1);
2942 iagp->inofreeback = cpu_to_le32(-1);
2943 } else {
2944 if ((rc = diIAGRead(imap, head, &hbp))) {
2945 rcx = rc;
2946 goto nextiag;
2947 }
2948 hiagp = (struct iag *) hbp->data;
2949 hiagp->inofreeback = iagp->iagnum;
2950 iagp->inofreefwd = cpu_to_le32(head);
2951 iagp->inofreeback = cpu_to_le32(-1);
2952 write_metapage(hbp);
2953 }
2954
2955 imap->im_agctl[n].inofree =
2956 le32_to_cpu(iagp->iagnum);
2957
2958 /* merge AG backed free inodes */
2959 imap->im_agctl[n].numfree +=
2960 le32_to_cpu(iagp->nfreeinos);
2961 xnumfree += le32_to_cpu(iagp->nfreeinos);
2962 }
2963
2964 /* if any free extents, insert at AG free extent list */
2965 if (le32_to_cpu(iagp->nfreeexts) > 0) {
2966 if ((head = imap->im_agctl[n].extfree) == -1) {
2967 iagp->extfreefwd = cpu_to_le32(-1);
2968 iagp->extfreeback = cpu_to_le32(-1);
2969 } else {
2970 if ((rc = diIAGRead(imap, head, &hbp))) {
2971 rcx = rc;
2972 goto nextiag;
2973 }
2974 hiagp = (struct iag *) hbp->data;
2975 hiagp->extfreeback = iagp->iagnum;
2976 iagp->extfreefwd = cpu_to_le32(head);
2977 iagp->extfreeback = cpu_to_le32(-1);
2978 write_metapage(hbp);
2979 }
2980
2981 imap->im_agctl[n].extfree =
2982 le32_to_cpu(iagp->iagnum);
2983 }
2984
2985 nextiag:
2986 write_metapage(bp);
2987 }
2988
2989 if (xnuminos != atomic_read(&imap->im_numinos) ||
2990 xnumfree != atomic_read(&imap->im_numfree)) {
2991 jfs_error(ipimap->i_sb,
2992 "diExtendFs: numinos or numfree incorrect");
2993 return -EIO;
2994 }
2995
2996 return rcx;
2997}
2998
2999
3000/*
3001 * duplicateIXtree()
3002 *
3003 * serialization: IWRITE_LOCK held on entry/exit
3004 *
3005 * note: shadow page with regular inode (rel.2);
3006 */
3007static void duplicateIXtree(struct super_block *sb, s64 blkno,
3008 int xlen, s64 *xaddr)
3009{
3010 struct jfs_superblock *j_sb;
3011 struct buffer_head *bh;
3012 struct inode *ip;
3013 tid_t tid;
3014
3015 /* if AIT2 ipmap2 is bad, do not try to update it */
3016 if (JFS_SBI(sb)->mntflag & JFS_BAD_SAIT) /* s_flag */
3017 return;
3018 ip = diReadSpecial(sb, FILESYSTEM_I, 1);
3019 if (ip == NULL) {
3020 JFS_SBI(sb)->mntflag |= JFS_BAD_SAIT;
3021 if (readSuper(sb, &bh))
3022 return;
3023 j_sb = (struct jfs_superblock *)bh->b_data;
3024 j_sb->s_flag |= cpu_to_le32(JFS_BAD_SAIT);
3025
3026 mark_buffer_dirty(bh);
3027 sync_dirty_buffer(bh);
3028 brelse(bh);
3029 return;
3030 }
3031
3032 /* start transaction */
3033 tid = txBegin(sb, COMMIT_FORCE);
3034 /* update the inode map addressing structure to point to it */
3035 if (xtInsert(tid, ip, 0, blkno, xlen, xaddr, 0)) {
3036 JFS_SBI(sb)->mntflag |= JFS_BAD_SAIT;
3037 txAbort(tid, 1);
3038 goto cleanup;
3039
3040 }
3041 /* update the inode map's inode to reflect the extension */
3042 ip->i_size += PSIZE;
3043 inode_add_bytes(ip, PSIZE);
3044 txCommit(tid, 1, &ip, COMMIT_FORCE);
3045 cleanup:
3046 txEnd(tid);
3047 diFreeSpecial(ip);
3048}
3049
3050/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05003051 * NAME: copy_from_dinode()
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -05003053 * FUNCTION: Copies inode info from disk inode to in-memory inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 *
3055 * RETURN VALUES:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05003056 * 0 - success
3057 * -ENOMEM - insufficient memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 */
3059static int copy_from_dinode(struct dinode * dip, struct inode *ip)
3060{
3061 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
Dave Kleikamp69eb66d2006-03-09 13:59:30 -06003062 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
3064 jfs_ip->fileset = le32_to_cpu(dip->di_fileset);
3065 jfs_ip->mode2 = le32_to_cpu(dip->di_mode);
Dave Kleikamp3e2221c2007-04-25 09:36:20 -05003066 jfs_set_inode_flags(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067
3068 ip->i_mode = le32_to_cpu(dip->di_mode) & 0xffff;
Dave Kleikamp69eb66d2006-03-09 13:59:30 -06003069 if (sbi->umask != -1) {
3070 ip->i_mode = (ip->i_mode & ~0777) | (0777 & ~sbi->umask);
3071 /* For directories, add x permission if r is allowed by umask */
3072 if (S_ISDIR(ip->i_mode)) {
3073 if (ip->i_mode & 0400)
3074 ip->i_mode |= 0100;
3075 if (ip->i_mode & 0040)
3076 ip->i_mode |= 0010;
3077 if (ip->i_mode & 0004)
3078 ip->i_mode |= 0001;
3079 }
3080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 ip->i_nlink = le32_to_cpu(dip->di_nlink);
Dave Kleikamp69eb66d2006-03-09 13:59:30 -06003082
3083 jfs_ip->saved_uid = le32_to_cpu(dip->di_uid);
3084 if (sbi->uid == -1)
3085 ip->i_uid = jfs_ip->saved_uid;
3086 else {
3087 ip->i_uid = sbi->uid;
3088 }
3089
3090 jfs_ip->saved_gid = le32_to_cpu(dip->di_gid);
3091 if (sbi->gid == -1)
3092 ip->i_gid = jfs_ip->saved_gid;
3093 else {
3094 ip->i_gid = sbi->gid;
3095 }
3096
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 ip->i_size = le64_to_cpu(dip->di_size);
3098 ip->i_atime.tv_sec = le32_to_cpu(dip->di_atime.tv_sec);
3099 ip->i_atime.tv_nsec = le32_to_cpu(dip->di_atime.tv_nsec);
3100 ip->i_mtime.tv_sec = le32_to_cpu(dip->di_mtime.tv_sec);
3101 ip->i_mtime.tv_nsec = le32_to_cpu(dip->di_mtime.tv_nsec);
3102 ip->i_ctime.tv_sec = le32_to_cpu(dip->di_ctime.tv_sec);
3103 ip->i_ctime.tv_nsec = le32_to_cpu(dip->di_ctime.tv_nsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 ip->i_blocks = LBLK2PBLK(ip->i_sb, le64_to_cpu(dip->di_nblocks));
3105 ip->i_generation = le32_to_cpu(dip->di_gen);
3106
3107 jfs_ip->ixpxd = dip->di_ixpxd; /* in-memory pxd's are little-endian */
3108 jfs_ip->acl = dip->di_acl; /* as are dxd's */
3109 jfs_ip->ea = dip->di_ea;
3110 jfs_ip->next_index = le32_to_cpu(dip->di_next_index);
3111 jfs_ip->otime = le32_to_cpu(dip->di_otime.tv_sec);
3112 jfs_ip->acltype = le32_to_cpu(dip->di_acltype);
3113
3114 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode)) {
3115 jfs_ip->dev = le32_to_cpu(dip->di_rdev);
3116 ip->i_rdev = new_decode_dev(jfs_ip->dev);
3117 }
3118
3119 if (S_ISDIR(ip->i_mode)) {
3120 memcpy(&jfs_ip->i_dirtable, &dip->di_dirtable, 384);
3121 } else if (S_ISREG(ip->i_mode) || S_ISLNK(ip->i_mode)) {
3122 memcpy(&jfs_ip->i_xtroot, &dip->di_xtroot, 288);
3123 } else
3124 memcpy(&jfs_ip->i_inline_ea, &dip->di_inlineea, 128);
3125
3126 /* Zero the in-memory-only stuff */
3127 jfs_ip->cflag = 0;
3128 jfs_ip->btindex = 0;
3129 jfs_ip->btorder = 0;
3130 jfs_ip->bxflag = 0;
3131 jfs_ip->blid = 0;
3132 jfs_ip->atlhead = 0;
3133 jfs_ip->atltail = 0;
3134 jfs_ip->xtlid = 0;
3135 return (0);
3136}
3137
3138/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05003139 * NAME: copy_to_dinode()
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -05003141 * FUNCTION: Copies inode info from in-memory inode to disk inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 */
3143static void copy_to_dinode(struct dinode * dip, struct inode *ip)
3144{
3145 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
Dave Kleikamp69eb66d2006-03-09 13:59:30 -06003146 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147
3148 dip->di_fileset = cpu_to_le32(jfs_ip->fileset);
Dave Kleikamp69eb66d2006-03-09 13:59:30 -06003149 dip->di_inostamp = cpu_to_le32(sbi->inostamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 dip->di_number = cpu_to_le32(ip->i_ino);
3151 dip->di_gen = cpu_to_le32(ip->i_generation);
3152 dip->di_size = cpu_to_le64(ip->i_size);
3153 dip->di_nblocks = cpu_to_le64(PBLK2LBLK(ip->i_sb, ip->i_blocks));
3154 dip->di_nlink = cpu_to_le32(ip->i_nlink);
Dave Kleikamp69eb66d2006-03-09 13:59:30 -06003155 if (sbi->uid == -1)
3156 dip->di_uid = cpu_to_le32(ip->i_uid);
3157 else
3158 dip->di_uid = cpu_to_le32(jfs_ip->saved_uid);
3159 if (sbi->gid == -1)
3160 dip->di_gid = cpu_to_le32(ip->i_gid);
3161 else
3162 dip->di_gid = cpu_to_le32(jfs_ip->saved_gid);
Dave Kleikamp3e2221c2007-04-25 09:36:20 -05003163 jfs_get_inode_flags(jfs_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 /*
3165 * mode2 is only needed for storing the higher order bits.
3166 * Trust i_mode for the lower order ones
3167 */
Dave Kleikamp69eb66d2006-03-09 13:59:30 -06003168 if (sbi->umask == -1)
3169 dip->di_mode = cpu_to_le32((jfs_ip->mode2 & 0xffff0000) |
3170 ip->i_mode);
3171 else /* Leave the original permissions alone */
3172 dip->di_mode = cpu_to_le32(jfs_ip->mode2);
3173
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 dip->di_atime.tv_sec = cpu_to_le32(ip->i_atime.tv_sec);
3175 dip->di_atime.tv_nsec = cpu_to_le32(ip->i_atime.tv_nsec);
3176 dip->di_ctime.tv_sec = cpu_to_le32(ip->i_ctime.tv_sec);
3177 dip->di_ctime.tv_nsec = cpu_to_le32(ip->i_ctime.tv_nsec);
3178 dip->di_mtime.tv_sec = cpu_to_le32(ip->i_mtime.tv_sec);
3179 dip->di_mtime.tv_nsec = cpu_to_le32(ip->i_mtime.tv_nsec);
3180 dip->di_ixpxd = jfs_ip->ixpxd; /* in-memory pxd's are little-endian */
3181 dip->di_acl = jfs_ip->acl; /* as are dxd's */
3182 dip->di_ea = jfs_ip->ea;
3183 dip->di_next_index = cpu_to_le32(jfs_ip->next_index);
3184 dip->di_otime.tv_sec = cpu_to_le32(jfs_ip->otime);
3185 dip->di_otime.tv_nsec = 0;
3186 dip->di_acltype = cpu_to_le32(jfs_ip->acltype);
3187 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode))
3188 dip->di_rdev = cpu_to_le32(jfs_ip->dev);
3189}