inode.c 10 KB

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