@@ -1118,20 +1118,33 @@ static int fill_super(struct super_block *sb, struct gfs2_args *args, int silent
11181118 }
11191119
11201120 error = init_names (sdp , silent );
1121- if (error )
1122- goto fail ;
1121+ if (error ) {
1122+ /* In this case, we haven't initialized sysfs, so we have to
1123+ manually free the sdp. */
1124+ free_percpu (sdp -> sd_lkstats );
1125+ kfree (sdp );
1126+ sb -> s_fs_info = NULL ;
1127+ return error ;
1128+ }
11231129
11241130 snprintf (sdp -> sd_fsname , GFS2_FSNAME_LEN , "%s" , sdp -> sd_table_name );
11251131
1126- gfs2_create_debugfs_file (sdp );
1127-
11281132 error = gfs2_sys_fs_add (sdp );
1133+ /*
1134+ * If we hit an error here, gfs2_sys_fs_add will have called function
1135+ * kobject_put which causes the sysfs usage count to go to zero, which
1136+ * causes sysfs to call function gfs2_sbd_release, which frees sdp.
1137+ * Subsequent error paths here will call gfs2_sys_fs_del, which also
1138+ * kobject_put to free sdp.
1139+ */
11291140 if (error )
1130- goto fail ;
1141+ return error ;
1142+
1143+ gfs2_create_debugfs_file (sdp );
11311144
11321145 error = gfs2_lm_mount (sdp , silent );
11331146 if (error )
1134- goto fail_sys ;
1147+ goto fail_debug ;
11351148
11361149 error = init_locking (sdp , & mount_gh , DO );
11371150 if (error )
@@ -1215,12 +1228,12 @@ static int fill_super(struct super_block *sb, struct gfs2_args *args, int silent
12151228fail_lm :
12161229 gfs2_gl_hash_clear (sdp );
12171230 gfs2_lm_unmount (sdp );
1218- fail_sys :
1219- gfs2_sys_fs_del (sdp );
1220- fail :
1231+ fail_debug :
12211232 gfs2_delete_debugfs_file (sdp );
12221233 free_percpu (sdp -> sd_lkstats );
1223- kfree (sdp );
1234+ /* gfs2_sys_fs_del must be the last thing we do, since it causes
1235+ * sysfs to call function gfs2_sbd_release, which frees sdp. */
1236+ gfs2_sys_fs_del (sdp );
12241237 sb -> s_fs_info = NULL ;
12251238 return error ;
12261239}
@@ -1390,10 +1403,9 @@ static void gfs2_kill_sb(struct super_block *sb)
13901403 sdp -> sd_root_dir = NULL ;
13911404 sdp -> sd_master_dir = NULL ;
13921405 shrink_dcache_sb (sb );
1393- kill_block_super (sb );
13941406 gfs2_delete_debugfs_file (sdp );
13951407 free_percpu (sdp -> sd_lkstats );
1396- kfree ( sdp );
1408+ kill_block_super ( sb );
13971409}
13981410
13991411struct file_system_type gfs2_fs_type = {
0 commit comments