xref: /openbmc/linux/fs/hpfs/buffer.c (revision 5a0e3ad6af8660be21ca98a971cd00f331318c05)
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>
9*5a0e3ad6STejun 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
171da177e4SLinus Torvalds 	down(&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
251da177e4SLinus Torvalds 	up(&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 
351da177e4SLinus Torvalds 	cond_resched();
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds 	*bhp = bh = sb_bread(s, secno);
381da177e4SLinus Torvalds 	if (bh != NULL)
391da177e4SLinus Torvalds 		return bh->b_data;
401da177e4SLinus Torvalds 	else {
411da177e4SLinus Torvalds 		printk("HPFS: hpfs_map_sector: read error\n");
421da177e4SLinus Torvalds 		return NULL;
431da177e4SLinus Torvalds 	}
441da177e4SLinus Torvalds }
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds /* Like hpfs_map_sector but don't read anything */
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds void *hpfs_get_sector(struct super_block *s, unsigned secno, struct buffer_head **bhp)
491da177e4SLinus Torvalds {
501da177e4SLinus Torvalds 	struct buffer_head *bh;
511da177e4SLinus Torvalds 	/*return hpfs_map_sector(s, secno, bhp, 0);*/
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds 	cond_resched();
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds 	if ((*bhp = bh = sb_getblk(s, secno)) != NULL) {
561da177e4SLinus Torvalds 		if (!buffer_uptodate(bh)) wait_on_buffer(bh);
571da177e4SLinus Torvalds 		set_buffer_uptodate(bh);
581da177e4SLinus Torvalds 		return bh->b_data;
591da177e4SLinus Torvalds 	} else {
601da177e4SLinus Torvalds 		printk("HPFS: hpfs_get_sector: getblk failed\n");
611da177e4SLinus Torvalds 		return NULL;
621da177e4SLinus Torvalds 	}
631da177e4SLinus Torvalds }
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds /* Map 4 sectors into a 4buffer and return pointers to it and to the buffer. */
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds void *hpfs_map_4sectors(struct super_block *s, unsigned secno, struct quad_buffer_head *qbh,
681da177e4SLinus Torvalds 		   int ahead)
691da177e4SLinus Torvalds {
701da177e4SLinus Torvalds 	struct buffer_head *bh;
711da177e4SLinus Torvalds 	char *data;
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds 	cond_resched();
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds 	if (secno & 3) {
761da177e4SLinus Torvalds 		printk("HPFS: hpfs_map_4sectors: unaligned read\n");
771da177e4SLinus Torvalds 		return NULL;
781da177e4SLinus Torvalds 	}
791da177e4SLinus Torvalds 
80f52720caSPanagiotis Issaris 	qbh->data = data = kmalloc(2048, GFP_NOFS);
811da177e4SLinus Torvalds 	if (!data) {
821da177e4SLinus Torvalds 		printk("HPFS: hpfs_map_4sectors: out of memory\n");
831da177e4SLinus Torvalds 		goto bail;
841da177e4SLinus Torvalds 	}
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds 	qbh->bh[0] = bh = sb_bread(s, secno);
871da177e4SLinus Torvalds 	if (!bh)
881da177e4SLinus Torvalds 		goto bail0;
891da177e4SLinus Torvalds 	memcpy(data, bh->b_data, 512);
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds 	qbh->bh[1] = bh = sb_bread(s, secno + 1);
921da177e4SLinus Torvalds 	if (!bh)
931da177e4SLinus Torvalds 		goto bail1;
941da177e4SLinus Torvalds 	memcpy(data + 512, bh->b_data, 512);
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds 	qbh->bh[2] = bh = sb_bread(s, secno + 2);
971da177e4SLinus Torvalds 	if (!bh)
981da177e4SLinus Torvalds 		goto bail2;
991da177e4SLinus Torvalds 	memcpy(data + 2 * 512, bh->b_data, 512);
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds 	qbh->bh[3] = bh = sb_bread(s, secno + 3);
1021da177e4SLinus Torvalds 	if (!bh)
1031da177e4SLinus Torvalds 		goto bail3;
1041da177e4SLinus Torvalds 	memcpy(data + 3 * 512, bh->b_data, 512);
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds 	return data;
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds  bail3:
1091da177e4SLinus Torvalds 	brelse(qbh->bh[2]);
1101da177e4SLinus Torvalds  bail2:
1111da177e4SLinus Torvalds 	brelse(qbh->bh[1]);
1121da177e4SLinus Torvalds  bail1:
1131da177e4SLinus Torvalds 	brelse(qbh->bh[0]);
1141da177e4SLinus Torvalds  bail0:
1151da177e4SLinus Torvalds 	kfree(data);
1161da177e4SLinus Torvalds 	printk("HPFS: hpfs_map_4sectors: read error\n");
1171da177e4SLinus Torvalds  bail:
1181da177e4SLinus Torvalds 	return NULL;
1191da177e4SLinus Torvalds }
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds /* Don't read sectors */
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds void *hpfs_get_4sectors(struct super_block *s, unsigned secno,
1241da177e4SLinus Torvalds                           struct quad_buffer_head *qbh)
1251da177e4SLinus Torvalds {
1261da177e4SLinus Torvalds 	cond_resched();
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds 	if (secno & 3) {
1291da177e4SLinus Torvalds 		printk("HPFS: hpfs_get_4sectors: unaligned read\n");
1301da177e4SLinus Torvalds 		return NULL;
1311da177e4SLinus Torvalds 	}
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds 	/*return hpfs_map_4sectors(s, secno, qbh, 0);*/
1341da177e4SLinus Torvalds 	if (!(qbh->data = kmalloc(2048, GFP_NOFS))) {
1351da177e4SLinus Torvalds 		printk("HPFS: hpfs_get_4sectors: out of memory\n");
1361da177e4SLinus Torvalds 		return NULL;
1371da177e4SLinus Torvalds 	}
1381da177e4SLinus Torvalds 	if (!(hpfs_get_sector(s, secno, &qbh->bh[0]))) goto bail0;
1391da177e4SLinus Torvalds 	if (!(hpfs_get_sector(s, secno + 1, &qbh->bh[1]))) goto bail1;
1401da177e4SLinus Torvalds 	if (!(hpfs_get_sector(s, secno + 2, &qbh->bh[2]))) goto bail2;
1411da177e4SLinus Torvalds 	if (!(hpfs_get_sector(s, secno + 3, &qbh->bh[3]))) goto bail3;
1421da177e4SLinus Torvalds 	memcpy(qbh->data, qbh->bh[0]->b_data, 512);
1431da177e4SLinus Torvalds 	memcpy(qbh->data + 512, qbh->bh[1]->b_data, 512);
1441da177e4SLinus Torvalds 	memcpy(qbh->data + 2*512, qbh->bh[2]->b_data, 512);
1451da177e4SLinus Torvalds 	memcpy(qbh->data + 3*512, qbh->bh[3]->b_data, 512);
1461da177e4SLinus Torvalds 	return qbh->data;
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds 	bail3:	brelse(qbh->bh[2]);
1491da177e4SLinus Torvalds 	bail2:	brelse(qbh->bh[1]);
1501da177e4SLinus Torvalds 	bail1:	brelse(qbh->bh[0]);
1511da177e4SLinus Torvalds 	bail0:
1521da177e4SLinus Torvalds 	return NULL;
1531da177e4SLinus Torvalds }
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds void hpfs_brelse4(struct quad_buffer_head *qbh)
1571da177e4SLinus Torvalds {
1581da177e4SLinus Torvalds 	brelse(qbh->bh[3]);
1591da177e4SLinus Torvalds 	brelse(qbh->bh[2]);
1601da177e4SLinus Torvalds 	brelse(qbh->bh[1]);
1611da177e4SLinus Torvalds 	brelse(qbh->bh[0]);
1621da177e4SLinus Torvalds 	kfree(qbh->data);
1631da177e4SLinus Torvalds }
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds void hpfs_mark_4buffers_dirty(struct quad_buffer_head *qbh)
1661da177e4SLinus Torvalds {
1671da177e4SLinus Torvalds 	PRINTK(("hpfs_mark_4buffers_dirty\n"));
1681da177e4SLinus Torvalds 	memcpy(qbh->bh[0]->b_data, qbh->data, 512);
1691da177e4SLinus Torvalds 	memcpy(qbh->bh[1]->b_data, qbh->data + 512, 512);
1701da177e4SLinus Torvalds 	memcpy(qbh->bh[2]->b_data, qbh->data + 2 * 512, 512);
1711da177e4SLinus Torvalds 	memcpy(qbh->bh[3]->b_data, qbh->data + 3 * 512, 512);
1721da177e4SLinus Torvalds 	mark_buffer_dirty(qbh->bh[0]);
1731da177e4SLinus Torvalds 	mark_buffer_dirty(qbh->bh[1]);
1741da177e4SLinus Torvalds 	mark_buffer_dirty(qbh->bh[2]);
1751da177e4SLinus Torvalds 	mark_buffer_dirty(qbh->bh[3]);
1761da177e4SLinus Torvalds }
177