@@ -157,13 +157,37 @@ static int backing_aio_init_wq(struct kiocb *iocb)
157157 return sb_init_dio_done_wq (sb );
158158}
159159
160+ static int do_backing_file_read_iter (struct file * file , struct iov_iter * iter ,
161+ struct kiocb * iocb , int flags )
162+ {
163+ struct backing_aio * aio = NULL ;
164+ int ret ;
165+
166+ if (is_sync_kiocb (iocb )) {
167+ rwf_t rwf = iocb_to_rw_flags (flags );
168+
169+ return vfs_iter_read (file , iter , & iocb -> ki_pos , rwf );
170+ }
171+
172+ aio = kmem_cache_zalloc (backing_aio_cachep , GFP_KERNEL );
173+ if (!aio )
174+ return - ENOMEM ;
175+
176+ aio -> orig_iocb = iocb ;
177+ kiocb_clone (& aio -> iocb , iocb , get_file (file ));
178+ aio -> iocb .ki_complete = backing_aio_rw_complete ;
179+ refcount_set (& aio -> ref , 2 );
180+ ret = vfs_iocb_iter_read (file , & aio -> iocb , iter );
181+ backing_aio_put (aio );
182+ if (ret != - EIOCBQUEUED )
183+ backing_aio_cleanup (aio , ret );
184+ return ret ;
185+ }
160186
161187ssize_t backing_file_read_iter (struct file * file , struct iov_iter * iter ,
162188 struct kiocb * iocb , int flags ,
163189 struct backing_file_ctx * ctx )
164190{
165- struct backing_aio * aio = NULL ;
166- const struct cred * old_cred ;
167191 ssize_t ret ;
168192
169193 if (WARN_ON_ONCE (!(file -> f_mode & FMODE_BACKING )))
@@ -176,41 +200,57 @@ ssize_t backing_file_read_iter(struct file *file, struct iov_iter *iter,
176200 !(file -> f_mode & FMODE_CAN_ODIRECT ))
177201 return - EINVAL ;
178202
179- old_cred = override_creds (ctx -> cred );
203+ scoped_with_creds (ctx -> cred )
204+ ret = do_backing_file_read_iter (file , iter , iocb , flags );
205+
206+ if (ctx -> accessed )
207+ ctx -> accessed (iocb -> ki_filp );
208+
209+ return ret ;
210+ }
211+ EXPORT_SYMBOL_GPL (backing_file_read_iter );
212+
213+ static int do_backing_file_write_iter (struct file * file , struct iov_iter * iter ,
214+ struct kiocb * iocb , int flags ,
215+ void (* end_write )(struct kiocb * , ssize_t ))
216+ {
217+ struct backing_aio * aio ;
218+ int ret ;
219+
180220 if (is_sync_kiocb (iocb )) {
181221 rwf_t rwf = iocb_to_rw_flags (flags );
182222
183- ret = vfs_iter_read (file , iter , & iocb -> ki_pos , rwf );
184- } else {
185- ret = - ENOMEM ;
186- aio = kmem_cache_zalloc (backing_aio_cachep , GFP_KERNEL );
187- if (!aio )
188- goto out ;
189-
190- aio -> orig_iocb = iocb ;
191- kiocb_clone (& aio -> iocb , iocb , get_file (file ));
192- aio -> iocb .ki_complete = backing_aio_rw_complete ;
193- refcount_set (& aio -> ref , 2 );
194- ret = vfs_iocb_iter_read (file , & aio -> iocb , iter );
195- backing_aio_put (aio );
196- if (ret != - EIOCBQUEUED )
197- backing_aio_cleanup (aio , ret );
223+ ret = vfs_iter_write (file , iter , & iocb -> ki_pos , rwf );
224+ if (end_write )
225+ end_write (iocb , ret );
226+ return ret ;
198227 }
199- out :
200- revert_creds (old_cred );
201228
202- if (ctx -> accessed )
203- ctx -> accessed (iocb -> ki_filp );
229+ ret = backing_aio_init_wq (iocb );
230+ if (ret )
231+ return ret ;
232+
233+ aio = kmem_cache_zalloc (backing_aio_cachep , GFP_KERNEL );
234+ if (!aio )
235+ return - ENOMEM ;
204236
237+ aio -> orig_iocb = iocb ;
238+ aio -> end_write = end_write ;
239+ kiocb_clone (& aio -> iocb , iocb , get_file (file ));
240+ aio -> iocb .ki_flags = flags ;
241+ aio -> iocb .ki_complete = backing_aio_queue_completion ;
242+ refcount_set (& aio -> ref , 2 );
243+ ret = vfs_iocb_iter_write (file , & aio -> iocb , iter );
244+ backing_aio_put (aio );
245+ if (ret != - EIOCBQUEUED )
246+ backing_aio_cleanup (aio , ret );
205247 return ret ;
206248}
207- EXPORT_SYMBOL_GPL (backing_file_read_iter );
208249
209250ssize_t backing_file_write_iter (struct file * file , struct iov_iter * iter ,
210251 struct kiocb * iocb , int flags ,
211252 struct backing_file_ctx * ctx )
212253{
213- const struct cred * old_cred ;
214254 ssize_t ret ;
215255
216256 if (WARN_ON_ONCE (!(file -> f_mode & FMODE_BACKING )))
@@ -233,40 +273,8 @@ ssize_t backing_file_write_iter(struct file *file, struct iov_iter *iter,
233273 */
234274 flags &= ~IOCB_DIO_CALLER_COMP ;
235275
236- old_cred = override_creds (ctx -> cred );
237- if (is_sync_kiocb (iocb )) {
238- rwf_t rwf = iocb_to_rw_flags (flags );
239-
240- ret = vfs_iter_write (file , iter , & iocb -> ki_pos , rwf );
241- if (ctx -> end_write )
242- ctx -> end_write (iocb , ret );
243- } else {
244- struct backing_aio * aio ;
245-
246- ret = backing_aio_init_wq (iocb );
247- if (ret )
248- goto out ;
249-
250- ret = - ENOMEM ;
251- aio = kmem_cache_zalloc (backing_aio_cachep , GFP_KERNEL );
252- if (!aio )
253- goto out ;
254-
255- aio -> orig_iocb = iocb ;
256- aio -> end_write = ctx -> end_write ;
257- kiocb_clone (& aio -> iocb , iocb , get_file (file ));
258- aio -> iocb .ki_flags = flags ;
259- aio -> iocb .ki_complete = backing_aio_queue_completion ;
260- refcount_set (& aio -> ref , 2 );
261- ret = vfs_iocb_iter_write (file , & aio -> iocb , iter );
262- backing_aio_put (aio );
263- if (ret != - EIOCBQUEUED )
264- backing_aio_cleanup (aio , ret );
265- }
266- out :
267- revert_creds (old_cred );
268-
269- return ret ;
276+ scoped_with_creds (ctx -> cred )
277+ return do_backing_file_write_iter (file , iter , iocb , flags , ctx -> end_write );
270278}
271279EXPORT_SYMBOL_GPL (backing_file_write_iter );
272280
@@ -275,15 +283,13 @@ ssize_t backing_file_splice_read(struct file *in, struct kiocb *iocb,
275283 unsigned int flags ,
276284 struct backing_file_ctx * ctx )
277285{
278- const struct cred * old_cred ;
279286 ssize_t ret ;
280287
281288 if (WARN_ON_ONCE (!(in -> f_mode & FMODE_BACKING )))
282289 return - EIO ;
283290
284- old_cred = override_creds (ctx -> cred );
285- ret = vfs_splice_read (in , & iocb -> ki_pos , pipe , len , flags );
286- revert_creds (old_cred );
291+ scoped_with_creds (ctx -> cred )
292+ ret = vfs_splice_read (in , & iocb -> ki_pos , pipe , len , flags );
287293
288294 if (ctx -> accessed )
289295 ctx -> accessed (iocb -> ki_filp );
@@ -297,7 +303,6 @@ ssize_t backing_file_splice_write(struct pipe_inode_info *pipe,
297303 size_t len , unsigned int flags ,
298304 struct backing_file_ctx * ctx )
299305{
300- const struct cred * old_cred ;
301306 ssize_t ret ;
302307
303308 if (WARN_ON_ONCE (!(out -> f_mode & FMODE_BACKING )))
@@ -310,11 +315,11 @@ ssize_t backing_file_splice_write(struct pipe_inode_info *pipe,
310315 if (ret )
311316 return ret ;
312317
313- old_cred = override_creds (ctx -> cred );
314- file_start_write (out );
315- ret = out -> f_op -> splice_write (pipe , out , & iocb -> ki_pos , len , flags );
316- file_end_write (out );
317- revert_creds ( old_cred );
318+ scoped_with_creds (ctx -> cred ) {
319+ file_start_write (out );
320+ ret = out -> f_op -> splice_write (pipe , out , & iocb -> ki_pos , len , flags );
321+ file_end_write (out );
322+ }
318323
319324 if (ctx -> end_write )
320325 ctx -> end_write (iocb , ret );
@@ -326,7 +331,6 @@ EXPORT_SYMBOL_GPL(backing_file_splice_write);
326331int backing_file_mmap (struct file * file , struct vm_area_struct * vma ,
327332 struct backing_file_ctx * ctx )
328333{
329- const struct cred * old_cred ;
330334 struct file * user_file = vma -> vm_file ;
331335 int ret ;
332336
@@ -338,9 +342,8 @@ int backing_file_mmap(struct file *file, struct vm_area_struct *vma,
338342
339343 vma_set_file (vma , file );
340344
341- old_cred = override_creds (ctx -> cred );
342- ret = vfs_mmap (vma -> vm_file , vma );
343- revert_creds (old_cred );
345+ scoped_with_creds (ctx -> cred )
346+ ret = vfs_mmap (vma -> vm_file , vma );
344347
345348 if (ctx -> accessed )
346349 ctx -> accessed (user_file );
0 commit comments