/kern_oII/fs/jfs/jfs_imap.c

http://omnia2droid.googlecode.com/ · C · 3188 lines · 1602 code · 377 blank · 1209 comment · 350 complexity · 702b0d3fa15acb1cf47ec717e9212f48 MD5 · raw file

Large files are truncated click here to view the full file

  1. /*
  2. * Copyright (C) International Business Machines Corp., 2000-2004
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  12. * the GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. /*
  19. * jfs_imap.c: inode allocation map manager
  20. *
  21. * Serialization:
  22. * Each AG has a simple lock which is used to control the serialization of
  23. * the AG level lists. This lock should be taken first whenever an AG
  24. * level list will be modified or accessed.
  25. *
  26. * Each IAG is locked by obtaining the buffer for the IAG page.
  27. *
  28. * There is also a inode lock for the inode map inode. A read lock needs to
  29. * be taken whenever an IAG is read from the map or the global level
  30. * information is read. A write lock needs to be taken whenever the global
  31. * level information is modified or an atomic operation needs to be used.
  32. *
  33. * If more than one IAG is read at one time, the read lock may not
  34. * be given up until all of the IAG's are read. Otherwise, a deadlock
  35. * may occur when trying to obtain the read lock while another thread
  36. * holding the read lock is waiting on the IAG already being held.
  37. *
  38. * The control page of the inode map is read into memory by diMount().
  39. * Thereafter it should only be modified in memory and then it will be
  40. * written out when the filesystem is unmounted by diUnmount().
  41. */
  42. #include <linux/fs.h>
  43. #include <linux/buffer_head.h>
  44. #include <linux/pagemap.h>
  45. #include <linux/quotaops.h>
  46. #include "jfs_incore.h"
  47. #include "jfs_inode.h"
  48. #include "jfs_filsys.h"
  49. #include "jfs_dinode.h"
  50. #include "jfs_dmap.h"
  51. #include "jfs_imap.h"
  52. #include "jfs_metapage.h"
  53. #include "jfs_superblock.h"
  54. #include "jfs_debug.h"
  55. /*
  56. * imap locks
  57. */
  58. /* iag free list lock */
  59. #define IAGFREE_LOCK_INIT(imap) mutex_init(&imap->im_freelock)
  60. #define IAGFREE_LOCK(imap) mutex_lock(&imap->im_freelock)
  61. #define IAGFREE_UNLOCK(imap) mutex_unlock(&imap->im_freelock)
  62. /* per ag iag list locks */
  63. #define AG_LOCK_INIT(imap,index) mutex_init(&(imap->im_aglock[index]))
  64. #define AG_LOCK(imap,agno) mutex_lock(&imap->im_aglock[agno])
  65. #define AG_UNLOCK(imap,agno) mutex_unlock(&imap->im_aglock[agno])
  66. /*
  67. * forward references
  68. */
  69. static int diAllocAG(struct inomap *, int, bool, struct inode *);
  70. static int diAllocAny(struct inomap *, int, bool, struct inode *);
  71. static int diAllocBit(struct inomap *, struct iag *, int);
  72. static int diAllocExt(struct inomap *, int, struct inode *);
  73. static int diAllocIno(struct inomap *, int, struct inode *);
  74. static int diFindFree(u32, int);
  75. static int diNewExt(struct inomap *, struct iag *, int);
  76. static int diNewIAG(struct inomap *, int *, int, struct metapage **);
  77. static void duplicateIXtree(struct super_block *, s64, int, s64 *);
  78. static int diIAGRead(struct inomap * imap, int, struct metapage **);
  79. static int copy_from_dinode(struct dinode *, struct inode *);
  80. static void copy_to_dinode(struct dinode *, struct inode *);
  81. /*
  82. * NAME: diMount()
  83. *
  84. * FUNCTION: initialize the incore inode map control structures for
  85. * a fileset or aggregate init time.
  86. *
  87. * the inode map's control structure (dinomap) is
  88. * brought in from disk and placed in virtual memory.
  89. *
  90. * PARAMETERS:
  91. * ipimap - pointer to inode map inode for the aggregate or fileset.
  92. *
  93. * RETURN VALUES:
  94. * 0 - success
  95. * -ENOMEM - insufficient free virtual memory.
  96. * -EIO - i/o error.
  97. */
  98. int diMount(struct inode *ipimap)
  99. {
  100. struct inomap *imap;
  101. struct metapage *mp;
  102. int index;
  103. struct dinomap_disk *dinom_le;
  104. /*
  105. * allocate/initialize the in-memory inode map control structure
  106. */
  107. /* allocate the in-memory inode map control structure. */
  108. imap = kmalloc(sizeof(struct inomap), GFP_KERNEL);
  109. if (imap == NULL) {
  110. jfs_err("diMount: kmalloc returned NULL!");
  111. return -ENOMEM;
  112. }
  113. /* read the on-disk inode map control structure. */
  114. mp = read_metapage(ipimap,
  115. IMAPBLKNO << JFS_SBI(ipimap->i_sb)->l2nbperpage,
  116. PSIZE, 0);
  117. if (mp == NULL) {
  118. kfree(imap);
  119. return -EIO;
  120. }
  121. /* copy the on-disk version to the in-memory version. */
  122. dinom_le = (struct dinomap_disk *) mp->data;
  123. imap->im_freeiag = le32_to_cpu(dinom_le->in_freeiag);
  124. imap->im_nextiag = le32_to_cpu(dinom_le->in_nextiag);
  125. atomic_set(&imap->im_numinos, le32_to_cpu(dinom_le->in_numinos));
  126. atomic_set(&imap->im_numfree, le32_to_cpu(dinom_le->in_numfree));
  127. imap->im_nbperiext = le32_to_cpu(dinom_le->in_nbperiext);
  128. imap->im_l2nbperiext = le32_to_cpu(dinom_le->in_l2nbperiext);
  129. for (index = 0; index < MAXAG; index++) {
  130. imap->im_agctl[index].inofree =
  131. le32_to_cpu(dinom_le->in_agctl[index].inofree);
  132. imap->im_agctl[index].extfree =
  133. le32_to_cpu(dinom_le->in_agctl[index].extfree);
  134. imap->im_agctl[index].numinos =
  135. le32_to_cpu(dinom_le->in_agctl[index].numinos);
  136. imap->im_agctl[index].numfree =
  137. le32_to_cpu(dinom_le->in_agctl[index].numfree);
  138. }
  139. /* release the buffer. */
  140. release_metapage(mp);
  141. /*
  142. * allocate/initialize inode allocation map locks
  143. */
  144. /* allocate and init iag free list lock */
  145. IAGFREE_LOCK_INIT(imap);
  146. /* allocate and init ag list locks */
  147. for (index = 0; index < MAXAG; index++) {
  148. AG_LOCK_INIT(imap, index);
  149. }
  150. /* bind the inode map inode and inode map control structure
  151. * to each other.
  152. */
  153. imap->im_ipimap = ipimap;
  154. JFS_IP(ipimap)->i_imap = imap;
  155. return (0);
  156. }
  157. /*
  158. * NAME: diUnmount()
  159. *
  160. * FUNCTION: write to disk the incore inode map control structures for
  161. * a fileset or aggregate at unmount time.
  162. *
  163. * PARAMETERS:
  164. * ipimap - pointer to inode map inode for the aggregate or fileset.
  165. *
  166. * RETURN VALUES:
  167. * 0 - success
  168. * -ENOMEM - insufficient free virtual memory.
  169. * -EIO - i/o error.
  170. */
  171. int diUnmount(struct inode *ipimap, int mounterror)
  172. {
  173. struct inomap *imap = JFS_IP(ipimap)->i_imap;
  174. /*
  175. * update the on-disk inode map control structure
  176. */
  177. if (!(mounterror || isReadOnly(ipimap)))
  178. diSync(ipimap);
  179. /*
  180. * Invalidate the page cache buffers
  181. */
  182. truncate_inode_pages(ipimap->i_mapping, 0);
  183. /*
  184. * free in-memory control structure
  185. */
  186. kfree(imap);
  187. return (0);
  188. }
  189. /*
  190. * diSync()
  191. */
  192. int diSync(struct inode *ipimap)
  193. {
  194. struct dinomap_disk *dinom_le;
  195. struct inomap *imp = JFS_IP(ipimap)->i_imap;
  196. struct metapage *mp;
  197. int index;
  198. /*
  199. * write imap global conrol page
  200. */
  201. /* read the on-disk inode map control structure */
  202. mp = get_metapage(ipimap,
  203. IMAPBLKNO << JFS_SBI(ipimap->i_sb)->l2nbperpage,
  204. PSIZE, 0);
  205. if (mp == NULL) {
  206. jfs_err("diSync: get_metapage failed!");
  207. return -EIO;
  208. }
  209. /* copy the in-memory version to the on-disk version */
  210. dinom_le = (struct dinomap_disk *) mp->data;
  211. dinom_le->in_freeiag = cpu_to_le32(imp->im_freeiag);
  212. dinom_le->in_nextiag = cpu_to_le32(imp->im_nextiag);
  213. dinom_le->in_numinos = cpu_to_le32(atomic_read(&imp->im_numinos));
  214. dinom_le->in_numfree = cpu_to_le32(atomic_read(&imp->im_numfree));
  215. dinom_le->in_nbperiext = cpu_to_le32(imp->im_nbperiext);
  216. dinom_le->in_l2nbperiext = cpu_to_le32(imp->im_l2nbperiext);
  217. for (index = 0; index < MAXAG; index++) {
  218. dinom_le->in_agctl[index].inofree =
  219. cpu_to_le32(imp->im_agctl[index].inofree);
  220. dinom_le->in_agctl[index].extfree =
  221. cpu_to_le32(imp->im_agctl[index].extfree);
  222. dinom_le->in_agctl[index].numinos =
  223. cpu_to_le32(imp->im_agctl[index].numinos);
  224. dinom_le->in_agctl[index].numfree =
  225. cpu_to_le32(imp->im_agctl[index].numfree);
  226. }
  227. /* write out the control structure */
  228. write_metapage(mp);
  229. /*
  230. * write out dirty pages of imap
  231. */
  232. filemap_write_and_wait(ipimap->i_mapping);
  233. diWriteSpecial(ipimap, 0);
  234. return (0);
  235. }
  236. /*
  237. * NAME: diRead()
  238. *
  239. * FUNCTION: initialize an incore inode from disk.
  240. *
  241. * on entry, the specifed incore inode should itself
  242. * specify the disk inode number corresponding to the
  243. * incore inode (i.e. i_number should be initialized).
  244. *
  245. * this routine handles incore inode initialization for
  246. * both "special" and "regular" inodes. special inodes
  247. * are those required early in the mount process and
  248. * require special handling since much of the file system
  249. * is not yet initialized. these "special" inodes are
  250. * identified by a NULL inode map inode pointer and are
  251. * actually initialized by a call to diReadSpecial().
  252. *
  253. * for regular inodes, the iag describing the disk inode
  254. * is read from disk to determine the inode extent address
  255. * for the disk inode. with the inode extent address in
  256. * hand, the page of the extent that contains the disk
  257. * inode is read and the disk inode is copied to the
  258. * incore inode.
  259. *
  260. * PARAMETERS:
  261. * ip - pointer to incore inode to be initialized from disk.
  262. *
  263. * RETURN VALUES:
  264. * 0 - success
  265. * -EIO - i/o error.
  266. * -ENOMEM - insufficient memory
  267. *
  268. */
  269. int diRead(struct inode *ip)
  270. {
  271. struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
  272. int iagno, ino, extno, rc;
  273. struct inode *ipimap;
  274. struct dinode *dp;
  275. struct iag *iagp;
  276. struct metapage *mp;
  277. s64 blkno, agstart;
  278. struct inomap *imap;
  279. int block_offset;
  280. int inodes_left;
  281. unsigned long pageno;
  282. int rel_inode;
  283. jfs_info("diRead: ino = %ld", ip->i_ino);
  284. ipimap = sbi->ipimap;
  285. JFS_IP(ip)->ipimap = ipimap;
  286. /* determine the iag number for this inode (number) */
  287. iagno = INOTOIAG(ip->i_ino);
  288. /* read the iag */
  289. imap = JFS_IP(ipimap)->i_imap;
  290. IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
  291. rc = diIAGRead(imap, iagno, &mp);
  292. IREAD_UNLOCK(ipimap);
  293. if (rc) {
  294. jfs_err("diRead: diIAGRead returned %d", rc);
  295. return (rc);
  296. }
  297. iagp = (struct iag *) mp->data;
  298. /* determine inode extent that holds the disk inode */
  299. ino = ip->i_ino & (INOSPERIAG - 1);
  300. extno = ino >> L2INOSPEREXT;
  301. if ((lengthPXD(&iagp->inoext[extno]) != imap->im_nbperiext) ||
  302. (addressPXD(&iagp->inoext[extno]) == 0)) {
  303. release_metapage(mp);
  304. return -ESTALE;
  305. }
  306. /* get disk block number of the page within the inode extent
  307. * that holds the disk inode.
  308. */
  309. blkno = INOPBLK(&iagp->inoext[extno], ino, sbi->l2nbperpage);
  310. /* get the ag for the iag */
  311. agstart = le64_to_cpu(iagp->agstart);
  312. release_metapage(mp);
  313. rel_inode = (ino & (INOSPERPAGE - 1));
  314. pageno = blkno >> sbi->l2nbperpage;
  315. if ((block_offset = ((u32) blkno & (sbi->nbperpage - 1)))) {
  316. /*
  317. * OS/2 didn't always align inode extents on page boundaries
  318. */
  319. inodes_left =
  320. (sbi->nbperpage - block_offset) << sbi->l2niperblk;
  321. if (rel_inode < inodes_left)
  322. rel_inode += block_offset << sbi->l2niperblk;
  323. else {
  324. pageno += 1;
  325. rel_inode -= inodes_left;
  326. }
  327. }
  328. /* read the page of disk inode */
  329. mp = read_metapage(ipimap, pageno << sbi->l2nbperpage, PSIZE, 1);
  330. if (!mp) {
  331. jfs_err("diRead: read_metapage failed");
  332. return -EIO;
  333. }
  334. /* locate the disk inode requested */
  335. dp = (struct dinode *) mp->data;
  336. dp += rel_inode;
  337. if (ip->i_ino != le32_to_cpu(dp->di_number)) {
  338. jfs_error(ip->i_sb, "diRead: i_ino != di_number");
  339. rc = -EIO;
  340. } else if (le32_to_cpu(dp->di_nlink) == 0)
  341. rc = -ESTALE;
  342. else
  343. /* copy the disk inode to the in-memory inode */
  344. rc = copy_from_dinode(dp, ip);
  345. release_metapage(mp);
  346. /* set the ag for the inode */
  347. JFS_IP(ip)->agno = BLKTOAG(agstart, sbi);
  348. JFS_IP(ip)->active_ag = -1;
  349. return (rc);
  350. }
  351. /*
  352. * NAME: diReadSpecial()
  353. *
  354. * FUNCTION: initialize a 'special' inode from disk.
  355. *
  356. * this routines handles aggregate level inodes. The
  357. * inode cache cannot differentiate between the
  358. * aggregate inodes and the filesystem inodes, so we
  359. * handle these here. We don't actually use the aggregate
  360. * inode map, since these inodes are at a fixed location
  361. * and in some cases the aggregate inode map isn't initialized
  362. * yet.
  363. *
  364. * PARAMETERS:
  365. * sb - filesystem superblock
  366. * inum - aggregate inode number
  367. * secondary - 1 if secondary aggregate inode table
  368. *
  369. * RETURN VALUES:
  370. * new inode - success
  371. * NULL - i/o error.
  372. */
  373. struct inode *diReadSpecial(struct super_block *sb, ino_t inum, int secondary)
  374. {
  375. struct jfs_sb_info *sbi = JFS_SBI(sb);
  376. uint address;
  377. struct dinode *dp;
  378. struct inode *ip;
  379. struct metapage *mp;
  380. ip = new_inode(sb);
  381. if (ip == NULL) {
  382. jfs_err("diReadSpecial: new_inode returned NULL!");
  383. return ip;
  384. }
  385. if (secondary) {
  386. address = addressPXD(&sbi->ait2) >> sbi->l2nbperpage;
  387. JFS_IP(ip)->ipimap = sbi->ipaimap2;
  388. } else {
  389. address = AITBL_OFF >> L2PSIZE;
  390. JFS_IP(ip)->ipimap = sbi->ipaimap;
  391. }
  392. ASSERT(inum < INOSPEREXT);
  393. ip->i_ino = inum;
  394. address += inum >> 3; /* 8 inodes per 4K page */
  395. /* read the page of fixed disk inode (AIT) in raw mode */
  396. mp = read_metapage(ip, address << sbi->l2nbperpage, PSIZE, 1);
  397. if (mp == NULL) {
  398. ip->i_nlink = 1; /* Don't want iput() deleting it */
  399. iput(ip);
  400. return (NULL);
  401. }
  402. /* get the pointer to the disk inode of interest */
  403. dp = (struct dinode *) (mp->data);
  404. dp += inum % 8; /* 8 inodes per 4K page */
  405. /* copy on-disk inode to in-memory inode */
  406. if ((copy_from_dinode(dp, ip)) != 0) {
  407. /* handle bad return by returning NULL for ip */
  408. ip->i_nlink = 1; /* Don't want iput() deleting it */
  409. iput(ip);
  410. /* release the page */
  411. release_metapage(mp);
  412. return (NULL);
  413. }
  414. ip->i_mapping->a_ops = &jfs_metapage_aops;
  415. mapping_set_gfp_mask(ip->i_mapping, GFP_NOFS);
  416. /* Allocations to metadata inodes should not affect quotas */
  417. ip->i_flags |= S_NOQUOTA;
  418. if ((inum == FILESYSTEM_I) && (JFS_IP(ip)->ipimap == sbi->ipaimap)) {
  419. sbi->gengen = le32_to_cpu(dp->di_gengen);
  420. sbi->inostamp = le32_to_cpu(dp->di_inostamp);
  421. }
  422. /* release the page */
  423. release_metapage(mp);
  424. /*
  425. * __mark_inode_dirty expects inodes to be hashed. Since we don't
  426. * want special inodes in the fileset inode space, we make them
  427. * appear hashed, but do not put on any lists. hlist_del()
  428. * will work fine and require no locking.
  429. */
  430. ip->i_hash.pprev = &ip->i_hash.next;
  431. return (ip);
  432. }
  433. /*
  434. * NAME: diWriteSpecial()
  435. *
  436. * FUNCTION: Write the special inode to disk
  437. *
  438. * PARAMETERS:
  439. * ip - special inode
  440. * secondary - 1 if secondary aggregate inode table
  441. *
  442. * RETURN VALUES: none
  443. */
  444. void diWriteSpecial(struct inode *ip, int secondary)
  445. {
  446. struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
  447. uint address;
  448. struct dinode *dp;
  449. ino_t inum = ip->i_ino;
  450. struct metapage *mp;
  451. if (secondary)
  452. address = addressPXD(&sbi->ait2) >> sbi->l2nbperpage;
  453. else
  454. address = AITBL_OFF >> L2PSIZE;
  455. ASSERT(inum < INOSPEREXT);
  456. address += inum >> 3; /* 8 inodes per 4K page */
  457. /* read the page of fixed disk inode (AIT) in raw mode */
  458. mp = read_metapage(ip, address << sbi->l2nbperpage, PSIZE, 1);
  459. if (mp == NULL) {
  460. jfs_err("diWriteSpecial: failed to read aggregate inode "
  461. "extent!");
  462. return;
  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. /* copy on-disk inode to in-memory inode */
  468. copy_to_dinode(dp, ip);
  469. memcpy(&dp->di_xtroot, &JFS_IP(ip)->i_xtroot, 288);
  470. if (inum == FILESYSTEM_I)
  471. dp->di_gengen = cpu_to_le32(sbi->gengen);
  472. /* write the page */
  473. write_metapage(mp);
  474. }
  475. /*
  476. * NAME: diFreeSpecial()
  477. *
  478. * FUNCTION: Free allocated space for special inode
  479. */
  480. void diFreeSpecial(struct inode *ip)
  481. {
  482. if (ip == NULL) {
  483. jfs_err("diFreeSpecial called with NULL ip!");
  484. return;
  485. }
  486. filemap_write_and_wait(ip->i_mapping);
  487. truncate_inode_pages(ip->i_mapping, 0);
  488. iput(ip);
  489. }
  490. /*
  491. * NAME: diWrite()
  492. *
  493. * FUNCTION: write the on-disk inode portion of the in-memory inode
  494. * to its corresponding on-disk inode.
  495. *
  496. * on entry, the specifed incore inode should itself
  497. * specify the disk inode number corresponding to the
  498. * incore inode (i.e. i_number should be initialized).
  499. *
  500. * the inode contains the inode extent address for the disk
  501. * inode. with the inode extent address in hand, the
  502. * page of the extent that contains the disk inode is
  503. * read and the disk inode portion of the incore inode
  504. * is copied to the disk inode.
  505. *
  506. * PARAMETERS:
  507. * tid - transacation id
  508. * ip - pointer to incore inode to be written to the inode extent.
  509. *
  510. * RETURN VALUES:
  511. * 0 - success
  512. * -EIO - i/o error.
  513. */
  514. int diWrite(tid_t tid, struct inode *ip)
  515. {
  516. struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
  517. struct jfs_inode_info *jfs_ip = JFS_IP(ip);
  518. int rc = 0;
  519. s32 ino;
  520. struct dinode *dp;
  521. s64 blkno;
  522. int block_offset;
  523. int inodes_left;
  524. struct metapage *mp;
  525. unsigned long pageno;
  526. int rel_inode;
  527. int dioffset;
  528. struct inode *ipimap;
  529. uint type;
  530. lid_t lid;
  531. struct tlock *ditlck, *tlck;
  532. struct linelock *dilinelock, *ilinelock;
  533. struct lv *lv;
  534. int n;
  535. ipimap = jfs_ip->ipimap;
  536. ino = ip->i_ino & (INOSPERIAG - 1);
  537. if (!addressPXD(&(jfs_ip->ixpxd)) ||
  538. (lengthPXD(&(jfs_ip->ixpxd)) !=
  539. JFS_IP(ipimap)->i_imap->im_nbperiext)) {
  540. jfs_error(ip->i_sb, "diWrite: ixpxd invalid");
  541. return -EIO;
  542. }
  543. /*
  544. * read the page of disk inode containing the specified inode:
  545. */
  546. /* compute the block address of the page */
  547. blkno = INOPBLK(&(jfs_ip->ixpxd), ino, sbi->l2nbperpage);
  548. rel_inode = (ino & (INOSPERPAGE - 1));
  549. pageno = blkno >> sbi->l2nbperpage;
  550. if ((block_offset = ((u32) blkno & (sbi->nbperpage - 1)))) {
  551. /*
  552. * OS/2 didn't always align inode extents on page boundaries
  553. */
  554. inodes_left =
  555. (sbi->nbperpage - block_offset) << sbi->l2niperblk;
  556. if (rel_inode < inodes_left)
  557. rel_inode += block_offset << sbi->l2niperblk;
  558. else {
  559. pageno += 1;
  560. rel_inode -= inodes_left;
  561. }
  562. }
  563. /* read the page of disk inode */
  564. retry:
  565. mp = read_metapage(ipimap, pageno << sbi->l2nbperpage, PSIZE, 1);
  566. if (!mp)
  567. return -EIO;
  568. /* get the pointer to the disk inode */
  569. dp = (struct dinode *) mp->data;
  570. dp += rel_inode;
  571. dioffset = (ino & (INOSPERPAGE - 1)) << L2DISIZE;
  572. /*
  573. * acquire transaction lock on the on-disk inode;
  574. * N.B. tlock is acquired on ipimap not ip;
  575. */
  576. if ((ditlck =
  577. txLock(tid, ipimap, mp, tlckINODE | tlckENTRY)) == NULL)
  578. goto retry;
  579. dilinelock = (struct linelock *) & ditlck->lock;
  580. /*
  581. * copy btree root from in-memory inode to on-disk inode
  582. *
  583. * (tlock is taken from inline B+-tree root in in-memory
  584. * inode when the B+-tree root is updated, which is pointed
  585. * by jfs_ip->blid as well as being on tx tlock list)
  586. *
  587. * further processing of btree root is based on the copy
  588. * in in-memory inode, where txLog() will log from, and,
  589. * for xtree root, txUpdateMap() will update map and reset
  590. * XAD_NEW bit;
  591. */
  592. if (S_ISDIR(ip->i_mode) && (lid = jfs_ip->xtlid)) {
  593. /*
  594. * This is the special xtree inside the directory for storing
  595. * the directory table
  596. */
  597. xtpage_t *p, *xp;
  598. xad_t *xad;
  599. jfs_ip->xtlid = 0;
  600. tlck = lid_to_tlock(lid);
  601. assert(tlck->type & tlckXTREE);
  602. tlck->type |= tlckBTROOT;
  603. tlck->mp = mp;
  604. ilinelock = (struct linelock *) & tlck->lock;
  605. /*
  606. * copy xtree root from inode to dinode:
  607. */
  608. p = &jfs_ip->i_xtroot;
  609. xp = (xtpage_t *) &dp->di_dirtable;
  610. lv = ilinelock->lv;
  611. for (n = 0; n < ilinelock->index; n++, lv++) {
  612. memcpy(&xp->xad[lv->offset], &p->xad[lv->offset],
  613. lv->length << L2XTSLOTSIZE);
  614. }
  615. /* reset on-disk (metadata page) xtree XAD_NEW bit */
  616. xad = &xp->xad[XTENTRYSTART];
  617. for (n = XTENTRYSTART;
  618. n < le16_to_cpu(xp->header.nextindex); n++, xad++)
  619. if (xad->flag & (XAD_NEW | XAD_EXTENDED))
  620. xad->flag &= ~(XAD_NEW | XAD_EXTENDED);
  621. }
  622. if ((lid = jfs_ip->blid) == 0)
  623. goto inlineData;
  624. jfs_ip->blid = 0;
  625. tlck = lid_to_tlock(lid);
  626. type = tlck->type;
  627. tlck->type |= tlckBTROOT;
  628. tlck->mp = mp;
  629. ilinelock = (struct linelock *) & tlck->lock;
  630. /*
  631. * regular file: 16 byte (XAD slot) granularity
  632. */
  633. if (type & tlckXTREE) {
  634. xtpage_t *p, *xp;
  635. xad_t *xad;
  636. /*
  637. * copy xtree root from inode to dinode:
  638. */
  639. p = &jfs_ip->i_xtroot;
  640. xp = &dp->di_xtroot;
  641. lv = ilinelock->lv;
  642. for (n = 0; n < ilinelock->index; n++, lv++) {
  643. memcpy(&xp->xad[lv->offset], &p->xad[lv->offset],
  644. lv->length << L2XTSLOTSIZE);
  645. }
  646. /* reset on-disk (metadata page) xtree XAD_NEW bit */
  647. xad = &xp->xad[XTENTRYSTART];
  648. for (n = XTENTRYSTART;
  649. n < le16_to_cpu(xp->header.nextindex); n++, xad++)
  650. if (xad->flag & (XAD_NEW | XAD_EXTENDED))
  651. xad->flag &= ~(XAD_NEW | XAD_EXTENDED);
  652. }
  653. /*
  654. * directory: 32 byte (directory entry slot) granularity
  655. */
  656. else if (type & tlckDTREE) {
  657. dtpage_t *p, *xp;
  658. /*
  659. * copy dtree root from inode to dinode:
  660. */
  661. p = (dtpage_t *) &jfs_ip->i_dtroot;
  662. xp = (dtpage_t *) & dp->di_dtroot;
  663. lv = ilinelock->lv;
  664. for (n = 0; n < ilinelock->index; n++, lv++) {
  665. memcpy(&xp->slot[lv->offset], &p->slot[lv->offset],
  666. lv->length << L2DTSLOTSIZE);
  667. }
  668. } else {
  669. jfs_err("diWrite: UFO tlock");
  670. }
  671. inlineData:
  672. /*
  673. * copy inline symlink from in-memory inode to on-disk inode
  674. */
  675. if (S_ISLNK(ip->i_mode) && ip->i_size < IDATASIZE) {
  676. lv = & dilinelock->lv[dilinelock->index];
  677. lv->offset = (dioffset + 2 * 128) >> L2INODESLOTSIZE;
  678. lv->length = 2;
  679. memcpy(&dp->di_fastsymlink, jfs_ip->i_inline, IDATASIZE);
  680. dilinelock->index++;
  681. }
  682. /*
  683. * copy inline data from in-memory inode to on-disk inode:
  684. * 128 byte slot granularity
  685. */
  686. if (test_cflag(COMMIT_Inlineea, ip)) {
  687. lv = & dilinelock->lv[dilinelock->index];
  688. lv->offset = (dioffset + 3 * 128) >> L2INODESLOTSIZE;
  689. lv->length = 1;
  690. memcpy(&dp->di_inlineea, jfs_ip->i_inline_ea, INODESLOTSIZE);
  691. dilinelock->index++;
  692. clear_cflag(COMMIT_Inlineea, ip);
  693. }
  694. /*
  695. * lock/copy inode base: 128 byte slot granularity
  696. */
  697. lv = & dilinelock->lv[dilinelock->index];
  698. lv->offset = dioffset >> L2INODESLOTSIZE;
  699. copy_to_dinode(dp, ip);
  700. if (test_and_clear_cflag(COMMIT_Dirtable, ip)) {
  701. lv->length = 2;
  702. memcpy(&dp->di_dirtable, &jfs_ip->i_dirtable, 96);
  703. } else
  704. lv->length = 1;
  705. dilinelock->index++;
  706. /* release the buffer holding the updated on-disk inode.
  707. * the buffer will be later written by commit processing.
  708. */
  709. write_metapage(mp);
  710. return (rc);
  711. }
  712. /*
  713. * NAME: diFree(ip)
  714. *
  715. * FUNCTION: free a specified inode from the inode working map
  716. * for a fileset or aggregate.
  717. *
  718. * if the inode to be freed represents the first (only)
  719. * free inode within the iag, the iag will be placed on
  720. * the ag free inode list.
  721. *
  722. * freeing the inode will cause the inode extent to be
  723. * freed if the inode is the only allocated inode within
  724. * the extent. in this case all the disk resource backing
  725. * up the inode extent will be freed. in addition, the iag
  726. * will be placed on the ag extent free list if the extent
  727. * is the first free extent in the iag. if freeing the
  728. * extent also means that no free inodes will exist for
  729. * the iag, the iag will also be removed from the ag free
  730. * inode list.
  731. *
  732. * the iag describing the inode will be freed if the extent
  733. * is to be freed and it is the only backed extent within
  734. * the iag. in this case, the iag will be removed from the
  735. * ag free extent list and ag free inode list and placed on
  736. * the inode map's free iag list.
  737. *
  738. * a careful update approach is used to provide consistency
  739. * in the face of updates to multiple buffers. under this
  740. * approach, all required buffers are obtained before making
  741. * any updates and are held until all updates are complete.
  742. *
  743. * PARAMETERS:
  744. * ip - inode to be freed.
  745. *
  746. * RETURN VALUES:
  747. * 0 - success
  748. * -EIO - i/o error.
  749. */
  750. int diFree(struct inode *ip)
  751. {
  752. int rc;
  753. ino_t inum = ip->i_ino;
  754. struct iag *iagp, *aiagp, *biagp, *ciagp, *diagp;
  755. struct metapage *mp, *amp, *bmp, *cmp, *dmp;
  756. int iagno, ino, extno, bitno, sword, agno;
  757. int back, fwd;
  758. u32 bitmap, mask;
  759. struct inode *ipimap = JFS_SBI(ip->i_sb)->ipimap;
  760. struct inomap *imap = JFS_IP(ipimap)->i_imap;
  761. pxd_t freepxd;
  762. tid_t tid;
  763. struct inode *iplist[3];
  764. struct tlock *tlck;
  765. struct pxd_lock *pxdlock;
  766. /*
  767. * This is just to suppress compiler warnings. The same logic that
  768. * references these variables is used to initialize them.
  769. */
  770. aiagp = biagp = ciagp = diagp = NULL;
  771. /* get the iag number containing the inode.
  772. */
  773. iagno = INOTOIAG(inum);
  774. /* make sure that the iag is contained within
  775. * the map.
  776. */
  777. if (iagno >= imap->im_nextiag) {
  778. print_hex_dump(KERN_ERR, "imap: ", DUMP_PREFIX_ADDRESS, 16, 4,
  779. imap, 32, 0);
  780. jfs_error(ip->i_sb,
  781. "diFree: inum = %d, iagno = %d, nextiag = %d",
  782. (uint) inum, iagno, imap->im_nextiag);
  783. return -EIO;
  784. }
  785. /* get the allocation group for this ino.
  786. */
  787. agno = JFS_IP(ip)->agno;
  788. /* Lock the AG specific inode map information
  789. */
  790. AG_LOCK(imap, agno);
  791. /* Obtain read lock in imap inode. Don't release it until we have
  792. * read all of the IAG's that we are going to.
  793. */
  794. IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
  795. /* read the iag.
  796. */
  797. if ((rc = diIAGRead(imap, iagno, &mp))) {
  798. IREAD_UNLOCK(ipimap);
  799. AG_UNLOCK(imap, agno);
  800. return (rc);
  801. }
  802. iagp = (struct iag *) mp->data;
  803. /* get the inode number and extent number of the inode within
  804. * the iag and the inode number within the extent.
  805. */
  806. ino = inum & (INOSPERIAG - 1);
  807. extno = ino >> L2INOSPEREXT;
  808. bitno = ino & (INOSPEREXT - 1);
  809. mask = HIGHORDER >> bitno;
  810. if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
  811. jfs_error(ip->i_sb,
  812. "diFree: wmap shows inode already free");
  813. }
  814. if (!addressPXD(&iagp->inoext[extno])) {
  815. release_metapage(mp);
  816. IREAD_UNLOCK(ipimap);
  817. AG_UNLOCK(imap, agno);
  818. jfs_error(ip->i_sb, "diFree: invalid inoext");
  819. return -EIO;
  820. }
  821. /* compute the bitmap for the extent reflecting the freed inode.
  822. */
  823. bitmap = le32_to_cpu(iagp->wmap[extno]) & ~mask;
  824. if (imap->im_agctl[agno].numfree > imap->im_agctl[agno].numinos) {
  825. release_metapage(mp);
  826. IREAD_UNLOCK(ipimap);
  827. AG_UNLOCK(imap, agno);
  828. jfs_error(ip->i_sb, "diFree: numfree > numinos");
  829. return -EIO;
  830. }
  831. /*
  832. * inode extent still has some inodes or below low water mark:
  833. * keep the inode extent;
  834. */
  835. if (bitmap ||
  836. imap->im_agctl[agno].numfree < 96 ||
  837. (imap->im_agctl[agno].numfree < 288 &&
  838. (((imap->im_agctl[agno].numfree * 100) /
  839. imap->im_agctl[agno].numinos) <= 25))) {
  840. /* if the iag currently has no free inodes (i.e.,
  841. * the inode being freed is the first free inode of iag),
  842. * insert the iag at head of the inode free list for the ag.
  843. */
  844. if (iagp->nfreeinos == 0) {
  845. /* check if there are any iags on the ag inode
  846. * free list. if so, read the first one so that
  847. * we can link the current iag onto the list at
  848. * the head.
  849. */
  850. if ((fwd = imap->im_agctl[agno].inofree) >= 0) {
  851. /* read the iag that currently is the head
  852. * of the list.
  853. */
  854. if ((rc = diIAGRead(imap, fwd, &amp))) {
  855. IREAD_UNLOCK(ipimap);
  856. AG_UNLOCK(imap, agno);
  857. release_metapage(mp);
  858. return (rc);
  859. }
  860. aiagp = (struct iag *) amp->data;
  861. /* make current head point back to the iag.
  862. */
  863. aiagp->inofreeback = cpu_to_le32(iagno);
  864. write_metapage(amp);
  865. }
  866. /* iag points forward to current head and iag
  867. * becomes the new head of the list.
  868. */
  869. iagp->inofreefwd =
  870. cpu_to_le32(imap->im_agctl[agno].inofree);
  871. iagp->inofreeback = cpu_to_le32(-1);
  872. imap->im_agctl[agno].inofree = iagno;
  873. }
  874. IREAD_UNLOCK(ipimap);
  875. /* update the free inode summary map for the extent if
  876. * freeing the inode means the extent will now have free
  877. * inodes (i.e., the inode being freed is the first free
  878. * inode of extent),
  879. */
  880. if (iagp->wmap[extno] == cpu_to_le32(ONES)) {
  881. sword = extno >> L2EXTSPERSUM;
  882. bitno = extno & (EXTSPERSUM - 1);
  883. iagp->inosmap[sword] &=
  884. cpu_to_le32(~(HIGHORDER >> bitno));
  885. }
  886. /* update the bitmap.
  887. */
  888. iagp->wmap[extno] = cpu_to_le32(bitmap);
  889. /* update the free inode counts at the iag, ag and
  890. * map level.
  891. */
  892. le32_add_cpu(&iagp->nfreeinos, 1);
  893. imap->im_agctl[agno].numfree += 1;
  894. atomic_inc(&imap->im_numfree);
  895. /* release the AG inode map lock
  896. */
  897. AG_UNLOCK(imap, agno);
  898. /* write the iag */
  899. write_metapage(mp);
  900. return (0);
  901. }
  902. /*
  903. * inode extent has become free and above low water mark:
  904. * free the inode extent;
  905. */
  906. /*
  907. * prepare to update iag list(s) (careful update step 1)
  908. */
  909. amp = bmp = cmp = dmp = NULL;
  910. fwd = back = -1;
  911. /* check if the iag currently has no free extents. if so,
  912. * it will be placed on the head of the ag extent free list.
  913. */
  914. if (iagp->nfreeexts == 0) {
  915. /* check if the ag extent free list has any iags.
  916. * if so, read the iag at the head of the list now.
  917. * this (head) iag will be updated later to reflect
  918. * the addition of the current iag at the head of
  919. * the list.
  920. */
  921. if ((fwd = imap->im_agctl[agno].extfree) >= 0) {
  922. if ((rc = diIAGRead(imap, fwd, &amp)))
  923. goto error_out;
  924. aiagp = (struct iag *) amp->data;
  925. }
  926. } else {
  927. /* iag has free extents. check if the addition of a free
  928. * extent will cause all extents to be free within this
  929. * iag. if so, the iag will be removed from the ag extent
  930. * free list and placed on the inode map's free iag list.
  931. */
  932. if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG - 1)) {
  933. /* in preparation for removing the iag from the
  934. * ag extent free list, read the iags preceeding
  935. * and following the iag on the ag extent free
  936. * list.
  937. */
  938. if ((fwd = le32_to_cpu(iagp->extfreefwd)) >= 0) {
  939. if ((rc = diIAGRead(imap, fwd, &amp)))
  940. goto error_out;
  941. aiagp = (struct iag *) amp->data;
  942. }
  943. if ((back = le32_to_cpu(iagp->extfreeback)) >= 0) {
  944. if ((rc = diIAGRead(imap, back, &bmp)))
  945. goto error_out;
  946. biagp = (struct iag *) bmp->data;
  947. }
  948. }
  949. }
  950. /* remove the iag from the ag inode free list if freeing
  951. * this extent cause the iag to have no free inodes.
  952. */
  953. if (iagp->nfreeinos == cpu_to_le32(INOSPEREXT - 1)) {
  954. int inofreeback = le32_to_cpu(iagp->inofreeback);
  955. int inofreefwd = le32_to_cpu(iagp->inofreefwd);
  956. /* in preparation for removing the iag from the
  957. * ag inode free list, read the iags preceeding
  958. * and following the iag on the ag inode free
  959. * list. before reading these iags, we must make
  960. * sure that we already don't have them in hand
  961. * from up above, since re-reading an iag (buffer)
  962. * we are currently holding would cause a deadlock.
  963. */
  964. if (inofreefwd >= 0) {
  965. if (inofreefwd == fwd)
  966. ciagp = (struct iag *) amp->data;
  967. else if (inofreefwd == back)
  968. ciagp = (struct iag *) bmp->data;
  969. else {
  970. if ((rc =
  971. diIAGRead(imap, inofreefwd, &cmp)))
  972. goto error_out;
  973. ciagp = (struct iag *) cmp->data;
  974. }
  975. assert(ciagp != NULL);
  976. }
  977. if (inofreeback >= 0) {
  978. if (inofreeback == fwd)
  979. diagp = (struct iag *) amp->data;
  980. else if (inofreeback == back)
  981. diagp = (struct iag *) bmp->data;
  982. else {
  983. if ((rc =
  984. diIAGRead(imap, inofreeback, &dmp)))
  985. goto error_out;
  986. diagp = (struct iag *) dmp->data;
  987. }
  988. assert(diagp != NULL);
  989. }
  990. }
  991. IREAD_UNLOCK(ipimap);
  992. /*
  993. * invalidate any page of the inode extent freed from buffer cache;
  994. */
  995. freepxd = iagp->inoext[extno];
  996. invalidate_pxd_metapages(ip, freepxd);
  997. /*
  998. * update iag list(s) (careful update step 2)
  999. */
  1000. /* add the iag to the ag extent free list if this is the
  1001. * first free extent for the iag.
  1002. */
  1003. if (iagp->nfreeexts == 0) {
  1004. if (fwd >= 0)
  1005. aiagp->extfreeback = cpu_to_le32(iagno);
  1006. iagp->extfreefwd =
  1007. cpu_to_le32(imap->im_agctl[agno].extfree);
  1008. iagp->extfreeback = cpu_to_le32(-1);
  1009. imap->im_agctl[agno].extfree = iagno;
  1010. } else {
  1011. /* remove the iag from the ag extent list if all extents
  1012. * are now free and place it on the inode map iag free list.
  1013. */
  1014. if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG - 1)) {
  1015. if (fwd >= 0)
  1016. aiagp->extfreeback = iagp->extfreeback;
  1017. if (back >= 0)
  1018. biagp->extfreefwd = iagp->extfreefwd;
  1019. else
  1020. imap->im_agctl[agno].extfree =
  1021. le32_to_cpu(iagp->extfreefwd);
  1022. iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
  1023. IAGFREE_LOCK(imap);
  1024. iagp->iagfree = cpu_to_le32(imap->im_freeiag);
  1025. imap->im_freeiag = iagno;
  1026. IAGFREE_UNLOCK(imap);
  1027. }
  1028. }
  1029. /* remove the iag from the ag inode free list if freeing
  1030. * this extent causes the iag to have no free inodes.
  1031. */
  1032. if (iagp->nfreeinos == cpu_to_le32(INOSPEREXT - 1)) {
  1033. if ((int) le32_to_cpu(iagp->inofreefwd) >= 0)
  1034. ciagp->inofreeback = iagp->inofreeback;
  1035. if ((int) le32_to_cpu(iagp->inofreeback) >= 0)
  1036. diagp->inofreefwd = iagp->inofreefwd;
  1037. else
  1038. imap->im_agctl[agno].inofree =
  1039. le32_to_cpu(iagp->inofreefwd);
  1040. iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
  1041. }
  1042. /* update the inode extent address and working map
  1043. * to reflect the free extent.
  1044. * the permanent map should have been updated already
  1045. * for the inode being freed.
  1046. */
  1047. if (iagp->pmap[extno] != 0) {
  1048. jfs_error(ip->i_sb, "diFree: the pmap does not show inode free");
  1049. }
  1050. iagp->wmap[extno] = 0;
  1051. PXDlength(&iagp->inoext[extno], 0);
  1052. PXDaddress(&iagp->inoext[extno], 0);
  1053. /* update the free extent and free inode summary maps
  1054. * to reflect the freed extent.
  1055. * the inode summary map is marked to indicate no inodes
  1056. * available for the freed extent.
  1057. */
  1058. sword = extno >> L2EXTSPERSUM;
  1059. bitno = extno & (EXTSPERSUM - 1);
  1060. mask = HIGHORDER >> bitno;
  1061. iagp->inosmap[sword] |= cpu_to_le32(mask);
  1062. iagp->extsmap[sword] &= cpu_to_le32(~mask);
  1063. /* update the number of free inodes and number of free extents
  1064. * for the iag.
  1065. */
  1066. le32_add_cpu(&iagp->nfreeinos, -(INOSPEREXT - 1));
  1067. le32_add_cpu(&iagp->nfreeexts, 1);
  1068. /* update the number of free inodes and backed inodes
  1069. * at the ag and inode map level.
  1070. */
  1071. imap->im_agctl[agno].numfree -= (INOSPEREXT - 1);
  1072. imap->im_agctl[agno].numinos -= INOSPEREXT;
  1073. atomic_sub(INOSPEREXT - 1, &imap->im_numfree);
  1074. atomic_sub(INOSPEREXT, &imap->im_numinos);
  1075. if (amp)
  1076. write_metapage(amp);
  1077. if (bmp)
  1078. write_metapage(bmp);
  1079. if (cmp)
  1080. write_metapage(cmp);
  1081. if (dmp)
  1082. write_metapage(dmp);
  1083. /*
  1084. * start transaction to update block allocation map
  1085. * for the inode extent freed;
  1086. *
  1087. * N.B. AG_LOCK is released and iag will be released below, and
  1088. * other thread may allocate inode from/reusing the ixad freed
  1089. * BUT with new/different backing inode extent from the extent
  1090. * to be freed by the transaction;
  1091. */
  1092. tid = txBegin(ipimap->i_sb, COMMIT_FORCE);
  1093. mutex_lock(&JFS_IP(ipimap)->commit_mutex);
  1094. /* acquire tlock of the iag page of the freed ixad
  1095. * to force the page NOHOMEOK (even though no data is
  1096. * logged from the iag page) until NOREDOPAGE|FREEXTENT log
  1097. * for the free of the extent is committed;
  1098. * write FREEXTENT|NOREDOPAGE log record
  1099. * N.B. linelock is overlaid as freed extent descriptor;
  1100. */
  1101. tlck = txLock(tid, ipimap, mp, tlckINODE | tlckFREE);
  1102. pxdlock = (struct pxd_lock *) & tlck->lock;
  1103. pxdlock->flag = mlckFREEPXD;
  1104. pxdlock->pxd = freepxd;
  1105. pxdlock->index = 1;
  1106. write_metapage(mp);
  1107. iplist[0] = ipimap;
  1108. /*
  1109. * logredo needs the IAG number and IAG extent index in order
  1110. * to ensure that the IMap is consistent. The least disruptive
  1111. * way to pass these values through to the transaction manager
  1112. * is in the iplist array.
  1113. *
  1114. * It's not pretty, but it works.
  1115. */
  1116. iplist[1] = (struct inode *) (size_t)iagno;
  1117. iplist[2] = (struct inode *) (size_t)extno;
  1118. rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
  1119. txEnd(tid);
  1120. mutex_unlock(&JFS_IP(ipimap)->commit_mutex);
  1121. /* unlock the AG inode map information */
  1122. AG_UNLOCK(imap, agno);
  1123. return (0);
  1124. error_out:
  1125. IREAD_UNLOCK(ipimap);
  1126. if (amp)
  1127. release_metapage(amp);
  1128. if (bmp)
  1129. release_metapage(bmp);
  1130. if (cmp)
  1131. release_metapage(cmp);
  1132. if (dmp)
  1133. release_metapage(dmp);
  1134. AG_UNLOCK(imap, agno);
  1135. release_metapage(mp);
  1136. return (rc);
  1137. }
  1138. /*
  1139. * There are several places in the diAlloc* routines where we initialize
  1140. * the inode.
  1141. */
  1142. static inline void
  1143. diInitInode(struct inode *ip, int iagno, int ino, int extno, struct iag * iagp)
  1144. {
  1145. struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
  1146. struct jfs_inode_info *jfs_ip = JFS_IP(ip);
  1147. ip->i_ino = (iagno << L2INOSPERIAG) + ino;
  1148. jfs_ip->ixpxd = iagp->inoext[extno];
  1149. jfs_ip->agno = BLKTOAG(le64_to_cpu(iagp->agstart), sbi);
  1150. jfs_ip->active_ag = -1;
  1151. }
  1152. /*
  1153. * NAME: diAlloc(pip,dir,ip)
  1154. *
  1155. * FUNCTION: allocate a disk inode from the inode working map
  1156. * for a fileset or aggregate.
  1157. *
  1158. * PARAMETERS:
  1159. * pip - pointer to incore inode for the parent inode.
  1160. * dir - 'true' if the new disk inode is for a directory.
  1161. * ip - pointer to a new inode
  1162. *
  1163. * RETURN VALUES:
  1164. * 0 - success.
  1165. * -ENOSPC - insufficient disk resources.
  1166. * -EIO - i/o error.
  1167. */
  1168. int diAlloc(struct inode *pip, bool dir, struct inode *ip)
  1169. {
  1170. int rc, ino, iagno, addext, extno, bitno, sword;
  1171. int nwords, rem, i, agno;
  1172. u32 mask, inosmap, extsmap;
  1173. struct inode *ipimap;
  1174. struct metapage *mp;
  1175. ino_t inum;
  1176. struct iag *iagp;
  1177. struct inomap *imap;
  1178. /* get the pointers to the inode map inode and the
  1179. * corresponding imap control structure.
  1180. */
  1181. ipimap = JFS_SBI(pip->i_sb)->ipimap;
  1182. imap = JFS_IP(ipimap)->i_imap;
  1183. JFS_IP(ip)->ipimap = ipimap;
  1184. JFS_IP(ip)->fileset = FILESYSTEM_I;
  1185. /* for a directory, the allocation policy is to start
  1186. * at the ag level using the preferred ag.
  1187. */
  1188. if (dir) {
  1189. agno = dbNextAG(JFS_SBI(pip->i_sb)->ipbmap);
  1190. AG_LOCK(imap, agno);
  1191. goto tryag;
  1192. }
  1193. /* for files, the policy starts off by trying to allocate from
  1194. * the same iag containing the parent disk inode:
  1195. * try to allocate the new disk inode close to the parent disk
  1196. * inode, using parent disk inode number + 1 as the allocation
  1197. * hint. (we use a left-to-right policy to attempt to avoid
  1198. * moving backward on the disk.) compute the hint within the
  1199. * file system and the iag.
  1200. */
  1201. /* get the ag number of this iag */
  1202. agno = JFS_IP(pip)->agno;
  1203. if (atomic_read(&JFS_SBI(pip->i_sb)->bmap->db_active[agno])) {
  1204. /*
  1205. * There is an open file actively growing. We want to
  1206. * allocate new inodes from a different ag to avoid
  1207. * fragmentation problems.
  1208. */
  1209. agno = dbNextAG(JFS_SBI(pip->i_sb)->ipbmap);
  1210. AG_LOCK(imap, agno);
  1211. goto tryag;
  1212. }
  1213. inum = pip->i_ino + 1;
  1214. ino = inum & (INOSPERIAG - 1);
  1215. /* back off the hint if it is outside of the iag */
  1216. if (ino == 0)
  1217. inum = pip->i_ino;
  1218. /* lock the AG inode map information */
  1219. AG_LOCK(imap, agno);
  1220. /* Get read lock on imap inode */
  1221. IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
  1222. /* get the iag number and read the iag */
  1223. iagno = INOTOIAG(inum);
  1224. if ((rc = diIAGRead(imap, iagno, &mp))) {
  1225. IREAD_UNLOCK(ipimap);
  1226. AG_UNLOCK(imap, agno);
  1227. return (rc);
  1228. }
  1229. iagp = (struct iag *) mp->data;
  1230. /* determine if new inode extent is allowed to be added to the iag.
  1231. * new inode extent can be added to the iag if the ag
  1232. * has less than 32 free disk inodes and the iag has free extents.
  1233. */
  1234. addext = (imap->im_agctl[agno].numfree < 32 && iagp->nfreeexts);
  1235. /*
  1236. * try to allocate from the IAG
  1237. */
  1238. /* check if the inode may be allocated from the iag
  1239. * (i.e. the inode has free inodes or new extent can be added).
  1240. */
  1241. if (iagp->nfreeinos || addext) {
  1242. /* determine the extent number of the hint.
  1243. */
  1244. extno = ino >> L2INOSPEREXT;
  1245. /* check if the extent containing the hint has backed
  1246. * inodes. if so, try to allocate within this extent.
  1247. */
  1248. if (addressPXD(&iagp->inoext[extno])) {
  1249. bitno = ino & (INOSPEREXT - 1);
  1250. if ((bitno =
  1251. diFindFree(le32_to_cpu(iagp->wmap[extno]),
  1252. bitno))
  1253. < INOSPEREXT) {
  1254. ino = (extno << L2INOSPEREXT) + bitno;
  1255. /* a free inode (bit) was found within this
  1256. * extent, so allocate it.
  1257. */
  1258. rc = diAllocBit(imap, iagp, ino);
  1259. IREAD_UNLOCK(ipimap);
  1260. if (rc) {
  1261. assert(rc == -EIO);
  1262. } else {
  1263. /* set the results of the allocation
  1264. * and write the iag.
  1265. */
  1266. diInitInode(ip, iagno, ino, extno,
  1267. iagp);
  1268. mark_metapage_dirty(mp);
  1269. }
  1270. release_metapage(mp);
  1271. /* free the AG lock and return.
  1272. */
  1273. AG_UNLOCK(imap, agno);
  1274. return (rc);
  1275. }
  1276. if (!addext)
  1277. extno =
  1278. (extno ==
  1279. EXTSPERIAG - 1) ? 0 : extno + 1;
  1280. }
  1281. /*
  1282. * no free inodes within the extent containing the hint.
  1283. *
  1284. * try to allocate from the backed extents following
  1285. * hint or, if appropriate (i.e. addext is true), allocate
  1286. * an extent of free inodes at or following the extent
  1287. * containing the hint.
  1288. *
  1289. * the free inode and free extent summary maps are used
  1290. * here, so determine the starting summary map position
  1291. * and the number of words we'll have to examine. again,
  1292. * the approach is to allocate following the hint, so we
  1293. * might have to initially ignore prior bits of the summary
  1294. * map that represent extents prior to the extent containing
  1295. * the hint and later revisit these bits.
  1296. */
  1297. bitno = extno & (EXTSPERSUM - 1);
  1298. nwords = (bitno == 0) ? SMAPSZ : SMAPSZ + 1;
  1299. sword = extno >> L2EXTSPERSUM;
  1300. /* mask any prior bits for the starting words of the
  1301. * summary map.
  1302. */
  1303. mask = ONES << (EXTSPERSUM - bitno);
  1304. inosmap = le32_to_cpu(iagp->inosmap[sword]) | mask;
  1305. extsmap = le32_to_cpu(iagp->extsmap[sword]) | mask;
  1306. /* scan the free inode and free extent summary maps for
  1307. * free resources.
  1308. */
  1309. for (i = 0; i < nwords; i++) {
  1310. /* check if this word of the free inode summary
  1311. * map describes an extent with free inodes.
  1312. */
  1313. if (~inosmap) {
  1314. /* an extent with free inodes has been
  1315. * found. determine the extent number
  1316. * and the inode number within the extent.
  1317. */
  1318. rem = diFindFree(inosmap, 0);
  1319. extno = (sword << L2EXTSPERSUM) + rem;
  1320. rem = diFindFree(le32_to_cpu(iagp->wmap[extno]),
  1321. 0);
  1322. if (rem >= INOSPEREXT) {
  1323. IREAD_UNLOCK(ipimap);
  1324. release_metapage(mp);
  1325. AG_UNLOCK(imap, agno);
  1326. jfs_error(ip->i_sb,
  1327. "diAlloc: can't find free bit "
  1328. "in wmap");
  1329. return -EIO;
  1330. }
  1331. /* determine the inode number within the
  1332. * iag and allocate the inode from the
  1333. * map.
  1334. */
  1335. ino = (extno << L2INOSPEREXT) + rem;
  1336. rc = diAllocBit(imap, iagp, ino);
  1337. IREAD_UNLOCK(ipimap);
  1338. if (rc)
  1339. assert(rc == -EIO);
  1340. else {
  1341. /* set the results of the allocation
  1342. * and write the iag.
  1343. */
  1344. diInitInode(ip, iagno, ino, extno,
  1345. iagp);
  1346. mark_metapage_dirty(mp);
  1347. }
  1348. release_metapage(mp);
  1349. /* free the AG lock and return.
  1350. */
  1351. AG_UNLOCK(imap, agno);
  1352. return (rc);
  1353. }
  1354. /* check if we may allocate an extent of free
  1355. * inodes and whether this word of the free
  1356. * extents summary map describes a free extent.
  1357. */
  1358. if (addext && ~extsmap) {
  1359. /* a free extent has been found. determine
  1360. * the extent number.
  1361. */
  1362. rem = diFindFree(extsmap, 0);
  1363. extno = (sword << L2EXTSPERSUM) + rem;
  1364. /* allocate an extent of free inodes.
  1365. */
  1366. if ((rc = diNewExt(imap, iagp, extno))) {
  1367. /* if there is no disk space for a
  1368. * new extent, try to allocate the
  1369. * disk inode from somewhere else.
  1370. */
  1371. if (rc == -ENOSPC)
  1372. break;
  1373. assert(rc == -EIO);
  1374. } else {
  1375. /* set the results of the allocation
  1376. * and write the iag.
  1377. */
  1378. diInitInode(ip, iagno,
  1379. extno << L2INOSPEREXT,
  1380. extno, iagp);
  1381. mark_metapage_dirty(mp);
  1382. }
  1383. release_metapage(mp);
  1384. /* free the imap inode & the AG lock & return.
  1385. */
  1386. IREAD_UNLOCK(ipimap);
  1387. AG_UNLOCK(imap, agno);
  1388. return (rc);
  1389. }
  1390. /* move on to the next set of summary map words.
  1391. */
  1392. sword = (sword == SMAPSZ - 1) ? 0 : sword + 1;
  1393. inosmap = le32_to_cpu(iagp->inosmap[sword]);
  1394. extsmap = le32_to_cpu(iagp->extsmap[sword]);
  1395. }
  1396. }
  1397. /* unlock imap inode */
  1398. IREAD_UNLOCK(ipimap);
  1399. /* nothing doing in this iag, so release it. */
  1400. release_metapage(mp);
  1401. tryag:
  1402. /*
  1403. * try to allocate anywhere within the same AG as the parent inode.
  1404. */
  1405. rc = diAllocAG(imap, agno, dir, ip);
  1406. AG_UNLOCK(imap, agno);
  1407. if (rc != -ENOSPC)
  1408. return (rc);
  1409. /*
  1410. * try to allocate in any AG.
  1411. */
  1412. return (diAllocAny(imap, agno, dir, ip));
  1413. }
  1414. /*
  1415. * NAME: diAllocAG(imap,agno,dir,ip)
  1416. *
  1417. * FUNCTION: allocate a disk inode from the allocation group.
  1418. *
  1419. * this routine first determines if a new extent of free
  1420. * inodes should be added for the allocation group, with
  1421. * the current request satisfied from this extent. if this
  1422. * is the case, an attempt will be made to do just that. if
  1423. * this attempt fails or it has been determined that a new
  1424. * extent should not be added, an attempt is made to satisfy
  1425. * the request by allocating an existing (backed) free inode
  1426. * from the allocation group.
  1427. *
  1428. * PRE CONDITION: Already have the AG lock for this AG.
  1429. *
  1430. * PARAMETERS:
  1431. * imap - pointer to inode map control structure.
  1432. * agno - allocation group to allocate from.
  1433. * dir - 'true' if the new disk inode is for a directory.
  1434. * ip - pointer to the new inode to be filled in on successful return
  1435. * with the disk inode number allocated, its extent address
  1436. * and the start of the ag.
  1437. *
  1438. * RETURN VALUES:
  1439. * 0 - success.
  1440. * -ENOSPC - insufficient disk resources.
  1441. * -EIO - i/o error.
  1442. */
  1443. static int
  1444. diAllocAG(struct inomap * imap, int agno, bool dir, struct inode *ip)
  1445. {
  1446. int rc, addext, numfree, numinos;
  1447. /* get the number of free and the number of backed disk
  1448. * inodes currently within the ag.
  1449. */
  1450. numfree = imap->im_agctl[agno].numfree;
  1451. numinos = imap->im_agctl[agno].numinos;
  1452. if (numfree > numinos) {
  1453. jfs_error(ip->i_sb, "diAllocAG: numfree > numinos");
  1454. return -EIO;
  1455. }
  1456. /* determine if we should allocate a new extent of free inodes
  1457. * within the ag: for directory inodes, add a new extent
  1458. * if there are a small number of free inodes or number of free
  1459. * inodes is a small percentage of the number of backed inodes.
  1460. */
  1461. if (dir)
  1462. addext = (numfree < 64 ||
  1463. (numfree < 256
  1464. && ((numfree * 100) / numinos) <= 20));
  1465. else
  1466. addext = (numfree == 0);
  1467. /*
  1468. * try to allocate a new extent of free inodes.
  1469. */
  1470. if (addext) {
  1471. /* if free space is not avaliable for this new extent, try
  1472. * below to allocate a free and existing (already backed)
  1473. * inode from the ag.
  1474. */
  1475. if ((rc = diAllocExt(imap, agno, ip)) != -ENOSPC)
  1476. return (rc);
  1477. }
  1478. /*
  1479. * try to allocate an existing free inode from the ag.
  1480. */
  1481. return (diAllocIno(imap, agno, ip));
  1482. }
  1483. /*
  1484. * NAME: diAllocAny(imap,agno,dir,iap)
  1485. *
  1486. * FUNCTION: allocate a disk inode from any other allocation group.
  1487. *
  1488. * this routine is called when an allocation attempt within
  1489. * the primary allocation group has failed. if attempts to
  1490. * allocate an inode from any allocation group other than the
  1491. * specified primary group.
  1492. *
  1493. * PARAMETERS:
  1494. * imap - pointer to inode map control structure.
  1495. * agno - primary allocation group (to avoid).
  1496. * dir - 'true' if the new disk inode is for a directory.
  1497. * ip - pointer to a new inode to be filled in on successful return
  1498. * with the disk inode number allocated, its extent address
  1499. * and the start of the ag.
  1500. *
  1501. * RETURN VALUES:
  1502. * 0 - success.
  1503. * -ENOSPC - insufficient disk resources.
  1504. * -EIO - i/o error.
  1505. */
  1506. static int
  1507. diAllocAny(struct inomap * imap, int agno, bool dir, struct inode *ip)
  1508. {
  1509. int ag, rc;
  1510. int maxag = JFS_SBI(imap->im_ipimap->i_sb)->bmap->db_maxag;
  1511. /* try to allocate from the ags following agno up to
  1512. * the maximum ag number.
  1513. */
  1514. for (ag = agno + 1; ag <= maxag; ag++) {
  1515. AG_LOCK(imap, ag);
  1516. rc = diAllocAG(imap, ag, dir, ip);
  1517. AG_UNLOCK(imap, ag);
  1518. if (rc != -ENOSPC)
  1519. return (rc);
  1520. }
  1521. /* try to allocate from the ags in front of agno.
  1522. */
  1523. for (ag = 0; ag < agno; ag++) {
  1524. AG_LOCK(imap, ag);
  1525. rc = diAllocAG(imap, ag, dir, ip);
  1526. AG_UNLOCK(imap, ag);
  1527. if (rc != -ENOSPC)
  1528. return (rc);
  1529. }
  1530. /* no free disk inodes.
  1531. */
  1532. return -ENOSPC;
  1533. }
  1534. /*
  1535. * NAME: diAllocIno(imap,agno,ip)
  1536. *
  1537. * FUNCTION: allocate a disk inode from the allocation group's free
  1538. * inode list, returning an error if this free list is
  1539. * empty (i.e. no iags on the list).
  1540. *
  1541. * allocation occurs from the first iag on the list using
  1542. * the iag's free inode summary map to find the leftmost
  1543. * free inode in the iag.
  1544. *
  1545. * PRE CONDITION: Already have AG lock for this AG.
  1546. *
  1547. * PARAMETERS:
  1548. * imap - pointer to inode map control structure.
  1549. * agno - allocation group.
  1550. * ip - pointer to new inode to be filled in on successful return
  1551. * with the disk inode number allocated, its extent address
  1552. * and the start of the ag.
  1553. *
  1554. * RETURN VALUES:
  1555. * 0 - success.
  1556. * -ENOSPC - insufficient disk resources.
  1557. * -EIO - i/o error.
  1558. */
  1559. static int diAllocIno(struct inomap * imap, int agno, struct inode *ip)
  1560. {
  1561. int iagno, ino, rc, rem, extno, sword;
  1562. struct metapage *mp;
  1563. struct iag *iagp;
  1564. /* check if there are iags on the ag's free inode list.
  1565. */
  1566. if ((iagno = imap->im_agctl[agno].inofree) < 0)
  1567. return -ENOSPC;
  1568. /* obtain read lock on imap inode */
  1569. IREAD_LOCK(imap->im_ipimap, RDWRLOCK_IMAP);
  1570. /* read the iag at the head of the list.
  1571. */
  1572. if ((rc = diIAGRead(imap, iagno, &mp))) {
  1573. IREAD_UNLOCK(imap->im_ipimap);
  1574. return (rc);
  1575. }
  1576. iagp = (struct iag *) mp->data;
  1577. /* better be free inodes in this iag if it is on the
  1578. * list.
  1579. */
  1580. if (!iagp->nfreeinos) {
  1581. IREAD_UNLOCK(imap->im_ipimap);
  1582. release_metapage(mp);
  1583. jfs_error(ip->i_sb,
  1584. "diAllocIno: nfreeinos = 0, but iag on freelist");
  1585. return -EIO;
  1586. }
  1587. /* scan the free inode summary map to find an extent
  1588. * with free inodes.
  1589. */
  1590. for (sword = 0;; sword++) {
  1591. if (sword >= SMAPSZ) {
  1592. IREAD_UNLOCK(imap->im_ipimap);
  1593. release_metapage(mp);
  1594. jfs_error(ip->i_sb,
  1595. "diAllocIno: free inode not found in summary map");
  1596. return -EIO;
  1597. }
  1598. if (~iagp->inosmap[sword])
  1599. break;
  1600. }
  1601. /* found a extent with free inodes. determine
  1602. * the extent number.
  1603. */
  1604. rem = diFindFree(le32_to_cpu(iagp->inosmap[sword]), 0);
  1605. if (rem >= EXTSPERSUM) {
  1606. IREAD_UNLOCK(imap->im_ipimap);
  1607. release_metapage(mp);
  1608. jfs_error(ip->i_sb, "diAllocIno: no free extent found");
  1609. return -EIO;
  1610. }
  1611. extno = (sword << L2EXTSPERSUM) + rem;
  1612. /* find the first free inode in the extent.
  1613. */
  1614. rem