3030#define PTMX_MINOR 2
3131
3232extern int pty_limit ; /* Config limit on Unix98 ptys */
33- static DEFINE_IDA (allocated_ptys );
3433static DEFINE_MUTEX (allocated_ptys_lock );
3534
3635static struct vfsmount * devpts_mnt ;
@@ -55,6 +54,15 @@ static const match_table_t tokens = {
5554 {Opt_err , NULL }
5655};
5756
57+ struct pts_fs_info {
58+ struct ida allocated_ptys ;
59+ };
60+
61+ static inline struct pts_fs_info * DEVPTS_SB (struct super_block * sb )
62+ {
63+ return sb -> s_fs_info ;
64+ }
65+
5866static inline struct super_block * pts_sb_from_inode (struct inode * inode )
5967{
6068 if (inode -> i_sb -> s_magic == DEVPTS_SUPER_MAGIC )
@@ -126,6 +134,19 @@ static const struct super_operations devpts_sops = {
126134 .show_options = devpts_show_options ,
127135};
128136
137+ static void * new_pts_fs_info (void )
138+ {
139+ struct pts_fs_info * fsi ;
140+
141+ fsi = kzalloc (sizeof (struct pts_fs_info ), GFP_KERNEL );
142+ if (!fsi )
143+ return NULL ;
144+
145+ ida_init (& fsi -> allocated_ptys );
146+
147+ return fsi ;
148+ }
149+
129150static int
130151devpts_fill_super (struct super_block * s , void * data , int silent )
131152{
@@ -137,9 +158,13 @@ devpts_fill_super(struct super_block *s, void *data, int silent)
137158 s -> s_op = & devpts_sops ;
138159 s -> s_time_gran = 1 ;
139160
161+ s -> s_fs_info = new_pts_fs_info ();
162+ if (!s -> s_fs_info )
163+ goto fail ;
164+
140165 inode = new_inode (s );
141166 if (!inode )
142- goto fail ;
167+ goto free_fsi ;
143168 inode -> i_ino = 1 ;
144169 inode -> i_mtime = inode -> i_atime = inode -> i_ctime = CURRENT_TIME ;
145170 inode -> i_blocks = 0 ;
@@ -155,6 +180,9 @@ devpts_fill_super(struct super_block *s, void *data, int silent)
155180
156181 printk ("devpts: get root dentry failed\n" );
157182 iput (inode );
183+
184+ free_fsi :
185+ kfree (s -> s_fs_info );
158186fail :
159187 return - ENOMEM ;
160188}
@@ -165,11 +193,19 @@ static int devpts_get_sb(struct file_system_type *fs_type,
165193 return get_sb_single (fs_type , flags , data , devpts_fill_super , mnt );
166194}
167195
196+ static void devpts_kill_sb (struct super_block * sb )
197+ {
198+ struct pts_fs_info * fsi = DEVPTS_SB (sb );
199+
200+ kfree (fsi );
201+ kill_anon_super (sb );
202+ }
203+
168204static struct file_system_type devpts_fs_type = {
169205 .owner = THIS_MODULE ,
170206 .name = "devpts" ,
171207 .get_sb = devpts_get_sb ,
172- .kill_sb = kill_anon_super ,
208+ .kill_sb = devpts_kill_sb ,
173209};
174210
175211/*
@@ -179,16 +215,18 @@ static struct file_system_type devpts_fs_type = {
179215
180216int devpts_new_index (struct inode * ptmx_inode )
181217{
218+ struct super_block * sb = pts_sb_from_inode (ptmx_inode );
219+ struct pts_fs_info * fsi = DEVPTS_SB (sb );
182220 int index ;
183221 int ida_ret ;
184222
185223retry :
186- if (!ida_pre_get (& allocated_ptys , GFP_KERNEL )) {
224+ if (!ida_pre_get (& fsi -> allocated_ptys , GFP_KERNEL )) {
187225 return - ENOMEM ;
188226 }
189227
190228 mutex_lock (& allocated_ptys_lock );
191- ida_ret = ida_get_new (& allocated_ptys , & index );
229+ ida_ret = ida_get_new (& fsi -> allocated_ptys , & index );
192230 if (ida_ret < 0 ) {
193231 mutex_unlock (& allocated_ptys_lock );
194232 if (ida_ret == - EAGAIN )
@@ -197,7 +235,7 @@ int devpts_new_index(struct inode *ptmx_inode)
197235 }
198236
199237 if (index >= pty_limit ) {
200- ida_remove (& allocated_ptys , index );
238+ ida_remove (& fsi -> allocated_ptys , index );
201239 mutex_unlock (& allocated_ptys_lock );
202240 return - EIO ;
203241 }
@@ -207,8 +245,11 @@ int devpts_new_index(struct inode *ptmx_inode)
207245
208246void devpts_kill_index (struct inode * ptmx_inode , int idx )
209247{
248+ struct super_block * sb = pts_sb_from_inode (ptmx_inode );
249+ struct pts_fs_info * fsi = DEVPTS_SB (sb );
250+
210251 mutex_lock (& allocated_ptys_lock );
211- ida_remove (& allocated_ptys , idx );
252+ ida_remove (& fsi -> allocated_ptys , idx );
212253 mutex_unlock (& allocated_ptys_lock );
213254}
214255
0 commit comments