cifs_dfs_ref.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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/slab.h>
  18. #include <linux/vfs.h>
  19. #include <linux/fs.h>
  20. #include "cifsglob.h"
  21. #include "cifsproto.h"
  22. #include "cifsfs.h"
  23. #include "dns_resolve.h"
  24. #include "cifs_debug.h"
  25. static LIST_HEAD(cifs_dfs_automount_list);
  26. static void cifs_dfs_expire_automounts(struct work_struct *work);
  27. static DECLARE_DELAYED_WORK(cifs_dfs_automount_task,
  28. cifs_dfs_expire_automounts);
  29. static int cifs_dfs_mountpoint_expiry_timeout = 500 * HZ;
  30. static void cifs_dfs_expire_automounts(struct work_struct *work)
  31. {
  32. struct list_head *list = &cifs_dfs_automount_list;
  33. mark_mounts_for_expiry(list);
  34. if (!list_empty(list))
  35. schedule_delayed_work(&cifs_dfs_automount_task,
  36. cifs_dfs_mountpoint_expiry_timeout);
  37. }
  38. void cifs_dfs_release_automount_timer(void)
  39. {
  40. BUG_ON(!list_empty(&cifs_dfs_automount_list));
  41. cancel_delayed_work_sync(&cifs_dfs_automount_task);
  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 necessary.
  51. * Returns pointer to share name on success or ERR_PTR 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 ERR_PTR(-ENOMEM);
  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 ERR_PTR(-EINVAL);
  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. * cifs_compose_mount_options - creates mount options for refferral
  94. * @sb_mountdata: parent/root DFS mount options (template)
  95. * @fullpath: full path in UNC format
  96. * @ref: server's referral
  97. * @devname: pointer for saving device name
  98. *
  99. * creates mount options for submount based on template options sb_mountdata
  100. * and replacing unc,ip,prefixpath options with ones we've got form ref_unc.
  101. *
  102. * Returns: pointer to new mount options or ERR_PTR.
  103. * Caller is responcible for freeing retunrned value if it is not error.
  104. */
  105. char *cifs_compose_mount_options(const char *sb_mountdata,
  106. const char *fullpath,
  107. const struct dfs_info3_param *ref,
  108. char **devname)
  109. {
  110. int rc;
  111. char *mountdata = NULL;
  112. int md_len;
  113. char *tkn_e;
  114. char *srvIP = NULL;
  115. char sep = ',';
  116. int off, noff;
  117. if (sb_mountdata == NULL)
  118. return ERR_PTR(-EINVAL);
  119. *devname = cifs_get_share_name(ref->node_name);
  120. if (IS_ERR(*devname)) {
  121. rc = PTR_ERR(*devname);
  122. *devname = NULL;
  123. goto compose_mount_options_err;
  124. }
  125. rc = dns_resolve_server_name_to_ip(*devname, &srvIP);
  126. if (rc < 0) {
  127. cERROR(1, "%s: Failed to resolve server part of %s to IP: %d",
  128. __func__, *devname, rc);
  129. goto compose_mount_options_err;
  130. }
  131. /* md_len = strlen(...) + 12 for 'sep+prefixpath='
  132. * assuming that we have 'unc=' and 'ip=' in
  133. * the original sb_mountdata
  134. */
  135. md_len = strlen(sb_mountdata) + rc + strlen(ref->node_name) + 12;
  136. mountdata = kzalloc(md_len+1, GFP_KERNEL);
  137. if (mountdata == NULL) {
  138. rc = -ENOMEM;
  139. goto compose_mount_options_err;
  140. }
  141. /* copy all options except of unc,ip,prefixpath */
  142. off = 0;
  143. if (strncmp(sb_mountdata, "sep=", 4) == 0) {
  144. sep = sb_mountdata[4];
  145. strncpy(mountdata, sb_mountdata, 5);
  146. off += 5;
  147. }
  148. do {
  149. tkn_e = strchr(sb_mountdata + off, sep);
  150. if (tkn_e == NULL)
  151. noff = strlen(sb_mountdata + off);
  152. else
  153. noff = tkn_e - (sb_mountdata + off) + 1;
  154. if (strnicmp(sb_mountdata + off, "unc=", 4) == 0) {
  155. off += noff;
  156. continue;
  157. }
  158. if (strnicmp(sb_mountdata + off, "ip=", 3) == 0) {
  159. off += noff;
  160. continue;
  161. }
  162. if (strnicmp(sb_mountdata + off, "prefixpath=", 11) == 0) {
  163. off += noff;
  164. continue;
  165. }
  166. strncat(mountdata, sb_mountdata + off, noff);
  167. off += noff;
  168. } while (tkn_e);
  169. strcat(mountdata, sb_mountdata + off);
  170. mountdata[md_len] = '\0';
  171. /* copy new IP and ref share name */
  172. if (mountdata[strlen(mountdata) - 1] != sep)
  173. strncat(mountdata, &sep, 1);
  174. strcat(mountdata, "ip=");
  175. strcat(mountdata, srvIP);
  176. strncat(mountdata, &sep, 1);
  177. strcat(mountdata, "unc=");
  178. strcat(mountdata, *devname);
  179. /* find & copy prefixpath */
  180. tkn_e = strchr(ref->node_name + 2, '\\');
  181. if (tkn_e == NULL) {
  182. /* invalid unc, missing share name*/
  183. rc = -EINVAL;
  184. goto compose_mount_options_err;
  185. }
  186. tkn_e = strchr(tkn_e + 1, '\\');
  187. if (tkn_e || (strlen(fullpath) - ref->path_consumed)) {
  188. strncat(mountdata, &sep, 1);
  189. strcat(mountdata, "prefixpath=");
  190. if (tkn_e)
  191. strcat(mountdata, tkn_e + 1);
  192. strcat(mountdata, fullpath + ref->path_consumed);
  193. }
  194. /*cFYI(1, "%s: parent mountdata: %s", __func__,sb_mountdata);*/
  195. /*cFYI(1, "%s: submount mountdata: %s", __func__, mountdata );*/
  196. compose_mount_options_out:
  197. kfree(srvIP);
  198. return mountdata;
  199. compose_mount_options_err:
  200. kfree(mountdata);
  201. mountdata = ERR_PTR(rc);
  202. goto compose_mount_options_out;
  203. }
  204. /**
  205. * cifs_dfs_do_refmount - mounts specified path using provided refferal
  206. * @cifs_sb: parent/root superblock
  207. * @fullpath: full path in UNC format
  208. * @ref: server's referral
  209. */
  210. static struct vfsmount *cifs_dfs_do_refmount(struct cifs_sb_info *cifs_sb,
  211. const char *fullpath, const struct dfs_info3_param *ref)
  212. {
  213. struct vfsmount *mnt;
  214. char *mountdata;
  215. char *devname = NULL;
  216. /* strip first '\' from fullpath */
  217. mountdata = cifs_compose_mount_options(cifs_sb->mountdata,
  218. fullpath + 1, ref, &devname);
  219. if (IS_ERR(mountdata))
  220. return (struct vfsmount *)mountdata;
  221. mnt = vfs_kern_mount(&cifs_fs_type, 0, devname, mountdata);
  222. kfree(mountdata);
  223. kfree(devname);
  224. return mnt;
  225. }
  226. static int add_mount_helper(struct vfsmount *newmnt, struct nameidata *nd,
  227. struct list_head *mntlist)
  228. {
  229. /* stolen from afs code */
  230. int err;
  231. mntget(newmnt);
  232. err = do_add_mount(newmnt, &nd->path, nd->path.mnt->mnt_flags | MNT_SHRINKABLE, mntlist);
  233. switch (err) {
  234. case 0:
  235. path_put(&nd->path);
  236. nd->path.mnt = newmnt;
  237. nd->path.dentry = dget(newmnt->mnt_root);
  238. schedule_delayed_work(&cifs_dfs_automount_task,
  239. cifs_dfs_mountpoint_expiry_timeout);
  240. break;
  241. case -EBUSY:
  242. /* someone else made a mount here whilst we were busy */
  243. while (d_mountpoint(nd->path.dentry) &&
  244. follow_down(&nd->path))
  245. ;
  246. err = 0;
  247. default:
  248. mntput(newmnt);
  249. break;
  250. }
  251. return err;
  252. }
  253. static void dump_referral(const struct dfs_info3_param *ref)
  254. {
  255. cFYI(1, "DFS: ref path: %s", ref->path_name);
  256. cFYI(1, "DFS: node path: %s", ref->node_name);
  257. cFYI(1, "DFS: fl: %hd, srv_type: %hd", ref->flags, ref->server_type);
  258. cFYI(1, "DFS: ref_flags: %hd, path_consumed: %hd", ref->ref_flag,
  259. ref->path_consumed);
  260. }
  261. static void*
  262. cifs_dfs_follow_mountpoint(struct dentry *dentry, struct nameidata *nd)
  263. {
  264. struct dfs_info3_param *referrals = NULL;
  265. unsigned int num_referrals = 0;
  266. struct cifs_sb_info *cifs_sb;
  267. struct cifsSesInfo *ses;
  268. char *full_path = NULL;
  269. int xid, i;
  270. int rc = 0;
  271. struct vfsmount *mnt = ERR_PTR(-ENOENT);
  272. struct tcon_link *tlink;
  273. cFYI(1, "in %s", __func__);
  274. BUG_ON(IS_ROOT(dentry));
  275. xid = GetXid();
  276. dput(nd->path.dentry);
  277. nd->path.dentry = dget(dentry);
  278. /*
  279. * The MSDFS spec states that paths in DFS referral requests and
  280. * responses must be prefixed by a single '\' character instead of
  281. * the double backslashes usually used in the UNC. This function
  282. * gives us the latter, so we must adjust the result.
  283. */
  284. full_path = build_path_from_dentry(dentry);
  285. if (full_path == NULL) {
  286. rc = -ENOMEM;
  287. goto out_err;
  288. }
  289. cifs_sb = CIFS_SB(dentry->d_inode->i_sb);
  290. tlink = cifs_sb_tlink(cifs_sb);
  291. if (IS_ERR(tlink)) {
  292. rc = PTR_ERR(tlink);
  293. goto out_err;
  294. }
  295. ses = tlink_tcon(tlink)->ses;
  296. rc = get_dfs_path(xid, ses, full_path + 1, cifs_sb->local_nls,
  297. &num_referrals, &referrals,
  298. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  299. cifs_put_tlink(tlink);
  300. for (i = 0; i < num_referrals; i++) {
  301. int len;
  302. dump_referral(referrals+i);
  303. /* connect to a node */
  304. len = strlen(referrals[i].node_name);
  305. if (len < 2) {
  306. cERROR(1, "%s: Net Address path too short: %s",
  307. __func__, referrals[i].node_name);
  308. rc = -EINVAL;
  309. goto out_err;
  310. }
  311. mnt = cifs_dfs_do_refmount(cifs_sb,
  312. full_path, referrals + i);
  313. cFYI(1, "%s: cifs_dfs_do_refmount:%s , mnt:%p", __func__,
  314. referrals[i].node_name, mnt);
  315. /* complete mount procedure if we accured submount */
  316. if (!IS_ERR(mnt))
  317. break;
  318. }
  319. /* we need it cause for() above could exit without valid submount */
  320. rc = PTR_ERR(mnt);
  321. if (IS_ERR(mnt))
  322. goto out_err;
  323. rc = add_mount_helper(mnt, nd, &cifs_dfs_automount_list);
  324. out:
  325. FreeXid(xid);
  326. free_dfs_info_array(referrals, num_referrals);
  327. kfree(full_path);
  328. cFYI(1, "leaving %s" , __func__);
  329. return ERR_PTR(rc);
  330. out_err:
  331. path_put(&nd->path);
  332. goto out;
  333. }
  334. const struct inode_operations cifs_dfs_referral_inode_operations = {
  335. .follow_link = cifs_dfs_follow_mountpoint,
  336. };