2727#include <linux/mount.h>
2828#include "ecryptfs_kernel.h"
2929
30- struct kmem_cache * ecryptfs_open_req_cache ;
30+ struct ecryptfs_open_req {
31+ struct file * * lower_file ;
32+ struct dentry * lower_dentry ;
33+ struct vfsmount * lower_mnt ;
34+ struct completion done ;
35+ struct list_head kthread_ctl_list ;
36+ };
3137
3238static struct ecryptfs_kthread_ctl {
3339#define ECRYPTFS_KTHREAD_ZOMBIE 0x00000001
@@ -67,18 +73,13 @@ static int ecryptfs_threadfn(void *ignored)
6773 req = list_first_entry (& ecryptfs_kthread_ctl .req_list ,
6874 struct ecryptfs_open_req ,
6975 kthread_ctl_list );
70- mutex_lock (& req -> mux );
7176 list_del (& req -> kthread_ctl_list );
72- if (!(req -> flags & ECRYPTFS_REQ_ZOMBIE )) {
73- dget (req -> lower_dentry );
74- mntget (req -> lower_mnt );
75- (* req -> lower_file ) = dentry_open (
76- req -> lower_dentry , req -> lower_mnt ,
77- (O_RDWR | O_LARGEFILE ), current_cred ());
78- req -> flags |= ECRYPTFS_REQ_PROCESSED ;
79- }
80- wake_up (& req -> wait );
81- mutex_unlock (& req -> mux );
77+ dget (req -> lower_dentry );
78+ mntget (req -> lower_mnt );
79+ (* req -> lower_file ) = dentry_open (
80+ req -> lower_dentry , req -> lower_mnt ,
81+ (O_RDWR | O_LARGEFILE ), current_cred ());
82+ complete (& req -> done );
8283 }
8384 mutex_unlock (& ecryptfs_kthread_ctl .mux );
8485 }
@@ -111,10 +112,9 @@ void ecryptfs_destroy_kthread(void)
111112 ecryptfs_kthread_ctl .flags |= ECRYPTFS_KTHREAD_ZOMBIE ;
112113 list_for_each_entry (req , & ecryptfs_kthread_ctl .req_list ,
113114 kthread_ctl_list ) {
114- mutex_lock (& req -> mux );
115- req -> flags |= ECRYPTFS_REQ_ZOMBIE ;
116- wake_up (& req -> wait );
117- mutex_unlock (& req -> mux );
115+ list_del (& req -> kthread_ctl_list );
116+ * req -> lower_file = ERR_PTR (- EIO );
117+ complete (& req -> done );
118118 }
119119 mutex_unlock (& ecryptfs_kthread_ctl .mux );
120120 kthread_stop (ecryptfs_kthread );
@@ -136,7 +136,7 @@ int ecryptfs_privileged_open(struct file **lower_file,
136136 struct vfsmount * lower_mnt ,
137137 const struct cred * cred )
138138{
139- struct ecryptfs_open_req * req ;
139+ struct ecryptfs_open_req req ;
140140 int flags = O_LARGEFILE ;
141141 int rc = 0 ;
142142
@@ -153,45 +153,25 @@ int ecryptfs_privileged_open(struct file **lower_file,
153153 rc = PTR_ERR ((* lower_file ));
154154 goto out ;
155155 }
156- req = kmem_cache_alloc (ecryptfs_open_req_cache , GFP_KERNEL );
157- if (!req ) {
158- rc = - ENOMEM ;
159- goto out ;
160- }
161- mutex_init (& req -> mux );
162- req -> lower_file = lower_file ;
163- req -> lower_dentry = lower_dentry ;
164- req -> lower_mnt = lower_mnt ;
165- init_waitqueue_head (& req -> wait );
166- req -> flags = 0 ;
156+ init_completion (& req .done );
157+ req .lower_file = lower_file ;
158+ req .lower_dentry = lower_dentry ;
159+ req .lower_mnt = lower_mnt ;
167160 mutex_lock (& ecryptfs_kthread_ctl .mux );
168161 if (ecryptfs_kthread_ctl .flags & ECRYPTFS_KTHREAD_ZOMBIE ) {
169162 rc = - EIO ;
170163 mutex_unlock (& ecryptfs_kthread_ctl .mux );
171164 printk (KERN_ERR "%s: We are in the middle of shutting down; "
172165 "aborting privileged request to open lower file\n" ,
173166 __func__ );
174- goto out_free ;
167+ goto out ;
175168 }
176- list_add_tail (& req -> kthread_ctl_list , & ecryptfs_kthread_ctl .req_list );
169+ list_add_tail (& req . kthread_ctl_list , & ecryptfs_kthread_ctl .req_list );
177170 mutex_unlock (& ecryptfs_kthread_ctl .mux );
178171 wake_up (& ecryptfs_kthread_ctl .wait );
179- wait_event (req -> wait , (req -> flags != 0 ));
180- mutex_lock (& req -> mux );
181- BUG_ON (req -> flags == 0 );
182- if (req -> flags & ECRYPTFS_REQ_DROPPED
183- || req -> flags & ECRYPTFS_REQ_ZOMBIE ) {
184- rc = - EIO ;
185- printk (KERN_WARNING "%s: Privileged open request dropped\n" ,
186- __func__ );
187- goto out_unlock ;
188- }
189- if (IS_ERR (* req -> lower_file ))
190- rc = PTR_ERR (* req -> lower_file );
191- out_unlock :
192- mutex_unlock (& req -> mux );
193- out_free :
194- kmem_cache_free (ecryptfs_open_req_cache , req );
172+ wait_for_completion (& req .done );
173+ if (IS_ERR (* lower_file ))
174+ rc = PTR_ERR (* lower_file );
195175out :
196176 return rc ;
197177}
0 commit comments