xref: /openbmc/linux/fs/hpfs/buffer.c (revision 7dd29d8d865efdb00c0542a5d2c87af8c52ea6c7)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/hpfs/buffer.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *  general buffer i/o
71da177e4SLinus Torvalds  */
8e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
95a0e3ad6STejun Heo #include <linux/slab.h>
101da177e4SLinus Torvalds #include "hpfs_fn.h"
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds void hpfs_lock_creation(struct super_block *s)
131da177e4SLinus Torvalds {
141da177e4SLinus Torvalds #ifdef DEBUG_LOCKS
151da177e4SLinus Torvalds 	printk("lock creation\n");
161da177e4SLinus Torvalds #endif
17117bf5fbSThomas Gleixner 	mutex_lock(&hpfs_sb(s)->hpfs_creation_de);
181da177e4SLinus Torvalds }
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds void hpfs_unlock_creation(struct super_block *s)
211da177e4SLinus Torvalds {
221da177e4SLinus Torvalds #ifdef DEBUG_LOCKS
231da177e4SLinus Torvalds 	printk("unlock creation\n");
241da177e4SLinus Torvalds #endif
25117bf5fbSThomas Gleixner 	mutex_unlock(&hpfs_sb(s)->hpfs_creation_de);
261da177e4SLinus Torvalds }
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds /* Map a sector into a buffer and return pointers to it and to the buffer. */
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds void *hpfs_map_sector(struct super_block *s, unsigned secno, struct buffer_head **bhp,
311da177e4SLinus Torvalds 		 int ahead)
321da177e4SLinus Torvalds {
331da177e4SLinus Torvalds 	struct buffer_head *bh;
341da177e4SLinus Torvalds 
35*7dd29d8dSMikulas Patocka 	hpfs_lock_assert(s);
36*7dd29d8dSMikulas Patocka 
371da177e4SLinus Torvalds 	cond_resched();
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds 	*bhp = bh = sb_bread(s, secno);
401da177e4SLinus Torvalds 	if (bh != NULL)
411da177e4SLinus Torvalds 		return bh->b_data;
421da177e4SLinus Torvalds 	else {
431da177e4SLinus Torvalds 		printk("HPFS: hpfs_map_sector: read error\n");
441da177e4SLinus Torvalds 		return NULL;
451da177e4SLinus Torvalds 	}
461da177e4SLinus Torvalds }
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds /* Like hpfs_map_sector but don't read anything */
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds void *hpfs_get_sector(struct super_block *s, unsigned secno, struct buffer_head **bhp)
511da177e4SLinus Torvalds {
521da177e4SLinus Torvalds 	struct buffer_head *bh;
531da177e4SLinus Torvalds 	/*return hpfs_map_sector(s, secno, bhp, 0);*/
541da177e4SLinus Torvalds 
55*7dd29d8dSMikulas Patocka 	hpfs_lock_assert(s);
56*7dd29d8dSMikulas Patocka 
571da177e4SLinus Torvalds 	cond_resched();
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds 	if ((*bhp = bh = sb_getblk(s, secno)) != NULL) {
601da177e4SLinus Torvalds 		if (!buffer_uptodate(bh)) wait_on_buffer(bh);
611da177e4SLinus Torvalds 		set_buffer_uptodate(bh);
621da177e4SLinus Torvalds 		return bh->b_data;
631da177e4SLinus Torvalds 	} else {
641da177e4SLinus Torvalds 		printk("HPFS: hpfs_get_sector: getblk failed\n");
651da177e4SLinus Torvalds 		return NULL;
661da177e4SLinus Torvalds 	}
671da177e4SLinus Torvalds }
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds /* Map 4 sectors into a 4buffer and return pointers to it and to the buffer. */
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds void *hpfs_map_4sectors(struct super_block *s, unsigned secno, struct quad_buffer_head *qbh,
721da177e4SLinus Torvalds 		   int ahead)
731da177e4SLinus Torvalds {
741da177e4SLinus Torvalds 	struct buffer_head *bh;
751da177e4SLinus Torvalds 	char *data;
761da177e4SLinus Torvalds 
77*7dd29d8dSMikulas Patocka 	hpfs_lock_assert(s);
78*7dd29d8dSMikulas Patocka 
791da177e4SLinus Torvalds 	cond_resched();
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds 	if (secno & 3) {
821da177e4SLinus Torvalds 		printk("HPFS: hpfs_map_4sectors: unaligned read\n");
831da177e4SLinus Torvalds 		return NULL;
841da177e4SLinus Torvalds 	}
851da177e4SLinus Torvalds 
86f52720caSPanagiotis Issaris 	qbh->data = data = kmalloc(2048, GFP_NOFS);
871da177e4SLinus Torvalds 	if (!data) {
881da177e4SLinus Torvalds 		printk("HPFS: hpfs_map_4sectors: out of memory\n");
891da177e4SLinus Torvalds 		goto bail;
901da177e4SLinus Torvalds 	}
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds 	qbh->bh[0] = bh = sb_bread(s, secno);
931da177e4SLinus Torvalds 	if (!bh)
941da177e4SLinus Torvalds 		goto bail0;
951da177e4SLinus Torvalds 	memcpy(data, bh->b_data, 512);
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds 	qbh->bh[1] = bh = sb_bread(s, secno + 1);
981da177e4SLinus Torvalds 	if (!bh)
991da177e4SLinus Torvalds 		goto bail1;
1001da177e4SLinus Torvalds 	memcpy(data + 512, bh->b_data, 512);
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds 	qbh->bh[2] = bh = sb_bread(s, secno + 2);
1031da177e4SLinus Torvalds 	if (!bh)
1041da177e4SLinus Torvalds 		goto bail2;
1051da177e4SLinus Torvalds 	memcpy(data + 2 * 512, bh->b_data, 512);
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds 	qbh->bh[3] = bh = sb_bread(s, secno + 3);
1081da177e4SLinus Torvalds 	if (!bh)
1091da177e4SLinus Torvalds 		goto bail3;
1101da177e4SLinus Torvalds 	memcpy(data + 3 * 512, bh->b_data, 512);
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds 	return data;
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds  bail3:
1151da177e4SLinus Torvalds 	brelse(qbh->bh[2]);
1161da177e4SLinus Torvalds  bail2:
1171da177e4SLinus Torvalds 	brelse(qbh->bh[1]);
1181da177e4SLinus Torvalds  bail1:
1191da177e4SLinus Torvalds 	brelse(qbh->bh[0]);
1201da177e4SLinus Torvalds  bail0:
1211da177e4SLinus Torvalds 	kfree(data);
1221da177e4SLinus Torvalds 	printk("HPFS: hpfs_map_4sectors: read error\n");
1231da177e4SLinus Torvalds  bail:
1241da177e4SLinus Torvalds 	return NULL;
1251da177e4SLinus Torvalds }
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds /* Don't read sectors */
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds void *hpfs_get_4sectors(struct super_block *s, unsigned secno,
1301da177e4SLinus Torvalds                           struct quad_buffer_head *qbh)
1311da177e4SLinus Torvalds {
1321da177e4SLinus Torvalds 	cond_resched();
1331da177e4SLinus Torvalds 
134*7dd29d8dSMikulas Patocka 	hpfs_lock_assert(s);
135*7dd29d8dSMikulas Patocka 
1361da177e4SLinus Torvalds 	if (secno & 3) {
1371da177e4SLinus Torvalds 		printk("HPFS: hpfs_get_4sectors: unaligned read\n");
1381da177e4SLinus Torvalds 		return NULL;
1391da177e4SLinus Torvalds 	}
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds 	/*return hpfs_map_4sectors(s, secno, qbh, 0);*/
1421da177e4SLinus Torvalds 	if (!(qbh->data = kmalloc(2048, GFP_NOFS))) {
1431da177e4SLinus Torvalds 		printk("HPFS: hpfs_get_4sectors: out of memory\n");
1441da177e4SLinus Torvalds 		return NULL;
1451da177e4SLinus Torvalds 	}
1461da177e4SLinus Torvalds 	if (!(hpfs_get_sector(s, secno, &qbh->bh[0]))) goto bail0;
1471da177e4SLinus Torvalds 	if (!(hpfs_get_sector(s, secno + 1, &qbh->bh[1]))) goto bail1;
1481da177e4SLinus Torvalds 	if (!(hpfs_get_sector(s, secno + 2, &qbh->bh[2]))) goto bail2;
1491da177e4SLinus Torvalds 	if (!(hpfs_get_sector(s, secno + 3, &qbh->bh[3]))) goto bail3;
1501da177e4SLinus Torvalds 	memcpy(qbh->data, qbh->bh[0]->b_data, 512);
1511da177e4SLinus Torvalds 	memcpy(qbh->data + 512, qbh->bh[1]->b_data, 512);
1521da177e4SLinus Torvalds 	memcpy(qbh->data + 2*512, qbh->bh[2]->b_data, 512);
1531da177e4SLinus Torvalds 	memcpy(qbh->data + 3*512, qbh->bh[3]->b_data, 512);
1541da177e4SLinus Torvalds 	return qbh->data;
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds 	bail3:	brelse(qbh->bh[2]);
1571da177e4SLinus Torvalds 	bail2:	brelse(qbh->bh[1]);
1581da177e4SLinus Torvalds 	bail1:	brelse(qbh->bh[0]);
1591da177e4SLinus Torvalds 	bail0:
1601da177e4SLinus Torvalds 	return NULL;
1611da177e4SLinus Torvalds }
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds void hpfs_brelse4(struct quad_buffer_head *qbh)
1651da177e4SLinus Torvalds {
1661da177e4SLinus Torvalds 	brelse(qbh->bh[3]);
1671da177e4SLinus Torvalds 	brelse(qbh->bh[2]);
1681da177e4SLinus Torvalds 	brelse(qbh->bh[1]);
1691da177e4SLinus Torvalds 	brelse(qbh->bh[0]);
1701da177e4SLinus Torvalds 	kfree(qbh->data);
1711da177e4SLinus Torvalds }
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds void hpfs_mark_4buffers_dirty(struct quad_buffer_head *qbh)
1741da177e4SLinus Torvalds {
1751da177e4SLinus Torvalds 	PRINTK(("hpfs_mark_4buffers_dirty\n"));
1761da177e4SLinus Torvalds 	memcpy(qbh->bh[0]->b_data, qbh->data, 512);
1771da177e4SLinus Torvalds 	memcpy(qbh->bh[1]->b_data, qbh->data + 512, 512);
1781da177e4SLinus Torvalds 	memcpy(qbh->bh[2]->b_data, qbh->data + 2 * 512, 512);
1791da177e4SLinus Torvalds 	memcpy(qbh->bh[3]->b_data, qbh->data + 3 * 512, 512);
1801da177e4SLinus Torvalds 	mark_buffer_dirty(qbh->bh[0]);
1811da177e4SLinus Torvalds 	mark_buffer_dirty(qbh->bh[1]);
1821da177e4SLinus Torvalds 	mark_buffer_dirty(qbh->bh[2]);
1831da177e4SLinus Torvalds 	mark_buffer_dirty(qbh->bh[3]);
1841da177e4SLinus Torvalds }
185