inode.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /* -*- c -*- --------------------------------------------------------------- *
  2. *
  3. * linux/fs/autofs/inode.c
  4. *
  5. * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
  6. *
  7. * This file is part of the Linux kernel and is made available under
  8. * the terms of the GNU General Public License, version 2, or at your
  9. * option, any later version, incorporated herein by reference.
  10. *
  11. * ------------------------------------------------------------------------- */
  12. #include <linux/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/file.h>
  15. #include <linux/pagemap.h>
  16. #include <linux/parser.h>
  17. #include <linux/bitops.h>
  18. #include <linux/smp_lock.h>
  19. #include "autofs_i.h"
  20. #include <linux/module.h>
  21. static void ino_lnkfree(struct autofs_info *ino)
  22. {
  23. kfree(ino->u.symlink);
  24. ino->u.symlink = NULL;
  25. }
  26. struct autofs_info *autofs4_init_ino(struct autofs_info *ino,
  27. struct autofs_sb_info *sbi, mode_t mode)
  28. {
  29. int reinit = 1;
  30. if (ino == NULL) {
  31. reinit = 0;
  32. ino = kmalloc(sizeof(*ino), GFP_KERNEL);
  33. }
  34. if (ino == NULL)
  35. return NULL;
  36. ino->flags = 0;
  37. ino->mode = mode;
  38. ino->inode = NULL;
  39. ino->dentry = NULL;
  40. ino->size = 0;
  41. ino->last_used = jiffies;
  42. ino->sbi = sbi;
  43. if (reinit && ino->free)
  44. (ino->free)(ino);
  45. memset(&ino->u, 0, sizeof(ino->u));
  46. ino->free = NULL;
  47. if (S_ISLNK(mode))
  48. ino->free = ino_lnkfree;
  49. return ino;
  50. }
  51. void autofs4_free_ino(struct autofs_info *ino)
  52. {
  53. if (ino->dentry) {
  54. ino->dentry->d_fsdata = NULL;
  55. if (ino->dentry->d_inode)
  56. dput(ino->dentry);
  57. ino->dentry = NULL;
  58. }
  59. if (ino->free)
  60. (ino->free)(ino);
  61. kfree(ino);
  62. }
  63. /*
  64. * Deal with the infamous "Busy inodes after umount ..." message.
  65. *
  66. * Clean up the dentry tree. This happens with autofs if the user
  67. * space program goes away due to a SIGKILL, SIGSEGV etc.
  68. */
  69. static void autofs4_force_release(struct autofs_sb_info *sbi)
  70. {
  71. struct dentry *this_parent = sbi->root;
  72. struct list_head *next;
  73. spin_lock(&dcache_lock);
  74. repeat:
  75. next = this_parent->d_subdirs.next;
  76. resume:
  77. while (next != &this_parent->d_subdirs) {
  78. struct dentry *dentry = list_entry(next, struct dentry, d_u.d_child);
  79. /* Negative dentry - don`t care */
  80. if (!simple_positive(dentry)) {
  81. next = next->next;
  82. continue;
  83. }
  84. if (!list_empty(&dentry->d_subdirs)) {
  85. this_parent = dentry;
  86. goto repeat;
  87. }
  88. next = next->next;
  89. spin_unlock(&dcache_lock);
  90. DPRINTK("dentry %p %.*s",
  91. dentry, (int)dentry->d_name.len, dentry->d_name.name);
  92. dput(dentry);
  93. spin_lock(&dcache_lock);
  94. }
  95. if (this_parent != sbi->root) {
  96. struct dentry *dentry = this_parent;
  97. next = this_parent->d_u.d_child.next;
  98. this_parent = this_parent->d_parent;
  99. spin_unlock(&dcache_lock);
  100. DPRINTK("parent dentry %p %.*s",
  101. dentry, (int)dentry->d_name.len, dentry->d_name.name);
  102. dput(dentry);
  103. spin_lock(&dcache_lock);
  104. goto resume;
  105. }
  106. spin_unlock(&dcache_lock);
  107. dput(sbi->root);
  108. sbi->root = NULL;
  109. shrink_dcache_sb(sbi->sb);
  110. return;
  111. }
  112. static void autofs4_put_super(struct super_block *sb)
  113. {
  114. struct autofs_sb_info *sbi = autofs4_sbi(sb);
  115. sb->s_fs_info = NULL;
  116. if ( !sbi->catatonic )
  117. autofs4_catatonic_mode(sbi); /* Free wait queues, close pipe */
  118. /* Clean up and release dangling references */
  119. if (sbi)
  120. autofs4_force_release(sbi);
  121. kfree(sbi);
  122. DPRINTK("shutting down");
  123. }
  124. static struct super_operations autofs4_sops = {
  125. .put_super = autofs4_put_super,
  126. .statfs = simple_statfs,
  127. };
  128. enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto};
  129. static match_table_t tokens = {
  130. {Opt_fd, "fd=%u"},
  131. {Opt_uid, "uid=%u"},
  132. {Opt_gid, "gid=%u"},
  133. {Opt_pgrp, "pgrp=%u"},
  134. {Opt_minproto, "minproto=%u"},
  135. {Opt_maxproto, "maxproto=%u"},
  136. {Opt_err, NULL}
  137. };
  138. static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid,
  139. pid_t *pgrp, int *minproto, int *maxproto)
  140. {
  141. char *p;
  142. substring_t args[MAX_OPT_ARGS];
  143. int option;
  144. *uid = current->uid;
  145. *gid = current->gid;
  146. *pgrp = process_group(current);
  147. *minproto = AUTOFS_MIN_PROTO_VERSION;
  148. *maxproto = AUTOFS_MAX_PROTO_VERSION;
  149. *pipefd = -1;
  150. if (!options)
  151. return 1;
  152. while ((p = strsep(&options, ",")) != NULL) {
  153. int token;
  154. if (!*p)
  155. continue;
  156. token = match_token(p, tokens, args);
  157. switch (token) {
  158. case Opt_fd:
  159. if (match_int(args, pipefd))
  160. return 1;
  161. break;
  162. case Opt_uid:
  163. if (match_int(args, &option))
  164. return 1;
  165. *uid = option;
  166. break;
  167. case Opt_gid:
  168. if (match_int(args, &option))
  169. return 1;
  170. *gid = option;
  171. break;
  172. case Opt_pgrp:
  173. if (match_int(args, &option))
  174. return 1;
  175. *pgrp = option;
  176. break;
  177. case Opt_minproto:
  178. if (match_int(args, &option))
  179. return 1;
  180. *minproto = option;
  181. break;
  182. case Opt_maxproto:
  183. if (match_int(args, &option))
  184. return 1;
  185. *maxproto = option;
  186. break;
  187. default:
  188. return 1;
  189. }
  190. }
  191. return (*pipefd < 0);
  192. }
  193. static struct autofs_info *autofs4_mkroot(struct autofs_sb_info *sbi)
  194. {
  195. struct autofs_info *ino;
  196. ino = autofs4_init_ino(NULL, sbi, S_IFDIR | 0755);
  197. if (!ino)
  198. return NULL;
  199. return ino;
  200. }
  201. int autofs4_fill_super(struct super_block *s, void *data, int silent)
  202. {
  203. struct inode * root_inode;
  204. struct dentry * root;
  205. struct file * pipe;
  206. int pipefd;
  207. struct autofs_sb_info *sbi;
  208. struct autofs_info *ino;
  209. int minproto, maxproto;
  210. sbi = (struct autofs_sb_info *) kmalloc(sizeof(*sbi), GFP_KERNEL);
  211. if ( !sbi )
  212. goto fail_unlock;
  213. DPRINTK("starting up, sbi = %p",sbi);
  214. memset(sbi, 0, sizeof(*sbi));
  215. s->s_fs_info = sbi;
  216. sbi->magic = AUTOFS_SBI_MAGIC;
  217. sbi->root = NULL;
  218. sbi->catatonic = 0;
  219. sbi->exp_timeout = 0;
  220. sbi->oz_pgrp = process_group(current);
  221. sbi->sb = s;
  222. sbi->version = 0;
  223. sbi->sub_version = 0;
  224. init_MUTEX(&sbi->wq_sem);
  225. spin_lock_init(&sbi->fs_lock);
  226. sbi->queues = NULL;
  227. s->s_blocksize = 1024;
  228. s->s_blocksize_bits = 10;
  229. s->s_magic = AUTOFS_SUPER_MAGIC;
  230. s->s_op = &autofs4_sops;
  231. s->s_time_gran = 1;
  232. /*
  233. * Get the root inode and dentry, but defer checking for errors.
  234. */
  235. ino = autofs4_mkroot(sbi);
  236. if (!ino)
  237. goto fail_free;
  238. root_inode = autofs4_get_inode(s, ino);
  239. kfree(ino);
  240. if (!root_inode)
  241. goto fail_free;
  242. root_inode->i_op = &autofs4_root_inode_operations;
  243. root_inode->i_fop = &autofs4_root_operations;
  244. root = d_alloc_root(root_inode);
  245. pipe = NULL;
  246. if (!root)
  247. goto fail_iput;
  248. /* Can this call block? */
  249. if (parse_options(data, &pipefd,
  250. &root_inode->i_uid, &root_inode->i_gid,
  251. &sbi->oz_pgrp,
  252. &minproto, &maxproto)) {
  253. printk("autofs: called with bogus options\n");
  254. goto fail_dput;
  255. }
  256. /* Couldn't this be tested earlier? */
  257. if (maxproto < AUTOFS_MIN_PROTO_VERSION ||
  258. minproto > AUTOFS_MAX_PROTO_VERSION) {
  259. printk("autofs: kernel does not match daemon version "
  260. "daemon (%d, %d) kernel (%d, %d)\n",
  261. minproto, maxproto,
  262. AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
  263. goto fail_dput;
  264. }
  265. sbi->version = maxproto > AUTOFS_MAX_PROTO_VERSION ? AUTOFS_MAX_PROTO_VERSION : maxproto;
  266. sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
  267. DPRINTK("pipe fd = %d, pgrp = %u", pipefd, sbi->oz_pgrp);
  268. pipe = fget(pipefd);
  269. if ( !pipe ) {
  270. printk("autofs: could not open pipe file descriptor\n");
  271. goto fail_dput;
  272. }
  273. if ( !pipe->f_op || !pipe->f_op->write )
  274. goto fail_fput;
  275. sbi->pipe = pipe;
  276. /*
  277. * Take a reference to the root dentry so we get a chance to
  278. * clean up the dentry tree on umount.
  279. * See autofs4_force_release.
  280. */
  281. sbi->root = dget(root);
  282. /*
  283. * Success! Install the root dentry now to indicate completion.
  284. */
  285. s->s_root = root;
  286. return 0;
  287. /*
  288. * Failure ... clean up.
  289. */
  290. fail_fput:
  291. printk("autofs: pipe file descriptor does not contain proper ops\n");
  292. fput(pipe);
  293. /* fall through */
  294. fail_dput:
  295. dput(root);
  296. goto fail_free;
  297. fail_iput:
  298. printk("autofs: get root dentry failed\n");
  299. iput(root_inode);
  300. fail_free:
  301. kfree(sbi);
  302. fail_unlock:
  303. return -EINVAL;
  304. }
  305. struct inode *autofs4_get_inode(struct super_block *sb,
  306. struct autofs_info *inf)
  307. {
  308. struct inode *inode = new_inode(sb);
  309. if (inode == NULL)
  310. return NULL;
  311. inf->inode = inode;
  312. inode->i_mode = inf->mode;
  313. if (sb->s_root) {
  314. inode->i_uid = sb->s_root->d_inode->i_uid;
  315. inode->i_gid = sb->s_root->d_inode->i_gid;
  316. } else {
  317. inode->i_uid = 0;
  318. inode->i_gid = 0;
  319. }
  320. inode->i_blksize = PAGE_CACHE_SIZE;
  321. inode->i_blocks = 0;
  322. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  323. if (S_ISDIR(inf->mode)) {
  324. inode->i_nlink = 2;
  325. inode->i_op = &autofs4_dir_inode_operations;
  326. inode->i_fop = &autofs4_dir_operations;
  327. } else if (S_ISLNK(inf->mode)) {
  328. inode->i_size = inf->size;
  329. inode->i_op = &autofs4_symlink_inode_operations;
  330. }
  331. return inode;
  332. }