inode.c 8.5 KB

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