root.c 26 KB

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