inode.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. #include <linux/module.h>
  20. #include <linux/fs.h>
  21. #include <linux/fsnotify.h>
  22. #include <linux/pagemap.h>
  23. #include <linux/highmem.h>
  24. #include <linux/time.h>
  25. #include <linux/init.h>
  26. #include <linux/list.h>
  27. #include <linux/string.h>
  28. #include <linux/mount.h>
  29. #include <linux/seq_file.h>
  30. #include <linux/ramfs.h>
  31. #include <linux/parser.h>
  32. #include <linux/sched.h>
  33. #include <linux/magic.h>
  34. #include <linux/pstore.h>
  35. #include <linux/slab.h>
  36. #include <linux/spinlock.h>
  37. #include <linux/uaccess.h>
  38. #include "internal.h"
  39. #define PSTORE_NAMELEN 64
  40. static DEFINE_SPINLOCK(allpstore_lock);
  41. static LIST_HEAD(allpstore);
  42. struct pstore_private {
  43. struct list_head list;
  44. struct pstore_info *psi;
  45. enum pstore_type_id type;
  46. u64 id;
  47. int count;
  48. ssize_t size;
  49. char data[];
  50. };
  51. struct pstore_ftrace_seq_data {
  52. const void *ptr;
  53. size_t off;
  54. size_t size;
  55. };
  56. #define REC_SIZE sizeof(struct pstore_ftrace_record)
  57. static void *pstore_ftrace_seq_start(struct seq_file *s, loff_t *pos)
  58. {
  59. struct pstore_private *ps = s->private;
  60. struct pstore_ftrace_seq_data *data;
  61. data = kzalloc(sizeof(*data), GFP_KERNEL);
  62. if (!data)
  63. return NULL;
  64. data->off = ps->size % REC_SIZE;
  65. data->off += *pos * REC_SIZE;
  66. if (data->off + REC_SIZE > ps->size) {
  67. kfree(data);
  68. return NULL;
  69. }
  70. return data;
  71. }
  72. static void pstore_ftrace_seq_stop(struct seq_file *s, void *v)
  73. {
  74. kfree(v);
  75. }
  76. static void *pstore_ftrace_seq_next(struct seq_file *s, void *v, loff_t *pos)
  77. {
  78. struct pstore_private *ps = s->private;
  79. struct pstore_ftrace_seq_data *data = v;
  80. data->off += REC_SIZE;
  81. if (data->off + REC_SIZE > ps->size)
  82. return NULL;
  83. (*pos)++;
  84. return data;
  85. }
  86. static int pstore_ftrace_seq_show(struct seq_file *s, void *v)
  87. {
  88. struct pstore_private *ps = s->private;
  89. struct pstore_ftrace_seq_data *data = v;
  90. struct pstore_ftrace_record *rec = (void *)(ps->data + data->off);
  91. seq_printf(s, "%d %08lx %08lx %pf <- %pF\n",
  92. pstore_ftrace_decode_cpu(rec), rec->ip, rec->parent_ip,
  93. (void *)rec->ip, (void *)rec->parent_ip);
  94. return 0;
  95. }
  96. static const struct seq_operations pstore_ftrace_seq_ops = {
  97. .start = pstore_ftrace_seq_start,
  98. .next = pstore_ftrace_seq_next,
  99. .stop = pstore_ftrace_seq_stop,
  100. .show = pstore_ftrace_seq_show,
  101. };
  102. static ssize_t pstore_file_read(struct file *file, char __user *userbuf,
  103. size_t count, loff_t *ppos)
  104. {
  105. struct seq_file *sf = file->private_data;
  106. struct pstore_private *ps = sf->private;
  107. if (ps->type == PSTORE_TYPE_FTRACE)
  108. return seq_read(file, userbuf, count, ppos);
  109. return simple_read_from_buffer(userbuf, count, ppos, ps->data, ps->size);
  110. }
  111. static int pstore_file_open(struct inode *inode, struct file *file)
  112. {
  113. struct pstore_private *ps = inode->i_private;
  114. struct seq_file *sf;
  115. int err;
  116. const struct seq_operations *sops = NULL;
  117. if (ps->type == PSTORE_TYPE_FTRACE)
  118. sops = &pstore_ftrace_seq_ops;
  119. err = seq_open(file, sops);
  120. if (err < 0)
  121. return err;
  122. sf = file->private_data;
  123. sf->private = ps;
  124. return 0;
  125. }
  126. static loff_t pstore_file_llseek(struct file *file, loff_t off, int whence)
  127. {
  128. struct seq_file *sf = file->private_data;
  129. if (sf->op)
  130. return seq_lseek(file, off, whence);
  131. return default_llseek(file, off, whence);
  132. }
  133. static const struct file_operations pstore_file_operations = {
  134. .open = pstore_file_open,
  135. .read = pstore_file_read,
  136. .llseek = pstore_file_llseek,
  137. .release = seq_release,
  138. };
  139. /*
  140. * When a file is unlinked from our file system we call the
  141. * platform driver to erase the record from persistent store.
  142. */
  143. static int pstore_unlink(struct inode *dir, struct dentry *dentry)
  144. {
  145. struct pstore_private *p = dentry->d_inode->i_private;
  146. if (p->psi->erase)
  147. p->psi->erase(p->type, p->id, p->count,
  148. dentry->d_inode->i_ctime, p->psi);
  149. return simple_unlink(dir, dentry);
  150. }
  151. static void pstore_evict_inode(struct inode *inode)
  152. {
  153. struct pstore_private *p = inode->i_private;
  154. unsigned long flags;
  155. clear_inode(inode);
  156. if (p) {
  157. spin_lock_irqsave(&allpstore_lock, flags);
  158. list_del(&p->list);
  159. spin_unlock_irqrestore(&allpstore_lock, flags);
  160. kfree(p);
  161. }
  162. }
  163. static const struct inode_operations pstore_dir_inode_operations = {
  164. .lookup = simple_lookup,
  165. .unlink = pstore_unlink,
  166. };
  167. static struct inode *pstore_get_inode(struct super_block *sb)
  168. {
  169. struct inode *inode = new_inode(sb);
  170. if (inode) {
  171. inode->i_ino = get_next_ino();
  172. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  173. }
  174. return inode;
  175. }
  176. enum {
  177. Opt_kmsg_bytes, Opt_err
  178. };
  179. static const match_table_t tokens = {
  180. {Opt_kmsg_bytes, "kmsg_bytes=%u"},
  181. {Opt_err, NULL}
  182. };
  183. static void parse_options(char *options)
  184. {
  185. char *p;
  186. substring_t args[MAX_OPT_ARGS];
  187. int option;
  188. if (!options)
  189. return;
  190. while ((p = strsep(&options, ",")) != NULL) {
  191. int token;
  192. if (!*p)
  193. continue;
  194. token = match_token(p, tokens, args);
  195. switch (token) {
  196. case Opt_kmsg_bytes:
  197. if (!match_int(&args[0], &option))
  198. pstore_set_kmsg_bytes(option);
  199. break;
  200. }
  201. }
  202. }
  203. static int pstore_remount(struct super_block *sb, int *flags, char *data)
  204. {
  205. parse_options(data);
  206. return 0;
  207. }
  208. static const struct super_operations pstore_ops = {
  209. .statfs = simple_statfs,
  210. .drop_inode = generic_delete_inode,
  211. .evict_inode = pstore_evict_inode,
  212. .remount_fs = pstore_remount,
  213. .show_options = generic_show_options,
  214. };
  215. static struct super_block *pstore_sb;
  216. int pstore_is_mounted(void)
  217. {
  218. return pstore_sb != NULL;
  219. }
  220. /*
  221. * Make a regular file in the root directory of our file system.
  222. * Load it up with "size" bytes of data from "buf".
  223. * Set the mtime & ctime to the date that this record was originally stored.
  224. */
  225. int pstore_mkfile(enum pstore_type_id type, char *psname, u64 id, int count,
  226. char *data, size_t size, struct timespec time,
  227. struct pstore_info *psi)
  228. {
  229. struct dentry *root = pstore_sb->s_root;
  230. struct dentry *dentry;
  231. struct inode *inode;
  232. int rc = 0;
  233. char name[PSTORE_NAMELEN];
  234. struct pstore_private *private, *pos;
  235. unsigned long flags;
  236. spin_lock_irqsave(&allpstore_lock, flags);
  237. list_for_each_entry(pos, &allpstore, list) {
  238. if (pos->type == type &&
  239. pos->id == id &&
  240. pos->psi == psi) {
  241. rc = -EEXIST;
  242. break;
  243. }
  244. }
  245. spin_unlock_irqrestore(&allpstore_lock, flags);
  246. if (rc)
  247. return rc;
  248. rc = -ENOMEM;
  249. inode = pstore_get_inode(pstore_sb);
  250. if (!inode)
  251. goto fail;
  252. inode->i_mode = S_IFREG | 0444;
  253. inode->i_fop = &pstore_file_operations;
  254. private = kmalloc(sizeof *private + size, GFP_KERNEL);
  255. if (!private)
  256. goto fail_alloc;
  257. private->type = type;
  258. private->id = id;
  259. private->count = count;
  260. private->psi = psi;
  261. switch (type) {
  262. case PSTORE_TYPE_DMESG:
  263. sprintf(name, "dmesg-%s-%lld", psname, id);
  264. break;
  265. case PSTORE_TYPE_CONSOLE:
  266. sprintf(name, "console-%s", psname);
  267. break;
  268. case PSTORE_TYPE_FTRACE:
  269. sprintf(name, "ftrace-%s", psname);
  270. break;
  271. case PSTORE_TYPE_MCE:
  272. sprintf(name, "mce-%s-%lld", psname, id);
  273. break;
  274. case PSTORE_TYPE_UNKNOWN:
  275. sprintf(name, "unknown-%s-%lld", psname, id);
  276. break;
  277. default:
  278. sprintf(name, "type%d-%s-%lld", type, psname, id);
  279. break;
  280. }
  281. mutex_lock(&root->d_inode->i_mutex);
  282. rc = -ENOSPC;
  283. dentry = d_alloc_name(root, name);
  284. if (IS_ERR(dentry))
  285. goto fail_lockedalloc;
  286. memcpy(private->data, data, size);
  287. inode->i_size = private->size = size;
  288. inode->i_private = private;
  289. if (time.tv_sec)
  290. inode->i_mtime = inode->i_ctime = time;
  291. d_add(dentry, inode);
  292. spin_lock_irqsave(&allpstore_lock, flags);
  293. list_add(&private->list, &allpstore);
  294. spin_unlock_irqrestore(&allpstore_lock, flags);
  295. mutex_unlock(&root->d_inode->i_mutex);
  296. return 0;
  297. fail_lockedalloc:
  298. mutex_unlock(&root->d_inode->i_mutex);
  299. kfree(private);
  300. fail_alloc:
  301. iput(inode);
  302. fail:
  303. return rc;
  304. }
  305. static int pstore_fill_super(struct super_block *sb, void *data, int silent)
  306. {
  307. struct inode *inode;
  308. save_mount_options(sb, data);
  309. pstore_sb = sb;
  310. sb->s_maxbytes = MAX_LFS_FILESIZE;
  311. sb->s_blocksize = PAGE_CACHE_SIZE;
  312. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  313. sb->s_magic = PSTOREFS_MAGIC;
  314. sb->s_op = &pstore_ops;
  315. sb->s_time_gran = 1;
  316. parse_options(data);
  317. inode = pstore_get_inode(sb);
  318. if (inode) {
  319. inode->i_mode = S_IFDIR | 0755;
  320. inode->i_op = &pstore_dir_inode_operations;
  321. inode->i_fop = &simple_dir_operations;
  322. inc_nlink(inode);
  323. }
  324. sb->s_root = d_make_root(inode);
  325. if (!sb->s_root)
  326. return -ENOMEM;
  327. pstore_get_records(0);
  328. return 0;
  329. }
  330. static struct dentry *pstore_mount(struct file_system_type *fs_type,
  331. int flags, const char *dev_name, void *data)
  332. {
  333. return mount_single(fs_type, flags, data, pstore_fill_super);
  334. }
  335. static void pstore_kill_sb(struct super_block *sb)
  336. {
  337. kill_litter_super(sb);
  338. pstore_sb = NULL;
  339. }
  340. static struct file_system_type pstore_fs_type = {
  341. .name = "pstore",
  342. .mount = pstore_mount,
  343. .kill_sb = pstore_kill_sb,
  344. };
  345. static struct kobject *pstore_kobj;
  346. static int __init init_pstore_fs(void)
  347. {
  348. int err = 0;
  349. /* Create a convenient mount point for people to access pstore */
  350. pstore_kobj = kobject_create_and_add("pstore", fs_kobj);
  351. if (!pstore_kobj) {
  352. err = -ENOMEM;
  353. goto out;
  354. }
  355. err = register_filesystem(&pstore_fs_type);
  356. if (err < 0)
  357. kobject_put(pstore_kobj);
  358. out:
  359. return err;
  360. }
  361. module_init(init_pstore_fs)
  362. MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>");
  363. MODULE_LICENSE("GPL");