CIFS: Add set_file_info support for SMB2
[linux-3.10.git] / fs / cifs / smb2inode.c
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
40 static int
41 smb2_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 file_attributes, __u32 create_options,
45                    void *data, int command)
46 {
47         int rc, tmprc = 0;
48         u64 persistent_fid, volatile_fid;
49         __le16 *utf16_path;
50
51         utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
52         if (!utf16_path)
53                 return -ENOMEM;
54
55         rc = SMB2_open(xid, tcon, utf16_path, &persistent_fid, &volatile_fid,
56                        desired_access, create_disposition, file_attributes,
57                        create_options, NULL);
58         if (rc) {
59                 kfree(utf16_path);
60                 return rc;
61         }
62
63         switch (command) {
64         case SMB2_OP_DELETE:
65                 break;
66         case SMB2_OP_QUERY_INFO:
67                 tmprc = SMB2_query_info(xid, tcon, persistent_fid,
68                                         volatile_fid,
69                                         (struct smb2_file_all_info *)data);
70                 break;
71         case SMB2_OP_MKDIR:
72                 /*
73                  * Directories are created through parameters in the
74                  * SMB2_open() call.
75                  */
76                 break;
77         case SMB2_OP_RENAME:
78                 tmprc = SMB2_rename(xid, tcon, persistent_fid, volatile_fid,
79                                     (__le16 *)data);
80                 break;
81         case SMB2_OP_HARDLINK:
82                 tmprc = SMB2_set_hardlink(xid, tcon, persistent_fid,
83                                           volatile_fid, (__le16 *)data);
84                 break;
85         case SMB2_OP_SET_EOF:
86                 tmprc = SMB2_set_eof(xid, tcon, persistent_fid, volatile_fid,
87                                      current->tgid, (__le64 *)data);
88                 break;
89         case SMB2_OP_SET_INFO:
90                 tmprc = SMB2_set_info(xid, tcon, persistent_fid, volatile_fid,
91                                       (FILE_BASIC_INFO *)data);
92                 break;
93         default:
94                 cERROR(1, "Invalid command");
95                 break;
96         }
97
98         rc = SMB2_close(xid, tcon, persistent_fid, volatile_fid);
99         if (tmprc)
100                 rc = tmprc;
101         kfree(utf16_path);
102         return rc;
103 }
104
105 void
106 move_smb2_info_to_cifs(FILE_ALL_INFO *dst, struct smb2_file_all_info *src)
107 {
108         memcpy(dst, src, (size_t)(&src->CurrentByteOffset) - (size_t)src);
109         dst->CurrentByteOffset = src->CurrentByteOffset;
110         dst->Mode = src->Mode;
111         dst->AlignmentRequirement = src->AlignmentRequirement;
112         dst->IndexNumber1 = 0; /* we don't use it */
113 }
114
115 int
116 smb2_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
117                      struct cifs_sb_info *cifs_sb, const char *full_path,
118                      FILE_ALL_INFO *data, bool *adjust_tz)
119 {
120         int rc;
121         struct smb2_file_all_info *smb2_data;
122
123         *adjust_tz = false;
124
125         smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + MAX_NAME * 2,
126                             GFP_KERNEL);
127         if (smb2_data == NULL)
128                 return -ENOMEM;
129
130         rc = smb2_open_op_close(xid, tcon, cifs_sb, full_path,
131                                 FILE_READ_ATTRIBUTES, FILE_OPEN, 0, 0,
132                                 smb2_data, SMB2_OP_QUERY_INFO);
133         if (rc)
134                 goto out;
135
136         move_smb2_info_to_cifs(data, smb2_data);
137 out:
138         kfree(smb2_data);
139         return rc;
140 }
141
142 int
143 smb2_mkdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
144            struct cifs_sb_info *cifs_sb)
145 {
146         return smb2_open_op_close(xid, tcon, cifs_sb, name,
147                                   FILE_WRITE_ATTRIBUTES, FILE_CREATE, 0,
148                                   CREATE_NOT_FILE, NULL, SMB2_OP_MKDIR);
149 }
150
151 void
152 smb2_mkdir_setinfo(struct inode *inode, const char *name,
153                    struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
154                    const unsigned int xid)
155 {
156         FILE_BASIC_INFO data;
157         struct cifsInodeInfo *cifs_i;
158         u32 dosattrs;
159         int tmprc;
160
161         memset(&data, 0, sizeof(data));
162         cifs_i = CIFS_I(inode);
163         dosattrs = cifs_i->cifsAttrs | ATTR_READONLY;
164         data.Attributes = cpu_to_le32(dosattrs);
165         tmprc = smb2_open_op_close(xid, tcon, cifs_sb, name,
166                                    FILE_WRITE_ATTRIBUTES, FILE_CREATE, 0,
167                                    CREATE_NOT_FILE, &data, SMB2_OP_SET_INFO);
168         if (tmprc == 0)
169                 cifs_i->cifsAttrs = dosattrs;
170 }
171
172 int
173 smb2_rmdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
174            struct cifs_sb_info *cifs_sb)
175 {
176         return smb2_open_op_close(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
177                                   0, CREATE_NOT_FILE | CREATE_DELETE_ON_CLOSE,
178                                   NULL, SMB2_OP_DELETE);
179 }
180
181 int
182 smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
183             struct cifs_sb_info *cifs_sb)
184 {
185         return smb2_open_op_close(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
186                                   0, CREATE_DELETE_ON_CLOSE, NULL,
187                                   SMB2_OP_DELETE);
188 }
189
190 static int
191 smb2_set_path_attr(const unsigned int xid, struct cifs_tcon *tcon,
192                    const char *from_name, const char *to_name,
193                    struct cifs_sb_info *cifs_sb, __u32 access, int command)
194 {
195         __le16 *smb2_to_name = NULL;
196         int rc;
197
198         smb2_to_name = cifs_convert_path_to_utf16(to_name, cifs_sb);
199         if (smb2_to_name == NULL) {
200                 rc = -ENOMEM;
201                 goto smb2_rename_path;
202         }
203
204         rc = smb2_open_op_close(xid, tcon, cifs_sb, from_name, access,
205                                 FILE_OPEN, 0, 0, smb2_to_name, command);
206 smb2_rename_path:
207         kfree(smb2_to_name);
208         return rc;
209 }
210
211 int
212 smb2_rename_path(const unsigned int xid, struct cifs_tcon *tcon,
213                  const char *from_name, const char *to_name,
214                  struct cifs_sb_info *cifs_sb)
215 {
216         return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
217                                   DELETE, SMB2_OP_RENAME);
218 }
219
220 int
221 smb2_create_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
222                      const char *from_name, const char *to_name,
223                      struct cifs_sb_info *cifs_sb)
224 {
225         return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
226                                   FILE_READ_ATTRIBUTES, SMB2_OP_HARDLINK);
227 }
228
229 int
230 smb2_set_path_size(const unsigned int xid, struct cifs_tcon *tcon,
231                    const char *full_path, __u64 size,
232                    struct cifs_sb_info *cifs_sb, bool set_alloc)
233 {
234         __le64 eof = cpu_to_le64(size);
235         return smb2_open_op_close(xid, tcon, cifs_sb, full_path,
236                                   FILE_WRITE_DATA, FILE_OPEN, 0, 0, &eof,
237                                   SMB2_OP_SET_EOF);
238 }
239
240 int
241 smb2_set_file_info(struct inode *inode, const char *full_path,
242                    FILE_BASIC_INFO *buf, const unsigned int xid)
243 {
244         struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
245         struct tcon_link *tlink;
246         int rc;
247
248         tlink = cifs_sb_tlink(cifs_sb);
249         if (IS_ERR(tlink))
250                 return PTR_ERR(tlink);
251         rc = smb2_open_op_close(xid, tlink_tcon(tlink), cifs_sb, full_path,
252                                 FILE_WRITE_ATTRIBUTES, FILE_OPEN, 0, 0, buf,
253                                 SMB2_OP_SET_INFO);
254         cifs_put_tlink(tlink);
255         return rc;
256 }