/* * Ceph MDS operations are specified in terms of a base ino and * relative path. Thus, the client can specify an operation on a * specific inode (e.g., a getattr due to fstat(2)), or as a path * relative to, say, the root directory. * * Normally, we limit ourselves to strict inode ops (no path component) * or dentry operations (a single path component relative to an ino). The * exception to this is open_root_dentry(), which will open the mount * point by name.
*/
staticunsigned fpos_off(loff_t p)
{ return p & OFFSET_MASK;
}
staticint fpos_cmp(loff_t l, loff_t r)
{ int v = ceph_frag_compare(fpos_frag(l), fpos_frag(r)); if (v) return v; return (int)(fpos_off(l) - fpos_off(r));
}
/* * make note of the last dentry we read, so we can * continue at the same lexicographical point, * regardless of what dir changes take place on the * server.
*/ staticint note_last_dentry(struct ceph_fs_client *fsc, struct ceph_dir_file_info *dfi, constchar *name, int len, unsigned next_offset)
{ char *buf = kmalloc(len+1, GFP_KERNEL); if (!buf) return -ENOMEM;
kfree(dfi->last_name);
dfi->last_name = buf;
memcpy(dfi->last_name, name, len);
dfi->last_name[len] = 0;
dfi->next_offset = next_offset;
doutc(fsc->client, "'%s'\n", dfi->last_name); return 0;
}
if (!cache_ctl->folio || ptr_pgoff != cache_ctl->folio->index) {
ceph_readdir_cache_release(cache_ctl);
cache_ctl->folio = filemap_lock_folio(&dir->i_data, ptr_pgoff); if (IS_ERR(cache_ctl->folio)) {
cache_ctl->folio = NULL;
doutc(cl, " folio %lu not found\n", ptr_pgoff); return ERR_PTR(-EAGAIN);
} /* reading/filling the cache are serialized by
i_rwsem, no need to use folio lock */
folio_unlock(cache_ctl->folio);
cache_ctl->dentries = kmap_local_folio(cache_ctl->folio, 0);
}
cache_ctl->index = idx & idx_mask;
rcu_read_lock();
spin_lock(&parent->d_lock); /* check i_size again here, because empty directory can be
* marked as complete while not holding the i_rwsem. */ if (ceph_dir_is_complete_ordered(dir) && ptr_pos < i_size_read(dir))
dentry = cache_ctl->dentries[cache_ctl->index]; else
dentry = NULL;
spin_unlock(&parent->d_lock); if (dentry && !lockref_get_not_dead(&dentry->d_lockref))
dentry = NULL;
rcu_read_unlock(); return dentry ? : ERR_PTR(-EAGAIN);
}
/* * When possible, we try to satisfy a readdir by peeking at the * dcache. We make this work by carefully ordering dentries on * d_children when we initially get results back from the MDS, and * falling back to a "normal" sync readdir if any dentries in the dir * are dropped. * * Complete dir indicates that we have all dentries in the dir. It is * defined IFF we hold CEPH_CAP_FILE_SHARED (which will be revoked by * the MDS if/when the directory is modified).
*/ staticint __dcache_readdir(struct file *file, struct dir_context *ctx, int shared_gen)
{ struct ceph_dir_file_info *dfi = file->private_data; struct dentry *parent = file->f_path.dentry; struct inode *dir = d_inode(parent); struct ceph_fs_client *fsc = ceph_inode_to_fs_client(dir); struct ceph_client *cl = ceph_inode_to_client(dir); struct dentry *dentry, *last = NULL; struct ceph_dentry_info *di; struct ceph_readdir_cache_control cache_ctl = {};
u64 idx = 0; int err = 0;
doutc(cl, "%p %llx.%llx v%u at %llx\n", dir, ceph_vinop(dir),
(unsigned)shared_gen, ctx->pos);
/* search start position */ if (ctx->pos > 2) {
u64 count = div_u64(i_size_read(dir), sizeof(struct dentry *)); while (count > 0) {
u64 step = count >> 1;
dentry = __dcache_find_get_entry(parent, idx + step,
&cache_ctl); if (!dentry) { /* use linear search */
idx = 0; break;
} if (IS_ERR(dentry)) {
err = PTR_ERR(dentry); goto out;
}
di = ceph_dentry(dentry);
spin_lock(&dentry->d_lock); if (fpos_cmp(di->offset, ctx->pos) < 0) {
idx += step + 1;
count -= step + 1;
} else {
count = step;
}
spin_unlock(&dentry->d_lock);
dput(dentry);
}
err = ceph_fscrypt_prepare_readdir(inode); if (err < 0) return err;
spin_lock(&ci->i_ceph_lock); /* request Fx cap. if have Fx, we don't need to release Fs cap
* for later create/unlink. */
__ceph_touch_fmode(ci, mdsc, CEPH_FILE_MODE_WR); /* can we use the dcache? */ if (ceph_test_mount_opt(fsc, DCACHE) &&
!ceph_test_mount_opt(fsc, NOASYNCREADDIR) &&
ceph_snap(inode) != CEPH_SNAPDIR &&
__ceph_dir_is_complete_ordered(ci) &&
__ceph_caps_issued_mask_metric(ci, CEPH_CAP_FILE_SHARED, 1)) { int shared_gen = atomic_read(&ci->i_shared_gen);
/* proceed with a normal readdir */
more: /* do we have the correct frag content buffered? */ if (need_send_readdir(dfi, ctx->pos)) { struct ceph_mds_request *req; int op = ceph_snap(inode) == CEPH_SNAPDIR ?
CEPH_MDS_OP_LSSNAP : CEPH_MDS_OP_READDIR;
/* discard old result, if any */ if (dfi->last_readdir) {
ceph_mdsc_put_request(dfi->last_readdir);
dfi->last_readdir = NULL;
}
if (is_hash_order(ctx->pos)) { /* fragtree isn't always accurate. choose frag
* based on previous reply when possible. */ if (frag == (unsigned)-1)
frag = ceph_choose_frag(ci, fpos_hash(ctx->pos),
NULL, NULL);
} else {
frag = fpos_frag(ctx->pos);
}
if (!dir_emit(ctx, rde->name, rde->name_len,
ceph_present_ino(inode->i_sb, le64_to_cpu(rde->inode.in->ino)),
le32_to_cpu(rde->inode.in->mode) >> 12)) { /* * NOTE: Here no need to put the 'dfi->last_readdir', * because when dir_emit stops us it's most likely * doesn't have enough memory, etc. So for next readdir * it will continue.
*/
doutc(cl, "filldir stopping us...\n"); return 0;
}
/* Reset the lengths to their original allocated vals */
ctx->pos++;
}
/* more frags? */ if (!ceph_frag_is_rightmost(dfi->frag)) {
frag = ceph_frag_next(dfi->frag); if (is_hash_order(ctx->pos)) {
loff_t new_pos = ceph_make_fpos(ceph_frag_value(frag),
dfi->next_offset, true); if (new_pos > ctx->pos)
ctx->pos = new_pos; /* keep last_name */
} else {
ctx->pos = ceph_make_fpos(frag, dfi->next_offset, false);
kfree(dfi->last_name);
dfi->last_name = NULL;
}
doutc(cl, "%p %llx.%llx next frag is %x\n", inode,
ceph_vinop(inode), frag); goto more;
}
dfi->file_info.flags |= CEPH_F_ATEND;
/* * if dir_release_count still matches the dir, no dentries * were released during the whole readdir, and we should have * the complete dir contents in our cache.
*/ if (atomic64_read(&ci->i_release_count) ==
dfi->dir_release_count) {
spin_lock(&ci->i_ceph_lock); if (dfi->dir_ordered_count ==
atomic64_read(&ci->i_ordered_count)) {
doutc(cl, " marking %p %llx.%llx complete and ordered\n",
inode, ceph_vinop(inode)); /* use i_size to track number of entries in
* readdir cache */
BUG_ON(dfi->readdir_cache_idx < 0);
i_size_write(inode, dfi->readdir_cache_idx * sizeof(struct dentry*));
} else {
doutc(cl, " marking %llx.%llx complete\n",
ceph_vinop(inode));
}
__ceph_dir_set_complete(ci, dfi->dir_release_count,
dfi->dir_ordered_count);
spin_unlock(&ci->i_ceph_lock);
}
doutc(cl, "%p %llx.%llx file %p done.\n", inode, ceph_vinop(inode),
file); return 0;
}
/* * discard buffered readdir content on seekdir(0), or seek to new frag, * or seek prior to current chunk
*/ staticbool need_reset_readdir(struct ceph_dir_file_info *dfi, loff_t new_pos)
{ struct ceph_mds_reply_info_parsed *rinfo;
loff_t chunk_offset; if (new_pos == 0) returntrue; if (is_hash_order(new_pos)) { /* no need to reset last_name for a forward seek when
* dentries are sorted in hash order */
} elseif (dfi->frag != fpos_frag(new_pos)) { returntrue;
}
rinfo = dfi->last_readdir ? &dfi->last_readdir->r_reply_info : NULL; if (!rinfo || !rinfo->dir_nr) returntrue;
chunk_offset = rinfo->dir_entries[0].offset; return new_pos < chunk_offset ||
is_hash_order(new_pos) != is_hash_order(chunk_offset);
}
res = d_splice_alias(inode, dentry);
doutc(cl, "ENOENT on snapdir %p '%pd', linking to " "snapdir %p %llx.%llx. Spliced dentry %p\n",
dentry, dentry, inode, ceph_vinop(inode), res); if (res)
dentry = res;
} return dentry;
}
/* * Figure out final result of a lookup/open request. * * Mainly, make sure we return the final req->r_dentry (if it already * existed) in place of the original VFS-provided dentry when they * differ. * * Gracefully handle the case where the MDS replies with -ENOENT and * no trace (which it may do, at its discretion, e.g., if it doesn't * care to issue a lease on the negative dentry).
*/ struct dentry *ceph_finish_lookup(struct ceph_mds_request *req, struct dentry *dentry, int err)
{ struct ceph_client *cl = req->r_mdsc->fsc->client;
if (err == -ENOENT) { /* no trace? */
err = 0; if (!req->r_reply_info.head->is_dentry) {
doutc(cl, "ENOENT and no trace, dentry %p inode %llx.%llx\n",
dentry, ceph_vinop(d_inode(dentry))); if (d_really_is_positive(dentry)) {
d_drop(dentry);
err = -ENOENT;
} else {
d_add(dentry, NULL);
}
}
} if (err)
dentry = ERR_PTR(err); elseif (dentry != req->r_dentry)
dentry = dget(req->r_dentry); /* we got spliced */ else
dentry = NULL; return dentry;
}
/* * Look up a single dir entry. If there is a lookup intent, inform * the MDS so that it gets our 'caps wanted' value in a single op.
*/ staticstruct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry, unsignedint flags)
{ struct ceph_fs_client *fsc = ceph_sb_to_fs_client(dir->i_sb); struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(dir->i_sb); struct ceph_client *cl = fsc->client; struct ceph_mds_request *req; int op; int mask; int err;
/* * If we do a create but get no trace back from the MDS, follow up with * a lookup (the VFS expects us to link up the provided dentry).
*/ int ceph_handle_notrace_create(struct inode *dir, struct dentry *dentry)
{ struct dentry *result = ceph_lookup(dir, dentry, 0);
if (result && !IS_ERR(result)) { /* * We created the item, then did a lookup, and found * it was already linked to another inode we already * had in our cache (and thus got spliced). To not * confuse VFS (especially when inode is a directory), * we don't link our dentry to that inode, return an * error instead. * * This event should be rare and it happens only when * we talk to old MDS. Recent MDS does not send traceless * reply for request that creates new inode.
*/
d_drop(result); return -ESTALE;
} return PTR_ERR(result);
}
/* If op failed, mark everyone involved for errors */ if (result) { struct ceph_path_info path_info = {0}; char *path = ceph_mdsc_build_path(mdsc, dentry, &path_info, 0);
/* mark error on parent + clear complete */
mapping_set_error(req->r_parent->i_mapping, result);
ceph_dir_clear_complete(req->r_parent);
/* drop the dentry -- we don't know its status */ if (!d_unhashed(dentry))
d_drop(dentry);
/* mark inode itself for an error (since metadata is bogus) */
mapping_set_error(req->r_old_inode->i_mapping, result);
/* If we didn't get anything, return 0 */ if (!got) return 0;
spin_lock(&dentry->d_lock);
di = ceph_dentry(dentry); /* * - We are holding Fx, which implies Fs caps. * - Only support async unlink for primary linkage
*/ if (atomic_read(&ci->i_shared_gen) != di->lease_shared_gen ||
!(di->flags & CEPH_DENTRY_PRIMARY_LINK))
want = 0;
spin_unlock(&dentry->d_lock);
/* Do we still want what we've got? */ if (want == got) return got;
ceph_put_cap_refs(ci, got); return 0;
}
/* * rmdir and unlink are differ only by the metadata op code
*/ staticint ceph_unlink(struct inode *dir, struct dentry *dentry)
{ struct ceph_fs_client *fsc = ceph_sb_to_fs_client(dir->i_sb); struct ceph_client *cl = fsc->client; struct ceph_mds_client *mdsc = fsc->mdsc; struct inode *inode = d_inode(dentry); struct ceph_mds_request *req; bool try_async = ceph_test_mount_opt(fsc, ASYNC_DIROPS); struct dentry *dn; int err = -EROFS; int op; char *path;
err = ceph_mdsc_submit_request(mdsc, dir, req); if (!err) { /* * We have enough caps, so we assume that the unlink * will succeed. Fix up the target inode and dcache.
*/
drop_nlink(inode);
d_delete(dentry);
} else {
spin_lock(&fsc->async_unlink_conflict_lock);
hash_del_rcu(&di->hnode);
spin_unlock(&fsc->async_unlink_conflict_lock);
doutc(cl, "%llx.%llx/'%pd' to %llx.%llx/'%pd'\n",
ceph_vinop(old_dir), old_dentry, ceph_vinop(new_dir),
new_dentry);
req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS); if (IS_ERR(req)) return PTR_ERR(req);
ihold(old_dir);
req->r_dentry = dget(new_dentry);
req->r_num_caps = 2;
req->r_old_dentry = dget(old_dentry);
req->r_old_dentry_dir = old_dir;
req->r_parent = new_dir;
ihold(new_dir);
set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
req->r_old_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_XATTR_EXCL;
req->r_old_dentry_unless = CEPH_CAP_FILE_EXCL;
req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_XATTR_EXCL;
req->r_dentry_unless = CEPH_CAP_FILE_EXCL; /* release LINK_RDCACHE on source inode (mds will lock it) */
req->r_old_inode_drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL; if (d_really_is_positive(new_dentry)) {
req->r_inode_drop =
ceph_drop_caps_for_unlink(d_inode(new_dentry));
}
err = ceph_mdsc_do_request(mdsc, old_dir, req); if (!err && !req->r_reply_info.head->is_dentry) { /* * Normally d_move() is done by fill_trace (called by * do_request, above). If there is no trace, we need * to do it here.
*/
d_move(old_dentry, new_dentry);
}
ceph_mdsc_put_request(req); return err;
}
/* * Move dentry to tail of mdsc->dentry_leases list when lease is updated. * Leases at front of the list will expire first. (Assume all leases have * similar duration) * * Called under dentry->d_lock.
*/ void __ceph_dentry_lease_touch(struct ceph_dentry_info *di)
{ struct dentry *dn = di->dentry; struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(dn->d_sb)->mdsc; struct ceph_client *cl = mdsc->fsc->client;
/* * When dir lease is used, add dentry to tail of mdsc->dentry_dir_leases * list if it's not in the list, otherwise set 'referenced' flag. * * Called under dentry->d_lock.
*/ void __ceph_dentry_dir_lease_touch(struct ceph_dentry_info *di)
{ struct dentry *dn = di->dentry; struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(dn->d_sb)->mdsc; struct ceph_client *cl = mdsc->fsc->client;
if (!list_empty(&di->lease_list)) { if (di->flags & CEPH_DENTRY_LEASE_LIST) { /* don't remove dentry from dentry lease list
* if its lease is valid */ if (__dentry_lease_is_valid(di)) return;
} else {
di->flags |= CEPH_DENTRY_REFERENCED; return;
}
}
list = lwc->dir_lease ? &mdsc->dentry_dir_leases : &mdsc->dentry_leases;
spin_lock(&mdsc->dentry_list_lock);
list_for_each_entry_safe(di, tmp, list, lease_list) { if (!lwc->nr_to_scan) break;
--lwc->nr_to_scan;
dentry = di->dentry; if (last == dentry) break;
if (!spin_trylock(&dentry->d_lock)) continue;
if (__lockref_is_dead(&dentry->d_lockref)) {
list_del_init(&di->lease_list); goto next;
}
if (lwc->dir_lease)
ret = __dir_lease_check(dentry, lwc); else
ret = __dentry_lease_check(dentry); if (ret & TOUCH) { /* move it into tail of dir lease list */
__dentry_dir_lease_touch(mdsc, di); if (!last)
last = dentry;
} if (ret & DELETE) { /* stale lease */
di->flags &= ~CEPH_DENTRY_REFERENCED; if (dentry->d_lockref.count > 0) { /* update_dentry_lease() will re-add * it to lease list, or * ceph_d_delete() will return 1 when
* last reference is dropped */
list_del_init(&di->lease_list);
} else {
di->flags |= CEPH_DENTRY_SHRINK_LIST;
list_move_tail(&di->lease_list, &dispose);
dget_dlock(dentry);
}
}
next:
spin_unlock(&dentry->d_lock); if (ret & STOP) break;
}
spin_unlock(&mdsc->dentry_list_lock);
while (!list_empty(&dispose)) {
di = list_first_entry(&dispose, struct ceph_dentry_info,
lease_list);
dentry = di->dentry;
spin_lock(&dentry->d_lock);
int ret = __dir_lease_try_check(dentry); if (ret == -EBUSY) return KEEP; if (ret > 0) { if (time_before(jiffies, di->time + lwc->dir_lease_ttl)) return STOP; /* Move dentry to tail of dir lease list if we don't want * to delete it. So dentries in the list are checked in a
* round robin manner */ if (!lwc->expire_dir_lease) return TOUCH; if (dentry->d_lockref.count > 0 ||
(di->flags & CEPH_DENTRY_REFERENCED)) return TOUCH; /* invalidate dir lease */
di->lease_shared_gen = 0;
} returnDELETE;
}
/* * Check if dentry lease is valid. If not, delete the lease. Try to * renew if the least is more than half up.
*/ staticbool __dentry_lease_is_valid(struct ceph_dentry_info *di)
{ struct ceph_mds_session *session;
if (!di->lease_gen) returnfalse;
session = di->lease_session; if (session) {
u32 gen; unsignedlong ttl;
gen = atomic_read(&session->s_cap_gen);
ttl = session->s_cap_ttl;
if (di->lease_gen == gen &&
time_before(jiffies, ttl) &&
time_before(jiffies, di->time)) returntrue;
}
di->lease_gen = 0; returnfalse;
}
spin_lock(&dentry->d_lock);
di = ceph_dentry(dentry); if (di && __dentry_lease_is_valid(di)) {
valid = 1;
if (di->lease_renew_after &&
time_after(jiffies, di->lease_renew_after)) { /* * We should renew. If we're in RCU walk mode * though, we can't do that so just return * -ECHILD.
*/ if (flags & LOOKUP_RCU) {
valid = -ECHILD;
} else {
session = ceph_get_mds_session(di->lease_session);
seq = di->lease_seq;
di->lease_renew_after = 0;
di->lease_renew_from = jiffies;
}
}
}
spin_unlock(&dentry->d_lock);
/* * When the VFS prunes a dentry from the cache, we need to clear the * complete flag on the parent directory. * * Called under dentry->d_lock.
*/ staticvoid ceph_d_prune(struct dentry *dentry)
{ struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(dentry->d_sb); struct ceph_client *cl = mdsc->fsc->client; struct ceph_inode_info *dir_ci; struct ceph_dentry_info *di;
doutc(cl, "dentry %p '%pd'\n", dentry, dentry);
/* do we have a valid parent? */ if (IS_ROOT(dentry)) return;
/* we hold d_lock, so d_parent is stable */
dir_ci = ceph_inode(d_inode(dentry->d_parent)); if (dir_ci->i_vino.snap == CEPH_SNAPDIR) return;
/* who calls d_delete() should also disable dcache readdir */ if (d_really_is_negative(dentry)) return;
/* d_fsdata does not get cleared until d_release */ if (!d_unhashed(dentry)) {
__ceph_dir_clear_complete(dir_ci); return;
}
/* Disable dcache readdir just in case that someone called d_drop() * or d_invalidate(), but MDS didn't revoke CEPH_CAP_FILE_SHARED
* properly (dcache readdir is still enabled) */
di = ceph_dentry(dentry); if (di->offset > 0 &&
di->lease_shared_gen == atomic_read(&dir_ci->i_shared_gen))
__ceph_dir_clear_ordered(dir_ci);
}
/* * read() on a dir. This weird interface hack only works if mounted * with '-o dirstat'.
*/ static ssize_t ceph_read_dir(struct file *file, char __user *buf, size_t size,
loff_t *ppos)
{ struct ceph_dir_file_info *dfi = file->private_data; struct inode *inode = file_inode(file); struct ceph_inode_info *ci = ceph_inode(inode); int left; constint bufsize = 1024;
if (!ceph_test_mount_opt(ceph_sb_to_fs_client(inode->i_sb), DIRSTAT)) return -EISDIR;
/* * Return name hash for a given dentry. This is dependent on * the parent directory's hash function.
*/ unsigned ceph_dentry_hash(struct inode *dir, struct dentry *dn)
{ struct ceph_inode_info *dci = ceph_inode(dir); unsigned hash;
switch (dci->i_dir_layout.dl_dir_hash) { case 0: /* for backward compat */ case CEPH_STR_HASH_LINUX: return dn->d_name.hash;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.