23
23
#include <linux/pstore.h>
24
24
#include <linux/slab.h>
25
25
#include <linux/uaccess.h>
26
+ #include <linux/cleanup.h>
26
27
27
28
#include "internal.h"
28
29
@@ -34,6 +35,8 @@ static LIST_HEAD(records_list);
34
35
static DEFINE_MUTEX (pstore_sb_lock );
35
36
static struct super_block * pstore_sb ;
36
37
38
+ DEFINE_FREE (pstore_iput , struct inode * , if (_T ) iput (_T ))
39
+
37
40
struct pstore_private {
38
41
struct list_head list ;
39
42
struct dentry * dentry ;
@@ -60,25 +63,23 @@ static void free_pstore_private(struct pstore_private *private)
60
63
}
61
64
kfree (private );
62
65
}
66
+ DEFINE_FREE (pstore_private , struct pstore_private * , free_pstore_private (_T ));
63
67
64
68
static void * pstore_ftrace_seq_start (struct seq_file * s , loff_t * pos )
65
69
{
66
70
struct pstore_private * ps = s -> private ;
67
- struct pstore_ftrace_seq_data * data ;
71
+ struct pstore_ftrace_seq_data * data __free ( kfree ) = NULL ;
68
72
69
73
data = kzalloc (sizeof (* data ), GFP_KERNEL );
70
74
if (!data )
71
75
return NULL ;
72
76
73
77
data -> off = ps -> total_size % REC_SIZE ;
74
78
data -> off += * pos * REC_SIZE ;
75
- if (data -> off + REC_SIZE > ps -> total_size ) {
76
- kfree (data );
79
+ if (data -> off + REC_SIZE > ps -> total_size )
77
80
return NULL ;
78
- }
79
-
80
- return data ;
81
81
82
+ return_ptr (data );
82
83
}
83
84
84
85
static void pstore_ftrace_seq_stop (struct seq_file * s , void * v )
@@ -182,25 +183,21 @@ static int pstore_unlink(struct inode *dir, struct dentry *dentry)
182
183
{
183
184
struct pstore_private * p = d_inode (dentry )-> i_private ;
184
185
struct pstore_record * record = p -> record ;
185
- int rc = 0 ;
186
186
187
187
if (!record -> psi -> erase )
188
188
return - EPERM ;
189
189
190
190
/* Make sure we can't race while removing this file. */
191
- mutex_lock (& records_list_lock );
192
- if (!list_empty (& p -> list ))
193
- list_del_init (& p -> list );
194
- else
195
- rc = - ENOENT ;
196
- p -> dentry = NULL ;
197
- mutex_unlock (& records_list_lock );
198
- if (rc )
199
- return rc ;
200
-
201
- mutex_lock (& record -> psi -> read_mutex );
202
- record -> psi -> erase (record );
203
- mutex_unlock (& record -> psi -> read_mutex );
191
+ scoped_guard (mutex , & records_list_lock ) {
192
+ if (!list_empty (& p -> list ))
193
+ list_del_init (& p -> list );
194
+ else
195
+ return - ENOENT ;
196
+ p -> dentry = NULL ;
197
+ }
198
+
199
+ scoped_guard (mutex , & record -> psi -> read_mutex )
200
+ record -> psi -> erase (record );
204
201
205
202
return simple_unlink (dir , dentry );
206
203
}
@@ -292,19 +289,16 @@ static struct dentry *psinfo_lock_root(void)
292
289
{
293
290
struct dentry * root ;
294
291
295
- mutex_lock (& pstore_sb_lock );
292
+ guard ( mutex ) (& pstore_sb_lock );
296
293
/*
297
294
* Having no backend is fine -- no records appear.
298
295
* Not being mounted is fine -- nothing to do.
299
296
*/
300
- if (!psinfo || !pstore_sb ) {
301
- mutex_unlock (& pstore_sb_lock );
297
+ if (!psinfo || !pstore_sb )
302
298
return NULL ;
303
- }
304
299
305
300
root = pstore_sb -> s_root ;
306
301
inode_lock (d_inode (root ));
307
- mutex_unlock (& pstore_sb_lock );
308
302
309
303
return root ;
310
304
}
@@ -319,19 +313,19 @@ int pstore_put_backend_records(struct pstore_info *psi)
319
313
if (!root )
320
314
return 0 ;
321
315
322
- mutex_lock (& records_list_lock );
323
- list_for_each_entry_safe (pos , tmp , & records_list , list ) {
324
- if (pos -> record -> psi == psi ) {
325
- list_del_init (& pos -> list );
326
- rc = simple_unlink (d_inode (root ), pos -> dentry );
327
- if (WARN_ON (rc ))
328
- break ;
329
- d_drop (pos -> dentry );
330
- dput (pos -> dentry );
331
- pos -> dentry = NULL ;
316
+ scoped_guard (mutex , & records_list_lock ) {
317
+ list_for_each_entry_safe (pos , tmp , & records_list , list ) {
318
+ if (pos -> record -> psi == psi ) {
319
+ list_del_init (& pos -> list );
320
+ rc = simple_unlink (d_inode (root ), pos -> dentry );
321
+ if (WARN_ON (rc ))
322
+ break ;
323
+ d_drop (pos -> dentry );
324
+ dput (pos -> dentry );
325
+ pos -> dentry = NULL ;
326
+ }
332
327
}
333
328
}
334
- mutex_unlock (& records_list_lock );
335
329
336
330
inode_unlock (d_inode (root ));
337
331
@@ -346,29 +340,27 @@ int pstore_put_backend_records(struct pstore_info *psi)
346
340
int pstore_mkfile (struct dentry * root , struct pstore_record * record )
347
341
{
348
342
struct dentry * dentry ;
349
- struct inode * inode ;
350
- int rc = 0 ;
343
+ struct inode * inode __free (pstore_iput ) = NULL ;
351
344
char name [PSTORE_NAMELEN ];
352
- struct pstore_private * private , * pos ;
345
+ struct pstore_private * private __free ( pstore_private ) = NULL , * pos ;
353
346
size_t size = record -> size + record -> ecc_notice_size ;
354
347
355
348
if (WARN_ON (!inode_is_locked (d_inode (root ))))
356
349
return - EINVAL ;
357
350
358
- rc = - EEXIST ;
351
+ guard (mutex )(& records_list_lock );
352
+
359
353
/* Skip records that are already present in the filesystem. */
360
- mutex_lock (& records_list_lock );
361
354
list_for_each_entry (pos , & records_list , list ) {
362
355
if (pos -> record -> type == record -> type &&
363
356
pos -> record -> id == record -> id &&
364
357
pos -> record -> psi == record -> psi )
365
- goto fail ;
358
+ return - EEXIST ;
366
359
}
367
360
368
- rc = - ENOMEM ;
369
361
inode = pstore_get_inode (root -> d_sb );
370
362
if (!inode )
371
- goto fail ;
363
+ return - ENOMEM ;
372
364
inode -> i_mode = S_IFREG | 0444 ;
373
365
inode -> i_fop = & pstore_file_operations ;
374
366
scnprintf (name , sizeof (name ), "%s-%s-%llu%s" ,
@@ -378,11 +370,11 @@ int pstore_mkfile(struct dentry *root, struct pstore_record *record)
378
370
379
371
private = kzalloc (sizeof (* private ), GFP_KERNEL );
380
372
if (!private )
381
- goto fail_inode ;
373
+ return - ENOMEM ;
382
374
383
375
dentry = d_alloc_name (root , name );
384
376
if (!dentry )
385
- goto fail_private ;
377
+ return - ENOMEM ;
386
378
387
379
private -> dentry = dentry ;
388
380
private -> record = record ;
@@ -393,20 +385,11 @@ int pstore_mkfile(struct dentry *root, struct pstore_record *record)
393
385
inode_set_mtime_to_ts (inode ,
394
386
inode_set_ctime_to_ts (inode , record -> time ));
395
387
396
- d_add (dentry , inode );
388
+ d_add (dentry , no_free_ptr ( inode ) );
397
389
398
- list_add (& private -> list , & records_list );
399
- mutex_unlock (& records_list_lock );
390
+ list_add (& (no_free_ptr (private ))-> list , & records_list );
400
391
401
392
return 0 ;
402
-
403
- fail_private :
404
- free_pstore_private (private );
405
- fail_inode :
406
- iput (inode );
407
- fail :
408
- mutex_unlock (& records_list_lock );
409
- return rc ;
410
393
}
411
394
412
395
/*
@@ -451,9 +434,8 @@ static int pstore_fill_super(struct super_block *sb, void *data, int silent)
451
434
if (!sb -> s_root )
452
435
return - ENOMEM ;
453
436
454
- mutex_lock (& pstore_sb_lock );
455
- pstore_sb = sb ;
456
- mutex_unlock (& pstore_sb_lock );
437
+ scoped_guard (mutex , & pstore_sb_lock )
438
+ pstore_sb = sb ;
457
439
458
440
pstore_get_records (0 );
459
441
@@ -468,17 +450,14 @@ static struct dentry *pstore_mount(struct file_system_type *fs_type,
468
450
469
451
static void pstore_kill_sb (struct super_block * sb )
470
452
{
471
- mutex_lock (& pstore_sb_lock );
453
+ guard ( mutex ) (& pstore_sb_lock );
472
454
WARN_ON (pstore_sb && pstore_sb != sb );
473
455
474
456
kill_litter_super (sb );
475
457
pstore_sb = NULL ;
476
458
477
- mutex_lock (& records_list_lock );
459
+ guard ( mutex ) (& records_list_lock );
478
460
INIT_LIST_HEAD (& records_list );
479
- mutex_unlock (& records_list_lock );
480
-
481
- mutex_unlock (& pstore_sb_lock );
482
461
}
483
462
484
463
static struct file_system_type pstore_fs_type = {
0 commit comments