cifs_dfs_ref.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 <linux/inet.h>
  21. #include "cifsglob.h"
  22. #include "cifsproto.h"
  23. #include "cifsfs.h"
  24. #include "dns_resolve.h"
  25. #include "cifs_debug.h"
  26. #include "cifs_unicode.h"
  27. static LIST_HEAD(cifs_dfs_automount_list);
  28. static void cifs_dfs_expire_automounts(struct work_struct *work);
  29. static DECLARE_DELAYED_WORK(cifs_dfs_automount_task,
  30. cifs_dfs_expire_automounts);
  31. static int cifs_dfs_mountpoint_expiry_timeout = 500 * HZ;
  32. static void cifs_dfs_expire_automounts(struct work_struct *work)
  33. {
  34. struct list_head *list = &cifs_dfs_automount_list;
  35. mark_mounts_for_expiry(list);
  36. if (!list_empty(list))
  37. schedule_delayed_work(&cifs_dfs_automount_task,
  38. cifs_dfs_mountpoint_expiry_timeout);
  39. }
  40. void cifs_dfs_release_automount_timer(void)
  41. {
  42. BUG_ON(!list_empty(&cifs_dfs_automount_list));
  43. cancel_delayed_work_sync(&cifs_dfs_automount_task);
  44. }
  45. /**
  46. * cifs_build_devname - build a devicename from a UNC and optional prepath
  47. * @nodename: pointer to UNC string
  48. * @prepath: pointer to prefixpath (or NULL if there isn't one)
  49. *
  50. * Build a new cifs devicename after chasing a DFS referral. Allocate a buffer
  51. * big enough to hold the final thing. Copy the UNC from the nodename, and
  52. * concatenate the prepath onto the end of it if there is one.
  53. *
  54. * Returns pointer to the built string, or a ERR_PTR. Caller is responsible
  55. * for freeing the returned string.
  56. */
  57. static char *
  58. cifs_build_devname(char *nodename, const char *prepath)
  59. {
  60. size_t pplen;
  61. size_t unclen;
  62. char *dev;
  63. char *pos;
  64. /* skip over any preceding delimiters */
  65. nodename += strspn(nodename, "\\");
  66. if (!*nodename)
  67. return ERR_PTR(-EINVAL);
  68. /* get length of UNC and set pos to last char */
  69. unclen = strlen(nodename);
  70. pos = nodename + unclen - 1;
  71. /* trim off any trailing delimiters */
  72. while (*pos == '\\') {
  73. --pos;
  74. --unclen;
  75. }
  76. /* allocate a buffer:
  77. * +2 for preceding "//"
  78. * +1 for delimiter between UNC and prepath
  79. * +1 for trailing NULL
  80. */
  81. pplen = prepath ? strlen(prepath) : 0;
  82. dev = kmalloc(2 + unclen + 1 + pplen + 1, GFP_KERNEL);
  83. if (!dev)
  84. return ERR_PTR(-ENOMEM);
  85. pos = dev;
  86. /* add the initial "//" */
  87. *pos = '/';
  88. ++pos;
  89. *pos = '/';
  90. ++pos;
  91. /* copy in the UNC portion from referral */
  92. memcpy(pos, nodename, unclen);
  93. pos += unclen;
  94. /* copy the prefixpath remainder (if there is one) */
  95. if (pplen) {
  96. *pos = '/';
  97. ++pos;
  98. memcpy(pos, prepath, pplen);
  99. pos += pplen;
  100. }
  101. /* NULL terminator */
  102. *pos = '\0';
  103. convert_delimiter(dev, '/');
  104. return dev;
  105. }
  106. /**
  107. * cifs_compose_mount_options - creates mount options for refferral
  108. * @sb_mountdata: parent/root DFS mount options (template)
  109. * @fullpath: full path in UNC format
  110. * @ref: server's referral
  111. * @devname: pointer for saving device name
  112. *
  113. * creates mount options for submount based on template options sb_mountdata
  114. * and replacing unc,ip,prefixpath options with ones we've got form ref_unc.
  115. *
  116. * Returns: pointer to new mount options or ERR_PTR.
  117. * Caller is responcible for freeing retunrned value if it is not error.
  118. */
  119. char *cifs_compose_mount_options(const char *sb_mountdata,
  120. const char *fullpath,
  121. const struct dfs_info3_param *ref,
  122. char **devname)
  123. {
  124. int rc;
  125. char *mountdata = NULL;
  126. const char *prepath = NULL;
  127. int md_len;
  128. char *tkn_e;
  129. char *srvIP = NULL;
  130. char sep = ',';
  131. int off, noff;
  132. if (sb_mountdata == NULL)
  133. return ERR_PTR(-EINVAL);
  134. if (strlen(fullpath) - ref->path_consumed)
  135. prepath = fullpath + ref->path_consumed;
  136. *devname = cifs_build_devname(ref->node_name, prepath);
  137. if (IS_ERR(*devname)) {
  138. rc = PTR_ERR(*devname);
  139. *devname = NULL;
  140. goto compose_mount_options_err;
  141. }
  142. rc = dns_resolve_server_name_to_ip(*devname, &srvIP);
  143. if (rc < 0) {
  144. cifs_dbg(FYI, "%s: Failed to resolve server part of %s to IP: %d\n",
  145. __func__, *devname, rc);
  146. goto compose_mount_options_err;
  147. }
  148. /*
  149. * In most cases, we'll be building a shorter string than the original,
  150. * but we do have to assume that the address in the ip= option may be
  151. * much longer than the original. Add the max length of an address
  152. * string to the length of the original string to allow for worst case.
  153. */
  154. md_len = strlen(sb_mountdata) + INET6_ADDRSTRLEN;
  155. mountdata = kzalloc(md_len + 1, GFP_KERNEL);
  156. if (mountdata == NULL) {
  157. rc = -ENOMEM;
  158. goto compose_mount_options_err;
  159. }
  160. /* copy all options except of unc,ip,prefixpath */
  161. off = 0;
  162. if (strncmp(sb_mountdata, "sep=", 4) == 0) {
  163. sep = sb_mountdata[4];
  164. strncpy(mountdata, sb_mountdata, 5);
  165. off += 5;
  166. }
  167. do {
  168. tkn_e = strchr(sb_mountdata + off, sep);
  169. if (tkn_e == NULL)
  170. noff = strlen(sb_mountdata + off);
  171. else
  172. noff = tkn_e - (sb_mountdata + off) + 1;
  173. if (strncasecmp(sb_mountdata + off, "unc=", 4) == 0) {
  174. off += noff;
  175. continue;
  176. }
  177. if (strncasecmp(sb_mountdata + off, "ip=", 3) == 0) {
  178. off += noff;
  179. continue;
  180. }
  181. if (strncasecmp(sb_mountdata + off, "prefixpath=", 11) == 0) {
  182. off += noff;
  183. continue;
  184. }
  185. strncat(mountdata, sb_mountdata + off, noff);
  186. off += noff;
  187. } while (tkn_e);
  188. strcat(mountdata, sb_mountdata + off);
  189. mountdata[md_len] = '\0';
  190. /* copy new IP and ref share name */
  191. if (mountdata[strlen(mountdata) - 1] != sep)
  192. strncat(mountdata, &sep, 1);
  193. strcat(mountdata, "ip=");
  194. strcat(mountdata, srvIP);
  195. /*cifs_dbg(FYI, "%s: parent mountdata: %s\n", __func__, sb_mountdata);*/
  196. /*cifs_dbg(FYI, "%s: submount mountdata: %s\n", __func__, mountdata );*/
  197. compose_mount_options_out:
  198. kfree(srvIP);
  199. return mountdata;
  200. compose_mount_options_err:
  201. kfree(mountdata);
  202. mountdata = ERR_PTR(rc);
  203. kfree(*devname);
  204. *devname = NULL;
  205. goto compose_mount_options_out;
  206. }
  207. /**
  208. * cifs_dfs_do_refmount - mounts specified path using provided refferal
  209. * @cifs_sb: parent/root superblock
  210. * @fullpath: full path in UNC format
  211. * @ref: server's referral
  212. */
  213. static struct vfsmount *cifs_dfs_do_refmount(struct cifs_sb_info *cifs_sb,
  214. const char *fullpath, const struct dfs_info3_param *ref)
  215. {
  216. struct vfsmount *mnt;
  217. char *mountdata;
  218. char *devname = NULL;
  219. /* strip first '\' from fullpath */
  220. mountdata = cifs_compose_mount_options(cifs_sb->mountdata,
  221. fullpath + 1, ref, &devname);
  222. if (IS_ERR(mountdata))
  223. return (struct vfsmount *)mountdata;
  224. mnt = vfs_kern_mount(&cifs_fs_type, 0, devname, mountdata);
  225. kfree(mountdata);
  226. kfree(devname);
  227. return mnt;
  228. }
  229. static void dump_referral(const struct dfs_info3_param *ref)
  230. {
  231. cifs_dbg(FYI, "DFS: ref path: %s\n", ref->path_name);
  232. cifs_dbg(FYI, "DFS: node path: %s\n", ref->node_name);
  233. cifs_dbg(FYI, "DFS: fl: %d, srv_type: %d\n",
  234. ref->flags, ref->server_type);
  235. cifs_dbg(FYI, "DFS: ref_flags: %d, path_consumed: %d\n",
  236. ref->ref_flag, ref->path_consumed);
  237. }
  238. /*
  239. * Create a vfsmount that we can automount
  240. */
  241. static struct vfsmount *cifs_dfs_do_automount(struct dentry *mntpt)
  242. {
  243. struct dfs_info3_param *referrals = NULL;
  244. unsigned int num_referrals = 0;
  245. struct cifs_sb_info *cifs_sb;
  246. struct cifs_ses *ses;
  247. char *full_path;
  248. unsigned int xid;
  249. int i;
  250. int rc;
  251. struct vfsmount *mnt;
  252. struct tcon_link *tlink;
  253. cifs_dbg(FYI, "in %s\n", __func__);
  254. BUG_ON(IS_ROOT(mntpt));
  255. /*
  256. * The MSDFS spec states that paths in DFS referral requests and
  257. * responses must be prefixed by a single '\' character instead of
  258. * the double backslashes usually used in the UNC. This function
  259. * gives us the latter, so we must adjust the result.
  260. */
  261. mnt = ERR_PTR(-ENOMEM);
  262. full_path = build_path_from_dentry(mntpt);
  263. if (full_path == NULL)
  264. goto cdda_exit;
  265. cifs_sb = CIFS_SB(d_inode(mntpt)->i_sb);
  266. tlink = cifs_sb_tlink(cifs_sb);
  267. if (IS_ERR(tlink)) {
  268. mnt = ERR_CAST(tlink);
  269. goto free_full_path;
  270. }
  271. ses = tlink_tcon(tlink)->ses;
  272. xid = get_xid();
  273. rc = get_dfs_path(xid, ses, full_path + 1, cifs_sb->local_nls,
  274. &num_referrals, &referrals,
  275. cifs_remap(cifs_sb));
  276. free_xid(xid);
  277. cifs_put_tlink(tlink);
  278. mnt = ERR_PTR(-ENOENT);
  279. for (i = 0; i < num_referrals; i++) {
  280. int len;
  281. dump_referral(referrals + i);
  282. /* connect to a node */
  283. len = strlen(referrals[i].node_name);
  284. if (len < 2) {
  285. cifs_dbg(VFS, "%s: Net Address path too short: %s\n",
  286. __func__, referrals[i].node_name);
  287. mnt = ERR_PTR(-EINVAL);
  288. break;
  289. }
  290. mnt = cifs_dfs_do_refmount(cifs_sb,
  291. full_path, referrals + i);
  292. cifs_dbg(FYI, "%s: cifs_dfs_do_refmount:%s , mnt:%p\n",
  293. __func__, referrals[i].node_name, mnt);
  294. if (!IS_ERR(mnt))
  295. goto success;
  296. }
  297. /* no valid submounts were found; return error from get_dfs_path() by
  298. * preference */
  299. if (rc != 0)
  300. mnt = ERR_PTR(rc);
  301. success:
  302. free_dfs_info_array(referrals, num_referrals);
  303. free_full_path:
  304. kfree(full_path);
  305. cdda_exit:
  306. cifs_dbg(FYI, "leaving %s\n" , __func__);
  307. return mnt;
  308. }
  309. /*
  310. * Attempt to automount the referral
  311. */
  312. struct vfsmount *cifs_dfs_d_automount(struct path *path)
  313. {
  314. struct vfsmount *newmnt;
  315. cifs_dbg(FYI, "in %s\n", __func__);
  316. newmnt = cifs_dfs_do_automount(path->dentry);
  317. if (IS_ERR(newmnt)) {
  318. cifs_dbg(FYI, "leaving %s [automount failed]\n" , __func__);
  319. return newmnt;
  320. }
  321. mntget(newmnt); /* prevent immediate expiration */
  322. mnt_set_expiry(newmnt, &cifs_dfs_automount_list);
  323. schedule_delayed_work(&cifs_dfs_automount_task,
  324. cifs_dfs_mountpoint_expiry_timeout);
  325. cifs_dbg(FYI, "leaving %s [ok]\n" , __func__);
  326. return newmnt;
  327. }
  328. const struct inode_operations cifs_dfs_referral_inode_operations = {
  329. };