cifs_dfs_ref.c 9.9 KB

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