@@ -43,7 +43,7 @@ int v9fs_file_open(struct inode *inode, struct file *file)
4343 int err ;
4444 struct v9fs_inode * v9inode ;
4545 struct v9fs_session_info * v9ses ;
46- struct p9_fid * fid , * writeback_fid ;
46+ struct p9_fid * fid ;
4747 int omode ;
4848
4949 p9_debug (P9_DEBUG_VFS , "inode: %p file: %p\n" , inode , file );
@@ -60,7 +60,19 @@ int v9fs_file_open(struct inode *inode, struct file *file)
6060 if (IS_ERR (fid ))
6161 return PTR_ERR (fid );
6262
63- err = p9_client_open (fid , omode );
63+ if ((v9ses -> cache >= CACHE_WRITEBACK ) && (omode & P9_OWRITE )) {
64+ int writeback_omode = (omode & ~P9_OWRITE ) | P9_ORDWR ;
65+
66+ p9_debug (P9_DEBUG_CACHE , "write-only file with writeback enabled, try opening O_RDWR\n" );
67+ err = p9_client_open (fid , writeback_omode );
68+ if (err < 0 ) {
69+ p9_debug (P9_DEBUG_CACHE , "could not open O_RDWR, disabling caches\n" );
70+ err = p9_client_open (fid , omode );
71+ fid -> mode |= P9L_DIRECT ;
72+ }
73+ } else {
74+ err = p9_client_open (fid , omode );
75+ }
6476 if (err < 0 ) {
6577 p9_fid_put (fid );
6678 return err ;
@@ -72,36 +84,14 @@ int v9fs_file_open(struct inode *inode, struct file *file)
7284 file -> private_data = fid ;
7385 }
7486
75- mutex_lock (& v9inode -> v_mutex );
76- if ((v9ses -> cache >= CACHE_WRITEBACK ) && !v9inode -> writeback_fid &&
77- ((file -> f_flags & O_ACCMODE ) != O_RDONLY )) {
78- /*
79- * clone a fid and add it to writeback_fid
80- * we do it during open time instead of
81- * page dirty time via write_begin/page_mkwrite
82- * because we want write after unlink usecase
83- * to work.
84- */
85- writeback_fid = v9fs_writeback_fid (file_dentry (file ));
86- if (IS_ERR (writeback_fid )) {
87- err = PTR_ERR (writeback_fid );
88- mutex_unlock (& v9inode -> v_mutex );
89- goto out_error ;
90- }
91- v9inode -> writeback_fid = (void * ) writeback_fid ;
92- }
93- mutex_unlock (& v9inode -> v_mutex );
9487#ifdef CONFIG_9P_FSCACHE
9588 if (v9ses -> cache == CACHE_FSCACHE )
9689 fscache_use_cookie (v9fs_inode_cookie (v9inode ),
9790 file -> f_mode & FMODE_WRITE );
9891#endif
92+ v9fs_fid_add_modes (fid , v9ses -> flags , v9ses -> cache , file -> f_flags );
9993 v9fs_open_fid_add (inode , & fid );
10094 return 0 ;
101- out_error :
102- p9_fid_put (file -> private_data );
103- file -> private_data = NULL ;
104- return err ;
10595}
10696
10797/**
@@ -367,14 +357,14 @@ v9fs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
367357{
368358 struct p9_fid * fid = iocb -> ki_filp -> private_data ;
369359 int ret , err = 0 ;
370- struct inode * inode = file_inode (iocb -> ki_filp );
371- struct v9fs_session_info * v9ses = v9fs_inode2v9ses (inode );
372360
373- p9_debug (P9_DEBUG_VFS , "count %zu offset %lld\n" ,
374- iov_iter_count (to ), iocb -> ki_pos );
361+ p9_debug (P9_DEBUG_VFS , "fid %d count %zu offset %lld\n" ,
362+ fid -> fid , iov_iter_count (to ), iocb -> ki_pos );
375363
376- if (v9ses -> cache > CACHE_MMAP )
364+ if (!(fid -> mode & P9L_DIRECT )) {
365+ p9_debug (P9_DEBUG_VFS , "(cached)\n" );
377366 return generic_file_read_iter (iocb , to );
367+ }
378368
379369 if (iocb -> ki_filp -> f_flags & O_NONBLOCK )
380370 ret = p9_client_read_once (fid , iocb -> ki_pos , to , & err );
@@ -397,14 +387,17 @@ static ssize_t
397387v9fs_file_write_iter (struct kiocb * iocb , struct iov_iter * from )
398388{
399389 struct file * file = iocb -> ki_filp ;
390+ struct p9_fid * fid = file -> private_data ;
400391 ssize_t retval ;
401392 loff_t origin ;
402393 int err = 0 ;
403- struct inode * inode = file_inode (iocb -> ki_filp );
404- struct v9fs_session_info * v9ses = v9fs_inode2v9ses (inode );
405394
406- if (v9ses -> cache >= CACHE_WRITEBACK )
395+ p9_debug (P9_DEBUG_VFS , "fid %d\n" , fid -> fid );
396+
397+ if (!(fid -> mode & (P9L_DIRECT | P9L_NOWRITECACHE ))) {
398+ p9_debug (P9_DEBUG_CACHE , "(cached)\n" );
407399 return generic_file_write_iter (iocb , from );
400+ }
408401
409402 retval = generic_write_checks (iocb , from );
410403 if (retval <= 0 )
@@ -488,36 +481,18 @@ v9fs_file_mmap(struct file *filp, struct vm_area_struct *vma)
488481{
489482 int retval ;
490483 struct inode * inode = file_inode (filp );
491- struct v9fs_inode * v9inode = V9FS_I (inode );
492484 struct v9fs_session_info * v9ses = v9fs_inode2v9ses (inode );
493- struct p9_fid * fid ;
485+
486+ p9_debug (P9_DEBUG_MMAP , "filp :%p\n" , filp );
494487
495488 if (v9ses -> cache < CACHE_MMAP ) {
489+ p9_debug (P9_DEBUG_CACHE , "(no mmap mode)" );
490+ if (vma -> vm_flags & VM_MAYSHARE )
491+ return - ENODEV ;
496492 invalidate_inode_pages2 (filp -> f_mapping );
497493 return generic_file_readonly_mmap (filp , vma );
498494 }
499495
500- mutex_lock (& v9inode -> v_mutex );
501- if (!v9inode -> writeback_fid &&
502- (vma -> vm_flags & VM_SHARED ) &&
503- (vma -> vm_flags & VM_WRITE )) {
504- /*
505- * clone a fid and add it to writeback_fid
506- * we do it during mmap instead of
507- * page dirty time via write_begin/page_mkwrite
508- * because we want write after unlink usecase
509- * to work.
510- */
511- fid = v9fs_writeback_fid (file_dentry (filp ));
512- if (IS_ERR (fid )) {
513- retval = PTR_ERR (fid );
514- mutex_unlock (& v9inode -> v_mutex );
515- return retval ;
516- }
517- v9inode -> writeback_fid = (void * ) fid ;
518- }
519- mutex_unlock (& v9inode -> v_mutex );
520-
521496 retval = generic_file_mmap (filp , vma );
522497 if (!retval )
523498 vma -> vm_ops = & v9fs_mmap_file_vm_ops ;
@@ -528,7 +503,6 @@ v9fs_file_mmap(struct file *filp, struct vm_area_struct *vma)
528503static vm_fault_t
529504v9fs_vm_page_mkwrite (struct vm_fault * vmf )
530505{
531- struct v9fs_inode * v9inode ;
532506 struct folio * folio = page_folio (vmf -> page );
533507 struct file * filp = vmf -> vma -> vm_file ;
534508 struct inode * inode = file_inode (filp );
@@ -537,8 +511,6 @@ v9fs_vm_page_mkwrite(struct vm_fault *vmf)
537511 p9_debug (P9_DEBUG_VFS , "folio %p fid %lx\n" ,
538512 folio , (unsigned long )filp -> private_data );
539513
540- v9inode = V9FS_I (inode );
541-
542514 /* Wait for the page to be written to the cache before we allow it to
543515 * be modified. We then assume the entire page will need writing back.
544516 */
@@ -551,7 +523,6 @@ v9fs_vm_page_mkwrite(struct vm_fault *vmf)
551523 /* Update file times before taking page lock */
552524 file_update_time (filp );
553525
554- BUG_ON (!v9inode -> writeback_fid );
555526 if (folio_lock_killable (folio ) < 0 )
556527 return VM_FAULT_RETRY ;
557528 if (folio_mapping (folio ) != inode -> i_mapping )
0 commit comments