inode.c 10 KB

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