PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/fs/autofs/root.c

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