root.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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 = iget(sb, ent->ino);
  104. if (!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 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. DPRINTK(("autofs_root_symlink: %s <- ", symname));
  242. autofs_say(dentry->d_name.name,dentry->d_name.len);
  243. lock_kernel();
  244. if (!autofs_oz_mode(sbi)) {
  245. unlock_kernel();
  246. return -EACCES;
  247. }
  248. if (autofs_hash_lookup(dh, &dentry->d_name)) {
  249. unlock_kernel();
  250. return -EEXIST;
  251. }
  252. n = find_first_zero_bit(sbi->symlink_bitmap,AUTOFS_MAX_SYMLINKS);
  253. if (n >= AUTOFS_MAX_SYMLINKS) {
  254. unlock_kernel();
  255. return -ENOSPC;
  256. }
  257. set_bit(n,sbi->symlink_bitmap);
  258. sl = &sbi->symlink[n];
  259. sl->len = strlen(symname);
  260. sl->data = kmalloc(slsize = sl->len+1, GFP_KERNEL);
  261. if (!sl->data) {
  262. clear_bit(n,sbi->symlink_bitmap);
  263. unlock_kernel();
  264. return -ENOSPC;
  265. }
  266. ent = kmalloc(sizeof(struct autofs_dir_ent), GFP_KERNEL);
  267. if (!ent) {
  268. kfree(sl->data);
  269. clear_bit(n,sbi->symlink_bitmap);
  270. unlock_kernel();
  271. return -ENOSPC;
  272. }
  273. ent->name = kmalloc(dentry->d_name.len+1, GFP_KERNEL);
  274. if (!ent->name) {
  275. kfree(sl->data);
  276. kfree(ent);
  277. clear_bit(n,sbi->symlink_bitmap);
  278. unlock_kernel();
  279. return -ENOSPC;
  280. }
  281. memcpy(sl->data,symname,slsize);
  282. sl->mtime = get_seconds();
  283. ent->ino = AUTOFS_FIRST_SYMLINK + n;
  284. ent->hash = dentry->d_name.hash;
  285. memcpy(ent->name, dentry->d_name.name, 1+(ent->len = dentry->d_name.len));
  286. ent->dentry = NULL; /* We don't keep the dentry for symlinks */
  287. autofs_hash_insert(dh,ent);
  288. d_instantiate(dentry, iget(dir->i_sb,ent->ino));
  289. unlock_kernel();
  290. return 0;
  291. }
  292. /*
  293. * NOTE!
  294. *
  295. * Normal filesystems would do a "d_delete()" to tell the VFS dcache
  296. * that the file no longer exists. However, doing that means that the
  297. * VFS layer can turn the dentry into a negative dentry, which we
  298. * obviously do not want (we're dropping the entry not because it
  299. * doesn't exist, but because it has timed out).
  300. *
  301. * Also see autofs_root_rmdir()..
  302. */
  303. static int autofs_root_unlink(struct inode *dir, struct dentry *dentry)
  304. {
  305. struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
  306. struct autofs_dirhash *dh = &sbi->dirhash;
  307. struct autofs_dir_ent *ent;
  308. unsigned int n;
  309. /* This allows root to remove symlinks */
  310. lock_kernel();
  311. if (!autofs_oz_mode(sbi) && !capable(CAP_SYS_ADMIN)) {
  312. unlock_kernel();
  313. return -EACCES;
  314. }
  315. ent = autofs_hash_lookup(dh, &dentry->d_name);
  316. if (!ent) {
  317. unlock_kernel();
  318. return -ENOENT;
  319. }
  320. n = ent->ino - AUTOFS_FIRST_SYMLINK;
  321. if (n >= AUTOFS_MAX_SYMLINKS) {
  322. unlock_kernel();
  323. return -EISDIR; /* It's a directory, dummy */
  324. }
  325. if (!test_bit(n,sbi->symlink_bitmap)) {
  326. unlock_kernel();
  327. return -EINVAL; /* Nonexistent symlink? Shouldn't happen */
  328. }
  329. dentry->d_time = (unsigned long)(struct autofs_dirhash *)NULL;
  330. autofs_hash_delete(ent);
  331. clear_bit(n,sbi->symlink_bitmap);
  332. kfree(sbi->symlink[n].data);
  333. d_drop(dentry);
  334. unlock_kernel();
  335. return 0;
  336. }
  337. static int autofs_root_rmdir(struct inode *dir, struct dentry *dentry)
  338. {
  339. struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
  340. struct autofs_dirhash *dh = &sbi->dirhash;
  341. struct autofs_dir_ent *ent;
  342. lock_kernel();
  343. if (!autofs_oz_mode(sbi)) {
  344. unlock_kernel();
  345. return -EACCES;
  346. }
  347. ent = autofs_hash_lookup(dh, &dentry->d_name);
  348. if (!ent) {
  349. unlock_kernel();
  350. return -ENOENT;
  351. }
  352. if ((unsigned int)ent->ino < AUTOFS_FIRST_DIR_INO) {
  353. unlock_kernel();
  354. return -ENOTDIR; /* Not a directory */
  355. }
  356. if (ent->dentry != dentry) {
  357. printk("autofs_rmdir: odentry != dentry for entry %s\n", dentry->d_name.name);
  358. }
  359. dentry->d_time = (unsigned long)(struct autofs_dir_ent *)NULL;
  360. autofs_hash_delete(ent);
  361. drop_nlink(dir);
  362. d_drop(dentry);
  363. unlock_kernel();
  364. return 0;
  365. }
  366. static int autofs_root_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  367. {
  368. struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
  369. struct autofs_dirhash *dh = &sbi->dirhash;
  370. struct autofs_dir_ent *ent;
  371. ino_t ino;
  372. lock_kernel();
  373. if (!autofs_oz_mode(sbi)) {
  374. unlock_kernel();
  375. return -EACCES;
  376. }
  377. ent = autofs_hash_lookup(dh, &dentry->d_name);
  378. if (ent) {
  379. unlock_kernel();
  380. return -EEXIST;
  381. }
  382. if (sbi->next_dir_ino < AUTOFS_FIRST_DIR_INO) {
  383. printk("autofs: Out of inode numbers -- what the heck did you do??\n");
  384. unlock_kernel();
  385. return -ENOSPC;
  386. }
  387. ino = sbi->next_dir_ino++;
  388. ent = kmalloc(sizeof(struct autofs_dir_ent), GFP_KERNEL);
  389. if (!ent) {
  390. unlock_kernel();
  391. return -ENOSPC;
  392. }
  393. ent->name = kmalloc(dentry->d_name.len+1, GFP_KERNEL);
  394. if (!ent->name) {
  395. kfree(ent);
  396. unlock_kernel();
  397. return -ENOSPC;
  398. }
  399. ent->hash = dentry->d_name.hash;
  400. memcpy(ent->name, dentry->d_name.name, 1+(ent->len = dentry->d_name.len));
  401. ent->ino = ino;
  402. ent->dentry = dentry;
  403. autofs_hash_insert(dh,ent);
  404. inc_nlink(dir);
  405. d_instantiate(dentry, iget(dir->i_sb,ino));
  406. unlock_kernel();
  407. return 0;
  408. }
  409. /* Get/set timeout ioctl() operation */
  410. static inline int autofs_get_set_timeout(struct autofs_sb_info *sbi,
  411. unsigned long __user *p)
  412. {
  413. unsigned long ntimeout;
  414. if (get_user(ntimeout, p) ||
  415. put_user(sbi->exp_timeout / HZ, p))
  416. return -EFAULT;
  417. if (ntimeout > ULONG_MAX/HZ)
  418. sbi->exp_timeout = 0;
  419. else
  420. sbi->exp_timeout = ntimeout * HZ;
  421. return 0;
  422. }
  423. /* Return protocol version */
  424. static inline int autofs_get_protover(int __user *p)
  425. {
  426. return put_user(AUTOFS_PROTO_VERSION, p);
  427. }
  428. /* Perform an expiry operation */
  429. static inline int autofs_expire_run(struct super_block *sb,
  430. struct autofs_sb_info *sbi,
  431. struct vfsmount *mnt,
  432. struct autofs_packet_expire __user *pkt_p)
  433. {
  434. struct autofs_dir_ent *ent;
  435. struct autofs_packet_expire pkt;
  436. memset(&pkt,0,sizeof pkt);
  437. pkt.hdr.proto_version = AUTOFS_PROTO_VERSION;
  438. pkt.hdr.type = autofs_ptype_expire;
  439. if (!sbi->exp_timeout || !(ent = autofs_expire(sb,sbi,mnt)))
  440. return -EAGAIN;
  441. pkt.len = ent->len;
  442. memcpy(pkt.name, ent->name, pkt.len);
  443. pkt.name[pkt.len] = '\0';
  444. if (copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)))
  445. return -EFAULT;
  446. return 0;
  447. }
  448. /*
  449. * ioctl()'s on the root directory is the chief method for the daemon to
  450. * generate kernel reactions
  451. */
  452. static int autofs_root_ioctl(struct inode *inode, struct file *filp,
  453. unsigned int cmd, unsigned long arg)
  454. {
  455. struct autofs_sb_info *sbi = autofs_sbi(inode->i_sb);
  456. void __user *argp = (void __user *)arg;
  457. DPRINTK(("autofs_ioctl: cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u\n",cmd,arg,sbi,task_pgrp_nr(current)));
  458. if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
  459. _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
  460. return -ENOTTY;
  461. if (!autofs_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
  462. return -EPERM;
  463. switch(cmd) {
  464. case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
  465. return autofs_wait_release(sbi,(autofs_wqt_t)arg,0);
  466. case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
  467. return autofs_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
  468. case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
  469. autofs_catatonic_mode(sbi);
  470. return 0;
  471. case AUTOFS_IOC_PROTOVER: /* Get protocol version */
  472. return autofs_get_protover(argp);
  473. case AUTOFS_IOC_SETTIMEOUT:
  474. return autofs_get_set_timeout(sbi, argp);
  475. case AUTOFS_IOC_EXPIRE:
  476. return autofs_expire_run(inode->i_sb, sbi, filp->f_path.mnt,
  477. argp);
  478. default:
  479. return -ENOSYS;
  480. }
  481. }