00001
00002
00003
00004
00005
00006 #include <stdlib.h>
00007 #include <string.h>
00008 #include <sys/types.h>
00009 #include <fcntl.h>
00010 #include <unistd.h>
00011
00012 #include "libjio.h"
00013 #include "common.h"
00014 #include "trans.h"
00015
00016
00017
00018
00019
00020
00021
00022 ssize_t jread(struct jfs *fs, void *buf, size_t count)
00023 {
00024 ssize_t rv;
00025 off_t pos;
00026
00027 pthread_mutex_lock(&(fs->lock));
00028
00029 pos = lseek(fs->fd, 0, SEEK_CUR);
00030
00031 plockf(fs->fd, F_LOCKR, pos, count);
00032 rv = spread(fs->fd, buf, count, pos);
00033 plockf(fs->fd, F_UNLOCK, pos, count);
00034
00035 if (rv > 0)
00036 lseek(fs->fd, rv, SEEK_CUR);
00037
00038 pthread_mutex_unlock(&(fs->lock));
00039
00040 return rv;
00041 }
00042
00043
00044 ssize_t jpread(struct jfs *fs, void *buf, size_t count, off_t offset)
00045 {
00046 ssize_t rv;
00047
00048 plockf(fs->fd, F_LOCKR, offset, count);
00049 rv = spread(fs->fd, buf, count, offset);
00050 plockf(fs->fd, F_UNLOCK, offset, count);
00051
00052 return rv;
00053 }
00054
00055
00056 ssize_t jreadv(struct jfs *fs, const struct iovec *vector, int count)
00057 {
00058 ssize_t rv;
00059 off_t pos;
00060
00061 pthread_mutex_lock(&(fs->lock));
00062 pos = lseek(fs->fd, 0, SEEK_CUR);
00063 if (pos < 0)
00064 return -1;
00065
00066 plockf(fs->fd, F_LOCKR, pos, count);
00067 rv = readv(fs->fd, vector, count);
00068 plockf(fs->fd, F_UNLOCK, pos, count);
00069
00070 pthread_mutex_unlock(&(fs->lock));
00071
00072 return rv;
00073 }
00074
00075
00076
00077
00078
00079
00080
00081 ssize_t jwrite(struct jfs *fs, const void *buf, size_t count)
00082 {
00083 ssize_t rv;
00084 off_t pos;
00085 struct jtrans *ts;
00086
00087 ts = jtrans_new(fs, 0);
00088 if (ts == NULL)
00089 return -1;
00090
00091 pthread_mutex_lock(&(fs->lock));
00092
00093 if (fs->open_flags & O_APPEND)
00094 pos = lseek(fs->fd, 0, SEEK_END);
00095 else
00096 pos = lseek(fs->fd, 0, SEEK_CUR);
00097
00098 rv = jtrans_add_w(ts, buf, count, pos);
00099 if (rv < 0)
00100 goto exit;
00101
00102 rv = jtrans_commit(ts);
00103
00104 if (rv >= 0)
00105 lseek(fs->fd, count, SEEK_CUR);
00106
00107 exit:
00108
00109 pthread_mutex_unlock(&(fs->lock));
00110
00111 jtrans_free(ts);
00112
00113 return (rv >= 0) ? count : rv;
00114 }
00115
00116
00117 ssize_t jpwrite(struct jfs *fs, const void *buf, size_t count, off_t offset)
00118 {
00119 ssize_t rv;
00120 struct jtrans *ts;
00121
00122 ts = jtrans_new(fs, 0);
00123 if (ts == NULL)
00124 return -1;
00125
00126 rv = jtrans_add_w(ts, buf, count, offset);
00127 if (rv < 0)
00128 goto exit;
00129
00130 rv = jtrans_commit(ts);
00131
00132 exit:
00133 jtrans_free(ts);
00134
00135 return (rv >= 0) ? count : rv;
00136 }
00137
00138
00139 ssize_t jwritev(struct jfs *fs, const struct iovec *vector, int count)
00140 {
00141 int i;
00142 size_t sum;
00143 ssize_t rv;
00144 off_t ipos, t;
00145 struct jtrans *ts;
00146
00147 ts = jtrans_new(fs, 0);
00148 if (ts == NULL)
00149 return -1;
00150
00151 pthread_mutex_lock(&(fs->lock));
00152
00153 if (fs->open_flags & O_APPEND)
00154 ipos = lseek(fs->fd, 0, SEEK_END);
00155 else
00156 ipos = lseek(fs->fd, 0, SEEK_CUR);
00157
00158 t = ipos;
00159
00160 sum = 0;
00161 for (i = 0; i < count; i++) {
00162 rv = jtrans_add_w(ts, vector[i].iov_base,
00163 vector[i].iov_len, t);
00164 if (rv < 0)
00165 goto exit;
00166
00167 sum += vector[i].iov_len;
00168 t += vector[i].iov_len;
00169 }
00170
00171 rv = jtrans_commit(ts);
00172
00173 if (rv >= 0)
00174 lseek(fs->fd, sum, SEEK_CUR);
00175
00176 exit:
00177 pthread_mutex_unlock(&(fs->lock));
00178
00179 jtrans_free(ts);
00180
00181 return (rv >= 0) ? sum : rv;
00182 }
00183
00184
00185 int jtruncate(struct jfs *fs, off_t length)
00186 {
00187 int rv;
00188
00189
00190 plockf(fs->fd, F_LOCKW, length, 0);
00191 rv = ftruncate(fs->fd, length);
00192 plockf(fs->fd, F_UNLOCK, length, 0);
00193
00194 return rv;
00195 }
00196
00197
00198 off_t jlseek(struct jfs *fs, off_t offset, int whence)
00199 {
00200 off_t rv;
00201
00202 pthread_mutex_lock(&(fs->lock));
00203 rv = lseek(fs->fd, offset, whence);
00204 pthread_mutex_unlock(&(fs->lock));
00205
00206 return rv;
00207 }
00208