inode.c 6.4 KB

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