cifs_dfs_ref.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. LIST_HEAD(cifs_dfs_automount_list);
  25. /*
  26. * DFS functions
  27. */
  28. void dfs_shrink_umount_helper(struct vfsmount *vfsmnt)
  29. {
  30. mark_mounts_for_expiry(&cifs_dfs_automount_list);
  31. mark_mounts_for_expiry(&cifs_dfs_automount_list);
  32. }
  33. /**
  34. * cifs_get_share_name - extracts share name from UNC
  35. * @node_name: pointer to UNC string
  36. *
  37. * Extracts sharename form full UNC.
  38. * i.e. strips from UNC trailing path that is not part of share
  39. * name and fixup missing '\' in the begining of DFS node refferal
  40. * if neccessary.
  41. * Returns pointer to share name on success or NULL on error.
  42. * Caller is responsible for freeing returned string.
  43. */
  44. static char *cifs_get_share_name(const char *node_name)
  45. {
  46. int len;
  47. char *UNC;
  48. char *pSep;
  49. len = strlen(node_name);
  50. UNC = kmalloc(len+2 /*for term null and additional \ if it's missed */,
  51. GFP_KERNEL);
  52. if (!UNC)
  53. return NULL;
  54. /* get share name and server name */
  55. if (node_name[1] != '\\') {
  56. UNC[0] = '\\';
  57. strncpy(UNC+1, node_name, len);
  58. len++;
  59. UNC[len] = 0;
  60. } else {
  61. strncpy(UNC, node_name, len);
  62. UNC[len] = 0;
  63. }
  64. /* find server name end */
  65. pSep = memchr(UNC+2, '\\', len-2);
  66. if (!pSep) {
  67. cERROR(1, ("%s: no server name end in node name: %s",
  68. __func__, node_name));
  69. kfree(UNC);
  70. return NULL;
  71. }
  72. /* find sharename end */
  73. pSep++;
  74. pSep = memchr(UNC+(pSep-UNC), '\\', len-(pSep-UNC));
  75. if (!pSep) {
  76. cERROR(1, ("%s:2 cant find share name in node name: %s",
  77. __func__, node_name));
  78. kfree(UNC);
  79. return NULL;
  80. }
  81. /* trim path up to sharename end
  82. * * now we have share name in UNC */
  83. *pSep = 0;
  84. return UNC;
  85. }
  86. /**
  87. * compose_mount_options - creates mount options for refferral
  88. * @sb_mountdata: parent/root DFS mount options (template)
  89. * @ref_unc: refferral server UNC
  90. * @devname: pointer for saving device name
  91. *
  92. * creates mount options for submount based on template options sb_mountdata
  93. * and replacing unc,ip,prefixpath options with ones we've got form ref_unc.
  94. *
  95. * Returns: pointer to new mount options or ERR_PTR.
  96. * Caller is responcible for freeing retunrned value if it is not error.
  97. */
  98. static char *compose_mount_options(const char *sb_mountdata,
  99. const char *ref_unc,
  100. char **devname)
  101. {
  102. int rc;
  103. char *mountdata;
  104. int md_len;
  105. char *tkn_e;
  106. char *srvIP = NULL;
  107. char sep = ',';
  108. int off, noff;
  109. if (sb_mountdata == NULL)
  110. return ERR_PTR(-EINVAL);
  111. *devname = cifs_get_share_name(ref_unc);
  112. rc = dns_resolve_server_name_to_ip(*devname, &srvIP);
  113. if (rc != 0) {
  114. cERROR(1, ("%s: Failed to resolve server part of %s to IP",
  115. __func__, *devname));
  116. mountdata = ERR_PTR(rc);
  117. goto compose_mount_options_out;
  118. }
  119. md_len = strlen(sb_mountdata) + strlen(srvIP) + strlen(ref_unc) + 3;
  120. mountdata = kzalloc(md_len+1, GFP_KERNEL);
  121. if (mountdata == NULL) {
  122. mountdata = ERR_PTR(-ENOMEM);
  123. goto compose_mount_options_out;
  124. }
  125. /* copy all options except of unc,ip,prefixpath */
  126. off = 0;
  127. if (strncmp(sb_mountdata, "sep=", 4) == 0) {
  128. sep = sb_mountdata[4];
  129. strncpy(mountdata, sb_mountdata, 5);
  130. off += 5;
  131. }
  132. while ((tkn_e = strchr(sb_mountdata+off, sep))) {
  133. noff = (tkn_e - (sb_mountdata+off)) + 1;
  134. if (strnicmp(sb_mountdata+off, "unc=", 4) == 0) {
  135. off += noff;
  136. continue;
  137. }
  138. if (strnicmp(sb_mountdata+off, "ip=", 3) == 0) {
  139. off += noff;
  140. continue;
  141. }
  142. if (strnicmp(sb_mountdata+off, "prefixpath=", 3) == 0) {
  143. off += noff;
  144. continue;
  145. }
  146. strncat(mountdata, sb_mountdata+off, noff);
  147. off += noff;
  148. }
  149. strcat(mountdata, sb_mountdata+off);
  150. mountdata[md_len] = '\0';
  151. /* copy new IP and ref share name */
  152. strcat(mountdata, ",ip=");
  153. strcat(mountdata, srvIP);
  154. strcat(mountdata, ",unc=");
  155. strcat(mountdata, *devname);
  156. /* find & copy prefixpath */
  157. tkn_e = strchr(ref_unc+2, '\\');
  158. if (tkn_e) {
  159. tkn_e = strchr(tkn_e+1, '\\');
  160. if (tkn_e) {
  161. strcat(mountdata, ",prefixpath=");
  162. strcat(mountdata, tkn_e);
  163. }
  164. }
  165. /*cFYI(1,("%s: parent mountdata: %s", __func__,sb_mountdata));*/
  166. /*cFYI(1, ("%s: submount mountdata: %s", __func__, mountdata ));*/
  167. compose_mount_options_out:
  168. kfree(srvIP);
  169. return mountdata;
  170. }
  171. static struct vfsmount *cifs_dfs_do_refmount(const struct vfsmount *mnt_parent,
  172. struct dentry *dentry, char *ref_unc)
  173. {
  174. struct cifs_sb_info *cifs_sb;
  175. struct vfsmount *mnt;
  176. char *mountdata;
  177. char *devname = NULL;
  178. cifs_sb = CIFS_SB(dentry->d_inode->i_sb);
  179. mountdata = compose_mount_options(cifs_sb->mountdata,
  180. ref_unc, &devname);
  181. if (IS_ERR(mountdata))
  182. return (struct vfsmount *)mountdata;
  183. mnt = vfs_kern_mount(&cifs_fs_type, 0, devname, mountdata);
  184. kfree(mountdata);
  185. kfree(devname);
  186. return mnt;
  187. }
  188. static char *build_full_dfs_path_from_dentry(struct dentry *dentry)
  189. {
  190. char *full_path = NULL;
  191. char *search_path;
  192. char *tmp_path;
  193. size_t l_max_len;
  194. struct cifs_sb_info *cifs_sb;
  195. if (dentry->d_inode == NULL)
  196. return NULL;
  197. cifs_sb = CIFS_SB(dentry->d_inode->i_sb);
  198. if (cifs_sb->tcon == NULL)
  199. return NULL;
  200. search_path = build_path_from_dentry(dentry);
  201. if (search_path == NULL)
  202. return NULL;
  203. if (cifs_sb->tcon->Flags & SMB_SHARE_IS_IN_DFS) {
  204. /* we should use full path name to correct working with DFS */
  205. l_max_len = strnlen(cifs_sb->tcon->treeName, MAX_TREE_SIZE+1) +
  206. strnlen(search_path, MAX_PATHCONF) + 1;
  207. tmp_path = kmalloc(l_max_len, GFP_KERNEL);
  208. if (tmp_path == NULL) {
  209. kfree(search_path);
  210. return NULL;
  211. }
  212. strncpy(tmp_path, cifs_sb->tcon->treeName, l_max_len);
  213. strcat(tmp_path, search_path);
  214. tmp_path[l_max_len-1] = 0;
  215. full_path = tmp_path;
  216. kfree(search_path);
  217. } else {
  218. full_path = search_path;
  219. }
  220. return full_path;
  221. }
  222. static int add_mount_helper(struct vfsmount *newmnt, struct nameidata *nd,
  223. struct list_head *mntlist)
  224. {
  225. /* stolen from afs code */
  226. int err;
  227. mntget(newmnt);
  228. err = do_add_mount(newmnt, nd, nd->path.mnt->mnt_flags, mntlist);
  229. switch (err) {
  230. case 0:
  231. dput(nd->path.dentry);
  232. mntput(nd->path.mnt);
  233. nd->path.mnt = newmnt;
  234. nd->path.dentry = dget(newmnt->mnt_root);
  235. break;
  236. case -EBUSY:
  237. /* someone else made a mount here whilst we were busy */
  238. while (d_mountpoint(nd->path.dentry) &&
  239. follow_down(&nd->path.mnt, &nd->path.dentry))
  240. ;
  241. err = 0;
  242. default:
  243. mntput(newmnt);
  244. break;
  245. }
  246. return err;
  247. }
  248. static void dump_referral(const struct dfs_info3_param *ref)
  249. {
  250. cFYI(1, ("DFS: ref path: %s", ref->path_name));
  251. cFYI(1, ("DFS: node path: %s", ref->node_name));
  252. cFYI(1, ("DFS: fl: %hd, srv_type: %hd", ref->flags, ref->server_type));
  253. cFYI(1, ("DFS: ref_flags: %hd, path_consumed: %hd", ref->ref_flag,
  254. ref->path_consumed));
  255. }
  256. static void*
  257. cifs_dfs_follow_mountpoint(struct dentry *dentry, struct nameidata *nd)
  258. {
  259. struct dfs_info3_param *referrals = NULL;
  260. unsigned int num_referrals = 0;
  261. struct cifs_sb_info *cifs_sb;
  262. struct cifsSesInfo *ses;
  263. char *full_path = NULL;
  264. int xid, i;
  265. int rc = 0;
  266. struct vfsmount *mnt = ERR_PTR(-ENOENT);
  267. cFYI(1, ("in %s", __func__));
  268. BUG_ON(IS_ROOT(dentry));
  269. xid = GetXid();
  270. dput(nd->path.dentry);
  271. nd->path.dentry = dget(dentry);
  272. cifs_sb = CIFS_SB(dentry->d_inode->i_sb);
  273. ses = cifs_sb->tcon->ses;
  274. if (!ses) {
  275. rc = -EINVAL;
  276. goto out_err;
  277. }
  278. full_path = build_full_dfs_path_from_dentry(dentry);
  279. if (full_path == NULL) {
  280. rc = -ENOMEM;
  281. goto out_err;
  282. }
  283. rc = get_dfs_path(xid, ses , full_path, cifs_sb->local_nls,
  284. &num_referrals, &referrals,
  285. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  286. for (i = 0; i < num_referrals; i++) {
  287. dump_referral(referrals+i);
  288. /* connect to a storage node */
  289. if (referrals[i].flags & DFSREF_STORAGE_SERVER) {
  290. int len;
  291. len = strlen(referrals[i].node_name);
  292. if (len < 2) {
  293. cERROR(1, ("%s: Net Address path too short: %s",
  294. __func__, referrals[i].node_name));
  295. rc = -EINVAL;
  296. goto out_err;
  297. }
  298. mnt = cifs_dfs_do_refmount(nd->path.mnt,
  299. nd->path.dentry,
  300. referrals[i].node_name);
  301. cFYI(1, ("%s: cifs_dfs_do_refmount:%s , mnt:%p",
  302. __func__,
  303. referrals[i].node_name, mnt));
  304. /* complete mount procedure if we accured submount */
  305. if (!IS_ERR(mnt))
  306. break;
  307. }
  308. }
  309. /* we need it cause for() above could exit without valid submount */
  310. rc = PTR_ERR(mnt);
  311. if (IS_ERR(mnt))
  312. goto out_err;
  313. nd->path.mnt->mnt_flags |= MNT_SHRINKABLE;
  314. rc = add_mount_helper(mnt, nd, &cifs_dfs_automount_list);
  315. out:
  316. FreeXid(xid);
  317. free_dfs_info_array(referrals, num_referrals);
  318. kfree(full_path);
  319. cFYI(1, ("leaving %s" , __func__));
  320. return ERR_PTR(rc);
  321. out_err:
  322. path_put(&nd->path);
  323. goto out;
  324. }
  325. struct inode_operations cifs_dfs_referral_inode_operations = {
  326. .follow_link = cifs_dfs_follow_mountpoint,
  327. };