root.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  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/mutex.h>
  22. #include "autofs_i.h"
  23. DEFINE_SPINLOCK(autofs4_lock);
  24. static int autofs4_dir_symlink(struct inode *,struct dentry *,const char *);
  25. static int autofs4_dir_unlink(struct inode *,struct dentry *);
  26. static int autofs4_dir_rmdir(struct inode *,struct dentry *);
  27. static int autofs4_dir_mkdir(struct inode *,struct dentry *,int);
  28. static long autofs4_root_ioctl(struct file *,unsigned int,unsigned long);
  29. #ifdef CONFIG_COMPAT
  30. static long autofs4_root_compat_ioctl(struct file *,unsigned int,unsigned long);
  31. #endif
  32. static int autofs4_dir_open(struct inode *inode, struct file *file);
  33. static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *);
  34. #define TRIGGER_FLAGS (LOOKUP_CONTINUE | LOOKUP_DIRECTORY)
  35. #define TRIGGER_INTENTS (LOOKUP_OPEN | LOOKUP_CREATE)
  36. const struct file_operations autofs4_root_operations = {
  37. .open = dcache_dir_open,
  38. .release = dcache_dir_close,
  39. .read = generic_read_dir,
  40. .readdir = dcache_readdir,
  41. .llseek = dcache_dir_lseek,
  42. .unlocked_ioctl = autofs4_root_ioctl,
  43. #ifdef CONFIG_COMPAT
  44. .compat_ioctl = autofs4_root_compat_ioctl,
  45. #endif
  46. };
  47. const struct file_operations autofs4_dir_operations = {
  48. .open = autofs4_dir_open,
  49. .release = dcache_dir_close,
  50. .read = generic_read_dir,
  51. .readdir = dcache_readdir,
  52. .llseek = dcache_dir_lseek,
  53. };
  54. const struct inode_operations autofs4_indirect_root_inode_operations = {
  55. .lookup = autofs4_lookup,
  56. .unlink = autofs4_dir_unlink,
  57. .symlink = autofs4_dir_symlink,
  58. .mkdir = autofs4_dir_mkdir,
  59. .rmdir = autofs4_dir_rmdir,
  60. };
  61. const struct inode_operations autofs4_direct_root_inode_operations = {
  62. .lookup = autofs4_lookup,
  63. .unlink = autofs4_dir_unlink,
  64. .mkdir = autofs4_dir_mkdir,
  65. .rmdir = autofs4_dir_rmdir,
  66. };
  67. const struct inode_operations autofs4_dir_inode_operations = {
  68. .lookup = autofs4_lookup,
  69. .unlink = autofs4_dir_unlink,
  70. .symlink = autofs4_dir_symlink,
  71. .mkdir = autofs4_dir_mkdir,
  72. .rmdir = autofs4_dir_rmdir,
  73. };
  74. static void autofs4_add_active(struct dentry *dentry)
  75. {
  76. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  77. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  78. if (ino) {
  79. spin_lock(&sbi->lookup_lock);
  80. if (!ino->active_count) {
  81. if (list_empty(&ino->active))
  82. list_add(&ino->active, &sbi->active_list);
  83. }
  84. ino->active_count++;
  85. spin_unlock(&sbi->lookup_lock);
  86. }
  87. return;
  88. }
  89. static void autofs4_del_active(struct dentry *dentry)
  90. {
  91. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  92. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  93. if (ino) {
  94. spin_lock(&sbi->lookup_lock);
  95. ino->active_count--;
  96. if (!ino->active_count) {
  97. if (!list_empty(&ino->active))
  98. list_del_init(&ino->active);
  99. }
  100. spin_unlock(&sbi->lookup_lock);
  101. }
  102. return;
  103. }
  104. static unsigned int autofs4_need_mount(unsigned int flags)
  105. {
  106. unsigned int res = 0;
  107. if (flags & (TRIGGER_FLAGS | TRIGGER_INTENTS))
  108. res = 1;
  109. return res;
  110. }
  111. static int autofs4_dir_open(struct inode *inode, struct file *file)
  112. {
  113. struct dentry *dentry = file->f_path.dentry;
  114. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  115. DPRINTK("file=%p dentry=%p %.*s",
  116. file, dentry, dentry->d_name.len, dentry->d_name.name);
  117. if (autofs4_oz_mode(sbi))
  118. goto out;
  119. /*
  120. * An empty directory in an autofs file system is always a
  121. * mount point. The daemon must have failed to mount this
  122. * during lookup so it doesn't exist. This can happen, for
  123. * example, if user space returns an incorrect status for a
  124. * mount request. Otherwise we're doing a readdir on the
  125. * autofs file system so just let the libfs routines handle
  126. * it.
  127. */
  128. spin_lock(&autofs4_lock);
  129. spin_lock(&dentry->d_lock);
  130. if (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
  131. spin_unlock(&dentry->d_lock);
  132. spin_unlock(&autofs4_lock);
  133. return -ENOENT;
  134. }
  135. spin_unlock(&dentry->d_lock);
  136. spin_unlock(&autofs4_lock);
  137. out:
  138. return dcache_dir_open(inode, file);
  139. }
  140. static int try_to_fill_dentry(struct dentry *dentry, int flags)
  141. {
  142. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  143. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  144. int status;
  145. DPRINTK("dentry=%p %.*s ino=%p",
  146. dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
  147. /*
  148. * Wait for a pending mount, triggering one if there
  149. * isn't one already
  150. */
  151. if (dentry->d_inode == NULL) {
  152. DPRINTK("waiting for mount name=%.*s",
  153. dentry->d_name.len, dentry->d_name.name);
  154. status = autofs4_wait(sbi, dentry, NFY_MOUNT);
  155. DPRINTK("mount done status=%d", status);
  156. /* Turn this into a real negative dentry? */
  157. if (status == -ENOENT) {
  158. spin_lock(&sbi->fs_lock);
  159. ino->flags &= ~AUTOFS_INF_PENDING;
  160. spin_unlock(&sbi->fs_lock);
  161. return status;
  162. } else if (status) {
  163. /* Return a negative dentry, but leave it "pending" */
  164. return status;
  165. }
  166. /* Trigger mount for path component or follow link */
  167. } else if (ino->flags & AUTOFS_INF_PENDING ||
  168. autofs4_need_mount(flags)) {
  169. DPRINTK("waiting for mount name=%.*s",
  170. dentry->d_name.len, dentry->d_name.name);
  171. spin_lock(&sbi->fs_lock);
  172. ino->flags |= AUTOFS_INF_PENDING;
  173. spin_unlock(&sbi->fs_lock);
  174. status = autofs4_wait(sbi, dentry, NFY_MOUNT);
  175. DPRINTK("mount done status=%d", status);
  176. if (status) {
  177. spin_lock(&sbi->fs_lock);
  178. ino->flags &= ~AUTOFS_INF_PENDING;
  179. spin_unlock(&sbi->fs_lock);
  180. return status;
  181. }
  182. }
  183. /* Initialize expiry counter after successful mount */
  184. ino->last_used = jiffies;
  185. spin_lock(&sbi->fs_lock);
  186. ino->flags &= ~AUTOFS_INF_PENDING;
  187. spin_unlock(&sbi->fs_lock);
  188. return 0;
  189. }
  190. /* For autofs direct mounts the follow link triggers the mount */
  191. static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd)
  192. {
  193. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  194. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  195. int oz_mode = autofs4_oz_mode(sbi);
  196. unsigned int lookup_type;
  197. int status;
  198. DPRINTK("dentry=%p %.*s oz_mode=%d nd->flags=%d",
  199. dentry, dentry->d_name.len, dentry->d_name.name, oz_mode,
  200. nd->flags);
  201. /*
  202. * For an expire of a covered direct or offset mount we need
  203. * to break out of follow_down_one() at the autofs mount trigger
  204. * (d_mounted--), so we can see the expiring flag, and manage
  205. * the blocking and following here until the expire is completed.
  206. */
  207. if (oz_mode) {
  208. spin_lock(&sbi->fs_lock);
  209. if (ino->flags & AUTOFS_INF_EXPIRING) {
  210. spin_unlock(&sbi->fs_lock);
  211. /* Follow down to our covering mount. */
  212. if (!follow_down_one(&nd->path))
  213. goto done;
  214. goto follow;
  215. }
  216. spin_unlock(&sbi->fs_lock);
  217. goto done;
  218. }
  219. /* If an expire request is pending everyone must wait. */
  220. autofs4_expire_wait(dentry);
  221. /* We trigger a mount for almost all flags */
  222. lookup_type = autofs4_need_mount(nd->flags);
  223. spin_lock(&sbi->fs_lock);
  224. spin_lock(&autofs4_lock);
  225. spin_lock(&dentry->d_lock);
  226. if (!(lookup_type || ino->flags & AUTOFS_INF_PENDING)) {
  227. spin_unlock(&dentry->d_lock);
  228. spin_unlock(&autofs4_lock);
  229. spin_unlock(&sbi->fs_lock);
  230. goto follow;
  231. }
  232. /*
  233. * If the dentry contains directories then it is an autofs
  234. * multi-mount with no root mount offset. So don't try to
  235. * mount it again.
  236. */
  237. if (ino->flags & AUTOFS_INF_PENDING ||
  238. (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs))) {
  239. spin_unlock(&dentry->d_lock);
  240. spin_unlock(&autofs4_lock);
  241. spin_unlock(&sbi->fs_lock);
  242. status = try_to_fill_dentry(dentry, nd->flags);
  243. if (status)
  244. goto out_error;
  245. goto follow;
  246. }
  247. spin_unlock(&dentry->d_lock);
  248. spin_unlock(&autofs4_lock);
  249. spin_unlock(&sbi->fs_lock);
  250. follow:
  251. /*
  252. * If there is no root mount it must be an autofs
  253. * multi-mount with no root offset so we don't need
  254. * to follow it.
  255. */
  256. if (d_managed(dentry)) {
  257. status = follow_down(&nd->path, false);
  258. if (status < 0)
  259. goto out_error;
  260. }
  261. done:
  262. return NULL;
  263. out_error:
  264. path_put(&nd->path);
  265. return ERR_PTR(status);
  266. }
  267. /*
  268. * Revalidate is called on every cache lookup. Some of those
  269. * cache lookups may actually happen while the dentry is not
  270. * yet completely filled in, and revalidate has to delay such
  271. * lookups..
  272. */
  273. static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
  274. {
  275. struct inode *dir;
  276. struct autofs_sb_info *sbi;
  277. int oz_mode;
  278. int flags = nd ? nd->flags : 0;
  279. int status = 1;
  280. if (flags & LOOKUP_RCU)
  281. return -ECHILD;
  282. dir = dentry->d_parent->d_inode;
  283. sbi = autofs4_sbi(dir->i_sb);
  284. oz_mode = autofs4_oz_mode(sbi);
  285. /* Pending dentry */
  286. spin_lock(&sbi->fs_lock);
  287. if (autofs4_ispending(dentry)) {
  288. /* The daemon never causes a mount to trigger */
  289. spin_unlock(&sbi->fs_lock);
  290. if (oz_mode)
  291. return 1;
  292. /*
  293. * If the directory has gone away due to an expire
  294. * we have been called as ->d_revalidate() and so
  295. * we need to return false and proceed to ->lookup().
  296. */
  297. if (autofs4_expire_wait(dentry) == -EAGAIN)
  298. return 0;
  299. /*
  300. * A zero status is success otherwise we have a
  301. * negative error code.
  302. */
  303. status = try_to_fill_dentry(dentry, flags);
  304. if (status == 0)
  305. return 1;
  306. return status;
  307. }
  308. spin_unlock(&sbi->fs_lock);
  309. /* Negative dentry.. invalidate if "old" */
  310. if (dentry->d_inode == NULL)
  311. return 0;
  312. /* Check for a non-mountpoint directory with no contents */
  313. spin_lock(&autofs4_lock);
  314. spin_lock(&dentry->d_lock);
  315. if (S_ISDIR(dentry->d_inode->i_mode) &&
  316. !d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
  317. DPRINTK("dentry=%p %.*s, emptydir",
  318. dentry, dentry->d_name.len, dentry->d_name.name);
  319. spin_unlock(&dentry->d_lock);
  320. spin_unlock(&autofs4_lock);
  321. /* The daemon never causes a mount to trigger */
  322. if (oz_mode)
  323. return 1;
  324. /*
  325. * A zero status is success otherwise we have a
  326. * negative error code.
  327. */
  328. status = try_to_fill_dentry(dentry, flags);
  329. if (status == 0)
  330. return 1;
  331. return status;
  332. }
  333. spin_unlock(&dentry->d_lock);
  334. spin_unlock(&autofs4_lock);
  335. return 1;
  336. }
  337. void autofs4_dentry_release(struct dentry *de)
  338. {
  339. struct autofs_info *inf;
  340. DPRINTK("releasing %p", de);
  341. inf = autofs4_dentry_ino(de);
  342. de->d_fsdata = NULL;
  343. if (inf) {
  344. struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb);
  345. if (sbi) {
  346. spin_lock(&sbi->lookup_lock);
  347. if (!list_empty(&inf->active))
  348. list_del(&inf->active);
  349. if (!list_empty(&inf->expiring))
  350. list_del(&inf->expiring);
  351. spin_unlock(&sbi->lookup_lock);
  352. }
  353. inf->dentry = NULL;
  354. inf->inode = NULL;
  355. autofs4_free_ino(inf);
  356. }
  357. }
  358. /* For dentries of directories in the root dir */
  359. static const struct dentry_operations autofs4_root_dentry_operations = {
  360. .d_release = autofs4_dentry_release,
  361. };
  362. /* For other dentries */
  363. static const struct dentry_operations autofs4_dentry_operations = {
  364. .d_automount = autofs4_d_automount,
  365. .d_manage = autofs4_d_manage,
  366. .d_release = autofs4_dentry_release,
  367. };
  368. static struct dentry *autofs4_lookup_active(struct dentry *dentry)
  369. {
  370. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  371. struct dentry *parent = dentry->d_parent;
  372. struct qstr *name = &dentry->d_name;
  373. unsigned int len = name->len;
  374. unsigned int hash = name->hash;
  375. const unsigned char *str = name->name;
  376. struct list_head *p, *head;
  377. spin_lock(&autofs4_lock);
  378. spin_lock(&sbi->lookup_lock);
  379. head = &sbi->active_list;
  380. list_for_each(p, head) {
  381. struct autofs_info *ino;
  382. struct dentry *active;
  383. struct qstr *qstr;
  384. ino = list_entry(p, struct autofs_info, active);
  385. active = ino->dentry;
  386. spin_lock(&active->d_lock);
  387. /* Already gone? */
  388. if (active->d_count == 0)
  389. goto next;
  390. qstr = &active->d_name;
  391. if (active->d_name.hash != hash)
  392. goto next;
  393. if (active->d_parent != parent)
  394. goto next;
  395. if (qstr->len != len)
  396. goto next;
  397. if (memcmp(qstr->name, str, len))
  398. goto next;
  399. if (d_unhashed(active)) {
  400. dget_dlock(active);
  401. spin_unlock(&active->d_lock);
  402. spin_unlock(&sbi->lookup_lock);
  403. spin_unlock(&autofs4_lock);
  404. return active;
  405. }
  406. next:
  407. spin_unlock(&active->d_lock);
  408. }
  409. spin_unlock(&sbi->lookup_lock);
  410. spin_unlock(&autofs4_lock);
  411. return NULL;
  412. }
  413. static struct dentry *autofs4_lookup_expiring(struct dentry *dentry)
  414. {
  415. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  416. struct dentry *parent = dentry->d_parent;
  417. struct qstr *name = &dentry->d_name;
  418. unsigned int len = name->len;
  419. unsigned int hash = name->hash;
  420. const unsigned char *str = name->name;
  421. struct list_head *p, *head;
  422. spin_lock(&autofs4_lock);
  423. spin_lock(&sbi->lookup_lock);
  424. head = &sbi->expiring_list;
  425. list_for_each(p, head) {
  426. struct autofs_info *ino;
  427. struct dentry *expiring;
  428. struct qstr *qstr;
  429. ino = list_entry(p, struct autofs_info, expiring);
  430. expiring = ino->dentry;
  431. spin_lock(&expiring->d_lock);
  432. /* Bad luck, we've already been dentry_iput */
  433. if (!expiring->d_inode)
  434. goto next;
  435. qstr = &expiring->d_name;
  436. if (expiring->d_name.hash != hash)
  437. goto next;
  438. if (expiring->d_parent != parent)
  439. goto next;
  440. if (qstr->len != len)
  441. goto next;
  442. if (memcmp(qstr->name, str, len))
  443. goto next;
  444. if (d_unhashed(expiring)) {
  445. dget_dlock(expiring);
  446. spin_unlock(&expiring->d_lock);
  447. spin_unlock(&sbi->lookup_lock);
  448. spin_unlock(&autofs4_lock);
  449. return expiring;
  450. }
  451. next:
  452. spin_unlock(&expiring->d_lock);
  453. }
  454. spin_unlock(&sbi->lookup_lock);
  455. spin_unlock(&autofs4_lock);
  456. return NULL;
  457. }
  458. static int autofs4_mount_wait(struct dentry *dentry)
  459. {
  460. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  461. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  462. int status;
  463. if (ino->flags & AUTOFS_INF_PENDING) {
  464. DPRINTK("waiting for mount name=%.*s",
  465. dentry->d_name.len, dentry->d_name.name);
  466. status = autofs4_wait(sbi, dentry, NFY_MOUNT);
  467. DPRINTK("mount wait done status=%d", status);
  468. ino->last_used = jiffies;
  469. return status;
  470. }
  471. return 0;
  472. }
  473. static int do_expire_wait(struct dentry *dentry)
  474. {
  475. struct dentry *expiring;
  476. expiring = autofs4_lookup_expiring(dentry);
  477. if (!expiring)
  478. return autofs4_expire_wait(dentry);
  479. else {
  480. /*
  481. * If we are racing with expire the request might not
  482. * be quite complete, but the directory has been removed
  483. * so it must have been successful, just wait for it.
  484. */
  485. autofs4_expire_wait(expiring);
  486. autofs4_del_expiring(expiring);
  487. dput(expiring);
  488. }
  489. return 0;
  490. }
  491. static struct dentry *autofs4_mountpoint_changed(struct path *path)
  492. {
  493. struct dentry *dentry = path->dentry;
  494. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  495. /*
  496. * If this is an indirect mount the dentry could have gone away
  497. * as a result of an expire and a new one created.
  498. */
  499. if (autofs_type_indirect(sbi->type) && d_unhashed(dentry)) {
  500. struct dentry *parent = dentry->d_parent;
  501. struct dentry *new = d_lookup(parent, &dentry->d_name);
  502. if (!new)
  503. return NULL;
  504. dput(path->dentry);
  505. path->dentry = new;
  506. }
  507. return path->dentry;
  508. }
  509. struct vfsmount *autofs4_d_automount(struct path *path)
  510. {
  511. struct dentry *dentry = path->dentry;
  512. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  513. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  514. int status;
  515. DPRINTK("dentry=%p %.*s",
  516. dentry, dentry->d_name.len, dentry->d_name.name);
  517. /*
  518. * Someone may have manually umounted this or it was a submount
  519. * that has gone away.
  520. */
  521. spin_lock(&dentry->d_lock);
  522. if (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
  523. if (!(dentry->d_flags & DCACHE_MANAGE_TRANSIT) &&
  524. (dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
  525. __managed_dentry_set_transit(path->dentry);
  526. }
  527. spin_unlock(&dentry->d_lock);
  528. /* The daemon never triggers a mount. */
  529. if (autofs4_oz_mode(sbi))
  530. return NULL;
  531. /*
  532. * If an expire request is pending everyone must wait.
  533. * If the expire fails we're still mounted so continue
  534. * the follow and return. A return of -EAGAIN (which only
  535. * happens with indirect mounts) means the expire completed
  536. * and the directory was removed, so just go ahead and try
  537. * the mount.
  538. */
  539. status = do_expire_wait(dentry);
  540. if (status && status != -EAGAIN)
  541. return NULL;
  542. /* Callback to the daemon to perform the mount or wait */
  543. spin_lock(&sbi->fs_lock);
  544. if (ino->flags & AUTOFS_INF_PENDING) {
  545. spin_unlock(&sbi->fs_lock);
  546. status = autofs4_mount_wait(dentry);
  547. if (status)
  548. return ERR_PTR(status);
  549. spin_lock(&sbi->fs_lock);
  550. goto done;
  551. }
  552. /*
  553. * If the dentry is a symlink it's equivalent to a directory
  554. * having d_mountpoint() true, so there's no need to call back
  555. * to the daemon.
  556. */
  557. if (dentry->d_inode && S_ISLNK(dentry->d_inode->i_mode))
  558. goto done;
  559. if (!d_mountpoint(dentry)) {
  560. /*
  561. * It's possible that user space hasn't removed directories
  562. * after umounting a rootless multi-mount, although it
  563. * should. For v5 have_submounts() is sufficient to handle
  564. * this because the leaves of the directory tree under the
  565. * mount never trigger mounts themselves (they have an autofs
  566. * trigger mount mounted on them). But v4 pseudo direct mounts
  567. * do need the leaves to to trigger mounts. In this case we
  568. * have no choice but to use the list_empty() check and
  569. * require user space behave.
  570. */
  571. if (sbi->version > 4) {
  572. if (have_submounts(dentry))
  573. goto done;
  574. } else {
  575. spin_lock(&dentry->d_lock);
  576. if (!list_empty(&dentry->d_subdirs)) {
  577. spin_unlock(&dentry->d_lock);
  578. goto done;
  579. }
  580. spin_unlock(&dentry->d_lock);
  581. }
  582. ino->flags |= AUTOFS_INF_PENDING;
  583. spin_unlock(&sbi->fs_lock);
  584. status = autofs4_mount_wait(dentry);
  585. if (status)
  586. return ERR_PTR(status);
  587. spin_lock(&sbi->fs_lock);
  588. ino->flags &= ~AUTOFS_INF_PENDING;
  589. }
  590. done:
  591. if (!(ino->flags & AUTOFS_INF_EXPIRING)) {
  592. /*
  593. * Any needed mounting has been completed and the path updated
  594. * so turn this into a normal dentry so we don't continually
  595. * call ->d_automount() and ->d_manage().
  596. */
  597. spin_lock(&dentry->d_lock);
  598. __managed_dentry_clear_transit(dentry);
  599. /*
  600. * Only clear DMANAGED_AUTOMOUNT for rootless multi-mounts and
  601. * symlinks as in all other cases the dentry will be covered by
  602. * an actual mount so ->d_automount() won't be called during
  603. * the follow.
  604. */
  605. if ((!d_mountpoint(dentry) &&
  606. !list_empty(&dentry->d_subdirs)) ||
  607. (dentry->d_inode && S_ISLNK(dentry->d_inode->i_mode)))
  608. __managed_dentry_clear_automount(dentry);
  609. spin_unlock(&dentry->d_lock);
  610. }
  611. spin_unlock(&sbi->fs_lock);
  612. /* Mount succeeded, check if we ended up with a new dentry */
  613. dentry = autofs4_mountpoint_changed(path);
  614. if (!dentry)
  615. return ERR_PTR(-ENOENT);
  616. return NULL;
  617. }
  618. int autofs4_d_manage(struct dentry *dentry, bool mounting_here)
  619. {
  620. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  621. DPRINTK("dentry=%p %.*s",
  622. dentry, dentry->d_name.len, dentry->d_name.name);
  623. /* The daemon never waits. */
  624. if (autofs4_oz_mode(sbi) || mounting_here) {
  625. if (!d_mountpoint(dentry))
  626. return -EISDIR;
  627. return 0;
  628. }
  629. /* Wait for pending expires */
  630. do_expire_wait(dentry);
  631. /*
  632. * This dentry may be under construction so wait on mount
  633. * completion.
  634. */
  635. return autofs4_mount_wait(dentry);
  636. }
  637. /* Lookups in the root directory */
  638. static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  639. {
  640. struct autofs_sb_info *sbi;
  641. struct autofs_info *ino;
  642. struct dentry *active;
  643. DPRINTK("name = %.*s", dentry->d_name.len, dentry->d_name.name);
  644. /* File name too long to exist */
  645. if (dentry->d_name.len > NAME_MAX)
  646. return ERR_PTR(-ENAMETOOLONG);
  647. sbi = autofs4_sbi(dir->i_sb);
  648. DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
  649. current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode);
  650. active = autofs4_lookup_active(dentry);
  651. if (active) {
  652. return active;
  653. } else {
  654. d_set_d_op(dentry, &autofs4_root_dentry_operations);
  655. /*
  656. * A dentry that is not within the root can never trigger a
  657. * mount operation, unless the directory already exists, so we
  658. * can return fail immediately. The daemon however does need
  659. * to create directories within the file system.
  660. */
  661. if (!autofs4_oz_mode(sbi) && !IS_ROOT(dentry->d_parent))
  662. return ERR_PTR(-ENOENT);
  663. /* Mark entries in the root as mount triggers */
  664. if (autofs_type_indirect(sbi->type) && IS_ROOT(dentry->d_parent)) {
  665. d_set_d_op(dentry, &autofs4_dentry_operations);
  666. __managed_dentry_set_managed(dentry);
  667. }
  668. ino = autofs4_init_ino(NULL, sbi, 0555);
  669. if (!ino)
  670. return ERR_PTR(-ENOMEM);
  671. dentry->d_fsdata = ino;
  672. ino->dentry = dentry;
  673. autofs4_add_active(dentry);
  674. d_instantiate(dentry, NULL);
  675. }
  676. return NULL;
  677. }
  678. static int autofs4_dir_symlink(struct inode *dir,
  679. struct dentry *dentry,
  680. const char *symname)
  681. {
  682. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  683. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  684. struct autofs_info *p_ino;
  685. struct inode *inode;
  686. char *cp;
  687. DPRINTK("%s <- %.*s", symname,
  688. dentry->d_name.len, dentry->d_name.name);
  689. if (!autofs4_oz_mode(sbi))
  690. return -EACCES;
  691. ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
  692. if (!ino)
  693. return -ENOMEM;
  694. autofs4_del_active(dentry);
  695. ino->size = strlen(symname);
  696. cp = kmalloc(ino->size + 1, GFP_KERNEL);
  697. if (!cp) {
  698. if (!dentry->d_fsdata)
  699. kfree(ino);
  700. return -ENOMEM;
  701. }
  702. strcpy(cp, symname);
  703. inode = autofs4_get_inode(dir->i_sb, ino);
  704. if (!inode) {
  705. kfree(cp);
  706. if (!dentry->d_fsdata)
  707. kfree(ino);
  708. return -ENOMEM;
  709. }
  710. d_add(dentry, inode);
  711. dentry->d_fsdata = ino;
  712. ino->dentry = dget(dentry);
  713. atomic_inc(&ino->count);
  714. p_ino = autofs4_dentry_ino(dentry->d_parent);
  715. if (p_ino && dentry->d_parent != dentry)
  716. atomic_inc(&p_ino->count);
  717. ino->inode = inode;
  718. ino->u.symlink = cp;
  719. dir->i_mtime = CURRENT_TIME;
  720. return 0;
  721. }
  722. /*
  723. * NOTE!
  724. *
  725. * Normal filesystems would do a "d_delete()" to tell the VFS dcache
  726. * that the file no longer exists. However, doing that means that the
  727. * VFS layer can turn the dentry into a negative dentry. We don't want
  728. * this, because the unlink is probably the result of an expire.
  729. * We simply d_drop it and add it to a expiring list in the super block,
  730. * which allows the dentry lookup to check for an incomplete expire.
  731. *
  732. * If a process is blocked on the dentry waiting for the expire to finish,
  733. * it will invalidate the dentry and try to mount with a new one.
  734. *
  735. * Also see autofs4_dir_rmdir()..
  736. */
  737. static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
  738. {
  739. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  740. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  741. struct autofs_info *p_ino;
  742. /* This allows root to remove symlinks */
  743. if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
  744. return -EACCES;
  745. if (atomic_dec_and_test(&ino->count)) {
  746. p_ino = autofs4_dentry_ino(dentry->d_parent);
  747. if (p_ino && dentry->d_parent != dentry)
  748. atomic_dec(&p_ino->count);
  749. }
  750. dput(ino->dentry);
  751. dentry->d_inode->i_size = 0;
  752. clear_nlink(dentry->d_inode);
  753. dir->i_mtime = CURRENT_TIME;
  754. spin_lock(&autofs4_lock);
  755. autofs4_add_expiring(dentry);
  756. spin_lock(&dentry->d_lock);
  757. __d_drop(dentry);
  758. spin_unlock(&dentry->d_lock);
  759. spin_unlock(&autofs4_lock);
  760. return 0;
  761. }
  762. static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
  763. {
  764. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  765. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  766. struct autofs_info *p_ino;
  767. DPRINTK("dentry %p, removing %.*s",
  768. dentry, dentry->d_name.len, dentry->d_name.name);
  769. if (!autofs4_oz_mode(sbi))
  770. return -EACCES;
  771. spin_lock(&autofs4_lock);
  772. spin_lock(&sbi->lookup_lock);
  773. spin_lock(&dentry->d_lock);
  774. if (!list_empty(&dentry->d_subdirs)) {
  775. spin_unlock(&dentry->d_lock);
  776. spin_unlock(&sbi->lookup_lock);
  777. spin_unlock(&autofs4_lock);
  778. return -ENOTEMPTY;
  779. }
  780. __autofs4_add_expiring(dentry);
  781. spin_unlock(&sbi->lookup_lock);
  782. __d_drop(dentry);
  783. spin_unlock(&dentry->d_lock);
  784. spin_unlock(&autofs4_lock);
  785. if (atomic_dec_and_test(&ino->count)) {
  786. p_ino = autofs4_dentry_ino(dentry->d_parent);
  787. if (p_ino && dentry->d_parent != dentry)
  788. atomic_dec(&p_ino->count);
  789. }
  790. dput(ino->dentry);
  791. dentry->d_inode->i_size = 0;
  792. clear_nlink(dentry->d_inode);
  793. if (dir->i_nlink)
  794. drop_nlink(dir);
  795. return 0;
  796. }
  797. static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  798. {
  799. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  800. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  801. struct autofs_info *p_ino;
  802. struct inode *inode;
  803. if (!autofs4_oz_mode(sbi))
  804. return -EACCES;
  805. DPRINTK("dentry %p, creating %.*s",
  806. dentry, dentry->d_name.len, dentry->d_name.name);
  807. ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
  808. if (!ino)
  809. return -ENOMEM;
  810. autofs4_del_active(dentry);
  811. inode = autofs4_get_inode(dir->i_sb, ino);
  812. if (!inode) {
  813. if (!dentry->d_fsdata)
  814. kfree(ino);
  815. return -ENOMEM;
  816. }
  817. d_add(dentry, inode);
  818. dentry->d_fsdata = ino;
  819. ino->dentry = dget(dentry);
  820. atomic_inc(&ino->count);
  821. p_ino = autofs4_dentry_ino(dentry->d_parent);
  822. if (p_ino && dentry->d_parent != dentry)
  823. atomic_inc(&p_ino->count);
  824. ino->inode = inode;
  825. inc_nlink(dir);
  826. dir->i_mtime = CURRENT_TIME;
  827. return 0;
  828. }
  829. /* Get/set timeout ioctl() operation */
  830. #ifdef CONFIG_COMPAT
  831. static inline int autofs4_compat_get_set_timeout(struct autofs_sb_info *sbi,
  832. compat_ulong_t __user *p)
  833. {
  834. int rv;
  835. unsigned long ntimeout;
  836. if ((rv = get_user(ntimeout, p)) ||
  837. (rv = put_user(sbi->exp_timeout/HZ, p)))
  838. return rv;
  839. if (ntimeout > UINT_MAX/HZ)
  840. sbi->exp_timeout = 0;
  841. else
  842. sbi->exp_timeout = ntimeout * HZ;
  843. return 0;
  844. }
  845. #endif
  846. static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
  847. unsigned long __user *p)
  848. {
  849. int rv;
  850. unsigned long ntimeout;
  851. if ((rv = get_user(ntimeout, p)) ||
  852. (rv = put_user(sbi->exp_timeout/HZ, p)))
  853. return rv;
  854. if (ntimeout > ULONG_MAX/HZ)
  855. sbi->exp_timeout = 0;
  856. else
  857. sbi->exp_timeout = ntimeout * HZ;
  858. return 0;
  859. }
  860. /* Return protocol version */
  861. static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
  862. {
  863. return put_user(sbi->version, p);
  864. }
  865. /* Return protocol sub version */
  866. static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
  867. {
  868. return put_user(sbi->sub_version, p);
  869. }
  870. /*
  871. * Tells the daemon whether it can umount the autofs mount.
  872. */
  873. static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
  874. {
  875. int status = 0;
  876. if (may_umount(mnt))
  877. status = 1;
  878. DPRINTK("returning %d", status);
  879. status = put_user(status, p);
  880. return status;
  881. }
  882. /* Identify autofs4_dentries - this is so we can tell if there's
  883. an extra dentry refcount or not. We only hold a refcount on the
  884. dentry if its non-negative (ie, d_inode != NULL)
  885. */
  886. int is_autofs4_dentry(struct dentry *dentry)
  887. {
  888. return dentry && dentry->d_inode &&
  889. (dentry->d_op == &autofs4_root_dentry_operations ||
  890. dentry->d_op == &autofs4_dentry_operations) &&
  891. dentry->d_fsdata != NULL;
  892. }
  893. /*
  894. * ioctl()'s on the root directory is the chief method for the daemon to
  895. * generate kernel reactions
  896. */
  897. static int autofs4_root_ioctl_unlocked(struct inode *inode, struct file *filp,
  898. unsigned int cmd, unsigned long arg)
  899. {
  900. struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
  901. void __user *p = (void __user *)arg;
  902. DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
  903. cmd,arg,sbi,task_pgrp_nr(current));
  904. if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
  905. _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
  906. return -ENOTTY;
  907. if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
  908. return -EPERM;
  909. switch(cmd) {
  910. case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
  911. return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
  912. case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
  913. return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
  914. case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
  915. autofs4_catatonic_mode(sbi);
  916. return 0;
  917. case AUTOFS_IOC_PROTOVER: /* Get protocol version */
  918. return autofs4_get_protover(sbi, p);
  919. case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
  920. return autofs4_get_protosubver(sbi, p);
  921. case AUTOFS_IOC_SETTIMEOUT:
  922. return autofs4_get_set_timeout(sbi, p);
  923. #ifdef CONFIG_COMPAT
  924. case AUTOFS_IOC_SETTIMEOUT32:
  925. return autofs4_compat_get_set_timeout(sbi, p);
  926. #endif
  927. case AUTOFS_IOC_ASKUMOUNT:
  928. return autofs4_ask_umount(filp->f_path.mnt, p);
  929. /* return a single thing to expire */
  930. case AUTOFS_IOC_EXPIRE:
  931. return autofs4_expire_run(inode->i_sb,filp->f_path.mnt,sbi, p);
  932. /* same as above, but can send multiple expires through pipe */
  933. case AUTOFS_IOC_EXPIRE_MULTI:
  934. return autofs4_expire_multi(inode->i_sb,filp->f_path.mnt,sbi, p);
  935. default:
  936. return -ENOSYS;
  937. }
  938. }
  939. static long autofs4_root_ioctl(struct file *filp,
  940. unsigned int cmd, unsigned long arg)
  941. {
  942. struct inode *inode = filp->f_dentry->d_inode;
  943. return autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
  944. }
  945. #ifdef CONFIG_COMPAT
  946. static long autofs4_root_compat_ioctl(struct file *filp,
  947. unsigned int cmd, unsigned long arg)
  948. {
  949. struct inode *inode = filp->f_path.dentry->d_inode;
  950. int ret;
  951. if (cmd == AUTOFS_IOC_READY || cmd == AUTOFS_IOC_FAIL)
  952. ret = autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
  953. else
  954. ret = autofs4_root_ioctl_unlocked(inode, filp, cmd,
  955. (unsigned long)compat_ptr(arg));
  956. return ret;
  957. }
  958. #endif