Linux cgroups development
 help / color / mirror / Atom feed
* [PATCH 1/3] fuse: return ERR_PTR() from fuse_iget()
  2026-12-01 13:34 [PATCH 0/3] fs: return ERR_PTR() from iget_locked() and friends Konstantin Adzer
@ 2026-12-01 13:06 ` Konstantin Adzer
  2026-12-01 13:06 ` [PATCH 2/3] hfs: return ERR_PTR() from hfs_iget() Konstantin Adzer
  2026-12-01 13:09 ` [PATCH 3/3] fs: return ERR_PTR() from iget_locked() and friends Konstantin Adzer
  2 siblings, 0 replies; 4+ messages in thread
From: Konstantin Adzer @ 2026-12-01 13:06 UTC (permalink / raw)
  To: Alexander Viro, Christian Brauner
  Cc: Jonathan Corbet, Shuah Khan, Randy Dunlap, Dan Williams,
	Vishal Verma, Dave Jiang, Alison Schofield, Eric Van Hensbergen,
	Latchesar Ionkov, Dominique Martinet, Christian Schoenebeck,
	David Sterba, David Howells, Marc Dionne, Luis de Bethencourt,
	Salah Triki, Chris Mason, Ilya Dryomov, Alex Markuze, Xiubo Li,
	Viacheslav Dubeyko, Jan Harkes, coda, Nicolas Pitre, Tyler Hicks,
	Gao Xiang, Chao Yu, Yue Hu, Jingbo Xu, Sandeep Dhavale, Hongbo Li,
	Chunhai Guo, Jan Kara, Theodore Ts'o, Andreas Dilger,
	Baokun Li, Ojaswin Mujoo, Ritesh Harjani (IBM), Zhang Yi,
	Jaegeuk Kim, Miklos Szeredi, Andreas Gruenbacher,
	John Paul Adrian Glaubitz, Yangtao Li, Richard Weinberger,
	Anton Ivanov, Johannes Berg, Mikulas Patocka, David Woodhouse,
	Dave Kleikamp, Greg Kroah-Hartman, Tejun Heo, Trond Myklebust,
	Anna Schumaker, Ryusuke Konishi, Namjae Jeon, Hyunchul Lee,
	Konstantin Komarov, Mark Fasheh, Joel Becker, Joseph Qi,
	Heming Zhao, Mike Marshall, Martin Brandenburg, Amir Goldstein,
	Anders Larsen, Paulo Alcantara, Ronnie Sahlberg, Shyam Prasad N,
	Tom Talpey, Bharath SM, Phillip Lougher, Zhihao Cheng,
	Jori Koolstra, Damien Le Moal, Naohiro Aota, Johannes Thumshirn,
	Johannes Weiner, Michal Koutný, linux-doc, nvdimm, linux-cxl,
	v9fs, linux-fsdevel, linux-afs, linux-kernel, linux-btrfs,
	ceph-devel, codalist, ecryptfs, linux-erofs, linux-ext4,
	linux-f2fs-devel, fuse-devel, gfs2, linux-um, linux-mtd,
	jfs-discussion, driver-core, linux-nfs, linux-nilfs, ntfs, ntfs3,
	ocfs2-devel, devel, linux-unionfs, linux-cifs, samba-technical,
	cgroups

fuse_iget() returns NULL on any failure, and its callers turn that into
-ENOMEM. Return ERR_PTR(-ENOMEM) instead and pass the error on in the
callers, so that fuse_iget() can forward the error of iget5_locked()
once that returns ERR_PTR().

No functional change.

Signed-off-by: Konstantin Adzer <info@massivetech.ru>
---
 fs/fuse/dir.c     | 13 +++++++------
 fs/fuse/inode.c   | 14 +++++++++-----
 fs/fuse/readdir.c |  4 ++--
 3 files changed, 18 insertions(+), 13 deletions(-)

diff --git a/fs/fuse/dir.c b/fs/fuse/dir.c
index a04d93d5fc44..e6bf99b76993 100644
--- a/fs/fuse/dir.c
+++ b/fs/fuse/dir.c
@@ -599,8 +599,9 @@ int fuse_lookup_name(struct super_block *sb, u64 nodeid, const struct qstr *name
 	*inode = fuse_iget(sb, outarg->nodeid, outarg->generation,
 			   &outarg->attr, ATTR_TIMEOUT(outarg),
 			   attr_version, evict_ctr);
-	err = -ENOMEM;
-	if (!*inode) {
+	if (IS_ERR(*inode)) {
+		err = PTR_ERR(*inode);
+		*inode = NULL;
 		fuse_chan_queue_forget(fm->fc->chan, forget, outarg->nodeid, 1);
 		goto out;
 	}
@@ -903,11 +904,11 @@ static int fuse_create_open(const struct mnt_idmap *idmap, struct inode *dir,
 	ff->open_flags = outopenp->open_flags;
 	inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
 			  &outentry.attr, ATTR_TIMEOUT(&outentry), 0, 0);
-	if (!inode) {
+	if (IS_ERR(inode)) {
 		flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
 		fuse_sync_release(NULL, ff, flags);
 		fuse_chan_queue_forget(fm->fc->chan, forget, outentry.nodeid, 1);
-		err = -ENOMEM;
+		err = PTR_ERR(inode);
 		goto out_err;
 	}
 	kfree(forget);
@@ -1030,9 +1031,9 @@ static struct dentry *create_new_entry(const struct mnt_idmap *idmap, struct fus
 
 	inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
 			  &outarg.attr, ATTR_TIMEOUT(&outarg), 0, 0);
-	if (!inode) {
+	if (IS_ERR(inode)) {
 		fuse_chan_queue_forget(fm->fc->chan, forget, outarg.nodeid, 1);
-		return ERR_PTR(-ENOMEM);
+		return ERR_CAST(inode);
 	}
 	fuse_dentry_set_epoch(entry, epoch);
 	kfree(forget);
diff --git a/fs/fuse/inode.c b/fs/fuse/inode.c
index 399e5b28c111..23a4bbf5dfa0 100644
--- a/fs/fuse/inode.c
+++ b/fs/fuse/inode.c
@@ -498,7 +498,7 @@ struct inode *fuse_iget(struct super_block *sb, u64 nodeid,
 
 		inode = new_inode(sb);
 		if (!inode)
-			return NULL;
+			return ERR_PTR(-ENOMEM);
 
 		fuse_init_inode(inode, attr, fc);
 		fi = get_fuse_inode(inode);
@@ -506,7 +506,7 @@ struct inode *fuse_iget(struct super_block *sb, u64 nodeid,
 		fi->submount_lookup = fuse_alloc_submount_lookup();
 		if (!fi->submount_lookup) {
 			iput(inode);
-			return NULL;
+			return ERR_PTR(-ENOMEM);
 		}
 		/* Sets nlookup = 1 on fi->submount_lookup->nlookup */
 		fuse_init_submount_lookup(fi->submount_lookup, nodeid);
@@ -517,7 +517,7 @@ struct inode *fuse_iget(struct super_block *sb, u64 nodeid,
 retry:
 	inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid);
 	if (!inode)
-		return NULL;
+		return ERR_PTR(-ENOMEM);
 
 	is_new_inode = inode_state_read_once(inode) & I_NEW;
 	if (is_new_inode) {
@@ -1716,8 +1716,8 @@ static int fuse_fill_super_submount(struct super_block *sb,
 	fuse_fill_attr_from_inode(&root_attr, parent_fi);
 	root = fuse_iget(sb, parent_fi->nodeid, 0, &root_attr, 0, 0,
 			 fuse_get_evict_ctr(fm->fc));
-	if (!root)
-		return -ENOMEM;
+	if (IS_ERR(root))
+		return PTR_ERR(root);
 	/*
 	 * This inode is just a duplicate, so it is not looked up and
 	 * its nlookup should not be incremented.  fuse_iget() does
@@ -1858,6 +1858,10 @@ int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx)
 
 	err = -ENOMEM;
 	root = fuse_get_root_inode(sb, ctx->rootmode);
+	if (IS_ERR(root)) {
+		err = PTR_ERR(root);
+		goto err_free_vdax;
+	}
 	set_default_d_op(sb, &fuse_dentry_operations);
 	root_dentry = d_make_root(root);
 	if (!root_dentry)
diff --git a/fs/fuse/readdir.c b/fs/fuse/readdir.c
index c1e2a8fd834e..880b489ec42c 100644
--- a/fs/fuse/readdir.c
+++ b/fs/fuse/readdir.c
@@ -239,10 +239,10 @@ static int fuse_direntplus_link(struct file *file,
 		inode = fuse_iget(dir->i_sb, o->nodeid, o->generation,
 				  &o->attr, ATTR_TIMEOUT(o),
 				  attr_version, evict_ctr);
-		if (!inode) {
+		if (IS_ERR(inode)) {
 			d_lookup_done(dentry);
 			dput(dentry);
-			return -ENOMEM;
+			return PTR_ERR(inode);
 		}
 
 		fuse_dentry_set_epoch(dentry, epoch);
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/3] hfs: return ERR_PTR() from hfs_iget()
  2026-12-01 13:34 [PATCH 0/3] fs: return ERR_PTR() from iget_locked() and friends Konstantin Adzer
  2026-12-01 13:06 ` [PATCH 1/3] fuse: return ERR_PTR() from fuse_iget() Konstantin Adzer
@ 2026-12-01 13:06 ` Konstantin Adzer
  2026-12-01 13:09 ` [PATCH 3/3] fs: return ERR_PTR() from iget_locked() and friends Konstantin Adzer
  2 siblings, 0 replies; 4+ messages in thread
From: Konstantin Adzer @ 2026-12-01 13:06 UTC (permalink / raw)
  To: Alexander Viro, Christian Brauner
  Cc: Jonathan Corbet, Shuah Khan, Randy Dunlap, Dan Williams,
	Vishal Verma, Dave Jiang, Alison Schofield, Eric Van Hensbergen,
	Latchesar Ionkov, Dominique Martinet, Christian Schoenebeck,
	David Sterba, David Howells, Marc Dionne, Luis de Bethencourt,
	Salah Triki, Chris Mason, Ilya Dryomov, Alex Markuze, Xiubo Li,
	Viacheslav Dubeyko, Jan Harkes, coda, Nicolas Pitre, Tyler Hicks,
	Gao Xiang, Chao Yu, Yue Hu, Jingbo Xu, Sandeep Dhavale, Hongbo Li,
	Chunhai Guo, Jan Kara, Theodore Ts'o, Andreas Dilger,
	Baokun Li, Ojaswin Mujoo, Ritesh Harjani (IBM), Zhang Yi,
	Jaegeuk Kim, Miklos Szeredi, Andreas Gruenbacher,
	John Paul Adrian Glaubitz, Yangtao Li, Richard Weinberger,
	Anton Ivanov, Johannes Berg, Mikulas Patocka, David Woodhouse,
	Dave Kleikamp, Greg Kroah-Hartman, Tejun Heo, Trond Myklebust,
	Anna Schumaker, Ryusuke Konishi, Namjae Jeon, Hyunchul Lee,
	Konstantin Komarov, Mark Fasheh, Joel Becker, Joseph Qi,
	Heming Zhao, Mike Marshall, Martin Brandenburg, Amir Goldstein,
	Anders Larsen, Paulo Alcantara, Ronnie Sahlberg, Shyam Prasad N,
	Tom Talpey, Bharath SM, Phillip Lougher, Zhihao Cheng,
	Jori Koolstra, Damien Le Moal, Naohiro Aota, Johannes Thumshirn,
	Johannes Weiner, Michal Koutný, linux-doc, nvdimm, linux-cxl,
	v9fs, linux-fsdevel, linux-afs, linux-kernel, linux-btrfs,
	ceph-devel, codalist, ecryptfs, linux-erofs, linux-ext4,
	linux-f2fs-devel, fuse-devel, gfs2, linux-um, linux-mtd,
	jfs-discussion, driver-core, linux-nfs, linux-nilfs, ntfs, ntfs3,
	ocfs2-devel, devel, linux-unionfs, linux-cifs, samba-technical,
	cgroups

