From 9e7f2a12d16484bf6062bb2996e75345fe4c74c5 Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Thu, 17 Apr 2008 14:01:13 -0700 Subject: [PATCH] consolidate and clean up FILE_CAPS --- src/client/Client.cc | 10 +++---- src/client/Client.h | 12 ++++----- src/include/ceph_fs.h | 50 +++++++++++++++++++++++++++++++++++ src/include/types.h | 22 +-------------- src/kernel/file.c | 4 +-- src/kernel/inode.c | 4 +-- src/kernel/super.c | 2 +- src/kernel/super.h | 42 +---------------------------- src/mds/Locker.cc | 5 +--- src/mds/Server.cc | 12 ++++----- src/messages/MClientRequest.h | 2 +- 11 files changed, 76 insertions(+), 89 deletions(-) diff --git a/src/client/Client.cc b/src/client/Client.cc index 8cf6df0a6717b..62660e1da2f01 100644 --- a/src/client/Client.cc +++ b/src/client/Client.cc @@ -2753,7 +2753,7 @@ int Client::_open(const char *path, int flags, mode_t mode, Fh **fhp) req->head.args.open.flags = flags; req->head.args.open.mode = mode; - int cmode = file_flags_to_mode(flags); + int cmode = ceph_flags_to_mode(flags); // FIXME where does FUSE maintain user information req->set_caller_uid(getuid()); @@ -3047,7 +3047,7 @@ int Client::_read(Fh *f, off_t offset, off_t size, bufferlist *bl) movepos = true; } - bool lazy = f->mode == FILE_MODE_LAZY; + bool lazy = f->mode == CEPH_FILE_MODE_LAZY; // wait for RD cap and/or a valid file size while (1) { @@ -3206,7 +3206,7 @@ int Client::_write(Fh *f, off_t offset, off_t size, const char *buf) unlock_fh_pos(f); } - bool lazy = f->mode == FILE_MODE_LAZY; + bool lazy = f->mode == CEPH_FILE_MODE_LAZY; dout(10) << "cur file size is " << in->inode.size << dendl; @@ -3530,7 +3530,7 @@ int Client::lazyio_propogate(int fd, off_t offset, size_t count) Fh *f = fd_map[fd]; Inode *in = f->inode; - if (f->mode & FILE_MODE_LAZY) { + if (f->mode & CEPH_FILE_MODE_LAZY) { // wait for lazy cap while ((in->file_caps() & CEPH_CAP_LAZYIO) == 0) { dout(7) << " don't have lazy cap, waiting" << dendl; @@ -3566,7 +3566,7 @@ int Client::lazyio_synchronize(int fd, off_t offset, size_t count) Fh *f = fd_map[fd]; Inode *in = f->inode; - if (f->mode & FILE_MODE_LAZY) { + if (f->mode & CEPH_FILE_MODE_LAZY) { // wait for lazy cap while ((in->file_caps() & CEPH_CAP_LAZYIO) == 0) { dout(7) << " don't have lazy cap, waiting" << dendl; diff --git a/src/client/Client.h b/src/client/Client.h index 960d4ce5983a6..0d08c0135ee1c 100644 --- a/src/client/Client.h +++ b/src/client/Client.h @@ -262,14 +262,14 @@ class Inode { } void add_open(int cmode) { - if (cmode & FILE_MODE_R) num_open_rd++; - if (cmode & FILE_MODE_W) num_open_wr++; - if (cmode & FILE_MODE_LAZY) num_open_lazy++; + if (cmode & CEPH_FILE_MODE_RD) num_open_rd++; + if (cmode & CEPH_FILE_MODE_WR) num_open_wr++; + if (cmode & CEPH_FILE_MODE_LAZY) num_open_lazy++; } void sub_open(int cmode) { - if (cmode & FILE_MODE_R) num_open_rd--; - if (cmode & FILE_MODE_W) num_open_wr--; - if (cmode & FILE_MODE_LAZY) num_open_lazy--; + if (cmode & CEPH_FILE_MODE_RD) num_open_rd--; + if (cmode & CEPH_FILE_MODE_WR) num_open_wr--; + if (cmode & CEPH_FILE_MODE_LAZY) num_open_lazy--; } int authority(const string& dname) { diff --git a/src/include/ceph_fs.h b/src/include/ceph_fs.h index 57f9fa0b297f1..20449d1a89959 100644 --- a/src/include/ceph_fs.h +++ b/src/include/ceph_fs.h @@ -9,10 +9,12 @@ #ifdef __KERNEL__ # include # include +# include #else # include # include "inttypes.h" # include "byteorder.h" +# include #endif #define CEPH_MON_PORT 12345 @@ -494,6 +496,33 @@ struct ceph_mds_reply_dirfrag { __u32 dist[]; } __attribute__ ((packed)); +/* file access modes */ +#define CEPH_FILE_MODE_PIN 0 +#define CEPH_FILE_MODE_RD 1 +#define CEPH_FILE_MODE_WR 2 +#define CEPH_FILE_MODE_RDWR 3 /* RD | WR */ +#define CEPH_FILE_MODE_LAZY 4 +#define CEPH_FILE_MODE_NUM 8 /* bc these are bit fields.. mostly */ + +static inline int ceph_flags_to_mode(int flags) +{ + if ((flags & O_DIRECTORY) == O_DIRECTORY) + return CEPH_FILE_MODE_PIN; +#ifdef O_LAZY + if (flags & O_LAZY) + return CEPH_FILE_MODE_LAZY; +#endif + if ((flags & O_APPEND) == O_APPEND) + flags |= O_WRONLY; + + flags &= O_ACCMODE; + if ((flags & O_RDWR) == O_RDWR) + return CEPH_FILE_MODE_RDWR; + if ((flags & O_WRONLY) == O_WRONLY) + return CEPH_FILE_MODE_WR; + return CEPH_FILE_MODE_RD; +} + /* client file caps */ #define CEPH_CAP_PIN 1 /* no specific capabilities beyond the pin */ #define CEPH_CAP_RDCACHE 2 /* client can cache reads */ @@ -504,6 +533,27 @@ struct ceph_mds_reply_dirfrag { #define CEPH_CAP_LAZYIO 64 /* client can perform lazy io */ #define CEPH_CAP_EXCL 128 /* exclusive/loner access */ +static inline int ceph_caps_for_mode(int mode) +{ + switch (mode) { + case CEPH_FILE_MODE_PIN: + return CEPH_CAP_PIN; + case CEPH_FILE_MODE_RD: + return CEPH_CAP_PIN | + CEPH_CAP_RD | CEPH_CAP_RDCACHE; + case CEPH_FILE_MODE_RDWR: + return CEPH_CAP_PIN | + CEPH_CAP_RD | CEPH_CAP_RDCACHE | + CEPH_CAP_WR | CEPH_CAP_WRBUFFER | + CEPH_CAP_EXCL; + case CEPH_FILE_MODE_WR: + return CEPH_CAP_PIN | + CEPH_CAP_WR | CEPH_CAP_WRBUFFER | + CEPH_CAP_EXCL; + } + return 0; +} + enum { CEPH_CAP_OP_GRANT, /* mds->client grant */ CEPH_CAP_OP_ACK, /* client->mds ack (if prior grant was a recall) */ diff --git a/src/include/types.h b/src/include/types.h index 0122a86102aab..2a9192f40c36b 100644 --- a/src/include/types.h +++ b/src/include/types.h @@ -151,28 +151,8 @@ namespace __gnu_cxx { } -// these are bit masks -#define FILE_MODE_R 1 -#define FILE_MODE_W 2 -#define FILE_MODE_RW (1|2) -#define FILE_MODE_LAZY 4 -#define FILE_MODE_PIN 8 - -static inline int file_flags_to_mode(int flags) { - if (flags & O_DIRECTORY) - return FILE_MODE_PIN; - if (flags & O_LAZY) - return FILE_MODE_LAZY; - if (flags & O_WRONLY) - return FILE_MODE_W; - if (flags & O_RDWR) - return FILE_MODE_RW; - if (flags & O_APPEND) - return FILE_MODE_W; - return FILE_MODE_R; -} static inline bool file_mode_is_readonly(int mode) { - return (mode & FILE_MODE_W) == 0; + return (mode & CEPH_FILE_MODE_WR) == 0; } diff --git a/src/kernel/file.c b/src/kernel/file.c index f7c3de0150f8e..884c832d53114 100644 --- a/src/kernel/file.c +++ b/src/kernel/file.c @@ -36,7 +36,7 @@ prepare_open_request(struct super_block *sb, struct dentry *dentry, req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_OPEN, pathbase, path, 0, 0); req->r_expects_cap = 1; - req->r_fmode = ceph_file_mode(flags); + req->r_fmode = ceph_flags_to_mode(flags); kfree(path); if (!IS_ERR(req)) { rhead = req->r_request->front.iov_base; @@ -81,7 +81,7 @@ int ceph_open(struct inode *inode, struct file *file) if (S_ISDIR(inode->i_mode)) flags = O_DIRECTORY; - fmode = ceph_file_mode(flags); + fmode = ceph_flags_to_mode(flags); wantcaps = ceph_caps_for_mode(fmode); dout(5, "open inode %p ino %llx file %p\n", inode, diff --git a/src/kernel/inode.c b/src/kernel/inode.c index 911ffa3ce5c77..62daebbe9ee8f 100644 --- a/src/kernel/inode.c +++ b/src/kernel/inode.c @@ -691,11 +691,11 @@ void __ceph_remove_cap(struct ceph_inode_cap *cap) dout(10, "__ceph_remove_cap %p from %p\n", cap, &cap->ci->vfs_inode); /* remove from session list */ - list_del(&cap->session_caps); + list_del_init(&cap->session_caps); session->s_nr_caps--; /* remove from inode list */ - list_del(&cap->ci_caps); + list_del_init(&cap->ci_caps); cap->session = 0; cap->mds = -1; /* mark unused */ diff --git a/src/kernel/super.c b/src/kernel/super.c index e16317496613c..5e2fd01fb0851 100644 --- a/src/kernel/super.c +++ b/src/kernel/super.c @@ -141,7 +141,7 @@ static struct inode *ceph_alloc_inode(struct super_block *sb) INIT_LIST_HEAD(&ci->i_caps); for (i = 0; i < STATIC_CAPS; i++) ci->i_static_caps[i].mds = -1; - for (i = 0; i < 4; i++) + for (i = 0; i < CEPH_FILE_MODE_NUM; i++) ci->i_nr_by_mode[i] = 0; init_waitqueue_head(&ci->i_cap_wq); diff --git a/src/kernel/super.h b/src/kernel/super.h index 05ff42cacc91e..8de815f57db83 100644 --- a/src/kernel/super.h +++ b/src/kernel/super.h @@ -136,12 +136,6 @@ struct ceph_inode_frag_map_item { #define STATIC_CAPS 2 -enum { - FILE_MODE_PIN, - FILE_MODE_RDONLY, - FILE_MODE_RDWR, - FILE_MODE_WRONLY -}; struct ceph_inode_info { u64 i_ceph_ino; @@ -164,7 +158,7 @@ struct ceph_inode_info { wait_queue_head_t i_cap_wq; unsigned long i_hold_caps_until; /* jiffies */ - int i_nr_by_mode[4]; + int i_nr_by_mode[CEPH_FILE_MODE_NUM]; loff_t i_max_size; /* size authorized by mds */ loff_t i_reported_size; /* (max_)size reported to or requested of mds */ loff_t i_wanted_max_size; /* offset we'd like to write too */ @@ -271,27 +265,6 @@ static inline int __ceph_caps_used(struct ceph_inode_info *ci) return used; } -static inline int ceph_caps_for_mode(int mode) -{ - switch (mode) { - case FILE_MODE_PIN: - return CEPH_CAP_PIN; - case FILE_MODE_RDONLY: - return CEPH_CAP_PIN | - CEPH_CAP_RD | CEPH_CAP_RDCACHE; - case FILE_MODE_RDWR: - return CEPH_CAP_PIN | - CEPH_CAP_RD | CEPH_CAP_RDCACHE | - CEPH_CAP_WR | CEPH_CAP_WRBUFFER | - CEPH_CAP_EXCL; - case FILE_MODE_WRONLY: - return CEPH_CAP_PIN | - CEPH_CAP_WR | CEPH_CAP_WRBUFFER | - CEPH_CAP_EXCL; - } - return 0; -} - static inline int __ceph_caps_file_wanted(struct ceph_inode_info *ci) { int want = 0; @@ -310,19 +283,6 @@ static inline int __ceph_caps_wanted(struct ceph_inode_info *ci) return w; } -static inline int ceph_file_mode(int flags) -{ - if ((flags & O_DIRECTORY) == O_DIRECTORY) - return FILE_MODE_PIN; - if ((flags & O_ACCMODE) == O_RDWR) - return FILE_MODE_RDWR; - if ((flags & O_ACCMODE) == O_WRONLY) - return FILE_MODE_WRONLY; - if ((flags & O_ACCMODE) == O_RDONLY) - return FILE_MODE_RDONLY; - return FILE_MODE_RDWR; /* not -EINVAL under Linux, strangely */ -} - static inline void __ceph_get_fmode(struct ceph_inode_info *ci, int mode) { ci->i_nr_by_mode[mode]++; diff --git a/src/mds/Locker.cc b/src/mds/Locker.cc index e54e1bada6d77..27c88fdc84dcf 100644 --- a/src/mds/Locker.cc +++ b/src/mds/Locker.cc @@ -494,10 +494,7 @@ Capability* Locker::issue_new_caps(CInode *in, // my needs assert(session->inst.name.is_client()); int my_client = session->inst.name.num(); - int my_want = 0; - if (mode & FILE_MODE_PIN) my_want |= CEPH_CAP_PIN; - if (mode & FILE_MODE_R) my_want |= CEPH_CAP_RDCACHE | CEPH_CAP_RD; - if (mode & FILE_MODE_W) my_want |= CEPH_CAP_WRBUFFER | CEPH_CAP_WR | CEPH_CAP_EXCL; + int my_want = ceph_caps_for_mode(mode); // register a capability Capability *cap = in->get_client_cap(my_client); diff --git a/src/mds/Server.cc b/src/mds/Server.cc index 1fff74489dbbc..04e19c2694b46 100644 --- a/src/mds/Server.cc +++ b/src/mds/Server.cc @@ -3925,7 +3925,7 @@ void Server::handle_client_open(MDRequest *mdr) MClientRequest *req = mdr->client_request; int flags = req->head.args.open.flags; - int cmode = file_flags_to_mode(req->head.args.open.flags); + int cmode = ceph_flags_to_mode(req->head.args.open.flags); bool need_auth = !file_mode_is_readonly(cmode) || (flags & O_TRUNC); @@ -3935,7 +3935,7 @@ void Server::handle_client_open(MDRequest *mdr) if (!cur) return; // can only open a dir with mode FILE_MODE_PIN, at least for now. - if (cur->inode.is_dir()) cmode = FILE_MODE_PIN; + if (cur->inode.is_dir()) cmode = CEPH_FILE_MODE_PIN; dout(10) << "open flags = " << flags << ", filemode = " << cmode @@ -3981,8 +3981,8 @@ void Server::handle_client_open(MDRequest *mdr) void Server::_do_open(MDRequest *mdr, CInode *cur) { MClientRequest *req = mdr->client_request; - int cmode = file_flags_to_mode(req->head.args.open.flags); - if (cur->inode.is_dir()) cmode = FILE_MODE_PIN; + int cmode = ceph_flags_to_mode(req->head.args.open.flags); + if (cur->inode.is_dir()) cmode = CEPH_FILE_MODE_PIN; // register new cap Capability *cap = mds->locker->issue_new_caps(cur, cmode, mdr->session); @@ -3998,8 +3998,8 @@ void Server::_do_open(MDRequest *mdr, CInode *cur) // hit pop mdr->now = g_clock.now(); - if (cmode == FILE_MODE_RW || - cmode == FILE_MODE_W) + if (cmode == CEPH_FILE_MODE_RDWR || + cmode == CEPH_FILE_MODE_WR) mds->balancer->hit_inode(mdr->now, cur, META_POP_IWR); else mds->balancer->hit_inode(mdr->now, cur, META_POP_IRD, diff --git a/src/messages/MClientRequest.h b/src/messages/MClientRequest.h index 9ad89f8234720..086b6301024d5 100644 --- a/src/messages/MClientRequest.h +++ b/src/messages/MClientRequest.h @@ -96,7 +96,7 @@ public: } bool open_file_mode_is_readonly() { - return file_mode_is_readonly(file_flags_to_mode(head.args.open.flags)); + return file_mode_is_readonly(ceph_flags_to_mode(head.args.open.flags)); } bool is_idempotent() { if (head.op == CEPH_MDS_OP_OPEN) -- 2.39.5