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