inode.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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/ramfs.h>
  30. #include <linux/parser.h>
  31. #include <linux/sched.h>
  32. #include <linux/magic.h>
  33. #include <linux/pstore.h>
  34. #include <linux/slab.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/uaccess.h>
  37. #include "internal.h"
  38. #define PSTORE_NAMELEN 64
  39. static DEFINE_SPINLOCK(allpstore_lock);
  40. static LIST_HEAD(allpstore);
  41. struct pstore_private {
  42. struct list_head list;
  43. struct pstore_info *psi;
  44. enum pstore_type_id type;
  45. u64 id;
  46. ssize_t size;
  47. char data[];
  48. };
  49. static int pstore_file_open(struct inode *inode, struct file *file)
  50. {
  51. file->private_data = inode->i_private;
  52. return 0;
  53. }
  54. static ssize_t pstore_file_read(struct file *file, char __user *userbuf,
  55. size_t count, loff_t *ppos)
  56. {
  57. struct pstore_private *ps = file->private_data;
  58. return simple_read_from_buffer(userbuf, count, ppos, ps->data, ps->size);
  59. }
  60. static const struct file_operations pstore_file_operations = {
  61. .open = pstore_file_open,
  62. .read = pstore_file_read,
  63. .llseek = default_llseek,
  64. };
  65. /*
  66. * When a file is unlinked from our file system we call the
  67. * platform driver to erase the record from persistent store.
  68. */
  69. static int pstore_unlink(struct inode *dir, struct dentry *dentry)
  70. {
  71. struct pstore_private *p = dentry->d_inode->i_private;
  72. if (p->psi->erase)
  73. p->psi->erase(p->type, p->id, p->psi);
  74. return simple_unlink(dir, dentry);
  75. }
  76. static void pstore_evict_inode(struct inode *inode)
  77. {
  78. struct pstore_private *p = inode->i_private;
  79. unsigned long flags;
  80. end_writeback(inode);
  81. if (p) {
  82. spin_lock_irqsave(&allpstore_lock, flags);
  83. list_del(&p->list);
  84. spin_unlock_irqrestore(&allpstore_lock, flags);
  85. kfree(p);
  86. }
  87. }
  88. static const struct inode_operations pstore_dir_inode_operations = {
  89. .lookup = simple_lookup,
  90. .unlink = pstore_unlink,
  91. };
  92. static struct inode *pstore_get_inode(struct super_block *sb)
  93. {
  94. struct inode *inode = new_inode(sb);
  95. if (inode) {
  96. inode->i_ino = get_next_ino();
  97. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  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 = 0;
  158. char name[PSTORE_NAMELEN];
  159. struct pstore_private *private, *pos;
  160. unsigned long flags;
  161. spin_lock_irqsave(&allpstore_lock, flags);
  162. list_for_each_entry(pos, &allpstore, list) {
  163. if (pos->type == type &&
  164. pos->id == id &&
  165. pos->psi == psi) {
  166. rc = -EEXIST;
  167. break;
  168. }
  169. }
  170. spin_unlock_irqrestore(&allpstore_lock, flags);
  171. if (rc)
  172. return rc;
  173. rc = -ENOMEM;
  174. inode = pstore_get_inode(pstore_sb);
  175. if (!inode)
  176. goto fail;
  177. inode->i_mode = S_IFREG | 0444;
  178. inode->i_fop = &pstore_file_operations;
  179. private = kmalloc(sizeof *private + size, GFP_KERNEL);
  180. if (!private)
  181. goto fail_alloc;
  182. private->type = type;
  183. private->id = id;
  184. private->psi = psi;
  185. switch (type) {
  186. case PSTORE_TYPE_DMESG:
  187. sprintf(name, "dmesg-%s-%lld", psname, id);
  188. break;
  189. case PSTORE_TYPE_MCE:
  190. sprintf(name, "mce-%s-%lld", psname, id);
  191. break;
  192. case PSTORE_TYPE_UNKNOWN:
  193. sprintf(name, "unknown-%s-%lld", psname, id);
  194. break;
  195. default:
  196. sprintf(name, "type%d-%s-%lld", type, psname, id);
  197. break;
  198. }
  199. mutex_lock(&root->d_inode->i_mutex);
  200. rc = -ENOSPC;
  201. dentry = d_alloc_name(root, name);
  202. if (IS_ERR(dentry))
  203. goto fail_lockedalloc;
  204. memcpy(private->data, data, size);
  205. inode->i_size = private->size = size;
  206. inode->i_private = private;
  207. if (time.tv_sec)
  208. inode->i_mtime = inode->i_ctime = time;
  209. d_add(dentry, inode);
  210. spin_lock_irqsave(&allpstore_lock, flags);
  211. list_add(&private->list, &allpstore);
  212. spin_unlock_irqrestore(&allpstore_lock, flags);
  213. mutex_unlock(&root->d_inode->i_mutex);
  214. return 0;
  215. fail_lockedalloc:
  216. mutex_unlock(&root->d_inode->i_mutex);
  217. kfree(private);
  218. fail_alloc:
  219. iput(inode);
  220. fail:
  221. return rc;
  222. }
  223. int pstore_fill_super(struct super_block *sb, void *data, int silent)
  224. {
  225. struct inode *inode;
  226. save_mount_options(sb, data);
  227. pstore_sb = sb;
  228. sb->s_maxbytes = MAX_LFS_FILESIZE;
  229. sb->s_blocksize = PAGE_CACHE_SIZE;
  230. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  231. sb->s_magic = PSTOREFS_MAGIC;
  232. sb->s_op = &pstore_ops;
  233. sb->s_time_gran = 1;
  234. parse_options(data);
  235. inode = pstore_get_inode(sb);
  236. if (inode) {
  237. inode->i_mode = S_IFDIR | 0755;
  238. inode->i_op = &pstore_dir_inode_operations;
  239. inode->i_fop = &simple_dir_operations;
  240. inc_nlink(inode);
  241. }
  242. sb->s_root = d_make_root(inode);
  243. if (!sb->s_root)
  244. return -ENOMEM;
  245. pstore_get_records(0);
  246. return 0;
  247. }
  248. static struct dentry *pstore_mount(struct file_system_type *fs_type,
  249. int flags, const char *dev_name, void *data)
  250. {
  251. return mount_single(fs_type, flags, data, pstore_fill_super);
  252. }
  253. static void pstore_kill_sb(struct super_block *sb)
  254. {
  255. kill_litter_super(sb);
  256. pstore_sb = NULL;
  257. }
  258. static struct file_system_type pstore_fs_type = {
  259. .name = "pstore",
  260. .mount = pstore_mount,
  261. .kill_sb = pstore_kill_sb,
  262. };
  263. static int __init init_pstore_fs(void)
  264. {
  265. return register_filesystem(&pstore_fs_type);
  266. }
  267. module_init(init_pstore_fs)
  268. MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>");
  269. MODULE_LICENSE("GPL");