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