cifs_dfs_ref.c 9.8 KB

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