inode.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Persistent Storage - ramfs parts.
  4. *
  5. * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/fs.h>
  9. #include <linux/fsnotify.h>
  10. #include <linux/pagemap.h>
  11. #include <linux/highmem.h>
  12. #include <linux/time.h>
  13. #include <linux/init.h>
  14. #include <linux/list.h>
  15. #include <linux/string.h>
  16. #include <linux/mount.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/ramfs.h>
  19. #include <linux/parser.h>
  20. #include <linux/sched.h>
  21. #include <linux/magic.h>
  22. #include <linux/pstore.h>
  23. #include <linux/slab.h>
  24. #include <linux/uaccess.h>
  25. #include "internal.h"
  26. #define PSTORE_NAMELEN 64
  27. static DEFINE_MUTEX(records_list_lock);
  28. static LIST_HEAD(records_list);
  29. static DEFINE_MUTEX(pstore_sb_lock);
  30. static struct super_block *pstore_sb;
  31. struct pstore_private {
  32. struct list_head list;
  33. struct dentry *dentry;
  34. struct pstore_record *record;
  35. size_t total_size;
  36. };
  37. struct pstore_ftrace_seq_data {
  38. const void *ptr;
  39. size_t off;
  40. size_t size;
  41. };
  42. #define REC_SIZE sizeof(struct pstore_ftrace_record)
  43. static void free_pstore_private(struct pstore_private *private)
  44. {
  45. if (!private)
  46. return;
  47. if (private->record) {
  48. kfree(private->record->buf);
  49. kfree(private->record);
  50. }
  51. kfree(private);
  52. }
  53. static void *pstore_ftrace_seq_start(struct seq_file *s, loff_t *pos)
  54. {
  55. struct pstore_private *ps = s->private;
  56. struct pstore_ftrace_seq_data *data;
  57. data = kzalloc(sizeof(*data), GFP_KERNEL);
  58. if (!data)
  59. return NULL;
  60. data->off = ps->total_size % REC_SIZE;
  61. data->off += *pos * REC_SIZE;
  62. if (data->off + REC_SIZE > ps->total_size) {
  63. kfree(data);
  64. return NULL;
  65. }
  66. return data;
  67. }
  68. static void pstore_ftrace_seq_stop(struct seq_file *s, void *v)
  69. {
  70. kfree(v);
  71. }
  72. static void *pstore_ftrace_seq_next(struct seq_file *s, void *v, loff_t *pos)
  73. {
  74. struct pstore_private *ps = s->private;
  75. struct pstore_ftrace_seq_data *data = v;
  76. (*pos)++;
  77. data->off += REC_SIZE;
  78. if (data->off + REC_SIZE > ps->total_size)
  79. return NULL;
  80. return data;
  81. }
  82. static int pstore_ftrace_seq_show(struct seq_file *s, void *v)
  83. {
  84. struct pstore_private *ps = s->private;
  85. struct pstore_ftrace_seq_data *data = v;
  86. struct pstore_ftrace_record *rec;
  87. if (!data)
  88. return 0;
  89. rec = (struct pstore_ftrace_record *)(ps->record->buf + data->off);
  90. seq_printf(s, "CPU:%d ts:%llu %08lx %08lx %ps <- %pS\n",
  91. pstore_ftrace_decode_cpu(rec),
  92. pstore_ftrace_read_timestamp(rec),
  93. rec->ip, rec->parent_ip, (void *)rec->ip,
  94. (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 ssize_t pstore_file_read(struct file *file, char __user *userbuf,
  104. size_t count, loff_t *ppos)
  105. {
  106. struct seq_file *sf = file->private_data;
  107. struct pstore_private *ps = sf->private;
  108. if (ps->record->type == PSTORE_TYPE_FTRACE)
  109. return seq_read(file, userbuf, count, ppos);
  110. return simple_read_from_buffer(userbuf, count, ppos,
  111. ps->record->buf, ps->total_size);
  112. }
  113. static int pstore_file_open(struct inode *inode, struct file *file)
  114. {
  115. struct pstore_private *ps = inode->i_private;
  116. struct seq_file *sf;
  117. int err;
  118. const struct seq_operations *sops = NULL;
  119. if (ps->record->type == PSTORE_TYPE_FTRACE)
  120. sops = &pstore_ftrace_seq_ops;
  121. err = seq_open(file, sops);
  122. if (err < 0)
  123. return err;
  124. sf = file->private_data;
  125. sf->private = ps;
  126. return 0;
  127. }
  128. static loff_t pstore_file_llseek(struct file *file, loff_t off, int whence)
  129. {
  130. struct seq_file *sf = file->private_data;
  131. if (sf->op)
  132. return seq_lseek(file, off, whence);
  133. return default_llseek(file, off, whence);
  134. }
  135. static const struct file_operations pstore_file_operations = {
  136. .open = pstore_file_open,
  137. .read = pstore_file_read,
  138. .llseek = pstore_file_llseek,
  139. .release = seq_release,
  140. };
  141. /*
  142. * When a file is unlinked from our file system we call the
  143. * platform driver to erase the record from persistent store.
  144. */
  145. static int pstore_unlink(struct inode *dir, struct dentry *dentry)
  146. {
  147. struct pstore_private *p = d_inode(dentry)->i_private;
  148. struct pstore_record *record = p->record;
  149. int rc = 0;
  150. if (!record->psi->erase)
  151. return -EPERM;
  152. /* Make sure we can't race while removing this file. */
  153. mutex_lock(&records_list_lock);
  154. if (!list_empty(&p->list))
  155. list_del_init(&p->list);
  156. else
  157. rc = -ENOENT;
  158. p->dentry = NULL;
  159. mutex_unlock(&records_list_lock);
  160. if (rc)
  161. return rc;
  162. mutex_lock(&record->psi->read_mutex);
  163. record->psi->erase(record);
  164. mutex_unlock(&record->psi->read_mutex);
  165. return simple_unlink(dir, dentry);
  166. }
  167. static void pstore_evict_inode(struct inode *inode)
  168. {
  169. struct pstore_private *p = inode->i_private;
  170. clear_inode(inode);
  171. free_pstore_private(p);
  172. }
  173. static const struct inode_operations pstore_dir_inode_operations = {
  174. .lookup = simple_lookup,
  175. .unlink = pstore_unlink,
  176. };
  177. static struct inode *pstore_get_inode(struct super_block *sb)
  178. {
  179. struct inode *inode = new_inode(sb);
  180. if (inode) {
  181. inode->i_ino = get_next_ino();
  182. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  183. }
  184. return inode;
  185. }
  186. enum {
  187. Opt_kmsg_bytes, Opt_err
  188. };
  189. static const match_table_t tokens = {
  190. {Opt_kmsg_bytes, "kmsg_bytes=%u"},
  191. {Opt_err, NULL}
  192. };
  193. static void parse_options(char *options)
  194. {
  195. char *p;
  196. substring_t args[MAX_OPT_ARGS];
  197. int option;
  198. if (!options)
  199. return;
  200. while ((p = strsep(&options, ",")) != NULL) {
  201. int token;
  202. if (!*p)
  203. continue;
  204. token = match_token(p, tokens, args);
  205. switch (token) {
  206. case Opt_kmsg_bytes:
  207. if (!match_int(&args[0], &option))
  208. pstore_set_kmsg_bytes(option);
  209. break;
  210. }
  211. }
  212. }
  213. /*
  214. * Display the mount options in /proc/mounts.
  215. */
  216. static int pstore_show_options(struct seq_file *m, struct dentry *root)
  217. {
  218. if (kmsg_bytes != PSTORE_DEFAULT_KMSG_BYTES)
  219. seq_printf(m, ",kmsg_bytes=%lu", kmsg_bytes);
  220. return 0;
  221. }
  222. static int pstore_remount(struct super_block *sb, int *flags, char *data)
  223. {
  224. sync_filesystem(sb);
  225. parse_options(data);
  226. return 0;
  227. }
  228. static const struct super_operations pstore_ops = {
  229. .statfs = simple_statfs,
  230. .drop_inode = generic_delete_inode,
  231. .evict_inode = pstore_evict_inode,
  232. .remount_fs = pstore_remount,
  233. .show_options = pstore_show_options,
  234. };
  235. static struct dentry *psinfo_lock_root(void)
  236. {
  237. struct dentry *root;
  238. mutex_lock(&pstore_sb_lock);
  239. /*
  240. * Having no backend is fine -- no records appear.
  241. * Not being mounted is fine -- nothing to do.
  242. */
  243. if (!psinfo || !pstore_sb) {
  244. mutex_unlock(&pstore_sb_lock);
  245. return NULL;
  246. }
  247. root = pstore_sb->s_root;
  248. inode_lock(d_inode(root));
  249. mutex_unlock(&pstore_sb_lock);
  250. return root;
  251. }
  252. int pstore_put_backend_records(struct pstore_info *psi)
  253. {
  254. struct pstore_private *pos, *tmp;
  255. struct dentry *root;
  256. int rc = 0;
  257. root = psinfo_lock_root();
  258. if (!root)
  259. return 0;
  260. mutex_lock(&records_list_lock);
  261. list_for_each_entry_safe(pos, tmp, &records_list, list) {
  262. if (pos->record->psi == psi) {
  263. list_del_init(&pos->list);
  264. rc = simple_unlink(d_inode(root), pos->dentry);
  265. if (WARN_ON(rc))
  266. break;
  267. d_drop(pos->dentry);
  268. dput(pos->dentry);
  269. pos->dentry = NULL;
  270. }
  271. }
  272. mutex_unlock(&records_list_lock);
  273. inode_unlock(d_inode(root));
  274. return rc;
  275. }
  276. /*
  277. * Make a regular file in the root directory of our file system.
  278. * Load it up with "size" bytes of data from "buf".
  279. * Set the mtime & ctime to the date that this record was originally stored.
  280. */
  281. int pstore_mkfile(struct dentry *root, struct pstore_record *record)
  282. {
  283. struct dentry *dentry;
  284. struct inode *inode;
  285. int rc = 0;
  286. char name[PSTORE_NAMELEN];
  287. struct pstore_private *private, *pos;
  288. size_t size = record->size + record->ecc_notice_size;
  289. if (WARN_ON(!inode_is_locked(d_inode(root))))
  290. return -EINVAL;
  291. rc = -EEXIST;
  292. /* Skip records that are already present in the filesystem. */
  293. mutex_lock(&records_list_lock);
  294. list_for_each_entry(pos, &records_list, list) {
  295. if (pos->record->type == record->type &&
  296. pos->record->id == record->id &&
  297. pos->record->psi == record->psi)
  298. goto fail;
  299. }
  300. rc = -ENOMEM;
  301. inode = pstore_get_inode(root->d_sb);
  302. if (!inode)
  303. goto fail;
  304. inode->i_mode = S_IFREG | 0444;
  305. inode->i_fop = &pstore_file_operations;
  306. scnprintf(name, sizeof(name), "%s-%s-%llu%s",
  307. pstore_type_to_name(record->type),
  308. record->psi->name, record->id,
  309. record->compressed ? ".enc.z" : "");
  310. private = kzalloc(sizeof(*private), GFP_KERNEL);
  311. if (!private)
  312. goto fail_inode;
  313. dentry = d_alloc_name(root, name);
  314. if (!dentry)
  315. goto fail_private;
  316. private->dentry = dentry;
  317. private->record = record;
  318. inode->i_size = private->total_size = size;
  319. inode->i_private = private;
  320. if (record->time.tv_sec)
  321. inode->i_mtime = inode->i_ctime = record->time;
  322. d_add(dentry, inode);
  323. list_add(&private->list, &records_list);
  324. mutex_unlock(&records_list_lock);
  325. return 0;
  326. fail_private:
  327. free_pstore_private(private);
  328. fail_inode:
  329. iput(inode);
  330. fail:
  331. mutex_unlock(&records_list_lock);
  332. return rc;
  333. }
  334. /*
  335. * Read all the records from the persistent store. Create
  336. * files in our filesystem. Don't warn about -EEXIST errors
  337. * when we are re-scanning the backing store looking to add new
  338. * error records.
  339. */
  340. void pstore_get_records(int quiet)
  341. {
  342. struct dentry *root;
  343. root = psinfo_lock_root();
  344. if (!root)
  345. return;
  346. pstore_get_backend_records(psinfo, root, quiet);
  347. inode_unlock(d_inode(root));
  348. }
  349. static int pstore_fill_super(struct super_block *sb, void *data, int silent)
  350. {
  351. struct inode *inode;
  352. sb->s_maxbytes = MAX_LFS_FILESIZE;
  353. sb->s_blocksize = PAGE_SIZE;
  354. sb->s_blocksize_bits = PAGE_SHIFT;
  355. sb->s_magic = PSTOREFS_MAGIC;
  356. sb->s_op = &pstore_ops;
  357. sb->s_time_gran = 1;
  358. parse_options(data);
  359. inode = pstore_get_inode(sb);
  360. if (inode) {
  361. inode->i_mode = S_IFDIR | 0750;
  362. inode->i_op = &pstore_dir_inode_operations;
  363. inode->i_fop = &simple_dir_operations;
  364. inc_nlink(inode);
  365. }
  366. sb->s_root = d_make_root(inode);
  367. if (!sb->s_root)
  368. return -ENOMEM;
  369. mutex_lock(&pstore_sb_lock);
  370. pstore_sb = sb;
  371. mutex_unlock(&pstore_sb_lock);
  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. mutex_lock(&pstore_sb_lock);
  383. WARN_ON(pstore_sb && pstore_sb != sb);
  384. kill_litter_super(sb);
  385. pstore_sb = NULL;
  386. mutex_lock(&records_list_lock);
  387. INIT_LIST_HEAD(&records_list);
  388. mutex_unlock(&records_list_lock);
  389. mutex_unlock(&pstore_sb_lock);
  390. }
  391. static struct file_system_type pstore_fs_type = {
  392. .owner = THIS_MODULE,
  393. .name = "pstore",
  394. .mount = pstore_mount,
  395. .kill_sb = pstore_kill_sb,
  396. };
  397. int __init pstore_init_fs(void)
  398. {
  399. int err;
  400. /* Create a convenient mount point for people to access pstore */
  401. err = sysfs_create_mount_point(fs_kobj, "pstore");
  402. if (err)
  403. goto out;
  404. err = register_filesystem(&pstore_fs_type);
  405. if (err < 0)
  406. sysfs_remove_mount_point(fs_kobj, "pstore");
  407. out:
  408. return err;
  409. }
  410. void __exit pstore_exit_fs(void)
  411. {
  412. unregister_filesystem(&pstore_fs_type);
  413. sysfs_remove_mount_point(fs_kobj, "pstore");
  414. }