inode.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Super block/filesystem wide operations
  4. *
  5. * Copyright (C) 1996 Peter J. Braam <braam@maths.ox.ac.uk> and
  6. * Michael Callahan <callahan@maths.ox.ac.uk>
  7. *
  8. * Rewritten for Linux 2.1. Peter Braam <braam@cs.cmu.edu>
  9. * Copyright (C) Carnegie Mellon University
  10. */
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/mm.h>
  14. #include <linux/string.h>
  15. #include <linux/stat.h>
  16. #include <linux/errno.h>
  17. #include <linux/unistd.h>
  18. #include <linux/mutex.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/file.h>
  21. #include <linux/vfs.h>
  22. #include <linux/slab.h>
  23. #include <linux/pid_namespace.h>
  24. #include <linux/uaccess.h>
  25. #include <linux/fs.h>
  26. #include <linux/fs_context.h>
  27. #include <linux/fs_parser.h>
  28. #include <linux/vmalloc.h>
  29. #include <linux/coda.h>
  30. #include "coda_psdev.h"
  31. #include "coda_linux.h"
  32. #include "coda_cache.h"
  33. #include "coda_int.h"
  34. /* VFS super_block ops */
  35. static void coda_evict_inode(struct inode *);
  36. static void coda_put_super(struct super_block *);
  37. static int coda_statfs(struct dentry *dentry, struct kstatfs *buf);
  38. static struct kmem_cache * coda_inode_cachep;
  39. static struct inode *coda_alloc_inode(struct super_block *sb)
  40. {
  41. struct coda_inode_info *ei;
  42. ei = alloc_inode_sb(sb, coda_inode_cachep, GFP_KERNEL);
  43. if (!ei)
  44. return NULL;
  45. memset(&ei->c_fid, 0, sizeof(struct CodaFid));
  46. ei->c_flags = 0;
  47. ei->c_uid = GLOBAL_ROOT_UID;
  48. ei->c_cached_perm = 0;
  49. spin_lock_init(&ei->c_lock);
  50. return &ei->vfs_inode;
  51. }
  52. static void coda_free_inode(struct inode *inode)
  53. {
  54. kmem_cache_free(coda_inode_cachep, ITOC(inode));
  55. }
  56. static void init_once(void *foo)
  57. {
  58. struct coda_inode_info *ei = (struct coda_inode_info *) foo;
  59. inode_init_once(&ei->vfs_inode);
  60. }
  61. int __init coda_init_inodecache(void)
  62. {
  63. coda_inode_cachep = kmem_cache_create("coda_inode_cache",
  64. sizeof(struct coda_inode_info), 0,
  65. SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT,
  66. init_once);
  67. if (coda_inode_cachep == NULL)
  68. return -ENOMEM;
  69. return 0;
  70. }
  71. void coda_destroy_inodecache(void)
  72. {
  73. /*
  74. * Make sure all delayed rcu free inodes are flushed before we
  75. * destroy cache.
  76. */
  77. rcu_barrier();
  78. kmem_cache_destroy(coda_inode_cachep);
  79. }
  80. static int coda_reconfigure(struct fs_context *fc)
  81. {
  82. sync_filesystem(fc->root->d_sb);
  83. fc->sb_flags |= SB_NOATIME;
  84. return 0;
  85. }
  86. /* exported operations */
  87. static const struct super_operations coda_super_operations =
  88. {
  89. .alloc_inode = coda_alloc_inode,
  90. .free_inode = coda_free_inode,
  91. .evict_inode = coda_evict_inode,
  92. .put_super = coda_put_super,
  93. .statfs = coda_statfs,
  94. };
  95. struct coda_fs_context {
  96. int idx;
  97. };
  98. enum {
  99. Opt_fd,
  100. };
  101. static const struct fs_parameter_spec coda_param_specs[] = {
  102. fsparam_fd ("fd", Opt_fd),
  103. {}
  104. };
  105. static int coda_set_idx(struct fs_context *fc, struct file *file)
  106. {
  107. struct coda_fs_context *ctx = fc->fs_private;
  108. struct inode *inode;
  109. int idx;
  110. inode = file_inode(file);
  111. if (!S_ISCHR(inode->i_mode) || imajor(inode) != CODA_PSDEV_MAJOR) {
  112. return invalf(fc, "coda: Not coda psdev");
  113. }
  114. idx = iminor(inode);
  115. if (idx < 0 || idx >= MAX_CODADEVS)
  116. return invalf(fc, "coda: Bad minor number");
  117. ctx->idx = idx;
  118. return 0;
  119. }
  120. static int coda_parse_fd(struct fs_context *fc, struct fs_parameter *param,
  121. struct fs_parse_result *result)
  122. {
  123. struct file *file;
  124. int err;
  125. if (param->type == fs_value_is_file) {
  126. file = param->file;
  127. param->file = NULL;
  128. } else {
  129. file = fget(result->uint_32);
  130. }
  131. if (!file)
  132. return -EBADF;
  133. err = coda_set_idx(fc, file);
  134. fput(file);
  135. return err;
  136. }
  137. static int coda_parse_param(struct fs_context *fc, struct fs_parameter *param)
  138. {
  139. struct fs_parse_result result;
  140. int opt;
  141. opt = fs_parse(fc, coda_param_specs, param, &result);
  142. if (opt < 0)
  143. return opt;
  144. switch (opt) {
  145. case Opt_fd:
  146. return coda_parse_fd(fc, param, &result);
  147. }
  148. return 0;
  149. }
  150. /*
  151. * Parse coda's binary mount data form. We ignore any errors and go with index
  152. * 0 if we get one for backward compatibility.
  153. */
  154. static int coda_parse_monolithic(struct fs_context *fc, void *_data)
  155. {
  156. struct file *file;
  157. struct coda_mount_data *data = _data;
  158. if (!data)
  159. return invalf(fc, "coda: Bad mount data");
  160. if (data->version != CODA_MOUNT_VERSION)
  161. return invalf(fc, "coda: Bad mount version");
  162. file = fget(data->fd);
  163. if (file) {
  164. coda_set_idx(fc, file);
  165. fput(file);
  166. }
  167. return 0;
  168. }
  169. static int coda_fill_super(struct super_block *sb, struct fs_context *fc)
  170. {
  171. struct coda_fs_context *ctx = fc->fs_private;
  172. struct inode *root = NULL;
  173. struct venus_comm *vc;
  174. struct CodaFid fid;
  175. int error;
  176. infof(fc, "coda: device index: %i\n", ctx->idx);
  177. vc = &coda_comms[ctx->idx];
  178. mutex_lock(&vc->vc_mutex);
  179. if (!vc->vc_inuse) {
  180. errorf(fc, "coda: No pseudo device");
  181. error = -EINVAL;
  182. goto unlock_out;
  183. }
  184. if (vc->vc_sb) {
  185. errorf(fc, "coda: Device already mounted");
  186. error = -EBUSY;
  187. goto unlock_out;
  188. }
  189. vc->vc_sb = sb;
  190. mutex_unlock(&vc->vc_mutex);
  191. sb->s_fs_info = vc;
  192. sb->s_flags |= SB_NOATIME;
  193. sb->s_blocksize = 4096; /* XXXXX what do we put here?? */
  194. sb->s_blocksize_bits = 12;
  195. sb->s_magic = CODA_SUPER_MAGIC;
  196. sb->s_op = &coda_super_operations;
  197. set_default_d_op(sb, &coda_dentry_operations);
  198. sb->s_time_gran = 1;
  199. sb->s_time_min = S64_MIN;
  200. sb->s_time_max = S64_MAX;
  201. error = super_setup_bdi(sb);
  202. if (error)
  203. goto error;
  204. /* get root fid from Venus: this needs the root inode */
  205. error = venus_rootfid(sb, &fid);
  206. if ( error ) {
  207. pr_warn("%s: coda_get_rootfid failed with %d\n",
  208. __func__, error);
  209. goto error;
  210. }
  211. pr_info("%s: rootfid is %s\n", __func__, coda_f2s(&fid));
  212. /* make root inode */
  213. root = coda_cnode_make(&fid, sb);
  214. if (IS_ERR(root)) {
  215. error = PTR_ERR(root);
  216. pr_warn("Failure of coda_cnode_make for root: error %d\n",
  217. error);
  218. goto error;
  219. }
  220. pr_info("%s: rootinode is %ld dev %s\n",
  221. __func__, root->i_ino, root->i_sb->s_id);
  222. sb->s_root = d_make_root(root);
  223. if (!sb->s_root) {
  224. error = -EINVAL;
  225. goto error;
  226. }
  227. return 0;
  228. error:
  229. mutex_lock(&vc->vc_mutex);
  230. vc->vc_sb = NULL;
  231. sb->s_fs_info = NULL;
  232. unlock_out:
  233. mutex_unlock(&vc->vc_mutex);
  234. return error;
  235. }
  236. static void coda_put_super(struct super_block *sb)
  237. {
  238. struct venus_comm *vcp = coda_vcp(sb);
  239. mutex_lock(&vcp->vc_mutex);
  240. vcp->vc_sb = NULL;
  241. sb->s_fs_info = NULL;
  242. mutex_unlock(&vcp->vc_mutex);
  243. mutex_destroy(&vcp->vc_mutex);
  244. pr_info("Bye bye.\n");
  245. }
  246. static void coda_evict_inode(struct inode *inode)
  247. {
  248. truncate_inode_pages_final(&inode->i_data);
  249. clear_inode(inode);
  250. coda_cache_clear_inode(inode);
  251. }
  252. int coda_getattr(struct mnt_idmap *idmap, const struct path *path,
  253. struct kstat *stat, u32 request_mask, unsigned int flags)
  254. {
  255. int err = coda_revalidate_inode(d_inode(path->dentry));
  256. if (!err)
  257. generic_fillattr(&nop_mnt_idmap, request_mask,
  258. d_inode(path->dentry), stat);
  259. return err;
  260. }
  261. int coda_setattr(struct mnt_idmap *idmap, struct dentry *de,
  262. struct iattr *iattr)
  263. {
  264. struct inode *inode = d_inode(de);
  265. struct coda_vattr vattr;
  266. int error;
  267. memset(&vattr, 0, sizeof(vattr));
  268. inode_set_ctime_current(inode);
  269. coda_iattr_to_vattr(iattr, &vattr);
  270. vattr.va_type = C_VNON; /* cannot set type */
  271. /* Venus is responsible for truncating the container-file!!! */
  272. error = venus_setattr(inode->i_sb, coda_i2f(inode), &vattr);
  273. if (!error) {
  274. coda_vattr_to_iattr(inode, &vattr);
  275. coda_cache_clear_inode(inode);
  276. }
  277. return error;
  278. }
  279. const struct inode_operations coda_file_inode_operations = {
  280. .permission = coda_permission,
  281. .getattr = coda_getattr,
  282. .setattr = coda_setattr,
  283. };
  284. static int coda_statfs(struct dentry *dentry, struct kstatfs *buf)
  285. {
  286. int error;
  287. error = venus_statfs(dentry, buf);
  288. if (error) {
  289. /* fake something like AFS does */
  290. buf->f_blocks = 9000000;
  291. buf->f_bfree = 9000000;
  292. buf->f_bavail = 9000000;
  293. buf->f_files = 9000000;
  294. buf->f_ffree = 9000000;
  295. }
  296. /* and fill in the rest */
  297. buf->f_type = CODA_SUPER_MAGIC;
  298. buf->f_bsize = 4096;
  299. buf->f_namelen = CODA_MAXNAMLEN;
  300. return 0;
  301. }
  302. static int coda_get_tree(struct fs_context *fc)
  303. {
  304. if (task_active_pid_ns(current) != &init_pid_ns)
  305. return -EINVAL;
  306. return get_tree_nodev(fc, coda_fill_super);
  307. }
  308. static void coda_free_fc(struct fs_context *fc)
  309. {
  310. kfree(fc->fs_private);
  311. }
  312. static const struct fs_context_operations coda_context_ops = {
  313. .free = coda_free_fc,
  314. .parse_param = coda_parse_param,
  315. .parse_monolithic = coda_parse_monolithic,
  316. .get_tree = coda_get_tree,
  317. .reconfigure = coda_reconfigure,
  318. };
  319. static int coda_init_fs_context(struct fs_context *fc)
  320. {
  321. struct coda_fs_context *ctx;
  322. ctx = kzalloc_obj(struct coda_fs_context);
  323. if (!ctx)
  324. return -ENOMEM;
  325. fc->fs_private = ctx;
  326. fc->ops = &coda_context_ops;
  327. return 0;
  328. }
  329. struct file_system_type coda_fs_type = {
  330. .owner = THIS_MODULE,
  331. .name = "coda",
  332. .init_fs_context = coda_init_fs_context,
  333. .parameters = coda_param_specs,
  334. .kill_sb = kill_anon_super,
  335. .fs_flags = FS_BINARY_MOUNTDATA,
  336. };
  337. MODULE_ALIAS_FS("coda");