3 * libjio - A library for Journaled I/O
4 * Alberto Bertogli (albertogli@telpin.com.ar)
11 #include <sys/types.h>
18 /* read() family wrappers */
21 ssize_t
jread(struct jfs
*fs
, void *buf
, size_t count
)
26 pthread_mutex_lock(&(fs
->lock
));
28 pos
= lseek(fs
->fd
, 0, SEEK_CUR
);
30 plockf(fs
->fd
, F_LOCKR
, pos
, count
);
31 rv
= spread(fs
->fd
, buf
, count
, pos
);
32 plockf(fs
->fd
, F_UNLOCK
, pos
, count
);
35 /* if success, advance the file pointer */
36 lseek(fs
->fd
, count
, SEEK_CUR
);
39 pthread_mutex_unlock(&(fs
->lock
));
45 ssize_t
jpread(struct jfs
*fs
, void *buf
, size_t count
, off_t offset
)
49 plockf(fs
->fd
, F_LOCKR
, offset
, count
);
50 rv
= spread(fs
->fd
, buf
, count
, offset
);
51 plockf(fs
->fd
, F_UNLOCK
, offset
, count
);
57 ssize_t
jreadv(struct jfs
*fs
, struct iovec
*vector
, int count
)
64 for (i
= 0; i
< count
; i
++)
65 sum
+= vector
[i
].iov_len
;
67 pthread_mutex_lock(&(fs
->lock
));
68 pos
= lseek(fs
->fd
, 0, SEEK_CUR
);
69 plockf(fs
->fd
, F_LOCKR
, pos
, count
);
70 rv
= readv(fs
->fd
, vector
, count
);
71 plockf(fs
->fd
, F_UNLOCK
, pos
, count
);
72 pthread_mutex_unlock(&(fs
->lock
));
78 /* write family wrappers */
81 ssize_t
jwrite(struct jfs
*fs
, const void *buf
, size_t count
)
87 pthread_mutex_lock(&(fs
->lock
));
90 pos
= lseek(fs
->fd
, 0, SEEK_CUR
);
91 jtrans_add(&ts
, buf
, count
, pos
);
93 rv
= jtrans_commit(&ts
);
96 /* if success, advance the file pointer */
97 lseek(fs
->fd
, count
, SEEK_CUR
);
100 pthread_mutex_unlock(&(fs
->lock
));
108 ssize_t
jpwrite(struct jfs
*fs
, const void *buf
, size_t count
, off_t offset
)
113 jtrans_init(fs
, &ts
);
114 jtrans_add(&ts
, buf
, count
, offset
);
116 rv
= jtrans_commit(&ts
);
124 ssize_t
jwritev(struct jfs
*fs
, const struct iovec
*vector
, int count
)
131 pthread_mutex_lock(&(fs
->lock
));
133 jtrans_init(fs
, &ts
);
134 ipos
= lseek(fs
->fd
, 0, SEEK_CUR
);
138 for (i
= 0; i
< count
; i
++) {
139 jtrans_add(&ts
, vector
[i
].iov_base
, vector
[i
].iov_len
, t
);
140 sum
+= vector
[i
].iov_len
;
141 t
+= vector
[i
].iov_len
;
144 rv
= jtrans_commit(&ts
);
147 /* if success, advance the file pointer */
148 lseek(fs
->fd
, sum
, SEEK_CUR
);
151 pthread_mutex_unlock(&(fs
->lock
));
159 /* truncate a file - be careful with this */
160 int jtruncate(struct jfs
*fs
, off_t length
)
164 /* lock from length to the end of file */
165 plockf(fs
->fd
, F_LOCKW
, length
, 0);
166 rv
= ftruncate(fs
->fd
, length
);
167 plockf(fs
->fd
, F_UNLOCK
, length
, 0);
173 off_t
jlseek(struct jfs
*fs
, off_t offset
, int whence
)
177 pthread_mutex_lock(&(fs
->lock
));
178 rv
= lseek(fs
->fd
, offset
, whence
);
179 pthread_mutex_unlock(&(fs
->lock
));