inode.c 6.3 KB

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