Merge branch 'kvm-arm/vgic-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git...
[firefly-linux-kernel-4.4.55.git] / fs / 9p / fid.c
index 71bc36a03e72292f4067c717b774bc391bd90088..d51ec9fafcc81d097924a7c3472a406f1b75048c 100644 (file)
  *
  */
 
-int v9fs_fid_add(struct dentry *dentry, struct p9_fid *fid)
+static inline void __add_fid(struct dentry *dentry, struct p9_fid *fid)
 {
-       struct v9fs_dentry *dent;
-
-       p9_debug(P9_DEBUG_VFS, "fid %d dentry %s\n",
-                fid->fid, dentry->d_name.name);
-
-       dent = dentry->d_fsdata;
-       if (!dent) {
-               dent = kmalloc(sizeof(struct v9fs_dentry), GFP_KERNEL);
-               if (!dent)
-                       return -ENOMEM;
-
-               INIT_LIST_HEAD(&dent->fidlist);
-               dentry->d_fsdata = dent;
-       }
+       hlist_add_head(&fid->dlist, (struct hlist_head *)&dentry->d_fsdata);
+}
 
+void v9fs_fid_add(struct dentry *dentry, struct p9_fid *fid)
+{
        spin_lock(&dentry->d_lock);
-       list_add(&fid->dlist, &dent->fidlist);
+       __add_fid(dentry, fid);
        spin_unlock(&dentry->d_lock);
-
-       return 0;
 }
 
 /**
@@ -75,17 +63,17 @@ int v9fs_fid_add(struct dentry *dentry, struct p9_fid *fid)
 
 static struct p9_fid *v9fs_fid_find(struct dentry *dentry, kuid_t uid, int any)
 {
-       struct v9fs_dentry *dent;
        struct p9_fid *fid, *ret;
 
        p9_debug(P9_DEBUG_VFS, " dentry: %s (%p) uid %d any %d\n",
                 dentry->d_name.name, dentry, from_kuid(&init_user_ns, uid),
                 any);
-       dent = (struct v9fs_dentry *) dentry->d_fsdata;
        ret = NULL;
-       if (dent) {
+       /* we'll recheck under lock if there's anything to look in */
+       if (dentry->d_fsdata) {
+               struct hlist_head *h = (struct hlist_head *)&dentry->d_fsdata;
                spin_lock(&dentry->d_lock);
-               list_for_each_entry(fid, &dent->fidlist, dlist) {
+               hlist_for_each_entry(fid, h, dlist) {
                        if (any || uid_eq(fid->uid, uid)) {
                                ret = fid;
                                break;
@@ -214,8 +202,17 @@ static struct p9_fid *v9fs_fid_lookup_with_uid(struct dentry *dentry,
        }
        kfree(wnames);
 fid_out:
-       if (!IS_ERR(fid))
-               v9fs_fid_add(dentry, fid);
+       if (!IS_ERR(fid)) {
+               spin_lock(&dentry->d_lock);
+               if (d_unhashed(dentry)) {
+                       spin_unlock(&dentry->d_lock);
+                       p9_client_clunk(fid);
+                       fid = ERR_PTR(-ENOENT);
+               } else {
+                       __add_fid(dentry, fid);
+                       spin_unlock(&dentry->d_lock);
+               }
+       }
 err_out:
        up_read(&v9ses->rename_sem);
        return fid;