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 break 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))
  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)) {
  202. status = -ENOENT;
  203. goto out_error;
  204. }
  205. }
  206. done:
  207. return NULL;
  208. out_error:
  209. path_put(&nd->path);
  210. return ERR_PTR(status);
  211. }
  212. /*
  213. * Revalidate is called on every cache lookup. Some of those
  214. * cache lookups may actually happen while the dentry is not
  215. * yet completely filled in, and revalidate has to delay such
  216. * lookups..
  217. */
  218. static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
  219. {
  220. struct inode *dir = dentry->d_parent->d_inode;
  221. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  222. int oz_mode = autofs4_oz_mode(sbi);
  223. int flags = nd ? nd->flags : 0;
  224. int status = 1;
  225. /* Pending dentry */
  226. spin_lock(&sbi->fs_lock);
  227. if (autofs4_ispending(dentry)) {
  228. /* The daemon never causes a mount to trigger */
  229. spin_unlock(&sbi->fs_lock);
  230. if (oz_mode)
  231. return 1;
  232. /*
  233. * If the directory has gone away due to an expire
  234. * we have been called as ->d_revalidate() and so
  235. * we need to return false and proceed to ->lookup().
  236. */
  237. if (autofs4_expire_wait(dentry) == -EAGAIN)
  238. return 0;
  239. /*
  240. * A zero status is success otherwise we have a
  241. * negative error code.
  242. */
  243. status = try_to_fill_dentry(dentry, flags);
  244. if (status == 0)
  245. return 1;
  246. return status;
  247. }
  248. spin_unlock(&sbi->fs_lock);
  249. /* Negative dentry.. invalidate if "old" */
  250. if (dentry->d_inode == NULL)
  251. return 0;
  252. /* Check for a non-mountpoint directory with no contents */
  253. spin_lock(&dcache_lock);
  254. if (S_ISDIR(dentry->d_inode->i_mode) &&
  255. !d_mountpoint(dentry) &&
  256. __simple_empty(dentry)) {
  257. DPRINTK("dentry=%p %.*s, emptydir",
  258. dentry, dentry->d_name.len, dentry->d_name.name);
  259. spin_unlock(&dcache_lock);
  260. /* The daemon never causes a mount to trigger */
  261. if (oz_mode)
  262. return 1;
  263. /*
  264. * A zero status is success otherwise we have a
  265. * negative error code.
  266. */
  267. status = try_to_fill_dentry(dentry, flags);
  268. if (status == 0)
  269. return 1;
  270. return status;
  271. }
  272. spin_unlock(&dcache_lock);
  273. return 1;
  274. }
  275. void autofs4_dentry_release(struct dentry *de)
  276. {
  277. struct autofs_info *inf;
  278. DPRINTK("releasing %p", de);
  279. inf = autofs4_dentry_ino(de);
  280. de->d_fsdata = NULL;
  281. if (inf) {
  282. struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb);
  283. if (sbi) {
  284. spin_lock(&sbi->lookup_lock);
  285. if (!list_empty(&inf->active))
  286. list_del(&inf->active);
  287. if (!list_empty(&inf->expiring))
  288. list_del(&inf->expiring);
  289. spin_unlock(&sbi->lookup_lock);
  290. }
  291. inf->dentry = NULL;
  292. inf->inode = NULL;
  293. autofs4_free_ino(inf);
  294. }
  295. }
  296. /* For dentries of directories in the root dir */
  297. static const struct dentry_operations autofs4_root_dentry_operations = {
  298. .d_revalidate = autofs4_revalidate,
  299. .d_release = autofs4_dentry_release,
  300. };
  301. /* For other dentries */
  302. static const struct dentry_operations autofs4_dentry_operations = {
  303. .d_revalidate = autofs4_revalidate,
  304. .d_release = autofs4_dentry_release,
  305. };
  306. static struct dentry *autofs4_lookup_active(struct autofs_sb_info *sbi, struct dentry *parent, struct qstr *name)
  307. {
  308. unsigned int len = name->len;
  309. unsigned int hash = name->hash;
  310. const unsigned char *str = name->name;
  311. struct list_head *p, *head;
  312. spin_lock(&dcache_lock);
  313. spin_lock(&sbi->lookup_lock);
  314. head = &sbi->active_list;
  315. list_for_each(p, head) {
  316. struct autofs_info *ino;
  317. struct dentry *dentry;
  318. struct qstr *qstr;
  319. ino = list_entry(p, struct autofs_info, active);
  320. dentry = ino->dentry;
  321. spin_lock(&dentry->d_lock);
  322. /* Already gone? */
  323. if (atomic_read(&dentry->d_count) == 0)
  324. goto next;
  325. qstr = &dentry->d_name;
  326. if (dentry->d_name.hash != hash)
  327. goto next;
  328. if (dentry->d_parent != parent)
  329. goto next;
  330. if (qstr->len != len)
  331. goto next;
  332. if (memcmp(qstr->name, str, len))
  333. goto next;
  334. if (d_unhashed(dentry)) {
  335. dget(dentry);
  336. spin_unlock(&dentry->d_lock);
  337. spin_unlock(&sbi->lookup_lock);
  338. spin_unlock(&dcache_lock);
  339. return dentry;
  340. }
  341. next:
  342. spin_unlock(&dentry->d_lock);
  343. }
  344. spin_unlock(&sbi->lookup_lock);
  345. spin_unlock(&dcache_lock);
  346. return NULL;
  347. }
  348. static struct dentry *autofs4_lookup_expiring(struct autofs_sb_info *sbi, struct dentry *parent, struct qstr *name)
  349. {
  350. unsigned int len = name->len;
  351. unsigned int hash = name->hash;
  352. const unsigned char *str = name->name;
  353. struct list_head *p, *head;
  354. spin_lock(&dcache_lock);
  355. spin_lock(&sbi->lookup_lock);
  356. head = &sbi->expiring_list;
  357. list_for_each(p, head) {
  358. struct autofs_info *ino;
  359. struct dentry *dentry;
  360. struct qstr *qstr;
  361. ino = list_entry(p, struct autofs_info, expiring);
  362. dentry = ino->dentry;
  363. spin_lock(&dentry->d_lock);
  364. /* Bad luck, we've already been dentry_iput */
  365. if (!dentry->d_inode)
  366. goto next;
  367. qstr = &dentry->d_name;
  368. if (dentry->d_name.hash != hash)
  369. goto next;
  370. if (dentry->d_parent != parent)
  371. goto next;
  372. if (qstr->len != len)
  373. goto next;
  374. if (memcmp(qstr->name, str, len))
  375. goto next;
  376. if (d_unhashed(dentry)) {
  377. dget(dentry);
  378. spin_unlock(&dentry->d_lock);
  379. spin_unlock(&sbi->lookup_lock);
  380. spin_unlock(&dcache_lock);
  381. return dentry;
  382. }
  383. next:
  384. spin_unlock(&dentry->d_lock);
  385. }
  386. spin_unlock(&sbi->lookup_lock);
  387. spin_unlock(&dcache_lock);
  388. return NULL;
  389. }
  390. /* Lookups in the root directory */
  391. static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  392. {
  393. struct autofs_sb_info *sbi;
  394. struct autofs_info *ino;
  395. struct dentry *expiring, *unhashed;
  396. int oz_mode;
  397. DPRINTK("name = %.*s",
  398. dentry->d_name.len, dentry->d_name.name);
  399. /* File name too long to exist */
  400. if (dentry->d_name.len > NAME_MAX)
  401. return ERR_PTR(-ENAMETOOLONG);
  402. sbi = autofs4_sbi(dir->i_sb);
  403. oz_mode = autofs4_oz_mode(sbi);
  404. DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
  405. current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode);
  406. unhashed = autofs4_lookup_active(sbi, dentry->d_parent, &dentry->d_name);
  407. if (unhashed)
  408. dentry = unhashed;
  409. else {
  410. /*
  411. * Mark the dentry incomplete but don't hash it. We do this
  412. * to serialize our inode creation operations (symlink and
  413. * mkdir) which prevents deadlock during the callback to
  414. * the daemon. Subsequent user space lookups for the same
  415. * dentry are placed on the wait queue while the daemon
  416. * itself is allowed passage unresticted so the create
  417. * operation itself can then hash the dentry. Finally,
  418. * we check for the hashed dentry and return the newly
  419. * hashed dentry.
  420. */
  421. dentry->d_op = &autofs4_root_dentry_operations;
  422. /*
  423. * And we need to ensure that the same dentry is used for
  424. * all following lookup calls until it is hashed so that
  425. * the dentry flags are persistent throughout the request.
  426. */
  427. ino = autofs4_init_ino(NULL, sbi, 0555);
  428. if (!ino)
  429. return ERR_PTR(-ENOMEM);
  430. dentry->d_fsdata = ino;
  431. ino->dentry = dentry;
  432. spin_lock(&sbi->lookup_lock);
  433. list_add(&ino->active, &sbi->active_list);
  434. spin_unlock(&sbi->lookup_lock);
  435. d_instantiate(dentry, NULL);
  436. }
  437. if (!oz_mode) {
  438. mutex_unlock(&dir->i_mutex);
  439. expiring = autofs4_lookup_expiring(sbi,
  440. dentry->d_parent,
  441. &dentry->d_name);
  442. if (expiring) {
  443. /*
  444. * If we are racing with expire the request might not
  445. * be quite complete but the directory has been removed
  446. * so it must have been successful, so just wait for it.
  447. */
  448. ino = autofs4_dentry_ino(expiring);
  449. autofs4_expire_wait(expiring);
  450. spin_lock(&sbi->lookup_lock);
  451. if (!list_empty(&ino->expiring))
  452. list_del_init(&ino->expiring);
  453. spin_unlock(&sbi->lookup_lock);
  454. dput(expiring);
  455. }
  456. spin_lock(&dentry->d_lock);
  457. dentry->d_flags |= DCACHE_AUTOFS_PENDING;
  458. spin_unlock(&dentry->d_lock);
  459. if (dentry->d_op && dentry->d_op->d_revalidate)
  460. (dentry->d_op->d_revalidate)(dentry, nd);
  461. mutex_lock(&dir->i_mutex);
  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. }