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