hfs_iget() returns NULL both for a catalog record that is neither a
file nor a directory and for a failed iget5_locked(). hfs_lookup() turns
that into -EACCES and hfs_fill_super() into -EINVAL.

Return ERR_PTR(-EIO) for the unexpected record type, the error
hfs_read_inode() returns for a bad record, and ERR_PTR(-ENOMEM) for a
failed iget5_locked(), and pass the error on in both callers, so that
hfs_iget() can forward the error of iget5_locked() once that returns
ERR_PTR().

Signed-off-by: Konstantin Adzer <info@massivetech.ru>
---
 fs/hfs/dir.c   | 2 --
 fs/hfs/inode.c | 8 +++++---
 fs/hfs/super.c | 4 +++-
 3 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/fs/hfs/dir.c b/fs/hfs/dir.c
index f6b97da19788..b6b8945712a9 100644
--- a/fs/hfs/dir.c
+++ b/fs/hfs/dir.c
@@ -35,8 +35,6 @@ static struct dentry *hfs_lookup(struct inode *dir, struct dentry *dentry,
 			inode = ERR_PTR(res);
 	} else {
 		inode = hfs_iget(dir->i_sb, &fd.search_key->cat, &rec);
-		if (!inode)
-			inode = ERR_PTR(-EACCES);
 	}
 	hfs_find_exit(&fd);
 	return d_splice_alias(inode, dentry);
diff --git a/fs/hfs/inode.c b/fs/hfs/inode.c
index 7e590e1308ea..b836b2f8d544 100644
--- a/fs/hfs/inode.c
+++ b/fs/hfs/inode.c
@@ -433,7 +433,7 @@ static int hfs_read_inode(struct inode *inode, void *data)
  *
  * Given the MDB for a HFS filesystem, a 'key' and an 'entry' in
  * the catalog B-tree and the 'type' of the desired file return the
- * inode for that file/directory or NULL.  Note that 'type' indicates
+ * inode for that file/directory or an ERR_PTR().  Note that 'type' indicates
  * whether we want the actual file or directory, or the corresponding
  * metadata (AppleDouble header file or CAP metadata file).
  */
@@ -451,10 +451,12 @@ struct inode *hfs_iget(struct super_block *sb, struct hfs_cat_key *key, hfs_cat_
 		cnid = be32_to_cpu(rec->file.FlNum);
 		break;
 	default:
-		return NULL;
+		return ERR_PTR(-EIO);
 	}
 	inode = iget5_locked(sb, cnid, hfs_test_inode, hfs_read_inode, &data);
-	if (inode && (inode_state_read_once(inode) & I_NEW))
+	if (!inode)
+		return ERR_PTR(-ENOMEM);
+	if (inode_state_read_once(inode) & I_NEW)
 		unlock_new_inode(inode);
 	return inode;
 }
diff --git a/fs/hfs/super.c b/fs/hfs/super.c
index 7abc0e61a222..809da672fb0f 100644
--- a/fs/hfs/super.c
+++ b/fs/hfs/super.c
@@ -378,8 +378,10 @@ static int hfs_fill_super(struct super_block *sb, struct fs_context *fc)
 	res = -EINVAL;
 	root_inode = hfs_iget(sb, &fd.search_key->cat, &rec);
 	hfs_find_exit(&fd);
-	if (!root_inode)
+	if (IS_ERR(root_inode)) {
+		res = PTR_ERR(root_inode);
 		goto bail_no_root;
+	}
 
 	if (is_bad_inode(root_inode)) {
 		iput(root_inode);
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 3/3] fs: return ERR_PTR() from iget_locked() and friends
  2026-12-01 13:34 [PATCH 0/3] fs: return ERR_PTR() from iget_locked() and friends Konstantin Adzer
  2026-12-01 13:06 ` [PATCH 1/3] fuse: return ERR_PTR() from fuse_iget() Konstantin Adzer
  2026-12-01 13:06 ` [PATCH 2/3] hfs: return ERR_PTR() from hfs_iget() Konstantin Adzer
