root.c 26 KB

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