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 
9 #include <linux/string.h>
10 #include "hpfs_fn.h"
11 
hpfs_lock_creation(struct super_block * s)12 void hpfs_lock_creation(struct super_block *s)
13 {
14 #ifdef DEBUG_LOCKS
15 	printk("lock creation\n");
16 #endif
17 	while (s->s_hpfs_creation_de_lock) sleep_on(&s->s_hpfs_creation_de);
18 	s->s_hpfs_creation_de_lock = 1;
19 }
20 
hpfs_unlock_creation(struct super_block * s)21 void hpfs_unlock_creation(struct super_block *s)
22 {
23 #ifdef DEBUG_LOCKS
24 	printk("unlock creation\n");
25 #endif
26 	s->s_hpfs_creation_de_lock = 0;
27 	wake_up(&s->s_hpfs_creation_de);
28 }
29 
hpfs_lock_iget(struct super_block * s,int mode)30 void hpfs_lock_iget(struct super_block *s, int mode)
31 {
32 #ifdef DEBUG_LOCKS
33 	printk("lock iget\n");
34 #endif
35 	while (s->s_hpfs_rd_inode) sleep_on(&s->s_hpfs_iget_q);
36 	s->s_hpfs_rd_inode = mode;
37 }
38 
hpfs_unlock_iget(struct super_block * s)39 void hpfs_unlock_iget(struct super_block *s)
40 {
41 #ifdef DEBUG_LOCKS
42 	printk("unlock iget\n");
43 #endif
44 	s->s_hpfs_rd_inode = 0;
45 	wake_up(&s->s_hpfs_iget_q);
46 }
47 
hpfs_lock_inode(struct inode * i)48 void hpfs_lock_inode(struct inode *i)
49 {
50 	if (i) down(&i->i_hpfs_sem);
51 }
52 
hpfs_unlock_inode(struct inode * i)53 void hpfs_unlock_inode(struct inode *i)
54 {
55 	if (i) up(&i->i_hpfs_sem);
56 }
57 
hpfs_lock_2inodes(struct inode * i1,struct inode * i2)58 void hpfs_lock_2inodes(struct inode *i1, struct inode *i2)
59 {
60 	if (!i1) { if (i2) down(&i2->i_hpfs_sem); return; }
61 	if (!i2) { if (i1) down(&i1->i_hpfs_sem); return; }
62 	if (i1->i_ino < i2->i_ino) {
63 		down(&i1->i_hpfs_sem);
64 		down(&i2->i_hpfs_sem);
65 	} else if (i1->i_ino > i2->i_ino) {
66 		down(&i2->i_hpfs_sem);
67 		down(&i1->i_hpfs_sem);
68 	} else down(&i1->i_hpfs_sem);
69 }
70 
hpfs_unlock_2inodes(struct inode * i1,struct inode * i2)71 void hpfs_unlock_2inodes(struct inode *i1, struct inode *i2)
72 {
73 	if (!i1) { if (i2) up(&i2->i_hpfs_sem); return; }
74 	if (!i2) { if (i1) up(&i1->i_hpfs_sem); return; }
75 	if (i1->i_ino < i2->i_ino) {
76 		up(&i2->i_hpfs_sem);
77 		up(&i1->i_hpfs_sem);
78 	} else if (i1->i_ino > i2->i_ino) {
79 		up(&i1->i_hpfs_sem);
80 		up(&i2->i_hpfs_sem);
81 	} else up(&i1->i_hpfs_sem);
82 }
83 
hpfs_lock_3inodes(struct inode * i1,struct inode * i2,struct inode * i3)84 void hpfs_lock_3inodes(struct inode *i1, struct inode *i2, struct inode *i3)
85 {
86 	if (!i1) { hpfs_lock_2inodes(i2, i3); return; }
87 	if (!i2) { hpfs_lock_2inodes(i1, i3); return; }
88 	if (!i3) { hpfs_lock_2inodes(i1, i2); return; }
89 	if (i1->i_ino < i2->i_ino && i1->i_ino < i3->i_ino) {
90 		down(&i1->i_hpfs_sem);
91 		hpfs_lock_2inodes(i2, i3);
92 	} else if (i2->i_ino < i1->i_ino && i2->i_ino < i3->i_ino) {
93 		down(&i2->i_hpfs_sem);
94 		hpfs_lock_2inodes(i1, i3);
95 	} else if (i3->i_ino < i1->i_ino && i3->i_ino < i2->i_ino) {
96 		down(&i3->i_hpfs_sem);
97 		hpfs_lock_2inodes(i1, i2);
98 	} else if (i1->i_ino != i2->i_ino) hpfs_lock_2inodes(i1, i2);
99 	else hpfs_lock_2inodes(i1, i3);
100 }
101 
hpfs_unlock_3inodes(struct inode * i1,struct inode * i2,struct inode * i3)102 void hpfs_unlock_3inodes(struct inode *i1, struct inode *i2, struct inode *i3)
103 {
104 	if (!i1) { hpfs_unlock_2inodes(i2, i3); return; }
105 	if (!i2) { hpfs_unlock_2inodes(i1, i3); return; }
106 	if (!i3) { hpfs_unlock_2inodes(i1, i2); return; }
107 	if (i1->i_ino < i2->i_ino && i1->i_ino < i3->i_ino) {
108 		hpfs_unlock_2inodes(i2, i3);
109 		up(&i1->i_hpfs_sem);
110 	} else if (i2->i_ino < i1->i_ino && i2->i_ino < i3->i_ino) {
111 		hpfs_unlock_2inodes(i1, i3);
112 		up(&i2->i_hpfs_sem);
113 	} else if (i3->i_ino < i1->i_ino && i3->i_ino < i2->i_ino) {
114 		hpfs_unlock_2inodes(i1, i2);
115 		up(&i3->i_hpfs_sem);
116 	} else if (i1->i_ino != i2->i_ino) hpfs_unlock_2inodes(i1, i2);
117 	else hpfs_unlock_2inodes(i1, i3);
118 }
119 
120 /* Map a sector into a buffer and return pointers to it and to the buffer. */
121 
hpfs_map_sector(struct super_block * s,unsigned secno,struct buffer_head ** bhp,int ahead)122 void *hpfs_map_sector(struct super_block *s, unsigned secno, struct buffer_head **bhp,
123 		 int ahead)
124 {
125 	struct buffer_head *bh;
126 
127 	cond_resched();
128 
129 	*bhp = bh = sb_bread(s, secno);
130 	if (bh != NULL)
131 		return bh->b_data;
132 	else {
133 		printk("HPFS: hpfs_map_sector: read error\n");
134 		return NULL;
135 	}
136 }
137 
138 /* Like hpfs_map_sector but don't read anything */
139 
hpfs_get_sector(struct super_block * s,unsigned secno,struct buffer_head ** bhp)140 void *hpfs_get_sector(struct super_block *s, unsigned secno, struct buffer_head **bhp)
141 {
142 	struct buffer_head *bh;
143 	/*return hpfs_map_sector(s, secno, bhp, 0);*/
144 
145 	cond_resched();
146 
147 	if ((*bhp = bh = sb_getblk(s, secno)) != NULL) {
148 		if (!buffer_uptodate(bh)) wait_on_buffer(bh);
149 		mark_buffer_uptodate(bh, 1);
150 		return bh->b_data;
151 	} else {
152 		printk("HPFS: hpfs_get_sector: getblk failed\n");
153 		return NULL;
154 	}
155 }
156 
157 /* Map 4 sectors into a 4buffer and return pointers to it and to the buffer. */
158 
hpfs_map_4sectors(struct super_block * s,unsigned secno,struct quad_buffer_head * qbh,int ahead)159 void *hpfs_map_4sectors(struct super_block *s, unsigned secno, struct quad_buffer_head *qbh,
160 		   int ahead)
161 {
162 	struct buffer_head *bh;
163 	char *data;
164 
165 	cond_resched();
166 
167 	if (secno & 3) {
168 		printk("HPFS: hpfs_map_4sectors: unaligned read\n");
169 		return 0;
170 	}
171 
172 	qbh->data = data = (char *)kmalloc(2048, GFP_KERNEL);
173 	if (!data) {
174 		printk("HPFS: hpfs_map_4sectors: out of memory\n");
175 		goto bail;
176 	}
177 
178 	qbh->bh[0] = bh = sb_bread(s, secno);
179 	if (!bh)
180 		goto bail0;
181 	memcpy(data, bh->b_data, 512);
182 
183 	qbh->bh[1] = bh = sb_bread(s, secno + 1);
184 	if (!bh)
185 		goto bail1;
186 	memcpy(data + 512, bh->b_data, 512);
187 
188 	qbh->bh[2] = bh = sb_bread(s, secno + 2);
189 	if (!bh)
190 		goto bail2;
191 	memcpy(data + 2 * 512, bh->b_data, 512);
192 
193 	qbh->bh[3] = bh = sb_bread(s, secno + 3);
194 	if (!bh)
195 		goto bail3;
196 	memcpy(data + 3 * 512, bh->b_data, 512);
197 
198 	return data;
199 
200  bail3:
201 	brelse(qbh->bh[2]);
202  bail2:
203 	brelse(qbh->bh[1]);
204  bail1:
205 	brelse(qbh->bh[0]);
206  bail0:
207 	kfree(data);
208 	printk("HPFS: hpfs_map_4sectors: read error\n");
209  bail:
210 	return NULL;
211 }
212 
213 /* Don't read sectors */
214 
hpfs_get_4sectors(struct super_block * s,unsigned secno,struct quad_buffer_head * qbh)215 void *hpfs_get_4sectors(struct super_block *s, unsigned secno,
216                           struct quad_buffer_head *qbh)
217 {
218 	cond_resched();
219 
220 	if (secno & 3) {
221 		printk("HPFS: hpfs_get_4sectors: unaligned read\n");
222 		return 0;
223 	}
224 
225 	/*return hpfs_map_4sectors(s, secno, qbh, 0);*/
226 	if (!(qbh->data = kmalloc(2048, GFP_KERNEL))) {
227 		printk("HPFS: hpfs_get_4sectors: out of memory\n");
228 		return NULL;
229 	}
230 	if (!(hpfs_get_sector(s, secno, &qbh->bh[0]))) goto bail0;
231 	if (!(hpfs_get_sector(s, secno + 1, &qbh->bh[1]))) goto bail1;
232 	if (!(hpfs_get_sector(s, secno + 2, &qbh->bh[2]))) goto bail2;
233 	if (!(hpfs_get_sector(s, secno + 3, &qbh->bh[3]))) goto bail3;
234 	memcpy(qbh->data, qbh->bh[0]->b_data, 512);
235 	memcpy(qbh->data + 512, qbh->bh[1]->b_data, 512);
236 	memcpy(qbh->data + 2*512, qbh->bh[2]->b_data, 512);
237 	memcpy(qbh->data + 3*512, qbh->bh[3]->b_data, 512);
238 	return qbh->data;
239 
240 	bail3:	brelse(qbh->bh[2]);
241 	bail2:	brelse(qbh->bh[1]);
242 	bail1:	brelse(qbh->bh[0]);
243 	bail0:
244 	return NULL;
245 }
246 
247 
hpfs_brelse4(struct quad_buffer_head * qbh)248 void hpfs_brelse4(struct quad_buffer_head *qbh)
249 {
250 	brelse(qbh->bh[3]);
251 	brelse(qbh->bh[2]);
252 	brelse(qbh->bh[1]);
253 	brelse(qbh->bh[0]);
254 	kfree(qbh->data);
255 }
256 
hpfs_mark_4buffers_dirty(struct quad_buffer_head * qbh)257 void hpfs_mark_4buffers_dirty(struct quad_buffer_head *qbh)
258 {
259 	PRINTK(("hpfs_mark_4buffers_dirty\n"));
260 	memcpy(qbh->bh[0]->b_data, qbh->data, 512);
261 	memcpy(qbh->bh[1]->b_data, qbh->data + 512, 512);
262 	memcpy(qbh->bh[2]->b_data, qbh->data + 2 * 512, 512);
263 	memcpy(qbh->bh[3]->b_data, qbh->data + 3 * 512, 512);
264 	mark_buffer_dirty(qbh->bh[0]);
265 	mark_buffer_dirty(qbh->bh[1]);
266 	mark_buffer_dirty(qbh->bh[2]);
267 	mark_buffer_dirty(qbh->bh[3]);
268 }
269