buffer.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * linux/fs/hpfs/buffer.c
  3. *
  4. * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
  5. *
  6. * general buffer i/o
  7. */
  8. #include <linux/sched.h>
  9. #include <linux/slab.h>
  10. #include "hpfs_fn.h"
  11. void hpfs_lock_creation(struct super_block *s)
  12. {
  13. #ifdef DEBUG_LOCKS
  14. printk("lock creation\n");
  15. #endif
  16. mutex_lock(&hpfs_sb(s)->hpfs_creation_de);
  17. }
  18. void hpfs_unlock_creation(struct super_block *s)
  19. {
  20. #ifdef DEBUG_LOCKS
  21. printk("unlock creation\n");
  22. #endif
  23. mutex_unlock(&hpfs_sb(s)->hpfs_creation_de);
  24. }
  25. /* Map a sector into a buffer and return pointers to it and to the buffer. */
  26. void *hpfs_map_sector(struct super_block *s, unsigned secno, struct buffer_head **bhp,
  27. int ahead)
  28. {
  29. struct buffer_head *bh;
  30. hpfs_lock_assert(s);
  31. cond_resched();
  32. *bhp = bh = sb_bread(s, secno);
  33. if (bh != NULL)
  34. return bh->b_data;
  35. else {
  36. printk("HPFS: hpfs_map_sector: read error\n");
  37. return NULL;
  38. }
  39. }
  40. /* Like hpfs_map_sector but don't read anything */
  41. void *hpfs_get_sector(struct super_block *s, unsigned secno, struct buffer_head **bhp)
  42. {
  43. struct buffer_head *bh;
  44. /*return hpfs_map_sector(s, secno, bhp, 0);*/
  45. hpfs_lock_assert(s);
  46. cond_resched();
  47. if ((*bhp = bh = sb_getblk(s, secno)) != NULL) {
  48. if (!buffer_uptodate(bh)) wait_on_buffer(bh);
  49. set_buffer_uptodate(bh);
  50. return bh->b_data;
  51. } else {
  52. printk("HPFS: hpfs_get_sector: getblk failed\n");
  53. return NULL;
  54. }
  55. }
  56. /* Map 4 sectors into a 4buffer and return pointers to it and to the buffer. */
  57. void *hpfs_map_4sectors(struct super_block *s, unsigned secno, struct quad_buffer_head *qbh,
  58. int ahead)
  59. {
  60. struct buffer_head *bh;
  61. char *data;
  62. hpfs_lock_assert(s);
  63. cond_resched();
  64. if (secno & 3) {
  65. printk("HPFS: hpfs_map_4sectors: unaligned read\n");
  66. return NULL;
  67. }
  68. qbh->data = data = kmalloc(2048, GFP_NOFS);
  69. if (!data) {
  70. printk("HPFS: hpfs_map_4sectors: out of memory\n");
  71. goto bail;
  72. }
  73. qbh->bh[0] = bh = sb_bread(s, secno);
  74. if (!bh)
  75. goto bail0;
  76. memcpy(data, bh->b_data, 512);
  77. qbh->bh[1] = bh = sb_bread(s, secno + 1);
  78. if (!bh)
  79. goto bail1;
  80. memcpy(data + 512, bh->b_data, 512);
  81. qbh->bh[2] = bh = sb_bread(s, secno + 2);
  82. if (!bh)
  83. goto bail2;
  84. memcpy(data + 2 * 512, bh->b_data, 512);
  85. qbh->bh[3] = bh = sb_bread(s, secno + 3);
  86. if (!bh)
  87. goto bail3;
  88. memcpy(data + 3 * 512, bh->b_data, 512);
  89. return data;
  90. bail3:
  91. brelse(qbh->bh[2]);
  92. bail2:
  93. brelse(qbh->bh[1]);
  94. bail1:
  95. brelse(qbh->bh[0]);
  96. bail0:
  97. kfree(data);
  98. printk("HPFS: hpfs_map_4sectors: read error\n");
  99. bail:
  100. return NULL;
  101. }
  102. /* Don't read sectors */
  103. void *hpfs_get_4sectors(struct super_block *s, unsigned secno,
  104. struct quad_buffer_head *qbh)
  105. {
  106. cond_resched();
  107. hpfs_lock_assert(s);
  108. if (secno & 3) {
  109. printk("HPFS: hpfs_get_4sectors: unaligned read\n");
  110. return NULL;
  111. }
  112. /*return hpfs_map_4sectors(s, secno, qbh, 0);*/
  113. if (!(qbh->data = kmalloc(2048, GFP_NOFS))) {
  114. printk("HPFS: hpfs_get_4sectors: out of memory\n");
  115. return NULL;
  116. }
  117. if (!(hpfs_get_sector(s, secno, &qbh->bh[0]))) goto bail0;
  118. if (!(hpfs_get_sector(s, secno + 1, &qbh->bh[1]))) goto bail1;
  119. if (!(hpfs_get_sector(s, secno + 2, &qbh->bh[2]))) goto bail2;
  120. if (!(hpfs_get_sector(s, secno + 3, &qbh->bh[3]))) goto bail3;
  121. memcpy(qbh->data, qbh->bh[0]->b_data, 512);
  122. memcpy(qbh->data + 512, qbh->bh[1]->b_data, 512);
  123. memcpy(qbh->data + 2*512, qbh->bh[2]->b_data, 512);
  124. memcpy(qbh->data + 3*512, qbh->bh[3]->b_data, 512);
  125. return qbh->data;
  126. bail3: brelse(qbh->bh[2]);
  127. bail2: brelse(qbh->bh[1]);
  128. bail1: brelse(qbh->bh[0]);
  129. bail0:
  130. return NULL;
  131. }
  132. void hpfs_brelse4(struct quad_buffer_head *qbh)
  133. {
  134. brelse(qbh->bh[3]);
  135. brelse(qbh->bh[2]);
  136. brelse(qbh->bh[1]);
  137. brelse(qbh->bh[0]);
  138. kfree(qbh->data);
  139. }
  140. void hpfs_mark_4buffers_dirty(struct quad_buffer_head *qbh)
  141. {
  142. PRINTK(("hpfs_mark_4buffers_dirty\n"));
  143. memcpy(qbh->bh[0]->b_data, qbh->data, 512);
  144. memcpy(qbh->bh[1]->b_data, qbh->data + 512, 512);
  145. memcpy(qbh->bh[2]->b_data, qbh->data + 2 * 512, 512);
  146. memcpy(qbh->bh[3]->b_data, qbh->data + 3 * 512, 512);
  147. mark_buffer_dirty(qbh->bh[0]);
  148. mark_buffer_dirty(qbh->bh[1]);
  149. mark_buffer_dirty(qbh->bh[2]);
  150. mark_buffer_dirty(qbh->bh[3]);
  151. }