@ 2026-12-01 13:09 ` Konstantin Adzer
  2 siblings, 0 replies; 4+ messages in thread
From: Konstantin Adzer @ 2026-12-01 13:09 UTC (permalink / raw)
  To: Alexander Viro, Christian Brauner
  Cc: Jonathan Corbet, Shuah Khan, Randy Dunlap, Dan Williams,
	Vishal Verma, Dave Jiang, Alison Schofield, Eric Van Hensbergen,
	Latchesar Ionkov, Dominique Martinet, Christian Schoenebeck,
	David Sterba, David Howells, Marc Dionne, Luis de Bethencourt,
	Salah Triki, Chris Mason, Ilya Dryomov, Alex Markuze, Xiubo Li,
	Viacheslav Dubeyko, Jan Harkes, coda, Nicolas Pitre, Tyler Hicks,
	Gao Xiang, Chao Yu, Yue Hu, Jingbo Xu, Sandeep Dhavale, Hongbo Li,
	Chunhai Guo, Jan Kara, Theodore Ts'o, Andreas Dilger,
	Baokun Li, Ojaswin Mujoo, Ritesh Harjani (IBM), Zhang Yi,
	Jaegeuk Kim, Miklos Szeredi, Andreas Gruenbacher,
	John Paul Adrian Glaubitz, Yangtao Li, Richard Weinberger,
	Anton Ivanov, Johannes Berg, Mikulas Patocka, David Woodhouse,
	Dave Kleikamp, Greg Kroah-Hartman, Tejun Heo, Trond Myklebust,
	Anna Schumaker, Ryusuke Konishi, Namjae Jeon, Hyunchul Lee,
	Konstantin Komarov, Mark Fasheh, Joel Becker, Joseph Qi,
	Heming Zhao, Mike Marshall, Martin Brandenburg, Amir Goldstein,
	Anders Larsen, Paulo Alcantara, Ronnie Sahlberg, Shyam Prasad N,
	Tom Talpey, Bharath SM, Phillip Lougher, Zhihao Cheng,
	Jori Koolstra, Damien Le Moal, Naohiro Aota, Johannes Thumshirn,
	Johannes Weiner, Michal Koutný, linux-doc, nvdimm, linux-cxl,
	v9fs, linux-fsdevel, linux-afs, linux-kernel, linux-btrfs,
	ceph-devel, codalist, ecryptfs, linux-erofs, linux-ext4,
	linux-f2fs-devel, fuse-devel, gfs2, linux-um, linux-mtd,
	jfs-discussion, driver-core, linux-nfs, linux-nilfs, ntfs, ntfs3,
	ocfs2-devel, devel, linux-unionfs, linux-cifs, samba-technical,
	cgroups

find_inode() and find_inode_fast() return ERR_PTR(-ESTALE) for an inode
that is being created (I_CREATING), but iget_locked(), iget5_locked(),
iget5_locked_rcu() and inode_insert5() turn that into NULL, as they do
with a failed allocation and with an error from the set() callback.
Callers can only read NULL as -ENOMEM.

open_by_handle_at() on ext4 hits this when the handle points to a
deleted inode whose number is being reused by a file under creation:

  ext4_fh_to_dentry()
    ext4_nfs_get_inode()
      __ext4_iget(..., EXT4_IGET_HANDLE)
        iget_locked()        -> NULL, find_inode_fast() saw I_CREATING
      __ext4_iget()          -> ERR_PTR(-ENOMEM)

The caller gets ENOMEM instead of ESTALE although memory is not short,
and nfsd turns it into a jukebox/DELAY error, so an NFS client retries
instead of getting a stale file handle.

Make the four helpers return ERR_PTR(): -ENOMEM if no inode could be
allocated, -ESTALE for an inode under creation, and the error returned
by set(). insert_inode_locked4() still returns -EBUSY when it runs into
an inode under creation. Convert all callers to IS_ERR() and pass the
error on where they returned -ENOMEM so far. Wrappers that handed NULL
to their own callers (btrfs_iget_locked(), cifs_iget(), dax_dev_get(),
kernfs_get_inode(), nilfs_iget_locked(), ovl_iget5()) return the
ERR_PTR() as well.

Besides -ESTALE, callers now see:
- the -EIO or -ENOMEM of the set() callbacks of hfs, hostfs and ntfs
  instead of -ENOMEM;
- in ecryptfs, the error instead of -EACCES.
overlayfs keeps -EEXIST for a new inode that collides with an inode
under creation.

Fixes: c2b6d621c4ff ("new primitive: discard_new_inode()")
Closes: https://lore.kernel.org/179612584364.3457081.15142720814992093613@massivetech.ru
Signed-off-by: Konstantin Adzer <info@massivetech.ru>
---
 Documentation/filesystems/porting.rst |   9 ++
 drivers/dax/super.c                   |   8 +-
 fs/9p/vfs_inode.c                     |   4 +-
 fs/9p/vfs_inode_dotl.c                |   4 +-
 fs/affs/inode.c                       |   4 +-
 fs/afs/dynroot.c                      |  14 +--
 fs/afs/inode.c                        |  12 +--
 fs/befs/linuxvfs.c                    |   4 +-
 fs/btrfs/inode.c                      |  12 +--
 fs/ceph/inode.c                       |   4 +-
 fs/coda/cnode.c                       |   4 +-
 fs/cramfs/inode.c                     |   4 +-
 fs/ecryptfs/inode.c                   |   4 +-
 fs/erofs/inode.c                      |   4 +-
 fs/erofs/ishare.c                     |   6 +-
 fs/ext2/inode.c                       |   4 +-
 fs/ext4/inode.c                       |   4 +-
 fs/f2fs/inode.c                       |   4 +-
 fs/fuse/inode.c                       |   4 +-
 fs/gfs2/inode.c                       |   4 +-
 fs/hfs/btree.c                        |   2 +-
 fs/hfs/inode.c                        |   4 +-
 fs/hfsplus/super.c                    |   4 +-
 fs/hostfs/hostfs_kern.c               |   4 +-
 fs/hpfs/dir.c                         |   3 +-
 fs/hpfs/inode.c                       |   2 +-
 fs/hpfs/super.c                       |   2 +-
 fs/inode.c                            | 126 +++++++++++++++-----------
 fs/isofs/inode.c                      |   4 +-
 fs/jffs2/fs.c                         |   4 +-
 fs/jfs/inode.c                        |   4 +-
 fs/kernfs/inode.c                     |   8 +-
 fs/kernfs/mount.c                     |   4 +-
 fs/minix/inode.c                      |   4 +-
 fs/nfs/inode.c                        |   4 +-
 fs/nilfs2/cpfile.c                    |   4 +-
 fs/nilfs2/dat.c                       |   4 +-
 fs/nilfs2/ifile.c                     |   4 +-
 fs/nilfs2/inode.c                     |  16 ++--
 fs/nilfs2/sufile.c                    |   4 +-
 fs/ntfs/inode.c                       |  12 +--
 fs/ntfs3/inode.c                      |   4 +-
 fs/ocfs2/inode.c                      |   3 +-
 fs/openpromfs/inode.c                 |   5 +-
 fs/orangefs/inode.c                   |   4 +-
 fs/overlayfs/inode.c                  |  10 +-
 fs/qnx4/inode.c                       |   4 +-
 fs/qnx6/inode.c                       |   4 +-
 fs/romfs/super.c                      |   4 +-
 fs/smb/client/file.c                  |   5 +-
 fs/smb/client/inode.c                 |  12 ++-
 fs/smb/client/readdir.c               |   2 -
 fs/squashfs/inode.c                   |   4 +-
 fs/ubifs/super.c                      |   4 +-
 fs/udf/inode.c                        |   4 +-
 fs/ufs/inode.c                        |   4 +-
 fs/vboxsf/super.c                     |   4 +-
 fs/zonefs/super.c                     |   8 +-
 include/linux/kernfs.h                |   2 +-
 kernel/cgroup/cgroup.c                |   4 +-
 60 files changed, 228 insertions(+), 203 deletions(-)

diff --git a/Documentation/filesystems/porting.rst b/Documentation/filesystems/porting.rst
index 7d89f741f8b2..b13b11eb172c 100644
--- a/Documentation/filesystems/porting.rst
+++ b/Documentation/filesystems/porting.rst
@@ -1535,3 +1535,12 @@ the block device is still flushed, so the filesystem decides what its
 writeback still writes. Without a journal refusing only the inodes while
 the data and the bitmaps go out loses data. A remount to read-only must not
 mark the filesystem clean in that state.
+
+---
+
+**mandatory**
+
+iget_locked(), iget5_locked(), iget5_locked_rcu() and inode_insert5() return
+ERR_PTR() instead of NULL on failure: -ENOMEM if no inode could be allocated,
+-ESTALE if the inode is being created (I_CREATING), or the error returned by
+the set() callback. set() must return 0 or a negative errno.
diff --git a/drivers/dax/super.c b/drivers/dax/super.c
index c35a7a27583a..59db8ad6e076 100644
--- a/drivers/dax/super.c
+++ b/drivers/dax/super.c
@@ -573,8 +573,8 @@ static struct dax_device *dax_dev_get(dev_t devt)
 	inode = iget5_locked(dax_superblock, hash_32(devt + DAXFS_MAGIC, 31),
 			dax_test, dax_set, &devt);
 
-	if (!inode)
-		return NULL;
+	if (IS_ERR(inode))
+		return ERR_CAST(inode);
 
 	dax_dev = to_dax_dev(inode);
 	if (inode_state_read_once(inode) & I_NEW) {
@@ -612,7 +612,7 @@ struct dax_device *alloc_dax(void *private, const struct dax_operations *ops)
 
 	devt = MKDEV(MAJOR(dax_devt), minor);
 	dax_dev = dax_dev_get(devt);
-	if (!dax_dev)
+	if (IS_ERR(dax_dev))
 		goto err_dev;
 
 	dax_dev->ops = ops;
@@ -621,7 +621,7 @@ struct dax_device *alloc_dax(void *private, const struct dax_operations *ops)
 
  err_dev:
 	ida_free(&dax_minor_ida, minor);
-	return ERR_PTR(-ENOMEM);
+	return dax_dev;
 }
 EXPORT_SYMBOL_GPL(alloc_dax);
 
diff --git a/fs/9p/vfs_inode.c b/fs/9p/vfs_inode.c
index 73776416902c..4b3fd24c4b39 100644
--- a/fs/9p/vfs_inode.c
+++ b/fs/9p/vfs_inode.c
@@ -422,8 +422,8 @@ static struct inode *v9fs_qid_iget(struct super_block *sb,
 		test = v9fs_test_inode;
 
 	inode = iget5_locked(sb, QID2INO(qid), test, v9fs_set_inode, st);
-	if (!inode)
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(inode))
+		return inode;
 	if (!(inode_state_read_once(inode) & I_NEW))
 		return inode;
 	/*
diff --git a/fs/9p/vfs_inode_dotl.c b/fs/9p/vfs_inode_dotl.c
index b224a0893177..4bf691e71e14 100644
--- a/fs/9p/vfs_inode_dotl.c
+++ b/fs/9p/vfs_inode_dotl.c
@@ -110,8 +110,8 @@ static struct inode *v9fs_qid_iget_dotl(struct super_block *sb,
 		test = v9fs_test_inode_dotl;
 
 	inode = iget5_locked(sb, QID2INO(qid), test, v9fs_set_inode_dotl, st);
-	if (!inode)
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(inode))
+		return inode;
 	if (!(inode_state_read_once(inode) & I_NEW))
 		return inode;
 	/*
diff --git a/fs/affs/inode.c b/fs/affs/inode.c
index c7de9c793ab8..892469bb2389 100644
--- a/fs/affs/inode.c
+++ b/fs/affs/inode.c
@@ -27,8 +27,8 @@ struct inode *affs_iget(struct super_block *sb, unsigned long ino)
 	u16			 id;
 
 	inode = iget_locked(sb, ino);
-	if (!inode)
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(inode))
+		return inode;
 	if (!(inode_state_read_once(inode) & I_NEW))
 		return inode;
 
diff --git a/fs/afs/dynroot.c b/fs/afs/dynroot.c
index 6e3c8c691ba9..5cac5a2f9345 100644
--- a/fs/afs/dynroot.c
+++ b/fs/afs/dynroot.c
@@ -54,9 +54,9 @@ static struct inode *afs_iget_pseudo_dir(struct super_block *sb, ino_t ino)
 
 	inode = iget5_locked(sb, fid.vnode,
 			     afs_iget5_pseudo_test, afs_iget5_pseudo_set, &fid);
-	if (!inode) {
-		_leave(" = -ENOMEM");
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(inode)) {
+		_leave(" = %ld", PTR_ERR(inode));
+		return inode;
 	}
 
 	_debug("GOT INODE %p { ino=%llu, vl=%llx, vn=%llx, u=%x }",
@@ -254,8 +254,8 @@ static struct dentry *afs_lookup_atcell(struct inode *dir, struct dentry *dentry
 
 	inode = iget5_locked(dir->i_sb, fid.vnode,
 			     afs_iget5_pseudo_test, afs_iget5_pseudo_set, &fid);
-	if (!inode)
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(inode))
+		return ERR_CAST(inode);
 
 	vnode = AFS_FS_I(inode);
 
@@ -378,8 +378,8 @@ struct inode *afs_dynroot_iget_root(struct super_block *sb)
 
 	inode = iget5_locked(sb, fid.vnode,
 			     afs_iget5_pseudo_test, afs_iget5_pseudo_set, &fid);
-	if (!inode)
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(inode))
+		return inode;
 
 	vnode = AFS_FS_I(inode);
 
diff --git a/fs/afs/inode.c b/fs/afs/inode.c
index af95640da03f..d2d4e4ffd706 100644
--- a/fs/afs/inode.c
+++ b/fs/afs/inode.c
@@ -490,9 +490,9 @@ struct inode *afs_iget(struct afs_operation *op, struct afs_vnode_param *vp)
 	_enter(",{%llx:%llu.%u},,", vp->fid.vid, vp->fid.vnode, vp->fid.unique);
 
 	inode = iget5_locked(sb, vp->fid.vnode, afs_iget5_test, afs_iget5_set, vp);
-	if (!inode) {
-		_leave(" = -ENOMEM");
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(inode)) {
+		_leave(" = %ld", PTR_ERR(inode));
+		return inode;
 	}
 
 	vnode = AFS_FS_I(inode);
@@ -554,9 +554,9 @@ struct inode *afs_root_iget(struct super_block *sb, struct key *key)
 	_enter(",{%llx},,", as->volume->vid);
 
 	inode = iget5_locked(sb, 1, NULL, afs_iget5_set_root, NULL);
-	if (!inode) {
-		_leave(" = -ENOMEM");
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(inode)) {
+		_leave(" = %ld", PTR_ERR(inode));
+		return inode;
 	}
 
 	_debug("GOT ROOT INODE %p { vl=%llx }", inode, as->volume->vid);
diff --git a/fs/befs/linuxvfs.c b/fs/befs/linuxvfs.c
index f715dd71a38c..3233e5e1c284 100644
--- a/fs/befs/linuxvfs.c
+++ b/fs/befs/linuxvfs.c
@@ -307,8 +307,8 @@ static struct inode *befs_iget(struct super_block *sb, unsigned long ino)
 	befs_debug(sb, "---> %s inode = %lu", __func__, ino);
 
 	inode = iget_locked(sb, ino);
-	if (!inode)
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(inode))
+		return inode;
 	if (!(inode_state_read_once(inode) & I_NEW))
 		return inode;
 
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 415c5145fcbe..0cb4d831963c 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -5892,8 +5892,8 @@ static struct btrfs_inode *btrfs_iget_locked(u64 ino, struct btrfs_root *root)
 	inode = iget5_locked_rcu(root->fs_info->sb, hashval, btrfs_find_actor,
 			     btrfs_init_locked_inode,
 			     (void *)&args);
-	if (!inode)
-		return NULL;
+	if (IS_ERR(inode))
+		return ERR_CAST(inode);
 	return BTRFS_I(inode);
 }
 
@@ -5908,8 +5908,8 @@ struct btrfs_inode *btrfs_iget_path(u64 ino, struct btrfs_root *root,
 	int ret;
 
 	inode = btrfs_iget_locked(ino, root);
-	if (!inode)
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(inode))
+		return inode;
 
 	if (!(inode_state_read_once(&inode->vfs_inode) & I_NEW))
 		return inode;
@@ -5932,8 +5932,8 @@ struct btrfs_inode *btrfs_iget(u64 ino, struct btrfs_root *root)
 	int ret;
 
 	inode = btrfs_iget_locked(ino, root);
-	if (!inode)
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(inode))
+		return inode;
 
 	if (!(inode_state_read_once(&inode->vfs_inode) & I_NEW))
 		return inode;
diff --git a/fs/ceph/inode.c b/fs/ceph/inode.c
index 9bd117ce4c7c..14c07cb0df44 100644
--- a/fs/ceph/inode.c
+++ b/fs/ceph/inode.c
@@ -203,9 +203,9 @@ struct inode *ceph_get_inode(struct super_block *sb, struct ceph_vino vino,
 				     ceph_ino_compare, ceph_set_ino_cb, &vino);
 	}
 
-	if (!inode) {
+	if (IS_ERR(inode)) {
 		doutc(cl, "no inode found for %llx.%llx\n", vino.ino, vino.snap);
-		return ERR_PTR(-ENOMEM);
+		return inode;
 	}
 
 	doutc(cl, "on %llx=%llx.%llx got %p new %d\n",
diff --git a/fs/coda/cnode.c b/fs/coda/cnode.c
index 70bb0579b40c..d092c783d8dd 100644
--- a/fs/coda/cnode.c
+++ b/fs/coda/cnode.c
@@ -67,8 +67,8 @@ struct inode * coda_iget(struct super_block * sb, struct CodaFid * fid,
 
 retry:
 	inode = iget5_locked(sb, hash, coda_test_inode, coda_set_inode, fid);
-	if (!inode)
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(inode))
+		return inode;
 
 	if (inode_state_read_once(inode) & I_NEW) {
 		cii = ITOC(inode);
diff --git a/fs/cramfs/inode.c b/fs/cramfs/inode.c
index b55eb3bd29c1..b706b891023a 100644
--- a/fs/cramfs/inode.c
+++ b/fs/cramfs/inode.c
@@ -94,8 +94,8 @@ static struct inode *get_cramfs_inode(struct super_block *sb,
 	static struct timespec64 zerotime;
 
 	inode = iget_locked(sb, cramino(cramfs_inode, offset));
-	if (!inode)
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(inode))
+		return inode;
 	if (!(inode_state_read_once(inode) & I_NEW))
 		return inode;
 
diff --git a/fs/ecryptfs/inode.c b/fs/ecryptfs/inode.c
index 35afb94143b8..18e38838eaf7 100644
--- a/fs/ecryptfs/inode.c
+++ b/fs/ecryptfs/inode.c
@@ -99,9 +99,9 @@ static struct inode *__ecryptfs_get_inode(struct inode *lower_inode,
 	inode = iget5_locked(sb, (unsigned long)lower_inode,
 			     ecryptfs_inode_test, ecryptfs_inode_set,
 			     lower_inode);
-	if (!inode) {
+	if (IS_ERR(inode)) {
 		iput(lower_inode);
-		return ERR_PTR(-EACCES);
+		return inode;
 	}
 	if (!(inode_state_read_once(inode) & I_NEW))
 		iput(lower_inode);
diff --git a/fs/erofs/inode.c b/fs/erofs/inode.c
index 26ea3790ff21..a81b314651c1 100644
--- a/fs/erofs/inode.c
+++ b/fs/erofs/inode.c
@@ -296,8 +296,8 @@ struct inode *erofs_iget(struct super_block *sb, erofs_nid_t nid)
 
 	inode = iget5_locked(sb, erofs_squash_ino(sb, nid), erofs_iget5_eq,
 			     erofs_iget5_set, &nid);
-	if (!inode)
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(inode))
+		return inode;
 
 	if (inode_state_read_once(inode) & I_NEW) {
 		int err = erofs_fill_inode(inode);
diff --git a/fs/erofs/ishare.c b/fs/erofs/ishare.c
index ea146fa8a27c..373dcc682ac8 100644
--- a/fs/erofs/ishare.c
+++ b/fs/erofs/ishare.c
@@ -58,7 +58,7 @@ bool erofs_ishare_fill_inode(struct inode *inode)
 	si = iget5_locked(erofs_ishare_mnt->mnt_sb,
 			  xxh32(fp.opaque, fp.size, 0),
 			  erofs_ishare_iget5_eq, erofs_ishare_iget5_set, &fp);
-	if (si && (inode_state_read_once(si) & I_NEW)) {
+	if (!IS_ERR(si) && (inode_state_read_once(si) & I_NEW)) {
 		si->i_fop = &backing_fops;
 		si->i_mapping->a_ops = aops;
 		si->i_mode = 0444 | S_IFREG;
@@ -68,7 +68,9 @@ bool erofs_ishare_fill_inode(struct inode *inode)
 		unlock_new_inode(si);
 	} else {
 		kfree(fp.opaque);
-		if (!si || aops != si->i_mapping->a_ops ||
+		if (IS_ERR(si))
+			return false;
+		if (aops != si->i_mapping->a_ops ||
 		    si->i_blkbits != inode->i_blkbits) {
 			iput(si);
 			return false;
diff --git a/fs/ext2/inode.c b/fs/ext2/inode.c
index 24efe68b6cb8..8fac6513fae0 100644
--- a/fs/ext2/inode.c
+++ b/fs/ext2/inode.c
@@ -1359,8 +1359,8 @@ struct inode *ext2_iget (struct super_block *sb, unsigned long ino)
 	gid_t i_gid;
 
 	inode = iget_locked(sb, ino);
-	if (!inode)
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(inode))
+		return inode;
 	if (!(inode_state_read_once(inode) & I_NEW))
 		return inode;
 
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index e3129353d4ed..aa2298c7fb17 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -6226,8 +6226,8 @@ struct inode *__ext4_iget(struct super_block *sb, unsigned long ino,
 		}
 	} else {
 		inode = iget_locked(sb, ino);
-		if (!inode)
-			return ERR_PTR(-ENOMEM);
+		if (IS_ERR(inode))
+			return inode;
 	}
 
 	if (!(inode_state_read_once(inode) & I_NEW)) {
diff --git a/fs/f2fs/inode.c b/fs/f2fs/inode.c
index 9164a2b5d9f0..9b6aaf1ec7aa 100644
--- a/fs/f2fs/inode.c
+++ b/fs/f2fs/inode.c
@@ -581,8 +581,8 @@ struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)
 	int ret = 0;
 
 	inode = iget_locked(sb, ino);
-	if (!inode)
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(inode))
+		return inode;
 
 	if (!(inode_state_read_once(inode) & I_NEW)) {
 		trace_f2fs_iget(inode);
diff --git a/fs/fuse/inode.c b/fs/fuse/inode.c
index 23a4bbf5dfa0..99d3985cdbbe 100644
--- a/fs/fuse/inode.c
+++ b/fs/fuse/inode.c
@@ -516,8 +516,8 @@ struct inode *fuse_iget(struct super_block *sb, u64 nodeid,
 
 retry:
 	inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid);
-	if (!inode)
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(inode))
+		return inode;
 
 	is_new_inode = inode_state_read_once(inode) & I_NEW;
 	if (is_new_inode) {
diff --git a/fs/gfs2/inode.c b/fs/gfs2/inode.c
index 75fb1a0c9142..fefc6b914e7e 100644
--- a/fs/gfs2/inode.c
+++ b/fs/gfs2/inode.c
@@ -136,8 +136,8 @@ struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned int type,
 
 	gfs2_holder_mark_uninitialized(&i_gh);
 	inode = iget5_locked(sb, no_addr, iget_test, iget_set, &no_addr);
-	if (!inode)
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(inode))
+		return inode;
 
 	ip = GFS2_I(inode);
 
diff --git a/fs/hfs/btree.c b/fs/hfs/btree.c
index 4f0ddc76e8c5..012f29f2e92a 100644
--- a/fs/hfs/btree.c
+++ b/fs/hfs/btree.c
@@ -156,7 +156,7 @@ struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id, btree_keycmp ke
 	tree->keycmp = keycmp;
 
 	tree->inode = iget_locked(sb, id);
-	if (!tree->inode)
+	if (IS_ERR(tree->inode))
 		goto free_tree;
 	BUG_ON(!(inode_state_read_once(tree->inode) & I_NEW));
 	{
diff --git a/fs/hfs/inode.c b/fs/hfs/inode.c
index b836b2f8d544..152d5f566adf 100644
--- a/fs/hfs/inode.c
+++ b/fs/hfs/inode.c
@@ -454,8 +454,8 @@ struct inode *hfs_iget(struct super_block *sb, struct hfs_cat_key *key, hfs_cat_
 		return ERR_PTR(-EIO);
 	}
 	inode = iget5_locked(sb, cnid, hfs_test_inode, hfs_read_inode, &data);
-	if (!inode)
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(inode))
+		return inode;
 	if (inode_state_read_once(inode) & I_NEW)
 		unlock_new_inode(inode);
 	return inode;
diff --git a/fs/hfsplus/super.c b/fs/hfsplus/super.c
index 532f23502df3..ba13a3e6a4ce 100644
--- a/fs/hfsplus/super.c
+++ b/fs/hfsplus/super.c
@@ -69,8 +69,8 @@ struct inode *hfsplus_iget(struct super_block *sb, unsigned long ino)
 	int err;
 
 	inode = iget_locked(sb, ino);
-	if (!inode)
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(inode))
+		return inode;
 	if (!(inode_state_read_once(inode) & I_NEW))
 		return inode;
 
diff --git a/fs/hostfs/hostfs_kern.c b/fs/hostfs/hostfs_kern.c
index 613146e76dec..8e9eef568916 100644
--- a/fs/hostfs/hostfs_kern.c
+++ b/fs/hostfs/hostfs_kern.c
@@ -578,8 +578,8 @@ static struct inode *hostfs_iget(struct super_block *sb, char *name)
 
 	inode = iget5_locked(sb, st.ino, hostfs_inode_test, hostfs_inode_set,
 			     &st);
-	if (!inode)
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(inode))
+		return inode;
 
 	if (inode_state_read_once(inode) & I_NEW) {
 		unlock_new_inode(inode);
diff --git a/fs/hpfs/dir.c b/fs/hpfs/dir.c
index a82c06635b89..add849cdc429 100644
--- a/fs/hpfs/dir.c
+++ b/fs/hpfs/dir.c
@@ -242,9 +242,8 @@ struct dentry *hpfs_lookup(struct inode *dir, struct dentry *dentry, unsigned in
 	 */
 
 	result = iget_locked(dir->i_sb, ino);
-	if (!result) {
+	if (IS_ERR(result)) {
 		hpfs_error(dir->i_sb, "hpfs_lookup: can't get inode");
-		result = ERR_PTR(-ENOMEM);
 		goto bail1;
 	}
 	if (inode_state_read_once(result) & I_NEW) {
diff --git a/fs/hpfs/inode.c b/fs/hpfs/inode.c
index 396773d0b669..9dac9cae5eb8 100644
--- a/fs/hpfs/inode.c
+++ b/fs/hpfs/inode.c
@@ -194,7 +194,7 @@ void hpfs_write_inode(struct inode *i)
 		return;
 	}
 	parent = iget_locked(i->i_sb, hpfs_inode->i_parent_dir);
-	if (parent) {
+	if (!IS_ERR(parent)) {
 		hpfs_inode->i_dirty = 0;
 		if (inode_state_read_once(parent) & I_NEW) {
 			hpfs_init_inode(parent);
diff --git a/fs/hpfs/super.c b/fs/hpfs/super.c
index a9e26ae2e375..97a851e046d2 100644
--- a/fs/hpfs/super.c
+++ b/fs/hpfs/super.c
@@ -653,7 +653,7 @@ static int hpfs_fill_super(struct super_block *s, struct fs_context *fc)
 	brelse(bh0);
 
 	root = iget_locked(s, sbi->sb_root);
-	if (!root)
+	if (IS_ERR(root))
 		goto bail0;
 	hpfs_init_inode(root);
 	hpfs_read_inode(root);
diff --git a/fs/inode.c b/fs/inode.c
index bf9403c32188..b3b903cdeb56 100644
--- a/fs/inode.c
+++ b/fs/inode.c
@@ -1313,7 +1313,10 @@ EXPORT_SYMBOL(unlock_two_nondirectories);
  * to fill it in before unlocking it via unlock_new_inode().
  *
  * Note that both @test and @set are called with the inode_hash_lock held, so
- * they can't sleep.
+ * they can't sleep. @set returns 0 or a negative errno.
+ *
+ * Return: the inode, or ERR_PTR(-ESTALE) if a matching inode is being
+ * created, or the ERR_PTR() of the error returned by @set.
  */
 struct inode *inode_insert5(struct inode *inode, u64 hashval,
 			    int (*test)(struct inode *, void *),
@@ -1322,6 +1325,7 @@ struct inode *inode_insert5(struct inode *inode, u64 hashval,
 	struct hlist_head *head = inode_hashtable + hash(inode->i_sb, hashval);
 	struct inode *old;
 	bool isnew;
+	int err;
 
 	might_sleep();
 
@@ -1335,7 +1339,7 @@ struct inode *inode_insert5(struct inode *inode, u64 hashval,
 		 */
 		spin_unlock(&inode_hash_lock);
 		if (IS_ERR(old))
-			return NULL;
+			return old;
 		if (unlikely(isnew))
 			wait_on_new_inode(old);
 		if (unlikely(inode_unhashed(old))) {
@@ -1345,9 +1349,12 @@ struct inode *inode_insert5(struct inode *inode, u64 hashval,
 		return old;
 	}
 
-	if (set && unlikely(set(inode, data))) {
-		spin_unlock(&inode_hash_lock);
-		return NULL;
+	if (set) {
+		err = set(inode, data);
+		if (unlikely(err)) {
+			spin_unlock(&inode_hash_lock);
+			return ERR_PTR(err);
+		}
 	}
 
 	/*
@@ -1390,7 +1397,10 @@ EXPORT_SYMBOL(inode_insert5);
  * gets to fill it in before unlocking it via unlock_new_inode().
  *
  * Note that both @test and @set are called with the inode_hash_lock held, so
- * they can't sleep.
+ * they can't sleep. @set returns 0 or a negative errno.
+ *
+ * Return: the inode, or ERR_PTR(-ENOMEM) if no inode could be allocated,
+ * or an error from inode_insert5().
  */
 struct inode *iget5_locked(struct super_block *sb, u64 hashval,
 		int (*test)(struct inode *, void *),
@@ -1401,11 +1411,11 @@ struct inode *iget5_locked(struct super_block *sb, u64 hashval,
 	if (!inode) {
 		struct inode *new = alloc_inode(sb);
 
-		if (new) {
-			inode = inode_insert5(new, hashval, test, set, data);
-			if (unlikely(inode != new))
-				destroy_inode(new);
-		}
+		if (!new)
+			return ERR_PTR(-ENOMEM);
+		inode = inode_insert5(new, hashval, test, set, data);
+		if (unlikely(inode != new))
+			destroy_inode(new);
 	}
 	return inode;
 }
@@ -1421,6 +1431,10 @@ EXPORT_SYMBOL(iget5_locked);
  *
  * This is equivalent to iget5_locked, except the @test callback must
  * tolerate the inode not being stable, including being mid-teardown.
+ *
+ * Return: the inode, or ERR_PTR(-ENOMEM) if no inode could be allocated,
+ * or ERR_PTR(-ESTALE) if a matching inode is being created, or an error
+ * from inode_insert5().
  */
 struct inode *iget5_locked_rcu(struct super_block *sb, u64 hashval,
 		int (*test)(struct inode *, void *),
@@ -1436,7 +1450,7 @@ struct inode *iget5_locked_rcu(struct super_block *sb, u64 hashval,
 	inode = find_inode(sb, head, test, data, false, &isnew);
 	if (inode) {
 		if (IS_ERR(inode))
-			return NULL;
+			return inode;
 		if (unlikely(isnew))
 			wait_on_new_inode(inode);
 		if (unlikely(inode_unhashed(inode))) {
@@ -1447,11 +1461,11 @@ struct inode *iget5_locked_rcu(struct super_block *sb, u64 hashval,
 	}
 
 	new = alloc_inode(sb);
-	if (new) {
-		inode = inode_insert5(new, hashval, test, set, data);
-		if (unlikely(inode != new))
-			destroy_inode(new);
-	}
+	if (!new)
+		return ERR_PTR(-ENOMEM);
+	inode = inode_insert5(new, hashval, test, set, data);
+	if (unlikely(inode != new))
+		destroy_inode(new);
 	return inode;
 }
 EXPORT_SYMBOL_GPL(iget5_locked_rcu);
@@ -1468,11 +1482,14 @@ EXPORT_SYMBOL_GPL(iget5_locked_rcu);
  * If the inode is not in cache, allocate a new inode and return it locked,
  * hashed, and with the I_NEW flag set.  The file system gets to fill it in
  * before unlocking it via unlock_new_inode().
+ *
+ * Return: the inode, or ERR_PTR(-ENOMEM) if no inode could be allocated,
+ * or ERR_PTR(-ESTALE) if the inode is being created.
  */
 struct inode *iget_locked(struct super_block *sb, u64 ino)
 {
 	struct hlist_head *head = inode_hashtable + hash(sb, ino);
-	struct inode *inode;
+	struct inode *inode, *old;
 	bool isnew;
 
 	might_sleep();
@@ -1481,7 +1498,7 @@ struct inode *iget_locked(struct super_block *sb, u64 ino)
 	inode = find_inode_fast(sb, head, ino, false, &isnew);
 	if (inode) {
 		if (IS_ERR(inode))
-			return NULL;
+			return inode;
 		if (unlikely(isnew))
 			wait_on_new_inode(inode);
 		if (unlikely(inode_unhashed(inode))) {
@@ -1492,45 +1509,43 @@ struct inode *iget_locked(struct super_block *sb, u64 ino)
 	}
 
 	inode = alloc_inode(sb);
-	if (inode) {
-		struct inode *old;
-
-		spin_lock(&inode_hash_lock);
-		/* We released the lock, so.. */
-		old = find_inode_fast(sb, head, ino, true, &isnew);
-		if (!old) {
-			inode->i_ino = ino;
-			spin_lock(&inode->i_lock);
-			inode_state_assign(inode, I_NEW);
-			hlist_add_head_rcu(&inode->i_hash, head);
-			spin_unlock(&inode->i_lock);
-			spin_unlock(&inode_hash_lock);
-			inode_sb_list_add(inode);
+	if (!inode)
+		return ERR_PTR(-ENOMEM);
 
-			/* Return the locked inode with I_NEW set, the
-			 * caller is responsible for filling in the contents
-			 */
-			return inode;
-		}
+	spin_lock(&inode_hash_lock);
+	/* We released the lock, so.. */
+	old = find_inode_fast(sb, head, ino, true, &isnew);
+	if (!old) {
+		inode->i_ino = ino;
+		spin_lock(&inode->i_lock);
+		inode_state_assign(inode, I_NEW);
+		hlist_add_head_rcu(&inode->i_hash, head);
+		spin_unlock(&inode->i_lock);
+		spin_unlock(&inode_hash_lock);
+		inode_sb_list_add(inode);
 
-		/*
-		 * Uhhuh, somebody else created the same inode under
-		 * us. Use the old inode instead of the one we just
-		 * allocated.
+		/* Return the locked inode with I_NEW set, the
+		 * caller is responsible for filling in the contents
 		 */
-		spin_unlock(&inode_hash_lock);
-		destroy_inode(inode);
-		if (IS_ERR(old))
-			return NULL;
-		inode = old;
-		if (unlikely(isnew))
-			wait_on_new_inode(inode);
-		if (unlikely(inode_unhashed(inode))) {
-			iput(inode);
-			goto again;
-		}
+		return inode;
 	}
-	return inode;
+
+	/*
+	 * Uhhuh, somebody else created the same inode under
+	 * us. Use the old inode instead of the one we just
+	 * allocated.
+	 */
+	spin_unlock(&inode_hash_lock);
+	destroy_inode(inode);
+	if (IS_ERR(old))
+		return old;
+	if (unlikely(isnew))
+		wait_on_new_inode(old);
+	if (unlikely(inode_unhashed(old))) {
+		iput(old);
+		goto again;
+	}
+	return old;
 }
 EXPORT_SYMBOL(iget_locked);
 
@@ -1965,7 +1980,8 @@ int insert_inode_locked4(struct inode *inode, u64 hashval,
 	old = inode_insert5(inode, hashval, test, NULL, data);
 
 	if (old != inode) {
-		iput(old);
+		if (!IS_ERR(old))
+			iput(old);
 		return -EBUSY;
 	}
 	return 0;
diff --git a/fs/isofs/inode.c b/fs/isofs/inode.c
index 4824e42995cc..47212eed8269 100644
--- a/fs/isofs/inode.c
+++ b/fs/isofs/inode.c
@@ -1504,8 +1504,8 @@ struct inode *__isofs_iget(struct super_block *sb,
 	inode = iget5_locked(sb, hashval, &isofs_iget5_test,
 				&isofs_iget5_set, &data);
 
-	if (!inode)
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(inode))
+		return inode;
 
 	if (inode_state_read_once(inode) & I_NEW) {
 		ret = isofs_read_inode(inode, relocated);
diff --git a/fs/jffs2/fs.c b/fs/jffs2/fs.c
index 05cf860307c7..20f6b86a3cc6 100644
--- a/fs/jffs2/fs.c
+++ b/fs/jffs2/fs.c
@@ -263,8 +263,8 @@ struct inode *jffs2_iget(struct super_block *sb, unsigned long ino)
 	jffs2_dbg(1, "%s(): ino == %lu\n", __func__, ino);
 
 	inode = iget_locked(sb, ino);
-	if (!inode)
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(inode))
+		return inode;
 	if (!(inode_state_read_once(inode) & I_NEW))
 		return inode;
 
diff --git a/fs/jfs/inode.c b/fs/jfs/inode.c
index 2f9d6d311de9..6ba8a8ce194e 100644
--- a/fs/jfs/inode.c
+++ b/fs/jfs/inode.c
@@ -27,8 +27,8 @@ struct inode *jfs_iget(struct super_block *sb, unsigned long ino)
 	int ret;
 
 	inode = iget_locked(sb, ino);
-	if (!inode)
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(inode))
+		return inode;
 	if (!(inode_state_read_once(inode) & I_NEW))
 		return inode;
 
diff --git a/fs/kernfs/inode.c b/fs/kernfs/inode.c
index 580095e0fad9..30d1bf473930 100644
--- a/fs/kernfs/inode.c
+++ b/fs/kernfs/inode.c
@@ -331,14 +331,14 @@ static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode)
  *	sets up their inodes itself, with kernfs_rwsem held.
  *
  *	Return:
- *	Pointer to allocated inode on success, %NULL on failure.
+ *	Pointer to allocated inode on success, ERR_PTR() on failure.
  */
 struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn)
 {
 	struct inode *inode;
 
 	inode = iget_locked(sb, kernfs_ino(kn));
-	if (inode && (inode_state_read_once(inode) & I_NEW))
+	if (!IS_ERR(inode) && (inode_state_read_once(inode) & I_NEW))
 		kernfs_init_inode(kn, inode);
 
 	return inode;
@@ -362,8 +362,8 @@ static int kernfs_inode_test(struct inode *inode, void *data)
  * allocation can block in reclaim and hold up other users of the lock.
  *
  * Returns the inode now attached to @kn, which is @inode unless one was
- * already hashed for it (e.g. by a file handle decode), or NULL if @kn
- * is no longer active.
+ * already hashed for it (e.g. by a file handle decode), NULL if @kn
+ * is no longer active, or the ERR_PTR() returned by inode_insert5().
  */
 struct inode *kernfs_get_inode_locked(struct inode *inode,
 				      struct kernfs_node *kn)
diff --git a/fs/kernfs/mount.c b/fs/kernfs/mount.c
index 9691ad4218e1..f2e485606d31 100644
--- a/fs/kernfs/mount.c
+++ b/fs/kernfs/mount.c
@@ -327,9 +327,9 @@ static int kernfs_fill_super(struct super_block *sb, struct kernfs_fs_context *k
 	down_read(&kf_root->kernfs_rwsem);
 	inode = kernfs_get_inode_locked(new, info->root->kn);
 	up_read(&kf_root->kernfs_rwsem);
-	if (!inode) {
+	if (IS_ERR_OR_NULL(inode)) {
 		pr_debug("kernfs: could not get root inode\n");
-		return -ENOMEM;
+		return inode ? PTR_ERR(inode) : -ENOMEM;
 	}
 
 	/* instantiate and link root dentry */
diff --git a/fs/minix/inode.c b/fs/minix/inode.c
index 9fb938ec5868..ce4707df57f7 100644
--- a/fs/minix/inode.c
+++ b/fs/minix/inode.c
@@ -617,8 +617,8 @@ struct inode *minix_iget(struct super_block *sb, unsigned long ino)
 	struct inode *inode;
 
 	inode = iget_locked(sb, ino);
-	if (!inode)
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(inode))
+		return inode;
 	if (!(inode_state_read_once(inode) & I_NEW))
 		return inode;
 
diff --git a/fs/nfs/inode.c b/fs/nfs/inode.c
index 5a682f74d7d1..de6c70c47c64 100644
--- a/fs/nfs/inode.c
+++ b/fs/nfs/inode.c
@@ -455,10 +455,8 @@ nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr)
 	hash = fattr->fileid;
 
 	inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc);
-	if (inode == NULL) {
-		inode = ERR_PTR(-ENOMEM);
+	if (IS_ERR(inode))
 		goto out_no_inode;
-	}
 
 	if (inode_state_read_once(inode) & I_NEW) {
 		struct nfs_inode *nfsi = NFS_I(inode);
diff --git a/fs/nilfs2/cpfile.c b/fs/nilfs2/cpfile.c
index d3349fa58abe..a2484fbfdb68 100644
--- a/fs/nilfs2/cpfile.c
+++ b/fs/nilfs2/cpfile.c
@@ -1158,8 +1158,8 @@ int nilfs_cpfile_read(struct super_block *sb, size_t cpsize,
 	}
 
 	cpfile = nilfs_iget_locked(sb, NULL, NILFS_CPFILE_INO);
-	if (unlikely(!cpfile))
-		return -ENOMEM;
+	if (IS_ERR(cpfile))
+		return PTR_ERR(cpfile);
 	if (!(inode_state_read_once(cpfile) & I_NEW))
 		goto out;
 
diff --git a/fs/nilfs2/dat.c b/fs/nilfs2/dat.c
index 888dc1831c86..366a8a608266 100644
--- a/fs/nilfs2/dat.c
+++ b/fs/nilfs2/dat.c
@@ -504,8 +504,8 @@ int nilfs_dat_read(struct super_block *sb, size_t entry_size,
 	}
 
 	dat = nilfs_iget_locked(sb, NULL, NILFS_DAT_INO);
-	if (unlikely(!dat))
-		return -ENOMEM;
+	if (IS_ERR(dat))
+		return PTR_ERR(dat);
 	if (!(inode_state_read_once(dat) & I_NEW))
 		goto out;
 
diff --git a/fs/nilfs2/ifile.c b/fs/nilfs2/ifile.c
index c99e1e6356ae..b25b74e2ee40 100644
--- a/fs/nilfs2/ifile.c
+++ b/fs/nilfs2/ifile.c
@@ -185,8 +185,8 @@ int nilfs_ifile_read(struct super_block *sb, struct nilfs_root *root,
 	int err;
 
 	ifile = nilfs_iget_locked(sb, root, NILFS_IFILE_INO);
-	if (unlikely(!ifile))
-		return -ENOMEM;
+	if (IS_ERR(ifile))
+		return PTR_ERR(ifile);
 	if (!(inode_state_read_once(ifile) & I_NEW))
 		goto out;
 
diff --git a/fs/nilfs2/inode.c b/fs/nilfs2/inode.c
index 1e336af4a471..9522f4e1404c 100644
--- a/fs/nilfs2/inode.c
+++ b/fs/nilfs2/inode.c
@@ -538,8 +538,8 @@ struct inode *nilfs_iget(struct super_block *sb, struct nilfs_root *root,
 	int err;
 
 	inode = nilfs_iget_locked(sb, root, ino);
-	if (unlikely(!inode))
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(inode))
+		return inode;
 
 	if (!(inode_state_read_once(inode) & I_NEW)) {
 		if (!inode->i_nlink) {
@@ -567,8 +567,8 @@ struct inode *nilfs_iget_for_gc(struct super_block *sb, u64 ino, __u64 cno)
 	int err;
 
 	inode = iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args);
-	if (unlikely(!inode))
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(inode))
+		return inode;
 	if (!(inode_state_read_once(inode) & I_NEW))
 		return inode;
 
@@ -607,8 +607,8 @@ int nilfs_attach_btree_node_cache(struct inode *inode)
 
 	btnc_inode = iget5_locked(inode->i_sb, inode->i_ino, nilfs_iget_test,
 				  nilfs_iget_set, &args);
-	if (unlikely(!btnc_inode))
-		return -ENOMEM;
+	if (IS_ERR(btnc_inode))
+		return PTR_ERR(btnc_inode);
 	if (inode_state_read_once(btnc_inode) & I_NEW) {
 		nilfs_init_btnc_inode(btnc_inode);
 		unlock_new_inode(btnc_inode);
@@ -662,8 +662,8 @@ struct inode *nilfs_iget_for_shadow(struct inode *inode)
 
 	s_inode = iget5_locked(inode->i_sb, inode->i_ino, nilfs_iget_test,
 			       nilfs_iget_set, &args);
-	if (unlikely(!s_inode))
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(s_inode))
+		return s_inode;
 	if (!(inode_state_read_once(s_inode) & I_NEW))
 		return inode;
 
diff --git a/fs/nilfs2/sufile.c b/fs/nilfs2/sufile.c
index fec5af70c6dd..6bf6b20522f2 100644
--- a/fs/nilfs2/sufile.c
+++ b/fs/nilfs2/sufile.c
@@ -1232,8 +1232,8 @@ int nilfs_sufile_read(struct super_block *sb, size_t susize,
 	}
 
 	sufile = nilfs_iget_locked(sb, NULL, NILFS_SUFILE_INO);
-	if (unlikely(!sufile))
-		return -ENOMEM;
+	if (IS_ERR(sufile))
+		return PTR_ERR(sufile);
 	if (!(inode_state_read_once(sufile) & I_NEW))
 		goto out;
 
diff --git a/fs/ntfs/inode.c b/fs/ntfs/inode.c
index 48ec7a0c98b9..ae145ae8456c 100644
--- a/fs/ntfs/inode.c
+++ b/fs/ntfs/inode.c
@@ -200,8 +200,8 @@ struct inode *ntfs_iget(struct super_block *sb, u64 mft_no)
 
 	vi = iget5_locked(sb, mft_no, ntfs_test_inode,
 			ntfs_init_locked_inode, &na);
-	if (unlikely(!vi))
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(vi))
+		return vi;
 
 	err = 0;
 
@@ -264,8 +264,8 @@ struct inode *ntfs_attr_iget(struct inode *base_vi, __le32 type,
 
 	vi = iget5_locked(base_vi->i_sb, na.mft_no, ntfs_test_inode,
 			ntfs_init_locked_inode, &na);
-	if (unlikely(!vi))
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(vi))
+		return vi;
 	err = 0;
 
 	/* If this is a freshly allocated inode, need to read it now. */
@@ -320,8 +320,8 @@ struct inode *ntfs_index_iget(struct inode *base_vi, __le16 *name,
 
 	vi = iget5_locked(base_vi->i_sb, na.mft_no, ntfs_test_inode,
 			ntfs_init_locked_inode, &na);
-	if (unlikely(!vi))
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(vi))
+		return vi;
 
 	err = 0;
 
diff --git a/fs/ntfs3/inode.c b/fs/ntfs3/inode.c
index 45f894d182dd..832744c27ba4 100644
--- a/fs/ntfs3/inode.c
+++ b/fs/ntfs3/inode.c
@@ -708,8 +708,8 @@ struct inode *ntfs_iget5_flags(struct super_block *sb,
 	}
 
 	inode = iget5_locked(sb, ino, ntfs_test_inode, ntfs_set_inode, &ig5);
-	if (unlikely(!inode)) {
-		err = -ENOMEM;
+	if (IS_ERR(inode)) {
+		err = PTR_ERR(inode);
 		goto out;
 	}
 
diff --git a/fs/ocfs2/inode.c b/fs/ocfs2/inode.c
index e225a96d3753..eb3dbb6eb7f1 100644
--- a/fs/ocfs2/inode.c
+++ b/fs/ocfs2/inode.c
@@ -181,8 +181,7 @@ struct inode *ocfs2_iget(struct ocfs2_super *osb, u64 blkno, unsigned flags,
 	/* inode was *not* in the inode cache. 2.6.x requires
 	 * us to do our own read_inode call and unlock it
 	 * afterwards. */
-	if (inode == NULL) {
-		inode = ERR_PTR(-ENOMEM);
+	if (IS_ERR(inode)) {
 		mlog_errno(PTR_ERR(inode));
 		goto bail;
 	}
diff --git a/fs/openpromfs/inode.c b/fs/openpromfs/inode.c
index 6cd02d456bac..308635a0e62d 100644
--- a/fs/openpromfs/inode.c
+++ b/fs/openpromfs/inode.c
@@ -350,10 +350,7 @@ static void openprom_free_inode(struct inode *inode)
 
 static struct inode *openprom_iget(struct super_block *sb, ino_t ino)
 {
-	struct inode *inode = iget_locked(sb, ino);
-	if (!inode)
-		inode = ERR_PTR(-ENOMEM);
-	return inode;
+	return iget_locked(sb, ino);
 }
 
 static int openpromfs_reconfigure(struct fs_context *fc)
diff --git a/fs/orangefs/inode.c b/fs/orangefs/inode.c
index b1fed1c81a4d..111a314d6258 100644
--- a/fs/orangefs/inode.c
+++ b/fs/orangefs/inode.c
@@ -1082,8 +1082,8 @@ struct inode *orangefs_iget(struct super_block *sb,
 			orangefs_set_inode,
 			ref);
 
-	if (!inode)
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(inode))
+		return inode;
 
 	if (!(inode_state_read_once(inode) & I_NEW))
 		return inode;
diff --git a/fs/overlayfs/inode.c b/fs/overlayfs/inode.c
index f1c6df39d0ae..0b87c5a50e55 100644
--- a/fs/overlayfs/inode.c
+++ b/fs/overlayfs/inode.c
@@ -1163,8 +1163,8 @@ struct inode *ovl_get_trap_inode(struct super_block *sb, struct dentry *dir)
 
 	trap = iget5_locked(sb, (unsigned long) key, ovl_inode_test,
 			    ovl_inode_set, key);
-	if (!trap)
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(trap))
+		return trap;
 
 	if (!(inode_state_read_once(trap) & I_NEW)) {
 		/* Conflicting layer roots? */
@@ -1239,7 +1239,7 @@ struct inode *ovl_get_inode(struct super_block *sb,
 	int fsid = bylower ? lowerpath->layer->fsid : 0;
 	bool is_dir;
 	unsigned long ino = 0;
-	int err = oip->newinode ? -EEXIST : -ENOMEM;
+	int err;
 
 	if (!realinode)
 		realinode = d_inode(lowerdentry);
@@ -1255,8 +1255,10 @@ struct inode *ovl_get_inode(struct super_block *sb,
 		unsigned int nlink = is_dir ? 1 : realinode->i_nlink;
 
 		inode = ovl_iget5(sb, oip->newinode, key);
-		if (!inode)
+		if (IS_ERR(inode)) {
+			err = oip->newinode ? -EEXIST : PTR_ERR(inode);
 			goto out_err;
+		}
 		if (!(inode_state_read_once(inode) & I_NEW)) {
 			/*
 			 * Verify that the underlying files stored in the inode
diff --git a/fs/qnx4/inode.c b/fs/qnx4/inode.c
index 42fcd500fad2..a527cfd3e0f5 100644
--- a/fs/qnx4/inode.c
+++ b/fs/qnx4/inode.c
@@ -289,8 +289,8 @@ struct inode *qnx4_iget(struct super_block *sb, unsigned long ino)
 	struct inode *inode;
 
 	inode = iget_locked(sb, ino);
-	if (!inode)
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(inode))
+		return inode;
 	if (!(inode_state_read_once(inode) & I_NEW))
 		return inode;
 
diff --git a/fs/qnx6/inode.c b/fs/qnx6/inode.c
index 7df3eed5142d..ee350b158d92 100644
--- a/fs/qnx6/inode.c
+++ b/fs/qnx6/inode.c
@@ -519,8 +519,8 @@ struct inode *qnx6_iget(struct super_block *sb, unsigned ino)
 	u32 n, offs;
 
 	inode = iget_locked(sb, ino);
-	if (!inode)
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(inode))
+		return inode;
 	if (!(inode_state_read_once(inode) & I_NEW))
 		return inode;
 
diff --git a/fs/romfs/super.c b/fs/romfs/super.c
index 7f783d6173e8..45bf252d16bb 100644
--- a/fs/romfs/super.c
+++ b/fs/romfs/super.c
@@ -307,8 +307,8 @@ static struct inode *romfs_iget(struct super_block *sb, unsigned long pos)
 
 	/* get an inode for this image position */
 	i = iget_locked(sb, pos);
-	if (!i)
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(i))
+		return i;
 
 	if (!(inode_state_read_once(i) & I_NEW))
 		return i;
diff --git a/fs/smb/client/file.c b/fs/smb/client/file.c
index d272320436b3..ab35a214583a 100644
--- a/fs/smb/client/file.c
+++ b/fs/smb/client/file.c
@@ -979,8 +979,9 @@ int cifs_posix_open(const char *full_path, struct inode **pinode,
 	if (*pinode == NULL) {
 		cifs_fill_uniqueid(sb, &fattr);
 		*pinode = cifs_iget(sb, &fattr);
-		if (!*pinode) {
-			rc = -ENOMEM;
+		if (IS_ERR(*pinode)) {
+			rc = PTR_ERR(*pinode);
+			*pinode = NULL;
 			goto posix_open_ret;
 		}
 	} else {
diff --git a/fs/smb/client/inode.c b/fs/smb/client/inode.c
index 31f9a9ea5ec8..8a42160ef7ba 100644
--- a/fs/smb/client/inode.c
+++ b/fs/smb/client/inode.c
@@ -393,8 +393,10 @@ static int update_inode_info(struct super_block *sb,
 
 	if (!*inode) {
 		*inode = cifs_iget(sb, fattr);
-		if (!*inode)
-			rc = -ENOMEM;
+		if (IS_ERR(*inode)) {
+			rc = PTR_ERR(*inode);
+			*inode = NULL;
+		}
 		return rc;
 	}
 	/* We already have inode, update it.
@@ -1652,7 +1654,7 @@ cifs_iget(struct super_block *sb, struct cifs_fattr *fattr)
 	hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
 
 	inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr);
-	if (inode) {
+	if (!IS_ERR(inode)) {
 		/* was there a potentially problematic inode collision? */
 		if (fattr->cf_flags & CIFS_FATTR_INO_COLLISION) {
 			fattr->cf_flags &= ~CIFS_FATTR_INO_COLLISION;
@@ -1729,6 +1731,8 @@ struct inode *cifs_root_iget(struct super_block *sb)
 			cifs_autodisable_serverino(cifs_sb, "Cannot retrieve attributes for junction point", rc);
 		}
 		inode = cifs_iget(sb, &fattr);
+		if (IS_ERR(inode))
+			goto out;
 	}
 
 	if (!inode) {
@@ -2273,7 +2277,7 @@ cifs_posix_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode,
 	cifs_unix_basic_to_fattr(&fattr, info, cifs_sb);
 	cifs_fill_uniqueid(inode->i_sb, &fattr);
 	newinode = cifs_iget(inode->i_sb, &fattr);
-	if (!newinode)
+	if (IS_ERR(newinode))
 		goto posix_mkdir_get_info;
 
 	d_instantiate(dentry, newinode);
diff --git a/fs/smb/client/readdir.c b/fs/smb/client/readdir.c
index 85ed65241ece..cbcfe387dbd8 100644
--- a/fs/smb/client/readdir.c
+++ b/fs/smb/client/readdir.c
@@ -159,8 +159,6 @@ cifs_prime_dcache(struct dentry *parent, struct qstr *name,
 		goto retry;
 	} else {
 		inode = cifs_iget(sb, fattr);
-		if (!inode)
-			inode = ERR_PTR(-ENOMEM);
 		alias = d_splice_alias(inode, dentry);
 		d_lookup_done(dentry);
 		if (alias && !IS_ERR(alias))
diff --git a/fs/squashfs/inode.c b/fs/squashfs/inode.c
index 82b687414e65..b415975ac97b 100644
--- a/fs/squashfs/inode.c
+++ b/fs/squashfs/inode.c
@@ -84,8 +84,8 @@ struct inode *squashfs_iget(struct super_block *sb, long long ino,
 
 	TRACE("Entered squashfs_iget\n");
 
-	if (!inode)
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(inode))
+		return inode;
 	if (!(inode_state_read_once(inode) & I_NEW))
 		return inode;
 
diff --git a/fs/ubifs/super.c b/fs/ubifs/super.c
index a441bd2ddf83..174ce9ee501d 100644
--- a/fs/ubifs/super.c
+++ b/fs/ubifs/super.c
@@ -112,8 +112,8 @@ struct inode *ubifs_iget(struct super_block *sb, unsigned long inum)
 	dbg_gen("inode %lu", inum);
 
 	inode = iget_locked(sb, inum);
-	if (!inode)
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(inode))
+		return inode;
 	if (!(inode_state_read_once(inode) & I_NEW))
 		return inode;
 	ui = ubifs_inode(inode);
diff --git a/fs/udf/inode.c b/fs/udf/inode.c
index 29900065673c..f51a939ea2b6 100644
--- a/fs/udf/inode.c
+++ b/fs/udf/inode.c
@@ -2015,8 +2015,8 @@ struct inode *__udf_iget(struct super_block *sb, struct kernel_lb_addr *ino,
 	struct inode *inode = iget_locked(sb, block);
 	int err;
 
-	if (!inode)
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(inode))
+		return inode;
 
 	if (!(inode_state_read_once(inode) & I_NEW)) {
 		if (UDF_I(inode)->i_hidden != hidden_inode) {
diff --git a/fs/ufs/inode.c b/fs/ufs/inode.c
index fec701330d67..9d00e75c8fab 100644
--- a/fs/ufs/inode.c
+++ b/fs/ufs/inode.c
@@ -653,8 +653,8 @@ struct inode *ufs_iget(struct super_block *sb, unsigned long ino)
 	}
 
 	inode = iget_locked(sb, ino);
-	if (!inode)
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(inode))
+		return inode;
 	if (!(inode_state_read_once(inode) & I_NEW))
 		return inode;
 
diff --git a/fs/vboxsf/super.c b/fs/vboxsf/super.c
index 190b76f04d4f..edd5e8e2584b 100644
--- a/fs/vboxsf/super.c
+++ b/fs/vboxsf/super.c
@@ -200,8 +200,8 @@ static int vboxsf_fill_super(struct super_block *sb, struct fs_context *fc)
 	set_default_d_op(sb, &vboxsf_dentry_ops);
 
 	iroot = iget_locked(sb, 0);
-	if (!iroot) {
-		err = -ENOMEM;
+	if (IS_ERR(iroot)) {
+		err = PTR_ERR(iroot);
 		goto fail_unmap;
 	}
 	vboxsf_init_inode(sbi, iroot, &sbi->root_info, false);
diff --git a/fs/zonefs/super.c b/fs/zonefs/super.c
index d843e6148227..92b0a3c29cbe 100644
--- a/fs/zonefs/super.c
+++ b/fs/zonefs/super.c
@@ -657,8 +657,8 @@ static struct inode *zonefs_get_file_inode(struct inode *dir,
 	z = &zgroup->g_zones[fno];
 	ino = z->z_sector >> sbi->s_zone_sectors_shift;
 	inode = iget_locked(sb, ino);
-	if (!inode)
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(inode))
+		return inode;
 	if (!(inode_state_read_once(inode) & I_NEW)) {
 		WARN_ON_ONCE(inode->i_private != z);
 		return inode;
@@ -696,8 +696,8 @@ static struct inode *zonefs_get_zgroup_inode(struct super_block *sb,
 	ino_t ino = bdev_nr_zones(sb->s_bdev) + ztype + 1;
 
 	inode = iget_locked(sb, ino);
-	if (!inode)
-		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(inode))
+		return inode;
 	if (!(inode_state_read_once(inode) & I_NEW))
 		return inode;
 
diff --git a/include/linux/kernfs.h b/include/linux/kernfs.h
index 803294a67e47..582a19e2c995 100644
--- a/include/linux/kernfs.h
+++ b/include/linux/kernfs.h
@@ -529,7 +529,7 @@ static inline struct kernfs_root *kernfs_root_from_sb(struct super_block *sb)
 
 static inline struct inode *
 kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn)
-{ return NULL; }
+{ return ERR_PTR(-ENODEV); }
 
 static inline struct kernfs_root *
 kernfs_create_root(struct kernfs_syscall_ops *scops, unsigned int flags,
diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index 32479c0f77af..3774aebf2fa6 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -5434,8 +5434,8 @@ static int cgroup_may_write(const struct cgroup *cgrp, struct super_block *sb)
 	lockdep_assert_held(&cgroup_mutex);
 
 	inode = kernfs_get_inode(sb, cgrp->procs_file.kn);
-	if (!inode)
-		return -ENOMEM;
+	if (IS_ERR(inode))
+		return PTR_ERR(inode);
 
 	ret = inode_permission(&nop_mnt_idmap, inode, MAY_WRITE);
 	iput(inode);
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 0/3] fs: return ERR_PTR() from iget_locked() and friends
@ 2026-12-01 13:34 Konstantin Adzer
  2026-12-01 13:06 ` [PATCH 1/3] fuse: return ERR_PTR() from fuse_iget() Konstantin Adzer
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Konstantin Adzer @ 2026-12-01 13:34 UTC (permalink / raw)
  To: Alexander Viro, Christian Brauner
  Cc: Jonathan Corbet, Shuah Khan, Randy Dunlap, Dan Williams,
	Vishal Verma, Dave Jiang, Alison Schofield, Eric Van Hensbergen,
	Latchesar Ionkov, Dominique Martinet, Christian Schoenebeck,
	David Sterba, David Howells, Marc Dionne, Luis de Bethencourt,
	Salah Triki, Chris Mason, Ilya Dryomov, Alex Markuze, Xiubo Li,
	Viacheslav Dubeyko, Jan Harkes, coda, Nicolas Pitre, Tyler Hicks,
	Gao Xiang, Chao Yu, Yue Hu, Jingbo Xu, Sandeep Dhavale, Hongbo Li,
	Chunhai Guo, Jan Kara, Theodore Ts'o, Andreas Dilger,
	Baokun Li, Ojaswin Mujoo, Ritesh Harjani (IBM), Zhang Yi,
	Jaegeuk Kim, Miklos Szeredi, Andreas Gruenbacher,
	John Paul Adrian Glaubitz, Yangtao Li, Richard Weinberger,
	Anton Ivanov, Johannes Berg, Mikulas Patocka, David Woodhouse,
	Dave Kleikamp, Greg Kroah-Hartman, Tejun Heo, Trond Myklebust,
	Anna Schumaker, Ryusuke Konishi, Namjae Jeon, Hyunchul Lee,
	Konstantin Komarov, Mark Fasheh, Joel Becker, Joseph Qi,
	Heming Zhao, Mike Marshall, Martin Brandenburg, Amir Goldstein,
	Anders Larsen, Paulo Alcantara, Ronnie Sahlberg, Shyam Prasad N,
	Tom Talpey, Bharath SM, Phillip Lougher, Zhihao Cheng,
	Jori Koolstra, Damien Le Moal, Naohiro Aota, Johannes Thumshirn,
	Johannes Weiner, Michal Koutný, linux-doc, nvdimm, linux-cxl,
	v9fs, linux-fsdevel, linux-afs, linux-kernel, linux-btrfs,
	ceph-devel, codalist, ecryptfs, linux-erofs, linux-ext4,
	linux-f2fs-devel, fuse-devel, gfs2, linux-um, linux-mtd,
	jfs-discussion, driver-core, linux-nfs, linux-nilfs, ntfs, ntfs3,
	ocfs2-devel, devel, linux-unionfs, linux-cifs, samba-technical,
	cgroups

