link.c 14 KB

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