root.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  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_release = autofs4_dentry_release,
  366. };
  367. static struct dentry *autofs4_lookup_active(struct dentry *dentry)
  368. {
  369. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  370. struct dentry *parent = dentry->d_parent;
  371. struct qstr *name = &dentry->d_name;
  372. unsigned int len = name->len;
  373. unsigned int hash = name->hash;
  374. const unsigned char *str = name->name;
  375. struct list_head *p, *head;
  376. spin_lock(&autofs4_lock);
  377. spin_lock(&sbi->lookup_lock);
  378. head = &sbi->active_list;
  379. list_for_each(p, head) {
  380. struct autofs_info *ino;
  381. struct dentry *active;
  382. struct qstr *qstr;
  383. ino = list_entry(p, struct autofs_info, active);
  384. active = ino->dentry;
  385. spin_lock(&active->d_lock);
  386. /* Already gone? */
  387. if (active->d_count == 0)
  388. goto next;
  389. qstr = &active->d_name;
  390. if (active->d_name.hash != hash)
  391. goto next;
  392. if (active->d_parent != parent)
  393. goto next;
  394. if (qstr->len != len)
  395. goto next;
  396. if (memcmp(qstr->name, str, len))
  397. goto next;
  398. if (d_unhashed(active)) {
  399. dget_dlock(active);
  400. spin_unlock(&active->d_lock);
  401. spin_unlock(&sbi->lookup_lock);
  402. spin_unlock(&autofs4_lock);
  403. return active;
  404. }
  405. next:
  406. spin_unlock(&active->d_lock);
  407. }
  408. spin_unlock(&sbi->lookup_lock);
  409. spin_unlock(&autofs4_lock);
  410. return NULL;
  411. }
  412. static struct dentry *autofs4_lookup_expiring(struct dentry *dentry)
  413. {
  414. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  415. struct dentry *parent = dentry->d_parent;
  416. struct qstr *name = &dentry->d_name;
  417. unsigned int len = name->len;
  418. unsigned int hash = name->hash;
  419. const unsigned char *str = name->name;
  420. struct list_head *p, *head;
  421. spin_lock(&autofs4_lock);
  422. spin_lock(&sbi->lookup_lock);
  423. head = &sbi->expiring_list;
  424. list_for_each(p, head) {
  425. struct autofs_info *ino;
  426. struct dentry *expiring;
  427. struct qstr *qstr;
  428. ino = list_entry(p, struct autofs_info, expiring);
  429. expiring = ino->dentry;
  430. spin_lock(&expiring->d_lock);
  431. /* Bad luck, we've already been dentry_iput */
  432. if (!expiring->d_inode)
  433. goto next;
  434. qstr = &expiring->d_name;
  435. if (expiring->d_name.hash != hash)
  436. goto next;
  437. if (expiring->d_parent != parent)
  438. goto next;
  439. if (qstr->len != len)
  440. goto next;
  441. if (memcmp(qstr->name, str, len))
  442. goto next;
  443. if (d_unhashed(expiring)) {
  444. dget_dlock(expiring);
  445. spin_unlock(&expiring->d_lock);
  446. spin_unlock(&sbi->lookup_lock);
  447. spin_unlock(&autofs4_lock);
  448. return expiring;
  449. }
  450. next:
  451. spin_unlock(&expiring->d_lock);
  452. }
  453. spin_unlock(&sbi->lookup_lock);
  454. spin_unlock(&autofs4_lock);
  455. return NULL;
  456. }
  457. static int autofs4_mount_wait(struct dentry *dentry)
  458. {
  459. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  460. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  461. int status;
  462. if (ino->flags & AUTOFS_INF_PENDING) {
  463. DPRINTK("waiting for mount name=%.*s",
  464. dentry->d_name.len, dentry->d_name.name);
  465. status = autofs4_wait(sbi, dentry, NFY_MOUNT);
  466. DPRINTK("mount wait done status=%d", status);
  467. ino->last_used = jiffies;
  468. return status;
  469. }
  470. return 0;
  471. }
  472. static int do_expire_wait(struct dentry *dentry)
  473. {
  474. struct dentry *expiring;
  475. expiring = autofs4_lookup_expiring(dentry);
  476. if (!expiring)
  477. return autofs4_expire_wait(dentry);
  478. else {
  479. /*
  480. * If we are racing with expire the request might not
  481. * be quite complete, but the directory has been removed
  482. * so it must have been successful, just wait for it.
  483. */
  484. autofs4_expire_wait(expiring);
  485. autofs4_del_expiring(expiring);
  486. dput(expiring);
  487. }
  488. return 0;
  489. }
  490. static struct dentry *autofs4_mountpoint_changed(struct path *path)
  491. {
  492. struct dentry *dentry = path->dentry;
  493. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  494. /*
  495. * If this is an indirect mount the dentry could have gone away
  496. * as a result of an expire and a new one created.
  497. */
  498. if (autofs_type_indirect(sbi->type) && d_unhashed(dentry)) {
  499. struct dentry *parent = dentry->d_parent;
  500. struct dentry *new = d_lookup(parent, &dentry->d_name);
  501. if (!new)
  502. return NULL;
  503. dput(path->dentry);
  504. path->dentry = new;
  505. }
  506. return path->dentry;
  507. }
  508. struct vfsmount *autofs4_d_automount(struct path *path)
  509. {
  510. struct dentry *dentry = path->dentry;
  511. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  512. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  513. int status;
  514. DPRINTK("dentry=%p %.*s",
  515. dentry, dentry->d_name.len, dentry->d_name.name);
  516. /* The daemon never triggers a mount. */
  517. if (autofs4_oz_mode(sbi))
  518. return NULL;
  519. /*
  520. * If an expire request is pending everyone must wait.
  521. * If the expire fails we're still mounted so continue
  522. * the follow and return. A return of -EAGAIN (which only
  523. * happens with indirect mounts) means the expire completed
  524. * and the directory was removed, so just go ahead and try
  525. * the mount.
  526. */
  527. status = do_expire_wait(dentry);
  528. if (status && status != -EAGAIN)
  529. return NULL;
  530. /* Callback to the daemon to perform the mount or wait */
  531. spin_lock(&sbi->fs_lock);
  532. if (ino->flags & AUTOFS_INF_PENDING) {
  533. spin_unlock(&sbi->fs_lock);
  534. status = autofs4_mount_wait(dentry);
  535. if (status)
  536. return ERR_PTR(status);
  537. spin_lock(&sbi->fs_lock);
  538. goto done;
  539. }
  540. /*
  541. * If the dentry is a symlink it's equivalent to a directory
  542. * having d_mounted() true, so there's no need to call back
  543. * to the daemon.
  544. */
  545. if (dentry->d_inode && S_ISLNK(dentry->d_inode->i_mode))
  546. goto done;
  547. spin_lock(&dentry->d_lock);
  548. if (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
  549. ino->flags |= AUTOFS_INF_PENDING;
  550. spin_unlock(&dentry->d_lock);
  551. spin_unlock(&sbi->fs_lock);
  552. status = autofs4_mount_wait(dentry);
  553. if (status)
  554. return ERR_PTR(status);
  555. spin_lock(&sbi->fs_lock);
  556. ino->flags &= ~AUTOFS_INF_PENDING;
  557. goto done;
  558. }
  559. spin_unlock(&dentry->d_lock);
  560. done:
  561. /*
  562. * Any needed mounting has been completed and the path updated
  563. * so turn this into a normal dentry so we don't continually
  564. * call ->d_automount().
  565. */
  566. managed_dentry_clear_automount(dentry);
  567. spin_unlock(&sbi->fs_lock);
  568. /* Mount succeeded, check if we ended up with a new dentry */
  569. dentry = autofs4_mountpoint_changed(path);
  570. if (!dentry)
  571. return ERR_PTR(-ENOENT);
  572. return NULL;
  573. }
  574. /* Lookups in the root directory */
  575. static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  576. {
  577. struct autofs_sb_info *sbi;
  578. struct autofs_info *ino;
  579. struct dentry *active;
  580. DPRINTK("name = %.*s", dentry->d_name.len, dentry->d_name.name);
  581. /* File name too long to exist */
  582. if (dentry->d_name.len > NAME_MAX)
  583. return ERR_PTR(-ENAMETOOLONG);
  584. sbi = autofs4_sbi(dir->i_sb);
  585. DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
  586. current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode);
  587. active = autofs4_lookup_active(dentry);
  588. if (active) {
  589. return active;
  590. } else {
  591. d_set_d_op(dentry, &autofs4_root_dentry_operations);
  592. /*
  593. * A dentry that is not within the root can never trigger a
  594. * mount operation, unless the directory already exists, so we
  595. * can return fail immediately. The daemon however does need
  596. * to create directories within the file system.
  597. */
  598. if (!autofs4_oz_mode(sbi) && !IS_ROOT(dentry->d_parent))
  599. return ERR_PTR(-ENOENT);
  600. /* Mark entries in the root as mount triggers */
  601. if (autofs_type_indirect(sbi->type) && IS_ROOT(dentry->d_parent)) {
  602. d_set_d_op(dentry, &autofs4_dentry_operations);
  603. managed_dentry_set_automount(dentry);
  604. }
  605. ino = autofs4_init_ino(NULL, sbi, 0555);
  606. if (!ino)
  607. return ERR_PTR(-ENOMEM);
  608. dentry->d_fsdata = ino;
  609. ino->dentry = dentry;
  610. autofs4_add_active(dentry);
  611. d_instantiate(dentry, NULL);
  612. }
  613. return NULL;
  614. }
  615. static int autofs4_dir_symlink(struct inode *dir,
  616. struct dentry *dentry,
  617. const char *symname)
  618. {
  619. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  620. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  621. struct autofs_info *p_ino;
  622. struct inode *inode;
  623. char *cp;
  624. DPRINTK("%s <- %.*s", symname,
  625. dentry->d_name.len, dentry->d_name.name);
  626. if (!autofs4_oz_mode(sbi))
  627. return -EACCES;
  628. ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
  629. if (!ino)
  630. return -ENOMEM;
  631. autofs4_del_active(dentry);
  632. ino->size = strlen(symname);
  633. cp = kmalloc(ino->size + 1, GFP_KERNEL);
  634. if (!cp) {
  635. if (!dentry->d_fsdata)
  636. kfree(ino);
  637. return -ENOMEM;
  638. }
  639. strcpy(cp, symname);
  640. inode = autofs4_get_inode(dir->i_sb, ino);
  641. if (!inode) {
  642. kfree(cp);
  643. if (!dentry->d_fsdata)
  644. kfree(ino);
  645. return -ENOMEM;
  646. }
  647. d_add(dentry, inode);
  648. dentry->d_fsdata = ino;
  649. ino->dentry = dget(dentry);
  650. atomic_inc(&ino->count);
  651. p_ino = autofs4_dentry_ino(dentry->d_parent);
  652. if (p_ino && dentry->d_parent != dentry)
  653. atomic_inc(&p_ino->count);
  654. ino->inode = inode;
  655. ino->u.symlink = cp;
  656. dir->i_mtime = CURRENT_TIME;
  657. return 0;
  658. }
  659. /*
  660. * NOTE!
  661. *
  662. * Normal filesystems would do a "d_delete()" to tell the VFS dcache
  663. * that the file no longer exists. However, doing that means that the
  664. * VFS layer can turn the dentry into a negative dentry. We don't want
  665. * this, because the unlink is probably the result of an expire.
  666. * We simply d_drop it and add it to a expiring list in the super block,
  667. * which allows the dentry lookup to check for an incomplete expire.
  668. *
  669. * If a process is blocked on the dentry waiting for the expire to finish,
  670. * it will invalidate the dentry and try to mount with a new one.
  671. *
  672. * Also see autofs4_dir_rmdir()..
  673. */
  674. static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
  675. {
  676. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  677. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  678. struct autofs_info *p_ino;
  679. /* This allows root to remove symlinks */
  680. if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
  681. return -EACCES;
  682. if (atomic_dec_and_test(&ino->count)) {
  683. p_ino = autofs4_dentry_ino(dentry->d_parent);
  684. if (p_ino && dentry->d_parent != dentry)
  685. atomic_dec(&p_ino->count);
  686. }
  687. dput(ino->dentry);
  688. dentry->d_inode->i_size = 0;
  689. clear_nlink(dentry->d_inode);
  690. dir->i_mtime = CURRENT_TIME;
  691. spin_lock(&autofs4_lock);
  692. autofs4_add_expiring(dentry);
  693. spin_lock(&dentry->d_lock);
  694. __d_drop(dentry);
  695. spin_unlock(&dentry->d_lock);
  696. spin_unlock(&autofs4_lock);
  697. return 0;
  698. }
  699. static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
  700. {
  701. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  702. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  703. struct autofs_info *p_ino;
  704. DPRINTK("dentry %p, removing %.*s",
  705. dentry, dentry->d_name.len, dentry->d_name.name);
  706. if (!autofs4_oz_mode(sbi))
  707. return -EACCES;
  708. spin_lock(&autofs4_lock);
  709. spin_lock(&sbi->lookup_lock);
  710. spin_lock(&dentry->d_lock);
  711. if (!list_empty(&dentry->d_subdirs)) {
  712. spin_unlock(&dentry->d_lock);
  713. spin_unlock(&sbi->lookup_lock);
  714. spin_unlock(&autofs4_lock);
  715. return -ENOTEMPTY;
  716. }
  717. __autofs4_add_expiring(dentry);
  718. spin_unlock(&sbi->lookup_lock);
  719. __d_drop(dentry);
  720. spin_unlock(&dentry->d_lock);
  721. spin_unlock(&autofs4_lock);
  722. if (atomic_dec_and_test(&ino->count)) {
  723. p_ino = autofs4_dentry_ino(dentry->d_parent);
  724. if (p_ino && dentry->d_parent != dentry)
  725. atomic_dec(&p_ino->count);
  726. }
  727. dput(ino->dentry);
  728. dentry->d_inode->i_size = 0;
  729. clear_nlink(dentry->d_inode);
  730. if (dir->i_nlink)
  731. drop_nlink(dir);
  732. return 0;
  733. }
  734. static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  735. {
  736. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  737. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  738. struct autofs_info *p_ino;
  739. struct inode *inode;
  740. if (!autofs4_oz_mode(sbi))
  741. return -EACCES;
  742. DPRINTK("dentry %p, creating %.*s",
  743. dentry, dentry->d_name.len, dentry->d_name.name);
  744. ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
  745. if (!ino)
  746. return -ENOMEM;
  747. autofs4_del_active(dentry);
  748. inode = autofs4_get_inode(dir->i_sb, ino);
  749. if (!inode) {
  750. if (!dentry->d_fsdata)
  751. kfree(ino);
  752. return -ENOMEM;
  753. }
  754. d_add(dentry, inode);
  755. dentry->d_fsdata = ino;
  756. ino->dentry = dget(dentry);
  757. atomic_inc(&ino->count);
  758. p_ino = autofs4_dentry_ino(dentry->d_parent);
  759. if (p_ino && dentry->d_parent != dentry)
  760. atomic_inc(&p_ino->count);
  761. ino->inode = inode;
  762. inc_nlink(dir);
  763. dir->i_mtime = CURRENT_TIME;
  764. return 0;
  765. }
  766. /* Get/set timeout ioctl() operation */
  767. #ifdef CONFIG_COMPAT
  768. static inline int autofs4_compat_get_set_timeout(struct autofs_sb_info *sbi,
  769. compat_ulong_t __user *p)
  770. {
  771. int rv;
  772. unsigned long ntimeout;
  773. if ((rv = get_user(ntimeout, p)) ||
  774. (rv = put_user(sbi->exp_timeout/HZ, p)))
  775. return rv;
  776. if (ntimeout > UINT_MAX/HZ)
  777. sbi->exp_timeout = 0;
  778. else
  779. sbi->exp_timeout = ntimeout * HZ;
  780. return 0;
  781. }
  782. #endif
  783. static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
  784. unsigned long __user *p)
  785. {
  786. int rv;
  787. unsigned long ntimeout;
  788. if ((rv = get_user(ntimeout, p)) ||
  789. (rv = put_user(sbi->exp_timeout/HZ, p)))
  790. return rv;
  791. if (ntimeout > ULONG_MAX/HZ)
  792. sbi->exp_timeout = 0;
  793. else
  794. sbi->exp_timeout = ntimeout * HZ;
  795. return 0;
  796. }
  797. /* Return protocol version */
  798. static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
  799. {
  800. return put_user(sbi->version, p);
  801. }
  802. /* Return protocol sub version */
  803. static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
  804. {
  805. return put_user(sbi->sub_version, p);
  806. }
  807. /*
  808. * Tells the daemon whether it can umount the autofs mount.
  809. */
  810. static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
  811. {
  812. int status = 0;
  813. if (may_umount(mnt))
  814. status = 1;
  815. DPRINTK("returning %d", status);
  816. status = put_user(status, p);
  817. return status;
  818. }
  819. /* Identify autofs4_dentries - this is so we can tell if there's
  820. an extra dentry refcount or not. We only hold a refcount on the
  821. dentry if its non-negative (ie, d_inode != NULL)
  822. */
  823. int is_autofs4_dentry(struct dentry *dentry)
  824. {
  825. return dentry && dentry->d_inode &&
  826. (dentry->d_op == &autofs4_root_dentry_operations ||
  827. dentry->d_op == &autofs4_dentry_operations) &&
  828. dentry->d_fsdata != NULL;
  829. }
  830. /*
  831. * ioctl()'s on the root directory is the chief method for the daemon to
  832. * generate kernel reactions
  833. */
  834. static int autofs4_root_ioctl_unlocked(struct inode *inode, struct file *filp,
  835. unsigned int cmd, unsigned long arg)
  836. {
  837. struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
  838. void __user *p = (void __user *)arg;
  839. DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
  840. cmd,arg,sbi,task_pgrp_nr(current));
  841. if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
  842. _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
  843. return -ENOTTY;
  844. if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
  845. return -EPERM;
  846. switch(cmd) {
  847. case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
  848. return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
  849. case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
  850. return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
  851. case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
  852. autofs4_catatonic_mode(sbi);
  853. return 0;
  854. case AUTOFS_IOC_PROTOVER: /* Get protocol version */
  855. return autofs4_get_protover(sbi, p);
  856. case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
  857. return autofs4_get_protosubver(sbi, p);
  858. case AUTOFS_IOC_SETTIMEOUT:
  859. return autofs4_get_set_timeout(sbi, p);
  860. #ifdef CONFIG_COMPAT
  861. case AUTOFS_IOC_SETTIMEOUT32:
  862. return autofs4_compat_get_set_timeout(sbi, p);
  863. #endif
  864. case AUTOFS_IOC_ASKUMOUNT:
  865. return autofs4_ask_umount(filp->f_path.mnt, p);
  866. /* return a single thing to expire */
  867. case AUTOFS_IOC_EXPIRE:
  868. return autofs4_expire_run(inode->i_sb,filp->f_path.mnt,sbi, p);
  869. /* same as above, but can send multiple expires through pipe */
  870. case AUTOFS_IOC_EXPIRE_MULTI:
  871. return autofs4_expire_multi(inode->i_sb,filp->f_path.mnt,sbi, p);
  872. default:
  873. return -ENOSYS;
  874. }
  875. }
  876. static long autofs4_root_ioctl(struct file *filp,
  877. unsigned int cmd, unsigned long arg)
  878. {
  879. struct inode *inode = filp->f_dentry->d_inode;
  880. return autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
  881. }
  882. #ifdef CONFIG_COMPAT
  883. static long autofs4_root_compat_ioctl(struct file *filp,
  884. unsigned int cmd, unsigned long arg)
  885. {
  886. struct inode *inode = filp->f_path.dentry->d_inode;
  887. int ret;
  888. if (cmd == AUTOFS_IOC_READY || cmd == AUTOFS_IOC_FAIL)
  889. ret = autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
  890. else
  891. ret = autofs4_root_ioctl_unlocked(inode, filp, cmd,
  892. (unsigned long)compat_ptr(arg));
  893. return ret;
  894. }
  895. #endif