link.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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 tcon_link *tlink;
  214. struct cifsTconInfo *pTcon;
  215. u8 *buf;
  216. char *pbuf;
  217. unsigned int bytes_read = 0;
  218. int buf_type = CIFS_NO_BUFFER;
  219. unsigned int link_len = 0;
  220. FILE_ALL_INFO file_info;
  221. if (!CIFSCouldBeMFSymlink(fattr))
  222. /* it's not a symlink */
  223. return 0;
  224. tlink = cifs_sb_tlink(cifs_sb);
  225. if (IS_ERR(tlink))
  226. return PTR_ERR(tlink);
  227. pTcon = tlink_tcon(tlink);
  228. rc = CIFSSMBOpen(xid, pTcon, path, FILE_OPEN, GENERIC_READ,
  229. CREATE_NOT_DIR, &netfid, &oplock, &file_info,
  230. cifs_sb->local_nls,
  231. cifs_sb->mnt_cifs_flags &
  232. CIFS_MOUNT_MAP_SPECIAL_CHR);
  233. if (rc != 0)
  234. goto out;
  235. if (file_info.EndOfFile != CIFS_MF_SYMLINK_FILE_SIZE) {
  236. CIFSSMBClose(xid, pTcon, netfid);
  237. /* it's not a symlink */
  238. goto out;
  239. }
  240. buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
  241. if (!buf) {
  242. rc = -ENOMEM;
  243. goto out;
  244. }
  245. pbuf = buf;
  246. rc = CIFSSMBRead(xid, pTcon, netfid,
  247. CIFS_MF_SYMLINK_FILE_SIZE /* length */,
  248. 0 /* offset */,
  249. &bytes_read, &pbuf, &buf_type);
  250. CIFSSMBClose(xid, pTcon, netfid);
  251. if (rc != 0) {
  252. kfree(buf);
  253. goto out;
  254. }
  255. rc = CIFSParseMFSymlink(buf, bytes_read, &link_len, NULL);
  256. kfree(buf);
  257. if (rc == -EINVAL) {
  258. /* it's not a symlink */
  259. rc = 0;
  260. goto out;
  261. }
  262. if (rc != 0)
  263. goto out;
  264. /* it is a symlink */
  265. fattr->cf_eof = link_len;
  266. fattr->cf_mode &= ~S_IFMT;
  267. fattr->cf_mode |= S_IFLNK | S_IRWXU | S_IRWXG | S_IRWXO;
  268. fattr->cf_dtype = DT_LNK;
  269. out:
  270. cifs_put_tlink(tlink);
  271. return rc;
  272. }
  273. int
  274. cifs_hardlink(struct dentry *old_file, struct inode *inode,
  275. struct dentry *direntry)
  276. {
  277. int rc = -EACCES;
  278. int xid;
  279. char *fromName = NULL;
  280. char *toName = NULL;
  281. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  282. struct tcon_link *tlink;
  283. struct cifsTconInfo *pTcon;
  284. struct cifsInodeInfo *cifsInode;
  285. tlink = cifs_sb_tlink(cifs_sb);
  286. if (IS_ERR(tlink))
  287. return PTR_ERR(tlink);
  288. pTcon = tlink_tcon(tlink);
  289. xid = GetXid();
  290. fromName = build_path_from_dentry(old_file);
  291. toName = build_path_from_dentry(direntry);
  292. if ((fromName == NULL) || (toName == NULL)) {
  293. rc = -ENOMEM;
  294. goto cifs_hl_exit;
  295. }
  296. if (pTcon->unix_ext)
  297. rc = CIFSUnixCreateHardLink(xid, pTcon, fromName, toName,
  298. cifs_sb->local_nls,
  299. cifs_sb->mnt_cifs_flags &
  300. CIFS_MOUNT_MAP_SPECIAL_CHR);
  301. else {
  302. rc = CIFSCreateHardLink(xid, pTcon, fromName, toName,
  303. cifs_sb->local_nls,
  304. cifs_sb->mnt_cifs_flags &
  305. CIFS_MOUNT_MAP_SPECIAL_CHR);
  306. if ((rc == -EIO) || (rc == -EINVAL))
  307. rc = -EOPNOTSUPP;
  308. }
  309. d_drop(direntry); /* force new lookup from server of target */
  310. /* if source file is cached (oplocked) revalidate will not go to server
  311. until the file is closed or oplock broken so update nlinks locally */
  312. if (old_file->d_inode) {
  313. cifsInode = CIFS_I(old_file->d_inode);
  314. if (rc == 0) {
  315. old_file->d_inode->i_nlink++;
  316. /* BB should we make this contingent on superblock flag NOATIME? */
  317. /* old_file->d_inode->i_ctime = CURRENT_TIME;*/
  318. /* parent dir timestamps will update from srv
  319. within a second, would it really be worth it
  320. to set the parent dir cifs inode time to zero
  321. to force revalidate (faster) for it too? */
  322. }
  323. /* if not oplocked will force revalidate to get info
  324. on source file from srv */
  325. cifsInode->time = 0;
  326. /* Will update parent dir timestamps from srv within a second.
  327. Would it really be worth it to set the parent dir (cifs
  328. inode) time field to zero to force revalidate on parent
  329. directory faster ie
  330. CIFS_I(inode)->time = 0; */
  331. }
  332. cifs_hl_exit:
  333. kfree(fromName);
  334. kfree(toName);
  335. FreeXid(xid);
  336. cifs_put_tlink(tlink);
  337. return rc;
  338. }
  339. void *
  340. cifs_follow_link(struct dentry *direntry, struct nameidata *nd)
  341. {
  342. struct inode *inode = direntry->d_inode;
  343. int rc = -ENOMEM;
  344. int xid;
  345. char *full_path = NULL;
  346. char *target_path = NULL;
  347. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  348. struct tcon_link *tlink = NULL;
  349. struct cifsTconInfo *tcon;
  350. xid = GetXid();
  351. tlink = cifs_sb_tlink(cifs_sb);
  352. if (IS_ERR(tlink)) {
  353. rc = PTR_ERR(tlink);
  354. tlink = NULL;
  355. goto out;
  356. }
  357. tcon = tlink_tcon(tlink);
  358. /*
  359. * For now, we just handle symlinks with unix extensions enabled.
  360. * Eventually we should handle NTFS reparse points, and MacOS
  361. * symlink support. For instance...
  362. *
  363. * rc = CIFSSMBQueryReparseLinkInfo(...)
  364. *
  365. * For now, just return -EACCES when the server doesn't support posix
  366. * extensions. Note that we still allow querying symlinks when posix
  367. * extensions are manually disabled. We could disable these as well
  368. * but there doesn't seem to be any harm in allowing the client to
  369. * read them.
  370. */
  371. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
  372. && !(tcon->ses->capabilities & CAP_UNIX)) {
  373. rc = -EACCES;
  374. goto out;
  375. }
  376. full_path = build_path_from_dentry(direntry);
  377. if (!full_path)
  378. goto out;
  379. cFYI(1, "Full path: %s inode = 0x%p", full_path, inode);
  380. rc = -EACCES;
  381. /*
  382. * First try Minshall+French Symlinks, if configured
  383. * and fallback to UNIX Extensions Symlinks.
  384. */
  385. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
  386. rc = CIFSQueryMFSymLink(xid, tcon, full_path, &target_path,
  387. cifs_sb->local_nls,
  388. cifs_sb->mnt_cifs_flags &
  389. CIFS_MOUNT_MAP_SPECIAL_CHR);
  390. if ((rc != 0) && (tcon->ses->capabilities & CAP_UNIX))
  391. rc = CIFSSMBUnixQuerySymLink(xid, tcon, full_path, &target_path,
  392. cifs_sb->local_nls);
  393. kfree(full_path);
  394. out:
  395. if (rc != 0) {
  396. kfree(target_path);
  397. target_path = ERR_PTR(rc);
  398. }
  399. FreeXid(xid);
  400. if (tlink)
  401. cifs_put_tlink(tlink);
  402. nd_set_link(nd, target_path);
  403. return NULL;
  404. }
  405. int
  406. cifs_symlink(struct inode *inode, struct dentry *direntry, const char *symname)
  407. {
  408. int rc = -EOPNOTSUPP;
  409. int xid;
  410. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  411. struct tcon_link *tlink;
  412. struct cifsTconInfo *pTcon;
  413. char *full_path = NULL;
  414. struct inode *newinode = NULL;
  415. xid = GetXid();
  416. tlink = cifs_sb_tlink(cifs_sb);
  417. if (IS_ERR(tlink)) {
  418. rc = PTR_ERR(tlink);
  419. goto symlink_exit;
  420. }
  421. pTcon = tlink_tcon(tlink);
  422. full_path = build_path_from_dentry(direntry);
  423. if (full_path == NULL) {
  424. rc = -ENOMEM;
  425. goto symlink_exit;
  426. }
  427. cFYI(1, "Full path: %s", full_path);
  428. cFYI(1, "symname is %s", symname);
  429. /* BB what if DFS and this volume is on different share? BB */
  430. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
  431. rc = CIFSCreateMFSymLink(xid, pTcon, full_path, symname,
  432. cifs_sb->local_nls,
  433. cifs_sb->mnt_cifs_flags &
  434. CIFS_MOUNT_MAP_SPECIAL_CHR);
  435. else if (pTcon->unix_ext)
  436. rc = CIFSUnixCreateSymLink(xid, pTcon, full_path, symname,
  437. cifs_sb->local_nls);
  438. /* else
  439. rc = CIFSCreateReparseSymLink(xid, pTcon, fromName, toName,
  440. cifs_sb_target->local_nls); */
  441. if (rc == 0) {
  442. if (pTcon->unix_ext)
  443. rc = cifs_get_inode_info_unix(&newinode, full_path,
  444. inode->i_sb, xid);
  445. else
  446. rc = cifs_get_inode_info(&newinode, full_path, NULL,
  447. inode->i_sb, xid, NULL);
  448. if (rc != 0) {
  449. cFYI(1, "Create symlink ok, getinodeinfo fail rc = %d",
  450. rc);
  451. } else {
  452. if (pTcon->nocase)
  453. d_set_d_op(direntry, &cifs_ci_dentry_ops);
  454. else
  455. d_set_d_op(direntry, &cifs_dentry_ops);
  456. d_instantiate(direntry, newinode);
  457. }
  458. }
  459. symlink_exit:
  460. kfree(full_path);
  461. cifs_put_tlink(tlink);
  462. FreeXid(xid);
  463. return rc;
  464. }
  465. void cifs_put_link(struct dentry *direntry, struct nameidata *nd, void *cookie)
  466. {
  467. char *p = nd_get_link(nd);
  468. if (!IS_ERR(p))
  469. kfree(p);
  470. }