root.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. /* -*- c -*- --------------------------------------------------------------- *
  2. *
  3. * linux/fs/autofs/root.c
  4. *
  5. * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
  6. * Copyright 1999-2000 Jeremy Fitzhardinge <jeremy@goop.org>
  7. * Copyright 2001-2006 Ian Kent <raven@themaw.net>
  8. *
  9. * This file is part of the Linux kernel and is made available under
  10. * the terms of the GNU General Public License, version 2, or at your
  11. * option, any later version, incorporated herein by reference.
  12. *
  13. * ------------------------------------------------------------------------- */
  14. #include <linux/capability.h>
  15. #include <linux/errno.h>
  16. #include <linux/stat.h>
  17. #include <linux/param.h>
  18. #include <linux/time.h>
  19. #include "autofs_i.h"
  20. static int autofs4_dir_symlink(struct inode *,struct dentry *,const char *);
  21. static int autofs4_dir_unlink(struct inode *,struct dentry *);
  22. static int autofs4_dir_rmdir(struct inode *,struct dentry *);
  23. static int autofs4_dir_mkdir(struct inode *,struct dentry *,int);
  24. static int autofs4_root_ioctl(struct inode *, struct file *,unsigned int,unsigned long);
  25. static int autofs4_dir_open(struct inode *inode, struct file *file);
  26. static int autofs4_dir_close(struct inode *inode, struct file *file);
  27. static int autofs4_dir_readdir(struct file * filp, void * dirent, filldir_t filldir);
  28. static int autofs4_root_readdir(struct file * filp, void * dirent, filldir_t filldir);
  29. static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *);
  30. static void *autofs4_follow_link(struct dentry *, struct nameidata *);
  31. const struct file_operations autofs4_root_operations = {
  32. .open = dcache_dir_open,
  33. .release = dcache_dir_close,
  34. .read = generic_read_dir,
  35. .readdir = autofs4_root_readdir,
  36. .ioctl = autofs4_root_ioctl,
  37. };
  38. const struct file_operations autofs4_dir_operations = {
  39. .open = autofs4_dir_open,
  40. .release = autofs4_dir_close,
  41. .read = generic_read_dir,
  42. .readdir = autofs4_dir_readdir,
  43. };
  44. const struct inode_operations autofs4_indirect_root_inode_operations = {
  45. .lookup = autofs4_lookup,
  46. .unlink = autofs4_dir_unlink,
  47. .symlink = autofs4_dir_symlink,
  48. .mkdir = autofs4_dir_mkdir,
  49. .rmdir = autofs4_dir_rmdir,
  50. };
  51. const struct inode_operations autofs4_direct_root_inode_operations = {
  52. .lookup = autofs4_lookup,
  53. .unlink = autofs4_dir_unlink,
  54. .mkdir = autofs4_dir_mkdir,
  55. .rmdir = autofs4_dir_rmdir,
  56. .follow_link = autofs4_follow_link,
  57. };
  58. const struct inode_operations autofs4_dir_inode_operations = {
  59. .lookup = autofs4_lookup,
  60. .unlink = autofs4_dir_unlink,
  61. .symlink = autofs4_dir_symlink,
  62. .mkdir = autofs4_dir_mkdir,
  63. .rmdir = autofs4_dir_rmdir,
  64. };
  65. static int autofs4_root_readdir(struct file *file, void *dirent,
  66. filldir_t filldir)
  67. {
  68. struct autofs_sb_info *sbi = autofs4_sbi(file->f_path.dentry->d_sb);
  69. int oz_mode = autofs4_oz_mode(sbi);
  70. DPRINTK("called, filp->f_pos = %lld", file->f_pos);
  71. /*
  72. * Don't set reghost flag if:
  73. * 1) f_pos is larger than zero -- we've already been here.
  74. * 2) we haven't even enabled reghosting in the 1st place.
  75. * 3) this is the daemon doing a readdir
  76. */
  77. if (oz_mode && file->f_pos == 0 && sbi->reghost_enabled)
  78. sbi->needs_reghost = 1;
  79. DPRINTK("needs_reghost = %d", sbi->needs_reghost);
  80. return dcache_readdir(file, dirent, filldir);
  81. }
  82. static int autofs4_dir_open(struct inode *inode, struct file *file)
  83. {
  84. struct dentry *dentry = file->f_path.dentry;
  85. struct vfsmount *mnt = file->f_path.mnt;
  86. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  87. struct dentry *cursor;
  88. int status;
  89. status = dcache_dir_open(inode, file);
  90. if (status)
  91. goto out;
  92. cursor = file->private_data;
  93. cursor->d_fsdata = NULL;
  94. DPRINTK("file=%p dentry=%p %.*s",
  95. file, dentry, dentry->d_name.len, dentry->d_name.name);
  96. if (autofs4_oz_mode(sbi))
  97. goto out;
  98. if (autofs4_ispending(dentry)) {
  99. DPRINTK("dentry busy");
  100. dcache_dir_close(inode, file);
  101. status = -EBUSY;
  102. goto out;
  103. }
  104. status = -ENOENT;
  105. if (!d_mountpoint(dentry) && dentry->d_op && dentry->d_op->d_revalidate) {
  106. struct nameidata nd;
  107. int empty, ret;
  108. /* In case there are stale directory dentrys from a failed mount */
  109. spin_lock(&dcache_lock);
  110. empty = list_empty(&dentry->d_subdirs);
  111. spin_unlock(&dcache_lock);
  112. if (!empty)
  113. d_invalidate(dentry);
  114. nd.flags = LOOKUP_DIRECTORY;
  115. ret = (dentry->d_op->d_revalidate)(dentry, &nd);
  116. if (ret <= 0) {
  117. if (ret < 0)
  118. status = ret;
  119. dcache_dir_close(inode, file);
  120. goto out;
  121. }
  122. }
  123. if (d_mountpoint(dentry)) {
  124. struct file *fp = NULL;
  125. struct path fp_path = { .dentry = dentry, .mnt = mnt };
  126. path_get(&fp_path);
  127. if (!autofs4_follow_mount(&fp_path.mnt, &fp_path.dentry)) {
  128. path_put(&fp_path);
  129. dcache_dir_close(inode, file);
  130. goto out;
  131. }
  132. fp = dentry_open(fp_path.dentry, fp_path.mnt, file->f_flags);
  133. status = PTR_ERR(fp);
  134. if (IS_ERR(fp)) {
  135. dcache_dir_close(inode, file);
  136. goto out;
  137. }
  138. cursor->d_fsdata = fp;
  139. }
  140. return 0;
  141. out:
  142. return status;
  143. }
  144. static int autofs4_dir_close(struct inode *inode, struct file *file)
  145. {
  146. struct dentry *dentry = file->f_path.dentry;
  147. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  148. struct dentry *cursor = file->private_data;
  149. int status = 0;
  150. DPRINTK("file=%p dentry=%p %.*s",
  151. file, dentry, dentry->d_name.len, dentry->d_name.name);
  152. if (autofs4_oz_mode(sbi))
  153. goto out;
  154. if (autofs4_ispending(dentry)) {
  155. DPRINTK("dentry busy");
  156. status = -EBUSY;
  157. goto out;
  158. }
  159. if (d_mountpoint(dentry)) {
  160. struct file *fp = cursor->d_fsdata;
  161. if (!fp) {
  162. status = -ENOENT;
  163. goto out;
  164. }
  165. filp_close(fp, current->files);
  166. }
  167. out:
  168. dcache_dir_close(inode, file);
  169. return status;
  170. }
  171. static int autofs4_dir_readdir(struct file *file, void *dirent, filldir_t filldir)
  172. {
  173. struct dentry *dentry = file->f_path.dentry;
  174. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  175. struct dentry *cursor = file->private_data;
  176. int status;
  177. DPRINTK("file=%p dentry=%p %.*s",
  178. file, dentry, dentry->d_name.len, dentry->d_name.name);
  179. if (autofs4_oz_mode(sbi))
  180. goto out;
  181. if (autofs4_ispending(dentry)) {
  182. DPRINTK("dentry busy");
  183. return -EBUSY;
  184. }
  185. if (d_mountpoint(dentry)) {
  186. struct file *fp = cursor->d_fsdata;
  187. if (!fp)
  188. return -ENOENT;
  189. if (!fp->f_op || !fp->f_op->readdir)
  190. goto out;
  191. status = vfs_readdir(fp, filldir, dirent);
  192. file->f_pos = fp->f_pos;
  193. if (status)
  194. autofs4_copy_atime(file, fp);
  195. return status;
  196. }
  197. out:
  198. return dcache_readdir(file, dirent, filldir);
  199. }
  200. static int try_to_fill_dentry(struct dentry *dentry, int flags)
  201. {
  202. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  203. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  204. int status;
  205. /* Block on any pending expiry here; invalidate the dentry
  206. when expiration is done to trigger mount request with a new
  207. dentry */
  208. if (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
  209. DPRINTK("waiting for expire %p name=%.*s",
  210. dentry, dentry->d_name.len, dentry->d_name.name);
  211. status = autofs4_wait(sbi, dentry, NFY_NONE);
  212. DPRINTK("expire done status=%d", status);
  213. /*
  214. * If the directory still exists the mount request must
  215. * continue otherwise it can't be followed at the right
  216. * time during the walk.
  217. */
  218. status = d_invalidate(dentry);
  219. if (status != -EBUSY)
  220. return -EAGAIN;
  221. }
  222. DPRINTK("dentry=%p %.*s ino=%p",
  223. dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
  224. /*
  225. * Wait for a pending mount, triggering one if there
  226. * isn't one already
  227. */
  228. if (dentry->d_inode == NULL) {
  229. DPRINTK("waiting for mount name=%.*s",
  230. dentry->d_name.len, dentry->d_name.name);
  231. status = autofs4_wait(sbi, dentry, NFY_MOUNT);
  232. DPRINTK("mount done status=%d", status);
  233. /* Turn this into a real negative dentry? */
  234. if (status == -ENOENT) {
  235. spin_lock(&dentry->d_lock);
  236. dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
  237. spin_unlock(&dentry->d_lock);
  238. return status;
  239. } else if (status) {
  240. /* Return a negative dentry, but leave it "pending" */
  241. return status;
  242. }
  243. /* Trigger mount for path component or follow link */
  244. } else if (flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY) ||
  245. current->link_count) {
  246. DPRINTK("waiting for mount name=%.*s",
  247. dentry->d_name.len, dentry->d_name.name);
  248. spin_lock(&dentry->d_lock);
  249. dentry->d_flags |= DCACHE_AUTOFS_PENDING;
  250. spin_unlock(&dentry->d_lock);
  251. status = autofs4_wait(sbi, dentry, NFY_MOUNT);
  252. DPRINTK("mount done status=%d", status);
  253. if (status) {
  254. spin_lock(&dentry->d_lock);
  255. dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
  256. spin_unlock(&dentry->d_lock);
  257. return status;
  258. }
  259. }
  260. /* Initialize expiry counter after successful mount */
  261. if (ino)
  262. ino->last_used = jiffies;
  263. spin_lock(&dentry->d_lock);
  264. dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
  265. spin_unlock(&dentry->d_lock);
  266. return 0;
  267. }
  268. /* For autofs direct mounts the follow link triggers the mount */
  269. static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd)
  270. {
  271. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  272. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  273. int oz_mode = autofs4_oz_mode(sbi);
  274. unsigned int lookup_type;
  275. int status;
  276. DPRINTK("dentry=%p %.*s oz_mode=%d nd->flags=%d",
  277. dentry, dentry->d_name.len, dentry->d_name.name, oz_mode,
  278. nd->flags);
  279. /* If it's our master or we shouldn't trigger a mount we're done */
  280. lookup_type = nd->flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY);
  281. if (oz_mode || !lookup_type)
  282. goto done;
  283. /* If an expire request is pending wait for it. */
  284. if (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
  285. DPRINTK("waiting for active request %p name=%.*s",
  286. dentry, dentry->d_name.len, dentry->d_name.name);
  287. status = autofs4_wait(sbi, dentry, NFY_NONE);
  288. DPRINTK("request done status=%d", status);
  289. }
  290. /*
  291. * If the dentry contains directories then it is an
  292. * autofs multi-mount with no root mount offset. So
  293. * don't try to mount it again.
  294. */
  295. spin_lock(&dcache_lock);
  296. if (!d_mountpoint(dentry) && __simple_empty(dentry)) {
  297. spin_unlock(&dcache_lock);
  298. status = try_to_fill_dentry(dentry, 0);
  299. if (status)
  300. goto out_error;
  301. /*
  302. * The mount succeeded but if there is no root mount
  303. * it must be an autofs multi-mount with no root offset
  304. * so we don't need to follow the mount.
  305. */
  306. if (d_mountpoint(dentry)) {
  307. if (!autofs4_follow_mount(&nd->path.mnt,
  308. &nd->path.dentry)) {
  309. status = -ENOENT;
  310. goto out_error;
  311. }
  312. }
  313. goto done;
  314. }
  315. spin_unlock(&dcache_lock);
  316. done:
  317. return NULL;
  318. out_error:
  319. path_put(&nd->path);
  320. return ERR_PTR(status);
  321. }
  322. /*
  323. * Revalidate is called on every cache lookup. Some of those
  324. * cache lookups may actually happen while the dentry is not
  325. * yet completely filled in, and revalidate has to delay such
  326. * lookups..
  327. */
  328. static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
  329. {
  330. struct inode *dir = dentry->d_parent->d_inode;
  331. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  332. int oz_mode = autofs4_oz_mode(sbi);
  333. int flags = nd ? nd->flags : 0;
  334. int status = 1;
  335. /* Pending dentry */
  336. if (autofs4_ispending(dentry)) {
  337. /* The daemon never causes a mount to trigger */
  338. if (oz_mode)
  339. return 1;
  340. /*
  341. * A zero status is success otherwise we have a
  342. * negative error code.
  343. */
  344. status = try_to_fill_dentry(dentry, flags);
  345. if (status == 0)
  346. return 1;
  347. /*
  348. * A status of EAGAIN here means that the dentry has gone
  349. * away while waiting for an expire to complete. If we are
  350. * racing with expire lookup will wait for it so this must
  351. * be a revalidate and we need to send it to lookup.
  352. */
  353. if (status == -EAGAIN)
  354. return 0;
  355. return status;
  356. }
  357. /* Negative dentry.. invalidate if "old" */
  358. if (dentry->d_inode == NULL)
  359. return 0;
  360. /* Check for a non-mountpoint directory with no contents */
  361. spin_lock(&dcache_lock);
  362. if (S_ISDIR(dentry->d_inode->i_mode) &&
  363. !d_mountpoint(dentry) &&
  364. __simple_empty(dentry)) {
  365. DPRINTK("dentry=%p %.*s, emptydir",
  366. dentry, dentry->d_name.len, dentry->d_name.name);
  367. spin_unlock(&dcache_lock);
  368. /* The daemon never causes a mount to trigger */
  369. if (oz_mode)
  370. return 1;
  371. /*
  372. * A zero status is success otherwise we have a
  373. * negative error code.
  374. */
  375. status = try_to_fill_dentry(dentry, flags);
  376. if (status == 0)
  377. return 1;
  378. return status;
  379. }
  380. spin_unlock(&dcache_lock);
  381. return 1;
  382. }
  383. void autofs4_dentry_release(struct dentry *de)
  384. {
  385. struct autofs_info *inf;
  386. DPRINTK("releasing %p", de);
  387. inf = autofs4_dentry_ino(de);
  388. de->d_fsdata = NULL;
  389. if (inf) {
  390. struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb);
  391. if (sbi) {
  392. spin_lock(&sbi->lookup_lock);
  393. if (!list_empty(&inf->expiring))
  394. list_del(&inf->expiring);
  395. spin_unlock(&sbi->lookup_lock);
  396. }
  397. inf->dentry = NULL;
  398. inf->inode = NULL;
  399. autofs4_free_ino(inf);
  400. }
  401. }
  402. /* For dentries of directories in the root dir */
  403. static struct dentry_operations autofs4_root_dentry_operations = {
  404. .d_revalidate = autofs4_revalidate,
  405. .d_release = autofs4_dentry_release,
  406. };
  407. /* For other dentries */
  408. static struct dentry_operations autofs4_dentry_operations = {
  409. .d_revalidate = autofs4_revalidate,
  410. .d_release = autofs4_dentry_release,
  411. };
  412. static struct dentry *autofs4_lookup_expiring(struct autofs_sb_info *sbi, struct dentry *parent, struct qstr *name)
  413. {
  414. unsigned int len = name->len;
  415. unsigned int hash = name->hash;
  416. const unsigned char *str = name->name;
  417. struct list_head *p, *head;
  418. spin_lock(&dcache_lock);
  419. spin_lock(&sbi->lookup_lock);
  420. head = &sbi->expiring_list;
  421. list_for_each(p, head) {
  422. struct autofs_info *ino;
  423. struct dentry *dentry;
  424. struct qstr *qstr;
  425. ino = list_entry(p, struct autofs_info, expiring);
  426. dentry = ino->dentry;
  427. spin_lock(&dentry->d_lock);
  428. /* Bad luck, we've already been dentry_iput */
  429. if (!dentry->d_inode)
  430. goto next;
  431. qstr = &dentry->d_name;
  432. if (dentry->d_name.hash != hash)
  433. goto next;
  434. if (dentry->d_parent != parent)
  435. goto next;
  436. if (qstr->len != len)
  437. goto next;
  438. if (memcmp(qstr->name, str, len))
  439. goto next;
  440. if (d_unhashed(dentry)) {
  441. dget(dentry);
  442. spin_unlock(&dentry->d_lock);
  443. spin_unlock(&sbi->lookup_lock);
  444. spin_unlock(&dcache_lock);
  445. return dentry;
  446. }
  447. next:
  448. spin_unlock(&dentry->d_lock);
  449. }
  450. spin_unlock(&sbi->lookup_lock);
  451. spin_unlock(&dcache_lock);
  452. return NULL;
  453. }
  454. /* Lookups in the root directory */
  455. static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  456. {
  457. struct autofs_sb_info *sbi;
  458. struct dentry *expiring;
  459. int oz_mode;
  460. DPRINTK("name = %.*s",
  461. dentry->d_name.len, dentry->d_name.name);
  462. /* File name too long to exist */
  463. if (dentry->d_name.len > NAME_MAX)
  464. return ERR_PTR(-ENAMETOOLONG);
  465. sbi = autofs4_sbi(dir->i_sb);
  466. oz_mode = autofs4_oz_mode(sbi);
  467. DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
  468. current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode);
  469. expiring = autofs4_lookup_expiring(sbi, dentry->d_parent, &dentry->d_name);
  470. if (expiring) {
  471. struct autofs_info *ino = autofs4_dentry_ino(expiring);
  472. /*
  473. * If we are racing with expire the request might not
  474. * be quite complete but the directory has been removed
  475. * so it must have been successful, so just wait for it.
  476. */
  477. while (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
  478. DPRINTK("wait for incomplete expire %p name=%.*s",
  479. expiring, expiring->d_name.len,
  480. expiring->d_name.name);
  481. autofs4_wait(sbi, expiring, NFY_NONE);
  482. DPRINTK("request completed");
  483. }
  484. spin_lock(&sbi->lookup_lock);
  485. if (!list_empty(&ino->expiring))
  486. list_del_init(&ino->expiring);
  487. spin_unlock(&sbi->lookup_lock);
  488. dput(expiring);
  489. }
  490. /*
  491. * Mark the dentry incomplete but don't hash it. We do this
  492. * to serialize our inode creation operations (symlink and
  493. * mkdir) which prevents deadlock during the callback to
  494. * the daemon. Subsequent user space lookups for the same
  495. * dentry are placed on the wait queue while the daemon
  496. * itself is allowed passage unresticted so the create
  497. * operation itself can then hash the dentry. Finally,
  498. * we check for the hashed dentry and return the newly
  499. * hashed dentry.
  500. */
  501. dentry->d_op = &autofs4_root_dentry_operations;
  502. dentry->d_fsdata = NULL;
  503. d_instantiate(dentry, NULL);
  504. if (!oz_mode) {
  505. spin_lock(&dentry->d_lock);
  506. dentry->d_flags |= DCACHE_AUTOFS_PENDING;
  507. spin_unlock(&dentry->d_lock);
  508. }
  509. if (dentry->d_op && dentry->d_op->d_revalidate) {
  510. mutex_unlock(&dir->i_mutex);
  511. (dentry->d_op->d_revalidate)(dentry, nd);
  512. mutex_lock(&dir->i_mutex);
  513. }
  514. /*
  515. * If we are still pending, check if we had to handle
  516. * a signal. If so we can force a restart..
  517. */
  518. if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
  519. /* See if we were interrupted */
  520. if (signal_pending(current)) {
  521. sigset_t *sigset = &current->pending.signal;
  522. if (sigismember (sigset, SIGKILL) ||
  523. sigismember (sigset, SIGQUIT) ||
  524. sigismember (sigset, SIGINT)) {
  525. return ERR_PTR(-ERESTARTNOINTR);
  526. }
  527. }
  528. spin_lock(&dentry->d_lock);
  529. dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
  530. spin_unlock(&dentry->d_lock);
  531. }
  532. /*
  533. * If this dentry is unhashed, then we shouldn't honour this
  534. * lookup. Returning ENOENT here doesn't do the right thing
  535. * for all system calls, but it should be OK for the operations
  536. * we permit from an autofs.
  537. */
  538. if (!oz_mode && d_unhashed(dentry)) {
  539. /*
  540. * A user space application can (and has done in the past)
  541. * remove and re-create this directory during the callback.
  542. * This can leave us with an unhashed dentry, but a
  543. * successful mount! So we need to perform another
  544. * cached lookup in case the dentry now exists.
  545. */
  546. struct dentry *parent = dentry->d_parent;
  547. struct dentry *new = d_lookup(parent, &dentry->d_name);
  548. if (new != NULL)
  549. dentry = new;
  550. else
  551. dentry = ERR_PTR(-ENOENT);
  552. return dentry;
  553. }
  554. return NULL;
  555. }
  556. static int autofs4_dir_symlink(struct inode *dir,
  557. struct dentry *dentry,
  558. const char *symname)
  559. {
  560. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  561. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  562. struct autofs_info *p_ino;
  563. struct inode *inode;
  564. char *cp;
  565. DPRINTK("%s <- %.*s", symname,
  566. dentry->d_name.len, dentry->d_name.name);
  567. if (!autofs4_oz_mode(sbi))
  568. return -EACCES;
  569. ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
  570. if (ino == NULL)
  571. return -ENOSPC;
  572. ino->size = strlen(symname);
  573. ino->u.symlink = cp = kmalloc(ino->size + 1, GFP_KERNEL);
  574. if (cp == NULL) {
  575. kfree(ino);
  576. return -ENOSPC;
  577. }
  578. strcpy(cp, symname);
  579. inode = autofs4_get_inode(dir->i_sb, ino);
  580. d_add(dentry, inode);
  581. if (dir == dir->i_sb->s_root->d_inode)
  582. dentry->d_op = &autofs4_root_dentry_operations;
  583. else
  584. dentry->d_op = &autofs4_dentry_operations;
  585. dentry->d_fsdata = ino;
  586. ino->dentry = dget(dentry);
  587. atomic_inc(&ino->count);
  588. p_ino = autofs4_dentry_ino(dentry->d_parent);
  589. if (p_ino && dentry->d_parent != dentry)
  590. atomic_inc(&p_ino->count);
  591. ino->inode = inode;
  592. dir->i_mtime = CURRENT_TIME;
  593. return 0;
  594. }
  595. /*
  596. * NOTE!
  597. *
  598. * Normal filesystems would do a "d_delete()" to tell the VFS dcache
  599. * that the file no longer exists. However, doing that means that the
  600. * VFS layer can turn the dentry into a negative dentry. We don't want
  601. * this, because the unlink is probably the result of an expire.
  602. * We simply d_drop it and add it to a expiring list in the super block,
  603. * which allows the dentry lookup to check for an incomplete expire.
  604. *
  605. * If a process is blocked on the dentry waiting for the expire to finish,
  606. * it will invalidate the dentry and try to mount with a new one.
  607. *
  608. * Also see autofs4_dir_rmdir()..
  609. */
  610. static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
  611. {
  612. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  613. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  614. struct autofs_info *p_ino;
  615. /* This allows root to remove symlinks */
  616. if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
  617. return -EACCES;
  618. if (atomic_dec_and_test(&ino->count)) {
  619. p_ino = autofs4_dentry_ino(dentry->d_parent);
  620. if (p_ino && dentry->d_parent != dentry)
  621. atomic_dec(&p_ino->count);
  622. }
  623. dput(ino->dentry);
  624. dentry->d_inode->i_size = 0;
  625. clear_nlink(dentry->d_inode);
  626. dir->i_mtime = CURRENT_TIME;
  627. spin_lock(&dcache_lock);
  628. spin_lock(&sbi->lookup_lock);
  629. list_add(&ino->expiring, &sbi->expiring_list);
  630. spin_unlock(&sbi->lookup_lock);
  631. spin_lock(&dentry->d_lock);
  632. __d_drop(dentry);
  633. spin_unlock(&dentry->d_lock);
  634. spin_unlock(&dcache_lock);
  635. return 0;
  636. }
  637. static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
  638. {
  639. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  640. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  641. struct autofs_info *p_ino;
  642. DPRINTK("dentry %p, removing %.*s",
  643. dentry, dentry->d_name.len, dentry->d_name.name);
  644. if (!autofs4_oz_mode(sbi))
  645. return -EACCES;
  646. spin_lock(&dcache_lock);
  647. if (!list_empty(&dentry->d_subdirs)) {
  648. spin_unlock(&dcache_lock);
  649. return -ENOTEMPTY;
  650. }
  651. spin_lock(&sbi->lookup_lock);
  652. list_add(&ino->expiring, &sbi->expiring_list);
  653. spin_unlock(&sbi->lookup_lock);
  654. spin_lock(&dentry->d_lock);
  655. __d_drop(dentry);
  656. spin_unlock(&dentry->d_lock);
  657. spin_unlock(&dcache_lock);
  658. if (atomic_dec_and_test(&ino->count)) {
  659. p_ino = autofs4_dentry_ino(dentry->d_parent);
  660. if (p_ino && dentry->d_parent != dentry)
  661. atomic_dec(&p_ino->count);
  662. }
  663. dput(ino->dentry);
  664. dentry->d_inode->i_size = 0;
  665. clear_nlink(dentry->d_inode);
  666. if (dir->i_nlink)
  667. drop_nlink(dir);
  668. return 0;
  669. }
  670. static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  671. {
  672. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  673. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  674. struct autofs_info *p_ino;
  675. struct inode *inode;
  676. if (!autofs4_oz_mode(sbi))
  677. return -EACCES;
  678. DPRINTK("dentry %p, creating %.*s",
  679. dentry, dentry->d_name.len, dentry->d_name.name);
  680. ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
  681. if (ino == NULL)
  682. return -ENOSPC;
  683. inode = autofs4_get_inode(dir->i_sb, ino);
  684. d_add(dentry, inode);
  685. if (dir == dir->i_sb->s_root->d_inode)
  686. dentry->d_op = &autofs4_root_dentry_operations;
  687. else
  688. dentry->d_op = &autofs4_dentry_operations;
  689. dentry->d_fsdata = ino;
  690. ino->dentry = dget(dentry);
  691. atomic_inc(&ino->count);
  692. p_ino = autofs4_dentry_ino(dentry->d_parent);
  693. if (p_ino && dentry->d_parent != dentry)
  694. atomic_inc(&p_ino->count);
  695. ino->inode = inode;
  696. inc_nlink(dir);
  697. dir->i_mtime = CURRENT_TIME;
  698. return 0;
  699. }
  700. /* Get/set timeout ioctl() operation */
  701. static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
  702. unsigned long __user *p)
  703. {
  704. int rv;
  705. unsigned long ntimeout;
  706. if ((rv = get_user(ntimeout, p)) ||
  707. (rv = put_user(sbi->exp_timeout/HZ, p)))
  708. return rv;
  709. if (ntimeout > ULONG_MAX/HZ)
  710. sbi->exp_timeout = 0;
  711. else
  712. sbi->exp_timeout = ntimeout * HZ;
  713. return 0;
  714. }
  715. /* Return protocol version */
  716. static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
  717. {
  718. return put_user(sbi->version, p);
  719. }
  720. /* Return protocol sub version */
  721. static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
  722. {
  723. return put_user(sbi->sub_version, p);
  724. }
  725. /*
  726. * Tells the daemon whether we need to reghost or not. Also, clears
  727. * the reghost_needed flag.
  728. */
  729. static inline int autofs4_ask_reghost(struct autofs_sb_info *sbi, int __user *p)
  730. {
  731. int status;
  732. DPRINTK("returning %d", sbi->needs_reghost);
  733. status = put_user(sbi->needs_reghost, p);
  734. if (status)
  735. return status;
  736. sbi->needs_reghost = 0;
  737. return 0;
  738. }
  739. /*
  740. * Enable / Disable reghosting ioctl() operation
  741. */
  742. static inline int autofs4_toggle_reghost(struct autofs_sb_info *sbi, int __user *p)
  743. {
  744. int status;
  745. int val;
  746. status = get_user(val, p);
  747. DPRINTK("reghost = %d", val);
  748. if (status)
  749. return status;
  750. /* turn on/off reghosting, with the val */
  751. sbi->reghost_enabled = val;
  752. return 0;
  753. }
  754. /*
  755. * Tells the daemon whether it can umount the autofs mount.
  756. */
  757. static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
  758. {
  759. int status = 0;
  760. if (may_umount(mnt))
  761. status = 1;
  762. DPRINTK("returning %d", status);
  763. status = put_user(status, p);
  764. return status;
  765. }
  766. /* Identify autofs4_dentries - this is so we can tell if there's
  767. an extra dentry refcount or not. We only hold a refcount on the
  768. dentry if its non-negative (ie, d_inode != NULL)
  769. */
  770. int is_autofs4_dentry(struct dentry *dentry)
  771. {
  772. return dentry && dentry->d_inode &&
  773. (dentry->d_op == &autofs4_root_dentry_operations ||
  774. dentry->d_op == &autofs4_dentry_operations) &&
  775. dentry->d_fsdata != NULL;
  776. }
  777. /*
  778. * ioctl()'s on the root directory is the chief method for the daemon to
  779. * generate kernel reactions
  780. */
  781. static int autofs4_root_ioctl(struct inode *inode, struct file *filp,
  782. unsigned int cmd, unsigned long arg)
  783. {
  784. struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
  785. void __user *p = (void __user *)arg;
  786. DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
  787. cmd,arg,sbi,task_pgrp_nr(current));
  788. if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
  789. _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
  790. return -ENOTTY;
  791. if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
  792. return -EPERM;
  793. switch(cmd) {
  794. case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
  795. return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
  796. case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
  797. return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
  798. case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
  799. autofs4_catatonic_mode(sbi);
  800. return 0;
  801. case AUTOFS_IOC_PROTOVER: /* Get protocol version */
  802. return autofs4_get_protover(sbi, p);
  803. case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
  804. return autofs4_get_protosubver(sbi, p);
  805. case AUTOFS_IOC_SETTIMEOUT:
  806. return autofs4_get_set_timeout(sbi, p);
  807. case AUTOFS_IOC_TOGGLEREGHOST:
  808. return autofs4_toggle_reghost(sbi, p);
  809. case AUTOFS_IOC_ASKREGHOST:
  810. return autofs4_ask_reghost(sbi, p);
  811. case AUTOFS_IOC_ASKUMOUNT:
  812. return autofs4_ask_umount(filp->f_path.mnt, p);
  813. /* return a single thing to expire */
  814. case AUTOFS_IOC_EXPIRE:
  815. return autofs4_expire_run(inode->i_sb,filp->f_path.mnt,sbi, p);
  816. /* same as above, but can send multiple expires through pipe */
  817. case AUTOFS_IOC_EXPIRE_MULTI:
  818. return autofs4_expire_multi(inode->i_sb,filp->f_path.mnt,sbi, p);
  819. default:
  820. return -ENOSYS;
  821. }
  822. }