4242
4343#define NFSDBG_FACILITY NFSDBG_CLIENT
4444
45+ /*
46+ * Set the superblock root dentry.
47+ * Note that this function frees the inode in case of error.
48+ */
49+ static int nfs_superblock_set_dummy_root (struct super_block * sb , struct inode * inode )
50+ {
51+ /* The mntroot acts as the dummy root dentry for this superblock */
52+ if (sb -> s_root == NULL ) {
53+ sb -> s_root = d_alloc_root (inode );
54+ if (sb -> s_root == NULL ) {
55+ iput (inode );
56+ return - ENOMEM ;
57+ }
58+ /* Circumvent igrab(): we know the inode is not being freed */
59+ atomic_inc (& inode -> i_count );
60+ }
61+ return 0 ;
62+ }
63+
4564/*
4665 * get an NFS2/NFS3 root dentry from the root filehandle
4766 */
@@ -54,33 +73,6 @@ struct dentry *nfs_get_root(struct super_block *sb, struct nfs_fh *mntfh)
5473 struct inode * inode ;
5574 int error ;
5675
57- /* create a dummy root dentry with dummy inode for this superblock */
58- if (!sb -> s_root ) {
59- struct nfs_fh dummyfh ;
60- struct dentry * root ;
61- struct inode * iroot ;
62-
63- memset (& dummyfh , 0 , sizeof (dummyfh ));
64- memset (& fattr , 0 , sizeof (fattr ));
65- nfs_fattr_init (& fattr );
66- fattr .valid = NFS_ATTR_FATTR ;
67- fattr .type = NFDIR ;
68- fattr .mode = S_IFDIR | S_IRUSR | S_IWUSR ;
69- fattr .nlink = 2 ;
70-
71- iroot = nfs_fhget (sb , & dummyfh , & fattr );
72- if (IS_ERR (iroot ))
73- return ERR_PTR (PTR_ERR (iroot ));
74-
75- root = d_alloc_root (iroot );
76- if (!root ) {
77- iput (iroot );
78- return ERR_PTR (- ENOMEM );
79- }
80-
81- sb -> s_root = root ;
82- }
83-
8476 /* get the actual root for this mount */
8577 fsinfo .fattr = & fattr ;
8678
@@ -96,6 +88,10 @@ struct dentry *nfs_get_root(struct super_block *sb, struct nfs_fh *mntfh)
9688 return ERR_PTR (PTR_ERR (inode ));
9789 }
9890
91+ error = nfs_superblock_set_dummy_root (sb , inode );
92+ if (error != 0 )
93+ return ERR_PTR (error );
94+
9995 /* root dentries normally start off anonymous and get spliced in later
10096 * if the dentry tree reaches them; however if the dentry already
10197 * exists, we'll pick it up at this point and use it as the root
@@ -241,33 +237,6 @@ struct dentry *nfs4_get_root(struct super_block *sb, struct nfs_fh *mntfh)
241237
242238 dprintk ("--> nfs4_get_root()\n" );
243239
244- /* create a dummy root dentry with dummy inode for this superblock */
245- if (!sb -> s_root ) {
246- struct nfs_fh dummyfh ;
247- struct dentry * root ;
248- struct inode * iroot ;
249-
250- memset (& dummyfh , 0 , sizeof (dummyfh ));
251- memset (& fattr , 0 , sizeof (fattr ));
252- nfs_fattr_init (& fattr );
253- fattr .valid = NFS_ATTR_FATTR ;
254- fattr .type = NFDIR ;
255- fattr .mode = S_IFDIR | S_IRUSR | S_IWUSR ;
256- fattr .nlink = 2 ;
257-
258- iroot = nfs_fhget (sb , & dummyfh , & fattr );
259- if (IS_ERR (iroot ))
260- return ERR_PTR (PTR_ERR (iroot ));
261-
262- root = d_alloc_root (iroot );
263- if (!root ) {
264- iput (iroot );
265- return ERR_PTR (- ENOMEM );
266- }
267-
268- sb -> s_root = root ;
269- }
270-
271240 /* get the info about the server and filesystem */
272241 error = nfs4_server_capabilities (server , mntfh );
273242 if (error < 0 ) {
@@ -289,6 +258,10 @@ struct dentry *nfs4_get_root(struct super_block *sb, struct nfs_fh *mntfh)
289258 return ERR_PTR (PTR_ERR (inode ));
290259 }
291260
261+ error = nfs_superblock_set_dummy_root (sb , inode );
262+ if (error != 0 )
263+ return ERR_PTR (error );
264+
292265 /* root dentries normally start off anonymous and get spliced in later
293266 * if the dentry tree reaches them; however if the dentry already
294267 * exists, we'll pick it up at this point and use it as the root
0 commit comments