4 * vfs operations that deal with files
6 * Copyright (C) International Business Machines Corp., 2002,2003
7 * Author(s): Steve French (sfrench@us.ibm.com)
9 * This library is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published
11 * by the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
17 * the GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <linux/stat.h>
25 #include <linux/fcntl.h>
26 #include <linux/pagemap.h>
27 #include <linux/pagevec.h>
28 #include <linux/smp_lock.h>
29 #include <asm/div64.h>
33 #include "cifsproto.h"
34 #include "cifs_unicode.h"
35 #include "cifs_debug.h"
36 #include "cifs_fs_sb.h"
38 static inline struct cifsFileInfo *cifs_init_private(
39 struct cifsFileInfo *private_data, struct inode *inode,
40 struct file *file, __u16 netfid)
42 memset(private_data, 0, sizeof(struct cifsFileInfo));
43 private_data->netfid = netfid;
44 private_data->pid = current->tgid;
45 init_MUTEX(&private_data->fh_sem);
46 private_data->pfile = file; /* needed for writepage */
47 private_data->pInode = inode;
48 private_data->invalidHandle = FALSE;
49 private_data->closePend = FALSE;
54 static inline int cifs_convert_flags(unsigned int flags)
56 if ((flags & O_ACCMODE) == O_RDONLY)
58 else if ((flags & O_ACCMODE) == O_WRONLY)
60 else if ((flags & O_ACCMODE) == O_RDWR) {
61 /* GENERIC_ALL is too much permission to request
62 can cause unnecessary access denied on create */
63 /* return GENERIC_ALL; */
64 return (GENERIC_READ | GENERIC_WRITE);
70 static inline int cifs_get_disposition(unsigned int flags)
72 if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
74 else if ((flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
75 return FILE_OVERWRITE_IF;
76 else if ((flags & O_CREAT) == O_CREAT)
82 /* all arguments to this function must be checked for validity in caller */
83 static inline int cifs_open_inode_helper(struct inode *inode, struct file *file,
84 struct cifsInodeInfo *pCifsInode, struct cifsFileInfo *pCifsFile,
85 struct cifsTconInfo *pTcon, int *oplock, FILE_ALL_INFO *buf,
86 char *full_path, int xid)
91 /* want handles we can use to read with first
92 in the list so we do not have to walk the
93 list to search for one in prepare_write */
94 if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
95 list_add_tail(&pCifsFile->flist,
96 &pCifsInode->openFileList);
98 list_add(&pCifsFile->flist,
99 &pCifsInode->openFileList);
101 write_unlock(&GlobalSMBSeslock);
102 write_unlock(&file->f_owner.lock);
103 if (pCifsInode->clientCanCacheRead) {
104 /* we have the inode open somewhere else
105 no need to discard cache data */
106 goto client_can_cache;
109 /* BB need same check in cifs_create too? */
110 /* if not oplocked, invalidate inode pages if mtime or file
112 temp = cifs_NTtimeToUnix(le64_to_cpu(buf->LastWriteTime));
113 if (timespec_equal(&file->f_dentry->d_inode->i_mtime, &temp) &&
114 (file->f_dentry->d_inode->i_size ==
115 (loff_t)le64_to_cpu(buf->EndOfFile))) {
116 cFYI(1, ("inode unchanged on server"));
118 if (file->f_dentry->d_inode->i_mapping) {
119 /* BB no need to lock inode until after invalidate
120 since namei code should already have it locked? */
121 filemap_fdatawrite(file->f_dentry->d_inode->i_mapping);
122 filemap_fdatawait(file->f_dentry->d_inode->i_mapping);
124 cFYI(1, ("invalidating remote inode since open detected it "
126 invalidate_remote_inode(file->f_dentry->d_inode);
130 if (pTcon->ses->capabilities & CAP_UNIX)
131 rc = cifs_get_inode_info_unix(&file->f_dentry->d_inode,
132 full_path, inode->i_sb, xid);
134 rc = cifs_get_inode_info(&file->f_dentry->d_inode,
135 full_path, buf, inode->i_sb, xid);
137 if ((*oplock & 0xF) == OPLOCK_EXCLUSIVE) {
138 pCifsInode->clientCanCacheAll = TRUE;
139 pCifsInode->clientCanCacheRead = TRUE;
140 cFYI(1, ("Exclusive Oplock granted on inode %p",
141 file->f_dentry->d_inode));
142 } else if ((*oplock & 0xF) == OPLOCK_READ)
143 pCifsInode->clientCanCacheRead = TRUE;
148 int cifs_open(struct inode *inode, struct file *file)
152 struct cifs_sb_info *cifs_sb;
153 struct cifsTconInfo *pTcon;
154 struct cifsFileInfo *pCifsFile;
155 struct cifsInodeInfo *pCifsInode;
156 struct list_head *tmp;
157 char *full_path = NULL;
161 FILE_ALL_INFO *buf = NULL;
165 cifs_sb = CIFS_SB(inode->i_sb);
166 pTcon = cifs_sb->tcon;
168 if (file->f_flags & O_CREAT) {
169 /* search inode for this file and fill in file->private_data */
170 pCifsInode = CIFS_I(file->f_dentry->d_inode);
171 read_lock(&GlobalSMBSeslock);
172 list_for_each(tmp, &pCifsInode->openFileList) {
173 pCifsFile = list_entry(tmp, struct cifsFileInfo,
175 if ((pCifsFile->pfile == NULL) &&
176 (pCifsFile->pid == current->tgid)) {
177 /* mode set in cifs_create */
179 /* needed for writepage */
180 pCifsFile->pfile = file;
182 file->private_data = pCifsFile;
186 read_unlock(&GlobalSMBSeslock);
187 if (file->private_data != NULL) {
192 if (file->f_flags & O_EXCL)
193 cERROR(1, ("could not find file instance for "
194 "new file %p ", file));
198 down(&inode->i_sb->s_vfs_rename_sem);
199 full_path = build_path_from_dentry(file->f_dentry);
200 up(&inode->i_sb->s_vfs_rename_sem);
201 if (full_path == NULL) {
206 cFYI(1, (" inode = 0x%p file flags are 0x%x for %s",
207 inode, file->f_flags, full_path));
208 desiredAccess = cifs_convert_flags(file->f_flags);
210 /*********************************************************************
211 * open flag mapping table:
213 * POSIX Flag CIFS Disposition
214 * ---------- ----------------
215 * O_CREAT FILE_OPEN_IF
216 * O_CREAT | O_EXCL FILE_CREATE
217 * O_CREAT | O_TRUNC FILE_OVERWRITE_IF
218 * O_TRUNC FILE_OVERWRITE
219 * none of the above FILE_OPEN
221 * Note that there is not a direct match between disposition
222 * FILE_SUPERSEDE (ie create whether or not file exists although
223 * O_CREAT | O_TRUNC is similar but truncates the existing
224 * file rather than creating a new file as FILE_SUPERSEDE does
225 * (which uses the attributes / metadata passed in on open call)
227 *? O_SYNC is a reasonable match to CIFS writethrough flag
228 *? and the read write flags match reasonably. O_LARGEFILE
229 *? is irrelevant because largefile support is always used
230 *? by this client. Flags O_APPEND, O_DIRECT, O_DIRECTORY,
231 * O_FASYNC, O_NOFOLLOW, O_NONBLOCK need further investigation
232 *********************************************************************/
234 disposition = cifs_get_disposition(file->f_flags);
241 /* BB pass O_SYNC flag through on file attributes .. BB */
243 /* Also refresh inode by passing in file_info buf returned by SMBOpen
244 and calling get_inode_info with returned buf (at least helps
245 non-Unix server case) */
247 /* BB we can not do this if this is the second open of a file
248 and the first handle has writebehind data, we might be
249 able to simply do a filemap_fdatawrite/filemap_fdatawait first */
250 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
255 rc = CIFSSMBOpen(xid, pTcon, full_path, disposition, desiredAccess,
256 CREATE_NOT_DIR, &netfid, &oplock, buf,
259 cFYI(1, ("cifs_open returned 0x%x ", rc));
263 kmalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
264 if (file->private_data == NULL) {
268 pCifsFile = cifs_init_private(file->private_data, inode, file, netfid);
269 write_lock(&file->f_owner.lock);
270 write_lock(&GlobalSMBSeslock);
271 list_add(&pCifsFile->tlist, &pTcon->openFileList);
273 pCifsInode = CIFS_I(file->f_dentry->d_inode);
275 rc = cifs_open_inode_helper(inode, file, pCifsInode,
277 &oplock, buf, full_path, xid);
279 write_unlock(&GlobalSMBSeslock);
280 write_unlock(&file->f_owner.lock);
283 if (oplock & CIFS_CREATE_ACTION) {
284 /* time to set mode which we can not set earlier due to
285 problems creating new read-only files */
286 if (cifs_sb->tcon->ses->capabilities & CAP_UNIX) {
287 CIFSSMBUnixSetPerms(xid, pTcon, full_path,
289 (__u64)-1, (__u64)-1, 0 /* dev */,
292 /* BB implement via Windows security descriptors eg
293 CIFSSMBWinSetPerms(xid, pTcon, full_path, mode,
295 in the meantime could set r/o dos attribute when
296 perms are eg: mode & 0222 == 0 */
307 /* Try to reaquire byte range locks that were released when session */
308 /* to server was lost */
309 static int cifs_relock_file(struct cifsFileInfo *cifsFile)
313 /* BB list all locks open on this file and relock */
318 static int cifs_reopen_file(struct inode *inode, struct file *file,
323 struct cifs_sb_info *cifs_sb;
324 struct cifsTconInfo *pTcon;
325 struct cifsFileInfo *pCifsFile;
326 struct cifsInodeInfo *pCifsInode;
327 char *full_path = NULL;
329 int disposition = FILE_OPEN;
334 if (file->private_data) {
335 pCifsFile = (struct cifsFileInfo *)file->private_data;
340 down(&pCifsFile->fh_sem);
341 if (pCifsFile->invalidHandle == FALSE) {
342 up(&pCifsFile->fh_sem);
347 if (file->f_dentry == NULL) {
348 up(&pCifsFile->fh_sem);
349 cFYI(1, ("failed file reopen, no valid name if dentry freed"));
353 cifs_sb = CIFS_SB(inode->i_sb);
354 pTcon = cifs_sb->tcon;
355 /* can not grab rename sem here because various ops, including
356 those that already have the rename sem can end up causing writepage
357 to get called and if the server was down that means we end up here,
358 and we can never tell if the caller already has the rename_sem */
359 full_path = build_path_from_dentry(file->f_dentry);
360 if (full_path == NULL) {
361 up(&pCifsFile->fh_sem);
366 cFYI(1, (" inode = 0x%p file flags are 0x%x for %s",
367 inode, file->f_flags,full_path));
368 desiredAccess = cifs_convert_flags(file->f_flags);
375 /* Can not refresh inode by passing in file_info buf to be returned
376 by SMBOpen and then calling get_inode_info with returned buf
377 since file might have write behind data that needs to be flushed
378 and server version of file size can be stale. If we knew for sure
379 that inode was not dirty locally we could do this */
381 /* buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
383 up(&pCifsFile->fh_sem);
388 rc = CIFSSMBOpen(xid, pTcon, full_path, disposition, desiredAccess,
389 CREATE_NOT_DIR, &netfid, &oplock, NULL,
392 up(&pCifsFile->fh_sem);
393 cFYI(1, ("cifs_open returned 0x%x ", rc));
394 cFYI(1, ("oplock: %d ", oplock));
396 pCifsFile->netfid = netfid;
397 pCifsFile->invalidHandle = FALSE;
398 up(&pCifsFile->fh_sem);
399 pCifsInode = CIFS_I(inode);
402 filemap_fdatawrite(inode->i_mapping);
403 filemap_fdatawait(inode->i_mapping);
404 /* temporarily disable caching while we
405 go to server to get inode info */
406 pCifsInode->clientCanCacheAll = FALSE;
407 pCifsInode->clientCanCacheRead = FALSE;
408 if (pTcon->ses->capabilities & CAP_UNIX)
409 rc = cifs_get_inode_info_unix(&inode,
410 full_path, inode->i_sb, xid);
412 rc = cifs_get_inode_info(&inode,
413 full_path, NULL, inode->i_sb,
415 } /* else we are writing out data to server already
416 and could deadlock if we tried to flush data, and
417 since we do not know if we have data that would
418 invalidate the current end of file on the server
419 we can not go to the server to get the new inod
421 if ((oplock & 0xF) == OPLOCK_EXCLUSIVE) {
422 pCifsInode->clientCanCacheAll = TRUE;
423 pCifsInode->clientCanCacheRead = TRUE;
424 cFYI(1, ("Exclusive Oplock granted on inode %p",
425 file->f_dentry->d_inode));
426 } else if ((oplock & 0xF) == OPLOCK_READ) {
427 pCifsInode->clientCanCacheRead = TRUE;
428 pCifsInode->clientCanCacheAll = FALSE;
430 pCifsInode->clientCanCacheRead = FALSE;
431 pCifsInode->clientCanCacheAll = FALSE;
433 cifs_relock_file(pCifsFile);
442 int cifs_close(struct inode *inode, struct file *file)
446 struct cifs_sb_info *cifs_sb;
447 struct cifsTconInfo *pTcon;
448 struct cifsFileInfo *pSMBFile =
449 (struct cifsFileInfo *)file->private_data;
453 cifs_sb = CIFS_SB(inode->i_sb);
454 pTcon = cifs_sb->tcon;
456 pSMBFile->closePend = TRUE;
457 write_lock(&file->f_owner.lock);
459 /* no sense reconnecting to close a file that is
461 if (pTcon->tidStatus != CifsNeedReconnect) {
462 write_unlock(&file->f_owner.lock);
463 rc = CIFSSMBClose(xid, pTcon,
465 write_lock(&file->f_owner.lock);
468 write_lock(&GlobalSMBSeslock);
469 list_del(&pSMBFile->flist);
470 list_del(&pSMBFile->tlist);
471 write_unlock(&GlobalSMBSeslock);
472 write_unlock(&file->f_owner.lock);
473 kfree(pSMBFile->search_resume_name);
474 kfree(file->private_data);
475 file->private_data = NULL;
479 if (list_empty(&(CIFS_I(inode)->openFileList))) {
480 cFYI(1, ("closing last open instance for inode %p", inode));
481 /* if the file is not open we do not know if we can cache info
482 on this inode, much less write behind and read ahead */
483 CIFS_I(inode)->clientCanCacheRead = FALSE;
484 CIFS_I(inode)->clientCanCacheAll = FALSE;
486 if ((rc ==0) && CIFS_I(inode)->write_behind_rc)
487 rc = CIFS_I(inode)->write_behind_rc;
492 int cifs_closedir(struct inode *inode, struct file *file)
496 struct cifsFileInfo *pCFileStruct =
497 (struct cifsFileInfo *)file->private_data;
500 cFYI(1, ("Closedir inode = 0x%p with ", inode));
505 struct cifsTconInfo *pTcon;
506 struct cifs_sb_info *cifs_sb = CIFS_SB(file->f_dentry->d_sb);
508 pTcon = cifs_sb->tcon;
510 cFYI(1, ("Freeing private data in close dir"));
511 if (pCFileStruct->srch_inf.endOfSearch == FALSE) {
512 pCFileStruct->invalidHandle = TRUE;
513 rc = CIFSFindClose(xid, pTcon, pCFileStruct->netfid);
514 cFYI(1, ("Closing uncompleted readdir with rc %d",
516 /* not much we can do if it fails anyway, ignore rc */
519 ptmp = pCFileStruct->srch_inf.ntwrk_buf_start;
521 /* BB removeme BB */ cFYI(1, ("freeing smb buf in srch struct in closedir"));
522 pCFileStruct->srch_inf.ntwrk_buf_start = NULL;
523 cifs_buf_release(ptmp);
525 ptmp = pCFileStruct->search_resume_name;
527 /* BB removeme BB */ cFYI(1, ("freeing resume name in closedir"));
528 pCFileStruct->search_resume_name = NULL;
531 kfree(file->private_data);
532 file->private_data = NULL;
534 /* BB can we lock the filestruct while this is going on? */
539 int cifs_lock(struct file *file, int cmd, struct file_lock *pfLock)
542 __u32 lockType = LOCKING_ANDX_LARGE_FILES;
546 int wait_flag = FALSE;
547 struct cifs_sb_info *cifs_sb;
548 struct cifsTconInfo *pTcon;
550 length = 1 + pfLock->fl_end - pfLock->fl_start;
554 cFYI(1, ("Lock parm: 0x%x flockflags: "
555 "0x%x flocktype: 0x%x start: %lld end: %lld",
556 cmd, pfLock->fl_flags, pfLock->fl_type, pfLock->fl_start,
559 if (pfLock->fl_flags & FL_POSIX)
561 if (pfLock->fl_flags & FL_FLOCK)
563 if (pfLock->fl_flags & FL_SLEEP) {
564 cFYI(1, ("Blocking lock "));
567 if (pfLock->fl_flags & FL_ACCESS)
568 cFYI(1, ("Process suspended by mandatory locking - "
569 "not implemented yet "));
570 if (pfLock->fl_flags & FL_LEASE)
571 cFYI(1, ("Lease on file - not implemented yet"));
572 if (pfLock->fl_flags &
573 (~(FL_POSIX | FL_FLOCK | FL_SLEEP | FL_ACCESS | FL_LEASE)))
574 cFYI(1, ("Unknown lock flags 0x%x", pfLock->fl_flags));
576 if (pfLock->fl_type == F_WRLCK) {
577 cFYI(1, ("F_WRLCK "));
579 } else if (pfLock->fl_type == F_UNLCK) {
580 cFYI(1, ("F_UNLCK "));
582 } else if (pfLock->fl_type == F_RDLCK) {
583 cFYI(1, ("F_RDLCK "));
584 lockType |= LOCKING_ANDX_SHARED_LOCK;
586 } else if (pfLock->fl_type == F_EXLCK) {
587 cFYI(1, ("F_EXLCK "));
589 } else if (pfLock->fl_type == F_SHLCK) {
590 cFYI(1, ("F_SHLCK "));
591 lockType |= LOCKING_ANDX_SHARED_LOCK;
594 cFYI(1, ("Unknown type of lock "));
596 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
597 pTcon = cifs_sb->tcon;
599 if (file->private_data == NULL) {
605 rc = CIFSSMBLock(xid, pTcon,
606 ((struct cifsFileInfo *)file->
607 private_data)->netfid,
609 pfLock->fl_start, 0, 1, lockType,
612 rc = CIFSSMBLock(xid, pTcon,
613 ((struct cifsFileInfo *) file->
614 private_data)->netfid,
616 pfLock->fl_start, 1 /* numUnlock */ ,
617 0 /* numLock */ , lockType,
619 pfLock->fl_type = F_UNLCK;
621 cERROR(1, ("Error unlocking previously locked "
622 "range %d during test of lock ",
627 /* if rc == ERR_SHARING_VIOLATION ? */
628 rc = 0; /* do not change lock type to unlock
629 since range in use */
636 rc = CIFSSMBLock(xid, pTcon,
637 ((struct cifsFileInfo *) file->private_data)->
639 pfLock->fl_start, numUnlock, numLock, lockType,
641 if (rc == 0 && (pfLock->fl_flags & FL_POSIX))
642 posix_lock_file_wait(file, pfLock);
647 ssize_t cifs_user_write(struct file *file, const char __user *write_data,
648 size_t write_size, loff_t *poffset)
651 unsigned int bytes_written = 0;
652 unsigned int total_written;
653 struct cifs_sb_info *cifs_sb;
654 struct cifsTconInfo *pTcon;
656 struct cifsFileInfo *open_file;
658 if (file->f_dentry == NULL)
661 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
665 pTcon = cifs_sb->tcon;
668 (" write %d bytes to offset %lld of %s", write_size,
669 *poffset, file->f_dentry->d_name.name)); */
671 if (file->private_data == NULL)
674 open_file = (struct cifsFileInfo *) file->private_data;
677 if (file->f_dentry->d_inode == NULL) {
682 if (*poffset > file->f_dentry->d_inode->i_size)
683 long_op = 2; /* writes past end of file can take a long time */
687 for (total_written = 0; write_size > total_written;
688 total_written += bytes_written) {
690 while (rc == -EAGAIN) {
691 if (file->private_data == NULL) {
692 /* file has been closed on us */
694 /* if we have gotten here we have written some data
695 and blocked, and the file has been freed on us while
696 we blocked so return what we managed to write */
697 return total_written;
699 if (open_file->closePend) {
702 return total_written;
706 if (open_file->invalidHandle) {
707 if ((file->f_dentry == NULL) ||
708 (file->f_dentry->d_inode == NULL)) {
710 return total_written;
712 /* we could deadlock if we called
713 filemap_fdatawait from here so tell
714 reopen_file not to flush data to server
716 rc = cifs_reopen_file(file->f_dentry->d_inode,
722 rc = CIFSSMBWrite(xid, pTcon,
724 min_t(const int, cifs_sb->wsize,
725 write_size - total_written),
726 *poffset, &bytes_written,
727 NULL, write_data + total_written, long_op);
729 if (rc || (bytes_written == 0)) {
737 *poffset += bytes_written;
738 long_op = FALSE; /* subsequent writes fast -
739 15 seconds is plenty */
742 #ifdef CONFIG_CIFS_STATS
743 if (total_written > 0) {
744 atomic_inc(&pTcon->num_writes);
745 spin_lock(&pTcon->stat_lock);
746 pTcon->bytes_written += total_written;
747 spin_unlock(&pTcon->stat_lock);
751 /* since the write may have blocked check these pointers again */
752 if (file->f_dentry) {
753 if (file->f_dentry->d_inode) {
754 struct inode *inode = file->f_dentry->d_inode;
755 inode->i_ctime = inode->i_mtime =
756 current_fs_time(inode->i_sb);
757 if (total_written > 0) {
758 if (*poffset > file->f_dentry->d_inode->i_size)
759 i_size_write(file->f_dentry->d_inode,
762 mark_inode_dirty_sync(file->f_dentry->d_inode);
766 return total_written;
769 static ssize_t cifs_write(struct file *file, const char *write_data,
770 size_t write_size, loff_t *poffset)
773 unsigned int bytes_written = 0;
774 unsigned int total_written;
775 struct cifs_sb_info *cifs_sb;
776 struct cifsTconInfo *pTcon;
778 struct cifsFileInfo *open_file;
780 if (file->f_dentry == NULL)
783 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
787 pTcon = cifs_sb->tcon;
790 (" write %d bytes to offset %lld of %s", write_size,
791 *poffset, file->f_dentry->d_name.name)); */
793 if (file->private_data == NULL)
796 open_file = (struct cifsFileInfo *)file->private_data;
799 if (file->f_dentry->d_inode == NULL) {
804 if (*poffset > file->f_dentry->d_inode->i_size)
805 long_op = 2; /* writes past end of file can take a long time */
809 for (total_written = 0; write_size > total_written;
810 total_written += bytes_written) {
812 while (rc == -EAGAIN) {
813 if (file->private_data == NULL) {
814 /* file has been closed on us */
816 /* if we have gotten here we have written some data
817 and blocked, and the file has been freed on us
818 while we blocked so return what we managed to
820 return total_written;
822 if (open_file->closePend) {
825 return total_written;
829 if (open_file->invalidHandle) {
830 if ((file->f_dentry == NULL) ||
831 (file->f_dentry->d_inode == NULL)) {
833 return total_written;
835 /* we could deadlock if we called
836 filemap_fdatawait from here so tell
837 reopen_file not to flush data to
839 rc = cifs_reopen_file(file->f_dentry->d_inode,
845 rc = CIFSSMBWrite(xid, pTcon,
847 min_t(const int, cifs_sb->wsize,
848 write_size - total_written),
849 *poffset, &bytes_written,
850 write_data + total_written, NULL, long_op);
852 if (rc || (bytes_written == 0)) {
860 *poffset += bytes_written;
861 long_op = FALSE; /* subsequent writes fast -
862 15 seconds is plenty */
865 #ifdef CONFIG_CIFS_STATS
866 if (total_written > 0) {
867 atomic_inc(&pTcon->num_writes);
868 spin_lock(&pTcon->stat_lock);
869 pTcon->bytes_written += total_written;
870 spin_unlock(&pTcon->stat_lock);
874 /* since the write may have blocked check these pointers again */
875 if (file->f_dentry) {
876 if (file->f_dentry->d_inode) {
877 file->f_dentry->d_inode->i_ctime =
878 file->f_dentry->d_inode->i_mtime = CURRENT_TIME;
879 if (total_written > 0) {
880 if (*poffset > file->f_dentry->d_inode->i_size)
881 i_size_write(file->f_dentry->d_inode,
884 mark_inode_dirty_sync(file->f_dentry->d_inode);
888 return total_written;
891 static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to)
893 struct address_space *mapping = page->mapping;
894 loff_t offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
897 int bytes_written = 0;
898 struct cifs_sb_info *cifs_sb;
899 struct cifsTconInfo *pTcon;
901 struct cifsInodeInfo *cifsInode;
902 struct cifsFileInfo *open_file = NULL;
903 struct list_head *tmp;
904 struct list_head *tmp1;
906 if (!mapping || !mapping->host)
909 inode = page->mapping->host;
910 cifs_sb = CIFS_SB(inode->i_sb);
911 pTcon = cifs_sb->tcon;
913 offset += (loff_t)from;
914 write_data = kmap(page);
917 if ((to > PAGE_CACHE_SIZE) || (from > to)) {
922 /* racing with truncate? */
923 if (offset > mapping->host->i_size) {
925 return 0; /* don't care */
928 /* check to make sure that we are not extending the file */
929 if (mapping->host->i_size - offset < (loff_t)to)
930 to = (unsigned)(mapping->host->i_size - offset);
932 cifsInode = CIFS_I(mapping->host);
933 read_lock(&GlobalSMBSeslock);
934 /* BB we should start at the end */
935 list_for_each_safe(tmp, tmp1, &cifsInode->openFileList) {
936 open_file = list_entry(tmp, struct cifsFileInfo, flist);
937 if (open_file->closePend)
939 /* We check if file is open for writing first */
940 if ((open_file->pfile) &&
941 ((open_file->pfile->f_flags & O_RDWR) ||
942 (open_file->pfile->f_flags & O_WRONLY))) {
943 read_unlock(&GlobalSMBSeslock);
944 bytes_written = cifs_write(open_file->pfile,
947 read_lock(&GlobalSMBSeslock);
948 /* Does mm or vfs already set times? */
950 inode->i_mtime = current_fs_time(inode->i_sb);
951 if ((bytes_written > 0) && (offset)) {
953 } else if (bytes_written < 0) {
955 /* have seen a case in which kernel seemed to
956 have closed/freed a file even with writes
957 active so we might as well see if there are
958 other file structs to try for the same
959 inode before giving up */
964 break; /* now that we found a valid file handle and
965 tried to write to it we are done, no sense
966 continuing to loop looking for another */
968 if (tmp->next == NULL) {
969 cFYI(1, ("File instance %p removed", tmp));
973 read_unlock(&GlobalSMBSeslock);
974 if (open_file == NULL) {
975 cFYI(1, ("No writeable filehandles for inode"));
984 static int cifs_writepages(struct address_space *mapping,
985 struct writeback_control *wbc)
992 /* Find contiguous pages then iterate through repeating
993 call 16K write then Setpageuptodate or if LARGE_WRITE_X
994 support then send larger writes via kevec so as to eliminate
1001 static int cifs_writepage(struct page* page, struct writeback_control *wbc)
1007 /* BB add check for wbc flags */
1008 page_cache_get(page);
1009 if (!PageUptodate(page)) {
1010 cFYI(1, ("ppw - page not up to date"));
1013 rc = cifs_partialpagewrite(page, 0, PAGE_CACHE_SIZE);
1014 SetPageUptodate(page); /* BB add check for error and Clearuptodate? */
1016 page_cache_release(page);
1021 static int cifs_commit_write(struct file *file, struct page *page,
1022 unsigned offset, unsigned to)
1026 struct inode *inode = page->mapping->host;
1027 loff_t position = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
1031 cFYI(1, ("commit write for page %p up to position %lld for %d",
1032 page, position, to));
1033 if (position > inode->i_size) {
1034 i_size_write(inode, position);
1035 /* if (file->private_data == NULL) {
1038 open_file = (struct cifsFileInfo *)file->private_data;
1039 cifs_sb = CIFS_SB(inode->i_sb);
1041 while (rc == -EAGAIN) {
1042 if ((open_file->invalidHandle) &&
1043 (!open_file->closePend)) {
1044 rc = cifs_reopen_file(
1045 file->f_dentry->d_inode, file);
1049 if (!open_file->closePend) {
1050 rc = CIFSSMBSetFileSize(xid,
1051 cifs_sb->tcon, position,
1053 open_file->pid, FALSE);
1059 cFYI(1, (" SetEOF (commit write) rc = %d", rc));
1062 if (!PageUptodate(page)) {
1063 position = ((loff_t)page->index << PAGE_CACHE_SHIFT) + offset;
1064 /* can not rely on (or let) writepage write this data */
1066 cFYI(1, ("Illegal offsets, can not copy from %d to %d",
1071 /* this is probably better than directly calling
1072 partialpage_write since in this function the file handle is
1073 known which we might as well leverage */
1074 /* BB check if anything else missing out of ppw
1075 such as updating last write time */
1076 page_data = kmap(page);
1077 rc = cifs_write(file, page_data + offset, to-offset,
1081 /* else if (rc < 0) should we set writebehind rc? */
1084 set_page_dirty(page);
1091 int cifs_fsync(struct file *file, struct dentry *dentry, int datasync)
1095 struct inode *inode = file->f_dentry->d_inode;
1099 cFYI(1, ("Sync file - name: %s datasync: 0x%x ",
1100 dentry->d_name.name, datasync));
1102 rc = filemap_fdatawrite(inode->i_mapping);
1104 CIFS_I(inode)->write_behind_rc = 0;
1109 /* static int cifs_sync_page(struct page *page)
1111 struct address_space *mapping;
1112 struct inode *inode;
1113 unsigned long index = page->index;
1114 unsigned int rpages = 0;
1117 cFYI(1, ("sync page %p",page));
1118 mapping = page->mapping;
1121 inode = mapping->host;
1125 /* fill in rpages then
1126 result = cifs_pagein_inode(inode, index, rpages); */ /* BB finish */
1128 /* cFYI(1, ("rpages is %d for sync page of Index %ld ", rpages, index));
1136 * As file closes, flush all cached write data for this inode checking
1137 * for write behind errors.
1139 int cifs_flush(struct file *file)
1141 struct inode * inode = file->f_dentry->d_inode;
1144 /* Rather than do the steps manually:
1145 lock the inode for writing
1146 loop through pages looking for write behind data (dirty pages)
1147 coalesce into contiguous 16K (or smaller) chunks to write to server
1148 send to server (prefer in parallel)
1149 deal with writebehind errors
1150 unlock inode for writing
1151 filemapfdatawrite appears easier for the time being */
1153 rc = filemap_fdatawrite(inode->i_mapping);
1154 if (!rc) /* reset wb rc if we were able to write out dirty pages */
1155 CIFS_I(inode)->write_behind_rc = 0;
1157 cFYI(1, ("Flush inode %p file %p rc %d",inode,file,rc));
1162 ssize_t cifs_user_read(struct file *file, char __user *read_data,
1163 size_t read_size, loff_t *poffset)
1166 unsigned int bytes_read = 0;
1167 unsigned int total_read = 0;
1168 unsigned int current_read_size;
1169 struct cifs_sb_info *cifs_sb;
1170 struct cifsTconInfo *pTcon;
1172 struct cifsFileInfo *open_file;
1173 char *smb_read_data;
1174 char __user *current_offset;
1175 struct smb_com_read_rsp *pSMBr;
1178 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
1179 pTcon = cifs_sb->tcon;
1181 if (file->private_data == NULL) {
1185 open_file = (struct cifsFileInfo *)file->private_data;
1187 if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
1188 cFYI(1, ("attempting read on write only file instance"));
1190 for (total_read = 0, current_offset = read_data;
1191 read_size > total_read;
1192 total_read += bytes_read, current_offset += bytes_read) {
1193 current_read_size = min_t(const int, read_size - total_read,
1196 smb_read_data = NULL;
1197 while (rc == -EAGAIN) {
1198 if ((open_file->invalidHandle) &&
1199 (!open_file->closePend)) {
1200 rc = cifs_reopen_file(file->f_dentry->d_inode,
1206 rc = CIFSSMBRead(xid, pTcon,
1208 current_read_size, *poffset,
1209 &bytes_read, &smb_read_data);
1211 pSMBr = (struct smb_com_read_rsp *)smb_read_data;
1212 if (copy_to_user(current_offset,
1213 smb_read_data + 4 /* RFC1001 hdr */
1214 + le16_to_cpu(pSMBr->DataOffset),
1220 if (smb_read_data) {
1221 cifs_buf_release(smb_read_data);
1222 smb_read_data = NULL;
1225 if (rc || (bytes_read == 0)) {
1233 #ifdef CONFIG_CIFS_STATS
1234 atomic_inc(&pTcon->num_reads);
1235 spin_lock(&pTcon->stat_lock);
1236 pTcon->bytes_read += total_read;
1237 spin_unlock(&pTcon->stat_lock);
1239 *poffset += bytes_read;
1247 static ssize_t cifs_read(struct file *file, char *read_data, size_t read_size,
1251 unsigned int bytes_read = 0;
1252 unsigned int total_read;
1253 unsigned int current_read_size;
1254 struct cifs_sb_info *cifs_sb;
1255 struct cifsTconInfo *pTcon;
1257 char *current_offset;
1258 struct cifsFileInfo *open_file;
1261 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
1262 pTcon = cifs_sb->tcon;
1264 if (file->private_data == NULL) {
1268 open_file = (struct cifsFileInfo *)file->private_data;
1270 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
1271 cFYI(1, ("attempting read on write only file instance"));
1273 for (total_read = 0, current_offset = read_data;
1274 read_size > total_read;
1275 total_read += bytes_read, current_offset += bytes_read) {
1276 current_read_size = min_t(const int, read_size - total_read,
1279 while (rc == -EAGAIN) {
1280 if ((open_file->invalidHandle) &&
1281 (!open_file->closePend)) {
1282 rc = cifs_reopen_file(file->f_dentry->d_inode,
1288 rc = CIFSSMBRead(xid, pTcon,
1290 current_read_size, *poffset,
1291 &bytes_read, ¤t_offset);
1293 if (rc || (bytes_read == 0)) {
1301 #ifdef CONFIG_CIFS_STATS
1302 atomic_inc(&pTcon->num_reads);
1303 spin_lock(&pTcon->stat_lock);
1304 pTcon->bytes_read += total_read;
1305 spin_unlock(&pTcon->stat_lock);
1307 *poffset += bytes_read;
1314 int cifs_file_mmap(struct file *file, struct vm_area_struct *vma)
1316 struct dentry *dentry = file->f_dentry;
1320 rc = cifs_revalidate(dentry);
1322 cFYI(1, ("Validation prior to mmap failed, error=%d", rc));
1326 rc = generic_file_mmap(file, vma);
1332 static void cifs_copy_cache_pages(struct address_space *mapping,
1333 struct list_head *pages, int bytes_read, char *data,
1334 struct pagevec *plru_pvec)
1339 while (bytes_read > 0) {
1340 if (list_empty(pages))
1343 page = list_entry(pages->prev, struct page, lru);
1344 list_del(&page->lru);
1346 if (add_to_page_cache(page, mapping, page->index,
1348 page_cache_release(page);
1349 cFYI(1, ("Add page cache failed"));
1353 target = kmap_atomic(page,KM_USER0);
1355 if (PAGE_CACHE_SIZE > bytes_read) {
1356 memcpy(target, data, bytes_read);
1357 /* zero the tail end of this partial page */
1358 memset(target + bytes_read, 0,
1359 PAGE_CACHE_SIZE - bytes_read);
1362 memcpy(target, data, PAGE_CACHE_SIZE);
1363 bytes_read -= PAGE_CACHE_SIZE;
1365 kunmap_atomic(target, KM_USER0);
1367 flush_dcache_page(page);
1368 SetPageUptodate(page);
1370 if (!pagevec_add(plru_pvec, page))
1371 __pagevec_lru_add(plru_pvec);
1372 data += PAGE_CACHE_SIZE;
1377 static int cifs_readpages(struct file *file, struct address_space *mapping,
1378 struct list_head *page_list, unsigned num_pages)
1384 struct cifs_sb_info *cifs_sb;
1385 struct cifsTconInfo *pTcon;
1387 unsigned int read_size,i;
1388 char *smb_read_data = NULL;
1389 struct smb_com_read_rsp *pSMBr;
1390 struct pagevec lru_pvec;
1391 struct cifsFileInfo *open_file;
1394 if (file->private_data == NULL) {
1398 open_file = (struct cifsFileInfo *)file->private_data;
1399 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
1400 pTcon = cifs_sb->tcon;
1402 pagevec_init(&lru_pvec, 0);
1404 for (i = 0; i < num_pages; ) {
1405 unsigned contig_pages;
1406 struct page *tmp_page;
1407 unsigned long expected_index;
1409 if (list_empty(page_list))
1412 page = list_entry(page_list->prev, struct page, lru);
1413 offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
1415 /* count adjacent pages that we will read into */
1418 list_entry(page_list->prev, struct page, lru)->index;
1419 list_for_each_entry_reverse(tmp_page,page_list,lru) {
1420 if (tmp_page->index == expected_index) {
1426 if (contig_pages + i > num_pages)
1427 contig_pages = num_pages - i;
1429 /* for reads over a certain size could initiate async
1432 read_size = contig_pages * PAGE_CACHE_SIZE;
1433 /* Read size needs to be in multiples of one page */
1434 read_size = min_t(const unsigned int, read_size,
1435 cifs_sb->rsize & PAGE_CACHE_MASK);
1438 while (rc == -EAGAIN) {
1439 if ((open_file->invalidHandle) &&
1440 (!open_file->closePend)) {
1441 rc = cifs_reopen_file(file->f_dentry->d_inode,
1447 rc = CIFSSMBRead(xid, pTcon,
1450 &bytes_read, &smb_read_data);
1451 /* BB need to check return code here */
1453 if (smb_read_data) {
1454 cifs_buf_release(smb_read_data);
1455 smb_read_data = NULL;
1459 if ((rc < 0) || (smb_read_data == NULL)) {
1460 cFYI(1, ("Read error in readpages: %d", rc));
1461 /* clean up remaing pages off list */
1462 while (!list_empty(page_list) && (i < num_pages)) {
1463 page = list_entry(page_list->prev, struct page,
1465 list_del(&page->lru);
1466 page_cache_release(page);
1469 } else if (bytes_read > 0) {
1470 pSMBr = (struct smb_com_read_rsp *)smb_read_data;
1471 cifs_copy_cache_pages(mapping, page_list, bytes_read,
1472 smb_read_data + 4 /* RFC1001 hdr */ +
1473 le16_to_cpu(pSMBr->DataOffset), &lru_pvec);
1475 i += bytes_read >> PAGE_CACHE_SHIFT;
1476 #ifdef CONFIG_CIFS_STATS
1477 atomic_inc(&pTcon->num_reads);
1478 spin_lock(&pTcon->stat_lock);
1479 pTcon->bytes_read += bytes_read;
1480 spin_unlock(&pTcon->stat_lock);
1482 if ((int)(bytes_read & PAGE_CACHE_MASK) != bytes_read) {
1483 i++; /* account for partial page */
1485 /* server copy of file can have smaller size
1487 /* BB do we need to verify this common case ?
1488 this case is ok - if we are at server EOF
1489 we will hit it on next read */
1491 /* while (!list_empty(page_list) && (i < num_pages)) {
1492 page = list_entry(page_list->prev,
1494 list_del(&page->list);
1495 page_cache_release(page);
1500 cFYI(1, ("No bytes read (%d) at offset %lld . "
1501 "Cleaning remaining pages from readahead list",
1502 bytes_read, offset));
1503 /* BB turn off caching and do new lookup on
1504 file size at server? */
1505 while (!list_empty(page_list) && (i < num_pages)) {
1506 page = list_entry(page_list->prev, struct page,
1508 list_del(&page->lru);
1510 /* BB removeme - replace with zero of page? */
1511 page_cache_release(page);
1515 if (smb_read_data) {
1516 cifs_buf_release(smb_read_data);
1517 smb_read_data = NULL;
1522 pagevec_lru_add(&lru_pvec);
1524 /* need to free smb_read_data buf before exit */
1525 if (smb_read_data) {
1526 cifs_buf_release(smb_read_data);
1527 smb_read_data = NULL;
1534 static int cifs_readpage_worker(struct file *file, struct page *page,
1540 page_cache_get(page);
1541 read_data = kmap(page);
1542 /* for reads over a certain size could initiate async read ahead */
1544 rc = cifs_read(file, read_data, PAGE_CACHE_SIZE, poffset);
1549 cFYI(1, ("Bytes read %d ",rc));
1551 file->f_dentry->d_inode->i_atime =
1552 current_fs_time(file->f_dentry->d_inode->i_sb);
1554 if (PAGE_CACHE_SIZE > rc)
1555 memset(read_data + rc, 0, PAGE_CACHE_SIZE - rc);
1557 flush_dcache_page(page);
1558 SetPageUptodate(page);
1563 page_cache_release(page);
1567 static int cifs_readpage(struct file *file, struct page *page)
1569 loff_t offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
1575 if (file->private_data == NULL) {
1580 cFYI(1, ("readpage %p at offset %d 0x%x\n",
1581 page, (int)offset, (int)offset));
1583 rc = cifs_readpage_worker(file, page, &offset);
1591 /* We do not want to update the file size from server for inodes
1592 open for write - to avoid races with writepage extending
1593 the file - in the future we could consider allowing
1594 refreshing the inode only on increases in the file size
1595 but this is tricky to do without racing with writebehind
1596 page caching in the current Linux kernel design */
1597 int is_size_safe_to_change(struct cifsInodeInfo *cifsInode)
1599 struct list_head *tmp;
1600 struct list_head *tmp1;
1601 struct cifsFileInfo *open_file = NULL;
1604 if (cifsInode == NULL)
1607 read_lock(&GlobalSMBSeslock);
1608 list_for_each_safe(tmp, tmp1, &cifsInode->openFileList) {
1609 open_file = list_entry(tmp, struct cifsFileInfo, flist);
1610 if (open_file == NULL)
1612 if (open_file->closePend)
1614 /* We check if file is open for writing,
1615 BB we could supplement this with a check to see if file size
1616 changes have been flushed to server - ie inode metadata dirty */
1617 if ((open_file->pfile) &&
1618 ((open_file->pfile->f_flags & O_RDWR) ||
1619 (open_file->pfile->f_flags & O_WRONLY))) {
1623 if (tmp->next == NULL) {
1624 cFYI(1, ("File instance %p removed", tmp));
1628 read_unlock(&GlobalSMBSeslock);
1633 static int cifs_prepare_write(struct file *file, struct page *page,
1634 unsigned from, unsigned to)
1637 loff_t offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
1638 cFYI(1, ("prepare write for page %p from %d to %d",page,from,to));
1639 if (!PageUptodate(page)) {
1640 /* if (to - from != PAGE_CACHE_SIZE) {
1641 void *kaddr = kmap_atomic(page, KM_USER0);
1642 memset(kaddr, 0, from);
1643 memset(kaddr + to, 0, PAGE_CACHE_SIZE - to);
1644 flush_dcache_page(page);
1645 kunmap_atomic(kaddr, KM_USER0);
1647 /* If we are writing a full page it will be up to date,
1648 no need to read from the server */
1649 if ((to == PAGE_CACHE_SIZE) && (from == 0))
1650 SetPageUptodate(page);
1652 /* might as well read a page, it is fast enough */
1653 if ((file->f_flags & O_ACCMODE) != O_WRONLY) {
1654 rc = cifs_readpage_worker(file, page, &offset);
1656 /* should we try using another file handle if there is one -
1657 how would we lock it to prevent close of that handle
1658 racing with this read?
1659 In any case this will be written out by commit_write */
1663 /* BB should we pass any errors back?
1664 e.g. if we do not have read access to the file */
1668 struct address_space_operations cifs_addr_ops = {
1669 .readpage = cifs_readpage,
1670 .readpages = cifs_readpages,
1671 .writepage = cifs_writepage,
1672 .prepare_write = cifs_prepare_write,
1673 .commit_write = cifs_commit_write,
1674 .set_page_dirty = __set_page_dirty_nobuffers,
1675 /* .sync_page = cifs_sync_page, */