-
Notifications
You must be signed in to change notification settings - Fork 0
/
fops_status.c
116 lines (99 loc) · 2.65 KB
/
fops_status.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* fops_status.c
*
* fops_status_t functions used by ktext_mod.c.
*
* Copyright (C) 2011 Fabio Erculiani
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
* NON INFRINGEMENT. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <linux/slab.h>
#include <linux/kernel.h>
#include "ktext_config.h"
#include "fops_status.h"
/**
* fops_status_init() - initialize a fops_status_t object.
*
* @fs: the fops_status_t object
*
* This is a internal function used to initialize
* a new fops_status_t object, which is bound to a single
* struct file (and its lifecycle from ktext_open() to
* ktext_release()).
*/
int __must_check
fops_status_init(fops_status_t **fs, bool allocate_text)
{
int status;
#ifdef KTEXT_DEBUG
printk(KERN_NOTICE "fops_status_init: fs: %p\n", *fs);
#endif
status = 0;
if (*fs != NULL) {
BUG_ON(*fs);
BUG();
}
*fs = kmalloc(sizeof(fops_status_t), GFP_KERNEL);
if (*fs == NULL) {
printk(KERN_NOTICE "ktext, fops_status_init: unable to allocate fops_status_t!\n");
status = -ENOMEM;
goto fops_status_init_quit;
}
#ifdef KTEXT_DEBUG
printk(KERN_NOTICE "fops_status_init: allocated fs: %p\n", *fs);
#endif
if (allocate_text) {
(*fs)->text = (char *) kzalloc(sizeof(char) * KTEXT_SIZE, GFP_KERNEL);
if ((*fs)->text == NULL) {
printk(KERN_NOTICE "ktext, fops_status_init: unable to allocate text!\n");
status = -ENOMEM;
goto fops_status_init_dealloc_fs;
}
} else
(*fs)->text = NULL;
(*fs)->count = 0;
(*fs)->total = KTEXT_SIZE;
(*fs)->read_text_strlen = 0;
#ifdef KTEXT_DEBUG
printk(KERN_NOTICE "fops_status_init: all done.\n");
#endif
goto fops_status_init_quit;
fops_status_init_dealloc_fs:
kfree(*fs);
fops_status_init_quit:
return status;
}
/**
* fops_status_destroy() - destroy a fops_status_t object.
*
* @fs: the fops_status_t object
*
* This is a internal function used to destroy
* a previously allocated fops_status_t object.
*
*/
void
fops_status_destroy(fops_status_t *fs)
{
if (fs == NULL)
BUG();
if (fs->text) {
kfree(fs->text);
fs->text = NULL;
}
kfree(fs);
fs = NULL;
}