link.c 8.7 KB

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