inode.c 9.4 KB

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