inode.c 11 KB

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