iget_locked(), iget5_locked(), iget5_locked_rcu() and inode_insert5()
return NULL for a failed allocation and also for an inode under
creation, which find_inode() and find_inode_fast() report as -ESTALE.
Callers read NULL as -ENOMEM. As reported in [1], open_by_handle_at() on
ext4 then fails with ENOMEM, and nfsd returns a jukebox/DELAY error, for
the handle of a deleted inode whose number is being reused. Following
the discussion in [2], this series makes the helpers return ERR_PTR().

Patches 1 and 2 make fuse_iget() and hfs_iget() return ERR_PTR(), so
that patch 3 can convert the four helpers and all their callers at once.

Testing:
- The reproducer below, in virtme-ng (x86_64 defconfig, 4 CPUs, ext4 on
  a loop device, 10 seconds): one process creates and unlinks a file in
  a loop, the other calls open_by_handle_at() with the handle of an
  earlier, deleted file of that name.
    vfs.all at 84fb6f8a1ba9: ESTALE 2312698 ENOMEM 637252
    with this series:        ESTALE 3394396 ENOMEM 0
  dmesg has no messages from the test in either run.
- allmodconfig x86_64 builds of every directory the series touches, with
  all three patches and with patches 1-2 only; ARCH=um for hostfs;
  sparc64 for openpromfs. No new warnings. checkpatch --strict reports
  no errors.
