root.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. /* -*- linux-c -*- --------------------------------------------------------- *
  2. *
  3. * linux/fs/autofs/root.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/capability.h>
  13. #include <linux/errno.h>
  14. #include <linux/stat.h>
  15. #include <linux/slab.h>
  16. #include <linux/param.h>
  17. #include <linux/time.h>
  18. #include <linux/smp_lock.h>
  19. #include "autofs_i.h"
  20. static int autofs_root_readdir(struct file *,void *,filldir_t);
  21. static struct dentry *autofs_root_lookup(struct inode *,struct dentry *, struct nameidata *);
  22. static int autofs_root_symlink(struct inode *,struct dentry *,const char *);
  23. static int autofs_root_unlink(struct inode *,struct dentry *);
  24. static int autofs_root_rmdir(struct inode *,struct dentry *);
  25. static int autofs_root_mkdir(struct inode *,struct dentry *,int);
  26. static int autofs_root_ioctl(struct inode *, struct file *,unsigned int,unsigned long);
  27. const struct file_operations autofs_root_operations = {
  28. .llseek = generic_file_llseek,
  29. .read = generic_read_dir,
  30. .readdir = autofs_root_readdir,
  31. .ioctl = autofs_root_ioctl,
  32. };
  33. const struct inode_operations autofs_root_inode_operations = {
  34. .lookup = autofs_root_lookup,
  35. .unlink = autofs_root_unlink,
  36. .symlink = autofs_root_symlink,
  37. .mkdir = autofs_root_mkdir,
  38. .rmdir = autofs_root_rmdir,
  39. };
  40. static int autofs_root_readdir(struct file *filp, void *dirent, filldir_t filldir)
  41. {
  42. struct autofs_dir_ent *ent = NULL;
  43. struct autofs_dirhash *dirhash;
  44. struct autofs_sb_info *sbi;
  45. struct inode * inode = filp->f_path.dentry->d_inode;
  46. off_t onr, nr;
  47. lock_kernel();
  48. sbi = autofs_sbi(inode->i_sb);
  49. dirhash = &sbi->dirhash;
  50. nr = filp->f_pos;
  51. switch(nr)
  52. {
  53. case 0:
  54. if (filldir(dirent, ".", 1, nr, inode->i_ino, DT_DIR) < 0)
  55. goto out;
  56. filp->f_pos = ++nr;
  57. /* fall through */
  58. case 1:
  59. if (filldir(dirent, "..", 2, nr, inode->i_ino, DT_DIR) < 0)
  60. goto out;
  61. filp->f_pos = ++nr;
  62. /* fall through */
  63. default:
  64. while (onr = nr, ent = autofs_hash_enum(dirhash,&nr,ent)) {
  65. if (!ent->dentry || d_mountpoint(ent->dentry)) {
  66. if (filldir(dirent,ent->name,ent->len,onr,ent->ino,DT_UNKNOWN) < 0)
  67. goto out;
  68. filp->f_pos = nr;
  69. }
  70. }
  71. break;
  72. }
  73. out:
  74. unlock_kernel();
  75. return 0;
  76. }
  77. static int try_to_fill_dentry(struct dentry *dentry, struct super_block *sb, struct autofs_sb_info *sbi)
  78. {
  79. struct inode * inode;
  80. struct autofs_dir_ent *ent;
  81. int status = 0;
  82. if (!(ent = autofs_hash_lookup(&sbi->dirhash, &dentry->d_name))) {
  83. do {
  84. if (status && dentry->d_inode) {
  85. if (status != -ENOENT)
  86. printk("autofs warning: lookup failure on positive dentry, status = %d, name = %s\n", status, dentry->d_name.name);
  87. return 0; /* Try to get the kernel to invalidate this dentry */
  88. }
  89. /* Turn this into a real negative dentry? */
  90. if (status == -ENOENT) {
  91. dentry->d_time = jiffies + AUTOFS_NEGATIVE_TIMEOUT;
  92. dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
  93. return 1;
  94. } else if (status) {
  95. /* Return a negative dentry, but leave it "pending" */
  96. return 1;
  97. }
  98. status = autofs_wait(sbi, &dentry->d_name);
  99. } while (!(ent = autofs_hash_lookup(&sbi->dirhash, &dentry->d_name)));
  100. }
  101. /* Abuse this field as a pointer to the directory entry, used to
  102. find the expire list pointers */
  103. dentry->d_time = (unsigned long) ent;
  104. if (!dentry->d_inode) {
  105. inode = autofs_iget(sb, ent->ino);
  106. if (IS_ERR(inode)) {
  107. /* Failed, but leave pending for next time */
  108. return 1;
  109. }
  110. dentry->d_inode = inode;
  111. }
  112. /* If this is a directory that isn't a mount point, bitch at the
  113. daemon and fix it in user space */
  114. if (S_ISDIR(dentry->d_inode->i_mode) && !d_mountpoint(dentry)) {
  115. return !autofs_wait(sbi, &dentry->d_name);
  116. }
  117. /* We don't update the usages for the autofs daemon itself, this
  118. is necessary for recursive autofs mounts */
  119. if (!autofs_oz_mode(sbi)) {
  120. autofs_update_usage(&sbi->dirhash,ent);
  121. }
  122. dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
  123. return 1;
  124. }
  125. /*
  126. * Revalidate is called on every cache lookup. Some of those
  127. * cache lookups may actually happen while the dentry is not
  128. * yet completely filled in, and revalidate has to delay such
  129. * lookups..
  130. */
  131. static int autofs_revalidate(struct dentry * dentry, struct nameidata *nd)
  132. {
  133. struct inode * dir;
  134. struct autofs_sb_info *sbi;
  135. struct autofs_dir_ent *ent;
  136. int res;
  137. lock_kernel();
  138. dir = dentry->d_parent->d_inode;
  139. sbi = autofs_sbi(dir->i_sb);
  140. /* Pending dentry */
  141. if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
  142. if (autofs_oz_mode(sbi))
  143. res = 1;
  144. else
  145. res = try_to_fill_dentry(dentry, dir->i_sb, sbi);
  146. unlock_kernel();
  147. return res;
  148. }
  149. /* Negative dentry.. invalidate if "old" */
  150. if (!dentry->d_inode) {
  151. unlock_kernel();
  152. return (dentry->d_time - jiffies <= AUTOFS_NEGATIVE_TIMEOUT);
  153. }
  154. /* Check for a non-mountpoint directory */
  155. if (S_ISDIR(dentry->d_inode->i_mode) && !d_mountpoint(dentry)) {
  156. if (autofs_oz_mode(sbi))
  157. res = 1;
  158. else
  159. res = try_to_fill_dentry(dentry, dir->i_sb, sbi);
  160. unlock_kernel();
  161. return res;
  162. }
  163. /* Update the usage list */
  164. if (!autofs_oz_mode(sbi)) {
  165. ent = (struct autofs_dir_ent *) dentry->d_time;
  166. if (ent)
  167. autofs_update_usage(&sbi->dirhash,ent);
  168. }
  169. unlock_kernel();
  170. return 1;
  171. }
  172. static const struct dentry_operations autofs_dentry_operations = {
  173. .d_revalidate = autofs_revalidate,
  174. };
  175. static struct dentry *autofs_root_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  176. {
  177. struct autofs_sb_info *sbi;
  178. int oz_mode;
  179. DPRINTK(("autofs_root_lookup: name = "));
  180. lock_kernel();
  181. autofs_say(dentry->d_name.name,dentry->d_name.len);
  182. if (dentry->d_name.len > NAME_MAX) {
  183. unlock_kernel();
  184. return ERR_PTR(-ENAMETOOLONG);/* File name too long to exist */
  185. }
  186. sbi = autofs_sbi(dir->i_sb);
  187. oz_mode = autofs_oz_mode(sbi);
  188. DPRINTK(("autofs_lookup: pid = %u, pgrp = %u, catatonic = %d, "
  189. "oz_mode = %d\n", task_pid_nr(current),
  190. task_pgrp_nr(current), sbi->catatonic,
  191. oz_mode));
  192. /*
  193. * Mark the dentry incomplete, but add it. This is needed so
  194. * that the VFS layer knows about the dentry, and we can count
  195. * on catching any lookups through the revalidate.
  196. *
  197. * Let all the hard work be done by the revalidate function that
  198. * needs to be able to do this anyway..
  199. *
  200. * We need to do this before we release the directory semaphore.
  201. */
  202. dentry->d_op = &autofs_dentry_operations;
  203. dentry->d_flags |= DCACHE_AUTOFS_PENDING;
  204. d_add(dentry, NULL);
  205. mutex_unlock(&dir->i_mutex);
  206. autofs_revalidate(dentry, nd);
  207. mutex_lock(&dir->i_mutex);
  208. /*
  209. * If we are still pending, check if we had to handle
  210. * a signal. If so we can force a restart..
  211. */
  212. if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
  213. /* See if we were interrupted */
  214. if (signal_pending(current)) {
  215. sigset_t *sigset = &current->pending.signal;
  216. if (sigismember (sigset, SIGKILL) ||
  217. sigismember (sigset, SIGQUIT) ||
  218. sigismember (sigset, SIGINT)) {
  219. unlock_kernel();
  220. return ERR_PTR(-ERESTARTNOINTR);
  221. }
  222. }
  223. }
  224. unlock_kernel();
  225. /*
  226. * If this dentry is unhashed, then we shouldn't honour this
  227. * lookup even if the dentry is positive. Returning ENOENT here
  228. * doesn't do the right thing for all system calls, but it should
  229. * be OK for the operations we permit from an autofs.
  230. */
  231. if (dentry->d_inode && d_unhashed(dentry))
  232. return ERR_PTR(-ENOENT);
  233. return NULL;
  234. }
  235. static int autofs_root_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  236. {
  237. struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
  238. struct autofs_dirhash *dh = &sbi->dirhash;
  239. struct autofs_dir_ent *ent;
  240. unsigned int n;
  241. int slsize;
  242. struct autofs_symlink *sl;
  243. struct inode *inode;
  244. DPRINTK(("autofs_root_symlink: %s <- ", symname));
  245. autofs_say(dentry->d_name.name,dentry->d_name.len);
  246. lock_kernel();
  247. if (!autofs_oz_mode(sbi)) {
  248. unlock_kernel();
  249. return -EACCES;
  250. }
  251. if (autofs_hash_lookup(dh, &dentry->d_name)) {
  252. unlock_kernel();
  253. return -EEXIST;
  254. }
  255. n = find_first_zero_bit(sbi->symlink_bitmap,AUTOFS_MAX_SYMLINKS);
  256. if (n >= AUTOFS_MAX_SYMLINKS) {
  257. unlock_kernel();
  258. return -ENOSPC;
  259. }
  260. set_bit(n,sbi->symlink_bitmap);
  261. sl = &sbi->symlink[n];
  262. sl->len = strlen(symname);
  263. sl->data = kmalloc(slsize = sl->len+1, GFP_KERNEL);
  264. if (!sl->data) {
  265. clear_bit(n,sbi->symlink_bitmap);
  266. unlock_kernel();
  267. return -ENOSPC;
  268. }
  269. ent = kmalloc(sizeof(struct autofs_dir_ent), GFP_KERNEL);
  270. if (!ent) {
  271. kfree(sl->data);
  272. clear_bit(n,sbi->symlink_bitmap);
  273. unlock_kernel();
  274. return -ENOSPC;
  275. }
  276. ent->name = kmalloc(dentry->d_name.len+1, GFP_KERNEL);
  277. if (!ent->name) {
  278. kfree(sl->data);
  279. kfree(ent);
  280. clear_bit(n,sbi->symlink_bitmap);
  281. unlock_kernel();
  282. return -ENOSPC;
  283. }
  284. memcpy(sl->data,symname,slsize);
  285. sl->mtime = get_seconds();
  286. ent->ino = AUTOFS_FIRST_SYMLINK + n;
  287. ent->hash = dentry->d_name.hash;
  288. memcpy(ent->name, dentry->d_name.name, 1+(ent->len = dentry->d_name.len));
  289. ent->dentry = NULL; /* We don't keep the dentry for symlinks */
  290. autofs_hash_insert(dh,ent);
  291. inode = autofs_iget(dir->i_sb, ent->ino);
  292. if (IS_ERR(inode))
  293. return PTR_ERR(inode);
  294. d_instantiate(dentry, inode);
  295. unlock_kernel();
  296. return 0;
  297. }
  298. /*
  299. * NOTE!
  300. *
  301. * Normal filesystems would do a "d_delete()" to tell the VFS dcache
  302. * that the file no longer exists. However, doing that means that the
  303. * VFS layer can turn the dentry into a negative dentry, which we
  304. * obviously do not want (we're dropping the entry not because it
  305. * doesn't exist, but because it has timed out).
  306. *
  307. * Also see autofs_root_rmdir()..
  308. */
  309. static int autofs_root_unlink(struct inode *dir, struct dentry *dentry)
  310. {
  311. struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
  312. struct autofs_dirhash *dh = &sbi->dirhash;
  313. struct autofs_dir_ent *ent;
  314. unsigned int n;
  315. /* This allows root to remove symlinks */
  316. lock_kernel();
  317. if (!autofs_oz_mode(sbi) && !capable(CAP_SYS_ADMIN)) {
  318. unlock_kernel();
  319. return -EACCES;
  320. }
  321. ent = autofs_hash_lookup(dh, &dentry->d_name);
  322. if (!ent) {
  323. unlock_kernel();
  324. return -ENOENT;
  325. }
  326. n = ent->ino - AUTOFS_FIRST_SYMLINK;
  327. if (n >= AUTOFS_MAX_SYMLINKS) {
  328. unlock_kernel();
  329. return -EISDIR; /* It's a directory, dummy */
  330. }
  331. if (!test_bit(n,sbi->symlink_bitmap)) {
  332. unlock_kernel();
  333. return -EINVAL; /* Nonexistent symlink? Shouldn't happen */
  334. }
  335. dentry->d_time = (unsigned long)(struct autofs_dirhash *)NULL;
  336. autofs_hash_delete(ent);
  337. clear_bit(n,sbi->symlink_bitmap);
  338. kfree(sbi->symlink[n].data);
  339. d_drop(dentry);
  340. unlock_kernel();
  341. return 0;
  342. }
  343. static int autofs_root_rmdir(struct inode *dir, struct dentry *dentry)
  344. {
  345. struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
  346. struct autofs_dirhash *dh = &sbi->dirhash;
  347. struct autofs_dir_ent *ent;
  348. lock_kernel();
  349. if (!autofs_oz_mode(sbi)) {
  350. unlock_kernel();
  351. return -EACCES;
  352. }
  353. ent = autofs_hash_lookup(dh, &dentry->d_name);
  354. if (!ent) {
  355. unlock_kernel();
  356. return -ENOENT;
  357. }
  358. if ((unsigned int)ent->ino < AUTOFS_FIRST_DIR_INO) {
  359. unlock_kernel();
  360. return -ENOTDIR; /* Not a directory */
  361. }
  362. if (ent->dentry != dentry) {
  363. printk("autofs_rmdir: odentry != dentry for entry %s\n", dentry->d_name.name);
  364. }
  365. dentry->d_time = (unsigned long)(struct autofs_dir_ent *)NULL;
  366. autofs_hash_delete(ent);
  367. drop_nlink(dir);
  368. d_drop(dentry);
  369. unlock_kernel();
  370. return 0;
  371. }
  372. static int autofs_root_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  373. {
  374. struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
  375. struct autofs_dirhash *dh = &sbi->dirhash;
  376. struct autofs_dir_ent *ent;
  377. struct inode *inode;
  378. ino_t ino;
  379. lock_kernel();
  380. if (!autofs_oz_mode(sbi)) {
  381. unlock_kernel();
  382. return -EACCES;
  383. }
  384. ent = autofs_hash_lookup(dh, &dentry->d_name);
  385. if (ent) {
  386. unlock_kernel();
  387. return -EEXIST;
  388. }
  389. if (sbi->next_dir_ino < AUTOFS_FIRST_DIR_INO) {
  390. printk("autofs: Out of inode numbers -- what the heck did you do??\n");
  391. unlock_kernel();
  392. return -ENOSPC;
  393. }
  394. ino = sbi->next_dir_ino++;
  395. ent = kmalloc(sizeof(struct autofs_dir_ent), GFP_KERNEL);
  396. if (!ent) {
  397. unlock_kernel();
  398. return -ENOSPC;
  399. }
  400. ent->name = kmalloc(dentry->d_name.len+1, GFP_KERNEL);
  401. if (!ent->name) {
  402. kfree(ent);
  403. unlock_kernel();
  404. return -ENOSPC;
  405. }
  406. ent->hash = dentry->d_name.hash;
  407. memcpy(ent->name, dentry->d_name.name, 1+(ent->len = dentry->d_name.len));
  408. ent->ino = ino;
  409. ent->dentry = dentry;
  410. autofs_hash_insert(dh,ent);
  411. inc_nlink(dir);
  412. inode = autofs_iget(dir->i_sb, ino);
  413. if (IS_ERR(inode)) {
  414. drop_nlink(dir);
  415. return PTR_ERR(inode);
  416. }
  417. d_instantiate(dentry, inode);
  418. unlock_kernel();
  419. return 0;
  420. }
  421. /* Get/set timeout ioctl() operation */
  422. static inline int autofs_get_set_timeout(struct autofs_sb_info *sbi,
  423. unsigned long __user *p)
  424. {
  425. unsigned long ntimeout;
  426. if (get_user(ntimeout, p) ||
  427. put_user(sbi->exp_timeout / HZ, p))
  428. return -EFAULT;
  429. if (ntimeout > ULONG_MAX/HZ)
  430. sbi->exp_timeout = 0;
  431. else
  432. sbi->exp_timeout = ntimeout * HZ;
  433. return 0;
  434. }
  435. /* Return protocol version */
  436. static inline int autofs_get_protover(int __user *p)
  437. {
  438. return put_user(AUTOFS_PROTO_VERSION, p);
  439. }
  440. /* Perform an expiry operation */
  441. static inline int autofs_expire_run(struct super_block *sb,
  442. struct autofs_sb_info *sbi,
  443. struct vfsmount *mnt,
  444. struct autofs_packet_expire __user *pkt_p)
  445. {
  446. struct autofs_dir_ent *ent;
  447. struct autofs_packet_expire pkt;
  448. memset(&pkt,0,sizeof pkt);
  449. pkt.hdr.proto_version = AUTOFS_PROTO_VERSION;
  450. pkt.hdr.type = autofs_ptype_expire;
  451. if (!sbi->exp_timeout || !(ent = autofs_expire(sb,sbi,mnt)))
  452. return -EAGAIN;
  453. pkt.len = ent->len;
  454. memcpy(pkt.name, ent->name, pkt.len);
  455. pkt.name[pkt.len] = '\0';
  456. if (copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)))
  457. return -EFAULT;
  458. return 0;
  459. }
  460. /*
  461. * ioctl()'s on the root directory is the chief method for the daemon to
  462. * generate kernel reactions
  463. */
  464. static int autofs_root_ioctl(struct inode *inode, struct file *filp,
  465. unsigned int cmd, unsigned long arg)
  466. {
  467. struct autofs_sb_info *sbi = autofs_sbi(inode->i_sb);
  468. void __user *argp = (void __user *)arg;
  469. DPRINTK(("autofs_ioctl: cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u\n",cmd,arg,sbi,task_pgrp_nr(current)));
  470. if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
  471. _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
  472. return -ENOTTY;
  473. if (!autofs_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
  474. return -EPERM;
  475. switch(cmd) {
  476. case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
  477. return autofs_wait_release(sbi,(autofs_wqt_t)arg,0);
  478. case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
  479. return autofs_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
  480. case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
  481. autofs_catatonic_mode(sbi);
  482. return 0;
  483. case AUTOFS_IOC_PROTOVER: /* Get protocol version */
  484. return autofs_get_protover(argp);
  485. case AUTOFS_IOC_SETTIMEOUT:
  486. return autofs_get_set_timeout(sbi, argp);
  487. case AUTOFS_IOC_EXPIRE:
  488. return autofs_expire_run(inode->i_sb, sbi, filp->f_path.mnt,
  489. argp);
  490. default:
  491. return -ENOSYS;
  492. }
  493. }