From d7a9643b8b04e60ebee2546932eda27909ebf01e Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Thu, 31 Jan 2008 12:59:49 -0800 Subject: [PATCH] userspace: use structs to enforce type safety on __le64 etc --- src/client/Client.cc | 10 +++++----- src/include/byteorder.h | 35 ++++++++++++++++++++++++++++++++--- src/include/ceph_fs.h | 28 +++++++++++++++++++--------- src/include/types.h | 2 +- src/mds/Server.cc | 2 +- src/mds/mdstypes.h | 3 +-- src/messages/MClientReply.h | 10 +++++----- src/messages/MClientRequest.h | 14 +++++++------- src/messages/MOSDOp.h | 12 +++++++----- src/messages/MOSDOpReply.h | 6 +++--- src/mon/MonMap.h | 4 ++-- src/mon/PGMonitor.cc | 8 ++++---- src/osd/OSDMap.h | 4 ++-- src/osd/osd_types.h | 8 +++++--- 14 files changed, 94 insertions(+), 52 deletions(-) diff --git a/src/client/Client.cc b/src/client/Client.cc index 6be605f11668a..904ffc9a65617 100644 --- a/src/client/Client.cc +++ b/src/client/Client.cc @@ -2381,7 +2381,7 @@ int Client::_readdir_get_frag(DirResult *dirp) MClientRequest *req = new MClientRequest(CEPH_MDS_OP_READDIR, messenger->get_myinst()); req->set_filepath(dirp->path); - req->head.args.readdir.frag = fg; + req->head.args.readdir.frag = cpu_to_le32(fg); // FIXME where does FUSE maintain user information req->set_caller_uid(getuid()); @@ -3332,10 +3332,10 @@ int Client::_statfs(struct statvfs *stbuf) memset(stbuf, 0, sizeof(*stbuf)); stbuf->f_bsize = 4096; stbuf->f_frsize = 4096; - stbuf->f_blocks = req->reply->stfs.f_total / 4; - stbuf->f_bfree = req->reply->stfs.f_free / 4; - stbuf->f_bavail = req->reply->stfs.f_avail / 4; - stbuf->f_files = req->reply->stfs.f_objects; + stbuf->f_blocks = le64_to_cpu(req->reply->stfs.f_total) / 4; + stbuf->f_bfree = le64_to_cpu(req->reply->stfs.f_free) / 4; + stbuf->f_bavail = le64_to_cpu(req->reply->stfs.f_avail) / 4; + stbuf->f_files = le64_to_cpu(req->reply->stfs.f_objects); stbuf->f_ffree = -1; stbuf->f_favail = -1; stbuf->f_fsid = -1; // ?? diff --git a/src/include/byteorder.h b/src/include/byteorder.h index 982536100eef7..08aa4c9339ebb 100644 --- a/src/include/byteorder.h +++ b/src/include/byteorder.h @@ -7,9 +7,8 @@ #ifndef _CEPH_BYTEORDER_H #define _CEPH_BYTEORDER_H -typedef __u64 __le64; -typedef __u32 __le32; -typedef __u16 __le16; +// type-safe? +#define CEPH_TYPE_SAFE_BYTEORDER static __inline__ __u16 swab16(__u16 val) { @@ -34,6 +33,34 @@ static __inline__ __u64 swab64(__u64 val) ((val << 56))); } +#ifdef CEPH_TYPE_SAFE_BYTEORDER + +struct __le64 { __u64 v; } __attribute__ ((packed)); +struct __le32 { __u32 v; } __attribute__ ((packed)); +struct __le16 { __u16 v; } __attribute__ ((packed)); + +#ifdef WORDS_BIGENDIAN +static inline __le64 cpu_to_le64(__u64 v) { __le64 r = { swab64(v) }; return r; } +static inline __le32 cpu_to_le32(__u32 v) { __le32 r = { swab32(v) }; return r; } +static inline __le16 cpu_to_le16(__u16 v) { __le16 r = { swab16(v) }; return r; } +static inline __u64 le64_to_cpu(__le64 v) { return swab64(v.v); } +static inline __u32 le32_to_cpu(__le32 v) { return swab32(v.v); } +static inline __u16 le16_to_cpu(__le16 v) { return swab16(v.v); } +#else +static inline __le64 cpu_to_le64(__u64 v) { __le64 r = { v }; return r; } +static inline __le32 cpu_to_le32(__u32 v) { __le32 r = { v }; return r; } +static inline __le16 cpu_to_le16(__u16 v) { __le16 r = { v }; return r; } +static inline __u64 le64_to_cpu(__le64 v) { return v.v; } +static inline __u32 le32_to_cpu(__le32 v) { return v.v; } +static inline __u16 le16_to_cpu(__le16 v) { return v.v; } +#endif + +#else + +typedef __u64 __le64; +typedef __u32 __le32; +typedef __u16 __le16; + #ifdef WORDS_BIGENDIAN # define cpu_to_le64(x) swab64((x)) # define le64_to_cpu(x) swab64((x)) @@ -51,3 +78,5 @@ static __inline__ __u64 swab64(__u64 val) #endif #endif + +#endif diff --git a/src/include/ceph_fs.h b/src/include/ceph_fs.h index fba2ff2568a67..9971ff9058bb1 100644 --- a/src/include/ceph_fs.h +++ b/src/include/ceph_fs.h @@ -17,29 +17,39 @@ #define CEPH_MON_PORT 12345 +/* + * types in this file are defined as little-endian, and are + * primarily intended to describe data structures that pass + * over the wire or are stored on disk. + */ + -typedef __u64 ceph_version_t; -typedef __u64 ceph_tid_t; -typedef __u32 ceph_epoch_t; +/* + * some basics + */ +typedef __le64 ceph_version_t; +typedef __le64 ceph_tid_t; +typedef __le32 ceph_epoch_t; /* * fs id */ struct ceph_fsid { - __u64 major; - __u64 minor; + __le64 major; + __le64 minor; }; static inline int ceph_fsid_equal(const struct ceph_fsid *a, const struct ceph_fsid *b) { - return a->major == b->major && a->minor == b->minor; + return le64_to_cpu(a->major) == le64_to_cpu(b->major) && + le64_to_cpu(a->minor) == le64_to_cpu(b->minor); } /* * ino, object, etc. */ -typedef __u64 ceph_ino_t; +typedef __le64 ceph_ino_t; struct ceph_object { __le64 ino; /* inode "file" identifier */ @@ -57,7 +67,7 @@ struct ceph_timeval { /* * dir fragments */ -typedef __u32 ceph_frag_t; +typedef __le32 ceph_frag_t; static inline __u32 frag_make(__u32 b, __u32 v) { return (b << 24) | (v & (0xffffffu >> (24-b))); } static inline __u32 frag_bits(__u32 f) { return f >> 24; } @@ -153,7 +163,7 @@ struct ceph_object_layout { */ struct ceph_eversion { ceph_epoch_t epoch; - __u64 version; + __le64 version; } __attribute__ ((packed)); /* diff --git a/src/include/types.h b/src/include/types.h index de051046d14b3..a0a913bc2c85f 100644 --- a/src/include/types.h +++ b/src/include/types.h @@ -219,7 +219,7 @@ struct inode_t { // -- inline ostream& operator<<(ostream& out, ceph_fsid& f) { - return out << hex << f.major << '.' << f.minor << dec; + return out << hex << le64_to_cpu(f.major) << '.' << le64_to_cpu(f.minor) << dec; } diff --git a/src/mds/Server.cc b/src/mds/Server.cc index 5f080c937ac12..787081b6063e1 100644 --- a/src/mds/Server.cc +++ b/src/mds/Server.cc @@ -1639,7 +1639,7 @@ void Server::handle_client_readdir(MDRequest *mdr) } // which frag? - frag_t fg = req->head.args.readdir.frag; + frag_t fg = le32_to_cpu(req->head.args.readdir.frag); // does the frag exist? if (diri->dirfragtree[fg] != fg) { diff --git a/src/mds/mdstypes.h b/src/mds/mdstypes.h index 7ef2164684134..2e7c8a8f83dc9 100644 --- a/src/mds/mdstypes.h +++ b/src/mds/mdstypes.h @@ -51,9 +51,8 @@ using namespace std; struct metareqid_t { entity_name_t name; - uint64_t tid; + __u64 tid; metareqid_t() : tid(0) {} - //metareqid_t(int c, tid_t t) : tid(t) { name = entity_name_t::CLIENT(c); } metareqid_t(entity_name_t n, tid_t t) : name(n), tid(t) {} }; diff --git a/src/messages/MClientReply.h b/src/messages/MClientReply.h index f7831fc1dfcf6..1c7f2a886668f 100644 --- a/src/messages/MClientReply.h +++ b/src/messages/MClientReply.h @@ -105,7 +105,7 @@ struct InodeStat { void _decode(bufferlist::iterator &p) { struct ceph_mds_reply_inode e; ::_decode_simple(e, p); - inode.ino = e.ino; + inode.ino = le64_to_cpu(e.ino); inode.layout = e.layout; inode.ctime.decode_timeval(&e.ctime); inode.mtime.decode_timeval(&e.mtime); @@ -143,7 +143,7 @@ struct InodeStat { */ struct ceph_mds_reply_inode e; memset(&e, 0, sizeof(e)); - e.ino = in->inode.ino; + e.ino = cpu_to_le64(in->inode.ino); e.layout = in->inode.layout; in->inode.ctime.encode_timeval(&e.ctime); in->inode.mtime.encode_timeval(&e.mtime); @@ -185,7 +185,7 @@ class MClientReply : public Message { bufferlist dir_bl; public: - long get_tid() { return st.tid; } + long get_tid() { return le64_to_cpu(st.tid); } int get_op() { return st.op; } void set_mdsmap_epoch(epoch_t e) { st.mdsmap_epoch = e; } @@ -209,7 +209,7 @@ class MClientReply : public Message { MClientReply(MClientRequest *req, int result = 0) : Message(CEPH_MSG_CLIENT_REPLY), dir_dir(0) { memset(&st, 0, sizeof(st)); - this->st.tid = req->get_tid(); + this->st.tid = cpu_to_le64(req->get_tid()); this->st.op = req->get_op(); this->st.result = result; } @@ -223,7 +223,7 @@ class MClientReply : public Message { } const char *get_type_name() { return "creply"; } void print(ostream& o) { - o << "creply(" << env.dst.name << "." << st.tid; + o << "creply(" << env.dst.name << "." << le64_to_cpu(st.tid); o << " = " << st.result; if (st.result <= 0) o << " " << strerror(-st.result); diff --git a/src/messages/MClientRequest.h b/src/messages/MClientRequest.h index d401be7bd6471..e951132bc3b16 100644 --- a/src/messages/MClientRequest.h +++ b/src/messages/MClientRequest.h @@ -92,7 +92,7 @@ public: metareqid_t get_reqid() { // FIXME: for now, assume clients always have 1 incarnation - return metareqid_t(head.client_inst.name, head.tid); + return metareqid_t(head.client_inst.name, le64_to_cpu(head.tid)); } int get_open_file_mode() { @@ -152,8 +152,8 @@ public: // normal fields - void set_tid(tid_t t) { head.tid = t; } - void set_oldest_client_tid(tid_t t) { head.oldest_client_tid = t; } + void set_tid(tid_t t) { head.tid = cpu_to_le64(t); } + void set_oldest_client_tid(tid_t t) { head.oldest_client_tid = cpu_to_le64(t); } void inc_num_fwd() { head.num_fwd++; } void set_retry_attempt(int a) { head.retry_attempt = a; } void set_path(string& p) { path.set_path(p); } @@ -165,7 +165,7 @@ public: void set_caller_uid(int u) { head.caller_uid = u; } void set_caller_gid(int g) { head.caller_gid = g; } void set_mds_wants_replica_in_dirino(inodeno_t dirino) { - head.mds_wants_replica_in_dirino = dirino; } + head.mds_wants_replica_in_dirino = cpu_to_le64(dirino); } void set_client_inst(const entity_inst_t& i) { head.client_inst.name = i.name.v; @@ -175,8 +175,8 @@ public: return entity_inst_t(head.client_inst); } entity_name_t get_client() { return head.client_inst.name; } - tid_t get_tid() { return head.tid; } - tid_t get_oldest_client_tid() { return head.oldest_client_tid; } + tid_t get_tid() { return le64_to_cpu(head.tid); } + tid_t get_oldest_client_tid() { return le64_to_cpu(head.oldest_client_tid); } int get_num_fwd() { return head.num_fwd; } int get_retry_attempt() { return head.retry_attempt; } int get_op() { return head.op; } @@ -189,7 +189,7 @@ public: filepath& get_filepath2() { return path2; } inodeno_t get_mds_wants_replica_in_dirino() { - return head.mds_wants_replica_in_dirino; } + return le64_to_cpu(head.mds_wants_replica_in_dirino); } void decode_payload() { int off = 0; diff --git a/src/messages/MOSDOp.h b/src/messages/MOSDOp.h index 0e923dda79f2c..1a6e0a520c808 100644 --- a/src/messages/MOSDOp.h +++ b/src/messages/MOSDOp.h @@ -65,9 +65,11 @@ private: friend class MOSDOpReply; public: - osd_reqid_t get_reqid() { return osd_reqid_t(head.client_inst.name, head.client_inc, head.tid); } + osd_reqid_t get_reqid() { return osd_reqid_t(head.client_inst.name, + head.client_inc, + le64_to_cpu(head.tid)); } int get_client_inc() { return head.client_inc; } - tid_t get_client_tid() { return head.tid; } + tid_t get_client_tid() { return le64_to_cpu(head.tid); } entity_name_t get_client() { return head.client_inst.name; } entity_inst_t get_client_inst() { return head.client_inst; } @@ -76,7 +78,7 @@ public: object_t get_oid() { return object_t(head.oid); } pg_t get_pg() { return head.layout.ol_pgid; } ceph_object_layout get_layout() { return head.layout; } - epoch_t get_map_epoch() { return head.osdmap_epoch; } + epoch_t get_map_epoch() { return le32_to_cpu(head.osdmap_epoch); } eversion_t get_version() { return head.reassert_version; } @@ -103,11 +105,11 @@ public: memset(&head, 0, sizeof(head)); head.client_inst.name = asker.name.v; head.client_inst.addr = asker.addr.v; - head.tid = tid; + head.tid = cpu_to_le64(tid); head.client_inc = inc; head.oid = oid; head.layout = ol; - head.osdmap_epoch = mapepoch; + head.osdmap_epoch = cpu_to_le32(mapepoch); head.op = op; head.flags = CEPH_OSD_OP_ACK | CEPH_OSD_OP_SAFE; diff --git a/src/messages/MOSDOpReply.h b/src/messages/MOSDOpReply.h index ef770e43a5b1d..aafd9d61d98e3 100644 --- a/src/messages/MOSDOpReply.h +++ b/src/messages/MOSDOpReply.h @@ -33,7 +33,7 @@ class MOSDOpReply : public Message { ceph_osd_reply_head head; public: - long get_tid() { return head.tid; } + long get_tid() { return le64_to_cpu(head.tid); } object_t get_oid() { return head.oid; } pg_t get_pg() { return head.layout.ol_pgid; } int get_op() { return head.op; } @@ -52,7 +52,7 @@ class MOSDOpReply : public Message { void set_op(int op) { head.op = op; } // osdmap - epoch_t get_map_epoch() { return head.osdmap_epoch; } + epoch_t get_map_epoch() { return le32_to_cpu(head.osdmap_epoch); } public: @@ -64,7 +64,7 @@ public: head.flags = commit ? CEPH_OSD_OP_SAFE:0; head.oid = req->head.oid; head.layout = req->head.layout; - head.osdmap_epoch = e; + head.osdmap_epoch = cpu_to_le32(e); head.result = result; head.offset = req->head.offset; head.length = req->head.length; // speculative... OSD should ensure these are correct diff --git a/src/mon/MonMap.h b/src/mon/MonMap.h index 24770ace2384c..303ec37178cb7 100644 --- a/src/mon/MonMap.h +++ b/src/mon/MonMap.h @@ -68,8 +68,8 @@ class MonMap { void generate_fsid() { - fsid.major = ((uint64_t)rand() << 32) + rand(); - fsid.minor = ((uint64_t)rand() << 32) + rand(); + fsid.major = cpu_to_le64(((uint64_t)rand() << 32) + rand()); + fsid.minor = cpu_to_le64(((uint64_t)rand() << 32) + rand()); } // read from/write to a file diff --git a/src/mon/PGMonitor.cc b/src/mon/PGMonitor.cc index 5ef2f4b9ecdc9..18f7c4c008917 100644 --- a/src/mon/PGMonitor.cc +++ b/src/mon/PGMonitor.cc @@ -191,10 +191,10 @@ void PGMonitor::handle_statfs(MStatfs *statfs) memset(&reply->stfs, 0, sizeof(reply->stfs)); // these are in KB. - reply->stfs.f_total = 4*pg_map.total_osd_num_blocks; - reply->stfs.f_free = 4*pg_map.total_osd_num_blocks_avail; - reply->stfs.f_avail = 4*pg_map.total_osd_num_blocks_avail; - reply->stfs.f_objects = pg_map.total_osd_num_objects; + reply->stfs.f_total = cpu_to_le64(4*pg_map.total_osd_num_blocks); + reply->stfs.f_free = cpu_to_le64(4*pg_map.total_osd_num_blocks_avail); + reply->stfs.f_avail = cpu_to_le64(4*pg_map.total_osd_num_blocks_avail); + reply->stfs.f_objects = cpu_to_le64(pg_map.total_osd_num_objects); // reply mon->messenger->send_message(reply, statfs->get_source_inst()); diff --git a/src/osd/OSDMap.h b/src/osd/OSDMap.h index 118fb79072846..04bb7dbab1718 100644 --- a/src/osd/OSDMap.h +++ b/src/osd/OSDMap.h @@ -114,7 +114,7 @@ public: } Incremental(epoch_t e=0) : epoch(e), mon_epoch(0), new_max_osd(-1) { - fsid.major = fsid.minor = 0; + fsid.major = fsid.minor = cpu_to_le64(0); } }; @@ -144,7 +144,7 @@ private: pg_num(1<<5), localized_pg_num(1<<3), max_osd(0) { - fsid.major = fsid.minor = 0; + fsid.major = fsid.minor = cpu_to_le64(0); calc_pg_masks(); } diff --git a/src/osd/osd_types.h b/src/osd/osd_types.h index 44128fe713d29..a47be06997e02 100644 --- a/src/osd/osd_types.h +++ b/src/osd/osd_types.h @@ -176,11 +176,13 @@ public: eversion_t() : epoch(0), version(0) {} eversion_t(epoch_t e, version_t v) : epoch(e), version(v) {} - eversion_t(const ceph_eversion& ce) : epoch(ce.epoch), version(ce.version) {} + eversion_t(const ceph_eversion& ce) : + epoch(le32_to_cpu(ce.epoch)), + version(le64_to_cpu(ce.version)) {} operator ceph_eversion() { ceph_eversion c; - c.epoch = epoch; - c.version = version; + c.epoch = cpu_to_le32(epoch); + c.version = cpu_to_le64(version); return c; } }; -- 2.39.5