2021-06-18 05:31:49 +00:00
|
|
|
// SPDX-License-Identifier: LGPL-2.1
|
2012-09-18 23:20:26 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Copyright (C) International Business Machines Corp., 2002, 2011
|
|
|
|
* Author(s): Steve French (sfrench@us.ibm.com),
|
|
|
|
* Pavel Shilovsky ((pshilovsky@samba.org) 2012
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/stat.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/pagemap.h>
|
|
|
|
#include <asm/div64.h>
|
|
|
|
#include "cifsfs.h"
|
|
|
|
#include "cifspdu.h"
|
|
|
|
#include "cifsglob.h"
|
|
|
|
#include "cifsproto.h"
|
|
|
|
#include "cifs_debug.h"
|
|
|
|
#include "cifs_fs_sb.h"
|
|
|
|
#include "cifs_unicode.h"
|
|
|
|
#include "fscache.h"
|
|
|
|
#include "smb2proto.h"
|
|
|
|
|
|
|
|
int
|
2013-07-05 08:00:30 +00:00
|
|
|
smb2_open_file(const unsigned int xid, struct cifs_open_parms *oparms,
|
|
|
|
__u32 *oplock, FILE_ALL_INFO *buf)
|
2012-09-18 23:20:26 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
__le16 *smb2_path;
|
|
|
|
struct smb2_file_all_info *smb2_data = NULL;
|
2018-07-05 13:10:02 +00:00
|
|
|
__u8 smb2_oplock;
|
2013-07-05 08:00:30 +00:00
|
|
|
struct cifs_fid *fid = oparms->fid;
|
2015-11-03 16:08:53 +00:00
|
|
|
struct network_resiliency_req nr_ioctl_req;
|
2012-09-18 23:20:26 +00:00
|
|
|
|
2013-07-05 08:00:30 +00:00
|
|
|
smb2_path = cifs_convert_path_to_utf16(oparms->path, oparms->cifs_sb);
|
2012-09-18 23:20:26 +00:00
|
|
|
if (smb2_path == NULL) {
|
|
|
|
rc = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2014-08-22 09:32:11 +00:00
|
|
|
smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
|
2012-09-18 23:20:26 +00:00
|
|
|
GFP_KERNEL);
|
|
|
|
if (smb2_data == NULL) {
|
|
|
|
rc = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2013-07-05 08:00:30 +00:00
|
|
|
oparms->desired_access |= FILE_READ_ATTRIBUTES;
|
2018-07-05 13:10:02 +00:00
|
|
|
smb2_oplock = SMB2_OPLOCK_LEVEL_BATCH;
|
2012-09-19 13:22:44 +00:00
|
|
|
|
2018-07-05 13:10:02 +00:00
|
|
|
rc = SMB2_open(xid, oparms, smb2_path, &smb2_oplock, smb2_data, NULL,
|
2020-03-02 16:53:22 +00:00
|
|
|
NULL, NULL);
|
2012-09-18 23:20:26 +00:00
|
|
|
if (rc)
|
|
|
|
goto out;
|
|
|
|
|
2015-11-03 16:08:53 +00:00
|
|
|
|
2019-12-18 03:04:51 +00:00
|
|
|
if (oparms->tcon->use_resilient) {
|
2019-03-29 21:31:07 +00:00
|
|
|
/* default timeout is 0, servers pick default (120 seconds) */
|
|
|
|
nr_ioctl_req.Timeout =
|
|
|
|
cpu_to_le32(oparms->tcon->handle_timeout);
|
2015-11-03 16:08:53 +00:00
|
|
|
nr_ioctl_req.Reserved = 0;
|
|
|
|
rc = SMB2_ioctl(xid, oparms->tcon, fid->persistent_fid,
|
2017-02-28 14:08:41 +00:00
|
|
|
fid->volatile_fid, FSCTL_LMR_REQUEST_RESILIENCY,
|
2018-01-24 12:46:11 +00:00
|
|
|
true /* is_fsctl */,
|
2015-11-03 16:08:53 +00:00
|
|
|
(char *)&nr_ioctl_req, sizeof(nr_ioctl_req),
|
2019-03-29 03:32:49 +00:00
|
|
|
CIFSMaxBufSize, NULL, NULL /* no return info */);
|
2015-11-03 16:08:53 +00:00
|
|
|
if (rc == -EOPNOTSUPP) {
|
|
|
|
cifs_dbg(VFS,
|
|
|
|
"resiliency not supported by server, disabling\n");
|
|
|
|
oparms->tcon->use_resilient = false;
|
|
|
|
} else if (rc)
|
|
|
|
cifs_dbg(FYI, "error %d setting resiliency\n", rc);
|
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
}
|
|
|
|
|
2012-09-18 23:20:26 +00:00
|
|
|
if (buf) {
|
2019-07-18 22:22:18 +00:00
|
|
|
/* if open response does not have IndexNumber field - get it */
|
|
|
|
if (smb2_data->IndexNumber == 0) {
|
|
|
|
rc = SMB2_get_srv_num(xid, oparms->tcon,
|
|
|
|
fid->persistent_fid,
|
2012-09-18 23:20:26 +00:00
|
|
|
fid->volatile_fid,
|
|
|
|
&smb2_data->IndexNumber);
|
2019-07-18 22:22:18 +00:00
|
|
|
if (rc) {
|
|
|
|
/*
|
|
|
|
* let get_inode_info disable server inode
|
|
|
|
* numbers
|
|
|
|
*/
|
|
|
|
smb2_data->IndexNumber = 0;
|
|
|
|
rc = 0;
|
|
|
|
}
|
2012-09-18 23:20:26 +00:00
|
|
|
}
|
|
|
|
move_smb2_info_to_cifs(buf, smb2_data);
|
|
|
|
}
|
|
|
|
|
2018-07-05 13:10:02 +00:00
|
|
|
*oplock = smb2_oplock;
|
2012-09-18 23:20:26 +00:00
|
|
|
out:
|
|
|
|
kfree(smb2_data);
|
|
|
|
kfree(smb2_path);
|
|
|
|
return rc;
|
|
|
|
}
|
2012-09-19 13:22:43 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
smb2_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
|
|
|
|
const unsigned int xid)
|
|
|
|
{
|
|
|
|
int rc = 0, stored_rc;
|
|
|
|
unsigned int max_num, num = 0, max_buf;
|
|
|
|
struct smb2_lock_element *buf, *cur;
|
|
|
|
struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
|
2015-03-17 22:25:59 +00:00
|
|
|
struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
|
2012-09-19 13:22:43 +00:00
|
|
|
struct cifsLockInfo *li, *tmp;
|
|
|
|
__u64 length = 1 + flock->fl_end - flock->fl_start;
|
|
|
|
struct list_head tmp_llist;
|
|
|
|
|
|
|
|
INIT_LIST_HEAD(&tmp_llist);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Accessing maxBuf is racy with cifs_reconnect - need to store value
|
2019-01-08 18:30:57 +00:00
|
|
|
* and check it before using.
|
2012-09-19 13:22:43 +00:00
|
|
|
*/
|
|
|
|
max_buf = tcon->ses->server->maxBuf;
|
2019-01-08 18:30:57 +00:00
|
|
|
if (max_buf < sizeof(struct smb2_lock_element))
|
2012-09-19 13:22:43 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
2019-01-08 18:30:56 +00:00
|
|
|
BUILD_BUG_ON(sizeof(struct smb2_lock_element) > PAGE_SIZE);
|
|
|
|
max_buf = min_t(unsigned int, max_buf, PAGE_SIZE);
|
2012-09-19 13:22:43 +00:00
|
|
|
max_num = max_buf / sizeof(struct smb2_lock_element);
|
2014-12-10 23:41:20 +00:00
|
|
|
buf = kcalloc(max_num, sizeof(struct smb2_lock_element), GFP_KERNEL);
|
2012-09-19 13:22:43 +00:00
|
|
|
if (!buf)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
cur = buf;
|
|
|
|
|
2019-10-23 09:02:33 +00:00
|
|
|
cifs_down_write(&cinode->lock_sem);
|
2012-09-19 13:22:43 +00:00
|
|
|
list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
|
|
|
|
if (flock->fl_start > li->offset ||
|
|
|
|
(flock->fl_start + length) <
|
|
|
|
(li->offset + li->length))
|
|
|
|
continue;
|
|
|
|
if (current->tgid != li->pid)
|
2020-02-21 02:30:01 +00:00
|
|
|
/*
|
|
|
|
* flock and OFD lock are associated with an open
|
|
|
|
* file description, not the process.
|
|
|
|
*/
|
|
|
|
if (!(flock->fl_flags & (FL_FLOCK | FL_OFDLCK)))
|
|
|
|
continue;
|
2012-09-19 13:22:43 +00:00
|
|
|
if (cinode->can_cache_brlcks) {
|
|
|
|
/*
|
|
|
|
* We can cache brlock requests - simply remove a lock
|
|
|
|
* from the file's list.
|
|
|
|
*/
|
|
|
|
list_del(&li->llist);
|
|
|
|
cifs_del_lock_waiters(li);
|
|
|
|
kfree(li);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
cur->Length = cpu_to_le64(li->length);
|
|
|
|
cur->Offset = cpu_to_le64(li->offset);
|
|
|
|
cur->Flags = cpu_to_le32(SMB2_LOCKFLAG_UNLOCK);
|
|
|
|
/*
|
|
|
|
* We need to save a lock here to let us add it again to the
|
|
|
|
* file's list if the unlock range request fails on the server.
|
|
|
|
*/
|
|
|
|
list_move(&li->llist, &tmp_llist);
|
|
|
|
if (++num == max_num) {
|
|
|
|
stored_rc = smb2_lockv(xid, tcon,
|
|
|
|
cfile->fid.persistent_fid,
|
|
|
|
cfile->fid.volatile_fid,
|
|
|
|
current->tgid, num, buf);
|
|
|
|
if (stored_rc) {
|
|
|
|
/*
|
|
|
|
* We failed on the unlock range request - add
|
|
|
|
* all locks from the tmp list to the head of
|
|
|
|
* the file's list.
|
|
|
|
*/
|
|
|
|
cifs_move_llist(&tmp_llist,
|
|
|
|
&cfile->llist->locks);
|
|
|
|
rc = stored_rc;
|
|
|
|
} else
|
|
|
|
/*
|
|
|
|
* The unlock range request succeed - free the
|
|
|
|
* tmp list.
|
|
|
|
*/
|
|
|
|
cifs_free_llist(&tmp_llist);
|
|
|
|
cur = buf;
|
|
|
|
num = 0;
|
|
|
|
} else
|
|
|
|
cur++;
|
|
|
|
}
|
|
|
|
if (num) {
|
|
|
|
stored_rc = smb2_lockv(xid, tcon, cfile->fid.persistent_fid,
|
|
|
|
cfile->fid.volatile_fid, current->tgid,
|
|
|
|
num, buf);
|
|
|
|
if (stored_rc) {
|
|
|
|
cifs_move_llist(&tmp_llist, &cfile->llist->locks);
|
|
|
|
rc = stored_rc;
|
|
|
|
} else
|
|
|
|
cifs_free_llist(&tmp_llist);
|
|
|
|
}
|
2012-09-19 13:22:44 +00:00
|
|
|
up_write(&cinode->lock_sem);
|
2012-09-19 13:22:43 +00:00
|
|
|
|
|
|
|
kfree(buf);
|
|
|
|
return rc;
|
|
|
|
}
|
2012-09-19 13:22:44 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
smb2_push_mand_fdlocks(struct cifs_fid_locks *fdlocks, const unsigned int xid,
|
|
|
|
struct smb2_lock_element *buf, unsigned int max_num)
|
|
|
|
{
|
|
|
|
int rc = 0, stored_rc;
|
|
|
|
struct cifsFileInfo *cfile = fdlocks->cfile;
|
|
|
|
struct cifsLockInfo *li;
|
|
|
|
unsigned int num = 0;
|
|
|
|
struct smb2_lock_element *cur = buf;
|
|
|
|
struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
|
|
|
|
|
|
|
|
list_for_each_entry(li, &fdlocks->locks, llist) {
|
|
|
|
cur->Length = cpu_to_le64(li->length);
|
|
|
|
cur->Offset = cpu_to_le64(li->offset);
|
|
|
|
cur->Flags = cpu_to_le32(li->type |
|
|
|
|
SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
|
|
|
|
if (++num == max_num) {
|
|
|
|
stored_rc = smb2_lockv(xid, tcon,
|
|
|
|
cfile->fid.persistent_fid,
|
|
|
|
cfile->fid.volatile_fid,
|
|
|
|
current->tgid, num, buf);
|
|
|
|
if (stored_rc)
|
|
|
|
rc = stored_rc;
|
|
|
|
cur = buf;
|
|
|
|
num = 0;
|
|
|
|
} else
|
|
|
|
cur++;
|
|
|
|
}
|
|
|
|
if (num) {
|
|
|
|
stored_rc = smb2_lockv(xid, tcon,
|
|
|
|
cfile->fid.persistent_fid,
|
|
|
|
cfile->fid.volatile_fid,
|
|
|
|
current->tgid, num, buf);
|
|
|
|
if (stored_rc)
|
|
|
|
rc = stored_rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
smb2_push_mandatory_locks(struct cifsFileInfo *cfile)
|
|
|
|
{
|
|
|
|
int rc = 0, stored_rc;
|
|
|
|
unsigned int xid;
|
|
|
|
unsigned int max_num, max_buf;
|
|
|
|
struct smb2_lock_element *buf;
|
2015-03-17 22:25:59 +00:00
|
|
|
struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
|
2012-09-19 13:22:44 +00:00
|
|
|
struct cifs_fid_locks *fdlocks;
|
|
|
|
|
|
|
|
xid = get_xid();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Accessing maxBuf is racy with cifs_reconnect - need to store value
|
|
|
|
* and check it for zero before using.
|
|
|
|
*/
|
|
|
|
max_buf = tlink_tcon(cfile->tlink)->ses->server->maxBuf;
|
2016-11-30 00:14:43 +00:00
|
|
|
if (max_buf < sizeof(struct smb2_lock_element)) {
|
2012-09-19 13:22:44 +00:00
|
|
|
free_xid(xid);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2019-01-08 18:30:56 +00:00
|
|
|
BUILD_BUG_ON(sizeof(struct smb2_lock_element) > PAGE_SIZE);
|
|
|
|
max_buf = min_t(unsigned int, max_buf, PAGE_SIZE);
|
2012-09-19 13:22:44 +00:00
|
|
|
max_num = max_buf / sizeof(struct smb2_lock_element);
|
2014-12-10 23:41:20 +00:00
|
|
|
buf = kcalloc(max_num, sizeof(struct smb2_lock_element), GFP_KERNEL);
|
2012-09-19 13:22:44 +00:00
|
|
|
if (!buf) {
|
|
|
|
free_xid(xid);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
list_for_each_entry(fdlocks, &cinode->llist, llist) {
|
|
|
|
stored_rc = smb2_push_mand_fdlocks(fdlocks, xid, buf, max_num);
|
|
|
|
if (stored_rc)
|
|
|
|
rc = stored_rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
kfree(buf);
|
|
|
|
free_xid(xid);
|
|
|
|
return rc;
|
|
|
|
}
|