root.c 28 KB

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