inode.c 8.0 KB

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