inode.c 10 KB

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