blob: 9841df771f08eda368027b1737783e15f677a77c [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;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +040047 __le16 *utf16_path;
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;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +040051
52 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
53 if (!utf16_path)
54 return -ENOMEM;
55
Pavel Shilovsky064f6042013-07-09 18:20:30 +040056 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
62 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +040063 if (rc) {
64 kfree(utf16_path);
65 return rc;
66 }
67
68 switch (command) {
69 case SMB2_OP_DELETE:
70 break;
71 case SMB2_OP_QUERY_INFO:
Pavel Shilovsky064f6042013-07-09 18:20:30 +040072 tmprc = SMB2_query_info(xid, tcon, fid.persistent_fid,
73 fid.volatile_fid,
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +040074 (struct smb2_file_all_info *)data);
75 break;
76 case SMB2_OP_MKDIR:
77 /*
78 * Directories are created through parameters in the
79 * SMB2_open() call.
80 */
81 break;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -070082 case SMB2_OP_RENAME:
Pavel Shilovsky064f6042013-07-09 18:20:30 +040083 tmprc = SMB2_rename(xid, tcon, fid.persistent_fid,
84 fid.volatile_fid, (__le16 *)data);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -070085 break;
Pavel Shilovsky568798c2012-09-18 16:20:31 -070086 case SMB2_OP_HARDLINK:
Pavel Shilovsky064f6042013-07-09 18:20:30 +040087 tmprc = SMB2_set_hardlink(xid, tcon, fid.persistent_fid,
88 fid.volatile_fid, (__le16 *)data);
Pavel Shilovsky568798c2012-09-18 16:20:31 -070089 break;
Pavel Shilovskyc839ff22012-09-18 16:20:32 -070090 case SMB2_OP_SET_EOF:
Pavel Shilovsky064f6042013-07-09 18:20:30 +040091 tmprc = SMB2_set_eof(xid, tcon, fid.persistent_fid,
92 fid.volatile_fid, current->tgid,
93 (__le64 *)data);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -070094 break;
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -070095 case SMB2_OP_SET_INFO:
Pavel Shilovsky064f6042013-07-09 18:20:30 +040096 tmprc = SMB2_set_info(xid, tcon, fid.persistent_fid,
97 fid.volatile_fid,
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -070098 (FILE_BASIC_INFO *)data);
99 break;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +0400100 default:
Joe Perchesf96637b2013-05-04 22:12:25 -0500101 cifs_dbg(VFS, "Invalid command\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +0400102 break;
103 }
104
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400105 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +0400106 if (tmprc)
107 rc = tmprc;
108 kfree(utf16_path);
109 return rc;
110}
111
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700112void
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +0400113move_smb2_info_to_cifs(FILE_ALL_INFO *dst, struct smb2_file_all_info *src)
114{
115 memcpy(dst, src, (size_t)(&src->CurrentByteOffset) - (size_t)src);
116 dst->CurrentByteOffset = src->CurrentByteOffset;
117 dst->Mode = src->Mode;
118 dst->AlignmentRequirement = src->AlignmentRequirement;
119 dst->IndexNumber1 = 0; /* we don't use it */
120}
121
122int
123smb2_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
124 struct cifs_sb_info *cifs_sb, const char *full_path,
125 FILE_ALL_INFO *data, bool *adjust_tz)
126{
127 int rc;
128 struct smb2_file_all_info *smb2_data;
129
130 *adjust_tz = false;
131
132 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + MAX_NAME * 2,
133 GFP_KERNEL);
134 if (smb2_data == NULL)
135 return -ENOMEM;
136
137 rc = smb2_open_op_close(xid, tcon, cifs_sb, full_path,
Pavel Shilovskyca819832013-07-05 12:21:26 +0400138 FILE_READ_ATTRIBUTES, FILE_OPEN, 0, smb2_data,
139 SMB2_OP_QUERY_INFO);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +0400140 if (rc)
141 goto out;
142
143 move_smb2_info_to_cifs(data, smb2_data);
144out:
145 kfree(smb2_data);
146 return rc;
147}
Pavel Shilovskya0e73182011-07-19 12:56:37 +0400148
149int
150smb2_mkdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
151 struct cifs_sb_info *cifs_sb)
152{
153 return smb2_open_op_close(xid, tcon, cifs_sb, name,
Pavel Shilovskyca819832013-07-05 12:21:26 +0400154 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
Pavel Shilovskya0e73182011-07-19 12:56:37 +0400155 CREATE_NOT_FILE, NULL, SMB2_OP_MKDIR);
156}
157
158void
159smb2_mkdir_setinfo(struct inode *inode, const char *name,
160 struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
161 const unsigned int xid)
162{
163 FILE_BASIC_INFO data;
164 struct cifsInodeInfo *cifs_i;
165 u32 dosattrs;
166 int tmprc;
167
168 memset(&data, 0, sizeof(data));
169 cifs_i = CIFS_I(inode);
170 dosattrs = cifs_i->cifsAttrs | ATTR_READONLY;
171 data.Attributes = cpu_to_le32(dosattrs);
172 tmprc = smb2_open_op_close(xid, tcon, cifs_sb, name,
Pavel Shilovskyca819832013-07-05 12:21:26 +0400173 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
Pavel Shilovskya0e73182011-07-19 12:56:37 +0400174 CREATE_NOT_FILE, &data, SMB2_OP_SET_INFO);
175 if (tmprc == 0)
176 cifs_i->cifsAttrs = dosattrs;
177}
Pavel Shilovsky1a500f02012-07-10 16:14:38 +0400178
179int
180smb2_rmdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
181 struct cifs_sb_info *cifs_sb)
182{
183 return smb2_open_op_close(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
Pavel Shilovskyca819832013-07-05 12:21:26 +0400184 CREATE_NOT_FILE | CREATE_DELETE_ON_CLOSE,
Pavel Shilovsky1a500f02012-07-10 16:14:38 +0400185 NULL, SMB2_OP_DELETE);
186}
Pavel Shilovskycbe6f432012-09-18 16:20:25 -0700187
188int
189smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
190 struct cifs_sb_info *cifs_sb)
191{
192 return smb2_open_op_close(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
Pavel Shilovskyca819832013-07-05 12:21:26 +0400193 CREATE_DELETE_ON_CLOSE, NULL,
Pavel Shilovskycbe6f432012-09-18 16:20:25 -0700194 SMB2_OP_DELETE);
195}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -0700196
Pavel Shilovsky568798c2012-09-18 16:20:31 -0700197static int
198smb2_set_path_attr(const unsigned int xid, struct cifs_tcon *tcon,
199 const char *from_name, const char *to_name,
200 struct cifs_sb_info *cifs_sb, __u32 access, int command)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -0700201{
202 __le16 *smb2_to_name = NULL;
203 int rc;
204
205 smb2_to_name = cifs_convert_path_to_utf16(to_name, cifs_sb);
206 if (smb2_to_name == NULL) {
207 rc = -ENOMEM;
208 goto smb2_rename_path;
209 }
210
Pavel Shilovsky568798c2012-09-18 16:20:31 -0700211 rc = smb2_open_op_close(xid, tcon, cifs_sb, from_name, access,
Pavel Shilovskyca819832013-07-05 12:21:26 +0400212 FILE_OPEN, 0, smb2_to_name, command);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -0700213smb2_rename_path:
214 kfree(smb2_to_name);
215 return rc;
216}
Pavel Shilovsky568798c2012-09-18 16:20:31 -0700217
218int
219smb2_rename_path(const unsigned int xid, struct cifs_tcon *tcon,
220 const char *from_name, const char *to_name,
221 struct cifs_sb_info *cifs_sb)
222{
223 return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
224 DELETE, SMB2_OP_RENAME);
225}
226
227int
228smb2_create_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
229 const char *from_name, const char *to_name,
230 struct cifs_sb_info *cifs_sb)
231{
232 return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
233 FILE_READ_ATTRIBUTES, SMB2_OP_HARDLINK);
234}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -0700235
236int
237smb2_set_path_size(const unsigned int xid, struct cifs_tcon *tcon,
238 const char *full_path, __u64 size,
239 struct cifs_sb_info *cifs_sb, bool set_alloc)
240{
241 __le64 eof = cpu_to_le64(size);
242 return smb2_open_op_close(xid, tcon, cifs_sb, full_path,
Pavel Shilovskyca819832013-07-05 12:21:26 +0400243 FILE_WRITE_DATA, FILE_OPEN, 0, &eof,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -0700244 SMB2_OP_SET_EOF);
245}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -0700246
247int
248smb2_set_file_info(struct inode *inode, const char *full_path,
249 FILE_BASIC_INFO *buf, const unsigned int xid)
250{
251 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
252 struct tcon_link *tlink;
253 int rc;
254
255 tlink = cifs_sb_tlink(cifs_sb);
256 if (IS_ERR(tlink))
257 return PTR_ERR(tlink);
258 rc = smb2_open_op_close(xid, tlink_tcon(tlink), cifs_sb, full_path,
Pavel Shilovskyca819832013-07-05 12:21:26 +0400259 FILE_WRITE_ATTRIBUTES, FILE_OPEN, 0, buf,
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -0700260 SMB2_OP_SET_INFO);
261 cifs_put_tlink(tlink);
262 return rc;
263}