root.c 15 KB

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