]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * fs/cifs/file.c | |
3 | * | |
4 | * vfs operations that deal with files | |
fb8c4b14 | 5 | * |
f19159dc | 6 | * Copyright (C) International Business Machines Corp., 2002,2010 |
1da177e4 | 7 | * Author(s): Steve French ([email protected]) |
7ee1af76 | 8 | * Jeremy Allison ([email protected]) |
1da177e4 LT |
9 | * |
10 | * This library is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU Lesser General Public License as published | |
12 | * by the Free Software Foundation; either version 2.1 of the License, or | |
13 | * (at your option) any later version. | |
14 | * | |
15 | * This library is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See | |
18 | * the GNU Lesser General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU Lesser General Public License | |
21 | * along with this library; if not, write to the Free Software | |
22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
23 | */ | |
24 | #include <linux/fs.h> | |
37c0eb46 | 25 | #include <linux/backing-dev.h> |
1da177e4 LT |
26 | #include <linux/stat.h> |
27 | #include <linux/fcntl.h> | |
28 | #include <linux/pagemap.h> | |
29 | #include <linux/pagevec.h> | |
37c0eb46 | 30 | #include <linux/writeback.h> |
6f88cc2e | 31 | #include <linux/task_io_accounting_ops.h> |
23e7dd7d | 32 | #include <linux/delay.h> |
3bc303c2 | 33 | #include <linux/mount.h> |
5a0e3ad6 | 34 | #include <linux/slab.h> |
690c5e31 | 35 | #include <linux/swap.h> |
f86196ea | 36 | #include <linux/mm.h> |
1da177e4 LT |
37 | #include <asm/div64.h> |
38 | #include "cifsfs.h" | |
39 | #include "cifspdu.h" | |
40 | #include "cifsglob.h" | |
41 | #include "cifsproto.h" | |
42 | #include "cifs_unicode.h" | |
43 | #include "cifs_debug.h" | |
44 | #include "cifs_fs_sb.h" | |
9451a9a5 | 45 | #include "fscache.h" |
bd3dcc6a | 46 | #include "smbdirect.h" |
07b92d0d | 47 | |
1da177e4 LT |
48 | static inline int cifs_convert_flags(unsigned int flags) |
49 | { | |
50 | if ((flags & O_ACCMODE) == O_RDONLY) | |
51 | return GENERIC_READ; | |
52 | else if ((flags & O_ACCMODE) == O_WRONLY) | |
53 | return GENERIC_WRITE; | |
54 | else if ((flags & O_ACCMODE) == O_RDWR) { | |
55 | /* GENERIC_ALL is too much permission to request | |
56 | can cause unnecessary access denied on create */ | |
57 | /* return GENERIC_ALL; */ | |
58 | return (GENERIC_READ | GENERIC_WRITE); | |
59 | } | |
60 | ||
e10f7b55 JL |
61 | return (READ_CONTROL | FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES | |
62 | FILE_WRITE_EA | FILE_APPEND_DATA | FILE_WRITE_DATA | | |
63 | FILE_READ_DATA); | |
7fc8f4e9 | 64 | } |
e10f7b55 | 65 | |
608712fe | 66 | static u32 cifs_posix_convert_flags(unsigned int flags) |
7fc8f4e9 | 67 | { |
608712fe | 68 | u32 posix_flags = 0; |
e10f7b55 | 69 | |
7fc8f4e9 | 70 | if ((flags & O_ACCMODE) == O_RDONLY) |
608712fe | 71 | posix_flags = SMB_O_RDONLY; |
7fc8f4e9 | 72 | else if ((flags & O_ACCMODE) == O_WRONLY) |
608712fe JL |
73 | posix_flags = SMB_O_WRONLY; |
74 | else if ((flags & O_ACCMODE) == O_RDWR) | |
75 | posix_flags = SMB_O_RDWR; | |
76 | ||
07b92d0d | 77 | if (flags & O_CREAT) { |
608712fe | 78 | posix_flags |= SMB_O_CREAT; |
07b92d0d SF |
79 | if (flags & O_EXCL) |
80 | posix_flags |= SMB_O_EXCL; | |
81 | } else if (flags & O_EXCL) | |
f96637be JP |
82 | cifs_dbg(FYI, "Application %s pid %d has incorrectly set O_EXCL flag but not O_CREAT on file open. Ignoring O_EXCL\n", |
83 | current->comm, current->tgid); | |
07b92d0d | 84 | |
608712fe JL |
85 | if (flags & O_TRUNC) |
86 | posix_flags |= SMB_O_TRUNC; | |
87 | /* be safe and imply O_SYNC for O_DSYNC */ | |
6b2f3d1f | 88 | if (flags & O_DSYNC) |
608712fe | 89 | posix_flags |= SMB_O_SYNC; |
7fc8f4e9 | 90 | if (flags & O_DIRECTORY) |
608712fe | 91 | posix_flags |= SMB_O_DIRECTORY; |
7fc8f4e9 | 92 | if (flags & O_NOFOLLOW) |
608712fe | 93 | posix_flags |= SMB_O_NOFOLLOW; |
7fc8f4e9 | 94 | if (flags & O_DIRECT) |
608712fe | 95 | posix_flags |= SMB_O_DIRECT; |
7fc8f4e9 SF |
96 | |
97 | return posix_flags; | |
1da177e4 LT |
98 | } |
99 | ||
100 | static inline int cifs_get_disposition(unsigned int flags) | |
101 | { | |
102 | if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) | |
103 | return FILE_CREATE; | |
104 | else if ((flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC)) | |
105 | return FILE_OVERWRITE_IF; | |
106 | else if ((flags & O_CREAT) == O_CREAT) | |
107 | return FILE_OPEN_IF; | |
55aa2e09 SF |
108 | else if ((flags & O_TRUNC) == O_TRUNC) |
109 | return FILE_OVERWRITE; | |
1da177e4 LT |
110 | else |
111 | return FILE_OPEN; | |
112 | } | |
113 | ||
608712fe JL |
114 | int cifs_posix_open(char *full_path, struct inode **pinode, |
115 | struct super_block *sb, int mode, unsigned int f_flags, | |
6d5786a3 | 116 | __u32 *poplock, __u16 *pnetfid, unsigned int xid) |
608712fe JL |
117 | { |
118 | int rc; | |
119 | FILE_UNIX_BASIC_INFO *presp_data; | |
120 | __u32 posix_flags = 0; | |
121 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb); | |
122 | struct cifs_fattr fattr; | |
123 | struct tcon_link *tlink; | |
96daf2b0 | 124 | struct cifs_tcon *tcon; |
608712fe | 125 | |
f96637be | 126 | cifs_dbg(FYI, "posix open %s\n", full_path); |
608712fe JL |
127 | |
128 | presp_data = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL); | |
129 | if (presp_data == NULL) | |
130 | return -ENOMEM; | |
131 | ||
132 | tlink = cifs_sb_tlink(cifs_sb); | |
133 | if (IS_ERR(tlink)) { | |
134 | rc = PTR_ERR(tlink); | |
135 | goto posix_open_ret; | |
136 | } | |
137 | ||
138 | tcon = tlink_tcon(tlink); | |
139 | mode &= ~current_umask(); | |
140 | ||
141 | posix_flags = cifs_posix_convert_flags(f_flags); | |
142 | rc = CIFSPOSIXCreate(xid, tcon, posix_flags, mode, pnetfid, presp_data, | |
143 | poplock, full_path, cifs_sb->local_nls, | |
bc8ebdc4 | 144 | cifs_remap(cifs_sb)); |
608712fe JL |
145 | cifs_put_tlink(tlink); |
146 | ||
147 | if (rc) | |
148 | goto posix_open_ret; | |
149 | ||
150 | if (presp_data->Type == cpu_to_le32(-1)) | |
151 | goto posix_open_ret; /* open ok, caller does qpathinfo */ | |
152 | ||
153 | if (!pinode) | |
154 | goto posix_open_ret; /* caller does not need info */ | |
155 | ||
156 | cifs_unix_basic_to_fattr(&fattr, presp_data, cifs_sb); | |
157 | ||
158 | /* get new inode and set it up */ | |
159 | if (*pinode == NULL) { | |
160 | cifs_fill_uniqueid(sb, &fattr); | |
161 | *pinode = cifs_iget(sb, &fattr); | |
162 | if (!*pinode) { | |
163 | rc = -ENOMEM; | |
164 | goto posix_open_ret; | |
165 | } | |
166 | } else { | |
167 | cifs_fattr_to_inode(*pinode, &fattr); | |
168 | } | |
169 | ||
170 | posix_open_ret: | |
171 | kfree(presp_data); | |
172 | return rc; | |
173 | } | |
174 | ||
eeb910a6 PS |
175 | static int |
176 | cifs_nt_open(char *full_path, struct inode *inode, struct cifs_sb_info *cifs_sb, | |
fb1214e4 PS |
177 | struct cifs_tcon *tcon, unsigned int f_flags, __u32 *oplock, |
178 | struct cifs_fid *fid, unsigned int xid) | |
eeb910a6 PS |
179 | { |
180 | int rc; | |
fb1214e4 | 181 | int desired_access; |
eeb910a6 | 182 | int disposition; |
3d3ea8e6 | 183 | int create_options = CREATE_NOT_DIR; |
eeb910a6 | 184 | FILE_ALL_INFO *buf; |
b8c32dbb | 185 | struct TCP_Server_Info *server = tcon->ses->server; |
226730b4 | 186 | struct cifs_open_parms oparms; |
eeb910a6 | 187 | |
b8c32dbb | 188 | if (!server->ops->open) |
fb1214e4 PS |
189 | return -ENOSYS; |
190 | ||
191 | desired_access = cifs_convert_flags(f_flags); | |
eeb910a6 PS |
192 | |
193 | /********************************************************************* | |
194 | * open flag mapping table: | |
195 | * | |
196 | * POSIX Flag CIFS Disposition | |
197 | * ---------- ---------------- | |
198 | * O_CREAT FILE_OPEN_IF | |
199 | * O_CREAT | O_EXCL FILE_CREATE | |
200 | * O_CREAT | O_TRUNC FILE_OVERWRITE_IF | |
201 | * O_TRUNC FILE_OVERWRITE | |
202 | * none of the above FILE_OPEN | |
203 | * | |
204 | * Note that there is not a direct match between disposition | |
205 | * FILE_SUPERSEDE (ie create whether or not file exists although | |
206 | * O_CREAT | O_TRUNC is similar but truncates the existing | |
207 | * file rather than creating a new file as FILE_SUPERSEDE does | |
208 | * (which uses the attributes / metadata passed in on open call) | |
209 | *? | |
210 | *? O_SYNC is a reasonable match to CIFS writethrough flag | |
211 | *? and the read write flags match reasonably. O_LARGEFILE | |
212 | *? is irrelevant because largefile support is always used | |
213 | *? by this client. Flags O_APPEND, O_DIRECT, O_DIRECTORY, | |
214 | * O_FASYNC, O_NOFOLLOW, O_NONBLOCK need further investigation | |
215 | *********************************************************************/ | |
216 | ||
217 | disposition = cifs_get_disposition(f_flags); | |
218 | ||
219 | /* BB pass O_SYNC flag through on file attributes .. BB */ | |
220 | ||
221 | buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL); | |
222 | if (!buf) | |
223 | return -ENOMEM; | |
224 | ||
3d3ea8e6 SP |
225 | if (backup_cred(cifs_sb)) |
226 | create_options |= CREATE_OPEN_BACKUP_INTENT; | |
227 | ||
1013e760 SF |
228 | /* O_SYNC also has bit for O_DSYNC so following check picks up either */ |
229 | if (f_flags & O_SYNC) | |
230 | create_options |= CREATE_WRITE_THROUGH; | |
231 | ||
232 | if (f_flags & O_DIRECT) | |
233 | create_options |= CREATE_NO_BUFFER; | |
234 | ||
226730b4 PS |
235 | oparms.tcon = tcon; |
236 | oparms.cifs_sb = cifs_sb; | |
237 | oparms.desired_access = desired_access; | |
238 | oparms.create_options = create_options; | |
239 | oparms.disposition = disposition; | |
240 | oparms.path = full_path; | |
241 | oparms.fid = fid; | |
9cbc0b73 | 242 | oparms.reconnect = false; |
226730b4 PS |
243 | |
244 | rc = server->ops->open(xid, &oparms, oplock, buf); | |
eeb910a6 PS |
245 | |
246 | if (rc) | |
247 | goto out; | |
248 | ||
249 | if (tcon->unix_ext) | |
250 | rc = cifs_get_inode_info_unix(&inode, full_path, inode->i_sb, | |
251 | xid); | |
252 | else | |
253 | rc = cifs_get_inode_info(&inode, full_path, buf, inode->i_sb, | |
42eacf9e | 254 | xid, fid); |
eeb910a6 | 255 | |
30573a82 PS |
256 | if (rc) { |
257 | server->ops->close(xid, tcon, fid); | |
258 | if (rc == -ESTALE) | |
259 | rc = -EOPENSTALE; | |
260 | } | |
261 | ||
eeb910a6 PS |
262 | out: |
263 | kfree(buf); | |
264 | return rc; | |
265 | } | |
266 | ||
63b7d3a4 PS |
267 | static bool |
268 | cifs_has_mand_locks(struct cifsInodeInfo *cinode) | |
269 | { | |
270 | struct cifs_fid_locks *cur; | |
271 | bool has_locks = false; | |
272 | ||
273 | down_read(&cinode->lock_sem); | |
274 | list_for_each_entry(cur, &cinode->llist, llist) { | |
275 | if (!list_empty(&cur->locks)) { | |
276 | has_locks = true; | |
277 | break; | |
278 | } | |
279 | } | |
280 | up_read(&cinode->lock_sem); | |
281 | return has_locks; | |
282 | } | |
283 | ||
d46b0da7 DW |
284 | void |
285 | cifs_down_write(struct rw_semaphore *sem) | |
286 | { | |
287 | while (!down_write_trylock(sem)) | |
288 | msleep(10); | |
289 | } | |
290 | ||
15ecb436 | 291 | struct cifsFileInfo * |
fb1214e4 | 292 | cifs_new_fileinfo(struct cifs_fid *fid, struct file *file, |
15ecb436 JL |
293 | struct tcon_link *tlink, __u32 oplock) |
294 | { | |
1f1735cb | 295 | struct dentry *dentry = file_dentry(file); |
2b0143b5 | 296 | struct inode *inode = d_inode(dentry); |
4b4de76e PS |
297 | struct cifsInodeInfo *cinode = CIFS_I(inode); |
298 | struct cifsFileInfo *cfile; | |
f45d3416 | 299 | struct cifs_fid_locks *fdlocks; |
233839b1 | 300 | struct cifs_tcon *tcon = tlink_tcon(tlink); |
63b7d3a4 | 301 | struct TCP_Server_Info *server = tcon->ses->server; |
4b4de76e PS |
302 | |
303 | cfile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL); | |
304 | if (cfile == NULL) | |
305 | return cfile; | |
306 | ||
f45d3416 PS |
307 | fdlocks = kzalloc(sizeof(struct cifs_fid_locks), GFP_KERNEL); |
308 | if (!fdlocks) { | |
309 | kfree(cfile); | |
310 | return NULL; | |
311 | } | |
312 | ||
313 | INIT_LIST_HEAD(&fdlocks->locks); | |
314 | fdlocks->cfile = cfile; | |
315 | cfile->llist = fdlocks; | |
d46b0da7 | 316 | cifs_down_write(&cinode->lock_sem); |
f45d3416 | 317 | list_add(&fdlocks->llist, &cinode->llist); |
1b4b55a1 | 318 | up_write(&cinode->lock_sem); |
f45d3416 | 319 | |
4b4de76e | 320 | cfile->count = 1; |
4b4de76e PS |
321 | cfile->pid = current->tgid; |
322 | cfile->uid = current_fsuid(); | |
323 | cfile->dentry = dget(dentry); | |
324 | cfile->f_flags = file->f_flags; | |
325 | cfile->invalidHandle = false; | |
326 | cfile->tlink = cifs_get_tlink(tlink); | |
4b4de76e | 327 | INIT_WORK(&cfile->oplock_break, cifs_oplock_break); |
f45d3416 | 328 | mutex_init(&cfile->fh_mutex); |
3afca265 | 329 | spin_lock_init(&cfile->file_info_lock); |
15ecb436 | 330 | |
24261fc2 MG |
331 | cifs_sb_active(inode->i_sb); |
332 | ||
63b7d3a4 PS |
333 | /* |
334 | * If the server returned a read oplock and we have mandatory brlocks, | |
335 | * set oplock level to None. | |
336 | */ | |
53ef1016 | 337 | if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) { |
f96637be | 338 | cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n"); |
63b7d3a4 PS |
339 | oplock = 0; |
340 | } | |
341 | ||
3afca265 | 342 | spin_lock(&tcon->open_file_lock); |
63b7d3a4 | 343 | if (fid->pending_open->oplock != CIFS_OPLOCK_NO_CHANGE && oplock) |
233839b1 PS |
344 | oplock = fid->pending_open->oplock; |
345 | list_del(&fid->pending_open->olist); | |
346 | ||
42873b0a | 347 | fid->purge_cache = false; |
63b7d3a4 | 348 | server->ops->set_fid(cfile, fid, oplock); |
233839b1 PS |
349 | |
350 | list_add(&cfile->tlist, &tcon->openFileList); | |
fae8044c | 351 | atomic_inc(&tcon->num_local_opens); |
3afca265 | 352 | |
15ecb436 | 353 | /* if readable file instance put first in list*/ |
487317c9 | 354 | spin_lock(&cinode->open_file_lock); |
15ecb436 | 355 | if (file->f_mode & FMODE_READ) |
4b4de76e | 356 | list_add(&cfile->flist, &cinode->openFileList); |
15ecb436 | 357 | else |
4b4de76e | 358 | list_add_tail(&cfile->flist, &cinode->openFileList); |
487317c9 | 359 | spin_unlock(&cinode->open_file_lock); |
3afca265 | 360 | spin_unlock(&tcon->open_file_lock); |
15ecb436 | 361 | |
42873b0a | 362 | if (fid->purge_cache) |
4f73c7d3 | 363 | cifs_zap_mapping(inode); |
42873b0a | 364 | |
4b4de76e PS |
365 | file->private_data = cfile; |
366 | return cfile; | |
15ecb436 JL |
367 | } |
368 | ||
764a1b1a JL |
369 | struct cifsFileInfo * |
370 | cifsFileInfo_get(struct cifsFileInfo *cifs_file) | |
371 | { | |
3afca265 | 372 | spin_lock(&cifs_file->file_info_lock); |
764a1b1a | 373 | cifsFileInfo_get_locked(cifs_file); |
3afca265 | 374 | spin_unlock(&cifs_file->file_info_lock); |
764a1b1a JL |
375 | return cifs_file; |
376 | } | |
377 | ||
b98749ca AA |
378 | /** |
379 | * cifsFileInfo_put - release a reference of file priv data | |
380 | * | |
381 | * Always potentially wait for oplock handler. See _cifsFileInfo_put(). | |
cdff08e7 | 382 | */ |
b33879aa | 383 | void cifsFileInfo_put(struct cifsFileInfo *cifs_file) |
b98749ca AA |
384 | { |
385 | _cifsFileInfo_put(cifs_file, true); | |
386 | } | |
387 | ||
388 | /** | |
389 | * _cifsFileInfo_put - release a reference of file priv data | |
390 | * | |
391 | * This may involve closing the filehandle @cifs_file out on the | |
392 | * server. Must be called without holding tcon->open_file_lock and | |
393 | * cifs_file->file_info_lock. | |
394 | * | |
395 | * If @wait_for_oplock_handler is true and we are releasing the last | |
396 | * reference, wait for any running oplock break handler of the file | |
397 | * and cancel any pending one. If calling this function from the | |
398 | * oplock break handler, you need to pass false. | |
399 | * | |
400 | */ | |
401 | void _cifsFileInfo_put(struct cifsFileInfo *cifs_file, bool wait_oplock_handler) | |
b33879aa | 402 | { |
2b0143b5 | 403 | struct inode *inode = d_inode(cifs_file->dentry); |
96daf2b0 | 404 | struct cifs_tcon *tcon = tlink_tcon(cifs_file->tlink); |
233839b1 | 405 | struct TCP_Server_Info *server = tcon->ses->server; |
e66673e3 | 406 | struct cifsInodeInfo *cifsi = CIFS_I(inode); |
24261fc2 MG |
407 | struct super_block *sb = inode->i_sb; |
408 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb); | |
cdff08e7 | 409 | struct cifsLockInfo *li, *tmp; |
233839b1 PS |
410 | struct cifs_fid fid; |
411 | struct cifs_pending_open open; | |
ca7df8e0 | 412 | bool oplock_break_cancelled; |
cdff08e7 | 413 | |
3afca265 | 414 | spin_lock(&tcon->open_file_lock); |
1a67c415 | 415 | spin_lock(&cifsi->open_file_lock); |
3afca265 | 416 | spin_lock(&cifs_file->file_info_lock); |
5f6dbc9e | 417 | if (--cifs_file->count > 0) { |
3afca265 | 418 | spin_unlock(&cifs_file->file_info_lock); |
1a67c415 | 419 | spin_unlock(&cifsi->open_file_lock); |
3afca265 | 420 | spin_unlock(&tcon->open_file_lock); |
cdff08e7 SF |
421 | return; |
422 | } | |
3afca265 | 423 | spin_unlock(&cifs_file->file_info_lock); |
cdff08e7 | 424 | |
233839b1 PS |
425 | if (server->ops->get_lease_key) |
426 | server->ops->get_lease_key(inode, &fid); | |
427 | ||
428 | /* store open in pending opens to make sure we don't miss lease break */ | |
429 | cifs_add_pending_open_locked(&fid, cifs_file->tlink, &open); | |
430 | ||
cdff08e7 SF |
431 | /* remove it from the lists */ |
432 | list_del(&cifs_file->flist); | |
433 | list_del(&cifs_file->tlist); | |
fae8044c | 434 | atomic_dec(&tcon->num_local_opens); |
cdff08e7 SF |
435 | |
436 | if (list_empty(&cifsi->openFileList)) { | |
f96637be | 437 | cifs_dbg(FYI, "closing last open instance for inode %p\n", |
2b0143b5 | 438 | d_inode(cifs_file->dentry)); |
25364138 PS |
439 | /* |
440 | * In strict cache mode we need invalidate mapping on the last | |
441 | * close because it may cause a error when we open this file | |
442 | * again and get at least level II oplock. | |
443 | */ | |
4f8ba8a0 | 444 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) |
aff8d5ca | 445 | set_bit(CIFS_INO_INVALID_MAPPING, &cifsi->flags); |
c6723628 | 446 | cifs_set_oplock_level(cifsi, 0); |
cdff08e7 | 447 | } |
3afca265 | 448 | |
1a67c415 | 449 | spin_unlock(&cifsi->open_file_lock); |
3afca265 | 450 | spin_unlock(&tcon->open_file_lock); |
cdff08e7 | 451 | |
b98749ca AA |
452 | oplock_break_cancelled = wait_oplock_handler ? |
453 | cancel_work_sync(&cifs_file->oplock_break) : false; | |
ad635942 | 454 | |
cdff08e7 | 455 | if (!tcon->need_reconnect && !cifs_file->invalidHandle) { |
0ff78a22 | 456 | struct TCP_Server_Info *server = tcon->ses->server; |
6d5786a3 | 457 | unsigned int xid; |
0ff78a22 | 458 | |
6d5786a3 | 459 | xid = get_xid(); |
0ff78a22 | 460 | if (server->ops->close) |
760ad0ca PS |
461 | server->ops->close(xid, tcon, &cifs_file->fid); |
462 | _free_xid(xid); | |
cdff08e7 SF |
463 | } |
464 | ||
ca7df8e0 SP |
465 | if (oplock_break_cancelled) |
466 | cifs_done_oplock_break(cifsi); | |
467 | ||
233839b1 PS |
468 | cifs_del_pending_open(&open); |
469 | ||
f45d3416 PS |
470 | /* |
471 | * Delete any outstanding lock records. We'll lose them when the file | |
cdff08e7 SF |
472 | * is closed anyway. |
473 | */ | |
d46b0da7 | 474 | cifs_down_write(&cifsi->lock_sem); |
f45d3416 | 475 | list_for_each_entry_safe(li, tmp, &cifs_file->llist->locks, llist) { |
cdff08e7 | 476 | list_del(&li->llist); |
85160e03 | 477 | cifs_del_lock_waiters(li); |
cdff08e7 | 478 | kfree(li); |
b33879aa | 479 | } |
f45d3416 PS |
480 | list_del(&cifs_file->llist->llist); |
481 | kfree(cifs_file->llist); | |
1b4b55a1 | 482 | up_write(&cifsi->lock_sem); |
cdff08e7 SF |
483 | |
484 | cifs_put_tlink(cifs_file->tlink); | |
485 | dput(cifs_file->dentry); | |
24261fc2 | 486 | cifs_sb_deactive(sb); |
cdff08e7 | 487 | kfree(cifs_file); |
b33879aa JL |
488 | } |
489 | ||
1da177e4 | 490 | int cifs_open(struct inode *inode, struct file *file) |
233839b1 | 491 | |
1da177e4 LT |
492 | { |
493 | int rc = -EACCES; | |
6d5786a3 | 494 | unsigned int xid; |
590a3fe0 | 495 | __u32 oplock; |
1da177e4 | 496 | struct cifs_sb_info *cifs_sb; |
b8c32dbb | 497 | struct TCP_Server_Info *server; |
96daf2b0 | 498 | struct cifs_tcon *tcon; |
7ffec372 | 499 | struct tcon_link *tlink; |
fb1214e4 | 500 | struct cifsFileInfo *cfile = NULL; |
1da177e4 | 501 | char *full_path = NULL; |
7e12eddb | 502 | bool posix_open_ok = false; |
fb1214e4 | 503 | struct cifs_fid fid; |
233839b1 | 504 | struct cifs_pending_open open; |
1da177e4 | 505 | |
6d5786a3 | 506 | xid = get_xid(); |
1da177e4 LT |
507 | |
508 | cifs_sb = CIFS_SB(inode->i_sb); | |
7ffec372 JL |
509 | tlink = cifs_sb_tlink(cifs_sb); |
510 | if (IS_ERR(tlink)) { | |
6d5786a3 | 511 | free_xid(xid); |
7ffec372 JL |
512 | return PTR_ERR(tlink); |
513 | } | |
514 | tcon = tlink_tcon(tlink); | |
b8c32dbb | 515 | server = tcon->ses->server; |
1da177e4 | 516 | |
1f1735cb | 517 | full_path = build_path_from_dentry(file_dentry(file)); |
1da177e4 | 518 | if (full_path == NULL) { |
0f3bc09e | 519 | rc = -ENOMEM; |
232341ba | 520 | goto out; |
1da177e4 LT |
521 | } |
522 | ||
f96637be | 523 | cifs_dbg(FYI, "inode = 0x%p file flags are 0x%x for %s\n", |
b6b38f70 | 524 | inode, file->f_flags, full_path); |
276a74a4 | 525 | |
787aded6 NJ |
526 | if (file->f_flags & O_DIRECT && |
527 | cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) { | |
528 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL) | |
529 | file->f_op = &cifs_file_direct_nobrl_ops; | |
530 | else | |
531 | file->f_op = &cifs_file_direct_ops; | |
532 | } | |
533 | ||
233839b1 | 534 | if (server->oplocks) |
276a74a4 SF |
535 | oplock = REQ_OPLOCK; |
536 | else | |
537 | oplock = 0; | |
538 | ||
64cc2c63 | 539 | if (!tcon->broken_posix_open && tcon->unix_ext && |
29e20f9c PS |
540 | cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP & |
541 | le64_to_cpu(tcon->fsUnixInfo.Capability))) { | |
276a74a4 | 542 | /* can not refresh inode info since size could be stale */ |
2422f676 | 543 | rc = cifs_posix_open(full_path, &inode, inode->i_sb, |
fa588e0c | 544 | cifs_sb->mnt_file_mode /* ignored */, |
fb1214e4 | 545 | file->f_flags, &oplock, &fid.netfid, xid); |
276a74a4 | 546 | if (rc == 0) { |
f96637be | 547 | cifs_dbg(FYI, "posix open succeeded\n"); |
7e12eddb | 548 | posix_open_ok = true; |
64cc2c63 SF |
549 | } else if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) { |
550 | if (tcon->ses->serverNOS) | |
f96637be JP |
551 | cifs_dbg(VFS, "server %s of type %s returned unexpected error on SMB posix open, disabling posix open support. Check if server update available.\n", |
552 | tcon->ses->serverName, | |
553 | tcon->ses->serverNOS); | |
64cc2c63 | 554 | tcon->broken_posix_open = true; |
276a74a4 SF |
555 | } else if ((rc != -EIO) && (rc != -EREMOTE) && |
556 | (rc != -EOPNOTSUPP)) /* path not found or net err */ | |
557 | goto out; | |
fb1214e4 PS |
558 | /* |
559 | * Else fallthrough to retry open the old way on network i/o | |
560 | * or DFS errors. | |
561 | */ | |
276a74a4 SF |
562 | } |
563 | ||
233839b1 PS |
564 | if (server->ops->get_lease_key) |
565 | server->ops->get_lease_key(inode, &fid); | |
566 | ||
567 | cifs_add_pending_open(&fid, tlink, &open); | |
568 | ||
7e12eddb | 569 | if (!posix_open_ok) { |
b8c32dbb PS |
570 | if (server->ops->get_lease_key) |
571 | server->ops->get_lease_key(inode, &fid); | |
572 | ||
7e12eddb | 573 | rc = cifs_nt_open(full_path, inode, cifs_sb, tcon, |
fb1214e4 | 574 | file->f_flags, &oplock, &fid, xid); |
233839b1 PS |
575 | if (rc) { |
576 | cifs_del_pending_open(&open); | |
7e12eddb | 577 | goto out; |
233839b1 | 578 | } |
7e12eddb | 579 | } |
47c78b7f | 580 | |
fb1214e4 PS |
581 | cfile = cifs_new_fileinfo(&fid, file, tlink, oplock); |
582 | if (cfile == NULL) { | |
b8c32dbb PS |
583 | if (server->ops->close) |
584 | server->ops->close(xid, tcon, &fid); | |
233839b1 | 585 | cifs_del_pending_open(&open); |
1da177e4 LT |
586 | rc = -ENOMEM; |
587 | goto out; | |
588 | } | |
1da177e4 | 589 | |
9451a9a5 SJ |
590 | cifs_fscache_set_inode_cookie(inode, file); |
591 | ||
7e12eddb | 592 | if ((oplock & CIFS_CREATE_ACTION) && !posix_open_ok && tcon->unix_ext) { |
fb1214e4 PS |
593 | /* |
594 | * Time to set mode which we can not set earlier due to | |
595 | * problems creating new read-only files. | |
596 | */ | |
7e12eddb PS |
597 | struct cifs_unix_set_info_args args = { |
598 | .mode = inode->i_mode, | |
49418b2c EB |
599 | .uid = INVALID_UID, /* no change */ |
600 | .gid = INVALID_GID, /* no change */ | |
7e12eddb PS |
601 | .ctime = NO_CHANGE_64, |
602 | .atime = NO_CHANGE_64, | |
603 | .mtime = NO_CHANGE_64, | |
604 | .device = 0, | |
605 | }; | |
fb1214e4 PS |
606 | CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid.netfid, |
607 | cfile->pid); | |
1da177e4 LT |
608 | } |
609 | ||
610 | out: | |
1da177e4 | 611 | kfree(full_path); |
6d5786a3 | 612 | free_xid(xid); |
7ffec372 | 613 | cifs_put_tlink(tlink); |
1da177e4 LT |
614 | return rc; |
615 | } | |
616 | ||
f152fd5f PS |
617 | static int cifs_push_posix_locks(struct cifsFileInfo *cfile); |
618 | ||
2ae78ba8 PS |
619 | /* |
620 | * Try to reacquire byte range locks that were released when session | |
f152fd5f | 621 | * to server was lost. |
2ae78ba8 | 622 | */ |
f152fd5f PS |
623 | static int |
624 | cifs_relock_file(struct cifsFileInfo *cfile) | |
1da177e4 | 625 | { |
f152fd5f | 626 | struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb); |
2b0143b5 | 627 | struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); |
f152fd5f | 628 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); |
1da177e4 LT |
629 | int rc = 0; |
630 | ||
560d3889 | 631 | down_read_nested(&cinode->lock_sem, SINGLE_DEPTH_NESTING); |
f152fd5f | 632 | if (cinode->can_cache_brlcks) { |
689c3db4 PS |
633 | /* can cache locks - no need to relock */ |
634 | up_read(&cinode->lock_sem); | |
f152fd5f PS |
635 | return rc; |
636 | } | |
637 | ||
638 | if (cap_unix(tcon->ses) && | |
639 | (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) && | |
640 | ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) | |
641 | rc = cifs_push_posix_locks(cfile); | |
642 | else | |
643 | rc = tcon->ses->server->ops->push_mand_locks(cfile); | |
1da177e4 | 644 | |
689c3db4 | 645 | up_read(&cinode->lock_sem); |
1da177e4 LT |
646 | return rc; |
647 | } | |
648 | ||
2ae78ba8 PS |
649 | static int |
650 | cifs_reopen_file(struct cifsFileInfo *cfile, bool can_flush) | |
1da177e4 LT |
651 | { |
652 | int rc = -EACCES; | |
6d5786a3 | 653 | unsigned int xid; |
590a3fe0 | 654 | __u32 oplock; |
1da177e4 | 655 | struct cifs_sb_info *cifs_sb; |
96daf2b0 | 656 | struct cifs_tcon *tcon; |
2ae78ba8 PS |
657 | struct TCP_Server_Info *server; |
658 | struct cifsInodeInfo *cinode; | |
fb8c4b14 | 659 | struct inode *inode; |
1da177e4 | 660 | char *full_path = NULL; |
2ae78ba8 | 661 | int desired_access; |
1da177e4 | 662 | int disposition = FILE_OPEN; |
3d3ea8e6 | 663 | int create_options = CREATE_NOT_DIR; |
226730b4 | 664 | struct cifs_open_parms oparms; |
1da177e4 | 665 | |
6d5786a3 | 666 | xid = get_xid(); |
2ae78ba8 PS |
667 | mutex_lock(&cfile->fh_mutex); |
668 | if (!cfile->invalidHandle) { | |
669 | mutex_unlock(&cfile->fh_mutex); | |
0f3bc09e | 670 | rc = 0; |
6d5786a3 | 671 | free_xid(xid); |
0f3bc09e | 672 | return rc; |
1da177e4 LT |
673 | } |
674 | ||
2b0143b5 | 675 | inode = d_inode(cfile->dentry); |
1da177e4 | 676 | cifs_sb = CIFS_SB(inode->i_sb); |
2ae78ba8 PS |
677 | tcon = tlink_tcon(cfile->tlink); |
678 | server = tcon->ses->server; | |
679 | ||
680 | /* | |
681 | * Can not grab rename sem here because various ops, including those | |
682 | * that already have the rename sem can end up causing writepage to get | |
683 | * called and if the server was down that means we end up here, and we | |
684 | * can never tell if the caller already has the rename_sem. | |
685 | */ | |
686 | full_path = build_path_from_dentry(cfile->dentry); | |
1da177e4 | 687 | if (full_path == NULL) { |
3a9f462f | 688 | rc = -ENOMEM; |
2ae78ba8 | 689 | mutex_unlock(&cfile->fh_mutex); |
6d5786a3 | 690 | free_xid(xid); |
3a9f462f | 691 | return rc; |
1da177e4 LT |
692 | } |
693 | ||
f96637be JP |
694 | cifs_dbg(FYI, "inode = 0x%p file flags 0x%x for %s\n", |
695 | inode, cfile->f_flags, full_path); | |
1da177e4 | 696 | |
10b9b98e | 697 | if (tcon->ses->server->oplocks) |
1da177e4 LT |
698 | oplock = REQ_OPLOCK; |
699 | else | |
4b18f2a9 | 700 | oplock = 0; |
1da177e4 | 701 | |
29e20f9c | 702 | if (tcon->unix_ext && cap_unix(tcon->ses) && |
7fc8f4e9 | 703 | (CIFS_UNIX_POSIX_PATH_OPS_CAP & |
29e20f9c | 704 | le64_to_cpu(tcon->fsUnixInfo.Capability))) { |
608712fe JL |
705 | /* |
706 | * O_CREAT, O_EXCL and O_TRUNC already had their effect on the | |
707 | * original open. Must mask them off for a reopen. | |
708 | */ | |
2ae78ba8 | 709 | unsigned int oflags = cfile->f_flags & |
15886177 | 710 | ~(O_CREAT | O_EXCL | O_TRUNC); |
608712fe | 711 | |
2422f676 | 712 | rc = cifs_posix_open(full_path, NULL, inode->i_sb, |
2ae78ba8 | 713 | cifs_sb->mnt_file_mode /* ignored */, |
9cbc0b73 | 714 | oflags, &oplock, &cfile->fid.netfid, xid); |
7fc8f4e9 | 715 | if (rc == 0) { |
f96637be | 716 | cifs_dbg(FYI, "posix reopen succeeded\n"); |
fe090e4e | 717 | oparms.reconnect = true; |
7fc8f4e9 SF |
718 | goto reopen_success; |
719 | } | |
2ae78ba8 PS |
720 | /* |
721 | * fallthrough to retry open the old way on errors, especially | |
722 | * in the reconnect path it is important to retry hard | |
723 | */ | |
7fc8f4e9 SF |
724 | } |
725 | ||
2ae78ba8 | 726 | desired_access = cifs_convert_flags(cfile->f_flags); |
7fc8f4e9 | 727 | |
3d3ea8e6 SP |
728 | if (backup_cred(cifs_sb)) |
729 | create_options |= CREATE_OPEN_BACKUP_INTENT; | |
730 | ||
b8c32dbb | 731 | if (server->ops->get_lease_key) |
9cbc0b73 | 732 | server->ops->get_lease_key(inode, &cfile->fid); |
b8c32dbb | 733 | |
226730b4 PS |
734 | oparms.tcon = tcon; |
735 | oparms.cifs_sb = cifs_sb; | |
736 | oparms.desired_access = desired_access; | |
737 | oparms.create_options = create_options; | |
738 | oparms.disposition = disposition; | |
739 | oparms.path = full_path; | |
9cbc0b73 PS |
740 | oparms.fid = &cfile->fid; |
741 | oparms.reconnect = true; | |
226730b4 | 742 | |
2ae78ba8 PS |
743 | /* |
744 | * Can not refresh inode by passing in file_info buf to be returned by | |
d81b8a40 | 745 | * ops->open and then calling get_inode_info with returned buf since |
2ae78ba8 PS |
746 | * file might have write behind data that needs to be flushed and server |
747 | * version of file size can be stale. If we knew for sure that inode was | |
748 | * not dirty locally we could do this. | |
749 | */ | |
226730b4 | 750 | rc = server->ops->open(xid, &oparms, &oplock, NULL); |
b33fcf1c PS |
751 | if (rc == -ENOENT && oparms.reconnect == false) { |
752 | /* durable handle timeout is expired - open the file again */ | |
753 | rc = server->ops->open(xid, &oparms, &oplock, NULL); | |
754 | /* indicate that we need to relock the file */ | |
755 | oparms.reconnect = true; | |
756 | } | |
757 | ||
1da177e4 | 758 | if (rc) { |
2ae78ba8 | 759 | mutex_unlock(&cfile->fh_mutex); |
f96637be JP |
760 | cifs_dbg(FYI, "cifs_reopen returned 0x%x\n", rc); |
761 | cifs_dbg(FYI, "oplock: %d\n", oplock); | |
15886177 JL |
762 | goto reopen_error_exit; |
763 | } | |
764 | ||
7fc8f4e9 | 765 | reopen_success: |
2ae78ba8 PS |
766 | cfile->invalidHandle = false; |
767 | mutex_unlock(&cfile->fh_mutex); | |
768 | cinode = CIFS_I(inode); | |
15886177 JL |
769 | |
770 | if (can_flush) { | |
771 | rc = filemap_write_and_wait(inode->i_mapping); | |
9a66396f PS |
772 | if (!is_interrupt_error(rc)) |
773 | mapping_set_error(inode->i_mapping, rc); | |
15886177 | 774 | |
15886177 | 775 | if (tcon->unix_ext) |
2ae78ba8 PS |
776 | rc = cifs_get_inode_info_unix(&inode, full_path, |
777 | inode->i_sb, xid); | |
15886177 | 778 | else |
2ae78ba8 PS |
779 | rc = cifs_get_inode_info(&inode, full_path, NULL, |
780 | inode->i_sb, xid, NULL); | |
781 | } | |
782 | /* | |
783 | * Else we are writing out data to server already and could deadlock if | |
784 | * we tried to flush data, and since we do not know if we have data that | |
785 | * would invalidate the current end of file on the server we can not go | |
786 | * to the server to get the new inode info. | |
787 | */ | |
788 | ||
de740250 PS |
789 | /* |
790 | * If the server returned a read oplock and we have mandatory brlocks, | |
791 | * set oplock level to None. | |
792 | */ | |
793 | if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) { | |
794 | cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n"); | |
795 | oplock = 0; | |
796 | } | |
797 | ||
9cbc0b73 PS |
798 | server->ops->set_fid(cfile, &cfile->fid, oplock); |
799 | if (oparms.reconnect) | |
800 | cifs_relock_file(cfile); | |
15886177 JL |
801 | |
802 | reopen_error_exit: | |
1da177e4 | 803 | kfree(full_path); |
6d5786a3 | 804 | free_xid(xid); |
1da177e4 LT |
805 | return rc; |
806 | } | |
807 | ||
808 | int cifs_close(struct inode *inode, struct file *file) | |
809 | { | |
77970693 JL |
810 | if (file->private_data != NULL) { |
811 | cifsFileInfo_put(file->private_data); | |
812 | file->private_data = NULL; | |
813 | } | |
7ee1af76 | 814 | |
cdff08e7 SF |
815 | /* return code from the ->release op is always ignored */ |
816 | return 0; | |
1da177e4 LT |
817 | } |
818 | ||
52ace1ef SF |
819 | void |
820 | cifs_reopen_persistent_handles(struct cifs_tcon *tcon) | |
821 | { | |
f2cca6a7 | 822 | struct cifsFileInfo *open_file; |
52ace1ef SF |
823 | struct list_head *tmp; |
824 | struct list_head *tmp1; | |
f2cca6a7 PS |
825 | struct list_head tmp_list; |
826 | ||
96a988ff PS |
827 | if (!tcon->use_persistent || !tcon->need_reopen_files) |
828 | return; | |
829 | ||
830 | tcon->need_reopen_files = false; | |
831 | ||
f2cca6a7 PS |
832 | cifs_dbg(FYI, "Reopen persistent handles"); |
833 | INIT_LIST_HEAD(&tmp_list); | |
52ace1ef SF |
834 | |
835 | /* list all files open on tree connection, reopen resilient handles */ | |
836 | spin_lock(&tcon->open_file_lock); | |
f2cca6a7 | 837 | list_for_each(tmp, &tcon->openFileList) { |
52ace1ef | 838 | open_file = list_entry(tmp, struct cifsFileInfo, tlist); |
f2cca6a7 PS |
839 | if (!open_file->invalidHandle) |
840 | continue; | |
841 | cifsFileInfo_get(open_file); | |
842 | list_add_tail(&open_file->rlist, &tmp_list); | |
52ace1ef SF |
843 | } |
844 | spin_unlock(&tcon->open_file_lock); | |
f2cca6a7 PS |
845 | |
846 | list_for_each_safe(tmp, tmp1, &tmp_list) { | |
847 | open_file = list_entry(tmp, struct cifsFileInfo, rlist); | |
96a988ff PS |
848 | if (cifs_reopen_file(open_file, false /* do not flush */)) |
849 | tcon->need_reopen_files = true; | |
f2cca6a7 PS |
850 | list_del_init(&open_file->rlist); |
851 | cifsFileInfo_put(open_file); | |
852 | } | |
52ace1ef SF |
853 | } |
854 | ||
1da177e4 LT |
855 | int cifs_closedir(struct inode *inode, struct file *file) |
856 | { | |
857 | int rc = 0; | |
6d5786a3 | 858 | unsigned int xid; |
4b4de76e | 859 | struct cifsFileInfo *cfile = file->private_data; |
92fc65a7 PS |
860 | struct cifs_tcon *tcon; |
861 | struct TCP_Server_Info *server; | |
862 | char *buf; | |
1da177e4 | 863 | |
f96637be | 864 | cifs_dbg(FYI, "Closedir inode = 0x%p\n", inode); |
1da177e4 | 865 | |
92fc65a7 PS |
866 | if (cfile == NULL) |
867 | return rc; | |
868 | ||
6d5786a3 | 869 | xid = get_xid(); |
92fc65a7 PS |
870 | tcon = tlink_tcon(cfile->tlink); |
871 | server = tcon->ses->server; | |
1da177e4 | 872 | |
f96637be | 873 | cifs_dbg(FYI, "Freeing private data in close dir\n"); |
3afca265 | 874 | spin_lock(&cfile->file_info_lock); |
52755808 | 875 | if (server->ops->dir_needs_close(cfile)) { |
92fc65a7 | 876 | cfile->invalidHandle = true; |
3afca265 | 877 | spin_unlock(&cfile->file_info_lock); |
92fc65a7 PS |
878 | if (server->ops->close_dir) |
879 | rc = server->ops->close_dir(xid, tcon, &cfile->fid); | |
880 | else | |
881 | rc = -ENOSYS; | |
f96637be | 882 | cifs_dbg(FYI, "Closing uncompleted readdir with rc %d\n", rc); |
92fc65a7 PS |
883 | /* not much we can do if it fails anyway, ignore rc */ |
884 | rc = 0; | |
885 | } else | |
3afca265 | 886 | spin_unlock(&cfile->file_info_lock); |
92fc65a7 PS |
887 | |
888 | buf = cfile->srch_inf.ntwrk_buf_start; | |
889 | if (buf) { | |
f96637be | 890 | cifs_dbg(FYI, "closedir free smb buf in srch struct\n"); |
92fc65a7 PS |
891 | cfile->srch_inf.ntwrk_buf_start = NULL; |
892 | if (cfile->srch_inf.smallBuf) | |
893 | cifs_small_buf_release(buf); | |
894 | else | |
895 | cifs_buf_release(buf); | |
1da177e4 | 896 | } |
92fc65a7 PS |
897 | |
898 | cifs_put_tlink(cfile->tlink); | |
899 | kfree(file->private_data); | |
900 | file->private_data = NULL; | |
1da177e4 | 901 | /* BB can we lock the filestruct while this is going on? */ |
6d5786a3 | 902 | free_xid(xid); |
1da177e4 LT |
903 | return rc; |
904 | } | |
905 | ||
85160e03 | 906 | static struct cifsLockInfo * |
9645759c | 907 | cifs_lock_init(__u64 offset, __u64 length, __u8 type, __u16 flags) |
7ee1af76 | 908 | { |
a88b4707 | 909 | struct cifsLockInfo *lock = |
fb8c4b14 | 910 | kmalloc(sizeof(struct cifsLockInfo), GFP_KERNEL); |
a88b4707 PS |
911 | if (!lock) |
912 | return lock; | |
913 | lock->offset = offset; | |
914 | lock->length = length; | |
915 | lock->type = type; | |
a88b4707 | 916 | lock->pid = current->tgid; |
9645759c | 917 | lock->flags = flags; |
a88b4707 PS |
918 | INIT_LIST_HEAD(&lock->blist); |
919 | init_waitqueue_head(&lock->block_q); | |
920 | return lock; | |
85160e03 PS |
921 | } |
922 | ||
f7ba7fe6 | 923 | void |
85160e03 PS |
924 | cifs_del_lock_waiters(struct cifsLockInfo *lock) |
925 | { | |
926 | struct cifsLockInfo *li, *tmp; | |
927 | list_for_each_entry_safe(li, tmp, &lock->blist, blist) { | |
928 | list_del_init(&li->blist); | |
929 | wake_up(&li->block_q); | |
930 | } | |
931 | } | |
932 | ||
081c0414 PS |
933 | #define CIFS_LOCK_OP 0 |
934 | #define CIFS_READ_OP 1 | |
935 | #define CIFS_WRITE_OP 2 | |
936 | ||
937 | /* @rw_check : 0 - no op, 1 - read, 2 - write */ | |
85160e03 | 938 | static bool |
f45d3416 | 939 | cifs_find_fid_lock_conflict(struct cifs_fid_locks *fdlocks, __u64 offset, |
9645759c RS |
940 | __u64 length, __u8 type, __u16 flags, |
941 | struct cifsFileInfo *cfile, | |
081c0414 | 942 | struct cifsLockInfo **conf_lock, int rw_check) |
85160e03 | 943 | { |
fbd35aca | 944 | struct cifsLockInfo *li; |
f45d3416 | 945 | struct cifsFileInfo *cur_cfile = fdlocks->cfile; |
106dc538 | 946 | struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server; |
85160e03 | 947 | |
f45d3416 | 948 | list_for_each_entry(li, &fdlocks->locks, llist) { |
85160e03 PS |
949 | if (offset + length <= li->offset || |
950 | offset >= li->offset + li->length) | |
951 | continue; | |
081c0414 PS |
952 | if (rw_check != CIFS_LOCK_OP && current->tgid == li->pid && |
953 | server->ops->compare_fids(cfile, cur_cfile)) { | |
954 | /* shared lock prevents write op through the same fid */ | |
955 | if (!(li->type & server->vals->shared_lock_type) || | |
956 | rw_check != CIFS_WRITE_OP) | |
957 | continue; | |
958 | } | |
f45d3416 PS |
959 | if ((type & server->vals->shared_lock_type) && |
960 | ((server->ops->compare_fids(cfile, cur_cfile) && | |
961 | current->tgid == li->pid) || type == li->type)) | |
85160e03 | 962 | continue; |
9645759c RS |
963 | if (rw_check == CIFS_LOCK_OP && |
964 | (flags & FL_OFDLCK) && (li->flags & FL_OFDLCK) && | |
965 | server->ops->compare_fids(cfile, cur_cfile)) | |
966 | continue; | |
579f9053 PS |
967 | if (conf_lock) |
968 | *conf_lock = li; | |
f45d3416 | 969 | return true; |
85160e03 PS |
970 | } |
971 | return false; | |
972 | } | |
973 | ||
579f9053 | 974 | bool |
55157dfb | 975 | cifs_find_lock_conflict(struct cifsFileInfo *cfile, __u64 offset, __u64 length, |
9645759c RS |
976 | __u8 type, __u16 flags, |
977 | struct cifsLockInfo **conf_lock, int rw_check) | |
161ebf9f | 978 | { |
fbd35aca | 979 | bool rc = false; |
f45d3416 | 980 | struct cifs_fid_locks *cur; |
2b0143b5 | 981 | struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); |
fbd35aca | 982 | |
f45d3416 PS |
983 | list_for_each_entry(cur, &cinode->llist, llist) { |
984 | rc = cifs_find_fid_lock_conflict(cur, offset, length, type, | |
9645759c RS |
985 | flags, cfile, conf_lock, |
986 | rw_check); | |
fbd35aca PS |
987 | if (rc) |
988 | break; | |
989 | } | |
fbd35aca PS |
990 | |
991 | return rc; | |
161ebf9f PS |
992 | } |
993 | ||
9a5101c8 PS |
994 | /* |
995 | * Check if there is another lock that prevents us to set the lock (mandatory | |
996 | * style). If such a lock exists, update the flock structure with its | |
997 | * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks | |
998 | * or leave it the same if we can't. Returns 0 if we don't need to request to | |
999 | * the server or 1 otherwise. | |
1000 | */ | |
85160e03 | 1001 | static int |
fbd35aca PS |
1002 | cifs_lock_test(struct cifsFileInfo *cfile, __u64 offset, __u64 length, |
1003 | __u8 type, struct file_lock *flock) | |
85160e03 PS |
1004 | { |
1005 | int rc = 0; | |
1006 | struct cifsLockInfo *conf_lock; | |
2b0143b5 | 1007 | struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); |
106dc538 | 1008 | struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server; |
85160e03 PS |
1009 | bool exist; |
1010 | ||
1b4b55a1 | 1011 | down_read(&cinode->lock_sem); |
85160e03 | 1012 | |
55157dfb | 1013 | exist = cifs_find_lock_conflict(cfile, offset, length, type, |
9645759c RS |
1014 | flock->fl_flags, &conf_lock, |
1015 | CIFS_LOCK_OP); | |
85160e03 PS |
1016 | if (exist) { |
1017 | flock->fl_start = conf_lock->offset; | |
1018 | flock->fl_end = conf_lock->offset + conf_lock->length - 1; | |
1019 | flock->fl_pid = conf_lock->pid; | |
106dc538 | 1020 | if (conf_lock->type & server->vals->shared_lock_type) |
85160e03 PS |
1021 | flock->fl_type = F_RDLCK; |
1022 | else | |
1023 | flock->fl_type = F_WRLCK; | |
1024 | } else if (!cinode->can_cache_brlcks) | |
1025 | rc = 1; | |
1026 | else | |
1027 | flock->fl_type = F_UNLCK; | |
1028 | ||
1b4b55a1 | 1029 | up_read(&cinode->lock_sem); |
85160e03 PS |
1030 | return rc; |
1031 | } | |
1032 | ||
161ebf9f | 1033 | static void |
fbd35aca | 1034 | cifs_lock_add(struct cifsFileInfo *cfile, struct cifsLockInfo *lock) |
85160e03 | 1035 | { |
2b0143b5 | 1036 | struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); |
d46b0da7 | 1037 | cifs_down_write(&cinode->lock_sem); |
f45d3416 | 1038 | list_add_tail(&lock->llist, &cfile->llist->locks); |
1b4b55a1 | 1039 | up_write(&cinode->lock_sem); |
7ee1af76 JA |
1040 | } |
1041 | ||
9a5101c8 PS |
1042 | /* |
1043 | * Set the byte-range lock (mandatory style). Returns: | |
1044 | * 1) 0, if we set the lock and don't need to request to the server; | |
1045 | * 2) 1, if no locks prevent us but we need to request to the server; | |
413d6100 | 1046 | * 3) -EACCES, if there is a lock that prevents us and wait is false. |
9a5101c8 | 1047 | */ |
85160e03 | 1048 | static int |
fbd35aca | 1049 | cifs_lock_add_if(struct cifsFileInfo *cfile, struct cifsLockInfo *lock, |
161ebf9f | 1050 | bool wait) |
85160e03 | 1051 | { |
161ebf9f | 1052 | struct cifsLockInfo *conf_lock; |
2b0143b5 | 1053 | struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); |
85160e03 PS |
1054 | bool exist; |
1055 | int rc = 0; | |
1056 | ||
85160e03 PS |
1057 | try_again: |
1058 | exist = false; | |
d46b0da7 | 1059 | cifs_down_write(&cinode->lock_sem); |
85160e03 | 1060 | |
55157dfb | 1061 | exist = cifs_find_lock_conflict(cfile, lock->offset, lock->length, |
9645759c RS |
1062 | lock->type, lock->flags, &conf_lock, |
1063 | CIFS_LOCK_OP); | |
85160e03 | 1064 | if (!exist && cinode->can_cache_brlcks) { |
f45d3416 | 1065 | list_add_tail(&lock->llist, &cfile->llist->locks); |
1b4b55a1 | 1066 | up_write(&cinode->lock_sem); |
85160e03 PS |
1067 | return rc; |
1068 | } | |
1069 | ||
1070 | if (!exist) | |
1071 | rc = 1; | |
1072 | else if (!wait) | |
1073 | rc = -EACCES; | |
1074 | else { | |
1075 | list_add_tail(&lock->blist, &conf_lock->blist); | |
1b4b55a1 | 1076 | up_write(&cinode->lock_sem); |
85160e03 PS |
1077 | rc = wait_event_interruptible(lock->block_q, |
1078 | (lock->blist.prev == &lock->blist) && | |
1079 | (lock->blist.next == &lock->blist)); | |
1080 | if (!rc) | |
1081 | goto try_again; | |
d46b0da7 | 1082 | cifs_down_write(&cinode->lock_sem); |
a88b4707 | 1083 | list_del_init(&lock->blist); |
85160e03 PS |
1084 | } |
1085 | ||
1b4b55a1 | 1086 | up_write(&cinode->lock_sem); |
85160e03 PS |
1087 | return rc; |
1088 | } | |
1089 | ||
9a5101c8 PS |
1090 | /* |
1091 | * Check if there is another lock that prevents us to set the lock (posix | |
1092 | * style). If such a lock exists, update the flock structure with its | |
1093 | * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks | |
1094 | * or leave it the same if we can't. Returns 0 if we don't need to request to | |
1095 | * the server or 1 otherwise. | |
1096 | */ | |
85160e03 | 1097 | static int |
4f6bcec9 PS |
1098 | cifs_posix_lock_test(struct file *file, struct file_lock *flock) |
1099 | { | |
1100 | int rc = 0; | |
496ad9aa | 1101 | struct cifsInodeInfo *cinode = CIFS_I(file_inode(file)); |
4f6bcec9 PS |
1102 | unsigned char saved_type = flock->fl_type; |
1103 | ||
50792760 PS |
1104 | if ((flock->fl_flags & FL_POSIX) == 0) |
1105 | return 1; | |
1106 | ||
1b4b55a1 | 1107 | down_read(&cinode->lock_sem); |
4f6bcec9 PS |
1108 | posix_test_lock(file, flock); |
1109 | ||
1110 | if (flock->fl_type == F_UNLCK && !cinode->can_cache_brlcks) { | |
1111 | flock->fl_type = saved_type; | |
1112 | rc = 1; | |
1113 | } | |
1114 | ||
1b4b55a1 | 1115 | up_read(&cinode->lock_sem); |
4f6bcec9 PS |
1116 | return rc; |
1117 | } | |
1118 | ||
9a5101c8 PS |
1119 | /* |
1120 | * Set the byte-range lock (posix style). Returns: | |
1121 | * 1) 0, if we set the lock and don't need to request to the server; | |
1122 | * 2) 1, if we need to request to the server; | |
1123 | * 3) <0, if the error occurs while setting the lock. | |
1124 | */ | |
4f6bcec9 PS |
1125 | static int |
1126 | cifs_posix_lock_set(struct file *file, struct file_lock *flock) | |
1127 | { | |
496ad9aa | 1128 | struct cifsInodeInfo *cinode = CIFS_I(file_inode(file)); |
50792760 PS |
1129 | int rc = 1; |
1130 | ||
1131 | if ((flock->fl_flags & FL_POSIX) == 0) | |
1132 | return rc; | |
4f6bcec9 | 1133 | |
66189be7 | 1134 | try_again: |
d46b0da7 | 1135 | cifs_down_write(&cinode->lock_sem); |
4f6bcec9 | 1136 | if (!cinode->can_cache_brlcks) { |
1b4b55a1 | 1137 | up_write(&cinode->lock_sem); |
50792760 | 1138 | return rc; |
4f6bcec9 | 1139 | } |
66189be7 PS |
1140 | |
1141 | rc = posix_lock_file(file, flock, NULL); | |
1b4b55a1 | 1142 | up_write(&cinode->lock_sem); |
66189be7 | 1143 | if (rc == FILE_LOCK_DEFERRED) { |
ada5c1da | 1144 | rc = wait_event_interruptible(flock->fl_wait, !flock->fl_blocker); |
66189be7 PS |
1145 | if (!rc) |
1146 | goto try_again; | |
cb03f94f | 1147 | locks_delete_block(flock); |
66189be7 | 1148 | } |
9ebb389d | 1149 | return rc; |
4f6bcec9 PS |
1150 | } |
1151 | ||
d39a4f71 | 1152 | int |
4f6bcec9 | 1153 | cifs_push_mandatory_locks(struct cifsFileInfo *cfile) |
85160e03 | 1154 | { |
6d5786a3 PS |
1155 | unsigned int xid; |
1156 | int rc = 0, stored_rc; | |
85160e03 PS |
1157 | struct cifsLockInfo *li, *tmp; |
1158 | struct cifs_tcon *tcon; | |
0013fb4c | 1159 | unsigned int num, max_num, max_buf; |
32b9aaf1 | 1160 | LOCKING_ANDX_RANGE *buf, *cur; |
4d61eda8 CIK |
1161 | static const int types[] = { |
1162 | LOCKING_ANDX_LARGE_FILES, | |
1163 | LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES | |
1164 | }; | |
32b9aaf1 | 1165 | int i; |
85160e03 | 1166 | |
6d5786a3 | 1167 | xid = get_xid(); |
85160e03 PS |
1168 | tcon = tlink_tcon(cfile->tlink); |
1169 | ||
0013fb4c PS |
1170 | /* |
1171 | * Accessing maxBuf is racy with cifs_reconnect - need to store value | |
b9a74cde | 1172 | * and check it before using. |
0013fb4c PS |
1173 | */ |
1174 | max_buf = tcon->ses->server->maxBuf; | |
b9a74cde | 1175 | if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE))) { |
6d5786a3 | 1176 | free_xid(xid); |
0013fb4c PS |
1177 | return -EINVAL; |
1178 | } | |
1179 | ||
92a8109e RL |
1180 | BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) > |
1181 | PAGE_SIZE); | |
1182 | max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr), | |
1183 | PAGE_SIZE); | |
0013fb4c PS |
1184 | max_num = (max_buf - sizeof(struct smb_hdr)) / |
1185 | sizeof(LOCKING_ANDX_RANGE); | |
4b99d39b | 1186 | buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL); |
32b9aaf1 | 1187 | if (!buf) { |
6d5786a3 | 1188 | free_xid(xid); |
e2f2886a | 1189 | return -ENOMEM; |
32b9aaf1 PS |
1190 | } |
1191 | ||
1192 | for (i = 0; i < 2; i++) { | |
1193 | cur = buf; | |
1194 | num = 0; | |
f45d3416 | 1195 | list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) { |
32b9aaf1 PS |
1196 | if (li->type != types[i]) |
1197 | continue; | |
1198 | cur->Pid = cpu_to_le16(li->pid); | |
1199 | cur->LengthLow = cpu_to_le32((u32)li->length); | |
1200 | cur->LengthHigh = cpu_to_le32((u32)(li->length>>32)); | |
1201 | cur->OffsetLow = cpu_to_le32((u32)li->offset); | |
1202 | cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32)); | |
1203 | if (++num == max_num) { | |
4b4de76e PS |
1204 | stored_rc = cifs_lockv(xid, tcon, |
1205 | cfile->fid.netfid, | |
04a6aa8a PS |
1206 | (__u8)li->type, 0, num, |
1207 | buf); | |
32b9aaf1 PS |
1208 | if (stored_rc) |
1209 | rc = stored_rc; | |
1210 | cur = buf; | |
1211 | num = 0; | |
1212 | } else | |
1213 | cur++; | |
1214 | } | |
1215 | ||
1216 | if (num) { | |
4b4de76e | 1217 | stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid, |
04a6aa8a | 1218 | (__u8)types[i], 0, num, buf); |
32b9aaf1 PS |
1219 | if (stored_rc) |
1220 | rc = stored_rc; | |
1221 | } | |
85160e03 PS |
1222 | } |
1223 | ||
32b9aaf1 | 1224 | kfree(buf); |
6d5786a3 | 1225 | free_xid(xid); |
85160e03 PS |
1226 | return rc; |
1227 | } | |
1228 | ||
3d22462a JL |
1229 | static __u32 |
1230 | hash_lockowner(fl_owner_t owner) | |
1231 | { | |
1232 | return cifs_lock_secret ^ hash32_ptr((const void *)owner); | |
1233 | } | |
1234 | ||
d5751469 PS |
1235 | struct lock_to_push { |
1236 | struct list_head llist; | |
1237 | __u64 offset; | |
1238 | __u64 length; | |
1239 | __u32 pid; | |
1240 | __u16 netfid; | |
1241 | __u8 type; | |
1242 | }; | |
1243 | ||
4f6bcec9 | 1244 | static int |
b8db928b | 1245 | cifs_push_posix_locks(struct cifsFileInfo *cfile) |
4f6bcec9 | 1246 | { |
2b0143b5 | 1247 | struct inode *inode = d_inode(cfile->dentry); |
4f6bcec9 | 1248 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); |
bd61e0a9 JL |
1249 | struct file_lock *flock; |
1250 | struct file_lock_context *flctx = inode->i_flctx; | |
e084c1bd | 1251 | unsigned int count = 0, i; |
4f6bcec9 | 1252 | int rc = 0, xid, type; |
d5751469 PS |
1253 | struct list_head locks_to_send, *el; |
1254 | struct lock_to_push *lck, *tmp; | |
4f6bcec9 | 1255 | __u64 length; |
4f6bcec9 | 1256 | |
6d5786a3 | 1257 | xid = get_xid(); |
4f6bcec9 | 1258 | |
bd61e0a9 JL |
1259 | if (!flctx) |
1260 | goto out; | |
d5751469 | 1261 | |
e084c1bd JL |
1262 | spin_lock(&flctx->flc_lock); |
1263 | list_for_each(el, &flctx->flc_posix) { | |
1264 | count++; | |
1265 | } | |
1266 | spin_unlock(&flctx->flc_lock); | |
1267 | ||
4f6bcec9 PS |
1268 | INIT_LIST_HEAD(&locks_to_send); |
1269 | ||
d5751469 | 1270 | /* |
e084c1bd JL |
1271 | * Allocating count locks is enough because no FL_POSIX locks can be |
1272 | * added to the list while we are holding cinode->lock_sem that | |
ce85852b | 1273 | * protects locking operations of this inode. |
d5751469 | 1274 | */ |
e084c1bd | 1275 | for (i = 0; i < count; i++) { |
d5751469 PS |
1276 | lck = kmalloc(sizeof(struct lock_to_push), GFP_KERNEL); |
1277 | if (!lck) { | |
1278 | rc = -ENOMEM; | |
1279 | goto err_out; | |
1280 | } | |
1281 | list_add_tail(&lck->llist, &locks_to_send); | |
1282 | } | |
1283 | ||
d5751469 | 1284 | el = locks_to_send.next; |
6109c850 | 1285 | spin_lock(&flctx->flc_lock); |
bd61e0a9 | 1286 | list_for_each_entry(flock, &flctx->flc_posix, fl_list) { |
d5751469 | 1287 | if (el == &locks_to_send) { |
ce85852b PS |
1288 | /* |
1289 | * The list ended. We don't have enough allocated | |
1290 | * structures - something is really wrong. | |
1291 | */ | |
f96637be | 1292 | cifs_dbg(VFS, "Can't push all brlocks!\n"); |
d5751469 PS |
1293 | break; |
1294 | } | |
4f6bcec9 PS |
1295 | length = 1 + flock->fl_end - flock->fl_start; |
1296 | if (flock->fl_type == F_RDLCK || flock->fl_type == F_SHLCK) | |
1297 | type = CIFS_RDLCK; | |
1298 | else | |
1299 | type = CIFS_WRLCK; | |
d5751469 | 1300 | lck = list_entry(el, struct lock_to_push, llist); |
3d22462a | 1301 | lck->pid = hash_lockowner(flock->fl_owner); |
4b4de76e | 1302 | lck->netfid = cfile->fid.netfid; |
d5751469 PS |
1303 | lck->length = length; |
1304 | lck->type = type; | |
1305 | lck->offset = flock->fl_start; | |
4f6bcec9 | 1306 | } |
6109c850 | 1307 | spin_unlock(&flctx->flc_lock); |
4f6bcec9 PS |
1308 | |
1309 | list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) { | |
4f6bcec9 PS |
1310 | int stored_rc; |
1311 | ||
4f6bcec9 | 1312 | stored_rc = CIFSSMBPosixLock(xid, tcon, lck->netfid, lck->pid, |
c5fd363d | 1313 | lck->offset, lck->length, NULL, |
4f6bcec9 PS |
1314 | lck->type, 0); |
1315 | if (stored_rc) | |
1316 | rc = stored_rc; | |
1317 | list_del(&lck->llist); | |
1318 | kfree(lck); | |
1319 | } | |
1320 | ||
d5751469 | 1321 | out: |
6d5786a3 | 1322 | free_xid(xid); |
4f6bcec9 | 1323 | return rc; |
d5751469 PS |
1324 | err_out: |
1325 | list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) { | |
1326 | list_del(&lck->llist); | |
1327 | kfree(lck); | |
1328 | } | |
1329 | goto out; | |
4f6bcec9 PS |
1330 | } |
1331 | ||
9ec3c882 | 1332 | static int |
b8db928b | 1333 | cifs_push_locks(struct cifsFileInfo *cfile) |
9ec3c882 | 1334 | { |
b8db928b | 1335 | struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb); |
2b0143b5 | 1336 | struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); |
b8db928b | 1337 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); |
9ec3c882 PS |
1338 | int rc = 0; |
1339 | ||
1340 | /* we are going to update can_cache_brlcks here - need a write access */ | |
d46b0da7 | 1341 | cifs_down_write(&cinode->lock_sem); |
9ec3c882 PS |
1342 | if (!cinode->can_cache_brlcks) { |
1343 | up_write(&cinode->lock_sem); | |
1344 | return rc; | |
1345 | } | |
4f6bcec9 | 1346 | |
29e20f9c | 1347 | if (cap_unix(tcon->ses) && |
4f6bcec9 PS |
1348 | (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) && |
1349 | ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) | |
b8db928b PS |
1350 | rc = cifs_push_posix_locks(cfile); |
1351 | else | |
1352 | rc = tcon->ses->server->ops->push_mand_locks(cfile); | |
4f6bcec9 | 1353 | |
b8db928b PS |
1354 | cinode->can_cache_brlcks = false; |
1355 | up_write(&cinode->lock_sem); | |
1356 | return rc; | |
4f6bcec9 PS |
1357 | } |
1358 | ||
03776f45 | 1359 | static void |
04a6aa8a | 1360 | cifs_read_flock(struct file_lock *flock, __u32 *type, int *lock, int *unlock, |
106dc538 | 1361 | bool *wait_flag, struct TCP_Server_Info *server) |
1da177e4 | 1362 | { |
03776f45 | 1363 | if (flock->fl_flags & FL_POSIX) |
f96637be | 1364 | cifs_dbg(FYI, "Posix\n"); |
03776f45 | 1365 | if (flock->fl_flags & FL_FLOCK) |
f96637be | 1366 | cifs_dbg(FYI, "Flock\n"); |
03776f45 | 1367 | if (flock->fl_flags & FL_SLEEP) { |
f96637be | 1368 | cifs_dbg(FYI, "Blocking lock\n"); |
03776f45 | 1369 | *wait_flag = true; |
1da177e4 | 1370 | } |
03776f45 | 1371 | if (flock->fl_flags & FL_ACCESS) |
f96637be | 1372 | cifs_dbg(FYI, "Process suspended by mandatory locking - not implemented yet\n"); |
03776f45 | 1373 | if (flock->fl_flags & FL_LEASE) |
f96637be | 1374 | cifs_dbg(FYI, "Lease on file - not implemented yet\n"); |
03776f45 | 1375 | if (flock->fl_flags & |
3d6d854a | 1376 | (~(FL_POSIX | FL_FLOCK | FL_SLEEP | |
9645759c | 1377 | FL_ACCESS | FL_LEASE | FL_CLOSE | FL_OFDLCK))) |
f96637be | 1378 | cifs_dbg(FYI, "Unknown lock flags 0x%x\n", flock->fl_flags); |
1da177e4 | 1379 | |
106dc538 | 1380 | *type = server->vals->large_lock_type; |
03776f45 | 1381 | if (flock->fl_type == F_WRLCK) { |
f96637be | 1382 | cifs_dbg(FYI, "F_WRLCK\n"); |
106dc538 | 1383 | *type |= server->vals->exclusive_lock_type; |
03776f45 PS |
1384 | *lock = 1; |
1385 | } else if (flock->fl_type == F_UNLCK) { | |
f96637be | 1386 | cifs_dbg(FYI, "F_UNLCK\n"); |
106dc538 | 1387 | *type |= server->vals->unlock_lock_type; |
03776f45 PS |
1388 | *unlock = 1; |
1389 | /* Check if unlock includes more than one lock range */ | |
1390 | } else if (flock->fl_type == F_RDLCK) { | |
f96637be | 1391 | cifs_dbg(FYI, "F_RDLCK\n"); |
106dc538 | 1392 | *type |= server->vals->shared_lock_type; |
03776f45 PS |
1393 | *lock = 1; |
1394 | } else if (flock->fl_type == F_EXLCK) { | |
f96637be | 1395 | cifs_dbg(FYI, "F_EXLCK\n"); |
106dc538 | 1396 | *type |= server->vals->exclusive_lock_type; |
03776f45 PS |
1397 | *lock = 1; |
1398 | } else if (flock->fl_type == F_SHLCK) { | |
f96637be | 1399 | cifs_dbg(FYI, "F_SHLCK\n"); |
106dc538 | 1400 | *type |= server->vals->shared_lock_type; |
03776f45 | 1401 | *lock = 1; |
1da177e4 | 1402 | } else |
f96637be | 1403 | cifs_dbg(FYI, "Unknown type of lock\n"); |
03776f45 | 1404 | } |
1da177e4 | 1405 | |
03776f45 | 1406 | static int |
04a6aa8a | 1407 | cifs_getlk(struct file *file, struct file_lock *flock, __u32 type, |
6d5786a3 | 1408 | bool wait_flag, bool posix_lck, unsigned int xid) |
03776f45 PS |
1409 | { |
1410 | int rc = 0; | |
1411 | __u64 length = 1 + flock->fl_end - flock->fl_start; | |
4f6bcec9 PS |
1412 | struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data; |
1413 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); | |
106dc538 | 1414 | struct TCP_Server_Info *server = tcon->ses->server; |
4b4de76e | 1415 | __u16 netfid = cfile->fid.netfid; |
f05337c6 | 1416 | |
03776f45 PS |
1417 | if (posix_lck) { |
1418 | int posix_lock_type; | |
4f6bcec9 PS |
1419 | |
1420 | rc = cifs_posix_lock_test(file, flock); | |
1421 | if (!rc) | |
1422 | return rc; | |
1423 | ||
106dc538 | 1424 | if (type & server->vals->shared_lock_type) |
03776f45 PS |
1425 | posix_lock_type = CIFS_RDLCK; |
1426 | else | |
1427 | posix_lock_type = CIFS_WRLCK; | |
3d22462a JL |
1428 | rc = CIFSSMBPosixLock(xid, tcon, netfid, |
1429 | hash_lockowner(flock->fl_owner), | |
c5fd363d | 1430 | flock->fl_start, length, flock, |
4f6bcec9 | 1431 | posix_lock_type, wait_flag); |
03776f45 PS |
1432 | return rc; |
1433 | } | |
1da177e4 | 1434 | |
fbd35aca | 1435 | rc = cifs_lock_test(cfile, flock->fl_start, length, type, flock); |
85160e03 PS |
1436 | if (!rc) |
1437 | return rc; | |
1438 | ||
03776f45 | 1439 | /* BB we could chain these into one lock request BB */ |
d39a4f71 PS |
1440 | rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, type, |
1441 | 1, 0, false); | |
03776f45 | 1442 | if (rc == 0) { |
d39a4f71 PS |
1443 | rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, |
1444 | type, 0, 1, false); | |
03776f45 PS |
1445 | flock->fl_type = F_UNLCK; |
1446 | if (rc != 0) | |
f96637be JP |
1447 | cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n", |
1448 | rc); | |
a88b4707 | 1449 | return 0; |
1da177e4 | 1450 | } |
7ee1af76 | 1451 | |
106dc538 | 1452 | if (type & server->vals->shared_lock_type) { |
03776f45 | 1453 | flock->fl_type = F_WRLCK; |
a88b4707 | 1454 | return 0; |
7ee1af76 JA |
1455 | } |
1456 | ||
d39a4f71 PS |
1457 | type &= ~server->vals->exclusive_lock_type; |
1458 | ||
1459 | rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, | |
1460 | type | server->vals->shared_lock_type, | |
1461 | 1, 0, false); | |
03776f45 | 1462 | if (rc == 0) { |
d39a4f71 PS |
1463 | rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, |
1464 | type | server->vals->shared_lock_type, 0, 1, false); | |
03776f45 PS |
1465 | flock->fl_type = F_RDLCK; |
1466 | if (rc != 0) | |
f96637be JP |
1467 | cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n", |
1468 | rc); | |
03776f45 PS |
1469 | } else |
1470 | flock->fl_type = F_WRLCK; | |
1471 | ||
a88b4707 | 1472 | return 0; |
03776f45 PS |
1473 | } |
1474 | ||
f7ba7fe6 | 1475 | void |
9ee305b7 PS |
1476 | cifs_move_llist(struct list_head *source, struct list_head *dest) |
1477 | { | |
1478 | struct list_head *li, *tmp; | |
1479 | list_for_each_safe(li, tmp, source) | |
1480 | list_move(li, dest); | |
1481 | } | |
1482 | ||
f7ba7fe6 | 1483 | void |
9ee305b7 PS |
1484 | cifs_free_llist(struct list_head *llist) |
1485 | { | |
1486 | struct cifsLockInfo *li, *tmp; | |
1487 | list_for_each_entry_safe(li, tmp, llist, llist) { | |
1488 | cifs_del_lock_waiters(li); | |
1489 | list_del(&li->llist); | |
1490 | kfree(li); | |
1491 | } | |
1492 | } | |
1493 | ||
d39a4f71 | 1494 | int |
6d5786a3 PS |
1495 | cifs_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock, |
1496 | unsigned int xid) | |
9ee305b7 PS |
1497 | { |
1498 | int rc = 0, stored_rc; | |
4d61eda8 CIK |
1499 | static const int types[] = { |
1500 | LOCKING_ANDX_LARGE_FILES, | |
1501 | LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES | |
1502 | }; | |
9ee305b7 | 1503 | unsigned int i; |
0013fb4c | 1504 | unsigned int max_num, num, max_buf; |
9ee305b7 PS |
1505 | LOCKING_ANDX_RANGE *buf, *cur; |
1506 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); | |
2b0143b5 | 1507 | struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); |
9ee305b7 PS |
1508 | struct cifsLockInfo *li, *tmp; |
1509 | __u64 length = 1 + flock->fl_end - flock->fl_start; | |
1510 | struct list_head tmp_llist; | |
1511 | ||
1512 | INIT_LIST_HEAD(&tmp_llist); | |
1513 | ||
0013fb4c PS |
1514 | /* |
1515 | * Accessing maxBuf is racy with cifs_reconnect - need to store value | |
b9a74cde | 1516 | * and check it before using. |
0013fb4c PS |
1517 | */ |
1518 | max_buf = tcon->ses->server->maxBuf; | |
b9a74cde | 1519 | if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE))) |
0013fb4c PS |
1520 | return -EINVAL; |
1521 | ||
92a8109e RL |
1522 | BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) > |
1523 | PAGE_SIZE); | |
1524 | max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr), | |
1525 | PAGE_SIZE); | |
0013fb4c PS |
1526 | max_num = (max_buf - sizeof(struct smb_hdr)) / |
1527 | sizeof(LOCKING_ANDX_RANGE); | |
4b99d39b | 1528 | buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL); |
9ee305b7 PS |
1529 | if (!buf) |
1530 | return -ENOMEM; | |
1531 | ||
d46b0da7 | 1532 | cifs_down_write(&cinode->lock_sem); |
9ee305b7 PS |
1533 | for (i = 0; i < 2; i++) { |
1534 | cur = buf; | |
1535 | num = 0; | |
f45d3416 | 1536 | list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) { |
9ee305b7 PS |
1537 | if (flock->fl_start > li->offset || |
1538 | (flock->fl_start + length) < | |
1539 | (li->offset + li->length)) | |
1540 | continue; | |
1541 | if (current->tgid != li->pid) | |
1542 | continue; | |
9ee305b7 PS |
1543 | if (types[i] != li->type) |
1544 | continue; | |
ea319d57 | 1545 | if (cinode->can_cache_brlcks) { |
9ee305b7 PS |
1546 | /* |
1547 | * We can cache brlock requests - simply remove | |
fbd35aca | 1548 | * a lock from the file's list. |
9ee305b7 PS |
1549 | */ |
1550 | list_del(&li->llist); | |
1551 | cifs_del_lock_waiters(li); | |
1552 | kfree(li); | |
ea319d57 | 1553 | continue; |
9ee305b7 | 1554 | } |
ea319d57 PS |
1555 | cur->Pid = cpu_to_le16(li->pid); |
1556 | cur->LengthLow = cpu_to_le32((u32)li->length); | |
1557 | cur->LengthHigh = cpu_to_le32((u32)(li->length>>32)); | |
1558 | cur->OffsetLow = cpu_to_le32((u32)li->offset); | |
1559 | cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32)); | |
1560 | /* | |
1561 | * We need to save a lock here to let us add it again to | |
1562 | * the file's list if the unlock range request fails on | |
1563 | * the server. | |
1564 | */ | |
1565 | list_move(&li->llist, &tmp_llist); | |
1566 | if (++num == max_num) { | |
4b4de76e PS |
1567 | stored_rc = cifs_lockv(xid, tcon, |
1568 | cfile->fid.netfid, | |
ea319d57 PS |
1569 | li->type, num, 0, buf); |
1570 | if (stored_rc) { | |
1571 | /* | |
1572 | * We failed on the unlock range | |
1573 | * request - add all locks from the tmp | |
1574 | * list to the head of the file's list. | |
1575 | */ | |
1576 | cifs_move_llist(&tmp_llist, | |
f45d3416 | 1577 | &cfile->llist->locks); |
ea319d57 PS |
1578 | rc = stored_rc; |
1579 | } else | |
1580 | /* | |
1581 | * The unlock range request succeed - | |
1582 | * free the tmp list. | |
1583 | */ | |
1584 | cifs_free_llist(&tmp_llist); | |
1585 | cur = buf; | |
1586 | num = 0; | |
1587 | } else | |
1588 | cur++; | |
9ee305b7 PS |
1589 | } |
1590 | if (num) { | |
4b4de76e | 1591 | stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid, |
9ee305b7 PS |
1592 | types[i], num, 0, buf); |
1593 | if (stored_rc) { | |
f45d3416 PS |
1594 | cifs_move_llist(&tmp_llist, |
1595 | &cfile->llist->locks); | |
9ee305b7 PS |
1596 | rc = stored_rc; |
1597 | } else | |
1598 | cifs_free_llist(&tmp_llist); | |
1599 | } | |
1600 | } | |
1601 | ||
1b4b55a1 | 1602 | up_write(&cinode->lock_sem); |
9ee305b7 PS |
1603 | kfree(buf); |
1604 | return rc; | |
1605 | } | |
1606 | ||
03776f45 | 1607 | static int |
f45d3416 | 1608 | cifs_setlk(struct file *file, struct file_lock *flock, __u32 type, |
6d5786a3 PS |
1609 | bool wait_flag, bool posix_lck, int lock, int unlock, |
1610 | unsigned int xid) | |
03776f45 PS |
1611 | { |
1612 | int rc = 0; | |
1613 | __u64 length = 1 + flock->fl_end - flock->fl_start; | |
1614 | struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data; | |
1615 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); | |
106dc538 | 1616 | struct TCP_Server_Info *server = tcon->ses->server; |
2b0143b5 | 1617 | struct inode *inode = d_inode(cfile->dentry); |
03776f45 PS |
1618 | |
1619 | if (posix_lck) { | |
08547b03 | 1620 | int posix_lock_type; |
4f6bcec9 PS |
1621 | |
1622 | rc = cifs_posix_lock_set(file, flock); | |
1623 | if (!rc || rc < 0) | |
1624 | return rc; | |
1625 | ||
106dc538 | 1626 | if (type & server->vals->shared_lock_type) |
08547b03 SF |
1627 | posix_lock_type = CIFS_RDLCK; |
1628 | else | |
1629 | posix_lock_type = CIFS_WRLCK; | |
50c2f753 | 1630 | |
03776f45 | 1631 | if (unlock == 1) |
beb84dc8 | 1632 | posix_lock_type = CIFS_UNLCK; |
7ee1af76 | 1633 | |
f45d3416 | 1634 | rc = CIFSSMBPosixLock(xid, tcon, cfile->fid.netfid, |
3d22462a JL |
1635 | hash_lockowner(flock->fl_owner), |
1636 | flock->fl_start, length, | |
f45d3416 | 1637 | NULL, posix_lock_type, wait_flag); |
03776f45 PS |
1638 | goto out; |
1639 | } | |
7ee1af76 | 1640 | |
03776f45 | 1641 | if (lock) { |
161ebf9f PS |
1642 | struct cifsLockInfo *lock; |
1643 | ||
9645759c RS |
1644 | lock = cifs_lock_init(flock->fl_start, length, type, |
1645 | flock->fl_flags); | |
161ebf9f PS |
1646 | if (!lock) |
1647 | return -ENOMEM; | |
1648 | ||
fbd35aca | 1649 | rc = cifs_lock_add_if(cfile, lock, wait_flag); |
21cb2d90 | 1650 | if (rc < 0) { |
161ebf9f | 1651 | kfree(lock); |
21cb2d90 PS |
1652 | return rc; |
1653 | } | |
1654 | if (!rc) | |
85160e03 PS |
1655 | goto out; |
1656 | ||
63b7d3a4 PS |
1657 | /* |
1658 | * Windows 7 server can delay breaking lease from read to None | |
1659 | * if we set a byte-range lock on a file - break it explicitly | |
1660 | * before sending the lock to the server to be sure the next | |
1661 | * read won't conflict with non-overlapted locks due to | |
1662 | * pagereading. | |
1663 | */ | |
18cceb6a PS |
1664 | if (!CIFS_CACHE_WRITE(CIFS_I(inode)) && |
1665 | CIFS_CACHE_READ(CIFS_I(inode))) { | |
4f73c7d3 | 1666 | cifs_zap_mapping(inode); |
f96637be JP |
1667 | cifs_dbg(FYI, "Set no oplock for inode=%p due to mand locks\n", |
1668 | inode); | |
18cceb6a | 1669 | CIFS_I(inode)->oplock = 0; |
63b7d3a4 PS |
1670 | } |
1671 | ||
d39a4f71 PS |
1672 | rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, |
1673 | type, 1, 0, wait_flag); | |
161ebf9f PS |
1674 | if (rc) { |
1675 | kfree(lock); | |
21cb2d90 | 1676 | return rc; |
03776f45 | 1677 | } |
161ebf9f | 1678 | |
fbd35aca | 1679 | cifs_lock_add(cfile, lock); |
9ee305b7 | 1680 | } else if (unlock) |
d39a4f71 | 1681 | rc = server->ops->mand_unlock_range(cfile, flock, xid); |
03776f45 | 1682 | |
03776f45 | 1683 | out: |
d0677992 | 1684 | if ((flock->fl_flags & FL_POSIX) || (flock->fl_flags & FL_FLOCK)) { |
bc31d0cd AA |
1685 | /* |
1686 | * If this is a request to remove all locks because we | |
1687 | * are closing the file, it doesn't matter if the | |
1688 | * unlocking failed as both cifs.ko and the SMB server | |
1689 | * remove the lock on file close | |
1690 | */ | |
1691 | if (rc) { | |
1692 | cifs_dbg(VFS, "%s failed rc=%d\n", __func__, rc); | |
1693 | if (!(flock->fl_flags & FL_CLOSE)) | |
1694 | return rc; | |
1695 | } | |
4f656367 | 1696 | rc = locks_lock_file_wait(file, flock); |
bc31d0cd | 1697 | } |
03776f45 PS |
1698 | return rc; |
1699 | } | |
1700 | ||
d0677992 SF |
1701 | int cifs_flock(struct file *file, int cmd, struct file_lock *fl) |
1702 | { | |
1703 | int rc, xid; | |
1704 | int lock = 0, unlock = 0; | |
1705 | bool wait_flag = false; | |
1706 | bool posix_lck = false; | |
1707 | struct cifs_sb_info *cifs_sb; | |
1708 | struct cifs_tcon *tcon; | |
d0677992 | 1709 | struct cifsFileInfo *cfile; |
d0677992 SF |
1710 | __u32 type; |
1711 | ||
1712 | rc = -EACCES; | |
1713 | xid = get_xid(); | |
1714 | ||
1715 | if (!(fl->fl_flags & FL_FLOCK)) | |
1716 | return -ENOLCK; | |
1717 | ||
1718 | cfile = (struct cifsFileInfo *)file->private_data; | |
1719 | tcon = tlink_tcon(cfile->tlink); | |
1720 | ||
1721 | cifs_read_flock(fl, &type, &lock, &unlock, &wait_flag, | |
1722 | tcon->ses->server); | |
1723 | cifs_sb = CIFS_FILE_SB(file); | |
d0677992 SF |
1724 | |
1725 | if (cap_unix(tcon->ses) && | |
1726 | (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) && | |
1727 | ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) | |
1728 | posix_lck = true; | |
1729 | ||
1730 | if (!lock && !unlock) { | |
1731 | /* | |
1732 | * if no lock or unlock then nothing to do since we do not | |
1733 | * know what it is | |
1734 | */ | |
1735 | free_xid(xid); | |
1736 | return -EOPNOTSUPP; | |
1737 | } | |
1738 | ||
1739 | rc = cifs_setlk(file, fl, type, wait_flag, posix_lck, lock, unlock, | |
1740 | xid); | |
1741 | free_xid(xid); | |
1742 | return rc; | |
1743 | ||
1744 | ||
1745 | } | |
1746 | ||
03776f45 PS |
1747 | int cifs_lock(struct file *file, int cmd, struct file_lock *flock) |
1748 | { | |
1749 | int rc, xid; | |
1750 | int lock = 0, unlock = 0; | |
1751 | bool wait_flag = false; | |
1752 | bool posix_lck = false; | |
1753 | struct cifs_sb_info *cifs_sb; | |
1754 | struct cifs_tcon *tcon; | |
03776f45 | 1755 | struct cifsFileInfo *cfile; |
04a6aa8a | 1756 | __u32 type; |
03776f45 PS |
1757 | |
1758 | rc = -EACCES; | |
6d5786a3 | 1759 | xid = get_xid(); |
03776f45 | 1760 | |
f96637be JP |
1761 | cifs_dbg(FYI, "Lock parm: 0x%x flockflags: 0x%x flocktype: 0x%x start: %lld end: %lld\n", |
1762 | cmd, flock->fl_flags, flock->fl_type, | |
1763 | flock->fl_start, flock->fl_end); | |
03776f45 | 1764 | |
03776f45 PS |
1765 | cfile = (struct cifsFileInfo *)file->private_data; |
1766 | tcon = tlink_tcon(cfile->tlink); | |
106dc538 PS |
1767 | |
1768 | cifs_read_flock(flock, &type, &lock, &unlock, &wait_flag, | |
1769 | tcon->ses->server); | |
7119e220 | 1770 | cifs_sb = CIFS_FILE_SB(file); |
03776f45 | 1771 | |
29e20f9c | 1772 | if (cap_unix(tcon->ses) && |
03776f45 PS |
1773 | (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) && |
1774 | ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) | |
1775 | posix_lck = true; | |
1776 | /* | |
1777 | * BB add code here to normalize offset and length to account for | |
1778 | * negative length which we can not accept over the wire. | |
1779 | */ | |
1780 | if (IS_GETLK(cmd)) { | |
4f6bcec9 | 1781 | rc = cifs_getlk(file, flock, type, wait_flag, posix_lck, xid); |
6d5786a3 | 1782 | free_xid(xid); |
03776f45 PS |
1783 | return rc; |
1784 | } | |
1785 | ||
1786 | if (!lock && !unlock) { | |
1787 | /* | |
1788 | * if no lock or unlock then nothing to do since we do not | |
1789 | * know what it is | |
1790 | */ | |
6d5786a3 | 1791 | free_xid(xid); |
03776f45 | 1792 | return -EOPNOTSUPP; |
7ee1af76 JA |
1793 | } |
1794 | ||
03776f45 PS |
1795 | rc = cifs_setlk(file, flock, type, wait_flag, posix_lck, lock, unlock, |
1796 | xid); | |
6d5786a3 | 1797 | free_xid(xid); |
1da177e4 LT |
1798 | return rc; |
1799 | } | |
1800 | ||
597b027f JL |
1801 | /* |
1802 | * update the file size (if needed) after a write. Should be called with | |
1803 | * the inode->i_lock held | |
1804 | */ | |
72432ffc | 1805 | void |
fbec9ab9 JL |
1806 | cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset, |
1807 | unsigned int bytes_written) | |
1808 | { | |
1809 | loff_t end_of_write = offset + bytes_written; | |
1810 | ||
1811 | if (end_of_write > cifsi->server_eof) | |
1812 | cifsi->server_eof = end_of_write; | |
1813 | } | |
1814 | ||
ba9ad725 PS |
1815 | static ssize_t |
1816 | cifs_write(struct cifsFileInfo *open_file, __u32 pid, const char *write_data, | |
1817 | size_t write_size, loff_t *offset) | |
1da177e4 LT |
1818 | { |
1819 | int rc = 0; | |
1820 | unsigned int bytes_written = 0; | |
1821 | unsigned int total_written; | |
ba9ad725 PS |
1822 | struct cifs_tcon *tcon; |
1823 | struct TCP_Server_Info *server; | |
6d5786a3 | 1824 | unsigned int xid; |
7da4b49a | 1825 | struct dentry *dentry = open_file->dentry; |
2b0143b5 | 1826 | struct cifsInodeInfo *cifsi = CIFS_I(d_inode(dentry)); |
fa2989f4 | 1827 | struct cifs_io_parms io_parms; |
1da177e4 | 1828 | |
35c265e0 AV |
1829 | cifs_dbg(FYI, "write %zd bytes to offset %lld of %pd\n", |
1830 | write_size, *offset, dentry); | |
1da177e4 | 1831 | |
ba9ad725 PS |
1832 | tcon = tlink_tcon(open_file->tlink); |
1833 | server = tcon->ses->server; | |
1834 | ||
1835 | if (!server->ops->sync_write) | |
1836 | return -ENOSYS; | |
50c2f753 | 1837 | |
6d5786a3 | 1838 | xid = get_xid(); |
1da177e4 | 1839 | |
1da177e4 LT |
1840 | for (total_written = 0; write_size > total_written; |
1841 | total_written += bytes_written) { | |
1842 | rc = -EAGAIN; | |
1843 | while (rc == -EAGAIN) { | |
ca83ce3d JL |
1844 | struct kvec iov[2]; |
1845 | unsigned int len; | |
1846 | ||
1da177e4 | 1847 | if (open_file->invalidHandle) { |
1da177e4 LT |
1848 | /* we could deadlock if we called |
1849 | filemap_fdatawait from here so tell | |
fb8c4b14 | 1850 | reopen_file not to flush data to |
1da177e4 | 1851 | server now */ |
15886177 | 1852 | rc = cifs_reopen_file(open_file, false); |
1da177e4 LT |
1853 | if (rc != 0) |
1854 | break; | |
1855 | } | |
ca83ce3d | 1856 | |
2b0143b5 | 1857 | len = min(server->ops->wp_retry_size(d_inode(dentry)), |
cb7e9eab | 1858 | (unsigned int)write_size - total_written); |
ca83ce3d JL |
1859 | /* iov[0] is reserved for smb header */ |
1860 | iov[1].iov_base = (char *)write_data + total_written; | |
1861 | iov[1].iov_len = len; | |
fa2989f4 | 1862 | io_parms.pid = pid; |
ba9ad725 PS |
1863 | io_parms.tcon = tcon; |
1864 | io_parms.offset = *offset; | |
fa2989f4 | 1865 | io_parms.length = len; |
db8b631d SF |
1866 | rc = server->ops->sync_write(xid, &open_file->fid, |
1867 | &io_parms, &bytes_written, iov, 1); | |
1da177e4 LT |
1868 | } |
1869 | if (rc || (bytes_written == 0)) { | |
1870 | if (total_written) | |
1871 | break; | |
1872 | else { | |
6d5786a3 | 1873 | free_xid(xid); |
1da177e4 LT |
1874 | return rc; |
1875 | } | |
fbec9ab9 | 1876 | } else { |
2b0143b5 | 1877 | spin_lock(&d_inode(dentry)->i_lock); |
ba9ad725 | 1878 | cifs_update_eof(cifsi, *offset, bytes_written); |
2b0143b5 | 1879 | spin_unlock(&d_inode(dentry)->i_lock); |
ba9ad725 | 1880 | *offset += bytes_written; |
fbec9ab9 | 1881 | } |
1da177e4 LT |
1882 | } |
1883 | ||
ba9ad725 | 1884 | cifs_stats_bytes_written(tcon, total_written); |
1da177e4 | 1885 | |
7da4b49a | 1886 | if (total_written > 0) { |
2b0143b5 DH |
1887 | spin_lock(&d_inode(dentry)->i_lock); |
1888 | if (*offset > d_inode(dentry)->i_size) | |
1889 | i_size_write(d_inode(dentry), *offset); | |
1890 | spin_unlock(&d_inode(dentry)->i_lock); | |
1da177e4 | 1891 | } |
2b0143b5 | 1892 | mark_inode_dirty_sync(d_inode(dentry)); |
6d5786a3 | 1893 | free_xid(xid); |
1da177e4 LT |
1894 | return total_written; |
1895 | } | |
1896 | ||
6508d904 JL |
1897 | struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode, |
1898 | bool fsuid_only) | |
630f3f0c SF |
1899 | { |
1900 | struct cifsFileInfo *open_file = NULL; | |
6508d904 JL |
1901 | struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb); |
1902 | ||
1903 | /* only filter by fsuid on multiuser mounts */ | |
1904 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER)) | |
1905 | fsuid_only = false; | |
630f3f0c | 1906 | |
cb248819 | 1907 | spin_lock(&cifs_inode->open_file_lock); |
630f3f0c SF |
1908 | /* we could simply get the first_list_entry since write-only entries |
1909 | are always at the end of the list but since the first entry might | |
1910 | have a close pending, we go through the whole list */ | |
1911 | list_for_each_entry(open_file, &cifs_inode->openFileList, flist) { | |
fef59fd7 | 1912 | if (fsuid_only && !uid_eq(open_file->uid, current_fsuid())) |
6508d904 | 1913 | continue; |
2e396b83 | 1914 | if (OPEN_FMODE(open_file->f_flags) & FMODE_READ) { |
630f3f0c SF |
1915 | if (!open_file->invalidHandle) { |
1916 | /* found a good file */ | |
1917 | /* lock it so it will not be closed on us */ | |
3afca265 | 1918 | cifsFileInfo_get(open_file); |
cb248819 | 1919 | spin_unlock(&cifs_inode->open_file_lock); |
630f3f0c SF |
1920 | return open_file; |
1921 | } /* else might as well continue, and look for | |
1922 | another, or simply have the caller reopen it | |
1923 | again rather than trying to fix this handle */ | |
1924 | } else /* write only file */ | |
1925 | break; /* write only files are last so must be done */ | |
1926 | } | |
cb248819 | 1927 | spin_unlock(&cifs_inode->open_file_lock); |
630f3f0c SF |
1928 | return NULL; |
1929 | } | |
630f3f0c | 1930 | |
fe768d51 PS |
1931 | /* Return -EBADF if no handle is found and general rc otherwise */ |
1932 | int | |
1933 | cifs_get_writable_file(struct cifsInodeInfo *cifs_inode, bool fsuid_only, | |
1934 | struct cifsFileInfo **ret_file) | |
6148a742 | 1935 | { |
2c0c2a08 | 1936 | struct cifsFileInfo *open_file, *inv_file = NULL; |
d3892294 | 1937 | struct cifs_sb_info *cifs_sb; |
2846d386 | 1938 | bool any_available = false; |
fe768d51 | 1939 | int rc = -EBADF; |
2c0c2a08 | 1940 | unsigned int refind = 0; |
6148a742 | 1941 | |
fe768d51 PS |
1942 | *ret_file = NULL; |
1943 | ||
1944 | /* | |
1945 | * Having a null inode here (because mapping->host was set to zero by | |
1946 | * the VFS or MM) should not happen but we had reports of on oops (due | |
1947 | * to it being zero) during stress testcases so we need to check for it | |
1948 | */ | |
60808233 | 1949 | |
fb8c4b14 | 1950 | if (cifs_inode == NULL) { |
f96637be | 1951 | cifs_dbg(VFS, "Null inode passed to cifs_writeable_file\n"); |
60808233 | 1952 | dump_stack(); |
fe768d51 | 1953 | return rc; |
60808233 SF |
1954 | } |
1955 | ||
d3892294 JL |
1956 | cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb); |
1957 | ||
6508d904 JL |
1958 | /* only filter by fsuid on multiuser mounts */ |
1959 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER)) | |
1960 | fsuid_only = false; | |
1961 | ||
cb248819 | 1962 | spin_lock(&cifs_inode->open_file_lock); |
9b22b0b7 | 1963 | refind_writable: |
2c0c2a08 | 1964 | if (refind > MAX_REOPEN_ATT) { |
cb248819 | 1965 | spin_unlock(&cifs_inode->open_file_lock); |
fe768d51 | 1966 | return rc; |
2c0c2a08 | 1967 | } |
6148a742 | 1968 | list_for_each_entry(open_file, &cifs_inode->openFileList, flist) { |
6508d904 JL |
1969 | if (!any_available && open_file->pid != current->tgid) |
1970 | continue; | |
fef59fd7 | 1971 | if (fsuid_only && !uid_eq(open_file->uid, current_fsuid())) |
6148a742 | 1972 | continue; |
2e396b83 | 1973 | if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) { |
9b22b0b7 SF |
1974 | if (!open_file->invalidHandle) { |
1975 | /* found a good writable file */ | |
3afca265 | 1976 | cifsFileInfo_get(open_file); |
cb248819 | 1977 | spin_unlock(&cifs_inode->open_file_lock); |
fe768d51 PS |
1978 | *ret_file = open_file; |
1979 | return 0; | |
2c0c2a08 SP |
1980 | } else { |
1981 | if (!inv_file) | |
1982 | inv_file = open_file; | |
9b22b0b7 | 1983 | } |
6148a742 SF |
1984 | } |
1985 | } | |
2846d386 JL |
1986 | /* couldn't find useable FH with same pid, try any available */ |
1987 | if (!any_available) { | |
1988 | any_available = true; | |
1989 | goto refind_writable; | |
1990 | } | |
2c0c2a08 SP |
1991 | |
1992 | if (inv_file) { | |
1993 | any_available = false; | |
3afca265 | 1994 | cifsFileInfo_get(inv_file); |
2c0c2a08 SP |
1995 | } |
1996 | ||
cb248819 | 1997 | spin_unlock(&cifs_inode->open_file_lock); |
2c0c2a08 SP |
1998 | |
1999 | if (inv_file) { | |
2000 | rc = cifs_reopen_file(inv_file, false); | |
fe768d51 PS |
2001 | if (!rc) { |
2002 | *ret_file = inv_file; | |
2003 | return 0; | |
2c0c2a08 | 2004 | } |
fe768d51 | 2005 | |
487317c9 | 2006 | spin_lock(&cifs_inode->open_file_lock); |
fe768d51 | 2007 | list_move_tail(&inv_file->flist, &cifs_inode->openFileList); |
487317c9 | 2008 | spin_unlock(&cifs_inode->open_file_lock); |
fe768d51 PS |
2009 | cifsFileInfo_put(inv_file); |
2010 | ++refind; | |
2011 | inv_file = NULL; | |
cb248819 | 2012 | spin_lock(&cifs_inode->open_file_lock); |
fe768d51 | 2013 | goto refind_writable; |
2c0c2a08 SP |
2014 | } |
2015 | ||
fe768d51 PS |
2016 | return rc; |
2017 | } | |
2018 | ||
2019 | struct cifsFileInfo * | |
2020 | find_writable_file(struct cifsInodeInfo *cifs_inode, bool fsuid_only) | |
2021 | { | |
2022 | struct cifsFileInfo *cfile; | |
2023 | int rc; | |
2024 | ||
2025 | rc = cifs_get_writable_file(cifs_inode, fsuid_only, &cfile); | |
2026 | if (rc) | |
2027 | cifs_dbg(FYI, "couldn't find writable handle rc=%d", rc); | |
2028 | ||
2029 | return cfile; | |
6148a742 SF |
2030 | } |
2031 | ||
8de9e86c RS |
2032 | int |
2033 | cifs_get_writable_path(struct cifs_tcon *tcon, const char *name, | |
2034 | struct cifsFileInfo **ret_file) | |
2035 | { | |
2036 | struct list_head *tmp; | |
2037 | struct cifsFileInfo *cfile; | |
2038 | struct cifsInodeInfo *cinode; | |
2039 | char *full_path; | |
2040 | ||
2041 | *ret_file = NULL; | |
2042 | ||
2043 | spin_lock(&tcon->open_file_lock); | |
2044 | list_for_each(tmp, &tcon->openFileList) { | |
2045 | cfile = list_entry(tmp, struct cifsFileInfo, | |
2046 | tlist); | |
2047 | full_path = build_path_from_dentry(cfile->dentry); | |
2048 | if (full_path == NULL) { | |
2049 | spin_unlock(&tcon->open_file_lock); | |
2050 | return -ENOMEM; | |
2051 | } | |
2052 | if (strcmp(full_path, name)) { | |
2053 | kfree(full_path); | |
2054 | continue; | |
2055 | } | |
2056 | ||
2057 | kfree(full_path); | |
2058 | cinode = CIFS_I(d_inode(cfile->dentry)); | |
2059 | spin_unlock(&tcon->open_file_lock); | |
2060 | return cifs_get_writable_file(cinode, 0, ret_file); | |
2061 | } | |
2062 | ||
2063 | spin_unlock(&tcon->open_file_lock); | |
2064 | return -ENOENT; | |
2065 | } | |
2066 | ||
496902dc RS |
2067 | int |
2068 | cifs_get_readable_path(struct cifs_tcon *tcon, const char *name, | |
2069 | struct cifsFileInfo **ret_file) | |
2070 | { | |
2071 | struct list_head *tmp; | |
2072 | struct cifsFileInfo *cfile; | |
2073 | struct cifsInodeInfo *cinode; | |
2074 | char *full_path; | |
2075 | ||
2076 | *ret_file = NULL; | |
2077 | ||
2078 | spin_lock(&tcon->open_file_lock); | |
2079 | list_for_each(tmp, &tcon->openFileList) { | |
2080 | cfile = list_entry(tmp, struct cifsFileInfo, | |
2081 | tlist); | |
2082 | full_path = build_path_from_dentry(cfile->dentry); | |
2083 | if (full_path == NULL) { | |
2084 | spin_unlock(&tcon->open_file_lock); | |
2085 | return -ENOMEM; | |
2086 | } | |
2087 | if (strcmp(full_path, name)) { | |
2088 | kfree(full_path); | |
2089 | continue; | |
2090 | } | |
2091 | ||
2092 | kfree(full_path); | |
2093 | cinode = CIFS_I(d_inode(cfile->dentry)); | |
2094 | spin_unlock(&tcon->open_file_lock); | |
2095 | *ret_file = find_readable_file(cinode, 0); | |
2096 | return *ret_file ? 0 : -ENOENT; | |
2097 | } | |
2098 | ||
2099 | spin_unlock(&tcon->open_file_lock); | |
2100 | return -ENOENT; | |
2101 | } | |
2102 | ||
1da177e4 LT |
2103 | static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to) |
2104 | { | |
2105 | struct address_space *mapping = page->mapping; | |
09cbfeaf | 2106 | loff_t offset = (loff_t)page->index << PAGE_SHIFT; |
1da177e4 LT |
2107 | char *write_data; |
2108 | int rc = -EFAULT; | |
2109 | int bytes_written = 0; | |
1da177e4 | 2110 | struct inode *inode; |
6148a742 | 2111 | struct cifsFileInfo *open_file; |
1da177e4 LT |
2112 | |
2113 | if (!mapping || !mapping->host) | |
2114 | return -EFAULT; | |
2115 | ||
2116 | inode = page->mapping->host; | |
1da177e4 LT |
2117 | |
2118 | offset += (loff_t)from; | |
2119 | write_data = kmap(page); | |
2120 | write_data += from; | |
2121 | ||
09cbfeaf | 2122 | if ((to > PAGE_SIZE) || (from > to)) { |
1da177e4 LT |
2123 | kunmap(page); |
2124 | return -EIO; | |
2125 | } | |
2126 | ||
2127 | /* racing with truncate? */ | |
2128 | if (offset > mapping->host->i_size) { | |
2129 | kunmap(page); | |
2130 | return 0; /* don't care */ | |
2131 | } | |
2132 | ||
2133 | /* check to make sure that we are not extending the file */ | |
2134 | if (mapping->host->i_size - offset < (loff_t)to) | |
fb8c4b14 | 2135 | to = (unsigned)(mapping->host->i_size - offset); |
1da177e4 | 2136 | |
fe768d51 PS |
2137 | rc = cifs_get_writable_file(CIFS_I(mapping->host), false, &open_file); |
2138 | if (!rc) { | |
fa2989f4 PS |
2139 | bytes_written = cifs_write(open_file, open_file->pid, |
2140 | write_data, to - from, &offset); | |
6ab409b5 | 2141 | cifsFileInfo_put(open_file); |
1da177e4 | 2142 | /* Does mm or vfs already set times? */ |
c2050a45 | 2143 | inode->i_atime = inode->i_mtime = current_time(inode); |
bb5a9a04 | 2144 | if ((bytes_written > 0) && (offset)) |
6148a742 | 2145 | rc = 0; |
bb5a9a04 SF |
2146 | else if (bytes_written < 0) |
2147 | rc = bytes_written; | |
fe768d51 PS |
2148 | else |
2149 | rc = -EFAULT; | |
6148a742 | 2150 | } else { |
fe768d51 PS |
2151 | cifs_dbg(FYI, "No writable handle for write page rc=%d\n", rc); |
2152 | if (!is_retryable_error(rc)) | |
2153 | rc = -EIO; | |
1da177e4 LT |
2154 | } |
2155 | ||
2156 | kunmap(page); | |
2157 | return rc; | |
2158 | } | |
2159 | ||
90ac1387 PS |
2160 | static struct cifs_writedata * |
2161 | wdata_alloc_and_fillpages(pgoff_t tofind, struct address_space *mapping, | |
2162 | pgoff_t end, pgoff_t *index, | |
2163 | unsigned int *found_pages) | |
2164 | { | |
90ac1387 PS |
2165 | struct cifs_writedata *wdata; |
2166 | ||
2167 | wdata = cifs_writedata_alloc((unsigned int)tofind, | |
2168 | cifs_writev_complete); | |
2169 | if (!wdata) | |
2170 | return NULL; | |
2171 | ||
9c19a9cb JK |
2172 | *found_pages = find_get_pages_range_tag(mapping, index, end, |
2173 | PAGECACHE_TAG_DIRTY, tofind, wdata->pages); | |
90ac1387 PS |
2174 | return wdata; |
2175 | } | |
2176 | ||
7e48ff82 PS |
2177 | static unsigned int |
2178 | wdata_prepare_pages(struct cifs_writedata *wdata, unsigned int found_pages, | |
2179 | struct address_space *mapping, | |
2180 | struct writeback_control *wbc, | |
2181 | pgoff_t end, pgoff_t *index, pgoff_t *next, bool *done) | |
2182 | { | |
2183 | unsigned int nr_pages = 0, i; | |
2184 | struct page *page; | |
2185 | ||
2186 | for (i = 0; i < found_pages; i++) { | |
2187 | page = wdata->pages[i]; | |
2188 | /* | |
b93b0163 MW |
2189 | * At this point we hold neither the i_pages lock nor the |
2190 | * page lock: the page may be truncated or invalidated | |
2191 | * (changing page->mapping to NULL), or even swizzled | |
2192 | * back from swapper_space to tmpfs file mapping | |
7e48ff82 PS |
2193 | */ |
2194 | ||
2195 | if (nr_pages == 0) | |
2196 | lock_page(page); | |
2197 | else if (!trylock_page(page)) | |
2198 | break; | |
2199 | ||
2200 | if (unlikely(page->mapping != mapping)) { | |
2201 | unlock_page(page); | |
2202 | break; | |
2203 | } | |
2204 | ||
2205 | if (!wbc->range_cyclic && page->index > end) { | |
2206 | *done = true; | |
2207 | unlock_page(page); | |
2208 | break; | |
2209 | } | |
2210 | ||
2211 | if (*next && (page->index != *next)) { | |
2212 | /* Not next consecutive page */ | |
2213 | unlock_page(page); | |
2214 | break; | |
2215 | } | |
2216 | ||
2217 | if (wbc->sync_mode != WB_SYNC_NONE) | |
2218 | wait_on_page_writeback(page); | |
2219 | ||
2220 | if (PageWriteback(page) || | |
2221 | !clear_page_dirty_for_io(page)) { | |
2222 | unlock_page(page); | |
2223 | break; | |
2224 | } | |
2225 | ||
2226 | /* | |
2227 | * This actually clears the dirty bit in the radix tree. | |
2228 | * See cifs_writepage() for more commentary. | |
2229 | */ | |
2230 | set_page_writeback(page); | |
2231 | if (page_offset(page) >= i_size_read(mapping->host)) { | |
2232 | *done = true; | |
2233 | unlock_page(page); | |
2234 | end_page_writeback(page); | |
2235 | break; | |
2236 | } | |
2237 | ||
2238 | wdata->pages[i] = page; | |
2239 | *next = page->index + 1; | |
2240 | ++nr_pages; | |
2241 | } | |
2242 | ||
2243 | /* reset index to refind any pages skipped */ | |
2244 | if (nr_pages == 0) | |
2245 | *index = wdata->pages[0]->index + 1; | |
2246 | ||
2247 | /* put any pages we aren't going to use */ | |
2248 | for (i = nr_pages; i < found_pages; i++) { | |
09cbfeaf | 2249 | put_page(wdata->pages[i]); |
7e48ff82 PS |
2250 | wdata->pages[i] = NULL; |
2251 | } | |
2252 | ||
2253 | return nr_pages; | |
2254 | } | |
2255 | ||
619aa48e | 2256 | static int |
c4b8f657 PS |
2257 | wdata_send_pages(struct cifs_writedata *wdata, unsigned int nr_pages, |
2258 | struct address_space *mapping, struct writeback_control *wbc) | |
619aa48e | 2259 | { |
258f0603 | 2260 | int rc; |
c4b8f657 PS |
2261 | struct TCP_Server_Info *server = |
2262 | tlink_tcon(wdata->cfile->tlink)->ses->server; | |
619aa48e PS |
2263 | |
2264 | wdata->sync_mode = wbc->sync_mode; | |
2265 | wdata->nr_pages = nr_pages; | |
2266 | wdata->offset = page_offset(wdata->pages[0]); | |
09cbfeaf | 2267 | wdata->pagesz = PAGE_SIZE; |
619aa48e PS |
2268 | wdata->tailsz = min(i_size_read(mapping->host) - |
2269 | page_offset(wdata->pages[nr_pages - 1]), | |
09cbfeaf KS |
2270 | (loff_t)PAGE_SIZE); |
2271 | wdata->bytes = ((nr_pages - 1) * PAGE_SIZE) + wdata->tailsz; | |
c4b8f657 | 2272 | wdata->pid = wdata->cfile->pid; |
619aa48e | 2273 | |
9a1c67e8 PS |
2274 | rc = adjust_credits(server, &wdata->credits, wdata->bytes); |
2275 | if (rc) | |
258f0603 | 2276 | return rc; |
9a1c67e8 | 2277 | |
c4b8f657 PS |
2278 | if (wdata->cfile->invalidHandle) |
2279 | rc = -EAGAIN; | |
2280 | else | |
2281 | rc = server->ops->async_writev(wdata, cifs_writedata_release); | |
619aa48e | 2282 | |
619aa48e PS |
2283 | return rc; |
2284 | } | |
2285 | ||
1da177e4 | 2286 | static int cifs_writepages(struct address_space *mapping, |
37c0eb46 | 2287 | struct writeback_control *wbc) |
1da177e4 | 2288 | { |
c7d38dbe PS |
2289 | struct inode *inode = mapping->host; |
2290 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); | |
cb7e9eab | 2291 | struct TCP_Server_Info *server; |
c3d17b63 JL |
2292 | bool done = false, scanned = false, range_whole = false; |
2293 | pgoff_t end, index; | |
2294 | struct cifs_writedata *wdata; | |
c7d38dbe | 2295 | struct cifsFileInfo *cfile = NULL; |
37c0eb46 | 2296 | int rc = 0; |
9a66396f | 2297 | int saved_rc = 0; |
0cb012d1 | 2298 | unsigned int xid; |
50c2f753 | 2299 | |
37c0eb46 | 2300 | /* |
c3d17b63 | 2301 | * If wsize is smaller than the page cache size, default to writing |
37c0eb46 SF |
2302 | * one page at a time via cifs_writepage |
2303 | */ | |
09cbfeaf | 2304 | if (cifs_sb->wsize < PAGE_SIZE) |
37c0eb46 SF |
2305 | return generic_writepages(mapping, wbc); |
2306 | ||
0cb012d1 | 2307 | xid = get_xid(); |
111ebb6e | 2308 | if (wbc->range_cyclic) { |
37c0eb46 | 2309 | index = mapping->writeback_index; /* Start from prev offset */ |
111ebb6e OH |
2310 | end = -1; |
2311 | } else { | |
09cbfeaf KS |
2312 | index = wbc->range_start >> PAGE_SHIFT; |
2313 | end = wbc->range_end >> PAGE_SHIFT; | |
111ebb6e | 2314 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) |
c3d17b63 JL |
2315 | range_whole = true; |
2316 | scanned = true; | |
37c0eb46 | 2317 | } |
cb7e9eab | 2318 | server = cifs_sb_master_tcon(cifs_sb)->ses->server; |
37c0eb46 | 2319 | retry: |
c3d17b63 | 2320 | while (!done && index <= end) { |
335b7b62 | 2321 | unsigned int i, nr_pages, found_pages, wsize; |
66231a47 | 2322 | pgoff_t next = 0, tofind, saved_index = index; |
335b7b62 PS |
2323 | struct cifs_credits credits_on_stack; |
2324 | struct cifs_credits *credits = &credits_on_stack; | |
fe768d51 | 2325 | int get_file_rc = 0; |
c3d17b63 | 2326 | |
c7d38dbe PS |
2327 | if (cfile) |
2328 | cifsFileInfo_put(cfile); | |
2329 | ||
fe768d51 PS |
2330 | rc = cifs_get_writable_file(CIFS_I(inode), false, &cfile); |
2331 | ||
2332 | /* in case of an error store it to return later */ | |
2333 | if (rc) | |
2334 | get_file_rc = rc; | |
c7d38dbe | 2335 | |
cb7e9eab | 2336 | rc = server->ops->wait_mtu_credits(server, cifs_sb->wsize, |
335b7b62 | 2337 | &wsize, credits); |
9a66396f PS |
2338 | if (rc != 0) { |
2339 | done = true; | |
cb7e9eab | 2340 | break; |
9a66396f | 2341 | } |
c3d17b63 | 2342 | |
09cbfeaf | 2343 | tofind = min((wsize / PAGE_SIZE) - 1, end - index) + 1; |
c3d17b63 | 2344 | |
90ac1387 PS |
2345 | wdata = wdata_alloc_and_fillpages(tofind, mapping, end, &index, |
2346 | &found_pages); | |
c3d17b63 JL |
2347 | if (!wdata) { |
2348 | rc = -ENOMEM; | |
9a66396f | 2349 | done = true; |
cb7e9eab | 2350 | add_credits_and_wake_if(server, credits, 0); |
c3d17b63 JL |
2351 | break; |
2352 | } | |
2353 | ||
c3d17b63 JL |
2354 | if (found_pages == 0) { |
2355 | kref_put(&wdata->refcount, cifs_writedata_release); | |
cb7e9eab | 2356 | add_credits_and_wake_if(server, credits, 0); |
c3d17b63 JL |
2357 | break; |
2358 | } | |
2359 | ||
7e48ff82 PS |
2360 | nr_pages = wdata_prepare_pages(wdata, found_pages, mapping, wbc, |
2361 | end, &index, &next, &done); | |
37c0eb46 | 2362 | |
c3d17b63 JL |
2363 | /* nothing to write? */ |
2364 | if (nr_pages == 0) { | |
2365 | kref_put(&wdata->refcount, cifs_writedata_release); | |
cb7e9eab | 2366 | add_credits_and_wake_if(server, credits, 0); |
c3d17b63 | 2367 | continue; |
37c0eb46 | 2368 | } |
fbec9ab9 | 2369 | |
335b7b62 | 2370 | wdata->credits = credits_on_stack; |
c7d38dbe PS |
2371 | wdata->cfile = cfile; |
2372 | cfile = NULL; | |
941b853d | 2373 | |
c4b8f657 | 2374 | if (!wdata->cfile) { |
fe768d51 PS |
2375 | cifs_dbg(VFS, "No writable handle in writepages rc=%d\n", |
2376 | get_file_rc); | |
2377 | if (is_retryable_error(get_file_rc)) | |
2378 | rc = get_file_rc; | |
2379 | else | |
2380 | rc = -EBADF; | |
c4b8f657 PS |
2381 | } else |
2382 | rc = wdata_send_pages(wdata, nr_pages, mapping, wbc); | |
f3983c21 | 2383 | |
258f0603 PS |
2384 | for (i = 0; i < nr_pages; ++i) |
2385 | unlock_page(wdata->pages[i]); | |
2386 | ||
c3d17b63 JL |
2387 | /* send failure -- clean up the mess */ |
2388 | if (rc != 0) { | |
335b7b62 | 2389 | add_credits_and_wake_if(server, &wdata->credits, 0); |
c3d17b63 | 2390 | for (i = 0; i < nr_pages; ++i) { |
9a66396f | 2391 | if (is_retryable_error(rc)) |
c3d17b63 JL |
2392 | redirty_page_for_writepage(wbc, |
2393 | wdata->pages[i]); | |
2394 | else | |
2395 | SetPageError(wdata->pages[i]); | |
2396 | end_page_writeback(wdata->pages[i]); | |
09cbfeaf | 2397 | put_page(wdata->pages[i]); |
37c0eb46 | 2398 | } |
9a66396f | 2399 | if (!is_retryable_error(rc)) |
941b853d | 2400 | mapping_set_error(mapping, rc); |
c3d17b63 JL |
2401 | } |
2402 | kref_put(&wdata->refcount, cifs_writedata_release); | |
941b853d | 2403 | |
66231a47 PS |
2404 | if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN) { |
2405 | index = saved_index; | |
2406 | continue; | |
2407 | } | |
2408 | ||
9a66396f PS |
2409 | /* Return immediately if we received a signal during writing */ |
2410 | if (is_interrupt_error(rc)) { | |
2411 | done = true; | |
2412 | break; | |
2413 | } | |
2414 | ||
2415 | if (rc != 0 && saved_rc == 0) | |
2416 | saved_rc = rc; | |
2417 | ||
c3d17b63 JL |
2418 | wbc->nr_to_write -= nr_pages; |
2419 | if (wbc->nr_to_write <= 0) | |
2420 | done = true; | |
b066a48c | 2421 | |
c3d17b63 | 2422 | index = next; |
37c0eb46 | 2423 | } |
c3d17b63 | 2424 | |
37c0eb46 SF |
2425 | if (!scanned && !done) { |
2426 | /* | |
2427 | * We hit the last page and there is more work to be done: wrap | |
2428 | * back to the start of the file | |
2429 | */ | |
c3d17b63 | 2430 | scanned = true; |
37c0eb46 SF |
2431 | index = 0; |
2432 | goto retry; | |
2433 | } | |
c3d17b63 | 2434 | |
9a66396f PS |
2435 | if (saved_rc != 0) |
2436 | rc = saved_rc; | |
2437 | ||
111ebb6e | 2438 | if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) |
37c0eb46 SF |
2439 | mapping->writeback_index = index; |
2440 | ||
c7d38dbe PS |
2441 | if (cfile) |
2442 | cifsFileInfo_put(cfile); | |
0cb012d1 | 2443 | free_xid(xid); |
1da177e4 LT |
2444 | return rc; |
2445 | } | |
1da177e4 | 2446 | |
9ad1506b PS |
2447 | static int |
2448 | cifs_writepage_locked(struct page *page, struct writeback_control *wbc) | |
1da177e4 | 2449 | { |
9ad1506b | 2450 | int rc; |
6d5786a3 | 2451 | unsigned int xid; |
1da177e4 | 2452 | |
6d5786a3 | 2453 | xid = get_xid(); |
1da177e4 | 2454 | /* BB add check for wbc flags */ |
09cbfeaf | 2455 | get_page(page); |
ad7a2926 | 2456 | if (!PageUptodate(page)) |
f96637be | 2457 | cifs_dbg(FYI, "ppw - page not up to date\n"); |
cb876f45 LT |
2458 | |
2459 | /* | |
2460 | * Set the "writeback" flag, and clear "dirty" in the radix tree. | |
2461 | * | |
2462 | * A writepage() implementation always needs to do either this, | |
2463 | * or re-dirty the page with "redirty_page_for_writepage()" in | |
2464 | * the case of a failure. | |
2465 | * | |
2466 | * Just unlocking the page will cause the radix tree tag-bits | |
2467 | * to fail to update with the state of the page correctly. | |
2468 | */ | |
fb8c4b14 | 2469 | set_page_writeback(page); |
9ad1506b | 2470 | retry_write: |
09cbfeaf | 2471 | rc = cifs_partialpagewrite(page, 0, PAGE_SIZE); |
9a66396f PS |
2472 | if (is_retryable_error(rc)) { |
2473 | if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN) | |
97b37f24 | 2474 | goto retry_write; |
9ad1506b | 2475 | redirty_page_for_writepage(wbc, page); |
97b37f24 | 2476 | } else if (rc != 0) { |
9ad1506b | 2477 | SetPageError(page); |
97b37f24 JL |
2478 | mapping_set_error(page->mapping, rc); |
2479 | } else { | |
9ad1506b | 2480 | SetPageUptodate(page); |
97b37f24 | 2481 | } |
cb876f45 | 2482 | end_page_writeback(page); |
09cbfeaf | 2483 | put_page(page); |
6d5786a3 | 2484 | free_xid(xid); |
1da177e4 LT |
2485 | return rc; |
2486 | } | |
2487 | ||
9ad1506b PS |
2488 | static int cifs_writepage(struct page *page, struct writeback_control *wbc) |
2489 | { | |
2490 | int rc = cifs_writepage_locked(page, wbc); | |
2491 | unlock_page(page); | |
2492 | return rc; | |
2493 | } | |
2494 | ||
d9414774 NP |
2495 | static int cifs_write_end(struct file *file, struct address_space *mapping, |
2496 | loff_t pos, unsigned len, unsigned copied, | |
2497 | struct page *page, void *fsdata) | |
1da177e4 | 2498 | { |
d9414774 NP |
2499 | int rc; |
2500 | struct inode *inode = mapping->host; | |
d4ffff1f PS |
2501 | struct cifsFileInfo *cfile = file->private_data; |
2502 | struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb); | |
2503 | __u32 pid; | |
2504 | ||
2505 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) | |
2506 | pid = cfile->pid; | |
2507 | else | |
2508 | pid = current->tgid; | |
1da177e4 | 2509 | |
f96637be | 2510 | cifs_dbg(FYI, "write_end for page %p from pos %lld with %d bytes\n", |
b6b38f70 | 2511 | page, pos, copied); |
d9414774 | 2512 | |
a98ee8c1 JL |
2513 | if (PageChecked(page)) { |
2514 | if (copied == len) | |
2515 | SetPageUptodate(page); | |
2516 | ClearPageChecked(page); | |
09cbfeaf | 2517 | } else if (!PageUptodate(page) && copied == PAGE_SIZE) |
d9414774 | 2518 | SetPageUptodate(page); |
ad7a2926 | 2519 | |
1da177e4 | 2520 | if (!PageUptodate(page)) { |
d9414774 | 2521 | char *page_data; |
09cbfeaf | 2522 | unsigned offset = pos & (PAGE_SIZE - 1); |
6d5786a3 | 2523 | unsigned int xid; |
d9414774 | 2524 | |
6d5786a3 | 2525 | xid = get_xid(); |
1da177e4 LT |
2526 | /* this is probably better than directly calling |
2527 | partialpage_write since in this function the file handle is | |
2528 | known which we might as well leverage */ | |
2529 | /* BB check if anything else missing out of ppw | |
2530 | such as updating last write time */ | |
2531 | page_data = kmap(page); | |
d4ffff1f | 2532 | rc = cifs_write(cfile, pid, page_data + offset, copied, &pos); |
d9414774 | 2533 | /* if (rc < 0) should we set writebehind rc? */ |
1da177e4 | 2534 | kunmap(page); |
d9414774 | 2535 | |
6d5786a3 | 2536 | free_xid(xid); |
fb8c4b14 | 2537 | } else { |
d9414774 NP |
2538 | rc = copied; |
2539 | pos += copied; | |
ca8aa29c | 2540 | set_page_dirty(page); |
1da177e4 LT |
2541 | } |
2542 | ||
d9414774 NP |
2543 | if (rc > 0) { |
2544 | spin_lock(&inode->i_lock); | |
2545 | if (pos > inode->i_size) | |
2546 | i_size_write(inode, pos); | |
2547 | spin_unlock(&inode->i_lock); | |
2548 | } | |
2549 | ||
2550 | unlock_page(page); | |
09cbfeaf | 2551 | put_page(page); |
d9414774 | 2552 | |
1da177e4 LT |
2553 | return rc; |
2554 | } | |
2555 | ||
02c24a82 JB |
2556 | int cifs_strict_fsync(struct file *file, loff_t start, loff_t end, |
2557 | int datasync) | |
1da177e4 | 2558 | { |
6d5786a3 | 2559 | unsigned int xid; |
1da177e4 | 2560 | int rc = 0; |
96daf2b0 | 2561 | struct cifs_tcon *tcon; |
1d8c4c00 | 2562 | struct TCP_Server_Info *server; |
c21dfb69 | 2563 | struct cifsFileInfo *smbfile = file->private_data; |
496ad9aa | 2564 | struct inode *inode = file_inode(file); |
8be7e6ba | 2565 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
1da177e4 | 2566 | |
3b49c9a1 | 2567 | rc = file_write_and_wait_range(file, start, end); |
02c24a82 JB |
2568 | if (rc) |
2569 | return rc; | |
02c24a82 | 2570 | |
6d5786a3 | 2571 | xid = get_xid(); |
1da177e4 | 2572 | |
35c265e0 AV |
2573 | cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n", |
2574 | file, datasync); | |
50c2f753 | 2575 | |
18cceb6a | 2576 | if (!CIFS_CACHE_READ(CIFS_I(inode))) { |
4f73c7d3 | 2577 | rc = cifs_zap_mapping(inode); |
6feb9891 | 2578 | if (rc) { |
f96637be | 2579 | cifs_dbg(FYI, "rc: %d during invalidate phase\n", rc); |
6feb9891 PS |
2580 | rc = 0; /* don't care about it in fsync */ |
2581 | } | |
2582 | } | |
eb4b756b | 2583 | |
8be7e6ba | 2584 | tcon = tlink_tcon(smbfile->tlink); |
1d8c4c00 PS |
2585 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) { |
2586 | server = tcon->ses->server; | |
2587 | if (server->ops->flush) | |
2588 | rc = server->ops->flush(xid, tcon, &smbfile->fid); | |
2589 | else | |
2590 | rc = -ENOSYS; | |
2591 | } | |
8be7e6ba | 2592 | |
6d5786a3 | 2593 | free_xid(xid); |
8be7e6ba PS |
2594 | return rc; |
2595 | } | |
2596 | ||
02c24a82 | 2597 | int cifs_fsync(struct file *file, loff_t start, loff_t end, int datasync) |
8be7e6ba | 2598 | { |
6d5786a3 | 2599 | unsigned int xid; |
8be7e6ba | 2600 | int rc = 0; |
96daf2b0 | 2601 | struct cifs_tcon *tcon; |
1d8c4c00 | 2602 | struct TCP_Server_Info *server; |
8be7e6ba | 2603 | struct cifsFileInfo *smbfile = file->private_data; |
7119e220 | 2604 | struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file); |
02c24a82 | 2605 | |
3b49c9a1 | 2606 | rc = file_write_and_wait_range(file, start, end); |
02c24a82 JB |
2607 | if (rc) |
2608 | return rc; | |
8be7e6ba | 2609 | |
6d5786a3 | 2610 | xid = get_xid(); |
8be7e6ba | 2611 | |
35c265e0 AV |
2612 | cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n", |
2613 | file, datasync); | |
8be7e6ba PS |
2614 | |
2615 | tcon = tlink_tcon(smbfile->tlink); | |
1d8c4c00 PS |
2616 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) { |
2617 | server = tcon->ses->server; | |
2618 | if (server->ops->flush) | |
2619 | rc = server->ops->flush(xid, tcon, &smbfile->fid); | |
2620 | else | |
2621 | rc = -ENOSYS; | |
2622 | } | |
b298f223 | 2623 | |
6d5786a3 | 2624 | free_xid(xid); |
1da177e4 LT |
2625 | return rc; |
2626 | } | |
2627 | ||
1da177e4 LT |
2628 | /* |
2629 | * As file closes, flush all cached write data for this inode checking | |
2630 | * for write behind errors. | |
2631 | */ | |
75e1fcc0 | 2632 | int cifs_flush(struct file *file, fl_owner_t id) |
1da177e4 | 2633 | { |
496ad9aa | 2634 | struct inode *inode = file_inode(file); |
1da177e4 LT |
2635 | int rc = 0; |
2636 | ||
eb4b756b | 2637 | if (file->f_mode & FMODE_WRITE) |
d3f1322a | 2638 | rc = filemap_write_and_wait(inode->i_mapping); |
50c2f753 | 2639 | |
f96637be | 2640 | cifs_dbg(FYI, "Flush inode %p file %p rc %d\n", inode, file, rc); |
1da177e4 LT |
2641 | |
2642 | return rc; | |
2643 | } | |
2644 | ||
72432ffc PS |
2645 | static int |
2646 | cifs_write_allocate_pages(struct page **pages, unsigned long num_pages) | |
2647 | { | |
2648 | int rc = 0; | |
2649 | unsigned long i; | |
2650 | ||
2651 | for (i = 0; i < num_pages; i++) { | |
e94f7ba1 | 2652 | pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM); |
72432ffc PS |
2653 | if (!pages[i]) { |
2654 | /* | |
2655 | * save number of pages we have already allocated and | |
2656 | * return with ENOMEM error | |
2657 | */ | |
2658 | num_pages = i; | |
2659 | rc = -ENOMEM; | |
e94f7ba1 | 2660 | break; |
72432ffc PS |
2661 | } |
2662 | } | |
2663 | ||
e94f7ba1 JL |
2664 | if (rc) { |
2665 | for (i = 0; i < num_pages; i++) | |
2666 | put_page(pages[i]); | |
2667 | } | |
72432ffc PS |
2668 | return rc; |
2669 | } | |
2670 | ||
2671 | static inline | |
2672 | size_t get_numpages(const size_t wsize, const size_t len, size_t *cur_len) | |
2673 | { | |
2674 | size_t num_pages; | |
2675 | size_t clen; | |
2676 | ||
2677 | clen = min_t(const size_t, len, wsize); | |
a7103b99 | 2678 | num_pages = DIV_ROUND_UP(clen, PAGE_SIZE); |
72432ffc PS |
2679 | |
2680 | if (cur_len) | |
2681 | *cur_len = clen; | |
2682 | ||
2683 | return num_pages; | |
2684 | } | |
2685 | ||
da82f7e7 | 2686 | static void |
4a5c80d7 | 2687 | cifs_uncached_writedata_release(struct kref *refcount) |
da82f7e7 JL |
2688 | { |
2689 | int i; | |
4a5c80d7 SF |
2690 | struct cifs_writedata *wdata = container_of(refcount, |
2691 | struct cifs_writedata, refcount); | |
2692 | ||
c610c4b6 | 2693 | kref_put(&wdata->ctx->refcount, cifs_aio_ctx_release); |
4a5c80d7 SF |
2694 | for (i = 0; i < wdata->nr_pages; i++) |
2695 | put_page(wdata->pages[i]); | |
2696 | cifs_writedata_release(refcount); | |
2697 | } | |
2698 | ||
c610c4b6 PS |
2699 | static void collect_uncached_write_data(struct cifs_aio_ctx *ctx); |
2700 | ||
4a5c80d7 SF |
2701 | static void |
2702 | cifs_uncached_writev_complete(struct work_struct *work) | |
2703 | { | |
da82f7e7 JL |
2704 | struct cifs_writedata *wdata = container_of(work, |
2705 | struct cifs_writedata, work); | |
2b0143b5 | 2706 | struct inode *inode = d_inode(wdata->cfile->dentry); |
da82f7e7 JL |
2707 | struct cifsInodeInfo *cifsi = CIFS_I(inode); |
2708 | ||
2709 | spin_lock(&inode->i_lock); | |
2710 | cifs_update_eof(cifsi, wdata->offset, wdata->bytes); | |
2711 | if (cifsi->server_eof > inode->i_size) | |
2712 | i_size_write(inode, cifsi->server_eof); | |
2713 | spin_unlock(&inode->i_lock); | |
2714 | ||
2715 | complete(&wdata->done); | |
c610c4b6 PS |
2716 | collect_uncached_write_data(wdata->ctx); |
2717 | /* the below call can possibly free the last ref to aio ctx */ | |
4a5c80d7 | 2718 | kref_put(&wdata->refcount, cifs_uncached_writedata_release); |
da82f7e7 JL |
2719 | } |
2720 | ||
da82f7e7 | 2721 | static int |
66386c08 PS |
2722 | wdata_fill_from_iovec(struct cifs_writedata *wdata, struct iov_iter *from, |
2723 | size_t *len, unsigned long *num_pages) | |
da82f7e7 | 2724 | { |
66386c08 PS |
2725 | size_t save_len, copied, bytes, cur_len = *len; |
2726 | unsigned long i, nr_pages = *num_pages; | |
c9de5c80 | 2727 | |
66386c08 PS |
2728 | save_len = cur_len; |
2729 | for (i = 0; i < nr_pages; i++) { | |
2730 | bytes = min_t(const size_t, cur_len, PAGE_SIZE); | |
2731 | copied = copy_page_from_iter(wdata->pages[i], 0, bytes, from); | |
2732 | cur_len -= copied; | |
2733 | /* | |
2734 | * If we didn't copy as much as we expected, then that | |
2735 | * may mean we trod into an unmapped area. Stop copying | |
2736 | * at that point. On the next pass through the big | |
2737 | * loop, we'll likely end up getting a zero-length | |
2738 | * write and bailing out of it. | |
2739 | */ | |
2740 | if (copied < bytes) | |
2741 | break; | |
2742 | } | |
2743 | cur_len = save_len - cur_len; | |
2744 | *len = cur_len; | |
da82f7e7 | 2745 | |
66386c08 PS |
2746 | /* |
2747 | * If we have no data to send, then that probably means that | |
2748 | * the copy above failed altogether. That's most likely because | |
2749 | * the address in the iovec was bogus. Return -EFAULT and let | |
2750 | * the caller free anything we allocated and bail out. | |
2751 | */ | |
2752 | if (!cur_len) | |
2753 | return -EFAULT; | |
da82f7e7 | 2754 | |
66386c08 PS |
2755 | /* |
2756 | * i + 1 now represents the number of pages we actually used in | |
2757 | * the copy phase above. | |
2758 | */ | |
2759 | *num_pages = i + 1; | |
2760 | return 0; | |
da82f7e7 JL |
2761 | } |
2762 | ||
8c5f9c1a LL |
2763 | static int |
2764 | cifs_resend_wdata(struct cifs_writedata *wdata, struct list_head *wdata_list, | |
2765 | struct cifs_aio_ctx *ctx) | |
2766 | { | |
335b7b62 PS |
2767 | unsigned int wsize; |
2768 | struct cifs_credits credits; | |
8c5f9c1a LL |
2769 | int rc; |
2770 | struct TCP_Server_Info *server = | |
2771 | tlink_tcon(wdata->cfile->tlink)->ses->server; | |
2772 | ||
8c5f9c1a | 2773 | do { |
d53e292f LL |
2774 | if (wdata->cfile->invalidHandle) { |
2775 | rc = cifs_reopen_file(wdata->cfile, false); | |
2776 | if (rc == -EAGAIN) | |
2777 | continue; | |
2778 | else if (rc) | |
2779 | break; | |
2780 | } | |
8c5f9c1a | 2781 | |
8c5f9c1a | 2782 | |
d53e292f LL |
2783 | /* |
2784 | * Wait for credits to resend this wdata. | |
2785 | * Note: we are attempting to resend the whole wdata not in | |
2786 | * segments | |
2787 | */ | |
2788 | do { | |
2789 | rc = server->ops->wait_mtu_credits(server, wdata->bytes, | |
2790 | &wsize, &credits); | |
2791 | if (rc) | |
2792 | goto fail; | |
2793 | ||
2794 | if (wsize < wdata->bytes) { | |
2795 | add_credits_and_wake_if(server, &credits, 0); | |
2796 | msleep(1000); | |
2797 | } | |
2798 | } while (wsize < wdata->bytes); | |
2799 | wdata->credits = credits; | |
8c5f9c1a | 2800 | |
d53e292f LL |
2801 | rc = adjust_credits(server, &wdata->credits, wdata->bytes); |
2802 | ||
2803 | if (!rc) { | |
2804 | if (wdata->cfile->invalidHandle) | |
2805 | rc = -EAGAIN; | |
2806 | else | |
2807 | rc = server->ops->async_writev(wdata, | |
8c5f9c1a | 2808 | cifs_uncached_writedata_release); |
d53e292f | 2809 | } |
8c5f9c1a | 2810 | |
d53e292f LL |
2811 | /* If the write was successfully sent, we are done */ |
2812 | if (!rc) { | |
2813 | list_add_tail(&wdata->list, wdata_list); | |
2814 | return 0; | |
2815 | } | |
8c5f9c1a | 2816 | |
d53e292f LL |
2817 | /* Roll back credits and retry if needed */ |
2818 | add_credits_and_wake_if(server, &wdata->credits, 0); | |
2819 | } while (rc == -EAGAIN); | |
8c5f9c1a | 2820 | |
d53e292f LL |
2821 | fail: |
2822 | kref_put(&wdata->refcount, cifs_uncached_writedata_release); | |
8c5f9c1a LL |
2823 | return rc; |
2824 | } | |
2825 | ||
43de94ea PS |
2826 | static int |
2827 | cifs_write_from_iter(loff_t offset, size_t len, struct iov_iter *from, | |
2828 | struct cifsFileInfo *open_file, | |
c610c4b6 PS |
2829 | struct cifs_sb_info *cifs_sb, struct list_head *wdata_list, |
2830 | struct cifs_aio_ctx *ctx) | |
72432ffc | 2831 | { |
43de94ea PS |
2832 | int rc = 0; |
2833 | size_t cur_len; | |
66386c08 | 2834 | unsigned long nr_pages, num_pages, i; |
43de94ea | 2835 | struct cifs_writedata *wdata; |
fc56b983 | 2836 | struct iov_iter saved_from = *from; |
6ec0b01b | 2837 | loff_t saved_offset = offset; |
da82f7e7 | 2838 | pid_t pid; |
6ec0b01b | 2839 | struct TCP_Server_Info *server; |
8c5f9c1a LL |
2840 | struct page **pagevec; |
2841 | size_t start; | |
335b7b62 | 2842 | unsigned int xid; |
d4ffff1f PS |
2843 | |
2844 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) | |
2845 | pid = open_file->pid; | |
2846 | else | |
2847 | pid = current->tgid; | |
2848 | ||
6ec0b01b | 2849 | server = tlink_tcon(open_file->tlink)->ses->server; |
335b7b62 | 2850 | xid = get_xid(); |
6ec0b01b | 2851 | |
72432ffc | 2852 | do { |
335b7b62 PS |
2853 | unsigned int wsize; |
2854 | struct cifs_credits credits_on_stack; | |
2855 | struct cifs_credits *credits = &credits_on_stack; | |
cb7e9eab | 2856 | |
3e952994 PS |
2857 | if (open_file->invalidHandle) { |
2858 | rc = cifs_reopen_file(open_file, false); | |
2859 | if (rc == -EAGAIN) | |
2860 | continue; | |
2861 | else if (rc) | |
2862 | break; | |
2863 | } | |
2864 | ||
cb7e9eab | 2865 | rc = server->ops->wait_mtu_credits(server, cifs_sb->wsize, |
335b7b62 | 2866 | &wsize, credits); |
cb7e9eab PS |
2867 | if (rc) |
2868 | break; | |
da82f7e7 | 2869 | |
b6bc8a7b LL |
2870 | cur_len = min_t(const size_t, len, wsize); |
2871 | ||
8c5f9c1a | 2872 | if (ctx->direct_io) { |
b98e26df SF |
2873 | ssize_t result; |
2874 | ||
2875 | result = iov_iter_get_pages_alloc( | |
b6bc8a7b | 2876 | from, &pagevec, cur_len, &start); |
b98e26df | 2877 | if (result < 0) { |
8c5f9c1a LL |
2878 | cifs_dbg(VFS, |
2879 | "direct_writev couldn't get user pages " | |
2880 | "(rc=%zd) iter type %d iov_offset %zd " | |
2881 | "count %zd\n", | |
b98e26df | 2882 | result, from->type, |
8c5f9c1a LL |
2883 | from->iov_offset, from->count); |
2884 | dump_stack(); | |
54e94ff9 LL |
2885 | |
2886 | rc = result; | |
2887 | add_credits_and_wake_if(server, credits, 0); | |
8c5f9c1a LL |
2888 | break; |
2889 | } | |
b98e26df | 2890 | cur_len = (size_t)result; |
8c5f9c1a LL |
2891 | iov_iter_advance(from, cur_len); |
2892 | ||
2893 | nr_pages = | |
2894 | (cur_len + start + PAGE_SIZE - 1) / PAGE_SIZE; | |
2895 | ||
2896 | wdata = cifs_writedata_direct_alloc(pagevec, | |
da82f7e7 | 2897 | cifs_uncached_writev_complete); |
8c5f9c1a LL |
2898 | if (!wdata) { |
2899 | rc = -ENOMEM; | |
2900 | add_credits_and_wake_if(server, credits, 0); | |
2901 | break; | |
2902 | } | |
da82f7e7 | 2903 | |
da82f7e7 | 2904 | |
8c5f9c1a LL |
2905 | wdata->page_offset = start; |
2906 | wdata->tailsz = | |
2907 | nr_pages > 1 ? | |
2908 | cur_len - (PAGE_SIZE - start) - | |
2909 | (nr_pages - 2) * PAGE_SIZE : | |
2910 | cur_len; | |
2911 | } else { | |
2912 | nr_pages = get_numpages(wsize, len, &cur_len); | |
2913 | wdata = cifs_writedata_alloc(nr_pages, | |
2914 | cifs_uncached_writev_complete); | |
2915 | if (!wdata) { | |
2916 | rc = -ENOMEM; | |
2917 | add_credits_and_wake_if(server, credits, 0); | |
2918 | break; | |
2919 | } | |
5d81de8e | 2920 | |
8c5f9c1a LL |
2921 | rc = cifs_write_allocate_pages(wdata->pages, nr_pages); |
2922 | if (rc) { | |
9bda8723 | 2923 | kvfree(wdata->pages); |
8c5f9c1a LL |
2924 | kfree(wdata); |
2925 | add_credits_and_wake_if(server, credits, 0); | |
2926 | break; | |
2927 | } | |
2928 | ||
2929 | num_pages = nr_pages; | |
2930 | rc = wdata_fill_from_iovec( | |
2931 | wdata, from, &cur_len, &num_pages); | |
2932 | if (rc) { | |
2933 | for (i = 0; i < nr_pages; i++) | |
2934 | put_page(wdata->pages[i]); | |
9bda8723 | 2935 | kvfree(wdata->pages); |
8c5f9c1a LL |
2936 | kfree(wdata); |
2937 | add_credits_and_wake_if(server, credits, 0); | |
2938 | break; | |
2939 | } | |
2940 | ||
2941 | /* | |
2942 | * Bring nr_pages down to the number of pages we | |
2943 | * actually used, and free any pages that we didn't use. | |
2944 | */ | |
2945 | for ( ; nr_pages > num_pages; nr_pages--) | |
2946 | put_page(wdata->pages[nr_pages - 1]); | |
2947 | ||
2948 | wdata->tailsz = cur_len - ((nr_pages - 1) * PAGE_SIZE); | |
2949 | } | |
5d81de8e | 2950 | |
da82f7e7 JL |
2951 | wdata->sync_mode = WB_SYNC_ALL; |
2952 | wdata->nr_pages = nr_pages; | |
2953 | wdata->offset = (__u64)offset; | |
2954 | wdata->cfile = cifsFileInfo_get(open_file); | |
2955 | wdata->pid = pid; | |
2956 | wdata->bytes = cur_len; | |
eddb079d | 2957 | wdata->pagesz = PAGE_SIZE; |
335b7b62 | 2958 | wdata->credits = credits_on_stack; |
c610c4b6 PS |
2959 | wdata->ctx = ctx; |
2960 | kref_get(&ctx->refcount); | |
6ec0b01b | 2961 | |
9a1c67e8 PS |
2962 | rc = adjust_credits(server, &wdata->credits, wdata->bytes); |
2963 | ||
2964 | if (!rc) { | |
2965 | if (wdata->cfile->invalidHandle) | |
3e952994 PS |
2966 | rc = -EAGAIN; |
2967 | else | |
9a1c67e8 | 2968 | rc = server->ops->async_writev(wdata, |
6ec0b01b | 2969 | cifs_uncached_writedata_release); |
9a1c67e8 PS |
2970 | } |
2971 | ||
da82f7e7 | 2972 | if (rc) { |
335b7b62 | 2973 | add_credits_and_wake_if(server, &wdata->credits, 0); |
4a5c80d7 SF |
2974 | kref_put(&wdata->refcount, |
2975 | cifs_uncached_writedata_release); | |
6ec0b01b | 2976 | if (rc == -EAGAIN) { |
fc56b983 | 2977 | *from = saved_from; |
6ec0b01b PS |
2978 | iov_iter_advance(from, offset - saved_offset); |
2979 | continue; | |
2980 | } | |
72432ffc PS |
2981 | break; |
2982 | } | |
2983 | ||
43de94ea | 2984 | list_add_tail(&wdata->list, wdata_list); |
da82f7e7 JL |
2985 | offset += cur_len; |
2986 | len -= cur_len; | |
72432ffc PS |
2987 | } while (len > 0); |
2988 | ||
335b7b62 | 2989 | free_xid(xid); |
43de94ea PS |
2990 | return rc; |
2991 | } | |
2992 | ||
c610c4b6 | 2993 | static void collect_uncached_write_data(struct cifs_aio_ctx *ctx) |
43de94ea | 2994 | { |
c610c4b6 | 2995 | struct cifs_writedata *wdata, *tmp; |
43de94ea PS |
2996 | struct cifs_tcon *tcon; |
2997 | struct cifs_sb_info *cifs_sb; | |
c610c4b6 | 2998 | struct dentry *dentry = ctx->cfile->dentry; |
43de94ea PS |
2999 | int rc; |
3000 | ||
c610c4b6 PS |
3001 | tcon = tlink_tcon(ctx->cfile->tlink); |
3002 | cifs_sb = CIFS_SB(dentry->d_sb); | |
43de94ea | 3003 | |
c610c4b6 | 3004 | mutex_lock(&ctx->aio_mutex); |
43de94ea | 3005 | |
c610c4b6 PS |
3006 | if (list_empty(&ctx->list)) { |
3007 | mutex_unlock(&ctx->aio_mutex); | |
3008 | return; | |
3009 | } | |
da82f7e7 | 3010 | |
c610c4b6 | 3011 | rc = ctx->rc; |
da82f7e7 JL |
3012 | /* |
3013 | * Wait for and collect replies for any successful sends in order of | |
c610c4b6 PS |
3014 | * increasing offset. Once an error is hit, then return without waiting |
3015 | * for any more replies. | |
da82f7e7 JL |
3016 | */ |
3017 | restart_loop: | |
c610c4b6 | 3018 | list_for_each_entry_safe(wdata, tmp, &ctx->list, list) { |
da82f7e7 | 3019 | if (!rc) { |
c610c4b6 PS |
3020 | if (!try_wait_for_completion(&wdata->done)) { |
3021 | mutex_unlock(&ctx->aio_mutex); | |
3022 | return; | |
3023 | } | |
3024 | ||
3025 | if (wdata->result) | |
da82f7e7 JL |
3026 | rc = wdata->result; |
3027 | else | |
c610c4b6 | 3028 | ctx->total_len += wdata->bytes; |
da82f7e7 JL |
3029 | |
3030 | /* resend call if it's a retryable error */ | |
3031 | if (rc == -EAGAIN) { | |
6ec0b01b | 3032 | struct list_head tmp_list; |
c610c4b6 | 3033 | struct iov_iter tmp_from = ctx->iter; |
6ec0b01b PS |
3034 | |
3035 | INIT_LIST_HEAD(&tmp_list); | |
3036 | list_del_init(&wdata->list); | |
3037 | ||
8c5f9c1a LL |
3038 | if (ctx->direct_io) |
3039 | rc = cifs_resend_wdata( | |
3040 | wdata, &tmp_list, ctx); | |
3041 | else { | |
3042 | iov_iter_advance(&tmp_from, | |
c610c4b6 | 3043 | wdata->offset - ctx->pos); |
6ec0b01b | 3044 | |
8c5f9c1a | 3045 | rc = cifs_write_from_iter(wdata->offset, |
6ec0b01b | 3046 | wdata->bytes, &tmp_from, |
c610c4b6 PS |
3047 | ctx->cfile, cifs_sb, &tmp_list, |
3048 | ctx); | |
d53e292f LL |
3049 | |
3050 | kref_put(&wdata->refcount, | |
3051 | cifs_uncached_writedata_release); | |
8c5f9c1a | 3052 | } |
6ec0b01b | 3053 | |
c610c4b6 | 3054 | list_splice(&tmp_list, &ctx->list); |
da82f7e7 JL |
3055 | goto restart_loop; |
3056 | } | |
3057 | } | |
3058 | list_del_init(&wdata->list); | |
4a5c80d7 | 3059 | kref_put(&wdata->refcount, cifs_uncached_writedata_release); |
72432ffc PS |
3060 | } |
3061 | ||
c610c4b6 PS |
3062 | cifs_stats_bytes_written(tcon, ctx->total_len); |
3063 | set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(dentry->d_inode)->flags); | |
3064 | ||
3065 | ctx->rc = (rc == 0) ? ctx->total_len : rc; | |
3066 | ||
3067 | mutex_unlock(&ctx->aio_mutex); | |
3068 | ||
3069 | if (ctx->iocb && ctx->iocb->ki_complete) | |
3070 | ctx->iocb->ki_complete(ctx->iocb, ctx->rc, 0); | |
3071 | else | |
3072 | complete(&ctx->done); | |
3073 | } | |
3074 | ||
8c5f9c1a LL |
3075 | static ssize_t __cifs_writev( |
3076 | struct kiocb *iocb, struct iov_iter *from, bool direct) | |
c610c4b6 PS |
3077 | { |
3078 | struct file *file = iocb->ki_filp; | |
3079 | ssize_t total_written = 0; | |
3080 | struct cifsFileInfo *cfile; | |
3081 | struct cifs_tcon *tcon; | |
3082 | struct cifs_sb_info *cifs_sb; | |
3083 | struct cifs_aio_ctx *ctx; | |
3084 | struct iov_iter saved_from = *from; | |
8c5f9c1a | 3085 | size_t len = iov_iter_count(from); |
c610c4b6 PS |
3086 | int rc; |
3087 | ||
3088 | /* | |
8c5f9c1a LL |
3089 | * iov_iter_get_pages_alloc doesn't work with ITER_KVEC. |
3090 | * In this case, fall back to non-direct write function. | |
3091 | * this could be improved by getting pages directly in ITER_KVEC | |
c610c4b6 | 3092 | */ |
8c5f9c1a LL |
3093 | if (direct && from->type & ITER_KVEC) { |
3094 | cifs_dbg(FYI, "use non-direct cifs_writev for kvec I/O\n"); | |
3095 | direct = false; | |
3096 | } | |
c610c4b6 PS |
3097 | |
3098 | rc = generic_write_checks(iocb, from); | |
3099 | if (rc <= 0) | |
3100 | return rc; | |
3101 | ||
3102 | cifs_sb = CIFS_FILE_SB(file); | |
3103 | cfile = file->private_data; | |
3104 | tcon = tlink_tcon(cfile->tlink); | |
3105 | ||
3106 | if (!tcon->ses->server->ops->async_writev) | |
3107 | return -ENOSYS; | |
3108 | ||
3109 | ctx = cifs_aio_ctx_alloc(); | |
3110 | if (!ctx) | |
3111 | return -ENOMEM; | |
3112 | ||
3113 | ctx->cfile = cifsFileInfo_get(cfile); | |
3114 | ||
3115 | if (!is_sync_kiocb(iocb)) | |
3116 | ctx->iocb = iocb; | |
3117 | ||
3118 | ctx->pos = iocb->ki_pos; | |
3119 | ||
8c5f9c1a LL |
3120 | if (direct) { |
3121 | ctx->direct_io = true; | |
3122 | ctx->iter = *from; | |
3123 | ctx->len = len; | |
3124 | } else { | |
3125 | rc = setup_aio_ctx_iter(ctx, from, WRITE); | |
3126 | if (rc) { | |
3127 | kref_put(&ctx->refcount, cifs_aio_ctx_release); | |
3128 | return rc; | |
3129 | } | |
c610c4b6 PS |
3130 | } |
3131 | ||
3132 | /* grab a lock here due to read response handlers can access ctx */ | |
3133 | mutex_lock(&ctx->aio_mutex); | |
3134 | ||
3135 | rc = cifs_write_from_iter(iocb->ki_pos, ctx->len, &saved_from, | |
3136 | cfile, cifs_sb, &ctx->list, ctx); | |
3137 | ||
3138 | /* | |
3139 | * If at least one write was successfully sent, then discard any rc | |
3140 | * value from the later writes. If the other write succeeds, then | |
3141 | * we'll end up returning whatever was written. If it fails, then | |
3142 | * we'll get a new rc value from that. | |
3143 | */ | |
3144 | if (!list_empty(&ctx->list)) | |
3145 | rc = 0; | |
3146 | ||
3147 | mutex_unlock(&ctx->aio_mutex); | |
3148 | ||
3149 | if (rc) { | |
3150 | kref_put(&ctx->refcount, cifs_aio_ctx_release); | |
3151 | return rc; | |
3152 | } | |
3153 | ||
3154 | if (!is_sync_kiocb(iocb)) { | |
3155 | kref_put(&ctx->refcount, cifs_aio_ctx_release); | |
3156 | return -EIOCBQUEUED; | |
3157 | } | |
3158 | ||
3159 | rc = wait_for_completion_killable(&ctx->done); | |
3160 | if (rc) { | |
3161 | mutex_lock(&ctx->aio_mutex); | |
3162 | ctx->rc = rc = -EINTR; | |
3163 | total_written = ctx->total_len; | |
3164 | mutex_unlock(&ctx->aio_mutex); | |
3165 | } else { | |
3166 | rc = ctx->rc; | |
3167 | total_written = ctx->total_len; | |
3168 | } | |
3169 | ||
3170 | kref_put(&ctx->refcount, cifs_aio_ctx_release); | |
3171 | ||
e9d1593d AV |
3172 | if (unlikely(!total_written)) |
3173 | return rc; | |
72432ffc | 3174 | |
e9d1593d | 3175 | iocb->ki_pos += total_written; |
e9d1593d | 3176 | return total_written; |
72432ffc PS |
3177 | } |
3178 | ||
8c5f9c1a LL |
3179 | ssize_t cifs_direct_writev(struct kiocb *iocb, struct iov_iter *from) |
3180 | { | |
3181 | return __cifs_writev(iocb, from, true); | |
3182 | } | |
3183 | ||
3184 | ssize_t cifs_user_writev(struct kiocb *iocb, struct iov_iter *from) | |
3185 | { | |
3186 | return __cifs_writev(iocb, from, false); | |
3187 | } | |
3188 | ||
579f9053 | 3189 | static ssize_t |
3dae8750 | 3190 | cifs_writev(struct kiocb *iocb, struct iov_iter *from) |
72432ffc | 3191 | { |
579f9053 PS |
3192 | struct file *file = iocb->ki_filp; |
3193 | struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data; | |
3194 | struct inode *inode = file->f_mapping->host; | |
3195 | struct cifsInodeInfo *cinode = CIFS_I(inode); | |
3196 | struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server; | |
5f380c7f | 3197 | ssize_t rc; |
72432ffc | 3198 | |
966681c9 | 3199 | inode_lock(inode); |
579f9053 PS |
3200 | /* |
3201 | * We need to hold the sem to be sure nobody modifies lock list | |
3202 | * with a brlock that prevents writing. | |
3203 | */ | |
3204 | down_read(&cinode->lock_sem); | |
5f380c7f | 3205 | |
3309dd04 AV |
3206 | rc = generic_write_checks(iocb, from); |
3207 | if (rc <= 0) | |
5f380c7f AV |
3208 | goto out; |
3209 | ||
5f380c7f | 3210 | if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(from), |
9645759c RS |
3211 | server->vals->exclusive_lock_type, 0, |
3212 | NULL, CIFS_WRITE_OP)) | |
3dae8750 | 3213 | rc = __generic_file_write_iter(iocb, from); |
5f380c7f AV |
3214 | else |
3215 | rc = -EACCES; | |
3216 | out: | |
966681c9 | 3217 | up_read(&cinode->lock_sem); |
5955102c | 3218 | inode_unlock(inode); |
19dfc1f5 | 3219 | |
e2592217 CH |
3220 | if (rc > 0) |
3221 | rc = generic_write_sync(iocb, rc); | |
579f9053 PS |
3222 | return rc; |
3223 | } | |
3224 | ||
3225 | ssize_t | |
3dae8750 | 3226 | cifs_strict_writev(struct kiocb *iocb, struct iov_iter *from) |
579f9053 | 3227 | { |
496ad9aa | 3228 | struct inode *inode = file_inode(iocb->ki_filp); |
579f9053 PS |
3229 | struct cifsInodeInfo *cinode = CIFS_I(inode); |
3230 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); | |
3231 | struct cifsFileInfo *cfile = (struct cifsFileInfo *) | |
3232 | iocb->ki_filp->private_data; | |
3233 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); | |
88cf75aa | 3234 | ssize_t written; |
ca8aa29c | 3235 | |
c11f1df5 SP |
3236 | written = cifs_get_writer(cinode); |
3237 | if (written) | |
3238 | return written; | |
3239 | ||
18cceb6a | 3240 | if (CIFS_CACHE_WRITE(cinode)) { |
88cf75aa PS |
3241 | if (cap_unix(tcon->ses) && |
3242 | (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) | |
c11f1df5 | 3243 | && ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) { |
3dae8750 | 3244 | written = generic_file_write_iter(iocb, from); |
c11f1df5 SP |
3245 | goto out; |
3246 | } | |
3dae8750 | 3247 | written = cifs_writev(iocb, from); |
c11f1df5 | 3248 | goto out; |
25078105 | 3249 | } |
25078105 | 3250 | /* |
ca8aa29c PS |
3251 | * For non-oplocked files in strict cache mode we need to write the data |
3252 | * to the server exactly from the pos to pos+len-1 rather than flush all | |
3253 | * affected pages because it may cause a error with mandatory locks on | |
3254 | * these pages but not on the region from pos to ppos+len-1. | |
72432ffc | 3255 | */ |
3dae8750 | 3256 | written = cifs_user_writev(iocb, from); |
6dfbd846 | 3257 | if (CIFS_CACHE_READ(cinode)) { |
88cf75aa | 3258 | /* |
6dfbd846 PS |
3259 | * We have read level caching and we have just sent a write |
3260 | * request to the server thus making data in the cache stale. | |
3261 | * Zap the cache and set oplock/lease level to NONE to avoid | |
3262 | * reading stale data from the cache. All subsequent read | |
3263 | * operations will read new data from the server. | |
88cf75aa | 3264 | */ |
4f73c7d3 | 3265 | cifs_zap_mapping(inode); |
6dfbd846 | 3266 | cifs_dbg(FYI, "Set Oplock/Lease to NONE for inode=%p after write\n", |
f96637be | 3267 | inode); |
18cceb6a | 3268 | cinode->oplock = 0; |
88cf75aa | 3269 | } |
c11f1df5 SP |
3270 | out: |
3271 | cifs_put_writer(cinode); | |
88cf75aa | 3272 | return written; |
72432ffc PS |
3273 | } |
3274 | ||
0471ca3f | 3275 | static struct cifs_readdata * |
f9f5aca1 | 3276 | cifs_readdata_direct_alloc(struct page **pages, work_func_t complete) |
0471ca3f JL |
3277 | { |
3278 | struct cifs_readdata *rdata; | |
f4e49cd2 | 3279 | |
f9f5aca1 | 3280 | rdata = kzalloc(sizeof(*rdata), GFP_KERNEL); |
0471ca3f | 3281 | if (rdata != NULL) { |
f9f5aca1 | 3282 | rdata->pages = pages; |
6993f74a | 3283 | kref_init(&rdata->refcount); |
1c892549 JL |
3284 | INIT_LIST_HEAD(&rdata->list); |
3285 | init_completion(&rdata->done); | |
0471ca3f | 3286 | INIT_WORK(&rdata->work, complete); |
0471ca3f | 3287 | } |
f4e49cd2 | 3288 | |
0471ca3f JL |
3289 | return rdata; |
3290 | } | |
3291 | ||
f9f5aca1 LL |
3292 | static struct cifs_readdata * |
3293 | cifs_readdata_alloc(unsigned int nr_pages, work_func_t complete) | |
3294 | { | |
3295 | struct page **pages = | |
6396bb22 | 3296 | kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL); |
f9f5aca1 LL |
3297 | struct cifs_readdata *ret = NULL; |
3298 | ||
3299 | if (pages) { | |
3300 | ret = cifs_readdata_direct_alloc(pages, complete); | |
3301 | if (!ret) | |
3302 | kfree(pages); | |
3303 | } | |
3304 | ||
3305 | return ret; | |
3306 | } | |
3307 | ||
6993f74a JL |
3308 | void |
3309 | cifs_readdata_release(struct kref *refcount) | |
0471ca3f | 3310 | { |
6993f74a JL |
3311 | struct cifs_readdata *rdata = container_of(refcount, |
3312 | struct cifs_readdata, refcount); | |
bd3dcc6a LL |
3313 | #ifdef CONFIG_CIFS_SMB_DIRECT |
3314 | if (rdata->mr) { | |
3315 | smbd_deregister_mr(rdata->mr); | |
3316 | rdata->mr = NULL; | |
3317 | } | |
3318 | #endif | |
6993f74a JL |
3319 | if (rdata->cfile) |
3320 | cifsFileInfo_put(rdata->cfile); | |
3321 | ||
f9f5aca1 | 3322 | kvfree(rdata->pages); |
0471ca3f JL |
3323 | kfree(rdata); |
3324 | } | |
3325 | ||
1c892549 | 3326 | static int |
c5fab6f4 | 3327 | cifs_read_allocate_pages(struct cifs_readdata *rdata, unsigned int nr_pages) |
1c892549 JL |
3328 | { |
3329 | int rc = 0; | |
c5fab6f4 | 3330 | struct page *page; |
1c892549 JL |
3331 | unsigned int i; |
3332 | ||
c5fab6f4 | 3333 | for (i = 0; i < nr_pages; i++) { |
1c892549 JL |
3334 | page = alloc_page(GFP_KERNEL|__GFP_HIGHMEM); |
3335 | if (!page) { | |
3336 | rc = -ENOMEM; | |
3337 | break; | |
3338 | } | |
c5fab6f4 | 3339 | rdata->pages[i] = page; |
1c892549 JL |
3340 | } |
3341 | ||
3342 | if (rc) { | |
31fad7d4 RBC |
3343 | unsigned int nr_page_failed = i; |
3344 | ||
3345 | for (i = 0; i < nr_page_failed; i++) { | |
c5fab6f4 JL |
3346 | put_page(rdata->pages[i]); |
3347 | rdata->pages[i] = NULL; | |
1c892549 JL |
3348 | } |
3349 | } | |
3350 | return rc; | |
3351 | } | |
3352 | ||
3353 | static void | |
3354 | cifs_uncached_readdata_release(struct kref *refcount) | |
3355 | { | |
1c892549 JL |
3356 | struct cifs_readdata *rdata = container_of(refcount, |
3357 | struct cifs_readdata, refcount); | |
c5fab6f4 | 3358 | unsigned int i; |
1c892549 | 3359 | |
6685c5e2 | 3360 | kref_put(&rdata->ctx->refcount, cifs_aio_ctx_release); |
c5fab6f4 JL |
3361 | for (i = 0; i < rdata->nr_pages; i++) { |
3362 | put_page(rdata->pages[i]); | |
1c892549 JL |
3363 | } |
3364 | cifs_readdata_release(refcount); | |
3365 | } | |
3366 | ||
1c892549 JL |
3367 | /** |
3368 | * cifs_readdata_to_iov - copy data from pages in response to an iovec | |
3369 | * @rdata: the readdata response with list of pages holding data | |
7f25bba8 | 3370 | * @iter: destination for our data |
1c892549 JL |
3371 | * |
3372 | * This function copies data from a list of pages in a readdata response into | |
3373 | * an array of iovecs. It will first calculate where the data should go | |
3374 | * based on the info in the readdata and then copy the data into that spot. | |
3375 | */ | |
7f25bba8 AV |
3376 | static int |
3377 | cifs_readdata_to_iov(struct cifs_readdata *rdata, struct iov_iter *iter) | |
1c892549 | 3378 | { |
34a54d61 | 3379 | size_t remaining = rdata->got_bytes; |
c5fab6f4 | 3380 | unsigned int i; |
1c892549 | 3381 | |
c5fab6f4 | 3382 | for (i = 0; i < rdata->nr_pages; i++) { |
c5fab6f4 | 3383 | struct page *page = rdata->pages[i]; |
e686bd8d | 3384 | size_t copy = min_t(size_t, remaining, PAGE_SIZE); |
9c25702c PS |
3385 | size_t written; |
3386 | ||
00e23707 | 3387 | if (unlikely(iov_iter_is_pipe(iter))) { |
9c25702c PS |
3388 | void *addr = kmap_atomic(page); |
3389 | ||
3390 | written = copy_to_iter(addr, copy, iter); | |
3391 | kunmap_atomic(addr); | |
3392 | } else | |
3393 | written = copy_page_to_iter(page, 0, copy, iter); | |
7f25bba8 AV |
3394 | remaining -= written; |
3395 | if (written < copy && iov_iter_count(iter) > 0) | |
3396 | break; | |
1c892549 | 3397 | } |
7f25bba8 | 3398 | return remaining ? -EFAULT : 0; |
1c892549 JL |
3399 | } |
3400 | ||
6685c5e2 PS |
3401 | static void collect_uncached_read_data(struct cifs_aio_ctx *ctx); |
3402 | ||
1c892549 JL |
3403 | static void |
3404 | cifs_uncached_readv_complete(struct work_struct *work) | |
3405 | { | |
3406 | struct cifs_readdata *rdata = container_of(work, | |
3407 | struct cifs_readdata, work); | |
1c892549 JL |
3408 | |
3409 | complete(&rdata->done); | |
6685c5e2 PS |
3410 | collect_uncached_read_data(rdata->ctx); |
3411 | /* the below call can possibly free the last ref to aio ctx */ | |
1c892549 JL |
3412 | kref_put(&rdata->refcount, cifs_uncached_readdata_release); |
3413 | } | |
3414 | ||
3415 | static int | |
d70b9104 PS |
3416 | uncached_fill_pages(struct TCP_Server_Info *server, |
3417 | struct cifs_readdata *rdata, struct iov_iter *iter, | |
3418 | unsigned int len) | |
1c892549 | 3419 | { |
b3160aeb | 3420 | int result = 0; |
c5fab6f4 JL |
3421 | unsigned int i; |
3422 | unsigned int nr_pages = rdata->nr_pages; | |
1dbe3466 | 3423 | unsigned int page_offset = rdata->page_offset; |
1c892549 | 3424 | |
b3160aeb | 3425 | rdata->got_bytes = 0; |
8321fec4 | 3426 | rdata->tailsz = PAGE_SIZE; |
c5fab6f4 JL |
3427 | for (i = 0; i < nr_pages; i++) { |
3428 | struct page *page = rdata->pages[i]; | |
71335664 | 3429 | size_t n; |
1dbe3466 LL |
3430 | unsigned int segment_size = rdata->pagesz; |
3431 | ||
3432 | if (i == 0) | |
3433 | segment_size -= page_offset; | |
3434 | else | |
3435 | page_offset = 0; | |
3436 | ||
c5fab6f4 | 3437 | |
71335664 | 3438 | if (len <= 0) { |
1c892549 | 3439 | /* no need to hold page hostage */ |
c5fab6f4 JL |
3440 | rdata->pages[i] = NULL; |
3441 | rdata->nr_pages--; | |
1c892549 | 3442 | put_page(page); |
8321fec4 | 3443 | continue; |
1c892549 | 3444 | } |
1dbe3466 | 3445 | |
71335664 | 3446 | n = len; |
1dbe3466 | 3447 | if (len >= segment_size) |
71335664 | 3448 | /* enough data to fill the page */ |
1dbe3466 LL |
3449 | n = segment_size; |
3450 | else | |
71335664 | 3451 | rdata->tailsz = len; |
1dbe3466 LL |
3452 | len -= n; |
3453 | ||
d70b9104 | 3454 | if (iter) |
1dbe3466 LL |
3455 | result = copy_page_from_iter( |
3456 | page, page_offset, n, iter); | |
bd3dcc6a LL |
3457 | #ifdef CONFIG_CIFS_SMB_DIRECT |
3458 | else if (rdata->mr) | |
3459 | result = n; | |
3460 | #endif | |
d70b9104 | 3461 | else |
1dbe3466 LL |
3462 | result = cifs_read_page_from_socket( |
3463 | server, page, page_offset, n); | |
8321fec4 JL |
3464 | if (result < 0) |
3465 | break; | |
3466 | ||
b3160aeb | 3467 | rdata->got_bytes += result; |
1c892549 JL |
3468 | } |
3469 | ||
b3160aeb PS |
3470 | return rdata->got_bytes > 0 && result != -ECONNABORTED ? |
3471 | rdata->got_bytes : result; | |
1c892549 JL |
3472 | } |
3473 | ||
d70b9104 PS |
3474 | static int |
3475 | cifs_uncached_read_into_pages(struct TCP_Server_Info *server, | |
3476 | struct cifs_readdata *rdata, unsigned int len) | |
3477 | { | |
3478 | return uncached_fill_pages(server, rdata, NULL, len); | |
3479 | } | |
3480 | ||
3481 | static int | |
3482 | cifs_uncached_copy_into_pages(struct TCP_Server_Info *server, | |
3483 | struct cifs_readdata *rdata, | |
3484 | struct iov_iter *iter) | |
3485 | { | |
3486 | return uncached_fill_pages(server, rdata, iter, iter->count); | |
3487 | } | |
3488 | ||
6e6e2b86 LL |
3489 | static int cifs_resend_rdata(struct cifs_readdata *rdata, |
3490 | struct list_head *rdata_list, | |
3491 | struct cifs_aio_ctx *ctx) | |
3492 | { | |
335b7b62 PS |
3493 | unsigned int rsize; |
3494 | struct cifs_credits credits; | |
6e6e2b86 LL |
3495 | int rc; |
3496 | struct TCP_Server_Info *server = | |
3497 | tlink_tcon(rdata->cfile->tlink)->ses->server; | |
3498 | ||
6e6e2b86 | 3499 | do { |
0b0dfd59 LL |
3500 | if (rdata->cfile->invalidHandle) { |
3501 | rc = cifs_reopen_file(rdata->cfile, true); | |
3502 | if (rc == -EAGAIN) | |
3503 | continue; | |
3504 | else if (rc) | |
3505 | break; | |
3506 | } | |
3507 | ||
3508 | /* | |
3509 | * Wait for credits to resend this rdata. | |
3510 | * Note: we are attempting to resend the whole rdata not in | |
3511 | * segments | |
3512 | */ | |
3513 | do { | |
3514 | rc = server->ops->wait_mtu_credits(server, rdata->bytes, | |
6e6e2b86 LL |
3515 | &rsize, &credits); |
3516 | ||
0b0dfd59 LL |
3517 | if (rc) |
3518 | goto fail; | |
6e6e2b86 | 3519 | |
0b0dfd59 LL |
3520 | if (rsize < rdata->bytes) { |
3521 | add_credits_and_wake_if(server, &credits, 0); | |
3522 | msleep(1000); | |
3523 | } | |
3524 | } while (rsize < rdata->bytes); | |
3525 | rdata->credits = credits; | |
6e6e2b86 | 3526 | |
0b0dfd59 LL |
3527 | rc = adjust_credits(server, &rdata->credits, rdata->bytes); |
3528 | if (!rc) { | |
3529 | if (rdata->cfile->invalidHandle) | |
3530 | rc = -EAGAIN; | |
3531 | else | |
3532 | rc = server->ops->async_readv(rdata); | |
3533 | } | |
6e6e2b86 | 3534 | |
0b0dfd59 LL |
3535 | /* If the read was successfully sent, we are done */ |
3536 | if (!rc) { | |
3537 | /* Add to aio pending list */ | |
3538 | list_add_tail(&rdata->list, rdata_list); | |
3539 | return 0; | |
3540 | } | |
6e6e2b86 | 3541 | |
0b0dfd59 LL |
3542 | /* Roll back credits and retry if needed */ |
3543 | add_credits_and_wake_if(server, &rdata->credits, 0); | |
3544 | } while (rc == -EAGAIN); | |
6e6e2b86 | 3545 | |
0b0dfd59 LL |
3546 | fail: |
3547 | kref_put(&rdata->refcount, cifs_uncached_readdata_release); | |
6e6e2b86 LL |
3548 | return rc; |
3549 | } | |
3550 | ||
0ada36b2 PS |
3551 | static int |
3552 | cifs_send_async_read(loff_t offset, size_t len, struct cifsFileInfo *open_file, | |
6685c5e2 PS |
3553 | struct cifs_sb_info *cifs_sb, struct list_head *rdata_list, |
3554 | struct cifs_aio_ctx *ctx) | |
1da177e4 | 3555 | { |
0ada36b2 | 3556 | struct cifs_readdata *rdata; |
335b7b62 PS |
3557 | unsigned int npages, rsize; |
3558 | struct cifs_credits credits_on_stack; | |
3559 | struct cifs_credits *credits = &credits_on_stack; | |
0ada36b2 PS |
3560 | size_t cur_len; |
3561 | int rc; | |
1c892549 | 3562 | pid_t pid; |
25f40259 | 3563 | struct TCP_Server_Info *server; |
6e6e2b86 LL |
3564 | struct page **pagevec; |
3565 | size_t start; | |
3566 | struct iov_iter direct_iov = ctx->iter; | |
a70307ee | 3567 | |
25f40259 | 3568 | server = tlink_tcon(open_file->tlink)->ses->server; |
fc9c5966 | 3569 | |
d4ffff1f PS |
3570 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) |
3571 | pid = open_file->pid; | |
3572 | else | |
3573 | pid = current->tgid; | |
3574 | ||
6e6e2b86 LL |
3575 | if (ctx->direct_io) |
3576 | iov_iter_advance(&direct_iov, offset - ctx->pos); | |
3577 | ||
1c892549 | 3578 | do { |
3e952994 PS |
3579 | if (open_file->invalidHandle) { |
3580 | rc = cifs_reopen_file(open_file, true); | |
3581 | if (rc == -EAGAIN) | |
3582 | continue; | |
3583 | else if (rc) | |
3584 | break; | |
3585 | } | |
3586 | ||
bed9da02 | 3587 | rc = server->ops->wait_mtu_credits(server, cifs_sb->rsize, |
335b7b62 | 3588 | &rsize, credits); |
bed9da02 PS |
3589 | if (rc) |
3590 | break; | |
3591 | ||
3592 | cur_len = min_t(const size_t, len, rsize); | |
a70307ee | 3593 | |
6e6e2b86 | 3594 | if (ctx->direct_io) { |
b98e26df | 3595 | ssize_t result; |
6e6e2b86 | 3596 | |
b98e26df | 3597 | result = iov_iter_get_pages_alloc( |
6e6e2b86 LL |
3598 | &direct_iov, &pagevec, |
3599 | cur_len, &start); | |
b98e26df | 3600 | if (result < 0) { |
6e6e2b86 | 3601 | cifs_dbg(VFS, |
54e94ff9 | 3602 | "couldn't get user pages (rc=%zd)" |
6e6e2b86 LL |
3603 | " iter type %d" |
3604 | " iov_offset %zd count %zd\n", | |
b98e26df | 3605 | result, direct_iov.type, |
6e6e2b86 LL |
3606 | direct_iov.iov_offset, |
3607 | direct_iov.count); | |
3608 | dump_stack(); | |
54e94ff9 LL |
3609 | |
3610 | rc = result; | |
3611 | add_credits_and_wake_if(server, credits, 0); | |
6e6e2b86 LL |
3612 | break; |
3613 | } | |
b98e26df | 3614 | cur_len = (size_t)result; |
6e6e2b86 LL |
3615 | iov_iter_advance(&direct_iov, cur_len); |
3616 | ||
3617 | rdata = cifs_readdata_direct_alloc( | |
3618 | pagevec, cifs_uncached_readv_complete); | |
3619 | if (!rdata) { | |
3620 | add_credits_and_wake_if(server, credits, 0); | |
3621 | rc = -ENOMEM; | |
3622 | break; | |
3623 | } | |
3624 | ||
3625 | npages = (cur_len + start + PAGE_SIZE-1) / PAGE_SIZE; | |
3626 | rdata->page_offset = start; | |
3627 | rdata->tailsz = npages > 1 ? | |
3628 | cur_len-(PAGE_SIZE-start)-(npages-2)*PAGE_SIZE : | |
3629 | cur_len; | |
3630 | ||
3631 | } else { | |
3632 | ||
3633 | npages = DIV_ROUND_UP(cur_len, PAGE_SIZE); | |
3634 | /* allocate a readdata struct */ | |
3635 | rdata = cifs_readdata_alloc(npages, | |
1c892549 | 3636 | cifs_uncached_readv_complete); |
6e6e2b86 LL |
3637 | if (!rdata) { |
3638 | add_credits_and_wake_if(server, credits, 0); | |
3639 | rc = -ENOMEM; | |
3640 | break; | |
3641 | } | |
a70307ee | 3642 | |
6e6e2b86 | 3643 | rc = cifs_read_allocate_pages(rdata, npages); |
9bda8723 PS |
3644 | if (rc) { |
3645 | kvfree(rdata->pages); | |
3646 | kfree(rdata); | |
3647 | add_credits_and_wake_if(server, credits, 0); | |
3648 | break; | |
3649 | } | |
6e6e2b86 LL |
3650 | |
3651 | rdata->tailsz = PAGE_SIZE; | |
3652 | } | |
1c892549 JL |
3653 | |
3654 | rdata->cfile = cifsFileInfo_get(open_file); | |
c5fab6f4 | 3655 | rdata->nr_pages = npages; |
1c892549 JL |
3656 | rdata->offset = offset; |
3657 | rdata->bytes = cur_len; | |
3658 | rdata->pid = pid; | |
8321fec4 JL |
3659 | rdata->pagesz = PAGE_SIZE; |
3660 | rdata->read_into_pages = cifs_uncached_read_into_pages; | |
d70b9104 | 3661 | rdata->copy_into_pages = cifs_uncached_copy_into_pages; |
335b7b62 | 3662 | rdata->credits = credits_on_stack; |
6685c5e2 PS |
3663 | rdata->ctx = ctx; |
3664 | kref_get(&ctx->refcount); | |
1c892549 | 3665 | |
9a1c67e8 PS |
3666 | rc = adjust_credits(server, &rdata->credits, rdata->bytes); |
3667 | ||
3668 | if (!rc) { | |
3669 | if (rdata->cfile->invalidHandle) | |
3e952994 PS |
3670 | rc = -EAGAIN; |
3671 | else | |
9a1c67e8 PS |
3672 | rc = server->ops->async_readv(rdata); |
3673 | } | |
3674 | ||
1c892549 | 3675 | if (rc) { |
335b7b62 | 3676 | add_credits_and_wake_if(server, &rdata->credits, 0); |
1c892549 | 3677 | kref_put(&rdata->refcount, |
6e6e2b86 LL |
3678 | cifs_uncached_readdata_release); |
3679 | if (rc == -EAGAIN) { | |
3680 | iov_iter_revert(&direct_iov, cur_len); | |
25f40259 | 3681 | continue; |
6e6e2b86 | 3682 | } |
1c892549 JL |
3683 | break; |
3684 | } | |
3685 | ||
0ada36b2 | 3686 | list_add_tail(&rdata->list, rdata_list); |
1c892549 JL |
3687 | offset += cur_len; |
3688 | len -= cur_len; | |
3689 | } while (len > 0); | |
3690 | ||
0ada36b2 PS |
3691 | return rc; |
3692 | } | |
3693 | ||
6685c5e2 PS |
3694 | static void |
3695 | collect_uncached_read_data(struct cifs_aio_ctx *ctx) | |
0ada36b2 | 3696 | { |
6685c5e2 PS |
3697 | struct cifs_readdata *rdata, *tmp; |
3698 | struct iov_iter *to = &ctx->iter; | |
0ada36b2 | 3699 | struct cifs_sb_info *cifs_sb; |
6685c5e2 | 3700 | int rc; |
0ada36b2 | 3701 | |
6685c5e2 | 3702 | cifs_sb = CIFS_SB(ctx->cfile->dentry->d_sb); |
0ada36b2 | 3703 | |
6685c5e2 | 3704 | mutex_lock(&ctx->aio_mutex); |
0ada36b2 | 3705 | |
6685c5e2 PS |
3706 | if (list_empty(&ctx->list)) { |
3707 | mutex_unlock(&ctx->aio_mutex); | |
3708 | return; | |
3709 | } | |
1c892549 | 3710 | |
6685c5e2 | 3711 | rc = ctx->rc; |
1c892549 | 3712 | /* the loop below should proceed in the order of increasing offsets */ |
25f40259 | 3713 | again: |
6685c5e2 | 3714 | list_for_each_entry_safe(rdata, tmp, &ctx->list, list) { |
1c892549 | 3715 | if (!rc) { |
6685c5e2 PS |
3716 | if (!try_wait_for_completion(&rdata->done)) { |
3717 | mutex_unlock(&ctx->aio_mutex); | |
3718 | return; | |
3719 | } | |
3720 | ||
3721 | if (rdata->result == -EAGAIN) { | |
74027f4a | 3722 | /* resend call if it's a retryable error */ |
fb8a3e52 | 3723 | struct list_head tmp_list; |
d913ed17 | 3724 | unsigned int got_bytes = rdata->got_bytes; |
25f40259 | 3725 | |
fb8a3e52 PS |
3726 | list_del_init(&rdata->list); |
3727 | INIT_LIST_HEAD(&tmp_list); | |
25f40259 | 3728 | |
d913ed17 PS |
3729 | /* |
3730 | * Got a part of data and then reconnect has | |
3731 | * happened -- fill the buffer and continue | |
3732 | * reading. | |
3733 | */ | |
3734 | if (got_bytes && got_bytes < rdata->bytes) { | |
6e6e2b86 LL |
3735 | rc = 0; |
3736 | if (!ctx->direct_io) | |
3737 | rc = cifs_readdata_to_iov(rdata, to); | |
d913ed17 PS |
3738 | if (rc) { |
3739 | kref_put(&rdata->refcount, | |
6e6e2b86 | 3740 | cifs_uncached_readdata_release); |
d913ed17 PS |
3741 | continue; |
3742 | } | |
74027f4a | 3743 | } |
d913ed17 | 3744 | |
6e6e2b86 LL |
3745 | if (ctx->direct_io) { |
3746 | /* | |
3747 | * Re-use rdata as this is a | |
3748 | * direct I/O | |
3749 | */ | |
3750 | rc = cifs_resend_rdata( | |
3751 | rdata, | |
3752 | &tmp_list, ctx); | |
3753 | } else { | |
3754 | rc = cifs_send_async_read( | |
d913ed17 PS |
3755 | rdata->offset + got_bytes, |
3756 | rdata->bytes - got_bytes, | |
3757 | rdata->cfile, cifs_sb, | |
6685c5e2 | 3758 | &tmp_list, ctx); |
25f40259 | 3759 | |
6e6e2b86 LL |
3760 | kref_put(&rdata->refcount, |
3761 | cifs_uncached_readdata_release); | |
3762 | } | |
3763 | ||
6685c5e2 | 3764 | list_splice(&tmp_list, &ctx->list); |
25f40259 | 3765 | |
fb8a3e52 PS |
3766 | goto again; |
3767 | } else if (rdata->result) | |
3768 | rc = rdata->result; | |
6e6e2b86 | 3769 | else if (!ctx->direct_io) |
e6a7bcb4 | 3770 | rc = cifs_readdata_to_iov(rdata, to); |
1c892549 | 3771 | |
2e8a05d8 PS |
3772 | /* if there was a short read -- discard anything left */ |
3773 | if (rdata->got_bytes && rdata->got_bytes < rdata->bytes) | |
3774 | rc = -ENODATA; | |
6e6e2b86 LL |
3775 | |
3776 | ctx->total_len += rdata->got_bytes; | |
1da177e4 | 3777 | } |
1c892549 JL |
3778 | list_del_init(&rdata->list); |
3779 | kref_put(&rdata->refcount, cifs_uncached_readdata_release); | |
1da177e4 | 3780 | } |
a70307ee | 3781 | |
13f5938d | 3782 | if (!ctx->direct_io) |
6e6e2b86 | 3783 | ctx->total_len = ctx->len - iov_iter_count(to); |
6685c5e2 | 3784 | |
09a4707e PS |
3785 | /* mask nodata case */ |
3786 | if (rc == -ENODATA) | |
3787 | rc = 0; | |
3788 | ||
6685c5e2 PS |
3789 | ctx->rc = (rc == 0) ? ctx->total_len : rc; |
3790 | ||
3791 | mutex_unlock(&ctx->aio_mutex); | |
3792 | ||
3793 | if (ctx->iocb && ctx->iocb->ki_complete) | |
3794 | ctx->iocb->ki_complete(ctx->iocb, ctx->rc, 0); | |
3795 | else | |
3796 | complete(&ctx->done); | |
3797 | } | |
3798 | ||
6e6e2b86 LL |
3799 | static ssize_t __cifs_readv( |
3800 | struct kiocb *iocb, struct iov_iter *to, bool direct) | |
6685c5e2 | 3801 | { |
6685c5e2 | 3802 | size_t len; |
6e6e2b86 | 3803 | struct file *file = iocb->ki_filp; |
6685c5e2 | 3804 | struct cifs_sb_info *cifs_sb; |
6685c5e2 | 3805 | struct cifsFileInfo *cfile; |
6e6e2b86 LL |
3806 | struct cifs_tcon *tcon; |
3807 | ssize_t rc, total_read = 0; | |
3808 | loff_t offset = iocb->ki_pos; | |
6685c5e2 PS |
3809 | struct cifs_aio_ctx *ctx; |
3810 | ||
6e6e2b86 LL |
3811 | /* |
3812 | * iov_iter_get_pages_alloc() doesn't work with ITER_KVEC, | |
3813 | * fall back to data copy read path | |
3814 | * this could be improved by getting pages directly in ITER_KVEC | |
3815 | */ | |
3816 | if (direct && to->type & ITER_KVEC) { | |
3817 | cifs_dbg(FYI, "use non-direct cifs_user_readv for kvec I/O\n"); | |
3818 | direct = false; | |
3819 | } | |
3820 | ||
6685c5e2 PS |
3821 | len = iov_iter_count(to); |
3822 | if (!len) | |
3823 | return 0; | |
3824 | ||
3825 | cifs_sb = CIFS_FILE_SB(file); | |
3826 | cfile = file->private_data; | |
3827 | tcon = tlink_tcon(cfile->tlink); | |
3828 | ||
3829 | if (!tcon->ses->server->ops->async_readv) | |
3830 | return -ENOSYS; | |
3831 | ||
3832 | if ((file->f_flags & O_ACCMODE) == O_WRONLY) | |
3833 | cifs_dbg(FYI, "attempting read on write only file instance\n"); | |
3834 | ||
3835 | ctx = cifs_aio_ctx_alloc(); | |
3836 | if (!ctx) | |
3837 | return -ENOMEM; | |
3838 | ||
3839 | ctx->cfile = cifsFileInfo_get(cfile); | |
3840 | ||
3841 | if (!is_sync_kiocb(iocb)) | |
3842 | ctx->iocb = iocb; | |
3843 | ||
00e23707 | 3844 | if (iter_is_iovec(to)) |
6685c5e2 PS |
3845 | ctx->should_dirty = true; |
3846 | ||
6e6e2b86 LL |
3847 | if (direct) { |
3848 | ctx->pos = offset; | |
3849 | ctx->direct_io = true; | |
3850 | ctx->iter = *to; | |
3851 | ctx->len = len; | |
3852 | } else { | |
3853 | rc = setup_aio_ctx_iter(ctx, to, READ); | |
3854 | if (rc) { | |
3855 | kref_put(&ctx->refcount, cifs_aio_ctx_release); | |
3856 | return rc; | |
3857 | } | |
3858 | len = ctx->len; | |
6685c5e2 PS |
3859 | } |
3860 | ||
6685c5e2 PS |
3861 | /* grab a lock here due to read response handlers can access ctx */ |
3862 | mutex_lock(&ctx->aio_mutex); | |
3863 | ||
3864 | rc = cifs_send_async_read(offset, len, cfile, cifs_sb, &ctx->list, ctx); | |
3865 | ||
3866 | /* if at least one read request send succeeded, then reset rc */ | |
3867 | if (!list_empty(&ctx->list)) | |
3868 | rc = 0; | |
3869 | ||
3870 | mutex_unlock(&ctx->aio_mutex); | |
3871 | ||
3872 | if (rc) { | |
3873 | kref_put(&ctx->refcount, cifs_aio_ctx_release); | |
3874 | return rc; | |
3875 | } | |
3876 | ||
3877 | if (!is_sync_kiocb(iocb)) { | |
3878 | kref_put(&ctx->refcount, cifs_aio_ctx_release); | |
3879 | return -EIOCBQUEUED; | |
3880 | } | |
3881 | ||
3882 | rc = wait_for_completion_killable(&ctx->done); | |
3883 | if (rc) { | |
3884 | mutex_lock(&ctx->aio_mutex); | |
3885 | ctx->rc = rc = -EINTR; | |
3886 | total_read = ctx->total_len; | |
3887 | mutex_unlock(&ctx->aio_mutex); | |
3888 | } else { | |
3889 | rc = ctx->rc; | |
3890 | total_read = ctx->total_len; | |
3891 | } | |
3892 | ||
3893 | kref_put(&ctx->refcount, cifs_aio_ctx_release); | |
3894 | ||
0165e810 | 3895 | if (total_read) { |
e6a7bcb4 | 3896 | iocb->ki_pos += total_read; |
0165e810 AV |
3897 | return total_read; |
3898 | } | |
3899 | return rc; | |
a70307ee PS |
3900 | } |
3901 | ||
6e6e2b86 LL |
3902 | ssize_t cifs_direct_readv(struct kiocb *iocb, struct iov_iter *to) |
3903 | { | |
3904 | return __cifs_readv(iocb, to, true); | |
3905 | } | |
3906 | ||
3907 | ssize_t cifs_user_readv(struct kiocb *iocb, struct iov_iter *to) | |
3908 | { | |
3909 | return __cifs_readv(iocb, to, false); | |
3910 | } | |
3911 | ||
579f9053 | 3912 | ssize_t |
e6a7bcb4 | 3913 | cifs_strict_readv(struct kiocb *iocb, struct iov_iter *to) |
a70307ee | 3914 | { |
496ad9aa | 3915 | struct inode *inode = file_inode(iocb->ki_filp); |
579f9053 PS |
3916 | struct cifsInodeInfo *cinode = CIFS_I(inode); |
3917 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); | |
3918 | struct cifsFileInfo *cfile = (struct cifsFileInfo *) | |
3919 | iocb->ki_filp->private_data; | |
3920 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); | |
3921 | int rc = -EACCES; | |
a70307ee PS |
3922 | |
3923 | /* | |
3924 | * In strict cache mode we need to read from the server all the time | |
3925 | * if we don't have level II oplock because the server can delay mtime | |
3926 | * change - so we can't make a decision about inode invalidating. | |
3927 | * And we can also fail with pagereading if there are mandatory locks | |
3928 | * on pages affected by this read but not on the region from pos to | |
3929 | * pos+len-1. | |
3930 | */ | |
18cceb6a | 3931 | if (!CIFS_CACHE_READ(cinode)) |
e6a7bcb4 | 3932 | return cifs_user_readv(iocb, to); |
a70307ee | 3933 | |
579f9053 PS |
3934 | if (cap_unix(tcon->ses) && |
3935 | (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) && | |
3936 | ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) | |
e6a7bcb4 | 3937 | return generic_file_read_iter(iocb, to); |
579f9053 PS |
3938 | |
3939 | /* | |
3940 | * We need to hold the sem to be sure nobody modifies lock list | |
3941 | * with a brlock that prevents reading. | |
3942 | */ | |
3943 | down_read(&cinode->lock_sem); | |
e6a7bcb4 | 3944 | if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(to), |
579f9053 | 3945 | tcon->ses->server->vals->shared_lock_type, |
9645759c | 3946 | 0, NULL, CIFS_READ_OP)) |
e6a7bcb4 | 3947 | rc = generic_file_read_iter(iocb, to); |
579f9053 PS |
3948 | up_read(&cinode->lock_sem); |
3949 | return rc; | |
a70307ee | 3950 | } |
1da177e4 | 3951 | |
f9c6e234 PS |
3952 | static ssize_t |
3953 | cifs_read(struct file *file, char *read_data, size_t read_size, loff_t *offset) | |
1da177e4 LT |
3954 | { |
3955 | int rc = -EACCES; | |
3956 | unsigned int bytes_read = 0; | |
3957 | unsigned int total_read; | |
3958 | unsigned int current_read_size; | |
5eba8ab3 | 3959 | unsigned int rsize; |
1da177e4 | 3960 | struct cifs_sb_info *cifs_sb; |
29e20f9c | 3961 | struct cifs_tcon *tcon; |
f9c6e234 | 3962 | struct TCP_Server_Info *server; |
6d5786a3 | 3963 | unsigned int xid; |
f9c6e234 | 3964 | char *cur_offset; |
1da177e4 | 3965 | struct cifsFileInfo *open_file; |
d4ffff1f | 3966 | struct cifs_io_parms io_parms; |
ec637e3f | 3967 | int buf_type = CIFS_NO_BUFFER; |
d4ffff1f | 3968 | __u32 pid; |
1da177e4 | 3969 | |
6d5786a3 | 3970 | xid = get_xid(); |
7119e220 | 3971 | cifs_sb = CIFS_FILE_SB(file); |
1da177e4 | 3972 | |
5eba8ab3 JL |
3973 | /* FIXME: set up handlers for larger reads and/or convert to async */ |
3974 | rsize = min_t(unsigned int, cifs_sb->rsize, CIFSMaxBufSize); | |
3975 | ||
1da177e4 | 3976 | if (file->private_data == NULL) { |
0f3bc09e | 3977 | rc = -EBADF; |
6d5786a3 | 3978 | free_xid(xid); |
0f3bc09e | 3979 | return rc; |
1da177e4 | 3980 | } |
c21dfb69 | 3981 | open_file = file->private_data; |
29e20f9c | 3982 | tcon = tlink_tcon(open_file->tlink); |
f9c6e234 PS |
3983 | server = tcon->ses->server; |
3984 | ||
3985 | if (!server->ops->sync_read) { | |
3986 | free_xid(xid); | |
3987 | return -ENOSYS; | |
3988 | } | |
1da177e4 | 3989 | |
d4ffff1f PS |
3990 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) |
3991 | pid = open_file->pid; | |
3992 | else | |
3993 | pid = current->tgid; | |
3994 | ||
1da177e4 | 3995 | if ((file->f_flags & O_ACCMODE) == O_WRONLY) |
f96637be | 3996 | cifs_dbg(FYI, "attempting read on write only file instance\n"); |
1da177e4 | 3997 | |
f9c6e234 PS |
3998 | for (total_read = 0, cur_offset = read_data; read_size > total_read; |
3999 | total_read += bytes_read, cur_offset += bytes_read) { | |
e374d90f PS |
4000 | do { |
4001 | current_read_size = min_t(uint, read_size - total_read, | |
4002 | rsize); | |
4003 | /* | |
4004 | * For windows me and 9x we do not want to request more | |
4005 | * than it negotiated since it will refuse the read | |
4006 | * then. | |
4007 | */ | |
4008 | if ((tcon->ses) && !(tcon->ses->capabilities & | |
29e20f9c | 4009 | tcon->ses->server->vals->cap_large_files)) { |
e374d90f PS |
4010 | current_read_size = min_t(uint, |
4011 | current_read_size, CIFSMaxBufSize); | |
4012 | } | |
cdff08e7 | 4013 | if (open_file->invalidHandle) { |
15886177 | 4014 | rc = cifs_reopen_file(open_file, true); |
1da177e4 LT |
4015 | if (rc != 0) |
4016 | break; | |
4017 | } | |
d4ffff1f | 4018 | io_parms.pid = pid; |
29e20f9c | 4019 | io_parms.tcon = tcon; |
f9c6e234 | 4020 | io_parms.offset = *offset; |
d4ffff1f | 4021 | io_parms.length = current_read_size; |
db8b631d | 4022 | rc = server->ops->sync_read(xid, &open_file->fid, &io_parms, |
f9c6e234 PS |
4023 | &bytes_read, &cur_offset, |
4024 | &buf_type); | |
e374d90f PS |
4025 | } while (rc == -EAGAIN); |
4026 | ||
1da177e4 LT |
4027 | if (rc || (bytes_read == 0)) { |
4028 | if (total_read) { | |
4029 | break; | |
4030 | } else { | |
6d5786a3 | 4031 | free_xid(xid); |
1da177e4 LT |
4032 | return rc; |
4033 | } | |
4034 | } else { | |
29e20f9c | 4035 | cifs_stats_bytes_read(tcon, total_read); |
f9c6e234 | 4036 | *offset += bytes_read; |
1da177e4 LT |
4037 | } |
4038 | } | |
6d5786a3 | 4039 | free_xid(xid); |
1da177e4 LT |
4040 | return total_read; |
4041 | } | |
4042 | ||
ca83ce3d JL |
4043 | /* |
4044 | * If the page is mmap'ed into a process' page tables, then we need to make | |
4045 | * sure that it doesn't change while being written back. | |
4046 | */ | |
a5240cbd | 4047 | static vm_fault_t |
11bac800 | 4048 | cifs_page_mkwrite(struct vm_fault *vmf) |
ca83ce3d JL |
4049 | { |
4050 | struct page *page = vmf->page; | |
4051 | ||
4052 | lock_page(page); | |
4053 | return VM_FAULT_LOCKED; | |
4054 | } | |
4055 | ||
7cbea8dc | 4056 | static const struct vm_operations_struct cifs_file_vm_ops = { |
ca83ce3d | 4057 | .fault = filemap_fault, |
f1820361 | 4058 | .map_pages = filemap_map_pages, |
ca83ce3d JL |
4059 | .page_mkwrite = cifs_page_mkwrite, |
4060 | }; | |
4061 | ||
7a6a19b1 PS |
4062 | int cifs_file_strict_mmap(struct file *file, struct vm_area_struct *vma) |
4063 | { | |
f04a703c | 4064 | int xid, rc = 0; |
496ad9aa | 4065 | struct inode *inode = file_inode(file); |
7a6a19b1 | 4066 | |
6d5786a3 | 4067 | xid = get_xid(); |
7a6a19b1 | 4068 | |
f04a703c | 4069 | if (!CIFS_CACHE_READ(CIFS_I(inode))) |
4f73c7d3 | 4070 | rc = cifs_zap_mapping(inode); |
f04a703c MW |
4071 | if (!rc) |
4072 | rc = generic_file_mmap(file, vma); | |
4073 | if (!rc) | |
ca83ce3d | 4074 | vma->vm_ops = &cifs_file_vm_ops; |
f04a703c | 4075 | |
6d5786a3 | 4076 | free_xid(xid); |
7a6a19b1 PS |
4077 | return rc; |
4078 | } | |
4079 | ||
1da177e4 LT |
4080 | int cifs_file_mmap(struct file *file, struct vm_area_struct *vma) |
4081 | { | |
1da177e4 LT |
4082 | int rc, xid; |
4083 | ||
6d5786a3 | 4084 | xid = get_xid(); |
f04a703c | 4085 | |
abab095d | 4086 | rc = cifs_revalidate_file(file); |
f04a703c | 4087 | if (rc) |
f96637be JP |
4088 | cifs_dbg(FYI, "Validation prior to mmap failed, error=%d\n", |
4089 | rc); | |
f04a703c MW |
4090 | if (!rc) |
4091 | rc = generic_file_mmap(file, vma); | |
4092 | if (!rc) | |
ca83ce3d | 4093 | vma->vm_ops = &cifs_file_vm_ops; |
f04a703c | 4094 | |
6d5786a3 | 4095 | free_xid(xid); |
1da177e4 LT |
4096 | return rc; |
4097 | } | |
4098 | ||
0471ca3f JL |
4099 | static void |
4100 | cifs_readv_complete(struct work_struct *work) | |
4101 | { | |
b770ddfa | 4102 | unsigned int i, got_bytes; |
0471ca3f JL |
4103 | struct cifs_readdata *rdata = container_of(work, |
4104 | struct cifs_readdata, work); | |
0471ca3f | 4105 | |
b770ddfa | 4106 | got_bytes = rdata->got_bytes; |
c5fab6f4 JL |
4107 | for (i = 0; i < rdata->nr_pages; i++) { |
4108 | struct page *page = rdata->pages[i]; | |
4109 | ||
0471ca3f JL |
4110 | lru_cache_add_file(page); |
4111 | ||
b770ddfa PS |
4112 | if (rdata->result == 0 || |
4113 | (rdata->result == -EAGAIN && got_bytes)) { | |
0471ca3f JL |
4114 | flush_dcache_page(page); |
4115 | SetPageUptodate(page); | |
4116 | } | |
4117 | ||
4118 | unlock_page(page); | |
4119 | ||
b770ddfa PS |
4120 | if (rdata->result == 0 || |
4121 | (rdata->result == -EAGAIN && got_bytes)) | |
0471ca3f JL |
4122 | cifs_readpage_to_fscache(rdata->mapping->host, page); |
4123 | ||
09cbfeaf | 4124 | got_bytes -= min_t(unsigned int, PAGE_SIZE, got_bytes); |
b770ddfa | 4125 | |
09cbfeaf | 4126 | put_page(page); |
c5fab6f4 | 4127 | rdata->pages[i] = NULL; |
0471ca3f | 4128 | } |
6993f74a | 4129 | kref_put(&rdata->refcount, cifs_readdata_release); |
0471ca3f JL |
4130 | } |
4131 | ||
8d5ce4d2 | 4132 | static int |
d70b9104 PS |
4133 | readpages_fill_pages(struct TCP_Server_Info *server, |
4134 | struct cifs_readdata *rdata, struct iov_iter *iter, | |
4135 | unsigned int len) | |
8d5ce4d2 | 4136 | { |
b3160aeb | 4137 | int result = 0; |
c5fab6f4 | 4138 | unsigned int i; |
8d5ce4d2 JL |
4139 | u64 eof; |
4140 | pgoff_t eof_index; | |
c5fab6f4 | 4141 | unsigned int nr_pages = rdata->nr_pages; |
1dbe3466 | 4142 | unsigned int page_offset = rdata->page_offset; |
8d5ce4d2 JL |
4143 | |
4144 | /* determine the eof that the server (probably) has */ | |
4145 | eof = CIFS_I(rdata->mapping->host)->server_eof; | |
09cbfeaf | 4146 | eof_index = eof ? (eof - 1) >> PAGE_SHIFT : 0; |
f96637be | 4147 | cifs_dbg(FYI, "eof=%llu eof_index=%lu\n", eof, eof_index); |
8d5ce4d2 | 4148 | |
b3160aeb | 4149 | rdata->got_bytes = 0; |
09cbfeaf | 4150 | rdata->tailsz = PAGE_SIZE; |
c5fab6f4 JL |
4151 | for (i = 0; i < nr_pages; i++) { |
4152 | struct page *page = rdata->pages[i]; | |
1dbe3466 LL |
4153 | unsigned int to_read = rdata->pagesz; |
4154 | size_t n; | |
4155 | ||
4156 | if (i == 0) | |
4157 | to_read -= page_offset; | |
4158 | else | |
4159 | page_offset = 0; | |
4160 | ||
4161 | n = to_read; | |
c5fab6f4 | 4162 | |
1dbe3466 LL |
4163 | if (len >= to_read) { |
4164 | len -= to_read; | |
8321fec4 | 4165 | } else if (len > 0) { |
8d5ce4d2 | 4166 | /* enough for partial page, fill and zero the rest */ |
1dbe3466 | 4167 | zero_user(page, len + page_offset, to_read - len); |
71335664 | 4168 | n = rdata->tailsz = len; |
8321fec4 | 4169 | len = 0; |
8d5ce4d2 JL |
4170 | } else if (page->index > eof_index) { |
4171 | /* | |
4172 | * The VFS will not try to do readahead past the | |
4173 | * i_size, but it's possible that we have outstanding | |
4174 | * writes with gaps in the middle and the i_size hasn't | |
4175 | * caught up yet. Populate those with zeroed out pages | |
4176 | * to prevent the VFS from repeatedly attempting to | |
4177 | * fill them until the writes are flushed. | |
4178 | */ | |
09cbfeaf | 4179 | zero_user(page, 0, PAGE_SIZE); |
8d5ce4d2 JL |
4180 | lru_cache_add_file(page); |
4181 | flush_dcache_page(page); | |
4182 | SetPageUptodate(page); | |
4183 | unlock_page(page); | |
09cbfeaf | 4184 | put_page(page); |
c5fab6f4 JL |
4185 | rdata->pages[i] = NULL; |
4186 | rdata->nr_pages--; | |
8321fec4 | 4187 | continue; |
8d5ce4d2 JL |
4188 | } else { |
4189 | /* no need to hold page hostage */ | |
8d5ce4d2 JL |
4190 | lru_cache_add_file(page); |
4191 | unlock_page(page); | |
09cbfeaf | 4192 | put_page(page); |
c5fab6f4 JL |
4193 | rdata->pages[i] = NULL; |
4194 | rdata->nr_pages--; | |
8321fec4 | 4195 | continue; |
8d5ce4d2 | 4196 | } |
8321fec4 | 4197 | |
d70b9104 | 4198 | if (iter) |
1dbe3466 LL |
4199 | result = copy_page_from_iter( |
4200 | page, page_offset, n, iter); | |
bd3dcc6a LL |
4201 | #ifdef CONFIG_CIFS_SMB_DIRECT |
4202 | else if (rdata->mr) | |
4203 | result = n; | |
4204 | #endif | |
d70b9104 | 4205 | else |
1dbe3466 LL |
4206 | result = cifs_read_page_from_socket( |
4207 | server, page, page_offset, n); | |
8321fec4 JL |
4208 | if (result < 0) |
4209 | break; | |
4210 | ||
b3160aeb | 4211 | rdata->got_bytes += result; |
8d5ce4d2 JL |
4212 | } |
4213 | ||
b3160aeb PS |
4214 | return rdata->got_bytes > 0 && result != -ECONNABORTED ? |
4215 | rdata->got_bytes : result; | |
8d5ce4d2 JL |
4216 | } |
4217 | ||
d70b9104 PS |
4218 | static int |
4219 | cifs_readpages_read_into_pages(struct TCP_Server_Info *server, | |
4220 | struct cifs_readdata *rdata, unsigned int len) | |
4221 | { | |
4222 | return readpages_fill_pages(server, rdata, NULL, len); | |
4223 | } | |
4224 | ||
4225 | static int | |
4226 | cifs_readpages_copy_into_pages(struct TCP_Server_Info *server, | |
4227 | struct cifs_readdata *rdata, | |
4228 | struct iov_iter *iter) | |
4229 | { | |
4230 | return readpages_fill_pages(server, rdata, iter, iter->count); | |
4231 | } | |
4232 | ||
387eb92a PS |
4233 | static int |
4234 | readpages_get_pages(struct address_space *mapping, struct list_head *page_list, | |
4235 | unsigned int rsize, struct list_head *tmplist, | |
4236 | unsigned int *nr_pages, loff_t *offset, unsigned int *bytes) | |
4237 | { | |
4238 | struct page *page, *tpage; | |
4239 | unsigned int expected_index; | |
4240 | int rc; | |
8a5c743e | 4241 | gfp_t gfp = readahead_gfp_mask(mapping); |
387eb92a | 4242 | |
69cebd75 PS |
4243 | INIT_LIST_HEAD(tmplist); |
4244 | ||
f86196ea | 4245 | page = lru_to_page(page_list); |
387eb92a PS |
4246 | |
4247 | /* | |
4248 | * Lock the page and put it in the cache. Since no one else | |
4249 | * should have access to this page, we're safe to simply set | |
4250 | * PG_locked without checking it first. | |
4251 | */ | |
48c935ad | 4252 | __SetPageLocked(page); |
387eb92a | 4253 | rc = add_to_page_cache_locked(page, mapping, |
063d99b4 | 4254 | page->index, gfp); |
387eb92a PS |
4255 | |
4256 | /* give up if we can't stick it in the cache */ | |
4257 | if (rc) { | |
48c935ad | 4258 | __ClearPageLocked(page); |
387eb92a PS |
4259 | return rc; |
4260 | } | |
4261 | ||
4262 | /* move first page to the tmplist */ | |
09cbfeaf KS |
4263 | *offset = (loff_t)page->index << PAGE_SHIFT; |
4264 | *bytes = PAGE_SIZE; | |
387eb92a PS |
4265 | *nr_pages = 1; |
4266 | list_move_tail(&page->lru, tmplist); | |
4267 | ||
4268 | /* now try and add more pages onto the request */ | |
4269 | expected_index = page->index + 1; | |
4270 | list_for_each_entry_safe_reverse(page, tpage, page_list, lru) { | |
4271 | /* discontinuity ? */ | |
4272 | if (page->index != expected_index) | |
4273 | break; | |
4274 | ||
4275 | /* would this page push the read over the rsize? */ | |
09cbfeaf | 4276 | if (*bytes + PAGE_SIZE > rsize) |
387eb92a PS |
4277 | break; |
4278 | ||
48c935ad | 4279 | __SetPageLocked(page); |
063d99b4 | 4280 | if (add_to_page_cache_locked(page, mapping, page->index, gfp)) { |
48c935ad | 4281 | __ClearPageLocked(page); |
387eb92a PS |
4282 | break; |
4283 | } | |
4284 | list_move_tail(&page->lru, tmplist); | |
09cbfeaf | 4285 | (*bytes) += PAGE_SIZE; |
387eb92a PS |
4286 | expected_index++; |
4287 | (*nr_pages)++; | |
4288 | } | |
4289 | return rc; | |
8d5ce4d2 JL |
4290 | } |
4291 | ||
1da177e4 LT |
4292 | static int cifs_readpages(struct file *file, struct address_space *mapping, |
4293 | struct list_head *page_list, unsigned num_pages) | |
4294 | { | |
690c5e31 JL |
4295 | int rc; |
4296 | struct list_head tmplist; | |
4297 | struct cifsFileInfo *open_file = file->private_data; | |
7119e220 | 4298 | struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file); |
69cebd75 | 4299 | struct TCP_Server_Info *server; |
690c5e31 | 4300 | pid_t pid; |
0cb012d1 | 4301 | unsigned int xid; |
1da177e4 | 4302 | |
0cb012d1 | 4303 | xid = get_xid(); |
56698236 SJ |
4304 | /* |
4305 | * Reads as many pages as possible from fscache. Returns -ENOBUFS | |
4306 | * immediately if the cookie is negative | |
54afa990 DH |
4307 | * |
4308 | * After this point, every page in the list might have PG_fscache set, | |
4309 | * so we will need to clean that up off of every page we don't use. | |
56698236 SJ |
4310 | */ |
4311 | rc = cifs_readpages_from_fscache(mapping->host, mapping, page_list, | |
4312 | &num_pages); | |
0cb012d1 SF |
4313 | if (rc == 0) { |
4314 | free_xid(xid); | |
690c5e31 | 4315 | return rc; |
0cb012d1 | 4316 | } |
56698236 | 4317 | |
d4ffff1f PS |
4318 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) |
4319 | pid = open_file->pid; | |
4320 | else | |
4321 | pid = current->tgid; | |
4322 | ||
690c5e31 | 4323 | rc = 0; |
69cebd75 | 4324 | server = tlink_tcon(open_file->tlink)->ses->server; |
1da177e4 | 4325 | |
f96637be JP |
4326 | cifs_dbg(FYI, "%s: file=%p mapping=%p num_pages=%u\n", |
4327 | __func__, file, mapping, num_pages); | |
690c5e31 JL |
4328 | |
4329 | /* | |
4330 | * Start with the page at end of list and move it to private | |
4331 | * list. Do the same with any following pages until we hit | |
4332 | * the rsize limit, hit an index discontinuity, or run out of | |
4333 | * pages. Issue the async read and then start the loop again | |
4334 | * until the list is empty. | |
4335 | * | |
4336 | * Note that list order is important. The page_list is in | |
4337 | * the order of declining indexes. When we put the pages in | |
4338 | * the rdata->pages, then we want them in increasing order. | |
4339 | */ | |
4340 | while (!list_empty(page_list)) { | |
bed9da02 | 4341 | unsigned int i, nr_pages, bytes, rsize; |
690c5e31 JL |
4342 | loff_t offset; |
4343 | struct page *page, *tpage; | |
4344 | struct cifs_readdata *rdata; | |
335b7b62 PS |
4345 | struct cifs_credits credits_on_stack; |
4346 | struct cifs_credits *credits = &credits_on_stack; | |
1da177e4 | 4347 | |
3e952994 PS |
4348 | if (open_file->invalidHandle) { |
4349 | rc = cifs_reopen_file(open_file, true); | |
4350 | if (rc == -EAGAIN) | |
4351 | continue; | |
4352 | else if (rc) | |
4353 | break; | |
4354 | } | |
4355 | ||
bed9da02 | 4356 | rc = server->ops->wait_mtu_credits(server, cifs_sb->rsize, |
335b7b62 | 4357 | &rsize, credits); |
bed9da02 PS |
4358 | if (rc) |
4359 | break; | |
690c5e31 JL |
4360 | |
4361 | /* | |
69cebd75 PS |
4362 | * Give up immediately if rsize is too small to read an entire |
4363 | * page. The VFS will fall back to readpage. We should never | |
4364 | * reach this point however since we set ra_pages to 0 when the | |
4365 | * rsize is smaller than a cache page. | |
690c5e31 | 4366 | */ |
09cbfeaf | 4367 | if (unlikely(rsize < PAGE_SIZE)) { |
bed9da02 | 4368 | add_credits_and_wake_if(server, credits, 0); |
0cb012d1 | 4369 | free_xid(xid); |
69cebd75 | 4370 | return 0; |
bed9da02 | 4371 | } |
690c5e31 | 4372 | |
bed9da02 PS |
4373 | rc = readpages_get_pages(mapping, page_list, rsize, &tmplist, |
4374 | &nr_pages, &offset, &bytes); | |
690c5e31 | 4375 | if (rc) { |
bed9da02 | 4376 | add_credits_and_wake_if(server, credits, 0); |
690c5e31 JL |
4377 | break; |
4378 | } | |
4379 | ||
0471ca3f | 4380 | rdata = cifs_readdata_alloc(nr_pages, cifs_readv_complete); |
690c5e31 JL |
4381 | if (!rdata) { |
4382 | /* best to give up if we're out of mem */ | |
4383 | list_for_each_entry_safe(page, tpage, &tmplist, lru) { | |
4384 | list_del(&page->lru); | |
4385 | lru_cache_add_file(page); | |
4386 | unlock_page(page); | |
09cbfeaf | 4387 | put_page(page); |
690c5e31 JL |
4388 | } |
4389 | rc = -ENOMEM; | |
bed9da02 | 4390 | add_credits_and_wake_if(server, credits, 0); |
690c5e31 JL |
4391 | break; |
4392 | } | |
4393 | ||
6993f74a | 4394 | rdata->cfile = cifsFileInfo_get(open_file); |
690c5e31 JL |
4395 | rdata->mapping = mapping; |
4396 | rdata->offset = offset; | |
4397 | rdata->bytes = bytes; | |
4398 | rdata->pid = pid; | |
09cbfeaf | 4399 | rdata->pagesz = PAGE_SIZE; |
1dbe3466 | 4400 | rdata->tailsz = PAGE_SIZE; |
8321fec4 | 4401 | rdata->read_into_pages = cifs_readpages_read_into_pages; |
d70b9104 | 4402 | rdata->copy_into_pages = cifs_readpages_copy_into_pages; |
335b7b62 | 4403 | rdata->credits = credits_on_stack; |
c5fab6f4 JL |
4404 | |
4405 | list_for_each_entry_safe(page, tpage, &tmplist, lru) { | |
4406 | list_del(&page->lru); | |
4407 | rdata->pages[rdata->nr_pages++] = page; | |
4408 | } | |
690c5e31 | 4409 | |
9a1c67e8 PS |
4410 | rc = adjust_credits(server, &rdata->credits, rdata->bytes); |
4411 | ||
4412 | if (!rc) { | |
4413 | if (rdata->cfile->invalidHandle) | |
3e952994 PS |
4414 | rc = -EAGAIN; |
4415 | else | |
9a1c67e8 PS |
4416 | rc = server->ops->async_readv(rdata); |
4417 | } | |
4418 | ||
69cebd75 | 4419 | if (rc) { |
335b7b62 | 4420 | add_credits_and_wake_if(server, &rdata->credits, 0); |
c5fab6f4 JL |
4421 | for (i = 0; i < rdata->nr_pages; i++) { |
4422 | page = rdata->pages[i]; | |
690c5e31 JL |
4423 | lru_cache_add_file(page); |
4424 | unlock_page(page); | |
09cbfeaf | 4425 | put_page(page); |
1da177e4 | 4426 | } |
1209bbdf | 4427 | /* Fallback to the readpage in error/reconnect cases */ |
6993f74a | 4428 | kref_put(&rdata->refcount, cifs_readdata_release); |
1da177e4 LT |
4429 | break; |
4430 | } | |
6993f74a JL |
4431 | |
4432 | kref_put(&rdata->refcount, cifs_readdata_release); | |
1da177e4 LT |
4433 | } |
4434 | ||
54afa990 DH |
4435 | /* Any pages that have been shown to fscache but didn't get added to |
4436 | * the pagecache must be uncached before they get returned to the | |
4437 | * allocator. | |
4438 | */ | |
4439 | cifs_fscache_readpages_cancel(mapping->host, page_list); | |
0cb012d1 | 4440 | free_xid(xid); |
1da177e4 LT |
4441 | return rc; |
4442 | } | |
4443 | ||
a9e9b7bc SP |
4444 | /* |
4445 | * cifs_readpage_worker must be called with the page pinned | |
4446 | */ | |
1da177e4 LT |
4447 | static int cifs_readpage_worker(struct file *file, struct page *page, |
4448 | loff_t *poffset) | |
4449 | { | |
4450 | char *read_data; | |
4451 | int rc; | |
4452 | ||
56698236 | 4453 | /* Is the page cached? */ |
496ad9aa | 4454 | rc = cifs_readpage_from_fscache(file_inode(file), page); |
56698236 SJ |
4455 | if (rc == 0) |
4456 | goto read_complete; | |
4457 | ||
1da177e4 LT |
4458 | read_data = kmap(page); |
4459 | /* for reads over a certain size could initiate async read ahead */ | |
fb8c4b14 | 4460 | |
09cbfeaf | 4461 | rc = cifs_read(file, read_data, PAGE_SIZE, poffset); |
fb8c4b14 | 4462 | |
1da177e4 LT |
4463 | if (rc < 0) |
4464 | goto io_error; | |
4465 | else | |
f96637be | 4466 | cifs_dbg(FYI, "Bytes read %d\n", rc); |
fb8c4b14 | 4467 | |
9b9c5bea SF |
4468 | /* we do not want atime to be less than mtime, it broke some apps */ |
4469 | file_inode(file)->i_atime = current_time(file_inode(file)); | |
4470 | if (timespec64_compare(&(file_inode(file)->i_atime), &(file_inode(file)->i_mtime))) | |
4471 | file_inode(file)->i_atime = file_inode(file)->i_mtime; | |
4472 | else | |
4473 | file_inode(file)->i_atime = current_time(file_inode(file)); | |
fb8c4b14 | 4474 | |
09cbfeaf KS |
4475 | if (PAGE_SIZE > rc) |
4476 | memset(read_data + rc, 0, PAGE_SIZE - rc); | |
1da177e4 LT |
4477 | |
4478 | flush_dcache_page(page); | |
4479 | SetPageUptodate(page); | |
9dc06558 SJ |
4480 | |
4481 | /* send this page to the cache */ | |
496ad9aa | 4482 | cifs_readpage_to_fscache(file_inode(file), page); |
9dc06558 | 4483 | |
1da177e4 | 4484 | rc = 0; |
fb8c4b14 | 4485 | |
1da177e4 | 4486 | io_error: |
fb8c4b14 | 4487 | kunmap(page); |
466bd31b | 4488 | unlock_page(page); |
56698236 SJ |
4489 | |
4490 | read_complete: | |
1da177e4 LT |
4491 | return rc; |
4492 | } | |
4493 | ||
4494 | static int cifs_readpage(struct file *file, struct page *page) | |
4495 | { | |
09cbfeaf | 4496 | loff_t offset = (loff_t)page->index << PAGE_SHIFT; |
1da177e4 | 4497 | int rc = -EACCES; |
6d5786a3 | 4498 | unsigned int xid; |
1da177e4 | 4499 | |
6d5786a3 | 4500 | xid = get_xid(); |
1da177e4 LT |
4501 | |
4502 | if (file->private_data == NULL) { | |
0f3bc09e | 4503 | rc = -EBADF; |
6d5786a3 | 4504 | free_xid(xid); |
0f3bc09e | 4505 | return rc; |
1da177e4 LT |
4506 | } |
4507 | ||
f96637be | 4508 | cifs_dbg(FYI, "readpage %p at offset %d 0x%x\n", |
b6b38f70 | 4509 | page, (int)offset, (int)offset); |
1da177e4 LT |
4510 | |
4511 | rc = cifs_readpage_worker(file, page, &offset); | |
4512 | ||
6d5786a3 | 4513 | free_xid(xid); |
1da177e4 LT |
4514 | return rc; |
4515 | } | |
4516 | ||
a403a0a3 SF |
4517 | static int is_inode_writable(struct cifsInodeInfo *cifs_inode) |
4518 | { | |
4519 | struct cifsFileInfo *open_file; | |
4520 | ||
cb248819 | 4521 | spin_lock(&cifs_inode->open_file_lock); |
a403a0a3 | 4522 | list_for_each_entry(open_file, &cifs_inode->openFileList, flist) { |
2e396b83 | 4523 | if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) { |
cb248819 | 4524 | spin_unlock(&cifs_inode->open_file_lock); |
a403a0a3 SF |
4525 | return 1; |
4526 | } | |
4527 | } | |
cb248819 | 4528 | spin_unlock(&cifs_inode->open_file_lock); |
a403a0a3 SF |
4529 | return 0; |
4530 | } | |
4531 | ||
1da177e4 LT |
4532 | /* We do not want to update the file size from server for inodes |
4533 | open for write - to avoid races with writepage extending | |
4534 | the file - in the future we could consider allowing | |
fb8c4b14 | 4535 | refreshing the inode only on increases in the file size |
1da177e4 LT |
4536 | but this is tricky to do without racing with writebehind |
4537 | page caching in the current Linux kernel design */ | |
4b18f2a9 | 4538 | bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file) |
1da177e4 | 4539 | { |
a403a0a3 | 4540 | if (!cifsInode) |
4b18f2a9 | 4541 | return true; |
50c2f753 | 4542 | |
a403a0a3 SF |
4543 | if (is_inode_writable(cifsInode)) { |
4544 | /* This inode is open for write at least once */ | |
c32a0b68 SF |
4545 | struct cifs_sb_info *cifs_sb; |
4546 | ||
c32a0b68 | 4547 | cifs_sb = CIFS_SB(cifsInode->vfs_inode.i_sb); |
ad7a2926 | 4548 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) { |
fb8c4b14 | 4549 | /* since no page cache to corrupt on directio |
c32a0b68 | 4550 | we can change size safely */ |
4b18f2a9 | 4551 | return true; |
c32a0b68 SF |
4552 | } |
4553 | ||
fb8c4b14 | 4554 | if (i_size_read(&cifsInode->vfs_inode) < end_of_file) |
4b18f2a9 | 4555 | return true; |
7ba52631 | 4556 | |
4b18f2a9 | 4557 | return false; |
23e7dd7d | 4558 | } else |
4b18f2a9 | 4559 | return true; |
1da177e4 LT |
4560 | } |
4561 | ||
d9414774 NP |
4562 | static int cifs_write_begin(struct file *file, struct address_space *mapping, |
4563 | loff_t pos, unsigned len, unsigned flags, | |
4564 | struct page **pagep, void **fsdata) | |
1da177e4 | 4565 | { |
466bd31b | 4566 | int oncethru = 0; |
09cbfeaf KS |
4567 | pgoff_t index = pos >> PAGE_SHIFT; |
4568 | loff_t offset = pos & (PAGE_SIZE - 1); | |
a98ee8c1 JL |
4569 | loff_t page_start = pos & PAGE_MASK; |
4570 | loff_t i_size; | |
4571 | struct page *page; | |
4572 | int rc = 0; | |
d9414774 | 4573 | |
f96637be | 4574 | cifs_dbg(FYI, "write_begin from %lld len %d\n", (long long)pos, len); |
d9414774 | 4575 | |
466bd31b | 4576 | start: |
54566b2c | 4577 | page = grab_cache_page_write_begin(mapping, index, flags); |
a98ee8c1 JL |
4578 | if (!page) { |
4579 | rc = -ENOMEM; | |
4580 | goto out; | |
4581 | } | |
8a236264 | 4582 | |
a98ee8c1 JL |
4583 | if (PageUptodate(page)) |
4584 | goto out; | |
8a236264 | 4585 | |
a98ee8c1 JL |
4586 | /* |
4587 | * If we write a full page it will be up to date, no need to read from | |
4588 | * the server. If the write is short, we'll end up doing a sync write | |
4589 | * instead. | |
4590 | */ | |
09cbfeaf | 4591 | if (len == PAGE_SIZE) |
a98ee8c1 | 4592 | goto out; |
8a236264 | 4593 | |
a98ee8c1 JL |
4594 | /* |
4595 | * optimize away the read when we have an oplock, and we're not | |
4596 | * expecting to use any of the data we'd be reading in. That | |
4597 | * is, when the page lies beyond the EOF, or straddles the EOF | |
4598 | * and the write will cover all of the existing data. | |
4599 | */ | |
18cceb6a | 4600 | if (CIFS_CACHE_READ(CIFS_I(mapping->host))) { |
a98ee8c1 JL |
4601 | i_size = i_size_read(mapping->host); |
4602 | if (page_start >= i_size || | |
4603 | (offset == 0 && (pos + len) >= i_size)) { | |
4604 | zero_user_segments(page, 0, offset, | |
4605 | offset + len, | |
09cbfeaf | 4606 | PAGE_SIZE); |
a98ee8c1 JL |
4607 | /* |
4608 | * PageChecked means that the parts of the page | |
4609 | * to which we're not writing are considered up | |
4610 | * to date. Once the data is copied to the | |
4611 | * page, it can be set uptodate. | |
4612 | */ | |
4613 | SetPageChecked(page); | |
4614 | goto out; | |
4615 | } | |
4616 | } | |
d9414774 | 4617 | |
466bd31b | 4618 | if ((file->f_flags & O_ACCMODE) != O_WRONLY && !oncethru) { |
a98ee8c1 JL |
4619 | /* |
4620 | * might as well read a page, it is fast enough. If we get | |
4621 | * an error, we don't need to return it. cifs_write_end will | |
4622 | * do a sync write instead since PG_uptodate isn't set. | |
4623 | */ | |
4624 | cifs_readpage_worker(file, page, &page_start); | |
09cbfeaf | 4625 | put_page(page); |
466bd31b SP |
4626 | oncethru = 1; |
4627 | goto start; | |
8a236264 SF |
4628 | } else { |
4629 | /* we could try using another file handle if there is one - | |
4630 | but how would we lock it to prevent close of that handle | |
4631 | racing with this read? In any case | |
d9414774 | 4632 | this will be written out by write_end so is fine */ |
1da177e4 | 4633 | } |
a98ee8c1 JL |
4634 | out: |
4635 | *pagep = page; | |
4636 | return rc; | |
1da177e4 LT |
4637 | } |
4638 | ||
85f2d6b4 SJ |
4639 | static int cifs_release_page(struct page *page, gfp_t gfp) |
4640 | { | |
4641 | if (PagePrivate(page)) | |
4642 | return 0; | |
4643 | ||
4644 | return cifs_fscache_release_page(page, gfp); | |
4645 | } | |
4646 | ||
d47992f8 LC |
4647 | static void cifs_invalidate_page(struct page *page, unsigned int offset, |
4648 | unsigned int length) | |
85f2d6b4 SJ |
4649 | { |
4650 | struct cifsInodeInfo *cifsi = CIFS_I(page->mapping->host); | |
4651 | ||
09cbfeaf | 4652 | if (offset == 0 && length == PAGE_SIZE) |
85f2d6b4 SJ |
4653 | cifs_fscache_invalidate_page(page, &cifsi->vfs_inode); |
4654 | } | |
4655 | ||
9ad1506b PS |
4656 | static int cifs_launder_page(struct page *page) |
4657 | { | |
4658 | int rc = 0; | |
4659 | loff_t range_start = page_offset(page); | |
09cbfeaf | 4660 | loff_t range_end = range_start + (loff_t)(PAGE_SIZE - 1); |
9ad1506b PS |
4661 | struct writeback_control wbc = { |
4662 | .sync_mode = WB_SYNC_ALL, | |
4663 | .nr_to_write = 0, | |
4664 | .range_start = range_start, | |
4665 | .range_end = range_end, | |
4666 | }; | |
4667 | ||
f96637be | 4668 | cifs_dbg(FYI, "Launder page: %p\n", page); |
9ad1506b PS |
4669 | |
4670 | if (clear_page_dirty_for_io(page)) | |
4671 | rc = cifs_writepage_locked(page, &wbc); | |
4672 | ||
4673 | cifs_fscache_invalidate_page(page, page->mapping->host); | |
4674 | return rc; | |
4675 | } | |
4676 | ||
9b646972 | 4677 | void cifs_oplock_break(struct work_struct *work) |
3bc303c2 JL |
4678 | { |
4679 | struct cifsFileInfo *cfile = container_of(work, struct cifsFileInfo, | |
4680 | oplock_break); | |
2b0143b5 | 4681 | struct inode *inode = d_inode(cfile->dentry); |
3bc303c2 | 4682 | struct cifsInodeInfo *cinode = CIFS_I(inode); |
95a3f2f3 | 4683 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); |
c11f1df5 | 4684 | struct TCP_Server_Info *server = tcon->ses->server; |
eb4b756b | 4685 | int rc = 0; |
3bc303c2 | 4686 | |
c11f1df5 | 4687 | wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS, |
74316201 | 4688 | TASK_UNINTERRUPTIBLE); |
c11f1df5 SP |
4689 | |
4690 | server->ops->downgrade_oplock(server, cinode, | |
4691 | test_bit(CIFS_INODE_DOWNGRADE_OPLOCK_TO_L2, &cinode->flags)); | |
4692 | ||
18cceb6a | 4693 | if (!CIFS_CACHE_WRITE(cinode) && CIFS_CACHE_READ(cinode) && |
63b7d3a4 | 4694 | cifs_has_mand_locks(cinode)) { |
f96637be JP |
4695 | cifs_dbg(FYI, "Reset oplock to None for inode=%p due to mand locks\n", |
4696 | inode); | |
18cceb6a | 4697 | cinode->oplock = 0; |
63b7d3a4 PS |
4698 | } |
4699 | ||
3bc303c2 | 4700 | if (inode && S_ISREG(inode->i_mode)) { |
18cceb6a | 4701 | if (CIFS_CACHE_READ(cinode)) |
8737c930 | 4702 | break_lease(inode, O_RDONLY); |
d54ff732 | 4703 | else |
8737c930 | 4704 | break_lease(inode, O_WRONLY); |
3bc303c2 | 4705 | rc = filemap_fdatawrite(inode->i_mapping); |
18cceb6a | 4706 | if (!CIFS_CACHE_READ(cinode)) { |
eb4b756b JL |
4707 | rc = filemap_fdatawait(inode->i_mapping); |
4708 | mapping_set_error(inode->i_mapping, rc); | |
4f73c7d3 | 4709 | cifs_zap_mapping(inode); |
3bc303c2 | 4710 | } |
f96637be | 4711 | cifs_dbg(FYI, "Oplock flush inode %p rc %d\n", inode, rc); |
3bc303c2 JL |
4712 | } |
4713 | ||
85160e03 PS |
4714 | rc = cifs_push_locks(cfile); |
4715 | if (rc) | |
f96637be | 4716 | cifs_dbg(VFS, "Push locks rc = %d\n", rc); |
85160e03 | 4717 | |
3bc303c2 JL |
4718 | /* |
4719 | * releasing stale oplock after recent reconnect of smb session using | |
4720 | * a now incorrect file handle is not a data integrity issue but do | |
4721 | * not bother sending an oplock release if session to server still is | |
4722 | * disconnected since oplock already released by the server | |
4723 | */ | |
cdff08e7 | 4724 | if (!cfile->oplock_break_cancelled) { |
95a3f2f3 PS |
4725 | rc = tcon->ses->server->ops->oplock_response(tcon, &cfile->fid, |
4726 | cinode); | |
f96637be | 4727 | cifs_dbg(FYI, "Oplock release rc = %d\n", rc); |
3bc303c2 | 4728 | } |
b98749ca | 4729 | _cifsFileInfo_put(cfile, false /* do not wait for ourself */); |
c11f1df5 | 4730 | cifs_done_oplock_break(cinode); |
3bc303c2 JL |
4731 | } |
4732 | ||
dca69288 SF |
4733 | /* |
4734 | * The presence of cifs_direct_io() in the address space ops vector | |
4735 | * allowes open() O_DIRECT flags which would have failed otherwise. | |
4736 | * | |
4737 | * In the non-cached mode (mount with cache=none), we shunt off direct read and write requests | |
4738 | * so this method should never be called. | |
4739 | * | |
4740 | * Direct IO is not yet supported in the cached mode. | |
4741 | */ | |
4742 | static ssize_t | |
c8b8e32d | 4743 | cifs_direct_io(struct kiocb *iocb, struct iov_iter *iter) |
dca69288 SF |
4744 | { |
4745 | /* | |
4746 | * FIXME | |
4747 | * Eventually need to support direct IO for non forcedirectio mounts | |
4748 | */ | |
4749 | return -EINVAL; | |
4750 | } | |
4751 | ||
4752 | ||
f5e54d6e | 4753 | const struct address_space_operations cifs_addr_ops = { |
1da177e4 LT |
4754 | .readpage = cifs_readpage, |
4755 | .readpages = cifs_readpages, | |
4756 | .writepage = cifs_writepage, | |
37c0eb46 | 4757 | .writepages = cifs_writepages, |
d9414774 NP |
4758 | .write_begin = cifs_write_begin, |
4759 | .write_end = cifs_write_end, | |
1da177e4 | 4760 | .set_page_dirty = __set_page_dirty_nobuffers, |
85f2d6b4 | 4761 | .releasepage = cifs_release_page, |
dca69288 | 4762 | .direct_IO = cifs_direct_io, |
85f2d6b4 | 4763 | .invalidatepage = cifs_invalidate_page, |
9ad1506b | 4764 | .launder_page = cifs_launder_page, |
1da177e4 | 4765 | }; |
273d81d6 DK |
4766 | |
4767 | /* | |
4768 | * cifs_readpages requires the server to support a buffer large enough to | |
4769 | * contain the header plus one complete page of data. Otherwise, we need | |
4770 | * to leave cifs_readpages out of the address space operations. | |
4771 | */ | |
f5e54d6e | 4772 | const struct address_space_operations cifs_addr_ops_smallbuf = { |
273d81d6 DK |
4773 | .readpage = cifs_readpage, |
4774 | .writepage = cifs_writepage, | |
4775 | .writepages = cifs_writepages, | |
d9414774 NP |
4776 | .write_begin = cifs_write_begin, |
4777 | .write_end = cifs_write_end, | |
273d81d6 | 4778 | .set_page_dirty = __set_page_dirty_nobuffers, |
85f2d6b4 SJ |
4779 | .releasepage = cifs_release_page, |
4780 | .invalidatepage = cifs_invalidate_page, | |
9ad1506b | 4781 | .launder_page = cifs_launder_page, |
273d81d6 | 4782 | }; |