cifs: eliminate cifs_posix_open_inode_helper
[deliverable/linux.git] / fs / cifs / file.c
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 <asm/div64.h>
36 #include "cifsfs.h"
37 #include "cifspdu.h"
38 #include "cifsglob.h"
39 #include "cifsproto.h"
40 #include "cifs_unicode.h"
41 #include "cifs_debug.h"
42 #include "cifs_fs_sb.h"
43 #include "fscache.h"
44
45 static inline int cifs_convert_flags(unsigned int flags)
46 {
47 if ((flags & O_ACCMODE) == O_RDONLY)
48 return GENERIC_READ;
49 else if ((flags & O_ACCMODE) == O_WRONLY)
50 return GENERIC_WRITE;
51 else if ((flags & O_ACCMODE) == O_RDWR) {
52 /* GENERIC_ALL is too much permission to request
53 can cause unnecessary access denied on create */
54 /* return GENERIC_ALL; */
55 return (GENERIC_READ | GENERIC_WRITE);
56 }
57
58 return (READ_CONTROL | FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES |
59 FILE_WRITE_EA | FILE_APPEND_DATA | FILE_WRITE_DATA |
60 FILE_READ_DATA);
61 }
62
63 static inline fmode_t cifs_posix_convert_flags(unsigned int flags)
64 {
65 fmode_t posix_flags = 0;
66
67 if ((flags & O_ACCMODE) == O_RDONLY)
68 posix_flags = FMODE_READ;
69 else if ((flags & O_ACCMODE) == O_WRONLY)
70 posix_flags = FMODE_WRITE;
71 else if ((flags & O_ACCMODE) == O_RDWR) {
72 /* GENERIC_ALL is too much permission to request
73 can cause unnecessary access denied on create */
74 /* return GENERIC_ALL; */
75 posix_flags = FMODE_READ | FMODE_WRITE;
76 }
77 /* can not map O_CREAT or O_EXCL or O_TRUNC flags when
78 reopening a file. They had their effect on the original open */
79 if (flags & O_APPEND)
80 posix_flags |= (fmode_t)O_APPEND;
81 if (flags & O_DSYNC)
82 posix_flags |= (fmode_t)O_DSYNC;
83 if (flags & __O_SYNC)
84 posix_flags |= (fmode_t)__O_SYNC;
85 if (flags & O_DIRECTORY)
86 posix_flags |= (fmode_t)O_DIRECTORY;
87 if (flags & O_NOFOLLOW)
88 posix_flags |= (fmode_t)O_NOFOLLOW;
89 if (flags & O_DIRECT)
90 posix_flags |= (fmode_t)O_DIRECT;
91
92 return posix_flags;
93 }
94
95 static inline int cifs_get_disposition(unsigned int flags)
96 {
97 if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
98 return FILE_CREATE;
99 else if ((flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
100 return FILE_OVERWRITE_IF;
101 else if ((flags & O_CREAT) == O_CREAT)
102 return FILE_OPEN_IF;
103 else if ((flags & O_TRUNC) == O_TRUNC)
104 return FILE_OVERWRITE;
105 else
106 return FILE_OPEN;
107 }
108
109 static inline int cifs_open_inode_helper(struct inode *inode,
110 struct cifsTconInfo *pTcon, __u32 oplock, FILE_ALL_INFO *buf,
111 char *full_path, int xid)
112 {
113 struct cifsInodeInfo *pCifsInode = CIFS_I(inode);
114 struct timespec temp;
115 int rc;
116
117 if (pCifsInode->clientCanCacheRead) {
118 /* we have the inode open somewhere else
119 no need to discard cache data */
120 goto client_can_cache;
121 }
122
123 /* BB need same check in cifs_create too? */
124 /* if not oplocked, invalidate inode pages if mtime or file
125 size changed */
126 temp = cifs_NTtimeToUnix(buf->LastWriteTime);
127 if (timespec_equal(&inode->i_mtime, &temp) &&
128 (inode->i_size ==
129 (loff_t)le64_to_cpu(buf->EndOfFile))) {
130 cFYI(1, "inode unchanged on server");
131 } else {
132 if (inode->i_mapping) {
133 /* BB no need to lock inode until after invalidate
134 since namei code should already have it locked? */
135 rc = filemap_write_and_wait(inode->i_mapping);
136 if (rc != 0)
137 pCifsInode->write_behind_rc = rc;
138 }
139 cFYI(1, "invalidating remote inode since open detected it "
140 "changed");
141 invalidate_remote_inode(inode);
142 }
143
144 client_can_cache:
145 if (pTcon->unix_ext)
146 rc = cifs_get_inode_info_unix(&inode, full_path, inode->i_sb,
147 xid);
148 else
149 rc = cifs_get_inode_info(&inode, full_path, buf, inode->i_sb,
150 xid, NULL);
151
152 if ((oplock & 0xF) == OPLOCK_EXCLUSIVE) {
153 pCifsInode->clientCanCacheAll = true;
154 pCifsInode->clientCanCacheRead = true;
155 cFYI(1, "Exclusive Oplock granted on inode %p", inode);
156 } else if ((oplock & 0xF) == OPLOCK_READ)
157 pCifsInode->clientCanCacheRead = true;
158
159 return rc;
160 }
161
162 int cifs_open(struct inode *inode, struct file *file)
163 {
164 int rc = -EACCES;
165 int xid;
166 __u32 oplock;
167 struct cifs_sb_info *cifs_sb;
168 struct cifsTconInfo *tcon;
169 struct tcon_link *tlink;
170 struct cifsFileInfo *pCifsFile = NULL;
171 struct cifsInodeInfo *pCifsInode;
172 char *full_path = NULL;
173 int desiredAccess;
174 int disposition;
175 __u16 netfid;
176 FILE_ALL_INFO *buf = NULL;
177
178 xid = GetXid();
179
180 cifs_sb = CIFS_SB(inode->i_sb);
181 tlink = cifs_sb_tlink(cifs_sb);
182 if (IS_ERR(tlink)) {
183 FreeXid(xid);
184 return PTR_ERR(tlink);
185 }
186 tcon = tlink_tcon(tlink);
187
188 pCifsInode = CIFS_I(file->f_path.dentry->d_inode);
189
190 full_path = build_path_from_dentry(file->f_path.dentry);
191 if (full_path == NULL) {
192 rc = -ENOMEM;
193 goto out;
194 }
195
196 cFYI(1, "inode = 0x%p file flags are 0x%x for %s",
197 inode, file->f_flags, full_path);
198
199 if (oplockEnabled)
200 oplock = REQ_OPLOCK;
201 else
202 oplock = 0;
203
204 if (!tcon->broken_posix_open && tcon->unix_ext &&
205 (tcon->ses->capabilities & CAP_UNIX) &&
206 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
207 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
208 int oflags = (int) cifs_posix_convert_flags(file->f_flags);
209 oflags |= SMB_O_CREAT;
210 /* can not refresh inode info since size could be stale */
211 rc = cifs_posix_open(full_path, &inode, inode->i_sb,
212 cifs_sb->mnt_file_mode /* ignored */,
213 oflags, &oplock, &netfid, xid);
214 if (rc == 0) {
215 cFYI(1, "posix open succeeded");
216
217 pCifsFile = cifs_new_fileinfo(inode, netfid, file,
218 tlink, oflags, oplock);
219 if (pCifsFile == NULL) {
220 CIFSSMBClose(xid, tcon, netfid);
221 rc = -ENOMEM;
222 }
223
224 cifs_fscache_set_inode_cookie(inode, file);
225
226 goto out;
227 } else if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
228 if (tcon->ses->serverNOS)
229 cERROR(1, "server %s of type %s returned"
230 " unexpected error on SMB posix open"
231 ", disabling posix open support."
232 " Check if server update available.",
233 tcon->ses->serverName,
234 tcon->ses->serverNOS);
235 tcon->broken_posix_open = true;
236 } else if ((rc != -EIO) && (rc != -EREMOTE) &&
237 (rc != -EOPNOTSUPP)) /* path not found or net err */
238 goto out;
239 /* else fallthrough to retry open the old way on network i/o
240 or DFS errors */
241 }
242
243 desiredAccess = cifs_convert_flags(file->f_flags);
244
245 /*********************************************************************
246 * open flag mapping table:
247 *
248 * POSIX Flag CIFS Disposition
249 * ---------- ----------------
250 * O_CREAT FILE_OPEN_IF
251 * O_CREAT | O_EXCL FILE_CREATE
252 * O_CREAT | O_TRUNC FILE_OVERWRITE_IF
253 * O_TRUNC FILE_OVERWRITE
254 * none of the above FILE_OPEN
255 *
256 * Note that there is not a direct match between disposition
257 * FILE_SUPERSEDE (ie create whether or not file exists although
258 * O_CREAT | O_TRUNC is similar but truncates the existing
259 * file rather than creating a new file as FILE_SUPERSEDE does
260 * (which uses the attributes / metadata passed in on open call)
261 *?
262 *? O_SYNC is a reasonable match to CIFS writethrough flag
263 *? and the read write flags match reasonably. O_LARGEFILE
264 *? is irrelevant because largefile support is always used
265 *? by this client. Flags O_APPEND, O_DIRECT, O_DIRECTORY,
266 * O_FASYNC, O_NOFOLLOW, O_NONBLOCK need further investigation
267 *********************************************************************/
268
269 disposition = cifs_get_disposition(file->f_flags);
270
271 /* BB pass O_SYNC flag through on file attributes .. BB */
272
273 /* Also refresh inode by passing in file_info buf returned by SMBOpen
274 and calling get_inode_info with returned buf (at least helps
275 non-Unix server case) */
276
277 /* BB we can not do this if this is the second open of a file
278 and the first handle has writebehind data, we might be
279 able to simply do a filemap_fdatawrite/filemap_fdatawait first */
280 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
281 if (!buf) {
282 rc = -ENOMEM;
283 goto out;
284 }
285
286 if (tcon->ses->capabilities & CAP_NT_SMBS)
287 rc = CIFSSMBOpen(xid, tcon, full_path, disposition,
288 desiredAccess, CREATE_NOT_DIR, &netfid, &oplock, buf,
289 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags
290 & CIFS_MOUNT_MAP_SPECIAL_CHR);
291 else
292 rc = -EIO; /* no NT SMB support fall into legacy open below */
293
294 if (rc == -EIO) {
295 /* Old server, try legacy style OpenX */
296 rc = SMBLegacyOpen(xid, tcon, full_path, disposition,
297 desiredAccess, CREATE_NOT_DIR, &netfid, &oplock, buf,
298 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags
299 & CIFS_MOUNT_MAP_SPECIAL_CHR);
300 }
301 if (rc) {
302 cFYI(1, "cifs_open returned 0x%x", rc);
303 goto out;
304 }
305
306 rc = cifs_open_inode_helper(inode, tcon, oplock, buf, full_path, xid);
307 if (rc != 0)
308 goto out;
309
310 pCifsFile = cifs_new_fileinfo(inode, netfid, file, tlink,
311 file->f_flags, oplock);
312 if (pCifsFile == NULL) {
313 rc = -ENOMEM;
314 goto out;
315 }
316
317 cifs_fscache_set_inode_cookie(inode, file);
318
319 if (oplock & CIFS_CREATE_ACTION) {
320 /* time to set mode which we can not set earlier due to
321 problems creating new read-only files */
322 if (tcon->unix_ext) {
323 struct cifs_unix_set_info_args args = {
324 .mode = inode->i_mode,
325 .uid = NO_CHANGE_64,
326 .gid = NO_CHANGE_64,
327 .ctime = NO_CHANGE_64,
328 .atime = NO_CHANGE_64,
329 .mtime = NO_CHANGE_64,
330 .device = 0,
331 };
332 CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
333 cifs_sb->local_nls,
334 cifs_sb->mnt_cifs_flags &
335 CIFS_MOUNT_MAP_SPECIAL_CHR);
336 }
337 }
338
339 out:
340 kfree(buf);
341 kfree(full_path);
342 FreeXid(xid);
343 cifs_put_tlink(tlink);
344 return rc;
345 }
346
347 /* Try to reacquire byte range locks that were released when session */
348 /* to server was lost */
349 static int cifs_relock_file(struct cifsFileInfo *cifsFile)
350 {
351 int rc = 0;
352
353 /* BB list all locks open on this file and relock */
354
355 return rc;
356 }
357
358 static int cifs_reopen_file(struct file *file, bool can_flush)
359 {
360 int rc = -EACCES;
361 int xid;
362 __u32 oplock;
363 struct cifs_sb_info *cifs_sb;
364 struct cifsTconInfo *tcon;
365 struct cifsFileInfo *pCifsFile;
366 struct cifsInodeInfo *pCifsInode;
367 struct inode *inode;
368 char *full_path = NULL;
369 int desiredAccess;
370 int disposition = FILE_OPEN;
371 __u16 netfid;
372
373 if (file->private_data)
374 pCifsFile = file->private_data;
375 else
376 return -EBADF;
377
378 xid = GetXid();
379 mutex_lock(&pCifsFile->fh_mutex);
380 if (!pCifsFile->invalidHandle) {
381 mutex_unlock(&pCifsFile->fh_mutex);
382 rc = 0;
383 FreeXid(xid);
384 return rc;
385 }
386
387 if (file->f_path.dentry == NULL) {
388 cERROR(1, "no valid name if dentry freed");
389 dump_stack();
390 rc = -EBADF;
391 goto reopen_error_exit;
392 }
393
394 inode = file->f_path.dentry->d_inode;
395 if (inode == NULL) {
396 cERROR(1, "inode not valid");
397 dump_stack();
398 rc = -EBADF;
399 goto reopen_error_exit;
400 }
401
402 cifs_sb = CIFS_SB(inode->i_sb);
403 tcon = tlink_tcon(pCifsFile->tlink);
404
405 /* can not grab rename sem here because various ops, including
406 those that already have the rename sem can end up causing writepage
407 to get called and if the server was down that means we end up here,
408 and we can never tell if the caller already has the rename_sem */
409 full_path = build_path_from_dentry(file->f_path.dentry);
410 if (full_path == NULL) {
411 rc = -ENOMEM;
412 reopen_error_exit:
413 mutex_unlock(&pCifsFile->fh_mutex);
414 FreeXid(xid);
415 return rc;
416 }
417
418 cFYI(1, "inode = 0x%p file flags 0x%x for %s",
419 inode, file->f_flags, full_path);
420
421 if (oplockEnabled)
422 oplock = REQ_OPLOCK;
423 else
424 oplock = 0;
425
426 if (tcon->unix_ext && (tcon->ses->capabilities & CAP_UNIX) &&
427 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
428 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
429 int oflags = (int) cifs_posix_convert_flags(file->f_flags);
430 /* can not refresh inode info since size could be stale */
431 rc = cifs_posix_open(full_path, NULL, inode->i_sb,
432 cifs_sb->mnt_file_mode /* ignored */,
433 oflags, &oplock, &netfid, xid);
434 if (rc == 0) {
435 cFYI(1, "posix reopen succeeded");
436 goto reopen_success;
437 }
438 /* fallthrough to retry open the old way on errors, especially
439 in the reconnect path it is important to retry hard */
440 }
441
442 desiredAccess = cifs_convert_flags(file->f_flags);
443
444 /* Can not refresh inode by passing in file_info buf to be returned
445 by SMBOpen and then calling get_inode_info with returned buf
446 since file might have write behind data that needs to be flushed
447 and server version of file size can be stale. If we knew for sure
448 that inode was not dirty locally we could do this */
449
450 rc = CIFSSMBOpen(xid, tcon, full_path, disposition, desiredAccess,
451 CREATE_NOT_DIR, &netfid, &oplock, NULL,
452 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
453 CIFS_MOUNT_MAP_SPECIAL_CHR);
454 if (rc) {
455 mutex_unlock(&pCifsFile->fh_mutex);
456 cFYI(1, "cifs_open returned 0x%x", rc);
457 cFYI(1, "oplock: %d", oplock);
458 } else {
459 reopen_success:
460 pCifsFile->netfid = netfid;
461 pCifsFile->invalidHandle = false;
462 mutex_unlock(&pCifsFile->fh_mutex);
463 pCifsInode = CIFS_I(inode);
464 if (pCifsInode) {
465 if (can_flush) {
466 rc = filemap_write_and_wait(inode->i_mapping);
467 if (rc != 0)
468 CIFS_I(inode)->write_behind_rc = rc;
469 /* temporarily disable caching while we
470 go to server to get inode info */
471 pCifsInode->clientCanCacheAll = false;
472 pCifsInode->clientCanCacheRead = false;
473 if (tcon->unix_ext)
474 rc = cifs_get_inode_info_unix(&inode,
475 full_path, inode->i_sb, xid);
476 else
477 rc = cifs_get_inode_info(&inode,
478 full_path, NULL, inode->i_sb,
479 xid, NULL);
480 } /* else we are writing out data to server already
481 and could deadlock if we tried to flush data, and
482 since we do not know if we have data that would
483 invalidate the current end of file on the server
484 we can not go to the server to get the new inod
485 info */
486 if ((oplock & 0xF) == OPLOCK_EXCLUSIVE) {
487 pCifsInode->clientCanCacheAll = true;
488 pCifsInode->clientCanCacheRead = true;
489 cFYI(1, "Exclusive Oplock granted on inode %p",
490 file->f_path.dentry->d_inode);
491 } else if ((oplock & 0xF) == OPLOCK_READ) {
492 pCifsInode->clientCanCacheRead = true;
493 pCifsInode->clientCanCacheAll = false;
494 } else {
495 pCifsInode->clientCanCacheRead = false;
496 pCifsInode->clientCanCacheAll = false;
497 }
498 cifs_relock_file(pCifsFile);
499 }
500 }
501 kfree(full_path);
502 FreeXid(xid);
503 return rc;
504 }
505
506 int cifs_close(struct inode *inode, struct file *file)
507 {
508 int rc = 0;
509 int xid, timeout;
510 struct cifs_sb_info *cifs_sb;
511 struct cifsTconInfo *pTcon;
512 struct cifsFileInfo *pSMBFile = file->private_data;
513
514 xid = GetXid();
515
516 cifs_sb = CIFS_SB(inode->i_sb);
517 pTcon = tlink_tcon(pSMBFile->tlink);
518 if (pSMBFile) {
519 struct cifsLockInfo *li, *tmp;
520 write_lock(&GlobalSMBSeslock);
521 pSMBFile->closePend = true;
522 if (pTcon) {
523 /* no sense reconnecting to close a file that is
524 already closed */
525 if (!pTcon->need_reconnect) {
526 write_unlock(&GlobalSMBSeslock);
527 timeout = 2;
528 while ((atomic_read(&pSMBFile->count) != 1)
529 && (timeout <= 2048)) {
530 /* Give write a better chance to get to
531 server ahead of the close. We do not
532 want to add a wait_q here as it would
533 increase the memory utilization as
534 the struct would be in each open file,
535 but this should give enough time to
536 clear the socket */
537 cFYI(DBG2, "close delay, write pending");
538 msleep(timeout);
539 timeout *= 4;
540 }
541 if (!pTcon->need_reconnect &&
542 !pSMBFile->invalidHandle)
543 rc = CIFSSMBClose(xid, pTcon,
544 pSMBFile->netfid);
545 } else
546 write_unlock(&GlobalSMBSeslock);
547 } else
548 write_unlock(&GlobalSMBSeslock);
549
550 /* Delete any outstanding lock records.
551 We'll lose them when the file is closed anyway. */
552 mutex_lock(&pSMBFile->lock_mutex);
553 list_for_each_entry_safe(li, tmp, &pSMBFile->llist, llist) {
554 list_del(&li->llist);
555 kfree(li);
556 }
557 mutex_unlock(&pSMBFile->lock_mutex);
558
559 write_lock(&GlobalSMBSeslock);
560 list_del(&pSMBFile->flist);
561 list_del(&pSMBFile->tlist);
562 write_unlock(&GlobalSMBSeslock);
563 cifsFileInfo_put(file->private_data);
564 file->private_data = NULL;
565 } else
566 rc = -EBADF;
567
568 read_lock(&GlobalSMBSeslock);
569 if (list_empty(&(CIFS_I(inode)->openFileList))) {
570 cFYI(1, "closing last open instance for inode %p", inode);
571 /* if the file is not open we do not know if we can cache info
572 on this inode, much less write behind and read ahead */
573 CIFS_I(inode)->clientCanCacheRead = false;
574 CIFS_I(inode)->clientCanCacheAll = false;
575 }
576 read_unlock(&GlobalSMBSeslock);
577 if ((rc == 0) && CIFS_I(inode)->write_behind_rc)
578 rc = CIFS_I(inode)->write_behind_rc;
579 FreeXid(xid);
580 return rc;
581 }
582
583 int cifs_closedir(struct inode *inode, struct file *file)
584 {
585 int rc = 0;
586 int xid;
587 struct cifsFileInfo *pCFileStruct = file->private_data;
588 char *ptmp;
589
590 cFYI(1, "Closedir inode = 0x%p", inode);
591
592 xid = GetXid();
593
594 if (pCFileStruct) {
595 struct cifsTconInfo *pTcon = tlink_tcon(pCFileStruct->tlink);
596
597 cFYI(1, "Freeing private data in close dir");
598 write_lock(&GlobalSMBSeslock);
599 if (!pCFileStruct->srch_inf.endOfSearch &&
600 !pCFileStruct->invalidHandle) {
601 pCFileStruct->invalidHandle = true;
602 write_unlock(&GlobalSMBSeslock);
603 rc = CIFSFindClose(xid, pTcon, pCFileStruct->netfid);
604 cFYI(1, "Closing uncompleted readdir with rc %d",
605 rc);
606 /* not much we can do if it fails anyway, ignore rc */
607 rc = 0;
608 } else
609 write_unlock(&GlobalSMBSeslock);
610 ptmp = pCFileStruct->srch_inf.ntwrk_buf_start;
611 if (ptmp) {
612 cFYI(1, "closedir free smb buf in srch struct");
613 pCFileStruct->srch_inf.ntwrk_buf_start = NULL;
614 if (pCFileStruct->srch_inf.smallBuf)
615 cifs_small_buf_release(ptmp);
616 else
617 cifs_buf_release(ptmp);
618 }
619 cifs_put_tlink(pCFileStruct->tlink);
620 kfree(file->private_data);
621 file->private_data = NULL;
622 }
623 /* BB can we lock the filestruct while this is going on? */
624 FreeXid(xid);
625 return rc;
626 }
627
628 static int store_file_lock(struct cifsFileInfo *fid, __u64 len,
629 __u64 offset, __u8 lockType)
630 {
631 struct cifsLockInfo *li =
632 kmalloc(sizeof(struct cifsLockInfo), GFP_KERNEL);
633 if (li == NULL)
634 return -ENOMEM;
635 li->offset = offset;
636 li->length = len;
637 li->type = lockType;
638 mutex_lock(&fid->lock_mutex);
639 list_add(&li->llist, &fid->llist);
640 mutex_unlock(&fid->lock_mutex);
641 return 0;
642 }
643
644 int cifs_lock(struct file *file, int cmd, struct file_lock *pfLock)
645 {
646 int rc, xid;
647 __u32 numLock = 0;
648 __u32 numUnlock = 0;
649 __u64 length;
650 bool wait_flag = false;
651 struct cifs_sb_info *cifs_sb;
652 struct cifsTconInfo *tcon;
653 __u16 netfid;
654 __u8 lockType = LOCKING_ANDX_LARGE_FILES;
655 bool posix_locking = 0;
656
657 length = 1 + pfLock->fl_end - pfLock->fl_start;
658 rc = -EACCES;
659 xid = GetXid();
660
661 cFYI(1, "Lock parm: 0x%x flockflags: "
662 "0x%x flocktype: 0x%x start: %lld end: %lld",
663 cmd, pfLock->fl_flags, pfLock->fl_type, pfLock->fl_start,
664 pfLock->fl_end);
665
666 if (pfLock->fl_flags & FL_POSIX)
667 cFYI(1, "Posix");
668 if (pfLock->fl_flags & FL_FLOCK)
669 cFYI(1, "Flock");
670 if (pfLock->fl_flags & FL_SLEEP) {
671 cFYI(1, "Blocking lock");
672 wait_flag = true;
673 }
674 if (pfLock->fl_flags & FL_ACCESS)
675 cFYI(1, "Process suspended by mandatory locking - "
676 "not implemented yet");
677 if (pfLock->fl_flags & FL_LEASE)
678 cFYI(1, "Lease on file - not implemented yet");
679 if (pfLock->fl_flags &
680 (~(FL_POSIX | FL_FLOCK | FL_SLEEP | FL_ACCESS | FL_LEASE)))
681 cFYI(1, "Unknown lock flags 0x%x", pfLock->fl_flags);
682
683 if (pfLock->fl_type == F_WRLCK) {
684 cFYI(1, "F_WRLCK ");
685 numLock = 1;
686 } else if (pfLock->fl_type == F_UNLCK) {
687 cFYI(1, "F_UNLCK");
688 numUnlock = 1;
689 /* Check if unlock includes more than
690 one lock range */
691 } else if (pfLock->fl_type == F_RDLCK) {
692 cFYI(1, "F_RDLCK");
693 lockType |= LOCKING_ANDX_SHARED_LOCK;
694 numLock = 1;
695 } else if (pfLock->fl_type == F_EXLCK) {
696 cFYI(1, "F_EXLCK");
697 numLock = 1;
698 } else if (pfLock->fl_type == F_SHLCK) {
699 cFYI(1, "F_SHLCK");
700 lockType |= LOCKING_ANDX_SHARED_LOCK;
701 numLock = 1;
702 } else
703 cFYI(1, "Unknown type of lock");
704
705 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
706 tcon = tlink_tcon(((struct cifsFileInfo *)file->private_data)->tlink);
707
708 if (file->private_data == NULL) {
709 rc = -EBADF;
710 FreeXid(xid);
711 return rc;
712 }
713 netfid = ((struct cifsFileInfo *)file->private_data)->netfid;
714
715 if ((tcon->ses->capabilities & CAP_UNIX) &&
716 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
717 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
718 posix_locking = 1;
719 /* BB add code here to normalize offset and length to
720 account for negative length which we can not accept over the
721 wire */
722 if (IS_GETLK(cmd)) {
723 if (posix_locking) {
724 int posix_lock_type;
725 if (lockType & LOCKING_ANDX_SHARED_LOCK)
726 posix_lock_type = CIFS_RDLCK;
727 else
728 posix_lock_type = CIFS_WRLCK;
729 rc = CIFSSMBPosixLock(xid, tcon, netfid, 1 /* get */,
730 length, pfLock,
731 posix_lock_type, wait_flag);
732 FreeXid(xid);
733 return rc;
734 }
735
736 /* BB we could chain these into one lock request BB */
737 rc = CIFSSMBLock(xid, tcon, netfid, length, pfLock->fl_start,
738 0, 1, lockType, 0 /* wait flag */ );
739 if (rc == 0) {
740 rc = CIFSSMBLock(xid, tcon, netfid, length,
741 pfLock->fl_start, 1 /* numUnlock */ ,
742 0 /* numLock */ , lockType,
743 0 /* wait flag */ );
744 pfLock->fl_type = F_UNLCK;
745 if (rc != 0)
746 cERROR(1, "Error unlocking previously locked "
747 "range %d during test of lock", rc);
748 rc = 0;
749
750 } else {
751 /* if rc == ERR_SHARING_VIOLATION ? */
752 rc = 0;
753
754 if (lockType & LOCKING_ANDX_SHARED_LOCK) {
755 pfLock->fl_type = F_WRLCK;
756 } else {
757 rc = CIFSSMBLock(xid, tcon, netfid, length,
758 pfLock->fl_start, 0, 1,
759 lockType | LOCKING_ANDX_SHARED_LOCK,
760 0 /* wait flag */);
761 if (rc == 0) {
762 rc = CIFSSMBLock(xid, tcon, netfid,
763 length, pfLock->fl_start, 1, 0,
764 lockType |
765 LOCKING_ANDX_SHARED_LOCK,
766 0 /* wait flag */);
767 pfLock->fl_type = F_RDLCK;
768 if (rc != 0)
769 cERROR(1, "Error unlocking "
770 "previously locked range %d "
771 "during test of lock", rc);
772 rc = 0;
773 } else {
774 pfLock->fl_type = F_WRLCK;
775 rc = 0;
776 }
777 }
778 }
779
780 FreeXid(xid);
781 return rc;
782 }
783
784 if (!numLock && !numUnlock) {
785 /* if no lock or unlock then nothing
786 to do since we do not know what it is */
787 FreeXid(xid);
788 return -EOPNOTSUPP;
789 }
790
791 if (posix_locking) {
792 int posix_lock_type;
793 if (lockType & LOCKING_ANDX_SHARED_LOCK)
794 posix_lock_type = CIFS_RDLCK;
795 else
796 posix_lock_type = CIFS_WRLCK;
797
798 if (numUnlock == 1)
799 posix_lock_type = CIFS_UNLCK;
800
801 rc = CIFSSMBPosixLock(xid, tcon, netfid, 0 /* set */,
802 length, pfLock,
803 posix_lock_type, wait_flag);
804 } else {
805 struct cifsFileInfo *fid = file->private_data;
806
807 if (numLock) {
808 rc = CIFSSMBLock(xid, tcon, netfid, length,
809 pfLock->fl_start,
810 0, numLock, lockType, wait_flag);
811
812 if (rc == 0) {
813 /* For Windows locks we must store them. */
814 rc = store_file_lock(fid, length,
815 pfLock->fl_start, lockType);
816 }
817 } else if (numUnlock) {
818 /* For each stored lock that this unlock overlaps
819 completely, unlock it. */
820 int stored_rc = 0;
821 struct cifsLockInfo *li, *tmp;
822
823 rc = 0;
824 mutex_lock(&fid->lock_mutex);
825 list_for_each_entry_safe(li, tmp, &fid->llist, llist) {
826 if (pfLock->fl_start <= li->offset &&
827 (pfLock->fl_start + length) >=
828 (li->offset + li->length)) {
829 stored_rc = CIFSSMBLock(xid, tcon,
830 netfid,
831 li->length, li->offset,
832 1, 0, li->type, false);
833 if (stored_rc)
834 rc = stored_rc;
835 else {
836 list_del(&li->llist);
837 kfree(li);
838 }
839 }
840 }
841 mutex_unlock(&fid->lock_mutex);
842 }
843 }
844
845 if (pfLock->fl_flags & FL_POSIX)
846 posix_lock_file_wait(file, pfLock);
847 FreeXid(xid);
848 return rc;
849 }
850
851 /*
852 * Set the timeout on write requests past EOF. For some servers (Windows)
853 * these calls can be very long.
854 *
855 * If we're writing >10M past the EOF we give a 180s timeout. Anything less
856 * than that gets a 45s timeout. Writes not past EOF get 15s timeouts.
857 * The 10M cutoff is totally arbitrary. A better scheme for this would be
858 * welcome if someone wants to suggest one.
859 *
860 * We may be able to do a better job with this if there were some way to
861 * declare that a file should be sparse.
862 */
863 static int
864 cifs_write_timeout(struct cifsInodeInfo *cifsi, loff_t offset)
865 {
866 if (offset <= cifsi->server_eof)
867 return CIFS_STD_OP;
868 else if (offset > (cifsi->server_eof + (10 * 1024 * 1024)))
869 return CIFS_VLONG_OP;
870 else
871 return CIFS_LONG_OP;
872 }
873
874 /* update the file size (if needed) after a write */
875 static void
876 cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset,
877 unsigned int bytes_written)
878 {
879 loff_t end_of_write = offset + bytes_written;
880
881 if (end_of_write > cifsi->server_eof)
882 cifsi->server_eof = end_of_write;
883 }
884
885 ssize_t cifs_user_write(struct file *file, const char __user *write_data,
886 size_t write_size, loff_t *poffset)
887 {
888 int rc = 0;
889 unsigned int bytes_written = 0;
890 unsigned int total_written;
891 struct cifs_sb_info *cifs_sb;
892 struct cifsTconInfo *pTcon;
893 int xid, long_op;
894 struct cifsFileInfo *open_file;
895 struct cifsInodeInfo *cifsi = CIFS_I(file->f_path.dentry->d_inode);
896
897 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
898
899 /* cFYI(1, " write %d bytes to offset %lld of %s", write_size,
900 *poffset, file->f_path.dentry->d_name.name); */
901
902 if (file->private_data == NULL)
903 return -EBADF;
904
905 open_file = file->private_data;
906 pTcon = tlink_tcon(open_file->tlink);
907
908 rc = generic_write_checks(file, poffset, &write_size, 0);
909 if (rc)
910 return rc;
911
912 xid = GetXid();
913
914 long_op = cifs_write_timeout(cifsi, *poffset);
915 for (total_written = 0; write_size > total_written;
916 total_written += bytes_written) {
917 rc = -EAGAIN;
918 while (rc == -EAGAIN) {
919 if (file->private_data == NULL) {
920 /* file has been closed on us */
921 FreeXid(xid);
922 /* if we have gotten here we have written some data
923 and blocked, and the file has been freed on us while
924 we blocked so return what we managed to write */
925 return total_written;
926 }
927 if (open_file->closePend) {
928 FreeXid(xid);
929 if (total_written)
930 return total_written;
931 else
932 return -EBADF;
933 }
934 if (open_file->invalidHandle) {
935 /* we could deadlock if we called
936 filemap_fdatawait from here so tell
937 reopen_file not to flush data to server
938 now */
939 rc = cifs_reopen_file(file, false);
940 if (rc != 0)
941 break;
942 }
943
944 rc = CIFSSMBWrite(xid, pTcon,
945 open_file->netfid,
946 min_t(const int, cifs_sb->wsize,
947 write_size - total_written),
948 *poffset, &bytes_written,
949 NULL, write_data + total_written, long_op);
950 }
951 if (rc || (bytes_written == 0)) {
952 if (total_written)
953 break;
954 else {
955 FreeXid(xid);
956 return rc;
957 }
958 } else {
959 cifs_update_eof(cifsi, *poffset, bytes_written);
960 *poffset += bytes_written;
961 }
962 long_op = CIFS_STD_OP; /* subsequent writes fast -
963 15 seconds is plenty */
964 }
965
966 cifs_stats_bytes_written(pTcon, total_written);
967
968 /* since the write may have blocked check these pointers again */
969 if ((file->f_path.dentry) && (file->f_path.dentry->d_inode)) {
970 struct inode *inode = file->f_path.dentry->d_inode;
971 /* Do not update local mtime - server will set its actual value on write
972 * inode->i_ctime = inode->i_mtime =
973 * current_fs_time(inode->i_sb);*/
974 if (total_written > 0) {
975 spin_lock(&inode->i_lock);
976 if (*poffset > file->f_path.dentry->d_inode->i_size)
977 i_size_write(file->f_path.dentry->d_inode,
978 *poffset);
979 spin_unlock(&inode->i_lock);
980 }
981 mark_inode_dirty_sync(file->f_path.dentry->d_inode);
982 }
983 FreeXid(xid);
984 return total_written;
985 }
986
987 static ssize_t cifs_write(struct file *file, const char *write_data,
988 size_t write_size, loff_t *poffset)
989 {
990 int rc = 0;
991 unsigned int bytes_written = 0;
992 unsigned int total_written;
993 struct cifs_sb_info *cifs_sb;
994 struct cifsTconInfo *pTcon;
995 int xid, long_op;
996 struct cifsFileInfo *open_file;
997 struct cifsInodeInfo *cifsi = CIFS_I(file->f_path.dentry->d_inode);
998
999 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
1000
1001 cFYI(1, "write %zd bytes to offset %lld of %s", write_size,
1002 *poffset, file->f_path.dentry->d_name.name);
1003
1004 if (file->private_data == NULL)
1005 return -EBADF;
1006 open_file = file->private_data;
1007 pTcon = tlink_tcon(open_file->tlink);
1008
1009 xid = GetXid();
1010
1011 long_op = cifs_write_timeout(cifsi, *poffset);
1012 for (total_written = 0; write_size > total_written;
1013 total_written += bytes_written) {
1014 rc = -EAGAIN;
1015 while (rc == -EAGAIN) {
1016 if (file->private_data == NULL) {
1017 /* file has been closed on us */
1018 FreeXid(xid);
1019 /* if we have gotten here we have written some data
1020 and blocked, and the file has been freed on us
1021 while we blocked so return what we managed to
1022 write */
1023 return total_written;
1024 }
1025 if (open_file->closePend) {
1026 FreeXid(xid);
1027 if (total_written)
1028 return total_written;
1029 else
1030 return -EBADF;
1031 }
1032 if (open_file->invalidHandle) {
1033 /* we could deadlock if we called
1034 filemap_fdatawait from here so tell
1035 reopen_file not to flush data to
1036 server now */
1037 rc = cifs_reopen_file(file, false);
1038 if (rc != 0)
1039 break;
1040 }
1041 if (experimEnabled || (pTcon->ses->server &&
1042 ((pTcon->ses->server->secMode &
1043 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
1044 == 0))) {
1045 struct kvec iov[2];
1046 unsigned int len;
1047
1048 len = min((size_t)cifs_sb->wsize,
1049 write_size - total_written);
1050 /* iov[0] is reserved for smb header */
1051 iov[1].iov_base = (char *)write_data +
1052 total_written;
1053 iov[1].iov_len = len;
1054 rc = CIFSSMBWrite2(xid, pTcon,
1055 open_file->netfid, len,
1056 *poffset, &bytes_written,
1057 iov, 1, long_op);
1058 } else
1059 rc = CIFSSMBWrite(xid, pTcon,
1060 open_file->netfid,
1061 min_t(const int, cifs_sb->wsize,
1062 write_size - total_written),
1063 *poffset, &bytes_written,
1064 write_data + total_written,
1065 NULL, long_op);
1066 }
1067 if (rc || (bytes_written == 0)) {
1068 if (total_written)
1069 break;
1070 else {
1071 FreeXid(xid);
1072 return rc;
1073 }
1074 } else {
1075 cifs_update_eof(cifsi, *poffset, bytes_written);
1076 *poffset += bytes_written;
1077 }
1078 long_op = CIFS_STD_OP; /* subsequent writes fast -
1079 15 seconds is plenty */
1080 }
1081
1082 cifs_stats_bytes_written(pTcon, total_written);
1083
1084 /* since the write may have blocked check these pointers again */
1085 if ((file->f_path.dentry) && (file->f_path.dentry->d_inode)) {
1086 /*BB We could make this contingent on superblock ATIME flag too */
1087 /* file->f_path.dentry->d_inode->i_ctime =
1088 file->f_path.dentry->d_inode->i_mtime = CURRENT_TIME;*/
1089 if (total_written > 0) {
1090 spin_lock(&file->f_path.dentry->d_inode->i_lock);
1091 if (*poffset > file->f_path.dentry->d_inode->i_size)
1092 i_size_write(file->f_path.dentry->d_inode,
1093 *poffset);
1094 spin_unlock(&file->f_path.dentry->d_inode->i_lock);
1095 }
1096 mark_inode_dirty_sync(file->f_path.dentry->d_inode);
1097 }
1098 FreeXid(xid);
1099 return total_written;
1100 }
1101
1102 #ifdef CONFIG_CIFS_EXPERIMENTAL
1103 struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode,
1104 bool fsuid_only)
1105 {
1106 struct cifsFileInfo *open_file = NULL;
1107 struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb);
1108
1109 /* only filter by fsuid on multiuser mounts */
1110 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
1111 fsuid_only = false;
1112
1113 read_lock(&GlobalSMBSeslock);
1114 /* we could simply get the first_list_entry since write-only entries
1115 are always at the end of the list but since the first entry might
1116 have a close pending, we go through the whole list */
1117 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
1118 if (open_file->closePend)
1119 continue;
1120 if (fsuid_only && open_file->uid != current_fsuid())
1121 continue;
1122 if (open_file->pfile && ((open_file->pfile->f_flags & O_RDWR) ||
1123 (open_file->pfile->f_flags & O_RDONLY))) {
1124 if (!open_file->invalidHandle) {
1125 /* found a good file */
1126 /* lock it so it will not be closed on us */
1127 cifsFileInfo_get(open_file);
1128 read_unlock(&GlobalSMBSeslock);
1129 return open_file;
1130 } /* else might as well continue, and look for
1131 another, or simply have the caller reopen it
1132 again rather than trying to fix this handle */
1133 } else /* write only file */
1134 break; /* write only files are last so must be done */
1135 }
1136 read_unlock(&GlobalSMBSeslock);
1137 return NULL;
1138 }
1139 #endif
1140
1141 struct cifsFileInfo *find_writable_file(struct cifsInodeInfo *cifs_inode,
1142 bool fsuid_only)
1143 {
1144 struct cifsFileInfo *open_file;
1145 struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb);
1146 bool any_available = false;
1147 int rc;
1148
1149 /* Having a null inode here (because mapping->host was set to zero by
1150 the VFS or MM) should not happen but we had reports of on oops (due to
1151 it being zero) during stress testcases so we need to check for it */
1152
1153 if (cifs_inode == NULL) {
1154 cERROR(1, "Null inode passed to cifs_writeable_file");
1155 dump_stack();
1156 return NULL;
1157 }
1158
1159 /* only filter by fsuid on multiuser mounts */
1160 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
1161 fsuid_only = false;
1162
1163 read_lock(&GlobalSMBSeslock);
1164 refind_writable:
1165 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
1166 if (open_file->closePend)
1167 continue;
1168 if (!any_available && open_file->pid != current->tgid)
1169 continue;
1170 if (fsuid_only && open_file->uid != current_fsuid())
1171 continue;
1172 if (open_file->pfile &&
1173 ((open_file->pfile->f_flags & O_RDWR) ||
1174 (open_file->pfile->f_flags & O_WRONLY))) {
1175 cifsFileInfo_get(open_file);
1176
1177 if (!open_file->invalidHandle) {
1178 /* found a good writable file */
1179 read_unlock(&GlobalSMBSeslock);
1180 return open_file;
1181 }
1182
1183 read_unlock(&GlobalSMBSeslock);
1184 /* Had to unlock since following call can block */
1185 rc = cifs_reopen_file(open_file->pfile, false);
1186 if (!rc) {
1187 if (!open_file->closePend)
1188 return open_file;
1189 else { /* start over in case this was deleted */
1190 /* since the list could be modified */
1191 read_lock(&GlobalSMBSeslock);
1192 cifsFileInfo_put(open_file);
1193 goto refind_writable;
1194 }
1195 }
1196
1197 /* if it fails, try another handle if possible -
1198 (we can not do this if closePending since
1199 loop could be modified - in which case we
1200 have to start at the beginning of the list
1201 again. Note that it would be bad
1202 to hold up writepages here (rather than
1203 in caller) with continuous retries */
1204 cFYI(1, "wp failed on reopen file");
1205 read_lock(&GlobalSMBSeslock);
1206 /* can not use this handle, no write
1207 pending on this one after all */
1208 cifsFileInfo_put(open_file);
1209
1210 if (open_file->closePend) /* list could have changed */
1211 goto refind_writable;
1212 /* else we simply continue to the next entry. Thus
1213 we do not loop on reopen errors. If we
1214 can not reopen the file, for example if we
1215 reconnected to a server with another client
1216 racing to delete or lock the file we would not
1217 make progress if we restarted before the beginning
1218 of the loop here. */
1219 }
1220 }
1221 /* couldn't find useable FH with same pid, try any available */
1222 if (!any_available) {
1223 any_available = true;
1224 goto refind_writable;
1225 }
1226 read_unlock(&GlobalSMBSeslock);
1227 return NULL;
1228 }
1229
1230 static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to)
1231 {
1232 struct address_space *mapping = page->mapping;
1233 loff_t offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
1234 char *write_data;
1235 int rc = -EFAULT;
1236 int bytes_written = 0;
1237 struct cifs_sb_info *cifs_sb;
1238 struct inode *inode;
1239 struct cifsFileInfo *open_file;
1240
1241 if (!mapping || !mapping->host)
1242 return -EFAULT;
1243
1244 inode = page->mapping->host;
1245 cifs_sb = CIFS_SB(inode->i_sb);
1246
1247 offset += (loff_t)from;
1248 write_data = kmap(page);
1249 write_data += from;
1250
1251 if ((to > PAGE_CACHE_SIZE) || (from > to)) {
1252 kunmap(page);
1253 return -EIO;
1254 }
1255
1256 /* racing with truncate? */
1257 if (offset > mapping->host->i_size) {
1258 kunmap(page);
1259 return 0; /* don't care */
1260 }
1261
1262 /* check to make sure that we are not extending the file */
1263 if (mapping->host->i_size - offset < (loff_t)to)
1264 to = (unsigned)(mapping->host->i_size - offset);
1265
1266 open_file = find_writable_file(CIFS_I(mapping->host), false);
1267 if (open_file) {
1268 bytes_written = cifs_write(open_file->pfile, write_data,
1269 to-from, &offset);
1270 cifsFileInfo_put(open_file);
1271 /* Does mm or vfs already set times? */
1272 inode->i_atime = inode->i_mtime = current_fs_time(inode->i_sb);
1273 if ((bytes_written > 0) && (offset))
1274 rc = 0;
1275 else if (bytes_written < 0)
1276 rc = bytes_written;
1277 } else {
1278 cFYI(1, "No writeable filehandles for inode");
1279 rc = -EIO;
1280 }
1281
1282 kunmap(page);
1283 return rc;
1284 }
1285
1286 static int cifs_writepages(struct address_space *mapping,
1287 struct writeback_control *wbc)
1288 {
1289 struct backing_dev_info *bdi = mapping->backing_dev_info;
1290 unsigned int bytes_to_write;
1291 unsigned int bytes_written;
1292 struct cifs_sb_info *cifs_sb;
1293 int done = 0;
1294 pgoff_t end;
1295 pgoff_t index;
1296 int range_whole = 0;
1297 struct kvec *iov;
1298 int len;
1299 int n_iov = 0;
1300 pgoff_t next;
1301 int nr_pages;
1302 __u64 offset = 0;
1303 struct cifsFileInfo *open_file;
1304 struct cifsTconInfo *tcon;
1305 struct cifsInodeInfo *cifsi = CIFS_I(mapping->host);
1306 struct page *page;
1307 struct pagevec pvec;
1308 int rc = 0;
1309 int scanned = 0;
1310 int xid, long_op;
1311
1312 /*
1313 * BB: Is this meaningful for a non-block-device file system?
1314 * If it is, we should test it again after we do I/O
1315 */
1316 if (wbc->nonblocking && bdi_write_congested(bdi)) {
1317 wbc->encountered_congestion = 1;
1318 return 0;
1319 }
1320
1321 cifs_sb = CIFS_SB(mapping->host->i_sb);
1322
1323 /*
1324 * If wsize is smaller that the page cache size, default to writing
1325 * one page at a time via cifs_writepage
1326 */
1327 if (cifs_sb->wsize < PAGE_CACHE_SIZE)
1328 return generic_writepages(mapping, wbc);
1329
1330 iov = kmalloc(32 * sizeof(struct kvec), GFP_KERNEL);
1331 if (iov == NULL)
1332 return generic_writepages(mapping, wbc);
1333
1334 /*
1335 * if there's no open file, then this is likely to fail too,
1336 * but it'll at least handle the return. Maybe it should be
1337 * a BUG() instead?
1338 */
1339 open_file = find_writable_file(CIFS_I(mapping->host), false);
1340 if (!open_file) {
1341 kfree(iov);
1342 return generic_writepages(mapping, wbc);
1343 }
1344
1345 tcon = tlink_tcon(open_file->tlink);
1346 if (!experimEnabled && tcon->ses->server->secMode &
1347 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED)) {
1348 cifsFileInfo_put(open_file);
1349 return generic_writepages(mapping, wbc);
1350 }
1351 cifsFileInfo_put(open_file);
1352
1353 xid = GetXid();
1354
1355 pagevec_init(&pvec, 0);
1356 if (wbc->range_cyclic) {
1357 index = mapping->writeback_index; /* Start from prev offset */
1358 end = -1;
1359 } else {
1360 index = wbc->range_start >> PAGE_CACHE_SHIFT;
1361 end = wbc->range_end >> PAGE_CACHE_SHIFT;
1362 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
1363 range_whole = 1;
1364 scanned = 1;
1365 }
1366 retry:
1367 while (!done && (index <= end) &&
1368 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
1369 PAGECACHE_TAG_DIRTY,
1370 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1))) {
1371 int first;
1372 unsigned int i;
1373
1374 first = -1;
1375 next = 0;
1376 n_iov = 0;
1377 bytes_to_write = 0;
1378
1379 for (i = 0; i < nr_pages; i++) {
1380 page = pvec.pages[i];
1381 /*
1382 * At this point we hold neither mapping->tree_lock nor
1383 * lock on the page itself: the page may be truncated or
1384 * invalidated (changing page->mapping to NULL), or even
1385 * swizzled back from swapper_space to tmpfs file
1386 * mapping
1387 */
1388
1389 if (first < 0)
1390 lock_page(page);
1391 else if (!trylock_page(page))
1392 break;
1393
1394 if (unlikely(page->mapping != mapping)) {
1395 unlock_page(page);
1396 break;
1397 }
1398
1399 if (!wbc->range_cyclic && page->index > end) {
1400 done = 1;
1401 unlock_page(page);
1402 break;
1403 }
1404
1405 if (next && (page->index != next)) {
1406 /* Not next consecutive page */
1407 unlock_page(page);
1408 break;
1409 }
1410
1411 if (wbc->sync_mode != WB_SYNC_NONE)
1412 wait_on_page_writeback(page);
1413
1414 if (PageWriteback(page) ||
1415 !clear_page_dirty_for_io(page)) {
1416 unlock_page(page);
1417 break;
1418 }
1419
1420 /*
1421 * This actually clears the dirty bit in the radix tree.
1422 * See cifs_writepage() for more commentary.
1423 */
1424 set_page_writeback(page);
1425
1426 if (page_offset(page) >= mapping->host->i_size) {
1427 done = 1;
1428 unlock_page(page);
1429 end_page_writeback(page);
1430 break;
1431 }
1432
1433 /*
1434 * BB can we get rid of this? pages are held by pvec
1435 */
1436 page_cache_get(page);
1437
1438 len = min(mapping->host->i_size - page_offset(page),
1439 (loff_t)PAGE_CACHE_SIZE);
1440
1441 /* reserve iov[0] for the smb header */
1442 n_iov++;
1443 iov[n_iov].iov_base = kmap(page);
1444 iov[n_iov].iov_len = len;
1445 bytes_to_write += len;
1446
1447 if (first < 0) {
1448 first = i;
1449 offset = page_offset(page);
1450 }
1451 next = page->index + 1;
1452 if (bytes_to_write + PAGE_CACHE_SIZE > cifs_sb->wsize)
1453 break;
1454 }
1455 if (n_iov) {
1456 open_file = find_writable_file(CIFS_I(mapping->host),
1457 false);
1458 if (!open_file) {
1459 cERROR(1, "No writable handles for inode");
1460 rc = -EBADF;
1461 } else {
1462 long_op = cifs_write_timeout(cifsi, offset);
1463 rc = CIFSSMBWrite2(xid, tcon, open_file->netfid,
1464 bytes_to_write, offset,
1465 &bytes_written, iov, n_iov,
1466 long_op);
1467 cifsFileInfo_put(open_file);
1468 cifs_update_eof(cifsi, offset, bytes_written);
1469 }
1470
1471 if (rc || bytes_written < bytes_to_write) {
1472 cERROR(1, "Write2 ret %d, wrote %d",
1473 rc, bytes_written);
1474 /* BB what if continued retry is
1475 requested via mount flags? */
1476 if (rc == -ENOSPC)
1477 set_bit(AS_ENOSPC, &mapping->flags);
1478 else
1479 set_bit(AS_EIO, &mapping->flags);
1480 } else {
1481 cifs_stats_bytes_written(tcon, bytes_written);
1482 }
1483
1484 for (i = 0; i < n_iov; i++) {
1485 page = pvec.pages[first + i];
1486 /* Should we also set page error on
1487 success rc but too little data written? */
1488 /* BB investigate retry logic on temporary
1489 server crash cases and how recovery works
1490 when page marked as error */
1491 if (rc)
1492 SetPageError(page);
1493 kunmap(page);
1494 unlock_page(page);
1495 end_page_writeback(page);
1496 page_cache_release(page);
1497 }
1498 if ((wbc->nr_to_write -= n_iov) <= 0)
1499 done = 1;
1500 index = next;
1501 } else
1502 /* Need to re-find the pages we skipped */
1503 index = pvec.pages[0]->index + 1;
1504
1505 pagevec_release(&pvec);
1506 }
1507 if (!scanned && !done) {
1508 /*
1509 * We hit the last page and there is more work to be done: wrap
1510 * back to the start of the file
1511 */
1512 scanned = 1;
1513 index = 0;
1514 goto retry;
1515 }
1516 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1517 mapping->writeback_index = index;
1518
1519 FreeXid(xid);
1520 kfree(iov);
1521 return rc;
1522 }
1523
1524 static int cifs_writepage(struct page *page, struct writeback_control *wbc)
1525 {
1526 int rc = -EFAULT;
1527 int xid;
1528
1529 xid = GetXid();
1530 /* BB add check for wbc flags */
1531 page_cache_get(page);
1532 if (!PageUptodate(page))
1533 cFYI(1, "ppw - page not up to date");
1534
1535 /*
1536 * Set the "writeback" flag, and clear "dirty" in the radix tree.
1537 *
1538 * A writepage() implementation always needs to do either this,
1539 * or re-dirty the page with "redirty_page_for_writepage()" in
1540 * the case of a failure.
1541 *
1542 * Just unlocking the page will cause the radix tree tag-bits
1543 * to fail to update with the state of the page correctly.
1544 */
1545 set_page_writeback(page);
1546 rc = cifs_partialpagewrite(page, 0, PAGE_CACHE_SIZE);
1547 SetPageUptodate(page); /* BB add check for error and Clearuptodate? */
1548 unlock_page(page);
1549 end_page_writeback(page);
1550 page_cache_release(page);
1551 FreeXid(xid);
1552 return rc;
1553 }
1554
1555 static int cifs_write_end(struct file *file, struct address_space *mapping,
1556 loff_t pos, unsigned len, unsigned copied,
1557 struct page *page, void *fsdata)
1558 {
1559 int rc;
1560 struct inode *inode = mapping->host;
1561
1562 cFYI(1, "write_end for page %p from pos %lld with %d bytes",
1563 page, pos, copied);
1564
1565 if (PageChecked(page)) {
1566 if (copied == len)
1567 SetPageUptodate(page);
1568 ClearPageChecked(page);
1569 } else if (!PageUptodate(page) && copied == PAGE_CACHE_SIZE)
1570 SetPageUptodate(page);
1571
1572 if (!PageUptodate(page)) {
1573 char *page_data;
1574 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
1575 int xid;
1576
1577 xid = GetXid();
1578 /* this is probably better than directly calling
1579 partialpage_write since in this function the file handle is
1580 known which we might as well leverage */
1581 /* BB check if anything else missing out of ppw
1582 such as updating last write time */
1583 page_data = kmap(page);
1584 rc = cifs_write(file, page_data + offset, copied, &pos);
1585 /* if (rc < 0) should we set writebehind rc? */
1586 kunmap(page);
1587
1588 FreeXid(xid);
1589 } else {
1590 rc = copied;
1591 pos += copied;
1592 set_page_dirty(page);
1593 }
1594
1595 if (rc > 0) {
1596 spin_lock(&inode->i_lock);
1597 if (pos > inode->i_size)
1598 i_size_write(inode, pos);
1599 spin_unlock(&inode->i_lock);
1600 }
1601
1602 unlock_page(page);
1603 page_cache_release(page);
1604
1605 return rc;
1606 }
1607
1608 int cifs_fsync(struct file *file, int datasync)
1609 {
1610 int xid;
1611 int rc = 0;
1612 struct cifsTconInfo *tcon;
1613 struct cifsFileInfo *smbfile = file->private_data;
1614 struct inode *inode = file->f_path.dentry->d_inode;
1615
1616 xid = GetXid();
1617
1618 cFYI(1, "Sync file - name: %s datasync: 0x%x",
1619 file->f_path.dentry->d_name.name, datasync);
1620
1621 rc = filemap_write_and_wait(inode->i_mapping);
1622 if (rc == 0) {
1623 rc = CIFS_I(inode)->write_behind_rc;
1624 CIFS_I(inode)->write_behind_rc = 0;
1625 tcon = tlink_tcon(smbfile->tlink);
1626 if (!rc && tcon && smbfile &&
1627 !(CIFS_SB(inode->i_sb)->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC))
1628 rc = CIFSSMBFlush(xid, tcon, smbfile->netfid);
1629 }
1630
1631 FreeXid(xid);
1632 return rc;
1633 }
1634
1635 /* static void cifs_sync_page(struct page *page)
1636 {
1637 struct address_space *mapping;
1638 struct inode *inode;
1639 unsigned long index = page->index;
1640 unsigned int rpages = 0;
1641 int rc = 0;
1642
1643 cFYI(1, "sync page %p", page);
1644 mapping = page->mapping;
1645 if (!mapping)
1646 return 0;
1647 inode = mapping->host;
1648 if (!inode)
1649 return; */
1650
1651 /* fill in rpages then
1652 result = cifs_pagein_inode(inode, index, rpages); */ /* BB finish */
1653
1654 /* cFYI(1, "rpages is %d for sync page of Index %ld", rpages, index);
1655
1656 #if 0
1657 if (rc < 0)
1658 return rc;
1659 return 0;
1660 #endif
1661 } */
1662
1663 /*
1664 * As file closes, flush all cached write data for this inode checking
1665 * for write behind errors.
1666 */
1667 int cifs_flush(struct file *file, fl_owner_t id)
1668 {
1669 struct inode *inode = file->f_path.dentry->d_inode;
1670 int rc = 0;
1671
1672 /* Rather than do the steps manually:
1673 lock the inode for writing
1674 loop through pages looking for write behind data (dirty pages)
1675 coalesce into contiguous 16K (or smaller) chunks to write to server
1676 send to server (prefer in parallel)
1677 deal with writebehind errors
1678 unlock inode for writing
1679 filemapfdatawrite appears easier for the time being */
1680
1681 rc = filemap_fdatawrite(inode->i_mapping);
1682 /* reset wb rc if we were able to write out dirty pages */
1683 if (!rc) {
1684 rc = CIFS_I(inode)->write_behind_rc;
1685 CIFS_I(inode)->write_behind_rc = 0;
1686 }
1687
1688 cFYI(1, "Flush inode %p file %p rc %d", inode, file, rc);
1689
1690 return rc;
1691 }
1692
1693 ssize_t cifs_user_read(struct file *file, char __user *read_data,
1694 size_t read_size, loff_t *poffset)
1695 {
1696 int rc = -EACCES;
1697 unsigned int bytes_read = 0;
1698 unsigned int total_read = 0;
1699 unsigned int current_read_size;
1700 struct cifs_sb_info *cifs_sb;
1701 struct cifsTconInfo *pTcon;
1702 int xid;
1703 struct cifsFileInfo *open_file;
1704 char *smb_read_data;
1705 char __user *current_offset;
1706 struct smb_com_read_rsp *pSMBr;
1707
1708 xid = GetXid();
1709 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
1710
1711 if (file->private_data == NULL) {
1712 rc = -EBADF;
1713 FreeXid(xid);
1714 return rc;
1715 }
1716 open_file = file->private_data;
1717 pTcon = tlink_tcon(open_file->tlink);
1718
1719 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
1720 cFYI(1, "attempting read on write only file instance");
1721
1722 for (total_read = 0, current_offset = read_data;
1723 read_size > total_read;
1724 total_read += bytes_read, current_offset += bytes_read) {
1725 current_read_size = min_t(const int, read_size - total_read,
1726 cifs_sb->rsize);
1727 rc = -EAGAIN;
1728 smb_read_data = NULL;
1729 while (rc == -EAGAIN) {
1730 int buf_type = CIFS_NO_BUFFER;
1731 if ((open_file->invalidHandle) &&
1732 (!open_file->closePend)) {
1733 rc = cifs_reopen_file(file, true);
1734 if (rc != 0)
1735 break;
1736 }
1737 rc = CIFSSMBRead(xid, pTcon,
1738 open_file->netfid,
1739 current_read_size, *poffset,
1740 &bytes_read, &smb_read_data,
1741 &buf_type);
1742 pSMBr = (struct smb_com_read_rsp *)smb_read_data;
1743 if (smb_read_data) {
1744 if (copy_to_user(current_offset,
1745 smb_read_data +
1746 4 /* RFC1001 length field */ +
1747 le16_to_cpu(pSMBr->DataOffset),
1748 bytes_read))
1749 rc = -EFAULT;
1750
1751 if (buf_type == CIFS_SMALL_BUFFER)
1752 cifs_small_buf_release(smb_read_data);
1753 else if (buf_type == CIFS_LARGE_BUFFER)
1754 cifs_buf_release(smb_read_data);
1755 smb_read_data = NULL;
1756 }
1757 }
1758 if (rc || (bytes_read == 0)) {
1759 if (total_read) {
1760 break;
1761 } else {
1762 FreeXid(xid);
1763 return rc;
1764 }
1765 } else {
1766 cifs_stats_bytes_read(pTcon, bytes_read);
1767 *poffset += bytes_read;
1768 }
1769 }
1770 FreeXid(xid);
1771 return total_read;
1772 }
1773
1774
1775 static ssize_t cifs_read(struct file *file, char *read_data, size_t read_size,
1776 loff_t *poffset)
1777 {
1778 int rc = -EACCES;
1779 unsigned int bytes_read = 0;
1780 unsigned int total_read;
1781 unsigned int current_read_size;
1782 struct cifs_sb_info *cifs_sb;
1783 struct cifsTconInfo *pTcon;
1784 int xid;
1785 char *current_offset;
1786 struct cifsFileInfo *open_file;
1787 int buf_type = CIFS_NO_BUFFER;
1788
1789 xid = GetXid();
1790 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
1791
1792 if (file->private_data == NULL) {
1793 rc = -EBADF;
1794 FreeXid(xid);
1795 return rc;
1796 }
1797 open_file = file->private_data;
1798 pTcon = tlink_tcon(open_file->tlink);
1799
1800 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
1801 cFYI(1, "attempting read on write only file instance");
1802
1803 for (total_read = 0, current_offset = read_data;
1804 read_size > total_read;
1805 total_read += bytes_read, current_offset += bytes_read) {
1806 current_read_size = min_t(const int, read_size - total_read,
1807 cifs_sb->rsize);
1808 /* For windows me and 9x we do not want to request more
1809 than it negotiated since it will refuse the read then */
1810 if ((pTcon->ses) &&
1811 !(pTcon->ses->capabilities & CAP_LARGE_FILES)) {
1812 current_read_size = min_t(const int, current_read_size,
1813 pTcon->ses->server->maxBuf - 128);
1814 }
1815 rc = -EAGAIN;
1816 while (rc == -EAGAIN) {
1817 if ((open_file->invalidHandle) &&
1818 (!open_file->closePend)) {
1819 rc = cifs_reopen_file(file, true);
1820 if (rc != 0)
1821 break;
1822 }
1823 rc = CIFSSMBRead(xid, pTcon,
1824 open_file->netfid,
1825 current_read_size, *poffset,
1826 &bytes_read, &current_offset,
1827 &buf_type);
1828 }
1829 if (rc || (bytes_read == 0)) {
1830 if (total_read) {
1831 break;
1832 } else {
1833 FreeXid(xid);
1834 return rc;
1835 }
1836 } else {
1837 cifs_stats_bytes_read(pTcon, total_read);
1838 *poffset += bytes_read;
1839 }
1840 }
1841 FreeXid(xid);
1842 return total_read;
1843 }
1844
1845 int cifs_file_mmap(struct file *file, struct vm_area_struct *vma)
1846 {
1847 int rc, xid;
1848
1849 xid = GetXid();
1850 rc = cifs_revalidate_file(file);
1851 if (rc) {
1852 cFYI(1, "Validation prior to mmap failed, error=%d", rc);
1853 FreeXid(xid);
1854 return rc;
1855 }
1856 rc = generic_file_mmap(file, vma);
1857 FreeXid(xid);
1858 return rc;
1859 }
1860
1861
1862 static void cifs_copy_cache_pages(struct address_space *mapping,
1863 struct list_head *pages, int bytes_read, char *data)
1864 {
1865 struct page *page;
1866 char *target;
1867
1868 while (bytes_read > 0) {
1869 if (list_empty(pages))
1870 break;
1871
1872 page = list_entry(pages->prev, struct page, lru);
1873 list_del(&page->lru);
1874
1875 if (add_to_page_cache_lru(page, mapping, page->index,
1876 GFP_KERNEL)) {
1877 page_cache_release(page);
1878 cFYI(1, "Add page cache failed");
1879 data += PAGE_CACHE_SIZE;
1880 bytes_read -= PAGE_CACHE_SIZE;
1881 continue;
1882 }
1883 page_cache_release(page);
1884
1885 target = kmap_atomic(page, KM_USER0);
1886
1887 if (PAGE_CACHE_SIZE > bytes_read) {
1888 memcpy(target, data, bytes_read);
1889 /* zero the tail end of this partial page */
1890 memset(target + bytes_read, 0,
1891 PAGE_CACHE_SIZE - bytes_read);
1892 bytes_read = 0;
1893 } else {
1894 memcpy(target, data, PAGE_CACHE_SIZE);
1895 bytes_read -= PAGE_CACHE_SIZE;
1896 }
1897 kunmap_atomic(target, KM_USER0);
1898
1899 flush_dcache_page(page);
1900 SetPageUptodate(page);
1901 unlock_page(page);
1902 data += PAGE_CACHE_SIZE;
1903
1904 /* add page to FS-Cache */
1905 cifs_readpage_to_fscache(mapping->host, page);
1906 }
1907 return;
1908 }
1909
1910 static int cifs_readpages(struct file *file, struct address_space *mapping,
1911 struct list_head *page_list, unsigned num_pages)
1912 {
1913 int rc = -EACCES;
1914 int xid;
1915 loff_t offset;
1916 struct page *page;
1917 struct cifs_sb_info *cifs_sb;
1918 struct cifsTconInfo *pTcon;
1919 unsigned int bytes_read = 0;
1920 unsigned int read_size, i;
1921 char *smb_read_data = NULL;
1922 struct smb_com_read_rsp *pSMBr;
1923 struct cifsFileInfo *open_file;
1924 int buf_type = CIFS_NO_BUFFER;
1925
1926 xid = GetXid();
1927 if (file->private_data == NULL) {
1928 rc = -EBADF;
1929 FreeXid(xid);
1930 return rc;
1931 }
1932 open_file = file->private_data;
1933 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
1934 pTcon = tlink_tcon(open_file->tlink);
1935
1936 /*
1937 * Reads as many pages as possible from fscache. Returns -ENOBUFS
1938 * immediately if the cookie is negative
1939 */
1940 rc = cifs_readpages_from_fscache(mapping->host, mapping, page_list,
1941 &num_pages);
1942 if (rc == 0)
1943 goto read_complete;
1944
1945 cFYI(DBG2, "rpages: num pages %d", num_pages);
1946 for (i = 0; i < num_pages; ) {
1947 unsigned contig_pages;
1948 struct page *tmp_page;
1949 unsigned long expected_index;
1950
1951 if (list_empty(page_list))
1952 break;
1953
1954 page = list_entry(page_list->prev, struct page, lru);
1955 offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
1956
1957 /* count adjacent pages that we will read into */
1958 contig_pages = 0;
1959 expected_index =
1960 list_entry(page_list->prev, struct page, lru)->index;
1961 list_for_each_entry_reverse(tmp_page, page_list, lru) {
1962 if (tmp_page->index == expected_index) {
1963 contig_pages++;
1964 expected_index++;
1965 } else
1966 break;
1967 }
1968 if (contig_pages + i > num_pages)
1969 contig_pages = num_pages - i;
1970
1971 /* for reads over a certain size could initiate async
1972 read ahead */
1973
1974 read_size = contig_pages * PAGE_CACHE_SIZE;
1975 /* Read size needs to be in multiples of one page */
1976 read_size = min_t(const unsigned int, read_size,
1977 cifs_sb->rsize & PAGE_CACHE_MASK);
1978 cFYI(DBG2, "rpages: read size 0x%x contiguous pages %d",
1979 read_size, contig_pages);
1980 rc = -EAGAIN;
1981 while (rc == -EAGAIN) {
1982 if ((open_file->invalidHandle) &&
1983 (!open_file->closePend)) {
1984 rc = cifs_reopen_file(file, true);
1985 if (rc != 0)
1986 break;
1987 }
1988
1989 rc = CIFSSMBRead(xid, pTcon,
1990 open_file->netfid,
1991 read_size, offset,
1992 &bytes_read, &smb_read_data,
1993 &buf_type);
1994 /* BB more RC checks ? */
1995 if (rc == -EAGAIN) {
1996 if (smb_read_data) {
1997 if (buf_type == CIFS_SMALL_BUFFER)
1998 cifs_small_buf_release(smb_read_data);
1999 else if (buf_type == CIFS_LARGE_BUFFER)
2000 cifs_buf_release(smb_read_data);
2001 smb_read_data = NULL;
2002 }
2003 }
2004 }
2005 if ((rc < 0) || (smb_read_data == NULL)) {
2006 cFYI(1, "Read error in readpages: %d", rc);
2007 break;
2008 } else if (bytes_read > 0) {
2009 task_io_account_read(bytes_read);
2010 pSMBr = (struct smb_com_read_rsp *)smb_read_data;
2011 cifs_copy_cache_pages(mapping, page_list, bytes_read,
2012 smb_read_data + 4 /* RFC1001 hdr */ +
2013 le16_to_cpu(pSMBr->DataOffset));
2014
2015 i += bytes_read >> PAGE_CACHE_SHIFT;
2016 cifs_stats_bytes_read(pTcon, bytes_read);
2017 if ((bytes_read & PAGE_CACHE_MASK) != bytes_read) {
2018 i++; /* account for partial page */
2019
2020 /* server copy of file can have smaller size
2021 than client */
2022 /* BB do we need to verify this common case ?
2023 this case is ok - if we are at server EOF
2024 we will hit it on next read */
2025
2026 /* break; */
2027 }
2028 } else {
2029 cFYI(1, "No bytes read (%d) at offset %lld . "
2030 "Cleaning remaining pages from readahead list",
2031 bytes_read, offset);
2032 /* BB turn off caching and do new lookup on
2033 file size at server? */
2034 break;
2035 }
2036 if (smb_read_data) {
2037 if (buf_type == CIFS_SMALL_BUFFER)
2038 cifs_small_buf_release(smb_read_data);
2039 else if (buf_type == CIFS_LARGE_BUFFER)
2040 cifs_buf_release(smb_read_data);
2041 smb_read_data = NULL;
2042 }
2043 bytes_read = 0;
2044 }
2045
2046 /* need to free smb_read_data buf before exit */
2047 if (smb_read_data) {
2048 if (buf_type == CIFS_SMALL_BUFFER)
2049 cifs_small_buf_release(smb_read_data);
2050 else if (buf_type == CIFS_LARGE_BUFFER)
2051 cifs_buf_release(smb_read_data);
2052 smb_read_data = NULL;
2053 }
2054
2055 read_complete:
2056 FreeXid(xid);
2057 return rc;
2058 }
2059
2060 static int cifs_readpage_worker(struct file *file, struct page *page,
2061 loff_t *poffset)
2062 {
2063 char *read_data;
2064 int rc;
2065
2066 /* Is the page cached? */
2067 rc = cifs_readpage_from_fscache(file->f_path.dentry->d_inode, page);
2068 if (rc == 0)
2069 goto read_complete;
2070
2071 page_cache_get(page);
2072 read_data = kmap(page);
2073 /* for reads over a certain size could initiate async read ahead */
2074
2075 rc = cifs_read(file, read_data, PAGE_CACHE_SIZE, poffset);
2076
2077 if (rc < 0)
2078 goto io_error;
2079 else
2080 cFYI(1, "Bytes read %d", rc);
2081
2082 file->f_path.dentry->d_inode->i_atime =
2083 current_fs_time(file->f_path.dentry->d_inode->i_sb);
2084
2085 if (PAGE_CACHE_SIZE > rc)
2086 memset(read_data + rc, 0, PAGE_CACHE_SIZE - rc);
2087
2088 flush_dcache_page(page);
2089 SetPageUptodate(page);
2090
2091 /* send this page to the cache */
2092 cifs_readpage_to_fscache(file->f_path.dentry->d_inode, page);
2093
2094 rc = 0;
2095
2096 io_error:
2097 kunmap(page);
2098 page_cache_release(page);
2099
2100 read_complete:
2101 return rc;
2102 }
2103
2104 static int cifs_readpage(struct file *file, struct page *page)
2105 {
2106 loff_t offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
2107 int rc = -EACCES;
2108 int xid;
2109
2110 xid = GetXid();
2111
2112 if (file->private_data == NULL) {
2113 rc = -EBADF;
2114 FreeXid(xid);
2115 return rc;
2116 }
2117
2118 cFYI(1, "readpage %p at offset %d 0x%x\n",
2119 page, (int)offset, (int)offset);
2120
2121 rc = cifs_readpage_worker(file, page, &offset);
2122
2123 unlock_page(page);
2124
2125 FreeXid(xid);
2126 return rc;
2127 }
2128
2129 static int is_inode_writable(struct cifsInodeInfo *cifs_inode)
2130 {
2131 struct cifsFileInfo *open_file;
2132
2133 read_lock(&GlobalSMBSeslock);
2134 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
2135 if (open_file->closePend)
2136 continue;
2137 if (open_file->pfile &&
2138 ((open_file->pfile->f_flags & O_RDWR) ||
2139 (open_file->pfile->f_flags & O_WRONLY))) {
2140 read_unlock(&GlobalSMBSeslock);
2141 return 1;
2142 }
2143 }
2144 read_unlock(&GlobalSMBSeslock);
2145 return 0;
2146 }
2147
2148 /* We do not want to update the file size from server for inodes
2149 open for write - to avoid races with writepage extending
2150 the file - in the future we could consider allowing
2151 refreshing the inode only on increases in the file size
2152 but this is tricky to do without racing with writebehind
2153 page caching in the current Linux kernel design */
2154 bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file)
2155 {
2156 if (!cifsInode)
2157 return true;
2158
2159 if (is_inode_writable(cifsInode)) {
2160 /* This inode is open for write at least once */
2161 struct cifs_sb_info *cifs_sb;
2162
2163 cifs_sb = CIFS_SB(cifsInode->vfs_inode.i_sb);
2164 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
2165 /* since no page cache to corrupt on directio
2166 we can change size safely */
2167 return true;
2168 }
2169
2170 if (i_size_read(&cifsInode->vfs_inode) < end_of_file)
2171 return true;
2172
2173 return false;
2174 } else
2175 return true;
2176 }
2177
2178 static int cifs_write_begin(struct file *file, struct address_space *mapping,
2179 loff_t pos, unsigned len, unsigned flags,
2180 struct page **pagep, void **fsdata)
2181 {
2182 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2183 loff_t offset = pos & (PAGE_CACHE_SIZE - 1);
2184 loff_t page_start = pos & PAGE_MASK;
2185 loff_t i_size;
2186 struct page *page;
2187 int rc = 0;
2188
2189 cFYI(1, "write_begin from %lld len %d", (long long)pos, len);
2190
2191 page = grab_cache_page_write_begin(mapping, index, flags);
2192 if (!page) {
2193 rc = -ENOMEM;
2194 goto out;
2195 }
2196
2197 if (PageUptodate(page))
2198 goto out;
2199
2200 /*
2201 * If we write a full page it will be up to date, no need to read from
2202 * the server. If the write is short, we'll end up doing a sync write
2203 * instead.
2204 */
2205 if (len == PAGE_CACHE_SIZE)
2206 goto out;
2207
2208 /*
2209 * optimize away the read when we have an oplock, and we're not
2210 * expecting to use any of the data we'd be reading in. That
2211 * is, when the page lies beyond the EOF, or straddles the EOF
2212 * and the write will cover all of the existing data.
2213 */
2214 if (CIFS_I(mapping->host)->clientCanCacheRead) {
2215 i_size = i_size_read(mapping->host);
2216 if (page_start >= i_size ||
2217 (offset == 0 && (pos + len) >= i_size)) {
2218 zero_user_segments(page, 0, offset,
2219 offset + len,
2220 PAGE_CACHE_SIZE);
2221 /*
2222 * PageChecked means that the parts of the page
2223 * to which we're not writing are considered up
2224 * to date. Once the data is copied to the
2225 * page, it can be set uptodate.
2226 */
2227 SetPageChecked(page);
2228 goto out;
2229 }
2230 }
2231
2232 if ((file->f_flags & O_ACCMODE) != O_WRONLY) {
2233 /*
2234 * might as well read a page, it is fast enough. If we get
2235 * an error, we don't need to return it. cifs_write_end will
2236 * do a sync write instead since PG_uptodate isn't set.
2237 */
2238 cifs_readpage_worker(file, page, &page_start);
2239 } else {
2240 /* we could try using another file handle if there is one -
2241 but how would we lock it to prevent close of that handle
2242 racing with this read? In any case
2243 this will be written out by write_end so is fine */
2244 }
2245 out:
2246 *pagep = page;
2247 return rc;
2248 }
2249
2250 static int cifs_release_page(struct page *page, gfp_t gfp)
2251 {
2252 if (PagePrivate(page))
2253 return 0;
2254
2255 return cifs_fscache_release_page(page, gfp);
2256 }
2257
2258 static void cifs_invalidate_page(struct page *page, unsigned long offset)
2259 {
2260 struct cifsInodeInfo *cifsi = CIFS_I(page->mapping->host);
2261
2262 if (offset == 0)
2263 cifs_fscache_invalidate_page(page, &cifsi->vfs_inode);
2264 }
2265
2266 void cifs_oplock_break(struct work_struct *work)
2267 {
2268 struct cifsFileInfo *cfile = container_of(work, struct cifsFileInfo,
2269 oplock_break);
2270 struct inode *inode = cfile->dentry->d_inode;
2271 struct cifsInodeInfo *cinode = CIFS_I(inode);
2272 int rc, waitrc = 0;
2273
2274 if (inode && S_ISREG(inode->i_mode)) {
2275 if (cinode->clientCanCacheRead)
2276 break_lease(inode, O_RDONLY);
2277 else
2278 break_lease(inode, O_WRONLY);
2279 rc = filemap_fdatawrite(inode->i_mapping);
2280 if (cinode->clientCanCacheRead == 0) {
2281 waitrc = filemap_fdatawait(inode->i_mapping);
2282 invalidate_remote_inode(inode);
2283 }
2284 if (!rc)
2285 rc = waitrc;
2286 if (rc)
2287 cinode->write_behind_rc = rc;
2288 cFYI(1, "Oplock flush inode %p rc %d", inode, rc);
2289 }
2290
2291 /*
2292 * releasing stale oplock after recent reconnect of smb session using
2293 * a now incorrect file handle is not a data integrity issue but do
2294 * not bother sending an oplock release if session to server still is
2295 * disconnected since oplock already released by the server
2296 */
2297 if (!cfile->closePend && !cfile->oplock_break_cancelled) {
2298 rc = CIFSSMBLock(0, tlink_tcon(cfile->tlink), cfile->netfid, 0,
2299 0, 0, 0, LOCKING_ANDX_OPLOCK_RELEASE, false);
2300 cFYI(1, "Oplock release rc = %d", rc);
2301 }
2302
2303 /*
2304 * We might have kicked in before is_valid_oplock_break()
2305 * finished grabbing reference for us. Make sure it's done by
2306 * waiting for GlobalSMSSeslock.
2307 */
2308 write_lock(&GlobalSMBSeslock);
2309 write_unlock(&GlobalSMBSeslock);
2310
2311 cifs_oplock_break_put(cfile);
2312 }
2313
2314 void cifs_oplock_break_get(struct cifsFileInfo *cfile)
2315 {
2316 cifs_sb_active(cfile->dentry->d_sb);
2317 cifsFileInfo_get(cfile);
2318 }
2319
2320 void cifs_oplock_break_put(struct cifsFileInfo *cfile)
2321 {
2322 cifsFileInfo_put(cfile);
2323 cifs_sb_deactive(cfile->dentry->d_sb);
2324 }
2325
2326 const struct address_space_operations cifs_addr_ops = {
2327 .readpage = cifs_readpage,
2328 .readpages = cifs_readpages,
2329 .writepage = cifs_writepage,
2330 .writepages = cifs_writepages,
2331 .write_begin = cifs_write_begin,
2332 .write_end = cifs_write_end,
2333 .set_page_dirty = __set_page_dirty_nobuffers,
2334 .releasepage = cifs_release_page,
2335 .invalidatepage = cifs_invalidate_page,
2336 /* .sync_page = cifs_sync_page, */
2337 /* .direct_IO = */
2338 };
2339
2340 /*
2341 * cifs_readpages requires the server to support a buffer large enough to
2342 * contain the header plus one complete page of data. Otherwise, we need
2343 * to leave cifs_readpages out of the address space operations.
2344 */
2345 const struct address_space_operations cifs_addr_ops_smallbuf = {
2346 .readpage = cifs_readpage,
2347 .writepage = cifs_writepage,
2348 .writepages = cifs_writepages,
2349 .write_begin = cifs_write_begin,
2350 .write_end = cifs_write_end,
2351 .set_page_dirty = __set_page_dirty_nobuffers,
2352 .releasepage = cifs_release_page,
2353 .invalidatepage = cifs_invalidate_page,
2354 /* .sync_page = cifs_sync_page, */
2355 /* .direct_IO = */
2356 };
This page took 0.108783 seconds and 5 git commands to generate.