1 // SPDX-License-Identifier: LGPL-2.1
2 /*
3 *
4 * vfs operations that deal with files
5 *
6 * Copyright (C) International Business Machines Corp., 2002,2010
7 * Author(s): Steve French (sfrench@us.ibm.com)
8 * Jeremy Allison (jra@samba.org)
9 *
10 */
11 #include <linux/fs.h>
12 #include <linux/filelock.h>
13 #include <linux/backing-dev.h>
14 #include <linux/stat.h>
15 #include <linux/fcntl.h>
16 #include <linux/pagemap.h>
17 #include <linux/pagevec.h>
18 #include <linux/writeback.h>
19 #include <linux/task_io_accounting_ops.h>
20 #include <linux/delay.h>
21 #include <linux/mount.h>
22 #include <linux/slab.h>
23 #include <linux/swap.h>
24 #include <linux/mm.h>
25 #include <asm/div64.h>
26 #include "cifsfs.h"
27 #include "cifspdu.h"
28 #include "cifsglob.h"
29 #include "cifsproto.h"
30 #include "smb2proto.h"
31 #include "cifs_unicode.h"
32 #include "cifs_debug.h"
33 #include "cifs_fs_sb.h"
34 #include "fscache.h"
35 #include "smbdirect.h"
36 #include "fs_context.h"
37 #include "cifs_ioctl.h"
38 #include "cached_dir.h"
39
40 /*
41 * Remove the dirty flags from a span of pages.
42 */
cifs_undirty_folios(struct inode * inode,loff_t start,unsigned int len)43 static void cifs_undirty_folios(struct inode *inode, loff_t start, unsigned int len)
44 {
45 struct address_space *mapping = inode->i_mapping;
46 struct folio *folio;
47 pgoff_t end;
48
49 XA_STATE(xas, &mapping->i_pages, start / PAGE_SIZE);
50
51 rcu_read_lock();
52
53 end = (start + len - 1) / PAGE_SIZE;
54 xas_for_each_marked(&xas, folio, end, PAGECACHE_TAG_DIRTY) {
55 if (xas_retry(&xas, folio))
56 continue;
57 xas_pause(&xas);
58 rcu_read_unlock();
59 folio_lock(folio);
60 folio_clear_dirty_for_io(folio);
61 folio_unlock(folio);
62 rcu_read_lock();
63 }
64
65 rcu_read_unlock();
66 }
67
68 /*
69 * Completion of write to server.
70 */
cifs_pages_written_back(struct inode * inode,loff_t start,unsigned int len)71 void cifs_pages_written_back(struct inode *inode, loff_t start, unsigned int len)
72 {
73 struct address_space *mapping = inode->i_mapping;
74 struct folio *folio;
75 pgoff_t end;
76
77 XA_STATE(xas, &mapping->i_pages, start / PAGE_SIZE);
78
79 if (!len)
80 return;
81
82 rcu_read_lock();
83
84 end = (start + len - 1) / PAGE_SIZE;
85 xas_for_each(&xas, folio, end) {
86 if (xas_retry(&xas, folio))
87 continue;
88 if (!folio_test_writeback(folio)) {
89 WARN_ONCE(1, "bad %x @%llx page %lx %lx\n",
90 len, start, folio->index, end);
91 continue;
92 }
93
94 folio_detach_private(folio);
95 folio_end_writeback(folio);
96 }
97
98 rcu_read_unlock();
99 }
100
101 /*
102 * Failure of write to server.
103 */
cifs_pages_write_failed(struct inode * inode,loff_t start,unsigned int len)104 void cifs_pages_write_failed(struct inode *inode, loff_t start, unsigned int len)
105 {
106 struct address_space *mapping = inode->i_mapping;
107 struct folio *folio;
108 pgoff_t end;
109
110 XA_STATE(xas, &mapping->i_pages, start / PAGE_SIZE);
111
112 if (!len)
113 return;
114
115 rcu_read_lock();
116
117 end = (start + len - 1) / PAGE_SIZE;
118 xas_for_each(&xas, folio, end) {
119 if (xas_retry(&xas, folio))
120 continue;
121 if (!folio_test_writeback(folio)) {
122 WARN_ONCE(1, "bad %x @%llx page %lx %lx\n",
123 len, start, folio->index, end);
124 continue;
125 }
126
127 folio_set_error(folio);
128 folio_end_writeback(folio);
129 }
130
131 rcu_read_unlock();
132 }
133
134 /*
135 * Redirty pages after a temporary failure.
136 */
cifs_pages_write_redirty(struct inode * inode,loff_t start,unsigned int len)137 void cifs_pages_write_redirty(struct inode *inode, loff_t start, unsigned int len)
138 {
139 struct address_space *mapping = inode->i_mapping;
140 struct folio *folio;
141 pgoff_t end;
142
143 XA_STATE(xas, &mapping->i_pages, start / PAGE_SIZE);
144
145 if (!len)
146 return;
147
148 rcu_read_lock();
149
150 end = (start + len - 1) / PAGE_SIZE;
151 xas_for_each(&xas, folio, end) {
152 if (!folio_test_writeback(folio)) {
153 WARN_ONCE(1, "bad %x @%llx page %lx %lx\n",
154 len, start, folio->index, end);
155 continue;
156 }
157
158 filemap_dirty_folio(folio->mapping, folio);
159 folio_end_writeback(folio);
160 }
161
162 rcu_read_unlock();
163 }
164
165 /*
166 * Mark as invalid, all open files on tree connections since they
167 * were closed when session to server was lost.
168 */
169 void
cifs_mark_open_files_invalid(struct cifs_tcon * tcon)170 cifs_mark_open_files_invalid(struct cifs_tcon *tcon)
171 {
172 struct cifsFileInfo *open_file = NULL;
173 struct list_head *tmp;
174 struct list_head *tmp1;
175
176 /* only send once per connect */
177 spin_lock(&tcon->tc_lock);
178 if (tcon->need_reconnect)
179 tcon->status = TID_NEED_RECON;
180
181 if (tcon->status != TID_NEED_RECON) {
182 spin_unlock(&tcon->tc_lock);
183 return;
184 }
185 tcon->status = TID_IN_FILES_INVALIDATE;
186 spin_unlock(&tcon->tc_lock);
187
188 /* list all files open on tree connection and mark them invalid */
189 spin_lock(&tcon->open_file_lock);
190 list_for_each_safe(tmp, tmp1, &tcon->openFileList) {
191 open_file = list_entry(tmp, struct cifsFileInfo, tlist);
192 open_file->invalidHandle = true;
193 open_file->oplock_break_cancelled = true;
194 }
195 spin_unlock(&tcon->open_file_lock);
196
197 invalidate_all_cached_dirs(tcon);
198 spin_lock(&tcon->tc_lock);
199 if (tcon->status == TID_IN_FILES_INVALIDATE)
200 tcon->status = TID_NEED_TCON;
201 spin_unlock(&tcon->tc_lock);
202
203 /*
204 * BB Add call to invalidate_inodes(sb) for all superblocks mounted
205 * to this tcon.
206 */
207 }
208
cifs_convert_flags(unsigned int flags,int rdwr_for_fscache)209 static inline int cifs_convert_flags(unsigned int flags, int rdwr_for_fscache)
210 {
211 if ((flags & O_ACCMODE) == O_RDONLY)
212 return GENERIC_READ;
213 else if ((flags & O_ACCMODE) == O_WRONLY)
214 return rdwr_for_fscache == 1 ? (GENERIC_READ | GENERIC_WRITE) : GENERIC_WRITE;
215 else if ((flags & O_ACCMODE) == O_RDWR) {
216 /* GENERIC_ALL is too much permission to request
217 can cause unnecessary access denied on create */
218 /* return GENERIC_ALL; */
219 return (GENERIC_READ | GENERIC_WRITE);
220 }
221
222 return (READ_CONTROL | FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES |
223 FILE_WRITE_EA | FILE_APPEND_DATA | FILE_WRITE_DATA |
224 FILE_READ_DATA);
225 }
226
227 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
cifs_posix_convert_flags(unsigned int flags)228 static u32 cifs_posix_convert_flags(unsigned int flags)
229 {
230 u32 posix_flags = 0;
231
232 if ((flags & O_ACCMODE) == O_RDONLY)
233 posix_flags = SMB_O_RDONLY;
234 else if ((flags & O_ACCMODE) == O_WRONLY)
235 posix_flags = SMB_O_WRONLY;
236 else if ((flags & O_ACCMODE) == O_RDWR)
237 posix_flags = SMB_O_RDWR;
238
239 if (flags & O_CREAT) {
240 posix_flags |= SMB_O_CREAT;
241 if (flags & O_EXCL)
242 posix_flags |= SMB_O_EXCL;
243 } else if (flags & O_EXCL)
244 cifs_dbg(FYI, "Application %s pid %d has incorrectly set O_EXCL flag but not O_CREAT on file open. Ignoring O_EXCL\n",
245 current->comm, current->tgid);
246
247 if (flags & O_TRUNC)
248 posix_flags |= SMB_O_TRUNC;
249 /* be safe and imply O_SYNC for O_DSYNC */
250 if (flags & O_DSYNC)
251 posix_flags |= SMB_O_SYNC;
252 if (flags & O_DIRECTORY)
253 posix_flags |= SMB_O_DIRECTORY;
254 if (flags & O_NOFOLLOW)
255 posix_flags |= SMB_O_NOFOLLOW;
256 if (flags & O_DIRECT)
257 posix_flags |= SMB_O_DIRECT;
258
259 return posix_flags;
260 }
261 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
262
cifs_get_disposition(unsigned int flags)263 static inline int cifs_get_disposition(unsigned int flags)
264 {
265 if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
266 return FILE_CREATE;
267 else if ((flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
268 return FILE_OVERWRITE_IF;
269 else if ((flags & O_CREAT) == O_CREAT)
270 return FILE_OPEN_IF;
271 else if ((flags & O_TRUNC) == O_TRUNC)
272 return FILE_OVERWRITE;
273 else
274 return FILE_OPEN;
275 }
276
277 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
cifs_posix_open(const char * full_path,struct inode ** pinode,struct super_block * sb,int mode,unsigned int f_flags,__u32 * poplock,__u16 * pnetfid,unsigned int xid)278 int cifs_posix_open(const char *full_path, struct inode **pinode,
279 struct super_block *sb, int mode, unsigned int f_flags,
280 __u32 *poplock, __u16 *pnetfid, unsigned int xid)
281 {
282 int rc;
283 FILE_UNIX_BASIC_INFO *presp_data;
284 __u32 posix_flags = 0;
285 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
286 struct cifs_fattr fattr;
287 struct tcon_link *tlink;
288 struct cifs_tcon *tcon;
289
290 cifs_dbg(FYI, "posix open %s\n", full_path);
291
292 presp_data = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
293 if (presp_data == NULL)
294 return -ENOMEM;
295
296 tlink = cifs_sb_tlink(cifs_sb);
297 if (IS_ERR(tlink)) {
298 rc = PTR_ERR(tlink);
299 goto posix_open_ret;
300 }
301
302 tcon = tlink_tcon(tlink);
303 mode &= ~current_umask();
304
305 posix_flags = cifs_posix_convert_flags(f_flags);
306 rc = CIFSPOSIXCreate(xid, tcon, posix_flags, mode, pnetfid, presp_data,
307 poplock, full_path, cifs_sb->local_nls,
308 cifs_remap(cifs_sb));
309 cifs_put_tlink(tlink);
310
311 if (rc)
312 goto posix_open_ret;
313
314 if (presp_data->Type == cpu_to_le32(-1))
315 goto posix_open_ret; /* open ok, caller does qpathinfo */
316
317 if (!pinode)
318 goto posix_open_ret; /* caller does not need info */
319
320 cifs_unix_basic_to_fattr(&fattr, presp_data, cifs_sb);
321
322 /* get new inode and set it up */
323 if (*pinode == NULL) {
324 cifs_fill_uniqueid(sb, &fattr);
325 *pinode = cifs_iget(sb, &fattr);
326 if (!*pinode) {
327 rc = -ENOMEM;
328 goto posix_open_ret;
329 }
330 } else {
331 cifs_revalidate_mapping(*pinode);
332 rc = cifs_fattr_to_inode(*pinode, &fattr, false);
333 }
334
335 posix_open_ret:
336 kfree(presp_data);
337 return rc;
338 }
339 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
340
cifs_nt_open(const char * full_path,struct inode * inode,struct cifs_sb_info * cifs_sb,struct cifs_tcon * tcon,unsigned int f_flags,__u32 * oplock,struct cifs_fid * fid,unsigned int xid,struct cifs_open_info_data * buf)341 static int cifs_nt_open(const char *full_path, struct inode *inode, struct cifs_sb_info *cifs_sb,
342 struct cifs_tcon *tcon, unsigned int f_flags, __u32 *oplock,
343 struct cifs_fid *fid, unsigned int xid, struct cifs_open_info_data *buf)
344 {
345 int rc;
346 int desired_access;
347 int disposition;
348 int create_options = CREATE_NOT_DIR;
349 struct TCP_Server_Info *server = tcon->ses->server;
350 struct cifs_open_parms oparms;
351 int rdwr_for_fscache = 0;
352
353 if (!server->ops->open)
354 return -ENOSYS;
355
356 /* If we're caching, we need to be able to fill in around partial writes. */
357 if (cifs_fscache_enabled(inode) && (f_flags & O_ACCMODE) == O_WRONLY)
358 rdwr_for_fscache = 1;
359
360 desired_access = cifs_convert_flags(f_flags, rdwr_for_fscache);
361
362 /*********************************************************************
363 * open flag mapping table:
364 *
365 * POSIX Flag CIFS Disposition
366 * ---------- ----------------
367 * O_CREAT FILE_OPEN_IF
368 * O_CREAT | O_EXCL FILE_CREATE
369 * O_CREAT | O_TRUNC FILE_OVERWRITE_IF
370 * O_TRUNC FILE_OVERWRITE
371 * none of the above FILE_OPEN
372 *
373 * Note that there is not a direct match between disposition
374 * FILE_SUPERSEDE (ie create whether or not file exists although
375 * O_CREAT | O_TRUNC is similar but truncates the existing
376 * file rather than creating a new file as FILE_SUPERSEDE does
377 * (which uses the attributes / metadata passed in on open call)
378 *?
379 *? O_SYNC is a reasonable match to CIFS writethrough flag
380 *? and the read write flags match reasonably. O_LARGEFILE
381 *? is irrelevant because largefile support is always used
382 *? by this client. Flags O_APPEND, O_DIRECT, O_DIRECTORY,
383 * O_FASYNC, O_NOFOLLOW, O_NONBLOCK need further investigation
384 *********************************************************************/
385
386 disposition = cifs_get_disposition(f_flags);
387
388 /* BB pass O_SYNC flag through on file attributes .. BB */
389
390 /* O_SYNC also has bit for O_DSYNC so following check picks up either */
391 if (f_flags & O_SYNC)
392 create_options |= CREATE_WRITE_THROUGH;
393
394 if (f_flags & O_DIRECT)
395 create_options |= CREATE_NO_BUFFER;
396
397 retry_open:
398 oparms = (struct cifs_open_parms) {
399 .tcon = tcon,
400 .cifs_sb = cifs_sb,
401 .desired_access = desired_access,
402 .create_options = cifs_create_options(cifs_sb, create_options),
403 .disposition = disposition,
404 .path = full_path,
405 .fid = fid,
406 };
407
408 rc = server->ops->open(xid, &oparms, oplock, buf);
409 if (rc) {
410 if (rc == -EACCES && rdwr_for_fscache == 1) {
411 desired_access = cifs_convert_flags(f_flags, 0);
412 rdwr_for_fscache = 2;
413 goto retry_open;
414 }
415 return rc;
416 }
417 if (rdwr_for_fscache == 2)
418 cifs_invalidate_cache(inode, FSCACHE_INVAL_DIO_WRITE);
419
420 /* TODO: Add support for calling posix query info but with passing in fid */
421 if (tcon->unix_ext)
422 rc = cifs_get_inode_info_unix(&inode, full_path, inode->i_sb,
423 xid);
424 else
425 rc = cifs_get_inode_info(&inode, full_path, buf, inode->i_sb,
426 xid, fid);
427
428 if (rc) {
429 server->ops->close(xid, tcon, fid);
430 if (rc == -ESTALE)
431 rc = -EOPENSTALE;
432 }
433
434 return rc;
435 }
436
437 static bool
cifs_has_mand_locks(struct cifsInodeInfo * cinode)438 cifs_has_mand_locks(struct cifsInodeInfo *cinode)
439 {
440 struct cifs_fid_locks *cur;
441 bool has_locks = false;
442
443 down_read(&cinode->lock_sem);
444 list_for_each_entry(cur, &cinode->llist, llist) {
445 if (!list_empty(&cur->locks)) {
446 has_locks = true;
447 break;
448 }
449 }
450 up_read(&cinode->lock_sem);
451 return has_locks;
452 }
453
454 void
cifs_down_write(struct rw_semaphore * sem)455 cifs_down_write(struct rw_semaphore *sem)
456 {
457 while (!down_write_trylock(sem))
458 msleep(10);
459 }
460
461 static void cifsFileInfo_put_work(struct work_struct *work);
462 void serverclose_work(struct work_struct *work);
463
cifs_new_fileinfo(struct cifs_fid * fid,struct file * file,struct tcon_link * tlink,__u32 oplock,const char * symlink_target)464 struct cifsFileInfo *cifs_new_fileinfo(struct cifs_fid *fid, struct file *file,
465 struct tcon_link *tlink, __u32 oplock,
466 const char *symlink_target)
467 {
468 struct dentry *dentry = file_dentry(file);
469 struct inode *inode = d_inode(dentry);
470 struct cifsInodeInfo *cinode = CIFS_I(inode);
471 struct cifsFileInfo *cfile;
472 struct cifs_fid_locks *fdlocks;
473 struct cifs_tcon *tcon = tlink_tcon(tlink);
474 struct TCP_Server_Info *server = tcon->ses->server;
475
476 cfile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
477 if (cfile == NULL)
478 return cfile;
479
480 fdlocks = kzalloc(sizeof(struct cifs_fid_locks), GFP_KERNEL);
481 if (!fdlocks) {
482 kfree(cfile);
483 return NULL;
484 }
485
486 if (symlink_target) {
487 cfile->symlink_target = kstrdup(symlink_target, GFP_KERNEL);
488 if (!cfile->symlink_target) {
489 kfree(fdlocks);
490 kfree(cfile);
491 return NULL;
492 }
493 }
494
495 INIT_LIST_HEAD(&fdlocks->locks);
496 fdlocks->cfile = cfile;
497 cfile->llist = fdlocks;
498
499 cfile->count = 1;
500 cfile->pid = current->tgid;
501 cfile->uid = current_fsuid();
502 cfile->dentry = dget(dentry);
503 cfile->f_flags = file->f_flags;
504 cfile->invalidHandle = false;
505 cfile->deferred_close_scheduled = false;
506 cfile->tlink = cifs_get_tlink(tlink);
507 INIT_WORK(&cfile->oplock_break, cifs_oplock_break);
508 INIT_WORK(&cfile->put, cifsFileInfo_put_work);
509 INIT_WORK(&cfile->serverclose, serverclose_work);
510 INIT_DELAYED_WORK(&cfile->deferred, smb2_deferred_work_close);
511 mutex_init(&cfile->fh_mutex);
512 spin_lock_init(&cfile->file_info_lock);
513
514 cifs_sb_active(inode->i_sb);
515
516 /*
517 * If the server returned a read oplock and we have mandatory brlocks,
518 * set oplock level to None.
519 */
520 if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) {
521 cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n");
522 oplock = 0;
523 }
524
525 cifs_down_write(&cinode->lock_sem);
526 list_add(&fdlocks->llist, &cinode->llist);
527 up_write(&cinode->lock_sem);
528
529 spin_lock(&tcon->open_file_lock);
530 if (fid->pending_open->oplock != CIFS_OPLOCK_NO_CHANGE && oplock)
531 oplock = fid->pending_open->oplock;
532 list_del(&fid->pending_open->olist);
533
534 fid->purge_cache = false;
535 server->ops->set_fid(cfile, fid, oplock);
536
537 list_add(&cfile->tlist, &tcon->openFileList);
538 atomic_inc(&tcon->num_local_opens);
539
540 /* if readable file instance put first in list*/
541 spin_lock(&cinode->open_file_lock);
542 if (file->f_mode & FMODE_READ)
543 list_add(&cfile->flist, &cinode->openFileList);
544 else
545 list_add_tail(&cfile->flist, &cinode->openFileList);
546 spin_unlock(&cinode->open_file_lock);
547 spin_unlock(&tcon->open_file_lock);
548
549 if (fid->purge_cache)
550 cifs_zap_mapping(inode);
551
552 file->private_data = cfile;
553 return cfile;
554 }
555
556 struct cifsFileInfo *
cifsFileInfo_get(struct cifsFileInfo * cifs_file)557 cifsFileInfo_get(struct cifsFileInfo *cifs_file)
558 {
559 spin_lock(&cifs_file->file_info_lock);
560 cifsFileInfo_get_locked(cifs_file);
561 spin_unlock(&cifs_file->file_info_lock);
562 return cifs_file;
563 }
564
cifsFileInfo_put_final(struct cifsFileInfo * cifs_file)565 static void cifsFileInfo_put_final(struct cifsFileInfo *cifs_file)
566 {
567 struct inode *inode = d_inode(cifs_file->dentry);
568 struct cifsInodeInfo *cifsi = CIFS_I(inode);
569 struct cifsLockInfo *li, *tmp;
570 struct super_block *sb = inode->i_sb;
571
572 /*
573 * Delete any outstanding lock records. We'll lose them when the file
574 * is closed anyway.
575 */
576 cifs_down_write(&cifsi->lock_sem);
577 list_for_each_entry_safe(li, tmp, &cifs_file->llist->locks, llist) {
578 list_del(&li->llist);
579 cifs_del_lock_waiters(li);
580 kfree(li);
581 }
582 list_del(&cifs_file->llist->llist);
583 kfree(cifs_file->llist);
584 up_write(&cifsi->lock_sem);
585
586 cifs_put_tlink(cifs_file->tlink);
587 dput(cifs_file->dentry);
588 cifs_sb_deactive(sb);
589 kfree(cifs_file->symlink_target);
590 kfree(cifs_file);
591 }
592
cifsFileInfo_put_work(struct work_struct * work)593 static void cifsFileInfo_put_work(struct work_struct *work)
594 {
595 struct cifsFileInfo *cifs_file = container_of(work,
596 struct cifsFileInfo, put);
597
598 cifsFileInfo_put_final(cifs_file);
599 }
600
serverclose_work(struct work_struct * work)601 void serverclose_work(struct work_struct *work)
602 {
603 struct cifsFileInfo *cifs_file = container_of(work,
604 struct cifsFileInfo, serverclose);
605
606 struct cifs_tcon *tcon = tlink_tcon(cifs_file->tlink);
607
608 struct TCP_Server_Info *server = tcon->ses->server;
609 int rc = 0;
610 int retries = 0;
611 int MAX_RETRIES = 4;
612
613 do {
614 if (server->ops->close_getattr)
615 rc = server->ops->close_getattr(0, tcon, cifs_file);
616 else if (server->ops->close)
617 rc = server->ops->close(0, tcon, &cifs_file->fid);
618
619 if (rc == -EBUSY || rc == -EAGAIN) {
620 retries++;
621 msleep(250);
622 }
623 } while ((rc == -EBUSY || rc == -EAGAIN) && (retries < MAX_RETRIES)
624 );
625
626 if (retries == MAX_RETRIES)
627 pr_warn("Serverclose failed %d times, giving up\n", MAX_RETRIES);
628
629 if (cifs_file->offload)
630 queue_work(fileinfo_put_wq, &cifs_file->put);
631 else
632 cifsFileInfo_put_final(cifs_file);
633 }
634
635 /**
636 * cifsFileInfo_put - release a reference of file priv data
637 *
638 * Always potentially wait for oplock handler. See _cifsFileInfo_put().
639 *
640 * @cifs_file: cifs/smb3 specific info (eg refcounts) for an open file
641 */
cifsFileInfo_put(struct cifsFileInfo * cifs_file)642 void cifsFileInfo_put(struct cifsFileInfo *cifs_file)
643 {
644 _cifsFileInfo_put(cifs_file, true, true);
645 }
646
647 /**
648 * _cifsFileInfo_put - release a reference of file priv data
649 *
650 * This may involve closing the filehandle @cifs_file out on the
651 * server. Must be called without holding tcon->open_file_lock,
652 * cinode->open_file_lock and cifs_file->file_info_lock.
653 *
654 * If @wait_for_oplock_handler is true and we are releasing the last
655 * reference, wait for any running oplock break handler of the file
656 * and cancel any pending one.
657 *
658 * @cifs_file: cifs/smb3 specific info (eg refcounts) for an open file
659 * @wait_oplock_handler: must be false if called from oplock_break_handler
660 * @offload: not offloaded on close and oplock breaks
661 *
662 */
_cifsFileInfo_put(struct cifsFileInfo * cifs_file,bool wait_oplock_handler,bool offload)663 void _cifsFileInfo_put(struct cifsFileInfo *cifs_file,
664 bool wait_oplock_handler, bool offload)
665 {
666 struct inode *inode = d_inode(cifs_file->dentry);
667 struct cifs_tcon *tcon = tlink_tcon(cifs_file->tlink);
668 struct TCP_Server_Info *server = tcon->ses->server;
669 struct cifsInodeInfo *cifsi = CIFS_I(inode);
670 struct super_block *sb = inode->i_sb;
671 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
672 struct cifs_fid fid = {};
673 struct cifs_pending_open open;
674 bool oplock_break_cancelled;
675 bool serverclose_offloaded = false;
676
677 spin_lock(&tcon->open_file_lock);
678 spin_lock(&cifsi->open_file_lock);
679 spin_lock(&cifs_file->file_info_lock);
680
681 cifs_file->offload = offload;
682 if (--cifs_file->count > 0) {
683 spin_unlock(&cifs_file->file_info_lock);
684 spin_unlock(&cifsi->open_file_lock);
685 spin_unlock(&tcon->open_file_lock);
686 return;
687 }
688 spin_unlock(&cifs_file->file_info_lock);
689
690 if (server->ops->get_lease_key)
691 server->ops->get_lease_key(inode, &fid);
692
693 /* store open in pending opens to make sure we don't miss lease break */
694 cifs_add_pending_open_locked(&fid, cifs_file->tlink, &open);
695
696 /* remove it from the lists */
697 list_del(&cifs_file->flist);
698 list_del(&cifs_file->tlist);
699 atomic_dec(&tcon->num_local_opens);
700
701 if (list_empty(&cifsi->openFileList)) {
702 cifs_dbg(FYI, "closing last open instance for inode %p\n",
703 d_inode(cifs_file->dentry));
704 /*
705 * In strict cache mode we need invalidate mapping on the last
706 * close because it may cause a error when we open this file
707 * again and get at least level II oplock.
708 */
709 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)
710 set_bit(CIFS_INO_INVALID_MAPPING, &cifsi->flags);
711 cifs_set_oplock_level(cifsi, 0);
712 }
713
714 spin_unlock(&cifsi->open_file_lock);
715 spin_unlock(&tcon->open_file_lock);
716
717 oplock_break_cancelled = wait_oplock_handler ?
718 cancel_work_sync(&cifs_file->oplock_break) : false;
719
720 if (!tcon->need_reconnect && !cifs_file->invalidHandle) {
721 struct TCP_Server_Info *server = tcon->ses->server;
722 unsigned int xid;
723 int rc = 0;
724
725 xid = get_xid();
726 if (server->ops->close_getattr)
727 rc = server->ops->close_getattr(xid, tcon, cifs_file);
728 else if (server->ops->close)
729 rc = server->ops->close(xid, tcon, &cifs_file->fid);
730 _free_xid(xid);
731
732 if (rc == -EBUSY || rc == -EAGAIN) {
733 // Server close failed, hence offloading it as an async op
734 queue_work(serverclose_wq, &cifs_file->serverclose);
735 serverclose_offloaded = true;
736 }
737 }
738
739 if (oplock_break_cancelled)
740 cifs_done_oplock_break(cifsi);
741
742 cifs_del_pending_open(&open);
743
744 // if serverclose has been offloaded to wq (on failure), it will
745 // handle offloading put as well. If serverclose not offloaded,
746 // we need to handle offloading put here.
747 if (!serverclose_offloaded) {
748 if (offload)
749 queue_work(fileinfo_put_wq, &cifs_file->put);
750 else
751 cifsFileInfo_put_final(cifs_file);
752 }
753 }
754
cifs_open(struct inode * inode,struct file * file)755 int cifs_open(struct inode *inode, struct file *file)
756
757 {
758 int rc = -EACCES;
759 unsigned int xid;
760 __u32 oplock;
761 struct cifs_sb_info *cifs_sb;
762 struct TCP_Server_Info *server;
763 struct cifs_tcon *tcon;
764 struct tcon_link *tlink;
765 struct cifsFileInfo *cfile = NULL;
766 void *page;
767 const char *full_path;
768 bool posix_open_ok = false;
769 struct cifs_fid fid = {};
770 struct cifs_pending_open open;
771 struct cifs_open_info_data data = {};
772
773 xid = get_xid();
774
775 cifs_sb = CIFS_SB(inode->i_sb);
776 if (unlikely(cifs_forced_shutdown(cifs_sb))) {
777 free_xid(xid);
778 return -EIO;
779 }
780
781 tlink = cifs_sb_tlink(cifs_sb);
782 if (IS_ERR(tlink)) {
783 free_xid(xid);
784 return PTR_ERR(tlink);
785 }
786 tcon = tlink_tcon(tlink);
787 server = tcon->ses->server;
788
789 page = alloc_dentry_path();
790 full_path = build_path_from_dentry(file_dentry(file), page);
791 if (IS_ERR(full_path)) {
792 rc = PTR_ERR(full_path);
793 goto out;
794 }
795
796 cifs_dbg(FYI, "inode = 0x%p file flags are 0x%x for %s\n",
797 inode, file->f_flags, full_path);
798
799 if (file->f_flags & O_DIRECT &&
800 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
801 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
802 file->f_op = &cifs_file_direct_nobrl_ops;
803 else
804 file->f_op = &cifs_file_direct_ops;
805 }
806
807 /* Get the cached handle as SMB2 close is deferred */
808 rc = cifs_get_readable_path(tcon, full_path, &cfile);
809 if (rc == 0) {
810 if (file->f_flags == cfile->f_flags) {
811 file->private_data = cfile;
812 spin_lock(&CIFS_I(inode)->deferred_lock);
813 cifs_del_deferred_close(cfile);
814 spin_unlock(&CIFS_I(inode)->deferred_lock);
815 goto use_cache;
816 } else {
817 _cifsFileInfo_put(cfile, true, false);
818 }
819 } else {
820 /* hard link on the defeered close file */
821 rc = cifs_get_hardlink_path(tcon, inode, file);
822 if (rc)
823 cifs_close_deferred_file(CIFS_I(inode));
824 }
825
826 if (server->oplocks)
827 oplock = REQ_OPLOCK;
828 else
829 oplock = 0;
830
831 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
832 if (!tcon->broken_posix_open && tcon->unix_ext &&
833 cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
834 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
835 /* can not refresh inode info since size could be stale */
836 rc = cifs_posix_open(full_path, &inode, inode->i_sb,
837 cifs_sb->ctx->file_mode /* ignored */,
838 file->f_flags, &oplock, &fid.netfid, xid);
839 if (rc == 0) {
840 cifs_dbg(FYI, "posix open succeeded\n");
841 posix_open_ok = true;
842 } else if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
843 if (tcon->ses->serverNOS)
844 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",
845 tcon->ses->ip_addr,
846 tcon->ses->serverNOS);
847 tcon->broken_posix_open = true;
848 } else if ((rc != -EIO) && (rc != -EREMOTE) &&
849 (rc != -EOPNOTSUPP)) /* path not found or net err */
850 goto out;
851 /*
852 * Else fallthrough to retry open the old way on network i/o
853 * or DFS errors.
854 */
855 }
856 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
857
858 if (server->ops->get_lease_key)
859 server->ops->get_lease_key(inode, &fid);
860
861 cifs_add_pending_open(&fid, tlink, &open);
862
863 if (!posix_open_ok) {
864 if (server->ops->get_lease_key)
865 server->ops->get_lease_key(inode, &fid);
866
867 rc = cifs_nt_open(full_path, inode, cifs_sb, tcon, file->f_flags, &oplock, &fid,
868 xid, &data);
869 if (rc) {
870 cifs_del_pending_open(&open);
871 goto out;
872 }
873 }
874
875 cfile = cifs_new_fileinfo(&fid, file, tlink, oplock, data.symlink_target);
876 if (cfile == NULL) {
877 if (server->ops->close)
878 server->ops->close(xid, tcon, &fid);
879 cifs_del_pending_open(&open);
880 rc = -ENOMEM;
881 goto out;
882 }
883
884 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
885 if ((oplock & CIFS_CREATE_ACTION) && !posix_open_ok && tcon->unix_ext) {
886 /*
887 * Time to set mode which we can not set earlier due to
888 * problems creating new read-only files.
889 */
890 struct cifs_unix_set_info_args args = {
891 .mode = inode->i_mode,
892 .uid = INVALID_UID, /* no change */
893 .gid = INVALID_GID, /* no change */
894 .ctime = NO_CHANGE_64,
895 .atime = NO_CHANGE_64,
896 .mtime = NO_CHANGE_64,
897 .device = 0,
898 };
899 CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid.netfid,
900 cfile->pid);
901 }
902 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
903
904 use_cache:
905 fscache_use_cookie(cifs_inode_cookie(file_inode(file)),
906 file->f_mode & FMODE_WRITE);
907 if (!(file->f_flags & O_DIRECT))
908 goto out;
909 if ((file->f_flags & (O_ACCMODE | O_APPEND)) == O_RDONLY)
910 goto out;
911 cifs_invalidate_cache(file_inode(file), FSCACHE_INVAL_DIO_WRITE);
912
913 out:
914 free_dentry_path(page);
915 free_xid(xid);
916 cifs_put_tlink(tlink);
917 cifs_free_open_info(&data);
918 return rc;
919 }
920
921 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
922 static int cifs_push_posix_locks(struct cifsFileInfo *cfile);
923 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
924
925 /*
926 * Try to reacquire byte range locks that were released when session
927 * to server was lost.
928 */
929 static int
cifs_relock_file(struct cifsFileInfo * cfile)930 cifs_relock_file(struct cifsFileInfo *cfile)
931 {
932 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
933 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
934 int rc = 0;
935 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
936 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
937 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
938
939 down_read_nested(&cinode->lock_sem, SINGLE_DEPTH_NESTING);
940 if (cinode->can_cache_brlcks) {
941 /* can cache locks - no need to relock */
942 up_read(&cinode->lock_sem);
943 return rc;
944 }
945
946 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
947 if (cap_unix(tcon->ses) &&
948 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
949 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
950 rc = cifs_push_posix_locks(cfile);
951 else
952 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
953 rc = tcon->ses->server->ops->push_mand_locks(cfile);
954
955 up_read(&cinode->lock_sem);
956 return rc;
957 }
958
959 static int
cifs_reopen_file(struct cifsFileInfo * cfile,bool can_flush)960 cifs_reopen_file(struct cifsFileInfo *cfile, bool can_flush)
961 {
962 int rc = -EACCES;
963 unsigned int xid;
964 __u32 oplock;
965 struct cifs_sb_info *cifs_sb;
966 struct cifs_tcon *tcon;
967 struct TCP_Server_Info *server;
968 struct cifsInodeInfo *cinode;
969 struct inode *inode;
970 void *page;
971 const char *full_path;
972 int desired_access;
973 int disposition = FILE_OPEN;
974 int create_options = CREATE_NOT_DIR;
975 struct cifs_open_parms oparms;
976 int rdwr_for_fscache = 0;
977
978 xid = get_xid();
979 mutex_lock(&cfile->fh_mutex);
980 if (!cfile->invalidHandle) {
981 mutex_unlock(&cfile->fh_mutex);
982 free_xid(xid);
983 return 0;
984 }
985
986 inode = d_inode(cfile->dentry);
987 cifs_sb = CIFS_SB(inode->i_sb);
988 tcon = tlink_tcon(cfile->tlink);
989 server = tcon->ses->server;
990
991 /*
992 * Can not grab rename sem here because various ops, including those
993 * that already have the rename sem can end up causing writepage to get
994 * called and if the server was down that means we end up here, and we
995 * can never tell if the caller already has the rename_sem.
996 */
997 page = alloc_dentry_path();
998 full_path = build_path_from_dentry(cfile->dentry, page);
999 if (IS_ERR(full_path)) {
1000 mutex_unlock(&cfile->fh_mutex);
1001 free_dentry_path(page);
1002 free_xid(xid);
1003 return PTR_ERR(full_path);
1004 }
1005
1006 cifs_dbg(FYI, "inode = 0x%p file flags 0x%x for %s\n",
1007 inode, cfile->f_flags, full_path);
1008
1009 if (tcon->ses->server->oplocks)
1010 oplock = REQ_OPLOCK;
1011 else
1012 oplock = 0;
1013
1014 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1015 if (tcon->unix_ext && cap_unix(tcon->ses) &&
1016 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1017 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
1018 /*
1019 * O_CREAT, O_EXCL and O_TRUNC already had their effect on the
1020 * original open. Must mask them off for a reopen.
1021 */
1022 unsigned int oflags = cfile->f_flags &
1023 ~(O_CREAT | O_EXCL | O_TRUNC);
1024
1025 rc = cifs_posix_open(full_path, NULL, inode->i_sb,
1026 cifs_sb->ctx->file_mode /* ignored */,
1027 oflags, &oplock, &cfile->fid.netfid, xid);
1028 if (rc == 0) {
1029 cifs_dbg(FYI, "posix reopen succeeded\n");
1030 oparms.reconnect = true;
1031 goto reopen_success;
1032 }
1033 /*
1034 * fallthrough to retry open the old way on errors, especially
1035 * in the reconnect path it is important to retry hard
1036 */
1037 }
1038 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1039
1040 /* If we're caching, we need to be able to fill in around partial writes. */
1041 if (cifs_fscache_enabled(inode) && (cfile->f_flags & O_ACCMODE) == O_WRONLY)
1042 rdwr_for_fscache = 1;
1043
1044 desired_access = cifs_convert_flags(cfile->f_flags, rdwr_for_fscache);
1045
1046 /* O_SYNC also has bit for O_DSYNC so following check picks up either */
1047 if (cfile->f_flags & O_SYNC)
1048 create_options |= CREATE_WRITE_THROUGH;
1049
1050 if (cfile->f_flags & O_DIRECT)
1051 create_options |= CREATE_NO_BUFFER;
1052
1053 if (server->ops->get_lease_key)
1054 server->ops->get_lease_key(inode, &cfile->fid);
1055
1056 retry_open:
1057 oparms = (struct cifs_open_parms) {
1058 .tcon = tcon,
1059 .cifs_sb = cifs_sb,
1060 .desired_access = desired_access,
1061 .create_options = cifs_create_options(cifs_sb, create_options),
1062 .disposition = disposition,
1063 .path = full_path,
1064 .fid = &cfile->fid,
1065 .reconnect = true,
1066 };
1067
1068 /*
1069 * Can not refresh inode by passing in file_info buf to be returned by
1070 * ops->open and then calling get_inode_info with returned buf since
1071 * file might have write behind data that needs to be flushed and server
1072 * version of file size can be stale. If we knew for sure that inode was
1073 * not dirty locally we could do this.
1074 */
1075 rc = server->ops->open(xid, &oparms, &oplock, NULL);
1076 if (rc == -ENOENT && oparms.reconnect == false) {
1077 /* durable handle timeout is expired - open the file again */
1078 rc = server->ops->open(xid, &oparms, &oplock, NULL);
1079 /* indicate that we need to relock the file */
1080 oparms.reconnect = true;
1081 }
1082 if (rc == -EACCES && rdwr_for_fscache == 1) {
1083 desired_access = cifs_convert_flags(cfile->f_flags, 0);
1084 rdwr_for_fscache = 2;
1085 goto retry_open;
1086 }
1087
1088 if (rc) {
1089 mutex_unlock(&cfile->fh_mutex);
1090 cifs_dbg(FYI, "cifs_reopen returned 0x%x\n", rc);
1091 cifs_dbg(FYI, "oplock: %d\n", oplock);
1092 goto reopen_error_exit;
1093 }
1094
1095 if (rdwr_for_fscache == 2)
1096 cifs_invalidate_cache(inode, FSCACHE_INVAL_DIO_WRITE);
1097
1098 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1099 reopen_success:
1100 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1101 cfile->invalidHandle = false;
1102 mutex_unlock(&cfile->fh_mutex);
1103 cinode = CIFS_I(inode);
1104
1105 if (can_flush) {
1106 rc = filemap_write_and_wait(inode->i_mapping);
1107 if (!is_interrupt_error(rc))
1108 mapping_set_error(inode->i_mapping, rc);
1109
1110 if (tcon->posix_extensions) {
1111 rc = smb311_posix_get_inode_info(&inode, full_path,
1112 NULL, inode->i_sb, xid);
1113 } else if (tcon->unix_ext) {
1114 rc = cifs_get_inode_info_unix(&inode, full_path,
1115 inode->i_sb, xid);
1116 } else {
1117 rc = cifs_get_inode_info(&inode, full_path, NULL,
1118 inode->i_sb, xid, NULL);
1119 }
1120 }
1121 /*
1122 * Else we are writing out data to server already and could deadlock if
1123 * we tried to flush data, and since we do not know if we have data that
1124 * would invalidate the current end of file on the server we can not go
1125 * to the server to get the new inode info.
1126 */
1127
1128 /*
1129 * If the server returned a read oplock and we have mandatory brlocks,
1130 * set oplock level to None.
1131 */
1132 if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) {
1133 cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n");
1134 oplock = 0;
1135 }
1136
1137 server->ops->set_fid(cfile, &cfile->fid, oplock);
1138 if (oparms.reconnect)
1139 cifs_relock_file(cfile);
1140
1141 reopen_error_exit:
1142 free_dentry_path(page);
1143 free_xid(xid);
1144 return rc;
1145 }
1146
smb2_deferred_work_close(struct work_struct * work)1147 void smb2_deferred_work_close(struct work_struct *work)
1148 {
1149 struct cifsFileInfo *cfile = container_of(work,
1150 struct cifsFileInfo, deferred.work);
1151
1152 spin_lock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
1153 cifs_del_deferred_close(cfile);
1154 cfile->deferred_close_scheduled = false;
1155 spin_unlock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
1156 _cifsFileInfo_put(cfile, true, false);
1157 }
1158
1159 static bool
smb2_can_defer_close(struct inode * inode,struct cifs_deferred_close * dclose)1160 smb2_can_defer_close(struct inode *inode, struct cifs_deferred_close *dclose)
1161 {
1162 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1163 struct cifsInodeInfo *cinode = CIFS_I(inode);
1164
1165 return (cifs_sb->ctx->closetimeo && cinode->lease_granted && dclose &&
1166 (cinode->oplock == CIFS_CACHE_RHW_FLG ||
1167 cinode->oplock == CIFS_CACHE_RH_FLG) &&
1168 !test_bit(CIFS_INO_CLOSE_ON_LOCK, &cinode->flags));
1169
1170 }
1171
cifs_close(struct inode * inode,struct file * file)1172 int cifs_close(struct inode *inode, struct file *file)
1173 {
1174 struct cifsFileInfo *cfile;
1175 struct cifsInodeInfo *cinode = CIFS_I(inode);
1176 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1177 struct cifs_deferred_close *dclose;
1178
1179 cifs_fscache_unuse_inode_cookie(inode, file->f_mode & FMODE_WRITE);
1180
1181 if (file->private_data != NULL) {
1182 cfile = file->private_data;
1183 file->private_data = NULL;
1184 dclose = kmalloc(sizeof(struct cifs_deferred_close), GFP_KERNEL);
1185 if ((cfile->status_file_deleted == false) &&
1186 (smb2_can_defer_close(inode, dclose))) {
1187 if (test_and_clear_bit(CIFS_INO_MODIFIED_ATTR, &cinode->flags)) {
1188 inode_set_mtime_to_ts(inode,
1189 inode_set_ctime_current(inode));
1190 }
1191 spin_lock(&cinode->deferred_lock);
1192 cifs_add_deferred_close(cfile, dclose);
1193 if (cfile->deferred_close_scheduled &&
1194 delayed_work_pending(&cfile->deferred)) {
1195 /*
1196 * If there is no pending work, mod_delayed_work queues new work.
1197 * So, Increase the ref count to avoid use-after-free.
1198 */
1199 if (!mod_delayed_work(deferredclose_wq,
1200 &cfile->deferred, cifs_sb->ctx->closetimeo))
1201 cifsFileInfo_get(cfile);
1202 } else {
1203 /* Deferred close for files */
1204 queue_delayed_work(deferredclose_wq,
1205 &cfile->deferred, cifs_sb->ctx->closetimeo);
1206 cfile->deferred_close_scheduled = true;
1207 spin_unlock(&cinode->deferred_lock);
1208 return 0;
1209 }
1210 spin_unlock(&cinode->deferred_lock);
1211 _cifsFileInfo_put(cfile, true, false);
1212 } else {
1213 _cifsFileInfo_put(cfile, true, false);
1214 kfree(dclose);
1215 }
1216 }
1217
1218 /* return code from the ->release op is always ignored */
1219 return 0;
1220 }
1221
1222 void
cifs_reopen_persistent_handles(struct cifs_tcon * tcon)1223 cifs_reopen_persistent_handles(struct cifs_tcon *tcon)
1224 {
1225 struct cifsFileInfo *open_file, *tmp;
1226 struct list_head tmp_list;
1227
1228 if (!tcon->use_persistent || !tcon->need_reopen_files)
1229 return;
1230
1231 tcon->need_reopen_files = false;
1232
1233 cifs_dbg(FYI, "Reopen persistent handles\n");
1234 INIT_LIST_HEAD(&tmp_list);
1235
1236 /* list all files open on tree connection, reopen resilient handles */
1237 spin_lock(&tcon->open_file_lock);
1238 list_for_each_entry(open_file, &tcon->openFileList, tlist) {
1239 if (!open_file->invalidHandle)
1240 continue;
1241 cifsFileInfo_get(open_file);
1242 list_add_tail(&open_file->rlist, &tmp_list);
1243 }
1244 spin_unlock(&tcon->open_file_lock);
1245
1246 list_for_each_entry_safe(open_file, tmp, &tmp_list, rlist) {
1247 if (cifs_reopen_file(open_file, false /* do not flush */))
1248 tcon->need_reopen_files = true;
1249 list_del_init(&open_file->rlist);
1250 cifsFileInfo_put(open_file);
1251 }
1252 }
1253
cifs_closedir(struct inode * inode,struct file * file)1254 int cifs_closedir(struct inode *inode, struct file *file)
1255 {
1256 int rc = 0;
1257 unsigned int xid;
1258 struct cifsFileInfo *cfile = file->private_data;
1259 struct cifs_tcon *tcon;
1260 struct TCP_Server_Info *server;
1261 char *buf;
1262
1263 cifs_dbg(FYI, "Closedir inode = 0x%p\n", inode);
1264
1265 if (cfile == NULL)
1266 return rc;
1267
1268 xid = get_xid();
1269 tcon = tlink_tcon(cfile->tlink);
1270 server = tcon->ses->server;
1271
1272 cifs_dbg(FYI, "Freeing private data in close dir\n");
1273 spin_lock(&cfile->file_info_lock);
1274 if (server->ops->dir_needs_close(cfile)) {
1275 cfile->invalidHandle = true;
1276 spin_unlock(&cfile->file_info_lock);
1277 if (server->ops->close_dir)
1278 rc = server->ops->close_dir(xid, tcon, &cfile->fid);
1279 else
1280 rc = -ENOSYS;
1281 cifs_dbg(FYI, "Closing uncompleted readdir with rc %d\n", rc);
1282 /* not much we can do if it fails anyway, ignore rc */
1283 rc = 0;
1284 } else
1285 spin_unlock(&cfile->file_info_lock);
1286
1287 buf = cfile->srch_inf.ntwrk_buf_start;
1288 if (buf) {
1289 cifs_dbg(FYI, "closedir free smb buf in srch struct\n");
1290 cfile->srch_inf.ntwrk_buf_start = NULL;
1291 if (cfile->srch_inf.smallBuf)
1292 cifs_small_buf_release(buf);
1293 else
1294 cifs_buf_release(buf);
1295 }
1296
1297 cifs_put_tlink(cfile->tlink);
1298 kfree(file->private_data);
1299 file->private_data = NULL;
1300 /* BB can we lock the filestruct while this is going on? */
1301 free_xid(xid);
1302 return rc;
1303 }
1304
1305 static struct cifsLockInfo *
cifs_lock_init(__u64 offset,__u64 length,__u8 type,__u16 flags)1306 cifs_lock_init(__u64 offset, __u64 length, __u8 type, __u16 flags)
1307 {
1308 struct cifsLockInfo *lock =
1309 kmalloc(sizeof(struct cifsLockInfo), GFP_KERNEL);
1310 if (!lock)
1311 return lock;
1312 lock->offset = offset;
1313 lock->length = length;
1314 lock->type = type;
1315 lock->pid = current->tgid;
1316 lock->flags = flags;
1317 INIT_LIST_HEAD(&lock->blist);
1318 init_waitqueue_head(&lock->block_q);
1319 return lock;
1320 }
1321
1322 void
cifs_del_lock_waiters(struct cifsLockInfo * lock)1323 cifs_del_lock_waiters(struct cifsLockInfo *lock)
1324 {
1325 struct cifsLockInfo *li, *tmp;
1326 list_for_each_entry_safe(li, tmp, &lock->blist, blist) {
1327 list_del_init(&li->blist);
1328 wake_up(&li->block_q);
1329 }
1330 }
1331
1332 #define CIFS_LOCK_OP 0
1333 #define CIFS_READ_OP 1
1334 #define CIFS_WRITE_OP 2
1335
1336 /* @rw_check : 0 - no op, 1 - read, 2 - write */
1337 static bool
cifs_find_fid_lock_conflict(struct cifs_fid_locks * fdlocks,__u64 offset,__u64 length,__u8 type,__u16 flags,struct cifsFileInfo * cfile,struct cifsLockInfo ** conf_lock,int rw_check)1338 cifs_find_fid_lock_conflict(struct cifs_fid_locks *fdlocks, __u64 offset,
1339 __u64 length, __u8 type, __u16 flags,
1340 struct cifsFileInfo *cfile,
1341 struct cifsLockInfo **conf_lock, int rw_check)
1342 {
1343 struct cifsLockInfo *li;
1344 struct cifsFileInfo *cur_cfile = fdlocks->cfile;
1345 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1346
1347 list_for_each_entry(li, &fdlocks->locks, llist) {
1348 if (offset + length <= li->offset ||
1349 offset >= li->offset + li->length)
1350 continue;
1351 if (rw_check != CIFS_LOCK_OP && current->tgid == li->pid &&
1352 server->ops->compare_fids(cfile, cur_cfile)) {
1353 /* shared lock prevents write op through the same fid */
1354 if (!(li->type & server->vals->shared_lock_type) ||
1355 rw_check != CIFS_WRITE_OP)
1356 continue;
1357 }
1358 if ((type & server->vals->shared_lock_type) &&
1359 ((server->ops->compare_fids(cfile, cur_cfile) &&
1360 current->tgid == li->pid) || type == li->type))
1361 continue;
1362 if (rw_check == CIFS_LOCK_OP &&
1363 (flags & FL_OFDLCK) && (li->flags & FL_OFDLCK) &&
1364 server->ops->compare_fids(cfile, cur_cfile))
1365 continue;
1366 if (conf_lock)
1367 *conf_lock = li;
1368 return true;
1369 }
1370 return false;
1371 }
1372
1373 bool
cifs_find_lock_conflict(struct cifsFileInfo * cfile,__u64 offset,__u64 length,__u8 type,__u16 flags,struct cifsLockInfo ** conf_lock,int rw_check)1374 cifs_find_lock_conflict(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
1375 __u8 type, __u16 flags,
1376 struct cifsLockInfo **conf_lock, int rw_check)
1377 {
1378 bool rc = false;
1379 struct cifs_fid_locks *cur;
1380 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1381
1382 list_for_each_entry(cur, &cinode->llist, llist) {
1383 rc = cifs_find_fid_lock_conflict(cur, offset, length, type,
1384 flags, cfile, conf_lock,
1385 rw_check);
1386 if (rc)
1387 break;
1388 }
1389
1390 return rc;
1391 }
1392
1393 /*
1394 * Check if there is another lock that prevents us to set the lock (mandatory
1395 * style). If such a lock exists, update the flock structure with its
1396 * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
1397 * or leave it the same if we can't. Returns 0 if we don't need to request to
1398 * the server or 1 otherwise.
1399 */
1400 static int
cifs_lock_test(struct cifsFileInfo * cfile,__u64 offset,__u64 length,__u8 type,struct file_lock * flock)1401 cifs_lock_test(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
1402 __u8 type, struct file_lock *flock)
1403 {
1404 int rc = 0;
1405 struct cifsLockInfo *conf_lock;
1406 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1407 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1408 bool exist;
1409
1410 down_read(&cinode->lock_sem);
1411
1412 exist = cifs_find_lock_conflict(cfile, offset, length, type,
1413 flock->fl_flags, &conf_lock,
1414 CIFS_LOCK_OP);
1415 if (exist) {
1416 flock->fl_start = conf_lock->offset;
1417 flock->fl_end = conf_lock->offset + conf_lock->length - 1;
1418 flock->fl_pid = conf_lock->pid;
1419 if (conf_lock->type & server->vals->shared_lock_type)
1420 flock->fl_type = F_RDLCK;
1421 else
1422 flock->fl_type = F_WRLCK;
1423 } else if (!cinode->can_cache_brlcks)
1424 rc = 1;
1425 else
1426 flock->fl_type = F_UNLCK;
1427
1428 up_read(&cinode->lock_sem);
1429 return rc;
1430 }
1431
1432 static void
cifs_lock_add(struct cifsFileInfo * cfile,struct cifsLockInfo * lock)1433 cifs_lock_add(struct cifsFileInfo *cfile, struct cifsLockInfo *lock)
1434 {
1435 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1436 cifs_down_write(&cinode->lock_sem);
1437 list_add_tail(&lock->llist, &cfile->llist->locks);
1438 up_write(&cinode->lock_sem);
1439 }
1440
1441 /*
1442 * Set the byte-range lock (mandatory style). Returns:
1443 * 1) 0, if we set the lock and don't need to request to the server;
1444 * 2) 1, if no locks prevent us but we need to request to the server;
1445 * 3) -EACCES, if there is a lock that prevents us and wait is false.
1446 */
1447 static int
cifs_lock_add_if(struct cifsFileInfo * cfile,struct cifsLockInfo * lock,bool wait)1448 cifs_lock_add_if(struct cifsFileInfo *cfile, struct cifsLockInfo *lock,
1449 bool wait)
1450 {
1451 struct cifsLockInfo *conf_lock;
1452 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1453 bool exist;
1454 int rc = 0;
1455
1456 try_again:
1457 exist = false;
1458 cifs_down_write(&cinode->lock_sem);
1459
1460 exist = cifs_find_lock_conflict(cfile, lock->offset, lock->length,
1461 lock->type, lock->flags, &conf_lock,
1462 CIFS_LOCK_OP);
1463 if (!exist && cinode->can_cache_brlcks) {
1464 list_add_tail(&lock->llist, &cfile->llist->locks);
1465 up_write(&cinode->lock_sem);
1466 return rc;
1467 }
1468
1469 if (!exist)
1470 rc = 1;
1471 else if (!wait)
1472 rc = -EACCES;
1473 else {
1474 list_add_tail(&lock->blist, &conf_lock->blist);
1475 up_write(&cinode->lock_sem);
1476 rc = wait_event_interruptible(lock->block_q,
1477 (lock->blist.prev == &lock->blist) &&
1478 (lock->blist.next == &lock->blist));
1479 if (!rc)
1480 goto try_again;
1481 cifs_down_write(&cinode->lock_sem);
1482 list_del_init(&lock->blist);
1483 }
1484
1485 up_write(&cinode->lock_sem);
1486 return rc;
1487 }
1488
1489 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1490 /*
1491 * Check if there is another lock that prevents us to set the lock (posix
1492 * style). If such a lock exists, update the flock structure with its
1493 * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
1494 * or leave it the same if we can't. Returns 0 if we don't need to request to
1495 * the server or 1 otherwise.
1496 */
1497 static int
cifs_posix_lock_test(struct file * file,struct file_lock * flock)1498 cifs_posix_lock_test(struct file *file, struct file_lock *flock)
1499 {
1500 int rc = 0;
1501 struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
1502 unsigned char saved_type = flock->fl_type;
1503
1504 if ((flock->fl_flags & FL_POSIX) == 0)
1505 return 1;
1506
1507 down_read(&cinode->lock_sem);
1508 posix_test_lock(file, flock);
1509
1510 if (flock->fl_type == F_UNLCK && !cinode->can_cache_brlcks) {
1511 flock->fl_type = saved_type;
1512 rc = 1;
1513 }
1514
1515 up_read(&cinode->lock_sem);
1516 return rc;
1517 }
1518
1519 /*
1520 * Set the byte-range lock (posix style). Returns:
1521 * 1) <0, if the error occurs while setting the lock;
1522 * 2) 0, if we set the lock and don't need to request to the server;
1523 * 3) FILE_LOCK_DEFERRED, if we will wait for some other file_lock;
1524 * 4) FILE_LOCK_DEFERRED + 1, if we need to request to the server.
1525 */
1526 static int
cifs_posix_lock_set(struct file * file,struct file_lock * flock)1527 cifs_posix_lock_set(struct file *file, struct file_lock *flock)
1528 {
1529 struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
1530 int rc = FILE_LOCK_DEFERRED + 1;
1531
1532 if ((flock->fl_flags & FL_POSIX) == 0)
1533 return rc;
1534
1535 cifs_down_write(&cinode->lock_sem);
1536 if (!cinode->can_cache_brlcks) {
1537 up_write(&cinode->lock_sem);
1538 return rc;
1539 }
1540
1541 rc = posix_lock_file(file, flock, NULL);
1542 up_write(&cinode->lock_sem);
1543 return rc;
1544 }
1545
1546 int
cifs_push_mandatory_locks(struct cifsFileInfo * cfile)1547 cifs_push_mandatory_locks(struct cifsFileInfo *cfile)
1548 {
1549 unsigned int xid;
1550 int rc = 0, stored_rc;
1551 struct cifsLockInfo *li, *tmp;
1552 struct cifs_tcon *tcon;
1553 unsigned int num, max_num, max_buf;
1554 LOCKING_ANDX_RANGE *buf, *cur;
1555 static const int types[] = {
1556 LOCKING_ANDX_LARGE_FILES,
1557 LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES
1558 };
1559 int i;
1560
1561 xid = get_xid();
1562 tcon = tlink_tcon(cfile->tlink);
1563
1564 /*
1565 * Accessing maxBuf is racy with cifs_reconnect - need to store value
1566 * and check it before using.
1567 */
1568 max_buf = tcon->ses->server->maxBuf;
1569 if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE))) {
1570 free_xid(xid);
1571 return -EINVAL;
1572 }
1573
1574 BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) >
1575 PAGE_SIZE);
1576 max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr),
1577 PAGE_SIZE);
1578 max_num = (max_buf - sizeof(struct smb_hdr)) /
1579 sizeof(LOCKING_ANDX_RANGE);
1580 buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
1581 if (!buf) {
1582 free_xid(xid);
1583 return -ENOMEM;
1584 }
1585
1586 for (i = 0; i < 2; i++) {
1587 cur = buf;
1588 num = 0;
1589 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
1590 if (li->type != types[i])
1591 continue;
1592 cur->Pid = cpu_to_le16(li->pid);
1593 cur->LengthLow = cpu_to_le32((u32)li->length);
1594 cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
1595 cur->OffsetLow = cpu_to_le32((u32)li->offset);
1596 cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
1597 if (++num == max_num) {
1598 stored_rc = cifs_lockv(xid, tcon,
1599 cfile->fid.netfid,
1600 (__u8)li->type, 0, num,
1601 buf);
1602 if (stored_rc)
1603 rc = stored_rc;
1604 cur = buf;
1605 num = 0;
1606 } else
1607 cur++;
1608 }
1609
1610 if (num) {
1611 stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
1612 (__u8)types[i], 0, num, buf);
1613 if (stored_rc)
1614 rc = stored_rc;
1615 }
1616 }
1617
1618 kfree(buf);
1619 free_xid(xid);
1620 return rc;
1621 }
1622
1623 static __u32
hash_lockowner(fl_owner_t owner)1624 hash_lockowner(fl_owner_t owner)
1625 {
1626 return cifs_lock_secret ^ hash32_ptr((const void *)owner);
1627 }
1628 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1629
1630 struct lock_to_push {
1631 struct list_head llist;
1632 __u64 offset;
1633 __u64 length;
1634 __u32 pid;
1635 __u16 netfid;
1636 __u8 type;
1637 };
1638
1639 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1640 static int
cifs_push_posix_locks(struct cifsFileInfo * cfile)1641 cifs_push_posix_locks(struct cifsFileInfo *cfile)
1642 {
1643 struct inode *inode = d_inode(cfile->dentry);
1644 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1645 struct file_lock *flock;
1646 struct file_lock_context *flctx = locks_inode_context(inode);
1647 unsigned int count = 0, i;
1648 int rc = 0, xid, type;
1649 struct list_head locks_to_send, *el;
1650 struct lock_to_push *lck, *tmp;
1651 __u64 length;
1652
1653 xid = get_xid();
1654
1655 if (!flctx)
1656 goto out;
1657
1658 spin_lock(&flctx->flc_lock);
1659 list_for_each(el, &flctx->flc_posix) {
1660 count++;
1661 }
1662 spin_unlock(&flctx->flc_lock);
1663
1664 INIT_LIST_HEAD(&locks_to_send);
1665
1666 /*
1667 * Allocating count locks is enough because no FL_POSIX locks can be
1668 * added to the list while we are holding cinode->lock_sem that
1669 * protects locking operations of this inode.
1670 */
1671 for (i = 0; i < count; i++) {
1672 lck = kmalloc(sizeof(struct lock_to_push), GFP_KERNEL);
1673 if (!lck) {
1674 rc = -ENOMEM;
1675 goto err_out;
1676 }
1677 list_add_tail(&lck->llist, &locks_to_send);
1678 }
1679
1680 el = locks_to_send.next;
1681 spin_lock(&flctx->flc_lock);
1682 list_for_each_entry(flock, &flctx->flc_posix, fl_list) {
1683 if (el == &locks_to_send) {
1684 /*
1685 * The list ended. We don't have enough allocated
1686 * structures - something is really wrong.
1687 */
1688 cifs_dbg(VFS, "Can't push all brlocks!\n");
1689 break;
1690 }
1691 length = cifs_flock_len(flock);
1692 if (flock->fl_type == F_RDLCK || flock->fl_type == F_SHLCK)
1693 type = CIFS_RDLCK;
1694 else
1695 type = CIFS_WRLCK;
1696 lck = list_entry(el, struct lock_to_push, llist);
1697 lck->pid = hash_lockowner(flock->fl_owner);
1698 lck->netfid = cfile->fid.netfid;
1699 lck->length = length;
1700 lck->type = type;
1701 lck->offset = flock->fl_start;
1702 }
1703 spin_unlock(&flctx->flc_lock);
1704
1705 list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
1706 int stored_rc;
1707
1708 stored_rc = CIFSSMBPosixLock(xid, tcon, lck->netfid, lck->pid,
1709 lck->offset, lck->length, NULL,
1710 lck->type, 0);
1711 if (stored_rc)
1712 rc = stored_rc;
1713 list_del(&lck->llist);
1714 kfree(lck);
1715 }
1716
1717 out:
1718 free_xid(xid);
1719 return rc;
1720 err_out:
1721 list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
1722 list_del(&lck->llist);
1723 kfree(lck);
1724 }
1725 goto out;
1726 }
1727 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1728
1729 static int
cifs_push_locks(struct cifsFileInfo * cfile)1730 cifs_push_locks(struct cifsFileInfo *cfile)
1731 {
1732 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1733 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1734 int rc = 0;
1735 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1736 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
1737 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1738
1739 /* we are going to update can_cache_brlcks here - need a write access */
1740 cifs_down_write(&cinode->lock_sem);
1741 if (!cinode->can_cache_brlcks) {
1742 up_write(&cinode->lock_sem);
1743 return rc;
1744 }
1745
1746 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1747 if (cap_unix(tcon->ses) &&
1748 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
1749 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
1750 rc = cifs_push_posix_locks(cfile);
1751 else
1752 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1753 rc = tcon->ses->server->ops->push_mand_locks(cfile);
1754
1755 cinode->can_cache_brlcks = false;
1756 up_write(&cinode->lock_sem);
1757 return rc;
1758 }
1759
1760 static void
cifs_read_flock(struct file_lock * flock,__u32 * type,int * lock,int * unlock,bool * wait_flag,struct TCP_Server_Info * server)1761 cifs_read_flock(struct file_lock *flock, __u32 *type, int *lock, int *unlock,
1762 bool *wait_flag, struct TCP_Server_Info *server)
1763 {
1764 if (flock->fl_flags & FL_POSIX)
1765 cifs_dbg(FYI, "Posix\n");
1766 if (flock->fl_flags & FL_FLOCK)
1767 cifs_dbg(FYI, "Flock\n");
1768 if (flock->fl_flags & FL_SLEEP) {
1769 cifs_dbg(FYI, "Blocking lock\n");
1770 *wait_flag = true;
1771 }
1772 if (flock->fl_flags & FL_ACCESS)
1773 cifs_dbg(FYI, "Process suspended by mandatory locking - not implemented yet\n");
1774 if (flock->fl_flags & FL_LEASE)
1775 cifs_dbg(FYI, "Lease on file - not implemented yet\n");
1776 if (flock->fl_flags &
1777 (~(FL_POSIX | FL_FLOCK | FL_SLEEP |
1778 FL_ACCESS | FL_LEASE | FL_CLOSE | FL_OFDLCK)))
1779 cifs_dbg(FYI, "Unknown lock flags 0x%x\n", flock->fl_flags);
1780
1781 *type = server->vals->large_lock_type;
1782 if (flock->fl_type == F_WRLCK) {
1783 cifs_dbg(FYI, "F_WRLCK\n");
1784 *type |= server->vals->exclusive_lock_type;
1785 *lock = 1;
1786 } else if (flock->fl_type == F_UNLCK) {
1787 cifs_dbg(FYI, "F_UNLCK\n");
1788 *type |= server->vals->unlock_lock_type;
1789 *unlock = 1;
1790 /* Check if unlock includes more than one lock range */
1791 } else if (flock->fl_type == F_RDLCK) {
1792 cifs_dbg(FYI, "F_RDLCK\n");
1793 *type |= server->vals->shared_lock_type;
1794 *lock = 1;
1795 } else if (flock->fl_type == F_EXLCK) {
1796 cifs_dbg(FYI, "F_EXLCK\n");
1797 *type |= server->vals->exclusive_lock_type;
1798 *lock = 1;
1799 } else if (flock->fl_type == F_SHLCK) {
1800 cifs_dbg(FYI, "F_SHLCK\n");
1801 *type |= server->vals->shared_lock_type;
1802 *lock = 1;
1803 } else
1804 cifs_dbg(FYI, "Unknown type of lock\n");
1805 }
1806
1807 static int
cifs_getlk(struct file * file,struct file_lock * flock,__u32 type,bool wait_flag,bool posix_lck,unsigned int xid)1808 cifs_getlk(struct file *file, struct file_lock *flock, __u32 type,
1809 bool wait_flag, bool posix_lck, unsigned int xid)
1810 {
1811 int rc = 0;
1812 __u64 length = cifs_flock_len(flock);
1813 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
1814 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1815 struct TCP_Server_Info *server = tcon->ses->server;
1816 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1817 __u16 netfid = cfile->fid.netfid;
1818
1819 if (posix_lck) {
1820 int posix_lock_type;
1821
1822 rc = cifs_posix_lock_test(file, flock);
1823 if (!rc)
1824 return rc;
1825
1826 if (type & server->vals->shared_lock_type)
1827 posix_lock_type = CIFS_RDLCK;
1828 else
1829 posix_lock_type = CIFS_WRLCK;
1830 rc = CIFSSMBPosixLock(xid, tcon, netfid,
1831 hash_lockowner(flock->fl_owner),
1832 flock->fl_start, length, flock,
1833 posix_lock_type, wait_flag);
1834 return rc;
1835 }
1836 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1837
1838 rc = cifs_lock_test(cfile, flock->fl_start, length, type, flock);
1839 if (!rc)
1840 return rc;
1841
1842 /* BB we could chain these into one lock request BB */
1843 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, type,
1844 1, 0, false);
1845 if (rc == 0) {
1846 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1847 type, 0, 1, false);
1848 flock->fl_type = F_UNLCK;
1849 if (rc != 0)
1850 cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
1851 rc);
1852 return 0;
1853 }
1854
1855 if (type & server->vals->shared_lock_type) {
1856 flock->fl_type = F_WRLCK;
1857 return 0;
1858 }
1859
1860 type &= ~server->vals->exclusive_lock_type;
1861
1862 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1863 type | server->vals->shared_lock_type,
1864 1, 0, false);
1865 if (rc == 0) {
1866 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1867 type | server->vals->shared_lock_type, 0, 1, false);
1868 flock->fl_type = F_RDLCK;
1869 if (rc != 0)
1870 cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
1871 rc);
1872 } else
1873 flock->fl_type = F_WRLCK;
1874
1875 return 0;
1876 }
1877
1878 void
cifs_move_llist(struct list_head * source,struct list_head * dest)1879 cifs_move_llist(struct list_head *source, struct list_head *dest)
1880 {
1881 struct list_head *li, *tmp;
1882 list_for_each_safe(li, tmp, source)
1883 list_move(li, dest);
1884 }
1885
1886 int
cifs_get_hardlink_path(struct cifs_tcon * tcon,struct inode * inode,struct file * file)1887 cifs_get_hardlink_path(struct cifs_tcon *tcon, struct inode *inode,
1888 struct file *file)
1889 {
1890 struct cifsFileInfo *open_file = NULL;
1891 struct cifsInodeInfo *cinode = CIFS_I(inode);
1892 int rc = 0;
1893
1894 spin_lock(&tcon->open_file_lock);
1895 spin_lock(&cinode->open_file_lock);
1896
1897 list_for_each_entry(open_file, &cinode->openFileList, flist) {
1898 if (file->f_flags == open_file->f_flags) {
1899 rc = -EINVAL;
1900 break;
1901 }
1902 }
1903
1904 spin_unlock(&cinode->open_file_lock);
1905 spin_unlock(&tcon->open_file_lock);
1906 return rc;
1907 }
1908
1909 void
cifs_free_llist(struct list_head * llist)1910 cifs_free_llist(struct list_head *llist)
1911 {
1912 struct cifsLockInfo *li, *tmp;
1913 list_for_each_entry_safe(li, tmp, llist, llist) {
1914 cifs_del_lock_waiters(li);
1915 list_del(&li->llist);
1916 kfree(li);
1917 }
1918 }
1919
1920 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1921 int
cifs_unlock_range(struct cifsFileInfo * cfile,struct file_lock * flock,unsigned int xid)1922 cifs_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
1923 unsigned int xid)
1924 {
1925 int rc = 0, stored_rc;
1926 static const int types[] = {
1927 LOCKING_ANDX_LARGE_FILES,
1928 LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES
1929 };
1930 unsigned int i;
1931 unsigned int max_num, num, max_buf;
1932 LOCKING_ANDX_RANGE *buf, *cur;
1933 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1934 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1935 struct cifsLockInfo *li, *tmp;
1936 __u64 length = cifs_flock_len(flock);
1937 struct list_head tmp_llist;
1938
1939 INIT_LIST_HEAD(&tmp_llist);
1940
1941 /*
1942 * Accessing maxBuf is racy with cifs_reconnect - need to store value
1943 * and check it before using.
1944 */
1945 max_buf = tcon->ses->server->maxBuf;
1946 if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE)))
1947 return -EINVAL;
1948
1949 BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) >
1950 PAGE_SIZE);
1951 max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr),
1952 PAGE_SIZE);
1953 max_num = (max_buf - sizeof(struct smb_hdr)) /
1954 sizeof(LOCKING_ANDX_RANGE);
1955 buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
1956 if (!buf)
1957 return -ENOMEM;
1958
1959 cifs_down_write(&cinode->lock_sem);
1960 for (i = 0; i < 2; i++) {
1961 cur = buf;
1962 num = 0;
1963 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
1964 if (flock->fl_start > li->offset ||
1965 (flock->fl_start + length) <
1966 (li->offset + li->length))
1967 continue;
1968 if (current->tgid != li->pid)
1969 continue;
1970 if (types[i] != li->type)
1971 continue;
1972 if (cinode->can_cache_brlcks) {
1973 /*
1974 * We can cache brlock requests - simply remove
1975 * a lock from the file's list.
1976 */
1977 list_del(&li->llist);
1978 cifs_del_lock_waiters(li);
1979 kfree(li);
1980 continue;
1981 }
1982 cur->Pid = cpu_to_le16(li->pid);
1983 cur->LengthLow = cpu_to_le32((u32)li->length);
1984 cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
1985 cur->OffsetLow = cpu_to_le32((u32)li->offset);
1986 cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
1987 /*
1988 * We need to save a lock here to let us add it again to
1989 * the file's list if the unlock range request fails on
1990 * the server.
1991 */
1992 list_move(&li->llist, &tmp_llist);
1993 if (++num == max_num) {
1994 stored_rc = cifs_lockv(xid, tcon,
1995 cfile->fid.netfid,
1996 li->type, num, 0, buf);
1997 if (stored_rc) {
1998 /*
1999 * We failed on the unlock range
2000 * request - add all locks from the tmp
2001 * list to the head of the file's list.
2002 */
2003 cifs_move_llist(&tmp_llist,
2004 &cfile->llist->locks);
2005 rc = stored_rc;
2006 } else
2007 /*
2008 * The unlock range request succeed -
2009 * free the tmp list.
2010 */
2011 cifs_free_llist(&tmp_llist);
2012 cur = buf;
2013 num = 0;
2014 } else
2015 cur++;
2016 }
2017 if (num) {
2018 stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
2019 types[i], num, 0, buf);
2020 if (stored_rc) {
2021 cifs_move_llist(&tmp_llist,
2022 &cfile->llist->locks);
2023 rc = stored_rc;
2024 } else
2025 cifs_free_llist(&tmp_llist);
2026 }
2027 }
2028
2029 up_write(&cinode->lock_sem);
2030 kfree(buf);
2031 return rc;
2032 }
2033 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2034
2035 static int
cifs_setlk(struct file * file,struct file_lock * flock,__u32 type,bool wait_flag,bool posix_lck,int lock,int unlock,unsigned int xid)2036 cifs_setlk(struct file *file, struct file_lock *flock, __u32 type,
2037 bool wait_flag, bool posix_lck, int lock, int unlock,
2038 unsigned int xid)
2039 {
2040 int rc = 0;
2041 __u64 length = cifs_flock_len(flock);
2042 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
2043 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
2044 struct TCP_Server_Info *server = tcon->ses->server;
2045 struct inode *inode = d_inode(cfile->dentry);
2046
2047 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2048 if (posix_lck) {
2049 int posix_lock_type;
2050
2051 rc = cifs_posix_lock_set(file, flock);
2052 if (rc <= FILE_LOCK_DEFERRED)
2053 return rc;
2054
2055 if (type & server->vals->shared_lock_type)
2056 posix_lock_type = CIFS_RDLCK;
2057 else
2058 posix_lock_type = CIFS_WRLCK;
2059
2060 if (unlock == 1)
2061 posix_lock_type = CIFS_UNLCK;
2062
2063 rc = CIFSSMBPosixLock(xid, tcon, cfile->fid.netfid,
2064 hash_lockowner(flock->fl_owner),
2065 flock->fl_start, length,
2066 NULL, posix_lock_type, wait_flag);
2067 goto out;
2068 }
2069 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2070 if (lock) {
2071 struct cifsLockInfo *lock;
2072
2073 lock = cifs_lock_init(flock->fl_start, length, type,
2074 flock->fl_flags);
2075 if (!lock)
2076 return -ENOMEM;
2077
2078 rc = cifs_lock_add_if(cfile, lock, wait_flag);
2079 if (rc < 0) {
2080 kfree(lock);
2081 return rc;
2082 }
2083 if (!rc)
2084 goto out;
2085
2086 /*
2087 * Windows 7 server can delay breaking lease from read to None
2088 * if we set a byte-range lock on a file - break it explicitly
2089 * before sending the lock to the server to be sure the next
2090 * read won't conflict with non-overlapted locks due to
2091 * pagereading.
2092 */
2093 if (!CIFS_CACHE_WRITE(CIFS_I(inode)) &&
2094 CIFS_CACHE_READ(CIFS_I(inode))) {
2095 cifs_zap_mapping(inode);
2096 cifs_dbg(FYI, "Set no oplock for inode=%p due to mand locks\n",
2097 inode);
2098 CIFS_I(inode)->oplock = 0;
2099 }
2100
2101 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
2102 type, 1, 0, wait_flag);
2103 if (rc) {
2104 kfree(lock);
2105 return rc;
2106 }
2107
2108 cifs_lock_add(cfile, lock);
2109 } else if (unlock)
2110 rc = server->ops->mand_unlock_range(cfile, flock, xid);
2111
2112 out:
2113 if ((flock->fl_flags & FL_POSIX) || (flock->fl_flags & FL_FLOCK)) {
2114 /*
2115 * If this is a request to remove all locks because we
2116 * are closing the file, it doesn't matter if the
2117 * unlocking failed as both cifs.ko and the SMB server
2118 * remove the lock on file close
2119 */
2120 if (rc) {
2121 cifs_dbg(VFS, "%s failed rc=%d\n", __func__, rc);
2122 if (!(flock->fl_flags & FL_CLOSE))
2123 return rc;
2124 }
2125 rc = locks_lock_file_wait(file, flock);
2126 }
2127 return rc;
2128 }
2129
cifs_flock(struct file * file,int cmd,struct file_lock * fl)2130 int cifs_flock(struct file *file, int cmd, struct file_lock *fl)
2131 {
2132 int rc, xid;
2133 int lock = 0, unlock = 0;
2134 bool wait_flag = false;
2135 bool posix_lck = false;
2136 struct cifs_sb_info *cifs_sb;
2137 struct cifs_tcon *tcon;
2138 struct cifsFileInfo *cfile;
2139 __u32 type;
2140
2141 xid = get_xid();
2142
2143 if (!(fl->fl_flags & FL_FLOCK)) {
2144 rc = -ENOLCK;
2145 free_xid(xid);
2146 return rc;
2147 }
2148
2149 cfile = (struct cifsFileInfo *)file->private_data;
2150 tcon = tlink_tcon(cfile->tlink);
2151
2152 cifs_read_flock(fl, &type, &lock, &unlock, &wait_flag,
2153 tcon->ses->server);
2154 cifs_sb = CIFS_FILE_SB(file);
2155
2156 if (cap_unix(tcon->ses) &&
2157 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
2158 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
2159 posix_lck = true;
2160
2161 if (!lock && !unlock) {
2162 /*
2163 * if no lock or unlock then nothing to do since we do not
2164 * know what it is
2165 */
2166 rc = -EOPNOTSUPP;
2167 free_xid(xid);
2168 return rc;
2169 }
2170
2171 rc = cifs_setlk(file, fl, type, wait_flag, posix_lck, lock, unlock,
2172 xid);
2173 free_xid(xid);
2174 return rc;
2175
2176
2177 }
2178
cifs_lock(struct file * file,int cmd,struct file_lock * flock)2179 int cifs_lock(struct file *file, int cmd, struct file_lock *flock)
2180 {
2181 int rc, xid;
2182 int lock = 0, unlock = 0;
2183 bool wait_flag = false;
2184 bool posix_lck = false;
2185 struct cifs_sb_info *cifs_sb;
2186 struct cifs_tcon *tcon;
2187 struct cifsFileInfo *cfile;
2188 __u32 type;
2189
2190 rc = -EACCES;
2191 xid = get_xid();
2192
2193 cifs_dbg(FYI, "%s: %pD2 cmd=0x%x type=0x%x flags=0x%x r=%lld:%lld\n", __func__, file, cmd,
2194 flock->fl_flags, flock->fl_type, (long long)flock->fl_start,
2195 (long long)flock->fl_end);
2196
2197 cfile = (struct cifsFileInfo *)file->private_data;
2198 tcon = tlink_tcon(cfile->tlink);
2199
2200 cifs_read_flock(flock, &type, &lock, &unlock, &wait_flag,
2201 tcon->ses->server);
2202 cifs_sb = CIFS_FILE_SB(file);
2203 set_bit(CIFS_INO_CLOSE_ON_LOCK, &CIFS_I(d_inode(cfile->dentry))->flags);
2204
2205 if (cap_unix(tcon->ses) &&
2206 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
2207 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
2208 posix_lck = true;
2209 /*
2210 * BB add code here to normalize offset and length to account for
2211 * negative length which we can not accept over the wire.
2212 */
2213 if (IS_GETLK(cmd)) {
2214 rc = cifs_getlk(file, flock, type, wait_flag, posix_lck, xid);
2215 free_xid(xid);
2216 return rc;
2217 }
2218
2219 if (!lock && !unlock) {
2220 /*
2221 * if no lock or unlock then nothing to do since we do not
2222 * know what it is
2223 */
2224 free_xid(xid);
2225 return -EOPNOTSUPP;
2226 }
2227
2228 rc = cifs_setlk(file, flock, type, wait_flag, posix_lck, lock, unlock,
2229 xid);
2230 free_xid(xid);
2231 return rc;
2232 }
2233
2234 /*
2235 * update the file size (if needed) after a write. Should be called with
2236 * the inode->i_lock held
2237 */
2238 void
cifs_update_eof(struct cifsInodeInfo * cifsi,loff_t offset,unsigned int bytes_written)2239 cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset,
2240 unsigned int bytes_written)
2241 {
2242 loff_t end_of_write = offset + bytes_written;
2243
2244 if (end_of_write > cifsi->server_eof)
2245 cifsi->server_eof = end_of_write;
2246 }
2247
2248 static ssize_t
cifs_write(struct cifsFileInfo * open_file,__u32 pid,const char * write_data,size_t write_size,loff_t * offset)2249 cifs_write(struct cifsFileInfo *open_file, __u32 pid, const char *write_data,
2250 size_t write_size, loff_t *offset)
2251 {
2252 int rc = 0;
2253 unsigned int bytes_written = 0;
2254 unsigned int total_written;
2255 struct cifs_tcon *tcon;
2256 struct TCP_Server_Info *server;
2257 unsigned int xid;
2258 struct dentry *dentry = open_file->dentry;
2259 struct cifsInodeInfo *cifsi = CIFS_I(d_inode(dentry));
2260 struct cifs_io_parms io_parms = {0};
2261
2262 cifs_dbg(FYI, "write %zd bytes to offset %lld of %pd\n",
2263 write_size, *offset, dentry);
2264
2265 tcon = tlink_tcon(open_file->tlink);
2266 server = tcon->ses->server;
2267
2268 if (!server->ops->sync_write)
2269 return -ENOSYS;
2270
2271 xid = get_xid();
2272
2273 for (total_written = 0; write_size > total_written;
2274 total_written += bytes_written) {
2275 rc = -EAGAIN;
2276 while (rc == -EAGAIN) {
2277 struct kvec iov[2];
2278 unsigned int len;
2279
2280 if (open_file->invalidHandle) {
2281 /* we could deadlock if we called
2282 filemap_fdatawait from here so tell
2283 reopen_file not to flush data to
2284 server now */
2285 rc = cifs_reopen_file(open_file, false);
2286 if (rc != 0)
2287 break;
2288 }
2289
2290 len = min(server->ops->wp_retry_size(d_inode(dentry)),
2291 (unsigned int)write_size - total_written);
2292 /* iov[0] is reserved for smb header */
2293 iov[1].iov_base = (char *)write_data + total_written;
2294 iov[1].iov_len = len;
2295 io_parms.pid = pid;
2296 io_parms.tcon = tcon;
2297 io_parms.offset = *offset;
2298 io_parms.length = len;
2299 rc = server->ops->sync_write(xid, &open_file->fid,
2300 &io_parms, &bytes_written, iov, 1);
2301 }
2302 if (rc || (bytes_written == 0)) {
2303 if (total_written)
2304 break;
2305 else {
2306 free_xid(xid);
2307 return rc;
2308 }
2309 } else {
2310 spin_lock(&d_inode(dentry)->i_lock);
2311 cifs_update_eof(cifsi, *offset, bytes_written);
2312 spin_unlock(&d_inode(dentry)->i_lock);
2313 *offset += bytes_written;
2314 }
2315 }
2316
2317 cifs_stats_bytes_written(tcon, total_written);
2318
2319 if (total_written > 0) {
2320 spin_lock(&d_inode(dentry)->i_lock);
2321 if (*offset > d_inode(dentry)->i_size) {
2322 i_size_write(d_inode(dentry), *offset);
2323 d_inode(dentry)->i_blocks = (512 - 1 + *offset) >> 9;
2324 }
2325 spin_unlock(&d_inode(dentry)->i_lock);
2326 }
2327 mark_inode_dirty_sync(d_inode(dentry));
2328 free_xid(xid);
2329 return total_written;
2330 }
2331
find_readable_file(struct cifsInodeInfo * cifs_inode,bool fsuid_only)2332 struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode,
2333 bool fsuid_only)
2334 {
2335 struct cifsFileInfo *open_file = NULL;
2336 struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_inode->netfs.inode.i_sb);
2337
2338 /* only filter by fsuid on multiuser mounts */
2339 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
2340 fsuid_only = false;
2341
2342 spin_lock(&cifs_inode->open_file_lock);
2343 /* we could simply get the first_list_entry since write-only entries
2344 are always at the end of the list but since the first entry might
2345 have a close pending, we go through the whole list */
2346 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
2347 if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
2348 continue;
2349 if (OPEN_FMODE(open_file->f_flags) & FMODE_READ) {
2350 if ((!open_file->invalidHandle)) {
2351 /* found a good file */
2352 /* lock it so it will not be closed on us */
2353 cifsFileInfo_get(open_file);
2354 spin_unlock(&cifs_inode->open_file_lock);
2355 return open_file;
2356 } /* else might as well continue, and look for
2357 another, or simply have the caller reopen it
2358 again rather than trying to fix this handle */
2359 } else /* write only file */
2360 break; /* write only files are last so must be done */
2361 }
2362 spin_unlock(&cifs_inode->open_file_lock);
2363 return NULL;
2364 }
2365
2366 /* Return -EBADF if no handle is found and general rc otherwise */
2367 int
cifs_get_writable_file(struct cifsInodeInfo * cifs_inode,int flags,struct cifsFileInfo ** ret_file)2368 cifs_get_writable_file(struct cifsInodeInfo *cifs_inode, int flags,
2369 struct cifsFileInfo **ret_file)
2370 {
2371 struct cifsFileInfo *open_file, *inv_file = NULL;
2372 struct cifs_sb_info *cifs_sb;
2373 bool any_available = false;
2374 int rc = -EBADF;
2375 unsigned int refind = 0;
2376 bool fsuid_only = flags & FIND_WR_FSUID_ONLY;
2377 bool with_delete = flags & FIND_WR_WITH_DELETE;
2378 *ret_file = NULL;
2379
2380 /*
2381 * Having a null inode here (because mapping->host was set to zero by
2382 * the VFS or MM) should not happen but we had reports of on oops (due
2383 * to it being zero) during stress testcases so we need to check for it
2384 */
2385
2386 if (cifs_inode == NULL) {
2387 cifs_dbg(VFS, "Null inode passed to cifs_writeable_file\n");
2388 dump_stack();
2389 return rc;
2390 }
2391
2392 cifs_sb = CIFS_SB(cifs_inode->netfs.inode.i_sb);
2393
2394 /* only filter by fsuid on multiuser mounts */
2395 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
2396 fsuid_only = false;
2397
2398 spin_lock(&cifs_inode->open_file_lock);
2399 refind_writable:
2400 if (refind > MAX_REOPEN_ATT) {
2401 spin_unlock(&cifs_inode->open_file_lock);
2402 return rc;
2403 }
2404 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
2405 if (!any_available && open_file->pid != current->tgid)
2406 continue;
2407 if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
2408 continue;
2409 if (with_delete && !(open_file->fid.access & DELETE))
2410 continue;
2411 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
2412 if (!open_file->invalidHandle) {
2413 /* found a good writable file */
2414 cifsFileInfo_get(open_file);
2415 spin_unlock(&cifs_inode->open_file_lock);
2416 *ret_file = open_file;
2417 return 0;
2418 } else {
2419 if (!inv_file)
2420 inv_file = open_file;
2421 }
2422 }
2423 }
2424 /* couldn't find useable FH with same pid, try any available */
2425 if (!any_available) {
2426 any_available = true;
2427 goto refind_writable;
2428 }
2429
2430 if (inv_file) {
2431 any_available = false;
2432 cifsFileInfo_get(inv_file);
2433 }
2434
2435 spin_unlock(&cifs_inode->open_file_lock);
2436
2437 if (inv_file) {
2438 rc = cifs_reopen_file(inv_file, false);
2439 if (!rc) {
2440 *ret_file = inv_file;
2441 return 0;
2442 }
2443
2444 spin_lock(&cifs_inode->open_file_lock);
2445 list_move_tail(&inv_file->flist, &cifs_inode->openFileList);
2446 spin_unlock(&cifs_inode->open_file_lock);
2447 cifsFileInfo_put(inv_file);
2448 ++refind;
2449 inv_file = NULL;
2450 spin_lock(&cifs_inode->open_file_lock);
2451 goto refind_writable;
2452 }
2453
2454 return rc;
2455 }
2456
2457 struct cifsFileInfo *
find_writable_file(struct cifsInodeInfo * cifs_inode,int flags)2458 find_writable_file(struct cifsInodeInfo *cifs_inode, int flags)
2459 {
2460 struct cifsFileInfo *cfile;
2461 int rc;
2462
2463 rc = cifs_get_writable_file(cifs_inode, flags, &cfile);
2464 if (rc)
2465 cifs_dbg(FYI, "Couldn't find writable handle rc=%d\n", rc);
2466
2467 return cfile;
2468 }
2469
2470 int
cifs_get_writable_path(struct cifs_tcon * tcon,const char * name,int flags,struct cifsFileInfo ** ret_file)2471 cifs_get_writable_path(struct cifs_tcon *tcon, const char *name,
2472 int flags,
2473 struct cifsFileInfo **ret_file)
2474 {
2475 struct cifsFileInfo *cfile;
2476 void *page = alloc_dentry_path();
2477
2478 *ret_file = NULL;
2479
2480 spin_lock(&tcon->open_file_lock);
2481 list_for_each_entry(cfile, &tcon->openFileList, tlist) {
2482 struct cifsInodeInfo *cinode;
2483 const char *full_path = build_path_from_dentry(cfile->dentry, page);
2484 if (IS_ERR(full_path)) {
2485 spin_unlock(&tcon->open_file_lock);
2486 free_dentry_path(page);
2487 return PTR_ERR(full_path);
2488 }
2489 if (strcmp(full_path, name))
2490 continue;
2491
2492 cinode = CIFS_I(d_inode(cfile->dentry));
2493 spin_unlock(&tcon->open_file_lock);
2494 free_dentry_path(page);
2495 return cifs_get_writable_file(cinode, flags, ret_file);
2496 }
2497
2498 spin_unlock(&tcon->open_file_lock);
2499 free_dentry_path(page);
2500 return -ENOENT;
2501 }
2502
2503 int
cifs_get_readable_path(struct cifs_tcon * tcon,const char * name,struct cifsFileInfo ** ret_file)2504 cifs_get_readable_path(struct cifs_tcon *tcon, const char *name,
2505 struct cifsFileInfo **ret_file)
2506 {
2507 struct cifsFileInfo *cfile;
2508 void *page = alloc_dentry_path();
2509
2510 *ret_file = NULL;
2511
2512 spin_lock(&tcon->open_file_lock);
2513 list_for_each_entry(cfile, &tcon->openFileList, tlist) {
2514 struct cifsInodeInfo *cinode;
2515 const char *full_path = build_path_from_dentry(cfile->dentry, page);
2516 if (IS_ERR(full_path)) {
2517 spin_unlock(&tcon->open_file_lock);
2518 free_dentry_path(page);
2519 return PTR_ERR(full_path);
2520 }
2521 if (strcmp(full_path, name))
2522 continue;
2523
2524 cinode = CIFS_I(d_inode(cfile->dentry));
2525 spin_unlock(&tcon->open_file_lock);
2526 free_dentry_path(page);
2527 *ret_file = find_readable_file(cinode, 0);
2528 return *ret_file ? 0 : -ENOENT;
2529 }
2530
2531 spin_unlock(&tcon->open_file_lock);
2532 free_dentry_path(page);
2533 return -ENOENT;
2534 }
2535
2536 void
cifs_writedata_release(struct kref * refcount)2537 cifs_writedata_release(struct kref *refcount)
2538 {
2539 struct cifs_writedata *wdata = container_of(refcount,
2540 struct cifs_writedata, refcount);
2541 #ifdef CONFIG_CIFS_SMB_DIRECT
2542 if (wdata->mr) {
2543 smbd_deregister_mr(wdata->mr);
2544 wdata->mr = NULL;
2545 }
2546 #endif
2547
2548 if (wdata->cfile)
2549 cifsFileInfo_put(wdata->cfile);
2550
2551 kfree(wdata);
2552 }
2553
2554 /*
2555 * Write failed with a retryable error. Resend the write request. It's also
2556 * possible that the page was redirtied so re-clean the page.
2557 */
2558 static void
cifs_writev_requeue(struct cifs_writedata * wdata)2559 cifs_writev_requeue(struct cifs_writedata *wdata)
2560 {
2561 int rc = 0;
2562 struct inode *inode = d_inode(wdata->cfile->dentry);
2563 struct TCP_Server_Info *server;
2564 unsigned int rest_len = wdata->bytes;
2565 loff_t fpos = wdata->offset;
2566
2567 server = tlink_tcon(wdata->cfile->tlink)->ses->server;
2568 do {
2569 struct cifs_writedata *wdata2;
2570 unsigned int wsize, cur_len;
2571
2572 wsize = server->ops->wp_retry_size(inode);
2573 if (wsize < rest_len) {
2574 if (wsize < PAGE_SIZE) {
2575 rc = -EOPNOTSUPP;
2576 break;
2577 }
2578 cur_len = min(round_down(wsize, PAGE_SIZE), rest_len);
2579 } else {
2580 cur_len = rest_len;
2581 }
2582
2583 wdata2 = cifs_writedata_alloc(cifs_writev_complete);
2584 if (!wdata2) {
2585 rc = -ENOMEM;
2586 break;
2587 }
2588
2589 wdata2->sync_mode = wdata->sync_mode;
2590 wdata2->offset = fpos;
2591 wdata2->bytes = cur_len;
2592 wdata2->iter = wdata->iter;
2593
2594 iov_iter_advance(&wdata2->iter, fpos - wdata->offset);
2595 iov_iter_truncate(&wdata2->iter, wdata2->bytes);
2596
2597 if (iov_iter_is_xarray(&wdata2->iter))
2598 /* Check for pages having been redirtied and clean
2599 * them. We can do this by walking the xarray. If
2600 * it's not an xarray, then it's a DIO and we shouldn't
2601 * be mucking around with the page bits.
2602 */
2603 cifs_undirty_folios(inode, fpos, cur_len);
2604
2605 rc = cifs_get_writable_file(CIFS_I(inode), FIND_WR_ANY,
2606 &wdata2->cfile);
2607 if (!wdata2->cfile) {
2608 cifs_dbg(VFS, "No writable handle to retry writepages rc=%d\n",
2609 rc);
2610 if (!is_retryable_error(rc))
2611 rc = -EBADF;
2612 } else {
2613 wdata2->pid = wdata2->cfile->pid;
2614 rc = server->ops->async_writev(wdata2,
2615 cifs_writedata_release);
2616 }
2617
2618 kref_put(&wdata2->refcount, cifs_writedata_release);
2619 if (rc) {
2620 if (is_retryable_error(rc))
2621 continue;
2622 fpos += cur_len;
2623 rest_len -= cur_len;
2624 break;
2625 }
2626
2627 fpos += cur_len;
2628 rest_len -= cur_len;
2629 } while (rest_len > 0);
2630
2631 /* Clean up remaining pages from the original wdata */
2632 if (iov_iter_is_xarray(&wdata->iter))
2633 cifs_pages_write_failed(inode, fpos, rest_len);
2634
2635 if (rc != 0 && !is_retryable_error(rc))
2636 mapping_set_error(inode->i_mapping, rc);
2637 kref_put(&wdata->refcount, cifs_writedata_release);
2638 }
2639
2640 void
cifs_writev_complete(struct work_struct * work)2641 cifs_writev_complete(struct work_struct *work)
2642 {
2643 struct cifs_writedata *wdata = container_of(work,
2644 struct cifs_writedata, work);
2645 struct inode *inode = d_inode(wdata->cfile->dentry);
2646
2647 if (wdata->result == 0) {
2648 spin_lock(&inode->i_lock);
2649 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
2650 spin_unlock(&inode->i_lock);
2651 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
2652 wdata->bytes);
2653 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
2654 return cifs_writev_requeue(wdata);
2655
2656 if (wdata->result == -EAGAIN)
2657 cifs_pages_write_redirty(inode, wdata->offset, wdata->bytes);
2658 else if (wdata->result < 0)
2659 cifs_pages_write_failed(inode, wdata->offset, wdata->bytes);
2660 else
2661 cifs_pages_written_back(inode, wdata->offset, wdata->bytes);
2662
2663 if (wdata->result != -EAGAIN)
2664 mapping_set_error(inode->i_mapping, wdata->result);
2665 kref_put(&wdata->refcount, cifs_writedata_release);
2666 }
2667
cifs_writedata_alloc(work_func_t complete)2668 struct cifs_writedata *cifs_writedata_alloc(work_func_t complete)
2669 {
2670 struct cifs_writedata *wdata;
2671
2672 wdata = kzalloc(sizeof(*wdata), GFP_NOFS);
2673 if (wdata != NULL) {
2674 kref_init(&wdata->refcount);
2675 INIT_LIST_HEAD(&wdata->list);
2676 init_completion(&wdata->done);
2677 INIT_WORK(&wdata->work, complete);
2678 }
2679 return wdata;
2680 }
2681
cifs_partialpagewrite(struct page * page,unsigned from,unsigned to)2682 static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to)
2683 {
2684 struct address_space *mapping = page->mapping;
2685 loff_t offset = (loff_t)page->index << PAGE_SHIFT;
2686 char *write_data;
2687 int rc = -EFAULT;
2688 int bytes_written = 0;
2689 struct inode *inode;
2690 struct cifsFileInfo *open_file;
2691
2692 if (!mapping || !mapping->host)
2693 return -EFAULT;
2694
2695 inode = page->mapping->host;
2696
2697 offset += (loff_t)from;
2698 write_data = kmap(page);
2699 write_data += from;
2700
2701 if ((to > PAGE_SIZE) || (from > to)) {
2702 kunmap(page);
2703 return -EIO;
2704 }
2705
2706 /* racing with truncate? */
2707 if (offset > mapping->host->i_size) {
2708 kunmap(page);
2709 return 0; /* don't care */
2710 }
2711
2712 /* check to make sure that we are not extending the file */
2713 if (mapping->host->i_size - offset < (loff_t)to)
2714 to = (unsigned)(mapping->host->i_size - offset);
2715
2716 rc = cifs_get_writable_file(CIFS_I(mapping->host), FIND_WR_ANY,
2717 &open_file);
2718 if (!rc) {
2719 bytes_written = cifs_write(open_file, open_file->pid,
2720 write_data, to - from, &offset);
2721 cifsFileInfo_put(open_file);
2722 /* Does mm or vfs already set times? */
2723 simple_inode_init_ts(inode);
2724 if ((bytes_written > 0) && (offset))
2725 rc = 0;
2726 else if (bytes_written < 0)
2727 rc = bytes_written;
2728 else
2729 rc = -EFAULT;
2730 } else {
2731 cifs_dbg(FYI, "No writable handle for write page rc=%d\n", rc);
2732 if (!is_retryable_error(rc))
2733 rc = -EIO;
2734 }
2735
2736 kunmap(page);
2737 return rc;
2738 }
2739
2740 /*
2741 * Extend the region to be written back to include subsequent contiguously
2742 * dirty pages if possible, but don't sleep while doing so.
2743 */
cifs_extend_writeback(struct address_space * mapping,struct xa_state * xas,long * _count,loff_t start,int max_pages,loff_t max_len,size_t * _len)2744 static void cifs_extend_writeback(struct address_space *mapping,
2745 struct xa_state *xas,
2746 long *_count,
2747 loff_t start,
2748 int max_pages,
2749 loff_t max_len,
2750 size_t *_len)
2751 {
2752 struct folio_batch batch;
2753 struct folio *folio;
2754 unsigned int nr_pages;
2755 pgoff_t index = (start + *_len) / PAGE_SIZE;
2756 size_t len;
2757 bool stop = true;
2758 unsigned int i;
2759
2760 folio_batch_init(&batch);
2761
2762 do {
2763 /* Firstly, we gather up a batch of contiguous dirty pages
2764 * under the RCU read lock - but we can't clear the dirty flags
2765 * there if any of those pages are mapped.
2766 */
2767 rcu_read_lock();
2768
2769 xas_for_each(xas, folio, ULONG_MAX) {
2770 stop = true;
2771 if (xas_retry(xas, folio))
2772 continue;
2773 if (xa_is_value(folio))
2774 break;
2775 if (folio->index != index) {
2776 xas_reset(xas);
2777 break;
2778 }
2779
2780 if (!folio_try_get(folio)) {
2781 xas_reset(xas);
2782 continue;
2783 }
2784 nr_pages = folio_nr_pages(folio);
2785 if (nr_pages > max_pages) {
2786 xas_reset(xas);
2787 break;
2788 }
2789
2790 /* Has the page moved or been split? */
2791 if (unlikely(folio != xas_reload(xas))) {
2792 folio_put(folio);
2793 xas_reset(xas);
2794 break;
2795 }
2796
2797 if (!folio_trylock(folio)) {
2798 folio_put(folio);
2799 xas_reset(xas);
2800 break;
2801 }
2802 if (!folio_test_dirty(folio) ||
2803 folio_test_writeback(folio)) {
2804 folio_unlock(folio);
2805 folio_put(folio);
2806 xas_reset(xas);
2807 break;
2808 }
2809
2810 max_pages -= nr_pages;
2811 len = folio_size(folio);
2812 stop = false;
2813
2814 index += nr_pages;
2815 *_count -= nr_pages;
2816 *_len += len;
2817 if (max_pages <= 0 || *_len >= max_len || *_count <= 0)
2818 stop = true;
2819
2820 if (!folio_batch_add(&batch, folio))
2821 break;
2822 if (stop)
2823 break;
2824 }
2825
2826 xas_pause(xas);
2827 rcu_read_unlock();
2828
2829 /* Now, if we obtained any pages, we can shift them to being
2830 * writable and mark them for caching.
2831 */
2832 if (!folio_batch_count(&batch))
2833 break;
2834
2835 for (i = 0; i < folio_batch_count(&batch); i++) {
2836 folio = batch.folios[i];
2837 /* The folio should be locked, dirty and not undergoing
2838 * writeback from the loop above.
2839 */
2840 if (!folio_clear_dirty_for_io(folio))
2841 WARN_ON(1);
2842 folio_start_writeback(folio);
2843 folio_unlock(folio);
2844 }
2845
2846 folio_batch_release(&batch);
2847 cond_resched();
2848 } while (!stop);
2849 }
2850
2851 /*
2852 * Write back the locked page and any subsequent non-locked dirty pages.
2853 */
cifs_write_back_from_locked_folio(struct address_space * mapping,struct writeback_control * wbc,struct xa_state * xas,struct folio * folio,unsigned long long start,unsigned long long end)2854 static ssize_t cifs_write_back_from_locked_folio(struct address_space *mapping,
2855 struct writeback_control *wbc,
2856 struct xa_state *xas,
2857 struct folio *folio,
2858 unsigned long long start,
2859 unsigned long long end)
2860 {
2861 struct inode *inode = mapping->host;
2862 struct TCP_Server_Info *server;
2863 struct cifs_writedata *wdata;
2864 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2865 struct cifs_credits credits_on_stack;
2866 struct cifs_credits *credits = &credits_on_stack;
2867 struct cifsFileInfo *cfile = NULL;
2868 unsigned long long i_size = i_size_read(inode), max_len;
2869 unsigned int xid, wsize;
2870 size_t len = folio_size(folio);
2871 long count = wbc->nr_to_write;
2872 int rc;
2873
2874 /* The folio should be locked, dirty and not undergoing writeback. */
2875 if (!folio_clear_dirty_for_io(folio))
2876 WARN_ON_ONCE(1);
2877 folio_start_writeback(folio);
2878
2879 count -= folio_nr_pages(folio);
2880
2881 xid = get_xid();
2882 server = cifs_pick_channel(cifs_sb_master_tcon(cifs_sb)->ses);
2883
2884 rc = cifs_get_writable_file(CIFS_I(inode), FIND_WR_ANY, &cfile);
2885 if (rc) {
2886 cifs_dbg(VFS, "No writable handle in writepages rc=%d\n", rc);
2887 goto err_xid;
2888 }
2889
2890 rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->wsize,
2891 &wsize, credits);
2892 if (rc != 0)
2893 goto err_close;
2894
2895 wdata = cifs_writedata_alloc(cifs_writev_complete);
2896 if (!wdata) {
2897 rc = -ENOMEM;
2898 goto err_uncredit;
2899 }
2900
2901 wdata->sync_mode = wbc->sync_mode;
2902 wdata->offset = folio_pos(folio);
2903 wdata->pid = cfile->pid;
2904 wdata->credits = credits_on_stack;
2905 wdata->cfile = cfile;
2906 wdata->server = server;
2907 cfile = NULL;
2908
2909 /* Find all consecutive lockable dirty pages that have contiguous
2910 * written regions, stopping when we find a page that is not
2911 * immediately lockable, is not dirty or is missing, or we reach the
2912 * end of the range.
2913 */
2914 if (start < i_size) {
2915 /* Trim the write to the EOF; the extra data is ignored. Also
2916 * put an upper limit on the size of a single storedata op.
2917 */
2918 max_len = wsize;
2919 max_len = min_t(unsigned long long, max_len, end - start + 1);
2920 max_len = min_t(unsigned long long, max_len, i_size - start);
2921
2922 if (len < max_len) {
2923 int max_pages = INT_MAX;
2924
2925 #ifdef CONFIG_CIFS_SMB_DIRECT
2926 if (server->smbd_conn)
2927 max_pages = server->smbd_conn->max_frmr_depth;
2928 #endif
2929 max_pages -= folio_nr_pages(folio);
2930
2931 if (max_pages > 0)
2932 cifs_extend_writeback(mapping, xas, &count, start,
2933 max_pages, max_len, &len);
2934 }
2935 }
2936 len = min_t(unsigned long long, len, i_size - start);
2937
2938 /* We now have a contiguous set of dirty pages, each with writeback
2939 * set; the first page is still locked at this point, but all the rest
2940 * have been unlocked.
2941 */
2942 folio_unlock(folio);
2943 wdata->bytes = len;
2944
2945 if (start < i_size) {
2946 iov_iter_xarray(&wdata->iter, ITER_SOURCE, &mapping->i_pages,
2947 start, len);
2948
2949 rc = adjust_credits(wdata->server, &wdata->credits, wdata->bytes);
2950 if (rc)
2951 goto err_wdata;
2952
2953 if (wdata->cfile->invalidHandle)
2954 rc = -EAGAIN;
2955 else
2956 rc = wdata->server->ops->async_writev(wdata,
2957 cifs_writedata_release);
2958 if (rc >= 0) {
2959 kref_put(&wdata->refcount, cifs_writedata_release);
2960 goto err_close;
2961 }
2962 } else {
2963 /* The dirty region was entirely beyond the EOF. */
2964 cifs_pages_written_back(inode, start, len);
2965 rc = 0;
2966 }
2967
2968 err_wdata:
2969 kref_put(&wdata->refcount, cifs_writedata_release);
2970 err_uncredit:
2971 add_credits_and_wake_if(server, credits, 0);
2972 err_close:
2973 if (cfile)
2974 cifsFileInfo_put(cfile);
2975 err_xid:
2976 free_xid(xid);
2977 if (rc == 0) {
2978 wbc->nr_to_write = count;
2979 rc = len;
2980 } else if (is_retryable_error(rc)) {
2981 cifs_pages_write_redirty(inode, start, len);
2982 } else {
2983 cifs_pages_write_failed(inode, start, len);
2984 mapping_set_error(mapping, rc);
2985 }
2986 /* Indication to update ctime and mtime as close is deferred */
2987 set_bit(CIFS_INO_MODIFIED_ATTR, &CIFS_I(inode)->flags);
2988 return rc;
2989 }
2990
2991 /*
2992 * write a region of pages back to the server
2993 */
cifs_writepages_begin(struct address_space * mapping,struct writeback_control * wbc,struct xa_state * xas,unsigned long long * _start,unsigned long long end)2994 static ssize_t cifs_writepages_begin(struct address_space *mapping,
2995 struct writeback_control *wbc,
2996 struct xa_state *xas,
2997 unsigned long long *_start,
2998 unsigned long long end)
2999 {
3000 struct folio *folio;
3001 unsigned long long start = *_start;
3002 ssize_t ret;
3003 int skips = 0;
3004
3005 search_again:
3006 /* Find the first dirty page. */
3007 rcu_read_lock();
3008
3009 for (;;) {
3010 folio = xas_find_marked(xas, end / PAGE_SIZE, PAGECACHE_TAG_DIRTY);
3011 if (xas_retry(xas, folio) || xa_is_value(folio))
3012 continue;
3013 if (!folio)
3014 break;
3015
3016 if (!folio_try_get(folio)) {
3017 xas_reset(xas);
3018 continue;
3019 }
3020
3021 if (unlikely(folio != xas_reload(xas))) {
3022 folio_put(folio);
3023 xas_reset(xas);
3024 continue;
3025 }
3026
3027 xas_pause(xas);
3028 break;
3029 }
3030 rcu_read_unlock();
3031 if (!folio)
3032 return 0;
3033
3034 start = folio_pos(folio); /* May regress with THPs */
3035
3036 /* At this point we hold neither the i_pages lock nor the page lock:
3037 * the page may be truncated or invalidated (changing page->mapping to
3038 * NULL), or even swizzled back from swapper_space to tmpfs file
3039 * mapping
3040 */
3041 lock_again:
3042 if (wbc->sync_mode != WB_SYNC_NONE) {
3043 ret = folio_lock_killable(folio);
3044 if (ret < 0)
3045 return ret;
3046 } else {
3047 if (!folio_trylock(folio))
3048 goto search_again;
3049 }
3050
3051 if (folio->mapping != mapping ||
3052 !folio_test_dirty(folio)) {
3053 start += folio_size(folio);
3054 folio_unlock(folio);
3055 goto search_again;
3056 }
3057
3058 if (folio_test_writeback(folio) ||
3059 folio_test_fscache(folio)) {
3060 folio_unlock(folio);
3061 if (wbc->sync_mode != WB_SYNC_NONE) {
3062 folio_wait_writeback(folio);
3063 #ifdef CONFIG_CIFS_FSCACHE
3064 folio_wait_fscache(folio);
3065 #endif
3066 goto lock_again;
3067 }
3068
3069 start += folio_size(folio);
3070 if (wbc->sync_mode == WB_SYNC_NONE) {
3071 if (skips >= 5 || need_resched()) {
3072 ret = 0;
3073 goto out;
3074 }
3075 skips++;
3076 }
3077 goto search_again;
3078 }
3079
3080 ret = cifs_write_back_from_locked_folio(mapping, wbc, xas, folio, start, end);
3081 out:
3082 if (ret > 0)
3083 *_start = start + ret;
3084 return ret;
3085 }
3086
3087 /*
3088 * Write a region of pages back to the server
3089 */
cifs_writepages_region(struct address_space * mapping,struct writeback_control * wbc,unsigned long long * _start,unsigned long long end)3090 static int cifs_writepages_region(struct address_space *mapping,
3091 struct writeback_control *wbc,
3092 unsigned long long *_start,
3093 unsigned long long end)
3094 {
3095 ssize_t ret;
3096
3097 XA_STATE(xas, &mapping->i_pages, *_start / PAGE_SIZE);
3098
3099 do {
3100 ret = cifs_writepages_begin(mapping, wbc, &xas, _start, end);
3101 if (ret > 0 && wbc->nr_to_write > 0)
3102 cond_resched();
3103 } while (ret > 0 && wbc->nr_to_write > 0);
3104
3105 return ret > 0 ? 0 : ret;
3106 }
3107
3108 /*
3109 * Write some of the pending data back to the server
3110 */
cifs_writepages(struct address_space * mapping,struct writeback_control * wbc)3111 static int cifs_writepages(struct address_space *mapping,
3112 struct writeback_control *wbc)
3113 {
3114 loff_t start, end;
3115 int ret;
3116
3117 /* We have to be careful as we can end up racing with setattr()
3118 * truncating the pagecache since the caller doesn't take a lock here
3119 * to prevent it.
3120 */
3121
3122 if (wbc->range_cyclic && mapping->writeback_index) {
3123 start = mapping->writeback_index * PAGE_SIZE;
3124 ret = cifs_writepages_region(mapping, wbc, &start, LLONG_MAX);
3125 if (ret < 0)
3126 goto out;
3127
3128 if (wbc->nr_to_write <= 0) {
3129 mapping->writeback_index = start / PAGE_SIZE;
3130 goto out;
3131 }
3132
3133 start = 0;
3134 end = mapping->writeback_index * PAGE_SIZE;
3135 mapping->writeback_index = 0;
3136 ret = cifs_writepages_region(mapping, wbc, &start, end);
3137 if (ret == 0)
3138 mapping->writeback_index = start / PAGE_SIZE;
3139 } else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) {
3140 start = 0;
3141 ret = cifs_writepages_region(mapping, wbc, &start, LLONG_MAX);
3142 if (wbc->nr_to_write > 0 && ret == 0)
3143 mapping->writeback_index = start / PAGE_SIZE;
3144 } else {
3145 start = wbc->range_start;
3146 ret = cifs_writepages_region(mapping, wbc, &start, wbc->range_end);
3147 }
3148
3149 out:
3150 return ret;
3151 }
3152
3153 static int
cifs_writepage_locked(struct page * page,struct writeback_control * wbc)3154 cifs_writepage_locked(struct page *page, struct writeback_control *wbc)
3155 {
3156 int rc;
3157 unsigned int xid;
3158
3159 xid = get_xid();
3160 /* BB add check for wbc flags */
3161 get_page(page);
3162 if (!PageUptodate(page))
3163 cifs_dbg(FYI, "ppw - page not up to date\n");
3164
3165 /*
3166 * Set the "writeback" flag, and clear "dirty" in the radix tree.
3167 *
3168 * A writepage() implementation always needs to do either this,
3169 * or re-dirty the page with "redirty_page_for_writepage()" in
3170 * the case of a failure.
3171 *
3172 * Just unlocking the page will cause the radix tree tag-bits
3173 * to fail to update with the state of the page correctly.
3174 */
3175 set_page_writeback(page);
3176 retry_write:
3177 rc = cifs_partialpagewrite(page, 0, PAGE_SIZE);
3178 if (is_retryable_error(rc)) {
3179 if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN)
3180 goto retry_write;
3181 redirty_page_for_writepage(wbc, page);
3182 } else if (rc != 0) {
3183 SetPageError(page);
3184 mapping_set_error(page->mapping, rc);
3185 } else {
3186 SetPageUptodate(page);
3187 }
3188 end_page_writeback(page);
3189 put_page(page);
3190 free_xid(xid);
3191 return rc;
3192 }
3193
cifs_write_end(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,unsigned copied,struct page * page,void * fsdata)3194 static int cifs_write_end(struct file *file, struct address_space *mapping,
3195 loff_t pos, unsigned len, unsigned copied,
3196 struct page *page, void *fsdata)
3197 {
3198 int rc;
3199 struct inode *inode = mapping->host;
3200 struct cifsFileInfo *cfile = file->private_data;
3201 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
3202 struct folio *folio = page_folio(page);
3203 __u32 pid;
3204
3205 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
3206 pid = cfile->pid;
3207 else
3208 pid = current->tgid;
3209
3210 cifs_dbg(FYI, "write_end for page %p from pos %lld with %d bytes\n",
3211 page, pos, copied);
3212
3213 if (folio_test_checked(folio)) {
3214 if (copied == len)
3215 folio_mark_uptodate(folio);
3216 folio_clear_checked(folio);
3217 } else if (!folio_test_uptodate(folio) && copied == PAGE_SIZE)
3218 folio_mark_uptodate(folio);
3219
3220 if (!folio_test_uptodate(folio)) {
3221 char *page_data;
3222 unsigned offset = pos & (PAGE_SIZE - 1);
3223 unsigned int xid;
3224
3225 xid = get_xid();
3226 /* this is probably better than directly calling
3227 partialpage_write since in this function the file handle is
3228 known which we might as well leverage */
3229 /* BB check if anything else missing out of ppw
3230 such as updating last write time */
3231 page_data = kmap(page);
3232 rc = cifs_write(cfile, pid, page_data + offset, copied, &pos);
3233 /* if (rc < 0) should we set writebehind rc? */
3234 kunmap(page);
3235
3236 free_xid(xid);
3237 } else {
3238 rc = copied;
3239 pos += copied;
3240 set_page_dirty(page);
3241 }
3242
3243 if (rc > 0) {
3244 spin_lock(&inode->i_lock);
3245 if (pos > inode->i_size) {
3246 loff_t additional_blocks = (512 - 1 + copied) >> 9;
3247
3248 i_size_write(inode, pos);
3249 /*
3250 * Estimate new allocation size based on the amount written.
3251 * This will be updated from server on close (and on queryinfo)
3252 */
3253 inode->i_blocks = min_t(blkcnt_t, (512 - 1 + pos) >> 9,
3254 inode->i_blocks + additional_blocks);
3255 }
3256 spin_unlock(&inode->i_lock);
3257 }
3258
3259 unlock_page(page);
3260 put_page(page);
3261 /* Indication to update ctime and mtime as close is deferred */
3262 set_bit(CIFS_INO_MODIFIED_ATTR, &CIFS_I(inode)->flags);
3263
3264 return rc;
3265 }
3266
cifs_strict_fsync(struct file * file,loff_t start,loff_t end,int datasync)3267 int cifs_strict_fsync(struct file *file, loff_t start, loff_t end,
3268 int datasync)
3269 {
3270 unsigned int xid;
3271 int rc = 0;
3272 struct cifs_tcon *tcon;
3273 struct TCP_Server_Info *server;
3274 struct cifsFileInfo *smbfile = file->private_data;
3275 struct inode *inode = file_inode(file);
3276 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3277
3278 rc = file_write_and_wait_range(file, start, end);
3279 if (rc) {
3280 trace_cifs_fsync_err(inode->i_ino, rc);
3281 return rc;
3282 }
3283
3284 xid = get_xid();
3285
3286 cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
3287 file, datasync);
3288
3289 if (!CIFS_CACHE_READ(CIFS_I(inode))) {
3290 rc = cifs_zap_mapping(inode);
3291 if (rc) {
3292 cifs_dbg(FYI, "rc: %d during invalidate phase\n", rc);
3293 rc = 0; /* don't care about it in fsync */
3294 }
3295 }
3296
3297 tcon = tlink_tcon(smbfile->tlink);
3298 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
3299 server = tcon->ses->server;
3300 if (server->ops->flush == NULL) {
3301 rc = -ENOSYS;
3302 goto strict_fsync_exit;
3303 }
3304
3305 if ((OPEN_FMODE(smbfile->f_flags) & FMODE_WRITE) == 0) {
3306 smbfile = find_writable_file(CIFS_I(inode), FIND_WR_ANY);
3307 if (smbfile) {
3308 rc = server->ops->flush(xid, tcon, &smbfile->fid);
3309 cifsFileInfo_put(smbfile);
3310 } else
3311 cifs_dbg(FYI, "ignore fsync for file not open for write\n");
3312 } else
3313 rc = server->ops->flush(xid, tcon, &smbfile->fid);
3314 }
3315
3316 strict_fsync_exit:
3317 free_xid(xid);
3318 return rc;
3319 }
3320
cifs_fsync(struct file * file,loff_t start,loff_t end,int datasync)3321 int cifs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
3322 {
3323 unsigned int xid;
3324 int rc = 0;
3325 struct cifs_tcon *tcon;
3326 struct TCP_Server_Info *server;
3327 struct cifsFileInfo *smbfile = file->private_data;
3328 struct inode *inode = file_inode(file);
3329 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
3330
3331 rc = file_write_and_wait_range(file, start, end);
3332 if (rc) {
3333 trace_cifs_fsync_err(file_inode(file)->i_ino, rc);
3334 return rc;
3335 }
3336
3337 xid = get_xid();
3338
3339 cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
3340 file, datasync);
3341
3342 tcon = tlink_tcon(smbfile->tlink);
3343 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
3344 server = tcon->ses->server;
3345 if (server->ops->flush == NULL) {
3346 rc = -ENOSYS;
3347 goto fsync_exit;
3348 }
3349
3350 if ((OPEN_FMODE(smbfile->f_flags) & FMODE_WRITE) == 0) {
3351 smbfile = find_writable_file(CIFS_I(inode), FIND_WR_ANY);
3352 if (smbfile) {
3353 rc = server->ops->flush(xid, tcon, &smbfile->fid);
3354 cifsFileInfo_put(smbfile);
3355 } else
3356 cifs_dbg(FYI, "ignore fsync for file not open for write\n");
3357 } else
3358 rc = server->ops->flush(xid, tcon, &smbfile->fid);
3359 }
3360
3361 fsync_exit:
3362 free_xid(xid);
3363 return rc;
3364 }
3365
3366 /*
3367 * As file closes, flush all cached write data for this inode checking
3368 * for write behind errors.
3369 */
cifs_flush(struct file * file,fl_owner_t id)3370 int cifs_flush(struct file *file, fl_owner_t id)
3371 {
3372 struct inode *inode = file_inode(file);
3373 int rc = 0;
3374
3375 if (file->f_mode & FMODE_WRITE)
3376 rc = filemap_write_and_wait(inode->i_mapping);
3377
3378 cifs_dbg(FYI, "Flush inode %p file %p rc %d\n", inode, file, rc);
3379 if (rc) {
3380 /* get more nuanced writeback errors */
3381 rc = filemap_check_wb_err(file->f_mapping, 0);
3382 trace_cifs_flush_err(inode->i_ino, rc);
3383 }
3384 return rc;
3385 }
3386
3387 static void
cifs_uncached_writedata_release(struct kref * refcount)3388 cifs_uncached_writedata_release(struct kref *refcount)
3389 {
3390 struct cifs_writedata *wdata = container_of(refcount,
3391 struct cifs_writedata, refcount);
3392
3393 kref_put(&wdata->ctx->refcount, cifs_aio_ctx_release);
3394 cifs_writedata_release(refcount);
3395 }
3396
3397 static void collect_uncached_write_data(struct cifs_aio_ctx *ctx);
3398
3399 static void
cifs_uncached_writev_complete(struct work_struct * work)3400 cifs_uncached_writev_complete(struct work_struct *work)
3401 {
3402 struct cifs_writedata *wdata = container_of(work,
3403 struct cifs_writedata, work);
3404 struct inode *inode = d_inode(wdata->cfile->dentry);
3405 struct cifsInodeInfo *cifsi = CIFS_I(inode);
3406
3407 spin_lock(&inode->i_lock);
3408 cifs_update_eof(cifsi, wdata->offset, wdata->bytes);
3409 if (cifsi->server_eof > inode->i_size)
3410 i_size_write(inode, cifsi->server_eof);
3411 spin_unlock(&inode->i_lock);
3412
3413 complete(&wdata->done);
3414 collect_uncached_write_data(wdata->ctx);
3415 /* the below call can possibly free the last ref to aio ctx */
3416 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
3417 }
3418
3419 static int
cifs_resend_wdata(struct cifs_writedata * wdata,struct list_head * wdata_list,struct cifs_aio_ctx * ctx)3420 cifs_resend_wdata(struct cifs_writedata *wdata, struct list_head *wdata_list,
3421 struct cifs_aio_ctx *ctx)
3422 {
3423 unsigned int wsize;
3424 struct cifs_credits credits;
3425 int rc;
3426 struct TCP_Server_Info *server = wdata->server;
3427
3428 do {
3429 if (wdata->cfile->invalidHandle) {
3430 rc = cifs_reopen_file(wdata->cfile, false);
3431 if (rc == -EAGAIN)
3432 continue;
3433 else if (rc)
3434 break;
3435 }
3436
3437
3438 /*
3439 * Wait for credits to resend this wdata.
3440 * Note: we are attempting to resend the whole wdata not in
3441 * segments
3442 */
3443 do {
3444 rc = server->ops->wait_mtu_credits(server, wdata->bytes,
3445 &wsize, &credits);
3446 if (rc)
3447 goto fail;
3448
3449 if (wsize < wdata->bytes) {
3450 add_credits_and_wake_if(server, &credits, 0);
3451 msleep(1000);
3452 }
3453 } while (wsize < wdata->bytes);
3454 wdata->credits = credits;
3455
3456 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
3457
3458 if (!rc) {
3459 if (wdata->cfile->invalidHandle)
3460 rc = -EAGAIN;
3461 else {
3462 wdata->replay = true;
3463 #ifdef CONFIG_CIFS_SMB_DIRECT
3464 if (wdata->mr) {
3465 wdata->mr->need_invalidate = true;
3466 smbd_deregister_mr(wdata->mr);
3467 wdata->mr = NULL;
3468 }
3469 #endif
3470 rc = server->ops->async_writev(wdata,
3471 cifs_uncached_writedata_release);
3472 }
3473 }
3474
3475 /* If the write was successfully sent, we are done */
3476 if (!rc) {
3477 list_add_tail(&wdata->list, wdata_list);
3478 return 0;
3479 }
3480
3481 /* Roll back credits and retry if needed */
3482 add_credits_and_wake_if(server, &wdata->credits, 0);
3483 } while (rc == -EAGAIN);
3484
3485 fail:
3486 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
3487 return rc;
3488 }
3489
3490 /*
3491 * Select span of a bvec iterator we're going to use. Limit it by both maximum
3492 * size and maximum number of segments.
3493 */
cifs_limit_bvec_subset(const struct iov_iter * iter,size_t max_size,size_t max_segs,unsigned int * _nsegs)3494 static size_t cifs_limit_bvec_subset(const struct iov_iter *iter, size_t max_size,
3495 size_t max_segs, unsigned int *_nsegs)
3496 {
3497 const struct bio_vec *bvecs = iter->bvec;
3498 unsigned int nbv = iter->nr_segs, ix = 0, nsegs = 0;
3499 size_t len, span = 0, n = iter->count;
3500 size_t skip = iter->iov_offset;
3501
3502 if (WARN_ON(!iov_iter_is_bvec(iter)) || n == 0)
3503 return 0;
3504
3505 while (n && ix < nbv && skip) {
3506 len = bvecs[ix].bv_len;
3507 if (skip < len)
3508 break;
3509 skip -= len;
3510 n -= len;
3511 ix++;
3512 }
3513
3514 while (n && ix < nbv) {
3515 len = min3(n, bvecs[ix].bv_len - skip, max_size);
3516 span += len;
3517 max_size -= len;
3518 nsegs++;
3519 ix++;
3520 if (max_size == 0 || nsegs >= max_segs)
3521 break;
3522 skip = 0;
3523 n -= len;
3524 }
3525
3526 *_nsegs = nsegs;
3527 return span;
3528 }
3529
3530 static int
cifs_write_from_iter(loff_t fpos,size_t len,struct iov_iter * from,struct cifsFileInfo * open_file,struct cifs_sb_info * cifs_sb,struct list_head * wdata_list,struct cifs_aio_ctx * ctx)3531 cifs_write_from_iter(loff_t fpos, size_t len, struct iov_iter *from,
3532 struct cifsFileInfo *open_file,
3533 struct cifs_sb_info *cifs_sb, struct list_head *wdata_list,
3534 struct cifs_aio_ctx *ctx)
3535 {
3536 int rc = 0;
3537 size_t cur_len, max_len;
3538 struct cifs_writedata *wdata;
3539 pid_t pid;
3540 struct TCP_Server_Info *server;
3541 unsigned int xid, max_segs = INT_MAX;
3542
3543 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
3544 pid = open_file->pid;
3545 else
3546 pid = current->tgid;
3547
3548 server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses);
3549 xid = get_xid();
3550
3551 #ifdef CONFIG_CIFS_SMB_DIRECT
3552 if (server->smbd_conn)
3553 max_segs = server->smbd_conn->max_frmr_depth;
3554 #endif
3555
3556 do {
3557 struct cifs_credits credits_on_stack;
3558 struct cifs_credits *credits = &credits_on_stack;
3559 unsigned int wsize, nsegs = 0;
3560
3561 if (signal_pending(current)) {
3562 rc = -EINTR;
3563 break;
3564 }
3565
3566 if (open_file->invalidHandle) {
3567 rc = cifs_reopen_file(open_file, false);
3568 if (rc == -EAGAIN)
3569 continue;
3570 else if (rc)
3571 break;
3572 }
3573
3574 rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->wsize,
3575 &wsize, credits);
3576 if (rc)
3577 break;
3578
3579 max_len = min_t(const size_t, len, wsize);
3580 if (!max_len) {
3581 rc = -EAGAIN;
3582 add_credits_and_wake_if(server, credits, 0);
3583 break;
3584 }
3585
3586 cur_len = cifs_limit_bvec_subset(from, max_len, max_segs, &nsegs);
3587 cifs_dbg(FYI, "write_from_iter len=%zx/%zx nsegs=%u/%lu/%u\n",
3588 cur_len, max_len, nsegs, from->nr_segs, max_segs);
3589 if (cur_len == 0) {
3590 rc = -EIO;
3591 add_credits_and_wake_if(server, credits, 0);
3592 break;
3593 }
3594
3595 wdata = cifs_writedata_alloc(cifs_uncached_writev_complete);
3596 if (!wdata) {
3597 rc = -ENOMEM;
3598 add_credits_and_wake_if(server, credits, 0);
3599 break;
3600 }
3601
3602 wdata->sync_mode = WB_SYNC_ALL;
3603 wdata->offset = (__u64)fpos;
3604 wdata->cfile = cifsFileInfo_get(open_file);
3605 wdata->server = server;
3606 wdata->pid = pid;
3607 wdata->bytes = cur_len;
3608 wdata->credits = credits_on_stack;
3609 wdata->iter = *from;
3610 wdata->ctx = ctx;
3611 kref_get(&ctx->refcount);
3612
3613 iov_iter_truncate(&wdata->iter, cur_len);
3614
3615 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
3616
3617 if (!rc) {
3618 if (wdata->cfile->invalidHandle)
3619 rc = -EAGAIN;
3620 else
3621 rc = server->ops->async_writev(wdata,
3622 cifs_uncached_writedata_release);
3623 }
3624
3625 if (rc) {
3626 add_credits_and_wake_if(server, &wdata->credits, 0);
3627 kref_put(&wdata->refcount,
3628 cifs_uncached_writedata_release);
3629 if (rc == -EAGAIN)
3630 continue;
3631 break;
3632 }
3633
3634 list_add_tail(&wdata->list, wdata_list);
3635 iov_iter_advance(from, cur_len);
3636 fpos += cur_len;
3637 len -= cur_len;
3638 } while (len > 0);
3639
3640 free_xid(xid);
3641 return rc;
3642 }
3643
collect_uncached_write_data(struct cifs_aio_ctx * ctx)3644 static void collect_uncached_write_data(struct cifs_aio_ctx *ctx)
3645 {
3646 struct cifs_writedata *wdata, *tmp;
3647 struct cifs_tcon *tcon;
3648 struct cifs_sb_info *cifs_sb;
3649 struct dentry *dentry = ctx->cfile->dentry;
3650 ssize_t rc;
3651
3652 tcon = tlink_tcon(ctx->cfile->tlink);
3653 cifs_sb = CIFS_SB(dentry->d_sb);
3654
3655 mutex_lock(&ctx->aio_mutex);
3656
3657 if (list_empty(&ctx->list)) {
3658 mutex_unlock(&ctx->aio_mutex);
3659 return;
3660 }
3661
3662 rc = ctx->rc;
3663 /*
3664 * Wait for and collect replies for any successful sends in order of
3665 * increasing offset. Once an error is hit, then return without waiting
3666 * for any more replies.
3667 */
3668 restart_loop:
3669 list_for_each_entry_safe(wdata, tmp, &ctx->list, list) {
3670 if (!rc) {
3671 if (!try_wait_for_completion(&wdata->done)) {
3672 mutex_unlock(&ctx->aio_mutex);
3673 return;
3674 }
3675
3676 if (wdata->result)
3677 rc = wdata->result;
3678 else
3679 ctx->total_len += wdata->bytes;
3680
3681 /* resend call if it's a retryable error */
3682 if (rc == -EAGAIN) {
3683 struct list_head tmp_list;
3684 struct iov_iter tmp_from = ctx->iter;
3685
3686 INIT_LIST_HEAD(&tmp_list);
3687 list_del_init(&wdata->list);
3688
3689 if (ctx->direct_io)
3690 rc = cifs_resend_wdata(
3691 wdata, &tmp_list, ctx);
3692 else {
3693 iov_iter_advance(&tmp_from,
3694 wdata->offset - ctx->pos);
3695
3696 rc = cifs_write_from_iter(wdata->offset,
3697 wdata->bytes, &tmp_from,
3698 ctx->cfile, cifs_sb, &tmp_list,
3699 ctx);
3700
3701 kref_put(&wdata->refcount,
3702 cifs_uncached_writedata_release);
3703 }
3704
3705 list_splice(&tmp_list, &ctx->list);
3706 goto restart_loop;
3707 }
3708 }
3709 list_del_init(&wdata->list);
3710 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
3711 }
3712
3713 cifs_stats_bytes_written(tcon, ctx->total_len);
3714 set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(dentry->d_inode)->flags);
3715
3716 ctx->rc = (rc == 0) ? ctx->total_len : rc;
3717
3718 mutex_unlock(&ctx->aio_mutex);
3719
3720 if (ctx->iocb && ctx->iocb->ki_complete)
3721 ctx->iocb->ki_complete(ctx->iocb, ctx->rc);
3722 else
3723 complete(&ctx->done);
3724 }
3725
__cifs_writev(struct kiocb * iocb,struct iov_iter * from,bool direct)3726 static ssize_t __cifs_writev(
3727 struct kiocb *iocb, struct iov_iter *from, bool direct)
3728 {
3729 struct file *file = iocb->ki_filp;
3730 ssize_t total_written = 0;
3731 struct cifsFileInfo *cfile;
3732 struct cifs_tcon *tcon;
3733 struct cifs_sb_info *cifs_sb;
3734 struct cifs_aio_ctx *ctx;
3735 int rc;
3736
3737 rc = generic_write_checks(iocb, from);
3738 if (rc <= 0)
3739 return rc;
3740
3741 cifs_sb = CIFS_FILE_SB(file);
3742 cfile = file->private_data;
3743 tcon = tlink_tcon(cfile->tlink);
3744
3745 if (!tcon->ses->server->ops->async_writev)
3746 return -ENOSYS;
3747
3748 ctx = cifs_aio_ctx_alloc();
3749 if (!ctx)
3750 return -ENOMEM;
3751
3752 ctx->cfile = cifsFileInfo_get(cfile);
3753
3754 if (!is_sync_kiocb(iocb))
3755 ctx->iocb = iocb;
3756
3757 ctx->pos = iocb->ki_pos;
3758 ctx->direct_io = direct;
3759 ctx->nr_pinned_pages = 0;
3760
3761 if (user_backed_iter(from)) {
3762 /*
3763 * Extract IOVEC/UBUF-type iterators to a BVEC-type iterator as
3764 * they contain references to the calling process's virtual
3765 * memory layout which won't be available in an async worker
3766 * thread. This also takes a pin on every folio involved.
3767 */
3768 rc = netfs_extract_user_iter(from, iov_iter_count(from),
3769 &ctx->iter, 0);
3770 if (rc < 0) {
3771 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3772 return rc;
3773 }
3774
3775 ctx->nr_pinned_pages = rc;
3776 ctx->bv = (void *)ctx->iter.bvec;
3777 ctx->bv_need_unpin = iov_iter_extract_will_pin(from);
3778 } else if ((iov_iter_is_bvec(from) || iov_iter_is_kvec(from)) &&
3779 !is_sync_kiocb(iocb)) {
3780 /*
3781 * If the op is asynchronous, we need to copy the list attached
3782 * to a BVEC/KVEC-type iterator, but we assume that the storage
3783 * will be pinned by the caller; in any case, we may or may not
3784 * be able to pin the pages, so we don't try.
3785 */
3786 ctx->bv = (void *)dup_iter(&ctx->iter, from, GFP_KERNEL);
3787 if (!ctx->bv) {
3788 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3789 return -ENOMEM;
3790 }
3791 } else {
3792 /*
3793 * Otherwise, we just pass the iterator down as-is and rely on
3794 * the caller to make sure the pages referred to by the
3795 * iterator don't evaporate.
3796 */
3797 ctx->iter = *from;
3798 }
3799
3800 ctx->len = iov_iter_count(&ctx->iter);
3801
3802 /* grab a lock here due to read response handlers can access ctx */
3803 mutex_lock(&ctx->aio_mutex);
3804
3805 rc = cifs_write_from_iter(iocb->ki_pos, ctx->len, &ctx->iter,
3806 cfile, cifs_sb, &ctx->list, ctx);
3807
3808 /*
3809 * If at least one write was successfully sent, then discard any rc
3810 * value from the later writes. If the other write succeeds, then
3811 * we'll end up returning whatever was written. If it fails, then
3812 * we'll get a new rc value from that.
3813 */
3814 if (!list_empty(&ctx->list))
3815 rc = 0;
3816
3817 mutex_unlock(&ctx->aio_mutex);
3818
3819 if (rc) {
3820 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3821 return rc;
3822 }
3823
3824 if (!is_sync_kiocb(iocb)) {
3825 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3826 return -EIOCBQUEUED;
3827 }
3828
3829 rc = wait_for_completion_killable(&ctx->done);
3830 if (rc) {
3831 mutex_lock(&ctx->aio_mutex);
3832 ctx->rc = rc = -EINTR;
3833 total_written = ctx->total_len;
3834 mutex_unlock(&ctx->aio_mutex);
3835 } else {
3836 rc = ctx->rc;
3837 total_written = ctx->total_len;
3838 }
3839
3840 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3841
3842 if (unlikely(!total_written))
3843 return rc;
3844
3845 iocb->ki_pos += total_written;
3846 return total_written;
3847 }
3848
cifs_direct_writev(struct kiocb * iocb,struct iov_iter * from)3849 ssize_t cifs_direct_writev(struct kiocb *iocb, struct iov_iter *from)
3850 {
3851 struct file *file = iocb->ki_filp;
3852
3853 cifs_revalidate_mapping(file->f_inode);
3854 return __cifs_writev(iocb, from, true);
3855 }
3856
cifs_user_writev(struct kiocb * iocb,struct iov_iter * from)3857 ssize_t cifs_user_writev(struct kiocb *iocb, struct iov_iter *from)
3858 {
3859 return __cifs_writev(iocb, from, false);
3860 }
3861
3862 static ssize_t
cifs_writev(struct kiocb * iocb,struct iov_iter * from)3863 cifs_writev(struct kiocb *iocb, struct iov_iter *from)
3864 {
3865 struct file *file = iocb->ki_filp;
3866 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
3867 struct inode *inode = file->f_mapping->host;
3868 struct cifsInodeInfo *cinode = CIFS_I(inode);
3869 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
3870 ssize_t rc;
3871
3872 inode_lock(inode);
3873 /*
3874 * We need to hold the sem to be sure nobody modifies lock list
3875 * with a brlock that prevents writing.
3876 */
3877 down_read(&cinode->lock_sem);
3878
3879 rc = generic_write_checks(iocb, from);
3880 if (rc <= 0)
3881 goto out;
3882
3883 if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(from),
3884 server->vals->exclusive_lock_type, 0,
3885 NULL, CIFS_WRITE_OP))
3886 rc = __generic_file_write_iter(iocb, from);
3887 else
3888 rc = -EACCES;
3889 out:
3890 up_read(&cinode->lock_sem);
3891 inode_unlock(inode);
3892
3893 if (rc > 0)
3894 rc = generic_write_sync(iocb, rc);
3895 return rc;
3896 }
3897
3898 ssize_t
cifs_strict_writev(struct kiocb * iocb,struct iov_iter * from)3899 cifs_strict_writev(struct kiocb *iocb, struct iov_iter *from)
3900 {
3901 struct inode *inode = file_inode(iocb->ki_filp);
3902 struct cifsInodeInfo *cinode = CIFS_I(inode);
3903 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3904 struct cifsFileInfo *cfile = (struct cifsFileInfo *)
3905 iocb->ki_filp->private_data;
3906 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
3907 ssize_t written;
3908
3909 written = cifs_get_writer(cinode);
3910 if (written)
3911 return written;
3912
3913 if (CIFS_CACHE_WRITE(cinode)) {
3914 if (cap_unix(tcon->ses) &&
3915 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability))
3916 && ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) {
3917 written = generic_file_write_iter(iocb, from);
3918 goto out;
3919 }
3920 written = cifs_writev(iocb, from);
3921 goto out;
3922 }
3923 /*
3924 * For non-oplocked files in strict cache mode we need to write the data
3925 * to the server exactly from the pos to pos+len-1 rather than flush all
3926 * affected pages because it may cause a error with mandatory locks on
3927 * these pages but not on the region from pos to ppos+len-1.
3928 */
3929 written = cifs_user_writev(iocb, from);
3930 if (CIFS_CACHE_READ(cinode)) {
3931 /*
3932 * We have read level caching and we have just sent a write
3933 * request to the server thus making data in the cache stale.
3934 * Zap the cache and set oplock/lease level to NONE to avoid
3935 * reading stale data from the cache. All subsequent read
3936 * operations will read new data from the server.
3937 */
3938 cifs_zap_mapping(inode);
3939 cifs_dbg(FYI, "Set Oplock/Lease to NONE for inode=%p after write\n",
3940 inode);
3941 cinode->oplock = 0;
3942 }
3943 out:
3944 cifs_put_writer(cinode);
3945 return written;
3946 }
3947
cifs_readdata_alloc(work_func_t complete)3948 static struct cifs_readdata *cifs_readdata_alloc(work_func_t complete)
3949 {
3950 struct cifs_readdata *rdata;
3951
3952 rdata = kzalloc(sizeof(*rdata), GFP_KERNEL);
3953 if (rdata) {
3954 kref_init(&rdata->refcount);
3955 INIT_LIST_HEAD(&rdata->list);
3956 init_completion(&rdata->done);
3957 INIT_WORK(&rdata->work, complete);
3958 }
3959
3960 return rdata;
3961 }
3962
3963 void
cifs_readdata_release(struct kref * refcount)3964 cifs_readdata_release(struct kref *refcount)
3965 {
3966 struct cifs_readdata *rdata = container_of(refcount,
3967 struct cifs_readdata, refcount);
3968
3969 if (rdata->ctx)
3970 kref_put(&rdata->ctx->refcount, cifs_aio_ctx_release);
3971 #ifdef CONFIG_CIFS_SMB_DIRECT
3972 if (rdata->mr) {
3973 smbd_deregister_mr(rdata->mr);
3974 rdata->mr = NULL;
3975 }
3976 #endif
3977 if (rdata->cfile)
3978 cifsFileInfo_put(rdata->cfile);
3979
3980 kfree(rdata);
3981 }
3982
3983 static void collect_uncached_read_data(struct cifs_aio_ctx *ctx);
3984
3985 static void
cifs_uncached_readv_complete(struct work_struct * work)3986 cifs_uncached_readv_complete(struct work_struct *work)
3987 {
3988 struct cifs_readdata *rdata = container_of(work,
3989 struct cifs_readdata, work);
3990
3991 complete(&rdata->done);
3992 collect_uncached_read_data(rdata->ctx);
3993 /* the below call can possibly free the last ref to aio ctx */
3994 kref_put(&rdata->refcount, cifs_readdata_release);
3995 }
3996
cifs_resend_rdata(struct cifs_readdata * rdata,struct list_head * rdata_list,struct cifs_aio_ctx * ctx)3997 static int cifs_resend_rdata(struct cifs_readdata *rdata,
3998 struct list_head *rdata_list,
3999 struct cifs_aio_ctx *ctx)
4000 {
4001 unsigned int rsize;
4002 struct cifs_credits credits;
4003 int rc;
4004 struct TCP_Server_Info *server;
4005
4006 /* XXX: should we pick a new channel here? */
4007 server = rdata->server;
4008
4009 do {
4010 if (rdata->cfile->invalidHandle) {
4011 rc = cifs_reopen_file(rdata->cfile, true);
4012 if (rc == -EAGAIN)
4013 continue;
4014 else if (rc)
4015 break;
4016 }
4017
4018 /*
4019 * Wait for credits to resend this rdata.
4020 * Note: we are attempting to resend the whole rdata not in
4021 * segments
4022 */
4023 do {
4024 rc = server->ops->wait_mtu_credits(server, rdata->bytes,
4025 &rsize, &credits);
4026
4027 if (rc)
4028 goto fail;
4029
4030 if (rsize < rdata->bytes) {
4031 add_credits_and_wake_if(server, &credits, 0);
4032 msleep(1000);
4033 }
4034 } while (rsize < rdata->bytes);
4035 rdata->credits = credits;
4036
4037 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
4038 if (!rc) {
4039 if (rdata->cfile->invalidHandle)
4040 rc = -EAGAIN;
4041 else {
4042 #ifdef CONFIG_CIFS_SMB_DIRECT
4043 if (rdata->mr) {
4044 rdata->mr->need_invalidate = true;
4045 smbd_deregister_mr(rdata->mr);
4046 rdata->mr = NULL;
4047 }
4048 #endif
4049 rc = server->ops->async_readv(rdata);
4050 }
4051 }
4052
4053 /* If the read was successfully sent, we are done */
4054 if (!rc) {
4055 /* Add to aio pending list */
4056 list_add_tail(&rdata->list, rdata_list);
4057 return 0;
4058 }
4059
4060 /* Roll back credits and retry if needed */
4061 add_credits_and_wake_if(server, &rdata->credits, 0);
4062 } while (rc == -EAGAIN);
4063
4064 fail:
4065 kref_put(&rdata->refcount, cifs_readdata_release);
4066 return rc;
4067 }
4068
4069 static int
cifs_send_async_read(loff_t fpos,size_t len,struct cifsFileInfo * open_file,struct cifs_sb_info * cifs_sb,struct list_head * rdata_list,struct cifs_aio_ctx * ctx)4070 cifs_send_async_read(loff_t fpos, size_t len, struct cifsFileInfo *open_file,
4071 struct cifs_sb_info *cifs_sb, struct list_head *rdata_list,
4072 struct cifs_aio_ctx *ctx)
4073 {
4074 struct cifs_readdata *rdata;
4075 unsigned int rsize, nsegs, max_segs = INT_MAX;
4076 struct cifs_credits credits_on_stack;
4077 struct cifs_credits *credits = &credits_on_stack;
4078 size_t cur_len, max_len;
4079 int rc;
4080 pid_t pid;
4081 struct TCP_Server_Info *server;
4082
4083 server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses);
4084
4085 #ifdef CONFIG_CIFS_SMB_DIRECT
4086 if (server->smbd_conn)
4087 max_segs = server->smbd_conn->max_frmr_depth;
4088 #endif
4089
4090 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
4091 pid = open_file->pid;
4092 else
4093 pid = current->tgid;
4094
4095 do {
4096 if (open_file->invalidHandle) {
4097 rc = cifs_reopen_file(open_file, true);
4098 if (rc == -EAGAIN)
4099 continue;
4100 else if (rc)
4101 break;
4102 }
4103
4104 if (cifs_sb->ctx->rsize == 0)
4105 cifs_sb->ctx->rsize =
4106 server->ops->negotiate_rsize(tlink_tcon(open_file->tlink),
4107 cifs_sb->ctx);
4108
4109 rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->rsize,
4110 &rsize, credits);
4111 if (rc)
4112 break;
4113
4114 max_len = min_t(size_t, len, rsize);
4115
4116 cur_len = cifs_limit_bvec_subset(&ctx->iter, max_len,
4117 max_segs, &nsegs);
4118 cifs_dbg(FYI, "read-to-iter len=%zx/%zx nsegs=%u/%lu/%u\n",
4119 cur_len, max_len, nsegs, ctx->iter.nr_segs, max_segs);
4120 if (cur_len == 0) {
4121 rc = -EIO;
4122 add_credits_and_wake_if(server, credits, 0);
4123 break;
4124 }
4125
4126 rdata = cifs_readdata_alloc(cifs_uncached_readv_complete);
4127 if (!rdata) {
4128 add_credits_and_wake_if(server, credits, 0);
4129 rc = -ENOMEM;
4130 break;
4131 }
4132
4133 rdata->server = server;
4134 rdata->cfile = cifsFileInfo_get(open_file);
4135 rdata->offset = fpos;
4136 rdata->bytes = cur_len;
4137 rdata->pid = pid;
4138 rdata->credits = credits_on_stack;
4139 rdata->ctx = ctx;
4140 kref_get(&ctx->refcount);
4141
4142 rdata->iter = ctx->iter;
4143 iov_iter_truncate(&rdata->iter, cur_len);
4144
4145 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
4146
4147 if (!rc) {
4148 if (rdata->cfile->invalidHandle)
4149 rc = -EAGAIN;
4150 else
4151 rc = server->ops->async_readv(rdata);
4152 }
4153
4154 if (rc) {
4155 add_credits_and_wake_if(server, &rdata->credits, 0);
4156 kref_put(&rdata->refcount, cifs_readdata_release);
4157 if (rc == -EAGAIN)
4158 continue;
4159 break;
4160 }
4161
4162 list_add_tail(&rdata->list, rdata_list);
4163 iov_iter_advance(&ctx->iter, cur_len);
4164 fpos += cur_len;
4165 len -= cur_len;
4166 } while (len > 0);
4167
4168 return rc;
4169 }
4170
4171 static void
collect_uncached_read_data(struct cifs_aio_ctx * ctx)4172 collect_uncached_read_data(struct cifs_aio_ctx *ctx)
4173 {
4174 struct cifs_readdata *rdata, *tmp;
4175 struct cifs_sb_info *cifs_sb;
4176 int rc;
4177
4178 cifs_sb = CIFS_SB(ctx->cfile->dentry->d_sb);
4179
4180 mutex_lock(&ctx->aio_mutex);
4181
4182 if (list_empty(&ctx->list)) {
4183 mutex_unlock(&ctx->aio_mutex);
4184 return;
4185 }
4186
4187 rc = ctx->rc;
4188 /* the loop below should proceed in the order of increasing offsets */
4189 again:
4190 list_for_each_entry_safe(rdata, tmp, &ctx->list, list) {
4191 if (!rc) {
4192 if (!try_wait_for_completion(&rdata->done)) {
4193 mutex_unlock(&ctx->aio_mutex);
4194 return;
4195 }
4196
4197 if (rdata->result == -EAGAIN) {
4198 /* resend call if it's a retryable error */
4199 struct list_head tmp_list;
4200 unsigned int got_bytes = rdata->got_bytes;
4201
4202 list_del_init(&rdata->list);
4203 INIT_LIST_HEAD(&tmp_list);
4204
4205 if (ctx->direct_io) {
4206 /*
4207 * Re-use rdata as this is a
4208 * direct I/O
4209 */
4210 rc = cifs_resend_rdata(
4211 rdata,
4212 &tmp_list, ctx);
4213 } else {
4214 rc = cifs_send_async_read(
4215 rdata->offset + got_bytes,
4216 rdata->bytes - got_bytes,
4217 rdata->cfile, cifs_sb,
4218 &tmp_list, ctx);
4219
4220 kref_put(&rdata->refcount,
4221 cifs_readdata_release);
4222 }
4223
4224 list_splice(&tmp_list, &ctx->list);
4225
4226 goto again;
4227 } else if (rdata->result)
4228 rc = rdata->result;
4229
4230 /* if there was a short read -- discard anything left */
4231 if (rdata->got_bytes && rdata->got_bytes < rdata->bytes)
4232 rc = -ENODATA;
4233
4234 ctx->total_len += rdata->got_bytes;
4235 }
4236 list_del_init(&rdata->list);
4237 kref_put(&rdata->refcount, cifs_readdata_release);
4238 }
4239
4240 /* mask nodata case */
4241 if (rc == -ENODATA)
4242 rc = 0;
4243
4244 ctx->rc = (rc == 0) ? (ssize_t)ctx->total_len : rc;
4245
4246 mutex_unlock(&ctx->aio_mutex);
4247
4248 if (ctx->iocb && ctx->iocb->ki_complete)
4249 ctx->iocb->ki_complete(ctx->iocb, ctx->rc);
4250 else
4251 complete(&ctx->done);
4252 }
4253
__cifs_readv(struct kiocb * iocb,struct iov_iter * to,bool direct)4254 static ssize_t __cifs_readv(
4255 struct kiocb *iocb, struct iov_iter *to, bool direct)
4256 {
4257 size_t len;
4258 struct file *file = iocb->ki_filp;
4259 struct cifs_sb_info *cifs_sb;
4260 struct cifsFileInfo *cfile;
4261 struct cifs_tcon *tcon;
4262 ssize_t rc, total_read = 0;
4263 loff_t offset = iocb->ki_pos;
4264 struct cifs_aio_ctx *ctx;
4265
4266 len = iov_iter_count(to);
4267 if (!len)
4268 return 0;
4269
4270 cifs_sb = CIFS_FILE_SB(file);
4271 cfile = file->private_data;
4272 tcon = tlink_tcon(cfile->tlink);
4273
4274 if (!tcon->ses->server->ops->async_readv)
4275 return -ENOSYS;
4276
4277 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
4278 cifs_dbg(FYI, "attempting read on write only file instance\n");
4279
4280 ctx = cifs_aio_ctx_alloc();
4281 if (!ctx)
4282 return -ENOMEM;
4283
4284 ctx->pos = offset;
4285 ctx->direct_io = direct;
4286 ctx->len = len;
4287 ctx->cfile = cifsFileInfo_get(cfile);
4288 ctx->nr_pinned_pages = 0;
4289
4290 if (!is_sync_kiocb(iocb))
4291 ctx->iocb = iocb;
4292
4293 if (user_backed_iter(to)) {
4294 /*
4295 * Extract IOVEC/UBUF-type iterators to a BVEC-type iterator as
4296 * they contain references to the calling process's virtual
4297 * memory layout which won't be available in an async worker
4298 * thread. This also takes a pin on every folio involved.
4299 */
4300 rc = netfs_extract_user_iter(to, iov_iter_count(to),
4301 &ctx->iter, 0);
4302 if (rc < 0) {
4303 kref_put(&ctx->refcount, cifs_aio_ctx_release);
4304 return rc;
4305 }
4306
4307 ctx->nr_pinned_pages = rc;
4308 ctx->bv = (void *)ctx->iter.bvec;
4309 ctx->bv_need_unpin = iov_iter_extract_will_pin(to);
4310 ctx->should_dirty = true;
4311 } else if ((iov_iter_is_bvec(to) || iov_iter_is_kvec(to)) &&
4312 !is_sync_kiocb(iocb)) {
4313 /*
4314 * If the op is asynchronous, we need to copy the list attached
4315 * to a BVEC/KVEC-type iterator, but we assume that the storage
4316 * will be retained by the caller; in any case, we may or may
4317 * not be able to pin the pages, so we don't try.
4318 */
4319 ctx->bv = (void *)dup_iter(&ctx->iter, to, GFP_KERNEL);
4320 if (!ctx->bv) {
4321 kref_put(&ctx->refcount, cifs_aio_ctx_release);
4322 return -ENOMEM;
4323 }
4324 } else {
4325 /*
4326 * Otherwise, we just pass the iterator down as-is and rely on
4327 * the caller to make sure the pages referred to by the
4328 * iterator don't evaporate.
4329 */
4330 ctx->iter = *to;
4331 }
4332
4333 if (direct) {
4334 rc = filemap_write_and_wait_range(file->f_inode->i_mapping,
4335 offset, offset + len - 1);
4336 if (rc) {
4337 kref_put(&ctx->refcount, cifs_aio_ctx_release);
4338 return -EAGAIN;
4339 }
4340 }
4341
4342 /* grab a lock here due to read response handlers can access ctx */
4343 mutex_lock(&ctx->aio_mutex);
4344
4345 rc = cifs_send_async_read(offset, len, cfile, cifs_sb, &ctx->list, ctx);
4346
4347 /* if at least one read request send succeeded, then reset rc */
4348 if (!list_empty(&ctx->list))
4349 rc = 0;
4350
4351 mutex_unlock(&ctx->aio_mutex);
4352
4353 if (rc) {
4354 kref_put(&ctx->refcount, cifs_aio_ctx_release);
4355 return rc;
4356 }
4357
4358 if (!is_sync_kiocb(iocb)) {
4359 kref_put(&ctx->refcount, cifs_aio_ctx_release);
4360 return -EIOCBQUEUED;
4361 }
4362
4363 rc = wait_for_completion_killable(&ctx->done);
4364 if (rc) {
4365 mutex_lock(&ctx->aio_mutex);
4366 ctx->rc = rc = -EINTR;
4367 total_read = ctx->total_len;
4368 mutex_unlock(&ctx->aio_mutex);
4369 } else {
4370 rc = ctx->rc;
4371 total_read = ctx->total_len;
4372 }
4373
4374 kref_put(&ctx->refcount, cifs_aio_ctx_release);
4375
4376 if (total_read) {
4377 iocb->ki_pos += total_read;
4378 return total_read;
4379 }
4380 return rc;
4381 }
4382
cifs_direct_readv(struct kiocb * iocb,struct iov_iter * to)4383 ssize_t cifs_direct_readv(struct kiocb *iocb, struct iov_iter *to)
4384 {
4385 return __cifs_readv(iocb, to, true);
4386 }
4387
cifs_user_readv(struct kiocb * iocb,struct iov_iter * to)4388 ssize_t cifs_user_readv(struct kiocb *iocb, struct iov_iter *to)
4389 {
4390 return __cifs_readv(iocb, to, false);
4391 }
4392
4393 ssize_t
cifs_strict_readv(struct kiocb * iocb,struct iov_iter * to)4394 cifs_strict_readv(struct kiocb *iocb, struct iov_iter *to)
4395 {
4396 struct inode *inode = file_inode(iocb->ki_filp);
4397 struct cifsInodeInfo *cinode = CIFS_I(inode);
4398 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
4399 struct cifsFileInfo *cfile = (struct cifsFileInfo *)
4400 iocb->ki_filp->private_data;
4401 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
4402 int rc = -EACCES;
4403
4404 /*
4405 * In strict cache mode we need to read from the server all the time
4406 * if we don't have level II oplock because the server can delay mtime
4407 * change - so we can't make a decision about inode invalidating.
4408 * And we can also fail with pagereading if there are mandatory locks
4409 * on pages affected by this read but not on the region from pos to
4410 * pos+len-1.
4411 */
4412 if (!CIFS_CACHE_READ(cinode))
4413 return cifs_user_readv(iocb, to);
4414
4415 if (cap_unix(tcon->ses) &&
4416 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
4417 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
4418 return generic_file_read_iter(iocb, to);
4419
4420 /*
4421 * We need to hold the sem to be sure nobody modifies lock list
4422 * with a brlock that prevents reading.
4423 */
4424 down_read(&cinode->lock_sem);
4425 if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(to),
4426 tcon->ses->server->vals->shared_lock_type,
4427 0, NULL, CIFS_READ_OP))
4428 rc = generic_file_read_iter(iocb, to);
4429 up_read(&cinode->lock_sem);
4430 return rc;
4431 }
4432
4433 static ssize_t
cifs_read(struct file * file,char * read_data,size_t read_size,loff_t * offset)4434 cifs_read(struct file *file, char *read_data, size_t read_size, loff_t *offset)
4435 {
4436 int rc = -EACCES;
4437 unsigned int bytes_read = 0;
4438 unsigned int total_read;
4439 unsigned int current_read_size;
4440 unsigned int rsize;
4441 struct cifs_sb_info *cifs_sb;
4442 struct cifs_tcon *tcon;
4443 struct TCP_Server_Info *server;
4444 unsigned int xid;
4445 char *cur_offset;
4446 struct cifsFileInfo *open_file;
4447 struct cifs_io_parms io_parms = {0};
4448 int buf_type = CIFS_NO_BUFFER;
4449 __u32 pid;
4450
4451 xid = get_xid();
4452 cifs_sb = CIFS_FILE_SB(file);
4453
4454 /* FIXME: set up handlers for larger reads and/or convert to async */
4455 rsize = min_t(unsigned int, cifs_sb->ctx->rsize, CIFSMaxBufSize);
4456
4457 if (file->private_data == NULL) {
4458 rc = -EBADF;
4459 free_xid(xid);
4460 return rc;
4461 }
4462 open_file = file->private_data;
4463 tcon = tlink_tcon(open_file->tlink);
4464 server = cifs_pick_channel(tcon->ses);
4465
4466 if (!server->ops->sync_read) {
4467 free_xid(xid);
4468 return -ENOSYS;
4469 }
4470
4471 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
4472 pid = open_file->pid;
4473 else
4474 pid = current->tgid;
4475
4476 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
4477 cifs_dbg(FYI, "attempting read on write only file instance\n");
4478
4479 for (total_read = 0, cur_offset = read_data; read_size > total_read;
4480 total_read += bytes_read, cur_offset += bytes_read) {
4481 do {
4482 current_read_size = min_t(uint, read_size - total_read,
4483 rsize);
4484 /*
4485 * For windows me and 9x we do not want to request more
4486 * than it negotiated since it will refuse the read
4487 * then.
4488 */
4489 if (!(tcon->ses->capabilities &
4490 tcon->ses->server->vals->cap_large_files)) {
4491 current_read_size = min_t(uint,
4492 current_read_size, CIFSMaxBufSize);
4493 }
4494 if (open_file->invalidHandle) {
4495 rc = cifs_reopen_file(open_file, true);
4496 if (rc != 0)
4497 break;
4498 }
4499 io_parms.pid = pid;
4500 io_parms.tcon = tcon;
4501 io_parms.offset = *offset;
4502 io_parms.length = current_read_size;
4503 io_parms.server = server;
4504 rc = server->ops->sync_read(xid, &open_file->fid, &io_parms,
4505 &bytes_read, &cur_offset,
4506 &buf_type);
4507 } while (rc == -EAGAIN);
4508
4509 if (rc || (bytes_read == 0)) {
4510 if (total_read) {
4511 break;
4512 } else {
4513 free_xid(xid);
4514 return rc;
4515 }
4516 } else {
4517 cifs_stats_bytes_read(tcon, total_read);
4518 *offset += bytes_read;
4519 }
4520 }
4521 free_xid(xid);
4522 return total_read;
4523 }
4524
4525 /*
4526 * If the page is mmap'ed into a process' page tables, then we need to make
4527 * sure that it doesn't change while being written back.
4528 */
cifs_page_mkwrite(struct vm_fault * vmf)4529 static vm_fault_t cifs_page_mkwrite(struct vm_fault *vmf)
4530 {
4531 struct folio *folio = page_folio(vmf->page);
4532
4533 /* Wait for the folio to be written to the cache before we allow it to
4534 * be modified. We then assume the entire folio will need writing back.
4535 */
4536 #ifdef CONFIG_CIFS_FSCACHE
4537 if (folio_test_fscache(folio) &&
4538 folio_wait_fscache_killable(folio) < 0)
4539 return VM_FAULT_RETRY;
4540 #endif
4541
4542 folio_wait_writeback(folio);
4543
4544 if (folio_lock_killable(folio) < 0)
4545 return VM_FAULT_RETRY;
4546 return VM_FAULT_LOCKED;
4547 }
4548
4549 static const struct vm_operations_struct cifs_file_vm_ops = {
4550 .fault = filemap_fault,
4551 .map_pages = filemap_map_pages,
4552 .page_mkwrite = cifs_page_mkwrite,
4553 };
4554
cifs_file_strict_mmap(struct file * file,struct vm_area_struct * vma)4555 int cifs_file_strict_mmap(struct file *file, struct vm_area_struct *vma)
4556 {
4557 int xid, rc = 0;
4558 struct inode *inode = file_inode(file);
4559
4560 xid = get_xid();
4561
4562 if (!CIFS_CACHE_READ(CIFS_I(inode)))
4563 rc = cifs_zap_mapping(inode);
4564 if (!rc)
4565 rc = generic_file_mmap(file, vma);
4566 if (!rc)
4567 vma->vm_ops = &cifs_file_vm_ops;
4568
4569 free_xid(xid);
4570 return rc;
4571 }
4572
cifs_file_mmap(struct file * file,struct vm_area_struct * vma)4573 int cifs_file_mmap(struct file *file, struct vm_area_struct *vma)
4574 {
4575 int rc, xid;
4576
4577 xid = get_xid();
4578
4579 rc = cifs_revalidate_file(file);
4580 if (rc)
4581 cifs_dbg(FYI, "Validation prior to mmap failed, error=%d\n",
4582 rc);
4583 if (!rc)
4584 rc = generic_file_mmap(file, vma);
4585 if (!rc)
4586 vma->vm_ops = &cifs_file_vm_ops;
4587
4588 free_xid(xid);
4589 return rc;
4590 }
4591
4592 /*
4593 * Unlock a bunch of folios in the pagecache.
4594 */
cifs_unlock_folios(struct address_space * mapping,pgoff_t first,pgoff_t last)4595 static void cifs_unlock_folios(struct address_space *mapping, pgoff_t first, pgoff_t last)
4596 {
4597 struct folio *folio;
4598 XA_STATE(xas, &mapping->i_pages, first);
4599
4600 rcu_read_lock();
4601 xas_for_each(&xas, folio, last) {
4602 folio_unlock(folio);
4603 }
4604 rcu_read_unlock();
4605 }
4606
cifs_readahead_complete(struct work_struct * work)4607 static void cifs_readahead_complete(struct work_struct *work)
4608 {
4609 struct cifs_readdata *rdata = container_of(work,
4610 struct cifs_readdata, work);
4611 struct folio *folio;
4612 pgoff_t last;
4613 bool good = rdata->result == 0 || (rdata->result == -EAGAIN && rdata->got_bytes);
4614
4615 XA_STATE(xas, &rdata->mapping->i_pages, rdata->offset / PAGE_SIZE);
4616
4617 if (good)
4618 cifs_readahead_to_fscache(rdata->mapping->host,
4619 rdata->offset, rdata->bytes);
4620
4621 if (iov_iter_count(&rdata->iter) > 0)
4622 iov_iter_zero(iov_iter_count(&rdata->iter), &rdata->iter);
4623
4624 last = (rdata->offset + rdata->bytes - 1) / PAGE_SIZE;
4625
4626 rcu_read_lock();
4627 xas_for_each(&xas, folio, last) {
4628 if (good) {
4629 flush_dcache_folio(folio);
4630 folio_mark_uptodate(folio);
4631 }
4632 folio_unlock(folio);
4633 }
4634 rcu_read_unlock();
4635
4636 kref_put(&rdata->refcount, cifs_readdata_release);
4637 }
4638
cifs_readahead(struct readahead_control * ractl)4639 static void cifs_readahead(struct readahead_control *ractl)
4640 {
4641 struct cifsFileInfo *open_file = ractl->file->private_data;
4642 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(ractl->file);
4643 struct TCP_Server_Info *server;
4644 unsigned int xid, nr_pages, cache_nr_pages = 0;
4645 unsigned int ra_pages;
4646 pgoff_t next_cached = ULONG_MAX, ra_index;
4647 bool caching = fscache_cookie_enabled(cifs_inode_cookie(ractl->mapping->host)) &&
4648 cifs_inode_cookie(ractl->mapping->host)->cache_priv;
4649 bool check_cache = caching;
4650 pid_t pid;
4651 int rc = 0;
4652
4653 /* Note that readahead_count() lags behind our dequeuing of pages from
4654 * the ractl, wo we have to keep track for ourselves.
4655 */
4656 ra_pages = readahead_count(ractl);
4657 ra_index = readahead_index(ractl);
4658
4659 xid = get_xid();
4660
4661 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
4662 pid = open_file->pid;
4663 else
4664 pid = current->tgid;
4665
4666 server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses);
4667
4668 cifs_dbg(FYI, "%s: file=%p mapping=%p num_pages=%u\n",
4669 __func__, ractl->file, ractl->mapping, ra_pages);
4670
4671 /*
4672 * Chop the readahead request up into rsize-sized read requests.
4673 */
4674 while ((nr_pages = ra_pages)) {
4675 unsigned int i, rsize;
4676 struct cifs_readdata *rdata;
4677 struct cifs_credits credits_on_stack;
4678 struct cifs_credits *credits = &credits_on_stack;
4679 struct folio *folio;
4680 pgoff_t fsize;
4681
4682 /*
4683 * Find out if we have anything cached in the range of
4684 * interest, and if so, where the next chunk of cached data is.
4685 */
4686 if (caching) {
4687 if (check_cache) {
4688 rc = cifs_fscache_query_occupancy(
4689 ractl->mapping->host, ra_index, nr_pages,
4690 &next_cached, &cache_nr_pages);
4691 if (rc < 0)
4692 caching = false;
4693 check_cache = false;
4694 }
4695
4696 if (ra_index == next_cached) {
4697 /*
4698 * TODO: Send a whole batch of pages to be read
4699 * by the cache.
4700 */
4701 folio = readahead_folio(ractl);
4702 fsize = folio_nr_pages(folio);
4703 ra_pages -= fsize;
4704 ra_index += fsize;
4705 if (cifs_readpage_from_fscache(ractl->mapping->host,
4706 &folio->page) < 0) {
4707 /*
4708 * TODO: Deal with cache read failure
4709 * here, but for the moment, delegate
4710 * that to readpage.
4711 */
4712 caching = false;
4713 }
4714 folio_unlock(folio);
4715 next_cached += fsize;
4716 cache_nr_pages -= fsize;
4717 if (cache_nr_pages == 0)
4718 check_cache = true;
4719 continue;
4720 }
4721 }
4722
4723 if (open_file->invalidHandle) {
4724 rc = cifs_reopen_file(open_file, true);
4725 if (rc) {
4726 if (rc == -EAGAIN)
4727 continue;
4728 break;
4729 }
4730 }
4731
4732 if (cifs_sb->ctx->rsize == 0)
4733 cifs_sb->ctx->rsize =
4734 server->ops->negotiate_rsize(tlink_tcon(open_file->tlink),
4735 cifs_sb->ctx);
4736
4737 rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->rsize,
4738 &rsize, credits);
4739 if (rc)
4740 break;
4741 nr_pages = min_t(size_t, rsize / PAGE_SIZE, ra_pages);
4742 if (next_cached != ULONG_MAX)
4743 nr_pages = min_t(size_t, nr_pages, next_cached - ra_index);
4744
4745 /*
4746 * Give up immediately if rsize is too small to read an entire
4747 * page. The VFS will fall back to readpage. We should never
4748 * reach this point however since we set ra_pages to 0 when the
4749 * rsize is smaller than a cache page.
4750 */
4751 if (unlikely(!nr_pages)) {
4752 add_credits_and_wake_if(server, credits, 0);
4753 break;
4754 }
4755
4756 rdata = cifs_readdata_alloc(cifs_readahead_complete);
4757 if (!rdata) {
4758 /* best to give up if we're out of mem */
4759 add_credits_and_wake_if(server, credits, 0);
4760 break;
4761 }
4762
4763 rdata->offset = ra_index * PAGE_SIZE;
4764 rdata->bytes = nr_pages * PAGE_SIZE;
4765 rdata->cfile = cifsFileInfo_get(open_file);
4766 rdata->server = server;
4767 rdata->mapping = ractl->mapping;
4768 rdata->pid = pid;
4769 rdata->credits = credits_on_stack;
4770
4771 for (i = 0; i < nr_pages; i++) {
4772 if (!readahead_folio(ractl))
4773 WARN_ON(1);
4774 }
4775 ra_pages -= nr_pages;
4776 ra_index += nr_pages;
4777
4778 iov_iter_xarray(&rdata->iter, ITER_DEST, &rdata->mapping->i_pages,
4779 rdata->offset, rdata->bytes);
4780
4781 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
4782 if (!rc) {
4783 if (rdata->cfile->invalidHandle)
4784 rc = -EAGAIN;
4785 else
4786 rc = server->ops->async_readv(rdata);
4787 }
4788
4789 if (rc) {
4790 add_credits_and_wake_if(server, &rdata->credits, 0);
4791 cifs_unlock_folios(rdata->mapping,
4792 rdata->offset / PAGE_SIZE,
4793 (rdata->offset + rdata->bytes - 1) / PAGE_SIZE);
4794 /* Fallback to the readpage in error/reconnect cases */
4795 kref_put(&rdata->refcount, cifs_readdata_release);
4796 break;
4797 }
4798
4799 kref_put(&rdata->refcount, cifs_readdata_release);
4800 }
4801
4802 free_xid(xid);
4803 }
4804
4805 /*
4806 * cifs_readpage_worker must be called with the page pinned
4807 */
cifs_readpage_worker(struct file * file,struct page * page,loff_t * poffset)4808 static int cifs_readpage_worker(struct file *file, struct page *page,
4809 loff_t *poffset)
4810 {
4811 struct inode *inode = file_inode(file);
4812 struct timespec64 atime, mtime;
4813 char *read_data;
4814 int rc;
4815
4816 /* Is the page cached? */
4817 rc = cifs_readpage_from_fscache(inode, page);
4818 if (rc == 0)
4819 goto read_complete;
4820
4821 read_data = kmap(page);
4822 /* for reads over a certain size could initiate async read ahead */
4823
4824 rc = cifs_read(file, read_data, PAGE_SIZE, poffset);
4825
4826 if (rc < 0)
4827 goto io_error;
4828 else
4829 cifs_dbg(FYI, "Bytes read %d\n", rc);
4830
4831 /* we do not want atime to be less than mtime, it broke some apps */
4832 atime = inode_set_atime_to_ts(inode, current_time(inode));
4833 mtime = inode_get_mtime(inode);
4834 if (timespec64_compare(&atime, &mtime) < 0)
4835 inode_set_atime_to_ts(inode, inode_get_mtime(inode));
4836
4837 if (PAGE_SIZE > rc)
4838 memset(read_data + rc, 0, PAGE_SIZE - rc);
4839
4840 flush_dcache_page(page);
4841 SetPageUptodate(page);
4842 rc = 0;
4843
4844 io_error:
4845 kunmap(page);
4846
4847 read_complete:
4848 unlock_page(page);
4849 return rc;
4850 }
4851
cifs_read_folio(struct file * file,struct folio * folio)4852 static int cifs_read_folio(struct file *file, struct folio *folio)
4853 {
4854 struct page *page = &folio->page;
4855 loff_t offset = page_file_offset(page);
4856 int rc = -EACCES;
4857 unsigned int xid;
4858
4859 xid = get_xid();
4860
4861 if (file->private_data == NULL) {
4862 rc = -EBADF;
4863 free_xid(xid);
4864 return rc;
4865 }
4866
4867 cifs_dbg(FYI, "read_folio %p at offset %d 0x%x\n",
4868 page, (int)offset, (int)offset);
4869
4870 rc = cifs_readpage_worker(file, page, &offset);
4871
4872 free_xid(xid);
4873 return rc;
4874 }
4875
is_inode_writable(struct cifsInodeInfo * cifs_inode)4876 static int is_inode_writable(struct cifsInodeInfo *cifs_inode)
4877 {
4878 struct cifsFileInfo *open_file;
4879
4880 spin_lock(&cifs_inode->open_file_lock);
4881 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
4882 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
4883 spin_unlock(&cifs_inode->open_file_lock);
4884 return 1;
4885 }
4886 }
4887 spin_unlock(&cifs_inode->open_file_lock);
4888 return 0;
4889 }
4890
4891 /* We do not want to update the file size from server for inodes
4892 open for write - to avoid races with writepage extending
4893 the file - in the future we could consider allowing
4894 refreshing the inode only on increases in the file size
4895 but this is tricky to do without racing with writebehind
4896 page caching in the current Linux kernel design */
is_size_safe_to_change(struct cifsInodeInfo * cifsInode,__u64 end_of_file,bool from_readdir)4897 bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file,
4898 bool from_readdir)
4899 {
4900 if (!cifsInode)
4901 return true;
4902
4903 if (is_inode_writable(cifsInode) ||
4904 ((cifsInode->oplock & CIFS_CACHE_RW_FLG) != 0 && from_readdir)) {
4905 /* This inode is open for write at least once */
4906 struct cifs_sb_info *cifs_sb;
4907
4908 cifs_sb = CIFS_SB(cifsInode->netfs.inode.i_sb);
4909 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
4910 /* since no page cache to corrupt on directio
4911 we can change size safely */
4912 return true;
4913 }
4914
4915 if (i_size_read(&cifsInode->netfs.inode) < end_of_file)
4916 return true;
4917
4918 return false;
4919 } else
4920 return true;
4921 }
4922
cifs_write_begin(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,struct page ** pagep,void ** fsdata)4923 static int cifs_write_begin(struct file *file, struct address_space *mapping,
4924 loff_t pos, unsigned len,
4925 struct page **pagep, void **fsdata)
4926 {
4927 int oncethru = 0;
4928 pgoff_t index = pos >> PAGE_SHIFT;
4929 loff_t offset = pos & (PAGE_SIZE - 1);
4930 loff_t page_start = pos & PAGE_MASK;
4931 loff_t i_size;
4932 struct page *page;
4933 int rc = 0;
4934
4935 cifs_dbg(FYI, "write_begin from %lld len %d\n", (long long)pos, len);
4936
4937 start:
4938 page = grab_cache_page_write_begin(mapping, index);
4939 if (!page) {
4940 rc = -ENOMEM;
4941 goto out;
4942 }
4943
4944 if (PageUptodate(page))
4945 goto out;
4946
4947 /*
4948 * If we write a full page it will be up to date, no need to read from
4949 * the server. If the write is short, we'll end up doing a sync write
4950 * instead.
4951 */
4952 if (len == PAGE_SIZE)
4953 goto out;
4954
4955 /*
4956 * optimize away the read when we have an oplock, and we're not
4957 * expecting to use any of the data we'd be reading in. That
4958 * is, when the page lies beyond the EOF, or straddles the EOF
4959 * and the write will cover all of the existing data.
4960 */
4961 if (CIFS_CACHE_READ(CIFS_I(mapping->host))) {
4962 i_size = i_size_read(mapping->host);
4963 if (page_start >= i_size ||
4964 (offset == 0 && (pos + len) >= i_size)) {
4965 zero_user_segments(page, 0, offset,
4966 offset + len,
4967 PAGE_SIZE);
4968 /*
4969 * PageChecked means that the parts of the page
4970 * to which we're not writing are considered up
4971 * to date. Once the data is copied to the
4972 * page, it can be set uptodate.
4973 */
4974 SetPageChecked(page);
4975 goto out;
4976 }
4977 }
4978
4979 if ((file->f_flags & O_ACCMODE) != O_WRONLY && !oncethru) {
4980 /*
4981 * might as well read a page, it is fast enough. If we get
4982 * an error, we don't need to return it. cifs_write_end will
4983 * do a sync write instead since PG_uptodate isn't set.
4984 */
4985 cifs_readpage_worker(file, page, &page_start);
4986 put_page(page);
4987 oncethru = 1;
4988 goto start;
4989 } else {
4990 /* we could try using another file handle if there is one -
4991 but how would we lock it to prevent close of that handle
4992 racing with this read? In any case
4993 this will be written out by write_end so is fine */
4994 }
4995 out:
4996 *pagep = page;
4997 return rc;
4998 }
4999
cifs_release_folio(struct folio * folio,gfp_t gfp)5000 static bool cifs_release_folio(struct folio *folio, gfp_t gfp)
5001 {
5002 if (folio_test_private(folio))
5003 return 0;
5004 if (folio_test_fscache(folio)) {
5005 if (current_is_kswapd() || !(gfp & __GFP_FS))
5006 return false;
5007 folio_wait_fscache(folio);
5008 }
5009 fscache_note_page_release(cifs_inode_cookie(folio->mapping->host));
5010 return true;
5011 }
5012
cifs_invalidate_folio(struct folio * folio,size_t offset,size_t length)5013 static void cifs_invalidate_folio(struct folio *folio, size_t offset,
5014 size_t length)
5015 {
5016 folio_wait_fscache(folio);
5017 }
5018
cifs_launder_folio(struct folio * folio)5019 static int cifs_launder_folio(struct folio *folio)
5020 {
5021 int rc = 0;
5022 loff_t range_start = folio_pos(folio);
5023 loff_t range_end = range_start + folio_size(folio);
5024 struct writeback_control wbc = {
5025 .sync_mode = WB_SYNC_ALL,
5026 .nr_to_write = 0,
5027 .range_start = range_start,
5028 .range_end = range_end,
5029 };
5030
5031 cifs_dbg(FYI, "Launder page: %lu\n", folio->index);
5032
5033 if (folio_clear_dirty_for_io(folio))
5034 rc = cifs_writepage_locked(&folio->page, &wbc);
5035
5036 folio_wait_fscache(folio);
5037 return rc;
5038 }
5039
cifs_oplock_break(struct work_struct * work)5040 void cifs_oplock_break(struct work_struct *work)
5041 {
5042 struct cifsFileInfo *cfile = container_of(work, struct cifsFileInfo,
5043 oplock_break);
5044 struct inode *inode = d_inode(cfile->dentry);
5045 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
5046 struct cifsInodeInfo *cinode = CIFS_I(inode);
5047 struct cifs_tcon *tcon;
5048 struct TCP_Server_Info *server;
5049 struct tcon_link *tlink;
5050 int rc = 0;
5051 bool purge_cache = false, oplock_break_cancelled;
5052 __u64 persistent_fid, volatile_fid;
5053 __u16 net_fid;
5054
5055 wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS,
5056 TASK_UNINTERRUPTIBLE);
5057
5058 tlink = cifs_sb_tlink(cifs_sb);
5059 if (IS_ERR(tlink))
5060 goto out;
5061 tcon = tlink_tcon(tlink);
5062 server = tcon->ses->server;
5063
5064 server->ops->downgrade_oplock(server, cinode, cfile->oplock_level,
5065 cfile->oplock_epoch, &purge_cache);
5066
5067 if (!CIFS_CACHE_WRITE(cinode) && CIFS_CACHE_READ(cinode) &&
5068 cifs_has_mand_locks(cinode)) {
5069 cifs_dbg(FYI, "Reset oplock to None for inode=%p due to mand locks\n",
5070 inode);
5071 cinode->oplock = 0;
5072 }
5073
5074 if (inode && S_ISREG(inode->i_mode)) {
5075 if (CIFS_CACHE_READ(cinode))
5076 break_lease(inode, O_RDONLY);
5077 else
5078 break_lease(inode, O_WRONLY);
5079 rc = filemap_fdatawrite(inode->i_mapping);
5080 if (!CIFS_CACHE_READ(cinode) || purge_cache) {
5081 rc = filemap_fdatawait(inode->i_mapping);
5082 mapping_set_error(inode->i_mapping, rc);
5083 cifs_zap_mapping(inode);
5084 }
5085 cifs_dbg(FYI, "Oplock flush inode %p rc %d\n", inode, rc);
5086 if (CIFS_CACHE_WRITE(cinode))
5087 goto oplock_break_ack;
5088 }
5089
5090 rc = cifs_push_locks(cfile);
5091 if (rc)
5092 cifs_dbg(VFS, "Push locks rc = %d\n", rc);
5093
5094 oplock_break_ack:
5095 /*
5096 * When oplock break is received and there are no active
5097 * file handles but cached, then schedule deferred close immediately.
5098 * So, new open will not use cached handle.
5099 */
5100
5101 if (!CIFS_CACHE_HANDLE(cinode) && !list_empty(&cinode->deferred_closes))
5102 cifs_close_deferred_file(cinode);
5103
5104 persistent_fid = cfile->fid.persistent_fid;
5105 volatile_fid = cfile->fid.volatile_fid;
5106 net_fid = cfile->fid.netfid;
5107 oplock_break_cancelled = cfile->oplock_break_cancelled;
5108
5109 _cifsFileInfo_put(cfile, false /* do not wait for ourself */, false);
5110 /*
5111 * MS-SMB2 3.2.5.19.1 and 3.2.5.19.2 (and MS-CIFS 3.2.5.42) do not require
5112 * an acknowledgment to be sent when the file has already been closed.
5113 */
5114 spin_lock(&cinode->open_file_lock);
5115 /* check list empty since can race with kill_sb calling tree disconnect */
5116 if (!oplock_break_cancelled && !list_empty(&cinode->openFileList)) {
5117 spin_unlock(&cinode->open_file_lock);
5118 rc = server->ops->oplock_response(tcon, persistent_fid,
5119 volatile_fid, net_fid, cinode);
5120 cifs_dbg(FYI, "Oplock release rc = %d\n", rc);
5121 } else
5122 spin_unlock(&cinode->open_file_lock);
5123
5124 cifs_put_tlink(tlink);
5125 out:
5126 cifs_done_oplock_break(cinode);
5127 }
5128
5129 /*
5130 * The presence of cifs_direct_io() in the address space ops vector
5131 * allowes open() O_DIRECT flags which would have failed otherwise.
5132 *
5133 * In the non-cached mode (mount with cache=none), we shunt off direct read and write requests
5134 * so this method should never be called.
5135 *
5136 * Direct IO is not yet supported in the cached mode.
5137 */
5138 static ssize_t
cifs_direct_io(struct kiocb * iocb,struct iov_iter * iter)5139 cifs_direct_io(struct kiocb *iocb, struct iov_iter *iter)
5140 {
5141 /*
5142 * FIXME
5143 * Eventually need to support direct IO for non forcedirectio mounts
5144 */
5145 return -EINVAL;
5146 }
5147
cifs_swap_activate(struct swap_info_struct * sis,struct file * swap_file,sector_t * span)5148 static int cifs_swap_activate(struct swap_info_struct *sis,
5149 struct file *swap_file, sector_t *span)
5150 {
5151 struct cifsFileInfo *cfile = swap_file->private_data;
5152 struct inode *inode = swap_file->f_mapping->host;
5153 unsigned long blocks;
5154 long long isize;
5155
5156 cifs_dbg(FYI, "swap activate\n");
5157
5158 if (!swap_file->f_mapping->a_ops->swap_rw)
5159 /* Cannot support swap */
5160 return -EINVAL;
5161
5162 spin_lock(&inode->i_lock);
5163 blocks = inode->i_blocks;
5164 isize = inode->i_size;
5165 spin_unlock(&inode->i_lock);
5166 if (blocks*512 < isize) {
5167 pr_warn("swap activate: swapfile has holes\n");
5168 return -EINVAL;
5169 }
5170 *span = sis->pages;
5171
5172 pr_warn_once("Swap support over SMB3 is experimental\n");
5173
5174 /*
5175 * TODO: consider adding ACL (or documenting how) to prevent other
5176 * users (on this or other systems) from reading it
5177 */
5178
5179
5180 /* TODO: add sk_set_memalloc(inet) or similar */
5181
5182 if (cfile)
5183 cfile->swapfile = true;
5184 /*
5185 * TODO: Since file already open, we can't open with DENY_ALL here
5186 * but we could add call to grab a byte range lock to prevent others
5187 * from reading or writing the file
5188 */
5189
5190 sis->flags |= SWP_FS_OPS;
5191 return add_swap_extent(sis, 0, sis->max, 0);
5192 }
5193
cifs_swap_deactivate(struct file * file)5194 static void cifs_swap_deactivate(struct file *file)
5195 {
5196 struct cifsFileInfo *cfile = file->private_data;
5197
5198 cifs_dbg(FYI, "swap deactivate\n");
5199
5200 /* TODO: undo sk_set_memalloc(inet) will eventually be needed */
5201
5202 if (cfile)
5203 cfile->swapfile = false;
5204
5205 /* do we need to unpin (or unlock) the file */
5206 }
5207
5208 /*
5209 * Mark a page as having been made dirty and thus needing writeback. We also
5210 * need to pin the cache object to write back to.
5211 */
5212 #ifdef CONFIG_CIFS_FSCACHE
cifs_dirty_folio(struct address_space * mapping,struct folio * folio)5213 static bool cifs_dirty_folio(struct address_space *mapping, struct folio *folio)
5214 {
5215 return fscache_dirty_folio(mapping, folio,
5216 cifs_inode_cookie(mapping->host));
5217 }
5218 #else
5219 #define cifs_dirty_folio filemap_dirty_folio
5220 #endif
5221
5222 const struct address_space_operations cifs_addr_ops = {
5223 .read_folio = cifs_read_folio,
5224 .readahead = cifs_readahead,
5225 .writepages = cifs_writepages,
5226 .write_begin = cifs_write_begin,
5227 .write_end = cifs_write_end,
5228 .dirty_folio = cifs_dirty_folio,
5229 .release_folio = cifs_release_folio,
5230 .direct_IO = cifs_direct_io,
5231 .invalidate_folio = cifs_invalidate_folio,
5232 .launder_folio = cifs_launder_folio,
5233 .migrate_folio = filemap_migrate_folio,
5234 /*
5235 * TODO: investigate and if useful we could add an is_dirty_writeback
5236 * helper if needed
5237 */
5238 .swap_activate = cifs_swap_activate,
5239 .swap_deactivate = cifs_swap_deactivate,
5240 };
5241
5242 /*
5243 * cifs_readahead requires the server to support a buffer large enough to
5244 * contain the header plus one complete page of data. Otherwise, we need
5245 * to leave cifs_readahead out of the address space operations.
5246 */
5247 const struct address_space_operations cifs_addr_ops_smallbuf = {
5248 .read_folio = cifs_read_folio,
5249 .writepages = cifs_writepages,
5250 .write_begin = cifs_write_begin,
5251 .write_end = cifs_write_end,
5252 .dirty_folio = cifs_dirty_folio,
5253 .release_folio = cifs_release_folio,
5254 .invalidate_folio = cifs_invalidate_folio,
5255 .launder_folio = cifs_launder_folio,
5256 .migrate_folio = filemap_migrate_folio,
5257 };
5258