blob: 346057218edcdd440e1df24480264381c8101aa6 [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/*
60 * imap locks
61 */
62/* iag free list lock */
Ingo Molnar1de87442006-01-24 15:22:50 -060063#define IAGFREE_LOCK_INIT(imap) mutex_init(&imap->im_freelock)
64#define IAGFREE_LOCK(imap) mutex_lock(&imap->im_freelock)
65#define IAGFREE_UNLOCK(imap) mutex_unlock(&imap->im_freelock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67/* per ag iag list locks */
Ingo Molnar1de87442006-01-24 15:22:50 -060068#define AG_LOCK_INIT(imap,index) mutex_init(&(imap->im_aglock[index]))
69#define AG_LOCK(imap,agno) mutex_lock(&imap->im_aglock[agno])
70#define AG_UNLOCK(imap,agno) mutex_unlock(&imap->im_aglock[agno])
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 * forward references
74 */
Richard Knutsson4d817152006-09-30 23:27:14 -070075static int diAllocAG(struct inomap *, int, bool, struct inode *);
76static int diAllocAny(struct inomap *, int, bool, struct inode *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static int diAllocBit(struct inomap *, struct iag *, int);
78static int diAllocExt(struct inomap *, int, struct inode *);
79static int diAllocIno(struct inomap *, int, struct inode *);
80static int diFindFree(u32, int);
81static int diNewExt(struct inomap *, struct iag *, int);
82static int diNewIAG(struct inomap *, int *, int, struct metapage **);
83static void duplicateIXtree(struct super_block *, s64, int, s64 *);
84
85static int diIAGRead(struct inomap * imap, int, struct metapage **);
86static int copy_from_dinode(struct dinode *, struct inode *);
87static void copy_to_dinode(struct dinode *, struct inode *);
88
89/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -050090 * NAME: diMount()
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -050092 * FUNCTION: initialize the incore inode map control structures for
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * a fileset or aggregate init time.
94 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -050095 * the inode map's control structure (dinomap) is
96 * brought in from disk and placed in virtual memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 *
98 * PARAMETERS:
Dave Kleikampf720e3b2007-06-06 15:28:35 -050099 * ipimap - pointer to inode map inode for the aggregate or fileset.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 *
101 * RETURN VALUES:
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500102 * 0 - success
103 * -ENOMEM - insufficient free virtual memory.
104 * -EIO - i/o error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 */
106int diMount(struct inode *ipimap)
107{
108 struct inomap *imap;
109 struct metapage *mp;
110 int index;
111 struct dinomap_disk *dinom_le;
112
113 /*
114 * allocate/initialize the in-memory inode map control structure
115 */
116 /* allocate the in-memory inode map control structure. */
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800117 imap = kmalloc(sizeof(struct inomap), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 if (imap == NULL) {
119 jfs_err("diMount: kmalloc returned NULL!");
120 return -ENOMEM;
121 }
122
123 /* read the on-disk inode map control structure. */
124
125 mp = read_metapage(ipimap,
126 IMAPBLKNO << JFS_SBI(ipimap->i_sb)->l2nbperpage,
127 PSIZE, 0);
128 if (mp == NULL) {
129 kfree(imap);
130 return -EIO;
131 }
132
133 /* copy the on-disk version to the in-memory version. */
134 dinom_le = (struct dinomap_disk *) mp->data;
135 imap->im_freeiag = le32_to_cpu(dinom_le->in_freeiag);
136 imap->im_nextiag = le32_to_cpu(dinom_le->in_nextiag);
137 atomic_set(&imap->im_numinos, le32_to_cpu(dinom_le->in_numinos));
138 atomic_set(&imap->im_numfree, le32_to_cpu(dinom_le->in_numfree));
139 imap->im_nbperiext = le32_to_cpu(dinom_le->in_nbperiext);
140 imap->im_l2nbperiext = le32_to_cpu(dinom_le->in_l2nbperiext);
141 for (index = 0; index < MAXAG; index++) {
142 imap->im_agctl[index].inofree =
143 le32_to_cpu(dinom_le->in_agctl[index].inofree);
144 imap->im_agctl[index].extfree =
145 le32_to_cpu(dinom_le->in_agctl[index].extfree);
146 imap->im_agctl[index].numinos =
147 le32_to_cpu(dinom_le->in_agctl[index].numinos);
148 imap->im_agctl[index].numfree =
149 le32_to_cpu(dinom_le->in_agctl[index].numfree);
150 }
151
152 /* release the buffer. */
153 release_metapage(mp);
154
155 /*
156 * allocate/initialize inode allocation map locks
157 */
158 /* allocate and init iag free list lock */
159 IAGFREE_LOCK_INIT(imap);
160
161 /* allocate and init ag list locks */
162 for (index = 0; index < MAXAG; index++) {
163 AG_LOCK_INIT(imap, index);
164 }
165
166 /* bind the inode map inode and inode map control structure
167 * to each other.
168 */
169 imap->im_ipimap = ipimap;
170 JFS_IP(ipimap)->i_imap = imap;
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return (0);
173}
174
175
176/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500177 * NAME: diUnmount()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500179 * FUNCTION: write to disk the incore inode map control structures for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 * a fileset or aggregate at unmount time.
181 *
182 * PARAMETERS:
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500183 * ipimap - pointer to inode map inode for the aggregate or fileset.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 *
185 * RETURN VALUES:
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500186 * 0 - success
187 * -ENOMEM - insufficient free virtual memory.
188 * -EIO - i/o error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 */
190int diUnmount(struct inode *ipimap, int mounterror)
191{
192 struct inomap *imap = JFS_IP(ipimap)->i_imap;
193
194 /*
195 * update the on-disk inode map control structure
196 */
197
198 if (!(mounterror || isReadOnly(ipimap)))
199 diSync(ipimap);
200
201 /*
202 * Invalidate the page cache buffers
203 */
204 truncate_inode_pages(ipimap->i_mapping, 0);
205
206 /*
207 * free in-memory control structure
208 */
209 kfree(imap);
210
211 return (0);
212}
213
214
215/*
216 * diSync()
217 */
218int diSync(struct inode *ipimap)
219{
220 struct dinomap_disk *dinom_le;
221 struct inomap *imp = JFS_IP(ipimap)->i_imap;
222 struct metapage *mp;
223 int index;
224
225 /*
226 * write imap global conrol page
227 */
228 /* read the on-disk inode map control structure */
229 mp = get_metapage(ipimap,
230 IMAPBLKNO << JFS_SBI(ipimap->i_sb)->l2nbperpage,
231 PSIZE, 0);
232 if (mp == NULL) {
233 jfs_err("diSync: get_metapage failed!");
234 return -EIO;
235 }
236
237 /* copy the in-memory version to the on-disk version */
238 dinom_le = (struct dinomap_disk *) mp->data;
239 dinom_le->in_freeiag = cpu_to_le32(imp->im_freeiag);
240 dinom_le->in_nextiag = cpu_to_le32(imp->im_nextiag);
241 dinom_le->in_numinos = cpu_to_le32(atomic_read(&imp->im_numinos));
242 dinom_le->in_numfree = cpu_to_le32(atomic_read(&imp->im_numfree));
243 dinom_le->in_nbperiext = cpu_to_le32(imp->im_nbperiext);
244 dinom_le->in_l2nbperiext = cpu_to_le32(imp->im_l2nbperiext);
245 for (index = 0; index < MAXAG; index++) {
246 dinom_le->in_agctl[index].inofree =
247 cpu_to_le32(imp->im_agctl[index].inofree);
248 dinom_le->in_agctl[index].extfree =
249 cpu_to_le32(imp->im_agctl[index].extfree);
250 dinom_le->in_agctl[index].numinos =
251 cpu_to_le32(imp->im_agctl[index].numinos);
252 dinom_le->in_agctl[index].numfree =
253 cpu_to_le32(imp->im_agctl[index].numfree);
254 }
255
256 /* write out the control structure */
257 write_metapage(mp);
258
259 /*
260 * write out dirty pages of imap
261 */
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800262 filemap_write_and_wait(ipimap->i_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 diWriteSpecial(ipimap, 0);
265
266 return (0);
267}
268
269
270/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500271 * NAME: diRead()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500273 * FUNCTION: initialize an incore inode from disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 *
275 * on entry, the specifed incore inode should itself
276 * specify the disk inode number corresponding to the
277 * incore inode (i.e. i_number should be initialized).
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500278 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 * this routine handles incore inode initialization for
280 * both "special" and "regular" inodes. special inodes
281 * are those required early in the mount process and
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500282 * require special handling since much of the file system
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 * is not yet initialized. these "special" inodes are
284 * identified by a NULL inode map inode pointer and are
285 * actually initialized by a call to diReadSpecial().
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500286 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 * for regular inodes, the iag describing the disk inode
288 * is read from disk to determine the inode extent address
289 * for the disk inode. with the inode extent address in
290 * hand, the page of the extent that contains the disk
291 * inode is read and the disk inode is copied to the
292 * incore inode.
293 *
294 * PARAMETERS:
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500295 * ip - pointer to incore inode to be initialized from disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 *
297 * RETURN VALUES:
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500298 * 0 - success
299 * -EIO - i/o error.
300 * -ENOMEM - insufficient memory
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500301 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 */
303int diRead(struct inode *ip)
304{
305 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
306 int iagno, ino, extno, rc;
307 struct inode *ipimap;
308 struct dinode *dp;
309 struct iag *iagp;
310 struct metapage *mp;
311 s64 blkno, agstart;
312 struct inomap *imap;
313 int block_offset;
314 int inodes_left;
Dave Kleikamp8f6cff92006-10-13 12:42:36 -0500315 unsigned long pageno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 int rel_inode;
317
318 jfs_info("diRead: ino = %ld", ip->i_ino);
319
320 ipimap = sbi->ipimap;
321 JFS_IP(ip)->ipimap = ipimap;
322
323 /* determine the iag number for this inode (number) */
324 iagno = INOTOIAG(ip->i_ino);
325
326 /* read the iag */
327 imap = JFS_IP(ipimap)->i_imap;
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600328 IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 rc = diIAGRead(imap, iagno, &mp);
330 IREAD_UNLOCK(ipimap);
331 if (rc) {
332 jfs_err("diRead: diIAGRead returned %d", rc);
333 return (rc);
334 }
335
336 iagp = (struct iag *) mp->data;
337
338 /* determine inode extent that holds the disk inode */
339 ino = ip->i_ino & (INOSPERIAG - 1);
340 extno = ino >> L2INOSPEREXT;
341
342 if ((lengthPXD(&iagp->inoext[extno]) != imap->im_nbperiext) ||
343 (addressPXD(&iagp->inoext[extno]) == 0)) {
344 release_metapage(mp);
345 return -ESTALE;
346 }
347
348 /* get disk block number of the page within the inode extent
349 * that holds the disk inode.
350 */
351 blkno = INOPBLK(&iagp->inoext[extno], ino, sbi->l2nbperpage);
352
353 /* get the ag for the iag */
354 agstart = le64_to_cpu(iagp->agstart);
355
356 release_metapage(mp);
357
358 rel_inode = (ino & (INOSPERPAGE - 1));
359 pageno = blkno >> sbi->l2nbperpage;
360
361 if ((block_offset = ((u32) blkno & (sbi->nbperpage - 1)))) {
362 /*
363 * OS/2 didn't always align inode extents on page boundaries
364 */
365 inodes_left =
366 (sbi->nbperpage - block_offset) << sbi->l2niperblk;
367
368 if (rel_inode < inodes_left)
369 rel_inode += block_offset << sbi->l2niperblk;
370 else {
371 pageno += 1;
372 rel_inode -= inodes_left;
373 }
374 }
375
376 /* read the page of disk inode */
377 mp = read_metapage(ipimap, pageno << sbi->l2nbperpage, PSIZE, 1);
Joe Perches09aaa742007-11-13 22:16:08 -0600378 if (!mp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 jfs_err("diRead: read_metapage failed");
380 return -EIO;
381 }
382
Michael Opdenacker59c51592007-05-09 08:57:56 +0200383 /* locate the disk inode requested */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 dp = (struct dinode *) mp->data;
385 dp += rel_inode;
386
387 if (ip->i_ino != le32_to_cpu(dp->di_number)) {
388 jfs_error(ip->i_sb, "diRead: i_ino != di_number");
389 rc = -EIO;
390 } else if (le32_to_cpu(dp->di_nlink) == 0)
391 rc = -ESTALE;
392 else
393 /* copy the disk inode to the in-memory inode */
394 rc = copy_from_dinode(dp, ip);
395
396 release_metapage(mp);
397
398 /* set the ag for the inode */
399 JFS_IP(ip)->agno = BLKTOAG(agstart, sbi);
400 JFS_IP(ip)->active_ag = -1;
401
402 return (rc);
403}
404
405
406/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500407 * NAME: diReadSpecial()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500409 * FUNCTION: initialize a 'special' inode from disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 *
411 * this routines handles aggregate level inodes. The
412 * inode cache cannot differentiate between the
413 * aggregate inodes and the filesystem inodes, so we
414 * handle these here. We don't actually use the aggregate
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500415 * inode map, since these inodes are at a fixed location
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 * and in some cases the aggregate inode map isn't initialized
417 * yet.
418 *
419 * PARAMETERS:
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500420 * sb - filesystem superblock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 * inum - aggregate inode number
422 * secondary - 1 if secondary aggregate inode table
423 *
424 * RETURN VALUES:
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500425 * new inode - success
426 * NULL - i/o error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 */
428struct inode *diReadSpecial(struct super_block *sb, ino_t inum, int secondary)
429{
430 struct jfs_sb_info *sbi = JFS_SBI(sb);
431 uint address;
432 struct dinode *dp;
433 struct inode *ip;
434 struct metapage *mp;
435
436 ip = new_inode(sb);
437 if (ip == NULL) {
438 jfs_err("diReadSpecial: new_inode returned NULL!");
439 return ip;
440 }
441
442 if (secondary) {
443 address = addressPXD(&sbi->ait2) >> sbi->l2nbperpage;
444 JFS_IP(ip)->ipimap = sbi->ipaimap2;
445 } else {
446 address = AITBL_OFF >> L2PSIZE;
447 JFS_IP(ip)->ipimap = sbi->ipaimap;
448 }
449
450 ASSERT(inum < INOSPEREXT);
451
452 ip->i_ino = inum;
453
454 address += inum >> 3; /* 8 inodes per 4K page */
455
456 /* read the page of fixed disk inode (AIT) in raw mode */
457 mp = read_metapage(ip, address << sbi->l2nbperpage, PSIZE, 1);
458 if (mp == NULL) {
459 ip->i_nlink = 1; /* Don't want iput() deleting it */
460 iput(ip);
461 return (NULL);
462 }
463
464 /* get the pointer to the disk inode of interest */
465 dp = (struct dinode *) (mp->data);
466 dp += inum % 8; /* 8 inodes per 4K page */
467
468 /* copy on-disk inode to in-memory inode */
469 if ((copy_from_dinode(dp, ip)) != 0) {
470 /* handle bad return by returning NULL for ip */
471 ip->i_nlink = 1; /* Don't want iput() deleting it */
472 iput(ip);
473 /* release the page */
474 release_metapage(mp);
475 return (NULL);
476
477 }
478
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600479 ip->i_mapping->a_ops = &jfs_metapage_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 mapping_set_gfp_mask(ip->i_mapping, GFP_NOFS);
481
482 /* Allocations to metadata inodes should not affect quotas */
483 ip->i_flags |= S_NOQUOTA;
484
485 if ((inum == FILESYSTEM_I) && (JFS_IP(ip)->ipimap == sbi->ipaimap)) {
486 sbi->gengen = le32_to_cpu(dp->di_gengen);
487 sbi->inostamp = le32_to_cpu(dp->di_inostamp);
488 }
489
490 /* release the page */
491 release_metapage(mp);
492
Al Viro5b45d962008-12-29 07:40:31 -0500493 /*
Dave Kleikampda9c1382009-01-09 10:53:35 -0600494 * __mark_inode_dirty expects inodes to be hashed. Since we don't
495 * want special inodes in the fileset inode space, we make them
496 * appear hashed, but do not put on any lists. hlist_del()
Al Viro5b45d962008-12-29 07:40:31 -0500497 * will work fine and require no locking.
498 */
499 ip->i_hash.pprev = &ip->i_hash.next;
Dave Kleikampac17b8b2005-10-03 15:32:11 -0500500
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);
Joe Perches09aaa742007-11-13 22:16:08 -0600657 if (!mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 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) {
Dave Kleikamp288e4d82007-06-13 10:17:50 -0500893 print_hex_dump(KERN_ERR, "imap: ", DUMP_PREFIX_ADDRESS, 16, 4,
894 imap, 32, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 jfs_error(ip->i_sb,
896 "diFree: inum = %d, iagno = %d, nextiag = %d",
897 (uint) inum, iagno, imap->im_nextiag);
898 return -EIO;
899 }
900
901 /* get the allocation group for this ino.
902 */
903 agno = JFS_IP(ip)->agno;
904
905 /* Lock the AG specific inode map information
906 */
907 AG_LOCK(imap, agno);
908
909 /* Obtain read lock in imap inode. Don't release it until we have
910 * read all of the IAG's that we are going to.
911 */
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -0600912 IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 /* read the iag.
915 */
916 if ((rc = diIAGRead(imap, iagno, &mp))) {
917 IREAD_UNLOCK(ipimap);
918 AG_UNLOCK(imap, agno);
919 return (rc);
920 }
921 iagp = (struct iag *) mp->data;
922
923 /* get the inode number and extent number of the inode within
924 * the iag and the inode number within the extent.
925 */
926 ino = inum & (INOSPERIAG - 1);
927 extno = ino >> L2INOSPEREXT;
928 bitno = ino & (INOSPEREXT - 1);
929 mask = HIGHORDER >> bitno;
930
931 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
932 jfs_error(ip->i_sb,
933 "diFree: wmap shows inode already free");
934 }
935
936 if (!addressPXD(&iagp->inoext[extno])) {
937 release_metapage(mp);
938 IREAD_UNLOCK(ipimap);
939 AG_UNLOCK(imap, agno);
940 jfs_error(ip->i_sb, "diFree: invalid inoext");
941 return -EIO;
942 }
943
944 /* compute the bitmap for the extent reflecting the freed inode.
945 */
946 bitmap = le32_to_cpu(iagp->wmap[extno]) & ~mask;
947
948 if (imap->im_agctl[agno].numfree > imap->im_agctl[agno].numinos) {
949 release_metapage(mp);
950 IREAD_UNLOCK(ipimap);
951 AG_UNLOCK(imap, agno);
952 jfs_error(ip->i_sb, "diFree: numfree > numinos");
953 return -EIO;
954 }
955 /*
Dave Kleikampf720e3b2007-06-06 15:28:35 -0500956 * inode extent still has some inodes or below low water mark:
957 * keep the inode extent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 */
959 if (bitmap ||
960 imap->im_agctl[agno].numfree < 96 ||
961 (imap->im_agctl[agno].numfree < 288 &&
962 (((imap->im_agctl[agno].numfree * 100) /
963 imap->im_agctl[agno].numinos) <= 25))) {
964 /* if the iag currently has no free inodes (i.e.,
965 * the inode being freed is the first free inode of iag),
966 * insert the iag at head of the inode free list for the ag.
967 */
968 if (iagp->nfreeinos == 0) {
969 /* check if there are any iags on the ag inode
970 * free list. if so, read the first one so that
971 * we can link the current iag onto the list at
972 * the head.
973 */
974 if ((fwd = imap->im_agctl[agno].inofree) >= 0) {
975 /* read the iag that currently is the head
976 * of the list.
977 */
978 if ((rc = diIAGRead(imap, fwd, &amp))) {
979 IREAD_UNLOCK(ipimap);
980 AG_UNLOCK(imap, agno);
981 release_metapage(mp);
982 return (rc);
983 }
984 aiagp = (struct iag *) amp->data;
985
986 /* make current head point back to the iag.
987 */
988 aiagp->inofreeback = cpu_to_le32(iagno);
989
990 write_metapage(amp);
991 }
992
993 /* iag points forward to current head and iag
994 * becomes the new head of the list.
995 */
996 iagp->inofreefwd =
997 cpu_to_le32(imap->im_agctl[agno].inofree);
998 iagp->inofreeback = cpu_to_le32(-1);
999 imap->im_agctl[agno].inofree = iagno;
1000 }
1001 IREAD_UNLOCK(ipimap);
1002
1003 /* update the free inode summary map for the extent if
1004 * freeing the inode means the extent will now have free
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001005 * inodes (i.e., the inode being freed is the first free
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 * inode of extent),
1007 */
1008 if (iagp->wmap[extno] == cpu_to_le32(ONES)) {
1009 sword = extno >> L2EXTSPERSUM;
1010 bitno = extno & (EXTSPERSUM - 1);
1011 iagp->inosmap[sword] &=
1012 cpu_to_le32(~(HIGHORDER >> bitno));
1013 }
1014
1015 /* update the bitmap.
1016 */
1017 iagp->wmap[extno] = cpu_to_le32(bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 /* update the free inode counts at the iag, ag and
1020 * map level.
1021 */
Marcin Slusarz89145622008-02-13 15:34:20 -06001022 le32_add_cpu(&iagp->nfreeinos, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 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 */
Marcin Slusarz89145622008-02-13 15:34:20 -06001221 le32_add_cpu(&iagp->nfreeinos, -(INOSPEREXT - 1));
1222 le32_add_cpu(&iagp->nfreeexts, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 /* update the number of free inodes and backed inodes
1225 * at the ag and inode map level.
1226 */
1227 imap->im_agctl[agno].numfree -= (INOSPEREXT - 1);
1228 imap->im_agctl[agno].numinos -= INOSPEREXT;
1229 atomic_sub(INOSPEREXT - 1, &imap->im_numfree);
1230 atomic_sub(INOSPEREXT, &imap->im_numinos);
1231
1232 if (amp)
1233 write_metapage(amp);
1234 if (bmp)
1235 write_metapage(bmp);
1236 if (cmp)
1237 write_metapage(cmp);
1238 if (dmp)
1239 write_metapage(dmp);
1240
1241 /*
1242 * start transaction to update block allocation map
1243 * for the inode extent freed;
1244 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001245 * N.B. AG_LOCK is released and iag will be released below, and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 * other thread may allocate inode from/reusing the ixad freed
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001247 * BUT with new/different backing inode extent from the extent
1248 * to be freed by the transaction;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 */
1250 tid = txBegin(ipimap->i_sb, COMMIT_FORCE);
Ingo Molnar1de87442006-01-24 15:22:50 -06001251 mutex_lock(&JFS_IP(ipimap)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001253 /* acquire tlock of the iag page of the freed ixad
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 * to force the page NOHOMEOK (even though no data is
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001255 * logged from the iag page) until NOREDOPAGE|FREEXTENT log
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 * for the free of the extent is committed;
1257 * write FREEXTENT|NOREDOPAGE log record
1258 * N.B. linelock is overlaid as freed extent descriptor;
1259 */
1260 tlck = txLock(tid, ipimap, mp, tlckINODE | tlckFREE);
1261 pxdlock = (struct pxd_lock *) & tlck->lock;
1262 pxdlock->flag = mlckFREEPXD;
1263 pxdlock->pxd = freepxd;
1264 pxdlock->index = 1;
1265
1266 write_metapage(mp);
1267
1268 iplist[0] = ipimap;
1269
1270 /*
1271 * logredo needs the IAG number and IAG extent index in order
1272 * to ensure that the IMap is consistent. The least disruptive
1273 * way to pass these values through to the transaction manager
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001274 * is in the iplist array.
1275 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 * It's not pretty, but it works.
1277 */
1278 iplist[1] = (struct inode *) (size_t)iagno;
1279 iplist[2] = (struct inode *) (size_t)extno;
1280
1281 rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
1282
1283 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -06001284 mutex_unlock(&JFS_IP(ipimap)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 /* unlock the AG inode map information */
1287 AG_UNLOCK(imap, agno);
1288
1289 return (0);
1290
1291 error_out:
1292 IREAD_UNLOCK(ipimap);
1293
1294 if (amp)
1295 release_metapage(amp);
1296 if (bmp)
1297 release_metapage(bmp);
1298 if (cmp)
1299 release_metapage(cmp);
1300 if (dmp)
1301 release_metapage(dmp);
1302
1303 AG_UNLOCK(imap, agno);
1304
1305 release_metapage(mp);
1306
1307 return (rc);
1308}
1309
1310/*
1311 * There are several places in the diAlloc* routines where we initialize
1312 * the inode.
1313 */
1314static inline void
1315diInitInode(struct inode *ip, int iagno, int ino, int extno, struct iag * iagp)
1316{
1317 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
1318 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
1319
1320 ip->i_ino = (iagno << L2INOSPERIAG) + ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 jfs_ip->ixpxd = iagp->inoext[extno];
1322 jfs_ip->agno = BLKTOAG(le64_to_cpu(iagp->agstart), sbi);
1323 jfs_ip->active_ag = -1;
1324}
1325
1326
1327/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001328 * NAME: diAlloc(pip,dir,ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001330 * FUNCTION: allocate a disk inode from the inode working map
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 * for a fileset or aggregate.
1332 *
1333 * PARAMETERS:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001334 * pip - pointer to incore inode for the parent inode.
1335 * dir - 'true' if the new disk inode is for a directory.
1336 * ip - pointer to a new inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 *
1338 * RETURN VALUES:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001339 * 0 - success.
1340 * -ENOSPC - insufficient disk resources.
1341 * -EIO - i/o error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 */
Richard Knutsson4d817152006-09-30 23:27:14 -07001343int diAlloc(struct inode *pip, bool dir, struct inode *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344{
1345 int rc, ino, iagno, addext, extno, bitno, sword;
1346 int nwords, rem, i, agno;
1347 u32 mask, inosmap, extsmap;
1348 struct inode *ipimap;
1349 struct metapage *mp;
1350 ino_t inum;
1351 struct iag *iagp;
1352 struct inomap *imap;
1353
1354 /* get the pointers to the inode map inode and the
1355 * corresponding imap control structure.
1356 */
1357 ipimap = JFS_SBI(pip->i_sb)->ipimap;
1358 imap = JFS_IP(ipimap)->i_imap;
1359 JFS_IP(ip)->ipimap = ipimap;
1360 JFS_IP(ip)->fileset = FILESYSTEM_I;
1361
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001362 /* for a directory, the allocation policy is to start
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 * at the ag level using the preferred ag.
1364 */
Richard Knutsson4d817152006-09-30 23:27:14 -07001365 if (dir) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 agno = dbNextAG(JFS_SBI(pip->i_sb)->ipbmap);
1367 AG_LOCK(imap, agno);
1368 goto tryag;
1369 }
1370
1371 /* for files, the policy starts off by trying to allocate from
1372 * the same iag containing the parent disk inode:
1373 * try to allocate the new disk inode close to the parent disk
1374 * inode, using parent disk inode number + 1 as the allocation
1375 * hint. (we use a left-to-right policy to attempt to avoid
1376 * moving backward on the disk.) compute the hint within the
1377 * file system and the iag.
1378 */
1379
1380 /* get the ag number of this iag */
1381 agno = JFS_IP(pip)->agno;
1382
1383 if (atomic_read(&JFS_SBI(pip->i_sb)->bmap->db_active[agno])) {
1384 /*
1385 * There is an open file actively growing. We want to
1386 * allocate new inodes from a different ag to avoid
1387 * fragmentation problems.
1388 */
1389 agno = dbNextAG(JFS_SBI(pip->i_sb)->ipbmap);
1390 AG_LOCK(imap, agno);
1391 goto tryag;
1392 }
1393
1394 inum = pip->i_ino + 1;
1395 ino = inum & (INOSPERIAG - 1);
1396
Michael Opdenacker59c51592007-05-09 08:57:56 +02001397 /* back off the hint if it is outside of the iag */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 if (ino == 0)
1399 inum = pip->i_ino;
1400
1401 /* lock the AG inode map information */
1402 AG_LOCK(imap, agno);
1403
1404 /* Get read lock on imap inode */
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001405 IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 /* get the iag number and read the iag */
1408 iagno = INOTOIAG(inum);
1409 if ((rc = diIAGRead(imap, iagno, &mp))) {
1410 IREAD_UNLOCK(ipimap);
1411 AG_UNLOCK(imap, agno);
1412 return (rc);
1413 }
1414 iagp = (struct iag *) mp->data;
1415
1416 /* determine if new inode extent is allowed to be added to the iag.
1417 * new inode extent can be added to the iag if the ag
1418 * has less than 32 free disk inodes and the iag has free extents.
1419 */
1420 addext = (imap->im_agctl[agno].numfree < 32 && iagp->nfreeexts);
1421
1422 /*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001423 * try to allocate from the IAG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 */
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001425 /* check if the inode may be allocated from the iag
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 * (i.e. the inode has free inodes or new extent can be added).
1427 */
1428 if (iagp->nfreeinos || addext) {
1429 /* determine the extent number of the hint.
1430 */
1431 extno = ino >> L2INOSPEREXT;
1432
1433 /* check if the extent containing the hint has backed
1434 * inodes. if so, try to allocate within this extent.
1435 */
1436 if (addressPXD(&iagp->inoext[extno])) {
1437 bitno = ino & (INOSPEREXT - 1);
1438 if ((bitno =
1439 diFindFree(le32_to_cpu(iagp->wmap[extno]),
1440 bitno))
1441 < INOSPEREXT) {
1442 ino = (extno << L2INOSPEREXT) + bitno;
1443
1444 /* a free inode (bit) was found within this
1445 * extent, so allocate it.
1446 */
1447 rc = diAllocBit(imap, iagp, ino);
1448 IREAD_UNLOCK(ipimap);
1449 if (rc) {
1450 assert(rc == -EIO);
1451 } else {
1452 /* set the results of the allocation
1453 * and write the iag.
1454 */
1455 diInitInode(ip, iagno, ino, extno,
1456 iagp);
1457 mark_metapage_dirty(mp);
1458 }
1459 release_metapage(mp);
1460
1461 /* free the AG lock and return.
1462 */
1463 AG_UNLOCK(imap, agno);
1464 return (rc);
1465 }
1466
1467 if (!addext)
1468 extno =
1469 (extno ==
1470 EXTSPERIAG - 1) ? 0 : extno + 1;
1471 }
1472
1473 /*
1474 * no free inodes within the extent containing the hint.
1475 *
1476 * try to allocate from the backed extents following
1477 * hint or, if appropriate (i.e. addext is true), allocate
1478 * an extent of free inodes at or following the extent
1479 * containing the hint.
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001480 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 * the free inode and free extent summary maps are used
1482 * here, so determine the starting summary map position
1483 * and the number of words we'll have to examine. again,
1484 * the approach is to allocate following the hint, so we
1485 * might have to initially ignore prior bits of the summary
1486 * map that represent extents prior to the extent containing
1487 * the hint and later revisit these bits.
1488 */
1489 bitno = extno & (EXTSPERSUM - 1);
1490 nwords = (bitno == 0) ? SMAPSZ : SMAPSZ + 1;
1491 sword = extno >> L2EXTSPERSUM;
1492
1493 /* mask any prior bits for the starting words of the
1494 * summary map.
1495 */
1496 mask = ONES << (EXTSPERSUM - bitno);
1497 inosmap = le32_to_cpu(iagp->inosmap[sword]) | mask;
1498 extsmap = le32_to_cpu(iagp->extsmap[sword]) | mask;
1499
1500 /* scan the free inode and free extent summary maps for
1501 * free resources.
1502 */
1503 for (i = 0; i < nwords; i++) {
1504 /* check if this word of the free inode summary
1505 * map describes an extent with free inodes.
1506 */
1507 if (~inosmap) {
1508 /* an extent with free inodes has been
1509 * found. determine the extent number
1510 * and the inode number within the extent.
1511 */
1512 rem = diFindFree(inosmap, 0);
1513 extno = (sword << L2EXTSPERSUM) + rem;
1514 rem = diFindFree(le32_to_cpu(iagp->wmap[extno]),
1515 0);
1516 if (rem >= INOSPEREXT) {
1517 IREAD_UNLOCK(ipimap);
1518 release_metapage(mp);
1519 AG_UNLOCK(imap, agno);
1520 jfs_error(ip->i_sb,
1521 "diAlloc: can't find free bit "
1522 "in wmap");
Li Zefan3c65e872008-05-28 08:58:56 -05001523 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 }
1525
1526 /* determine the inode number within the
1527 * iag and allocate the inode from the
1528 * map.
1529 */
1530 ino = (extno << L2INOSPEREXT) + rem;
1531 rc = diAllocBit(imap, iagp, ino);
1532 IREAD_UNLOCK(ipimap);
1533 if (rc)
1534 assert(rc == -EIO);
1535 else {
1536 /* set the results of the allocation
1537 * and write the iag.
1538 */
1539 diInitInode(ip, iagno, ino, extno,
1540 iagp);
1541 mark_metapage_dirty(mp);
1542 }
1543 release_metapage(mp);
1544
1545 /* free the AG lock and return.
1546 */
1547 AG_UNLOCK(imap, agno);
1548 return (rc);
1549
1550 }
1551
1552 /* check if we may allocate an extent of free
1553 * inodes and whether this word of the free
1554 * extents summary map describes a free extent.
1555 */
1556 if (addext && ~extsmap) {
1557 /* a free extent has been found. determine
1558 * the extent number.
1559 */
1560 rem = diFindFree(extsmap, 0);
1561 extno = (sword << L2EXTSPERSUM) + rem;
1562
1563 /* allocate an extent of free inodes.
1564 */
1565 if ((rc = diNewExt(imap, iagp, extno))) {
1566 /* if there is no disk space for a
1567 * new extent, try to allocate the
1568 * disk inode from somewhere else.
1569 */
1570 if (rc == -ENOSPC)
1571 break;
1572
1573 assert(rc == -EIO);
1574 } else {
1575 /* set the results of the allocation
1576 * and write the iag.
1577 */
1578 diInitInode(ip, iagno,
1579 extno << L2INOSPEREXT,
1580 extno, iagp);
1581 mark_metapage_dirty(mp);
1582 }
1583 release_metapage(mp);
1584 /* free the imap inode & the AG lock & return.
1585 */
1586 IREAD_UNLOCK(ipimap);
1587 AG_UNLOCK(imap, agno);
1588 return (rc);
1589 }
1590
1591 /* move on to the next set of summary map words.
1592 */
1593 sword = (sword == SMAPSZ - 1) ? 0 : sword + 1;
1594 inosmap = le32_to_cpu(iagp->inosmap[sword]);
1595 extsmap = le32_to_cpu(iagp->extsmap[sword]);
1596 }
1597 }
1598 /* unlock imap inode */
1599 IREAD_UNLOCK(ipimap);
1600
1601 /* nothing doing in this iag, so release it. */
1602 release_metapage(mp);
1603
1604 tryag:
1605 /*
1606 * try to allocate anywhere within the same AG as the parent inode.
1607 */
1608 rc = diAllocAG(imap, agno, dir, ip);
1609
1610 AG_UNLOCK(imap, agno);
1611
1612 if (rc != -ENOSPC)
1613 return (rc);
1614
1615 /*
1616 * try to allocate in any AG.
1617 */
1618 return (diAllocAny(imap, agno, dir, ip));
1619}
1620
1621
1622/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001623 * NAME: diAllocAG(imap,agno,dir,ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001625 * FUNCTION: allocate a disk inode from the allocation group.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 *
1627 * this routine first determines if a new extent of free
1628 * inodes should be added for the allocation group, with
1629 * the current request satisfied from this extent. if this
1630 * is the case, an attempt will be made to do just that. if
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001631 * this attempt fails or it has been determined that a new
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 * extent should not be added, an attempt is made to satisfy
1633 * the request by allocating an existing (backed) free inode
1634 * from the allocation group.
1635 *
1636 * PRE CONDITION: Already have the AG lock for this AG.
1637 *
1638 * PARAMETERS:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001639 * imap - pointer to inode map control structure.
1640 * agno - allocation group to allocate from.
1641 * dir - 'true' if the new disk inode is for a directory.
1642 * ip - pointer to the new inode to be filled in on successful return
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 * with the disk inode number allocated, its extent address
1644 * and the start of the ag.
1645 *
1646 * RETURN VALUES:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001647 * 0 - success.
1648 * -ENOSPC - insufficient disk resources.
1649 * -EIO - i/o error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 */
1651static int
Richard Knutsson4d817152006-09-30 23:27:14 -07001652diAllocAG(struct inomap * imap, int agno, bool dir, struct inode *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653{
1654 int rc, addext, numfree, numinos;
1655
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001656 /* get the number of free and the number of backed disk
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 * inodes currently within the ag.
1658 */
1659 numfree = imap->im_agctl[agno].numfree;
1660 numinos = imap->im_agctl[agno].numinos;
1661
1662 if (numfree > numinos) {
1663 jfs_error(ip->i_sb, "diAllocAG: numfree > numinos");
1664 return -EIO;
1665 }
1666
1667 /* determine if we should allocate a new extent of free inodes
1668 * within the ag: for directory inodes, add a new extent
1669 * if there are a small number of free inodes or number of free
1670 * inodes is a small percentage of the number of backed inodes.
1671 */
Richard Knutsson4d817152006-09-30 23:27:14 -07001672 if (dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 addext = (numfree < 64 ||
1674 (numfree < 256
1675 && ((numfree * 100) / numinos) <= 20));
1676 else
1677 addext = (numfree == 0);
1678
1679 /*
1680 * try to allocate a new extent of free inodes.
1681 */
1682 if (addext) {
1683 /* if free space is not avaliable for this new extent, try
1684 * below to allocate a free and existing (already backed)
1685 * inode from the ag.
1686 */
1687 if ((rc = diAllocExt(imap, agno, ip)) != -ENOSPC)
1688 return (rc);
1689 }
1690
1691 /*
1692 * try to allocate an existing free inode from the ag.
1693 */
1694 return (diAllocIno(imap, agno, ip));
1695}
1696
1697
1698/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001699 * NAME: diAllocAny(imap,agno,dir,iap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001701 * FUNCTION: allocate a disk inode from any other allocation group.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 *
1703 * this routine is called when an allocation attempt within
1704 * the primary allocation group has failed. if attempts to
1705 * allocate an inode from any allocation group other than the
1706 * specified primary group.
1707 *
1708 * PARAMETERS:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001709 * imap - pointer to inode map control structure.
1710 * agno - primary allocation group (to avoid).
1711 * dir - 'true' if the new disk inode is for a directory.
1712 * ip - pointer to a new inode to be filled in on successful return
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 * with the disk inode number allocated, its extent address
1714 * and the start of the ag.
1715 *
1716 * RETURN VALUES:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001717 * 0 - success.
1718 * -ENOSPC - insufficient disk resources.
1719 * -EIO - i/o error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 */
1721static int
Richard Knutsson4d817152006-09-30 23:27:14 -07001722diAllocAny(struct inomap * imap, int agno, bool dir, struct inode *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723{
1724 int ag, rc;
1725 int maxag = JFS_SBI(imap->im_ipimap->i_sb)->bmap->db_maxag;
1726
1727
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001728 /* try to allocate from the ags following agno up to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 * the maximum ag number.
1730 */
1731 for (ag = agno + 1; ag <= maxag; ag++) {
1732 AG_LOCK(imap, ag);
1733
1734 rc = diAllocAG(imap, ag, dir, ip);
1735
1736 AG_UNLOCK(imap, ag);
1737
1738 if (rc != -ENOSPC)
1739 return (rc);
1740 }
1741
1742 /* try to allocate from the ags in front of agno.
1743 */
1744 for (ag = 0; ag < agno; ag++) {
1745 AG_LOCK(imap, ag);
1746
1747 rc = diAllocAG(imap, ag, dir, ip);
1748
1749 AG_UNLOCK(imap, ag);
1750
1751 if (rc != -ENOSPC)
1752 return (rc);
1753 }
1754
1755 /* no free disk inodes.
1756 */
1757 return -ENOSPC;
1758}
1759
1760
1761/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001762 * NAME: diAllocIno(imap,agno,ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001764 * FUNCTION: allocate a disk inode from the allocation group's free
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 * inode list, returning an error if this free list is
1766 * empty (i.e. no iags on the list).
1767 *
1768 * allocation occurs from the first iag on the list using
1769 * the iag's free inode summary map to find the leftmost
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001770 * free inode in the iag.
1771 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 * PRE CONDITION: Already have AG lock for this AG.
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001773 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 * PARAMETERS:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001775 * imap - pointer to inode map control structure.
1776 * agno - allocation group.
1777 * ip - pointer to new inode to be filled in on successful return
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 * with the disk inode number allocated, its extent address
1779 * and the start of the ag.
1780 *
1781 * RETURN VALUES:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001782 * 0 - success.
1783 * -ENOSPC - insufficient disk resources.
1784 * -EIO - i/o error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 */
1786static int diAllocIno(struct inomap * imap, int agno, struct inode *ip)
1787{
1788 int iagno, ino, rc, rem, extno, sword;
1789 struct metapage *mp;
1790 struct iag *iagp;
1791
1792 /* check if there are iags on the ag's free inode list.
1793 */
1794 if ((iagno = imap->im_agctl[agno].inofree) < 0)
1795 return -ENOSPC;
1796
1797 /* obtain read lock on imap inode */
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001798 IREAD_LOCK(imap->im_ipimap, RDWRLOCK_IMAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
1800 /* read the iag at the head of the list.
1801 */
1802 if ((rc = diIAGRead(imap, iagno, &mp))) {
1803 IREAD_UNLOCK(imap->im_ipimap);
1804 return (rc);
1805 }
1806 iagp = (struct iag *) mp->data;
1807
1808 /* better be free inodes in this iag if it is on the
1809 * list.
1810 */
1811 if (!iagp->nfreeinos) {
1812 IREAD_UNLOCK(imap->im_ipimap);
1813 release_metapage(mp);
1814 jfs_error(ip->i_sb,
1815 "diAllocIno: nfreeinos = 0, but iag on freelist");
1816 return -EIO;
1817 }
1818
1819 /* scan the free inode summary map to find an extent
1820 * with free inodes.
1821 */
1822 for (sword = 0;; sword++) {
1823 if (sword >= SMAPSZ) {
1824 IREAD_UNLOCK(imap->im_ipimap);
1825 release_metapage(mp);
1826 jfs_error(ip->i_sb,
1827 "diAllocIno: free inode not found in summary map");
1828 return -EIO;
1829 }
1830
1831 if (~iagp->inosmap[sword])
1832 break;
1833 }
1834
1835 /* found a extent with free inodes. determine
1836 * the extent number.
1837 */
1838 rem = diFindFree(le32_to_cpu(iagp->inosmap[sword]), 0);
1839 if (rem >= EXTSPERSUM) {
1840 IREAD_UNLOCK(imap->im_ipimap);
1841 release_metapage(mp);
1842 jfs_error(ip->i_sb, "diAllocIno: no free extent found");
1843 return -EIO;
1844 }
1845 extno = (sword << L2EXTSPERSUM) + rem;
1846
1847 /* find the first free inode in the extent.
1848 */
1849 rem = diFindFree(le32_to_cpu(iagp->wmap[extno]), 0);
1850 if (rem >= INOSPEREXT) {
1851 IREAD_UNLOCK(imap->im_ipimap);
1852 release_metapage(mp);
1853 jfs_error(ip->i_sb, "diAllocIno: free inode not found");
1854 return -EIO;
1855 }
1856
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001857 /* compute the inode number within the iag.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 */
1859 ino = (extno << L2INOSPEREXT) + rem;
1860
1861 /* allocate the inode.
1862 */
1863 rc = diAllocBit(imap, iagp, ino);
1864 IREAD_UNLOCK(imap->im_ipimap);
1865 if (rc) {
1866 release_metapage(mp);
1867 return (rc);
1868 }
1869
1870 /* set the results of the allocation and write the iag.
1871 */
1872 diInitInode(ip, iagno, ino, extno, iagp);
1873 write_metapage(mp);
1874
1875 return (0);
1876}
1877
1878
1879/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001880 * NAME: diAllocExt(imap,agno,ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001882 * FUNCTION: add a new extent of free inodes to an iag, allocating
1883 * an inode from this extent to satisfy the current allocation
1884 * request.
1885 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 * this routine first tries to find an existing iag with free
1887 * extents through the ag free extent list. if list is not
1888 * empty, the head of the list will be selected as the home
1889 * of the new extent of free inodes. otherwise (the list is
1890 * empty), a new iag will be allocated for the ag to contain
1891 * the extent.
Dave Kleikamp63f83c92006-10-02 09:55:27 -05001892 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 * once an iag has been selected, the free extent summary map
1894 * is used to locate a free extent within the iag and diNewExt()
1895 * is called to initialize the extent, with initialization
1896 * including the allocation of the first inode of the extent
1897 * for the purpose of satisfying this request.
1898 *
1899 * PARAMETERS:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001900 * imap - pointer to inode map control structure.
1901 * agno - allocation group number.
1902 * ip - pointer to new inode to be filled in on successful return
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 * with the disk inode number allocated, its extent address
1904 * and the start of the ag.
1905 *
1906 * RETURN VALUES:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05001907 * 0 - success.
1908 * -ENOSPC - insufficient disk resources.
1909 * -EIO - i/o error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 */
1911static int diAllocExt(struct inomap * imap, int agno, struct inode *ip)
1912{
1913 int rem, iagno, sword, extno, rc;
1914 struct metapage *mp;
1915 struct iag *iagp;
1916
1917 /* check if the ag has any iags with free extents. if not,
1918 * allocate a new iag for the ag.
1919 */
1920 if ((iagno = imap->im_agctl[agno].extfree) < 0) {
1921 /* If successful, diNewIAG will obtain the read lock on the
1922 * imap inode.
1923 */
1924 if ((rc = diNewIAG(imap, &iagno, agno, &mp))) {
1925 return (rc);
1926 }
1927 iagp = (struct iag *) mp->data;
1928
1929 /* set the ag number if this a brand new iag
1930 */
1931 iagp->agstart =
1932 cpu_to_le64(AGTOBLK(agno, imap->im_ipimap));
1933 } else {
1934 /* read the iag.
1935 */
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06001936 IREAD_LOCK(imap->im_ipimap, RDWRLOCK_IMAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 if ((rc = diIAGRead(imap, iagno, &mp))) {
1938 IREAD_UNLOCK(imap->im_ipimap);
1939 jfs_error(ip->i_sb, "diAllocExt: error reading iag");
1940 return rc;
1941 }
1942 iagp = (struct iag *) mp->data;
1943 }
1944
1945 /* using the free extent summary map, find a free extent.
1946 */
1947 for (sword = 0;; sword++) {
1948 if (sword >= SMAPSZ) {
1949 release_metapage(mp);
1950 IREAD_UNLOCK(imap->im_ipimap);
1951 jfs_error(ip->i_sb,
1952 "diAllocExt: free ext summary map not found");
1953 return -EIO;
1954 }
1955 if (~iagp->extsmap[sword])
1956 break;
1957 }
1958
1959 /* determine the extent number of the free extent.
1960 */
1961 rem = diFindFree(le32_to_cpu(iagp->extsmap[sword]), 0);
1962 if (rem >= EXTSPERSUM) {
1963 release_metapage(mp);
1964 IREAD_UNLOCK(imap->im_ipimap);
1965 jfs_error(ip->i_sb, "diAllocExt: free extent not found");
1966 return -EIO;
1967 }
1968 extno = (sword << L2EXTSPERSUM) + rem;
1969
1970 /* initialize the new extent.
1971 */
1972 rc = diNewExt(imap, iagp, extno);
1973 IREAD_UNLOCK(imap->im_ipimap);
1974 if (rc) {
1975 /* something bad happened. if a new iag was allocated,
1976 * place it back on the inode map's iag free list, and
1977 * clear the ag number information.
1978 */
1979 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
1980 IAGFREE_LOCK(imap);
1981 iagp->iagfree = cpu_to_le32(imap->im_freeiag);
1982 imap->im_freeiag = iagno;
1983 IAGFREE_UNLOCK(imap);
1984 }
1985 write_metapage(mp);
1986 return (rc);
1987 }
1988
1989 /* set the results of the allocation and write the iag.
1990 */
1991 diInitInode(ip, iagno, extno << L2INOSPEREXT, extno, iagp);
1992
1993 write_metapage(mp);
1994
1995 return (0);
1996}
1997
1998
1999/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002000 * NAME: diAllocBit(imap,iagp,ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -05002002 * FUNCTION: allocate a backed inode from an iag.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 *
2004 * this routine performs the mechanics of allocating a
2005 * specified inode from a backed extent.
2006 *
2007 * if the inode to be allocated represents the last free
2008 * inode within the iag, the iag will be removed from the
2009 * ag free inode list.
2010 *
2011 * a careful update approach is used to provide consistency
2012 * in the face of updates to multiple buffers. under this
2013 * approach, all required buffers are obtained before making
2014 * any updates and are held all are updates are complete.
Dave Kleikamp63f83c92006-10-02 09:55:27 -05002015 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 * PRE CONDITION: Already have buffer lock on iagp. Already have AG lock on
2017 * this AG. Must have read lock on imap inode.
2018 *
2019 * PARAMETERS:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002020 * imap - pointer to inode map control structure.
2021 * iagp - pointer to iag.
2022 * ino - inode number to be allocated within the iag.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 *
2024 * RETURN VALUES:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002025 * 0 - success.
2026 * -ENOSPC - insufficient disk resources.
2027 * -EIO - i/o error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 */
2029static int diAllocBit(struct inomap * imap, struct iag * iagp, int ino)
2030{
2031 int extno, bitno, agno, sword, rc;
2032 struct metapage *amp = NULL, *bmp = NULL;
2033 struct iag *aiagp = NULL, *biagp = NULL;
2034 u32 mask;
2035
2036 /* check if this is the last free inode within the iag.
2037 * if so, it will have to be removed from the ag free
2038 * inode list, so get the iags preceeding and following
2039 * it on the list.
2040 */
2041 if (iagp->nfreeinos == cpu_to_le32(1)) {
2042 if ((int) le32_to_cpu(iagp->inofreefwd) >= 0) {
2043 if ((rc =
2044 diIAGRead(imap, le32_to_cpu(iagp->inofreefwd),
2045 &amp)))
2046 return (rc);
2047 aiagp = (struct iag *) amp->data;
2048 }
2049
2050 if ((int) le32_to_cpu(iagp->inofreeback) >= 0) {
2051 if ((rc =
2052 diIAGRead(imap,
2053 le32_to_cpu(iagp->inofreeback),
2054 &bmp))) {
2055 if (amp)
2056 release_metapage(amp);
2057 return (rc);
2058 }
2059 biagp = (struct iag *) bmp->data;
2060 }
2061 }
2062
2063 /* get the ag number, extent number, inode number within
2064 * the extent.
2065 */
2066 agno = BLKTOAG(le64_to_cpu(iagp->agstart), JFS_SBI(imap->im_ipimap->i_sb));
2067 extno = ino >> L2INOSPEREXT;
2068 bitno = ino & (INOSPEREXT - 1);
2069
2070 /* compute the mask for setting the map.
2071 */
2072 mask = HIGHORDER >> bitno;
2073
2074 /* the inode should be free and backed.
2075 */
2076 if (((le32_to_cpu(iagp->pmap[extno]) & mask) != 0) ||
2077 ((le32_to_cpu(iagp->wmap[extno]) & mask) != 0) ||
2078 (addressPXD(&iagp->inoext[extno]) == 0)) {
2079 if (amp)
2080 release_metapage(amp);
2081 if (bmp)
2082 release_metapage(bmp);
2083
2084 jfs_error(imap->im_ipimap->i_sb,
2085 "diAllocBit: iag inconsistent");
2086 return -EIO;
2087 }
2088
2089 /* mark the inode as allocated in the working map.
2090 */
2091 iagp->wmap[extno] |= cpu_to_le32(mask);
2092
2093 /* check if all inodes within the extent are now
2094 * allocated. if so, update the free inode summary
2095 * map to reflect this.
2096 */
2097 if (iagp->wmap[extno] == cpu_to_le32(ONES)) {
2098 sword = extno >> L2EXTSPERSUM;
2099 bitno = extno & (EXTSPERSUM - 1);
2100 iagp->inosmap[sword] |= cpu_to_le32(HIGHORDER >> bitno);
2101 }
2102
2103 /* if this was the last free inode in the iag, remove the
2104 * iag from the ag free inode list.
2105 */
2106 if (iagp->nfreeinos == cpu_to_le32(1)) {
2107 if (amp) {
2108 aiagp->inofreeback = iagp->inofreeback;
2109 write_metapage(amp);
2110 }
2111
2112 if (bmp) {
2113 biagp->inofreefwd = iagp->inofreefwd;
2114 write_metapage(bmp);
2115 } else {
2116 imap->im_agctl[agno].inofree =
2117 le32_to_cpu(iagp->inofreefwd);
2118 }
2119 iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
2120 }
2121
2122 /* update the free inode count at the iag, ag, inode
2123 * map levels.
2124 */
Marcin Slusarz89145622008-02-13 15:34:20 -06002125 le32_add_cpu(&iagp->nfreeinos, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 imap->im_agctl[agno].numfree -= 1;
2127 atomic_dec(&imap->im_numfree);
2128
2129 return (0);
2130}
2131
2132
2133/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002134 * NAME: diNewExt(imap,iagp,extno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002136 * FUNCTION: initialize a new extent of inodes for an iag, allocating
2137 * the first inode of the extent for use for the current
2138 * allocation request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 *
2140 * disk resources are allocated for the new extent of inodes
2141 * and the inodes themselves are initialized to reflect their
2142 * existence within the extent (i.e. their inode numbers and
2143 * inode extent addresses are set) and their initial state
2144 * (mode and link count are set to zero).
2145 *
2146 * if the iag is new, it is not yet on an ag extent free list
2147 * but will now be placed on this list.
2148 *
2149 * if the allocation of the new extent causes the iag to
2150 * have no free extent, the iag will be removed from the
2151 * ag extent free list.
2152 *
2153 * if the iag has no free backed inodes, it will be placed
2154 * on the ag free inode list, since the addition of the new
2155 * extent will now cause it to have free inodes.
2156 *
2157 * a careful update approach is used to provide consistency
2158 * (i.e. list consistency) in the face of updates to multiple
2159 * buffers. under this approach, all required buffers are
2160 * obtained before making any updates and are held until all
2161 * updates are complete.
Dave Kleikamp63f83c92006-10-02 09:55:27 -05002162 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 * PRE CONDITION: Already have buffer lock on iagp. Already have AG lock on
2164 * this AG. Must have read lock on imap inode.
2165 *
2166 * PARAMETERS:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002167 * imap - pointer to inode map control structure.
2168 * iagp - pointer to iag.
2169 * extno - extent number.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 *
2171 * RETURN VALUES:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002172 * 0 - success.
2173 * -ENOSPC - insufficient disk resources.
2174 * -EIO - i/o error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 */
2176static int diNewExt(struct inomap * imap, struct iag * iagp, int extno)
2177{
2178 int agno, iagno, fwd, back, freei = 0, sword, rc;
2179 struct iag *aiagp = NULL, *biagp = NULL, *ciagp = NULL;
2180 struct metapage *amp, *bmp, *cmp, *dmp;
2181 struct inode *ipimap;
2182 s64 blkno, hint;
2183 int i, j;
2184 u32 mask;
2185 ino_t ino;
2186 struct dinode *dp;
2187 struct jfs_sb_info *sbi;
2188
2189 /* better have free extents.
2190 */
2191 if (!iagp->nfreeexts) {
2192 jfs_error(imap->im_ipimap->i_sb, "diNewExt: no free extents");
2193 return -EIO;
2194 }
2195
2196 /* get the inode map inode.
2197 */
2198 ipimap = imap->im_ipimap;
2199 sbi = JFS_SBI(ipimap->i_sb);
2200
2201 amp = bmp = cmp = NULL;
2202
2203 /* get the ag and iag numbers for this iag.
2204 */
2205 agno = BLKTOAG(le64_to_cpu(iagp->agstart), sbi);
2206 iagno = le32_to_cpu(iagp->iagnum);
2207
2208 /* check if this is the last free extent within the
2209 * iag. if so, the iag must be removed from the ag
2210 * free extent list, so get the iags preceeding and
2211 * following the iag on this list.
2212 */
2213 if (iagp->nfreeexts == cpu_to_le32(1)) {
2214 if ((fwd = le32_to_cpu(iagp->extfreefwd)) >= 0) {
2215 if ((rc = diIAGRead(imap, fwd, &amp)))
2216 return (rc);
2217 aiagp = (struct iag *) amp->data;
2218 }
2219
2220 if ((back = le32_to_cpu(iagp->extfreeback)) >= 0) {
2221 if ((rc = diIAGRead(imap, back, &bmp)))
2222 goto error_out;
2223 biagp = (struct iag *) bmp->data;
2224 }
2225 } else {
2226 /* the iag has free extents. if all extents are free
2227 * (as is the case for a newly allocated iag), the iag
2228 * must be added to the ag free extent list, so get
2229 * the iag at the head of the list in preparation for
2230 * adding this iag to this list.
2231 */
2232 fwd = back = -1;
2233 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2234 if ((fwd = imap->im_agctl[agno].extfree) >= 0) {
2235 if ((rc = diIAGRead(imap, fwd, &amp)))
2236 goto error_out;
2237 aiagp = (struct iag *) amp->data;
2238 }
2239 }
2240 }
2241
2242 /* check if the iag has no free inodes. if so, the iag
2243 * will have to be added to the ag free inode list, so get
2244 * the iag at the head of the list in preparation for
2245 * adding this iag to this list. in doing this, we must
2246 * check if we already have the iag at the head of
2247 * the list in hand.
2248 */
2249 if (iagp->nfreeinos == 0) {
2250 freei = imap->im_agctl[agno].inofree;
2251
2252 if (freei >= 0) {
2253 if (freei == fwd) {
2254 ciagp = aiagp;
2255 } else if (freei == back) {
2256 ciagp = biagp;
2257 } else {
2258 if ((rc = diIAGRead(imap, freei, &cmp)))
2259 goto error_out;
2260 ciagp = (struct iag *) cmp->data;
2261 }
2262 if (ciagp == NULL) {
2263 jfs_error(imap->im_ipimap->i_sb,
2264 "diNewExt: ciagp == NULL");
2265 rc = -EIO;
2266 goto error_out;
2267 }
2268 }
2269 }
2270
2271 /* allocate disk space for the inode extent.
2272 */
2273 if ((extno == 0) || (addressPXD(&iagp->inoext[extno - 1]) == 0))
2274 hint = ((s64) agno << sbi->bmap->db_agl2size) - 1;
2275 else
2276 hint = addressPXD(&iagp->inoext[extno - 1]) +
2277 lengthPXD(&iagp->inoext[extno - 1]) - 1;
2278
2279 if ((rc = dbAlloc(ipimap, hint, (s64) imap->im_nbperiext, &blkno)))
2280 goto error_out;
2281
2282 /* compute the inode number of the first inode within the
2283 * extent.
2284 */
2285 ino = (iagno << L2INOSPERIAG) + (extno << L2INOSPEREXT);
2286
2287 /* initialize the inodes within the newly allocated extent a
2288 * page at a time.
2289 */
2290 for (i = 0; i < imap->im_nbperiext; i += sbi->nbperpage) {
2291 /* get a buffer for this page of disk inodes.
2292 */
2293 dmp = get_metapage(ipimap, blkno + i, PSIZE, 1);
2294 if (dmp == NULL) {
2295 rc = -EIO;
2296 goto error_out;
2297 }
2298 dp = (struct dinode *) dmp->data;
2299
2300 /* initialize the inode number, mode, link count and
2301 * inode extent address.
2302 */
2303 for (j = 0; j < INOSPERPAGE; j++, dp++, ino++) {
2304 dp->di_inostamp = cpu_to_le32(sbi->inostamp);
2305 dp->di_number = cpu_to_le32(ino);
2306 dp->di_fileset = cpu_to_le32(FILESYSTEM_I);
2307 dp->di_mode = 0;
2308 dp->di_nlink = 0;
2309 PXDaddress(&(dp->di_ixpxd), blkno);
2310 PXDlength(&(dp->di_ixpxd), imap->im_nbperiext);
2311 }
2312 write_metapage(dmp);
2313 }
2314
2315 /* if this is the last free extent within the iag, remove the
2316 * iag from the ag free extent list.
2317 */
2318 if (iagp->nfreeexts == cpu_to_le32(1)) {
2319 if (fwd >= 0)
2320 aiagp->extfreeback = iagp->extfreeback;
2321
2322 if (back >= 0)
2323 biagp->extfreefwd = iagp->extfreefwd;
2324 else
2325 imap->im_agctl[agno].extfree =
2326 le32_to_cpu(iagp->extfreefwd);
2327
2328 iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
2329 } else {
2330 /* if the iag has all free extents (newly allocated iag),
2331 * add the iag to the ag free extent list.
2332 */
2333 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
2334 if (fwd >= 0)
2335 aiagp->extfreeback = cpu_to_le32(iagno);
2336
2337 iagp->extfreefwd = cpu_to_le32(fwd);
2338 iagp->extfreeback = cpu_to_le32(-1);
2339 imap->im_agctl[agno].extfree = iagno;
2340 }
2341 }
2342
2343 /* if the iag has no free inodes, add the iag to the
2344 * ag free inode list.
2345 */
2346 if (iagp->nfreeinos == 0) {
2347 if (freei >= 0)
2348 ciagp->inofreeback = cpu_to_le32(iagno);
2349
2350 iagp->inofreefwd =
2351 cpu_to_le32(imap->im_agctl[agno].inofree);
2352 iagp->inofreeback = cpu_to_le32(-1);
2353 imap->im_agctl[agno].inofree = iagno;
2354 }
2355
2356 /* initialize the extent descriptor of the extent. */
2357 PXDlength(&iagp->inoext[extno], imap->im_nbperiext);
2358 PXDaddress(&iagp->inoext[extno], blkno);
2359
2360 /* initialize the working and persistent map of the extent.
2361 * the working map will be initialized such that
2362 * it indicates the first inode of the extent is allocated.
2363 */
2364 iagp->wmap[extno] = cpu_to_le32(HIGHORDER);
2365 iagp->pmap[extno] = 0;
2366
2367 /* update the free inode and free extent summary maps
2368 * for the extent to indicate the extent has free inodes
2369 * and no longer represents a free extent.
2370 */
2371 sword = extno >> L2EXTSPERSUM;
2372 mask = HIGHORDER >> (extno & (EXTSPERSUM - 1));
2373 iagp->extsmap[sword] |= cpu_to_le32(mask);
2374 iagp->inosmap[sword] &= cpu_to_le32(~mask);
2375
2376 /* update the free inode and free extent counts for the
2377 * iag.
2378 */
Marcin Slusarz89145622008-02-13 15:34:20 -06002379 le32_add_cpu(&iagp->nfreeinos, (INOSPEREXT - 1));
2380 le32_add_cpu(&iagp->nfreeexts, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
2382 /* update the free and backed inode counts for the ag.
2383 */
2384 imap->im_agctl[agno].numfree += (INOSPEREXT - 1);
2385 imap->im_agctl[agno].numinos += INOSPEREXT;
2386
2387 /* update the free and backed inode counts for the inode map.
2388 */
2389 atomic_add(INOSPEREXT - 1, &imap->im_numfree);
2390 atomic_add(INOSPEREXT, &imap->im_numinos);
2391
2392 /* write the iags.
2393 */
2394 if (amp)
2395 write_metapage(amp);
2396 if (bmp)
2397 write_metapage(bmp);
2398 if (cmp)
2399 write_metapage(cmp);
2400
2401 return (0);
2402
2403 error_out:
2404
2405 /* release the iags.
2406 */
2407 if (amp)
2408 release_metapage(amp);
2409 if (bmp)
2410 release_metapage(bmp);
2411 if (cmp)
2412 release_metapage(cmp);
2413
2414 return (rc);
2415}
2416
2417
2418/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002419 * NAME: diNewIAG(imap,iagnop,agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -05002421 * FUNCTION: allocate a new iag for an allocation group.
2422 *
2423 * first tries to allocate the iag from the inode map
2424 * iagfree list:
2425 * if the list has free iags, the head of the list is removed
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 * and returned to satisfy the request.
2427 * if the inode map's iag free list is empty, the inode map
2428 * is extended to hold a new iag. this new iag is initialized
2429 * and returned to satisfy the request.
2430 *
2431 * PARAMETERS:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002432 * imap - pointer to inode map control structure.
2433 * iagnop - pointer to an iag number set with the number of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 * newly allocated iag upon successful return.
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002435 * agno - allocation group number.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 * bpp - Buffer pointer to be filled in with new IAG's buffer
2437 *
2438 * RETURN VALUES:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002439 * 0 - success.
2440 * -ENOSPC - insufficient disk resources.
2441 * -EIO - i/o error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -05002443 * serialization:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 * AG lock held on entry/exit;
2445 * write lock on the map is held inside;
2446 * read lock on the map is held on successful completion;
2447 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -05002448 * note: new iag transaction:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 * . synchronously write iag;
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002450 * . write log of xtree and inode of imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 * . commit;
2452 * . synchronous write of xtree (right to left, bottom to top);
2453 * . at start of logredo(): init in-memory imap with one additional iag page;
2454 * . at end of logredo(): re-read imap inode to determine
2455 * new imap size;
2456 */
2457static int
2458diNewIAG(struct inomap * imap, int *iagnop, int agno, struct metapage ** mpp)
2459{
2460 int rc;
2461 int iagno, i, xlen;
2462 struct inode *ipimap;
2463 struct super_block *sb;
2464 struct jfs_sb_info *sbi;
2465 struct metapage *mp;
2466 struct iag *iagp;
2467 s64 xaddr = 0;
2468 s64 blkno;
2469 tid_t tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 struct inode *iplist[1];
2471
2472 /* pick up pointers to the inode map and mount inodes */
2473 ipimap = imap->im_ipimap;
2474 sb = ipimap->i_sb;
2475 sbi = JFS_SBI(sb);
2476
2477 /* acquire the free iag lock */
2478 IAGFREE_LOCK(imap);
2479
Dave Kleikamp63f83c92006-10-02 09:55:27 -05002480 /* if there are any iags on the inode map free iag list,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 * allocate the iag from the head of the list.
2482 */
2483 if (imap->im_freeiag >= 0) {
2484 /* pick up the iag number at the head of the list */
2485 iagno = imap->im_freeiag;
2486
2487 /* determine the logical block number of the iag */
2488 blkno = IAGTOLBLK(iagno, sbi->l2nbperpage);
2489 } else {
2490 /* no free iags. the inode map will have to be extented
2491 * to include a new iag.
2492 */
2493
2494 /* acquire inode map lock */
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06002495 IWRITE_LOCK(ipimap, RDWRLOCK_IMAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
2497 if (ipimap->i_size >> L2PSIZE != imap->im_nextiag + 1) {
2498 IWRITE_UNLOCK(ipimap);
2499 IAGFREE_UNLOCK(imap);
2500 jfs_error(imap->im_ipimap->i_sb,
2501 "diNewIAG: ipimap->i_size is wrong");
2502 return -EIO;
2503 }
2504
2505
2506 /* get the next avaliable iag number */
2507 iagno = imap->im_nextiag;
2508
2509 /* make sure that we have not exceeded the maximum inode
2510 * number limit.
2511 */
2512 if (iagno > (MAXIAGS - 1)) {
2513 /* release the inode map lock */
2514 IWRITE_UNLOCK(ipimap);
2515
2516 rc = -ENOSPC;
2517 goto out;
2518 }
2519
2520 /*
2521 * synchronously append new iag page.
2522 */
2523 /* determine the logical address of iag page to append */
2524 blkno = IAGTOLBLK(iagno, sbi->l2nbperpage);
2525
2526 /* Allocate extent for new iag page */
2527 xlen = sbi->nbperpage;
2528 if ((rc = dbAlloc(ipimap, 0, (s64) xlen, &xaddr))) {
2529 /* release the inode map lock */
2530 IWRITE_UNLOCK(ipimap);
2531
2532 goto out;
2533 }
2534
Dave Kleikampd2e83702005-05-02 12:24:51 -06002535 /*
2536 * start transaction of update of the inode map
2537 * addressing structure pointing to the new iag page;
2538 */
2539 tid = txBegin(sb, COMMIT_FORCE);
Ingo Molnar1de87442006-01-24 15:22:50 -06002540 mutex_lock(&JFS_IP(ipimap)->commit_mutex);
Dave Kleikampd2e83702005-05-02 12:24:51 -06002541
2542 /* update the inode map addressing structure to point to it */
2543 if ((rc =
2544 xtInsert(tid, ipimap, 0, blkno, xlen, &xaddr, 0))) {
2545 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -06002546 mutex_unlock(&JFS_IP(ipimap)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 /* Free the blocks allocated for the iag since it was
2548 * not successfully added to the inode map
2549 */
2550 dbFree(ipimap, xaddr, (s64) xlen);
2551
2552 /* release the inode map lock */
2553 IWRITE_UNLOCK(ipimap);
2554
Dave Kleikampd2e83702005-05-02 12:24:51 -06002555 goto out;
2556 }
2557
2558 /* update the inode map's inode to reflect the extension */
2559 ipimap->i_size += PSIZE;
2560 inode_add_bytes(ipimap, PSIZE);
2561
2562 /* assign a buffer for the page */
2563 mp = get_metapage(ipimap, blkno, PSIZE, 0);
2564 if (!mp) {
2565 /*
2566 * This is very unlikely since we just created the
2567 * extent, but let's try to handle it correctly
2568 */
2569 xtTruncate(tid, ipimap, ipimap->i_size - PSIZE,
2570 COMMIT_PWMAP);
2571
2572 txAbort(tid, 0);
2573 txEnd(tid);
2574
2575 /* release the inode map lock */
2576 IWRITE_UNLOCK(ipimap);
2577
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 rc = -EIO;
2579 goto out;
2580 }
2581 iagp = (struct iag *) mp->data;
2582
2583 /* init the iag */
2584 memset(iagp, 0, sizeof(struct iag));
2585 iagp->iagnum = cpu_to_le32(iagno);
2586 iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
2587 iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
2588 iagp->iagfree = cpu_to_le32(-1);
2589 iagp->nfreeinos = 0;
2590 iagp->nfreeexts = cpu_to_le32(EXTSPERIAG);
2591
2592 /* initialize the free inode summary map (free extent
2593 * summary map initialization handled by bzero).
2594 */
2595 for (i = 0; i < SMAPSZ; i++)
2596 iagp->inosmap[i] = cpu_to_le32(ONES);
2597
2598 /*
Dave Kleikampd2e83702005-05-02 12:24:51 -06002599 * Write and sync the metapage
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 flush_metapage(mp);
2602
2603 /*
Dave Kleikamp63f83c92006-10-02 09:55:27 -05002604 * txCommit(COMMIT_FORCE) will synchronously write address
2605 * index pages and inode after commit in careful update order
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 * of address index pages (right to left, bottom up);
2607 */
2608 iplist[0] = ipimap;
2609 rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
2610
2611 txEnd(tid);
Ingo Molnar1de87442006-01-24 15:22:50 -06002612 mutex_unlock(&JFS_IP(ipimap)->commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
2614 duplicateIXtree(sb, blkno, xlen, &xaddr);
2615
2616 /* update the next avaliable iag number */
2617 imap->im_nextiag += 1;
2618
2619 /* Add the iag to the iag free list so we don't lose the iag
2620 * if a failure happens now.
2621 */
2622 imap->im_freeiag = iagno;
2623
2624 /* Until we have logredo working, we want the imap inode &
2625 * control page to be up to date.
2626 */
2627 diSync(ipimap);
2628
2629 /* release the inode map lock */
2630 IWRITE_UNLOCK(ipimap);
2631 }
2632
2633 /* obtain read lock on map */
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06002634 IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
2636 /* read the iag */
2637 if ((rc = diIAGRead(imap, iagno, &mp))) {
2638 IREAD_UNLOCK(ipimap);
2639 rc = -EIO;
2640 goto out;
2641 }
2642 iagp = (struct iag *) mp->data;
2643
2644 /* remove the iag from the iag free list */
2645 imap->im_freeiag = le32_to_cpu(iagp->iagfree);
2646 iagp->iagfree = cpu_to_le32(-1);
2647
2648 /* set the return iag number and buffer pointer */
2649 *iagnop = iagno;
2650 *mpp = mp;
2651
2652 out:
2653 /* release the iag free lock */
2654 IAGFREE_UNLOCK(imap);
2655
2656 return (rc);
2657}
2658
2659/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002660 * NAME: diIAGRead()
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002662 * FUNCTION: get the buffer for the specified iag within a fileset
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 * or aggregate inode map.
Dave Kleikamp63f83c92006-10-02 09:55:27 -05002664 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 * PARAMETERS:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002666 * imap - pointer to inode map control structure.
2667 * iagno - iag number.
2668 * bpp - point to buffer pointer to be filled in on successful
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 * exit.
2670 *
2671 * SERIALIZATION:
2672 * must have read lock on imap inode
2673 * (When called by diExtendFS, the filesystem is quiesced, therefore
2674 * the read lock is unnecessary.)
2675 *
2676 * RETURN VALUES:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002677 * 0 - success.
2678 * -EIO - i/o error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 */
2680static int diIAGRead(struct inomap * imap, int iagno, struct metapage ** mpp)
2681{
2682 struct inode *ipimap = imap->im_ipimap;
2683 s64 blkno;
2684
2685 /* compute the logical block number of the iag. */
2686 blkno = IAGTOLBLK(iagno, JFS_SBI(ipimap->i_sb)->l2nbperpage);
2687
2688 /* read the iag. */
2689 *mpp = read_metapage(ipimap, blkno, PSIZE, 0);
2690 if (*mpp == NULL) {
2691 return -EIO;
2692 }
2693
2694 return (0);
2695}
2696
2697/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002698 * NAME: diFindFree()
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002700 * FUNCTION: find the first free bit in a word starting at
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 * the specified bit position.
2702 *
2703 * PARAMETERS:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002704 * word - word to be examined.
2705 * start - starting bit position.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 *
2707 * RETURN VALUES:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002708 * bit position of first free bit in the word or 32 if
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 * no free bits were found.
2710 */
2711static int diFindFree(u32 word, int start)
2712{
2713 int bitno;
2714 assert(start < 32);
2715 /* scan the word for the first free bit. */
2716 for (word <<= start, bitno = start; bitno < 32;
2717 bitno++, word <<= 1) {
2718 if ((word & HIGHORDER) == 0)
2719 break;
2720 }
2721 return (bitno);
2722}
2723
2724/*
2725 * NAME: diUpdatePMap()
Dave Kleikamp63f83c92006-10-02 09:55:27 -05002726 *
2727 * FUNCTION: Update the persistent map in an IAG for the allocation or
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 * freeing of the specified inode.
Dave Kleikamp63f83c92006-10-02 09:55:27 -05002729 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 * PRE CONDITIONS: Working map has already been updated for allocate.
2731 *
2732 * PARAMETERS:
2733 * ipimap - Incore inode map inode
2734 * inum - Number of inode to mark in permanent map
Richard Knutsson4d817152006-09-30 23:27:14 -07002735 * is_free - If 'true' indicates inode should be marked freed, otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 * indicates inode should be marked allocated.
2737 *
Dave Kleikamp63f83c92006-10-02 09:55:27 -05002738 * RETURN VALUES:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 * 0 for success
2740 */
2741int
2742diUpdatePMap(struct inode *ipimap,
Richard Knutsson4d817152006-09-30 23:27:14 -07002743 unsigned long inum, bool is_free, struct tblock * tblk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744{
2745 int rc;
2746 struct iag *iagp;
2747 struct metapage *mp;
2748 int iagno, ino, extno, bitno;
2749 struct inomap *imap;
2750 u32 mask;
2751 struct jfs_log *log;
2752 int lsn, difft, diffp;
Dave Kleikamp7fab4792005-05-02 12:25:02 -06002753 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754
2755 imap = JFS_IP(ipimap)->i_imap;
2756 /* get the iag number containing the inode */
2757 iagno = INOTOIAG(inum);
2758 /* make sure that the iag is contained within the map */
2759 if (iagno >= imap->im_nextiag) {
2760 jfs_error(ipimap->i_sb,
2761 "diUpdatePMap: the iag is outside the map");
2762 return -EIO;
2763 }
2764 /* read the iag */
Dave Kleikamp82d5b9a2007-01-09 14:14:48 -06002765 IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 rc = diIAGRead(imap, iagno, &mp);
2767 IREAD_UNLOCK(ipimap);
2768 if (rc)
2769 return (rc);
Dave Kleikamp7fab4792005-05-02 12:25:02 -06002770 metapage_wait_for_io(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 iagp = (struct iag *) mp->data;
2772 /* get the inode number and extent number of the inode within
2773 * the iag and the inode number within the extent.
2774 */
2775 ino = inum & (INOSPERIAG - 1);
2776 extno = ino >> L2INOSPEREXT;
2777 bitno = ino & (INOSPEREXT - 1);
2778 mask = HIGHORDER >> bitno;
Dave Kleikamp63f83c92006-10-02 09:55:27 -05002779 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 * mark the inode free in persistent map:
2781 */
Richard Knutsson4d817152006-09-30 23:27:14 -07002782 if (is_free) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 /* The inode should have been allocated both in working
2784 * map and in persistent map;
2785 * the inode will be freed from working map at the release
2786 * of last reference release;
2787 */
2788 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
Dave Kleikamp63f83c92006-10-02 09:55:27 -05002789 jfs_error(ipimap->i_sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 "diUpdatePMap: inode %ld not marked as "
2791 "allocated in wmap!", inum);
2792 }
2793 if (!(le32_to_cpu(iagp->pmap[extno]) & mask)) {
2794 jfs_error(ipimap->i_sb,
2795 "diUpdatePMap: inode %ld not marked as "
2796 "allocated in pmap!", inum);
2797 }
2798 /* update the bitmap for the extent of the freed inode */
2799 iagp->pmap[extno] &= cpu_to_le32(~mask);
2800 }
2801 /*
2802 * mark the inode allocated in persistent map:
2803 */
2804 else {
2805 /* The inode should be already allocated in the working map
2806 * and should be free in persistent map;
2807 */
2808 if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
2809 release_metapage(mp);
2810 jfs_error(ipimap->i_sb,
2811 "diUpdatePMap: the inode is not allocated in "
2812 "the working map");
2813 return -EIO;
2814 }
2815 if ((le32_to_cpu(iagp->pmap[extno]) & mask) != 0) {
2816 release_metapage(mp);
2817 jfs_error(ipimap->i_sb,
2818 "diUpdatePMap: the inode is not free in the "
2819 "persistent map");
2820 return -EIO;
2821 }
2822 /* update the bitmap for the extent of the allocated inode */
2823 iagp->pmap[extno] |= cpu_to_le32(mask);
2824 }
2825 /*
2826 * update iag lsn
2827 */
2828 lsn = tblk->lsn;
2829 log = JFS_SBI(tblk->sb)->log;
Dave Kleikampbe0bf7d2006-03-08 10:59:15 -06002830 LOGSYNC_LOCK(log, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 if (mp->lsn != 0) {
2832 /* inherit older/smaller lsn */
2833 logdiff(difft, lsn, log);
2834 logdiff(diffp, mp->lsn, log);
2835 if (difft < diffp) {
2836 mp->lsn = lsn;
2837 /* move mp after tblock in logsync list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 list_move(&mp->synclist, &tblk->synclist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 }
2840 /* inherit younger/larger clsn */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 assert(mp->clsn);
2842 logdiff(difft, tblk->clsn, log);
2843 logdiff(diffp, mp->clsn, log);
2844 if (difft > diffp)
2845 mp->clsn = tblk->clsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 } else {
2847 mp->log = log;
2848 mp->lsn = lsn;
2849 /* insert mp after tblock in logsync list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 log->count++;
2851 list_add(&mp->synclist, &tblk->synclist);
2852 mp->clsn = tblk->clsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 }
Dave Kleikampbe0bf7d2006-03-08 10:59:15 -06002854 LOGSYNC_UNLOCK(log, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 write_metapage(mp);
2856 return (0);
2857}
2858
2859/*
2860 * diExtendFS()
2861 *
2862 * function: update imap for extendfs();
Dave Kleikamp63f83c92006-10-02 09:55:27 -05002863 *
2864 * note: AG size has been increased s.t. each k old contiguous AGs are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 * coalesced into a new AG;
2866 */
2867int diExtendFS(struct inode *ipimap, struct inode *ipbmap)
2868{
2869 int rc, rcx = 0;
2870 struct inomap *imap = JFS_IP(ipimap)->i_imap;
2871 struct iag *iagp = NULL, *hiagp = NULL;
2872 struct bmap *mp = JFS_SBI(ipbmap->i_sb)->bmap;
2873 struct metapage *bp, *hbp;
2874 int i, n, head;
2875 int numinos, xnuminos = 0, xnumfree = 0;
2876 s64 agstart;
2877
2878 jfs_info("diExtendFS: nextiag:%d numinos:%d numfree:%d",
2879 imap->im_nextiag, atomic_read(&imap->im_numinos),
2880 atomic_read(&imap->im_numfree));
2881
2882 /*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002883 * reconstruct imap
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 *
2885 * coalesce contiguous k (newAGSize/oldAGSize) AGs;
2886 * i.e., (AGi, ..., AGj) where i = k*n and j = k*(n+1) - 1 to AGn;
2887 * note: new AG size = old AG size * (2**x).
2888 */
2889
2890 /* init per AG control information im_agctl[] */
2891 for (i = 0; i < MAXAG; i++) {
2892 imap->im_agctl[i].inofree = -1;
2893 imap->im_agctl[i].extfree = -1;
2894 imap->im_agctl[i].numinos = 0; /* number of backed inodes */
2895 imap->im_agctl[i].numfree = 0; /* number of free backed inodes */
2896 }
2897
2898 /*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002899 * process each iag page of the map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 *
2901 * rebuild AG Free Inode List, AG Free Inode Extent List;
2902 */
2903 for (i = 0; i < imap->im_nextiag; i++) {
2904 if ((rc = diIAGRead(imap, i, &bp))) {
2905 rcx = rc;
2906 continue;
2907 }
2908 iagp = (struct iag *) bp->data;
2909 if (le32_to_cpu(iagp->iagnum) != i) {
2910 release_metapage(bp);
2911 jfs_error(ipimap->i_sb,
2912 "diExtendFs: unexpected value of iagnum");
2913 return -EIO;
2914 }
2915
2916 /* leave free iag in the free iag list */
Dave Kleikamp63f83c92006-10-02 09:55:27 -05002917 if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
Dave Kleikampf720e3b2007-06-06 15:28:35 -05002918 release_metapage(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 continue;
2920 }
2921
2922 /* agstart that computes to the same ag is treated as same; */
2923 agstart = le64_to_cpu(iagp->agstart);
2924 /* iagp->agstart = agstart & ~(mp->db_agsize - 1); */
2925 n = agstart >> mp->db_agl2size;
2926
2927 /* compute backed inodes */
2928 numinos = (EXTSPERIAG - le32_to_cpu(iagp->nfreeexts))
2929 << L2INOSPEREXT;
2930 if (numinos > 0) {
2931 /* merge AG backed inodes */
2932 imap->im_agctl[n].numinos += numinos;
2933 xnuminos += numinos;
2934 }
2935
2936 /* if any backed free inodes, insert at AG free inode list */
2937 if ((int) le32_to_cpu(iagp->nfreeinos) > 0) {
2938 if ((head = imap->im_agctl[n].inofree) == -1) {
2939 iagp->inofreefwd = cpu_to_le32(-1);
2940 iagp->inofreeback = cpu_to_le32(-1);
2941 } else {
2942 if ((rc = diIAGRead(imap, head, &hbp))) {
2943 rcx = rc;
2944 goto nextiag;
2945 }
2946 hiagp = (struct iag *) hbp->data;
2947 hiagp->inofreeback = iagp->iagnum;
2948 iagp->inofreefwd = cpu_to_le32(head);
2949 iagp->inofreeback = cpu_to_le32(-1);
2950 write_metapage(hbp);
2951 }
2952
2953 imap->im_agctl[n].inofree =
2954 le32_to_cpu(iagp->iagnum);
2955
2956 /* merge AG backed free inodes */
2957 imap->im_agctl[n].numfree +=
2958 le32_to_cpu(iagp->nfreeinos);
2959 xnumfree += le32_to_cpu(iagp->nfreeinos);
2960 }
2961
2962 /* if any free extents, insert at AG free extent list */
2963 if (le32_to_cpu(iagp->nfreeexts) > 0) {
2964 if ((head = imap->im_agctl[n].extfree) == -1) {
2965 iagp->extfreefwd = cpu_to_le32(-1);
2966 iagp->extfreeback = cpu_to_le32(-1);
2967 } else {
2968 if ((rc = diIAGRead(imap, head, &hbp))) {
2969 rcx = rc;
2970 goto nextiag;
2971 }
2972 hiagp = (struct iag *) hbp->data;
2973 hiagp->extfreeback = iagp->iagnum;
2974 iagp->extfreefwd = cpu_to_le32(head);
2975 iagp->extfreeback = cpu_to_le32(-1);
2976 write_metapage(hbp);
2977 }
2978
2979 imap->im_agctl[n].extfree =
2980 le32_to_cpu(iagp->iagnum);
2981 }
2982
2983 nextiag:
2984 write_metapage(bp);
2985 }
2986
2987 if (xnuminos != atomic_read(&imap->im_numinos) ||
2988 xnumfree != atomic_read(&imap->im_numfree)) {
2989 jfs_error(ipimap->i_sb,
2990 "diExtendFs: numinos or numfree incorrect");
2991 return -EIO;
2992 }
2993
2994 return rcx;
2995}
2996
2997
2998/*
2999 * duplicateIXtree()
3000 *
3001 * serialization: IWRITE_LOCK held on entry/exit
3002 *
3003 * note: shadow page with regular inode (rel.2);
3004 */
3005static void duplicateIXtree(struct super_block *sb, s64 blkno,
3006 int xlen, s64 *xaddr)
3007{
3008 struct jfs_superblock *j_sb;
3009 struct buffer_head *bh;
3010 struct inode *ip;
3011 tid_t tid;
3012
3013 /* if AIT2 ipmap2 is bad, do not try to update it */
3014 if (JFS_SBI(sb)->mntflag & JFS_BAD_SAIT) /* s_flag */
3015 return;
3016 ip = diReadSpecial(sb, FILESYSTEM_I, 1);
3017 if (ip == NULL) {
3018 JFS_SBI(sb)->mntflag |= JFS_BAD_SAIT;
3019 if (readSuper(sb, &bh))
3020 return;
3021 j_sb = (struct jfs_superblock *)bh->b_data;
3022 j_sb->s_flag |= cpu_to_le32(JFS_BAD_SAIT);
3023
3024 mark_buffer_dirty(bh);
3025 sync_dirty_buffer(bh);
3026 brelse(bh);
3027 return;
3028 }
3029
3030 /* start transaction */
3031 tid = txBegin(sb, COMMIT_FORCE);
3032 /* update the inode map addressing structure to point to it */
3033 if (xtInsert(tid, ip, 0, blkno, xlen, xaddr, 0)) {
3034 JFS_SBI(sb)->mntflag |= JFS_BAD_SAIT;
3035 txAbort(tid, 1);
3036 goto cleanup;
3037
3038 }
3039 /* update the inode map's inode to reflect the extension */
3040 ip->i_size += PSIZE;
3041 inode_add_bytes(ip, PSIZE);
3042 txCommit(tid, 1, &ip, COMMIT_FORCE);
3043 cleanup:
3044 txEnd(tid);
3045 diFreeSpecial(ip);
3046}
3047
3048/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05003049 * NAME: copy_from_dinode()
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -05003051 * FUNCTION: Copies inode info from disk inode to in-memory inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 *
3053 * RETURN VALUES:
Dave Kleikampf720e3b2007-06-06 15:28:35 -05003054 * 0 - success
3055 * -ENOMEM - insufficient memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 */
3057static int copy_from_dinode(struct dinode * dip, struct inode *ip)
3058{
3059 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
Dave Kleikamp69eb66d2006-03-09 13:59:30 -06003060 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061
3062 jfs_ip->fileset = le32_to_cpu(dip->di_fileset);
3063 jfs_ip->mode2 = le32_to_cpu(dip->di_mode);
Dave Kleikamp3e2221c2007-04-25 09:36:20 -05003064 jfs_set_inode_flags(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
3066 ip->i_mode = le32_to_cpu(dip->di_mode) & 0xffff;
Dave Kleikamp69eb66d2006-03-09 13:59:30 -06003067 if (sbi->umask != -1) {
3068 ip->i_mode = (ip->i_mode & ~0777) | (0777 & ~sbi->umask);
3069 /* For directories, add x permission if r is allowed by umask */
3070 if (S_ISDIR(ip->i_mode)) {
3071 if (ip->i_mode & 0400)
3072 ip->i_mode |= 0100;
3073 if (ip->i_mode & 0040)
3074 ip->i_mode |= 0010;
3075 if (ip->i_mode & 0004)
3076 ip->i_mode |= 0001;
3077 }
3078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 ip->i_nlink = le32_to_cpu(dip->di_nlink);
Dave Kleikamp69eb66d2006-03-09 13:59:30 -06003080
3081 jfs_ip->saved_uid = le32_to_cpu(dip->di_uid);
3082 if (sbi->uid == -1)
3083 ip->i_uid = jfs_ip->saved_uid;
3084 else {
3085 ip->i_uid = sbi->uid;
3086 }
3087
3088 jfs_ip->saved_gid = le32_to_cpu(dip->di_gid);
3089 if (sbi->gid == -1)
3090 ip->i_gid = jfs_ip->saved_gid;
3091 else {
3092 ip->i_gid = sbi->gid;
3093 }
3094
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 ip->i_size = le64_to_cpu(dip->di_size);
3096 ip->i_atime.tv_sec = le32_to_cpu(dip->di_atime.tv_sec);
3097 ip->i_atime.tv_nsec = le32_to_cpu(dip->di_atime.tv_nsec);
3098 ip->i_mtime.tv_sec = le32_to_cpu(dip->di_mtime.tv_sec);
3099 ip->i_mtime.tv_nsec = le32_to_cpu(dip->di_mtime.tv_nsec);
3100 ip->i_ctime.tv_sec = le32_to_cpu(dip->di_ctime.tv_sec);
3101 ip->i_ctime.tv_nsec = le32_to_cpu(dip->di_ctime.tv_nsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 ip->i_blocks = LBLK2PBLK(ip->i_sb, le64_to_cpu(dip->di_nblocks));
3103 ip->i_generation = le32_to_cpu(dip->di_gen);
3104
3105 jfs_ip->ixpxd = dip->di_ixpxd; /* in-memory pxd's are little-endian */
3106 jfs_ip->acl = dip->di_acl; /* as are dxd's */
3107 jfs_ip->ea = dip->di_ea;
3108 jfs_ip->next_index = le32_to_cpu(dip->di_next_index);
3109 jfs_ip->otime = le32_to_cpu(dip->di_otime.tv_sec);
3110 jfs_ip->acltype = le32_to_cpu(dip->di_acltype);
3111
3112 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode)) {
3113 jfs_ip->dev = le32_to_cpu(dip->di_rdev);
3114 ip->i_rdev = new_decode_dev(jfs_ip->dev);
3115 }
3116
3117 if (S_ISDIR(ip->i_mode)) {
3118 memcpy(&jfs_ip->i_dirtable, &dip->di_dirtable, 384);
3119 } else if (S_ISREG(ip->i_mode) || S_ISLNK(ip->i_mode)) {
3120 memcpy(&jfs_ip->i_xtroot, &dip->di_xtroot, 288);
3121 } else
3122 memcpy(&jfs_ip->i_inline_ea, &dip->di_inlineea, 128);
3123
3124 /* Zero the in-memory-only stuff */
3125 jfs_ip->cflag = 0;
3126 jfs_ip->btindex = 0;
3127 jfs_ip->btorder = 0;
3128 jfs_ip->bxflag = 0;
3129 jfs_ip->blid = 0;
3130 jfs_ip->atlhead = 0;
3131 jfs_ip->atltail = 0;
3132 jfs_ip->xtlid = 0;
3133 return (0);
3134}
3135
3136/*
Dave Kleikampf720e3b2007-06-06 15:28:35 -05003137 * NAME: copy_to_dinode()
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 *
Dave Kleikampf720e3b2007-06-06 15:28:35 -05003139 * FUNCTION: Copies inode info from in-memory inode to disk inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 */
3141static void copy_to_dinode(struct dinode * dip, struct inode *ip)
3142{
3143 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
Dave Kleikamp69eb66d2006-03-09 13:59:30 -06003144 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145
3146 dip->di_fileset = cpu_to_le32(jfs_ip->fileset);
Dave Kleikamp69eb66d2006-03-09 13:59:30 -06003147 dip->di_inostamp = cpu_to_le32(sbi->inostamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 dip->di_number = cpu_to_le32(ip->i_ino);
3149 dip->di_gen = cpu_to_le32(ip->i_generation);
3150 dip->di_size = cpu_to_le64(ip->i_size);
3151 dip->di_nblocks = cpu_to_le64(PBLK2LBLK(ip->i_sb, ip->i_blocks));
3152 dip->di_nlink = cpu_to_le32(ip->i_nlink);
Dave Kleikamp69eb66d2006-03-09 13:59:30 -06003153 if (sbi->uid == -1)
3154 dip->di_uid = cpu_to_le32(ip->i_uid);
3155 else
3156 dip->di_uid = cpu_to_le32(jfs_ip->saved_uid);
3157 if (sbi->gid == -1)
3158 dip->di_gid = cpu_to_le32(ip->i_gid);
3159 else
3160 dip->di_gid = cpu_to_le32(jfs_ip->saved_gid);
Dave Kleikamp3e2221c2007-04-25 09:36:20 -05003161 jfs_get_inode_flags(jfs_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 /*
3163 * mode2 is only needed for storing the higher order bits.
3164 * Trust i_mode for the lower order ones
3165 */
Dave Kleikamp69eb66d2006-03-09 13:59:30 -06003166 if (sbi->umask == -1)
3167 dip->di_mode = cpu_to_le32((jfs_ip->mode2 & 0xffff0000) |
3168 ip->i_mode);
3169 else /* Leave the original permissions alone */
3170 dip->di_mode = cpu_to_le32(jfs_ip->mode2);
3171
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 dip->di_atime.tv_sec = cpu_to_le32(ip->i_atime.tv_sec);
3173 dip->di_atime.tv_nsec = cpu_to_le32(ip->i_atime.tv_nsec);
3174 dip->di_ctime.tv_sec = cpu_to_le32(ip->i_ctime.tv_sec);
3175 dip->di_ctime.tv_nsec = cpu_to_le32(ip->i_ctime.tv_nsec);
3176 dip->di_mtime.tv_sec = cpu_to_le32(ip->i_mtime.tv_sec);
3177 dip->di_mtime.tv_nsec = cpu_to_le32(ip->i_mtime.tv_nsec);
3178 dip->di_ixpxd = jfs_ip->ixpxd; /* in-memory pxd's are little-endian */
3179 dip->di_acl = jfs_ip->acl; /* as are dxd's */
3180 dip->di_ea = jfs_ip->ea;
3181 dip->di_next_index = cpu_to_le32(jfs_ip->next_index);
3182 dip->di_otime.tv_sec = cpu_to_le32(jfs_ip->otime);
3183 dip->di_otime.tv_nsec = 0;
3184 dip->di_acltype = cpu_to_le32(jfs_ip->acltype);
3185 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode))
3186 dip->di_rdev = cpu_to_le32(jfs_ip->dev);
3187}