inode.c 8.4 KB

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