link.c 15 KB

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