summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--drivers/staging/ncpfs/dir.c42
-rw-r--r--fs/9p/vfs_inode.c35
-rw-r--r--fs/adfs/dir.c24
-rw-r--r--fs/bfs/dir.c43
-rw-r--r--fs/cifs/dir.c38
-rw-r--r--fs/cramfs/inode.c5
-rw-r--r--fs/freevxfs/vxfs_lookup.c8
-rw-r--r--fs/hfs/dir.c20
-rw-r--r--fs/hfs/inode.c4
-rw-r--r--fs/hfsplus/dir.c3
-rw-r--r--fs/minix/namei.c8
-rw-r--r--fs/omfs/dir.c7
-rw-r--r--fs/openpromfs/inode.c3
-rw-r--r--fs/orangefs/namei.c64
-rw-r--r--fs/proc/base.c136
-rw-r--r--fs/proc/fd.c138
-rw-r--r--fs/proc/generic.c3
-rw-r--r--fs/proc/internal.h4
-rw-r--r--fs/proc/namespaces.c24
-rw-r--r--fs/proc/proc_sysctl.c15
-rw-r--r--fs/qnx4/namei.c8
-rw-r--r--fs/qnx6/namei.c8
-rw-r--r--fs/romfs/super.c36
-rw-r--r--fs/sysv/namei.c9
-rw-r--r--fs/ubifs/dir.c43
-rw-r--r--fs/xfs/xfs_iops.c16
26 files changed, 293 insertions, 451 deletions
diff --git a/drivers/staging/ncpfs/dir.c b/drivers/staging/ncpfs/dir.c
index 0c57c5c5d40a..072bcb12898f 100644
--- a/drivers/staging/ncpfs/dir.c
+++ b/drivers/staging/ncpfs/dir.c
@@ -823,12 +823,11 @@ static struct dentry *ncp_lookup(struct inode *dir, struct dentry *dentry, unsig
823 struct ncp_server *server = NCP_SERVER(dir); 823 struct ncp_server *server = NCP_SERVER(dir);
824 struct inode *inode = NULL; 824 struct inode *inode = NULL;
825 struct ncp_entry_info finfo; 825 struct ncp_entry_info finfo;
826 int error, res, len; 826 int res, len;
827 __u8 __name[NCP_MAXPATHLEN + 1]; 827 __u8 __name[NCP_MAXPATHLEN + 1];
828 828
829 error = -EIO;
830 if (!ncp_conn_valid(server)) 829 if (!ncp_conn_valid(server))
831 goto finished; 830 return ERR_PTR(-EIO);
832 831
833 ncp_vdbg("server lookup for %pd2\n", dentry); 832 ncp_vdbg("server lookup for %pd2\n", dentry);
834 833
@@ -847,31 +846,20 @@ static struct dentry *ncp_lookup(struct inode *dir, struct dentry *dentry, unsig
847 res = ncp_obtain_info(server, dir, __name, &(finfo.i)); 846 res = ncp_obtain_info(server, dir, __name, &(finfo.i));
848 } 847 }
849 ncp_vdbg("looked for %pd2, res=%d\n", dentry, res); 848 ncp_vdbg("looked for %pd2, res=%d\n", dentry, res);
850 /* 849 if (!res) {
851 * If we didn't find an entry, make a negative dentry. 850 /*
852 */ 851 * Entry found; create an inode for it.
853 if (res) 852 */
854 goto add_entry; 853 finfo.opened = 0;
855 854 finfo.ino = iunique(dir->i_sb, 2);
856 /* 855 finfo.volume = finfo.i.volNumber;
857 * Create an inode for the entry. 856 inode = ncp_iget(dir->i_sb, &finfo);
858 */ 857 if (unlikely(!inode))
859 finfo.opened = 0; 858 inode = ERR_PTR(-EACCES);
860 finfo.ino = iunique(dir->i_sb, 2); 859 else
861 finfo.volume = finfo.i.volNumber; 860 ncp_new_dentry(dentry);
862 error = -EACCES;
863 inode = ncp_iget(dir->i_sb, &finfo);
864
865 if (inode) {
866 ncp_new_dentry(dentry);
867add_entry:
868 d_add(dentry, inode);
869 error = 0;
870 } 861 }
871 862 return d_splice_alias(inode, dentry);
872finished:
873 ncp_vdbg("result=%d\n", error);
874 return ERR_PTR(error);
875} 863}
876 864
877/* 865/*
diff --git a/fs/9p/vfs_inode.c b/fs/9p/vfs_inode.c
index 9ee534159cc6..42e102e2e74a 100644
--- a/fs/9p/vfs_inode.c
+++ b/fs/9p/vfs_inode.c
@@ -823,28 +823,21 @@ struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
823 if (IS_ERR(dfid)) 823 if (IS_ERR(dfid))
824 return ERR_CAST(dfid); 824 return ERR_CAST(dfid);
825 825
826 name = dentry->d_name.name;
827 fid = p9_client_walk(dfid, 1, &name, 1);
828 if (IS_ERR(fid)) {
829 if (fid == ERR_PTR(-ENOENT)) {
830 d_add(dentry, NULL);
831 return NULL;
832 }
833 return ERR_CAST(fid);
834 }
835 /* 826 /*
836 * Make sure we don't use a wrong inode due to parallel 827 * Make sure we don't use a wrong inode due to parallel
837 * unlink. For cached mode create calls request for new 828 * unlink. For cached mode create calls request for new
838 * inode. But with cache disabled, lookup should do this. 829 * inode. But with cache disabled, lookup should do this.
839 */ 830 */
840 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) 831 name = dentry->d_name.name;
832 fid = p9_client_walk(dfid, 1, &name, 1);
833 if (fid == ERR_PTR(-ENOENT))
834 inode = NULL;
835 else if (IS_ERR(fid))
836 inode = ERR_CAST(fid);
837 else if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
841 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb); 838 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
842 else 839 else
843 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb); 840 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
844 if (IS_ERR(inode)) {
845 p9_client_clunk(fid);
846 return ERR_CAST(inode);
847 }
848 /* 841 /*
849 * If we had a rename on the server and a parallel lookup 842 * If we had a rename on the server and a parallel lookup
850 * for the new name, then make sure we instantiate with 843 * for the new name, then make sure we instantiate with
@@ -853,12 +846,14 @@ struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
853 * k/b. 846 * k/b.
854 */ 847 */
855 res = d_splice_alias(inode, dentry); 848 res = d_splice_alias(inode, dentry);
856 if (!res) 849 if (!IS_ERR(fid)) {
857 v9fs_fid_add(dentry, fid); 850 if (!res)
858 else if (!IS_ERR(res)) 851 v9fs_fid_add(dentry, fid);
859 v9fs_fid_add(res, fid); 852 else if (!IS_ERR(res))
860 else 853 v9fs_fid_add(res, fid);
861 p9_client_clunk(fid); 854 else
855 p9_client_clunk(fid);
856 }
862 return res; 857 return res;
863} 858}
864 859
diff --git a/fs/adfs/dir.c b/fs/adfs/dir.c
index 29444c83da48..e18eff854e1a 100644
--- a/fs/adfs/dir.c
+++ b/fs/adfs/dir.c
@@ -146,20 +146,6 @@ adfs_dir_lookup_byname(struct inode *inode, const struct qstr *name, struct obje
146 146
147 obj->parent_id = inode->i_ino; 147 obj->parent_id = inode->i_ino;
148 148
149 /*
150 * '.' is handled by reserved_lookup() in fs/namei.c
151 */
152 if (name->len == 2 && name->name[0] == '.' && name->name[1] == '.') {
153 /*
154 * Currently unable to fill in the rest of 'obj',
155 * but this is better than nothing. We need to
156 * ascend one level to find it's parent.
157 */
158 obj->name_len = 0;
159 obj->file_id = obj->parent_id;
160 goto free_out;
161 }
162
163 read_lock(&adfs_dir_lock); 149 read_lock(&adfs_dir_lock);
164 150
165 ret = ops->setpos(&dir, 0); 151 ret = ops->setpos(&dir, 0);
@@ -266,17 +252,17 @@ adfs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
266 252
267 error = adfs_dir_lookup_byname(dir, &dentry->d_name, &obj); 253 error = adfs_dir_lookup_byname(dir, &dentry->d_name, &obj);
268 if (error == 0) { 254 if (error == 0) {
269 error = -EACCES;
270 /* 255 /*
271 * This only returns NULL if get_empty_inode 256 * This only returns NULL if get_empty_inode
272 * fails. 257 * fails.
273 */ 258 */
274 inode = adfs_iget(dir->i_sb, &obj); 259 inode = adfs_iget(dir->i_sb, &obj);
275 if (inode) 260 if (!inode)
276 error = 0; 261 inode = ERR_PTR(-EACCES);
262 } else if (error != -ENOENT) {
263 inode = ERR_PTR(error);
277 } 264 }
278 d_add(dentry, inode); 265 return d_splice_alias(inode, dentry);
279 return ERR_PTR(error);
280} 266}
281 267
282/* 268/*
diff --git a/fs/bfs/dir.c b/fs/bfs/dir.c
index ee832ca5f734..f32f21c3bbc7 100644
--- a/fs/bfs/dir.c
+++ b/fs/bfs/dir.c
@@ -21,10 +21,9 @@
21#define dprintf(x...) 21#define dprintf(x...)
22#endif 22#endif
23 23
24static int bfs_add_entry(struct inode *dir, const unsigned char *name, 24static int bfs_add_entry(struct inode *dir, const struct qstr *child, int ino);
25 int namelen, int ino);
26static struct buffer_head *bfs_find_entry(struct inode *dir, 25static struct buffer_head *bfs_find_entry(struct inode *dir,
27 const unsigned char *name, int namelen, 26 const struct qstr *child,
28 struct bfs_dirent **res_dir); 27 struct bfs_dirent **res_dir);
29 28
30static int bfs_readdir(struct file *f, struct dir_context *ctx) 29static int bfs_readdir(struct file *f, struct dir_context *ctx)
@@ -111,8 +110,7 @@ static int bfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
111 mark_inode_dirty(inode); 110 mark_inode_dirty(inode);
112 bfs_dump_imap("create", s); 111 bfs_dump_imap("create", s);
113 112
114 err = bfs_add_entry(dir, dentry->d_name.name, dentry->d_name.len, 113 err = bfs_add_entry(dir, &dentry->d_name, inode->i_ino);
115 inode->i_ino);
116 if (err) { 114 if (err) {
117 inode_dec_link_count(inode); 115 inode_dec_link_count(inode);
118 mutex_unlock(&info->bfs_lock); 116 mutex_unlock(&info->bfs_lock);
@@ -136,19 +134,14 @@ static struct dentry *bfs_lookup(struct inode *dir, struct dentry *dentry,
136 return ERR_PTR(-ENAMETOOLONG); 134 return ERR_PTR(-ENAMETOOLONG);
137 135
138 mutex_lock(&info->bfs_lock); 136 mutex_lock(&info->bfs_lock);
139 bh = bfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len, &de); 137 bh = bfs_find_entry(dir, &dentry->d_name, &de);
140 if (bh) { 138 if (bh) {
141 unsigned long ino = (unsigned long)le16_to_cpu(de->ino); 139 unsigned long ino = (unsigned long)le16_to_cpu(de->ino);
142 brelse(bh); 140 brelse(bh);
143 inode = bfs_iget(dir->i_sb, ino); 141 inode = bfs_iget(dir->i_sb, ino);
144 if (IS_ERR(inode)) {
145 mutex_unlock(&info->bfs_lock);
146 return ERR_CAST(inode);
147 }
148 } 142 }
149 mutex_unlock(&info->bfs_lock); 143 mutex_unlock(&info->bfs_lock);
150 d_add(dentry, inode); 144 return d_splice_alias(inode, dentry);
151 return NULL;
152} 145}
153 146
154static int bfs_link(struct dentry *old, struct inode *dir, 147static int bfs_link(struct dentry *old, struct inode *dir,
@@ -159,8 +152,7 @@ static int bfs_link(struct dentry *old, struct inode *dir,
159 int err; 152 int err;
160 153
161 mutex_lock(&info->bfs_lock); 154 mutex_lock(&info->bfs_lock);
162 err = bfs_add_entry(dir, new->d_name.name, new->d_name.len, 155 err = bfs_add_entry(dir, &new->d_name, inode->i_ino);
163 inode->i_ino);
164 if (err) { 156 if (err) {
165 mutex_unlock(&info->bfs_lock); 157 mutex_unlock(&info->bfs_lock);
166 return err; 158 return err;
@@ -183,7 +175,7 @@ static int bfs_unlink(struct inode *dir, struct dentry *dentry)
183 struct bfs_sb_info *info = BFS_SB(inode->i_sb); 175 struct bfs_sb_info *info = BFS_SB(inode->i_sb);
184 176
185 mutex_lock(&info->bfs_lock); 177 mutex_lock(&info->bfs_lock);
186 bh = bfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len, &de); 178 bh = bfs_find_entry(dir, &dentry->d_name, &de);
187 if (!bh || (le16_to_cpu(de->ino) != inode->i_ino)) 179 if (!bh || (le16_to_cpu(de->ino) != inode->i_ino))
188 goto out_brelse; 180 goto out_brelse;
189 181
@@ -228,27 +220,21 @@ static int bfs_rename(struct inode *old_dir, struct dentry *old_dentry,
228 info = BFS_SB(old_inode->i_sb); 220 info = BFS_SB(old_inode->i_sb);
229 221
230 mutex_lock(&info->bfs_lock); 222 mutex_lock(&info->bfs_lock);
231 old_bh = bfs_find_entry(old_dir, 223 old_bh = bfs_find_entry(old_dir, &old_dentry->d_name, &old_de);
232 old_dentry->d_name.name,
233 old_dentry->d_name.len, &old_de);
234 224
235 if (!old_bh || (le16_to_cpu(old_de->ino) != old_inode->i_ino)) 225 if (!old_bh || (le16_to_cpu(old_de->ino) != old_inode->i_ino))
236 goto end_rename; 226 goto end_rename;
237 227
238 error = -EPERM; 228 error = -EPERM;
239 new_inode = d_inode(new_dentry); 229 new_inode = d_inode(new_dentry);
240 new_bh = bfs_find_entry(new_dir, 230 new_bh = bfs_find_entry(new_dir, &new_dentry->d_name, &new_de);
241 new_dentry->d_name.name,
242 new_dentry->d_name.len, &new_de);
243 231
244 if (new_bh && !new_inode) { 232 if (new_bh && !new_inode) {
245 brelse(new_bh); 233 brelse(new_bh);
246 new_bh = NULL; 234 new_bh = NULL;
247 } 235 }
248 if (!new_bh) { 236 if (!new_bh) {
249 error = bfs_add_entry(new_dir, 237 error = bfs_add_entry(new_dir, &new_dentry->d_name,
250 new_dentry->d_name.name,
251 new_dentry->d_name.len,
252 old_inode->i_ino); 238 old_inode->i_ino);
253 if (error) 239 if (error)
254 goto end_rename; 240 goto end_rename;
@@ -278,9 +264,10 @@ const struct inode_operations bfs_dir_inops = {
278 .rename = bfs_rename, 264 .rename = bfs_rename,
279}; 265};
280 266
281static int bfs_add_entry(struct inode *dir, const unsigned char *name, 267static int bfs_add_entry(struct inode *dir, const struct qstr *child, int ino)
282 int namelen, int ino)
283{ 268{
269 const unsigned char *name = child->name;
270 int namelen = child->len;
284 struct buffer_head *bh; 271 struct buffer_head *bh;
285 struct bfs_dirent *de; 272 struct bfs_dirent *de;
286 int block, sblock, eblock, off, pos; 273 int block, sblock, eblock, off, pos;
@@ -332,12 +319,14 @@ static inline int bfs_namecmp(int len, const unsigned char *name,
332} 319}
333 320
334static struct buffer_head *bfs_find_entry(struct inode *dir, 321static struct buffer_head *bfs_find_entry(struct inode *dir,
335 const unsigned char *name, int namelen, 322 const struct qstr *child,
336 struct bfs_dirent **res_dir) 323 struct bfs_dirent **res_dir)
337{ 324{
338 unsigned long block = 0, offset = 0; 325 unsigned long block = 0, offset = 0;
339 struct buffer_head *bh = NULL; 326 struct buffer_head *bh = NULL;
340 struct bfs_dirent *de; 327 struct bfs_dirent *de;
328 const unsigned char *name = child->name;
329 int namelen = child->len;
341 330
342 *res_dir = NULL; 331 *res_dir = NULL;
343 if (namelen > BFS_NAMELEN) 332 if (namelen > BFS_NAMELEN)
diff --git a/fs/cifs/dir.c b/fs/cifs/dir.c
index 925844343038..9eb03e8b1ada 100644
--- a/fs/cifs/dir.c
+++ b/fs/cifs/dir.c
@@ -780,21 +780,25 @@ cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry,
780 tlink = cifs_sb_tlink(cifs_sb); 780 tlink = cifs_sb_tlink(cifs_sb);
781 if (IS_ERR(tlink)) { 781 if (IS_ERR(tlink)) {
782 free_xid(xid); 782 free_xid(xid);
783 return (struct dentry *)tlink; 783 return ERR_CAST(tlink);
784 } 784 }
785 pTcon = tlink_tcon(tlink); 785 pTcon = tlink_tcon(tlink);
786 786
787 rc = check_name(direntry, pTcon); 787 rc = check_name(direntry, pTcon);
788 if (rc) 788 if (unlikely(rc)) {
789 goto lookup_out; 789 cifs_put_tlink(tlink);
790 free_xid(xid);
791 return ERR_PTR(rc);
792 }
790 793
791 /* can not grab the rename sem here since it would 794 /* can not grab the rename sem here since it would
792 deadlock in the cases (beginning of sys_rename itself) 795 deadlock in the cases (beginning of sys_rename itself)
793 in which we already have the sb rename sem */ 796 in which we already have the sb rename sem */
794 full_path = build_path_from_dentry(direntry); 797 full_path = build_path_from_dentry(direntry);
795 if (full_path == NULL) { 798 if (full_path == NULL) {
796 rc = -ENOMEM; 799 cifs_put_tlink(tlink);
797 goto lookup_out; 800 free_xid(xid);
801 return ERR_PTR(-ENOMEM);
798 } 802 }
799 803
800 if (d_really_is_positive(direntry)) { 804 if (d_really_is_positive(direntry)) {
@@ -813,29 +817,25 @@ cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry,
813 parent_dir_inode->i_sb, xid, NULL); 817 parent_dir_inode->i_sb, xid, NULL);
814 } 818 }
815 819
816 if ((rc == 0) && (newInode != NULL)) { 820 if (rc == 0) {
817 d_add(direntry, newInode);
818 /* since paths are not looked up by component - the parent 821 /* since paths are not looked up by component - the parent
819 directories are presumed to be good here */ 822 directories are presumed to be good here */
820 renew_parental_timestamps(direntry); 823 renew_parental_timestamps(direntry);
821
822 } else if (rc == -ENOENT) { 824 } else if (rc == -ENOENT) {
823 rc = 0;
824 cifs_set_time(direntry, jiffies); 825 cifs_set_time(direntry, jiffies);
825 d_add(direntry, NULL); 826 newInode = NULL;
826 /* if it was once a directory (but how can we tell?) we could do 827 } else {
827 shrink_dcache_parent(direntry); */ 828 if (rc != -EACCES) {
828 } else if (rc != -EACCES) { 829 cifs_dbg(FYI, "Unexpected lookup error %d\n", rc);
829 cifs_dbg(FYI, "Unexpected lookup error %d\n", rc); 830 /* We special case check for Access Denied - since that
830 /* We special case check for Access Denied - since that 831 is a common return code */
831 is a common return code */ 832 }
833 newInode = ERR_PTR(rc);
832 } 834 }
833
834lookup_out:
835 kfree(full_path); 835 kfree(full_path);
836 cifs_put_tlink(tlink); 836 cifs_put_tlink(tlink);
837 free_xid(xid); 837 free_xid(xid);
838 return ERR_PTR(rc); 838 return d_splice_alias(newInode, direntry);
839} 839}
840 840
841static int 841static int
diff --git a/fs/cramfs/inode.c b/fs/cramfs/inode.c
index 124b093d14e5..c4fb9ad7c808 100644
--- a/fs/cramfs/inode.c
+++ b/fs/cramfs/inode.c
@@ -808,10 +808,7 @@ static struct dentry *cramfs_lookup(struct inode *dir, struct dentry *dentry, un
808 } 808 }
809out: 809out:
810 mutex_unlock(&read_mutex); 810 mutex_unlock(&read_mutex);
811 if (IS_ERR(inode)) 811 return d_splice_alias(inode, dentry);
812 return ERR_CAST(inode);
813 d_add(dentry, inode);
814 return NULL;
815} 812}
816 813
817static int cramfs_readpage(struct file *file, struct page *page) 814static int cramfs_readpage(struct file *file, struct page *page)
diff --git a/fs/freevxfs/vxfs_lookup.c b/fs/freevxfs/vxfs_lookup.c
index ce4785fd81c6..a51425634f65 100644
--- a/fs/freevxfs/vxfs_lookup.c
+++ b/fs/freevxfs/vxfs_lookup.c
@@ -193,13 +193,9 @@ vxfs_lookup(struct inode *dip, struct dentry *dp, unsigned int flags)
193 return ERR_PTR(-ENAMETOOLONG); 193 return ERR_PTR(-ENAMETOOLONG);
194 194
195 ino = vxfs_inode_by_name(dip, dp); 195 ino = vxfs_inode_by_name(dip, dp);
196 if (ino) { 196 if (ino)
197 ip = vxfs_iget(dip->i_sb, ino); 197 ip = vxfs_iget(dip->i_sb, ino);
198 if (IS_ERR(ip)) 198 return d_splice_alias(ip, dp);
199 return ERR_CAST(ip);
200 }
201 d_add(dp, ip);
202 return NULL;
203} 199}
204 200
205/** 201/**
diff --git a/fs/hfs/dir.c b/fs/hfs/dir.c
index 75b254280ff6..3bf2ae0e467c 100644
--- a/fs/hfs/dir.c
+++ b/fs/hfs/dir.c
@@ -31,21 +31,15 @@ static struct dentry *hfs_lookup(struct inode *dir, struct dentry *dentry,
31 hfs_cat_build_key(dir->i_sb, fd.search_key, dir->i_ino, &dentry->d_name); 31 hfs_cat_build_key(dir->i_sb, fd.search_key, dir->i_ino, &dentry->d_name);
32 res = hfs_brec_read(&fd, &rec, sizeof(rec)); 32 res = hfs_brec_read(&fd, &rec, sizeof(rec));
33 if (res) { 33 if (res) {
34 hfs_find_exit(&fd); 34 if (res != -ENOENT)
35 if (res == -ENOENT) { 35 inode = ERR_PTR(res);
36 /* No such entry */ 36 } else {
37 inode = NULL; 37 inode = hfs_iget(dir->i_sb, &fd.search_key->cat, &rec);
38 goto done; 38 if (!inode)
39 } 39 inode = ERR_PTR(-EACCES);
40 return ERR_PTR(res);
41 } 40 }
42 inode = hfs_iget(dir->i_sb, &fd.search_key->cat, &rec);
43 hfs_find_exit(&fd); 41 hfs_find_exit(&fd);
44 if (!inode) 42 return d_splice_alias(inode, dentry);
45 return ERR_PTR(-EACCES);
46done:
47 d_add(dentry, inode);
48 return NULL;
49} 43}
50 44
51/* 45/*
diff --git a/fs/hfs/inode.c b/fs/hfs/inode.c
index 2538b49cc349..b3309b83371a 100644
--- a/fs/hfs/inode.c
+++ b/fs/hfs/inode.c
@@ -543,9 +543,9 @@ static struct dentry *hfs_file_lookup(struct inode *dir, struct dentry *dentry,
543 igrab(dir); 543 igrab(dir);
544 hlist_add_fake(&inode->i_hash); 544 hlist_add_fake(&inode->i_hash);
545 mark_inode_dirty(inode); 545 mark_inode_dirty(inode);
546 dont_mount(dentry);
546out: 547out:
547 d_add(dentry, inode); 548 return d_splice_alias(inode, dentry);
548 return NULL;
549} 549}
550 550
551void hfs_evict_inode(struct inode *inode) 551void hfs_evict_inode(struct inode *inode)
diff --git a/fs/hfsplus/dir.c b/fs/hfsplus/dir.c
index 15e06fb552da..b5254378f011 100644
--- a/fs/hfsplus/dir.c
+++ b/fs/hfsplus/dir.c
@@ -122,8 +122,7 @@ again:
122 if (S_ISREG(inode->i_mode)) 122 if (S_ISREG(inode->i_mode))
123 HFSPLUS_I(inode)->linkid = linkid; 123 HFSPLUS_I(inode)->linkid = linkid;
124out: 124out:
125 d_add(dentry, inode); 125 return d_splice_alias(inode, dentry);
126 return NULL;
127fail: 126fail:
128 hfs_find_exit(&fd); 127 hfs_find_exit(&fd);
129 return ERR_PTR(err); 128 return ERR_PTR(err);
diff --git a/fs/minix/namei.c b/fs/minix/namei.c
index ccf0f00030bf..1a6084d2b02e 100644
--- a/fs/minix/namei.c
+++ b/fs/minix/namei.c
@@ -28,13 +28,9 @@ static struct dentry *minix_lookup(struct inode * dir, struct dentry *dentry, un
28 return ERR_PTR(-ENAMETOOLONG); 28 return ERR_PTR(-ENAMETOOLONG);
29 29
30 ino = minix_inode_by_name(dentry); 30 ino = minix_inode_by_name(dentry);
31 if (ino) { 31 if (ino)
32 inode = minix_iget(dir->i_sb, ino); 32 inode = minix_iget(dir->i_sb, ino);
33 if (IS_ERR(inode)) 33 return d_splice_alias(inode, dentry);
34 return ERR_CAST(inode);
35 }
36 d_add(dentry, inode);
37 return NULL;
38} 34}
39 35
40static int minix_mknod(struct inode * dir, struct dentry *dentry, umode_t mode, dev_t rdev) 36static int minix_mknod(struct inode * dir, struct dentry *dentry, umode_t mode, dev_t rdev)
diff --git a/fs/omfs/dir.c b/fs/omfs/dir.c
index b7146526afff..4bee3a72b9f3 100644
--- a/fs/omfs/dir.c
+++ b/fs/omfs/dir.c
@@ -305,11 +305,10 @@ static struct dentry *omfs_lookup(struct inode *dir, struct dentry *dentry,
305 ino_t ino = be64_to_cpu(oi->i_head.h_self); 305 ino_t ino = be64_to_cpu(oi->i_head.h_self);
306 brelse(bh); 306 brelse(bh);
307 inode = omfs_iget(dir->i_sb, ino); 307 inode = omfs_iget(dir->i_sb, ino);
308 if (IS_ERR(inode)) 308 } else if (bh != ERR_PTR(-ENOENT)) {
309 return ERR_CAST(inode); 309 inode = ERR_CAST(bh);
310 } 310 }
311 d_add(dentry, inode); 311 return d_splice_alias(inode, dentry);
312 return NULL;
313} 312}
314 313
315/* sanity check block's self pointer */ 314/* sanity check block's self pointer */
diff --git a/fs/openpromfs/inode.c b/fs/openpromfs/inode.c
index 2200662a9bf1..607092f367ad 100644
--- a/fs/openpromfs/inode.c
+++ b/fs/openpromfs/inode.c
@@ -256,8 +256,7 @@ found:
256 break; 256 break;
257 } 257 }
258 258
259 d_add(dentry, inode); 259 return d_splice_alias(inode, dentry);
260 return NULL;
261} 260}
262 261
263static int openpromfs_readdir(struct file *file, struct dir_context *ctx) 262static int openpromfs_readdir(struct file *file, struct dir_context *ctx)
diff --git a/fs/orangefs/namei.c b/fs/orangefs/namei.c
index 1b5707c44c3f..365cd73d9109 100644
--- a/fs/orangefs/namei.c
+++ b/fs/orangefs/namei.c
@@ -110,7 +110,6 @@ static struct dentry *orangefs_lookup(struct inode *dir, struct dentry *dentry,
110 struct orangefs_inode_s *parent = ORANGEFS_I(dir); 110 struct orangefs_inode_s *parent = ORANGEFS_I(dir);
111 struct orangefs_kernel_op_s *new_op; 111 struct orangefs_kernel_op_s *new_op;
112 struct inode *inode; 112 struct inode *inode;
113 struct dentry *res;
114 int ret = -EINVAL; 113 int ret = -EINVAL;
115 114
116 /* 115 /*
@@ -158,65 +157,18 @@ static struct dentry *orangefs_lookup(struct inode *dir, struct dentry *dentry,
158 new_op->downcall.resp.lookup.refn.fs_id, 157 new_op->downcall.resp.lookup.refn.fs_id,
159 ret); 158 ret);
160 159
161 if (ret < 0) { 160 if (ret >= 0) {
162 if (ret == -ENOENT) { 161 orangefs_set_timeout(dentry);
163 /* 162 inode = orangefs_iget(dir->i_sb, &new_op->downcall.resp.lookup.refn);
164 * if no inode was found, add a negative dentry to 163 } else if (ret == -ENOENT) {
165 * dcache anyway; if we don't, we don't hold expected 164 inode = NULL;
166 * lookup semantics and we most noticeably break 165 } else {
167 * during directory renames.
168 *
169 * however, if the operation failed or exited, do not
170 * add the dentry (e.g. in the case that a touch is
171 * issued on a file that already exists that was
172 * interrupted during this lookup -- no need to add
173 * another negative dentry for an existing file)
174 */
175
176 gossip_debug(GOSSIP_NAME_DEBUG,
177 "orangefs_lookup: Adding *negative* dentry "
178 "%p for %pd\n",
179 dentry,
180 dentry);
181
182 d_add(dentry, NULL);
183 res = NULL;
184 goto out;
185 }
186
187 /* must be a non-recoverable error */ 166 /* must be a non-recoverable error */
188 res = ERR_PTR(ret); 167 inode = ERR_PTR(ret);
189 goto out;
190 }
191
192 orangefs_set_timeout(dentry);
193
194 inode = orangefs_iget(dir->i_sb, &new_op->downcall.resp.lookup.refn);
195 if (IS_ERR(inode)) {
196 gossip_debug(GOSSIP_NAME_DEBUG,
197 "error %ld from iget\n", PTR_ERR(inode));
198 res = ERR_CAST(inode);
199 goto out;
200 } 168 }
201 169
202 gossip_debug(GOSSIP_NAME_DEBUG,
203 "%s:%s:%d "
204 "Found good inode [%lu] with count [%d]\n",
205 __FILE__,
206 __func__,
207 __LINE__,
208 inode->i_ino,
209 (int)atomic_read(&inode->i_count));
210
211 /* update dentry/inode pair into dcache */
212 res = d_splice_alias(inode, dentry);
213
214 gossip_debug(GOSSIP_NAME_DEBUG,
215 "Lookup success (inode ct = %d)\n",
216 (int)atomic_read(&inode->i_count));
217out:
218 op_release(new_op); 170 op_release(new_op);
219 return res; 171 return d_splice_alias(inode, dentry);
220} 172}
221 173
222/* return 0 on success; non-zero otherwise */ 174/* return 0 on success; non-zero otherwise */
diff --git a/fs/proc/base.c b/fs/proc/base.c
index 4e35593546b1..33ed1746927a 100644
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -1807,15 +1807,22 @@ int pid_getattr(const struct path *path, struct kstat *stat,
1807/* dentry stuff */ 1807/* dentry stuff */
1808 1808
1809/* 1809/*
1810 * Exceptional case: normally we are not allowed to unhash a busy 1810 * Set <pid>/... inode ownership (can change due to setuid(), etc.)
1811 * directory. In this case, however, we can do it - no aliasing problems 1811 */
1812 * due to the way we treat inodes. 1812void pid_update_inode(struct task_struct *task, struct inode *inode)
1813 * 1813{
1814 task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid);
1815
1816 inode->i_mode &= ~(S_ISUID | S_ISGID);
1817 security_task_to_inode(task, inode);
1818}
1819
1820/*
1814 * Rewrite the inode's ownerships here because the owning task may have 1821 * Rewrite the inode's ownerships here because the owning task may have
1815 * performed a setuid(), etc. 1822 * performed a setuid(), etc.
1816 * 1823 *
1817 */ 1824 */
1818int pid_revalidate(struct dentry *dentry, unsigned int flags) 1825static int pid_revalidate(struct dentry *dentry, unsigned int flags)
1819{ 1826{
1820 struct inode *inode; 1827 struct inode *inode;
1821 struct task_struct *task; 1828 struct task_struct *task;
@@ -1827,10 +1834,7 @@ int pid_revalidate(struct dentry *dentry, unsigned int flags)
1827 task = get_proc_task(inode); 1834 task = get_proc_task(inode);
1828 1835
1829 if (task) { 1836 if (task) {
1830 task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid); 1837 pid_update_inode(task, inode);
1831
1832 inode->i_mode &= ~(S_ISUID | S_ISGID);
1833 security_task_to_inode(task, inode);
1834 put_task_struct(task); 1838 put_task_struct(task);
1835 return 1; 1839 return 1;
1836 } 1840 }
@@ -1878,8 +1882,8 @@ bool proc_fill_cache(struct file *file, struct dir_context *ctx,
1878 struct dentry *child, *dir = file->f_path.dentry; 1882 struct dentry *child, *dir = file->f_path.dentry;
1879 struct qstr qname = QSTR_INIT(name, len); 1883 struct qstr qname = QSTR_INIT(name, len);
1880 struct inode *inode; 1884 struct inode *inode;
1881 unsigned type; 1885 unsigned type = DT_UNKNOWN;
1882 ino_t ino; 1886 ino_t ino = 1;
1883 1887
1884 child = d_hash_and_lookup(dir, &qname); 1888 child = d_hash_and_lookup(dir, &qname);
1885 if (!child) { 1889 if (!child) {
@@ -1888,22 +1892,23 @@ bool proc_fill_cache(struct file *file, struct dir_context *ctx,
1888 if (IS_ERR(child)) 1892 if (IS_ERR(child))
1889 goto end_instantiate; 1893 goto end_instantiate;
1890 if (d_in_lookup(child)) { 1894 if (d_in_lookup(child)) {
1891 int err = instantiate(d_inode(dir), child, task, ptr); 1895 struct dentry *res;
1896 res = instantiate(child, task, ptr);
1892 d_lookup_done(child); 1897 d_lookup_done(child);
1893 if (err < 0) { 1898 if (IS_ERR(res))
1894 dput(child);
1895 goto end_instantiate; 1899 goto end_instantiate;
1900 if (unlikely(res)) {
1901 dput(child);
1902 child = res;
1896 } 1903 }
1897 } 1904 }
1898 } 1905 }
1899 inode = d_inode(child); 1906 inode = d_inode(child);
1900 ino = inode->i_ino; 1907 ino = inode->i_ino;
1901 type = inode->i_mode >> 12; 1908 type = inode->i_mode >> 12;
1909end_instantiate:
1902 dput(child); 1910 dput(child);
1903 return dir_emit(ctx, name, len, ino, type); 1911 return dir_emit(ctx, name, len, ino, type);
1904
1905end_instantiate:
1906 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
1907} 1912}
1908 1913
1909/* 1914/*
@@ -2065,19 +2070,19 @@ static const struct inode_operations proc_map_files_link_inode_operations = {
2065 .setattr = proc_setattr, 2070 .setattr = proc_setattr,
2066}; 2071};
2067 2072
2068static int 2073static struct dentry *
2069proc_map_files_instantiate(struct inode *dir, struct dentry *dentry, 2074proc_map_files_instantiate(struct dentry *dentry,
2070 struct task_struct *task, const void *ptr) 2075 struct task_struct *task, const void *ptr)
2071{ 2076{
2072 fmode_t mode = (fmode_t)(unsigned long)ptr; 2077 fmode_t mode = (fmode_t)(unsigned long)ptr;
2073 struct proc_inode *ei; 2078 struct proc_inode *ei;
2074 struct inode *inode; 2079 struct inode *inode;
2075 2080
2076 inode = proc_pid_make_inode(dir->i_sb, task, S_IFLNK | 2081 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK |
2077 ((mode & FMODE_READ ) ? S_IRUSR : 0) | 2082 ((mode & FMODE_READ ) ? S_IRUSR : 0) |
2078 ((mode & FMODE_WRITE) ? S_IWUSR : 0)); 2083 ((mode & FMODE_WRITE) ? S_IWUSR : 0));
2079 if (!inode) 2084 if (!inode)
2080 return -ENOENT; 2085 return ERR_PTR(-ENOENT);
2081 2086
2082 ei = PROC_I(inode); 2087 ei = PROC_I(inode);
2083 ei->op.proc_get_link = map_files_get_link; 2088 ei->op.proc_get_link = map_files_get_link;
@@ -2086,9 +2091,7 @@ proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
2086 inode->i_size = 64; 2091 inode->i_size = 64;
2087 2092
2088 d_set_d_op(dentry, &tid_map_files_dentry_operations); 2093 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2089 d_add(dentry, inode); 2094 return d_splice_alias(inode, dentry);
2090
2091 return 0;
2092} 2095}
2093 2096
2094static struct dentry *proc_map_files_lookup(struct inode *dir, 2097static struct dentry *proc_map_files_lookup(struct inode *dir,
@@ -2097,19 +2100,19 @@ static struct dentry *proc_map_files_lookup(struct inode *dir,
2097 unsigned long vm_start, vm_end; 2100 unsigned long vm_start, vm_end;
2098 struct vm_area_struct *vma; 2101 struct vm_area_struct *vma;
2099 struct task_struct *task; 2102 struct task_struct *task;
2100 int result; 2103 struct dentry *result;
2101 struct mm_struct *mm; 2104 struct mm_struct *mm;
2102 2105
2103 result = -ENOENT; 2106 result = ERR_PTR(-ENOENT);
2104 task = get_proc_task(dir); 2107 task = get_proc_task(dir);
2105 if (!task) 2108 if (!task)
2106 goto out; 2109 goto out;
2107 2110
2108 result = -EACCES; 2111 result = ERR_PTR(-EACCES);
2109 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) 2112 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2110 goto out_put_task; 2113 goto out_put_task;
2111 2114
2112 result = -ENOENT; 2115 result = ERR_PTR(-ENOENT);
2113 if (dname_to_vma_addr(dentry, &vm_start, &vm_end)) 2116 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2114 goto out_put_task; 2117 goto out_put_task;
2115 2118
@@ -2123,7 +2126,7 @@ static struct dentry *proc_map_files_lookup(struct inode *dir,
2123 goto out_no_vma; 2126 goto out_no_vma;
2124 2127
2125 if (vma->vm_file) 2128 if (vma->vm_file)
2126 result = proc_map_files_instantiate(dir, dentry, task, 2129 result = proc_map_files_instantiate(dentry, task,
2127 (void *)(unsigned long)vma->vm_file->f_mode); 2130 (void *)(unsigned long)vma->vm_file->f_mode);
2128 2131
2129out_no_vma: 2132out_no_vma:
@@ -2132,7 +2135,7 @@ out_no_vma:
2132out_put_task: 2135out_put_task:
2133 put_task_struct(task); 2136 put_task_struct(task);
2134out: 2137out:
2135 return ERR_PTR(result); 2138 return result;
2136} 2139}
2137 2140
2138static const struct inode_operations proc_map_files_inode_operations = { 2141static const struct inode_operations proc_map_files_inode_operations = {
@@ -2433,16 +2436,16 @@ static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2433 .release = single_release, 2436 .release = single_release,
2434}; 2437};
2435 2438
2436static int proc_pident_instantiate(struct inode *dir, 2439static struct dentry *proc_pident_instantiate(struct dentry *dentry,
2437 struct dentry *dentry, struct task_struct *task, const void *ptr) 2440 struct task_struct *task, const void *ptr)
2438{ 2441{
2439 const struct pid_entry *p = ptr; 2442 const struct pid_entry *p = ptr;
2440 struct inode *inode; 2443 struct inode *inode;
2441 struct proc_inode *ei; 2444 struct proc_inode *ei;
2442 2445
2443 inode = proc_pid_make_inode(dir->i_sb, task, p->mode); 2446 inode = proc_pid_make_inode(dentry->d_sb, task, p->mode);
2444 if (!inode) 2447 if (!inode)
2445 goto out; 2448 return ERR_PTR(-ENOENT);
2446 2449
2447 ei = PROC_I(inode); 2450 ei = PROC_I(inode);
2448 if (S_ISDIR(inode->i_mode)) 2451 if (S_ISDIR(inode->i_mode))
@@ -2452,13 +2455,9 @@ static int proc_pident_instantiate(struct inode *dir,
2452 if (p->fop) 2455 if (p->fop)
2453 inode->i_fop = p->fop; 2456 inode->i_fop = p->fop;
2454 ei->op = p->op; 2457 ei->op = p->op;
2458 pid_update_inode(task, inode);
2455 d_set_d_op(dentry, &pid_dentry_operations); 2459 d_set_d_op(dentry, &pid_dentry_operations);
2456 d_add(dentry, inode); 2460 return d_splice_alias(inode, dentry);
2457 /* Close the race of the process dying before we return the dentry */
2458 if (pid_revalidate(dentry, 0))
2459 return 0;
2460out:
2461 return -ENOENT;
2462} 2461}
2463 2462
2464static struct dentry *proc_pident_lookup(struct inode *dir, 2463static struct dentry *proc_pident_lookup(struct inode *dir,
@@ -2466,11 +2465,9 @@ static struct dentry *proc_pident_lookup(struct inode *dir,
2466 const struct pid_entry *ents, 2465 const struct pid_entry *ents,
2467 unsigned int nents) 2466 unsigned int nents)
2468{ 2467{
2469 int error;
2470 struct task_struct *task = get_proc_task(dir); 2468 struct task_struct *task = get_proc_task(dir);
2471 const struct pid_entry *p, *last; 2469 const struct pid_entry *p, *last;
2472 2470 struct dentry *res = ERR_PTR(-ENOENT);
2473 error = -ENOENT;
2474 2471
2475 if (!task) 2472 if (!task)
2476 goto out_no_task; 2473 goto out_no_task;
@@ -2489,11 +2486,11 @@ static struct dentry *proc_pident_lookup(struct inode *dir,
2489 if (p >= last) 2486 if (p >= last)
2490 goto out; 2487 goto out;
2491 2488
2492 error = proc_pident_instantiate(dir, dentry, task, p); 2489 res = proc_pident_instantiate(dentry, task, p);
2493out: 2490out:
2494 put_task_struct(task); 2491 put_task_struct(task);
2495out_no_task: 2492out_no_task:
2496 return ERR_PTR(error); 2493 return res;
2497} 2494}
2498 2495
2499static int proc_pident_readdir(struct file *file, struct dir_context *ctx, 2496static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
@@ -3136,38 +3133,32 @@ void proc_flush_task(struct task_struct *task)
3136 } 3133 }
3137} 3134}
3138 3135
3139static int proc_pid_instantiate(struct inode *dir, 3136static struct dentry *proc_pid_instantiate(struct dentry * dentry,
3140 struct dentry * dentry,
3141 struct task_struct *task, const void *ptr) 3137 struct task_struct *task, const void *ptr)
3142{ 3138{
3143 struct inode *inode; 3139 struct inode *inode;
3144 3140
3145 inode = proc_pid_make_inode(dir->i_sb, task, S_IFDIR | S_IRUGO | S_IXUGO); 3141 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3146 if (!inode) 3142 if (!inode)
3147 goto out; 3143 return ERR_PTR(-ENOENT);
3148 3144
3149 inode->i_op = &proc_tgid_base_inode_operations; 3145 inode->i_op = &proc_tgid_base_inode_operations;
3150 inode->i_fop = &proc_tgid_base_operations; 3146 inode->i_fop = &proc_tgid_base_operations;
3151 inode->i_flags|=S_IMMUTABLE; 3147 inode->i_flags|=S_IMMUTABLE;
3152 3148
3153 set_nlink(inode, nlink_tgid); 3149 set_nlink(inode, nlink_tgid);
3150 pid_update_inode(task, inode);
3154 3151
3155 d_set_d_op(dentry, &pid_dentry_operations); 3152 d_set_d_op(dentry, &pid_dentry_operations);
3156 3153 return d_splice_alias(inode, dentry);
3157 d_add(dentry, inode);
3158 /* Close the race of the process dying before we return the dentry */
3159 if (pid_revalidate(dentry, 0))
3160 return 0;
3161out:
3162 return -ENOENT;
3163} 3154}
3164 3155
3165struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) 3156struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3166{ 3157{
3167 int result = -ENOENT;
3168 struct task_struct *task; 3158 struct task_struct *task;
3169 unsigned tgid; 3159 unsigned tgid;
3170 struct pid_namespace *ns; 3160 struct pid_namespace *ns;
3161 struct dentry *result = ERR_PTR(-ENOENT);
3171 3162
3172 tgid = name_to_int(&dentry->d_name); 3163 tgid = name_to_int(&dentry->d_name);
3173 if (tgid == ~0U) 3164 if (tgid == ~0U)
@@ -3182,10 +3173,10 @@ struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsign
3182 if (!task) 3173 if (!task)
3183 goto out; 3174 goto out;
3184 3175
3185 result = proc_pid_instantiate(dir, dentry, task, NULL); 3176 result = proc_pid_instantiate(dentry, task, NULL);
3186 put_task_struct(task); 3177 put_task_struct(task);
3187out: 3178out:
3188 return ERR_PTR(result); 3179 return result;
3189} 3180}
3190 3181
3191/* 3182/*
@@ -3433,37 +3424,32 @@ static const struct inode_operations proc_tid_base_inode_operations = {
3433 .setattr = proc_setattr, 3424 .setattr = proc_setattr,
3434}; 3425};
3435 3426
3436static int proc_task_instantiate(struct inode *dir, 3427static struct dentry *proc_task_instantiate(struct dentry *dentry,
3437 struct dentry *dentry, struct task_struct *task, const void *ptr) 3428 struct task_struct *task, const void *ptr)
3438{ 3429{
3439 struct inode *inode; 3430 struct inode *inode;
3440 inode = proc_pid_make_inode(dir->i_sb, task, S_IFDIR | S_IRUGO | S_IXUGO); 3431 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3441
3442 if (!inode) 3432 if (!inode)
3443 goto out; 3433 return ERR_PTR(-ENOENT);
3434
3444 inode->i_op = &proc_tid_base_inode_operations; 3435 inode->i_op = &proc_tid_base_inode_operations;
3445 inode->i_fop = &proc_tid_base_operations; 3436 inode->i_fop = &proc_tid_base_operations;
3446 inode->i_flags|=S_IMMUTABLE; 3437 inode->i_flags |= S_IMMUTABLE;
3447 3438
3448 set_nlink(inode, nlink_tid); 3439 set_nlink(inode, nlink_tid);
3440 pid_update_inode(task, inode);
3449 3441
3450 d_set_d_op(dentry, &pid_dentry_operations); 3442 d_set_d_op(dentry, &pid_dentry_operations);
3451 3443 return d_splice_alias(inode, dentry);
3452 d_add(dentry, inode);
3453 /* Close the race of the process dying before we return the dentry */
3454 if (pid_revalidate(dentry, 0))
3455 return 0;
3456out:
3457 return -ENOENT;
3458} 3444}
3459 3445
3460static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) 3446static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3461{ 3447{
3462 int result = -ENOENT;
3463 struct task_struct *task; 3448 struct task_struct *task;
3464 struct task_struct *leader = get_proc_task(dir); 3449 struct task_struct *leader = get_proc_task(dir);
3465 unsigned tid; 3450 unsigned tid;
3466 struct pid_namespace *ns; 3451 struct pid_namespace *ns;
3452 struct dentry *result = ERR_PTR(-ENOENT);
3467 3453
3468 if (!leader) 3454 if (!leader)
3469 goto out_no_task; 3455 goto out_no_task;
@@ -3483,13 +3469,13 @@ static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry
3483 if (!same_thread_group(leader, task)) 3469 if (!same_thread_group(leader, task))
3484 goto out_drop_task; 3470 goto out_drop_task;
3485 3471
3486 result = proc_task_instantiate(dir, dentry, task, NULL); 3472 result = proc_task_instantiate(dentry, task, NULL);
3487out_drop_task: 3473out_drop_task:
3488 put_task_struct(task); 3474 put_task_struct(task);
3489out: 3475out:
3490 put_task_struct(leader); 3476 put_task_struct(leader);
3491out_no_task: 3477out_no_task:
3492 return ERR_PTR(result); 3478 return result;
3493} 3479}
3494 3480
3495/* 3481/*
diff --git a/fs/proc/fd.c b/fs/proc/fd.c
index 6b80cd1e419a..05b9893e9a22 100644
--- a/fs/proc/fd.c
+++ b/fs/proc/fd.c
@@ -81,9 +81,41 @@ static const struct file_operations proc_fdinfo_file_operations = {
81 .release = single_release, 81 .release = single_release,
82}; 82};
83 83
84static bool tid_fd_mode(struct task_struct *task, unsigned fd, fmode_t *mode)
85{
86 struct files_struct *files = get_files_struct(task);
87 struct file *file;
88
89 if (!files)
90 return false;
91
92 rcu_read_lock();
93 file = fcheck_files(files, fd);
94 if (file)
95 *mode = file->f_mode;
96 rcu_read_unlock();
97 put_files_struct(files);
98 return !!file;
99}
100
101static void tid_fd_update_inode(struct task_struct *task, struct inode *inode,
102 fmode_t f_mode)
103{
104 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
105
106 if (S_ISLNK(inode->i_mode)) {
107 unsigned i_mode = S_IFLNK;
108 if (f_mode & FMODE_READ)
109 i_mode |= S_IRUSR | S_IXUSR;
110 if (f_mode & FMODE_WRITE)
111 i_mode |= S_IWUSR | S_IXUSR;
112 inode->i_mode = i_mode;
113 }
114 security_task_to_inode(task, inode);
115}
116
84static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags) 117static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags)
85{ 118{
86 struct files_struct *files;
87 struct task_struct *task; 119 struct task_struct *task;
88 struct inode *inode; 120 struct inode *inode;
89 unsigned int fd; 121 unsigned int fd;
@@ -96,35 +128,11 @@ static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags)
96 fd = proc_fd(inode); 128 fd = proc_fd(inode);
97 129
98 if (task) { 130 if (task) {
99 files = get_files_struct(task); 131 fmode_t f_mode;
100 if (files) { 132 if (tid_fd_mode(task, fd, &f_mode)) {
101 struct file *file; 133 tid_fd_update_inode(task, inode, f_mode);
102 134 put_task_struct(task);
103 rcu_read_lock(); 135 return 1;
104 file = fcheck_files(files, fd);
105 if (file) {
106 unsigned f_mode = file->f_mode;
107
108 rcu_read_unlock();
109 put_files_struct(files);
110
111 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
112
113 if (S_ISLNK(inode->i_mode)) {
114 unsigned i_mode = S_IFLNK;
115 if (f_mode & FMODE_READ)
116 i_mode |= S_IRUSR | S_IXUSR;
117 if (f_mode & FMODE_WRITE)
118 i_mode |= S_IWUSR | S_IXUSR;
119 inode->i_mode = i_mode;
120 }
121
122 security_task_to_inode(task, inode);
123 put_task_struct(task);
124 return 1;
125 }
126 rcu_read_unlock();
127 put_files_struct(files);
128 } 136 }
129 put_task_struct(task); 137 put_task_struct(task);
130 } 138 }
@@ -166,34 +174,33 @@ static int proc_fd_link(struct dentry *dentry, struct path *path)
166 return ret; 174 return ret;
167} 175}
168 176
169static int 177struct fd_data {
170proc_fd_instantiate(struct inode *dir, struct dentry *dentry, 178 fmode_t mode;
171 struct task_struct *task, const void *ptr) 179 unsigned fd;
180};
181
182static struct dentry *proc_fd_instantiate(struct dentry *dentry,
183 struct task_struct *task, const void *ptr)
172{ 184{
173 unsigned fd = (unsigned long)ptr; 185 const struct fd_data *data = ptr;
174 struct proc_inode *ei; 186 struct proc_inode *ei;
175 struct inode *inode; 187 struct inode *inode;
176 188
177 inode = proc_pid_make_inode(dir->i_sb, task, S_IFLNK); 189 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK);
178 if (!inode) 190 if (!inode)
179 goto out; 191 return ERR_PTR(-ENOENT);
180 192
181 ei = PROC_I(inode); 193 ei = PROC_I(inode);
182 ei->fd = fd; 194 ei->fd = data->fd;
183 195
184 inode->i_op = &proc_pid_link_inode_operations; 196 inode->i_op = &proc_pid_link_inode_operations;
185 inode->i_size = 64; 197 inode->i_size = 64;
186 198
187 ei->op.proc_get_link = proc_fd_link; 199 ei->op.proc_get_link = proc_fd_link;
200 tid_fd_update_inode(task, inode, data->mode);
188 201
189 d_set_d_op(dentry, &tid_fd_dentry_operations); 202 d_set_d_op(dentry, &tid_fd_dentry_operations);
190 d_add(dentry, inode); 203 return d_splice_alias(inode, dentry);
191
192 /* Close the race of the process dying before we return the dentry */
193 if (tid_fd_revalidate(dentry, 0))
194 return 0;
195 out:
196 return -ENOENT;
197} 204}
198 205
199static struct dentry *proc_lookupfd_common(struct inode *dir, 206static struct dentry *proc_lookupfd_common(struct inode *dir,
@@ -201,19 +208,21 @@ static struct dentry *proc_lookupfd_common(struct inode *dir,
201 instantiate_t instantiate) 208 instantiate_t instantiate)
202{ 209{
203 struct task_struct *task = get_proc_task(dir); 210 struct task_struct *task = get_proc_task(dir);
204 int result = -ENOENT; 211 struct fd_data data = {.fd = name_to_int(&dentry->d_name)};
205 unsigned fd = name_to_int(&dentry->d_name); 212 struct dentry *result = ERR_PTR(-ENOENT);
206 213
207 if (!task) 214 if (!task)
208 goto out_no_task; 215 goto out_no_task;
209 if (fd == ~0U) 216 if (data.fd == ~0U)
217 goto out;
218 if (!tid_fd_mode(task, data.fd, &data.mode))
210 goto out; 219 goto out;
211 220
212 result = instantiate(dir, dentry, task, (void *)(unsigned long)fd); 221 result = instantiate(dentry, task, &data);
213out: 222out:
214 put_task_struct(task); 223 put_task_struct(task);
215out_no_task: 224out_no_task:
216 return ERR_PTR(result); 225 return result;
217} 226}
218 227
219static int proc_readfd_common(struct file *file, struct dir_context *ctx, 228static int proc_readfd_common(struct file *file, struct dir_context *ctx,
@@ -236,17 +245,22 @@ static int proc_readfd_common(struct file *file, struct dir_context *ctx,
236 for (fd = ctx->pos - 2; 245 for (fd = ctx->pos - 2;
237 fd < files_fdtable(files)->max_fds; 246 fd < files_fdtable(files)->max_fds;
238 fd++, ctx->pos++) { 247 fd++, ctx->pos++) {
248 struct file *f;
249 struct fd_data data;
239 char name[10 + 1]; 250 char name[10 + 1];
240 int len; 251 int len;
241 252
242 if (!fcheck_files(files, fd)) 253 f = fcheck_files(files, fd);
254 if (!f)
243 continue; 255 continue;
256 data.mode = f->f_mode;
244 rcu_read_unlock(); 257 rcu_read_unlock();
258 data.fd = fd;
245 259
246 len = snprintf(name, sizeof(name), "%u", fd); 260 len = snprintf(name, sizeof(name), "%u", fd);
247 if (!proc_fill_cache(file, ctx, 261 if (!proc_fill_cache(file, ctx,
248 name, len, instantiate, p, 262 name, len, instantiate, p,
249 (void *)(unsigned long)fd)) 263 &data))
250 goto out_fd_loop; 264 goto out_fd_loop;
251 cond_resched(); 265 cond_resched();
252 rcu_read_lock(); 266 rcu_read_lock();
@@ -304,31 +318,25 @@ const struct inode_operations proc_fd_inode_operations = {
304 .setattr = proc_setattr, 318 .setattr = proc_setattr,
305}; 319};
306 320
307static int 321static struct dentry *proc_fdinfo_instantiate(struct dentry *dentry,
308proc_fdinfo_instantiate(struct inode *dir, struct dentry *dentry, 322 struct task_struct *task, const void *ptr)
309 struct task_struct *task, const void *ptr)
310{ 323{
311 unsigned fd = (unsigned long)ptr; 324 const struct fd_data *data = ptr;
312 struct proc_inode *ei; 325 struct proc_inode *ei;
313 struct inode *inode; 326 struct inode *inode;
314 327
315 inode = proc_pid_make_inode(dir->i_sb, task, S_IFREG | S_IRUSR); 328 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFREG | S_IRUSR);
316 if (!inode) 329 if (!inode)
317 goto out; 330 return ERR_PTR(-ENOENT);
318 331
319 ei = PROC_I(inode); 332 ei = PROC_I(inode);
320 ei->fd = fd; 333 ei->fd = data->fd;
321 334
322 inode->i_fop = &proc_fdinfo_file_operations; 335 inode->i_fop = &proc_fdinfo_file_operations;
336 tid_fd_update_inode(task, inode, 0);
323 337
324 d_set_d_op(dentry, &tid_fd_dentry_operations); 338 d_set_d_op(dentry, &tid_fd_dentry_operations);
325 d_add(dentry, inode); 339 return d_splice_alias(inode, dentry);
326
327 /* Close the race of the process dying before we return the dentry */
328 if (tid_fd_revalidate(dentry, 0))
329 return 0;
330 out:
331 return -ENOENT;
332} 340}
333 341
334static struct dentry * 342static struct dentry *
diff --git a/fs/proc/generic.c b/fs/proc/generic.c
index 02bb1914f5f7..7b4d9714f248 100644
--- a/fs/proc/generic.c
+++ b/fs/proc/generic.c
@@ -257,8 +257,7 @@ struct dentry *proc_lookup_de(struct inode *dir, struct dentry *dentry,
257 if (!inode) 257 if (!inode)
258 return ERR_PTR(-ENOMEM); 258 return ERR_PTR(-ENOMEM);
259 d_set_d_op(dentry, &proc_misc_dentry_ops); 259 d_set_d_op(dentry, &proc_misc_dentry_ops);
260 d_add(dentry, inode); 260 return d_splice_alias(inode, dentry);
261 return NULL;
262 } 261 }
263 read_unlock(&proc_subdir_lock); 262 read_unlock(&proc_subdir_lock);
264 return ERR_PTR(-ENOENT); 263 return ERR_PTR(-ENOENT);
diff --git a/fs/proc/internal.h b/fs/proc/internal.h
index a318ae5b36b4..43c70c9e6b62 100644
--- a/fs/proc/internal.h
+++ b/fs/proc/internal.h
@@ -152,14 +152,14 @@ extern const struct dentry_operations pid_dentry_operations;
152extern int pid_getattr(const struct path *, struct kstat *, u32, unsigned int); 152extern int pid_getattr(const struct path *, struct kstat *, u32, unsigned int);
153extern int proc_setattr(struct dentry *, struct iattr *); 153extern int proc_setattr(struct dentry *, struct iattr *);
154extern struct inode *proc_pid_make_inode(struct super_block *, struct task_struct *, umode_t); 154extern struct inode *proc_pid_make_inode(struct super_block *, struct task_struct *, umode_t);
155extern int pid_revalidate(struct dentry *, unsigned int); 155extern void pid_update_inode(struct task_struct *, struct inode *);
156extern int pid_delete_dentry(const struct dentry *); 156extern int pid_delete_dentry(const struct dentry *);
157extern int proc_pid_readdir(struct file *, struct dir_context *); 157extern int proc_pid_readdir(struct file *, struct dir_context *);
158extern struct dentry *proc_pid_lookup(struct inode *, struct dentry *, unsigned int); 158extern struct dentry *proc_pid_lookup(struct inode *, struct dentry *, unsigned int);
159extern loff_t mem_lseek(struct file *, loff_t, int); 159extern loff_t mem_lseek(struct file *, loff_t, int);
160 160
161/* Lookups */ 161/* Lookups */
162typedef int instantiate_t(struct inode *, struct dentry *, 162typedef struct dentry *instantiate_t(struct dentry *,
163 struct task_struct *, const void *); 163 struct task_struct *, const void *);
164extern bool proc_fill_cache(struct file *, struct dir_context *, const char *, int, 164extern bool proc_fill_cache(struct file *, struct dir_context *, const char *, int,
165 instantiate_t, struct task_struct *, const void *); 165 instantiate_t, struct task_struct *, const void *);
diff --git a/fs/proc/namespaces.c b/fs/proc/namespaces.c
index 59b17e509f46..dd2b35f78b09 100644
--- a/fs/proc/namespaces.c
+++ b/fs/proc/namespaces.c
@@ -87,28 +87,24 @@ static const struct inode_operations proc_ns_link_inode_operations = {
87 .setattr = proc_setattr, 87 .setattr = proc_setattr,
88}; 88};
89 89
90static int proc_ns_instantiate(struct inode *dir, 90static struct dentry *proc_ns_instantiate(struct dentry *dentry,
91 struct dentry *dentry, struct task_struct *task, const void *ptr) 91 struct task_struct *task, const void *ptr)
92{ 92{
93 const struct proc_ns_operations *ns_ops = ptr; 93 const struct proc_ns_operations *ns_ops = ptr;
94 struct inode *inode; 94 struct inode *inode;
95 struct proc_inode *ei; 95 struct proc_inode *ei;
96 96
97 inode = proc_pid_make_inode(dir->i_sb, task, S_IFLNK | S_IRWXUGO); 97 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK | S_IRWXUGO);
98 if (!inode) 98 if (!inode)
99 goto out; 99 return ERR_PTR(-ENOENT);
100 100
101 ei = PROC_I(inode); 101 ei = PROC_I(inode);
102 inode->i_op = &proc_ns_link_inode_operations; 102 inode->i_op = &proc_ns_link_inode_operations;
103 ei->ns_ops = ns_ops; 103 ei->ns_ops = ns_ops;
104 pid_update_inode(task, inode);
104 105
105 d_set_d_op(dentry, &pid_dentry_operations); 106 d_set_d_op(dentry, &pid_dentry_operations);
106 d_add(dentry, inode); 107 return d_splice_alias(inode, dentry);
107 /* Close the race of the process dying before we return the dentry */
108 if (pid_revalidate(dentry, 0))
109 return 0;
110out:
111 return -ENOENT;
112} 108}
113 109
114static int proc_ns_dir_readdir(struct file *file, struct dir_context *ctx) 110static int proc_ns_dir_readdir(struct file *file, struct dir_context *ctx)
@@ -147,12 +143,10 @@ const struct file_operations proc_ns_dir_operations = {
147static struct dentry *proc_ns_dir_lookup(struct inode *dir, 143static struct dentry *proc_ns_dir_lookup(struct inode *dir,
148 struct dentry *dentry, unsigned int flags) 144 struct dentry *dentry, unsigned int flags)
149{ 145{
150 int error;
151 struct task_struct *task = get_proc_task(dir); 146 struct task_struct *task = get_proc_task(dir);
152 const struct proc_ns_operations **entry, **last; 147 const struct proc_ns_operations **entry, **last;
153 unsigned int len = dentry->d_name.len; 148 unsigned int len = dentry->d_name.len;
154 149 struct dentry *res = ERR_PTR(-ENOENT);
155 error = -ENOENT;
156 150
157 if (!task) 151 if (!task)
158 goto out_no_task; 152 goto out_no_task;
@@ -167,11 +161,11 @@ static struct dentry *proc_ns_dir_lookup(struct inode *dir,
167 if (entry == last) 161 if (entry == last)
168 goto out; 162 goto out;
169 163
170 error = proc_ns_instantiate(dir, dentry, task, *entry); 164 res = proc_ns_instantiate(dentry, task, *entry);
171out: 165out:
172 put_task_struct(task); 166 put_task_struct(task);
173out_no_task: 167out_no_task:
174 return ERR_PTR(error); 168 return res;
175} 169}
176 170
177const struct inode_operations proc_ns_dir_inode_operations = { 171const struct inode_operations proc_ns_dir_inode_operations = {
diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
index 8989936f2995..4d765e5e91ed 100644
--- a/fs/proc/proc_sysctl.c
+++ b/fs/proc/proc_sysctl.c
@@ -554,9 +554,8 @@ static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
554 if (!inode) 554 if (!inode)
555 goto out; 555 goto out;
556 556
557 err = NULL;
558 d_set_d_op(dentry, &proc_sys_dentry_operations); 557 d_set_d_op(dentry, &proc_sys_dentry_operations);
559 d_add(dentry, inode); 558 err = d_splice_alias(inode, dentry);
560 559
561out: 560out:
562 if (h) 561 if (h)
@@ -684,6 +683,7 @@ static bool proc_sys_fill_cache(struct file *file,
684 if (IS_ERR(child)) 683 if (IS_ERR(child))
685 return false; 684 return false;
686 if (d_in_lookup(child)) { 685 if (d_in_lookup(child)) {
686 struct dentry *res;
687 inode = proc_sys_make_inode(dir->d_sb, head, table); 687 inode = proc_sys_make_inode(dir->d_sb, head, table);
688 if (!inode) { 688 if (!inode) {
689 d_lookup_done(child); 689 d_lookup_done(child);
@@ -691,7 +691,16 @@ static bool proc_sys_fill_cache(struct file *file,
691 return false; 691 return false;
692 } 692 }
693 d_set_d_op(child, &proc_sys_dentry_operations); 693 d_set_d_op(child, &proc_sys_dentry_operations);
694 d_add(child, inode); 694 res = d_splice_alias(inode, child);
695 d_lookup_done(child);
696 if (unlikely(res)) {
697 if (IS_ERR(res)) {
698 dput(child);
699 return false;
700 }
701 dput(child);
702 child = res;
703 }
695 } 704 }
696 } 705 }
697 inode = d_inode(child); 706 inode = d_inode(child);
diff --git a/fs/qnx4/namei.c b/fs/qnx4/namei.c
index eca27878079d..8d72221735d7 100644
--- a/fs/qnx4/namei.c
+++ b/fs/qnx4/namei.c
@@ -114,13 +114,9 @@ struct dentry * qnx4_lookup(struct inode *dir, struct dentry *dentry, unsigned i
114 brelse(bh); 114 brelse(bh);
115 115
116 foundinode = qnx4_iget(dir->i_sb, ino); 116 foundinode = qnx4_iget(dir->i_sb, ino);
117 if (IS_ERR(foundinode)) { 117 if (IS_ERR(foundinode))
118 QNX4DEBUG((KERN_ERR "qnx4: lookup->iget -> error %ld\n", 118 QNX4DEBUG((KERN_ERR "qnx4: lookup->iget -> error %ld\n",
119 PTR_ERR(foundinode))); 119 PTR_ERR(foundinode)));
120 return ERR_CAST(foundinode);
121 }
122out: 120out:
123 d_add(dentry, foundinode); 121 return d_splice_alias(foundinode, dentry);
124
125 return NULL;
126} 122}
diff --git a/fs/qnx6/namei.c b/fs/qnx6/namei.c
index 72c2770830be..e2e98e653b8d 100644
--- a/fs/qnx6/namei.c
+++ b/fs/qnx6/namei.c
@@ -29,15 +29,11 @@ struct dentry *qnx6_lookup(struct inode *dir, struct dentry *dentry,
29 if (ino) { 29 if (ino) {
30 foundinode = qnx6_iget(dir->i_sb, ino); 30 foundinode = qnx6_iget(dir->i_sb, ino);
31 qnx6_put_page(page); 31 qnx6_put_page(page);
32 if (IS_ERR(foundinode)) { 32 if (IS_ERR(foundinode))
33 pr_debug("lookup->iget -> error %ld\n", 33 pr_debug("lookup->iget -> error %ld\n",
34 PTR_ERR(foundinode)); 34 PTR_ERR(foundinode));
35 return ERR_CAST(foundinode);
36 }
37 } else { 35 } else {
38 pr_debug("%s(): not found %s\n", __func__, name); 36 pr_debug("%s(): not found %s\n", __func__, name);
39 return NULL;
40 } 37 }
41 d_add(dentry, foundinode); 38 return d_splice_alias(foundinode, dentry);
42 return NULL;
43} 39}
diff --git a/fs/romfs/super.c b/fs/romfs/super.c
index 8f06fd1f3d69..6ccb51993a76 100644
--- a/fs/romfs/super.c
+++ b/fs/romfs/super.c
@@ -213,7 +213,7 @@ static struct dentry *romfs_lookup(struct inode *dir, struct dentry *dentry,
213 unsigned int flags) 213 unsigned int flags)
214{ 214{
215 unsigned long offset, maxoff; 215 unsigned long offset, maxoff;
216 struct inode *inode; 216 struct inode *inode = NULL;
217 struct romfs_inode ri; 217 struct romfs_inode ri;
218 const char *name; /* got from dentry */ 218 const char *name; /* got from dentry */
219 int len, ret; 219 int len, ret;
@@ -233,7 +233,7 @@ static struct dentry *romfs_lookup(struct inode *dir, struct dentry *dentry,
233 233
234 for (;;) { 234 for (;;) {
235 if (!offset || offset >= maxoff) 235 if (!offset || offset >= maxoff)
236 goto out0; 236 break;
237 237
238 ret = romfs_dev_read(dir->i_sb, offset, &ri, sizeof(ri)); 238 ret = romfs_dev_read(dir->i_sb, offset, &ri, sizeof(ri));
239 if (ret < 0) 239 if (ret < 0)
@@ -244,37 +244,19 @@ static struct dentry *romfs_lookup(struct inode *dir, struct dentry *dentry,
244 len); 244 len);
245 if (ret < 0) 245 if (ret < 0)
246 goto error; 246 goto error;
247 if (ret == 1) 247 if (ret == 1) {
248 /* Hard link handling */
249 if ((be32_to_cpu(ri.next) & ROMFH_TYPE) == ROMFH_HRD)
250 offset = be32_to_cpu(ri.spec) & ROMFH_MASK;
251 inode = romfs_iget(dir->i_sb, offset);
248 break; 252 break;
253 }
249 254
250 /* next entry */ 255 /* next entry */
251 offset = be32_to_cpu(ri.next) & ROMFH_MASK; 256 offset = be32_to_cpu(ri.next) & ROMFH_MASK;
252 } 257 }
253 258
254 /* Hard link handling */ 259 return d_splice_alias(inode, dentry);
255 if ((be32_to_cpu(ri.next) & ROMFH_TYPE) == ROMFH_HRD)
256 offset = be32_to_cpu(ri.spec) & ROMFH_MASK;
257
258 inode = romfs_iget(dir->i_sb, offset);
259 if (IS_ERR(inode)) {
260 ret = PTR_ERR(inode);
261 goto error;
262 }
263 goto outi;
264
265 /*
266 * it's a bit funky, _lookup needs to return an error code
267 * (negative) or a NULL, both as a dentry. ENOENT should not
268 * be returned, instead we need to create a negative dentry by
269 * d_add(dentry, NULL); and return 0 as no error.
270 * (Although as I see, it only matters on writable file
271 * systems).
272 */
273out0:
274 inode = NULL;
275outi:
276 d_add(dentry, inode);
277 ret = 0;
278error: 260error:
279 return ERR_PTR(ret); 261 return ERR_PTR(ret);
280} 262}
diff --git a/fs/sysv/namei.c b/fs/sysv/namei.c
index 250b0755b908..4d5d20491ffd 100644
--- a/fs/sysv/namei.c
+++ b/fs/sysv/namei.c
@@ -51,14 +51,9 @@ static struct dentry *sysv_lookup(struct inode * dir, struct dentry * dentry, un
51 if (dentry->d_name.len > SYSV_NAMELEN) 51 if (dentry->d_name.len > SYSV_NAMELEN)
52 return ERR_PTR(-ENAMETOOLONG); 52 return ERR_PTR(-ENAMETOOLONG);
53 ino = sysv_inode_by_name(dentry); 53 ino = sysv_inode_by_name(dentry);
54 54 if (ino)
55 if (ino) {
56 inode = sysv_iget(dir->i_sb, ino); 55 inode = sysv_iget(dir->i_sb, ino);
57 if (IS_ERR(inode)) 56 return d_splice_alias(inode, dentry);
58 return ERR_CAST(inode);
59 }
60 d_add(dentry, inode);
61 return NULL;
62} 57}
63 58
64static int sysv_mknod(struct inode * dir, struct dentry * dentry, umode_t mode, dev_t rdev) 59static int sysv_mknod(struct inode * dir, struct dentry * dentry, umode_t mode, dev_t rdev)
diff --git a/fs/ubifs/dir.c b/fs/ubifs/dir.c
index 9d7fb88e172e..4e267cc21c77 100644
--- a/fs/ubifs/dir.c
+++ b/fs/ubifs/dir.c
@@ -214,7 +214,7 @@ static struct dentry *ubifs_lookup(struct inode *dir, struct dentry *dentry,
214 int err; 214 int err;
215 union ubifs_key key; 215 union ubifs_key key;
216 struct inode *inode = NULL; 216 struct inode *inode = NULL;
217 struct ubifs_dent_node *dent; 217 struct ubifs_dent_node *dent = NULL;
218 struct ubifs_info *c = dir->i_sb->s_fs_info; 218 struct ubifs_info *c = dir->i_sb->s_fs_info;
219 struct fscrypt_name nm; 219 struct fscrypt_name nm;
220 220
@@ -229,14 +229,14 @@ static struct dentry *ubifs_lookup(struct inode *dir, struct dentry *dentry,
229 return ERR_PTR(err); 229 return ERR_PTR(err);
230 230
231 if (fname_len(&nm) > UBIFS_MAX_NLEN) { 231 if (fname_len(&nm) > UBIFS_MAX_NLEN) {
232 err = -ENAMETOOLONG; 232 inode = ERR_PTR(-ENAMETOOLONG);
233 goto out_fname; 233 goto done;
234 } 234 }
235 235
236 dent = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS); 236 dent = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
237 if (!dent) { 237 if (!dent) {
238 err = -ENOMEM; 238 inode = ERR_PTR(-ENOMEM);
239 goto out_fname; 239 goto done;
240 } 240 }
241 241
242 if (nm.hash) { 242 if (nm.hash) {
@@ -250,16 +250,16 @@ static struct dentry *ubifs_lookup(struct inode *dir, struct dentry *dentry,
250 } 250 }
251 251
252 if (err) { 252 if (err) {
253 if (err == -ENOENT) { 253 if (err == -ENOENT)
254 dbg_gen("not found"); 254 dbg_gen("not found");
255 goto done; 255 else
256 } 256 inode = ERR_PTR(err);
257 goto out_dent; 257 goto done;
258 } 258 }
259 259
260 if (dbg_check_name(c, dent, &nm)) { 260 if (dbg_check_name(c, dent, &nm)) {
261 err = -EINVAL; 261 inode = ERR_PTR(-EINVAL);
262 goto out_dent; 262 goto done;
263 } 263 }
264 264
265 inode = ubifs_iget(dir->i_sb, le64_to_cpu(dent->inum)); 265 inode = ubifs_iget(dir->i_sb, le64_to_cpu(dent->inum));
@@ -272,7 +272,7 @@ static struct dentry *ubifs_lookup(struct inode *dir, struct dentry *dentry,
272 ubifs_err(c, "dead directory entry '%pd', error %d", 272 ubifs_err(c, "dead directory entry '%pd', error %d",
273 dentry, err); 273 dentry, err);
274 ubifs_ro_mode(c, err); 274 ubifs_ro_mode(c, err);
275 goto out_dent; 275 goto done;
276 } 276 }
277 277
278 if (ubifs_crypt_is_encrypted(dir) && 278 if (ubifs_crypt_is_encrypted(dir) &&
@@ -280,27 +280,14 @@ static struct dentry *ubifs_lookup(struct inode *dir, struct dentry *dentry,
280 !fscrypt_has_permitted_context(dir, inode)) { 280 !fscrypt_has_permitted_context(dir, inode)) {
281 ubifs_warn(c, "Inconsistent encryption contexts: %lu/%lu", 281 ubifs_warn(c, "Inconsistent encryption contexts: %lu/%lu",
282 dir->i_ino, inode->i_ino); 282 dir->i_ino, inode->i_ino);
283 err = -EPERM; 283 iput(inode);
284 goto out_inode; 284 inode = ERR_PTR(-EPERM);
285 } 285 }
286 286
287done: 287done:
288 kfree(dent); 288 kfree(dent);
289 fscrypt_free_filename(&nm); 289 fscrypt_free_filename(&nm);
290 /* 290 return d_splice_alias(inode, dentry);
291 * Note, d_splice_alias() would be required instead if we supported
292 * NFS.
293 */
294 d_add(dentry, inode);
295 return NULL;
296
297out_inode:
298 iput(inode);
299out_dent:
300 kfree(dent);
301out_fname:
302 fscrypt_free_filename(&nm);
303 return ERR_PTR(err);
304} 291}
305 292
306static int ubifs_create(struct inode *dir, struct dentry *dentry, umode_t mode, 293static int ubifs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
diff --git a/fs/xfs/xfs_iops.c b/fs/xfs/xfs_iops.c
index a3ed3c811dfa..df42e4cb4dc4 100644
--- a/fs/xfs/xfs_iops.c
+++ b/fs/xfs/xfs_iops.c
@@ -260,6 +260,7 @@ xfs_vn_lookup(
260 struct dentry *dentry, 260 struct dentry *dentry,
261 unsigned int flags) 261 unsigned int flags)
262{ 262{
263 struct inode *inode;
263 struct xfs_inode *cip; 264 struct xfs_inode *cip;
264 struct xfs_name name; 265 struct xfs_name name;
265 int error; 266 int error;
@@ -269,14 +270,13 @@ xfs_vn_lookup(
269 270
270 xfs_dentry_to_name(&name, dentry); 271 xfs_dentry_to_name(&name, dentry);
271 error = xfs_lookup(XFS_I(dir), &name, &cip, NULL); 272 error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
272 if (unlikely(error)) { 273 if (likely(!error))
273 if (unlikely(error != -ENOENT)) 274 inode = VFS_I(cip);
274 return ERR_PTR(error); 275 else if (likely(error == -ENOENT))
275 d_add(dentry, NULL); 276 inode = NULL;
276 return NULL; 277 else
277 } 278 inode = ERR_PTR(error);
278 279 return d_splice_alias(inode, dentry);
279 return d_splice_alias(VFS_I(cip), dentry);
280} 280}
281 281
282STATIC struct dentry * 282STATIC struct dentry *