12 #include <linux/stat.h>
13 #include <linux/time.h>
14 #include <linux/string.h>
16 #include <linux/capability.h>
17 #include <linux/bitops.h>
18 #include <asm/byteorder.h>
25 #define INVBLOCK ((u64)-1L)
27 static u64 ufs_add_fragments(
struct inode *,
u64,
unsigned,
unsigned,
int *);
28 static u64 ufs_alloc_fragments(
struct inode *,
unsigned,
u64,
unsigned,
int *);
31 static unsigned char ufs_fragtable_8fpb[], ufs_fragtable_other[];
44 unsigned cgno,
bit, end_bit, bbase, blkmap,
i;
48 uspi = UFS_SB(sb)->s_uspi;
51 UFSD(
"ENTER, fragment %llu, count %u\n",
52 (
unsigned long long)fragment, count);
55 ufs_error (sb,
"ufs_free_fragments",
"internal error");
59 cgno = ufs_dtog(uspi, fragment);
60 bit = ufs_dtogd(uspi, fragment);
61 if (cgno >= uspi->
s_ncg) {
62 ufs_panic (sb,
"ufs_free_fragments",
"freeing blocks are outside device");
71 ufs_panic (sb,
"ufs_free_fragments",
"internal error, bad magic number on cg %u", cgno);
75 end_bit = bit +
count;
78 ufs_fragacct (sb, blkmap, ucg->
cg_frsum, -1);
79 for (i = bit; i < end_bit; i++) {
84 "bit already cleared for fragment %u", i);
87 fs32_add(sb, &ucg->
cg_cs.cs_nffree, count);
91 ufs_fragacct(sb, blkmap, ucg->
cg_frsum, 1);
98 fs32_sub(sb, &ucg->
cg_cs.cs_nffree, uspi->
s_fpb);
100 fs32_sub(sb, &UFS_SB(sb)->
fs_cs(cgno).cs_nffree, uspi->
s_fpb);
102 ufs_clusteracct (sb, ucpi, blkno, 1);
103 fs32_add(sb, &ucg->
cg_cs.cs_nbfree, 1);
105 fs32_add(sb, &UFS_SB(sb)->
fs_cs(cgno).cs_nbfree, 1);
127 UFSD(
"EXIT (FAILED)\n");
145 uspi = UFS_SB(sb)->s_uspi;
148 UFSD(
"ENTER, fragment %llu, count %u\n",
149 (
unsigned long long)fragment, count);
152 ufs_error (sb,
"ufs_free_blocks",
"internal error, "
153 "fragment %llu, count %u\n",
154 (
unsigned long long)fragment, count);
162 cgno = ufs_dtog(uspi, fragment);
163 bit = ufs_dtogd(uspi, fragment);
164 if (cgno >= uspi->
s_ncg) {
165 ufs_panic (sb,
"ufs_free_blocks",
"freeing blocks are outside device");
168 end_bit = bit +
count;
169 if (end_bit > uspi->
s_fpg) {
170 overflow = bit + count - uspi->
s_fpg;
180 ufs_panic (sb,
"ufs_free_blocks",
"internal error, bad magic number on cg %u", cgno);
184 for (i = bit; i < end_bit; i += uspi->
s_fpb) {
187 ufs_error(sb,
"ufs_free_blocks",
"freeing free fragment");
191 ufs_clusteracct (sb, ucpi, blkno, 1);
193 fs32_add(sb, &ucg->
cg_cs.cs_nbfree, 1);
195 fs32_add(sb, &UFS_SB(sb)->
fs_cs(cgno).cs_nbfree, 1);
225 UFSD(
"EXIT (FAILED)\n");
243 const unsigned blks_per_page =
245 const unsigned mask = blks_per_page - 1;
248 unsigned pos,
j, lblock;
251 struct buffer_head *
head, *bh;
253 UFSD(
"ENTER, ino %lu, count %u, oldb %llu, newb %llu\n",
255 (
unsigned long long)oldb, (
unsigned long long)newb);
258 BUG_ON(!PageLocked(locked_page));
260 cur_index = locked_page->
index;
263 for (i = beg; i <
end; i = (i |
mask) + 1) {
266 if (
likely(cur_index != index)) {
272 "read of page %llu failed\n",
273 (
unsigned long long)index);
279 head = page_buffers(page);
282 for (j = 0; j <
pos; ++
j)
283 bh = bh->b_this_page;
289 lblock = blks_per_page;
296 if (!buffer_mapped(bh))
297 map_bh(bh, inode->
i_sb, oldb + pos);
298 if (!buffer_uptodate(bh)) {
301 if (!buffer_uptodate(bh)) {
303 "read of block failed\n");
308 UFSD(
" change from %llu to %llu, pos %u\n",
309 (
unsigned long long)(pos + oldb),
310 (
unsigned long long)(pos + newb), pos);
312 bh->b_blocknr = newb +
pos;
317 bh = bh->b_this_page;
318 }
while (bh != head);
320 if (
likely(cur_index != index))
321 ufs_put_locked_page(page);
326 static void ufs_clear_frags(
struct inode *inode,
sector_t beg,
unsigned int n,
329 struct buffer_head *bh;
332 for (; beg <
end; ++beg) {
333 bh = sb_getblk(inode->
i_sb, beg);
335 memset(bh->b_data, 0, inode->
i_sb->s_blocksize);
336 set_buffer_uptodate(bh);
346 u64 goal,
unsigned count,
int *
err,
347 struct page *locked_page)
352 unsigned cgno, oldcount, newcount;
355 UFSD(
"ENTER, ino %lu, fragment %llu, goal %llu, count %u\n",
356 inode->
i_ino, (
unsigned long long)fragment,
357 (
unsigned long long)goal, count);
360 uspi = UFS_SB(sb)->s_uspi;
365 tmp = ufs_data_ptr_to_cpu(sb, p);
368 ufs_warning(sb,
"ufs_new_fragments",
"internal warning"
369 " fragment %llu, count %u",
370 (
unsigned long long)fragment, count);
374 newcount = oldcount +
count;
381 ufs_error(sb,
"ufs_new_fragments",
"internal error, "
382 "fragment %llu, tmp %llu\n",
383 (
unsigned long long)fragment,
384 (
unsigned long long)tmp);
388 if (fragment < UFS_I(inode)->i_lastfrag) {
389 UFSD(
"EXIT (ALREADY ALLOCATED)\n");
396 UFSD(
"EXIT (ALREADY ALLOCATED)\n");
407 UFSD(
"EXIT (FAILED)\n");
416 cgno = ufs_dtog(uspi, goal);
422 result = ufs_alloc_fragments (inode, cgno, goal, count, err);
424 ufs_cpu_to_data_ptr(sb, p, result);
426 UFS_I(inode)->i_lastfrag =
427 max(UFS_I(inode)->i_lastfrag, fragment + count);
428 ufs_clear_frags(inode, result + oldcount,
429 newcount - oldcount, locked_page !=
NULL);
432 UFSD(
"EXIT, result %llu\n", (
unsigned long long)result);
439 result = ufs_add_fragments (inode, tmp, oldcount, newcount, err);
442 UFS_I(inode)->i_lastfrag =
max(UFS_I(inode)->i_lastfrag,
444 ufs_clear_frags(inode, result + oldcount, newcount - oldcount,
445 locked_page !=
NULL);
447 UFSD(
"EXIT, result %llu\n", (
unsigned long long)result);
454 switch (fs32_to_cpu(sb, usb1->
fs_optim)) {
466 request = uspi->
s_fpb;
473 result = ufs_alloc_fragments (inode, cgno, goal, request, err);
475 ufs_clear_frags(inode, result + oldcount, newcount - oldcount,
476 locked_page !=
NULL);
477 ufs_change_blocknr(inode, fragment - oldcount, oldcount,
479 uspi->
s_sbbase + result, locked_page);
480 ufs_cpu_to_data_ptr(sb, p, result);
482 UFS_I(inode)->i_lastfrag =
max(UFS_I(inode)->i_lastfrag,
485 if (newcount < request)
488 UFSD(
"EXIT, result %llu\n", (
unsigned long long)result);
493 UFSD(
"EXIT (FAILED)\n");
497 static u64 ufs_add_fragments(
struct inode *inode,
u64 fragment,
498 unsigned oldcount,
unsigned newcount,
int *
err)
505 unsigned cgno, fragno, fragoff,
count, fragsize,
i;
507 UFSD(
"ENTER, fragment %llu, oldcount %u, newcount %u\n",
508 (
unsigned long long)fragment, oldcount, newcount);
511 uspi = UFS_SB(sb)->s_uspi;
513 count = newcount - oldcount;
515 cgno = ufs_dtog(uspi, fragment);
526 "internal error, bad magic number on cg %u", cgno);
530 fragno = ufs_dtogd(uspi, fragment);
532 for (i = oldcount; i < newcount; i++)
539 for (i = newcount; i < (uspi->
s_fpb - fragoff); i++)
542 fragsize = i - oldcount;
543 if (!fs32_to_cpu(sb, ucg->
cg_frsum[fragsize]))
545 "internal error or corrupted bitmap on cg %u", cgno);
546 fs32_sub(sb, &ucg->
cg_frsum[fragsize], 1);
547 if (fragsize != count)
548 fs32_add(sb, &ucg->
cg_frsum[fragsize - count], 1);
549 for (i = oldcount; i < newcount; i++)
552 fs32_sub(sb, &ucg->
cg_cs.cs_nffree, count);
553 fs32_sub(sb, &UFS_SB(sb)->
fs_cs(cgno).cs_nffree, count);
562 UFSD(
"EXIT, fragment %llu\n", (
unsigned long long)fragment);
567 #define UFS_TEST_FREE_SPACE_CG \
568 ucg = (struct ufs_cylinder_group *) UFS_SB(sb)->s_ucg[cgno]->b_data; \
569 if (fs32_to_cpu(sb, ucg->cg_cs.cs_nbfree)) \
571 for (k = count; k < uspi->s_fpb; k++) \
572 if (fs32_to_cpu(sb, ucg->cg_frsum[k])) \
575 static u64 ufs_alloc_fragments(
struct inode *inode,
unsigned cgno,
576 u64 goal,
unsigned count,
int *err)
583 unsigned oldcg,
i,
j,
k, allocsize;
586 UFSD(
"ENTER, ino %lu, cgno %u, goal %llu, count %u\n",
587 inode->
i_ino, cgno, (
unsigned long long)goal, count);
590 uspi = UFS_SB(sb)->s_uspi;
602 for (j = 1; j < uspi->
s_ncg; j *= 2) {
604 if (cgno >= uspi->
s_ncg)
613 cgno = (oldcg + 1) % uspi->
s_ncg;
616 if (cgno >= uspi->
s_ncg)
621 UFSD(
"EXIT (FAILED)\n");
631 "internal error, bad magic number on cg %u", cgno);
634 if (count == uspi->
s_fpb) {
635 result = ufs_alloccg_block (inode, ucpi, goal, err);
641 for (allocsize = count; allocsize < uspi->
s_fpb; allocsize++)
642 if (fs32_to_cpu(sb, ucg->
cg_frsum[allocsize]) != 0)
645 if (allocsize == uspi->
s_fpb) {
646 result = ufs_alloccg_block (inode, ucpi, goal, err);
649 goal = ufs_dtogd(uspi, result);
650 for (i = count; i < uspi->
s_fpb; i++)
654 fs32_add(sb, &ucg->
cg_cs.cs_nffree, i);
656 fs32_add(sb, &UFS_SB(sb)->
fs_cs(cgno).cs_nffree, i);
661 result = ufs_bitmap_search (sb, ucpi, goal, allocsize);
664 for (i = 0; i <
count; i++)
667 fs32_sub(sb, &ucg->
cg_cs.cs_nffree, count);
669 fs32_sub(sb, &UFS_SB(sb)->
fs_cs(cgno).cs_nffree, count);
670 fs32_sub(sb, &ucg->
cg_frsum[allocsize], 1);
672 if (count != allocsize)
673 fs32_add(sb, &ucg->
cg_frsum[allocsize - count], 1);
682 result += cgno * uspi->
s_fpg;
683 UFSD(
"EXIT3, result %llu\n", (
unsigned long long)result);
687 static u64 ufs_alloccg_block(
struct inode *inode,
697 UFSD(
"ENTER, goal %llu\n", (
unsigned long long)goal);
700 uspi = UFS_SB(sb)->s_uspi;
709 goal = ufs_dtogd(uspi, goal);
720 result = ufs_bitmap_search (sb, ucpi, goal, uspi->
s_fpb);
728 ufs_clusteracct (sb, ucpi, blkno, -1);
730 fs32_sub(sb, &ucg->
cg_cs.cs_nbfree, 1);
732 fs32_sub(sb, &UFS_SB(sb)->
fs_cs(ucpi->
c_cgx).cs_nbfree, 1);
742 UFSD(
"EXIT, result %llu\n", (
unsigned long long)result);
749 unsigned begin,
unsigned size,
750 unsigned char *
table,
unsigned char mask)
756 offset = begin & ~uspi->
s_fmask;
759 if ((offset + size) < uspi->
s_fsize)
764 cp = ubh->
bh[begin]->b_data +
offset;
765 while ((table[*cp++] & mask) == 0 && --rest)
772 return (size + rest);
784 u64 goal,
unsigned count)
790 static const int mask_arr[9] = {
791 0x3, 0x7, 0xf, 0x1f, 0x3f, 0x7f, 0xff, 0x1ff, 0x3ff
793 static const int want_arr[9] = {
794 0x0, 0x2, 0x6, 0xe, 0x1e, 0x3e, 0x7e, 0xfe, 0x1fe
803 UFSD(
"ENTER, cg %u, goal %llu, count %u\n", ucpi->
c_cgx,
804 (
unsigned long long)goal, count);
810 start = ufs_dtogd(uspi, goal) >> 3;
815 loc = ubh_scanc(uspi, UCPI_UBH(ucpi), ucpi->
c_freeoff + start, length,
816 (uspi->
s_fpb == 8) ? ufs_fragtable_8fpb : ufs_fragtable_other,
817 1 << (count - 1 + (uspi->
s_fpb & 7)));
820 loc = ubh_scanc(uspi, UCPI_UBH(ucpi), ucpi->
c_freeoff, length,
821 (uspi->
s_fpb == 8) ? ufs_fragtable_8fpb :
823 1 << (count - 1 + (uspi->
s_fpb & 7)));
826 "bitmap corrupted on cg %u, start %u,"
827 " length %u, count %u, freeoff %u\n",
828 ucpi->
c_cgx, start, length, count,
834 result = (start + length -
loc) << 3;
841 for (end = result + 8; result <
end; result += uspi->
s_fpb) {
844 mask = mask_arr[
count];
845 want = want_arr[
count];
846 for (pos = 0; pos <= uspi->
s_fpb -
count; pos++) {
847 if ((blockmap & mask) == want) {
848 UFSD(
"EXIT, result %llu\n",
849 (
unsigned long long)result);
857 ufs_error(sb,
"ufs_bitmap_search",
"block not in map on cg %u\n",
859 UFSD(
"EXIT (FAILED)\n");
863 static void ufs_clusteracct(
struct super_block * sb,
869 uspi = UFS_SB(sb)->s_uspi;
917 static unsigned char ufs_fragtable_8fpb[] = {
918 0x00, 0x01, 0x01, 0x02, 0x01, 0x01, 0x02, 0x04, 0x01, 0x01, 0x01, 0x03, 0x02, 0x03, 0x04, 0x08,
919 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x02, 0x03, 0x03, 0x02, 0x04, 0x05, 0x08, 0x10,
920 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x01, 0x01, 0x01, 0x03, 0x03, 0x03, 0x05, 0x09,
921 0x02, 0x03, 0x03, 0x02, 0x03, 0x03, 0x02, 0x06, 0x04, 0x05, 0x05, 0x06, 0x08, 0x09, 0x10, 0x20,
922 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x01, 0x01, 0x01, 0x03, 0x03, 0x03, 0x05, 0x09,
923 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x03, 0x03, 0x03, 0x03, 0x05, 0x05, 0x09, 0x11,
924 0x02, 0x03, 0x03, 0x02, 0x03, 0x03, 0x02, 0x06, 0x03, 0x03, 0x03, 0x03, 0x02, 0x03, 0x06, 0x0A,
925 0x04, 0x05, 0x05, 0x06, 0x05, 0x05, 0x06, 0x04, 0x08, 0x09, 0x09, 0x0A, 0x10, 0x11, 0x20, 0x40,
926 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x01, 0x01, 0x01, 0x03, 0x03, 0x03, 0x05, 0x09,
927 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x03, 0x03, 0x03, 0x03, 0x05, 0x05, 0x09, 0x11,
928 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x01, 0x01, 0x01, 0x03, 0x03, 0x03, 0x05, 0x09,
929 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x07, 0x05, 0x05, 0x05, 0x07, 0x09, 0x09, 0x11, 0x21,
930 0x02, 0x03, 0x03, 0x02, 0x03, 0x03, 0x02, 0x06, 0x03, 0x03, 0x03, 0x03, 0x02, 0x03, 0x06, 0x0A,
931 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x07, 0x02, 0x03, 0x03, 0x02, 0x06, 0x07, 0x0A, 0x12,
932 0x04, 0x05, 0x05, 0x06, 0x05, 0x05, 0x06, 0x04, 0x05, 0x05, 0x05, 0x07, 0x06, 0x07, 0x04, 0x0C,
933 0x08, 0x09, 0x09, 0x0A, 0x09, 0x09, 0x0A, 0x0C, 0x10, 0x11, 0x11, 0x12, 0x20, 0x21, 0x40, 0x80,
936 static unsigned char ufs_fragtable_other[] = {
937 0x00, 0x16, 0x16, 0x2A, 0x16, 0x16, 0x26, 0x4E, 0x16, 0x16, 0x16, 0x3E, 0x2A, 0x3E, 0x4E, 0x8A,
938 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
939 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
940 0x2A, 0x3E, 0x3E, 0x2A, 0x3E, 0x3E, 0x2E, 0x6E, 0x3E, 0x3E, 0x3E, 0x3E, 0x2A, 0x3E, 0x6E, 0xAA,
941 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
942 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
943 0x26, 0x36, 0x36, 0x2E, 0x36, 0x36, 0x26, 0x6E, 0x36, 0x36, 0x36, 0x3E, 0x2E, 0x3E, 0x6E, 0xAE,
944 0x4E, 0x5E, 0x5E, 0x6E, 0x5E, 0x5E, 0x6E, 0x4E, 0x5E, 0x5E, 0x5E, 0x7E, 0x6E, 0x7E, 0x4E, 0xCE,
945 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
946 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
947 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
948 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x7E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x7E, 0xBE,
949 0x2A, 0x3E, 0x3E, 0x2A, 0x3E, 0x3E, 0x2E, 0x6E, 0x3E, 0x3E, 0x3E, 0x3E, 0x2A, 0x3E, 0x6E, 0xAA,
950 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x7E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x7E, 0xBE,
951 0x4E, 0x5E, 0x5E, 0x6E, 0x5E, 0x5E, 0x6E, 0x4E, 0x5E, 0x5E, 0x5E, 0x7E, 0x6E, 0x7E, 0x4E, 0xCE,
952 0x8A, 0x9E, 0x9E, 0xAA, 0x9E, 0x9E, 0xAE, 0xCE, 0x9E, 0x9E, 0x9E, 0xBE, 0xAA, 0xBE, 0xCE, 0x8A,