@@ -322,9 +322,12 @@ cifs_new_fileinfo(struct cifs_fid *fid, struct file *file,
322322 cfile -> dentry = dget (dentry );
323323 cfile -> f_flags = file -> f_flags ;
324324 cfile -> invalidHandle = false;
325+ cfile -> oplock_break_received = false;
326+ cfile -> deferred_scheduled = false;
325327 cfile -> tlink = cifs_get_tlink (tlink );
326328 INIT_WORK (& cfile -> oplock_break , cifs_oplock_break );
327329 INIT_WORK (& cfile -> put , cifsFileInfo_put_work );
330+ INIT_DELAYED_WORK (& cfile -> deferred , smb2_deferred_work_close );
328331 mutex_init (& cfile -> fh_mutex );
329332 spin_lock_init (& cfile -> file_info_lock );
330333
@@ -565,6 +568,23 @@ int cifs_open(struct inode *inode, struct file *file)
565568 file -> f_op = & cifs_file_direct_ops ;
566569 }
567570
571+ spin_lock (& CIFS_I (inode )-> deferred_lock );
572+ /* Get the cached handle as SMB2 close is deferred */
573+ rc = cifs_get_readable_path (tcon , full_path , & cfile );
574+ if (rc == 0 ) {
575+ if (file -> f_flags == cfile -> f_flags ) {
576+ file -> private_data = cfile ;
577+ cifs_del_deferred_close (cfile );
578+ spin_unlock (& CIFS_I (inode )-> deferred_lock );
579+ goto out ;
580+ } else {
581+ spin_unlock (& CIFS_I (inode )-> deferred_lock );
582+ _cifsFileInfo_put (cfile , true, false);
583+ }
584+ } else {
585+ spin_unlock (& CIFS_I (inode )-> deferred_lock );
586+ }
587+
568588 if (server -> oplocks )
569589 oplock = REQ_OPLOCK ;
570590 else
@@ -846,11 +866,52 @@ cifs_reopen_file(struct cifsFileInfo *cfile, bool can_flush)
846866 return rc ;
847867}
848868
869+ void smb2_deferred_work_close (struct work_struct * work )
870+ {
871+ struct cifsFileInfo * cfile = container_of (work ,
872+ struct cifsFileInfo , deferred .work );
873+
874+ spin_lock (& CIFS_I (d_inode (cfile -> dentry ))-> deferred_lock );
875+ cifs_del_deferred_close (cfile );
876+ cfile -> deferred_scheduled = false;
877+ spin_unlock (& CIFS_I (d_inode (cfile -> dentry ))-> deferred_lock );
878+ _cifsFileInfo_put (cfile , true, false);
879+ }
880+
849881int cifs_close (struct inode * inode , struct file * file )
850882{
883+ struct cifsFileInfo * cfile ;
884+ struct cifsInodeInfo * cinode = CIFS_I (inode );
885+ struct cifs_sb_info * cifs_sb = CIFS_SB (inode -> i_sb );
886+ struct cifs_deferred_close * dclose ;
887+
851888 if (file -> private_data != NULL ) {
852- _cifsFileInfo_put ( file -> private_data , true, false) ;
889+ cfile = file -> private_data ;
853890 file -> private_data = NULL ;
891+ dclose = kmalloc (sizeof (struct cifs_deferred_close ), GFP_KERNEL );
892+ if ((cinode -> oplock == CIFS_CACHE_RHW_FLG ) &&
893+ dclose ) {
894+ if (test_bit (CIFS_INO_MODIFIED_ATTR , & cinode -> flags ))
895+ inode -> i_ctime = inode -> i_mtime = current_time (inode );
896+ spin_lock (& cinode -> deferred_lock );
897+ cifs_add_deferred_close (cfile , dclose );
898+ if (cfile -> deferred_scheduled ) {
899+ mod_delayed_work (deferredclose_wq ,
900+ & cfile -> deferred , cifs_sb -> ctx -> acregmax );
901+ } else {
902+ /* Deferred close for files */
903+ queue_delayed_work (deferredclose_wq ,
904+ & cfile -> deferred , cifs_sb -> ctx -> acregmax );
905+ cfile -> deferred_scheduled = true;
906+ spin_unlock (& cinode -> deferred_lock );
907+ return 0 ;
908+ }
909+ spin_unlock (& cinode -> deferred_lock );
910+ _cifsFileInfo_put (cfile , true, false);
911+ } else {
912+ _cifsFileInfo_put (cfile , true, false);
913+ kfree (dclose );
914+ }
854915 }
855916
856917 /* return code from the ->release op is always ignored */
@@ -1947,7 +2008,8 @@ struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode,
19472008 if (fsuid_only && !uid_eq (open_file -> uid , current_fsuid ()))
19482009 continue ;
19492010 if (OPEN_FMODE (open_file -> f_flags ) & FMODE_READ ) {
1950- if (!open_file -> invalidHandle ) {
2011+ if ((!open_file -> invalidHandle ) &&
2012+ (!open_file -> oplock_break_received )) {
19512013 /* found a good file */
19522014 /* lock it so it will not be closed on us */
19532015 cifsFileInfo_get (open_file );
@@ -2476,6 +2538,8 @@ static int cifs_writepages(struct address_space *mapping,
24762538 if (cfile )
24772539 cifsFileInfo_put (cfile );
24782540 free_xid (xid );
2541+ /* Indication to update ctime and mtime as close is deferred */
2542+ set_bit (CIFS_INO_MODIFIED_ATTR , & CIFS_I (inode )-> flags );
24792543 return rc ;
24802544}
24812545
@@ -2584,6 +2648,8 @@ static int cifs_write_end(struct file *file, struct address_space *mapping,
25842648
25852649 unlock_page (page );
25862650 put_page (page );
2651+ /* Indication to update ctime and mtime as close is deferred */
2652+ set_bit (CIFS_INO_MODIFIED_ATTR , & CIFS_I (inode )-> flags );
25872653
25882654 return rc ;
25892655}
@@ -4744,6 +4810,8 @@ void cifs_oplock_break(struct work_struct *work)
47444810 struct TCP_Server_Info * server = tcon -> ses -> server ;
47454811 int rc = 0 ;
47464812 bool purge_cache = false;
4813+ bool is_deferred = false;
4814+ struct cifs_deferred_close * dclose ;
47474815
47484816 wait_on_bit (& cinode -> flags , CIFS_INODE_PENDING_WRITERS ,
47494817 TASK_UNINTERRUPTIBLE );
@@ -4791,6 +4859,18 @@ void cifs_oplock_break(struct work_struct *work)
47914859 cifs_dbg (FYI , "Oplock release rc = %d\n" , rc );
47924860 }
47934861 _cifsFileInfo_put (cfile , false /* do not wait for ourself */ , false);
4862+ /*
4863+ * When oplock break is received and there are no active
4864+ * file handles but cached, then set the flag oplock_break_received.
4865+ * So, new open will not use cached handle.
4866+ */
4867+ spin_lock (& CIFS_I (inode )-> deferred_lock );
4868+ is_deferred = cifs_is_deferred_close (cfile , & dclose );
4869+ if (is_deferred ) {
4870+ cfile -> oplock_break_received = true;
4871+ mod_delayed_work (deferredclose_wq , & cfile -> deferred , 0 );
4872+ }
4873+ spin_unlock (& CIFS_I (inode )-> deferred_lock );
47944874 cifs_done_oplock_break (cinode );
47954875}
47964876
0 commit comments