Line data Source code
1 : #include "clusterautoconfig.h" 2 : 3 : #include <stdio.h> 4 : #include <stdlib.h> 5 : #include <string.h> 6 : #include <stdint.h> 7 : #include <inttypes.h> 8 : #include <sys/types.h> 9 : #include <sys/stat.h> 10 : #include <fcntl.h> 11 : #include <unistd.h> 12 : #include <errno.h> 13 : 14 : #include "libgfs2.h" 15 : 16 : #ifndef IOV_MAX 17 : #ifdef UIO_MAXIOV 18 : #define IOV_MAX UIO_MAXIOV 19 : #else 20 : #define IOV_MAX (1024) 21 : #endif 22 : #endif 23 : 24 14286065 : struct lgfs2_buffer_head *lgfs2_bget(struct lgfs2_sbd *sdp, uint64_t num) 25 : { 26 : struct lgfs2_buffer_head *bh; 27 : 28 14286065 : bh = calloc(1, sizeof(struct lgfs2_buffer_head) + sdp->sd_bsize); 29 14286065 : if (bh == NULL) 30 0 : return NULL; 31 : 32 14286065 : bh->b_blocknr = num; 33 14286065 : bh->sdp = sdp; 34 14286065 : bh->b_data = (char *)bh + sizeof(struct lgfs2_buffer_head); 35 14286065 : return bh; 36 : } 37 : 38 14284592 : struct lgfs2_buffer_head *__lgfs2_bread(struct lgfs2_sbd *sdp, uint64_t num, int line, 39 : const char *caller) 40 : { 41 : struct lgfs2_buffer_head *bh; 42 : ssize_t ret; 43 : 44 14284592 : bh = lgfs2_bget(sdp, num); 45 14284592 : if (bh == NULL) 46 0 : return NULL; 47 : 48 14284592 : ret = pread(sdp->device_fd, bh->b_data, sdp->sd_bsize, num * sdp->sd_bsize); 49 14284592 : if (ret != sdp->sd_bsize) { 50 0 : fprintf(stderr, "%s:%d: Error reading block %"PRIu64": %s\n", 51 0 : caller, line, num, strerror(errno)); 52 0 : free(bh); 53 0 : bh = NULL; 54 : } 55 14284592 : return bh; 56 : } 57 : 58 168461 : int lgfs2_bwrite(struct lgfs2_buffer_head *bh) 59 : { 60 168461 : struct lgfs2_sbd *sdp = bh->sdp; 61 168461 : off_t offset = sdp->sd_bsize * bh->b_blocknr; 62 : 63 168461 : if (pwrite(sdp->device_fd, bh->b_data, sdp->sd_bsize, offset) != sdp->sd_bsize) 64 0 : return -1; 65 168461 : bh->b_modified = 0; 66 168461 : return 0; 67 : } 68 : 69 14272546 : int lgfs2_brelse(struct lgfs2_buffer_head *bh) 70 : { 71 14272546 : int error = 0; 72 : 73 14272546 : if (bh->b_blocknr == -1) 74 0 : printf("Double free!\n"); 75 14272546 : if (bh->b_modified) 76 160231 : error = lgfs2_bwrite(bh); 77 14272546 : bh->b_blocknr = -1; 78 14272546 : if (bh->b_altlist.next && !osi_list_empty(&bh->b_altlist)) 79 21919 : osi_list_del(&bh->b_altlist); 80 14272546 : free(bh); 81 14272546 : return error; 82 : } 83 : 84 : /** 85 : * Free a buffer head, discarding modifications. 86 : * @bhp: Pointer to the buffer 87 : */ 88 12 : void lgfs2_bfree(struct lgfs2_buffer_head **bhp) 89 : { 90 12 : free(*bhp); 91 12 : *bhp = NULL; 92 12 : } 93 : 94 23 : uint32_t lgfs2_get_block_type(const char *buf) 95 : { 96 23 : const struct gfs2_meta_header *mh = (void *)buf; 97 : 98 23 : if (be32_to_cpu(mh->mh_magic) == GFS2_MAGIC) 99 23 : return be32_to_cpu(mh->mh_type); 100 : 101 0 : return 0; 102 : }