root.c 26 KB

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