root.c 24 KB

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