inode.c 6.7 KB

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