link.c 14 KB

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