cifs_dfs_ref.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * Contains the CIFS DFS referral mounting routines used for handling
  3. * traversal via DFS junction point
  4. *
  5. * Copyright (c) 2007 Igor Mammedov
  6. * Copyright (C) International Business Machines Corp., 2008
  7. * Author(s): Igor Mammedov (niallain@gmail.com)
  8. * Steve French (sfrench@us.ibm.com)
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. */
  14. #include <linux/dcache.h>
  15. #include <linux/mount.h>
  16. #include <linux/namei.h>
  17. #include <linux/vfs.h>
  18. #include <linux/fs.h>
  19. #include "cifsglob.h"
  20. #include "cifsproto.h"
  21. #include "cifsfs.h"
  22. #include "dns_resolve.h"
  23. #include "cifs_debug.h"
  24. static LIST_HEAD(cifs_dfs_automount_list);
  25. static void cifs_dfs_expire_automounts(struct work_struct *work);
  26. static DECLARE_DELAYED_WORK(cifs_dfs_automount_task,
  27. cifs_dfs_expire_automounts);
  28. static int cifs_dfs_mountpoint_expiry_timeout = 500 * HZ;
  29. static void cifs_dfs_expire_automounts(struct work_struct *work)
  30. {
  31. struct list_head *list = &cifs_dfs_automount_list;
  32. mark_mounts_for_expiry(list);
  33. if (!list_empty(list))
  34. schedule_delayed_work(&cifs_dfs_automount_task,
  35. cifs_dfs_mountpoint_expiry_timeout);
  36. }
  37. void cifs_dfs_release_automount_timer(void)
  38. {
  39. BUG_ON(!list_empty(&cifs_dfs_automount_list));
  40. cancel_delayed_work(&cifs_dfs_automount_task);
  41. flush_scheduled_work();
  42. }
  43. /**
  44. * cifs_get_share_name - extracts share name from UNC
  45. * @node_name: pointer to UNC string
  46. *
  47. * Extracts sharename form full UNC.
  48. * i.e. strips from UNC trailing path that is not part of share
  49. * name and fixup missing '\' in the begining of DFS node refferal
  50. * if neccessary.
  51. * Returns pointer to share name on success or NULL on error.
  52. * Caller is responsible for freeing returned string.
  53. */
  54. static char *cifs_get_share_name(const char *node_name)
  55. {
  56. int len;
  57. char *UNC;
  58. char *pSep;
  59. len = strlen(node_name);
  60. UNC = kmalloc(len+2 /*for term null and additional \ if it's missed */,
  61. GFP_KERNEL);
  62. if (!UNC)
  63. return NULL;
  64. /* get share name and server name */
  65. if (node_name[1] != '\\') {
  66. UNC[0] = '\\';
  67. strncpy(UNC+1, node_name, len);
  68. len++;
  69. UNC[len] = 0;
  70. } else {
  71. strncpy(UNC, node_name, len);
  72. UNC[len] = 0;
  73. }
  74. /* find server name end */
  75. pSep = memchr(UNC+2, '\\', len-2);
  76. if (!pSep) {
  77. cERROR(1, ("%s: no server name end in node name: %s",
  78. __func__, node_name));
  79. kfree(UNC);
  80. return NULL;
  81. }
  82. /* find sharename end */
  83. pSep++;
  84. pSep = memchr(UNC+(pSep-UNC), '\\', len-(pSep-UNC));
  85. if (pSep) {
  86. /* trim path up to sharename end
  87. * now we have share name in UNC */
  88. *pSep = 0;
  89. }
  90. return UNC;
  91. }
  92. /**
  93. * compose_mount_options - creates mount options for refferral
  94. * @sb_mountdata: parent/root DFS mount options (template)
  95. * @ref_unc: refferral server UNC
  96. * @devname: pointer for saving device name
  97. *
  98. * creates mount options for submount based on template options sb_mountdata
  99. * and replacing unc,ip,prefixpath options with ones we've got form ref_unc.
  100. *
  101. * Returns: pointer to new mount options or ERR_PTR.
  102. * Caller is responcible for freeing retunrned value if it is not error.
  103. */
  104. static char *compose_mount_options(const char *sb_mountdata,
  105. const char *ref_unc,
  106. char **devname)
  107. {
  108. int rc;
  109. char *mountdata;
  110. int md_len;
  111. char *tkn_e;
  112. char *srvIP = NULL;
  113. char sep = ',';
  114. int off, noff;
  115. if (sb_mountdata == NULL)
  116. return ERR_PTR(-EINVAL);
  117. *devname = cifs_get_share_name(ref_unc);
  118. rc = dns_resolve_server_name_to_ip(*devname, &srvIP);
  119. if (rc != 0) {
  120. cERROR(1, ("%s: Failed to resolve server part of %s to IP",
  121. __func__, *devname));
  122. mountdata = ERR_PTR(rc);
  123. goto compose_mount_options_out;
  124. }
  125. md_len = strlen(sb_mountdata) + strlen(srvIP) + strlen(ref_unc) + 3;
  126. mountdata = kzalloc(md_len+1, GFP_KERNEL);
  127. if (mountdata == NULL) {
  128. mountdata = ERR_PTR(-ENOMEM);
  129. goto compose_mount_options_out;
  130. }
  131. /* copy all options except of unc,ip,prefixpath */
  132. off = 0;
  133. if (strncmp(sb_mountdata, "sep=", 4) == 0) {
  134. sep = sb_mountdata[4];
  135. strncpy(mountdata, sb_mountdata, 5);
  136. off += 5;
  137. }
  138. while ((tkn_e = strchr(sb_mountdata+off, sep))) {
  139. noff = (tkn_e - (sb_mountdata+off)) + 1;
  140. if (strnicmp(sb_mountdata+off, "unc=", 4) == 0) {
  141. off += noff;
  142. continue;
  143. }
  144. if (strnicmp(sb_mountdata+off, "ip=", 3) == 0) {
  145. off += noff;
  146. continue;
  147. }
  148. if (strnicmp(sb_mountdata+off, "prefixpath=", 3) == 0) {
  149. off += noff;
  150. continue;
  151. }
  152. strncat(mountdata, sb_mountdata+off, noff);
  153. off += noff;
  154. }
  155. strcat(mountdata, sb_mountdata+off);
  156. mountdata[md_len] = '\0';
  157. /* copy new IP and ref share name */
  158. strcat(mountdata, ",ip=");
  159. strcat(mountdata, srvIP);
  160. strcat(mountdata, ",unc=");
  161. strcat(mountdata, *devname);
  162. /* find & copy prefixpath */
  163. tkn_e = strchr(ref_unc+2, '\\');
  164. if (tkn_e) {
  165. tkn_e = strchr(tkn_e+1, '\\');
  166. if (tkn_e) {
  167. strcat(mountdata, ",prefixpath=");
  168. strcat(mountdata, tkn_e+1);
  169. }
  170. }
  171. /*cFYI(1,("%s: parent mountdata: %s", __func__,sb_mountdata));*/
  172. /*cFYI(1, ("%s: submount mountdata: %s", __func__, mountdata ));*/
  173. compose_mount_options_out:
  174. kfree(srvIP);
  175. return mountdata;
  176. }
  177. static struct vfsmount *cifs_dfs_do_refmount(const struct vfsmount *mnt_parent,
  178. struct dentry *dentry, char *ref_unc)
  179. {
  180. struct cifs_sb_info *cifs_sb;
  181. struct vfsmount *mnt;
  182. char *mountdata;
  183. char *devname = NULL;
  184. cifs_sb = CIFS_SB(dentry->d_inode->i_sb);
  185. mountdata = compose_mount_options(cifs_sb->mountdata,
  186. ref_unc, &devname);
  187. if (IS_ERR(mountdata))
  188. return (struct vfsmount *)mountdata;
  189. mnt = vfs_kern_mount(&cifs_fs_type, 0, devname, mountdata);
  190. kfree(mountdata);
  191. kfree(devname);
  192. return mnt;
  193. }
  194. static int add_mount_helper(struct vfsmount *newmnt, struct nameidata *nd,
  195. struct list_head *mntlist)
  196. {
  197. /* stolen from afs code */
  198. int err;
  199. mntget(newmnt);
  200. err = do_add_mount(newmnt, nd, nd->path.mnt->mnt_flags, mntlist);
  201. switch (err) {
  202. case 0:
  203. path_put(&nd->path);
  204. nd->path.mnt = newmnt;
  205. nd->path.dentry = dget(newmnt->mnt_root);
  206. schedule_delayed_work(&cifs_dfs_automount_task,
  207. cifs_dfs_mountpoint_expiry_timeout);
  208. break;
  209. case -EBUSY:
  210. /* someone else made a mount here whilst we were busy */
  211. while (d_mountpoint(nd->path.dentry) &&
  212. follow_down(&nd->path.mnt, &nd->path.dentry))
  213. ;
  214. err = 0;
  215. default:
  216. mntput(newmnt);
  217. break;
  218. }
  219. return err;
  220. }
  221. static void dump_referral(const struct dfs_info3_param *ref)
  222. {
  223. cFYI(1, ("DFS: ref path: %s", ref->path_name));
  224. cFYI(1, ("DFS: node path: %s", ref->node_name));
  225. cFYI(1, ("DFS: fl: %hd, srv_type: %hd", ref->flags, ref->server_type));
  226. cFYI(1, ("DFS: ref_flags: %hd, path_consumed: %hd", ref->ref_flag,
  227. ref->path_consumed));
  228. }
  229. static void*
  230. cifs_dfs_follow_mountpoint(struct dentry *dentry, struct nameidata *nd)
  231. {
  232. struct dfs_info3_param *referrals = NULL;
  233. unsigned int num_referrals = 0;
  234. struct cifs_sb_info *cifs_sb;
  235. struct cifsSesInfo *ses;
  236. char *full_path = NULL;
  237. int xid, i;
  238. int rc = 0;
  239. struct vfsmount *mnt = ERR_PTR(-ENOENT);
  240. cFYI(1, ("in %s", __func__));
  241. BUG_ON(IS_ROOT(dentry));
  242. xid = GetXid();
  243. dput(nd->path.dentry);
  244. nd->path.dentry = dget(dentry);
  245. cifs_sb = CIFS_SB(dentry->d_inode->i_sb);
  246. ses = cifs_sb->tcon->ses;
  247. if (!ses) {
  248. rc = -EINVAL;
  249. goto out_err;
  250. }
  251. full_path = build_path_from_dentry(dentry);
  252. if (full_path == NULL) {
  253. rc = -ENOMEM;
  254. goto out_err;
  255. }
  256. rc = get_dfs_path(xid, ses , full_path, cifs_sb->local_nls,
  257. &num_referrals, &referrals,
  258. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  259. for (i = 0; i < num_referrals; i++) {
  260. dump_referral(referrals+i);
  261. /* connect to a storage node */
  262. if (referrals[i].flags & DFSREF_STORAGE_SERVER) {
  263. int len;
  264. len = strlen(referrals[i].node_name);
  265. if (len < 2) {
  266. cERROR(1, ("%s: Net Address path too short: %s",
  267. __func__, referrals[i].node_name));
  268. rc = -EINVAL;
  269. goto out_err;
  270. }
  271. mnt = cifs_dfs_do_refmount(nd->path.mnt,
  272. nd->path.dentry,
  273. referrals[i].node_name);
  274. cFYI(1, ("%s: cifs_dfs_do_refmount:%s , mnt:%p",
  275. __func__,
  276. referrals[i].node_name, mnt));
  277. /* complete mount procedure if we accured submount */
  278. if (!IS_ERR(mnt))
  279. break;
  280. }
  281. }
  282. /* we need it cause for() above could exit without valid submount */
  283. rc = PTR_ERR(mnt);
  284. if (IS_ERR(mnt))
  285. goto out_err;
  286. nd->path.mnt->mnt_flags |= MNT_SHRINKABLE;
  287. rc = add_mount_helper(mnt, nd, &cifs_dfs_automount_list);
  288. out:
  289. FreeXid(xid);
  290. free_dfs_info_array(referrals, num_referrals);
  291. kfree(full_path);
  292. cFYI(1, ("leaving %s" , __func__));
  293. return ERR_PTR(rc);
  294. out_err:
  295. path_put(&nd->path);
  296. goto out;
  297. }
  298. struct inode_operations cifs_dfs_referral_inode_operations = {
  299. .follow_link = cifs_dfs_follow_mountpoint,
  300. };