Commit | Line | Data |
---|---|---|
be4cb9e3 PS |
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, | |
ca81983f | 44 | __u32 create_options, void *data, int command) |
be4cb9e3 PS |
45 | { |
46 | int rc, tmprc = 0; | |
3d4ef9a1 | 47 | __le16 *utf16_path = NULL; |
2e44b288 | 48 | __u8 oplock = SMB2_OPLOCK_LEVEL_NONE; |
064f6047 PS |
49 | struct cifs_open_parms oparms; |
50 | struct cifs_fid fid; | |
3d4ef9a1 | 51 | bool use_cached_root_handle = false; |
be4cb9e3 | 52 | |
3d4ef9a1 SF |
53 | 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 | } | |
61 | ||
62 | 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; | |
be4cb9e3 | 66 | |
3d4ef9a1 SF |
67 | 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; | |
064f6047 | 73 | |
9d874c36 RS |
74 | rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, |
75 | NULL); | |
3d4ef9a1 SF |
76 | if (rc) { |
77 | kfree(utf16_path); | |
78 | return rc; | |
79 | } | |
be4cb9e3 PS |
80 | } |
81 | ||
82 | switch (command) { | |
83 | case SMB2_OP_DELETE: | |
84 | break; | |
85 | case SMB2_OP_QUERY_INFO: | |
064f6047 PS |
86 | tmprc = SMB2_query_info(xid, tcon, fid.persistent_fid, |
87 | fid.volatile_fid, | |
be4cb9e3 PS |
88 | (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; | |
897fba11 SF |
96 | case SMB2_OP_RMDIR: |
97 | tmprc = SMB2_rmdir(xid, tcon, fid.persistent_fid, | |
98 | fid.volatile_fid); | |
99 | break; | |
35143eb5 | 100 | case SMB2_OP_RENAME: |
064f6047 PS |
101 | tmprc = SMB2_rename(xid, tcon, fid.persistent_fid, |
102 | fid.volatile_fid, (__le16 *)data); | |
35143eb5 | 103 | break; |
568798cc | 104 | case SMB2_OP_HARDLINK: |
064f6047 PS |
105 | tmprc = SMB2_set_hardlink(xid, tcon, fid.persistent_fid, |
106 | fid.volatile_fid, (__le16 *)data); | |
568798cc | 107 | break; |
c839ff24 | 108 | case SMB2_OP_SET_EOF: |
064f6047 PS |
109 | tmprc = SMB2_set_eof(xid, tcon, fid.persistent_fid, |
110 | fid.volatile_fid, current->tgid, | |
f29ebb47 | 111 | (__le64 *)data, false); |
c839ff24 | 112 | break; |
1feeaac7 | 113 | case SMB2_OP_SET_INFO: |
064f6047 PS |
114 | tmprc = SMB2_set_info(xid, tcon, fid.persistent_fid, |
115 | fid.volatile_fid, | |
1feeaac7 PS |
116 | (FILE_BASIC_INFO *)data); |
117 | break; | |
be4cb9e3 | 118 | default: |
f96637be | 119 | cifs_dbg(VFS, "Invalid command\n"); |
be4cb9e3 PS |
120 | break; |
121 | } | |
122 | ||
9da6ec77 RS |
123 | if (use_cached_root_handle) |
124 | close_shroot(&tcon->crfid); | |
125 | else | |
3d4ef9a1 | 126 | rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid); |
be4cb9e3 PS |
127 | if (tmprc) |
128 | rc = tmprc; | |
129 | kfree(utf16_path); | |
130 | return rc; | |
131 | } | |
132 | ||
f0df737e | 133 | void |
be4cb9e3 PS |
134 | move_smb2_info_to_cifs(FILE_ALL_INFO *dst, struct smb2_file_all_info *src) |
135 | { | |
136 | memcpy(dst, src, (size_t)(&src->CurrentByteOffset) - (size_t)src); | |
137 | dst->CurrentByteOffset = src->CurrentByteOffset; | |
138 | dst->Mode = src->Mode; | |
139 | dst->AlignmentRequirement = src->AlignmentRequirement; | |
140 | dst->IndexNumber1 = 0; /* we don't use it */ | |
141 | } | |
142 | ||
143 | int | |
144 | smb2_query_path_info(const unsigned int xid, struct cifs_tcon *tcon, | |
145 | struct cifs_sb_info *cifs_sb, const char *full_path, | |
eb85d94b | 146 | FILE_ALL_INFO *data, bool *adjust_tz, bool *symlink) |
be4cb9e3 PS |
147 | { |
148 | int rc; | |
149 | struct smb2_file_all_info *smb2_data; | |
150 | ||
151 | *adjust_tz = false; | |
eb85d94b | 152 | *symlink = false; |
be4cb9e3 | 153 | |
1bbe4997 | 154 | smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2, |
be4cb9e3 PS |
155 | GFP_KERNEL); |
156 | if (smb2_data == NULL) | |
157 | return -ENOMEM; | |
158 | ||
159 | rc = smb2_open_op_close(xid, tcon, cifs_sb, full_path, | |
eb85d94b PS |
160 | FILE_READ_ATTRIBUTES, FILE_OPEN, 0, |
161 | smb2_data, SMB2_OP_QUERY_INFO); | |
162 | if (rc == -EOPNOTSUPP) { | |
163 | *symlink = true; | |
164 | /* Failed on a symbolic link - query a reparse point info */ | |
165 | rc = smb2_open_op_close(xid, tcon, cifs_sb, full_path, | |
166 | FILE_READ_ATTRIBUTES, FILE_OPEN, | |
167 | OPEN_REPARSE_POINT, smb2_data, | |
168 | SMB2_OP_QUERY_INFO); | |
169 | } | |
be4cb9e3 PS |
170 | if (rc) |
171 | goto out; | |
172 | ||
173 | move_smb2_info_to_cifs(data, smb2_data); | |
174 | out: | |
175 | kfree(smb2_data); | |
176 | return rc; | |
177 | } | |
a0e73183 PS |
178 | |
179 | int | |
180 | smb2_mkdir(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, | |
ca81983f | 184 | FILE_WRITE_ATTRIBUTES, FILE_CREATE, |
a0e73183 PS |
185 | CREATE_NOT_FILE, NULL, SMB2_OP_MKDIR); |
186 | } | |
187 | ||
188 | void | |
189 | smb2_mkdir_setinfo(struct inode *inode, const char *name, | |
190 | struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon, | |
191 | const unsigned int xid) | |
192 | { | |
193 | FILE_BASIC_INFO data; | |
194 | struct cifsInodeInfo *cifs_i; | |
195 | u32 dosattrs; | |
196 | int tmprc; | |
197 | ||
198 | memset(&data, 0, sizeof(data)); | |
199 | cifs_i = CIFS_I(inode); | |
200 | dosattrs = cifs_i->cifsAttrs | ATTR_READONLY; | |
201 | data.Attributes = cpu_to_le32(dosattrs); | |
202 | tmprc = smb2_open_op_close(xid, tcon, cifs_sb, name, | |
ca81983f | 203 | FILE_WRITE_ATTRIBUTES, FILE_CREATE, |
a0e73183 PS |
204 | CREATE_NOT_FILE, &data, SMB2_OP_SET_INFO); |
205 | if (tmprc == 0) | |
206 | cifs_i->cifsAttrs = dosattrs; | |
207 | } | |
1a500f01 PS |
208 | |
209 | int | |
210 | smb2_rmdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name, | |
211 | struct cifs_sb_info *cifs_sb) | |
212 | { | |
213 | return smb2_open_op_close(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN, | |
897fba11 SF |
214 | CREATE_NOT_FILE, |
215 | NULL, SMB2_OP_RMDIR); | |
1a500f01 | 216 | } |
cbe6f439 PS |
217 | |
218 | int | |
219 | smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name, | |
220 | struct cifs_sb_info *cifs_sb) | |
221 | { | |
222 | return smb2_open_op_close(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN, | |
b42bf888 PS |
223 | CREATE_DELETE_ON_CLOSE | OPEN_REPARSE_POINT, |
224 | NULL, SMB2_OP_DELETE); | |
cbe6f439 | 225 | } |
35143eb5 | 226 | |
568798cc PS |
227 | static int |
228 | smb2_set_path_attr(const unsigned int xid, struct cifs_tcon *tcon, | |
229 | const char *from_name, const char *to_name, | |
230 | struct cifs_sb_info *cifs_sb, __u32 access, int command) | |
35143eb5 PS |
231 | { |
232 | __le16 *smb2_to_name = NULL; | |
233 | int rc; | |
234 | ||
235 | smb2_to_name = cifs_convert_path_to_utf16(to_name, cifs_sb); | |
236 | if (smb2_to_name == NULL) { | |
237 | rc = -ENOMEM; | |
238 | goto smb2_rename_path; | |
239 | } | |
240 | ||
568798cc | 241 | rc = smb2_open_op_close(xid, tcon, cifs_sb, from_name, access, |
ca81983f | 242 | FILE_OPEN, 0, smb2_to_name, command); |
35143eb5 PS |
243 | smb2_rename_path: |
244 | kfree(smb2_to_name); | |
245 | return rc; | |
246 | } | |
568798cc PS |
247 | |
248 | int | |
249 | smb2_rename_path(const unsigned int xid, struct cifs_tcon *tcon, | |
250 | const char *from_name, const char *to_name, | |
251 | struct cifs_sb_info *cifs_sb) | |
252 | { | |
253 | return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb, | |
254 | DELETE, SMB2_OP_RENAME); | |
255 | } | |
256 | ||
257 | int | |
258 | smb2_create_hardlink(const unsigned int xid, struct cifs_tcon *tcon, | |
259 | const char *from_name, const char *to_name, | |
260 | struct cifs_sb_info *cifs_sb) | |
261 | { | |
262 | return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb, | |
263 | FILE_READ_ATTRIBUTES, SMB2_OP_HARDLINK); | |
264 | } | |
c839ff24 PS |
265 | |
266 | int | |
267 | smb2_set_path_size(const unsigned int xid, struct cifs_tcon *tcon, | |
268 | const char *full_path, __u64 size, | |
269 | struct cifs_sb_info *cifs_sb, bool set_alloc) | |
270 | { | |
271 | __le64 eof = cpu_to_le64(size); | |
272 | return smb2_open_op_close(xid, tcon, cifs_sb, full_path, | |
ca81983f | 273 | FILE_WRITE_DATA, FILE_OPEN, 0, &eof, |
c839ff24 PS |
274 | SMB2_OP_SET_EOF); |
275 | } | |
1feeaac7 PS |
276 | |
277 | int | |
278 | smb2_set_file_info(struct inode *inode, const char *full_path, | |
279 | FILE_BASIC_INFO *buf, const unsigned int xid) | |
280 | { | |
281 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); | |
282 | struct tcon_link *tlink; | |
283 | int rc; | |
284 | ||
18dd8e1a | 285 | if ((buf->CreationTime == 0) && (buf->LastAccessTime == 0) && |
fd09b7d3 | 286 | (buf->LastWriteTime == 0) && (buf->ChangeTime == 0) && |
18dd8e1a SF |
287 | (buf->Attributes == 0)) |
288 | return 0; /* would be a no op, no sense sending this */ | |
289 | ||
1feeaac7 PS |
290 | tlink = cifs_sb_tlink(cifs_sb); |
291 | if (IS_ERR(tlink)) | |
292 | return PTR_ERR(tlink); | |
18dd8e1a | 293 | |
1feeaac7 | 294 | rc = smb2_open_op_close(xid, tlink_tcon(tlink), cifs_sb, full_path, |
ca81983f | 295 | FILE_WRITE_ATTRIBUTES, FILE_OPEN, 0, buf, |
1feeaac7 PS |
296 | SMB2_OP_SET_INFO); |
297 | cifs_put_tlink(tlink); | |
298 | return rc; | |
299 | } |