inode.c 9.9 KB

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