inode.c 10 KB

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