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/stat.h>
11 #include <linux/slab.h>
12 #include <linux/pagemap.h>
13 #include <asm/div64.h>
14 #include "cifsfs.h"
15 #include "cifspdu.h"
16 #include "cifsglob.h"
17 #include "cifsproto.h"
18 #include "cifs_debug.h"
19 #include "cifs_fs_sb.h"
20 #include "cifs_unicode.h"
21 #include "fscache.h"
22 #include "smb2proto.h"
23 
24 int
smb2_open_file(const unsigned int xid,struct cifs_open_parms * oparms,__u32 * oplock,FILE_ALL_INFO * buf)25 smb2_open_file(const unsigned int xid, struct cifs_open_parms *oparms,
26 	       __u32 *oplock, FILE_ALL_INFO *buf)
27 {
28 	int rc;
29 	__le16 *smb2_path;
30 	struct smb2_file_all_info *smb2_data = NULL;
31 	__u8 smb2_oplock;
32 	struct cifs_fid *fid = oparms->fid;
33 	struct network_resiliency_req nr_ioctl_req;
34 
35 	smb2_path = cifs_convert_path_to_utf16(oparms->path, oparms->cifs_sb);
36 	if (smb2_path == NULL) {
37 		rc = -ENOMEM;
38 		goto out;
39 	}
40 
41 	smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
42 			    GFP_KERNEL);
43 	if (smb2_data == NULL) {
44 		rc = -ENOMEM;
45 		goto out;
46 	}
47 
48 	oparms->desired_access |= FILE_READ_ATTRIBUTES;
49 	smb2_oplock = SMB2_OPLOCK_LEVEL_BATCH;
50 
51 	rc = SMB2_open(xid, oparms, smb2_path, &smb2_oplock, smb2_data, NULL,
52 		       NULL, NULL);
53 	if (rc)
54 		goto out;
55 
56 
57 	if (oparms->tcon->use_resilient) {
58 		/* default timeout is 0, servers pick default (120 seconds) */
59 		nr_ioctl_req.Timeout =
60 			cpu_to_le32(oparms->tcon->handle_timeout);
61 		nr_ioctl_req.Reserved = 0;
62 		rc = SMB2_ioctl(xid, oparms->tcon, fid->persistent_fid,
63 			fid->volatile_fid, FSCTL_LMR_REQUEST_RESILIENCY,
64 			(char *)&nr_ioctl_req, sizeof(nr_ioctl_req),
65 			CIFSMaxBufSize, NULL, NULL /* no return info */);
66 		if (rc == -EOPNOTSUPP) {
67 			cifs_dbg(VFS,
68 			     "resiliency not supported by server, disabling\n");
69 			oparms->tcon->use_resilient = false;
70 		} else if (rc)
71 			cifs_dbg(FYI, "error %d setting resiliency\n", rc);
72 
73 		rc = 0;
74 	}
75 
76 	if (buf) {
77 		/* if open response does not have IndexNumber field - get it */
78 		if (smb2_data->IndexNumber == 0) {
79 			rc = SMB2_get_srv_num(xid, oparms->tcon,
80 				      fid->persistent_fid,
81 				      fid->volatile_fid,
82 				      &smb2_data->IndexNumber);
83 			if (rc) {
84 				/*
85 				 * let get_inode_info disable server inode
86 				 * numbers
87 				 */
88 				smb2_data->IndexNumber = 0;
89 				rc = 0;
90 			}
91 		}
92 		move_smb2_info_to_cifs(buf, smb2_data);
93 	}
94 
95 	*oplock = smb2_oplock;
96 out:
97 	kfree(smb2_data);
98 	kfree(smb2_path);
99 	return rc;
100 }
101 
102 int
smb2_unlock_range(struct cifsFileInfo * cfile,struct file_lock * flock,const unsigned int xid)103 smb2_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
104 		  const unsigned int xid)
105 {
106 	int rc = 0, stored_rc;
107 	unsigned int max_num, num = 0, max_buf;
108 	struct smb2_lock_element *buf, *cur;
109 	struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
110 	struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
111 	struct cifsLockInfo *li, *tmp;
112 	__u64 length = 1 + flock->fl_end - flock->fl_start;
113 	struct list_head tmp_llist;
114 
115 	INIT_LIST_HEAD(&tmp_llist);
116 
117 	/*
118 	 * Accessing maxBuf is racy with cifs_reconnect - need to store value
119 	 * and check it before using.
120 	 */
121 	max_buf = tcon->ses->server->maxBuf;
122 	if (max_buf < sizeof(struct smb2_lock_element))
123 		return -EINVAL;
124 
125 	BUILD_BUG_ON(sizeof(struct smb2_lock_element) > PAGE_SIZE);
126 	max_buf = min_t(unsigned int, max_buf, PAGE_SIZE);
127 	max_num = max_buf / sizeof(struct smb2_lock_element);
128 	buf = kcalloc(max_num, sizeof(struct smb2_lock_element), GFP_KERNEL);
129 	if (!buf)
130 		return -ENOMEM;
131 
132 	cur = buf;
133 
134 	cifs_down_write(&cinode->lock_sem);
135 	list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
136 		if (flock->fl_start > li->offset ||
137 		    (flock->fl_start + length) <
138 		    (li->offset + li->length))
139 			continue;
140 		if (current->tgid != li->pid)
141 			/*
142 			 * flock and OFD lock are associated with an open
143 			 * file description, not the process.
144 			 */
145 			if (!(flock->fl_flags & (FL_FLOCK | FL_OFDLCK)))
146 				continue;
147 		if (cinode->can_cache_brlcks) {
148 			/*
149 			 * We can cache brlock requests - simply remove a lock
150 			 * from the file's list.
151 			 */
152 			list_del(&li->llist);
153 			cifs_del_lock_waiters(li);
154 			kfree(li);
155 			continue;
156 		}
157 		cur->Length = cpu_to_le64(li->length);
158 		cur->Offset = cpu_to_le64(li->offset);
159 		cur->Flags = cpu_to_le32(SMB2_LOCKFLAG_UNLOCK);
160 		/*
161 		 * We need to save a lock here to let us add it again to the
162 		 * file's list if the unlock range request fails on the server.
163 		 */
164 		list_move(&li->llist, &tmp_llist);
165 		if (++num == max_num) {
166 			stored_rc = smb2_lockv(xid, tcon,
167 					       cfile->fid.persistent_fid,
168 					       cfile->fid.volatile_fid,
169 					       current->tgid, num, buf);
170 			if (stored_rc) {
171 				/*
172 				 * We failed on the unlock range request - add
173 				 * all locks from the tmp list to the head of
174 				 * the file's list.
175 				 */
176 				cifs_move_llist(&tmp_llist,
177 						&cfile->llist->locks);
178 				rc = stored_rc;
179 			} else
180 				/*
181 				 * The unlock range request succeed - free the
182 				 * tmp list.
183 				 */
184 				cifs_free_llist(&tmp_llist);
185 			cur = buf;
186 			num = 0;
187 		} else
188 			cur++;
189 	}
190 	if (num) {
191 		stored_rc = smb2_lockv(xid, tcon, cfile->fid.persistent_fid,
192 				       cfile->fid.volatile_fid, current->tgid,
193 				       num, buf);
194 		if (stored_rc) {
195 			cifs_move_llist(&tmp_llist, &cfile->llist->locks);
196 			rc = stored_rc;
197 		} else
198 			cifs_free_llist(&tmp_llist);
199 	}
200 	up_write(&cinode->lock_sem);
201 
202 	kfree(buf);
203 	return rc;
204 }
205 
206 static int
smb2_push_mand_fdlocks(struct cifs_fid_locks * fdlocks,const unsigned int xid,struct smb2_lock_element * buf,unsigned int max_num)207 smb2_push_mand_fdlocks(struct cifs_fid_locks *fdlocks, const unsigned int xid,
208 		       struct smb2_lock_element *buf, unsigned int max_num)
209 {
210 	int rc = 0, stored_rc;
211 	struct cifsFileInfo *cfile = fdlocks->cfile;
212 	struct cifsLockInfo *li;
213 	unsigned int num = 0;
214 	struct smb2_lock_element *cur = buf;
215 	struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
216 
217 	list_for_each_entry(li, &fdlocks->locks, llist) {
218 		cur->Length = cpu_to_le64(li->length);
219 		cur->Offset = cpu_to_le64(li->offset);
220 		cur->Flags = cpu_to_le32(li->type |
221 						SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
222 		if (++num == max_num) {
223 			stored_rc = smb2_lockv(xid, tcon,
224 					       cfile->fid.persistent_fid,
225 					       cfile->fid.volatile_fid,
226 					       current->tgid, num, buf);
227 			if (stored_rc)
228 				rc = stored_rc;
229 			cur = buf;
230 			num = 0;
231 		} else
232 			cur++;
233 	}
234 	if (num) {
235 		stored_rc = smb2_lockv(xid, tcon,
236 				       cfile->fid.persistent_fid,
237 				       cfile->fid.volatile_fid,
238 				       current->tgid, num, buf);
239 		if (stored_rc)
240 			rc = stored_rc;
241 	}
242 
243 	return rc;
244 }
245 
246 int
smb2_push_mandatory_locks(struct cifsFileInfo * cfile)247 smb2_push_mandatory_locks(struct cifsFileInfo *cfile)
248 {
249 	int rc = 0, stored_rc;
250 	unsigned int xid;
251 	unsigned int max_num, max_buf;
252 	struct smb2_lock_element *buf;
253 	struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
254 	struct cifs_fid_locks *fdlocks;
255 
256 	xid = get_xid();
257 
258 	/*
259 	 * Accessing maxBuf is racy with cifs_reconnect - need to store value
260 	 * and check it for zero before using.
261 	 */
262 	max_buf = tlink_tcon(cfile->tlink)->ses->server->maxBuf;
263 	if (max_buf < sizeof(struct smb2_lock_element)) {
264 		free_xid(xid);
265 		return -EINVAL;
266 	}
267 
268 	BUILD_BUG_ON(sizeof(struct smb2_lock_element) > PAGE_SIZE);
269 	max_buf = min_t(unsigned int, max_buf, PAGE_SIZE);
270 	max_num = max_buf / sizeof(struct smb2_lock_element);
271 	buf = kcalloc(max_num, sizeof(struct smb2_lock_element), GFP_KERNEL);
272 	if (!buf) {
273 		free_xid(xid);
274 		return -ENOMEM;
275 	}
276 
277 	list_for_each_entry(fdlocks, &cinode->llist, llist) {
278 		stored_rc = smb2_push_mand_fdlocks(fdlocks, xid, buf, max_num);
279 		if (stored_rc)
280 			rc = stored_rc;
281 	}
282 
283 	kfree(buf);
284 	free_xid(xid);
285 	return rc;
286 }
287