inode.c 11 KB

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