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