root.c 24 KB

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