371 lines · c
1// SPDX-License-Identifier: LGPL-2.12/*3 *4 * Copyright (C) International Business Machines Corp., 2002, 20115 * Author(s): Steve French (sfrench@us.ibm.com),6 * Pavel Shilovsky ((pshilovsky@samba.org) 20127 *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 "../common/smb2status.h"25 26static 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 66int 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 100int 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 inode172 * numbers173 */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;182out:183 free_rsp_buf(err_buftype, err_iov.iov_base);184 kfree(smb2_path);185 return rc;186}187 188int189smb2_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 LIST_HEAD(tmp_llist);200 201 /*202 * Accessing maxBuf is racy with cifs_reconnect - need to store value203 * and check it before using.204 */205 max_buf = tcon->ses->server->maxBuf;206 if (max_buf < sizeof(struct smb2_lock_element))207 return -EINVAL;208 209 BUILD_BUG_ON(sizeof(struct smb2_lock_element) > PAGE_SIZE);210 max_buf = min_t(unsigned int, max_buf, PAGE_SIZE);211 max_num = max_buf / sizeof(struct smb2_lock_element);212 buf = kcalloc(max_num, sizeof(struct smb2_lock_element), GFP_KERNEL);213 if (!buf)214 return -ENOMEM;215 216 cur = buf;217 218 cifs_down_write(&cinode->lock_sem);219 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {220 if (flock->fl_start > li->offset ||221 (flock->fl_start + length) <222 (li->offset + li->length))223 continue;224 if (current->tgid != li->pid)225 /*226 * flock and OFD lock are associated with an open227 * file description, not the process.228 */229 if (!(flock->c.flc_flags & (FL_FLOCK | FL_OFDLCK)))230 continue;231 if (cinode->can_cache_brlcks) {232 /*233 * We can cache brlock requests - simply remove a lock234 * from the file's list.235 */236 list_del(&li->llist);237 cifs_del_lock_waiters(li);238 kfree(li);239 continue;240 }241 cur->Length = cpu_to_le64(li->length);242 cur->Offset = cpu_to_le64(li->offset);243 cur->Flags = cpu_to_le32(SMB2_LOCKFLAG_UNLOCK);244 /*245 * We need to save a lock here to let us add it again to the246 * file's list if the unlock range request fails on the server.247 */248 list_move(&li->llist, &tmp_llist);249 if (++num == max_num) {250 stored_rc = smb2_lockv(xid, tcon,251 cfile->fid.persistent_fid,252 cfile->fid.volatile_fid,253 current->tgid, num, buf);254 if (stored_rc) {255 /*256 * We failed on the unlock range request - add257 * all locks from the tmp list to the head of258 * the file's list.259 */260 cifs_move_llist(&tmp_llist,261 &cfile->llist->locks);262 rc = stored_rc;263 } else264 /*265 * The unlock range request succeed - free the266 * tmp list.267 */268 cifs_free_llist(&tmp_llist);269 cur = buf;270 num = 0;271 } else272 cur++;273 }274 if (num) {275 stored_rc = smb2_lockv(xid, tcon, cfile->fid.persistent_fid,276 cfile->fid.volatile_fid, current->tgid,277 num, buf);278 if (stored_rc) {279 cifs_move_llist(&tmp_llist, &cfile->llist->locks);280 rc = stored_rc;281 } else282 cifs_free_llist(&tmp_llist);283 }284 up_write(&cinode->lock_sem);285 286 kfree(buf);287 return rc;288}289 290static int291smb2_push_mand_fdlocks(struct cifs_fid_locks *fdlocks, const unsigned int xid,292 struct smb2_lock_element *buf, unsigned int max_num)293{294 int rc = 0, stored_rc;295 struct cifsFileInfo *cfile = fdlocks->cfile;296 struct cifsLockInfo *li;297 unsigned int num = 0;298 struct smb2_lock_element *cur = buf;299 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);300 301 list_for_each_entry(li, &fdlocks->locks, llist) {302 cur->Length = cpu_to_le64(li->length);303 cur->Offset = cpu_to_le64(li->offset);304 cur->Flags = cpu_to_le32(li->type |305 SMB2_LOCKFLAG_FAIL_IMMEDIATELY);306 if (++num == max_num) {307 stored_rc = smb2_lockv(xid, tcon,308 cfile->fid.persistent_fid,309 cfile->fid.volatile_fid,310 current->tgid, num, buf);311 if (stored_rc)312 rc = stored_rc;313 cur = buf;314 num = 0;315 } else316 cur++;317 }318 if (num) {319 stored_rc = smb2_lockv(xid, tcon,320 cfile->fid.persistent_fid,321 cfile->fid.volatile_fid,322 current->tgid, num, buf);323 if (stored_rc)324 rc = stored_rc;325 }326 327 return rc;328}329 330int331smb2_push_mandatory_locks(struct cifsFileInfo *cfile)332{333 int rc = 0, stored_rc;334 unsigned int xid;335 unsigned int max_num, max_buf;336 struct smb2_lock_element *buf;337 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));338 struct cifs_fid_locks *fdlocks;339 340 xid = get_xid();341 342 /*343 * Accessing maxBuf is racy with cifs_reconnect - need to store value344 * and check it for zero before using.345 */346 max_buf = tlink_tcon(cfile->tlink)->ses->server->maxBuf;347 if (max_buf < sizeof(struct smb2_lock_element)) {348 free_xid(xid);349 return -EINVAL;350 }351 352 BUILD_BUG_ON(sizeof(struct smb2_lock_element) > PAGE_SIZE);353 max_buf = min_t(unsigned int, max_buf, PAGE_SIZE);354 max_num = max_buf / sizeof(struct smb2_lock_element);355 buf = kcalloc(max_num, sizeof(struct smb2_lock_element), GFP_KERNEL);356 if (!buf) {357 free_xid(xid);358 return -ENOMEM;359 }360 361 list_for_each_entry(fdlocks, &cinode->llist, llist) {362 stored_rc = smb2_push_mand_fdlocks(fdlocks, xid, buf, max_num);363 if (stored_rc)364 rc = stored_rc;365 }366 367 kfree(buf);368 free_xid(xid);369 return rc;370}371