inode.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. data->off += REC_SIZE;
  87. if (data->off + REC_SIZE > ps->total_size)
  88. return NULL;
  89. (*pos)++;
  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. rec = (struct pstore_ftrace_record *)(ps->record->buf + data->off);
  98. seq_printf(s, "CPU:%d ts:%llu %08lx %08lx %pf <- %pF\n",
  99. pstore_ftrace_decode_cpu(rec),
  100. pstore_ftrace_read_timestamp(rec),
  101. rec->ip, rec->parent_ip, (void *)rec->ip,
  102. (void *)rec->parent_ip);
  103. return 0;
  104. }
  105. static const struct seq_operations pstore_ftrace_seq_ops = {
  106. .start = pstore_ftrace_seq_start,
  107. .next = pstore_ftrace_seq_next,
  108. .stop = pstore_ftrace_seq_stop,
  109. .show = pstore_ftrace_seq_show,
  110. };
  111. static ssize_t pstore_file_read(struct file *file, char __user *userbuf,
  112. size_t count, loff_t *ppos)
  113. {
  114. struct seq_file *sf = file->private_data;
  115. struct pstore_private *ps = sf->private;
  116. if (ps->record->type == PSTORE_TYPE_FTRACE)
  117. return seq_read(file, userbuf, count, ppos);
  118. return simple_read_from_buffer(userbuf, count, ppos,
  119. ps->record->buf, ps->total_size);
  120. }
  121. static int pstore_file_open(struct inode *inode, struct file *file)
  122. {
  123. struct pstore_private *ps = inode->i_private;
  124. struct seq_file *sf;
  125. int err;
  126. const struct seq_operations *sops = NULL;
  127. if (ps->record->type == PSTORE_TYPE_FTRACE)
  128. sops = &pstore_ftrace_seq_ops;
  129. err = seq_open(file, sops);
  130. if (err < 0)
  131. return err;
  132. sf = file->private_data;
  133. sf->private = ps;
  134. return 0;
  135. }
  136. static loff_t pstore_file_llseek(struct file *file, loff_t off, int whence)
  137. {
  138. struct seq_file *sf = file->private_data;
  139. if (sf->op)
  140. return seq_lseek(file, off, whence);
  141. return default_llseek(file, off, whence);
  142. }
  143. static const struct file_operations pstore_file_operations = {
  144. .open = pstore_file_open,
  145. .read = pstore_file_read,
  146. .llseek = pstore_file_llseek,
  147. .release = seq_release,
  148. };
  149. /*
  150. * When a file is unlinked from our file system we call the
  151. * platform driver to erase the record from persistent store.
  152. */
  153. static int pstore_unlink(struct inode *dir, struct dentry *dentry)
  154. {
  155. struct pstore_private *p = d_inode(dentry)->i_private;
  156. struct pstore_record *record = p->record;
  157. if (!record->psi->erase)
  158. return -EPERM;
  159. mutex_lock(&record->psi->read_mutex);
  160. record->psi->erase(record);
  161. mutex_unlock(&record->psi->read_mutex);
  162. return simple_unlink(dir, dentry);
  163. }
  164. static void pstore_evict_inode(struct inode *inode)
  165. {
  166. struct pstore_private *p = inode->i_private;
  167. unsigned long flags;
  168. clear_inode(inode);
  169. if (p) {
  170. spin_lock_irqsave(&allpstore_lock, flags);
  171. list_del(&p->list);
  172. spin_unlock_irqrestore(&allpstore_lock, flags);
  173. free_pstore_private(p);
  174. }
  175. }
  176. static const struct inode_operations pstore_dir_inode_operations = {
  177. .lookup = simple_lookup,
  178. .unlink = pstore_unlink,
  179. };
  180. static struct inode *pstore_get_inode(struct super_block *sb)
  181. {
  182. struct inode *inode = new_inode(sb);
  183. if (inode) {
  184. inode->i_ino = get_next_ino();
  185. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  186. }
  187. return inode;
  188. }
  189. enum {
  190. Opt_kmsg_bytes, Opt_err
  191. };
  192. static const match_table_t tokens = {
  193. {Opt_kmsg_bytes, "kmsg_bytes=%u"},
  194. {Opt_err, NULL}
  195. };
  196. static void parse_options(char *options)
  197. {
  198. char *p;
  199. substring_t args[MAX_OPT_ARGS];
  200. int option;
  201. if (!options)
  202. return;
  203. while ((p = strsep(&options, ",")) != NULL) {
  204. int token;
  205. if (!*p)
  206. continue;
  207. token = match_token(p, tokens, args);
  208. switch (token) {
  209. case Opt_kmsg_bytes:
  210. if (!match_int(&args[0], &option))
  211. pstore_set_kmsg_bytes(option);
  212. break;
  213. }
  214. }
  215. }
  216. /*
  217. * Display the mount options in /proc/mounts.
  218. */
  219. static int pstore_show_options(struct seq_file *m, struct dentry *root)
  220. {
  221. if (kmsg_bytes != PSTORE_DEFAULT_KMSG_BYTES)
  222. seq_printf(m, ",kmsg_bytes=%lu", kmsg_bytes);
  223. return 0;
  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 = pstore_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(struct dentry *root, struct pstore_record *record)
  249. {
  250. struct dentry *dentry;
  251. struct inode *inode;
  252. int rc = 0;
  253. char name[PSTORE_NAMELEN];
  254. struct pstore_private *private, *pos;
  255. unsigned long flags;
  256. size_t size = record->size + record->ecc_notice_size;
  257. WARN_ON(!inode_is_locked(d_inode(root)));
  258. spin_lock_irqsave(&allpstore_lock, flags);
  259. list_for_each_entry(pos, &allpstore, list) {
  260. if (pos->record->type == record->type &&
  261. pos->record->id == record->id &&
  262. pos->record->psi == record->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(root->d_sb);
  272. if (!inode)
  273. goto fail;
  274. inode->i_mode = S_IFREG | 0444;
  275. inode->i_fop = &pstore_file_operations;
  276. switch (record->type) {
  277. case PSTORE_TYPE_DMESG:
  278. scnprintf(name, sizeof(name), "dmesg-%s-%llu%s",
  279. record->psi->name, record->id,
  280. record->compressed ? ".enc.z" : "");
  281. break;
  282. case PSTORE_TYPE_CONSOLE:
  283. scnprintf(name, sizeof(name), "console-%s-%llu",
  284. record->psi->name, record->id);
  285. break;
  286. case PSTORE_TYPE_FTRACE:
  287. scnprintf(name, sizeof(name), "ftrace-%s-%llu",
  288. record->psi->name, record->id);
  289. break;
  290. case PSTORE_TYPE_MCE:
  291. scnprintf(name, sizeof(name), "mce-%s-%llu",
  292. record->psi->name, record->id);
  293. break;
  294. case PSTORE_TYPE_PPC_RTAS:
  295. scnprintf(name, sizeof(name), "rtas-%s-%llu",
  296. record->psi->name, record->id);
  297. break;
  298. case PSTORE_TYPE_PPC_OF:
  299. scnprintf(name, sizeof(name), "powerpc-ofw-%s-%llu",
  300. record->psi->name, record->id);
  301. break;
  302. case PSTORE_TYPE_PPC_COMMON:
  303. scnprintf(name, sizeof(name), "powerpc-common-%s-%llu",
  304. record->psi->name, record->id);
  305. break;
  306. case PSTORE_TYPE_PMSG:
  307. scnprintf(name, sizeof(name), "pmsg-%s-%llu",
  308. record->psi->name, record->id);
  309. break;
  310. case PSTORE_TYPE_PPC_OPAL:
  311. scnprintf(name, sizeof(name), "powerpc-opal-%s-%llu",
  312. record->psi->name, record->id);
  313. break;
  314. case PSTORE_TYPE_UNKNOWN:
  315. scnprintf(name, sizeof(name), "unknown-%s-%llu",
  316. record->psi->name, record->id);
  317. break;
  318. default:
  319. scnprintf(name, sizeof(name), "type%d-%s-%llu",
  320. record->type, record->psi->name, record->id);
  321. break;
  322. }
  323. private = kzalloc(sizeof(*private), GFP_KERNEL);
  324. if (!private)
  325. goto fail_inode;
  326. dentry = d_alloc_name(root, name);
  327. if (!dentry)
  328. goto fail_private;
  329. private->record = record;
  330. inode->i_size = private->total_size = size;
  331. inode->i_private = private;
  332. if (record->time.tv_sec)
  333. inode->i_mtime = inode->i_ctime = record->time;
  334. d_add(dentry, inode);
  335. spin_lock_irqsave(&allpstore_lock, flags);
  336. list_add(&private->list, &allpstore);
  337. spin_unlock_irqrestore(&allpstore_lock, flags);
  338. return 0;
  339. fail_private:
  340. free_pstore_private(private);
  341. fail_inode:
  342. iput(inode);
  343. fail:
  344. return rc;
  345. }
  346. /*
  347. * Read all the records from the persistent store. Create
  348. * files in our filesystem. Don't warn about -EEXIST errors
  349. * when we are re-scanning the backing store looking to add new
  350. * error records.
  351. */
  352. void pstore_get_records(int quiet)
  353. {
  354. struct pstore_info *psi = psinfo;
  355. struct dentry *root;
  356. if (!psi || !pstore_sb)
  357. return;
  358. root = pstore_sb->s_root;
  359. inode_lock(d_inode(root));
  360. pstore_get_backend_records(psi, root, quiet);
  361. inode_unlock(d_inode(root));
  362. }
  363. static int pstore_fill_super(struct super_block *sb, void *data, int silent)
  364. {
  365. struct inode *inode;
  366. pstore_sb = sb;
  367. sb->s_maxbytes = MAX_LFS_FILESIZE;
  368. sb->s_blocksize = PAGE_SIZE;
  369. sb->s_blocksize_bits = PAGE_SHIFT;
  370. sb->s_magic = PSTOREFS_MAGIC;
  371. sb->s_op = &pstore_ops;
  372. sb->s_time_gran = 1;
  373. parse_options(data);
  374. inode = pstore_get_inode(sb);
  375. if (inode) {
  376. inode->i_mode = S_IFDIR | 0750;
  377. inode->i_op = &pstore_dir_inode_operations;
  378. inode->i_fop = &simple_dir_operations;
  379. inc_nlink(inode);
  380. }
  381. sb->s_root = d_make_root(inode);
  382. if (!sb->s_root)
  383. return -ENOMEM;
  384. pstore_get_records(0);
  385. return 0;
  386. }
  387. static struct dentry *pstore_mount(struct file_system_type *fs_type,
  388. int flags, const char *dev_name, void *data)
  389. {
  390. return mount_single(fs_type, flags, data, pstore_fill_super);
  391. }
  392. static void pstore_kill_sb(struct super_block *sb)
  393. {
  394. kill_litter_super(sb);
  395. pstore_sb = NULL;
  396. }
  397. static struct file_system_type pstore_fs_type = {
  398. .owner = THIS_MODULE,
  399. .name = "pstore",
  400. .mount = pstore_mount,
  401. .kill_sb = pstore_kill_sb,
  402. };
  403. int __init pstore_init_fs(void)
  404. {
  405. int err;
  406. /* Create a convenient mount point for people to access pstore */
  407. err = sysfs_create_mount_point(fs_kobj, "pstore");
  408. if (err)
  409. goto out;
  410. err = register_filesystem(&pstore_fs_type);
  411. if (err < 0)
  412. sysfs_remove_mount_point(fs_kobj, "pstore");
  413. out:
  414. return err;
  415. }
  416. void __exit pstore_exit_fs(void)
  417. {
  418. unregister_filesystem(&pstore_fs_type);
  419. sysfs_remove_mount_point(fs_kobj, "pstore");
  420. }