link.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. static int
  83. CIFSFormatMFSymlink(u8 *buf, unsigned int buf_len, const char *link_str)
  84. {
  85. unsigned int link_len;
  86. unsigned int ofs;
  87. struct MD5Context md5_ctx;
  88. u8 md5_hash[16];
  89. if (buf_len != CIFS_MF_SYMLINK_FILE_SIZE)
  90. return -EINVAL;
  91. link_len = strlen(link_str);
  92. if (link_len > CIFS_MF_SYMLINK_LINK_MAXLEN)
  93. return -ENAMETOOLONG;
  94. cifs_MD5_init(&md5_ctx);
  95. cifs_MD5_update(&md5_ctx, (const u8 *)link_str, link_len);
  96. cifs_MD5_final(md5_hash, &md5_ctx);
  97. snprintf(buf, buf_len,
  98. CIFS_MF_SYMLINK_LEN_FORMAT CIFS_MF_SYMLINK_MD5_FORMAT,
  99. link_len,
  100. CIFS_MF_SYMLINK_MD5_ARGS(md5_hash));
  101. ofs = CIFS_MF_SYMLINK_LINK_OFFSET;
  102. memcpy(buf + ofs, link_str, link_len);
  103. ofs += link_len;
  104. if (ofs < CIFS_MF_SYMLINK_FILE_SIZE) {
  105. buf[ofs] = '\n';
  106. ofs++;
  107. }
  108. while (ofs < CIFS_MF_SYMLINK_FILE_SIZE) {
  109. buf[ofs] = ' ';
  110. ofs++;
  111. }
  112. return 0;
  113. }
  114. static int
  115. CIFSCreateMFSymLink(const int xid, struct cifsTconInfo *tcon,
  116. const char *fromName, const char *toName,
  117. const struct nls_table *nls_codepage, int remap)
  118. {
  119. int rc;
  120. int oplock = 0;
  121. __u16 netfid = 0;
  122. u8 *buf;
  123. unsigned int bytes_written = 0;
  124. buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
  125. if (!buf)
  126. return -ENOMEM;
  127. rc = CIFSFormatMFSymlink(buf, CIFS_MF_SYMLINK_FILE_SIZE, toName);
  128. if (rc != 0) {
  129. kfree(buf);
  130. return rc;
  131. }
  132. rc = CIFSSMBOpen(xid, tcon, fromName, FILE_CREATE, GENERIC_WRITE,
  133. CREATE_NOT_DIR, &netfid, &oplock, NULL,
  134. nls_codepage, remap);
  135. if (rc != 0) {
  136. kfree(buf);
  137. return rc;
  138. }
  139. rc = CIFSSMBWrite(xid, tcon, netfid,
  140. CIFS_MF_SYMLINK_FILE_SIZE /* length */,
  141. 0 /* offset */,
  142. &bytes_written, buf, NULL, 0);
  143. CIFSSMBClose(xid, tcon, netfid);
  144. kfree(buf);
  145. if (rc != 0)
  146. return rc;
  147. if (bytes_written != CIFS_MF_SYMLINK_FILE_SIZE)
  148. return -EIO;
  149. return 0;
  150. }
  151. static int
  152. CIFSQueryMFSymLink(const int xid, struct cifsTconInfo *tcon,
  153. const unsigned char *searchName, char **symlinkinfo,
  154. const struct nls_table *nls_codepage, int remap)
  155. {
  156. int rc;
  157. int oplock = 0;
  158. __u16 netfid = 0;
  159. u8 *buf;
  160. char *pbuf;
  161. unsigned int bytes_read = 0;
  162. int buf_type = CIFS_NO_BUFFER;
  163. unsigned int link_len = 0;
  164. FILE_ALL_INFO file_info;
  165. rc = CIFSSMBOpen(xid, tcon, searchName, FILE_OPEN, GENERIC_READ,
  166. CREATE_NOT_DIR, &netfid, &oplock, &file_info,
  167. nls_codepage, remap);
  168. if (rc != 0)
  169. return rc;
  170. if (file_info.EndOfFile != CIFS_MF_SYMLINK_FILE_SIZE) {
  171. CIFSSMBClose(xid, tcon, netfid);
  172. /* it's not a symlink */
  173. return -EINVAL;
  174. }
  175. buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
  176. if (!buf)
  177. return -ENOMEM;
  178. pbuf = buf;
  179. rc = CIFSSMBRead(xid, tcon, netfid,
  180. CIFS_MF_SYMLINK_FILE_SIZE /* length */,
  181. 0 /* offset */,
  182. &bytes_read, &pbuf, &buf_type);
  183. CIFSSMBClose(xid, tcon, netfid);
  184. if (rc != 0) {
  185. kfree(buf);
  186. return rc;
  187. }
  188. rc = CIFSParseMFSymlink(buf, bytes_read, &link_len, symlinkinfo);
  189. kfree(buf);
  190. if (rc != 0)
  191. return rc;
  192. return 0;
  193. }
  194. bool
  195. CIFSCouldBeMFSymlink(const struct cifs_fattr *fattr)
  196. {
  197. if (!(fattr->cf_mode & S_IFREG))
  198. /* it's not a symlink */
  199. return false;
  200. if (fattr->cf_eof != CIFS_MF_SYMLINK_FILE_SIZE)
  201. /* it's not a symlink */
  202. return false;
  203. return true;
  204. }
  205. int
  206. CIFSCheckMFSymlink(struct cifs_fattr *fattr,
  207. const unsigned char *path,
  208. struct cifs_sb_info *cifs_sb, int xid)
  209. {
  210. int rc;
  211. int oplock = 0;
  212. __u16 netfid = 0;
  213. struct cifsTconInfo *pTcon = cifs_sb->tcon;
  214. u8 *buf;
  215. char *pbuf;
  216. unsigned int bytes_read = 0;
  217. int buf_type = CIFS_NO_BUFFER;
  218. unsigned int link_len = 0;
  219. FILE_ALL_INFO file_info;
  220. if (!CIFSCouldBeMFSymlink(fattr))
  221. /* it's not a symlink */
  222. return 0;
  223. rc = CIFSSMBOpen(xid, pTcon, path, FILE_OPEN, GENERIC_READ,
  224. CREATE_NOT_DIR, &netfid, &oplock, &file_info,
  225. cifs_sb->local_nls,
  226. cifs_sb->mnt_cifs_flags &
  227. CIFS_MOUNT_MAP_SPECIAL_CHR);
  228. if (rc != 0)
  229. return rc;
  230. if (file_info.EndOfFile != CIFS_MF_SYMLINK_FILE_SIZE) {
  231. CIFSSMBClose(xid, pTcon, netfid);
  232. /* it's not a symlink */
  233. return 0;
  234. }
  235. buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
  236. if (!buf)
  237. return -ENOMEM;
  238. pbuf = buf;
  239. rc = CIFSSMBRead(xid, pTcon, netfid,
  240. CIFS_MF_SYMLINK_FILE_SIZE /* length */,
  241. 0 /* offset */,
  242. &bytes_read, &pbuf, &buf_type);
  243. CIFSSMBClose(xid, pTcon, netfid);
  244. if (rc != 0) {
  245. kfree(buf);
  246. return rc;
  247. }
  248. rc = CIFSParseMFSymlink(buf, bytes_read, &link_len, NULL);
  249. kfree(buf);
  250. if (rc == -EINVAL)
  251. /* it's not a symlink */
  252. return 0;
  253. if (rc != 0)
  254. return rc;
  255. /* it is a symlink */
  256. fattr->cf_eof = link_len;
  257. fattr->cf_mode &= ~S_IFMT;
  258. fattr->cf_mode |= S_IFLNK | S_IRWXU | S_IRWXG | S_IRWXO;
  259. fattr->cf_dtype = DT_LNK;
  260. return 0;
  261. }
  262. int
  263. cifs_hardlink(struct dentry *old_file, struct inode *inode,
  264. struct dentry *direntry)
  265. {
  266. int rc = -EACCES;
  267. int xid;
  268. char *fromName = NULL;
  269. char *toName = NULL;
  270. struct cifs_sb_info *cifs_sb_target;
  271. struct cifsTconInfo *pTcon;
  272. struct cifsInodeInfo *cifsInode;
  273. xid = GetXid();
  274. cifs_sb_target = CIFS_SB(inode->i_sb);
  275. pTcon = cifs_sb_target->tcon;
  276. /* No need to check for cross device links since server will do that
  277. BB note DFS case in future though (when we may have to check) */
  278. fromName = build_path_from_dentry(old_file);
  279. toName = build_path_from_dentry(direntry);
  280. if ((fromName == NULL) || (toName == NULL)) {
  281. rc = -ENOMEM;
  282. goto cifs_hl_exit;
  283. }
  284. /* if (cifs_sb_target->tcon->ses->capabilities & CAP_UNIX)*/
  285. if (pTcon->unix_ext)
  286. rc = CIFSUnixCreateHardLink(xid, pTcon, fromName, toName,
  287. cifs_sb_target->local_nls,
  288. cifs_sb_target->mnt_cifs_flags &
  289. CIFS_MOUNT_MAP_SPECIAL_CHR);
  290. else {
  291. rc = CIFSCreateHardLink(xid, pTcon, fromName, toName,
  292. cifs_sb_target->local_nls,
  293. cifs_sb_target->mnt_cifs_flags &
  294. CIFS_MOUNT_MAP_SPECIAL_CHR);
  295. if ((rc == -EIO) || (rc == -EINVAL))
  296. rc = -EOPNOTSUPP;
  297. }
  298. d_drop(direntry); /* force new lookup from server of target */
  299. /* if source file is cached (oplocked) revalidate will not go to server
  300. until the file is closed or oplock broken so update nlinks locally */
  301. if (old_file->d_inode) {
  302. cifsInode = CIFS_I(old_file->d_inode);
  303. if (rc == 0) {
  304. old_file->d_inode->i_nlink++;
  305. /* BB should we make this contingent on superblock flag NOATIME? */
  306. /* old_file->d_inode->i_ctime = CURRENT_TIME;*/
  307. /* parent dir timestamps will update from srv
  308. within a second, would it really be worth it
  309. to set the parent dir cifs inode time to zero
  310. to force revalidate (faster) for it too? */
  311. }
  312. /* if not oplocked will force revalidate to get info
  313. on source file from srv */
  314. cifsInode->time = 0;
  315. /* Will update parent dir timestamps from srv within a second.
  316. Would it really be worth it to set the parent dir (cifs
  317. inode) time field to zero to force revalidate on parent
  318. directory faster ie
  319. CIFS_I(inode)->time = 0; */
  320. }
  321. cifs_hl_exit:
  322. kfree(fromName);
  323. kfree(toName);
  324. FreeXid(xid);
  325. return rc;
  326. }
  327. void *
  328. cifs_follow_link(struct dentry *direntry, struct nameidata *nd)
  329. {
  330. struct inode *inode = direntry->d_inode;
  331. int rc = -ENOMEM;
  332. int xid;
  333. char *full_path = NULL;
  334. char *target_path = NULL;
  335. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  336. struct cifsTconInfo *tcon = cifs_sb->tcon;
  337. xid = GetXid();
  338. /*
  339. * For now, we just handle symlinks with unix extensions enabled.
  340. * Eventually we should handle NTFS reparse points, and MacOS
  341. * symlink support. For instance...
  342. *
  343. * rc = CIFSSMBQueryReparseLinkInfo(...)
  344. *
  345. * For now, just return -EACCES when the server doesn't support posix
  346. * extensions. Note that we still allow querying symlinks when posix
  347. * extensions are manually disabled. We could disable these as well
  348. * but there doesn't seem to be any harm in allowing the client to
  349. * read them.
  350. */
  351. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
  352. && !(tcon->ses->capabilities & CAP_UNIX)) {
  353. rc = -EACCES;
  354. goto out;
  355. }
  356. full_path = build_path_from_dentry(direntry);
  357. if (!full_path)
  358. goto out;
  359. cFYI(1, "Full path: %s inode = 0x%p", full_path, inode);
  360. rc = -EACCES;
  361. /*
  362. * First try Minshall+French Symlinks, if configured
  363. * and fallback to UNIX Extensions Symlinks.
  364. */
  365. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
  366. rc = CIFSQueryMFSymLink(xid, tcon, full_path, &target_path,
  367. cifs_sb->local_nls,
  368. cifs_sb->mnt_cifs_flags &
  369. CIFS_MOUNT_MAP_SPECIAL_CHR);
  370. if ((rc != 0) && (tcon->ses->capabilities & CAP_UNIX))
  371. rc = CIFSSMBUnixQuerySymLink(xid, tcon, full_path, &target_path,
  372. cifs_sb->local_nls);
  373. kfree(full_path);
  374. out:
  375. if (rc != 0) {
  376. kfree(target_path);
  377. target_path = ERR_PTR(rc);
  378. }
  379. FreeXid(xid);
  380. nd_set_link(nd, target_path);
  381. return NULL;
  382. }
  383. int
  384. cifs_symlink(struct inode *inode, struct dentry *direntry, const char *symname)
  385. {
  386. int rc = -EOPNOTSUPP;
  387. int xid;
  388. struct cifs_sb_info *cifs_sb;
  389. struct cifsTconInfo *pTcon;
  390. char *full_path = NULL;
  391. struct inode *newinode = NULL;
  392. xid = GetXid();
  393. cifs_sb = CIFS_SB(inode->i_sb);
  394. pTcon = cifs_sb->tcon;
  395. full_path = build_path_from_dentry(direntry);
  396. if (full_path == NULL) {
  397. rc = -ENOMEM;
  398. FreeXid(xid);
  399. return rc;
  400. }
  401. cFYI(1, "Full path: %s", full_path);
  402. cFYI(1, "symname is %s", symname);
  403. /* BB what if DFS and this volume is on different share? BB */
  404. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
  405. rc = CIFSCreateMFSymLink(xid, pTcon, full_path, symname,
  406. cifs_sb->local_nls,
  407. cifs_sb->mnt_cifs_flags &
  408. CIFS_MOUNT_MAP_SPECIAL_CHR);
  409. else if (pTcon->unix_ext)
  410. rc = CIFSUnixCreateSymLink(xid, pTcon, full_path, symname,
  411. cifs_sb->local_nls);
  412. /* else
  413. rc = CIFSCreateReparseSymLink(xid, pTcon, fromName, toName,
  414. cifs_sb_target->local_nls); */
  415. if (rc == 0) {
  416. if (pTcon->unix_ext)
  417. rc = cifs_get_inode_info_unix(&newinode, full_path,
  418. inode->i_sb, xid);
  419. else
  420. rc = cifs_get_inode_info(&newinode, full_path, NULL,
  421. inode->i_sb, xid, NULL);
  422. if (rc != 0) {
  423. cFYI(1, "Create symlink ok, getinodeinfo fail rc = %d",
  424. rc);
  425. } else {
  426. if (pTcon->nocase)
  427. direntry->d_op = &cifs_ci_dentry_ops;
  428. else
  429. direntry->d_op = &cifs_dentry_ops;
  430. d_instantiate(direntry, newinode);
  431. }
  432. }
  433. kfree(full_path);
  434. FreeXid(xid);
  435. return rc;
  436. }
  437. void cifs_put_link(struct dentry *direntry, struct nameidata *nd, void *cookie)
  438. {
  439. char *p = nd_get_link(nd);
  440. if (!IS_ERR(p))
  441. kfree(p);
  442. }