cifs_dfs_ref.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 ([email protected])
  8. * Steve French ([email protected])
  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. /* skip initial delimiter */
  137. if (*prepath == '/' || *prepath == '\\')
  138. prepath++;
  139. }
  140. *devname = cifs_build_devname(ref->node_name, prepath);
  141. if (IS_ERR(*devname)) {
  142. rc = PTR_ERR(*devname);
  143. *devname = NULL;
  144. goto compose_mount_options_err;
  145. }
  146. rc = dns_resolve_server_name_to_ip(*devname, &srvIP);
  147. if (rc < 0) {
  148. cifs_dbg(FYI, "%s: Failed to resolve server part of %s to IP: %d\n",
  149. __func__, *devname, rc);
  150. goto compose_mount_options_err;
  151. }
  152. /*
  153. * In most cases, we'll be building a shorter string than the original,
  154. * but we do have to assume that the address in the ip= option may be
  155. * much longer than the original. Add the max length of an address
  156. * string to the length of the original string to allow for worst case.
  157. */
  158. md_len = strlen(sb_mountdata) + INET6_ADDRSTRLEN;
  159. mountdata = kzalloc(md_len + sizeof("ip=") + 1, GFP_KERNEL);
  160. if (mountdata == NULL) {
  161. rc = -ENOMEM;
  162. goto compose_mount_options_err;
  163. }
  164. /* copy all options except of unc,ip,prefixpath */
  165. off = 0;
  166. if (strncmp(sb_mountdata, "sep=", 4) == 0) {
  167. sep = sb_mountdata[4];
  168. strncpy(mountdata, sb_mountdata, 5);
  169. off += 5;
  170. }
  171. do {
  172. tkn_e = strchr(sb_mountdata + off, sep);
  173. if (tkn_e == NULL)
  174. noff = strlen(sb_mountdata + off);
  175. else
  176. noff = tkn_e - (sb_mountdata + off) + 1;
  177. if (strncasecmp(sb_mountdata + off, "unc=", 4) == 0) {
  178. off += noff;
  179. continue;
  180. }
  181. if (strncasecmp(sb_mountdata + off, "ip=", 3) == 0) {
  182. off += noff;
  183. continue;
  184. }
  185. if (strncasecmp(sb_mountdata + off, "prefixpath=", 11) == 0) {
  186. off += noff;
  187. continue;
  188. }
  189. strncat(mountdata, sb_mountdata + off, noff);
  190. off += noff;
  191. } while (tkn_e);
  192. strcat(mountdata, sb_mountdata + off);
  193. mountdata[md_len] = '\0';
  194. /* copy new IP and ref share name */
  195. if (mountdata[strlen(mountdata) - 1] != sep)
  196. strncat(mountdata, &sep, 1);
  197. strcat(mountdata, "ip=");
  198. strcat(mountdata, srvIP);
  199. /*cifs_dbg(FYI, "%s: parent mountdata: %s\n", __func__, sb_mountdata);*/
  200. /*cifs_dbg(FYI, "%s: submount mountdata: %s\n", __func__, mountdata );*/
  201. compose_mount_options_out:
  202. kfree(srvIP);
  203. return mountdata;
  204. compose_mount_options_err:
  205. kfree(mountdata);
  206. mountdata = ERR_PTR(rc);
  207. kfree(*devname);
  208. *devname = NULL;
  209. goto compose_mount_options_out;
  210. }
  211. /**
  212. * cifs_dfs_do_refmount - mounts specified path using provided refferal
  213. * @cifs_sb: parent/root superblock
  214. * @fullpath: full path in UNC format
  215. * @ref: server's referral
  216. */
  217. static struct vfsmount *cifs_dfs_do_refmount(struct dentry *mntpt,
  218. struct cifs_sb_info *cifs_sb,
  219. const char *fullpath, const struct dfs_info3_param *ref)
  220. {
  221. struct vfsmount *mnt;
  222. char *mountdata;
  223. char *devname = NULL;
  224. /* strip first '\' from fullpath */
  225. mountdata = cifs_compose_mount_options(cifs_sb->mountdata,
  226. fullpath + 1, ref, &devname);
  227. if (IS_ERR(mountdata))
  228. return (struct vfsmount *)mountdata;
  229. mnt = vfs_submount(mntpt, &cifs_fs_type, devname, mountdata);
  230. kfree(mountdata);
  231. kfree(devname);
  232. return mnt;
  233. }
  234. static void dump_referral(const struct dfs_info3_param *ref)
  235. {
  236. cifs_dbg(FYI, "DFS: ref path: %s\n", ref->path_name);
  237. cifs_dbg(FYI, "DFS: node path: %s\n", ref->node_name);
  238. cifs_dbg(FYI, "DFS: fl: %d, srv_type: %d\n",
  239. ref->flags, ref->server_type);
  240. cifs_dbg(FYI, "DFS: ref_flags: %d, path_consumed: %d\n",
  241. ref->ref_flag, ref->path_consumed);
  242. }
  243. /*
  244. * Create a vfsmount that we can automount
  245. */
  246. static struct vfsmount *cifs_dfs_do_automount(struct dentry *mntpt)
  247. {
  248. struct dfs_info3_param *referrals = NULL;
  249. unsigned int num_referrals = 0;
  250. struct cifs_sb_info *cifs_sb;
  251. struct cifs_ses *ses;
  252. char *full_path;
  253. unsigned int xid;
  254. int i;
  255. int rc;
  256. struct vfsmount *mnt;
  257. struct tcon_link *tlink;
  258. cifs_dbg(FYI, "in %s\n", __func__);
  259. BUG_ON(IS_ROOT(mntpt));
  260. /*
  261. * The MSDFS spec states that paths in DFS referral requests and
  262. * responses must be prefixed by a single '\' character instead of
  263. * the double backslashes usually used in the UNC. This function
  264. * gives us the latter, so we must adjust the result.
  265. */
  266. mnt = ERR_PTR(-ENOMEM);
  267. full_path = build_path_from_dentry(mntpt);
  268. if (full_path == NULL)
  269. goto cdda_exit;
  270. cifs_sb = CIFS_SB(mntpt->d_sb);
  271. tlink = cifs_sb_tlink(cifs_sb);
  272. if (IS_ERR(tlink)) {
  273. mnt = ERR_CAST(tlink);
  274. goto free_full_path;
  275. }
  276. ses = tlink_tcon(tlink)->ses;
  277. xid = get_xid();
  278. rc = get_dfs_path(xid, ses, full_path + 1, cifs_sb->local_nls,
  279. &num_referrals, &referrals,
  280. cifs_remap(cifs_sb));
  281. free_xid(xid);
  282. cifs_put_tlink(tlink);
  283. mnt = ERR_PTR(-ENOENT);
  284. for (i = 0; i < num_referrals; i++) {
  285. int len;
  286. dump_referral(referrals + i);
  287. /* connect to a node */
  288. len = strlen(referrals[i].node_name);
  289. if (len < 2) {
  290. cifs_dbg(VFS, "%s: Net Address path too short: %s\n",
  291. __func__, referrals[i].node_name);
  292. mnt = ERR_PTR(-EINVAL);
  293. break;
  294. }
  295. mnt = cifs_dfs_do_refmount(mntpt, cifs_sb,
  296. full_path, referrals + i);
  297. cifs_dbg(FYI, "%s: cifs_dfs_do_refmount:%s , mnt:%p\n",
  298. __func__, referrals[i].node_name, mnt);
  299. if (!IS_ERR(mnt))
  300. goto success;
  301. }
  302. /* no valid submounts were found; return error from get_dfs_path() by
  303. * preference */
  304. if (rc != 0)
  305. mnt = ERR_PTR(rc);
  306. success:
  307. free_dfs_info_array(referrals, num_referrals);
  308. free_full_path:
  309. kfree(full_path);
  310. cdda_exit:
  311. cifs_dbg(FYI, "leaving %s\n" , __func__);
  312. return mnt;
  313. }
  314. /*
  315. * Attempt to automount the referral
  316. */
  317. struct vfsmount *cifs_dfs_d_automount(struct path *path)
  318. {
  319. struct vfsmount *newmnt;
  320. cifs_dbg(FYI, "in %s\n", __func__);
  321. newmnt = cifs_dfs_do_automount(path->dentry);
  322. if (IS_ERR(newmnt)) {
  323. cifs_dbg(FYI, "leaving %s [automount failed]\n" , __func__);
  324. return newmnt;
  325. }
  326. mntget(newmnt); /* prevent immediate expiration */
  327. mnt_set_expiry(newmnt, &cifs_dfs_automount_list);
  328. schedule_delayed_work(&cifs_dfs_automount_task,
  329. cifs_dfs_mountpoint_expiry_timeout);
  330. cifs_dbg(FYI, "leaving %s [ok]\n" , __func__);
  331. return newmnt;
  332. }
  333. const struct inode_operations cifs_dfs_referral_inode_operations = {
  334. };