xref: /openbmc/linux/fs/smb/client/smb2file.c (revision e961f8c6)
1 // SPDX-License-Identifier: LGPL-2.1
2 /*
3  *
4  *   Copyright (C) International Business Machines  Corp., 2002, 2011
5  *   Author(s): Steve French (sfrench@us.ibm.com),
6  *              Pavel Shilovsky ((pshilovsky@samba.org) 2012
7  *
8  */
9 #include <linux/fs.h>
10 #include <linux/filelock.h>
11 #include <linux/stat.h>
12 #include <linux/slab.h>
13 #include <linux/pagemap.h>
14 #include <asm/div64.h>
15 #include "cifsfs.h"
16 #include "cifspdu.h"
17 #include "cifsglob.h"
18 #include "cifsproto.h"
19 #include "cifs_debug.h"
20 #include "cifs_fs_sb.h"
21 #include "cifs_unicode.h"
22 #include "fscache.h"
23 #include "smb2proto.h"
24 #include "smb2status.h"
25 
26 static struct smb2_symlink_err_rsp *symlink_data(const struct kvec *iov)
27 {
28 	struct smb2_err_rsp *err = iov->iov_base;
29 	struct smb2_symlink_err_rsp *sym = ERR_PTR(-EINVAL);
30 	u32 len;
31 
32 	if (err->ErrorContextCount) {
33 		struct smb2_error_context_rsp *p, *end;
34 
35 		len = (u32)err->ErrorContextCount * (offsetof(struct smb2_error_context_rsp,
36 							      ErrorContextData) +
37 						     sizeof(struct smb2_symlink_err_rsp));
38 		if (le32_to_cpu(err->ByteCount) < len || iov->iov_len < len + sizeof(*err) + 1)
39 			return ERR_PTR(-EINVAL);
40 
41 		p = (struct smb2_error_context_rsp *)err->ErrorData;
42 		end = (struct smb2_error_context_rsp *)((u8 *)err + iov->iov_len);
43 		do {
44 			if (le32_to_cpu(p->ErrorId) == SMB2_ERROR_ID_DEFAULT) {
45 				sym = (struct smb2_symlink_err_rsp *)&p->ErrorContextData;
46 				break;
47 			}
48 			cifs_dbg(FYI, "%s: skipping unhandled error context: 0x%x\n",
49 				 __func__, le32_to_cpu(p->ErrorId));
50 
51 			len = ALIGN(le32_to_cpu(p->ErrorDataLength), 8);
52 			p = (struct smb2_error_context_rsp *)((u8 *)&p->ErrorContextData + len);
53 		} while (p < end);
54 	} else if (le32_to_cpu(err->ByteCount) >= sizeof(*sym) &&
55 		   iov->iov_len >= SMB2_SYMLINK_STRUCT_SIZE) {
56 		sym = (struct smb2_symlink_err_rsp *)err->ErrorData;
57 	}
58 
59 	if (!IS_ERR(sym) && (le32_to_cpu(sym->SymLinkErrorTag) != SYMLINK_ERROR_TAG ||
60 			     le32_to_cpu(sym->ReparseTag) != IO_REPARSE_TAG_SYMLINK))
61 		sym = ERR_PTR(-EINVAL);
62 
63 	return sym;
64 }
65 
66 int smb2_parse_symlink_response(struct cifs_sb_info *cifs_sb, const struct kvec *iov, char **path)
67 {
68 	struct smb2_symlink_err_rsp *sym;
69 	unsigned int sub_offs, sub_len;
70 	unsigned int print_offs, print_len;
71 	char *s;
72 
73 	if (!cifs_sb || !iov || !iov->iov_base || !iov->iov_len || !path)
74 		return -EINVAL;
75 
76 	sym = symlink_data(iov);
77 	if (IS_ERR(sym))
78 		return PTR_ERR(sym);
79 
80 	sub_len = le16_to_cpu(sym->SubstituteNameLength);
81 	sub_offs = le16_to_cpu(sym->SubstituteNameOffset);
82 	print_len = le16_to_cpu(sym->PrintNameLength);
83 	print_offs = le16_to_cpu(sym->PrintNameOffset);
84 
85 	if (iov->iov_len < SMB2_SYMLINK_STRUCT_SIZE + sub_offs + sub_len ||
86 	    iov->iov_len < SMB2_SYMLINK_STRUCT_SIZE + print_offs + print_len)
87 		return -EINVAL;
88 
89 	s = cifs_strndup_from_utf16((char *)sym->PathBuffer + sub_offs, sub_len, true,
90 				    cifs_sb->local_nls);
91 	if (!s)
92 		return -ENOMEM;
93 	convert_delimiter(s, '/');
94 	cifs_dbg(FYI, "%s: symlink target: %s\n", __func__, s);
95 
96 	*path = s;
97 	return 0;
98 }
99 
100 int smb2_open_file(const unsigned int xid, struct cifs_open_parms *oparms, __u32 *oplock, void *buf)
101 {
102 	int rc;
103 	__le16 *smb2_path;
104 	__u8 smb2_oplock;
105 	struct cifs_open_info_data *data = buf;
106 	struct smb2_file_all_info file_info = {};
107 	struct smb2_file_all_info *smb2_data = data ? &file_info : NULL;
108 	struct kvec err_iov = {};
109 	int err_buftype = CIFS_NO_BUFFER;
110 	struct cifs_fid *fid = oparms->fid;
111 	struct network_resiliency_req nr_ioctl_req;
112 
113 	smb2_path = cifs_convert_path_to_utf16(oparms->path, oparms->cifs_sb);
114 	if (smb2_path == NULL)
115 		return -ENOMEM;
116 
117 	oparms->desired_access |= FILE_READ_ATTRIBUTES;
118 	smb2_oplock = SMB2_OPLOCK_LEVEL_BATCH;
119 
120 	rc = SMB2_open(xid, oparms, smb2_path, &smb2_oplock, smb2_data, NULL, &err_iov,
121 		       &err_buftype);
122 	if (rc && data) {
123 		struct smb2_hdr *hdr = err_iov.iov_base;
124 
125 		if (unlikely(!err_iov.iov_base || err_buftype == CIFS_NO_BUFFER))
126 			goto out;
127 		if (hdr->Status == STATUS_STOPPED_ON_SYMLINK) {
128 			rc = smb2_parse_symlink_response(oparms->cifs_sb, &err_iov,
129 							 &data->symlink_target);
130 			if (!rc) {
131 				memset(smb2_data, 0, sizeof(*smb2_data));
132 				oparms->create_options |= OPEN_REPARSE_POINT;
133 				rc = SMB2_open(xid, oparms, smb2_path, &smb2_oplock, smb2_data,
134 					       NULL, NULL, NULL);
135 				oparms->create_options &= ~OPEN_REPARSE_POINT;
136 			}
137 		}
138 	}
139 
140 	if (rc)
141 		goto out;
142 
143 	if (oparms->tcon->use_resilient) {
144 		/* default timeout is 0, servers pick default (120 seconds) */
145 		nr_ioctl_req.Timeout =
146 			cpu_to_le32(oparms->tcon->handle_timeout);
147 		nr_ioctl_req.Reserved = 0;
148 		rc = SMB2_ioctl(xid, oparms->tcon, fid->persistent_fid,
149 			fid->volatile_fid, FSCTL_LMR_REQUEST_RESILIENCY,
150 			(char *)&nr_ioctl_req, sizeof(nr_ioctl_req),
151 			CIFSMaxBufSize, NULL, NULL /* no return info */);
152 		if (rc == -EOPNOTSUPP) {
153 			cifs_dbg(VFS,
154 			     "resiliency not supported by server, disabling\n");
155 			oparms->tcon->use_resilient = false;
156 		} else if (rc)
157 			cifs_dbg(FYI, "error %d setting resiliency\n", rc);
158 
159 		rc = 0;
160 	}
161 
162 	if (smb2_data) {
163 		/* if open response does not have IndexNumber field - get it */
164 		if (smb2_data->IndexNumber == 0) {
165 			rc = SMB2_get_srv_num(xid, oparms->tcon,
166 				      fid->persistent_fid,
167 				      fid->volatile_fid,
168 				      &smb2_data->IndexNumber);
169 			if (rc) {
170 				/*
171 				 * let get_inode_info disable server inode
172 				 * numbers
173 				 */
174 				smb2_data->IndexNumber = 0;
175 				rc = 0;
176 			}
177 		}
178 		memcpy(&data->fi, smb2_data, sizeof(data->fi));
179 	}
180 
181 	*oplock = smb2_oplock;
182 out:
183 	free_rsp_buf(err_buftype, err_iov.iov_base);
184 	kfree(smb2_path);
185 	return rc;
186 }
187 
188 int
189 smb2_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
190 		  const unsigned int xid)
191 {
192 	int rc = 0, stored_rc;
193 	unsigned int max_num, num = 0, max_buf;
194 	struct smb2_lock_element *buf, *cur;
195 	struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
196 	struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
197 	struct cifsLockInfo *li, *tmp;
198 	__u64 length = 1 + flock->fl_end - flock->fl_start;
199 	struct list_head tmp_llist;
200 
201 	INIT_LIST_HEAD(&tmp_llist);
202 
203 	/*
204 	 * Accessing maxBuf is racy with cifs_reconnect - need to store value
205 	 * and check it before using.
206 	 */
207 	max_buf = tcon->ses->server->maxBuf;
208 	if (max_buf < sizeof(struct smb2_lock_element))
209 		return -EINVAL;
210 
211 	BUILD_BUG_ON(sizeof(struct smb2_lock_element) > PAGE_SIZE);
212 	max_buf = min_t(unsigned int, max_buf, PAGE_SIZE);
213 	max_num = max_buf / sizeof(struct smb2_lock_element);
214 	buf = kcalloc(max_num, sizeof(struct smb2_lock_element), GFP_KERNEL);
215 	if (!buf)
216 		return -ENOMEM;
217 
218 	cur = buf;
219 
220 	cifs_down_write(&cinode->lock_sem);
221 	list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
222 		if (flock->fl_start > li->offset ||
223 		    (flock->fl_start + length) <
224 		    (li->offset + li->length))
225 			continue;
226 		if (current->tgid != li->pid)
227 			/*
228 			 * flock and OFD lock are associated with an open
229 			 * file description, not the process.
230 			 */
231 			if (!(flock->fl_flags & (FL_FLOCK | FL_OFDLCK)))
232 				continue;
233 		if (cinode->can_cache_brlcks) {
234 			/*
235 			 * We can cache brlock requests - simply remove a lock
236 			 * from the file's list.
237 			 */
238 			list_del(&li->llist);
239 			cifs_del_lock_waiters(li);
240 			kfree(li);
241 			continue;
242 		}
243 		cur->Length = cpu_to_le64(li->length);
244 		cur->Offset = cpu_to_le64(li->offset);
245 		cur->Flags = cpu_to_le32(SMB2_LOCKFLAG_UNLOCK);
246 		/*
247 		 * We need to save a lock here to let us add it again to the
248 		 * file's list if the unlock range request fails on the server.
249 		 */
250 		list_move(&li->llist, &tmp_llist);
251 		if (++num == max_num) {
252 			stored_rc = smb2_lockv(xid, tcon,
253 					       cfile->fid.persistent_fid,
254 					       cfile->fid.volatile_fid,
255 					       current->tgid, num, buf);
256 			if (stored_rc) {
257 				/*
258 				 * We failed on the unlock range request - add
259 				 * all locks from the tmp list to the head of
260 				 * the file's list.
261 				 */
262 				cifs_move_llist(&tmp_llist,
263 						&cfile->llist->locks);
264 				rc = stored_rc;
265 			} else
266 				/*
267 				 * The unlock range request succeed - free the
268 				 * tmp list.
269 				 */
270 				cifs_free_llist(&tmp_llist);
271 			cur = buf;
272 			num = 0;
273 		} else
274 			cur++;
275 	}
276 	if (num) {
277 		stored_rc = smb2_lockv(xid, tcon, cfile->fid.persistent_fid,
278 				       cfile->fid.volatile_fid, current->tgid,
279 				       num, buf);
280 		if (stored_rc) {
281 			cifs_move_llist(&tmp_llist, &cfile->llist->locks);
282 			rc = stored_rc;
283 		} else
284 			cifs_free_llist(&tmp_llist);
285 	}
286 	up_write(&cinode->lock_sem);
287 
288 	kfree(buf);
289 	return rc;
290 }
291 
292 static int
293 smb2_push_mand_fdlocks(struct cifs_fid_locks *fdlocks, const unsigned int xid,
294 		       struct smb2_lock_element *buf, unsigned int max_num)
295 {
296 	int rc = 0, stored_rc;
297 	struct cifsFileInfo *cfile = fdlocks->cfile;
298 	struct cifsLockInfo *li;
299 	unsigned int num = 0;
300 	struct smb2_lock_element *cur = buf;
301 	struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
302 
303 	list_for_each_entry(li, &fdlocks->locks, llist) {
304 		cur->Length = cpu_to_le64(li->length);
305 		cur->Offset = cpu_to_le64(li->offset);
306 		cur->Flags = cpu_to_le32(li->type |
307 						SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
308 		if (++num == max_num) {
309 			stored_rc = smb2_lockv(xid, tcon,
310 					       cfile->fid.persistent_fid,
311 					       cfile->fid.volatile_fid,
312 					       current->tgid, num, buf);
313 			if (stored_rc)
314 				rc = stored_rc;
315 			cur = buf;
316 			num = 0;
317 		} else
318 			cur++;
319 	}
320 	if (num) {
321 		stored_rc = smb2_lockv(xid, tcon,
322 				       cfile->fid.persistent_fid,
323 				       cfile->fid.volatile_fid,
324 				       current->tgid, num, buf);
325 		if (stored_rc)
326 			rc = stored_rc;
327 	}
328 
329 	return rc;
330 }
331 
332 int
333 smb2_push_mandatory_locks(struct cifsFileInfo *cfile)
334 {
335 	int rc = 0, stored_rc;
336 	unsigned int xid;
337 	unsigned int max_num, max_buf;
338 	struct smb2_lock_element *buf;
339 	struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
340 	struct cifs_fid_locks *fdlocks;
341 
342 	xid = get_xid();
343 
344 	/*
345 	 * Accessing maxBuf is racy with cifs_reconnect - need to store value
346 	 * and check it for zero before using.
347 	 */
348 	max_buf = tlink_tcon(cfile->tlink)->ses->server->maxBuf;
349 	if (max_buf < sizeof(struct smb2_lock_element)) {
350 		free_xid(xid);
351 		return -EINVAL;
352 	}
353 
354 	BUILD_BUG_ON(sizeof(struct smb2_lock_element) > PAGE_SIZE);
355 	max_buf = min_t(unsigned int, max_buf, PAGE_SIZE);
356 	max_num = max_buf / sizeof(struct smb2_lock_element);
357 	buf = kcalloc(max_num, sizeof(struct smb2_lock_element), GFP_KERNEL);
358 	if (!buf) {
359 		free_xid(xid);
360 		return -ENOMEM;
361 	}
362 
363 	list_for_each_entry(fdlocks, &cinode->llist, llist) {
364 		stored_rc = smb2_push_mand_fdlocks(fdlocks, xid, buf, max_num);
365 		if (stored_rc)
366 			rc = stored_rc;
367 	}
368 
369 	kfree(buf);
370 	free_xid(xid);
371 	return rc;
372 }
373