libufs_subr.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*-
  2. * Copyright (c) 1982, 1986, 1989, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. *
  29. * @(#)ffs_subr.c 8.5 (Berkeley) 3/21/95
  30. */
  31. #include <u.h>
  32. #include <libc.h>
  33. #include <ufs/libufsdat.h>
  34. #include <ufs/freebsd_util.h>
  35. #include <ufs/quota.h>
  36. #include <ufs/fs.h>
  37. #include <ufs/inode.h>
  38. void panic(char*, ...);
  39. /*
  40. * Update the frsum fields to reflect addition or deletion
  41. * of some frags.
  42. */
  43. void
  44. ffs_fragacct(Fs *fs, int fragmap, uint32_t fraglist[], int cnt)
  45. {
  46. int inblk;
  47. int field, subfield;
  48. int siz, pos;
  49. inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
  50. fragmap <<= 1;
  51. for (siz = 1; siz < fs->fs_frag; siz++) {
  52. if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
  53. continue;
  54. field = around[siz];
  55. subfield = inside[siz];
  56. for (pos = siz; pos <= fs->fs_frag; pos++) {
  57. if ((fragmap & field) == subfield) {
  58. fraglist[siz] += cnt;
  59. pos += siz;
  60. field <<= siz;
  61. subfield <<= siz;
  62. }
  63. field <<= 1;
  64. subfield <<= 1;
  65. }
  66. }
  67. }
  68. /*
  69. * block operations
  70. *
  71. * check if a block is available
  72. */
  73. int
  74. ffs_isblock(Fs *fs, unsigned char *cp, ufs1_daddr_t h)
  75. {
  76. unsigned char mask;
  77. switch ((int)fs->fs_frag) {
  78. case 8:
  79. return (cp[h] == 0xff);
  80. case 4:
  81. mask = 0x0f << ((h & 0x1) << 2);
  82. return ((cp[h >> 1] & mask) == mask);
  83. case 2:
  84. mask = 0x03 << ((h & 0x3) << 1);
  85. return ((cp[h >> 2] & mask) == mask);
  86. case 1:
  87. mask = 0x01 << (h & 0x7);
  88. return ((cp[h >> 3] & mask) == mask);
  89. default:
  90. panic("ffs_isblock");
  91. break;
  92. }
  93. return (0);
  94. }
  95. /*
  96. * check if a block is free
  97. */
  98. int
  99. ffs_isfreeblock(Fs *fs, uint8_t *cp, ufs1_daddr_t h)
  100. {
  101. switch ((int)fs->fs_frag) {
  102. case 8:
  103. return (cp[h] == 0);
  104. case 4:
  105. return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
  106. case 2:
  107. return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
  108. case 1:
  109. return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
  110. default:
  111. panic("ffs_isfreeblock");
  112. break;
  113. }
  114. return (0);
  115. }
  116. /*
  117. * take a block out of the map
  118. */
  119. void
  120. ffs_clrblock(Fs *fs, uint8_t *cp, ufs1_daddr_t h)
  121. {
  122. switch ((int)fs->fs_frag) {
  123. case 8:
  124. cp[h] = 0;
  125. return;
  126. case 4:
  127. cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
  128. return;
  129. case 2:
  130. cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
  131. return;
  132. case 1:
  133. cp[h >> 3] &= ~(0x01 << (h & 0x7));
  134. return;
  135. default:
  136. panic("ffs_clrblock");
  137. break;
  138. }
  139. }
  140. /*
  141. * put a block into the map
  142. */
  143. void
  144. ffs_setblock(Fs *fs, uint8_t *cp, ufs1_daddr_t h)
  145. {
  146. switch ((int)fs->fs_frag) {
  147. case 8:
  148. cp[h] = 0xff;
  149. return;
  150. case 4:
  151. cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
  152. return;
  153. case 2:
  154. cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
  155. return;
  156. case 1:
  157. cp[h >> 3] |= (0x01 << (h & 0x7));
  158. return;
  159. default:
  160. panic("ffs_setblock");
  161. break;
  162. }
  163. }
  164. /*
  165. * Update the cluster map because of an allocation or free.
  166. *
  167. * Cnt == 1 means free; cnt == -1 means allocating.
  168. */
  169. void
  170. ffs_clusteracct(Fs *fs, Cg *cgp, ufs1_daddr_t blkno, int cnt)
  171. {
  172. int32_t *sump;
  173. int32_t *lp;
  174. uint8_t *freemapp, *mapp;
  175. int i, start, end, forw, back, map, bit;
  176. if (fs->fs_contigsumsize <= 0)
  177. return;
  178. freemapp = cg_clustersfree(cgp);
  179. sump = cg_clustersum(cgp);
  180. /*
  181. * Allocate or clear the actual block.
  182. */
  183. if (cnt > 0)
  184. setbit(freemapp, blkno);
  185. else
  186. clrbit(freemapp, blkno);
  187. /*
  188. * Find the size of the cluster going forward.
  189. */
  190. start = blkno + 1;
  191. end = start + fs->fs_contigsumsize;
  192. if (end >= cgp->cg_nclusterblks)
  193. end = cgp->cg_nclusterblks;
  194. mapp = &freemapp[start / NBBY];
  195. map = *mapp++;
  196. bit = 1 << (start % NBBY);
  197. for (i = start; i < end; i++) {
  198. if ((map & bit) == 0)
  199. break;
  200. if ((i & (NBBY - 1)) != (NBBY - 1)) {
  201. bit <<= 1;
  202. } else {
  203. map = *mapp++;
  204. bit = 1;
  205. }
  206. }
  207. forw = i - start;
  208. /*
  209. * Find the size of the cluster going backward.
  210. */
  211. start = blkno - 1;
  212. end = start - fs->fs_contigsumsize;
  213. if (end < 0)
  214. end = -1;
  215. mapp = &freemapp[start / NBBY];
  216. map = *mapp--;
  217. bit = 1 << (start % NBBY);
  218. for (i = start; i > end; i--) {
  219. if ((map & bit) == 0)
  220. break;
  221. if ((i & (NBBY - 1)) != 0) {
  222. bit >>= 1;
  223. } else {
  224. map = *mapp--;
  225. bit = 1 << (NBBY - 1);
  226. }
  227. }
  228. back = start - i;
  229. /*
  230. * Account for old cluster and the possibly new forward and
  231. * back clusters.
  232. */
  233. i = back + forw + 1;
  234. if (i > fs->fs_contigsumsize)
  235. i = fs->fs_contigsumsize;
  236. sump[i] += cnt;
  237. if (back > 0)
  238. sump[back] -= cnt;
  239. if (forw > 0)
  240. sump[forw] -= cnt;
  241. /*
  242. * Update cluster summary information.
  243. */
  244. lp = &sump[fs->fs_contigsumsize];
  245. for (i = fs->fs_contigsumsize; i > 0; i--)
  246. if (*lp-- > 0)
  247. break;
  248. fs->fs_maxcluster[cgp->cg_cgx] = i;
  249. }