/filesystems/unixfs/common/linux/linux.c
C | 49 lines | 43 code | 6 blank | 0 comment | 8 complexity | d4b75bc9f06aed0770b5f0bfec53600c MD5 | raw file
1#include <sys/types.h> 2#include <sys/uio.h> 3#include <unistd.h> 4#include <stdlib.h> 5 6#include "unixfs_internal.h" 7#include "linux.h" 8 9int 10sb_bread_intobh(struct super_block* sb, off_t block, struct buffer_head* bh) 11{ 12 if (pread(sb->s_bdev, bh->b_data, sb->s_blocksize, 13 block * (off_t)sb->s_blocksize) != sb->s_blocksize) 14 return EIO; 15 16 return 0; 17} 18 19void 20brelse(struct buffer_head* bh) 21{ 22 if (bh && bh->b_flags.dynamic) 23 free((void*)bh); 24} 25 26struct buffer_head* 27sb_getblk(struct super_block* sb, sector_t block) 28{ 29 struct buffer_head* bh = calloc(1, sizeof(struct buffer_head)); 30 if (!bh) { 31 fprintf(stderr, "*** fatal error: cannot allocate buffer\n"); 32 abort(); 33 } 34 bh->b_flags.dynamic = 1; 35 bh->b_size = PAGE_SIZE; 36 bh->b_blocknr = block; 37 return bh; 38} 39 40struct buffer_head* 41sb_bread(struct super_block* sb, off_t block) 42{ 43 struct buffer_head* bh = sb_getblk(sb, block); 44 if (bh && sb_bread_intobh(sb, block, bh) != 0) { 45 brelse(bh); 46 return NULL; 47 } 48 return bh; 49}