inode.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* -*- c -*- --------------------------------------------------------------- *
  2. *
  3. * linux/fs/autofs/inode.c
  4. *
  5. * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
  6. * Copyright 2005-2006 Ian Kent <raven@themaw.net>
  7. *
  8. * This file is part of the Linux kernel and is made available under
  9. * the terms of the GNU General Public License, version 2, or at your
  10. * option, any later version, incorporated herein by reference.
  11. *
  12. * ------------------------------------------------------------------------- */
  13. #include <linux/kernel.h>
  14. #include <linux/slab.h>
  15. #include <linux/file.h>
  16. #include <linux/seq_file.h>
  17. #include <linux/pagemap.h>
  18. #include <linux/parser.h>
  19. #include <linux/bitops.h>
  20. #include <linux/magic.h>
  21. #include "autofs_i.h"
  22. #include <linux/module.h>
  23. struct autofs_info *autofs4_init_ino(struct autofs_info *ino,
  24. struct autofs_sb_info *sbi)
  25. {
  26. int reinit = 1;
  27. if (ino == NULL) {
  28. reinit = 0;
  29. ino = kmalloc(sizeof(*ino), GFP_KERNEL);
  30. }
  31. if (ino == NULL)
  32. return NULL;
  33. if (!reinit) {
  34. ino->flags = 0;
  35. ino->dentry = NULL;
  36. INIT_LIST_HEAD(&ino->active);
  37. ino->active_count = 0;
  38. INIT_LIST_HEAD(&ino->expiring);
  39. atomic_set(&ino->count, 0);
  40. }
  41. ino->uid = 0;
  42. ino->gid = 0;
  43. ino->last_used = jiffies;
  44. ino->sbi = sbi;
  45. return ino;
  46. }
  47. void autofs4_free_ino(struct autofs_info *ino)
  48. {
  49. if (ino->dentry) {
  50. ino->dentry->d_fsdata = NULL;
  51. ino->dentry = NULL;
  52. }
  53. kfree(ino);
  54. }
  55. void autofs4_kill_sb(struct super_block *sb)
  56. {
  57. struct autofs_sb_info *sbi = autofs4_sbi(sb);
  58. /*
  59. * In the event of a failure in get_sb_nodev the superblock
  60. * info is not present so nothing else has been setup, so
  61. * just call kill_anon_super when we are called from
  62. * deactivate_super.
  63. */
  64. if (!sbi)
  65. goto out_kill_sb;
  66. /* Free wait queues, close pipe */
  67. autofs4_catatonic_mode(sbi);
  68. sb->s_fs_info = NULL;
  69. kfree(sbi);
  70. out_kill_sb:
  71. DPRINTK("shutting down");
  72. kill_litter_super(sb);
  73. }
  74. static int autofs4_show_options(struct seq_file *m, struct vfsmount *mnt)
  75. {
  76. struct autofs_sb_info *sbi = autofs4_sbi(mnt->mnt_sb);
  77. struct inode *root_inode = mnt->mnt_sb->s_root->d_inode;
  78. if (!sbi)
  79. return 0;
  80. seq_printf(m, ",fd=%d", sbi->pipefd);
  81. if (root_inode->i_uid != 0)
  82. seq_printf(m, ",uid=%u", root_inode->i_uid);
  83. if (root_inode->i_gid != 0)
  84. seq_printf(m, ",gid=%u", root_inode->i_gid);
  85. seq_printf(m, ",pgrp=%d", sbi->oz_pgrp);
  86. seq_printf(m, ",timeout=%lu", sbi->exp_timeout/HZ);
  87. seq_printf(m, ",minproto=%d", sbi->min_proto);
  88. seq_printf(m, ",maxproto=%d", sbi->max_proto);
  89. if (autofs_type_offset(sbi->type))
  90. seq_printf(m, ",offset");
  91. else if (autofs_type_direct(sbi->type))
  92. seq_printf(m, ",direct");
  93. else
  94. seq_printf(m, ",indirect");
  95. return 0;
  96. }
  97. static void autofs4_evict_inode(struct inode *inode)
  98. {
  99. end_writeback(inode);
  100. kfree(inode->i_private);
  101. }
  102. static const struct super_operations autofs4_sops = {
  103. .statfs = simple_statfs,
  104. .show_options = autofs4_show_options,
  105. .evict_inode = autofs4_evict_inode,
  106. };
  107. enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto,
  108. Opt_indirect, Opt_direct, Opt_offset};
  109. static const match_table_t tokens = {
  110. {Opt_fd, "fd=%u"},
  111. {Opt_uid, "uid=%u"},
  112. {Opt_gid, "gid=%u"},
  113. {Opt_pgrp, "pgrp=%u"},
  114. {Opt_minproto, "minproto=%u"},
  115. {Opt_maxproto, "maxproto=%u"},
  116. {Opt_indirect, "indirect"},
  117. {Opt_direct, "direct"},
  118. {Opt_offset, "offset"},
  119. {Opt_err, NULL}
  120. };
  121. static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid,
  122. pid_t *pgrp, unsigned int *type, int *minproto, int *maxproto)
  123. {
  124. char *p;
  125. substring_t args[MAX_OPT_ARGS];
  126. int option;
  127. *uid = current_uid();
  128. *gid = current_gid();
  129. *pgrp = task_pgrp_nr(current);
  130. *minproto = AUTOFS_MIN_PROTO_VERSION;
  131. *maxproto = AUTOFS_MAX_PROTO_VERSION;
  132. *pipefd = -1;
  133. if (!options)
  134. return 1;
  135. while ((p = strsep(&options, ",")) != NULL) {
  136. int token;
  137. if (!*p)
  138. continue;
  139. token = match_token(p, tokens, args);
  140. switch (token) {
  141. case Opt_fd:
  142. if (match_int(args, pipefd))
  143. return 1;
  144. break;
  145. case Opt_uid:
  146. if (match_int(args, &option))
  147. return 1;
  148. *uid = option;
  149. break;
  150. case Opt_gid:
  151. if (match_int(args, &option))
  152. return 1;
  153. *gid = option;
  154. break;
  155. case Opt_pgrp:
  156. if (match_int(args, &option))
  157. return 1;
  158. *pgrp = option;
  159. break;
  160. case Opt_minproto:
  161. if (match_int(args, &option))
  162. return 1;
  163. *minproto = option;
  164. break;
  165. case Opt_maxproto:
  166. if (match_int(args, &option))
  167. return 1;
  168. *maxproto = option;
  169. break;
  170. case Opt_indirect:
  171. set_autofs_type_indirect(type);
  172. break;
  173. case Opt_direct:
  174. set_autofs_type_direct(type);
  175. break;
  176. case Opt_offset:
  177. set_autofs_type_offset(type);
  178. break;
  179. default:
  180. return 1;
  181. }
  182. }
  183. return (*pipefd < 0);
  184. }
  185. int autofs4_fill_super(struct super_block *s, void *data, int silent)
  186. {
  187. struct inode * root_inode;
  188. struct dentry * root;
  189. struct file * pipe;
  190. int pipefd;
  191. struct autofs_sb_info *sbi;
  192. struct autofs_info *ino;
  193. sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
  194. if (!sbi)
  195. goto fail_unlock;
  196. DPRINTK("starting up, sbi = %p",sbi);
  197. s->s_fs_info = sbi;
  198. sbi->magic = AUTOFS_SBI_MAGIC;
  199. sbi->pipefd = -1;
  200. sbi->pipe = NULL;
  201. sbi->catatonic = 1;
  202. sbi->exp_timeout = 0;
  203. sbi->oz_pgrp = task_pgrp_nr(current);
  204. sbi->sb = s;
  205. sbi->version = 0;
  206. sbi->sub_version = 0;
  207. set_autofs_type_indirect(&sbi->type);
  208. sbi->min_proto = 0;
  209. sbi->max_proto = 0;
  210. mutex_init(&sbi->wq_mutex);
  211. spin_lock_init(&sbi->fs_lock);
  212. sbi->queues = NULL;
  213. spin_lock_init(&sbi->lookup_lock);
  214. INIT_LIST_HEAD(&sbi->active_list);
  215. INIT_LIST_HEAD(&sbi->expiring_list);
  216. s->s_blocksize = 1024;
  217. s->s_blocksize_bits = 10;
  218. s->s_magic = AUTOFS_SUPER_MAGIC;
  219. s->s_op = &autofs4_sops;
  220. s->s_d_op = &autofs4_dentry_operations;
  221. s->s_time_gran = 1;
  222. /*
  223. * Get the root inode and dentry, but defer checking for errors.
  224. */
  225. ino = autofs4_init_ino(NULL, sbi);
  226. if (!ino)
  227. goto fail_free;
  228. root_inode = autofs4_get_inode(s, S_IFDIR | 0755);
  229. if (!root_inode)
  230. goto fail_ino;
  231. root = d_alloc_root(root_inode);
  232. if (!root)
  233. goto fail_iput;
  234. pipe = NULL;
  235. root->d_fsdata = ino;
  236. /* Can this call block? */
  237. if (parse_options(data, &pipefd, &root_inode->i_uid, &root_inode->i_gid,
  238. &sbi->oz_pgrp, &sbi->type, &sbi->min_proto,
  239. &sbi->max_proto)) {
  240. printk("autofs: called with bogus options\n");
  241. goto fail_dput;
  242. }
  243. if (autofs_type_trigger(sbi->type))
  244. __managed_dentry_set_managed(root);
  245. root_inode->i_fop = &autofs4_root_operations;
  246. root_inode->i_op = &autofs4_dir_inode_operations;
  247. /* Couldn't this be tested earlier? */
  248. if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
  249. sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
  250. printk("autofs: kernel does not match daemon version "
  251. "daemon (%d, %d) kernel (%d, %d)\n",
  252. sbi->min_proto, sbi->max_proto,
  253. AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
  254. goto fail_dput;
  255. }
  256. /* Establish highest kernel protocol version */
  257. if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
  258. sbi->version = AUTOFS_MAX_PROTO_VERSION;
  259. else
  260. sbi->version = sbi->max_proto;
  261. sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
  262. DPRINTK("pipe fd = %d, pgrp = %u", pipefd, sbi->oz_pgrp);
  263. pipe = fget(pipefd);
  264. if (!pipe) {
  265. printk("autofs: could not open pipe file descriptor\n");
  266. goto fail_dput;
  267. }
  268. if (!pipe->f_op || !pipe->f_op->write)
  269. goto fail_fput;
  270. sbi->pipe = pipe;
  271. sbi->pipefd = pipefd;
  272. sbi->catatonic = 0;
  273. /*
  274. * Success! Install the root dentry now to indicate completion.
  275. */
  276. s->s_root = root;
  277. return 0;
  278. /*
  279. * Failure ... clean up.
  280. */
  281. fail_fput:
  282. printk("autofs: pipe file descriptor does not contain proper ops\n");
  283. fput(pipe);
  284. /* fall through */
  285. fail_dput:
  286. dput(root);
  287. goto fail_free;
  288. fail_iput:
  289. printk("autofs: get root dentry failed\n");
  290. iput(root_inode);
  291. fail_ino:
  292. kfree(ino);
  293. fail_free:
  294. kfree(sbi);
  295. s->s_fs_info = NULL;
  296. fail_unlock:
  297. return -EINVAL;
  298. }
  299. struct inode *autofs4_get_inode(struct super_block *sb, mode_t mode)
  300. {
  301. struct inode *inode = new_inode(sb);
  302. if (inode == NULL)
  303. return NULL;
  304. inode->i_mode = mode;
  305. if (sb->s_root) {
  306. inode->i_uid = sb->s_root->d_inode->i_uid;
  307. inode->i_gid = sb->s_root->d_inode->i_gid;
  308. }
  309. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  310. inode->i_ino = get_next_ino();
  311. if (S_ISDIR(mode)) {
  312. inode->i_nlink = 2;
  313. inode->i_op = &autofs4_dir_inode_operations;
  314. inode->i_fop = &autofs4_dir_operations;
  315. } else if (S_ISLNK(mode)) {
  316. inode->i_op = &autofs4_symlink_inode_operations;
  317. }
  318. return inode;
  319. }