inode.c 9.3 KB

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