inode.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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/string.h>
  27. #include <linux/mount.h>
  28. #include <linux/ramfs.h>
  29. #include <linux/parser.h>
  30. #include <linux/sched.h>
  31. #include <linux/magic.h>
  32. #include <linux/pstore.h>
  33. #include <linux/slab.h>
  34. #include <linux/uaccess.h>
  35. #include "internal.h"
  36. #define PSTORE_NAMELEN 64
  37. struct pstore_private {
  38. struct pstore_info *psi;
  39. enum pstore_type_id type;
  40. u64 id;
  41. ssize_t size;
  42. char data[];
  43. };
  44. static int pstore_file_open(struct inode *inode, struct file *file)
  45. {
  46. file->private_data = inode->i_private;
  47. return 0;
  48. }
  49. static ssize_t pstore_file_read(struct file *file, char __user *userbuf,
  50. size_t count, loff_t *ppos)
  51. {
  52. struct pstore_private *ps = file->private_data;
  53. return simple_read_from_buffer(userbuf, count, ppos, ps->data, ps->size);
  54. }
  55. static const struct file_operations pstore_file_operations = {
  56. .open = pstore_file_open,
  57. .read = pstore_file_read,
  58. .llseek = default_llseek,
  59. };
  60. /*
  61. * When a file is unlinked from our file system we call the
  62. * platform driver to erase the record from persistent store.
  63. */
  64. static int pstore_unlink(struct inode *dir, struct dentry *dentry)
  65. {
  66. struct pstore_private *p = dentry->d_inode->i_private;
  67. p->psi->erase(p->type, p->id, p->psi);
  68. return simple_unlink(dir, dentry);
  69. }
  70. static void pstore_evict_inode(struct inode *inode)
  71. {
  72. end_writeback(inode);
  73. kfree(inode->i_private);
  74. }
  75. static const struct inode_operations pstore_dir_inode_operations = {
  76. .lookup = simple_lookup,
  77. .unlink = pstore_unlink,
  78. };
  79. static struct inode *pstore_get_inode(struct super_block *sb,
  80. const struct inode *dir, int mode, dev_t dev)
  81. {
  82. struct inode *inode = new_inode(sb);
  83. if (inode) {
  84. inode->i_ino = get_next_ino();
  85. inode->i_uid = inode->i_gid = 0;
  86. inode->i_mode = mode;
  87. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  88. switch (mode & S_IFMT) {
  89. case S_IFREG:
  90. inode->i_fop = &pstore_file_operations;
  91. break;
  92. case S_IFDIR:
  93. inode->i_op = &pstore_dir_inode_operations;
  94. inode->i_fop = &simple_dir_operations;
  95. inc_nlink(inode);
  96. break;
  97. }
  98. }
  99. return inode;
  100. }
  101. enum {
  102. Opt_kmsg_bytes, Opt_err
  103. };
  104. static const match_table_t tokens = {
  105. {Opt_kmsg_bytes, "kmsg_bytes=%u"},
  106. {Opt_err, NULL}
  107. };
  108. static void parse_options(char *options)
  109. {
  110. char *p;
  111. substring_t args[MAX_OPT_ARGS];
  112. int option;
  113. if (!options)
  114. return;
  115. while ((p = strsep(&options, ",")) != NULL) {
  116. int token;
  117. if (!*p)
  118. continue;
  119. token = match_token(p, tokens, args);
  120. switch (token) {
  121. case Opt_kmsg_bytes:
  122. if (!match_int(&args[0], &option))
  123. pstore_set_kmsg_bytes(option);
  124. break;
  125. }
  126. }
  127. }
  128. static int pstore_remount(struct super_block *sb, int *flags, char *data)
  129. {
  130. parse_options(data);
  131. return 0;
  132. }
  133. static const struct super_operations pstore_ops = {
  134. .statfs = simple_statfs,
  135. .drop_inode = generic_delete_inode,
  136. .evict_inode = pstore_evict_inode,
  137. .remount_fs = pstore_remount,
  138. .show_options = generic_show_options,
  139. };
  140. static struct super_block *pstore_sb;
  141. int pstore_is_mounted(void)
  142. {
  143. return pstore_sb != NULL;
  144. }
  145. /*
  146. * Make a regular file in the root directory of our file system.
  147. * Load it up with "size" bytes of data from "buf".
  148. * Set the mtime & ctime to the date that this record was originally stored.
  149. */
  150. int pstore_mkfile(enum pstore_type_id type, char *psname, u64 id,
  151. char *data, size_t size, struct timespec time,
  152. struct pstore_info *psi)
  153. {
  154. struct dentry *root = pstore_sb->s_root;
  155. struct dentry *dentry;
  156. struct inode *inode;
  157. int rc;
  158. char name[PSTORE_NAMELEN];
  159. struct pstore_private *private;
  160. rc = -ENOMEM;
  161. inode = pstore_get_inode(pstore_sb, root->d_inode, S_IFREG | 0444, 0);
  162. if (!inode)
  163. goto fail;
  164. private = kmalloc(sizeof *private + size, GFP_KERNEL);
  165. if (!private)
  166. goto fail_alloc;
  167. private->type = type;
  168. private->id = id;
  169. private->psi = psi;
  170. switch (type) {
  171. case PSTORE_TYPE_DMESG:
  172. sprintf(name, "dmesg-%s-%lld", psname, id);
  173. break;
  174. case PSTORE_TYPE_MCE:
  175. sprintf(name, "mce-%s-%lld", psname, id);
  176. break;
  177. case PSTORE_TYPE_UNKNOWN:
  178. sprintf(name, "unknown-%s-%lld", psname, id);
  179. break;
  180. default:
  181. sprintf(name, "type%d-%s-%lld", type, psname, id);
  182. break;
  183. }
  184. mutex_lock(&root->d_inode->i_mutex);
  185. rc = -ENOSPC;
  186. dentry = d_alloc_name(root, name);
  187. if (IS_ERR(dentry))
  188. goto fail_lockedalloc;
  189. memcpy(private->data, data, size);
  190. inode->i_size = private->size = size;
  191. inode->i_private = private;
  192. if (time.tv_sec)
  193. inode->i_mtime = inode->i_ctime = time;
  194. d_add(dentry, inode);
  195. mutex_unlock(&root->d_inode->i_mutex);
  196. return 0;
  197. fail_lockedalloc:
  198. mutex_unlock(&root->d_inode->i_mutex);
  199. kfree(private);
  200. fail_alloc:
  201. iput(inode);
  202. fail:
  203. return rc;
  204. }
  205. int pstore_fill_super(struct super_block *sb, void *data, int silent)
  206. {
  207. struct inode *inode = NULL;
  208. struct dentry *root;
  209. int err;
  210. save_mount_options(sb, data);
  211. pstore_sb = sb;
  212. sb->s_maxbytes = MAX_LFS_FILESIZE;
  213. sb->s_blocksize = PAGE_CACHE_SIZE;
  214. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  215. sb->s_magic = PSTOREFS_MAGIC;
  216. sb->s_op = &pstore_ops;
  217. sb->s_time_gran = 1;
  218. parse_options(data);
  219. inode = pstore_get_inode(sb, NULL, S_IFDIR | 0755, 0);
  220. if (!inode) {
  221. err = -ENOMEM;
  222. goto fail;
  223. }
  224. /* override ramfs "dir" options so we catch unlink(2) */
  225. inode->i_op = &pstore_dir_inode_operations;
  226. root = d_alloc_root(inode);
  227. sb->s_root = root;
  228. if (!root) {
  229. err = -ENOMEM;
  230. goto fail;
  231. }
  232. pstore_get_records();
  233. return 0;
  234. fail:
  235. iput(inode);
  236. return err;
  237. }
  238. static struct dentry *pstore_mount(struct file_system_type *fs_type,
  239. int flags, const char *dev_name, void *data)
  240. {
  241. return mount_single(fs_type, flags, data, pstore_fill_super);
  242. }
  243. static void pstore_kill_sb(struct super_block *sb)
  244. {
  245. kill_litter_super(sb);
  246. pstore_sb = NULL;
  247. }
  248. static struct file_system_type pstore_fs_type = {
  249. .name = "pstore",
  250. .mount = pstore_mount,
  251. .kill_sb = pstore_kill_sb,
  252. };
  253. static int __init init_pstore_fs(void)
  254. {
  255. return register_filesystem(&pstore_fs_type);
  256. }
  257. module_init(init_pstore_fs)
  258. MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>");
  259. MODULE_LICENSE("GPL");