CIFS: Add SMB2 support for hardlink operation
[profile/ivi/kernel-adaptation-intel-automotive.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         default:
86                 cERROR(1, "Invalid command");
87                 break;
88         }
89
90         rc = SMB2_close(xid, tcon, persistent_fid, volatile_fid);
91         if (tmprc)
92                 rc = tmprc;
93         kfree(utf16_path);
94         return rc;
95 }
96
97 void
98 move_smb2_info_to_cifs(FILE_ALL_INFO *dst, struct smb2_file_all_info *src)
99 {
100         memcpy(dst, src, (size_t)(&src->CurrentByteOffset) - (size_t)src);
101         dst->CurrentByteOffset = src->CurrentByteOffset;
102         dst->Mode = src->Mode;
103         dst->AlignmentRequirement = src->AlignmentRequirement;
104         dst->IndexNumber1 = 0; /* we don't use it */
105 }
106
107 int
108 smb2_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
109                      struct cifs_sb_info *cifs_sb, const char *full_path,
110                      FILE_ALL_INFO *data, bool *adjust_tz)
111 {
112         int rc;
113         struct smb2_file_all_info *smb2_data;
114
115         *adjust_tz = false;
116
117         smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + MAX_NAME * 2,
118                             GFP_KERNEL);
119         if (smb2_data == NULL)
120                 return -ENOMEM;
121
122         rc = smb2_open_op_close(xid, tcon, cifs_sb, full_path,
123                                 FILE_READ_ATTRIBUTES, FILE_OPEN, 0, 0,
124                                 smb2_data, SMB2_OP_QUERY_INFO);
125         if (rc)
126                 goto out;
127
128         move_smb2_info_to_cifs(data, smb2_data);
129 out:
130         kfree(smb2_data);
131         return rc;
132 }
133
134 int
135 smb2_mkdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
136            struct cifs_sb_info *cifs_sb)
137 {
138         return smb2_open_op_close(xid, tcon, cifs_sb, name,
139                                   FILE_WRITE_ATTRIBUTES, FILE_CREATE, 0,
140                                   CREATE_NOT_FILE, NULL, SMB2_OP_MKDIR);
141 }
142
143 void
144 smb2_mkdir_setinfo(struct inode *inode, const char *name,
145                    struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
146                    const unsigned int xid)
147 {
148         FILE_BASIC_INFO data;
149         struct cifsInodeInfo *cifs_i;
150         u32 dosattrs;
151         int tmprc;
152
153         memset(&data, 0, sizeof(data));
154         cifs_i = CIFS_I(inode);
155         dosattrs = cifs_i->cifsAttrs | ATTR_READONLY;
156         data.Attributes = cpu_to_le32(dosattrs);
157         tmprc = smb2_open_op_close(xid, tcon, cifs_sb, name,
158                                    FILE_WRITE_ATTRIBUTES, FILE_CREATE, 0,
159                                    CREATE_NOT_FILE, &data, SMB2_OP_SET_INFO);
160         if (tmprc == 0)
161                 cifs_i->cifsAttrs = dosattrs;
162 }
163
164 int
165 smb2_rmdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
166            struct cifs_sb_info *cifs_sb)
167 {
168         return smb2_open_op_close(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
169                                   0, CREATE_NOT_FILE | CREATE_DELETE_ON_CLOSE,
170                                   NULL, SMB2_OP_DELETE);
171 }
172
173 int
174 smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
175             struct cifs_sb_info *cifs_sb)
176 {
177         return smb2_open_op_close(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
178                                   0, CREATE_DELETE_ON_CLOSE, NULL,
179                                   SMB2_OP_DELETE);
180 }
181
182 static int
183 smb2_set_path_attr(const unsigned int xid, struct cifs_tcon *tcon,
184                    const char *from_name, const char *to_name,
185                    struct cifs_sb_info *cifs_sb, __u32 access, int command)
186 {
187         __le16 *smb2_to_name = NULL;
188         int rc;
189
190         smb2_to_name = cifs_convert_path_to_utf16(to_name, cifs_sb);
191         if (smb2_to_name == NULL) {
192                 rc = -ENOMEM;
193                 goto smb2_rename_path;
194         }
195
196         rc = smb2_open_op_close(xid, tcon, cifs_sb, from_name, access,
197                                 FILE_OPEN, 0, 0, smb2_to_name, command);
198 smb2_rename_path:
199         kfree(smb2_to_name);
200         return rc;
201 }
202
203 int
204 smb2_rename_path(const unsigned int xid, struct cifs_tcon *tcon,
205                  const char *from_name, const char *to_name,
206                  struct cifs_sb_info *cifs_sb)
207 {
208         return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
209                                   DELETE, SMB2_OP_RENAME);
210 }
211
212 int
213 smb2_create_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
214                      const char *from_name, const char *to_name,
215                      struct cifs_sb_info *cifs_sb)
216 {
217         return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
218                                   FILE_READ_ATTRIBUTES, SMB2_OP_HARDLINK);
219 }