link.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * fs/cifs/link.c
  3. *
  4. * Copyright (C) International Business Machines Corp., 2002,2003
  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/namei.h>
  24. #include "cifsfs.h"
  25. #include "cifspdu.h"
  26. #include "cifsglob.h"
  27. #include "cifsproto.h"
  28. #include "cifs_debug.h"
  29. #include "cifs_fs_sb.h"
  30. int
  31. cifs_hardlink(struct dentry *old_file, struct inode *inode,
  32. struct dentry *direntry)
  33. {
  34. int rc = -EACCES;
  35. int xid;
  36. char *fromName = NULL;
  37. char *toName = NULL;
  38. struct cifs_sb_info *cifs_sb_target;
  39. struct cifsTconInfo *pTcon;
  40. struct cifsInodeInfo *cifsInode;
  41. xid = GetXid();
  42. cifs_sb_target = CIFS_SB(inode->i_sb);
  43. pTcon = cifs_sb_target->tcon;
  44. /* No need to check for cross device links since server will do that
  45. BB note DFS case in future though (when we may have to check) */
  46. mutex_lock(&inode->i_sb->s_vfs_rename_mutex);
  47. fromName = build_path_from_dentry(old_file);
  48. toName = build_path_from_dentry(direntry);
  49. mutex_unlock(&inode->i_sb->s_vfs_rename_mutex);
  50. if((fromName == NULL) || (toName == NULL)) {
  51. rc = -ENOMEM;
  52. goto cifs_hl_exit;
  53. }
  54. if (cifs_sb_target->tcon->ses->capabilities & CAP_UNIX)
  55. rc = CIFSUnixCreateHardLink(xid, pTcon, fromName, toName,
  56. cifs_sb_target->local_nls,
  57. cifs_sb_target->mnt_cifs_flags &
  58. CIFS_MOUNT_MAP_SPECIAL_CHR);
  59. else {
  60. rc = CIFSCreateHardLink(xid, pTcon, fromName, toName,
  61. cifs_sb_target->local_nls,
  62. cifs_sb_target->mnt_cifs_flags &
  63. CIFS_MOUNT_MAP_SPECIAL_CHR);
  64. if((rc == -EIO) || (rc == -EINVAL))
  65. rc = -EOPNOTSUPP;
  66. }
  67. /* if (!rc) */
  68. {
  69. /* renew_parental_timestamps(old_file);
  70. inode->i_nlink++;
  71. mark_inode_dirty(inode);
  72. d_instantiate(direntry, inode); */
  73. /* BB add call to either mark inode dirty or refresh its data and timestamp to current time */
  74. }
  75. d_drop(direntry); /* force new lookup from server */
  76. cifsInode = CIFS_I(old_file->d_inode);
  77. cifsInode->time = 0; /* will force revalidate to go get info when needed */
  78. cifs_hl_exit:
  79. kfree(fromName);
  80. kfree(toName);
  81. FreeXid(xid);
  82. return rc;
  83. }
  84. void *
  85. cifs_follow_link(struct dentry *direntry, struct nameidata *nd)
  86. {
  87. struct inode *inode = direntry->d_inode;
  88. int rc = -EACCES;
  89. int xid;
  90. char *full_path = NULL;
  91. char * target_path = ERR_PTR(-ENOMEM);
  92. struct cifs_sb_info *cifs_sb;
  93. struct cifsTconInfo *pTcon;
  94. xid = GetXid();
  95. mutex_lock(&direntry->d_sb->s_vfs_rename_mutex);
  96. full_path = build_path_from_dentry(direntry);
  97. mutex_unlock(&direntry->d_sb->s_vfs_rename_mutex);
  98. if (!full_path)
  99. goto out_no_free;
  100. cFYI(1, ("Full path: %s inode = 0x%p", full_path, inode));
  101. cifs_sb = CIFS_SB(inode->i_sb);
  102. pTcon = cifs_sb->tcon;
  103. target_path = kmalloc(PATH_MAX, GFP_KERNEL);
  104. if (!target_path) {
  105. target_path = ERR_PTR(-ENOMEM);
  106. goto out;
  107. }
  108. /* BB add read reparse point symlink code and Unix extensions symlink code here BB */
  109. if (pTcon->ses->capabilities & CAP_UNIX)
  110. rc = CIFSSMBUnixQuerySymLink(xid, pTcon, full_path,
  111. target_path,
  112. PATH_MAX-1,
  113. cifs_sb->local_nls);
  114. else {
  115. /* rc = CIFSSMBQueryReparseLinkInfo */
  116. /* BB Add code to Query ReparsePoint info */
  117. /* BB Add MAC style xsymlink check here if enabled */
  118. }
  119. if (rc == 0) {
  120. /* BB Add special case check for Samba DFS symlinks */
  121. target_path[PATH_MAX-1] = 0;
  122. } else {
  123. kfree(target_path);
  124. target_path = ERR_PTR(rc);
  125. }
  126. out:
  127. kfree(full_path);
  128. out_no_free:
  129. FreeXid(xid);
  130. nd_set_link(nd, target_path);
  131. return NULL; /* No cookie */
  132. }
  133. int
  134. cifs_symlink(struct inode *inode, struct dentry *direntry, const char *symname)
  135. {
  136. int rc = -EOPNOTSUPP;
  137. int xid;
  138. struct cifs_sb_info *cifs_sb;
  139. struct cifsTconInfo *pTcon;
  140. char *full_path = NULL;
  141. struct inode *newinode = NULL;
  142. xid = GetXid();
  143. cifs_sb = CIFS_SB(inode->i_sb);
  144. pTcon = cifs_sb->tcon;
  145. mutex_lock(&inode->i_sb->s_vfs_rename_mutex);
  146. full_path = build_path_from_dentry(direntry);
  147. mutex_unlock(&inode->i_sb->s_vfs_rename_mutex);
  148. if(full_path == NULL) {
  149. FreeXid(xid);
  150. return -ENOMEM;
  151. }
  152. cFYI(1, ("Full path: %s ", full_path));
  153. cFYI(1, ("symname is %s", symname));
  154. /* BB what if DFS and this volume is on different share? BB */
  155. if (cifs_sb->tcon->ses->capabilities & CAP_UNIX)
  156. rc = CIFSUnixCreateSymLink(xid, pTcon, full_path, symname,
  157. cifs_sb->local_nls);
  158. /* else
  159. rc = CIFSCreateReparseSymLink(xid, pTcon, fromName, toName,cifs_sb_target->local_nls); */
  160. if (rc == 0) {
  161. if (pTcon->ses->capabilities & CAP_UNIX)
  162. rc = cifs_get_inode_info_unix(&newinode, full_path,
  163. inode->i_sb,xid);
  164. else
  165. rc = cifs_get_inode_info(&newinode, full_path, NULL,
  166. inode->i_sb,xid);
  167. if (rc != 0) {
  168. cFYI(1,
  169. ("Create symlink worked but get_inode_info failed with rc = %d ",
  170. rc));
  171. } else {
  172. if (pTcon->nocase)
  173. direntry->d_op = &cifs_ci_dentry_ops;
  174. else
  175. direntry->d_op = &cifs_dentry_ops;
  176. d_instantiate(direntry, newinode);
  177. }
  178. }
  179. kfree(full_path);
  180. FreeXid(xid);
  181. return rc;
  182. }
  183. int
  184. cifs_readlink(struct dentry *direntry, char __user *pBuffer, int buflen)
  185. {
  186. struct inode *inode = direntry->d_inode;
  187. int rc = -EACCES;
  188. int xid;
  189. int oplock = FALSE;
  190. struct cifs_sb_info *cifs_sb;
  191. struct cifsTconInfo *pTcon;
  192. char *full_path = NULL;
  193. char *tmp_path = NULL;
  194. char * tmpbuffer;
  195. unsigned char * referrals = NULL;
  196. int num_referrals = 0;
  197. int len;
  198. __u16 fid;
  199. xid = GetXid();
  200. cifs_sb = CIFS_SB(inode->i_sb);
  201. pTcon = cifs_sb->tcon;
  202. /* BB would it be safe against deadlock to grab this sem
  203. even though rename itself grabs the sem and calls lookup? */
  204. /* mutex_lock(&inode->i_sb->s_vfs_rename_mutex);*/
  205. full_path = build_path_from_dentry(direntry);
  206. /* mutex_unlock(&inode->i_sb->s_vfs_rename_mutex);*/
  207. if(full_path == NULL) {
  208. FreeXid(xid);
  209. return -ENOMEM;
  210. }
  211. cFYI(1,
  212. ("Full path: %s inode = 0x%p pBuffer = 0x%p buflen = %d",
  213. full_path, inode, pBuffer, buflen));
  214. if(buflen > PATH_MAX)
  215. len = PATH_MAX;
  216. else
  217. len = buflen;
  218. tmpbuffer = kmalloc(len,GFP_KERNEL);
  219. if(tmpbuffer == NULL) {
  220. kfree(full_path);
  221. FreeXid(xid);
  222. return -ENOMEM;
  223. }
  224. /* BB add read reparse point symlink code and Unix extensions symlink code here BB */
  225. if (cifs_sb->tcon->ses->capabilities & CAP_UNIX)
  226. rc = CIFSSMBUnixQuerySymLink(xid, pTcon, full_path,
  227. tmpbuffer,
  228. len - 1,
  229. cifs_sb->local_nls);
  230. else {
  231. rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN, GENERIC_READ,
  232. OPEN_REPARSE_POINT,&fid, &oplock, NULL,
  233. cifs_sb->local_nls,
  234. cifs_sb->mnt_cifs_flags &
  235. CIFS_MOUNT_MAP_SPECIAL_CHR);
  236. if(!rc) {
  237. rc = CIFSSMBQueryReparseLinkInfo(xid, pTcon, full_path,
  238. tmpbuffer,
  239. len - 1,
  240. fid,
  241. cifs_sb->local_nls);
  242. if(CIFSSMBClose(xid, pTcon, fid)) {
  243. cFYI(1,("Error closing junction point (open for ioctl)"));
  244. }
  245. if(rc == -EIO) {
  246. /* Query if DFS Junction */
  247. tmp_path =
  248. kmalloc(MAX_TREE_SIZE + MAX_PATHCONF + 1,
  249. GFP_KERNEL);
  250. if (tmp_path) {
  251. strncpy(tmp_path, pTcon->treeName, MAX_TREE_SIZE);
  252. strncat(tmp_path, full_path, MAX_PATHCONF);
  253. rc = get_dfs_path(xid, pTcon->ses, tmp_path,
  254. cifs_sb->local_nls,
  255. &num_referrals, &referrals,
  256. cifs_sb->mnt_cifs_flags &
  257. CIFS_MOUNT_MAP_SPECIAL_CHR);
  258. cFYI(1,("Get DFS for %s rc = %d ",tmp_path, rc));
  259. if((num_referrals == 0) && (rc == 0))
  260. rc = -EACCES;
  261. else {
  262. cFYI(1,("num referral: %d",num_referrals));
  263. if(referrals) {
  264. cFYI(1,("referral string: %s ",referrals));
  265. strncpy(tmpbuffer, referrals, len-1);
  266. }
  267. }
  268. kfree(referrals);
  269. kfree(tmp_path);
  270. }
  271. /* BB add code like else decode referrals then memcpy to
  272. tmpbuffer and free referrals string array BB */
  273. }
  274. }
  275. }
  276. /* BB Anything else to do to handle recursive links? */
  277. /* BB Should we be using page ops here? */
  278. /* BB null terminate returned string in pBuffer? BB */
  279. if (rc == 0) {
  280. rc = vfs_readlink(direntry, pBuffer, len, tmpbuffer);
  281. cFYI(1,
  282. ("vfs_readlink called from cifs_readlink returned %d",
  283. rc));
  284. }
  285. kfree(tmpbuffer);
  286. kfree(full_path);
  287. FreeXid(xid);
  288. return rc;
  289. }
  290. void cifs_put_link(struct dentry *direntry, struct nameidata *nd, void *cookie)
  291. {
  292. char *p = nd_get_link(nd);
  293. if (!IS_ERR(p))
  294. kfree(p);
  295. }