inode.c 7.1 KB

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