15
15
* file or directory. The caller must hold the inode lock.
16
16
*/
17
17
static bool __cachefiles_mark_inode_in_use (struct cachefiles_object * object ,
18
- struct dentry * dentry )
18
+ struct inode * inode )
19
19
{
20
- struct inode * inode = d_backing_inode (dentry );
21
20
bool can_use = false;
22
21
23
22
if (!(inode -> i_flags & S_KERNEL_FILE )) {
@@ -26,21 +25,18 @@ static bool __cachefiles_mark_inode_in_use(struct cachefiles_object *object,
26
25
can_use = true;
27
26
} else {
28
27
trace_cachefiles_mark_failed (object , inode );
29
- pr_notice ("cachefiles: Inode already in use: %pd (B=%lx)\n" ,
30
- dentry , inode -> i_ino );
31
28
}
32
29
33
30
return can_use ;
34
31
}
35
32
36
33
static bool cachefiles_mark_inode_in_use (struct cachefiles_object * object ,
37
- struct dentry * dentry )
34
+ struct inode * inode )
38
35
{
39
- struct inode * inode = d_backing_inode (dentry );
40
36
bool can_use ;
41
37
42
38
inode_lock (inode );
43
- can_use = __cachefiles_mark_inode_in_use (object , dentry );
39
+ can_use = __cachefiles_mark_inode_in_use (object , inode );
44
40
inode_unlock (inode );
45
41
return can_use ;
46
42
}
@@ -49,21 +45,17 @@ static bool cachefiles_mark_inode_in_use(struct cachefiles_object *object,
49
45
* Unmark a backing inode. The caller must hold the inode lock.
50
46
*/
51
47
static void __cachefiles_unmark_inode_in_use (struct cachefiles_object * object ,
52
- struct dentry * dentry )
48
+ struct inode * inode )
53
49
{
54
- struct inode * inode = d_backing_inode (dentry );
55
-
56
50
inode -> i_flags &= ~S_KERNEL_FILE ;
57
51
trace_cachefiles_mark_inactive (object , inode );
58
52
}
59
53
60
54
static void cachefiles_do_unmark_inode_in_use (struct cachefiles_object * object ,
61
- struct dentry * dentry )
55
+ struct inode * inode )
62
56
{
63
- struct inode * inode = d_backing_inode (dentry );
64
-
65
57
inode_lock (inode );
66
- __cachefiles_unmark_inode_in_use (object , dentry );
58
+ __cachefiles_unmark_inode_in_use (object , inode );
67
59
inode_unlock (inode );
68
60
}
69
61
@@ -77,14 +69,12 @@ void cachefiles_unmark_inode_in_use(struct cachefiles_object *object,
77
69
struct cachefiles_cache * cache = object -> volume -> cache ;
78
70
struct inode * inode = file_inode (file );
79
71
80
- if (inode ) {
81
- cachefiles_do_unmark_inode_in_use (object , file -> f_path .dentry );
72
+ cachefiles_do_unmark_inode_in_use (object , inode );
82
73
83
- if (!test_bit (CACHEFILES_OBJECT_USING_TMPFILE , & object -> flags )) {
84
- atomic_long_add (inode -> i_blocks , & cache -> b_released );
85
- if (atomic_inc_return (& cache -> f_released ))
86
- cachefiles_state_changed (cache );
87
- }
74
+ if (!test_bit (CACHEFILES_OBJECT_USING_TMPFILE , & object -> flags )) {
75
+ atomic_long_add (inode -> i_blocks , & cache -> b_released );
76
+ if (atomic_inc_return (& cache -> f_released ))
77
+ cachefiles_state_changed (cache );
88
78
}
89
79
}
90
80
@@ -164,8 +154,11 @@ struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
164
154
inode_lock (d_inode (subdir ));
165
155
inode_unlock (d_inode (dir ));
166
156
167
- if (!__cachefiles_mark_inode_in_use (NULL , subdir ))
157
+ if (!__cachefiles_mark_inode_in_use (NULL , d_inode (subdir ))) {
158
+ pr_notice ("cachefiles: Inode already in use: %pd (B=%lx)\n" ,
159
+ subdir , d_inode (subdir )-> i_ino );
168
160
goto mark_error ;
161
+ }
169
162
170
163
inode_unlock (d_inode (subdir ));
171
164
@@ -224,9 +217,7 @@ struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
224
217
void cachefiles_put_directory (struct dentry * dir )
225
218
{
226
219
if (dir ) {
227
- inode_lock (dir -> d_inode );
228
- __cachefiles_unmark_inode_in_use (NULL , dir );
229
- inode_unlock (dir -> d_inode );
220
+ cachefiles_do_unmark_inode_in_use (NULL , d_inode (dir ));
230
221
dput (dir );
231
222
}
232
223
}
@@ -410,7 +401,7 @@ int cachefiles_bury_object(struct cachefiles_cache *cache,
410
401
"Rename failed with error %d" , ret );
411
402
}
412
403
413
- __cachefiles_unmark_inode_in_use (object , rep );
404
+ __cachefiles_unmark_inode_in_use (object , d_inode ( rep ) );
414
405
unlock_rename (cache -> graveyard , dir );
415
406
dput (grave );
416
407
_leave (" = 0" );
@@ -451,84 +442,72 @@ struct file *cachefiles_create_tmpfile(struct cachefiles_object *object)
451
442
const struct cred * saved_cred ;
452
443
struct dentry * fan = volume -> fanout [(u8 )object -> cookie -> key_hash ];
453
444
struct file * file ;
454
- struct path path ;
445
+ const struct path parentpath = { . mnt = cache -> mnt , . dentry = fan } ;
455
446
uint64_t ni_size ;
456
447
long ret ;
457
448
458
449
459
450
cachefiles_begin_secure (cache , & saved_cred );
460
451
461
- path .mnt = cache -> mnt ;
462
452
ret = cachefiles_inject_write_error ();
463
- if (ret == 0 )
464
- path .dentry = vfs_tmpfile (& init_user_ns , fan , S_IFREG , O_RDWR );
465
- else
466
- path .dentry = ERR_PTR (ret );
467
- if (IS_ERR (path .dentry )) {
468
- trace_cachefiles_vfs_error (object , d_inode (fan ), PTR_ERR (path .dentry ),
453
+ if (ret == 0 ) {
454
+ file = vfs_tmpfile_open (& init_user_ns , & parentpath , S_IFREG ,
455
+ O_RDWR | O_LARGEFILE | O_DIRECT ,
456
+ cache -> cache_cred );
457
+ ret = PTR_ERR_OR_ZERO (file );
458
+ }
459
+ if (ret ) {
460
+ trace_cachefiles_vfs_error (object , d_inode (fan ), ret ,
469
461
cachefiles_trace_tmpfile_error );
470
- if (PTR_ERR ( path . dentry ) == - EIO )
462
+ if (ret == - EIO )
471
463
cachefiles_io_error_obj (object , "Failed to create tmpfile" );
472
- file = ERR_CAST (path .dentry );
473
- goto out ;
464
+ goto err ;
474
465
}
475
466
476
- trace_cachefiles_tmpfile (object , d_backing_inode ( path . dentry ));
467
+ trace_cachefiles_tmpfile (object , file_inode ( file ));
477
468
478
- if (!cachefiles_mark_inode_in_use (object , path .dentry )) {
479
- file = ERR_PTR (- EBUSY );
480
- goto out_dput ;
481
- }
469
+ /* This is a newly created file with no other possible user */
470
+ if (!cachefiles_mark_inode_in_use (object , file_inode (file )))
471
+ WARN_ON (1 );
482
472
483
473
ret = cachefiles_ondemand_init_object (object );
484
- if (ret < 0 ) {
485
- file = ERR_PTR (ret );
486
- goto out_unuse ;
487
- }
474
+ if (ret < 0 )
475
+ goto err_unuse ;
488
476
489
477
ni_size = object -> cookie -> object_size ;
490
478
ni_size = round_up (ni_size , CACHEFILES_DIO_BLOCK_SIZE );
491
479
492
480
if (ni_size > 0 ) {
493
- trace_cachefiles_trunc (object , d_backing_inode ( path . dentry ), 0 , ni_size ,
481
+ trace_cachefiles_trunc (object , file_inode ( file ), 0 , ni_size ,
494
482
cachefiles_trunc_expand_tmpfile );
495
483
ret = cachefiles_inject_write_error ();
496
484
if (ret == 0 )
497
- ret = vfs_truncate (& path , ni_size );
485
+ ret = vfs_truncate (& file -> f_path , ni_size );
498
486
if (ret < 0 ) {
499
487
trace_cachefiles_vfs_error (
500
- object , d_backing_inode ( path . dentry ), ret ,
488
+ object , file_inode ( file ), ret ,
501
489
cachefiles_trace_trunc_error );
502
- file = ERR_PTR (ret );
503
- goto out_unuse ;
490
+ goto err_unuse ;
504
491
}
505
492
}
506
493
507
- file = open_with_fake_path (& path , O_RDWR | O_LARGEFILE | O_DIRECT ,
508
- d_backing_inode (path .dentry ), cache -> cache_cred );
509
- if (IS_ERR (file )) {
510
- trace_cachefiles_vfs_error (object , d_backing_inode (path .dentry ),
511
- PTR_ERR (file ),
512
- cachefiles_trace_open_error );
513
- goto out_unuse ;
514
- }
494
+ ret = - EINVAL ;
515
495
if (unlikely (!file -> f_op -> read_iter ) ||
516
496
unlikely (!file -> f_op -> write_iter )) {
517
497
fput (file );
518
498
pr_notice ("Cache does not support read_iter and write_iter\n" );
519
- file = ERR_PTR (- EINVAL );
520
- goto out_unuse ;
499
+ goto err_unuse ;
521
500
}
522
-
523
- goto out_dput ;
524
-
525
- out_unuse :
526
- cachefiles_do_unmark_inode_in_use (object , path .dentry );
527
- out_dput :
528
- dput (path .dentry );
529
501
out :
530
502
cachefiles_end_secure (cache , saved_cred );
531
503
return file ;
504
+
505
+ err_unuse :
506
+ cachefiles_do_unmark_inode_in_use (object , file_inode (file ));
507
+ fput (file );
508
+ err :
509
+ file = ERR_PTR (ret );
510
+ goto out ;
532
511
}
533
512
534
513
/*
@@ -569,8 +548,11 @@ static bool cachefiles_open_file(struct cachefiles_object *object,
569
548
570
549
_enter ("%pd" , dentry );
571
550
572
- if (!cachefiles_mark_inode_in_use (object , dentry ))
551
+ if (!cachefiles_mark_inode_in_use (object , d_inode (dentry ))) {
552
+ pr_notice ("cachefiles: Inode already in use: %pd (B=%lx)\n" ,
553
+ dentry , d_inode (dentry )-> i_ino );
573
554
return false;
555
+ }
574
556
575
557
/* We need to open a file interface onto a data file now as we can't do
576
558
* it on demand because writeback called from do_exit() sees
@@ -624,7 +606,7 @@ static bool cachefiles_open_file(struct cachefiles_object *object,
624
606
error_fput :
625
607
fput (file );
626
608
error :
627
- cachefiles_do_unmark_inode_in_use (object , dentry );
609
+ cachefiles_do_unmark_inode_in_use (object , d_inode ( dentry ) );
628
610
dput (dentry );
629
611
return false;
630
612
}
0 commit comments