link.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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. #define CIFS_MF_SYMLINK_LEN_OFFSET (4+1)
  32. #define CIFS_MF_SYMLINK_MD5_OFFSET (CIFS_MF_SYMLINK_LEN_OFFSET+(4+1))
  33. #define CIFS_MF_SYMLINK_LINK_OFFSET (CIFS_MF_SYMLINK_MD5_OFFSET+(32+1))
  34. #define CIFS_MF_SYMLINK_LINK_MAXLEN (1024)
  35. #define CIFS_MF_SYMLINK_FILE_SIZE \
  36. (CIFS_MF_SYMLINK_LINK_OFFSET + CIFS_MF_SYMLINK_LINK_MAXLEN)
  37. #define CIFS_MF_SYMLINK_LEN_FORMAT "XSym\n%04u\n"
  38. #define CIFS_MF_SYMLINK_MD5_FORMAT \
  39. "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n"
  40. #define CIFS_MF_SYMLINK_MD5_ARGS(md5_hash) \
  41. md5_hash[0], md5_hash[1], md5_hash[2], md5_hash[3], \
  42. md5_hash[4], md5_hash[5], md5_hash[6], md5_hash[7], \
  43. md5_hash[8], md5_hash[9], md5_hash[10], md5_hash[11],\
  44. md5_hash[12], md5_hash[13], md5_hash[14], md5_hash[15]
  45. static int
  46. symlink_hash(unsigned int link_len, const char *link_str, u8 *md5_hash)
  47. {
  48. int rc;
  49. unsigned int size;
  50. struct crypto_shash *md5;
  51. struct sdesc *sdescmd5;
  52. md5 = crypto_alloc_shash("md5", 0, 0);
  53. if (IS_ERR(md5)) {
  54. cERROR(1, "%s: Crypto md5 allocation error %d\n", __func__, rc);
  55. return PTR_ERR(md5);
  56. }
  57. size = sizeof(struct shash_desc) + crypto_shash_descsize(md5);
  58. sdescmd5 = kmalloc(size, GFP_KERNEL);
  59. if (!sdescmd5) {
  60. rc = -ENOMEM;
  61. cERROR(1, "%s: Memory allocation failure\n", __func__);
  62. goto symlink_hash_err;
  63. }
  64. sdescmd5->shash.tfm = md5;
  65. sdescmd5->shash.flags = 0x0;
  66. rc = crypto_shash_init(&sdescmd5->shash);
  67. if (rc) {
  68. cERROR(1, "%s: Could not init md5 shash\n", __func__);
  69. goto symlink_hash_err;
  70. }
  71. crypto_shash_update(&sdescmd5->shash, link_str, link_len);
  72. rc = crypto_shash_final(&sdescmd5->shash, md5_hash);
  73. symlink_hash_err:
  74. crypto_free_shash(md5);
  75. kfree(sdescmd5);
  76. return rc;
  77. }
  78. static int
  79. CIFSParseMFSymlink(const u8 *buf,
  80. unsigned int buf_len,
  81. unsigned int *_link_len,
  82. char **_link_str)
  83. {
  84. int rc;
  85. unsigned int link_len;
  86. const char *md5_str1;
  87. const char *link_str;
  88. u8 md5_hash[16];
  89. char md5_str2[34];
  90. if (buf_len != CIFS_MF_SYMLINK_FILE_SIZE)
  91. return -EINVAL;
  92. md5_str1 = (const char *)&buf[CIFS_MF_SYMLINK_MD5_OFFSET];
  93. link_str = (const char *)&buf[CIFS_MF_SYMLINK_LINK_OFFSET];
  94. rc = sscanf(buf, CIFS_MF_SYMLINK_LEN_FORMAT, &link_len);
  95. if (rc != 1)
  96. return -EINVAL;
  97. rc = symlink_hash(link_len, link_str, md5_hash);
  98. if (rc) {
  99. cFYI(1, "%s: MD5 hash failure: %d\n", __func__, rc);
  100. return rc;
  101. }
  102. snprintf(md5_str2, sizeof(md5_str2),
  103. CIFS_MF_SYMLINK_MD5_FORMAT,
  104. CIFS_MF_SYMLINK_MD5_ARGS(md5_hash));
  105. if (strncmp(md5_str1, md5_str2, 17) != 0)
  106. return -EINVAL;
  107. if (_link_str) {
  108. *_link_str = kstrndup(link_str, link_len, GFP_KERNEL);
  109. if (!*_link_str)
  110. return -ENOMEM;
  111. }
  112. *_link_len = link_len;
  113. return 0;
  114. }
  115. static int
  116. CIFSFormatMFSymlink(u8 *buf, unsigned int buf_len, const char *link_str)
  117. {
  118. int rc;
  119. unsigned int link_len;
  120. unsigned int ofs;
  121. u8 md5_hash[16];
  122. if (buf_len != CIFS_MF_SYMLINK_FILE_SIZE)
  123. return -EINVAL;
  124. link_len = strlen(link_str);
  125. if (link_len > CIFS_MF_SYMLINK_LINK_MAXLEN)
  126. return -ENAMETOOLONG;
  127. rc = symlink_hash(link_len, link_str, md5_hash);
  128. if (rc) {
  129. cFYI(1, "%s: MD5 hash failure: %d\n", __func__, rc);
  130. return rc;
  131. }
  132. snprintf(buf, buf_len,
  133. CIFS_MF_SYMLINK_LEN_FORMAT CIFS_MF_SYMLINK_MD5_FORMAT,
  134. link_len,
  135. CIFS_MF_SYMLINK_MD5_ARGS(md5_hash));
  136. ofs = CIFS_MF_SYMLINK_LINK_OFFSET;
  137. memcpy(buf + ofs, link_str, link_len);
  138. ofs += link_len;
  139. if (ofs < CIFS_MF_SYMLINK_FILE_SIZE) {
  140. buf[ofs] = '\n';
  141. ofs++;
  142. }
  143. while (ofs < CIFS_MF_SYMLINK_FILE_SIZE) {
  144. buf[ofs] = ' ';
  145. ofs++;
  146. }
  147. return 0;
  148. }
  149. static int
  150. CIFSCreateMFSymLink(const int xid, struct cifsTconInfo *tcon,
  151. const char *fromName, const char *toName,
  152. const struct nls_table *nls_codepage, int remap)
  153. {
  154. int rc;
  155. int oplock = 0;
  156. __u16 netfid = 0;
  157. u8 *buf;
  158. unsigned int bytes_written = 0;
  159. buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
  160. if (!buf)
  161. return -ENOMEM;
  162. rc = CIFSFormatMFSymlink(buf, CIFS_MF_SYMLINK_FILE_SIZE, toName);
  163. if (rc != 0) {
  164. kfree(buf);
  165. return rc;
  166. }
  167. rc = CIFSSMBOpen(xid, tcon, fromName, FILE_CREATE, GENERIC_WRITE,
  168. CREATE_NOT_DIR, &netfid, &oplock, NULL,
  169. nls_codepage, remap);
  170. if (rc != 0) {
  171. kfree(buf);
  172. return rc;
  173. }
  174. rc = CIFSSMBWrite(xid, tcon, netfid,
  175. CIFS_MF_SYMLINK_FILE_SIZE /* length */,
  176. 0 /* offset */,
  177. &bytes_written, buf, NULL, 0);
  178. CIFSSMBClose(xid, tcon, netfid);
  179. kfree(buf);
  180. if (rc != 0)
  181. return rc;
  182. if (bytes_written != CIFS_MF_SYMLINK_FILE_SIZE)
  183. return -EIO;
  184. return 0;
  185. }
  186. static int
  187. CIFSQueryMFSymLink(const int xid, struct cifsTconInfo *tcon,
  188. const unsigned char *searchName, char **symlinkinfo,
  189. const struct nls_table *nls_codepage, int remap)
  190. {
  191. int rc;
  192. int oplock = 0;
  193. __u16 netfid = 0;
  194. u8 *buf;
  195. char *pbuf;
  196. unsigned int bytes_read = 0;
  197. int buf_type = CIFS_NO_BUFFER;
  198. unsigned int link_len = 0;
  199. FILE_ALL_INFO file_info;
  200. rc = CIFSSMBOpen(xid, tcon, searchName, FILE_OPEN, GENERIC_READ,
  201. CREATE_NOT_DIR, &netfid, &oplock, &file_info,
  202. nls_codepage, remap);
  203. if (rc != 0)
  204. return rc;
  205. if (file_info.EndOfFile != CIFS_MF_SYMLINK_FILE_SIZE) {
  206. CIFSSMBClose(xid, tcon, netfid);
  207. /* it's not a symlink */
  208. return -EINVAL;
  209. }
  210. buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
  211. if (!buf)
  212. return -ENOMEM;
  213. pbuf = buf;
  214. rc = CIFSSMBRead(xid, tcon, netfid,
  215. CIFS_MF_SYMLINK_FILE_SIZE /* length */,
  216. 0 /* offset */,
  217. &bytes_read, &pbuf, &buf_type);
  218. CIFSSMBClose(xid, tcon, netfid);
  219. if (rc != 0) {
  220. kfree(buf);
  221. return rc;
  222. }
  223. rc = CIFSParseMFSymlink(buf, bytes_read, &link_len, symlinkinfo);
  224. kfree(buf);
  225. if (rc != 0)
  226. return rc;
  227. return 0;
  228. }
  229. bool
  230. CIFSCouldBeMFSymlink(const struct cifs_fattr *fattr)
  231. {
  232. if (!(fattr->cf_mode & S_IFREG))
  233. /* it's not a symlink */
  234. return false;
  235. if (fattr->cf_eof != CIFS_MF_SYMLINK_FILE_SIZE)
  236. /* it's not a symlink */
  237. return false;
  238. return true;
  239. }
  240. int
  241. CIFSCheckMFSymlink(struct cifs_fattr *fattr,
  242. const unsigned char *path,
  243. struct cifs_sb_info *cifs_sb, int xid)
  244. {
  245. int rc;
  246. int oplock = 0;
  247. __u16 netfid = 0;
  248. struct tcon_link *tlink;
  249. struct cifsTconInfo *pTcon;
  250. u8 *buf;
  251. char *pbuf;
  252. unsigned int bytes_read = 0;
  253. int buf_type = CIFS_NO_BUFFER;
  254. unsigned int link_len = 0;
  255. FILE_ALL_INFO file_info;
  256. if (!CIFSCouldBeMFSymlink(fattr))
  257. /* it's not a symlink */
  258. return 0;
  259. tlink = cifs_sb_tlink(cifs_sb);
  260. if (IS_ERR(tlink))
  261. return PTR_ERR(tlink);
  262. pTcon = tlink_tcon(tlink);
  263. rc = CIFSSMBOpen(xid, pTcon, path, FILE_OPEN, GENERIC_READ,
  264. CREATE_NOT_DIR, &netfid, &oplock, &file_info,
  265. cifs_sb->local_nls,
  266. cifs_sb->mnt_cifs_flags &
  267. CIFS_MOUNT_MAP_SPECIAL_CHR);
  268. if (rc != 0)
  269. goto out;
  270. if (file_info.EndOfFile != CIFS_MF_SYMLINK_FILE_SIZE) {
  271. CIFSSMBClose(xid, pTcon, netfid);
  272. /* it's not a symlink */
  273. goto out;
  274. }
  275. buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
  276. if (!buf) {
  277. rc = -ENOMEM;
  278. goto out;
  279. }
  280. pbuf = buf;
  281. rc = CIFSSMBRead(xid, pTcon, netfid,
  282. CIFS_MF_SYMLINK_FILE_SIZE /* length */,
  283. 0 /* offset */,
  284. &bytes_read, &pbuf, &buf_type);
  285. CIFSSMBClose(xid, pTcon, netfid);
  286. if (rc != 0) {
  287. kfree(buf);
  288. goto out;
  289. }
  290. rc = CIFSParseMFSymlink(buf, bytes_read, &link_len, NULL);
  291. kfree(buf);
  292. if (rc == -EINVAL) {
  293. /* it's not a symlink */
  294. rc = 0;
  295. goto out;
  296. }
  297. if (rc != 0)
  298. goto out;
  299. /* it is a symlink */
  300. fattr->cf_eof = link_len;
  301. fattr->cf_mode &= ~S_IFMT;
  302. fattr->cf_mode |= S_IFLNK | S_IRWXU | S_IRWXG | S_IRWXO;
  303. fattr->cf_dtype = DT_LNK;
  304. out:
  305. cifs_put_tlink(tlink);
  306. return rc;
  307. }
  308. int
  309. cifs_hardlink(struct dentry *old_file, struct inode *inode,
  310. struct dentry *direntry)
  311. {
  312. int rc = -EACCES;
  313. int xid;
  314. char *fromName = NULL;
  315. char *toName = NULL;
  316. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  317. struct tcon_link *tlink;
  318. struct cifsTconInfo *pTcon;
  319. struct cifsInodeInfo *cifsInode;
  320. tlink = cifs_sb_tlink(cifs_sb);
  321. if (IS_ERR(tlink))
  322. return PTR_ERR(tlink);
  323. pTcon = tlink_tcon(tlink);
  324. xid = GetXid();
  325. fromName = build_path_from_dentry(old_file);
  326. toName = build_path_from_dentry(direntry);
  327. if ((fromName == NULL) || (toName == NULL)) {
  328. rc = -ENOMEM;
  329. goto cifs_hl_exit;
  330. }
  331. if (pTcon->unix_ext)
  332. rc = CIFSUnixCreateHardLink(xid, pTcon, fromName, toName,
  333. cifs_sb->local_nls,
  334. cifs_sb->mnt_cifs_flags &
  335. CIFS_MOUNT_MAP_SPECIAL_CHR);
  336. else {
  337. rc = CIFSCreateHardLink(xid, pTcon, fromName, toName,
  338. cifs_sb->local_nls,
  339. cifs_sb->mnt_cifs_flags &
  340. CIFS_MOUNT_MAP_SPECIAL_CHR);
  341. if ((rc == -EIO) || (rc == -EINVAL))
  342. rc = -EOPNOTSUPP;
  343. }
  344. d_drop(direntry); /* force new lookup from server of target */
  345. /* if source file is cached (oplocked) revalidate will not go to server
  346. until the file is closed or oplock broken so update nlinks locally */
  347. if (old_file->d_inode) {
  348. cifsInode = CIFS_I(old_file->d_inode);
  349. if (rc == 0) {
  350. old_file->d_inode->i_nlink++;
  351. /* BB should we make this contingent on superblock flag NOATIME? */
  352. /* old_file->d_inode->i_ctime = CURRENT_TIME;*/
  353. /* parent dir timestamps will update from srv
  354. within a second, would it really be worth it
  355. to set the parent dir cifs inode time to zero
  356. to force revalidate (faster) for it too? */
  357. }
  358. /* if not oplocked will force revalidate to get info
  359. on source file from srv */
  360. cifsInode->time = 0;
  361. /* Will update parent dir timestamps from srv within a second.
  362. Would it really be worth it to set the parent dir (cifs
  363. inode) time field to zero to force revalidate on parent
  364. directory faster ie
  365. CIFS_I(inode)->time = 0; */
  366. }
  367. cifs_hl_exit:
  368. kfree(fromName);
  369. kfree(toName);
  370. FreeXid(xid);
  371. cifs_put_tlink(tlink);
  372. return rc;
  373. }
  374. void *
  375. cifs_follow_link(struct dentry *direntry, struct nameidata *nd)
  376. {
  377. struct inode *inode = direntry->d_inode;
  378. int rc = -ENOMEM;
  379. int xid;
  380. char *full_path = NULL;
  381. char *target_path = NULL;
  382. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  383. struct tcon_link *tlink = NULL;
  384. struct cifsTconInfo *tcon;
  385. xid = GetXid();
  386. tlink = cifs_sb_tlink(cifs_sb);
  387. if (IS_ERR(tlink)) {
  388. rc = PTR_ERR(tlink);
  389. tlink = NULL;
  390. goto out;
  391. }
  392. tcon = tlink_tcon(tlink);
  393. /*
  394. * For now, we just handle symlinks with unix extensions enabled.
  395. * Eventually we should handle NTFS reparse points, and MacOS
  396. * symlink support. For instance...
  397. *
  398. * rc = CIFSSMBQueryReparseLinkInfo(...)
  399. *
  400. * For now, just return -EACCES when the server doesn't support posix
  401. * extensions. Note that we still allow querying symlinks when posix
  402. * extensions are manually disabled. We could disable these as well
  403. * but there doesn't seem to be any harm in allowing the client to
  404. * read them.
  405. */
  406. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
  407. && !(tcon->ses->capabilities & CAP_UNIX)) {
  408. rc = -EACCES;
  409. goto out;
  410. }
  411. full_path = build_path_from_dentry(direntry);
  412. if (!full_path)
  413. goto out;
  414. cFYI(1, "Full path: %s inode = 0x%p", full_path, inode);
  415. rc = -EACCES;
  416. /*
  417. * First try Minshall+French Symlinks, if configured
  418. * and fallback to UNIX Extensions Symlinks.
  419. */
  420. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
  421. rc = CIFSQueryMFSymLink(xid, tcon, full_path, &target_path,
  422. cifs_sb->local_nls,
  423. cifs_sb->mnt_cifs_flags &
  424. CIFS_MOUNT_MAP_SPECIAL_CHR);
  425. if ((rc != 0) && (tcon->ses->capabilities & CAP_UNIX))
  426. rc = CIFSSMBUnixQuerySymLink(xid, tcon, full_path, &target_path,
  427. cifs_sb->local_nls);
  428. kfree(full_path);
  429. out:
  430. if (rc != 0) {
  431. kfree(target_path);
  432. target_path = ERR_PTR(rc);
  433. }
  434. FreeXid(xid);
  435. if (tlink)
  436. cifs_put_tlink(tlink);
  437. nd_set_link(nd, target_path);
  438. return NULL;
  439. }
  440. int
  441. cifs_symlink(struct inode *inode, struct dentry *direntry, const char *symname)
  442. {
  443. int rc = -EOPNOTSUPP;
  444. int xid;
  445. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  446. struct tcon_link *tlink;
  447. struct cifsTconInfo *pTcon;
  448. char *full_path = NULL;
  449. struct inode *newinode = NULL;
  450. xid = GetXid();
  451. tlink = cifs_sb_tlink(cifs_sb);
  452. if (IS_ERR(tlink)) {
  453. rc = PTR_ERR(tlink);
  454. goto symlink_exit;
  455. }
  456. pTcon = tlink_tcon(tlink);
  457. full_path = build_path_from_dentry(direntry);
  458. if (full_path == NULL) {
  459. rc = -ENOMEM;
  460. goto symlink_exit;
  461. }
  462. cFYI(1, "Full path: %s", full_path);
  463. cFYI(1, "symname is %s", symname);
  464. /* BB what if DFS and this volume is on different share? BB */
  465. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
  466. rc = CIFSCreateMFSymLink(xid, pTcon, full_path, symname,
  467. cifs_sb->local_nls,
  468. cifs_sb->mnt_cifs_flags &
  469. CIFS_MOUNT_MAP_SPECIAL_CHR);
  470. else if (pTcon->unix_ext)
  471. rc = CIFSUnixCreateSymLink(xid, pTcon, full_path, symname,
  472. cifs_sb->local_nls);
  473. /* else
  474. rc = CIFSCreateReparseSymLink(xid, pTcon, fromName, toName,
  475. cifs_sb_target->local_nls); */
  476. if (rc == 0) {
  477. if (pTcon->unix_ext)
  478. rc = cifs_get_inode_info_unix(&newinode, full_path,
  479. inode->i_sb, xid);
  480. else
  481. rc = cifs_get_inode_info(&newinode, full_path, NULL,
  482. inode->i_sb, xid, NULL);
  483. if (rc != 0) {
  484. cFYI(1, "Create symlink ok, getinodeinfo fail rc = %d",
  485. rc);
  486. } else {
  487. d_instantiate(direntry, newinode);
  488. }
  489. }
  490. symlink_exit:
  491. kfree(full_path);
  492. cifs_put_tlink(tlink);
  493. FreeXid(xid);
  494. return rc;
  495. }
  496. void cifs_put_link(struct dentry *direntry, struct nameidata *nd, void *cookie)
  497. {
  498. char *p = nd_get_link(nd);
  499. if (!IS_ERR(p))
  500. kfree(p);
  501. }