inode.c 10 KB

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