- Not done: run-time tests of filesystems other than ext4 and of the
  changed error paths in the callers.

The series was written with Claude Code (claude-opus-5-5). I asked it to
find out why open_by_handle_at() returns ENOMEM to our file scanner and
to fix the cause in the kernel. It traced the error to iget_locked(),
wrote the reproducer, the patches, their changelogs and this letter, and
ran the builds and the tests above.

[1] https://lore.kernel.org/179612584364.3457081.15142720814992093613@massivetech.ru
[2] https://lore.kernel.org/all/20261201-advent-fahrkarte-zierlich-b5b73df9e312@brauner/

Reproducer, run as root: ./iget-repro <empty directory on ext4> <seconds>

#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>

int main(int argc, char **argv)
{
	long estale = 0, enomem = 0, other = 0, opened = 0;
	struct file_handle *fh;
	int mount_id, mfd, fd;
	time_t end;
	pid_t pid;

	if (argc != 3) {
		fprintf(stderr, "usage: %s DIR SECONDS\n", argv[0]);
		return 2;
	}
	if (chdir(argv[1]))
		return perror("chdir"), 1;

	fh = malloc(sizeof(*fh) + MAX_HANDLE_SZ);
	fh->handle_bytes = MAX_HANDLE_SZ;
	fd = open("f", O_CREAT | O_EXCL | O_WRONLY, 0600);
	if (fd < 0 || name_to_handle_at(AT_FDCWD, "f", fh, &mount_id, 0))
		return perror("name_to_handle_at"), 1;
	close(fd);
	unlink("f");

	mfd = open(".", O_RDONLY | O_DIRECTORY);
	pid = fork();
	if (!pid) {
		for (;;) {
			fd = open("f", O_CREAT | O_EXCL | O_WRONLY, 0600);
			if (fd >= 0)
				close(fd);
			unlink("f");
		}
	}

	end = time(NULL) + atoi(argv[2]);
	while (time(NULL) < end) {
		fd = open_by_handle_at(mfd, fh, O_RDONLY);
		if (fd >= 0) {
			opened++;
			close(fd);
		} else if (errno == ESTALE) {
			estale++;
		} else if (errno == ENOMEM) {
			enomem++;
		} else {
			other++;
		}
	}
	kill(pid, SIGKILL);
	waitpid(pid, NULL, 0);
	printf("ESTALE %ld ENOMEM %ld other %ld opened %ld\n",
	       estale, enomem, other, opened);
	return enomem != 0;
}

