inode.c 6.2 KB

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