4 * Copyright (C) International Business Machines Corp., 2002, 2011
6 * Author(s): Pavel Shilovsky (pshilovsky@samba.org),
7 * Steve French (sfrench@us.ibm.com)
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.
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.
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
24 #include <linux/stat.h>
25 #include <linux/slab.h>
26 #include <linux/pagemap.h>
27 #include <asm/div64.h>
31 #include "cifsproto.h"
32 #include "cifs_debug.h"
33 #include "cifs_fs_sb.h"
34 #include "cifs_unicode.h"
38 #include "smb2proto.h"
41 free_set_inf_compound(struct smb_rqst
*rqst
)
44 SMB2_set_info_free(&rqst
[1]);
46 SMB2_close_free(&rqst
[2]);
51 smb2_compound_op(const unsigned int xid
, struct cifs_tcon
*tcon
,
52 struct cifs_sb_info
*cifs_sb
, const char *full_path
,
53 __u32 desired_access
, __u32 create_disposition
,
54 __u32 create_options
, umode_t mode
, void *ptr
, int command
,
55 struct cifsFileInfo
*cfile
)
58 __le16
*utf16_path
= NULL
;
59 __u8 oplock
= SMB2_OPLOCK_LEVEL_NONE
;
60 struct cifs_open_parms oparms
;
62 struct cifs_ses
*ses
= tcon
->ses
;
64 struct smb_rqst rqst
[3];
66 struct kvec rsp_iov
[3];
67 struct kvec open_iov
[SMB2_CREATE_IOV_SIZE
];
68 struct kvec qi_iov
[1];
69 struct kvec si_iov
[SMB2_SET_INFO_IOV_SIZE
];
70 struct kvec close_iov
[1];
71 struct smb2_query_info_rsp
*qi_rsp
= NULL
;
73 __u8 delete_pending
[8] = {1, 0, 0, 0, 0, 0, 0, 0};
76 struct smb2_file_rename_info rename_info
;
77 struct smb2_file_link_info link_info
;
80 if (smb3_encryption_required(tcon
))
81 flags
|= CIFS_TRANSFORM_REQ
;
83 memset(rqst
, 0, sizeof(rqst
));
84 resp_buftype
[0] = resp_buftype
[1] = resp_buftype
[2] = CIFS_NO_BUFFER
;
85 memset(rsp_iov
, 0, sizeof(rsp_iov
));
87 /* We already have a handle so we can skip the open */
92 utf16_path
= cifs_convert_path_to_utf16(full_path
, cifs_sb
);
98 memset(&oparms
, 0, sizeof(struct cifs_open_parms
));
100 oparms
.desired_access
= desired_access
;
101 oparms
.disposition
= create_disposition
;
102 oparms
.create_options
= cifs_create_options(cifs_sb
, create_options
);
104 oparms
.reconnect
= false;
107 memset(&open_iov
, 0, sizeof(open_iov
));
108 rqst
[num_rqst
].rq_iov
= open_iov
;
109 rqst
[num_rqst
].rq_nvec
= SMB2_CREATE_IOV_SIZE
;
110 rc
= SMB2_open_init(tcon
, &rqst
[num_rqst
], &oplock
, &oparms
,
116 smb2_set_next_command(tcon
, &rqst
[num_rqst
]);
123 case SMB2_OP_QUERY_INFO
:
124 memset(&qi_iov
, 0, sizeof(qi_iov
));
125 rqst
[num_rqst
].rq_iov
= qi_iov
;
126 rqst
[num_rqst
].rq_nvec
= 1;
129 rc
= SMB2_query_info_init(tcon
, &rqst
[num_rqst
],
130 cfile
->fid
.persistent_fid
,
131 cfile
->fid
.volatile_fid
,
132 FILE_ALL_INFORMATION
,
134 sizeof(struct smb2_file_all_info
) +
135 PATH_MAX
* 2, 0, NULL
);
137 rc
= SMB2_query_info_init(tcon
, &rqst
[num_rqst
],
140 FILE_ALL_INFORMATION
,
142 sizeof(struct smb2_file_all_info
) +
143 PATH_MAX
* 2, 0, NULL
);
145 smb2_set_next_command(tcon
, &rqst
[num_rqst
]);
146 smb2_set_related(&rqst
[num_rqst
]);
153 trace_smb3_query_info_compound_enter(xid
, ses
->Suid
, tcon
->tid
,
157 trace_smb3_delete_enter(xid
, ses
->Suid
, tcon
->tid
, full_path
);
161 * Directories are created through parameters in the
164 trace_smb3_mkdir_enter(xid
, ses
->Suid
, tcon
->tid
, full_path
);
167 memset(&si_iov
, 0, sizeof(si_iov
));
168 rqst
[num_rqst
].rq_iov
= si_iov
;
169 rqst
[num_rqst
].rq_nvec
= 1;
171 size
[0] = 1; /* sizeof __u8 See MS-FSCC section 2.4.11 */
172 data
[0] = &delete_pending
[0];
174 rc
= SMB2_set_info_init(tcon
, &rqst
[num_rqst
], COMPOUND_FID
,
175 COMPOUND_FID
, current
->tgid
,
176 FILE_DISPOSITION_INFORMATION
,
177 SMB2_O_INFO_FILE
, 0, data
, size
);
180 smb2_set_next_command(tcon
, &rqst
[num_rqst
]);
181 smb2_set_related(&rqst
[num_rqst
++]);
182 trace_smb3_rmdir_enter(xid
, ses
->Suid
, tcon
->tid
, full_path
);
184 case SMB2_OP_SET_EOF
:
185 memset(&si_iov
, 0, sizeof(si_iov
));
186 rqst
[num_rqst
].rq_iov
= si_iov
;
187 rqst
[num_rqst
].rq_nvec
= 1;
189 size
[0] = 8; /* sizeof __le64 */
192 rc
= SMB2_set_info_init(tcon
, &rqst
[num_rqst
], COMPOUND_FID
,
193 COMPOUND_FID
, current
->tgid
,
194 FILE_END_OF_FILE_INFORMATION
,
195 SMB2_O_INFO_FILE
, 0, data
, size
);
198 smb2_set_next_command(tcon
, &rqst
[num_rqst
]);
199 smb2_set_related(&rqst
[num_rqst
++]);
200 trace_smb3_set_eof_enter(xid
, ses
->Suid
, tcon
->tid
, full_path
);
202 case SMB2_OP_SET_INFO
:
203 memset(&si_iov
, 0, sizeof(si_iov
));
204 rqst
[num_rqst
].rq_iov
= si_iov
;
205 rqst
[num_rqst
].rq_nvec
= 1;
208 size
[0] = sizeof(FILE_BASIC_INFO
);
212 rc
= SMB2_set_info_init(tcon
, &rqst
[num_rqst
],
213 cfile
->fid
.persistent_fid
,
214 cfile
->fid
.volatile_fid
, current
->tgid
,
215 FILE_BASIC_INFORMATION
,
216 SMB2_O_INFO_FILE
, 0, data
, size
);
218 rc
= SMB2_set_info_init(tcon
, &rqst
[num_rqst
],
220 COMPOUND_FID
, current
->tgid
,
221 FILE_BASIC_INFORMATION
,
222 SMB2_O_INFO_FILE
, 0, data
, size
);
224 smb2_set_next_command(tcon
, &rqst
[num_rqst
]);
225 smb2_set_related(&rqst
[num_rqst
]);
232 trace_smb3_set_info_compound_enter(xid
, ses
->Suid
, tcon
->tid
,
236 memset(&si_iov
, 0, sizeof(si_iov
));
237 rqst
[num_rqst
].rq_iov
= si_iov
;
238 rqst
[num_rqst
].rq_nvec
= 2;
240 len
= (2 * UniStrnlen((wchar_t *)ptr
, PATH_MAX
));
242 rename_info
.ReplaceIfExists
= 1;
243 rename_info
.RootDirectory
= 0;
244 rename_info
.FileNameLength
= cpu_to_le32(len
);
246 size
[0] = sizeof(struct smb2_file_rename_info
);
247 data
[0] = &rename_info
;
249 size
[1] = len
+ 2 /* null */;
250 data
[1] = (__le16
*)ptr
;
253 rc
= SMB2_set_info_init(tcon
, &rqst
[num_rqst
],
254 cfile
->fid
.persistent_fid
,
255 cfile
->fid
.volatile_fid
,
256 current
->tgid
, FILE_RENAME_INFORMATION
,
257 SMB2_O_INFO_FILE
, 0, data
, size
);
259 rc
= SMB2_set_info_init(tcon
, &rqst
[num_rqst
],
260 COMPOUND_FID
, COMPOUND_FID
,
261 current
->tgid
, FILE_RENAME_INFORMATION
,
262 SMB2_O_INFO_FILE
, 0, data
, size
);
264 smb2_set_next_command(tcon
, &rqst
[num_rqst
]);
265 smb2_set_related(&rqst
[num_rqst
]);
271 trace_smb3_rename_enter(xid
, ses
->Suid
, tcon
->tid
, full_path
);
273 case SMB2_OP_HARDLINK
:
274 memset(&si_iov
, 0, sizeof(si_iov
));
275 rqst
[num_rqst
].rq_iov
= si_iov
;
276 rqst
[num_rqst
].rq_nvec
= 2;
278 len
= (2 * UniStrnlen((wchar_t *)ptr
, PATH_MAX
));
280 link_info
.ReplaceIfExists
= 0;
281 link_info
.RootDirectory
= 0;
282 link_info
.FileNameLength
= cpu_to_le32(len
);
284 size
[0] = sizeof(struct smb2_file_link_info
);
285 data
[0] = &link_info
;
287 size
[1] = len
+ 2 /* null */;
288 data
[1] = (__le16
*)ptr
;
290 rc
= SMB2_set_info_init(tcon
, &rqst
[num_rqst
], COMPOUND_FID
,
291 COMPOUND_FID
, current
->tgid
,
292 FILE_LINK_INFORMATION
,
293 SMB2_O_INFO_FILE
, 0, data
, size
);
296 smb2_set_next_command(tcon
, &rqst
[num_rqst
]);
297 smb2_set_related(&rqst
[num_rqst
++]);
298 trace_smb3_hardlink_enter(xid
, ses
->Suid
, tcon
->tid
, full_path
);
301 cifs_dbg(VFS
, "Invalid command\n");
307 /* We already have a handle so we can skip the close */
311 memset(&close_iov
, 0, sizeof(close_iov
));
312 rqst
[num_rqst
].rq_iov
= close_iov
;
313 rqst
[num_rqst
].rq_nvec
= 1;
314 rc
= SMB2_close_init(tcon
, &rqst
[num_rqst
], COMPOUND_FID
,
315 COMPOUND_FID
, false);
316 smb2_set_related(&rqst
[num_rqst
]);
323 cifsFileInfo_put(cfile
);
325 rc
= compound_send_recv(xid
, ses
, flags
, num_rqst
- 2,
326 &rqst
[1], &resp_buftype
[1],
329 rc
= compound_send_recv(xid
, ses
, flags
, num_rqst
,
335 cifsFileInfo_put(cfile
);
337 SMB2_open_free(&rqst
[0]);
338 if (rc
== -EREMCHG
) {
339 printk_once(KERN_WARNING
"server share %s deleted\n",
341 tcon
->need_reconnect
= true;
345 case SMB2_OP_QUERY_INFO
:
347 qi_rsp
= (struct smb2_query_info_rsp
*)
349 rc
= smb2_validate_and_copy_iov(
350 le16_to_cpu(qi_rsp
->OutputBufferOffset
),
351 le32_to_cpu(qi_rsp
->OutputBufferLength
),
352 &rsp_iov
[1], sizeof(struct smb2_file_all_info
),
356 SMB2_query_info_free(&rqst
[1]);
358 SMB2_close_free(&rqst
[2]);
360 trace_smb3_query_info_compound_err(xid
, ses
->Suid
,
363 trace_smb3_query_info_compound_done(xid
, ses
->Suid
,
368 trace_smb3_delete_err(xid
, ses
->Suid
, tcon
->tid
, rc
);
370 trace_smb3_delete_done(xid
, ses
->Suid
, tcon
->tid
);
372 SMB2_close_free(&rqst
[1]);
376 trace_smb3_mkdir_err(xid
, ses
->Suid
, tcon
->tid
, rc
);
378 trace_smb3_mkdir_done(xid
, ses
->Suid
, tcon
->tid
);
380 SMB2_close_free(&rqst
[1]);
382 case SMB2_OP_HARDLINK
:
384 trace_smb3_hardlink_err(xid
, ses
->Suid
, tcon
->tid
, rc
);
386 trace_smb3_hardlink_done(xid
, ses
->Suid
, tcon
->tid
);
387 free_set_inf_compound(rqst
);
391 trace_smb3_rename_err(xid
, ses
->Suid
, tcon
->tid
, rc
);
393 trace_smb3_rename_done(xid
, ses
->Suid
, tcon
->tid
);
394 free_set_inf_compound(rqst
);
398 trace_smb3_rmdir_err(xid
, ses
->Suid
, tcon
->tid
, rc
);
400 trace_smb3_rmdir_done(xid
, ses
->Suid
, tcon
->tid
);
401 free_set_inf_compound(rqst
);
403 case SMB2_OP_SET_EOF
:
405 trace_smb3_set_eof_err(xid
, ses
->Suid
, tcon
->tid
, rc
);
407 trace_smb3_set_eof_done(xid
, ses
->Suid
, tcon
->tid
);
408 free_set_inf_compound(rqst
);
410 case SMB2_OP_SET_INFO
:
412 trace_smb3_set_info_compound_err(xid
, ses
->Suid
,
415 trace_smb3_set_info_compound_done(xid
, ses
->Suid
,
417 free_set_inf_compound(rqst
);
420 free_rsp_buf(resp_buftype
[0], rsp_iov
[0].iov_base
);
421 free_rsp_buf(resp_buftype
[1], rsp_iov
[1].iov_base
);
422 free_rsp_buf(resp_buftype
[2], rsp_iov
[2].iov_base
);
427 move_smb2_info_to_cifs(FILE_ALL_INFO
*dst
, struct smb2_file_all_info
*src
)
429 memcpy(dst
, src
, (size_t)(&src
->CurrentByteOffset
) - (size_t)src
);
430 dst
->CurrentByteOffset
= src
->CurrentByteOffset
;
431 dst
->Mode
= src
->Mode
;
432 dst
->AlignmentRequirement
= src
->AlignmentRequirement
;
433 dst
->IndexNumber1
= 0; /* we don't use it */
437 smb2_query_path_info(const unsigned int xid
, struct cifs_tcon
*tcon
,
438 struct cifs_sb_info
*cifs_sb
, const char *full_path
,
439 FILE_ALL_INFO
*data
, bool *adjust_tz
, bool *symlink
)
442 struct smb2_file_all_info
*smb2_data
;
443 __u32 create_options
= 0;
445 bool no_cached_open
= tcon
->nohandlecache
;
446 struct cifsFileInfo
*cfile
;
451 smb2_data
= kzalloc(sizeof(struct smb2_file_all_info
) + PATH_MAX
* 2,
453 if (smb2_data
== NULL
)
456 /* If it is a root and its handle is cached then use it */
457 if (!strlen(full_path
) && !no_cached_open
) {
458 rc
= open_shroot(xid
, tcon
, cifs_sb
, &fid
);
462 if (tcon
->crfid
.file_all_info_is_valid
) {
463 move_smb2_info_to_cifs(data
,
464 &tcon
->crfid
.file_all_info
);
466 rc
= SMB2_query_info(xid
, tcon
, fid
.persistent_fid
,
467 fid
.volatile_fid
, smb2_data
);
469 move_smb2_info_to_cifs(data
, smb2_data
);
471 close_shroot(&tcon
->crfid
);
475 cifs_get_readable_path(tcon
, full_path
, &cfile
);
476 rc
= smb2_compound_op(xid
, tcon
, cifs_sb
, full_path
,
477 FILE_READ_ATTRIBUTES
, FILE_OPEN
, create_options
,
478 ACL_NO_MODE
, smb2_data
, SMB2_OP_QUERY_INFO
, cfile
);
479 if (rc
== -EOPNOTSUPP
) {
481 create_options
|= OPEN_REPARSE_POINT
;
483 /* Failed on a symbolic link - query a reparse point info */
484 rc
= smb2_compound_op(xid
, tcon
, cifs_sb
, full_path
,
485 FILE_READ_ATTRIBUTES
, FILE_OPEN
,
486 create_options
, ACL_NO_MODE
,
487 smb2_data
, SMB2_OP_QUERY_INFO
, NULL
);
492 move_smb2_info_to_cifs(data
, smb2_data
);
499 smb2_mkdir(const unsigned int xid
, struct inode
*parent_inode
, umode_t mode
,
500 struct cifs_tcon
*tcon
, const char *name
,
501 struct cifs_sb_info
*cifs_sb
)
503 return smb2_compound_op(xid
, tcon
, cifs_sb
, name
,
504 FILE_WRITE_ATTRIBUTES
, FILE_CREATE
,
505 CREATE_NOT_FILE
, mode
, NULL
, SMB2_OP_MKDIR
,
510 smb2_mkdir_setinfo(struct inode
*inode
, const char *name
,
511 struct cifs_sb_info
*cifs_sb
, struct cifs_tcon
*tcon
,
512 const unsigned int xid
)
514 FILE_BASIC_INFO data
;
515 struct cifsInodeInfo
*cifs_i
;
516 struct cifsFileInfo
*cfile
;
520 memset(&data
, 0, sizeof(data
));
521 cifs_i
= CIFS_I(inode
);
522 dosattrs
= cifs_i
->cifsAttrs
| ATTR_READONLY
;
523 data
.Attributes
= cpu_to_le32(dosattrs
);
524 cifs_get_writable_path(tcon
, name
, FIND_WR_ANY
, &cfile
);
525 tmprc
= smb2_compound_op(xid
, tcon
, cifs_sb
, name
,
526 FILE_WRITE_ATTRIBUTES
, FILE_CREATE
,
527 CREATE_NOT_FILE
, ACL_NO_MODE
,
528 &data
, SMB2_OP_SET_INFO
, cfile
);
530 cifs_i
->cifsAttrs
= dosattrs
;
534 smb2_rmdir(const unsigned int xid
, struct cifs_tcon
*tcon
, const char *name
,
535 struct cifs_sb_info
*cifs_sb
)
537 return smb2_compound_op(xid
, tcon
, cifs_sb
, name
, DELETE
, FILE_OPEN
,
538 CREATE_NOT_FILE
, ACL_NO_MODE
,
539 NULL
, SMB2_OP_RMDIR
, NULL
);
543 smb2_unlink(const unsigned int xid
, struct cifs_tcon
*tcon
, const char *name
,
544 struct cifs_sb_info
*cifs_sb
)
546 return smb2_compound_op(xid
, tcon
, cifs_sb
, name
, DELETE
, FILE_OPEN
,
547 CREATE_DELETE_ON_CLOSE
| OPEN_REPARSE_POINT
,
548 ACL_NO_MODE
, NULL
, SMB2_OP_DELETE
, NULL
);
552 smb2_set_path_attr(const unsigned int xid
, struct cifs_tcon
*tcon
,
553 const char *from_name
, const char *to_name
,
554 struct cifs_sb_info
*cifs_sb
, __u32 access
, int command
,
555 struct cifsFileInfo
*cfile
)
557 __le16
*smb2_to_name
= NULL
;
560 smb2_to_name
= cifs_convert_path_to_utf16(to_name
, cifs_sb
);
561 if (smb2_to_name
== NULL
) {
563 goto smb2_rename_path
;
565 rc
= smb2_compound_op(xid
, tcon
, cifs_sb
, from_name
, access
,
566 FILE_OPEN
, 0, ACL_NO_MODE
, smb2_to_name
,
574 smb2_rename_path(const unsigned int xid
, struct cifs_tcon
*tcon
,
575 const char *from_name
, const char *to_name
,
576 struct cifs_sb_info
*cifs_sb
)
578 struct cifsFileInfo
*cfile
;
580 cifs_get_writable_path(tcon
, from_name
, FIND_WR_WITH_DELETE
, &cfile
);
582 return smb2_set_path_attr(xid
, tcon
, from_name
, to_name
,
583 cifs_sb
, DELETE
, SMB2_OP_RENAME
, cfile
);
587 smb2_create_hardlink(const unsigned int xid
, struct cifs_tcon
*tcon
,
588 const char *from_name
, const char *to_name
,
589 struct cifs_sb_info
*cifs_sb
)
591 return smb2_set_path_attr(xid
, tcon
, from_name
, to_name
, cifs_sb
,
592 FILE_READ_ATTRIBUTES
, SMB2_OP_HARDLINK
,
597 smb2_set_path_size(const unsigned int xid
, struct cifs_tcon
*tcon
,
598 const char *full_path
, __u64 size
,
599 struct cifs_sb_info
*cifs_sb
, bool set_alloc
)
601 __le64 eof
= cpu_to_le64(size
);
603 return smb2_compound_op(xid
, tcon
, cifs_sb
, full_path
,
604 FILE_WRITE_DATA
, FILE_OPEN
, 0, ACL_NO_MODE
,
605 &eof
, SMB2_OP_SET_EOF
, NULL
);
609 smb2_set_file_info(struct inode
*inode
, const char *full_path
,
610 FILE_BASIC_INFO
*buf
, const unsigned int xid
)
612 struct cifs_sb_info
*cifs_sb
= CIFS_SB(inode
->i_sb
);
613 struct tcon_link
*tlink
;
616 if ((buf
->CreationTime
== 0) && (buf
->LastAccessTime
== 0) &&
617 (buf
->LastWriteTime
== 0) && (buf
->ChangeTime
== 0) &&
618 (buf
->Attributes
== 0))
619 return 0; /* would be a no op, no sense sending this */
621 tlink
= cifs_sb_tlink(cifs_sb
);
623 return PTR_ERR(tlink
);
625 rc
= smb2_compound_op(xid
, tlink_tcon(tlink
), cifs_sb
, full_path
,
626 FILE_WRITE_ATTRIBUTES
, FILE_OPEN
,
627 0, ACL_NO_MODE
, buf
, SMB2_OP_SET_INFO
, NULL
);
628 cifs_put_tlink(tlink
);