Konstantin Adzer (3):
  fuse: return ERR_PTR() from fuse_iget()
  hfs: return ERR_PTR() from hfs_iget()
  fs: return ERR_PTR() from iget_locked() and friends

 Documentation/filesystems/porting.rst |   9 ++
 drivers/dax/super.c                   |   8 +-
 fs/9p/vfs_inode.c                     |   4 +-
 fs/9p/vfs_inode_dotl.c                |   4 +-
 fs/affs/inode.c                       |   4 +-
 fs/afs/dynroot.c                      |  14 +--
 fs/afs/inode.c                        |  12 +--
 fs/befs/linuxvfs.c                    |   4 +-
 fs/btrfs/inode.c                      |  12 +--
 fs/ceph/inode.c                       |   4 +-
 fs/coda/cnode.c                       |   4 +-
 fs/cramfs/inode.c                     |   4 +-
 fs/ecryptfs/inode.c                   |   4 +-
 fs/erofs/inode.c                      |   4 +-
 fs/erofs/ishare.c                     |   6 +-
 fs/ext2/inode.c                       |   4 +-
 fs/ext4/inode.c                       |   4 +-
 fs/f2fs/inode.c                       |   4 +-
 fs/fuse/dir.c                         |  13 +--
 fs/fuse/inode.c                       |  16 ++--
 fs/fuse/readdir.c                     |   4 +-
 fs/gfs2/inode.c                       |   4 +-
 fs/hfs/btree.c                        |   2 +-
 fs/hfs/dir.c                          |   2 -
 fs/hfs/inode.c                        |   8 +-
 fs/hfs/super.c                        |   4 +-
 fs/hfsplus/super.c                    |   4 +-
 fs/hostfs/hostfs_kern.c               |   4 +-
 fs/hpfs/dir.c                         |   3 +-
 fs/hpfs/inode.c                       |   2 +-
 fs/hpfs/super.c                       |   2 +-
 fs/inode.c                            | 126 +++++++++++++++-----------
 fs/isofs/inode.c                      |   4 +-
 fs/jffs2/fs.c                         |   4 +-
 fs/jfs/inode.c                        |   4 +-
 fs/kernfs/inode.c                     |   8 +-
 fs/kernfs/mount.c                     |   4 +-
 fs/minix/inode.c                      |   4 +-
 fs/nfs/inode.c                        |   4 +-
 fs/nilfs2/cpfile.c                    |   4 +-
 fs/nilfs2/dat.c                       |   4 +-
 fs/nilfs2/ifile.c                     |   4 +-
 fs/nilfs2/inode.c                     |  16 ++--
 fs/nilfs2/sufile.c                    |   4 +-
 fs/ntfs/inode.c                       |  12 +--
 fs/ntfs3/inode.c                      |   4 +-
 fs/ocfs2/inode.c                      |   3 +-
 fs/openpromfs/inode.c                 |   5 +-
 fs/orangefs/inode.c                   |   4 +-
 fs/overlayfs/inode.c                  |  10 +-
 fs/qnx4/inode.c                       |   4 +-
 fs/qnx6/inode.c                       |   4 +-
 fs/romfs/super.c                      |   4 +-
 fs/smb/client/file.c                  |   5 +-
 fs/smb/client/inode.c                 |  12 ++-
 fs/smb/client/readdir.c               |   2 -
 fs/squashfs/inode.c                   |   4 +-
 fs/ubifs/super.c                      |   4 +-
 fs/udf/inode.c                        |   4 +-
 fs/ufs/inode.c                        |   4 +-
 fs/vboxsf/super.c                     |   4 +-
 fs/zonefs/super.c                     |   8 +-
 include/linux/kernfs.h                |   2 +-
 kernel/cgroup/cgroup.c                |   4 +-
 64 files changed, 251 insertions(+), 219 deletions(-)


base-commit: 84fb6f8a1ba91eee3ffe6aa519686fce851a0e42
-- 
2.56.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-01 13:35 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 13:34 [PATCH 0/3] fs: return ERR_PTR() from iget_locked() and friends Konstantin Adzer
2026-12-01 13:06 ` [PATCH 1/3] fuse: return ERR_PTR() from fuse_iget() Konstantin Adzer
2026-12-01 13:06 ` [PATCH 2/3] hfs: return ERR_PTR() from hfs_iget() Konstantin Adzer
2026-12-01 13:09 ` [PATCH 3/3] fs: return ERR_PTR() from iget_locked() and friends Konstantin Adzer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox