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
, void *ptr
, int command
)
57 __le16
*utf16_path
= NULL
;
58 __u8 oplock
= SMB2_OPLOCK_LEVEL_NONE
;
59 struct cifs_open_parms oparms
;
61 struct cifs_ses
*ses
= tcon
->ses
;
63 struct smb_rqst rqst
[3];
65 struct kvec rsp_iov
[3];
66 struct kvec open_iov
[SMB2_CREATE_IOV_SIZE
];
67 struct kvec qi_iov
[1];
68 struct kvec si_iov
[SMB2_SET_INFO_IOV_SIZE
];
69 struct kvec close_iov
[1];
70 struct smb2_query_info_rsp
*qi_rsp
= NULL
;
72 __u8 delete_pending
[8] = {1, 0, 0, 0, 0, 0, 0, 0};
75 struct smb2_file_rename_info rename_info
;
76 struct smb2_file_link_info link_info
;
79 if (smb3_encryption_required(tcon
))
80 flags
|= CIFS_TRANSFORM_REQ
;
82 memset(rqst
, 0, sizeof(rqst
));
83 resp_buftype
[0] = resp_buftype
[1] = resp_buftype
[2] = CIFS_NO_BUFFER
;
84 memset(rsp_iov
, 0, sizeof(rsp_iov
));
87 utf16_path
= cifs_convert_path_to_utf16(full_path
, cifs_sb
);
92 oparms
.desired_access
= desired_access
;
93 oparms
.disposition
= create_disposition
;
94 oparms
.create_options
= create_options
;
95 if (backup_cred(cifs_sb
))
96 oparms
.create_options
|= CREATE_OPEN_BACKUP_INTENT
;
98 oparms
.reconnect
= false;
100 memset(&open_iov
, 0, sizeof(open_iov
));
101 rqst
[num_rqst
].rq_iov
= open_iov
;
102 rqst
[num_rqst
].rq_nvec
= SMB2_CREATE_IOV_SIZE
;
103 rc
= SMB2_open_init(tcon
, &rqst
[num_rqst
], &oplock
, &oparms
,
109 smb2_set_next_command(tcon
, &rqst
[num_rqst
++]);
113 case SMB2_OP_QUERY_INFO
:
114 memset(&qi_iov
, 0, sizeof(qi_iov
));
115 rqst
[num_rqst
].rq_iov
= qi_iov
;
116 rqst
[num_rqst
].rq_nvec
= 1;
118 rc
= SMB2_query_info_init(tcon
, &rqst
[num_rqst
], COMPOUND_FID
,
119 COMPOUND_FID
, FILE_ALL_INFORMATION
,
121 sizeof(struct smb2_file_all_info
) +
122 PATH_MAX
* 2, 0, NULL
);
123 smb2_set_next_command(tcon
, &rqst
[num_rqst
]);
124 smb2_set_related(&rqst
[num_rqst
++]);
125 trace_smb3_query_info_compound_enter(xid
, ses
->Suid
, tcon
->tid
,
129 trace_smb3_delete_enter(xid
, ses
->Suid
, tcon
->tid
, full_path
);
133 * Directories are created through parameters in the
136 trace_smb3_mkdir_enter(xid
, ses
->Suid
, tcon
->tid
, full_path
);
139 memset(&si_iov
, 0, sizeof(si_iov
));
140 rqst
[num_rqst
].rq_iov
= si_iov
;
141 rqst
[num_rqst
].rq_nvec
= 1;
143 size
[0] = 1; /* sizeof __u8 See MS-FSCC section 2.4.11 */
144 data
[0] = &delete_pending
[0];
146 rc
= SMB2_set_info_init(tcon
, &rqst
[num_rqst
], COMPOUND_FID
,
147 COMPOUND_FID
, current
->tgid
,
148 FILE_DISPOSITION_INFORMATION
,
149 SMB2_O_INFO_FILE
, 0, data
, size
);
150 smb2_set_next_command(tcon
, &rqst
[num_rqst
]);
151 smb2_set_related(&rqst
[num_rqst
++]);
152 trace_smb3_rmdir_enter(xid
, ses
->Suid
, tcon
->tid
, full_path
);
154 case SMB2_OP_SET_EOF
:
155 memset(&si_iov
, 0, sizeof(si_iov
));
156 rqst
[num_rqst
].rq_iov
= si_iov
;
157 rqst
[num_rqst
].rq_nvec
= 1;
159 size
[0] = 8; /* sizeof __le64 */
162 rc
= SMB2_set_info_init(tcon
, &rqst
[num_rqst
], COMPOUND_FID
,
163 COMPOUND_FID
, current
->tgid
,
164 FILE_END_OF_FILE_INFORMATION
,
165 SMB2_O_INFO_FILE
, 0, data
, size
);
166 smb2_set_next_command(tcon
, &rqst
[num_rqst
]);
167 smb2_set_related(&rqst
[num_rqst
++]);
168 trace_smb3_set_eof_enter(xid
, ses
->Suid
, tcon
->tid
, full_path
);
170 case SMB2_OP_SET_INFO
:
171 memset(&si_iov
, 0, sizeof(si_iov
));
172 rqst
[num_rqst
].rq_iov
= si_iov
;
173 rqst
[num_rqst
].rq_nvec
= 1;
176 size
[0] = sizeof(FILE_BASIC_INFO
);
179 rc
= SMB2_set_info_init(tcon
, &rqst
[num_rqst
], COMPOUND_FID
,
180 COMPOUND_FID
, current
->tgid
,
181 FILE_BASIC_INFORMATION
,
182 SMB2_O_INFO_FILE
, 0, data
, size
);
183 smb2_set_next_command(tcon
, &rqst
[num_rqst
]);
184 smb2_set_related(&rqst
[num_rqst
++]);
185 trace_smb3_set_info_compound_enter(xid
, ses
->Suid
, tcon
->tid
,
189 memset(&si_iov
, 0, sizeof(si_iov
));
190 rqst
[num_rqst
].rq_iov
= si_iov
;
191 rqst
[num_rqst
].rq_nvec
= 2;
193 len
= (2 * UniStrnlen((wchar_t *)ptr
, PATH_MAX
));
195 rename_info
.ReplaceIfExists
= 1;
196 rename_info
.RootDirectory
= 0;
197 rename_info
.FileNameLength
= cpu_to_le32(len
);
199 size
[0] = sizeof(struct smb2_file_rename_info
);
200 data
[0] = &rename_info
;
202 size
[1] = len
+ 2 /* null */;
203 data
[1] = (__le16
*)ptr
;
205 rc
= SMB2_set_info_init(tcon
, &rqst
[num_rqst
], COMPOUND_FID
,
206 COMPOUND_FID
, current
->tgid
,
207 FILE_RENAME_INFORMATION
,
208 SMB2_O_INFO_FILE
, 0, data
, size
);
209 smb2_set_next_command(tcon
, &rqst
[num_rqst
]);
210 smb2_set_related(&rqst
[num_rqst
++]);
211 trace_smb3_rename_enter(xid
, ses
->Suid
, tcon
->tid
, full_path
);
213 case SMB2_OP_HARDLINK
:
214 memset(&si_iov
, 0, sizeof(si_iov
));
215 rqst
[num_rqst
].rq_iov
= si_iov
;
216 rqst
[num_rqst
].rq_nvec
= 2;
218 len
= (2 * UniStrnlen((wchar_t *)ptr
, PATH_MAX
));
220 link_info
.ReplaceIfExists
= 0;
221 link_info
.RootDirectory
= 0;
222 link_info
.FileNameLength
= cpu_to_le32(len
);
224 size
[0] = sizeof(struct smb2_file_link_info
);
225 data
[0] = &link_info
;
227 size
[1] = len
+ 2 /* null */;
228 data
[1] = (__le16
*)ptr
;
230 rc
= SMB2_set_info_init(tcon
, &rqst
[num_rqst
], COMPOUND_FID
,
231 COMPOUND_FID
, current
->tgid
,
232 FILE_LINK_INFORMATION
,
233 SMB2_O_INFO_FILE
, 0, data
, size
);
234 smb2_set_next_command(tcon
, &rqst
[num_rqst
]);
235 smb2_set_related(&rqst
[num_rqst
++]);
236 trace_smb3_hardlink_enter(xid
, ses
->Suid
, tcon
->tid
, full_path
);
239 cifs_dbg(VFS
, "Invalid command\n");
246 memset(&close_iov
, 0, sizeof(close_iov
));
247 rqst
[num_rqst
].rq_iov
= close_iov
;
248 rqst
[num_rqst
].rq_nvec
= 1;
249 rc
= SMB2_close_init(tcon
, &rqst
[num_rqst
], COMPOUND_FID
,
251 smb2_set_related(&rqst
[num_rqst
++]);
255 rc
= compound_send_recv(xid
, ses
, flags
, num_rqst
, rqst
,
256 resp_buftype
, rsp_iov
);
259 SMB2_open_free(&rqst
[0]);
261 case SMB2_OP_QUERY_INFO
:
263 qi_rsp
= (struct smb2_query_info_rsp
*)
265 rc
= smb2_validate_and_copy_iov(
266 le16_to_cpu(qi_rsp
->OutputBufferOffset
),
267 le32_to_cpu(qi_rsp
->OutputBufferLength
),
268 &rsp_iov
[1], sizeof(struct smb2_file_all_info
),
272 SMB2_query_info_free(&rqst
[1]);
274 SMB2_close_free(&rqst
[2]);
276 trace_smb3_query_info_compound_err(xid
, ses
->Suid
,
279 trace_smb3_query_info_compound_done(xid
, ses
->Suid
,
284 trace_smb3_delete_err(xid
, ses
->Suid
, tcon
->tid
, rc
);
286 trace_smb3_delete_done(xid
, ses
->Suid
, tcon
->tid
);
288 SMB2_close_free(&rqst
[1]);
292 trace_smb3_mkdir_err(xid
, ses
->Suid
, tcon
->tid
, rc
);
294 trace_smb3_mkdir_done(xid
, ses
->Suid
, tcon
->tid
);
296 SMB2_close_free(&rqst
[1]);
298 case SMB2_OP_HARDLINK
:
300 trace_smb3_hardlink_err(xid
, ses
->Suid
, tcon
->tid
, rc
);
302 trace_smb3_hardlink_done(xid
, ses
->Suid
, tcon
->tid
);
303 free_set_inf_compound(rqst
);
307 trace_smb3_rename_err(xid
, ses
->Suid
, tcon
->tid
, rc
);
309 trace_smb3_rename_done(xid
, ses
->Suid
, tcon
->tid
);
310 free_set_inf_compound(rqst
);
314 trace_smb3_rmdir_err(xid
, ses
->Suid
, tcon
->tid
, rc
);
316 trace_smb3_rmdir_done(xid
, ses
->Suid
, tcon
->tid
);
317 free_set_inf_compound(rqst
);
319 case SMB2_OP_SET_EOF
:
321 trace_smb3_set_eof_err(xid
, ses
->Suid
, tcon
->tid
, rc
);
323 trace_smb3_set_eof_done(xid
, ses
->Suid
, tcon
->tid
);
324 free_set_inf_compound(rqst
);
326 case SMB2_OP_SET_INFO
:
328 trace_smb3_set_info_compound_err(xid
, ses
->Suid
,
331 trace_smb3_set_info_compound_done(xid
, ses
->Suid
,
333 free_set_inf_compound(rqst
);
336 free_rsp_buf(resp_buftype
[0], rsp_iov
[0].iov_base
);
337 free_rsp_buf(resp_buftype
[1], rsp_iov
[1].iov_base
);
338 free_rsp_buf(resp_buftype
[2], rsp_iov
[2].iov_base
);
343 move_smb2_info_to_cifs(FILE_ALL_INFO
*dst
, struct smb2_file_all_info
*src
)
345 memcpy(dst
, src
, (size_t)(&src
->CurrentByteOffset
) - (size_t)src
);
346 dst
->CurrentByteOffset
= src
->CurrentByteOffset
;
347 dst
->Mode
= src
->Mode
;
348 dst
->AlignmentRequirement
= src
->AlignmentRequirement
;
349 dst
->IndexNumber1
= 0; /* we don't use it */
353 smb2_query_path_info(const unsigned int xid
, struct cifs_tcon
*tcon
,
354 struct cifs_sb_info
*cifs_sb
, const char *full_path
,
355 FILE_ALL_INFO
*data
, bool *adjust_tz
, bool *symlink
)
358 struct smb2_file_all_info
*smb2_data
;
359 __u32 create_options
= 0;
361 bool no_cached_open
= tcon
->nohandlecache
;
366 smb2_data
= kzalloc(sizeof(struct smb2_file_all_info
) + PATH_MAX
* 2,
368 if (smb2_data
== NULL
)
371 /* If it is a root and its handle is cached then use it */
372 if (!strlen(full_path
) && !no_cached_open
) {
373 rc
= open_shroot(xid
, tcon
, &fid
);
377 if (tcon
->crfid
.file_all_info_is_valid
) {
378 move_smb2_info_to_cifs(data
,
379 &tcon
->crfid
.file_all_info
);
381 rc
= SMB2_query_info(xid
, tcon
, fid
.persistent_fid
,
382 fid
.volatile_fid
, smb2_data
);
384 move_smb2_info_to_cifs(data
, smb2_data
);
386 close_shroot(&tcon
->crfid
);
390 if (backup_cred(cifs_sb
))
391 create_options
|= CREATE_OPEN_BACKUP_INTENT
;
393 rc
= smb2_compound_op(xid
, tcon
, cifs_sb
, full_path
,
394 FILE_READ_ATTRIBUTES
, FILE_OPEN
, create_options
,
395 smb2_data
, SMB2_OP_QUERY_INFO
);
396 if (rc
== -EOPNOTSUPP
) {
398 create_options
|= OPEN_REPARSE_POINT
;
400 /* Failed on a symbolic link - query a reparse point info */
401 rc
= smb2_compound_op(xid
, tcon
, cifs_sb
, full_path
,
402 FILE_READ_ATTRIBUTES
, FILE_OPEN
,
403 create_options
, smb2_data
,
409 move_smb2_info_to_cifs(data
, smb2_data
);
416 smb2_mkdir(const unsigned int xid
, struct cifs_tcon
*tcon
, const char *name
,
417 struct cifs_sb_info
*cifs_sb
)
419 return smb2_compound_op(xid
, tcon
, cifs_sb
, name
,
420 FILE_WRITE_ATTRIBUTES
, FILE_CREATE
,
421 CREATE_NOT_FILE
, NULL
, SMB2_OP_MKDIR
);
425 smb2_mkdir_setinfo(struct inode
*inode
, const char *name
,
426 struct cifs_sb_info
*cifs_sb
, struct cifs_tcon
*tcon
,
427 const unsigned int xid
)
429 FILE_BASIC_INFO data
;
430 struct cifsInodeInfo
*cifs_i
;
434 memset(&data
, 0, sizeof(data
));
435 cifs_i
= CIFS_I(inode
);
436 dosattrs
= cifs_i
->cifsAttrs
| ATTR_READONLY
;
437 data
.Attributes
= cpu_to_le32(dosattrs
);
438 tmprc
= smb2_compound_op(xid
, tcon
, cifs_sb
, name
,
439 FILE_WRITE_ATTRIBUTES
, FILE_CREATE
,
440 CREATE_NOT_FILE
, &data
, SMB2_OP_SET_INFO
);
442 cifs_i
->cifsAttrs
= dosattrs
;
446 smb2_rmdir(const unsigned int xid
, struct cifs_tcon
*tcon
, const char *name
,
447 struct cifs_sb_info
*cifs_sb
)
449 return smb2_compound_op(xid
, tcon
, cifs_sb
, name
, DELETE
, FILE_OPEN
,
451 NULL
, SMB2_OP_RMDIR
);
455 smb2_unlink(const unsigned int xid
, struct cifs_tcon
*tcon
, const char *name
,
456 struct cifs_sb_info
*cifs_sb
)
458 return smb2_compound_op(xid
, tcon
, cifs_sb
, name
, DELETE
, FILE_OPEN
,
459 CREATE_DELETE_ON_CLOSE
| OPEN_REPARSE_POINT
,
460 NULL
, SMB2_OP_DELETE
);
464 smb2_set_path_attr(const unsigned int xid
, struct cifs_tcon
*tcon
,
465 const char *from_name
, const char *to_name
,
466 struct cifs_sb_info
*cifs_sb
, __u32 access
, int command
)
468 __le16
*smb2_to_name
= NULL
;
471 smb2_to_name
= cifs_convert_path_to_utf16(to_name
, cifs_sb
);
472 if (smb2_to_name
== NULL
) {
474 goto smb2_rename_path
;
476 rc
= smb2_compound_op(xid
, tcon
, cifs_sb
, from_name
, access
,
477 FILE_OPEN
, 0, smb2_to_name
, command
);
484 smb2_rename_path(const unsigned int xid
, struct cifs_tcon
*tcon
,
485 const char *from_name
, const char *to_name
,
486 struct cifs_sb_info
*cifs_sb
)
488 return smb2_set_path_attr(xid
, tcon
, from_name
, to_name
, cifs_sb
,
489 DELETE
, SMB2_OP_RENAME
);
493 smb2_create_hardlink(const unsigned int xid
, struct cifs_tcon
*tcon
,
494 const char *from_name
, const char *to_name
,
495 struct cifs_sb_info
*cifs_sb
)
497 return smb2_set_path_attr(xid
, tcon
, from_name
, to_name
, cifs_sb
,
498 FILE_READ_ATTRIBUTES
, SMB2_OP_HARDLINK
);
502 smb2_set_path_size(const unsigned int xid
, struct cifs_tcon
*tcon
,
503 const char *full_path
, __u64 size
,
504 struct cifs_sb_info
*cifs_sb
, bool set_alloc
)
506 __le64 eof
= cpu_to_le64(size
);
508 return smb2_compound_op(xid
, tcon
, cifs_sb
, full_path
,
509 FILE_WRITE_DATA
, FILE_OPEN
, 0, &eof
,
514 smb2_set_file_info(struct inode
*inode
, const char *full_path
,
515 FILE_BASIC_INFO
*buf
, const unsigned int xid
)
517 struct cifs_sb_info
*cifs_sb
= CIFS_SB(inode
->i_sb
);
518 struct tcon_link
*tlink
;
521 if ((buf
->CreationTime
== 0) && (buf
->LastAccessTime
== 0) &&
522 (buf
->LastWriteTime
== 0) && (buf
->ChangeTime
== 0) &&
523 (buf
->Attributes
== 0))
524 return 0; /* would be a no op, no sense sending this */
526 tlink
= cifs_sb_tlink(cifs_sb
);
528 return PTR_ERR(tlink
);
530 rc
= smb2_compound_op(xid
, tlink_tcon(tlink
), cifs_sb
, full_path
,
531 FILE_WRITE_ATTRIBUTES
, FILE_OPEN
, 0, buf
,
533 cifs_put_tlink(tlink
);