inode.c 11 KB

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