blob: d01ad706d7fc9a4db4f60e1e7e60ad9c7ba757ce [file] [log] [blame]
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001/*
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,
Pavel Shilovskyca819832013-07-05 12:21:26 +040044 __u32 create_options, void *data, int command)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +040045{
46 int rc, tmprc = 0;
Steve French3d4ef9a2018-04-25 22:19:09 -050047 __le16 *utf16_path = NULL;
Pavel Shilovsky2e44b282012-09-18 16:20:33 -070048 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +040049 struct cifs_open_parms oparms;
50 struct cifs_fid fid;
Steve French3d4ef9a2018-04-25 22:19:09 -050051 bool use_cached_root_handle = false;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +040052
Steve French3d4ef9a2018-04-25 22:19:09 -050053 if ((strcmp(full_path, "") == 0) && (create_options == 0) &&
54 (desired_access == FILE_READ_ATTRIBUTES) &&
55 (create_disposition == FILE_OPEN) &&
56 (tcon->nohandlecache == false)) {
57 rc = open_shroot(xid, tcon, &fid);
58 if (rc == 0)
59 use_cached_root_handle = true;
60 }
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +040061
Steve French3d4ef9a2018-04-25 22:19:09 -050062 if (use_cached_root_handle == false) {
63 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
64 if (!utf16_path)
65 return -ENOMEM;
Pavel Shilovsky064f6042013-07-09 18:20:30 +040066
Steve French3d4ef9a2018-04-25 22:19:09 -050067 oparms.tcon = tcon;
68 oparms.desired_access = desired_access;
69 oparms.disposition = create_disposition;
70 oparms.create_options = create_options;
71 oparms.fid = &fid;
72 oparms.reconnect = false;
73
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +100074 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL,
75 NULL);
Steve French3d4ef9a2018-04-25 22:19:09 -050076 if (rc) {
77 kfree(utf16_path);
78 return rc;
79 }
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +040080 }
81
82 switch (command) {
83 case SMB2_OP_DELETE:
84 break;
85 case SMB2_OP_QUERY_INFO:
Pavel Shilovsky064f6042013-07-09 18:20:30 +040086 tmprc = SMB2_query_info(xid, tcon, fid.persistent_fid,
87 fid.volatile_fid,
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +040088 (struct smb2_file_all_info *)data);
89 break;
90 case SMB2_OP_MKDIR:
91 /*
92 * Directories are created through parameters in the
93 * SMB2_open() call.
94 */
95 break;
Steve French897fba12016-05-12 21:20:36 -050096 case SMB2_OP_RMDIR:
97 tmprc = SMB2_rmdir(xid, tcon, fid.persistent_fid,
98 fid.volatile_fid);
99 break;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -0700100 case SMB2_OP_RENAME:
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400101 tmprc = SMB2_rename(xid, tcon, fid.persistent_fid,
102 fid.volatile_fid, (__le16 *)data);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -0700103 break;
Pavel Shilovsky568798c2012-09-18 16:20:31 -0700104 case SMB2_OP_HARDLINK:
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400105 tmprc = SMB2_set_hardlink(xid, tcon, fid.persistent_fid,
106 fid.volatile_fid, (__le16 *)data);
Pavel Shilovsky568798c2012-09-18 16:20:31 -0700107 break;
Pavel Shilovskyc839ff22012-09-18 16:20:32 -0700108 case SMB2_OP_SET_EOF:
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400109 tmprc = SMB2_set_eof(xid, tcon, fid.persistent_fid,
110 fid.volatile_fid, current->tgid,
Steve Frenchf29ebb42014-07-19 21:44:58 -0500111 (__le64 *)data, false);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -0700112 break;
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -0700113 case SMB2_OP_SET_INFO:
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400114 tmprc = SMB2_set_info(xid, tcon, fid.persistent_fid,
115 fid.volatile_fid,
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -0700116 (FILE_BASIC_INFO *)data);
117 break;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +0400118 default:
Joe Perchesf96637b2013-05-04 22:12:25 -0500119 cifs_dbg(VFS, "Invalid command\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +0400120 break;
121 }
122
Steve French3d4ef9a2018-04-25 22:19:09 -0500123 if (use_cached_root_handle == false)
124 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +0400125 if (tmprc)
126 rc = tmprc;
127 kfree(utf16_path);
128 return rc;
129}
130
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700131void
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +0400132move_smb2_info_to_cifs(FILE_ALL_INFO *dst, struct smb2_file_all_info *src)
133{
134 memcpy(dst, src, (size_t)(&src->CurrentByteOffset) - (size_t)src);
135 dst->CurrentByteOffset = src->CurrentByteOffset;
136 dst->Mode = src->Mode;
137 dst->AlignmentRequirement = src->AlignmentRequirement;
138 dst->IndexNumber1 = 0; /* we don't use it */
139}
140
141int
142smb2_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
143 struct cifs_sb_info *cifs_sb, const char *full_path,
Pavel Shilovskyeb85d94b2013-10-23 17:49:47 +0400144 FILE_ALL_INFO *data, bool *adjust_tz, bool *symlink)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +0400145{
146 int rc;
147 struct smb2_file_all_info *smb2_data;
148
149 *adjust_tz = false;
Pavel Shilovskyeb85d94b2013-10-23 17:49:47 +0400150 *symlink = false;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +0400151
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +0400152 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +0400153 GFP_KERNEL);
154 if (smb2_data == NULL)
155 return -ENOMEM;
156
157 rc = smb2_open_op_close(xid, tcon, cifs_sb, full_path,
Pavel Shilovskyeb85d94b2013-10-23 17:49:47 +0400158 FILE_READ_ATTRIBUTES, FILE_OPEN, 0,
159 smb2_data, SMB2_OP_QUERY_INFO);
160 if (rc == -EOPNOTSUPP) {
161 *symlink = true;
162 /* Failed on a symbolic link - query a reparse point info */
163 rc = smb2_open_op_close(xid, tcon, cifs_sb, full_path,
164 FILE_READ_ATTRIBUTES, FILE_OPEN,
165 OPEN_REPARSE_POINT, smb2_data,
166 SMB2_OP_QUERY_INFO);
167 }
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +0400168 if (rc)
169 goto out;
170
171 move_smb2_info_to_cifs(data, smb2_data);
172out:
173 kfree(smb2_data);
174 return rc;
175}
Pavel Shilovskya0e73182011-07-19 12:56:37 +0400176
177int
178smb2_mkdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
179 struct cifs_sb_info *cifs_sb)
180{
181 return smb2_open_op_close(xid, tcon, cifs_sb, name,
Pavel Shilovskyca819832013-07-05 12:21:26 +0400182 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
Pavel Shilovskya0e73182011-07-19 12:56:37 +0400183 CREATE_NOT_FILE, NULL, SMB2_OP_MKDIR);
184}
185
186void
187smb2_mkdir_setinfo(struct inode *inode, const char *name,
188 struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
189 const unsigned int xid)
190{
191 FILE_BASIC_INFO data;
192 struct cifsInodeInfo *cifs_i;
193 u32 dosattrs;
194 int tmprc;
195
196 memset(&data, 0, sizeof(data));
197 cifs_i = CIFS_I(inode);
198 dosattrs = cifs_i->cifsAttrs | ATTR_READONLY;
199 data.Attributes = cpu_to_le32(dosattrs);
200 tmprc = smb2_open_op_close(xid, tcon, cifs_sb, name,
Pavel Shilovskyca819832013-07-05 12:21:26 +0400201 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
Pavel Shilovskya0e73182011-07-19 12:56:37 +0400202 CREATE_NOT_FILE, &data, SMB2_OP_SET_INFO);
203 if (tmprc == 0)
204 cifs_i->cifsAttrs = dosattrs;
205}
Pavel Shilovsky1a500f02012-07-10 16:14:38 +0400206
207int
208smb2_rmdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
209 struct cifs_sb_info *cifs_sb)
210{
211 return smb2_open_op_close(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
Steve French897fba12016-05-12 21:20:36 -0500212 CREATE_NOT_FILE,
213 NULL, SMB2_OP_RMDIR);
Pavel Shilovsky1a500f02012-07-10 16:14:38 +0400214}
Pavel Shilovskycbe6f432012-09-18 16:20:25 -0700215
216int
217smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
218 struct cifs_sb_info *cifs_sb)
219{
220 return smb2_open_op_close(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +0400221 CREATE_DELETE_ON_CLOSE | OPEN_REPARSE_POINT,
222 NULL, SMB2_OP_DELETE);
Pavel Shilovskycbe6f432012-09-18 16:20:25 -0700223}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -0700224
Pavel Shilovsky568798c2012-09-18 16:20:31 -0700225static int
226smb2_set_path_attr(const unsigned int xid, struct cifs_tcon *tcon,
227 const char *from_name, const char *to_name,
228 struct cifs_sb_info *cifs_sb, __u32 access, int command)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -0700229{
230 __le16 *smb2_to_name = NULL;
231 int rc;
232
233 smb2_to_name = cifs_convert_path_to_utf16(to_name, cifs_sb);
234 if (smb2_to_name == NULL) {
235 rc = -ENOMEM;
236 goto smb2_rename_path;
237 }
238
Pavel Shilovsky568798c2012-09-18 16:20:31 -0700239 rc = smb2_open_op_close(xid, tcon, cifs_sb, from_name, access,
Pavel Shilovskyca819832013-07-05 12:21:26 +0400240 FILE_OPEN, 0, smb2_to_name, command);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -0700241smb2_rename_path:
242 kfree(smb2_to_name);
243 return rc;
244}
Pavel Shilovsky568798c2012-09-18 16:20:31 -0700245
246int
247smb2_rename_path(const unsigned int xid, struct cifs_tcon *tcon,
248 const char *from_name, const char *to_name,
249 struct cifs_sb_info *cifs_sb)
250{
251 return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
252 DELETE, SMB2_OP_RENAME);
253}
254
255int
256smb2_create_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
257 const char *from_name, const char *to_name,
258 struct cifs_sb_info *cifs_sb)
259{
260 return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
261 FILE_READ_ATTRIBUTES, SMB2_OP_HARDLINK);
262}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -0700263
264int
265smb2_set_path_size(const unsigned int xid, struct cifs_tcon *tcon,
266 const char *full_path, __u64 size,
267 struct cifs_sb_info *cifs_sb, bool set_alloc)
268{
269 __le64 eof = cpu_to_le64(size);
270 return smb2_open_op_close(xid, tcon, cifs_sb, full_path,
Pavel Shilovskyca819832013-07-05 12:21:26 +0400271 FILE_WRITE_DATA, FILE_OPEN, 0, &eof,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -0700272 SMB2_OP_SET_EOF);
273}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -0700274
275int
276smb2_set_file_info(struct inode *inode, const char *full_path,
277 FILE_BASIC_INFO *buf, const unsigned int xid)
278{
279 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
280 struct tcon_link *tlink;
281 int rc;
282
Steve French18dd8e12016-09-26 14:23:08 -0500283 if ((buf->CreationTime == 0) && (buf->LastAccessTime == 0) &&
284 (buf->LastWriteTime == 0) && (buf->ChangeTime) &&
285 (buf->Attributes == 0))
286 return 0; /* would be a no op, no sense sending this */
287
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -0700288 tlink = cifs_sb_tlink(cifs_sb);
289 if (IS_ERR(tlink))
290 return PTR_ERR(tlink);
Steve French18dd8e12016-09-26 14:23:08 -0500291
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -0700292 rc = smb2_open_op_close(xid, tlink_tcon(tlink), cifs_sb, full_path,
Pavel Shilovskyca819832013-07-05 12:21:26 +0400293 FILE_WRITE_ATTRIBUTES, FILE_OPEN, 0, buf,
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -0700294 SMB2_OP_SET_INFO);
295 cifs_put_tlink(tlink);
296 return rc;
297}