link.c 14 KB

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