Loading...
1// SPDX-License-Identifier: LGPL-2.1
2/*
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 */
10#include <linux/fs.h>
11#include <linux/stat.h>
12#include <linux/slab.h>
13#include <linux/pagemap.h>
14#include <asm/div64.h>
15#include "cifsfs.h"
16#include "cifspdu.h"
17#include "cifsglob.h"
18#include "cifsproto.h"
19#include "cifs_debug.h"
20#include "cifs_fs_sb.h"
21#include "cifs_unicode.h"
22#include "fscache.h"
23#include "smb2glob.h"
24#include "smb2pdu.h"
25#include "smb2proto.h"
26#include "cached_dir.h"
27#include "smb2status.h"
28
29static void
30free_set_inf_compound(struct smb_rqst *rqst)
31{
32 if (rqst[1].rq_iov)
33 SMB2_set_info_free(&rqst[1]);
34 if (rqst[2].rq_iov)
35 SMB2_close_free(&rqst[2]);
36}
37
38
39struct cop_vars {
40 struct cifs_open_parms oparms;
41 struct kvec rsp_iov[3];
42 struct smb_rqst rqst[3];
43 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
44 struct kvec qi_iov[1];
45 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
46 struct kvec close_iov[1];
47 struct smb2_file_rename_info rename_info;
48 struct smb2_file_link_info link_info;
49};
50
51/*
52 * note: If cfile is passed, the reference to it is dropped here.
53 * So make sure that you do not reuse cfile after return from this func.
54 *
55 * If passing @err_iov and @err_buftype, ensure to make them both large enough (>= 3) to hold all
56 * error responses. Caller is also responsible for freeing them up.
57 */
58static int smb2_compound_op(const unsigned int xid, struct cifs_tcon *tcon,
59 struct cifs_sb_info *cifs_sb, const char *full_path,
60 __u32 desired_access, __u32 create_disposition, __u32 create_options,
61 umode_t mode, void *ptr, int command, struct cifsFileInfo *cfile,
62 __u8 **extbuf, size_t *extbuflen,
63 struct kvec *err_iov, int *err_buftype)
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 struct cifs_open_info_data *idata;
78 int flags = 0;
79 __u8 delete_pending[8] = {1, 0, 0, 0, 0, 0, 0, 0};
80 unsigned int size[2];
81 void *data[2];
82 int len;
83
84 vars = kzalloc(sizeof(*vars), GFP_ATOMIC);
85 if (vars == NULL)
86 return -ENOMEM;
87 rqst = &vars->rqst[0];
88 rsp_iov = &vars->rsp_iov[0];
89
90 server = cifs_pick_channel(ses);
91
92 if (smb3_encryption_required(tcon))
93 flags |= CIFS_TRANSFORM_REQ;
94
95 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
96
97 /* We already have a handle so we can skip the open */
98 if (cfile)
99 goto after_open;
100
101 /* Open */
102 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
103 if (!utf16_path) {
104 rc = -ENOMEM;
105 goto finished;
106 }
107
108 vars->oparms.tcon = tcon;
109 vars->oparms.desired_access = desired_access;
110 vars->oparms.disposition = create_disposition;
111 vars->oparms.create_options = cifs_create_options(cifs_sb, create_options);
112 vars->oparms.fid = &fid;
113 vars->oparms.reconnect = false;
114 vars->oparms.mode = mode;
115 vars->oparms.cifs_sb = cifs_sb;
116
117 rqst[num_rqst].rq_iov = &vars->open_iov[0];
118 rqst[num_rqst].rq_nvec = SMB2_CREATE_IOV_SIZE;
119 rc = SMB2_open_init(tcon, server,
120 &rqst[num_rqst], &oplock, &vars->oparms,
121 utf16_path);
122 kfree(utf16_path);
123 if (rc)
124 goto finished;
125
126 smb2_set_next_command(tcon, &rqst[num_rqst]);
127 after_open:
128 num_rqst++;
129 rc = 0;
130
131 /* Operation */
132 switch (command) {
133 case SMB2_OP_QUERY_INFO:
134 rqst[num_rqst].rq_iov = &vars->qi_iov[0];
135 rqst[num_rqst].rq_nvec = 1;
136
137 if (cfile)
138 rc = SMB2_query_info_init(tcon, server,
139 &rqst[num_rqst],
140 cfile->fid.persistent_fid,
141 cfile->fid.volatile_fid,
142 FILE_ALL_INFORMATION,
143 SMB2_O_INFO_FILE, 0,
144 sizeof(struct smb2_file_all_info) +
145 PATH_MAX * 2, 0, NULL);
146 else {
147 rc = SMB2_query_info_init(tcon, server,
148 &rqst[num_rqst],
149 COMPOUND_FID,
150 COMPOUND_FID,
151 FILE_ALL_INFORMATION,
152 SMB2_O_INFO_FILE, 0,
153 sizeof(struct smb2_file_all_info) +
154 PATH_MAX * 2, 0, NULL);
155 if (!rc) {
156 smb2_set_next_command(tcon, &rqst[num_rqst]);
157 smb2_set_related(&rqst[num_rqst]);
158 }
159 }
160
161 if (rc)
162 goto finished;
163 num_rqst++;
164 trace_smb3_query_info_compound_enter(xid, ses->Suid, tcon->tid,
165 full_path);
166 break;
167 case SMB2_OP_POSIX_QUERY_INFO:
168 rqst[num_rqst].rq_iov = &vars->qi_iov[0];
169 rqst[num_rqst].rq_nvec = 1;
170
171 if (cfile)
172 rc = SMB2_query_info_init(tcon, server,
173 &rqst[num_rqst],
174 cfile->fid.persistent_fid,
175 cfile->fid.volatile_fid,
176 SMB_FIND_FILE_POSIX_INFO,
177 SMB2_O_INFO_FILE, 0,
178 /* TBD: fix following to allow for longer SIDs */
179 sizeof(struct smb311_posix_qinfo *) + (PATH_MAX * 2) +
180 (sizeof(struct cifs_sid) * 2), 0, NULL);
181 else {
182 rc = SMB2_query_info_init(tcon, server,
183 &rqst[num_rqst],
184 COMPOUND_FID,
185 COMPOUND_FID,
186 SMB_FIND_FILE_POSIX_INFO,
187 SMB2_O_INFO_FILE, 0,
188 sizeof(struct smb311_posix_qinfo *) + (PATH_MAX * 2) +
189 (sizeof(struct cifs_sid) * 2), 0, NULL);
190 if (!rc) {
191 smb2_set_next_command(tcon, &rqst[num_rqst]);
192 smb2_set_related(&rqst[num_rqst]);
193 }
194 }
195
196 if (rc)
197 goto finished;
198 num_rqst++;
199 trace_smb3_posix_query_info_compound_enter(xid, ses->Suid, tcon->tid, full_path);
200 break;
201 case SMB2_OP_DELETE:
202 trace_smb3_delete_enter(xid, ses->Suid, tcon->tid, full_path);
203 break;
204 case SMB2_OP_MKDIR:
205 /*
206 * Directories are created through parameters in the
207 * SMB2_open() call.
208 */
209 trace_smb3_mkdir_enter(xid, ses->Suid, tcon->tid, full_path);
210 break;
211 case SMB2_OP_RMDIR:
212 rqst[num_rqst].rq_iov = &vars->si_iov[0];
213 rqst[num_rqst].rq_nvec = 1;
214
215 size[0] = 1; /* sizeof __u8 See MS-FSCC section 2.4.11 */
216 data[0] = &delete_pending[0];
217
218 rc = SMB2_set_info_init(tcon, server,
219 &rqst[num_rqst], COMPOUND_FID,
220 COMPOUND_FID, current->tgid,
221 FILE_DISPOSITION_INFORMATION,
222 SMB2_O_INFO_FILE, 0, data, size);
223 if (rc)
224 goto finished;
225 smb2_set_next_command(tcon, &rqst[num_rqst]);
226 smb2_set_related(&rqst[num_rqst++]);
227 trace_smb3_rmdir_enter(xid, ses->Suid, tcon->tid, full_path);
228 break;
229 case SMB2_OP_SET_EOF:
230 rqst[num_rqst].rq_iov = &vars->si_iov[0];
231 rqst[num_rqst].rq_nvec = 1;
232
233 size[0] = 8; /* sizeof __le64 */
234 data[0] = ptr;
235
236 rc = SMB2_set_info_init(tcon, server,
237 &rqst[num_rqst], COMPOUND_FID,
238 COMPOUND_FID, current->tgid,
239 FILE_END_OF_FILE_INFORMATION,
240 SMB2_O_INFO_FILE, 0, data, size);
241 if (rc)
242 goto finished;
243 smb2_set_next_command(tcon, &rqst[num_rqst]);
244 smb2_set_related(&rqst[num_rqst++]);
245 trace_smb3_set_eof_enter(xid, ses->Suid, tcon->tid, full_path);
246 break;
247 case SMB2_OP_SET_INFO:
248 rqst[num_rqst].rq_iov = &vars->si_iov[0];
249 rqst[num_rqst].rq_nvec = 1;
250
251
252 size[0] = sizeof(FILE_BASIC_INFO);
253 data[0] = ptr;
254
255 if (cfile)
256 rc = SMB2_set_info_init(tcon, server,
257 &rqst[num_rqst],
258 cfile->fid.persistent_fid,
259 cfile->fid.volatile_fid, current->tgid,
260 FILE_BASIC_INFORMATION,
261 SMB2_O_INFO_FILE, 0, data, size);
262 else {
263 rc = SMB2_set_info_init(tcon, server,
264 &rqst[num_rqst],
265 COMPOUND_FID,
266 COMPOUND_FID, current->tgid,
267 FILE_BASIC_INFORMATION,
268 SMB2_O_INFO_FILE, 0, data, size);
269 if (!rc) {
270 smb2_set_next_command(tcon, &rqst[num_rqst]);
271 smb2_set_related(&rqst[num_rqst]);
272 }
273 }
274
275 if (rc)
276 goto finished;
277 num_rqst++;
278 trace_smb3_set_info_compound_enter(xid, ses->Suid, tcon->tid,
279 full_path);
280 break;
281 case SMB2_OP_RENAME:
282 rqst[num_rqst].rq_iov = &vars->si_iov[0];
283 rqst[num_rqst].rq_nvec = 2;
284
285 len = (2 * UniStrnlen((wchar_t *)ptr, PATH_MAX));
286
287 vars->rename_info.ReplaceIfExists = 1;
288 vars->rename_info.RootDirectory = 0;
289 vars->rename_info.FileNameLength = cpu_to_le32(len);
290
291 size[0] = sizeof(struct smb2_file_rename_info);
292 data[0] = &vars->rename_info;
293
294 size[1] = len + 2 /* null */;
295 data[1] = (__le16 *)ptr;
296
297 if (cfile)
298 rc = SMB2_set_info_init(tcon, server,
299 &rqst[num_rqst],
300 cfile->fid.persistent_fid,
301 cfile->fid.volatile_fid,
302 current->tgid, FILE_RENAME_INFORMATION,
303 SMB2_O_INFO_FILE, 0, data, size);
304 else {
305 rc = SMB2_set_info_init(tcon, server,
306 &rqst[num_rqst],
307 COMPOUND_FID, COMPOUND_FID,
308 current->tgid, FILE_RENAME_INFORMATION,
309 SMB2_O_INFO_FILE, 0, data, size);
310 if (!rc) {
311 smb2_set_next_command(tcon, &rqst[num_rqst]);
312 smb2_set_related(&rqst[num_rqst]);
313 }
314 }
315 if (rc)
316 goto finished;
317 num_rqst++;
318 trace_smb3_rename_enter(xid, ses->Suid, tcon->tid, full_path);
319 break;
320 case SMB2_OP_HARDLINK:
321 rqst[num_rqst].rq_iov = &vars->si_iov[0];
322 rqst[num_rqst].rq_nvec = 2;
323
324 len = (2 * UniStrnlen((wchar_t *)ptr, PATH_MAX));
325
326 vars->link_info.ReplaceIfExists = 0;
327 vars->link_info.RootDirectory = 0;
328 vars->link_info.FileNameLength = cpu_to_le32(len);
329
330 size[0] = sizeof(struct smb2_file_link_info);
331 data[0] = &vars->link_info;
332
333 size[1] = len + 2 /* null */;
334 data[1] = (__le16 *)ptr;
335
336 rc = SMB2_set_info_init(tcon, server,
337 &rqst[num_rqst], COMPOUND_FID,
338 COMPOUND_FID, current->tgid,
339 FILE_LINK_INFORMATION,
340 SMB2_O_INFO_FILE, 0, data, size);
341 if (rc)
342 goto finished;
343 smb2_set_next_command(tcon, &rqst[num_rqst]);
344 smb2_set_related(&rqst[num_rqst++]);
345 trace_smb3_hardlink_enter(xid, ses->Suid, tcon->tid, full_path);
346 break;
347 default:
348 cifs_dbg(VFS, "Invalid command\n");
349 rc = -EINVAL;
350 }
351 if (rc)
352 goto finished;
353
354 /* We already have a handle so we can skip the close */
355 if (cfile)
356 goto after_close;
357 /* Close */
358 flags |= CIFS_CP_CREATE_CLOSE_OP;
359 rqst[num_rqst].rq_iov = &vars->close_iov[0];
360 rqst[num_rqst].rq_nvec = 1;
361 rc = SMB2_close_init(tcon, server,
362 &rqst[num_rqst], COMPOUND_FID,
363 COMPOUND_FID, false);
364 smb2_set_related(&rqst[num_rqst]);
365 if (rc)
366 goto finished;
367 after_close:
368 num_rqst++;
369
370 if (cfile) {
371 rc = compound_send_recv(xid, ses, server,
372 flags, num_rqst - 2,
373 &rqst[1], &resp_buftype[1],
374 &rsp_iov[1]);
375 } else
376 rc = compound_send_recv(xid, ses, server,
377 flags, num_rqst,
378 rqst, resp_buftype,
379 rsp_iov);
380
381 finished:
382 if (cfile)
383 cifsFileInfo_put(cfile);
384
385 SMB2_open_free(&rqst[0]);
386 if (rc == -EREMCHG) {
387 pr_warn_once("server share %s deleted\n", tcon->tree_name);
388 tcon->need_reconnect = true;
389 }
390
391 switch (command) {
392 case SMB2_OP_QUERY_INFO:
393 idata = ptr;
394 if (rc == 0 && cfile && cfile->symlink_target) {
395 idata->symlink_target = kstrdup(cfile->symlink_target, GFP_KERNEL);
396 if (!idata->symlink_target)
397 rc = -ENOMEM;
398 }
399 if (rc == 0) {
400 qi_rsp = (struct smb2_query_info_rsp *)
401 rsp_iov[1].iov_base;
402 rc = smb2_validate_and_copy_iov(
403 le16_to_cpu(qi_rsp->OutputBufferOffset),
404 le32_to_cpu(qi_rsp->OutputBufferLength),
405 &rsp_iov[1], sizeof(idata->fi), (char *)&idata->fi);
406 }
407 if (rqst[1].rq_iov)
408 SMB2_query_info_free(&rqst[1]);
409 if (rqst[2].rq_iov)
410 SMB2_close_free(&rqst[2]);
411 if (rc)
412 trace_smb3_query_info_compound_err(xid, ses->Suid,
413 tcon->tid, rc);
414 else
415 trace_smb3_query_info_compound_done(xid, ses->Suid,
416 tcon->tid);
417 break;
418 case SMB2_OP_POSIX_QUERY_INFO:
419 idata = ptr;
420 if (rc == 0 && cfile && cfile->symlink_target) {
421 idata->symlink_target = kstrdup(cfile->symlink_target, GFP_KERNEL);
422 if (!idata->symlink_target)
423 rc = -ENOMEM;
424 }
425 if (rc == 0) {
426 qi_rsp = (struct smb2_query_info_rsp *)
427 rsp_iov[1].iov_base;
428 rc = smb2_validate_and_copy_iov(
429 le16_to_cpu(qi_rsp->OutputBufferOffset),
430 le32_to_cpu(qi_rsp->OutputBufferLength),
431 &rsp_iov[1], sizeof(idata->posix_fi) /* add SIDs */,
432 (char *)&idata->posix_fi);
433 }
434 if (rc == 0) {
435 unsigned int length = le32_to_cpu(qi_rsp->OutputBufferLength);
436
437 if (length > sizeof(idata->posix_fi)) {
438 char *base = (char *)rsp_iov[1].iov_base +
439 le16_to_cpu(qi_rsp->OutputBufferOffset) +
440 sizeof(idata->posix_fi);
441 *extbuflen = length - sizeof(idata->posix_fi);
442 *extbuf = kmemdup(base, *extbuflen, GFP_KERNEL);
443 if (!*extbuf)
444 rc = -ENOMEM;
445 } else {
446 rc = -EINVAL;
447 }
448 }
449 if (rqst[1].rq_iov)
450 SMB2_query_info_free(&rqst[1]);
451 if (rqst[2].rq_iov)
452 SMB2_close_free(&rqst[2]);
453 if (rc)
454 trace_smb3_posix_query_info_compound_err(xid, ses->Suid, tcon->tid, rc);
455 else
456 trace_smb3_posix_query_info_compound_done(xid, ses->Suid, tcon->tid);
457 break;
458 case SMB2_OP_DELETE:
459 if (rc)
460 trace_smb3_delete_err(xid, ses->Suid, tcon->tid, rc);
461 else
462 trace_smb3_delete_done(xid, ses->Suid, tcon->tid);
463 if (rqst[1].rq_iov)
464 SMB2_close_free(&rqst[1]);
465 break;
466 case SMB2_OP_MKDIR:
467 if (rc)
468 trace_smb3_mkdir_err(xid, ses->Suid, tcon->tid, rc);
469 else
470 trace_smb3_mkdir_done(xid, ses->Suid, tcon->tid);
471 if (rqst[1].rq_iov)
472 SMB2_close_free(&rqst[1]);
473 break;
474 case SMB2_OP_HARDLINK:
475 if (rc)
476 trace_smb3_hardlink_err(xid, ses->Suid, tcon->tid, rc);
477 else
478 trace_smb3_hardlink_done(xid, ses->Suid, tcon->tid);
479 free_set_inf_compound(rqst);
480 break;
481 case SMB2_OP_RENAME:
482 if (rc)
483 trace_smb3_rename_err(xid, ses->Suid, tcon->tid, rc);
484 else
485 trace_smb3_rename_done(xid, ses->Suid, tcon->tid);
486 free_set_inf_compound(rqst);
487 break;
488 case SMB2_OP_RMDIR:
489 if (rc)
490 trace_smb3_rmdir_err(xid, ses->Suid, tcon->tid, rc);
491 else
492 trace_smb3_rmdir_done(xid, ses->Suid, tcon->tid);
493 free_set_inf_compound(rqst);
494 break;
495 case SMB2_OP_SET_EOF:
496 if (rc)
497 trace_smb3_set_eof_err(xid, ses->Suid, tcon->tid, rc);
498 else
499 trace_smb3_set_eof_done(xid, ses->Suid, tcon->tid);
500 free_set_inf_compound(rqst);
501 break;
502 case SMB2_OP_SET_INFO:
503 if (rc)
504 trace_smb3_set_info_compound_err(xid, ses->Suid,
505 tcon->tid, rc);
506 else
507 trace_smb3_set_info_compound_done(xid, ses->Suid,
508 tcon->tid);
509 free_set_inf_compound(rqst);
510 break;
511 }
512
513 if (rc && err_iov && err_buftype) {
514 memcpy(err_iov, rsp_iov, 3 * sizeof(*err_iov));
515 memcpy(err_buftype, resp_buftype, 3 * sizeof(*err_buftype));
516 } else {
517 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
518 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
519 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
520 }
521 kfree(vars);
522 return rc;
523}
524
525int smb2_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
526 struct cifs_sb_info *cifs_sb, const char *full_path,
527 struct cifs_open_info_data *data, bool *adjust_tz, bool *reparse)
528{
529 int rc;
530 __u32 create_options = 0;
531 struct cifsFileInfo *cfile;
532 struct cached_fid *cfid = NULL;
533 struct kvec err_iov[3] = {};
534 int err_buftype[3] = {};
535
536 *adjust_tz = false;
537 *reparse = false;
538
539 if (strcmp(full_path, ""))
540 rc = -ENOENT;
541 else
542 rc = open_cached_dir(xid, tcon, full_path, cifs_sb, false, &cfid);
543 /* If it is a root and its handle is cached then use it */
544 if (!rc) {
545 if (cfid->file_all_info_is_valid) {
546 memcpy(&data->fi, &cfid->file_all_info, sizeof(data->fi));
547 } else {
548 rc = SMB2_query_info(xid, tcon, cfid->fid.persistent_fid,
549 cfid->fid.volatile_fid, &data->fi);
550 }
551 close_cached_dir(cfid);
552 return rc;
553 }
554
555 cifs_get_readable_path(tcon, full_path, &cfile);
556 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path, FILE_READ_ATTRIBUTES, FILE_OPEN,
557 create_options, ACL_NO_MODE, data, SMB2_OP_QUERY_INFO, cfile,
558 NULL, NULL, err_iov, err_buftype);
559 if (rc) {
560 struct smb2_hdr *hdr = err_iov[0].iov_base;
561
562 if (unlikely(!hdr || err_buftype[0] == CIFS_NO_BUFFER))
563 goto out;
564 if (rc == -EOPNOTSUPP && hdr->Command == SMB2_CREATE &&
565 hdr->Status == STATUS_STOPPED_ON_SYMLINK) {
566 rc = smb2_parse_symlink_response(cifs_sb, err_iov,
567 &data->symlink_target);
568 if (rc)
569 goto out;
570
571 *reparse = true;
572 create_options |= OPEN_REPARSE_POINT;
573
574 /* Failed on a symbolic link - query a reparse point info */
575 cifs_get_readable_path(tcon, full_path, &cfile);
576 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
577 FILE_READ_ATTRIBUTES, FILE_OPEN,
578 create_options, ACL_NO_MODE, data,
579 SMB2_OP_QUERY_INFO, cfile, NULL, NULL,
580 NULL, NULL);
581 goto out;
582 } else if (rc != -EREMOTE && IS_ENABLED(CONFIG_CIFS_DFS_UPCALL) &&
583 hdr->Status == STATUS_OBJECT_NAME_INVALID) {
584 /*
585 * Handle weird Windows SMB server behaviour. It responds with
586 * STATUS_OBJECT_NAME_INVALID code to SMB2 QUERY_INFO request
587 * for "\<server>\<dfsname>\<linkpath>" DFS reference,
588 * where <dfsname> contains non-ASCII unicode symbols.
589 */
590 rc = -EREMOTE;
591 }
592 if (rc == -EREMOTE && IS_ENABLED(CONFIG_CIFS_DFS_UPCALL) && cifs_sb &&
593 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS))
594 rc = -EOPNOTSUPP;
595 }
596
597out:
598 free_rsp_buf(err_buftype[0], err_iov[0].iov_base);
599 free_rsp_buf(err_buftype[1], err_iov[1].iov_base);
600 free_rsp_buf(err_buftype[2], err_iov[2].iov_base);
601 return rc;
602}
603
604
605int smb311_posix_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
606 struct cifs_sb_info *cifs_sb, const char *full_path,
607 struct cifs_open_info_data *data,
608 struct cifs_sid *owner,
609 struct cifs_sid *group,
610 bool *adjust_tz, bool *reparse)
611{
612 int rc;
613 __u32 create_options = 0;
614 struct cifsFileInfo *cfile;
615 struct kvec err_iov[3] = {};
616 int err_buftype[3] = {};
617 __u8 *sidsbuf = NULL;
618 __u8 *sidsbuf_end = NULL;
619 size_t sidsbuflen = 0;
620 size_t owner_len, group_len;
621
622 *adjust_tz = false;
623 *reparse = false;
624
625 /*
626 * BB TODO: Add support for using the cached root handle.
627 * Create SMB2_query_posix_info worker function to do non-compounded query
628 * when we already have an open file handle for this. For now this is fast enough
629 * (always using the compounded version).
630 */
631
632 cifs_get_readable_path(tcon, full_path, &cfile);
633 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path, FILE_READ_ATTRIBUTES, FILE_OPEN,
634 create_options, ACL_NO_MODE, data, SMB2_OP_POSIX_QUERY_INFO, cfile,
635 &sidsbuf, &sidsbuflen, err_iov, err_buftype);
636 if (rc == -EOPNOTSUPP) {
637 /* BB TODO: When support for special files added to Samba re-verify this path */
638 if (err_iov[0].iov_base && err_buftype[0] != CIFS_NO_BUFFER &&
639 ((struct smb2_hdr *)err_iov[0].iov_base)->Command == SMB2_CREATE &&
640 ((struct smb2_hdr *)err_iov[0].iov_base)->Status == STATUS_STOPPED_ON_SYMLINK) {
641 rc = smb2_parse_symlink_response(cifs_sb, err_iov, &data->symlink_target);
642 if (rc)
643 goto out;
644 }
645 *reparse = true;
646 create_options |= OPEN_REPARSE_POINT;
647
648 /* Failed on a symbolic link - query a reparse point info */
649 cifs_get_readable_path(tcon, full_path, &cfile);
650 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path, FILE_READ_ATTRIBUTES,
651 FILE_OPEN, create_options, ACL_NO_MODE, data,
652 SMB2_OP_POSIX_QUERY_INFO, cfile,
653 &sidsbuf, &sidsbuflen, NULL, NULL);
654 }
655
656 if (rc == 0) {
657 sidsbuf_end = sidsbuf + sidsbuflen;
658
659 owner_len = posix_info_sid_size(sidsbuf, sidsbuf_end);
660 if (owner_len == -1) {
661 rc = -EINVAL;
662 goto out;
663 }
664 memcpy(owner, sidsbuf, owner_len);
665
666 group_len = posix_info_sid_size(
667 sidsbuf + owner_len, sidsbuf_end);
668 if (group_len == -1) {
669 rc = -EINVAL;
670 goto out;
671 }
672 memcpy(group, sidsbuf + owner_len, group_len);
673 }
674
675out:
676 kfree(sidsbuf);
677 free_rsp_buf(err_buftype[0], err_iov[0].iov_base);
678 free_rsp_buf(err_buftype[1], err_iov[1].iov_base);
679 free_rsp_buf(err_buftype[2], err_iov[2].iov_base);
680 return rc;
681}
682
683int
684smb2_mkdir(const unsigned int xid, struct inode *parent_inode, umode_t mode,
685 struct cifs_tcon *tcon, const char *name,
686 struct cifs_sb_info *cifs_sb)
687{
688 return smb2_compound_op(xid, tcon, cifs_sb, name,
689 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
690 CREATE_NOT_FILE, mode, NULL, SMB2_OP_MKDIR,
691 NULL, NULL, NULL, NULL, NULL);
692}
693
694void
695smb2_mkdir_setinfo(struct inode *inode, const char *name,
696 struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
697 const unsigned int xid)
698{
699 FILE_BASIC_INFO data;
700 struct cifsInodeInfo *cifs_i;
701 struct cifsFileInfo *cfile;
702 u32 dosattrs;
703 int tmprc;
704
705 memset(&data, 0, sizeof(data));
706 cifs_i = CIFS_I(inode);
707 dosattrs = cifs_i->cifsAttrs | ATTR_READONLY;
708 data.Attributes = cpu_to_le32(dosattrs);
709 cifs_get_writable_path(tcon, name, FIND_WR_ANY, &cfile);
710 tmprc = smb2_compound_op(xid, tcon, cifs_sb, name,
711 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
712 CREATE_NOT_FILE, ACL_NO_MODE,
713 &data, SMB2_OP_SET_INFO, cfile, NULL, NULL, NULL, NULL);
714 if (tmprc == 0)
715 cifs_i->cifsAttrs = dosattrs;
716}
717
718int
719smb2_rmdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
720 struct cifs_sb_info *cifs_sb)
721{
722 drop_cached_dir_by_name(xid, tcon, name, cifs_sb);
723 return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
724 CREATE_NOT_FILE, ACL_NO_MODE,
725 NULL, SMB2_OP_RMDIR, NULL, NULL, NULL, NULL, NULL);
726}
727
728int
729smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
730 struct cifs_sb_info *cifs_sb)
731{
732 return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
733 CREATE_DELETE_ON_CLOSE | OPEN_REPARSE_POINT,
734 ACL_NO_MODE, NULL, SMB2_OP_DELETE, NULL, NULL, NULL, NULL, NULL);
735}
736
737static int
738smb2_set_path_attr(const unsigned int xid, struct cifs_tcon *tcon,
739 const char *from_name, const char *to_name,
740 struct cifs_sb_info *cifs_sb, __u32 access, int command,
741 struct cifsFileInfo *cfile)
742{
743 __le16 *smb2_to_name = NULL;
744 int rc;
745
746 smb2_to_name = cifs_convert_path_to_utf16(to_name, cifs_sb);
747 if (smb2_to_name == NULL) {
748 rc = -ENOMEM;
749 goto smb2_rename_path;
750 }
751 rc = smb2_compound_op(xid, tcon, cifs_sb, from_name, access,
752 FILE_OPEN, 0, ACL_NO_MODE, smb2_to_name,
753 command, cfile, NULL, NULL, NULL, NULL);
754smb2_rename_path:
755 kfree(smb2_to_name);
756 return rc;
757}
758
759int
760smb2_rename_path(const unsigned int xid, struct cifs_tcon *tcon,
761 const char *from_name, const char *to_name,
762 struct cifs_sb_info *cifs_sb)
763{
764 struct cifsFileInfo *cfile;
765
766 drop_cached_dir_by_name(xid, tcon, from_name, cifs_sb);
767 cifs_get_writable_path(tcon, from_name, FIND_WR_WITH_DELETE, &cfile);
768
769 return smb2_set_path_attr(xid, tcon, from_name, to_name,
770 cifs_sb, DELETE, SMB2_OP_RENAME, cfile);
771}
772
773int
774smb2_create_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
775 const char *from_name, const char *to_name,
776 struct cifs_sb_info *cifs_sb)
777{
778 return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
779 FILE_READ_ATTRIBUTES, SMB2_OP_HARDLINK,
780 NULL);
781}
782
783int
784smb2_set_path_size(const unsigned int xid, struct cifs_tcon *tcon,
785 const char *full_path, __u64 size,
786 struct cifs_sb_info *cifs_sb, bool set_alloc)
787{
788 __le64 eof = cpu_to_le64(size);
789 struct cifsFileInfo *cfile;
790
791 cifs_get_writable_path(tcon, full_path, FIND_WR_ANY, &cfile);
792 return smb2_compound_op(xid, tcon, cifs_sb, full_path,
793 FILE_WRITE_DATA, FILE_OPEN, 0, ACL_NO_MODE,
794 &eof, SMB2_OP_SET_EOF, cfile, NULL, NULL, NULL, NULL);
795}
796
797int
798smb2_set_file_info(struct inode *inode, const char *full_path,
799 FILE_BASIC_INFO *buf, const unsigned int xid)
800{
801 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
802 struct tcon_link *tlink;
803 struct cifs_tcon *tcon;
804 struct cifsFileInfo *cfile;
805 int rc;
806
807 if ((buf->CreationTime == 0) && (buf->LastAccessTime == 0) &&
808 (buf->LastWriteTime == 0) && (buf->ChangeTime == 0) &&
809 (buf->Attributes == 0))
810 return 0; /* would be a no op, no sense sending this */
811
812 tlink = cifs_sb_tlink(cifs_sb);
813 if (IS_ERR(tlink))
814 return PTR_ERR(tlink);
815 tcon = tlink_tcon(tlink);
816
817 cifs_get_writable_path(tcon, full_path, FIND_WR_ANY, &cfile);
818 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
819 FILE_WRITE_ATTRIBUTES, FILE_OPEN,
820 0, ACL_NO_MODE, buf, SMB2_OP_SET_INFO, cfile,
821 NULL, NULL, NULL, NULL);
822 cifs_put_tlink(tlink);
823 return rc;
824}
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
40static int
41smb2_open_op_close(const unsigned int xid, struct cifs_tcon *tcon,
42 struct cifs_sb_info *cifs_sb, const char *full_path,
43 __u32 desired_access, __u32 create_disposition,
44 __u32 create_options, void *data, int command)
45{
46 int rc, tmprc = 0;
47 __le16 *utf16_path;
48 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
49 struct cifs_open_parms oparms;
50 struct cifs_fid fid;
51
52 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
53 if (!utf16_path)
54 return -ENOMEM;
55
56 oparms.tcon = tcon;
57 oparms.desired_access = desired_access;
58 oparms.disposition = create_disposition;
59 oparms.create_options = create_options;
60 oparms.fid = &fid;
61 oparms.reconnect = false;
62
63 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
64 if (rc) {
65 kfree(utf16_path);
66 return rc;
67 }
68
69 switch (command) {
70 case SMB2_OP_DELETE:
71 break;
72 case SMB2_OP_QUERY_INFO:
73 tmprc = SMB2_query_info(xid, tcon, fid.persistent_fid,
74 fid.volatile_fid,
75 (struct smb2_file_all_info *)data);
76 break;
77 case SMB2_OP_MKDIR:
78 /*
79 * Directories are created through parameters in the
80 * SMB2_open() call.
81 */
82 break;
83 case SMB2_OP_RMDIR:
84 tmprc = SMB2_rmdir(xid, tcon, fid.persistent_fid,
85 fid.volatile_fid);
86 break;
87 case SMB2_OP_RENAME:
88 tmprc = SMB2_rename(xid, tcon, fid.persistent_fid,
89 fid.volatile_fid, (__le16 *)data);
90 break;
91 case SMB2_OP_HARDLINK:
92 tmprc = SMB2_set_hardlink(xid, tcon, fid.persistent_fid,
93 fid.volatile_fid, (__le16 *)data);
94 break;
95 case SMB2_OP_SET_EOF:
96 tmprc = SMB2_set_eof(xid, tcon, fid.persistent_fid,
97 fid.volatile_fid, current->tgid,
98 (__le64 *)data, false);
99 break;
100 case SMB2_OP_SET_INFO:
101 tmprc = SMB2_set_info(xid, tcon, fid.persistent_fid,
102 fid.volatile_fid,
103 (FILE_BASIC_INFO *)data);
104 break;
105 default:
106 cifs_dbg(VFS, "Invalid command\n");
107 break;
108 }
109
110 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
111 if (tmprc)
112 rc = tmprc;
113 kfree(utf16_path);
114 return rc;
115}
116
117void
118move_smb2_info_to_cifs(FILE_ALL_INFO *dst, struct smb2_file_all_info *src)
119{
120 memcpy(dst, src, (size_t)(&src->CurrentByteOffset) - (size_t)src);
121 dst->CurrentByteOffset = src->CurrentByteOffset;
122 dst->Mode = src->Mode;
123 dst->AlignmentRequirement = src->AlignmentRequirement;
124 dst->IndexNumber1 = 0; /* we don't use it */
125}
126
127int
128smb2_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
129 struct cifs_sb_info *cifs_sb, const char *full_path,
130 FILE_ALL_INFO *data, bool *adjust_tz, bool *symlink)
131{
132 int rc;
133 struct smb2_file_all_info *smb2_data;
134
135 *adjust_tz = false;
136 *symlink = false;
137
138 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
139 GFP_KERNEL);
140 if (smb2_data == NULL)
141 return -ENOMEM;
142
143 rc = smb2_open_op_close(xid, tcon, cifs_sb, full_path,
144 FILE_READ_ATTRIBUTES, FILE_OPEN, 0,
145 smb2_data, SMB2_OP_QUERY_INFO);
146 if (rc == -EOPNOTSUPP) {
147 *symlink = true;
148 /* Failed on a symbolic link - query a reparse point info */
149 rc = smb2_open_op_close(xid, tcon, cifs_sb, full_path,
150 FILE_READ_ATTRIBUTES, FILE_OPEN,
151 OPEN_REPARSE_POINT, smb2_data,
152 SMB2_OP_QUERY_INFO);
153 }
154 if (rc)
155 goto out;
156
157 move_smb2_info_to_cifs(data, smb2_data);
158out:
159 kfree(smb2_data);
160 return rc;
161}
162
163int
164smb2_mkdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
165 struct cifs_sb_info *cifs_sb)
166{
167 return smb2_open_op_close(xid, tcon, cifs_sb, name,
168 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
169 CREATE_NOT_FILE, NULL, SMB2_OP_MKDIR);
170}
171
172void
173smb2_mkdir_setinfo(struct inode *inode, const char *name,
174 struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
175 const unsigned int xid)
176{
177 FILE_BASIC_INFO data;
178 struct cifsInodeInfo *cifs_i;
179 u32 dosattrs;
180 int tmprc;
181
182 memset(&data, 0, sizeof(data));
183 cifs_i = CIFS_I(inode);
184 dosattrs = cifs_i->cifsAttrs | ATTR_READONLY;
185 data.Attributes = cpu_to_le32(dosattrs);
186 tmprc = smb2_open_op_close(xid, tcon, cifs_sb, name,
187 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
188 CREATE_NOT_FILE, &data, SMB2_OP_SET_INFO);
189 if (tmprc == 0)
190 cifs_i->cifsAttrs = dosattrs;
191}
192
193int
194smb2_rmdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
195 struct cifs_sb_info *cifs_sb)
196{
197 return smb2_open_op_close(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
198 CREATE_NOT_FILE,
199 NULL, SMB2_OP_RMDIR);
200}
201
202int
203smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
204 struct cifs_sb_info *cifs_sb)
205{
206 return smb2_open_op_close(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
207 CREATE_DELETE_ON_CLOSE | OPEN_REPARSE_POINT,
208 NULL, SMB2_OP_DELETE);
209}
210
211static int
212smb2_set_path_attr(const unsigned int xid, struct cifs_tcon *tcon,
213 const char *from_name, const char *to_name,
214 struct cifs_sb_info *cifs_sb, __u32 access, int command)
215{
216 __le16 *smb2_to_name = NULL;
217 int rc;
218
219 smb2_to_name = cifs_convert_path_to_utf16(to_name, cifs_sb);
220 if (smb2_to_name == NULL) {
221 rc = -ENOMEM;
222 goto smb2_rename_path;
223 }
224
225 rc = smb2_open_op_close(xid, tcon, cifs_sb, from_name, access,
226 FILE_OPEN, 0, smb2_to_name, command);
227smb2_rename_path:
228 kfree(smb2_to_name);
229 return rc;
230}
231
232int
233smb2_rename_path(const unsigned int xid, struct cifs_tcon *tcon,
234 const char *from_name, const char *to_name,
235 struct cifs_sb_info *cifs_sb)
236{
237 return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
238 DELETE, SMB2_OP_RENAME);
239}
240
241int
242smb2_create_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
243 const char *from_name, const char *to_name,
244 struct cifs_sb_info *cifs_sb)
245{
246 return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
247 FILE_READ_ATTRIBUTES, SMB2_OP_HARDLINK);
248}
249
250int
251smb2_set_path_size(const unsigned int xid, struct cifs_tcon *tcon,
252 const char *full_path, __u64 size,
253 struct cifs_sb_info *cifs_sb, bool set_alloc)
254{
255 __le64 eof = cpu_to_le64(size);
256 return smb2_open_op_close(xid, tcon, cifs_sb, full_path,
257 FILE_WRITE_DATA, FILE_OPEN, 0, &eof,
258 SMB2_OP_SET_EOF);
259}
260
261int
262smb2_set_file_info(struct inode *inode, const char *full_path,
263 FILE_BASIC_INFO *buf, const unsigned int xid)
264{
265 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
266 struct tcon_link *tlink;
267 int rc;
268
269 if ((buf->CreationTime == 0) && (buf->LastAccessTime == 0) &&
270 (buf->LastWriteTime == 0) && (buf->ChangeTime) &&
271 (buf->Attributes == 0))
272 return 0; /* would be a no op, no sense sending this */
273
274 tlink = cifs_sb_tlink(cifs_sb);
275 if (IS_ERR(tlink))
276 return PTR_ERR(tlink);
277
278 rc = smb2_open_op_close(xid, tlink_tcon(tlink), cifs_sb, full_path,
279 FILE_WRITE_ATTRIBUTES, FILE_OPEN, 0, buf,
280 SMB2_OP_SET_INFO);
281 cifs_put_tlink(tlink);
282 return rc;
283}