Pavel Shilovsky | f0df737 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 37 | int |
Pavel Shilovsky | 226730b | 2013-07-05 12:00:30 +0400 | [diff] [blame] | 38 | smb2_open_file(const unsigned int xid, struct cifs_open_parms *oparms, |
| 39 | __u32 *oplock, FILE_ALL_INFO *buf) |
Pavel Shilovsky | f0df737 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 40 | { |
| 41 | int rc; |
| 42 | __le16 *smb2_path; |
| 43 | struct smb2_file_all_info *smb2_data = NULL; |
Stefano Brivio | 729c0c9 | 2018-07-05 15:10:02 +0200 | [diff] [blame] | 44 | __u8 smb2_oplock; |
Pavel Shilovsky | 226730b | 2013-07-05 12:00:30 +0400 | [diff] [blame] | 45 | struct cifs_fid *fid = oparms->fid; |
Steve French | 592fafe | 2015-11-03 10:08:53 -0600 | [diff] [blame] | 46 | struct network_resiliency_req nr_ioctl_req; |
Pavel Shilovsky | f0df737 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 47 | |
Pavel Shilovsky | 226730b | 2013-07-05 12:00:30 +0400 | [diff] [blame] | 48 | smb2_path = cifs_convert_path_to_utf16(oparms->path, oparms->cifs_sb); |
Pavel Shilovsky | f0df737 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 49 | if (smb2_path == NULL) { |
| 50 | rc = -ENOMEM; |
| 51 | goto out; |
| 52 | } |
| 53 | |
Pavel Shilovsky | 1bbe499 | 2014-08-22 13:32:11 +0400 | [diff] [blame] | 54 | smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2, |
Pavel Shilovsky | f0df737 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 55 | GFP_KERNEL); |
| 56 | if (smb2_data == NULL) { |
| 57 | rc = -ENOMEM; |
| 58 | goto out; |
| 59 | } |
| 60 | |
Pavel Shilovsky | 226730b | 2013-07-05 12:00:30 +0400 | [diff] [blame] | 61 | oparms->desired_access |= FILE_READ_ATTRIBUTES; |
Stefano Brivio | 729c0c9 | 2018-07-05 15:10:02 +0200 | [diff] [blame] | 62 | smb2_oplock = SMB2_OPLOCK_LEVEL_BATCH; |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 63 | |
Stefano Brivio | 729c0c9 | 2018-07-05 15:10:02 +0200 | [diff] [blame] | 64 | rc = SMB2_open(xid, oparms, smb2_path, &smb2_oplock, smb2_data, NULL, |
Ronnie Sahlberg | 9d874c3 | 2018-06-08 13:21:18 +1000 | [diff] [blame] | 65 | NULL); |
Pavel Shilovsky | f0df737 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 66 | if (rc) |
| 67 | goto out; |
| 68 | |
Steve French | 592fafe | 2015-11-03 10:08:53 -0600 | [diff] [blame] | 69 | |
| 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 Aptel | 5114662 | 2017-02-28 15:08:41 +0100 | [diff] [blame] | 74 | fid->volatile_fid, FSCTL_LMR_REQUEST_RESILIENCY, |
Aurelien Aptel | 63a83b8 | 2018-01-24 13:46:11 +0100 | [diff] [blame] | 75 | true /* is_fsctl */, |
Steve French | 592fafe | 2015-11-03 10:08:53 -0600 | [diff] [blame] | 76 | (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 Shilovsky | f0df737 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 88 | if (buf) { |
| 89 | /* open response does not have IndexNumber field - get it */ |
Pavel Shilovsky | 226730b | 2013-07-05 12:00:30 +0400 | [diff] [blame] | 90 | rc = SMB2_get_srv_num(xid, oparms->tcon, fid->persistent_fid, |
Pavel Shilovsky | f0df737 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 91 | 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 Brivio | 729c0c9 | 2018-07-05 15:10:02 +0200 | [diff] [blame] | 101 | *oplock = smb2_oplock; |
Pavel Shilovsky | f0df737 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 102 | out: |
Pavel Shilovsky | f0df737 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 103 | kfree(smb2_data); |
| 104 | kfree(smb2_path); |
| 105 | return rc; |
| 106 | } |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 107 | |
| 108 | int |
| 109 | smb2_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 Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 116 | struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 117 | 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 Frederick | 662e9b2 | 2014-12-10 15:41:20 -0800 | [diff] [blame] | 132 | buf = kcalloc(max_num, sizeof(struct smb2_lock_element), GFP_KERNEL); |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 133 | if (!buf) |
| 134 | return -ENOMEM; |
| 135 | |
| 136 | cur = buf; |
| 137 | |
Pavel Shilovsky | 1b4b55a | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 138 | down_write(&cinode->lock_sem); |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 139 | 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 Shilovsky | 1b4b55a | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 199 | up_write(&cinode->lock_sem); |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 200 | |
| 201 | kfree(buf); |
| 202 | return rc; |
| 203 | } |
Pavel Shilovsky | b140799 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 204 | |
| 205 | static int |
| 206 | smb2_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 | |
| 245 | int |
| 246 | smb2_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 Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 252 | struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); |
Pavel Shilovsky | b140799 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 253 | struct cifs_fid_locks *fdlocks; |
| 254 | |
| 255 | xid = get_xid(); |
Pavel Shilovsky | b140799 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 256 | |
| 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 Shilovsky | e3d240e | 2016-11-29 16:14:43 -0800 | [diff] [blame] | 262 | if (max_buf < sizeof(struct smb2_lock_element)) { |
Pavel Shilovsky | b140799 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 263 | free_xid(xid); |
| 264 | return -EINVAL; |
| 265 | } |
| 266 | |
| 267 | max_num = max_buf / sizeof(struct smb2_lock_element); |
Fabian Frederick | 662e9b2 | 2014-12-10 15:41:20 -0800 | [diff] [blame] | 268 | buf = kcalloc(max_num, sizeof(struct smb2_lock_element), GFP_KERNEL); |
Pavel Shilovsky | b140799 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 269 | if (!buf) { |
Pavel Shilovsky | b140799 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 270 | 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 Shilovsky | b140799 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 280 | kfree(buf); |
Pavel Shilovsky | b140799 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 281 | free_xid(xid); |
| 282 | return rc; |
| 283 | } |