root.c 24 KB

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