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