link.c 9.3 KB

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