inode.c 8.7 KB

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