libufs_subr.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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/ufsdat.h>
  34. #include <ufs/freebsd_util.h>
  35. #include <ufs/ufsdat.h>
  36. #include <ufs/quota.h>
  37. #include <ufs/fs.h>
  38. #include <ufs/inode.h>
  39. void panic(char*, ...);
  40. /*
  41. * Update the frsum fields to reflect addition or deletion
  42. * of some frags.
  43. */
  44. void
  45. ffs_fragacct(Fs *fs, int fragmap, uint32_t fraglist[], int cnt)
  46. {
  47. int inblk;
  48. int field, subfield;
  49. int siz, pos;
  50. inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
  51. fragmap <<= 1;
  52. for (siz = 1; siz < fs->fs_frag; siz++) {
  53. if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
  54. continue;
  55. field = around[siz];
  56. subfield = inside[siz];
  57. for (pos = siz; pos <= fs->fs_frag; pos++) {
  58. if ((fragmap & field) == subfield) {
  59. fraglist[siz] += cnt;
  60. pos += siz;
  61. field <<= siz;
  62. subfield <<= siz;
  63. }
  64. field <<= 1;
  65. subfield <<= 1;
  66. }
  67. }
  68. }
  69. /*
  70. * block operations
  71. *
  72. * check if a block is available
  73. */
  74. int
  75. ffs_isblock(Fs *fs, unsigned char *cp, ufs1_daddr_t h)
  76. {
  77. unsigned char mask;
  78. switch ((int)fs->fs_frag) {
  79. case 8:
  80. return (cp[h] == 0xff);
  81. case 4:
  82. mask = 0x0f << ((h & 0x1) << 2);
  83. return ((cp[h >> 1] & mask) == mask);
  84. case 2:
  85. mask = 0x03 << ((h & 0x3) << 1);
  86. return ((cp[h >> 2] & mask) == mask);
  87. case 1:
  88. mask = 0x01 << (h & 0x7);
  89. return ((cp[h >> 3] & mask) == mask);
  90. default:
  91. panic("ffs_isblock");
  92. break;
  93. }
  94. return (0);
  95. }
  96. /*
  97. * check if a block is free
  98. */
  99. int
  100. ffs_isfreeblock(Fs *fs, uint8_t *cp, ufs1_daddr_t h)
  101. {
  102. switch ((int)fs->fs_frag) {
  103. case 8:
  104. return (cp[h] == 0);
  105. case 4:
  106. return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
  107. case 2:
  108. return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
  109. case 1:
  110. return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
  111. default:
  112. panic("ffs_isfreeblock");
  113. break;
  114. }
  115. return (0);
  116. }
  117. /*
  118. * take a block out of the map
  119. */
  120. void
  121. ffs_clrblock(Fs *fs, uint8_t *cp, ufs1_daddr_t h)
  122. {
  123. switch ((int)fs->fs_frag) {
  124. case 8:
  125. cp[h] = 0;
  126. return;
  127. case 4:
  128. cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
  129. return;
  130. case 2:
  131. cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
  132. return;
  133. case 1:
  134. cp[h >> 3] &= ~(0x01 << (h & 0x7));
  135. return;
  136. default:
  137. panic("ffs_clrblock");
  138. break;
  139. }
  140. }
  141. /*
  142. * put a block into the map
  143. */
  144. void
  145. ffs_setblock(Fs *fs, uint8_t *cp, ufs1_daddr_t h)
  146. {
  147. switch ((int)fs->fs_frag) {
  148. case 8:
  149. cp[h] = 0xff;
  150. return;
  151. case 4:
  152. cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
  153. return;
  154. case 2:
  155. cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
  156. return;
  157. case 1:
  158. cp[h >> 3] |= (0x01 << (h & 0x7));
  159. return;
  160. default:
  161. panic("ffs_setblock");
  162. break;
  163. }
  164. }
  165. /*
  166. * Update the cluster map because of an allocation or free.
  167. *
  168. * Cnt == 1 means free; cnt == -1 means allocating.
  169. */
  170. void
  171. ffs_clusteracct(Fs *fs, Cg *cgp, ufs1_daddr_t blkno, int cnt)
  172. {
  173. int32_t *sump;
  174. int32_t *lp;
  175. uint8_t *freemapp, *mapp;
  176. int i, start, end, forw, back, map, bit;
  177. if (fs->fs_contigsumsize <= 0)
  178. return;
  179. freemapp = cg_clustersfree(cgp);
  180. sump = cg_clustersum(cgp);
  181. /*
  182. * Allocate or clear the actual block.
  183. */
  184. if (cnt > 0)
  185. setbit(freemapp, blkno);
  186. else
  187. clrbit(freemapp, blkno);
  188. /*
  189. * Find the size of the cluster going forward.
  190. */
  191. start = blkno + 1;
  192. end = start + fs->fs_contigsumsize;
  193. if (end >= cgp->cg_nclusterblks)
  194. end = cgp->cg_nclusterblks;
  195. mapp = &freemapp[start / NBBY];
  196. map = *mapp++;
  197. bit = 1 << (start % NBBY);
  198. for (i = start; i < end; i++) {
  199. if ((map & bit) == 0)
  200. break;
  201. if ((i & (NBBY - 1)) != (NBBY - 1)) {
  202. bit <<= 1;
  203. } else {
  204. map = *mapp++;
  205. bit = 1;
  206. }
  207. }
  208. forw = i - start;
  209. /*
  210. * Find the size of the cluster going backward.
  211. */
  212. start = blkno - 1;
  213. end = start - fs->fs_contigsumsize;
  214. if (end < 0)
  215. end = -1;
  216. mapp = &freemapp[start / NBBY];
  217. map = *mapp--;
  218. bit = 1 << (start % NBBY);
  219. for (i = start; i > end; i--) {
  220. if ((map & bit) == 0)
  221. break;
  222. if ((i & (NBBY - 1)) != 0) {
  223. bit >>= 1;
  224. } else {
  225. map = *mapp--;
  226. bit = 1 << (NBBY - 1);
  227. }
  228. }
  229. back = start - i;
  230. /*
  231. * Account for old cluster and the possibly new forward and
  232. * back clusters.
  233. */
  234. i = back + forw + 1;
  235. if (i > fs->fs_contigsumsize)
  236. i = fs->fs_contigsumsize;
  237. sump[i] += cnt;
  238. if (back > 0)
  239. sump[back] -= cnt;
  240. if (forw > 0)
  241. sump[forw] -= cnt;
  242. /*
  243. * Update cluster summary information.
  244. */
  245. lp = &sump[fs->fs_contigsumsize];
  246. for (i = fs->fs_contigsumsize; i > 0; i--)
  247. if (*lp-- > 0)
  248. break;
  249. fs->fs_maxcluster[cgp->cg_cgx] = i;
  250. }