blob: 4ed10dd086e6f31f2816462c8a082ec8939175ae [file] [log] [blame]
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001/*
2 * fs/cifs/smb2file.c
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 * This library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published
10 * by the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
16 * the GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22#include <linux/fs.h>
23#include <linux/stat.h>
24#include <linux/slab.h>
25#include <linux/pagemap.h>
26#include <asm/div64.h>
27#include "cifsfs.h"
28#include "cifspdu.h"
29#include "cifsglob.h"
30#include "cifsproto.h"
31#include "cifs_debug.h"
32#include "cifs_fs_sb.h"
33#include "cifs_unicode.h"
34#include "fscache.h"
35#include "smb2proto.h"
36
37int
Pavel Shilovsky226730b2013-07-05 12:00:30 +040038smb2_open_file(const unsigned int xid, struct cifs_open_parms *oparms,
39 __u32 *oplock, FILE_ALL_INFO *buf)
Pavel Shilovskyf0df7372012-09-18 16:20:26 -070040{
41 int rc;
42 __le16 *smb2_path;
43 struct smb2_file_all_info *smb2_data = NULL;
Stefano Brivio729c0c92018-07-05 15:10:02 +020044 __u8 smb2_oplock;
Pavel Shilovsky226730b2013-07-05 12:00:30 +040045 struct cifs_fid *fid = oparms->fid;
Steve French592fafe2015-11-03 10:08:53 -060046 struct network_resiliency_req nr_ioctl_req;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -070047
Pavel Shilovsky226730b2013-07-05 12:00:30 +040048 smb2_path = cifs_convert_path_to_utf16(oparms->path, oparms->cifs_sb);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -070049 if (smb2_path == NULL) {
50 rc = -ENOMEM;
51 goto out;
52 }
53
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +040054 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -070055 GFP_KERNEL);
56 if (smb2_data == NULL) {
57 rc = -ENOMEM;
58 goto out;
59 }
60
Pavel Shilovsky226730b2013-07-05 12:00:30 +040061 oparms->desired_access |= FILE_READ_ATTRIBUTES;
Stefano Brivio729c0c92018-07-05 15:10:02 +020062 smb2_oplock = SMB2_OPLOCK_LEVEL_BATCH;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -070063
Stefano Brivio729c0c92018-07-05 15:10:02 +020064 rc = SMB2_open(xid, oparms, smb2_path, &smb2_oplock, smb2_data, NULL,
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +100065 NULL);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -070066 if (rc)
67 goto out;
68
Steve French592fafe2015-11-03 10:08:53 -060069
70 if (oparms->tcon->use_resilient) {
71 nr_ioctl_req.Timeout = 0; /* use server default (120 seconds) */
72 nr_ioctl_req.Reserved = 0;
73 rc = SMB2_ioctl(xid, oparms->tcon, fid->persistent_fid,
Aurelien Aptel51146622017-02-28 15:08:41 +010074 fid->volatile_fid, FSCTL_LMR_REQUEST_RESILIENCY,
Aurelien Aptel63a83b82018-01-24 13:46:11 +010075 true /* is_fsctl */,
Steve French592fafe2015-11-03 10:08:53 -060076 (char *)&nr_ioctl_req, sizeof(nr_ioctl_req),
77 NULL, NULL /* no return info */);
78 if (rc == -EOPNOTSUPP) {
79 cifs_dbg(VFS,
80 "resiliency not supported by server, disabling\n");
81 oparms->tcon->use_resilient = false;
82 } else if (rc)
83 cifs_dbg(FYI, "error %d setting resiliency\n", rc);
84
85 rc = 0;
86 }
87
Pavel Shilovskyf0df7372012-09-18 16:20:26 -070088 if (buf) {
89 /* open response does not have IndexNumber field - get it */
Pavel Shilovsky226730b2013-07-05 12:00:30 +040090 rc = SMB2_get_srv_num(xid, oparms->tcon, fid->persistent_fid,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -070091 fid->volatile_fid,
92 &smb2_data->IndexNumber);
93 if (rc) {
94 /* let get_inode_info disable server inode numbers */
95 smb2_data->IndexNumber = 0;
96 rc = 0;
97 }
98 move_smb2_info_to_cifs(buf, smb2_data);
99 }
100
Stefano Brivio729c0c92018-07-05 15:10:02 +0200101 *oplock = smb2_oplock;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700102out:
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700103 kfree(smb2_data);
104 kfree(smb2_path);
105 return rc;
106}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -0700107
108int
109smb2_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
110 const unsigned int xid)
111{
112 int rc = 0, stored_rc;
113 unsigned int max_num, num = 0, max_buf;
114 struct smb2_lock_element *buf, *cur;
115 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
David Howells2b0143b2015-03-17 22:25:59 +0000116 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -0700117 struct cifsLockInfo *li, *tmp;
118 __u64 length = 1 + flock->fl_end - flock->fl_start;
119 struct list_head tmp_llist;
120
121 INIT_LIST_HEAD(&tmp_llist);
122
123 /*
124 * Accessing maxBuf is racy with cifs_reconnect - need to store value
125 * and check it for zero before using.
126 */
127 max_buf = tcon->ses->server->maxBuf;
128 if (!max_buf)
129 return -EINVAL;
130
131 max_num = max_buf / sizeof(struct smb2_lock_element);
Fabian Frederick662e9b22014-12-10 15:41:20 -0800132 buf = kcalloc(max_num, sizeof(struct smb2_lock_element), GFP_KERNEL);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -0700133 if (!buf)
134 return -ENOMEM;
135
136 cur = buf;
137
Pavel Shilovsky1b4b55a2012-09-19 06:22:44 -0700138 down_write(&cinode->lock_sem);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -0700139 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
140 if (flock->fl_start > li->offset ||
141 (flock->fl_start + length) <
142 (li->offset + li->length))
143 continue;
144 if (current->tgid != li->pid)
145 continue;
146 if (cinode->can_cache_brlcks) {
147 /*
148 * We can cache brlock requests - simply remove a lock
149 * from the file's list.
150 */
151 list_del(&li->llist);
152 cifs_del_lock_waiters(li);
153 kfree(li);
154 continue;
155 }
156 cur->Length = cpu_to_le64(li->length);
157 cur->Offset = cpu_to_le64(li->offset);
158 cur->Flags = cpu_to_le32(SMB2_LOCKFLAG_UNLOCK);
159 /*
160 * We need to save a lock here to let us add it again to the
161 * file's list if the unlock range request fails on the server.
162 */
163 list_move(&li->llist, &tmp_llist);
164 if (++num == max_num) {
165 stored_rc = smb2_lockv(xid, tcon,
166 cfile->fid.persistent_fid,
167 cfile->fid.volatile_fid,
168 current->tgid, num, buf);
169 if (stored_rc) {
170 /*
171 * We failed on the unlock range request - add
172 * all locks from the tmp list to the head of
173 * the file's list.
174 */
175 cifs_move_llist(&tmp_llist,
176 &cfile->llist->locks);
177 rc = stored_rc;
178 } else
179 /*
180 * The unlock range request succeed - free the
181 * tmp list.
182 */
183 cifs_free_llist(&tmp_llist);
184 cur = buf;
185 num = 0;
186 } else
187 cur++;
188 }
189 if (num) {
190 stored_rc = smb2_lockv(xid, tcon, cfile->fid.persistent_fid,
191 cfile->fid.volatile_fid, current->tgid,
192 num, buf);
193 if (stored_rc) {
194 cifs_move_llist(&tmp_llist, &cfile->llist->locks);
195 rc = stored_rc;
196 } else
197 cifs_free_llist(&tmp_llist);
198 }
Pavel Shilovsky1b4b55a2012-09-19 06:22:44 -0700199 up_write(&cinode->lock_sem);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -0700200
201 kfree(buf);
202 return rc;
203}
Pavel Shilovskyb1407992012-09-19 06:22:44 -0700204
205static int
206smb2_push_mand_fdlocks(struct cifs_fid_locks *fdlocks, const unsigned int xid,
207 struct smb2_lock_element *buf, unsigned int max_num)
208{
209 int rc = 0, stored_rc;
210 struct cifsFileInfo *cfile = fdlocks->cfile;
211 struct cifsLockInfo *li;
212 unsigned int num = 0;
213 struct smb2_lock_element *cur = buf;
214 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
215
216 list_for_each_entry(li, &fdlocks->locks, llist) {
217 cur->Length = cpu_to_le64(li->length);
218 cur->Offset = cpu_to_le64(li->offset);
219 cur->Flags = cpu_to_le32(li->type |
220 SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
221 if (++num == max_num) {
222 stored_rc = smb2_lockv(xid, tcon,
223 cfile->fid.persistent_fid,
224 cfile->fid.volatile_fid,
225 current->tgid, num, buf);
226 if (stored_rc)
227 rc = stored_rc;
228 cur = buf;
229 num = 0;
230 } else
231 cur++;
232 }
233 if (num) {
234 stored_rc = smb2_lockv(xid, tcon,
235 cfile->fid.persistent_fid,
236 cfile->fid.volatile_fid,
237 current->tgid, num, buf);
238 if (stored_rc)
239 rc = stored_rc;
240 }
241
242 return rc;
243}
244
245int
246smb2_push_mandatory_locks(struct cifsFileInfo *cfile)
247{
248 int rc = 0, stored_rc;
249 unsigned int xid;
250 unsigned int max_num, max_buf;
251 struct smb2_lock_element *buf;
David Howells2b0143b2015-03-17 22:25:59 +0000252 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
Pavel Shilovskyb1407992012-09-19 06:22:44 -0700253 struct cifs_fid_locks *fdlocks;
254
255 xid = get_xid();
Pavel Shilovskyb1407992012-09-19 06:22:44 -0700256
257 /*
258 * Accessing maxBuf is racy with cifs_reconnect - need to store value
259 * and check it for zero before using.
260 */
261 max_buf = tlink_tcon(cfile->tlink)->ses->server->maxBuf;
Pavel Shilovskye3d240e2016-11-29 16:14:43 -0800262 if (max_buf < sizeof(struct smb2_lock_element)) {
Pavel Shilovskyb1407992012-09-19 06:22:44 -0700263 free_xid(xid);
264 return -EINVAL;
265 }
266
267 max_num = max_buf / sizeof(struct smb2_lock_element);
Fabian Frederick662e9b22014-12-10 15:41:20 -0800268 buf = kcalloc(max_num, sizeof(struct smb2_lock_element), GFP_KERNEL);
Pavel Shilovskyb1407992012-09-19 06:22:44 -0700269 if (!buf) {
Pavel Shilovskyb1407992012-09-19 06:22:44 -0700270 free_xid(xid);
271 return -ENOMEM;
272 }
273
274 list_for_each_entry(fdlocks, &cinode->llist, llist) {
275 stored_rc = smb2_push_mand_fdlocks(fdlocks, xid, buf, max_num);
276 if (stored_rc)
277 rc = stored_rc;
278 }
279
Pavel Shilovskyb1407992012-09-19 06:22:44 -0700280 kfree(buf);
Pavel Shilovskyb1407992012-09-19 06:22:44 -0700281 free_xid(xid);
282 return rc;
283}