smb2inode.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. /*
  2. * fs/cifs/smb2inode.c
  3. *
  4. * Copyright (C) International Business Machines Corp., 2002, 2011
  5. * Etersoft, 2012
  6. * Author(s): Pavel Shilovsky (pshilovsky@samba.org),
  7. * Steve French (sfrench@us.ibm.com)
  8. *
  9. * This library is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as published
  11. * by the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  17. * the GNU Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <linux/fs.h>
  24. #include <linux/stat.h>
  25. #include <linux/slab.h>
  26. #include <linux/pagemap.h>
  27. #include <asm/div64.h>
  28. #include "cifsfs.h"
  29. #include "cifspdu.h"
  30. #include "cifsglob.h"
  31. #include "cifsproto.h"
  32. #include "cifs_debug.h"
  33. #include "cifs_fs_sb.h"
  34. #include "cifs_unicode.h"
  35. #include "fscache.h"
  36. #include "smb2glob.h"
  37. #include "smb2pdu.h"
  38. #include "smb2proto.h"
  39. static void
  40. free_set_inf_compound(struct smb_rqst *rqst)
  41. {
  42. if (rqst[1].rq_iov)
  43. SMB2_set_info_free(&rqst[1]);
  44. if (rqst[2].rq_iov)
  45. SMB2_close_free(&rqst[2]);
  46. }
  47. struct cop_vars {
  48. struct cifs_open_parms oparms;
  49. struct kvec rsp_iov[3];
  50. struct smb_rqst rqst[3];
  51. struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
  52. struct kvec qi_iov[1];
  53. struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
  54. struct kvec close_iov[1];
  55. struct smb2_file_rename_info rename_info;
  56. struct smb2_file_link_info link_info;
  57. };
  58. static int
  59. smb2_compound_op(const unsigned int xid, struct cifs_tcon *tcon,
  60. struct cifs_sb_info *cifs_sb, const char *full_path,
  61. __u32 desired_access, __u32 create_disposition,
  62. __u32 create_options, umode_t mode, void *ptr, int command,
  63. struct cifsFileInfo *cfile)
  64. {
  65. struct cop_vars *vars = NULL;
  66. struct kvec *rsp_iov;
  67. struct smb_rqst *rqst;
  68. int rc;
  69. __le16 *utf16_path = NULL;
  70. __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  71. struct cifs_fid fid;
  72. struct cifs_ses *ses = tcon->ses;
  73. struct TCP_Server_Info *server;
  74. int num_rqst = 0;
  75. int resp_buftype[3];
  76. struct smb2_query_info_rsp *qi_rsp = NULL;
  77. int flags = 0;
  78. __u8 delete_pending[8] = {1, 0, 0, 0, 0, 0, 0, 0};
  79. unsigned int size[2];
  80. void *data[2];
  81. int len;
  82. vars = kzalloc(sizeof(*vars), GFP_ATOMIC);
  83. if (vars == NULL)
  84. return -ENOMEM;
  85. rqst = &vars->rqst[0];
  86. rsp_iov = &vars->rsp_iov[0];
  87. server = cifs_pick_channel(ses);
  88. if (smb3_encryption_required(tcon))
  89. flags |= CIFS_TRANSFORM_REQ;
  90. resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
  91. /* We already have a handle so we can skip the open */
  92. if (cfile)
  93. goto after_open;
  94. /* Open */
  95. utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
  96. if (!utf16_path) {
  97. rc = -ENOMEM;
  98. goto finished;
  99. }
  100. vars->oparms.tcon = tcon;
  101. vars->oparms.desired_access = desired_access;
  102. vars->oparms.disposition = create_disposition;
  103. vars->oparms.create_options = cifs_create_options(cifs_sb, create_options);
  104. vars->oparms.fid = &fid;
  105. vars->oparms.reconnect = false;
  106. vars->oparms.mode = mode;
  107. vars->oparms.cifs_sb = cifs_sb;
  108. rqst[num_rqst].rq_iov = &vars->open_iov[0];
  109. rqst[num_rqst].rq_nvec = SMB2_CREATE_IOV_SIZE;
  110. rc = SMB2_open_init(tcon, server,
  111. &rqst[num_rqst], &oplock, &vars->oparms,
  112. utf16_path);
  113. kfree(utf16_path);
  114. if (rc)
  115. goto finished;
  116. smb2_set_next_command(tcon, &rqst[num_rqst]);
  117. after_open:
  118. num_rqst++;
  119. rc = 0;
  120. /* Operation */
  121. switch (command) {
  122. case SMB2_OP_QUERY_INFO:
  123. rqst[num_rqst].rq_iov = &vars->qi_iov[0];
  124. rqst[num_rqst].rq_nvec = 1;
  125. if (cfile)
  126. rc = SMB2_query_info_init(tcon, server,
  127. &rqst[num_rqst],
  128. cfile->fid.persistent_fid,
  129. cfile->fid.volatile_fid,
  130. FILE_ALL_INFORMATION,
  131. SMB2_O_INFO_FILE, 0,
  132. sizeof(struct smb2_file_all_info) +
  133. PATH_MAX * 2, 0, NULL);
  134. else {
  135. rc = SMB2_query_info_init(tcon, server,
  136. &rqst[num_rqst],
  137. COMPOUND_FID,
  138. COMPOUND_FID,
  139. FILE_ALL_INFORMATION,
  140. SMB2_O_INFO_FILE, 0,
  141. sizeof(struct smb2_file_all_info) +
  142. PATH_MAX * 2, 0, NULL);
  143. if (!rc) {
  144. smb2_set_next_command(tcon, &rqst[num_rqst]);
  145. smb2_set_related(&rqst[num_rqst]);
  146. }
  147. }
  148. if (rc)
  149. goto finished;
  150. num_rqst++;
  151. trace_smb3_query_info_compound_enter(xid, ses->Suid, tcon->tid,
  152. full_path);
  153. break;
  154. case SMB2_OP_POSIX_QUERY_INFO:
  155. rqst[num_rqst].rq_iov = &vars->qi_iov[0];
  156. rqst[num_rqst].rq_nvec = 1;
  157. if (cfile)
  158. rc = SMB2_query_info_init(tcon, server,
  159. &rqst[num_rqst],
  160. cfile->fid.persistent_fid,
  161. cfile->fid.volatile_fid,
  162. SMB_FIND_FILE_POSIX_INFO,
  163. SMB2_O_INFO_FILE, 0,
  164. /* TBD: fix following to allow for longer SIDs */
  165. sizeof(struct smb311_posix_qinfo *) + (PATH_MAX * 2) +
  166. (sizeof(struct cifs_sid) * 2), 0, NULL);
  167. else {
  168. rc = SMB2_query_info_init(tcon, server,
  169. &rqst[num_rqst],
  170. COMPOUND_FID,
  171. COMPOUND_FID,
  172. SMB_FIND_FILE_POSIX_INFO,
  173. SMB2_O_INFO_FILE, 0,
  174. sizeof(struct smb311_posix_qinfo *) + (PATH_MAX * 2) +
  175. (sizeof(struct cifs_sid) * 2), 0, NULL);
  176. if (!rc) {
  177. smb2_set_next_command(tcon, &rqst[num_rqst]);
  178. smb2_set_related(&rqst[num_rqst]);
  179. }
  180. }
  181. if (rc)
  182. goto finished;
  183. num_rqst++;
  184. trace_smb3_posix_query_info_compound_enter(xid, ses->Suid, tcon->tid, full_path);
  185. break;
  186. case SMB2_OP_DELETE:
  187. trace_smb3_delete_enter(xid, ses->Suid, tcon->tid, full_path);
  188. break;
  189. case SMB2_OP_MKDIR:
  190. /*
  191. * Directories are created through parameters in the
  192. * SMB2_open() call.
  193. */
  194. trace_smb3_mkdir_enter(xid, ses->Suid, tcon->tid, full_path);
  195. break;
  196. case SMB2_OP_RMDIR:
  197. rqst[num_rqst].rq_iov = &vars->si_iov[0];
  198. rqst[num_rqst].rq_nvec = 1;
  199. size[0] = 1; /* sizeof __u8 See MS-FSCC section 2.4.11 */
  200. data[0] = &delete_pending[0];
  201. rc = SMB2_set_info_init(tcon, server,
  202. &rqst[num_rqst], COMPOUND_FID,
  203. COMPOUND_FID, current->tgid,
  204. FILE_DISPOSITION_INFORMATION,
  205. SMB2_O_INFO_FILE, 0, data, size);
  206. if (rc)
  207. goto finished;
  208. smb2_set_next_command(tcon, &rqst[num_rqst]);
  209. smb2_set_related(&rqst[num_rqst++]);
  210. trace_smb3_rmdir_enter(xid, ses->Suid, tcon->tid, full_path);
  211. break;
  212. case SMB2_OP_SET_EOF:
  213. rqst[num_rqst].rq_iov = &vars->si_iov[0];
  214. rqst[num_rqst].rq_nvec = 1;
  215. size[0] = 8; /* sizeof __le64 */
  216. data[0] = ptr;
  217. rc = SMB2_set_info_init(tcon, server,
  218. &rqst[num_rqst], COMPOUND_FID,
  219. COMPOUND_FID, current->tgid,
  220. FILE_END_OF_FILE_INFORMATION,
  221. SMB2_O_INFO_FILE, 0, data, size);
  222. if (rc)
  223. goto finished;
  224. smb2_set_next_command(tcon, &rqst[num_rqst]);
  225. smb2_set_related(&rqst[num_rqst++]);
  226. trace_smb3_set_eof_enter(xid, ses->Suid, tcon->tid, full_path);
  227. break;
  228. case SMB2_OP_SET_INFO:
  229. rqst[num_rqst].rq_iov = &vars->si_iov[0];
  230. rqst[num_rqst].rq_nvec = 1;
  231. size[0] = sizeof(FILE_BASIC_INFO);
  232. data[0] = ptr;
  233. if (cfile)
  234. rc = SMB2_set_info_init(tcon, server,
  235. &rqst[num_rqst],
  236. cfile->fid.persistent_fid,
  237. cfile->fid.volatile_fid, current->tgid,
  238. FILE_BASIC_INFORMATION,
  239. SMB2_O_INFO_FILE, 0, data, size);
  240. else {
  241. rc = SMB2_set_info_init(tcon, server,
  242. &rqst[num_rqst],
  243. COMPOUND_FID,
  244. COMPOUND_FID, current->tgid,
  245. FILE_BASIC_INFORMATION,
  246. SMB2_O_INFO_FILE, 0, data, size);
  247. if (!rc) {
  248. smb2_set_next_command(tcon, &rqst[num_rqst]);
  249. smb2_set_related(&rqst[num_rqst]);
  250. }
  251. }
  252. if (rc)
  253. goto finished;
  254. num_rqst++;
  255. trace_smb3_set_info_compound_enter(xid, ses->Suid, tcon->tid,
  256. full_path);
  257. break;
  258. case SMB2_OP_RENAME:
  259. rqst[num_rqst].rq_iov = &vars->si_iov[0];
  260. rqst[num_rqst].rq_nvec = 2;
  261. len = (2 * UniStrnlen((wchar_t *)ptr, PATH_MAX));
  262. vars->rename_info.ReplaceIfExists = 1;
  263. vars->rename_info.RootDirectory = 0;
  264. vars->rename_info.FileNameLength = cpu_to_le32(len);
  265. size[0] = sizeof(struct smb2_file_rename_info);
  266. data[0] = &vars->rename_info;
  267. size[1] = len + 2 /* null */;
  268. data[1] = (__le16 *)ptr;
  269. if (cfile)
  270. rc = SMB2_set_info_init(tcon, server,
  271. &rqst[num_rqst],
  272. cfile->fid.persistent_fid,
  273. cfile->fid.volatile_fid,
  274. current->tgid, FILE_RENAME_INFORMATION,
  275. SMB2_O_INFO_FILE, 0, data, size);
  276. else {
  277. rc = SMB2_set_info_init(tcon, server,
  278. &rqst[num_rqst],
  279. COMPOUND_FID, COMPOUND_FID,
  280. current->tgid, FILE_RENAME_INFORMATION,
  281. SMB2_O_INFO_FILE, 0, data, size);
  282. if (!rc) {
  283. smb2_set_next_command(tcon, &rqst[num_rqst]);
  284. smb2_set_related(&rqst[num_rqst]);
  285. }
  286. }
  287. if (rc)
  288. goto finished;
  289. num_rqst++;
  290. trace_smb3_rename_enter(xid, ses->Suid, tcon->tid, full_path);
  291. break;
  292. case SMB2_OP_HARDLINK:
  293. rqst[num_rqst].rq_iov = &vars->si_iov[0];
  294. rqst[num_rqst].rq_nvec = 2;
  295. len = (2 * UniStrnlen((wchar_t *)ptr, PATH_MAX));
  296. vars->link_info.ReplaceIfExists = 0;
  297. vars->link_info.RootDirectory = 0;
  298. vars->link_info.FileNameLength = cpu_to_le32(len);
  299. size[0] = sizeof(struct smb2_file_link_info);
  300. data[0] = &vars->link_info;
  301. size[1] = len + 2 /* null */;
  302. data[1] = (__le16 *)ptr;
  303. rc = SMB2_set_info_init(tcon, server,
  304. &rqst[num_rqst], COMPOUND_FID,
  305. COMPOUND_FID, current->tgid,
  306. FILE_LINK_INFORMATION,
  307. SMB2_O_INFO_FILE, 0, data, size);
  308. if (rc)
  309. goto finished;
  310. smb2_set_next_command(tcon, &rqst[num_rqst]);
  311. smb2_set_related(&rqst[num_rqst++]);
  312. trace_smb3_hardlink_enter(xid, ses->Suid, tcon->tid, full_path);
  313. break;
  314. default:
  315. cifs_dbg(VFS, "Invalid command\n");
  316. rc = -EINVAL;
  317. }
  318. if (rc)
  319. goto finished;
  320. /* We already have a handle so we can skip the close */
  321. if (cfile)
  322. goto after_close;
  323. /* Close */
  324. flags |= CIFS_CP_CREATE_CLOSE_OP;
  325. rqst[num_rqst].rq_iov = &vars->close_iov[0];
  326. rqst[num_rqst].rq_nvec = 1;
  327. rc = SMB2_close_init(tcon, server,
  328. &rqst[num_rqst], COMPOUND_FID,
  329. COMPOUND_FID, false);
  330. smb2_set_related(&rqst[num_rqst]);
  331. if (rc)
  332. goto finished;
  333. after_close:
  334. num_rqst++;
  335. if (cfile) {
  336. cifsFileInfo_put(cfile);
  337. cfile = NULL;
  338. rc = compound_send_recv(xid, ses, server,
  339. flags, num_rqst - 2,
  340. &rqst[1], &resp_buftype[1],
  341. &rsp_iov[1]);
  342. } else
  343. rc = compound_send_recv(xid, ses, server,
  344. flags, num_rqst,
  345. rqst, resp_buftype,
  346. rsp_iov);
  347. finished:
  348. if (cfile)
  349. cifsFileInfo_put(cfile);
  350. SMB2_open_free(&rqst[0]);
  351. if (rc == -EREMCHG) {
  352. pr_warn_once("server share %s deleted\n", tcon->treeName);
  353. tcon->need_reconnect = true;
  354. }
  355. switch (command) {
  356. case SMB2_OP_QUERY_INFO:
  357. if (rc == 0) {
  358. qi_rsp = (struct smb2_query_info_rsp *)
  359. rsp_iov[1].iov_base;
  360. rc = smb2_validate_and_copy_iov(
  361. le16_to_cpu(qi_rsp->OutputBufferOffset),
  362. le32_to_cpu(qi_rsp->OutputBufferLength),
  363. &rsp_iov[1], sizeof(struct smb2_file_all_info),
  364. ptr);
  365. }
  366. if (rqst[1].rq_iov)
  367. SMB2_query_info_free(&rqst[1]);
  368. if (rqst[2].rq_iov)
  369. SMB2_close_free(&rqst[2]);
  370. if (rc)
  371. trace_smb3_query_info_compound_err(xid, ses->Suid,
  372. tcon->tid, rc);
  373. else
  374. trace_smb3_query_info_compound_done(xid, ses->Suid,
  375. tcon->tid);
  376. break;
  377. case SMB2_OP_POSIX_QUERY_INFO:
  378. if (rc == 0) {
  379. qi_rsp = (struct smb2_query_info_rsp *)
  380. rsp_iov[1].iov_base;
  381. rc = smb2_validate_and_copy_iov(
  382. le16_to_cpu(qi_rsp->OutputBufferOffset),
  383. le32_to_cpu(qi_rsp->OutputBufferLength),
  384. &rsp_iov[1], sizeof(struct smb311_posix_qinfo) /* add SIDs */, ptr);
  385. }
  386. if (rqst[1].rq_iov)
  387. SMB2_query_info_free(&rqst[1]);
  388. if (rqst[2].rq_iov)
  389. SMB2_close_free(&rqst[2]);
  390. if (rc)
  391. trace_smb3_posix_query_info_compound_err(xid, ses->Suid, tcon->tid, rc);
  392. else
  393. trace_smb3_posix_query_info_compound_done(xid, ses->Suid, tcon->tid);
  394. break;
  395. case SMB2_OP_DELETE:
  396. if (rc)
  397. trace_smb3_delete_err(xid, ses->Suid, tcon->tid, rc);
  398. else
  399. trace_smb3_delete_done(xid, ses->Suid, tcon->tid);
  400. if (rqst[1].rq_iov)
  401. SMB2_close_free(&rqst[1]);
  402. break;
  403. case SMB2_OP_MKDIR:
  404. if (rc)
  405. trace_smb3_mkdir_err(xid, ses->Suid, tcon->tid, rc);
  406. else
  407. trace_smb3_mkdir_done(xid, ses->Suid, tcon->tid);
  408. if (rqst[1].rq_iov)
  409. SMB2_close_free(&rqst[1]);
  410. break;
  411. case SMB2_OP_HARDLINK:
  412. if (rc)
  413. trace_smb3_hardlink_err(xid, ses->Suid, tcon->tid, rc);
  414. else
  415. trace_smb3_hardlink_done(xid, ses->Suid, tcon->tid);
  416. free_set_inf_compound(rqst);
  417. break;
  418. case SMB2_OP_RENAME:
  419. if (rc)
  420. trace_smb3_rename_err(xid, ses->Suid, tcon->tid, rc);
  421. else
  422. trace_smb3_rename_done(xid, ses->Suid, tcon->tid);
  423. free_set_inf_compound(rqst);
  424. break;
  425. case SMB2_OP_RMDIR:
  426. if (rc)
  427. trace_smb3_rmdir_err(xid, ses->Suid, tcon->tid, rc);
  428. else
  429. trace_smb3_rmdir_done(xid, ses->Suid, tcon->tid);
  430. free_set_inf_compound(rqst);
  431. break;
  432. case SMB2_OP_SET_EOF:
  433. if (rc)
  434. trace_smb3_set_eof_err(xid, ses->Suid, tcon->tid, rc);
  435. else
  436. trace_smb3_set_eof_done(xid, ses->Suid, tcon->tid);
  437. free_set_inf_compound(rqst);
  438. break;
  439. case SMB2_OP_SET_INFO:
  440. if (rc)
  441. trace_smb3_set_info_compound_err(xid, ses->Suid,
  442. tcon->tid, rc);
  443. else
  444. trace_smb3_set_info_compound_done(xid, ses->Suid,
  445. tcon->tid);
  446. free_set_inf_compound(rqst);
  447. break;
  448. }
  449. free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
  450. free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
  451. free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
  452. kfree(vars);
  453. return rc;
  454. }
  455. void
  456. move_smb2_info_to_cifs(FILE_ALL_INFO *dst, struct smb2_file_all_info *src)
  457. {
  458. memcpy(dst, src, (size_t)(&src->CurrentByteOffset) - (size_t)src);
  459. dst->CurrentByteOffset = src->CurrentByteOffset;
  460. dst->Mode = src->Mode;
  461. dst->AlignmentRequirement = src->AlignmentRequirement;
  462. dst->IndexNumber1 = 0; /* we don't use it */
  463. }
  464. int
  465. smb2_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
  466. struct cifs_sb_info *cifs_sb, const char *full_path,
  467. FILE_ALL_INFO *data, bool *adjust_tz, bool *reparse)
  468. {
  469. int rc;
  470. struct smb2_file_all_info *smb2_data;
  471. __u32 create_options = 0;
  472. bool no_cached_open = tcon->nohandlecache;
  473. struct cifsFileInfo *cfile;
  474. struct cached_fid *cfid = NULL;
  475. *adjust_tz = false;
  476. *reparse = false;
  477. smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
  478. GFP_KERNEL);
  479. if (smb2_data == NULL)
  480. return -ENOMEM;
  481. /* If it is a root and its handle is cached then use it */
  482. if (!strlen(full_path) && !no_cached_open) {
  483. rc = open_shroot(xid, tcon, cifs_sb, &cfid);
  484. if (rc)
  485. goto out;
  486. if (tcon->crfid.file_all_info_is_valid) {
  487. move_smb2_info_to_cifs(data,
  488. &tcon->crfid.file_all_info);
  489. } else {
  490. rc = SMB2_query_info(xid, tcon,
  491. cfid->fid->persistent_fid,
  492. cfid->fid->volatile_fid, smb2_data);
  493. if (!rc)
  494. move_smb2_info_to_cifs(data, smb2_data);
  495. }
  496. close_shroot(cfid);
  497. goto out;
  498. }
  499. cifs_get_readable_path(tcon, full_path, &cfile);
  500. rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
  501. FILE_READ_ATTRIBUTES, FILE_OPEN, create_options,
  502. ACL_NO_MODE, smb2_data, SMB2_OP_QUERY_INFO, cfile);
  503. if (rc == -EOPNOTSUPP) {
  504. *reparse = true;
  505. create_options |= OPEN_REPARSE_POINT;
  506. /* Failed on a symbolic link - query a reparse point info */
  507. rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
  508. FILE_READ_ATTRIBUTES, FILE_OPEN,
  509. create_options, ACL_NO_MODE,
  510. smb2_data, SMB2_OP_QUERY_INFO, NULL);
  511. }
  512. if (rc)
  513. goto out;
  514. move_smb2_info_to_cifs(data, smb2_data);
  515. out:
  516. kfree(smb2_data);
  517. return rc;
  518. }
  519. int
  520. smb311_posix_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
  521. struct cifs_sb_info *cifs_sb, const char *full_path,
  522. struct smb311_posix_qinfo *data, bool *adjust_tz, bool *reparse)
  523. {
  524. int rc;
  525. __u32 create_options = 0;
  526. struct cifsFileInfo *cfile;
  527. struct smb311_posix_qinfo *smb2_data;
  528. *adjust_tz = false;
  529. *reparse = false;
  530. /* BB TODO: Make struct larger when add support for parsing owner SIDs */
  531. smb2_data = kzalloc(sizeof(struct smb311_posix_qinfo),
  532. GFP_KERNEL);
  533. if (smb2_data == NULL)
  534. return -ENOMEM;
  535. /*
  536. * BB TODO: Add support for using the cached root handle.
  537. * Create SMB2_query_posix_info worker function to do non-compounded query
  538. * when we already have an open file handle for this. For now this is fast enough
  539. * (always using the compounded version).
  540. */
  541. cifs_get_readable_path(tcon, full_path, &cfile);
  542. rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
  543. FILE_READ_ATTRIBUTES, FILE_OPEN, create_options,
  544. ACL_NO_MODE, smb2_data, SMB2_OP_POSIX_QUERY_INFO, cfile);
  545. if (rc == -EOPNOTSUPP) {
  546. /* BB TODO: When support for special files added to Samba re-verify this path */
  547. *reparse = true;
  548. create_options |= OPEN_REPARSE_POINT;
  549. /* Failed on a symbolic link - query a reparse point info */
  550. rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
  551. FILE_READ_ATTRIBUTES, FILE_OPEN,
  552. create_options, ACL_NO_MODE,
  553. smb2_data, SMB2_OP_POSIX_QUERY_INFO, NULL);
  554. }
  555. if (rc)
  556. goto out;
  557. /* TODO: will need to allow for the 2 SIDs when add support for getting owner UID/GID */
  558. memcpy(data, smb2_data, sizeof(struct smb311_posix_qinfo));
  559. out:
  560. kfree(smb2_data);
  561. return rc;
  562. }
  563. int
  564. smb2_mkdir(const unsigned int xid, struct inode *parent_inode, umode_t mode,
  565. struct cifs_tcon *tcon, const char *name,
  566. struct cifs_sb_info *cifs_sb)
  567. {
  568. return smb2_compound_op(xid, tcon, cifs_sb, name,
  569. FILE_WRITE_ATTRIBUTES, FILE_CREATE,
  570. CREATE_NOT_FILE, mode, NULL, SMB2_OP_MKDIR,
  571. NULL);
  572. }
  573. void
  574. smb2_mkdir_setinfo(struct inode *inode, const char *name,
  575. struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
  576. const unsigned int xid)
  577. {
  578. FILE_BASIC_INFO data;
  579. struct cifsInodeInfo *cifs_i;
  580. struct cifsFileInfo *cfile;
  581. u32 dosattrs;
  582. int tmprc;
  583. memset(&data, 0, sizeof(data));
  584. cifs_i = CIFS_I(inode);
  585. dosattrs = cifs_i->cifsAttrs | ATTR_READONLY;
  586. data.Attributes = cpu_to_le32(dosattrs);
  587. cifs_get_writable_path(tcon, name, FIND_WR_ANY, &cfile);
  588. tmprc = smb2_compound_op(xid, tcon, cifs_sb, name,
  589. FILE_WRITE_ATTRIBUTES, FILE_CREATE,
  590. CREATE_NOT_FILE, ACL_NO_MODE,
  591. &data, SMB2_OP_SET_INFO, cfile);
  592. if (tmprc == 0)
  593. cifs_i->cifsAttrs = dosattrs;
  594. }
  595. int
  596. smb2_rmdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
  597. struct cifs_sb_info *cifs_sb)
  598. {
  599. return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
  600. CREATE_NOT_FILE, ACL_NO_MODE,
  601. NULL, SMB2_OP_RMDIR, NULL);
  602. }
  603. int
  604. smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
  605. struct cifs_sb_info *cifs_sb)
  606. {
  607. return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
  608. CREATE_DELETE_ON_CLOSE | OPEN_REPARSE_POINT,
  609. ACL_NO_MODE, NULL, SMB2_OP_DELETE, NULL);
  610. }
  611. static int
  612. smb2_set_path_attr(const unsigned int xid, struct cifs_tcon *tcon,
  613. const char *from_name, const char *to_name,
  614. struct cifs_sb_info *cifs_sb, __u32 access, int command,
  615. struct cifsFileInfo *cfile)
  616. {
  617. __le16 *smb2_to_name = NULL;
  618. int rc;
  619. smb2_to_name = cifs_convert_path_to_utf16(to_name, cifs_sb);
  620. if (smb2_to_name == NULL) {
  621. rc = -ENOMEM;
  622. goto smb2_rename_path;
  623. }
  624. rc = smb2_compound_op(xid, tcon, cifs_sb, from_name, access,
  625. FILE_OPEN, 0, ACL_NO_MODE, smb2_to_name,
  626. command, cfile);
  627. smb2_rename_path:
  628. kfree(smb2_to_name);
  629. return rc;
  630. }
  631. int
  632. smb2_rename_path(const unsigned int xid, struct cifs_tcon *tcon,
  633. const char *from_name, const char *to_name,
  634. struct cifs_sb_info *cifs_sb)
  635. {
  636. struct cifsFileInfo *cfile;
  637. cifs_get_writable_path(tcon, from_name, FIND_WR_WITH_DELETE, &cfile);
  638. return smb2_set_path_attr(xid, tcon, from_name, to_name,
  639. cifs_sb, DELETE, SMB2_OP_RENAME, cfile);
  640. }
  641. int
  642. smb2_create_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
  643. const char *from_name, const char *to_name,
  644. struct cifs_sb_info *cifs_sb)
  645. {
  646. return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
  647. FILE_READ_ATTRIBUTES, SMB2_OP_HARDLINK,
  648. NULL);
  649. }
  650. int
  651. smb2_set_path_size(const unsigned int xid, struct cifs_tcon *tcon,
  652. const char *full_path, __u64 size,
  653. struct cifs_sb_info *cifs_sb, bool set_alloc)
  654. {
  655. __le64 eof = cpu_to_le64(size);
  656. return smb2_compound_op(xid, tcon, cifs_sb, full_path,
  657. FILE_WRITE_DATA, FILE_OPEN, 0, ACL_NO_MODE,
  658. &eof, SMB2_OP_SET_EOF, NULL);
  659. }
  660. int
  661. smb2_set_file_info(struct inode *inode, const char *full_path,
  662. FILE_BASIC_INFO *buf, const unsigned int xid)
  663. {
  664. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  665. struct tcon_link *tlink;
  666. int rc;
  667. if ((buf->CreationTime == 0) && (buf->LastAccessTime == 0) &&
  668. (buf->LastWriteTime == 0) && (buf->ChangeTime == 0) &&
  669. (buf->Attributes == 0))
  670. return 0; /* would be a no op, no sense sending this */
  671. tlink = cifs_sb_tlink(cifs_sb);
  672. if (IS_ERR(tlink))
  673. return PTR_ERR(tlink);
  674. rc = smb2_compound_op(xid, tlink_tcon(tlink), cifs_sb, full_path,
  675. FILE_WRITE_ATTRIBUTES, FILE_OPEN,
  676. 0, ACL_NO_MODE, buf, SMB2_OP_SET_INFO, NULL);
  677. cifs_put_tlink(tlink);
  678. return rc;
  679. }