root.c 15 KB

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