123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486 |
- #include <linux/fs.h>
- #include <linux/types.h>
- #include <linux/slab.h>
- #include <linux/namei.h>
- #include <cluster/masklog.h>
- #include "ocfs2.h"
- #include "alloc.h"
- #include "dcache.h"
- #include "dlmglue.h"
- #include "file.h"
- #include "inode.h"
- #include "ocfs2_trace.h"
- void ocfs2_dentry_attach_gen(struct dentry *dentry)
- {
- unsigned long gen =
- OCFS2_I(d_inode(dentry->d_parent))->ip_dir_lock_gen;
- BUG_ON(d_inode(dentry));
- dentry->d_fsdata = (void *)gen;
- }
- static int ocfs2_dentry_revalidate(struct dentry *dentry, unsigned int flags)
- {
- struct inode *inode;
- int ret = 0;
- struct ocfs2_super *osb;
- if (flags & LOOKUP_RCU)
- return -ECHILD;
- inode = d_inode(dentry);
- osb = OCFS2_SB(dentry->d_sb);
- trace_ocfs2_dentry_revalidate(dentry, dentry->d_name.len,
- dentry->d_name.name);
-
- if (inode == NULL) {
- unsigned long gen = (unsigned long) dentry->d_fsdata;
- unsigned long pgen;
- spin_lock(&dentry->d_lock);
- pgen = OCFS2_I(d_inode(dentry->d_parent))->ip_dir_lock_gen;
- spin_unlock(&dentry->d_lock);
- trace_ocfs2_dentry_revalidate_negative(dentry->d_name.len,
- dentry->d_name.name,
- pgen, gen);
- if (gen != pgen)
- goto bail;
- goto valid;
- }
- BUG_ON(!osb);
- if (inode == osb->root_inode || is_bad_inode(inode))
- goto bail;
- spin_lock(&OCFS2_I(inode)->ip_lock);
-
- if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
- spin_unlock(&OCFS2_I(inode)->ip_lock);
- trace_ocfs2_dentry_revalidate_delete(
- (unsigned long long)OCFS2_I(inode)->ip_blkno);
- goto bail;
- }
- spin_unlock(&OCFS2_I(inode)->ip_lock);
-
- if (inode->i_nlink == 0) {
- trace_ocfs2_dentry_revalidate_orphaned(
- (unsigned long long)OCFS2_I(inode)->ip_blkno,
- S_ISDIR(inode->i_mode));
- goto bail;
- }
-
- if (!dentry->d_fsdata) {
- trace_ocfs2_dentry_revalidate_nofsdata(
- (unsigned long long)OCFS2_I(inode)->ip_blkno);
- goto bail;
- }
- valid:
- ret = 1;
- bail:
- trace_ocfs2_dentry_revalidate_ret(ret);
- return ret;
- }
- static int ocfs2_match_dentry(struct dentry *dentry,
- u64 parent_blkno,
- int skip_unhashed)
- {
- struct inode *parent;
-
- if (!dentry->d_fsdata)
- return 0;
- if (!dentry->d_parent)
- return 0;
- if (skip_unhashed && d_unhashed(dentry))
- return 0;
- parent = d_inode(dentry->d_parent);
-
- if (!parent)
- return 0;
-
- if (OCFS2_I(parent)->ip_blkno != parent_blkno)
- return 0;
- return 1;
- }
- struct dentry *ocfs2_find_local_alias(struct inode *inode,
- u64 parent_blkno,
- int skip_unhashed)
- {
- struct dentry *dentry;
- spin_lock(&inode->i_lock);
- hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
- spin_lock(&dentry->d_lock);
- if (ocfs2_match_dentry(dentry, parent_blkno, skip_unhashed)) {
- trace_ocfs2_find_local_alias(dentry->d_name.len,
- dentry->d_name.name);
- dget_dlock(dentry);
- spin_unlock(&dentry->d_lock);
- spin_unlock(&inode->i_lock);
- return dentry;
- }
- spin_unlock(&dentry->d_lock);
- }
- spin_unlock(&inode->i_lock);
- return NULL;
- }
- DEFINE_SPINLOCK(dentry_attach_lock);
- int ocfs2_dentry_attach_lock(struct dentry *dentry,
- struct inode *inode,
- u64 parent_blkno)
- {
- int ret;
- struct dentry *alias;
- struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
- trace_ocfs2_dentry_attach_lock(dentry->d_name.len, dentry->d_name.name,
- (unsigned long long)parent_blkno, dl);
-
- if (!inode)
- return 0;
- if (d_really_is_negative(dentry) && dentry->d_fsdata) {
-
- dentry->d_fsdata = dl = NULL;
- }
- if (dl) {
- mlog_bug_on_msg(dl->dl_parent_blkno != parent_blkno,
- " \"%pd\": old parent: %llu, new: %llu\n",
- dentry,
- (unsigned long long)parent_blkno,
- (unsigned long long)dl->dl_parent_blkno);
- return 0;
- }
- alias = ocfs2_find_local_alias(inode, parent_blkno, 0);
- if (alias) {
-
- dl = alias->d_fsdata;
- mlog_bug_on_msg(!dl, "parent %llu, ino %llu\n",
- (unsigned long long)parent_blkno,
- (unsigned long long)OCFS2_I(inode)->ip_blkno);
- mlog_bug_on_msg(dl->dl_parent_blkno != parent_blkno,
- " \"%pd\": old parent: %llu, new: %llu\n",
- dentry,
- (unsigned long long)parent_blkno,
- (unsigned long long)dl->dl_parent_blkno);
- trace_ocfs2_dentry_attach_lock_found(dl->dl_lockres.l_name,
- (unsigned long long)parent_blkno,
- (unsigned long long)OCFS2_I(inode)->ip_blkno);
- goto out_attach;
- }
-
- dl = kmalloc(sizeof(*dl), GFP_NOFS);
- if (!dl) {
- ret = -ENOMEM;
- mlog_errno(ret);
- return ret;
- }
- dl->dl_count = 0;
-
- dl->dl_inode = igrab(inode);
- dl->dl_parent_blkno = parent_blkno;
- ocfs2_dentry_lock_res_init(dl, parent_blkno, inode);
- out_attach:
- spin_lock(&dentry_attach_lock);
- if (unlikely(dentry->d_fsdata && !alias)) {
-
- spin_unlock(&dentry_attach_lock);
- iput(dl->dl_inode);
- ocfs2_lock_res_free(&dl->dl_lockres);
- kfree(dl);
- return 0;
- }
- dentry->d_fsdata = dl;
- dl->dl_count++;
- spin_unlock(&dentry_attach_lock);
-
- ret = ocfs2_dentry_lock(dentry, 0);
- if (!ret)
- ocfs2_dentry_unlock(dentry, 0);
- else
- mlog_errno(ret);
-
- if (ret < 0 && !alias) {
- ocfs2_lock_res_free(&dl->dl_lockres);
- BUG_ON(dl->dl_count != 1);
- spin_lock(&dentry_attach_lock);
- dentry->d_fsdata = NULL;
- spin_unlock(&dentry_attach_lock);
- kfree(dl);
- iput(inode);
- }
- dput(alias);
- return ret;
- }
- static void ocfs2_drop_dentry_lock(struct ocfs2_super *osb,
- struct ocfs2_dentry_lock *dl)
- {
- iput(dl->dl_inode);
- ocfs2_simple_drop_lockres(osb, &dl->dl_lockres);
- ocfs2_lock_res_free(&dl->dl_lockres);
- kfree(dl);
- }
- void ocfs2_dentry_lock_put(struct ocfs2_super *osb,
- struct ocfs2_dentry_lock *dl)
- {
- int unlock = 0;
- BUG_ON(dl->dl_count == 0);
- spin_lock(&dentry_attach_lock);
- dl->dl_count--;
- unlock = !dl->dl_count;
- spin_unlock(&dentry_attach_lock);
- if (unlock)
- ocfs2_drop_dentry_lock(osb, dl);
- }
- static void ocfs2_dentry_iput(struct dentry *dentry, struct inode *inode)
- {
- struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
- if (!dl) {
-
- if (!(dentry->d_flags & DCACHE_DISCONNECTED) &&
- !d_unhashed(dentry)) {
- unsigned long long ino = 0ULL;
- if (inode)
- ino = (unsigned long long)OCFS2_I(inode)->ip_blkno;
- mlog(ML_ERROR, "Dentry is missing cluster lock. "
- "inode: %llu, d_flags: 0x%x, d_name: %pd\n",
- ino, dentry->d_flags, dentry);
- }
- goto out;
- }
- mlog_bug_on_msg(dl->dl_count == 0, "dentry: %pd, count: %u\n",
- dentry, dl->dl_count);
- ocfs2_dentry_lock_put(OCFS2_SB(dentry->d_sb), dl);
- out:
- iput(inode);
- }
- void ocfs2_dentry_move(struct dentry *dentry, struct dentry *target,
- struct inode *old_dir, struct inode *new_dir)
- {
- int ret;
- struct ocfs2_super *osb = OCFS2_SB(old_dir->i_sb);
- struct inode *inode = d_inode(dentry);
-
- if (old_dir == new_dir)
- goto out_move;
- ocfs2_dentry_lock_put(osb, dentry->d_fsdata);
- dentry->d_fsdata = NULL;
- ret = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(new_dir)->ip_blkno);
- if (ret)
- mlog_errno(ret);
- out_move:
- d_move(dentry, target);
- }
- const struct dentry_operations ocfs2_dentry_ops = {
- .d_revalidate = ocfs2_dentry_revalidate,
- .d_iput = ocfs2_dentry_iput,
- };
|