inode.c 12 KB

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