root.c 24 KB

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