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