@@ -70,6 +70,24 @@ static struct kernfs_open_node *of_on(struct kernfs_open_file *of)
70
70
!list_empty (& of -> list ));
71
71
}
72
72
73
+ /* Get active reference to kernfs node for an open file */
74
+ static struct kernfs_open_file * kernfs_get_active_of (struct kernfs_open_file * of )
75
+ {
76
+ /* Skip if file was already released */
77
+ if (unlikely (of -> released ))
78
+ return NULL ;
79
+
80
+ if (!kernfs_get_active (of -> kn ))
81
+ return NULL ;
82
+
83
+ return of ;
84
+ }
85
+
86
+ static void kernfs_put_active_of (struct kernfs_open_file * of )
87
+ {
88
+ return kernfs_put_active (of -> kn );
89
+ }
90
+
73
91
/**
74
92
* kernfs_deref_open_node_locked - Get kernfs_open_node corresponding to @kn
75
93
*
@@ -139,7 +157,7 @@ static void kernfs_seq_stop_active(struct seq_file *sf, void *v)
139
157
140
158
if (ops -> seq_stop )
141
159
ops -> seq_stop (sf , v );
142
- kernfs_put_active (of -> kn );
160
+ kernfs_put_active_of (of );
143
161
}
144
162
145
163
static void * kernfs_seq_start (struct seq_file * sf , loff_t * ppos )
@@ -152,7 +170,7 @@ static void *kernfs_seq_start(struct seq_file *sf, loff_t *ppos)
152
170
* the ops aren't called concurrently for the same open file.
153
171
*/
154
172
mutex_lock (& of -> mutex );
155
- if (!kernfs_get_active (of -> kn ))
173
+ if (!kernfs_get_active_of (of ))
156
174
return ERR_PTR (- ENODEV );
157
175
158
176
ops = kernfs_ops (of -> kn );
@@ -238,7 +256,7 @@ static ssize_t kernfs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
238
256
* the ops aren't called concurrently for the same open file.
239
257
*/
240
258
mutex_lock (& of -> mutex );
241
- if (!kernfs_get_active (of -> kn )) {
259
+ if (!kernfs_get_active_of (of )) {
242
260
len = - ENODEV ;
243
261
mutex_unlock (& of -> mutex );
244
262
goto out_free ;
@@ -252,7 +270,7 @@ static ssize_t kernfs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
252
270
else
253
271
len = - EINVAL ;
254
272
255
- kernfs_put_active (of -> kn );
273
+ kernfs_put_active_of (of );
256
274
mutex_unlock (& of -> mutex );
257
275
258
276
if (len < 0 )
@@ -323,7 +341,7 @@ static ssize_t kernfs_fop_write_iter(struct kiocb *iocb, struct iov_iter *iter)
323
341
* the ops aren't called concurrently for the same open file.
324
342
*/
325
343
mutex_lock (& of -> mutex );
326
- if (!kernfs_get_active (of -> kn )) {
344
+ if (!kernfs_get_active_of (of )) {
327
345
mutex_unlock (& of -> mutex );
328
346
len = - ENODEV ;
329
347
goto out_free ;
@@ -335,7 +353,7 @@ static ssize_t kernfs_fop_write_iter(struct kiocb *iocb, struct iov_iter *iter)
335
353
else
336
354
len = - EINVAL ;
337
355
338
- kernfs_put_active (of -> kn );
356
+ kernfs_put_active_of (of );
339
357
mutex_unlock (& of -> mutex );
340
358
341
359
if (len > 0 )
@@ -357,13 +375,13 @@ static void kernfs_vma_open(struct vm_area_struct *vma)
357
375
if (!of -> vm_ops )
358
376
return ;
359
377
360
- if (!kernfs_get_active (of -> kn ))
378
+ if (!kernfs_get_active_of (of ))
361
379
return ;
362
380
363
381
if (of -> vm_ops -> open )
364
382
of -> vm_ops -> open (vma );
365
383
366
- kernfs_put_active (of -> kn );
384
+ kernfs_put_active_of (of );
367
385
}
368
386
369
387
static vm_fault_t kernfs_vma_fault (struct vm_fault * vmf )
@@ -375,14 +393,14 @@ static vm_fault_t kernfs_vma_fault(struct vm_fault *vmf)
375
393
if (!of -> vm_ops )
376
394
return VM_FAULT_SIGBUS ;
377
395
378
- if (!kernfs_get_active (of -> kn ))
396
+ if (!kernfs_get_active_of (of ))
379
397
return VM_FAULT_SIGBUS ;
380
398
381
399
ret = VM_FAULT_SIGBUS ;
382
400
if (of -> vm_ops -> fault )
383
401
ret = of -> vm_ops -> fault (vmf );
384
402
385
- kernfs_put_active (of -> kn );
403
+ kernfs_put_active_of (of );
386
404
return ret ;
387
405
}
388
406
@@ -395,7 +413,7 @@ static vm_fault_t kernfs_vma_page_mkwrite(struct vm_fault *vmf)
395
413
if (!of -> vm_ops )
396
414
return VM_FAULT_SIGBUS ;
397
415
398
- if (!kernfs_get_active (of -> kn ))
416
+ if (!kernfs_get_active_of (of ))
399
417
return VM_FAULT_SIGBUS ;
400
418
401
419
ret = 0 ;
@@ -404,7 +422,7 @@ static vm_fault_t kernfs_vma_page_mkwrite(struct vm_fault *vmf)
404
422
else
405
423
file_update_time (file );
406
424
407
- kernfs_put_active (of -> kn );
425
+ kernfs_put_active_of (of );
408
426
return ret ;
409
427
}
410
428
@@ -418,14 +436,14 @@ static int kernfs_vma_access(struct vm_area_struct *vma, unsigned long addr,
418
436
if (!of -> vm_ops )
419
437
return - EINVAL ;
420
438
421
- if (!kernfs_get_active (of -> kn ))
439
+ if (!kernfs_get_active_of (of ))
422
440
return - EINVAL ;
423
441
424
442
ret = - EINVAL ;
425
443
if (of -> vm_ops -> access )
426
444
ret = of -> vm_ops -> access (vma , addr , buf , len , write );
427
445
428
- kernfs_put_active (of -> kn );
446
+ kernfs_put_active_of (of );
429
447
return ret ;
430
448
}
431
449
@@ -455,7 +473,7 @@ static int kernfs_fop_mmap(struct file *file, struct vm_area_struct *vma)
455
473
mutex_lock (& of -> mutex );
456
474
457
475
rc = - ENODEV ;
458
- if (!kernfs_get_active (of -> kn ))
476
+ if (!kernfs_get_active_of (of ))
459
477
goto out_unlock ;
460
478
461
479
ops = kernfs_ops (of -> kn );
@@ -490,7 +508,7 @@ static int kernfs_fop_mmap(struct file *file, struct vm_area_struct *vma)
490
508
}
491
509
vma -> vm_ops = & kernfs_vm_ops ;
492
510
out_put :
493
- kernfs_put_active (of -> kn );
511
+ kernfs_put_active_of (of );
494
512
out_unlock :
495
513
mutex_unlock (& of -> mutex );
496
514
@@ -852,15 +870,15 @@ static __poll_t kernfs_fop_poll(struct file *filp, poll_table *wait)
852
870
struct kernfs_node * kn = kernfs_dentry_node (filp -> f_path .dentry );
853
871
__poll_t ret ;
854
872
855
- if (!kernfs_get_active ( kn ))
873
+ if (!kernfs_get_active_of ( of ))
856
874
return DEFAULT_POLLMASK |EPOLLERR |EPOLLPRI ;
857
875
858
876
if (kn -> attr .ops -> poll )
859
877
ret = kn -> attr .ops -> poll (of , wait );
860
878
else
861
879
ret = kernfs_generic_poll (of , wait );
862
880
863
- kernfs_put_active ( kn );
881
+ kernfs_put_active_of ( of );
864
882
return ret ;
865
883
}
866
884
@@ -875,7 +893,7 @@ static loff_t kernfs_fop_llseek(struct file *file, loff_t offset, int whence)
875
893
* the ops aren't called concurrently for the same open file.
876
894
*/
877
895
mutex_lock (& of -> mutex );
878
- if (!kernfs_get_active (of -> kn )) {
896
+ if (!kernfs_get_active_of (of )) {
879
897
mutex_unlock (& of -> mutex );
880
898
return - ENODEV ;
881
899
}
@@ -886,7 +904,7 @@ static loff_t kernfs_fop_llseek(struct file *file, loff_t offset, int whence)
886
904
else
887
905
ret = generic_file_llseek (file , offset , whence );
888
906
889
- kernfs_put_active (of -> kn );
907
+ kernfs_put_active_of (of );
890
908
mutex_unlock (& of -> mutex );
891
909
return ret ;
892
910
}
0 commit comments