root.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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, oz_mode = %d\n",
  187. current->pid, process_group(current), sbi->catatonic, oz_mode));
  188. /*
  189. * Mark the dentry incomplete, but add it. This is needed so
  190. * that the VFS layer knows about the dentry, and we can count
  191. * on catching any lookups through the revalidate.
  192. *
  193. * Let all the hard work be done by the revalidate function that
  194. * needs to be able to do this anyway..
  195. *
  196. * We need to do this before we release the directory semaphore.
  197. */
  198. dentry->d_op = &autofs_dentry_operations;
  199. dentry->d_flags |= DCACHE_AUTOFS_PENDING;
  200. d_add(dentry, NULL);
  201. mutex_unlock(&dir->i_mutex);
  202. autofs_revalidate(dentry, nd);
  203. mutex_lock(&dir->i_mutex);
  204. /*
  205. * If we are still pending, check if we had to handle
  206. * a signal. If so we can force a restart..
  207. */
  208. if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
  209. /* See if we were interrupted */
  210. if (signal_pending(current)) {
  211. sigset_t *sigset = &current->pending.signal;
  212. if (sigismember (sigset, SIGKILL) ||
  213. sigismember (sigset, SIGQUIT) ||
  214. sigismember (sigset, SIGINT)) {
  215. unlock_kernel();
  216. return ERR_PTR(-ERESTARTNOINTR);
  217. }
  218. }
  219. }
  220. unlock_kernel();
  221. /*
  222. * If this dentry is unhashed, then we shouldn't honour this
  223. * lookup even if the dentry is positive. Returning ENOENT here
  224. * doesn't do the right thing for all system calls, but it should
  225. * be OK for the operations we permit from an autofs.
  226. */
  227. if ( dentry->d_inode && d_unhashed(dentry) )
  228. return ERR_PTR(-ENOENT);
  229. return NULL;
  230. }
  231. static int autofs_root_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  232. {
  233. struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
  234. struct autofs_dirhash *dh = &sbi->dirhash;
  235. struct autofs_dir_ent *ent;
  236. unsigned int n;
  237. int slsize;
  238. struct autofs_symlink *sl;
  239. DPRINTK(("autofs_root_symlink: %s <- ", symname));
  240. autofs_say(dentry->d_name.name,dentry->d_name.len);
  241. lock_kernel();
  242. if ( !autofs_oz_mode(sbi) ) {
  243. unlock_kernel();
  244. return -EACCES;
  245. }
  246. if ( autofs_hash_lookup(dh, &dentry->d_name) ) {
  247. unlock_kernel();
  248. return -EEXIST;
  249. }
  250. n = find_first_zero_bit(sbi->symlink_bitmap,AUTOFS_MAX_SYMLINKS);
  251. if ( n >= AUTOFS_MAX_SYMLINKS ) {
  252. unlock_kernel();
  253. return -ENOSPC;
  254. }
  255. set_bit(n,sbi->symlink_bitmap);
  256. sl = &sbi->symlink[n];
  257. sl->len = strlen(symname);
  258. sl->data = kmalloc(slsize = sl->len+1, GFP_KERNEL);
  259. if ( !sl->data ) {
  260. clear_bit(n,sbi->symlink_bitmap);
  261. unlock_kernel();
  262. return -ENOSPC;
  263. }
  264. ent = kmalloc(sizeof(struct autofs_dir_ent), GFP_KERNEL);
  265. if ( !ent ) {
  266. kfree(sl->data);
  267. clear_bit(n,sbi->symlink_bitmap);
  268. unlock_kernel();
  269. return -ENOSPC;
  270. }
  271. ent->name = kmalloc(dentry->d_name.len+1, GFP_KERNEL);
  272. if ( !ent->name ) {
  273. kfree(sl->data);
  274. kfree(ent);
  275. clear_bit(n,sbi->symlink_bitmap);
  276. unlock_kernel();
  277. return -ENOSPC;
  278. }
  279. memcpy(sl->data,symname,slsize);
  280. sl->mtime = get_seconds();
  281. ent->ino = AUTOFS_FIRST_SYMLINK + n;
  282. ent->hash = dentry->d_name.hash;
  283. memcpy(ent->name, dentry->d_name.name, 1+(ent->len = dentry->d_name.len));
  284. ent->dentry = NULL; /* We don't keep the dentry for symlinks */
  285. autofs_hash_insert(dh,ent);
  286. d_instantiate(dentry, iget(dir->i_sb,ent->ino));
  287. unlock_kernel();
  288. return 0;
  289. }
  290. /*
  291. * NOTE!
  292. *
  293. * Normal filesystems would do a "d_delete()" to tell the VFS dcache
  294. * that the file no longer exists. However, doing that means that the
  295. * VFS layer can turn the dentry into a negative dentry, which we
  296. * obviously do not want (we're dropping the entry not because it
  297. * doesn't exist, but because it has timed out).
  298. *
  299. * Also see autofs_root_rmdir()..
  300. */
  301. static int autofs_root_unlink(struct inode *dir, struct dentry *dentry)
  302. {
  303. struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
  304. struct autofs_dirhash *dh = &sbi->dirhash;
  305. struct autofs_dir_ent *ent;
  306. unsigned int n;
  307. /* This allows root to remove symlinks */
  308. lock_kernel();
  309. if ( !autofs_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) ) {
  310. unlock_kernel();
  311. return -EACCES;
  312. }
  313. ent = autofs_hash_lookup(dh, &dentry->d_name);
  314. if ( !ent ) {
  315. unlock_kernel();
  316. return -ENOENT;
  317. }
  318. n = ent->ino - AUTOFS_FIRST_SYMLINK;
  319. if ( n >= AUTOFS_MAX_SYMLINKS ) {
  320. unlock_kernel();
  321. return -EISDIR; /* It's a directory, dummy */
  322. }
  323. if ( !test_bit(n,sbi->symlink_bitmap) ) {
  324. unlock_kernel();
  325. return -EINVAL; /* Nonexistent symlink? Shouldn't happen */
  326. }
  327. dentry->d_time = (unsigned long)(struct autofs_dirhash *)NULL;
  328. autofs_hash_delete(ent);
  329. clear_bit(n,sbi->symlink_bitmap);
  330. kfree(sbi->symlink[n].data);
  331. d_drop(dentry);
  332. unlock_kernel();
  333. return 0;
  334. }
  335. static int autofs_root_rmdir(struct inode *dir, struct dentry *dentry)
  336. {
  337. struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
  338. struct autofs_dirhash *dh = &sbi->dirhash;
  339. struct autofs_dir_ent *ent;
  340. lock_kernel();
  341. if ( !autofs_oz_mode(sbi) ) {
  342. unlock_kernel();
  343. return -EACCES;
  344. }
  345. ent = autofs_hash_lookup(dh, &dentry->d_name);
  346. if ( !ent ) {
  347. unlock_kernel();
  348. return -ENOENT;
  349. }
  350. if ( (unsigned int)ent->ino < AUTOFS_FIRST_DIR_INO ) {
  351. unlock_kernel();
  352. return -ENOTDIR; /* Not a directory */
  353. }
  354. if ( ent->dentry != dentry ) {
  355. printk("autofs_rmdir: odentry != dentry for entry %s\n", dentry->d_name.name);
  356. }
  357. dentry->d_time = (unsigned long)(struct autofs_dir_ent *)NULL;
  358. autofs_hash_delete(ent);
  359. drop_nlink(dir);
  360. d_drop(dentry);
  361. unlock_kernel();
  362. return 0;
  363. }
  364. static int autofs_root_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  365. {
  366. struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
  367. struct autofs_dirhash *dh = &sbi->dirhash;
  368. struct autofs_dir_ent *ent;
  369. ino_t ino;
  370. lock_kernel();
  371. if ( !autofs_oz_mode(sbi) ) {
  372. unlock_kernel();
  373. return -EACCES;
  374. }
  375. ent = autofs_hash_lookup(dh, &dentry->d_name);
  376. if ( ent ) {
  377. unlock_kernel();
  378. return -EEXIST;
  379. }
  380. if ( sbi->next_dir_ino < AUTOFS_FIRST_DIR_INO ) {
  381. printk("autofs: Out of inode numbers -- what the heck did you do??\n");
  382. unlock_kernel();
  383. return -ENOSPC;
  384. }
  385. ino = sbi->next_dir_ino++;
  386. ent = kmalloc(sizeof(struct autofs_dir_ent), GFP_KERNEL);
  387. if ( !ent ) {
  388. unlock_kernel();
  389. return -ENOSPC;
  390. }
  391. ent->name = kmalloc(dentry->d_name.len+1, GFP_KERNEL);
  392. if ( !ent->name ) {
  393. kfree(ent);
  394. unlock_kernel();
  395. return -ENOSPC;
  396. }
  397. ent->hash = dentry->d_name.hash;
  398. memcpy(ent->name, dentry->d_name.name, 1+(ent->len = dentry->d_name.len));
  399. ent->ino = ino;
  400. ent->dentry = dentry;
  401. autofs_hash_insert(dh,ent);
  402. inc_nlink(dir);
  403. d_instantiate(dentry, iget(dir->i_sb,ino));
  404. unlock_kernel();
  405. return 0;
  406. }
  407. /* Get/set timeout ioctl() operation */
  408. static inline int autofs_get_set_timeout(struct autofs_sb_info *sbi,
  409. unsigned long __user *p)
  410. {
  411. unsigned long ntimeout;
  412. if (get_user(ntimeout, p) ||
  413. put_user(sbi->exp_timeout / HZ, p))
  414. return -EFAULT;
  415. if ( ntimeout > ULONG_MAX/HZ )
  416. sbi->exp_timeout = 0;
  417. else
  418. sbi->exp_timeout = ntimeout * HZ;
  419. return 0;
  420. }
  421. /* Return protocol version */
  422. static inline int autofs_get_protover(int __user *p)
  423. {
  424. return put_user(AUTOFS_PROTO_VERSION, p);
  425. }
  426. /* Perform an expiry operation */
  427. static inline int autofs_expire_run(struct super_block *sb,
  428. struct autofs_sb_info *sbi,
  429. struct vfsmount *mnt,
  430. struct autofs_packet_expire __user *pkt_p)
  431. {
  432. struct autofs_dir_ent *ent;
  433. struct autofs_packet_expire pkt;
  434. memset(&pkt,0,sizeof pkt);
  435. pkt.hdr.proto_version = AUTOFS_PROTO_VERSION;
  436. pkt.hdr.type = autofs_ptype_expire;
  437. if ( !sbi->exp_timeout ||
  438. !(ent = autofs_expire(sb,sbi,mnt)) )
  439. return -EAGAIN;
  440. pkt.len = ent->len;
  441. memcpy(pkt.name, ent->name, pkt.len);
  442. pkt.name[pkt.len] = '\0';
  443. if ( copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)) )
  444. return -EFAULT;
  445. return 0;
  446. }
  447. /*
  448. * ioctl()'s on the root directory is the chief method for the daemon to
  449. * generate kernel reactions
  450. */
  451. static int autofs_root_ioctl(struct inode *inode, struct file *filp,
  452. unsigned int cmd, unsigned long arg)
  453. {
  454. struct autofs_sb_info *sbi = autofs_sbi(inode->i_sb);
  455. void __user *argp = (void __user *)arg;
  456. DPRINTK(("autofs_ioctl: cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u\n",cmd,arg,sbi,process_group(current)));
  457. if ( _IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
  458. _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT )
  459. return -ENOTTY;
  460. if ( !autofs_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
  461. return -EPERM;
  462. switch(cmd) {
  463. case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
  464. return autofs_wait_release(sbi,(autofs_wqt_t)arg,0);
  465. case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
  466. return autofs_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
  467. case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
  468. autofs_catatonic_mode(sbi);
  469. return 0;
  470. case AUTOFS_IOC_PROTOVER: /* Get protocol version */
  471. return autofs_get_protover(argp);
  472. case AUTOFS_IOC_SETTIMEOUT:
  473. return autofs_get_set_timeout(sbi, argp);
  474. case AUTOFS_IOC_EXPIRE:
  475. return autofs_expire_run(inode->i_sb, sbi, filp->f_path.mnt,
  476. argp);
  477. default:
  478. return -ENOSYS;
  479. }
  480. }