00001
00002
00003
00004
00005
00006 #include <sys/types.h>
00007 #include <fcntl.h>
00008 #include <unistd.h>
00009 #include <string.h>
00010 #include <libgen.h>
00011 #include <limits.h>
00012 #include <stdio.h>
00013 #include <stdlib.h>
00014 #include <arpa/inet.h>
00015
00016 #include "libjio.h"
00017 #include "common.h"
00018
00019
00021 off_t plockf(int fd, int cmd, off_t offset, off_t len)
00022 {
00023 struct flock fl;
00024 int op;
00025
00026 op = -1;
00027 fl.l_type = -1;
00028
00029 if (cmd & _F_READ) {
00030 fl.l_type = F_RDLCK;
00031 } else if (cmd & _F_WRITE) {
00032 fl.l_type = F_WRLCK;
00033 }
00034
00035 if (cmd & _F_LOCK) {
00036 op = F_SETLKW;
00037 } else if (cmd & _F_TLOCK) {
00038 op = F_SETLK;
00039 } else if (cmd & F_UNLOCK) {
00040 fl.l_type = F_UNLCK;
00041 op = F_SETLKW;
00042 }
00043
00044 fl.l_whence = SEEK_SET;
00045 fl.l_start = offset;
00046 fl.l_len = len;
00047
00048 return fcntl(fd, op, &fl);
00049 }
00050
00053 ssize_t spread(int fd, void *buf, size_t count, off_t offset)
00054 {
00055 ssize_t rv;
00056 size_t c;
00057
00058 c = 0;
00059
00060 while (c < count) {
00061 rv = pread(fd, (char *) buf + c, count - c, offset + c);
00062
00063 if (rv < 0)
00064
00065 return rv;
00066 else if (rv == 0)
00067
00068 return c;
00069
00070
00071 c += rv;
00072 }
00073
00074 return count;
00075 }
00076
00078 ssize_t spwrite(int fd, const void *buf, size_t count, off_t offset)
00079 {
00080 ssize_t rv;
00081 size_t c;
00082
00083 c = 0;
00084
00085 while (c < count) {
00086 rv = pwrite(fd, (char *) buf + c, count - c, offset + c);
00087
00088 if (rv < 0)
00089 return rv;
00090
00091
00092 c += rv;
00093 }
00094
00095 return count;
00096 }
00097
00101 ssize_t swritev(int fd, struct iovec *iov, int iovcnt)
00102 {
00103 int i;
00104 ssize_t rv;
00105 size_t c, t, total;
00106
00107 total = 0;
00108 for (i = 0; i < iovcnt; i++)
00109 total += iov[i].iov_len;
00110
00111 c = 0;
00112 while (c < total) {
00113 rv = writev(fd, iov, iovcnt);
00114
00115 if (rv < 0)
00116 return rv;
00117
00118 c += rv;
00119
00120
00121
00122 if (c == total)
00123 break;
00124
00125
00126 t = 0;
00127 for (i = 0; i < iovcnt; i++) {
00128 if (t + iov[i].iov_len > rv) {
00129 iov[i].iov_base = (char *)
00130 iov[i].iov_base + rv - t;
00131 iov[i].iov_len -= rv - t;
00132 break;
00133 } else {
00134 t += iov[i].iov_len;
00135 }
00136 }
00137
00138 iovcnt -= i;
00139 iov = iov + i;
00140 }
00141
00142 return c;
00143 }
00144
00146 int get_jdir(const char *filename, char *jdir)
00147 {
00148 char *base, *baset;
00149 char *dir, *dirt;
00150
00151 baset = strdup(filename);
00152 if (baset == NULL)
00153 return 0;
00154 base = basename(baset);
00155
00156 dirt = strdup(filename);
00157 if (dirt == NULL) {
00158 free(baset);
00159 return 0;
00160 }
00161 dir = dirname(dirt);
00162
00163 snprintf(jdir, PATH_MAX, "%s/.%s.jio", dir, base);
00164
00165 free(baset);
00166 free(dirt);
00167
00168 return 1;
00169 }
00170
00173 void get_jtfile(struct jfs *fs, unsigned int tid, char *jtfile)
00174 {
00175 snprintf(jtfile, PATH_MAX, "%s/%u", fs->jdir, tid);
00176 }
00177
00178
00179
00180
00181
00182
00184 uint64_t ntohll(uint64_t x)
00185 {
00186 static int endianness = 0;
00187
00188
00189
00190 if (endianness == 0) {
00191 if (htonl(1) == 1)
00192 endianness = 1;
00193 else
00194 endianness = -1;
00195 }
00196
00197 if (endianness == 1) {
00198
00199 return x;
00200 }
00201
00202
00203 return ( ntohl( (x >> 32) & 0xFFFFFFFF ) | \
00204 ( (uint64_t) ntohl(x & 0xFFFFFFFF) ) << 32 );
00205 }
00206
00208 uint64_t htonll(uint64_t x)
00209 {
00210 static int endianness = 0;
00211
00212
00213
00214 if (endianness == 0) {
00215 if (htonl(1) == 1)
00216 endianness = 1;
00217 else
00218 endianness = -1;
00219 }
00220
00221 if (endianness == 1) {
00222
00223 return x;
00224 }
00225
00226
00227 return ( htonl( (x >> 32) & 0xFFFFFFFF ) | \
00228 ( (uint64_t) htonl(x & 0xFFFFFFFF) ) << 32 );
00229 }
00230
00231