root.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  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 struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *);
  27. static void *autofs4_follow_link(struct dentry *, struct nameidata *);
  28. #define TRIGGER_FLAGS (LOOKUP_CONTINUE | LOOKUP_DIRECTORY)
  29. #define TRIGGER_INTENTS (LOOKUP_OPEN | LOOKUP_CREATE)
  30. const struct file_operations autofs4_root_operations = {
  31. .open = dcache_dir_open,
  32. .release = dcache_dir_close,
  33. .read = generic_read_dir,
  34. .readdir = dcache_readdir,
  35. .llseek = dcache_dir_lseek,
  36. .ioctl = autofs4_root_ioctl,
  37. };
  38. const struct file_operations autofs4_dir_operations = {
  39. .open = autofs4_dir_open,
  40. .release = dcache_dir_close,
  41. .read = generic_read_dir,
  42. .readdir = dcache_readdir,
  43. .llseek = dcache_dir_lseek,
  44. };
  45. const struct inode_operations autofs4_indirect_root_inode_operations = {
  46. .lookup = autofs4_lookup,
  47. .unlink = autofs4_dir_unlink,
  48. .symlink = autofs4_dir_symlink,
  49. .mkdir = autofs4_dir_mkdir,
  50. .rmdir = autofs4_dir_rmdir,
  51. };
  52. const struct inode_operations autofs4_direct_root_inode_operations = {
  53. .lookup = autofs4_lookup,
  54. .unlink = autofs4_dir_unlink,
  55. .mkdir = autofs4_dir_mkdir,
  56. .rmdir = autofs4_dir_rmdir,
  57. .follow_link = autofs4_follow_link,
  58. };
  59. const struct inode_operations autofs4_dir_inode_operations = {
  60. .lookup = autofs4_lookup,
  61. .unlink = autofs4_dir_unlink,
  62. .symlink = autofs4_dir_symlink,
  63. .mkdir = autofs4_dir_mkdir,
  64. .rmdir = autofs4_dir_rmdir,
  65. };
  66. static void autofs4_add_active(struct dentry *dentry)
  67. {
  68. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  69. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  70. if (ino) {
  71. spin_lock(&sbi->lookup_lock);
  72. if (!ino->active_count) {
  73. if (list_empty(&ino->active))
  74. list_add(&ino->active, &sbi->active_list);
  75. }
  76. ino->active_count++;
  77. spin_unlock(&sbi->lookup_lock);
  78. }
  79. return;
  80. }
  81. static void autofs4_del_active(struct dentry *dentry)
  82. {
  83. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  84. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  85. if (ino) {
  86. spin_lock(&sbi->lookup_lock);
  87. ino->active_count--;
  88. if (!ino->active_count) {
  89. if (!list_empty(&ino->active))
  90. list_del_init(&ino->active);
  91. }
  92. spin_unlock(&sbi->lookup_lock);
  93. }
  94. return;
  95. }
  96. static unsigned int autofs4_need_mount(unsigned int flags)
  97. {
  98. unsigned int res = 0;
  99. if (flags & (TRIGGER_FLAGS | TRIGGER_INTENTS))
  100. res = 1;
  101. return res;
  102. }
  103. static int autofs4_dir_open(struct inode *inode, struct file *file)
  104. {
  105. struct dentry *dentry = file->f_path.dentry;
  106. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  107. DPRINTK("file=%p dentry=%p %.*s",
  108. file, dentry, dentry->d_name.len, dentry->d_name.name);
  109. if (autofs4_oz_mode(sbi))
  110. goto out;
  111. /*
  112. * An empty directory in an autofs file system is always a
  113. * mount point. The daemon must have failed to mount this
  114. * during lookup so it doesn't exist. This can happen, for
  115. * example, if user space returns an incorrect status for a
  116. * mount request. Otherwise we're doing a readdir on the
  117. * autofs file system so just let the libfs routines handle
  118. * it.
  119. */
  120. spin_lock(&dcache_lock);
  121. if (!d_mountpoint(dentry) && __simple_empty(dentry)) {
  122. spin_unlock(&dcache_lock);
  123. return -ENOENT;
  124. }
  125. spin_unlock(&dcache_lock);
  126. out:
  127. return dcache_dir_open(inode, file);
  128. }
  129. static int try_to_fill_dentry(struct dentry *dentry, int flags)
  130. {
  131. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  132. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  133. int status;
  134. DPRINTK("dentry=%p %.*s ino=%p",
  135. dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
  136. /*
  137. * Wait for a pending mount, triggering one if there
  138. * isn't one already
  139. */
  140. if (dentry->d_inode == NULL) {
  141. DPRINTK("waiting for mount name=%.*s",
  142. dentry->d_name.len, dentry->d_name.name);
  143. status = autofs4_wait(sbi, dentry, NFY_MOUNT);
  144. DPRINTK("mount done status=%d", status);
  145. /* Turn this into a real negative dentry? */
  146. if (status == -ENOENT) {
  147. spin_lock(&sbi->fs_lock);
  148. ino->flags &= ~AUTOFS_INF_PENDING;
  149. spin_unlock(&sbi->fs_lock);
  150. return status;
  151. } else if (status) {
  152. /* Return a negative dentry, but leave it "pending" */
  153. return status;
  154. }
  155. /* Trigger mount for path component or follow link */
  156. } else if (ino->flags & AUTOFS_INF_PENDING ||
  157. autofs4_need_mount(flags) ||
  158. current->link_count) {
  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) && __simple_empty(dentry))) {
  228. spin_unlock(&dcache_lock);
  229. spin_unlock(&sbi->fs_lock);
  230. status = try_to_fill_dentry(dentry, 0);
  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) &&
  299. __simple_empty(dentry)) {
  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 autofs_sb_info *sbi, struct dentry *parent, struct qstr *name)
  350. {
  351. unsigned int len = name->len;
  352. unsigned int hash = name->hash;
  353. const unsigned char *str = name->name;
  354. struct list_head *p, *head;
  355. spin_lock(&dcache_lock);
  356. spin_lock(&sbi->lookup_lock);
  357. head = &sbi->active_list;
  358. list_for_each(p, head) {
  359. struct autofs_info *ino;
  360. struct dentry *dentry;
  361. struct qstr *qstr;
  362. ino = list_entry(p, struct autofs_info, active);
  363. dentry = ino->dentry;
  364. spin_lock(&dentry->d_lock);
  365. /* Already gone? */
  366. if (atomic_read(&dentry->d_count) == 0)
  367. goto next;
  368. qstr = &dentry->d_name;
  369. if (dentry->d_name.hash != hash)
  370. goto next;
  371. if (dentry->d_parent != parent)
  372. goto next;
  373. if (qstr->len != len)
  374. goto next;
  375. if (memcmp(qstr->name, str, len))
  376. goto next;
  377. if (d_unhashed(dentry)) {
  378. dget(dentry);
  379. spin_unlock(&dentry->d_lock);
  380. spin_unlock(&sbi->lookup_lock);
  381. spin_unlock(&dcache_lock);
  382. return dentry;
  383. }
  384. next:
  385. spin_unlock(&dentry->d_lock);
  386. }
  387. spin_unlock(&sbi->lookup_lock);
  388. spin_unlock(&dcache_lock);
  389. return NULL;
  390. }
  391. static struct dentry *autofs4_lookup_expiring(struct autofs_sb_info *sbi, struct dentry *parent, struct qstr *name)
  392. {
  393. unsigned int len = name->len;
  394. unsigned int hash = name->hash;
  395. const unsigned char *str = name->name;
  396. struct list_head *p, *head;
  397. spin_lock(&dcache_lock);
  398. spin_lock(&sbi->lookup_lock);
  399. head = &sbi->expiring_list;
  400. list_for_each(p, head) {
  401. struct autofs_info *ino;
  402. struct dentry *dentry;
  403. struct qstr *qstr;
  404. ino = list_entry(p, struct autofs_info, expiring);
  405. dentry = ino->dentry;
  406. spin_lock(&dentry->d_lock);
  407. /* Bad luck, we've already been dentry_iput */
  408. if (!dentry->d_inode)
  409. goto next;
  410. qstr = &dentry->d_name;
  411. if (dentry->d_name.hash != hash)
  412. goto next;
  413. if (dentry->d_parent != parent)
  414. goto next;
  415. if (qstr->len != len)
  416. goto next;
  417. if (memcmp(qstr->name, str, len))
  418. goto next;
  419. if (d_unhashed(dentry)) {
  420. dget(dentry);
  421. spin_unlock(&dentry->d_lock);
  422. spin_unlock(&sbi->lookup_lock);
  423. spin_unlock(&dcache_lock);
  424. return dentry;
  425. }
  426. next:
  427. spin_unlock(&dentry->d_lock);
  428. }
  429. spin_unlock(&sbi->lookup_lock);
  430. spin_unlock(&dcache_lock);
  431. return NULL;
  432. }
  433. /* Lookups in the root directory */
  434. static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  435. {
  436. struct autofs_sb_info *sbi;
  437. struct autofs_info *ino;
  438. struct dentry *expiring, *unhashed;
  439. int oz_mode;
  440. DPRINTK("name = %.*s",
  441. dentry->d_name.len, dentry->d_name.name);
  442. /* File name too long to exist */
  443. if (dentry->d_name.len > NAME_MAX)
  444. return ERR_PTR(-ENAMETOOLONG);
  445. sbi = autofs4_sbi(dir->i_sb);
  446. oz_mode = autofs4_oz_mode(sbi);
  447. DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
  448. current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode);
  449. unhashed = autofs4_lookup_active(sbi, dentry->d_parent, &dentry->d_name);
  450. if (unhashed) {
  451. dentry = unhashed;
  452. ino = autofs4_dentry_ino(dentry);
  453. } else {
  454. /*
  455. * Mark the dentry incomplete but don't hash it. We do this
  456. * to serialize our inode creation operations (symlink and
  457. * mkdir) which prevents deadlock during the callback to
  458. * the daemon. Subsequent user space lookups for the same
  459. * dentry are placed on the wait queue while the daemon
  460. * itself is allowed passage unresticted so the create
  461. * operation itself can then hash the dentry. Finally,
  462. * we check for the hashed dentry and return the newly
  463. * hashed dentry.
  464. */
  465. dentry->d_op = &autofs4_root_dentry_operations;
  466. /*
  467. * And we need to ensure that the same dentry is used for
  468. * all following lookup calls until it is hashed so that
  469. * the dentry flags are persistent throughout the request.
  470. */
  471. ino = autofs4_init_ino(NULL, sbi, 0555);
  472. if (!ino)
  473. return ERR_PTR(-ENOMEM);
  474. dentry->d_fsdata = ino;
  475. ino->dentry = dentry;
  476. autofs4_add_active(dentry);
  477. d_instantiate(dentry, NULL);
  478. }
  479. if (!oz_mode) {
  480. mutex_unlock(&dir->i_mutex);
  481. expiring = autofs4_lookup_expiring(sbi,
  482. dentry->d_parent,
  483. &dentry->d_name);
  484. if (expiring) {
  485. /*
  486. * If we are racing with expire the request might not
  487. * be quite complete but the directory has been removed
  488. * so it must have been successful, so just wait for it.
  489. */
  490. autofs4_expire_wait(expiring);
  491. autofs4_del_expiring(expiring);
  492. dput(expiring);
  493. }
  494. spin_lock(&sbi->fs_lock);
  495. ino->flags |= AUTOFS_INF_PENDING;
  496. spin_unlock(&sbi->fs_lock);
  497. if (dentry->d_op && dentry->d_op->d_revalidate)
  498. (dentry->d_op->d_revalidate)(dentry, nd);
  499. mutex_lock(&dir->i_mutex);
  500. }
  501. /*
  502. * If we are still pending, check if we had to handle
  503. * a signal. If so we can force a restart..
  504. */
  505. if (ino->flags & AUTOFS_INF_PENDING) {
  506. /* See if we were interrupted */
  507. if (signal_pending(current)) {
  508. sigset_t *sigset = &current->pending.signal;
  509. if (sigismember (sigset, SIGKILL) ||
  510. sigismember (sigset, SIGQUIT) ||
  511. sigismember (sigset, SIGINT)) {
  512. if (unhashed)
  513. dput(unhashed);
  514. return ERR_PTR(-ERESTARTNOINTR);
  515. }
  516. }
  517. if (!oz_mode) {
  518. spin_lock(&sbi->fs_lock);
  519. ino->flags &= ~AUTOFS_INF_PENDING;
  520. spin_unlock(&sbi->fs_lock);
  521. }
  522. }
  523. /*
  524. * If this dentry is unhashed, then we shouldn't honour this
  525. * lookup. Returning ENOENT here doesn't do the right thing
  526. * for all system calls, but it should be OK for the operations
  527. * we permit from an autofs.
  528. */
  529. if (!oz_mode && d_unhashed(dentry)) {
  530. /*
  531. * A user space application can (and has done in the past)
  532. * remove and re-create this directory during the callback.
  533. * This can leave us with an unhashed dentry, but a
  534. * successful mount! So we need to perform another
  535. * cached lookup in case the dentry now exists.
  536. */
  537. struct dentry *parent = dentry->d_parent;
  538. struct dentry *new = d_lookup(parent, &dentry->d_name);
  539. if (new != NULL)
  540. dentry = new;
  541. else
  542. dentry = ERR_PTR(-ENOENT);
  543. if (unhashed)
  544. dput(unhashed);
  545. return dentry;
  546. }
  547. if (unhashed)
  548. return unhashed;
  549. return NULL;
  550. }
  551. static int autofs4_dir_symlink(struct inode *dir,
  552. struct dentry *dentry,
  553. const char *symname)
  554. {
  555. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  556. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  557. struct autofs_info *p_ino;
  558. struct inode *inode;
  559. char *cp;
  560. DPRINTK("%s <- %.*s", symname,
  561. dentry->d_name.len, dentry->d_name.name);
  562. if (!autofs4_oz_mode(sbi))
  563. return -EACCES;
  564. ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
  565. if (!ino)
  566. return -ENOMEM;
  567. autofs4_del_active(dentry);
  568. ino->size = strlen(symname);
  569. cp = kmalloc(ino->size + 1, GFP_KERNEL);
  570. if (!cp) {
  571. if (!dentry->d_fsdata)
  572. kfree(ino);
  573. return -ENOMEM;
  574. }
  575. strcpy(cp, symname);
  576. inode = autofs4_get_inode(dir->i_sb, ino);
  577. if (!inode) {
  578. kfree(cp);
  579. if (!dentry->d_fsdata)
  580. kfree(ino);
  581. return -ENOMEM;
  582. }
  583. d_add(dentry, inode);
  584. if (dir == dir->i_sb->s_root->d_inode)
  585. dentry->d_op = &autofs4_root_dentry_operations;
  586. else
  587. dentry->d_op = &autofs4_dentry_operations;
  588. dentry->d_fsdata = ino;
  589. ino->dentry = dget(dentry);
  590. atomic_inc(&ino->count);
  591. p_ino = autofs4_dentry_ino(dentry->d_parent);
  592. if (p_ino && dentry->d_parent != dentry)
  593. atomic_inc(&p_ino->count);
  594. ino->inode = inode;
  595. ino->u.symlink = cp;
  596. dir->i_mtime = CURRENT_TIME;
  597. return 0;
  598. }
  599. /*
  600. * NOTE!
  601. *
  602. * Normal filesystems would do a "d_delete()" to tell the VFS dcache
  603. * that the file no longer exists. However, doing that means that the
  604. * VFS layer can turn the dentry into a negative dentry. We don't want
  605. * this, because the unlink is probably the result of an expire.
  606. * We simply d_drop it and add it to a expiring list in the super block,
  607. * which allows the dentry lookup to check for an incomplete expire.
  608. *
  609. * If a process is blocked on the dentry waiting for the expire to finish,
  610. * it will invalidate the dentry and try to mount with a new one.
  611. *
  612. * Also see autofs4_dir_rmdir()..
  613. */
  614. static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
  615. {
  616. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  617. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  618. struct autofs_info *p_ino;
  619. /* This allows root to remove symlinks */
  620. if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
  621. return -EACCES;
  622. if (atomic_dec_and_test(&ino->count)) {
  623. p_ino = autofs4_dentry_ino(dentry->d_parent);
  624. if (p_ino && dentry->d_parent != dentry)
  625. atomic_dec(&p_ino->count);
  626. }
  627. dput(ino->dentry);
  628. dentry->d_inode->i_size = 0;
  629. clear_nlink(dentry->d_inode);
  630. dir->i_mtime = CURRENT_TIME;
  631. spin_lock(&dcache_lock);
  632. autofs4_add_expiring(dentry);
  633. spin_lock(&dentry->d_lock);
  634. __d_drop(dentry);
  635. spin_unlock(&dentry->d_lock);
  636. spin_unlock(&dcache_lock);
  637. return 0;
  638. }
  639. static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
  640. {
  641. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  642. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  643. struct autofs_info *p_ino;
  644. DPRINTK("dentry %p, removing %.*s",
  645. dentry, dentry->d_name.len, dentry->d_name.name);
  646. if (!autofs4_oz_mode(sbi))
  647. return -EACCES;
  648. spin_lock(&dcache_lock);
  649. if (!list_empty(&dentry->d_subdirs)) {
  650. spin_unlock(&dcache_lock);
  651. return -ENOTEMPTY;
  652. }
  653. autofs4_add_expiring(dentry);
  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)
  682. return -ENOMEM;
  683. autofs4_del_active(dentry);
  684. inode = autofs4_get_inode(dir->i_sb, ino);
  685. if (!inode) {
  686. if (!dentry->d_fsdata)
  687. kfree(ino);
  688. return -ENOMEM;
  689. }
  690. d_add(dentry, inode);
  691. if (dir == dir->i_sb->s_root->d_inode)
  692. dentry->d_op = &autofs4_root_dentry_operations;
  693. else
  694. dentry->d_op = &autofs4_dentry_operations;
  695. dentry->d_fsdata = ino;
  696. ino->dentry = dget(dentry);
  697. atomic_inc(&ino->count);
  698. p_ino = autofs4_dentry_ino(dentry->d_parent);
  699. if (p_ino && dentry->d_parent != dentry)
  700. atomic_inc(&p_ino->count);
  701. ino->inode = inode;
  702. inc_nlink(dir);
  703. dir->i_mtime = CURRENT_TIME;
  704. return 0;
  705. }
  706. /* Get/set timeout ioctl() operation */
  707. static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
  708. unsigned long __user *p)
  709. {
  710. int rv;
  711. unsigned long ntimeout;
  712. if ((rv = get_user(ntimeout, p)) ||
  713. (rv = put_user(sbi->exp_timeout/HZ, p)))
  714. return rv;
  715. if (ntimeout > ULONG_MAX/HZ)
  716. sbi->exp_timeout = 0;
  717. else
  718. sbi->exp_timeout = ntimeout * HZ;
  719. return 0;
  720. }
  721. /* Return protocol version */
  722. static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
  723. {
  724. return put_user(sbi->version, p);
  725. }
  726. /* Return protocol sub version */
  727. static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
  728. {
  729. return put_user(sbi->sub_version, p);
  730. }
  731. /*
  732. * Tells the daemon whether it can umount the autofs mount.
  733. */
  734. static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
  735. {
  736. int status = 0;
  737. if (may_umount(mnt))
  738. status = 1;
  739. DPRINTK("returning %d", status);
  740. status = put_user(status, p);
  741. return status;
  742. }
  743. /* Identify autofs4_dentries - this is so we can tell if there's
  744. an extra dentry refcount or not. We only hold a refcount on the
  745. dentry if its non-negative (ie, d_inode != NULL)
  746. */
  747. int is_autofs4_dentry(struct dentry *dentry)
  748. {
  749. return dentry && dentry->d_inode &&
  750. (dentry->d_op == &autofs4_root_dentry_operations ||
  751. dentry->d_op == &autofs4_dentry_operations) &&
  752. dentry->d_fsdata != NULL;
  753. }
  754. /*
  755. * ioctl()'s on the root directory is the chief method for the daemon to
  756. * generate kernel reactions
  757. */
  758. static int autofs4_root_ioctl(struct inode *inode, struct file *filp,
  759. unsigned int cmd, unsigned long arg)
  760. {
  761. struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
  762. void __user *p = (void __user *)arg;
  763. DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
  764. cmd,arg,sbi,task_pgrp_nr(current));
  765. if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
  766. _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
  767. return -ENOTTY;
  768. if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
  769. return -EPERM;
  770. switch(cmd) {
  771. case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
  772. return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
  773. case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
  774. return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
  775. case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
  776. autofs4_catatonic_mode(sbi);
  777. return 0;
  778. case AUTOFS_IOC_PROTOVER: /* Get protocol version */
  779. return autofs4_get_protover(sbi, p);
  780. case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
  781. return autofs4_get_protosubver(sbi, p);
  782. case AUTOFS_IOC_SETTIMEOUT:
  783. return autofs4_get_set_timeout(sbi, p);
  784. case AUTOFS_IOC_ASKUMOUNT:
  785. return autofs4_ask_umount(filp->f_path.mnt, p);
  786. /* return a single thing to expire */
  787. case AUTOFS_IOC_EXPIRE:
  788. return autofs4_expire_run(inode->i_sb,filp->f_path.mnt,sbi, p);
  789. /* same as above, but can send multiple expires through pipe */
  790. case AUTOFS_IOC_EXPIRE_MULTI:
  791. return autofs4_expire_multi(inode->i_sb,filp->f_path.mnt,sbi, p);
  792. default:
  793. return -ENOSYS;
  794. }
  795. }