4 * vfs operations that deal with files
6 * Copyright (C) International Business Machines Corp., 2002,2010
7 * Author(s): Steve French (sfrench@us.ibm.com)
8 * Jeremy Allison (jra@samba.org)
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.
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.
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
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>
39 #include "cifsproto.h"
40 #include "cifs_unicode.h"
41 #include "cifs_debug.h"
42 #include "cifs_fs_sb.h"
45 static inline int cifs_convert_flags(unsigned int flags
)
47 if ((flags
& O_ACCMODE
) == O_RDONLY
)
49 else if ((flags
& O_ACCMODE
) == O_WRONLY
)
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
);
58 return (READ_CONTROL
| FILE_WRITE_ATTRIBUTES
| FILE_READ_ATTRIBUTES
|
59 FILE_WRITE_EA
| FILE_APPEND_DATA
| FILE_WRITE_DATA
|
63 static inline fmode_t
cifs_posix_convert_flags(unsigned int flags
)
65 fmode_t posix_flags
= 0;
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
;
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 */
80 posix_flags
|= (fmode_t
)O_APPEND
;
82 posix_flags
|= (fmode_t
)O_DSYNC
;
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
;
90 posix_flags
|= (fmode_t
)O_DIRECT
;
95 static inline int cifs_get_disposition(unsigned int flags
)
97 if ((flags
& (O_CREAT
| O_EXCL
)) == (O_CREAT
| O_EXCL
))
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
)
103 else if ((flags
& O_TRUNC
) == O_TRUNC
)
104 return FILE_OVERWRITE
;
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
)
113 struct cifsInodeInfo
*pCifsInode
= CIFS_I(inode
);
114 struct timespec temp
;
117 if (pCifsInode
->clientCanCacheRead
) {
118 /* we have the inode open somewhere else
119 no need to discard cache data */
120 goto client_can_cache
;
123 /* BB need same check in cifs_create too? */
124 /* if not oplocked, invalidate inode pages if mtime or file
126 temp
= cifs_NTtimeToUnix(buf
->LastWriteTime
);
127 if (timespec_equal(&inode
->i_mtime
, &temp
) &&
129 (loff_t
)le64_to_cpu(buf
->EndOfFile
))) {
130 cFYI(1, "inode unchanged on server");
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
);
137 pCifsInode
->write_behind_rc
= rc
;
139 cFYI(1, "invalidating remote inode since open detected it "
141 invalidate_remote_inode(inode
);
146 rc
= cifs_get_inode_info_unix(&inode
, full_path
, inode
->i_sb
,
149 rc
= cifs_get_inode_info(&inode
, full_path
, buf
, inode
->i_sb
,
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;
162 int cifs_open(struct inode
*inode
, struct file
*file
)
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
;
176 FILE_ALL_INFO
*buf
= NULL
;
180 cifs_sb
= CIFS_SB(inode
->i_sb
);
181 tlink
= cifs_sb_tlink(cifs_sb
);
184 return PTR_ERR(tlink
);
186 tcon
= tlink_tcon(tlink
);
188 pCifsInode
= CIFS_I(file
->f_path
.dentry
->d_inode
);
190 full_path
= build_path_from_dentry(file
->f_path
.dentry
);
191 if (full_path
== NULL
) {
196 cFYI(1, "inode = 0x%p file flags are 0x%x for %s",
197 inode
, file
->f_flags
, full_path
);
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
);
215 cFYI(1, "posix open succeeded");
217 pCifsFile
= cifs_new_fileinfo(inode
, netfid
, file
,
218 tlink
, oflags
, oplock
);
219 if (pCifsFile
== NULL
) {
220 CIFSSMBClose(xid
, tcon
, netfid
);
224 cifs_fscache_set_inode_cookie(inode
, file
);
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 */
239 /* else fallthrough to retry open the old way on network i/o
243 desiredAccess
= cifs_convert_flags(file
->f_flags
);
245 /*********************************************************************
246 * open flag mapping table:
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
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)
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 *********************************************************************/
269 disposition
= cifs_get_disposition(file
->f_flags
);
271 /* BB pass O_SYNC flag through on file attributes .. BB */
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) */
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
);
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
);
292 rc
= -EIO
; /* no NT SMB support fall into legacy open below */
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
);
302 cFYI(1, "cifs_open returned 0x%x", rc
);
306 rc
= cifs_open_inode_helper(inode
, tcon
, oplock
, buf
, full_path
, xid
);
310 pCifsFile
= cifs_new_fileinfo(inode
, netfid
, file
, tlink
,
311 file
->f_flags
, oplock
);
312 if (pCifsFile
== NULL
) {
317 cifs_fscache_set_inode_cookie(inode
, file
);
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
,
327 .ctime
= NO_CHANGE_64
,
328 .atime
= NO_CHANGE_64
,
329 .mtime
= NO_CHANGE_64
,
332 CIFSSMBUnixSetPathInfo(xid
, tcon
, full_path
, &args
,
334 cifs_sb
->mnt_cifs_flags
&
335 CIFS_MOUNT_MAP_SPECIAL_CHR
);
343 cifs_put_tlink(tlink
);
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
)
353 /* BB list all locks open on this file and relock */
358 static int cifs_reopen_file(struct file
*file
, bool can_flush
)
363 struct cifs_sb_info
*cifs_sb
;
364 struct cifsTconInfo
*tcon
;
365 struct cifsFileInfo
*pCifsFile
;
366 struct cifsInodeInfo
*pCifsInode
;
368 char *full_path
= NULL
;
370 int disposition
= FILE_OPEN
;
373 if (file
->private_data
)
374 pCifsFile
= file
->private_data
;
379 mutex_lock(&pCifsFile
->fh_mutex
);
380 if (!pCifsFile
->invalidHandle
) {
381 mutex_unlock(&pCifsFile
->fh_mutex
);
387 if (file
->f_path
.dentry
== NULL
) {
388 cERROR(1, "no valid name if dentry freed");
391 goto reopen_error_exit
;
394 inode
= file
->f_path
.dentry
->d_inode
;
396 cERROR(1, "inode not valid");
399 goto reopen_error_exit
;
402 cifs_sb
= CIFS_SB(inode
->i_sb
);
403 tcon
= tlink_tcon(pCifsFile
->tlink
);
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
) {
413 mutex_unlock(&pCifsFile
->fh_mutex
);
418 cFYI(1, "inode = 0x%p file flags 0x%x for %s",
419 inode
, file
->f_flags
, full_path
);
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
);
435 cFYI(1, "posix reopen succeeded");
438 /* fallthrough to retry open the old way on errors, especially
439 in the reconnect path it is important to retry hard */
442 desiredAccess
= cifs_convert_flags(file
->f_flags
);
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 */
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
);
455 mutex_unlock(&pCifsFile
->fh_mutex
);
456 cFYI(1, "cifs_open returned 0x%x", rc
);
457 cFYI(1, "oplock: %d", oplock
);
460 pCifsFile
->netfid
= netfid
;
461 pCifsFile
->invalidHandle
= false;
462 mutex_unlock(&pCifsFile
->fh_mutex
);
463 pCifsInode
= CIFS_I(inode
);
466 rc
= filemap_write_and_wait(inode
->i_mapping
);
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;
474 rc
= cifs_get_inode_info_unix(&inode
,
475 full_path
, inode
->i_sb
, xid
);
477 rc
= cifs_get_inode_info(&inode
,
478 full_path
, NULL
, inode
->i_sb
,
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
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;
495 pCifsInode
->clientCanCacheRead
= false;
496 pCifsInode
->clientCanCacheAll
= false;
498 cifs_relock_file(pCifsFile
);
506 int cifs_close(struct inode
*inode
, struct file
*file
)
510 struct cifs_sb_info
*cifs_sb
;
511 struct cifsTconInfo
*pTcon
;
512 struct cifsFileInfo
*pSMBFile
= file
->private_data
;
516 cifs_sb
= CIFS_SB(inode
->i_sb
);
517 pTcon
= tlink_tcon(pSMBFile
->tlink
);
519 struct cifsLockInfo
*li
, *tmp
;
520 write_lock(&GlobalSMBSeslock
);
521 pSMBFile
->closePend
= true;
523 /* no sense reconnecting to close a file that is
525 if (!pTcon
->need_reconnect
) {
526 write_unlock(&GlobalSMBSeslock
);
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
537 cFYI(DBG2
, "close delay, write pending");
541 if (!pTcon
->need_reconnect
&&
542 !pSMBFile
->invalidHandle
)
543 rc
= CIFSSMBClose(xid
, pTcon
,
546 write_unlock(&GlobalSMBSeslock
);
548 write_unlock(&GlobalSMBSeslock
);
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
);
557 mutex_unlock(&pSMBFile
->lock_mutex
);
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
;
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;
576 read_unlock(&GlobalSMBSeslock
);
577 if ((rc
== 0) && CIFS_I(inode
)->write_behind_rc
)
578 rc
= CIFS_I(inode
)->write_behind_rc
;
583 int cifs_closedir(struct inode
*inode
, struct file
*file
)
587 struct cifsFileInfo
*pCFileStruct
= file
->private_data
;
590 cFYI(1, "Closedir inode = 0x%p", inode
);
595 struct cifsTconInfo
*pTcon
= tlink_tcon(pCFileStruct
->tlink
);
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",
606 /* not much we can do if it fails anyway, ignore rc */
609 write_unlock(&GlobalSMBSeslock
);
610 ptmp
= pCFileStruct
->srch_inf
.ntwrk_buf_start
;
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
);
617 cifs_buf_release(ptmp
);
619 cifs_put_tlink(pCFileStruct
->tlink
);
620 kfree(file
->private_data
);
621 file
->private_data
= NULL
;
623 /* BB can we lock the filestruct while this is going on? */
628 static int store_file_lock(struct cifsFileInfo
*fid
, __u64 len
,
629 __u64 offset
, __u8 lockType
)
631 struct cifsLockInfo
*li
=
632 kmalloc(sizeof(struct cifsLockInfo
), GFP_KERNEL
);
638 mutex_lock(&fid
->lock_mutex
);
639 list_add(&li
->llist
, &fid
->llist
);
640 mutex_unlock(&fid
->lock_mutex
);
644 int cifs_lock(struct file
*file
, int cmd
, struct file_lock
*pfLock
)
650 bool wait_flag
= false;
651 struct cifs_sb_info
*cifs_sb
;
652 struct cifsTconInfo
*tcon
;
654 __u8 lockType
= LOCKING_ANDX_LARGE_FILES
;
655 bool posix_locking
= 0;
657 length
= 1 + pfLock
->fl_end
- pfLock
->fl_start
;
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
,
666 if (pfLock
->fl_flags
& FL_POSIX
)
668 if (pfLock
->fl_flags
& FL_FLOCK
)
670 if (pfLock
->fl_flags
& FL_SLEEP
) {
671 cFYI(1, "Blocking lock");
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
);
683 if (pfLock
->fl_type
== F_WRLCK
) {
686 } else if (pfLock
->fl_type
== F_UNLCK
) {
689 /* Check if unlock includes more than
691 } else if (pfLock
->fl_type
== F_RDLCK
) {
693 lockType
|= LOCKING_ANDX_SHARED_LOCK
;
695 } else if (pfLock
->fl_type
== F_EXLCK
) {
698 } else if (pfLock
->fl_type
== F_SHLCK
) {
700 lockType
|= LOCKING_ANDX_SHARED_LOCK
;
703 cFYI(1, "Unknown type of lock");
705 cifs_sb
= CIFS_SB(file
->f_path
.dentry
->d_sb
);
706 tcon
= tlink_tcon(((struct cifsFileInfo
*)file
->private_data
)->tlink
);
708 if (file
->private_data
== NULL
) {
713 netfid
= ((struct cifsFileInfo
*)file
->private_data
)->netfid
;
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))
719 /* BB add code here to normalize offset and length to
720 account for negative length which we can not accept over the
725 if (lockType
& LOCKING_ANDX_SHARED_LOCK
)
726 posix_lock_type
= CIFS_RDLCK
;
728 posix_lock_type
= CIFS_WRLCK
;
729 rc
= CIFSSMBPosixLock(xid
, tcon
, netfid
, 1 /* get */,
731 posix_lock_type
, wait_flag
);
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 */ );
740 rc
= CIFSSMBLock(xid
, tcon
, netfid
, length
,
741 pfLock
->fl_start
, 1 /* numUnlock */ ,
742 0 /* numLock */ , lockType
,
744 pfLock
->fl_type
= F_UNLCK
;
746 cERROR(1, "Error unlocking previously locked "
747 "range %d during test of lock", rc
);
751 /* if rc == ERR_SHARING_VIOLATION ? */
754 if (lockType
& LOCKING_ANDX_SHARED_LOCK
) {
755 pfLock
->fl_type
= F_WRLCK
;
757 rc
= CIFSSMBLock(xid
, tcon
, netfid
, length
,
758 pfLock
->fl_start
, 0, 1,
759 lockType
| LOCKING_ANDX_SHARED_LOCK
,
762 rc
= CIFSSMBLock(xid
, tcon
, netfid
,
763 length
, pfLock
->fl_start
, 1, 0,
765 LOCKING_ANDX_SHARED_LOCK
,
767 pfLock
->fl_type
= F_RDLCK
;
769 cERROR(1, "Error unlocking "
770 "previously locked range %d "
771 "during test of lock", rc
);
774 pfLock
->fl_type
= F_WRLCK
;
784 if (!numLock
&& !numUnlock
) {
785 /* if no lock or unlock then nothing
786 to do since we do not know what it is */
793 if (lockType
& LOCKING_ANDX_SHARED_LOCK
)
794 posix_lock_type
= CIFS_RDLCK
;
796 posix_lock_type
= CIFS_WRLCK
;
799 posix_lock_type
= CIFS_UNLCK
;
801 rc
= CIFSSMBPosixLock(xid
, tcon
, netfid
, 0 /* set */,
803 posix_lock_type
, wait_flag
);
805 struct cifsFileInfo
*fid
= file
->private_data
;
808 rc
= CIFSSMBLock(xid
, tcon
, netfid
, length
,
810 0, numLock
, lockType
, wait_flag
);
813 /* For Windows locks we must store them. */
814 rc
= store_file_lock(fid
, length
,
815 pfLock
->fl_start
, lockType
);
817 } else if (numUnlock
) {
818 /* For each stored lock that this unlock overlaps
819 completely, unlock it. */
821 struct cifsLockInfo
*li
, *tmp
;
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
,
831 li
->length
, li
->offset
,
832 1, 0, li
->type
, false);
836 list_del(&li
->llist
);
841 mutex_unlock(&fid
->lock_mutex
);
845 if (pfLock
->fl_flags
& FL_POSIX
)
846 posix_lock_file_wait(file
, pfLock
);
852 * Set the timeout on write requests past EOF. For some servers (Windows)
853 * these calls can be very long.
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.
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.
864 cifs_write_timeout(struct cifsInodeInfo
*cifsi
, loff_t offset
)
866 if (offset
<= cifsi
->server_eof
)
868 else if (offset
> (cifsi
->server_eof
+ (10 * 1024 * 1024)))
869 return CIFS_VLONG_OP
;
874 /* update the file size (if needed) after a write */
876 cifs_update_eof(struct cifsInodeInfo
*cifsi
, loff_t offset
,
877 unsigned int bytes_written
)
879 loff_t end_of_write
= offset
+ bytes_written
;
881 if (end_of_write
> cifsi
->server_eof
)
882 cifsi
->server_eof
= end_of_write
;
885 ssize_t
cifs_user_write(struct file
*file
, const char __user
*write_data
,
886 size_t write_size
, loff_t
*poffset
)
889 unsigned int bytes_written
= 0;
890 unsigned int total_written
;
891 struct cifs_sb_info
*cifs_sb
;
892 struct cifsTconInfo
*pTcon
;
894 struct cifsFileInfo
*open_file
;
895 struct cifsInodeInfo
*cifsi
= CIFS_I(file
->f_path
.dentry
->d_inode
);
897 cifs_sb
= CIFS_SB(file
->f_path
.dentry
->d_sb
);
899 /* cFYI(1, " write %d bytes to offset %lld of %s", write_size,
900 *poffset, file->f_path.dentry->d_name.name); */
902 if (file
->private_data
== NULL
)
905 open_file
= file
->private_data
;
906 pTcon
= tlink_tcon(open_file
->tlink
);
908 rc
= generic_write_checks(file
, poffset
, &write_size
, 0);
914 long_op
= cifs_write_timeout(cifsi
, *poffset
);
915 for (total_written
= 0; write_size
> total_written
;
916 total_written
+= bytes_written
) {
918 while (rc
== -EAGAIN
) {
919 if (file
->private_data
== NULL
) {
920 /* file has been closed on us */
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
;
927 if (open_file
->closePend
) {
930 return total_written
;
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
939 rc
= cifs_reopen_file(file
, false);
944 rc
= CIFSSMBWrite(xid
, pTcon
,
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
);
951 if (rc
|| (bytes_written
== 0)) {
959 cifs_update_eof(cifsi
, *poffset
, bytes_written
);
960 *poffset
+= bytes_written
;
962 long_op
= CIFS_STD_OP
; /* subsequent writes fast -
963 15 seconds is plenty */
966 cifs_stats_bytes_written(pTcon
, total_written
);
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
,
979 spin_unlock(&inode
->i_lock
);
981 mark_inode_dirty_sync(file
->f_path
.dentry
->d_inode
);
984 return total_written
;
987 static ssize_t
cifs_write(struct file
*file
, const char *write_data
,
988 size_t write_size
, loff_t
*poffset
)
991 unsigned int bytes_written
= 0;
992 unsigned int total_written
;
993 struct cifs_sb_info
*cifs_sb
;
994 struct cifsTconInfo
*pTcon
;
996 struct cifsFileInfo
*open_file
;
997 struct cifsInodeInfo
*cifsi
= CIFS_I(file
->f_path
.dentry
->d_inode
);
999 cifs_sb
= CIFS_SB(file
->f_path
.dentry
->d_sb
);
1001 cFYI(1, "write %zd bytes to offset %lld of %s", write_size
,
1002 *poffset
, file
->f_path
.dentry
->d_name
.name
);
1004 if (file
->private_data
== NULL
)
1006 open_file
= file
->private_data
;
1007 pTcon
= tlink_tcon(open_file
->tlink
);
1011 long_op
= cifs_write_timeout(cifsi
, *poffset
);
1012 for (total_written
= 0; write_size
> total_written
;
1013 total_written
+= bytes_written
) {
1015 while (rc
== -EAGAIN
) {
1016 if (file
->private_data
== NULL
) {
1017 /* file has been closed on us */
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
1023 return total_written
;
1025 if (open_file
->closePend
) {
1028 return total_written
;
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
1037 rc
= cifs_reopen_file(file
, false);
1041 if (experimEnabled
|| (pTcon
->ses
->server
&&
1042 ((pTcon
->ses
->server
->secMode
&
1043 (SECMODE_SIGN_REQUIRED
| SECMODE_SIGN_ENABLED
))
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
+
1053 iov
[1].iov_len
= len
;
1054 rc
= CIFSSMBWrite2(xid
, pTcon
,
1055 open_file
->netfid
, len
,
1056 *poffset
, &bytes_written
,
1059 rc
= CIFSSMBWrite(xid
, pTcon
,
1061 min_t(const int, cifs_sb
->wsize
,
1062 write_size
- total_written
),
1063 *poffset
, &bytes_written
,
1064 write_data
+ total_written
,
1067 if (rc
|| (bytes_written
== 0)) {
1075 cifs_update_eof(cifsi
, *poffset
, bytes_written
);
1076 *poffset
+= bytes_written
;
1078 long_op
= CIFS_STD_OP
; /* subsequent writes fast -
1079 15 seconds is plenty */
1082 cifs_stats_bytes_written(pTcon
, total_written
);
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
,
1094 spin_unlock(&file
->f_path
.dentry
->d_inode
->i_lock
);
1096 mark_inode_dirty_sync(file
->f_path
.dentry
->d_inode
);
1099 return total_written
;
1102 #ifdef CONFIG_CIFS_EXPERIMENTAL
1103 struct cifsFileInfo
*find_readable_file(struct cifsInodeInfo
*cifs_inode
,
1106 struct cifsFileInfo
*open_file
= NULL
;
1107 struct cifs_sb_info
*cifs_sb
= CIFS_SB(cifs_inode
->vfs_inode
.i_sb
);
1109 /* only filter by fsuid on multiuser mounts */
1110 if (!(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_MULTIUSER
))
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
)
1120 if (fsuid_only
&& open_file
->uid
!= current_fsuid())
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
);
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 */
1136 read_unlock(&GlobalSMBSeslock
);
1141 struct cifsFileInfo
*find_writable_file(struct cifsInodeInfo
*cifs_inode
,
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;
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 */
1153 if (cifs_inode
== NULL
) {
1154 cERROR(1, "Null inode passed to cifs_writeable_file");
1159 /* only filter by fsuid on multiuser mounts */
1160 if (!(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_MULTIUSER
))
1163 read_lock(&GlobalSMBSeslock
);
1165 list_for_each_entry(open_file
, &cifs_inode
->openFileList
, flist
) {
1166 if (open_file
->closePend
)
1168 if (!any_available
&& open_file
->pid
!= current
->tgid
)
1170 if (fsuid_only
&& open_file
->uid
!= current_fsuid())
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
);
1177 if (!open_file
->invalidHandle
) {
1178 /* found a good writable file */
1179 read_unlock(&GlobalSMBSeslock
);
1183 read_unlock(&GlobalSMBSeslock
);
1184 /* Had to unlock since following call can block */
1185 rc
= cifs_reopen_file(open_file
->pfile
, false);
1187 if (!open_file
->closePend
)
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
;
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
);
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. */
1221 /* couldn't find useable FH with same pid, try any available */
1222 if (!any_available
) {
1223 any_available
= true;
1224 goto refind_writable
;
1226 read_unlock(&GlobalSMBSeslock
);
1230 static int cifs_partialpagewrite(struct page
*page
, unsigned from
, unsigned to
)
1232 struct address_space
*mapping
= page
->mapping
;
1233 loff_t offset
= (loff_t
)page
->index
<< PAGE_CACHE_SHIFT
;
1236 int bytes_written
= 0;
1237 struct cifs_sb_info
*cifs_sb
;
1238 struct inode
*inode
;
1239 struct cifsFileInfo
*open_file
;
1241 if (!mapping
|| !mapping
->host
)
1244 inode
= page
->mapping
->host
;
1245 cifs_sb
= CIFS_SB(inode
->i_sb
);
1247 offset
+= (loff_t
)from
;
1248 write_data
= kmap(page
);
1251 if ((to
> PAGE_CACHE_SIZE
) || (from
> to
)) {
1256 /* racing with truncate? */
1257 if (offset
> mapping
->host
->i_size
) {
1259 return 0; /* don't care */
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
);
1266 open_file
= find_writable_file(CIFS_I(mapping
->host
), false);
1268 bytes_written
= cifs_write(open_file
->pfile
, write_data
,
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
))
1275 else if (bytes_written
< 0)
1278 cFYI(1, "No writeable filehandles for inode");
1286 static int cifs_writepages(struct address_space
*mapping
,
1287 struct writeback_control
*wbc
)
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
;
1296 int range_whole
= 0;
1303 struct cifsFileInfo
*open_file
;
1304 struct cifsTconInfo
*tcon
;
1305 struct cifsInodeInfo
*cifsi
= CIFS_I(mapping
->host
);
1307 struct pagevec pvec
;
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
1316 if (wbc
->nonblocking
&& bdi_write_congested(bdi
)) {
1317 wbc
->encountered_congestion
= 1;
1321 cifs_sb
= CIFS_SB(mapping
->host
->i_sb
);
1324 * If wsize is smaller that the page cache size, default to writing
1325 * one page at a time via cifs_writepage
1327 if (cifs_sb
->wsize
< PAGE_CACHE_SIZE
)
1328 return generic_writepages(mapping
, wbc
);
1330 iov
= kmalloc(32 * sizeof(struct kvec
), GFP_KERNEL
);
1332 return generic_writepages(mapping
, wbc
);
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
1339 open_file
= find_writable_file(CIFS_I(mapping
->host
), false);
1342 return generic_writepages(mapping
, wbc
);
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
);
1351 cifsFileInfo_put(open_file
);
1355 pagevec_init(&pvec
, 0);
1356 if (wbc
->range_cyclic
) {
1357 index
= mapping
->writeback_index
; /* Start from prev offset */
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
)
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))) {
1379 for (i
= 0; i
< nr_pages
; i
++) {
1380 page
= pvec
.pages
[i
];
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
1391 else if (!trylock_page(page
))
1394 if (unlikely(page
->mapping
!= mapping
)) {
1399 if (!wbc
->range_cyclic
&& page
->index
> end
) {
1405 if (next
&& (page
->index
!= next
)) {
1406 /* Not next consecutive page */
1411 if (wbc
->sync_mode
!= WB_SYNC_NONE
)
1412 wait_on_page_writeback(page
);
1414 if (PageWriteback(page
) ||
1415 !clear_page_dirty_for_io(page
)) {
1421 * This actually clears the dirty bit in the radix tree.
1422 * See cifs_writepage() for more commentary.
1424 set_page_writeback(page
);
1426 if (page_offset(page
) >= mapping
->host
->i_size
) {
1429 end_page_writeback(page
);
1434 * BB can we get rid of this? pages are held by pvec
1436 page_cache_get(page
);
1438 len
= min(mapping
->host
->i_size
- page_offset(page
),
1439 (loff_t
)PAGE_CACHE_SIZE
);
1441 /* reserve iov[0] for the smb header */
1443 iov
[n_iov
].iov_base
= kmap(page
);
1444 iov
[n_iov
].iov_len
= len
;
1445 bytes_to_write
+= len
;
1449 offset
= page_offset(page
);
1451 next
= page
->index
+ 1;
1452 if (bytes_to_write
+ PAGE_CACHE_SIZE
> cifs_sb
->wsize
)
1456 open_file
= find_writable_file(CIFS_I(mapping
->host
),
1459 cERROR(1, "No writable handles for inode");
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
,
1467 cifsFileInfo_put(open_file
);
1468 cifs_update_eof(cifsi
, offset
, bytes_written
);
1471 if (rc
|| bytes_written
< bytes_to_write
) {
1472 cERROR(1, "Write2 ret %d, wrote %d",
1474 /* BB what if continued retry is
1475 requested via mount flags? */
1477 set_bit(AS_ENOSPC
, &mapping
->flags
);
1479 set_bit(AS_EIO
, &mapping
->flags
);
1481 cifs_stats_bytes_written(tcon
, bytes_written
);
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 */
1495 end_page_writeback(page
);
1496 page_cache_release(page
);
1498 if ((wbc
->nr_to_write
-= n_iov
) <= 0)
1502 /* Need to re-find the pages we skipped */
1503 index
= pvec
.pages
[0]->index
+ 1;
1505 pagevec_release(&pvec
);
1507 if (!scanned
&& !done
) {
1509 * We hit the last page and there is more work to be done: wrap
1510 * back to the start of the file
1516 if (wbc
->range_cyclic
|| (range_whole
&& wbc
->nr_to_write
> 0))
1517 mapping
->writeback_index
= index
;
1524 static int cifs_writepage(struct page
*page
, struct writeback_control
*wbc
)
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");
1536 * Set the "writeback" flag, and clear "dirty" in the radix tree.
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.
1542 * Just unlocking the page will cause the radix tree tag-bits
1543 * to fail to update with the state of the page correctly.
1545 set_page_writeback(page
);
1546 rc
= cifs_partialpagewrite(page
, 0, PAGE_CACHE_SIZE
);
1547 SetPageUptodate(page
); /* BB add check for error and Clearuptodate? */
1549 end_page_writeback(page
);
1550 page_cache_release(page
);
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
)
1560 struct inode
*inode
= mapping
->host
;
1562 cFYI(1, "write_end for page %p from pos %lld with %d bytes",
1565 if (PageChecked(page
)) {
1567 SetPageUptodate(page
);
1568 ClearPageChecked(page
);
1569 } else if (!PageUptodate(page
) && copied
== PAGE_CACHE_SIZE
)
1570 SetPageUptodate(page
);
1572 if (!PageUptodate(page
)) {
1574 unsigned offset
= pos
& (PAGE_CACHE_SIZE
- 1);
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? */
1592 set_page_dirty(page
);
1596 spin_lock(&inode
->i_lock
);
1597 if (pos
> inode
->i_size
)
1598 i_size_write(inode
, pos
);
1599 spin_unlock(&inode
->i_lock
);
1603 page_cache_release(page
);
1608 int cifs_fsync(struct file
*file
, int datasync
)
1612 struct cifsTconInfo
*tcon
;
1613 struct cifsFileInfo
*smbfile
= file
->private_data
;
1614 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1618 cFYI(1, "Sync file - name: %s datasync: 0x%x",
1619 file
->f_path
.dentry
->d_name
.name
, datasync
);
1621 rc
= filemap_write_and_wait(inode
->i_mapping
);
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
);
1635 /* static void cifs_sync_page(struct page *page)
1637 struct address_space *mapping;
1638 struct inode *inode;
1639 unsigned long index = page->index;
1640 unsigned int rpages = 0;
1643 cFYI(1, "sync page %p", page);
1644 mapping = page->mapping;
1647 inode = mapping->host;
1651 /* fill in rpages then
1652 result = cifs_pagein_inode(inode, index, rpages); */ /* BB finish */
1654 /* cFYI(1, "rpages is %d for sync page of Index %ld", rpages, index);
1664 * As file closes, flush all cached write data for this inode checking
1665 * for write behind errors.
1667 int cifs_flush(struct file
*file
, fl_owner_t id
)
1669 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
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 */
1681 rc
= filemap_fdatawrite(inode
->i_mapping
);
1682 /* reset wb rc if we were able to write out dirty pages */
1684 rc
= CIFS_I(inode
)->write_behind_rc
;
1685 CIFS_I(inode
)->write_behind_rc
= 0;
1688 cFYI(1, "Flush inode %p file %p rc %d", inode
, file
, rc
);
1693 ssize_t
cifs_user_read(struct file
*file
, char __user
*read_data
,
1694 size_t read_size
, loff_t
*poffset
)
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
;
1703 struct cifsFileInfo
*open_file
;
1704 char *smb_read_data
;
1705 char __user
*current_offset
;
1706 struct smb_com_read_rsp
*pSMBr
;
1709 cifs_sb
= CIFS_SB(file
->f_path
.dentry
->d_sb
);
1711 if (file
->private_data
== NULL
) {
1716 open_file
= file
->private_data
;
1717 pTcon
= tlink_tcon(open_file
->tlink
);
1719 if ((file
->f_flags
& O_ACCMODE
) == O_WRONLY
)
1720 cFYI(1, "attempting read on write only file instance");
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
,
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);
1737 rc
= CIFSSMBRead(xid
, pTcon
,
1739 current_read_size
, *poffset
,
1740 &bytes_read
, &smb_read_data
,
1742 pSMBr
= (struct smb_com_read_rsp
*)smb_read_data
;
1743 if (smb_read_data
) {
1744 if (copy_to_user(current_offset
,
1746 4 /* RFC1001 length field */ +
1747 le16_to_cpu(pSMBr
->DataOffset
),
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
;
1758 if (rc
|| (bytes_read
== 0)) {
1766 cifs_stats_bytes_read(pTcon
, bytes_read
);
1767 *poffset
+= bytes_read
;
1775 static ssize_t
cifs_read(struct file
*file
, char *read_data
, size_t read_size
,
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
;
1785 char *current_offset
;
1786 struct cifsFileInfo
*open_file
;
1787 int buf_type
= CIFS_NO_BUFFER
;
1790 cifs_sb
= CIFS_SB(file
->f_path
.dentry
->d_sb
);
1792 if (file
->private_data
== NULL
) {
1797 open_file
= file
->private_data
;
1798 pTcon
= tlink_tcon(open_file
->tlink
);
1800 if ((file
->f_flags
& O_ACCMODE
) == O_WRONLY
)
1801 cFYI(1, "attempting read on write only file instance");
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
,
1808 /* For windows me and 9x we do not want to request more
1809 than it negotiated since it will refuse the read then */
1811 !(pTcon
->ses
->capabilities
& CAP_LARGE_FILES
)) {
1812 current_read_size
= min_t(const int, current_read_size
,
1813 pTcon
->ses
->server
->maxBuf
- 128);
1816 while (rc
== -EAGAIN
) {
1817 if ((open_file
->invalidHandle
) &&
1818 (!open_file
->closePend
)) {
1819 rc
= cifs_reopen_file(file
, true);
1823 rc
= CIFSSMBRead(xid
, pTcon
,
1825 current_read_size
, *poffset
,
1826 &bytes_read
, ¤t_offset
,
1829 if (rc
|| (bytes_read
== 0)) {
1837 cifs_stats_bytes_read(pTcon
, total_read
);
1838 *poffset
+= bytes_read
;
1845 int cifs_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1850 rc
= cifs_revalidate_file(file
);
1852 cFYI(1, "Validation prior to mmap failed, error=%d", rc
);
1856 rc
= generic_file_mmap(file
, vma
);
1862 static void cifs_copy_cache_pages(struct address_space
*mapping
,
1863 struct list_head
*pages
, int bytes_read
, char *data
)
1868 while (bytes_read
> 0) {
1869 if (list_empty(pages
))
1872 page
= list_entry(pages
->prev
, struct page
, lru
);
1873 list_del(&page
->lru
);
1875 if (add_to_page_cache_lru(page
, mapping
, page
->index
,
1877 page_cache_release(page
);
1878 cFYI(1, "Add page cache failed");
1879 data
+= PAGE_CACHE_SIZE
;
1880 bytes_read
-= PAGE_CACHE_SIZE
;
1883 page_cache_release(page
);
1885 target
= kmap_atomic(page
, KM_USER0
);
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
);
1894 memcpy(target
, data
, PAGE_CACHE_SIZE
);
1895 bytes_read
-= PAGE_CACHE_SIZE
;
1897 kunmap_atomic(target
, KM_USER0
);
1899 flush_dcache_page(page
);
1900 SetPageUptodate(page
);
1902 data
+= PAGE_CACHE_SIZE
;
1904 /* add page to FS-Cache */
1905 cifs_readpage_to_fscache(mapping
->host
, page
);
1910 static int cifs_readpages(struct file
*file
, struct address_space
*mapping
,
1911 struct list_head
*page_list
, unsigned num_pages
)
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
;
1927 if (file
->private_data
== NULL
) {
1932 open_file
= file
->private_data
;
1933 cifs_sb
= CIFS_SB(file
->f_path
.dentry
->d_sb
);
1934 pTcon
= tlink_tcon(open_file
->tlink
);
1937 * Reads as many pages as possible from fscache. Returns -ENOBUFS
1938 * immediately if the cookie is negative
1940 rc
= cifs_readpages_from_fscache(mapping
->host
, mapping
, page_list
,
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
;
1951 if (list_empty(page_list
))
1954 page
= list_entry(page_list
->prev
, struct page
, lru
);
1955 offset
= (loff_t
)page
->index
<< PAGE_CACHE_SHIFT
;
1957 /* count adjacent pages that we will read into */
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
) {
1968 if (contig_pages
+ i
> num_pages
)
1969 contig_pages
= num_pages
- i
;
1971 /* for reads over a certain size could initiate async
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
);
1981 while (rc
== -EAGAIN
) {
1982 if ((open_file
->invalidHandle
) &&
1983 (!open_file
->closePend
)) {
1984 rc
= cifs_reopen_file(file
, true);
1989 rc
= CIFSSMBRead(xid
, pTcon
,
1992 &bytes_read
, &smb_read_data
,
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
;
2005 if ((rc
< 0) || (smb_read_data
== NULL
)) {
2006 cFYI(1, "Read error in readpages: %d", rc
);
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
));
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 */
2020 /* server copy of file can have smaller size
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 */
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? */
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
;
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
;
2060 static int cifs_readpage_worker(struct file
*file
, struct page
*page
,
2066 /* Is the page cached? */
2067 rc
= cifs_readpage_from_fscache(file
->f_path
.dentry
->d_inode
, page
);
2071 page_cache_get(page
);
2072 read_data
= kmap(page
);
2073 /* for reads over a certain size could initiate async read ahead */
2075 rc
= cifs_read(file
, read_data
, PAGE_CACHE_SIZE
, poffset
);
2080 cFYI(1, "Bytes read %d", rc
);
2082 file
->f_path
.dentry
->d_inode
->i_atime
=
2083 current_fs_time(file
->f_path
.dentry
->d_inode
->i_sb
);
2085 if (PAGE_CACHE_SIZE
> rc
)
2086 memset(read_data
+ rc
, 0, PAGE_CACHE_SIZE
- rc
);
2088 flush_dcache_page(page
);
2089 SetPageUptodate(page
);
2091 /* send this page to the cache */
2092 cifs_readpage_to_fscache(file
->f_path
.dentry
->d_inode
, page
);
2098 page_cache_release(page
);
2104 static int cifs_readpage(struct file
*file
, struct page
*page
)
2106 loff_t offset
= (loff_t
)page
->index
<< PAGE_CACHE_SHIFT
;
2112 if (file
->private_data
== NULL
) {
2118 cFYI(1, "readpage %p at offset %d 0x%x\n",
2119 page
, (int)offset
, (int)offset
);
2121 rc
= cifs_readpage_worker(file
, page
, &offset
);
2129 static int is_inode_writable(struct cifsInodeInfo
*cifs_inode
)
2131 struct cifsFileInfo
*open_file
;
2133 read_lock(&GlobalSMBSeslock
);
2134 list_for_each_entry(open_file
, &cifs_inode
->openFileList
, flist
) {
2135 if (open_file
->closePend
)
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
);
2144 read_unlock(&GlobalSMBSeslock
);
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
)
2159 if (is_inode_writable(cifsInode
)) {
2160 /* This inode is open for write at least once */
2161 struct cifs_sb_info
*cifs_sb
;
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 */
2170 if (i_size_read(&cifsInode
->vfs_inode
) < end_of_file
)
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
)
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
;
2189 cFYI(1, "write_begin from %lld len %d", (long long)pos
, len
);
2191 page
= grab_cache_page_write_begin(mapping
, index
, flags
);
2197 if (PageUptodate(page
))
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
2205 if (len
== PAGE_CACHE_SIZE
)
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.
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
,
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.
2227 SetPageChecked(page
);
2232 if ((file
->f_flags
& O_ACCMODE
) != O_WRONLY
) {
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.
2238 cifs_readpage_worker(file
, page
, &page_start
);
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 */
2250 static int cifs_release_page(struct page
*page
, gfp_t gfp
)
2252 if (PagePrivate(page
))
2255 return cifs_fscache_release_page(page
, gfp
);
2258 static void cifs_invalidate_page(struct page
*page
, unsigned long offset
)
2260 struct cifsInodeInfo
*cifsi
= CIFS_I(page
->mapping
->host
);
2263 cifs_fscache_invalidate_page(page
, &cifsi
->vfs_inode
);
2266 void cifs_oplock_break(struct work_struct
*work
)
2268 struct cifsFileInfo
*cfile
= container_of(work
, struct cifsFileInfo
,
2270 struct inode
*inode
= cfile
->dentry
->d_inode
;
2271 struct cifsInodeInfo
*cinode
= CIFS_I(inode
);
2274 if (inode
&& S_ISREG(inode
->i_mode
)) {
2275 if (cinode
->clientCanCacheRead
)
2276 break_lease(inode
, O_RDONLY
);
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
);
2287 cinode
->write_behind_rc
= rc
;
2288 cFYI(1, "Oplock flush inode %p rc %d", inode
, rc
);
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
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
);
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.
2308 write_lock(&GlobalSMBSeslock
);
2309 write_unlock(&GlobalSMBSeslock
);
2311 cifs_oplock_break_put(cfile
);
2314 void cifs_oplock_break_get(struct cifsFileInfo
*cfile
)
2316 cifs_sb_active(cfile
->dentry
->d_sb
);
2317 cifsFileInfo_get(cfile
);
2320 void cifs_oplock_break_put(struct cifsFileInfo
*cfile
)
2322 cifsFileInfo_put(cfile
);
2323 cifs_sb_deactive(cfile
->dentry
->d_sb
);
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, */
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.
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, */