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 14352617 : struct lgfs2_buffer_head *lgfs2_bget(struct lgfs2_sbd *sdp, uint64_t num)
25 : {
26 : struct lgfs2_buffer_head *bh;
27 :
28 14352617 : bh = calloc(1, sizeof(struct lgfs2_buffer_head) + sdp->sd_bsize);
29 14352617 : if (bh == NULL)
30 0 : return NULL;
31 :
32 14352617 : bh->b_blocknr = num;
33 14352617 : bh->sdp = sdp;
34 14352617 : bh->b_data = (char *)bh + sizeof(struct lgfs2_buffer_head);
35 14352617 : return bh;
36 : }
37 :
38 14351130 : 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 14351130 : bh = lgfs2_bget(sdp, num);
45 14351130 : if (bh == NULL)
46 0 : return NULL;
47 :
48 14351130 : ret = pread(sdp->device_fd, bh->b_data, sdp->sd_bsize, num * sdp->sd_bsize);
49 14351130 : 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 14351130 : return bh;
56 : }
57 :
58 168838 : int lgfs2_bwrite(struct lgfs2_buffer_head *bh)
59 : {
60 168838 : struct lgfs2_sbd *sdp = bh->sdp;
61 168838 : off_t offset = sdp->sd_bsize * bh->b_blocknr;
62 :
63 168838 : if (pwrite(sdp->device_fd, bh->b_data, sdp->sd_bsize, offset) != sdp->sd_bsize)
64 0 : return -1;
65 168838 : bh->b_modified = 0;
66 168838 : return 0;
67 : }
68 :
69 14339094 : int lgfs2_brelse(struct lgfs2_buffer_head *bh)
70 : {
71 14339094 : int error = 0;
72 :
73 14339094 : if (bh->b_blocknr == -1)
74 0 : printf("Double free!\n");
75 14339094 : if (bh->b_modified)
76 160542 : error = lgfs2_bwrite(bh);
77 14339094 : bh->b_blocknr = -1;
78 14339094 : if (bh->b_altlist.next && !osi_list_empty(&bh->b_altlist))
79 22114 : osi_list_del(&bh->b_altlist);
80 14339094 : free(bh);
81 14339094 : 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 : }
|