root.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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 const 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 const 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. unhashed = autofs4_lookup_active(sbi, dentry->d_parent, &dentry->d_name);
  408. if (unhashed)
  409. dentry = unhashed;
  410. else {
  411. /*
  412. * Mark the dentry incomplete but don't hash it. We do this
  413. * to serialize our inode creation operations (symlink and
  414. * mkdir) which prevents deadlock during the callback to
  415. * the daemon. Subsequent user space lookups for the same
  416. * dentry are placed on the wait queue while the daemon
  417. * itself is allowed passage unresticted so the create
  418. * operation itself can then hash the dentry. Finally,
  419. * we check for the hashed dentry and return the newly
  420. * hashed dentry.
  421. */
  422. dentry->d_op = &autofs4_root_dentry_operations;
  423. /*
  424. * And we need to ensure that the same dentry is used for
  425. * all following lookup calls until it is hashed so that
  426. * the dentry flags are persistent throughout the request.
  427. */
  428. ino = autofs4_init_ino(NULL, sbi, 0555);
  429. if (!ino)
  430. return ERR_PTR(-ENOMEM);
  431. dentry->d_fsdata = ino;
  432. ino->dentry = dentry;
  433. spin_lock(&sbi->lookup_lock);
  434. list_add(&ino->active, &sbi->active_list);
  435. spin_unlock(&sbi->lookup_lock);
  436. d_instantiate(dentry, NULL);
  437. }
  438. if (!oz_mode) {
  439. mutex_unlock(&dir->i_mutex);
  440. expiring = autofs4_lookup_expiring(sbi,
  441. dentry->d_parent,
  442. &dentry->d_name);
  443. if (expiring) {
  444. /*
  445. * If we are racing with expire the request might not
  446. * be quite complete but the directory has been removed
  447. * so it must have been successful, so just wait for it.
  448. */
  449. ino = autofs4_dentry_ino(expiring);
  450. autofs4_expire_wait(expiring);
  451. spin_lock(&sbi->lookup_lock);
  452. if (!list_empty(&ino->expiring))
  453. list_del_init(&ino->expiring);
  454. spin_unlock(&sbi->lookup_lock);
  455. dput(expiring);
  456. }
  457. spin_lock(&dentry->d_lock);
  458. dentry->d_flags |= DCACHE_AUTOFS_PENDING;
  459. spin_unlock(&dentry->d_lock);
  460. if (dentry->d_op && dentry->d_op->d_revalidate)
  461. (dentry->d_op->d_revalidate)(dentry, nd);
  462. mutex_lock(&dir->i_mutex);
  463. }
  464. /*
  465. * If we are still pending, check if we had to handle
  466. * a signal. If so we can force a restart..
  467. */
  468. if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
  469. /* See if we were interrupted */
  470. if (signal_pending(current)) {
  471. sigset_t *sigset = &current->pending.signal;
  472. if (sigismember (sigset, SIGKILL) ||
  473. sigismember (sigset, SIGQUIT) ||
  474. sigismember (sigset, SIGINT)) {
  475. if (unhashed)
  476. dput(unhashed);
  477. return ERR_PTR(-ERESTARTNOINTR);
  478. }
  479. }
  480. if (!oz_mode) {
  481. spin_lock(&dentry->d_lock);
  482. dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
  483. spin_unlock(&dentry->d_lock);
  484. }
  485. }
  486. /*
  487. * If this dentry is unhashed, then we shouldn't honour this
  488. * lookup. Returning ENOENT here doesn't do the right thing
  489. * for all system calls, but it should be OK for the operations
  490. * we permit from an autofs.
  491. */
  492. if (!oz_mode && d_unhashed(dentry)) {
  493. /*
  494. * A user space application can (and has done in the past)
  495. * remove and re-create this directory during the callback.
  496. * This can leave us with an unhashed dentry, but a
  497. * successful mount! So we need to perform another
  498. * cached lookup in case the dentry now exists.
  499. */
  500. struct dentry *parent = dentry->d_parent;
  501. struct dentry *new = d_lookup(parent, &dentry->d_name);
  502. if (new != NULL)
  503. dentry = new;
  504. else
  505. dentry = ERR_PTR(-ENOENT);
  506. if (unhashed)
  507. dput(unhashed);
  508. return dentry;
  509. }
  510. if (unhashed)
  511. return unhashed;
  512. return NULL;
  513. }
  514. static int autofs4_dir_symlink(struct inode *dir,
  515. struct dentry *dentry,
  516. const char *symname)
  517. {
  518. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  519. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  520. struct autofs_info *p_ino;
  521. struct inode *inode;
  522. char *cp;
  523. DPRINTK("%s <- %.*s", symname,
  524. dentry->d_name.len, dentry->d_name.name);
  525. if (!autofs4_oz_mode(sbi))
  526. return -EACCES;
  527. ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
  528. if (!ino)
  529. return -ENOMEM;
  530. spin_lock(&sbi->lookup_lock);
  531. if (!list_empty(&ino->active))
  532. list_del_init(&ino->active);
  533. spin_unlock(&sbi->lookup_lock);
  534. ino->size = strlen(symname);
  535. cp = kmalloc(ino->size + 1, GFP_KERNEL);
  536. if (!cp) {
  537. if (!dentry->d_fsdata)
  538. kfree(ino);
  539. return -ENOMEM;
  540. }
  541. strcpy(cp, symname);
  542. inode = autofs4_get_inode(dir->i_sb, ino);
  543. if (!inode) {
  544. kfree(cp);
  545. if (!dentry->d_fsdata)
  546. kfree(ino);
  547. return -ENOMEM;
  548. }
  549. d_add(dentry, inode);
  550. if (dir == dir->i_sb->s_root->d_inode)
  551. dentry->d_op = &autofs4_root_dentry_operations;
  552. else
  553. dentry->d_op = &autofs4_dentry_operations;
  554. dentry->d_fsdata = ino;
  555. ino->dentry = dget(dentry);
  556. atomic_inc(&ino->count);
  557. p_ino = autofs4_dentry_ino(dentry->d_parent);
  558. if (p_ino && dentry->d_parent != dentry)
  559. atomic_inc(&p_ino->count);
  560. ino->inode = inode;
  561. ino->u.symlink = cp;
  562. dir->i_mtime = CURRENT_TIME;
  563. return 0;
  564. }
  565. /*
  566. * NOTE!
  567. *
  568. * Normal filesystems would do a "d_delete()" to tell the VFS dcache
  569. * that the file no longer exists. However, doing that means that the
  570. * VFS layer can turn the dentry into a negative dentry. We don't want
  571. * this, because the unlink is probably the result of an expire.
  572. * We simply d_drop it and add it to a expiring list in the super block,
  573. * which allows the dentry lookup to check for an incomplete expire.
  574. *
  575. * If a process is blocked on the dentry waiting for the expire to finish,
  576. * it will invalidate the dentry and try to mount with a new one.
  577. *
  578. * Also see autofs4_dir_rmdir()..
  579. */
  580. static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
  581. {
  582. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  583. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  584. struct autofs_info *p_ino;
  585. /* This allows root to remove symlinks */
  586. if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
  587. return -EACCES;
  588. if (atomic_dec_and_test(&ino->count)) {
  589. p_ino = autofs4_dentry_ino(dentry->d_parent);
  590. if (p_ino && dentry->d_parent != dentry)
  591. atomic_dec(&p_ino->count);
  592. }
  593. dput(ino->dentry);
  594. dentry->d_inode->i_size = 0;
  595. clear_nlink(dentry->d_inode);
  596. dir->i_mtime = CURRENT_TIME;
  597. spin_lock(&dcache_lock);
  598. spin_lock(&sbi->lookup_lock);
  599. if (list_empty(&ino->expiring))
  600. list_add(&ino->expiring, &sbi->expiring_list);
  601. spin_unlock(&sbi->lookup_lock);
  602. spin_lock(&dentry->d_lock);
  603. __d_drop(dentry);
  604. spin_unlock(&dentry->d_lock);
  605. spin_unlock(&dcache_lock);
  606. return 0;
  607. }
  608. static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
  609. {
  610. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  611. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  612. struct autofs_info *p_ino;
  613. DPRINTK("dentry %p, removing %.*s",
  614. dentry, dentry->d_name.len, dentry->d_name.name);
  615. if (!autofs4_oz_mode(sbi))
  616. return -EACCES;
  617. spin_lock(&dcache_lock);
  618. if (!list_empty(&dentry->d_subdirs)) {
  619. spin_unlock(&dcache_lock);
  620. return -ENOTEMPTY;
  621. }
  622. spin_lock(&sbi->lookup_lock);
  623. if (list_empty(&ino->expiring))
  624. list_add(&ino->expiring, &sbi->expiring_list);
  625. spin_unlock(&sbi->lookup_lock);
  626. spin_lock(&dentry->d_lock);
  627. __d_drop(dentry);
  628. spin_unlock(&dentry->d_lock);
  629. spin_unlock(&dcache_lock);
  630. if (atomic_dec_and_test(&ino->count)) {
  631. p_ino = autofs4_dentry_ino(dentry->d_parent);
  632. if (p_ino && dentry->d_parent != dentry)
  633. atomic_dec(&p_ino->count);
  634. }
  635. dput(ino->dentry);
  636. dentry->d_inode->i_size = 0;
  637. clear_nlink(dentry->d_inode);
  638. if (dir->i_nlink)
  639. drop_nlink(dir);
  640. return 0;
  641. }
  642. static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  643. {
  644. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  645. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  646. struct autofs_info *p_ino;
  647. struct inode *inode;
  648. if (!autofs4_oz_mode(sbi))
  649. return -EACCES;
  650. DPRINTK("dentry %p, creating %.*s",
  651. dentry, dentry->d_name.len, dentry->d_name.name);
  652. ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
  653. if (!ino)
  654. return -ENOMEM;
  655. spin_lock(&sbi->lookup_lock);
  656. if (!list_empty(&ino->active))
  657. list_del_init(&ino->active);
  658. spin_unlock(&sbi->lookup_lock);
  659. inode = autofs4_get_inode(dir->i_sb, ino);
  660. if (!inode) {
  661. if (!dentry->d_fsdata)
  662. kfree(ino);
  663. return -ENOMEM;
  664. }
  665. d_add(dentry, inode);
  666. if (dir == dir->i_sb->s_root->d_inode)
  667. dentry->d_op = &autofs4_root_dentry_operations;
  668. else
  669. dentry->d_op = &autofs4_dentry_operations;
  670. dentry->d_fsdata = ino;
  671. ino->dentry = dget(dentry);
  672. atomic_inc(&ino->count);
  673. p_ino = autofs4_dentry_ino(dentry->d_parent);
  674. if (p_ino && dentry->d_parent != dentry)
  675. atomic_inc(&p_ino->count);
  676. ino->inode = inode;
  677. inc_nlink(dir);
  678. dir->i_mtime = CURRENT_TIME;
  679. return 0;
  680. }
  681. /* Get/set timeout ioctl() operation */
  682. static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
  683. unsigned long __user *p)
  684. {
  685. int rv;
  686. unsigned long ntimeout;
  687. if ((rv = get_user(ntimeout, p)) ||
  688. (rv = put_user(sbi->exp_timeout/HZ, p)))
  689. return rv;
  690. if (ntimeout > ULONG_MAX/HZ)
  691. sbi->exp_timeout = 0;
  692. else
  693. sbi->exp_timeout = ntimeout * HZ;
  694. return 0;
  695. }
  696. /* Return protocol version */
  697. static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
  698. {
  699. return put_user(sbi->version, p);
  700. }
  701. /* Return protocol sub version */
  702. static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
  703. {
  704. return put_user(sbi->sub_version, p);
  705. }
  706. /*
  707. * Tells the daemon whether it can umount the autofs mount.
  708. */
  709. static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
  710. {
  711. int status = 0;
  712. if (may_umount(mnt))
  713. status = 1;
  714. DPRINTK("returning %d", status);
  715. status = put_user(status, p);
  716. return status;
  717. }
  718. /* Identify autofs4_dentries - this is so we can tell if there's
  719. an extra dentry refcount or not. We only hold a refcount on the
  720. dentry if its non-negative (ie, d_inode != NULL)
  721. */
  722. int is_autofs4_dentry(struct dentry *dentry)
  723. {
  724. return dentry && dentry->d_inode &&
  725. (dentry->d_op == &autofs4_root_dentry_operations ||
  726. dentry->d_op == &autofs4_dentry_operations) &&
  727. dentry->d_fsdata != NULL;
  728. }
  729. /*
  730. * ioctl()'s on the root directory is the chief method for the daemon to
  731. * generate kernel reactions
  732. */
  733. static int autofs4_root_ioctl(struct inode *inode, struct file *filp,
  734. unsigned int cmd, unsigned long arg)
  735. {
  736. struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
  737. void __user *p = (void __user *)arg;
  738. DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
  739. cmd,arg,sbi,task_pgrp_nr(current));
  740. if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
  741. _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
  742. return -ENOTTY;
  743. if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
  744. return -EPERM;
  745. switch(cmd) {
  746. case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
  747. return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
  748. case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
  749. return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
  750. case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
  751. autofs4_catatonic_mode(sbi);
  752. return 0;
  753. case AUTOFS_IOC_PROTOVER: /* Get protocol version */
  754. return autofs4_get_protover(sbi, p);
  755. case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
  756. return autofs4_get_protosubver(sbi, p);
  757. case AUTOFS_IOC_SETTIMEOUT:
  758. return autofs4_get_set_timeout(sbi, p);
  759. case AUTOFS_IOC_ASKUMOUNT:
  760. return autofs4_ask_umount(filp->f_path.mnt, p);
  761. /* return a single thing to expire */
  762. case AUTOFS_IOC_EXPIRE:
  763. return autofs4_expire_run(inode->i_sb,filp->f_path.mnt,sbi, p);
  764. /* same as above, but can send multiple expires through pipe */
  765. case AUTOFS_IOC_EXPIRE_MULTI:
  766. return autofs4_expire_multi(inode->i_sb,filp->f_path.mnt,sbi, p);
  767. default:
  768. return -ENOSYS;
  769. }
  770. }