link.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * fs/cifs/link.c
  3. *
  4. * Copyright (C) International Business Machines Corp., 2002,2008
  5. * Author(s): Steve French (sfrench@us.ibm.com)
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published
  9. * by the Free Software Foundation; either version 2.1 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  15. * the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/fs.h>
  22. #include <linux/stat.h>
  23. #include <linux/slab.h>
  24. #include <linux/namei.h>
  25. #include "cifsfs.h"
  26. #include "cifspdu.h"
  27. #include "cifsglob.h"
  28. #include "cifsproto.h"
  29. #include "cifs_debug.h"
  30. #include "cifs_fs_sb.h"
  31. #include "md5.h"
  32. #define CIFS_MF_SYMLINK_LEN_OFFSET (4+1)
  33. #define CIFS_MF_SYMLINK_MD5_OFFSET (CIFS_MF_SYMLINK_LEN_OFFSET+(4+1))
  34. #define CIFS_MF_SYMLINK_LINK_OFFSET (CIFS_MF_SYMLINK_MD5_OFFSET+(32+1))
  35. #define CIFS_MF_SYMLINK_LINK_MAXLEN (1024)
  36. #define CIFS_MF_SYMLINK_FILE_SIZE \
  37. (CIFS_MF_SYMLINK_LINK_OFFSET + CIFS_MF_SYMLINK_LINK_MAXLEN)
  38. #define CIFS_MF_SYMLINK_LEN_FORMAT "XSym\n%04u\n"
  39. #define CIFS_MF_SYMLINK_MD5_FORMAT \
  40. "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n"
  41. #define CIFS_MF_SYMLINK_MD5_ARGS(md5_hash) \
  42. md5_hash[0], md5_hash[1], md5_hash[2], md5_hash[3], \
  43. md5_hash[4], md5_hash[5], md5_hash[6], md5_hash[7], \
  44. md5_hash[8], md5_hash[9], md5_hash[10], md5_hash[11],\
  45. md5_hash[12], md5_hash[13], md5_hash[14], md5_hash[15]
  46. static int
  47. CIFSParseMFSymlink(const u8 *buf,
  48. unsigned int buf_len,
  49. unsigned int *_link_len,
  50. char **_link_str)
  51. {
  52. int rc;
  53. unsigned int link_len;
  54. const char *md5_str1;
  55. const char *link_str;
  56. struct MD5Context md5_ctx;
  57. u8 md5_hash[16];
  58. char md5_str2[34];
  59. if (buf_len != CIFS_MF_SYMLINK_FILE_SIZE)
  60. return -EINVAL;
  61. md5_str1 = (const char *)&buf[CIFS_MF_SYMLINK_MD5_OFFSET];
  62. link_str = (const char *)&buf[CIFS_MF_SYMLINK_LINK_OFFSET];
  63. rc = sscanf(buf, CIFS_MF_SYMLINK_LEN_FORMAT, &link_len);
  64. if (rc != 1)
  65. return -EINVAL;
  66. cifs_MD5_init(&md5_ctx);
  67. cifs_MD5_update(&md5_ctx, (const u8 *)link_str, link_len);
  68. cifs_MD5_final(md5_hash, &md5_ctx);
  69. snprintf(md5_str2, sizeof(md5_str2),
  70. CIFS_MF_SYMLINK_MD5_FORMAT,
  71. CIFS_MF_SYMLINK_MD5_ARGS(md5_hash));
  72. if (strncmp(md5_str1, md5_str2, 17) != 0)
  73. return -EINVAL;
  74. if (_link_str) {
  75. *_link_str = kstrndup(link_str, link_len, GFP_KERNEL);
  76. if (!*_link_str)
  77. return -ENOMEM;
  78. }
  79. *_link_len = link_len;
  80. return 0;
  81. }
  82. int
  83. cifs_hardlink(struct dentry *old_file, struct inode *inode,
  84. struct dentry *direntry)
  85. {
  86. int rc = -EACCES;
  87. int xid;
  88. char *fromName = NULL;
  89. char *toName = NULL;
  90. struct cifs_sb_info *cifs_sb_target;
  91. struct cifsTconInfo *pTcon;
  92. struct cifsInodeInfo *cifsInode;
  93. xid = GetXid();
  94. cifs_sb_target = CIFS_SB(inode->i_sb);
  95. pTcon = cifs_sb_target->tcon;
  96. /* No need to check for cross device links since server will do that
  97. BB note DFS case in future though (when we may have to check) */
  98. fromName = build_path_from_dentry(old_file);
  99. toName = build_path_from_dentry(direntry);
  100. if ((fromName == NULL) || (toName == NULL)) {
  101. rc = -ENOMEM;
  102. goto cifs_hl_exit;
  103. }
  104. /* if (cifs_sb_target->tcon->ses->capabilities & CAP_UNIX)*/
  105. if (pTcon->unix_ext)
  106. rc = CIFSUnixCreateHardLink(xid, pTcon, fromName, toName,
  107. cifs_sb_target->local_nls,
  108. cifs_sb_target->mnt_cifs_flags &
  109. CIFS_MOUNT_MAP_SPECIAL_CHR);
  110. else {
  111. rc = CIFSCreateHardLink(xid, pTcon, fromName, toName,
  112. cifs_sb_target->local_nls,
  113. cifs_sb_target->mnt_cifs_flags &
  114. CIFS_MOUNT_MAP_SPECIAL_CHR);
  115. if ((rc == -EIO) || (rc == -EINVAL))
  116. rc = -EOPNOTSUPP;
  117. }
  118. d_drop(direntry); /* force new lookup from server of target */
  119. /* if source file is cached (oplocked) revalidate will not go to server
  120. until the file is closed or oplock broken so update nlinks locally */
  121. if (old_file->d_inode) {
  122. cifsInode = CIFS_I(old_file->d_inode);
  123. if (rc == 0) {
  124. old_file->d_inode->i_nlink++;
  125. /* BB should we make this contingent on superblock flag NOATIME? */
  126. /* old_file->d_inode->i_ctime = CURRENT_TIME;*/
  127. /* parent dir timestamps will update from srv
  128. within a second, would it really be worth it
  129. to set the parent dir cifs inode time to zero
  130. to force revalidate (faster) for it too? */
  131. }
  132. /* if not oplocked will force revalidate to get info
  133. on source file from srv */
  134. cifsInode->time = 0;
  135. /* Will update parent dir timestamps from srv within a second.
  136. Would it really be worth it to set the parent dir (cifs
  137. inode) time field to zero to force revalidate on parent
  138. directory faster ie
  139. CIFS_I(inode)->time = 0; */
  140. }
  141. cifs_hl_exit:
  142. kfree(fromName);
  143. kfree(toName);
  144. FreeXid(xid);
  145. return rc;
  146. }
  147. void *
  148. cifs_follow_link(struct dentry *direntry, struct nameidata *nd)
  149. {
  150. struct inode *inode = direntry->d_inode;
  151. int rc = -ENOMEM;
  152. int xid;
  153. char *full_path = NULL;
  154. char *target_path = NULL;
  155. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  156. struct cifsTconInfo *tcon = cifs_sb->tcon;
  157. xid = GetXid();
  158. /*
  159. * For now, we just handle symlinks with unix extensions enabled.
  160. * Eventually we should handle NTFS reparse points, and MacOS
  161. * symlink support. For instance...
  162. *
  163. * rc = CIFSSMBQueryReparseLinkInfo(...)
  164. *
  165. * For now, just return -EACCES when the server doesn't support posix
  166. * extensions. Note that we still allow querying symlinks when posix
  167. * extensions are manually disabled. We could disable these as well
  168. * but there doesn't seem to be any harm in allowing the client to
  169. * read them.
  170. */
  171. if (!(tcon->ses->capabilities & CAP_UNIX)) {
  172. rc = -EACCES;
  173. goto out;
  174. }
  175. full_path = build_path_from_dentry(direntry);
  176. if (!full_path)
  177. goto out;
  178. cFYI(1, "Full path: %s inode = 0x%p", full_path, inode);
  179. rc = CIFSSMBUnixQuerySymLink(xid, tcon, full_path, &target_path,
  180. cifs_sb->local_nls);
  181. kfree(full_path);
  182. out:
  183. if (rc != 0) {
  184. kfree(target_path);
  185. target_path = ERR_PTR(rc);
  186. }
  187. FreeXid(xid);
  188. nd_set_link(nd, target_path);
  189. return NULL;
  190. }
  191. int
  192. cifs_symlink(struct inode *inode, struct dentry *direntry, const char *symname)
  193. {
  194. int rc = -EOPNOTSUPP;
  195. int xid;
  196. struct cifs_sb_info *cifs_sb;
  197. struct cifsTconInfo *pTcon;
  198. char *full_path = NULL;
  199. struct inode *newinode = NULL;
  200. xid = GetXid();
  201. cifs_sb = CIFS_SB(inode->i_sb);
  202. pTcon = cifs_sb->tcon;
  203. full_path = build_path_from_dentry(direntry);
  204. if (full_path == NULL) {
  205. rc = -ENOMEM;
  206. FreeXid(xid);
  207. return rc;
  208. }
  209. cFYI(1, "Full path: %s", full_path);
  210. cFYI(1, "symname is %s", symname);
  211. /* BB what if DFS and this volume is on different share? BB */
  212. if (pTcon->unix_ext)
  213. rc = CIFSUnixCreateSymLink(xid, pTcon, full_path, symname,
  214. cifs_sb->local_nls);
  215. /* else
  216. rc = CIFSCreateReparseSymLink(xid, pTcon, fromName, toName,
  217. cifs_sb_target->local_nls); */
  218. if (rc == 0) {
  219. if (pTcon->unix_ext)
  220. rc = cifs_get_inode_info_unix(&newinode, full_path,
  221. inode->i_sb, xid);
  222. else
  223. rc = cifs_get_inode_info(&newinode, full_path, NULL,
  224. inode->i_sb, xid, NULL);
  225. if (rc != 0) {
  226. cFYI(1, "Create symlink ok, getinodeinfo fail rc = %d",
  227. rc);
  228. } else {
  229. if (pTcon->nocase)
  230. direntry->d_op = &cifs_ci_dentry_ops;
  231. else
  232. direntry->d_op = &cifs_dentry_ops;
  233. d_instantiate(direntry, newinode);
  234. }
  235. }
  236. kfree(full_path);
  237. FreeXid(xid);
  238. return rc;
  239. }
  240. void cifs_put_link(struct dentry *direntry, struct nameidata *nd, void *cookie)
  241. {
  242. char *p = nd_get_link(nd);
  243. if (!IS_ERR(p))
  244. kfree(p);
  245. }