link.c 14 KB

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