@@ -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
@@ -504,7 +522,7 @@ static int kernfs_fop_mmap(struct file *file, struct vm_area_struct *vma)
504
522
mutex_lock (& of -> mutex );
505
523
506
524
rc = - ENODEV ;
507
- if (!kernfs_get_active (of -> kn ))
525
+ if (!kernfs_get_active_of (of ))
508
526
goto out_unlock ;
509
527
510
528
ops = kernfs_ops (of -> kn );
@@ -539,7 +557,7 @@ static int kernfs_fop_mmap(struct file *file, struct vm_area_struct *vma)
539
557
}
540
558
vma -> vm_ops = & kernfs_vm_ops ;
541
559
out_put :
542
- kernfs_put_active (of -> kn );
560
+ kernfs_put_active_of (of );
543
561
out_unlock :
544
562
mutex_unlock (& of -> mutex );
545
563
@@ -894,15 +912,15 @@ static __poll_t kernfs_fop_poll(struct file *filp, poll_table *wait)
894
912
struct kernfs_node * kn = kernfs_dentry_node (filp -> f_path .dentry );
895
913
__poll_t ret ;
896
914
897
- if (!kernfs_get_active ( kn ))
915
+ if (!kernfs_get_active_of ( of ))
898
916
return DEFAULT_POLLMASK |EPOLLERR |EPOLLPRI ;
899
917
900
918
if (kn -> attr .ops -> poll )
901
919
ret = kn -> attr .ops -> poll (of , wait );
902
920
else
903
921
ret = kernfs_generic_poll (of , wait );
904
922
905
- kernfs_put_active ( kn );
923
+ kernfs_put_active_of ( of );
906
924
return ret ;
907
925
}
908
926
0 commit comments