]>
Commit | Line | Data |
---|---|---|
ca01d6dd TL |
1 | /* |
2 | * Persistent Storage - ramfs parts. | |
3 | * | |
4 | * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2 as | |
8 | * published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | */ | |
19 | ||
20 | #include <linux/module.h> | |
21 | #include <linux/fs.h> | |
22 | #include <linux/fsnotify.h> | |
23 | #include <linux/pagemap.h> | |
24 | #include <linux/highmem.h> | |
25 | #include <linux/time.h> | |
26 | #include <linux/init.h> | |
6dda9266 | 27 | #include <linux/list.h> |
ca01d6dd TL |
28 | #include <linux/string.h> |
29 | #include <linux/mount.h> | |
060287b8 | 30 | #include <linux/seq_file.h> |
ca01d6dd | 31 | #include <linux/ramfs.h> |
366f7e7a | 32 | #include <linux/parser.h> |
ca01d6dd TL |
33 | #include <linux/sched.h> |
34 | #include <linux/magic.h> | |
35 | #include <linux/pstore.h> | |
36 | #include <linux/slab.h> | |
6dda9266 | 37 | #include <linux/spinlock.h> |
ca01d6dd TL |
38 | #include <linux/uaccess.h> |
39 | ||
40 | #include "internal.h" | |
41 | ||
42 | #define PSTORE_NAMELEN 64 | |
43 | ||
6dda9266 TL |
44 | static DEFINE_SPINLOCK(allpstore_lock); |
45 | static LIST_HEAD(allpstore); | |
46 | ||
ca01d6dd | 47 | struct pstore_private { |
6dda9266 | 48 | struct list_head list; |
638c1fd3 | 49 | struct pstore_info *psi; |
56280682 MG |
50 | enum pstore_type_id type; |
51 | u64 id; | |
fbe0aa1f TL |
52 | ssize_t size; |
53 | char data[]; | |
ca01d6dd TL |
54 | }; |
55 | ||
060287b8 AV |
56 | struct pstore_ftrace_seq_data { |
57 | const void *ptr; | |
58 | size_t off; | |
59 | size_t size; | |
60 | }; | |
61 | ||
62 | #define REC_SIZE sizeof(struct pstore_ftrace_record) | |
63 | ||
64 | static void *pstore_ftrace_seq_start(struct seq_file *s, loff_t *pos) | |
65 | { | |
66 | struct pstore_private *ps = s->private; | |
67 | struct pstore_ftrace_seq_data *data; | |
68 | ||
69 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
70 | if (!data) | |
71 | return NULL; | |
72 | ||
73 | data->off = ps->size % REC_SIZE; | |
74 | data->off += *pos * REC_SIZE; | |
75 | if (data->off + REC_SIZE > ps->size) { | |
76 | kfree(data); | |
77 | return NULL; | |
78 | } | |
79 | ||
80 | return data; | |
81 | ||
82 | } | |
83 | ||
84 | static void pstore_ftrace_seq_stop(struct seq_file *s, void *v) | |
85 | { | |
86 | kfree(v); | |
87 | } | |
88 | ||
89 | static void *pstore_ftrace_seq_next(struct seq_file *s, void *v, loff_t *pos) | |
90 | { | |
91 | struct pstore_private *ps = s->private; | |
92 | struct pstore_ftrace_seq_data *data = v; | |
93 | ||
94 | data->off += REC_SIZE; | |
95 | if (data->off + REC_SIZE > ps->size) | |
96 | return NULL; | |
97 | ||
98 | (*pos)++; | |
99 | return data; | |
100 | } | |
101 | ||
102 | static int pstore_ftrace_seq_show(struct seq_file *s, void *v) | |
103 | { | |
104 | struct pstore_private *ps = s->private; | |
105 | struct pstore_ftrace_seq_data *data = v; | |
106 | struct pstore_ftrace_record *rec = (void *)(ps->data + data->off); | |
107 | ||
108 | seq_printf(s, "%d %08lx %08lx %pf <- %pF\n", | |
109 | pstore_ftrace_decode_cpu(rec), rec->ip, rec->parent_ip, | |
110 | (void *)rec->ip, (void *)rec->parent_ip); | |
111 | ||
112 | return 0; | |
113 | } | |
114 | ||
115 | static const struct seq_operations pstore_ftrace_seq_ops = { | |
116 | .start = pstore_ftrace_seq_start, | |
117 | .next = pstore_ftrace_seq_next, | |
118 | .stop = pstore_ftrace_seq_stop, | |
119 | .show = pstore_ftrace_seq_show, | |
120 | }; | |
121 | ||
fbe0aa1f TL |
122 | static ssize_t pstore_file_read(struct file *file, char __user *userbuf, |
123 | size_t count, loff_t *ppos) | |
124 | { | |
060287b8 AV |
125 | struct seq_file *sf = file->private_data; |
126 | struct pstore_private *ps = sf->private; | |
fbe0aa1f | 127 | |
060287b8 AV |
128 | if (ps->type == PSTORE_TYPE_FTRACE) |
129 | return seq_read(file, userbuf, count, ppos); | |
fbe0aa1f TL |
130 | return simple_read_from_buffer(userbuf, count, ppos, ps->data, ps->size); |
131 | } | |
132 | ||
060287b8 AV |
133 | static int pstore_file_open(struct inode *inode, struct file *file) |
134 | { | |
135 | struct pstore_private *ps = inode->i_private; | |
136 | struct seq_file *sf; | |
137 | int err; | |
138 | const struct seq_operations *sops = NULL; | |
139 | ||
140 | if (ps->type == PSTORE_TYPE_FTRACE) | |
141 | sops = &pstore_ftrace_seq_ops; | |
142 | ||
143 | err = seq_open(file, sops); | |
144 | if (err < 0) | |
145 | return err; | |
146 | ||
147 | sf = file->private_data; | |
148 | sf->private = ps; | |
149 | ||
150 | return 0; | |
151 | } | |
152 | ||
153 | static loff_t pstore_file_llseek(struct file *file, loff_t off, int origin) | |
154 | { | |
155 | struct seq_file *sf = file->private_data; | |
156 | ||
157 | if (sf->op) | |
158 | return seq_lseek(file, off, origin); | |
159 | return default_llseek(file, off, origin); | |
160 | } | |
161 | ||
fbe0aa1f | 162 | static const struct file_operations pstore_file_operations = { |
060287b8 AV |
163 | .open = pstore_file_open, |
164 | .read = pstore_file_read, | |
165 | .llseek = pstore_file_llseek, | |
166 | .release = seq_release, | |
fbe0aa1f | 167 | }; |
ca01d6dd TL |
168 | |
169 | /* | |
170 | * When a file is unlinked from our file system we call the | |
171 | * platform driver to erase the record from persistent store. | |
172 | */ | |
173 | static int pstore_unlink(struct inode *dir, struct dentry *dentry) | |
174 | { | |
175 | struct pstore_private *p = dentry->d_inode->i_private; | |
176 | ||
2174f6df | 177 | if (p->psi->erase) |
a9efd39c SA |
178 | p->psi->erase(p->type, p->id, dentry->d_inode->i_ctime, |
179 | p->psi); | |
ca01d6dd TL |
180 | |
181 | return simple_unlink(dir, dentry); | |
182 | } | |
183 | ||
a872d510 TL |
184 | static void pstore_evict_inode(struct inode *inode) |
185 | { | |
6dda9266 TL |
186 | struct pstore_private *p = inode->i_private; |
187 | unsigned long flags; | |
188 | ||
dbd5768f | 189 | clear_inode(inode); |
6dda9266 TL |
190 | if (p) { |
191 | spin_lock_irqsave(&allpstore_lock, flags); | |
192 | list_del(&p->list); | |
193 | spin_unlock_irqrestore(&allpstore_lock, flags); | |
194 | kfree(p); | |
195 | } | |
a872d510 TL |
196 | } |
197 | ||
ca01d6dd TL |
198 | static const struct inode_operations pstore_dir_inode_operations = { |
199 | .lookup = simple_lookup, | |
200 | .unlink = pstore_unlink, | |
201 | }; | |
202 | ||
22a71c30 | 203 | static struct inode *pstore_get_inode(struct super_block *sb) |
fbe0aa1f TL |
204 | { |
205 | struct inode *inode = new_inode(sb); | |
fbe0aa1f TL |
206 | if (inode) { |
207 | inode->i_ino = get_next_ino(); | |
fbe0aa1f | 208 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
fbe0aa1f TL |
209 | } |
210 | return inode; | |
211 | } | |
212 | ||
366f7e7a TL |
213 | enum { |
214 | Opt_kmsg_bytes, Opt_err | |
215 | }; | |
216 | ||
217 | static const match_table_t tokens = { | |
218 | {Opt_kmsg_bytes, "kmsg_bytes=%u"}, | |
219 | {Opt_err, NULL} | |
220 | }; | |
221 | ||
222 | static void parse_options(char *options) | |
223 | { | |
224 | char *p; | |
225 | substring_t args[MAX_OPT_ARGS]; | |
226 | int option; | |
227 | ||
228 | if (!options) | |
229 | return; | |
230 | ||
231 | while ((p = strsep(&options, ",")) != NULL) { | |
232 | int token; | |
233 | ||
234 | if (!*p) | |
235 | continue; | |
236 | ||
237 | token = match_token(p, tokens, args); | |
238 | switch (token) { | |
239 | case Opt_kmsg_bytes: | |
240 | if (!match_int(&args[0], &option)) | |
241 | pstore_set_kmsg_bytes(option); | |
242 | break; | |
243 | } | |
244 | } | |
245 | } | |
246 | ||
247 | static int pstore_remount(struct super_block *sb, int *flags, char *data) | |
248 | { | |
249 | parse_options(data); | |
250 | ||
251 | return 0; | |
252 | } | |
253 | ||
ca01d6dd TL |
254 | static const struct super_operations pstore_ops = { |
255 | .statfs = simple_statfs, | |
256 | .drop_inode = generic_delete_inode, | |
a872d510 | 257 | .evict_inode = pstore_evict_inode, |
366f7e7a | 258 | .remount_fs = pstore_remount, |
ca01d6dd TL |
259 | .show_options = generic_show_options, |
260 | }; | |
261 | ||
262 | static struct super_block *pstore_sb; | |
ca01d6dd TL |
263 | |
264 | int pstore_is_mounted(void) | |
265 | { | |
fbe0aa1f | 266 | return pstore_sb != NULL; |
ca01d6dd TL |
267 | } |
268 | ||
269 | /* | |
270 | * Make a regular file in the root directory of our file system. | |
271 | * Load it up with "size" bytes of data from "buf". | |
272 | * Set the mtime & ctime to the date that this record was originally stored. | |
273 | */ | |
274 | int pstore_mkfile(enum pstore_type_id type, char *psname, u64 id, | |
638c1fd3 MG |
275 | char *data, size_t size, struct timespec time, |
276 | struct pstore_info *psi) | |
ca01d6dd TL |
277 | { |
278 | struct dentry *root = pstore_sb->s_root; | |
279 | struct dentry *dentry; | |
280 | struct inode *inode; | |
6dda9266 | 281 | int rc = 0; |
ca01d6dd | 282 | char name[PSTORE_NAMELEN]; |
6dda9266 TL |
283 | struct pstore_private *private, *pos; |
284 | unsigned long flags; | |
285 | ||
286 | spin_lock_irqsave(&allpstore_lock, flags); | |
287 | list_for_each_entry(pos, &allpstore, list) { | |
288 | if (pos->type == type && | |
289 | pos->id == id && | |
290 | pos->psi == psi) { | |
291 | rc = -EEXIST; | |
292 | break; | |
293 | } | |
294 | } | |
295 | spin_unlock_irqrestore(&allpstore_lock, flags); | |
296 | if (rc) | |
297 | return rc; | |
ca01d6dd TL |
298 | |
299 | rc = -ENOMEM; | |
22a71c30 | 300 | inode = pstore_get_inode(pstore_sb); |
ca01d6dd TL |
301 | if (!inode) |
302 | goto fail; | |
22a71c30 AV |
303 | inode->i_mode = S_IFREG | 0444; |
304 | inode->i_fop = &pstore_file_operations; | |
fbe0aa1f | 305 | private = kmalloc(sizeof *private + size, GFP_KERNEL); |
ca01d6dd TL |
306 | if (!private) |
307 | goto fail_alloc; | |
56280682 | 308 | private->type = type; |
ca01d6dd | 309 | private->id = id; |
638c1fd3 | 310 | private->psi = psi; |
ca01d6dd TL |
311 | |
312 | switch (type) { | |
313 | case PSTORE_TYPE_DMESG: | |
314 | sprintf(name, "dmesg-%s-%lld", psname, id); | |
315 | break; | |
f29e5956 AV |
316 | case PSTORE_TYPE_CONSOLE: |
317 | sprintf(name, "console-%s", psname); | |
318 | break; | |
060287b8 AV |
319 | case PSTORE_TYPE_FTRACE: |
320 | sprintf(name, "ftrace-%s", psname); | |
321 | break; | |
ca01d6dd TL |
322 | case PSTORE_TYPE_MCE: |
323 | sprintf(name, "mce-%s-%lld", psname, id); | |
324 | break; | |
325 | case PSTORE_TYPE_UNKNOWN: | |
326 | sprintf(name, "unknown-%s-%lld", psname, id); | |
327 | break; | |
328 | default: | |
329 | sprintf(name, "type%d-%s-%lld", type, psname, id); | |
330 | break; | |
331 | } | |
332 | ||
333 | mutex_lock(&root->d_inode->i_mutex); | |
334 | ||
335 | rc = -ENOSPC; | |
336 | dentry = d_alloc_name(root, name); | |
337 | if (IS_ERR(dentry)) | |
338 | goto fail_lockedalloc; | |
339 | ||
fbe0aa1f TL |
340 | memcpy(private->data, data, size); |
341 | inode->i_size = private->size = size; | |
ca01d6dd TL |
342 | |
343 | inode->i_private = private; | |
344 | ||
345 | if (time.tv_sec) | |
346 | inode->i_mtime = inode->i_ctime = time; | |
347 | ||
fbe0aa1f | 348 | d_add(dentry, inode); |
ca01d6dd | 349 | |
6dda9266 TL |
350 | spin_lock_irqsave(&allpstore_lock, flags); |
351 | list_add(&private->list, &allpstore); | |
352 | spin_unlock_irqrestore(&allpstore_lock, flags); | |
353 | ||
ca01d6dd | 354 | mutex_unlock(&root->d_inode->i_mutex); |
fbe0aa1f TL |
355 | |
356 | return 0; | |
ca01d6dd TL |
357 | |
358 | fail_lockedalloc: | |
359 | mutex_unlock(&root->d_inode->i_mutex); | |
360 | kfree(private); | |
361 | fail_alloc: | |
362 | iput(inode); | |
363 | ||
364 | fail: | |
365 | return rc; | |
366 | } | |
367 | ||
364ed2f4 | 368 | static int pstore_fill_super(struct super_block *sb, void *data, int silent) |
ca01d6dd | 369 | { |
318ceed0 | 370 | struct inode *inode; |
ca01d6dd TL |
371 | |
372 | save_mount_options(sb, data); | |
373 | ||
374 | pstore_sb = sb; | |
375 | ||
376 | sb->s_maxbytes = MAX_LFS_FILESIZE; | |
377 | sb->s_blocksize = PAGE_CACHE_SIZE; | |
378 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; | |
379 | sb->s_magic = PSTOREFS_MAGIC; | |
380 | sb->s_op = &pstore_ops; | |
381 | sb->s_time_gran = 1; | |
382 | ||
366f7e7a TL |
383 | parse_options(data); |
384 | ||
22a71c30 | 385 | inode = pstore_get_inode(sb); |
318ceed0 | 386 | if (inode) { |
22a71c30 | 387 | inode->i_mode = S_IFDIR | 0755; |
318ceed0 | 388 | inode->i_op = &pstore_dir_inode_operations; |
22a71c30 AV |
389 | inode->i_fop = &simple_dir_operations; |
390 | inc_nlink(inode); | |
ca01d6dd | 391 | } |
318ceed0 AV |
392 | sb->s_root = d_make_root(inode); |
393 | if (!sb->s_root) | |
394 | return -ENOMEM; | |
ca01d6dd | 395 | |
6dda9266 | 396 | pstore_get_records(0); |
ca01d6dd TL |
397 | |
398 | return 0; | |
ca01d6dd TL |
399 | } |
400 | ||
fbe0aa1f TL |
401 | static struct dentry *pstore_mount(struct file_system_type *fs_type, |
402 | int flags, const char *dev_name, void *data) | |
ca01d6dd | 403 | { |
fbe0aa1f | 404 | return mount_single(fs_type, flags, data, pstore_fill_super); |
ca01d6dd TL |
405 | } |
406 | ||
407 | static void pstore_kill_sb(struct super_block *sb) | |
408 | { | |
409 | kill_litter_super(sb); | |
410 | pstore_sb = NULL; | |
ca01d6dd TL |
411 | } |
412 | ||
413 | static struct file_system_type pstore_fs_type = { | |
414 | .name = "pstore", | |
fbe0aa1f | 415 | .mount = pstore_mount, |
ca01d6dd TL |
416 | .kill_sb = pstore_kill_sb, |
417 | }; | |
418 | ||
419 | static int __init init_pstore_fs(void) | |
420 | { | |
366f7e7a | 421 | return register_filesystem(&pstore_fs_type); |
ca01d6dd TL |
422 | } |
423 | module_init(init_pstore_fs) | |
424 | ||
425 | MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>"); | |
426 | MODULE_LICENSE("GPL"); |