file.c 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656
  1. /*
  2. * fs/cifs/file.c
  3. *
  4. * vfs operations that deal with files
  5. *
  6. * Copyright (C) International Business Machines Corp., 2002,2010
  7. * Author(s): Steve French (sfrench@us.ibm.com)
  8. * Jeremy Allison (jra@samba.org)
  9. *
  10. * This library is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as published
  12. * by the Free Software Foundation; either version 2.1 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  18. * the GNU Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. #include <linux/fs.h>
  25. #include <linux/backing-dev.h>
  26. #include <linux/stat.h>
  27. #include <linux/fcntl.h>
  28. #include <linux/pagemap.h>
  29. #include <linux/pagevec.h>
  30. #include <linux/writeback.h>
  31. #include <linux/task_io_accounting_ops.h>
  32. #include <linux/delay.h>
  33. #include <linux/mount.h>
  34. #include <linux/slab.h>
  35. #include <linux/swap.h>
  36. #include <asm/div64.h>
  37. #include "cifsfs.h"
  38. #include "cifspdu.h"
  39. #include "cifsglob.h"
  40. #include "cifsproto.h"
  41. #include "cifs_unicode.h"
  42. #include "cifs_debug.h"
  43. #include "cifs_fs_sb.h"
  44. #include "fscache.h"
  45. static inline int cifs_convert_flags(unsigned int flags)
  46. {
  47. if ((flags & O_ACCMODE) == O_RDONLY)
  48. return GENERIC_READ;
  49. else if ((flags & O_ACCMODE) == O_WRONLY)
  50. return GENERIC_WRITE;
  51. else if ((flags & O_ACCMODE) == O_RDWR) {
  52. /* GENERIC_ALL is too much permission to request
  53. can cause unnecessary access denied on create */
  54. /* return GENERIC_ALL; */
  55. return (GENERIC_READ | GENERIC_WRITE);
  56. }
  57. return (READ_CONTROL | FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES |
  58. FILE_WRITE_EA | FILE_APPEND_DATA | FILE_WRITE_DATA |
  59. FILE_READ_DATA);
  60. }
  61. static u32 cifs_posix_convert_flags(unsigned int flags)
  62. {
  63. u32 posix_flags = 0;
  64. if ((flags & O_ACCMODE) == O_RDONLY)
  65. posix_flags = SMB_O_RDONLY;
  66. else if ((flags & O_ACCMODE) == O_WRONLY)
  67. posix_flags = SMB_O_WRONLY;
  68. else if ((flags & O_ACCMODE) == O_RDWR)
  69. posix_flags = SMB_O_RDWR;
  70. if (flags & O_CREAT) {
  71. posix_flags |= SMB_O_CREAT;
  72. if (flags & O_EXCL)
  73. posix_flags |= SMB_O_EXCL;
  74. } else if (flags & O_EXCL)
  75. cFYI(1, "Application %s pid %d has incorrectly set O_EXCL flag"
  76. "but not O_CREAT on file open. Ignoring O_EXCL",
  77. current->comm, current->tgid);
  78. if (flags & O_TRUNC)
  79. posix_flags |= SMB_O_TRUNC;
  80. /* be safe and imply O_SYNC for O_DSYNC */
  81. if (flags & O_DSYNC)
  82. posix_flags |= SMB_O_SYNC;
  83. if (flags & O_DIRECTORY)
  84. posix_flags |= SMB_O_DIRECTORY;
  85. if (flags & O_NOFOLLOW)
  86. posix_flags |= SMB_O_NOFOLLOW;
  87. if (flags & O_DIRECT)
  88. posix_flags |= SMB_O_DIRECT;
  89. return posix_flags;
  90. }
  91. static inline int cifs_get_disposition(unsigned int flags)
  92. {
  93. if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
  94. return FILE_CREATE;
  95. else if ((flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
  96. return FILE_OVERWRITE_IF;
  97. else if ((flags & O_CREAT) == O_CREAT)
  98. return FILE_OPEN_IF;
  99. else if ((flags & O_TRUNC) == O_TRUNC)
  100. return FILE_OVERWRITE;
  101. else
  102. return FILE_OPEN;
  103. }
  104. int cifs_posix_open(char *full_path, struct inode **pinode,
  105. struct super_block *sb, int mode, unsigned int f_flags,
  106. __u32 *poplock, __u16 *pnetfid, unsigned int xid)
  107. {
  108. int rc;
  109. FILE_UNIX_BASIC_INFO *presp_data;
  110. __u32 posix_flags = 0;
  111. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  112. struct cifs_fattr fattr;
  113. struct tcon_link *tlink;
  114. struct cifs_tcon *tcon;
  115. cFYI(1, "posix open %s", full_path);
  116. presp_data = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
  117. if (presp_data == NULL)
  118. return -ENOMEM;
  119. tlink = cifs_sb_tlink(cifs_sb);
  120. if (IS_ERR(tlink)) {
  121. rc = PTR_ERR(tlink);
  122. goto posix_open_ret;
  123. }
  124. tcon = tlink_tcon(tlink);
  125. mode &= ~current_umask();
  126. posix_flags = cifs_posix_convert_flags(f_flags);
  127. rc = CIFSPOSIXCreate(xid, tcon, posix_flags, mode, pnetfid, presp_data,
  128. poplock, full_path, cifs_sb->local_nls,
  129. cifs_sb->mnt_cifs_flags &
  130. CIFS_MOUNT_MAP_SPECIAL_CHR);
  131. cifs_put_tlink(tlink);
  132. if (rc)
  133. goto posix_open_ret;
  134. if (presp_data->Type == cpu_to_le32(-1))
  135. goto posix_open_ret; /* open ok, caller does qpathinfo */
  136. if (!pinode)
  137. goto posix_open_ret; /* caller does not need info */
  138. cifs_unix_basic_to_fattr(&fattr, presp_data, cifs_sb);
  139. /* get new inode and set it up */
  140. if (*pinode == NULL) {
  141. cifs_fill_uniqueid(sb, &fattr);
  142. *pinode = cifs_iget(sb, &fattr);
  143. if (!*pinode) {
  144. rc = -ENOMEM;
  145. goto posix_open_ret;
  146. }
  147. } else {
  148. cifs_fattr_to_inode(*pinode, &fattr);
  149. }
  150. posix_open_ret:
  151. kfree(presp_data);
  152. return rc;
  153. }
  154. static int
  155. cifs_nt_open(char *full_path, struct inode *inode, struct cifs_sb_info *cifs_sb,
  156. struct cifs_tcon *tcon, unsigned int f_flags, __u32 *oplock,
  157. struct cifs_fid *fid, unsigned int xid)
  158. {
  159. int rc;
  160. int desired_access;
  161. int disposition;
  162. int create_options = CREATE_NOT_DIR;
  163. FILE_ALL_INFO *buf;
  164. struct TCP_Server_Info *server = tcon->ses->server;
  165. if (!server->ops->open)
  166. return -ENOSYS;
  167. desired_access = cifs_convert_flags(f_flags);
  168. /*********************************************************************
  169. * open flag mapping table:
  170. *
  171. * POSIX Flag CIFS Disposition
  172. * ---------- ----------------
  173. * O_CREAT FILE_OPEN_IF
  174. * O_CREAT | O_EXCL FILE_CREATE
  175. * O_CREAT | O_TRUNC FILE_OVERWRITE_IF
  176. * O_TRUNC FILE_OVERWRITE
  177. * none of the above FILE_OPEN
  178. *
  179. * Note that there is not a direct match between disposition
  180. * FILE_SUPERSEDE (ie create whether or not file exists although
  181. * O_CREAT | O_TRUNC is similar but truncates the existing
  182. * file rather than creating a new file as FILE_SUPERSEDE does
  183. * (which uses the attributes / metadata passed in on open call)
  184. *?
  185. *? O_SYNC is a reasonable match to CIFS writethrough flag
  186. *? and the read write flags match reasonably. O_LARGEFILE
  187. *? is irrelevant because largefile support is always used
  188. *? by this client. Flags O_APPEND, O_DIRECT, O_DIRECTORY,
  189. * O_FASYNC, O_NOFOLLOW, O_NONBLOCK need further investigation
  190. *********************************************************************/
  191. disposition = cifs_get_disposition(f_flags);
  192. /* BB pass O_SYNC flag through on file attributes .. BB */
  193. buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  194. if (!buf)
  195. return -ENOMEM;
  196. if (backup_cred(cifs_sb))
  197. create_options |= CREATE_OPEN_BACKUP_INTENT;
  198. rc = server->ops->open(xid, tcon, full_path, disposition,
  199. desired_access, create_options, fid, oplock, buf,
  200. cifs_sb);
  201. if (rc)
  202. goto out;
  203. if (tcon->unix_ext)
  204. rc = cifs_get_inode_info_unix(&inode, full_path, inode->i_sb,
  205. xid);
  206. else
  207. rc = cifs_get_inode_info(&inode, full_path, buf, inode->i_sb,
  208. xid, &fid->netfid);
  209. out:
  210. kfree(buf);
  211. return rc;
  212. }
  213. static bool
  214. cifs_has_mand_locks(struct cifsInodeInfo *cinode)
  215. {
  216. struct cifs_fid_locks *cur;
  217. bool has_locks = false;
  218. down_read(&cinode->lock_sem);
  219. list_for_each_entry(cur, &cinode->llist, llist) {
  220. if (!list_empty(&cur->locks)) {
  221. has_locks = true;
  222. break;
  223. }
  224. }
  225. up_read(&cinode->lock_sem);
  226. return has_locks;
  227. }
  228. struct cifsFileInfo *
  229. cifs_new_fileinfo(struct cifs_fid *fid, struct file *file,
  230. struct tcon_link *tlink, __u32 oplock)
  231. {
  232. struct dentry *dentry = file->f_path.dentry;
  233. struct inode *inode = dentry->d_inode;
  234. struct cifsInodeInfo *cinode = CIFS_I(inode);
  235. struct cifsFileInfo *cfile;
  236. struct cifs_fid_locks *fdlocks;
  237. struct cifs_tcon *tcon = tlink_tcon(tlink);
  238. struct TCP_Server_Info *server = tcon->ses->server;
  239. cfile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
  240. if (cfile == NULL)
  241. return cfile;
  242. fdlocks = kzalloc(sizeof(struct cifs_fid_locks), GFP_KERNEL);
  243. if (!fdlocks) {
  244. kfree(cfile);
  245. return NULL;
  246. }
  247. INIT_LIST_HEAD(&fdlocks->locks);
  248. fdlocks->cfile = cfile;
  249. cfile->llist = fdlocks;
  250. down_write(&cinode->lock_sem);
  251. list_add(&fdlocks->llist, &cinode->llist);
  252. up_write(&cinode->lock_sem);
  253. cfile->count = 1;
  254. cfile->pid = current->tgid;
  255. cfile->uid = current_fsuid();
  256. cfile->dentry = dget(dentry);
  257. cfile->f_flags = file->f_flags;
  258. cfile->invalidHandle = false;
  259. cfile->tlink = cifs_get_tlink(tlink);
  260. INIT_WORK(&cfile->oplock_break, cifs_oplock_break);
  261. mutex_init(&cfile->fh_mutex);
  262. /*
  263. * If the server returned a read oplock and we have mandatory brlocks,
  264. * set oplock level to None.
  265. */
  266. if (oplock == server->vals->oplock_read &&
  267. cifs_has_mand_locks(cinode)) {
  268. cFYI(1, "Reset oplock val from read to None due to mand locks");
  269. oplock = 0;
  270. }
  271. spin_lock(&cifs_file_list_lock);
  272. if (fid->pending_open->oplock != CIFS_OPLOCK_NO_CHANGE && oplock)
  273. oplock = fid->pending_open->oplock;
  274. list_del(&fid->pending_open->olist);
  275. server->ops->set_fid(cfile, fid, oplock);
  276. list_add(&cfile->tlist, &tcon->openFileList);
  277. /* if readable file instance put first in list*/
  278. if (file->f_mode & FMODE_READ)
  279. list_add(&cfile->flist, &cinode->openFileList);
  280. else
  281. list_add_tail(&cfile->flist, &cinode->openFileList);
  282. spin_unlock(&cifs_file_list_lock);
  283. file->private_data = cfile;
  284. return cfile;
  285. }
  286. struct cifsFileInfo *
  287. cifsFileInfo_get(struct cifsFileInfo *cifs_file)
  288. {
  289. spin_lock(&cifs_file_list_lock);
  290. cifsFileInfo_get_locked(cifs_file);
  291. spin_unlock(&cifs_file_list_lock);
  292. return cifs_file;
  293. }
  294. /*
  295. * Release a reference on the file private data. This may involve closing
  296. * the filehandle out on the server. Must be called without holding
  297. * cifs_file_list_lock.
  298. */
  299. void cifsFileInfo_put(struct cifsFileInfo *cifs_file)
  300. {
  301. struct inode *inode = cifs_file->dentry->d_inode;
  302. struct cifs_tcon *tcon = tlink_tcon(cifs_file->tlink);
  303. struct TCP_Server_Info *server = tcon->ses->server;
  304. struct cifsInodeInfo *cifsi = CIFS_I(inode);
  305. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  306. struct cifsLockInfo *li, *tmp;
  307. struct cifs_fid fid;
  308. struct cifs_pending_open open;
  309. spin_lock(&cifs_file_list_lock);
  310. if (--cifs_file->count > 0) {
  311. spin_unlock(&cifs_file_list_lock);
  312. return;
  313. }
  314. if (server->ops->get_lease_key)
  315. server->ops->get_lease_key(inode, &fid);
  316. /* store open in pending opens to make sure we don't miss lease break */
  317. cifs_add_pending_open_locked(&fid, cifs_file->tlink, &open);
  318. /* remove it from the lists */
  319. list_del(&cifs_file->flist);
  320. list_del(&cifs_file->tlist);
  321. if (list_empty(&cifsi->openFileList)) {
  322. cFYI(1, "closing last open instance for inode %p",
  323. cifs_file->dentry->d_inode);
  324. /*
  325. * In strict cache mode we need invalidate mapping on the last
  326. * close because it may cause a error when we open this file
  327. * again and get at least level II oplock.
  328. */
  329. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)
  330. CIFS_I(inode)->invalid_mapping = true;
  331. cifs_set_oplock_level(cifsi, 0);
  332. }
  333. spin_unlock(&cifs_file_list_lock);
  334. cancel_work_sync(&cifs_file->oplock_break);
  335. if (!tcon->need_reconnect && !cifs_file->invalidHandle) {
  336. struct TCP_Server_Info *server = tcon->ses->server;
  337. unsigned int xid;
  338. xid = get_xid();
  339. if (server->ops->close)
  340. server->ops->close(xid, tcon, &cifs_file->fid);
  341. _free_xid(xid);
  342. }
  343. cifs_del_pending_open(&open);
  344. /*
  345. * Delete any outstanding lock records. We'll lose them when the file
  346. * is closed anyway.
  347. */
  348. down_write(&cifsi->lock_sem);
  349. list_for_each_entry_safe(li, tmp, &cifs_file->llist->locks, llist) {
  350. list_del(&li->llist);
  351. cifs_del_lock_waiters(li);
  352. kfree(li);
  353. }
  354. list_del(&cifs_file->llist->llist);
  355. kfree(cifs_file->llist);
  356. up_write(&cifsi->lock_sem);
  357. cifs_put_tlink(cifs_file->tlink);
  358. dput(cifs_file->dentry);
  359. kfree(cifs_file);
  360. }
  361. int cifs_open(struct inode *inode, struct file *file)
  362. {
  363. int rc = -EACCES;
  364. unsigned int xid;
  365. __u32 oplock;
  366. struct cifs_sb_info *cifs_sb;
  367. struct TCP_Server_Info *server;
  368. struct cifs_tcon *tcon;
  369. struct tcon_link *tlink;
  370. struct cifsFileInfo *cfile = NULL;
  371. char *full_path = NULL;
  372. bool posix_open_ok = false;
  373. struct cifs_fid fid;
  374. struct cifs_pending_open open;
  375. xid = get_xid();
  376. cifs_sb = CIFS_SB(inode->i_sb);
  377. tlink = cifs_sb_tlink(cifs_sb);
  378. if (IS_ERR(tlink)) {
  379. free_xid(xid);
  380. return PTR_ERR(tlink);
  381. }
  382. tcon = tlink_tcon(tlink);
  383. server = tcon->ses->server;
  384. full_path = build_path_from_dentry(file->f_path.dentry);
  385. if (full_path == NULL) {
  386. rc = -ENOMEM;
  387. goto out;
  388. }
  389. cFYI(1, "inode = 0x%p file flags are 0x%x for %s",
  390. inode, file->f_flags, full_path);
  391. if (server->oplocks)
  392. oplock = REQ_OPLOCK;
  393. else
  394. oplock = 0;
  395. if (!tcon->broken_posix_open && tcon->unix_ext &&
  396. cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
  397. le64_to_cpu(tcon->fsUnixInfo.Capability))) {
  398. /* can not refresh inode info since size could be stale */
  399. rc = cifs_posix_open(full_path, &inode, inode->i_sb,
  400. cifs_sb->mnt_file_mode /* ignored */,
  401. file->f_flags, &oplock, &fid.netfid, xid);
  402. if (rc == 0) {
  403. cFYI(1, "posix open succeeded");
  404. posix_open_ok = true;
  405. } else if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
  406. if (tcon->ses->serverNOS)
  407. cERROR(1, "server %s of type %s returned"
  408. " unexpected error on SMB posix open"
  409. ", disabling posix open support."
  410. " Check if server update available.",
  411. tcon->ses->serverName,
  412. tcon->ses->serverNOS);
  413. tcon->broken_posix_open = true;
  414. } else if ((rc != -EIO) && (rc != -EREMOTE) &&
  415. (rc != -EOPNOTSUPP)) /* path not found or net err */
  416. goto out;
  417. /*
  418. * Else fallthrough to retry open the old way on network i/o
  419. * or DFS errors.
  420. */
  421. }
  422. if (server->ops->get_lease_key)
  423. server->ops->get_lease_key(inode, &fid);
  424. cifs_add_pending_open(&fid, tlink, &open);
  425. if (!posix_open_ok) {
  426. if (server->ops->get_lease_key)
  427. server->ops->get_lease_key(inode, &fid);
  428. rc = cifs_nt_open(full_path, inode, cifs_sb, tcon,
  429. file->f_flags, &oplock, &fid, xid);
  430. if (rc) {
  431. cifs_del_pending_open(&open);
  432. goto out;
  433. }
  434. }
  435. cfile = cifs_new_fileinfo(&fid, file, tlink, oplock);
  436. if (cfile == NULL) {
  437. if (server->ops->close)
  438. server->ops->close(xid, tcon, &fid);
  439. cifs_del_pending_open(&open);
  440. rc = -ENOMEM;
  441. goto out;
  442. }
  443. cifs_fscache_set_inode_cookie(inode, file);
  444. if ((oplock & CIFS_CREATE_ACTION) && !posix_open_ok && tcon->unix_ext) {
  445. /*
  446. * Time to set mode which we can not set earlier due to
  447. * problems creating new read-only files.
  448. */
  449. struct cifs_unix_set_info_args args = {
  450. .mode = inode->i_mode,
  451. .uid = INVALID_UID, /* no change */
  452. .gid = INVALID_GID, /* no change */
  453. .ctime = NO_CHANGE_64,
  454. .atime = NO_CHANGE_64,
  455. .mtime = NO_CHANGE_64,
  456. .device = 0,
  457. };
  458. CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid.netfid,
  459. cfile->pid);
  460. }
  461. out:
  462. kfree(full_path);
  463. free_xid(xid);
  464. cifs_put_tlink(tlink);
  465. return rc;
  466. }
  467. static int cifs_push_posix_locks(struct cifsFileInfo *cfile);
  468. /*
  469. * Try to reacquire byte range locks that were released when session
  470. * to server was lost.
  471. */
  472. static int
  473. cifs_relock_file(struct cifsFileInfo *cfile)
  474. {
  475. struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
  476. struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode);
  477. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  478. int rc = 0;
  479. /* we are going to update can_cache_brlcks here - need a write access */
  480. down_write(&cinode->lock_sem);
  481. if (cinode->can_cache_brlcks) {
  482. /* can cache locks - no need to push them */
  483. up_write(&cinode->lock_sem);
  484. return rc;
  485. }
  486. if (cap_unix(tcon->ses) &&
  487. (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
  488. ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
  489. rc = cifs_push_posix_locks(cfile);
  490. else
  491. rc = tcon->ses->server->ops->push_mand_locks(cfile);
  492. up_write(&cinode->lock_sem);
  493. return rc;
  494. }
  495. static int
  496. cifs_reopen_file(struct cifsFileInfo *cfile, bool can_flush)
  497. {
  498. int rc = -EACCES;
  499. unsigned int xid;
  500. __u32 oplock;
  501. struct cifs_sb_info *cifs_sb;
  502. struct cifs_tcon *tcon;
  503. struct TCP_Server_Info *server;
  504. struct cifsInodeInfo *cinode;
  505. struct inode *inode;
  506. char *full_path = NULL;
  507. int desired_access;
  508. int disposition = FILE_OPEN;
  509. int create_options = CREATE_NOT_DIR;
  510. struct cifs_fid fid;
  511. xid = get_xid();
  512. mutex_lock(&cfile->fh_mutex);
  513. if (!cfile->invalidHandle) {
  514. mutex_unlock(&cfile->fh_mutex);
  515. rc = 0;
  516. free_xid(xid);
  517. return rc;
  518. }
  519. inode = cfile->dentry->d_inode;
  520. cifs_sb = CIFS_SB(inode->i_sb);
  521. tcon = tlink_tcon(cfile->tlink);
  522. server = tcon->ses->server;
  523. /*
  524. * Can not grab rename sem here because various ops, including those
  525. * that already have the rename sem can end up causing writepage to get
  526. * called and if the server was down that means we end up here, and we
  527. * can never tell if the caller already has the rename_sem.
  528. */
  529. full_path = build_path_from_dentry(cfile->dentry);
  530. if (full_path == NULL) {
  531. rc = -ENOMEM;
  532. mutex_unlock(&cfile->fh_mutex);
  533. free_xid(xid);
  534. return rc;
  535. }
  536. cFYI(1, "inode = 0x%p file flags 0x%x for %s", inode, cfile->f_flags,
  537. full_path);
  538. if (tcon->ses->server->oplocks)
  539. oplock = REQ_OPLOCK;
  540. else
  541. oplock = 0;
  542. if (tcon->unix_ext && cap_unix(tcon->ses) &&
  543. (CIFS_UNIX_POSIX_PATH_OPS_CAP &
  544. le64_to_cpu(tcon->fsUnixInfo.Capability))) {
  545. /*
  546. * O_CREAT, O_EXCL and O_TRUNC already had their effect on the
  547. * original open. Must mask them off for a reopen.
  548. */
  549. unsigned int oflags = cfile->f_flags &
  550. ~(O_CREAT | O_EXCL | O_TRUNC);
  551. rc = cifs_posix_open(full_path, NULL, inode->i_sb,
  552. cifs_sb->mnt_file_mode /* ignored */,
  553. oflags, &oplock, &fid.netfid, xid);
  554. if (rc == 0) {
  555. cFYI(1, "posix reopen succeeded");
  556. goto reopen_success;
  557. }
  558. /*
  559. * fallthrough to retry open the old way on errors, especially
  560. * in the reconnect path it is important to retry hard
  561. */
  562. }
  563. desired_access = cifs_convert_flags(cfile->f_flags);
  564. if (backup_cred(cifs_sb))
  565. create_options |= CREATE_OPEN_BACKUP_INTENT;
  566. if (server->ops->get_lease_key)
  567. server->ops->get_lease_key(inode, &fid);
  568. /*
  569. * Can not refresh inode by passing in file_info buf to be returned by
  570. * CIFSSMBOpen and then calling get_inode_info with returned buf since
  571. * file might have write behind data that needs to be flushed and server
  572. * version of file size can be stale. If we knew for sure that inode was
  573. * not dirty locally we could do this.
  574. */
  575. rc = server->ops->open(xid, tcon, full_path, disposition,
  576. desired_access, create_options, &fid, &oplock,
  577. NULL, cifs_sb);
  578. if (rc) {
  579. mutex_unlock(&cfile->fh_mutex);
  580. cFYI(1, "cifs_reopen returned 0x%x", rc);
  581. cFYI(1, "oplock: %d", oplock);
  582. goto reopen_error_exit;
  583. }
  584. reopen_success:
  585. cfile->invalidHandle = false;
  586. mutex_unlock(&cfile->fh_mutex);
  587. cinode = CIFS_I(inode);
  588. if (can_flush) {
  589. rc = filemap_write_and_wait(inode->i_mapping);
  590. mapping_set_error(inode->i_mapping, rc);
  591. if (tcon->unix_ext)
  592. rc = cifs_get_inode_info_unix(&inode, full_path,
  593. inode->i_sb, xid);
  594. else
  595. rc = cifs_get_inode_info(&inode, full_path, NULL,
  596. inode->i_sb, xid, NULL);
  597. }
  598. /*
  599. * Else we are writing out data to server already and could deadlock if
  600. * we tried to flush data, and since we do not know if we have data that
  601. * would invalidate the current end of file on the server we can not go
  602. * to the server to get the new inode info.
  603. */
  604. server->ops->set_fid(cfile, &fid, oplock);
  605. cifs_relock_file(cfile);
  606. reopen_error_exit:
  607. kfree(full_path);
  608. free_xid(xid);
  609. return rc;
  610. }
  611. int cifs_close(struct inode *inode, struct file *file)
  612. {
  613. if (file->private_data != NULL) {
  614. cifsFileInfo_put(file->private_data);
  615. file->private_data = NULL;
  616. }
  617. /* return code from the ->release op is always ignored */
  618. return 0;
  619. }
  620. int cifs_closedir(struct inode *inode, struct file *file)
  621. {
  622. int rc = 0;
  623. unsigned int xid;
  624. struct cifsFileInfo *cfile = file->private_data;
  625. struct cifs_tcon *tcon;
  626. struct TCP_Server_Info *server;
  627. char *buf;
  628. cFYI(1, "Closedir inode = 0x%p", inode);
  629. if (cfile == NULL)
  630. return rc;
  631. xid = get_xid();
  632. tcon = tlink_tcon(cfile->tlink);
  633. server = tcon->ses->server;
  634. cFYI(1, "Freeing private data in close dir");
  635. spin_lock(&cifs_file_list_lock);
  636. if (!cfile->srch_inf.endOfSearch && !cfile->invalidHandle) {
  637. cfile->invalidHandle = true;
  638. spin_unlock(&cifs_file_list_lock);
  639. if (server->ops->close_dir)
  640. rc = server->ops->close_dir(xid, tcon, &cfile->fid);
  641. else
  642. rc = -ENOSYS;
  643. cFYI(1, "Closing uncompleted readdir with rc %d", rc);
  644. /* not much we can do if it fails anyway, ignore rc */
  645. rc = 0;
  646. } else
  647. spin_unlock(&cifs_file_list_lock);
  648. buf = cfile->srch_inf.ntwrk_buf_start;
  649. if (buf) {
  650. cFYI(1, "closedir free smb buf in srch struct");
  651. cfile->srch_inf.ntwrk_buf_start = NULL;
  652. if (cfile->srch_inf.smallBuf)
  653. cifs_small_buf_release(buf);
  654. else
  655. cifs_buf_release(buf);
  656. }
  657. cifs_put_tlink(cfile->tlink);
  658. kfree(file->private_data);
  659. file->private_data = NULL;
  660. /* BB can we lock the filestruct while this is going on? */
  661. free_xid(xid);
  662. return rc;
  663. }
  664. static struct cifsLockInfo *
  665. cifs_lock_init(__u64 offset, __u64 length, __u8 type)
  666. {
  667. struct cifsLockInfo *lock =
  668. kmalloc(sizeof(struct cifsLockInfo), GFP_KERNEL);
  669. if (!lock)
  670. return lock;
  671. lock->offset = offset;
  672. lock->length = length;
  673. lock->type = type;
  674. lock->pid = current->tgid;
  675. INIT_LIST_HEAD(&lock->blist);
  676. init_waitqueue_head(&lock->block_q);
  677. return lock;
  678. }
  679. void
  680. cifs_del_lock_waiters(struct cifsLockInfo *lock)
  681. {
  682. struct cifsLockInfo *li, *tmp;
  683. list_for_each_entry_safe(li, tmp, &lock->blist, blist) {
  684. list_del_init(&li->blist);
  685. wake_up(&li->block_q);
  686. }
  687. }
  688. #define CIFS_LOCK_OP 0
  689. #define CIFS_READ_OP 1
  690. #define CIFS_WRITE_OP 2
  691. /* @rw_check : 0 - no op, 1 - read, 2 - write */
  692. static bool
  693. cifs_find_fid_lock_conflict(struct cifs_fid_locks *fdlocks, __u64 offset,
  694. __u64 length, __u8 type, struct cifsFileInfo *cfile,
  695. struct cifsLockInfo **conf_lock, int rw_check)
  696. {
  697. struct cifsLockInfo *li;
  698. struct cifsFileInfo *cur_cfile = fdlocks->cfile;
  699. struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
  700. list_for_each_entry(li, &fdlocks->locks, llist) {
  701. if (offset + length <= li->offset ||
  702. offset >= li->offset + li->length)
  703. continue;
  704. if (rw_check != CIFS_LOCK_OP && current->tgid == li->pid &&
  705. server->ops->compare_fids(cfile, cur_cfile)) {
  706. /* shared lock prevents write op through the same fid */
  707. if (!(li->type & server->vals->shared_lock_type) ||
  708. rw_check != CIFS_WRITE_OP)
  709. continue;
  710. }
  711. if ((type & server->vals->shared_lock_type) &&
  712. ((server->ops->compare_fids(cfile, cur_cfile) &&
  713. current->tgid == li->pid) || type == li->type))
  714. continue;
  715. if (conf_lock)
  716. *conf_lock = li;
  717. return true;
  718. }
  719. return false;
  720. }
  721. bool
  722. cifs_find_lock_conflict(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
  723. __u8 type, struct cifsLockInfo **conf_lock,
  724. int rw_check)
  725. {
  726. bool rc = false;
  727. struct cifs_fid_locks *cur;
  728. struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode);
  729. list_for_each_entry(cur, &cinode->llist, llist) {
  730. rc = cifs_find_fid_lock_conflict(cur, offset, length, type,
  731. cfile, conf_lock, rw_check);
  732. if (rc)
  733. break;
  734. }
  735. return rc;
  736. }
  737. /*
  738. * Check if there is another lock that prevents us to set the lock (mandatory
  739. * style). If such a lock exists, update the flock structure with its
  740. * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
  741. * or leave it the same if we can't. Returns 0 if we don't need to request to
  742. * the server or 1 otherwise.
  743. */
  744. static int
  745. cifs_lock_test(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
  746. __u8 type, struct file_lock *flock)
  747. {
  748. int rc = 0;
  749. struct cifsLockInfo *conf_lock;
  750. struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode);
  751. struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
  752. bool exist;
  753. down_read(&cinode->lock_sem);
  754. exist = cifs_find_lock_conflict(cfile, offset, length, type,
  755. &conf_lock, CIFS_LOCK_OP);
  756. if (exist) {
  757. flock->fl_start = conf_lock->offset;
  758. flock->fl_end = conf_lock->offset + conf_lock->length - 1;
  759. flock->fl_pid = conf_lock->pid;
  760. if (conf_lock->type & server->vals->shared_lock_type)
  761. flock->fl_type = F_RDLCK;
  762. else
  763. flock->fl_type = F_WRLCK;
  764. } else if (!cinode->can_cache_brlcks)
  765. rc = 1;
  766. else
  767. flock->fl_type = F_UNLCK;
  768. up_read(&cinode->lock_sem);
  769. return rc;
  770. }
  771. static void
  772. cifs_lock_add(struct cifsFileInfo *cfile, struct cifsLockInfo *lock)
  773. {
  774. struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode);
  775. down_write(&cinode->lock_sem);
  776. list_add_tail(&lock->llist, &cfile->llist->locks);
  777. up_write(&cinode->lock_sem);
  778. }
  779. /*
  780. * Set the byte-range lock (mandatory style). Returns:
  781. * 1) 0, if we set the lock and don't need to request to the server;
  782. * 2) 1, if no locks prevent us but we need to request to the server;
  783. * 3) -EACCESS, if there is a lock that prevents us and wait is false.
  784. */
  785. static int
  786. cifs_lock_add_if(struct cifsFileInfo *cfile, struct cifsLockInfo *lock,
  787. bool wait)
  788. {
  789. struct cifsLockInfo *conf_lock;
  790. struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode);
  791. bool exist;
  792. int rc = 0;
  793. try_again:
  794. exist = false;
  795. down_write(&cinode->lock_sem);
  796. exist = cifs_find_lock_conflict(cfile, lock->offset, lock->length,
  797. lock->type, &conf_lock, CIFS_LOCK_OP);
  798. if (!exist && cinode->can_cache_brlcks) {
  799. list_add_tail(&lock->llist, &cfile->llist->locks);
  800. up_write(&cinode->lock_sem);
  801. return rc;
  802. }
  803. if (!exist)
  804. rc = 1;
  805. else if (!wait)
  806. rc = -EACCES;
  807. else {
  808. list_add_tail(&lock->blist, &conf_lock->blist);
  809. up_write(&cinode->lock_sem);
  810. rc = wait_event_interruptible(lock->block_q,
  811. (lock->blist.prev == &lock->blist) &&
  812. (lock->blist.next == &lock->blist));
  813. if (!rc)
  814. goto try_again;
  815. down_write(&cinode->lock_sem);
  816. list_del_init(&lock->blist);
  817. }
  818. up_write(&cinode->lock_sem);
  819. return rc;
  820. }
  821. /*
  822. * Check if there is another lock that prevents us to set the lock (posix
  823. * style). If such a lock exists, update the flock structure with its
  824. * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
  825. * or leave it the same if we can't. Returns 0 if we don't need to request to
  826. * the server or 1 otherwise.
  827. */
  828. static int
  829. cifs_posix_lock_test(struct file *file, struct file_lock *flock)
  830. {
  831. int rc = 0;
  832. struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
  833. unsigned char saved_type = flock->fl_type;
  834. if ((flock->fl_flags & FL_POSIX) == 0)
  835. return 1;
  836. down_read(&cinode->lock_sem);
  837. posix_test_lock(file, flock);
  838. if (flock->fl_type == F_UNLCK && !cinode->can_cache_brlcks) {
  839. flock->fl_type = saved_type;
  840. rc = 1;
  841. }
  842. up_read(&cinode->lock_sem);
  843. return rc;
  844. }
  845. /*
  846. * Set the byte-range lock (posix style). Returns:
  847. * 1) 0, if we set the lock and don't need to request to the server;
  848. * 2) 1, if we need to request to the server;
  849. * 3) <0, if the error occurs while setting the lock.
  850. */
  851. static int
  852. cifs_posix_lock_set(struct file *file, struct file_lock *flock)
  853. {
  854. struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
  855. int rc = 1;
  856. if ((flock->fl_flags & FL_POSIX) == 0)
  857. return rc;
  858. try_again:
  859. down_write(&cinode->lock_sem);
  860. if (!cinode->can_cache_brlcks) {
  861. up_write(&cinode->lock_sem);
  862. return rc;
  863. }
  864. rc = posix_lock_file(file, flock, NULL);
  865. up_write(&cinode->lock_sem);
  866. if (rc == FILE_LOCK_DEFERRED) {
  867. rc = wait_event_interruptible(flock->fl_wait, !flock->fl_next);
  868. if (!rc)
  869. goto try_again;
  870. locks_delete_block(flock);
  871. }
  872. return rc;
  873. }
  874. int
  875. cifs_push_mandatory_locks(struct cifsFileInfo *cfile)
  876. {
  877. unsigned int xid;
  878. int rc = 0, stored_rc;
  879. struct cifsLockInfo *li, *tmp;
  880. struct cifs_tcon *tcon;
  881. unsigned int num, max_num, max_buf;
  882. LOCKING_ANDX_RANGE *buf, *cur;
  883. int types[] = {LOCKING_ANDX_LARGE_FILES,
  884. LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES};
  885. int i;
  886. xid = get_xid();
  887. tcon = tlink_tcon(cfile->tlink);
  888. /*
  889. * Accessing maxBuf is racy with cifs_reconnect - need to store value
  890. * and check it for zero before using.
  891. */
  892. max_buf = tcon->ses->server->maxBuf;
  893. if (!max_buf) {
  894. free_xid(xid);
  895. return -EINVAL;
  896. }
  897. max_num = (max_buf - sizeof(struct smb_hdr)) /
  898. sizeof(LOCKING_ANDX_RANGE);
  899. buf = kzalloc(max_num * sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
  900. if (!buf) {
  901. free_xid(xid);
  902. return -ENOMEM;
  903. }
  904. for (i = 0; i < 2; i++) {
  905. cur = buf;
  906. num = 0;
  907. list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
  908. if (li->type != types[i])
  909. continue;
  910. cur->Pid = cpu_to_le16(li->pid);
  911. cur->LengthLow = cpu_to_le32((u32)li->length);
  912. cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
  913. cur->OffsetLow = cpu_to_le32((u32)li->offset);
  914. cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
  915. if (++num == max_num) {
  916. stored_rc = cifs_lockv(xid, tcon,
  917. cfile->fid.netfid,
  918. (__u8)li->type, 0, num,
  919. buf);
  920. if (stored_rc)
  921. rc = stored_rc;
  922. cur = buf;
  923. num = 0;
  924. } else
  925. cur++;
  926. }
  927. if (num) {
  928. stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
  929. (__u8)types[i], 0, num, buf);
  930. if (stored_rc)
  931. rc = stored_rc;
  932. }
  933. }
  934. kfree(buf);
  935. free_xid(xid);
  936. return rc;
  937. }
  938. /* copied from fs/locks.c with a name change */
  939. #define cifs_for_each_lock(inode, lockp) \
  940. for (lockp = &inode->i_flock; *lockp != NULL; \
  941. lockp = &(*lockp)->fl_next)
  942. struct lock_to_push {
  943. struct list_head llist;
  944. __u64 offset;
  945. __u64 length;
  946. __u32 pid;
  947. __u16 netfid;
  948. __u8 type;
  949. };
  950. static int
  951. cifs_push_posix_locks(struct cifsFileInfo *cfile)
  952. {
  953. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  954. struct file_lock *flock, **before;
  955. unsigned int count = 0, i = 0;
  956. int rc = 0, xid, type;
  957. struct list_head locks_to_send, *el;
  958. struct lock_to_push *lck, *tmp;
  959. __u64 length;
  960. xid = get_xid();
  961. lock_flocks();
  962. cifs_for_each_lock(cfile->dentry->d_inode, before) {
  963. if ((*before)->fl_flags & FL_POSIX)
  964. count++;
  965. }
  966. unlock_flocks();
  967. INIT_LIST_HEAD(&locks_to_send);
  968. /*
  969. * Allocating count locks is enough because no FL_POSIX locks can be
  970. * added to the list while we are holding cinode->lock_sem that
  971. * protects locking operations of this inode.
  972. */
  973. for (; i < count; i++) {
  974. lck = kmalloc(sizeof(struct lock_to_push), GFP_KERNEL);
  975. if (!lck) {
  976. rc = -ENOMEM;
  977. goto err_out;
  978. }
  979. list_add_tail(&lck->llist, &locks_to_send);
  980. }
  981. el = locks_to_send.next;
  982. lock_flocks();
  983. cifs_for_each_lock(cfile->dentry->d_inode, before) {
  984. flock = *before;
  985. if ((flock->fl_flags & FL_POSIX) == 0)
  986. continue;
  987. if (el == &locks_to_send) {
  988. /*
  989. * The list ended. We don't have enough allocated
  990. * structures - something is really wrong.
  991. */
  992. cERROR(1, "Can't push all brlocks!");
  993. break;
  994. }
  995. length = 1 + flock->fl_end - flock->fl_start;
  996. if (flock->fl_type == F_RDLCK || flock->fl_type == F_SHLCK)
  997. type = CIFS_RDLCK;
  998. else
  999. type = CIFS_WRLCK;
  1000. lck = list_entry(el, struct lock_to_push, llist);
  1001. lck->pid = flock->fl_pid;
  1002. lck->netfid = cfile->fid.netfid;
  1003. lck->length = length;
  1004. lck->type = type;
  1005. lck->offset = flock->fl_start;
  1006. el = el->next;
  1007. }
  1008. unlock_flocks();
  1009. list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
  1010. int stored_rc;
  1011. stored_rc = CIFSSMBPosixLock(xid, tcon, lck->netfid, lck->pid,
  1012. lck->offset, lck->length, NULL,
  1013. lck->type, 0);
  1014. if (stored_rc)
  1015. rc = stored_rc;
  1016. list_del(&lck->llist);
  1017. kfree(lck);
  1018. }
  1019. out:
  1020. free_xid(xid);
  1021. return rc;
  1022. err_out:
  1023. list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
  1024. list_del(&lck->llist);
  1025. kfree(lck);
  1026. }
  1027. goto out;
  1028. }
  1029. static int
  1030. cifs_push_locks(struct cifsFileInfo *cfile)
  1031. {
  1032. struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
  1033. struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode);
  1034. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  1035. int rc = 0;
  1036. /* we are going to update can_cache_brlcks here - need a write access */
  1037. down_write(&cinode->lock_sem);
  1038. if (!cinode->can_cache_brlcks) {
  1039. up_write(&cinode->lock_sem);
  1040. return rc;
  1041. }
  1042. if (cap_unix(tcon->ses) &&
  1043. (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
  1044. ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
  1045. rc = cifs_push_posix_locks(cfile);
  1046. else
  1047. rc = tcon->ses->server->ops->push_mand_locks(cfile);
  1048. cinode->can_cache_brlcks = false;
  1049. up_write(&cinode->lock_sem);
  1050. return rc;
  1051. }
  1052. static void
  1053. cifs_read_flock(struct file_lock *flock, __u32 *type, int *lock, int *unlock,
  1054. bool *wait_flag, struct TCP_Server_Info *server)
  1055. {
  1056. if (flock->fl_flags & FL_POSIX)
  1057. cFYI(1, "Posix");
  1058. if (flock->fl_flags & FL_FLOCK)
  1059. cFYI(1, "Flock");
  1060. if (flock->fl_flags & FL_SLEEP) {
  1061. cFYI(1, "Blocking lock");
  1062. *wait_flag = true;
  1063. }
  1064. if (flock->fl_flags & FL_ACCESS)
  1065. cFYI(1, "Process suspended by mandatory locking - "
  1066. "not implemented yet");
  1067. if (flock->fl_flags & FL_LEASE)
  1068. cFYI(1, "Lease on file - not implemented yet");
  1069. if (flock->fl_flags &
  1070. (~(FL_POSIX | FL_FLOCK | FL_SLEEP |
  1071. FL_ACCESS | FL_LEASE | FL_CLOSE)))
  1072. cFYI(1, "Unknown lock flags 0x%x", flock->fl_flags);
  1073. *type = server->vals->large_lock_type;
  1074. if (flock->fl_type == F_WRLCK) {
  1075. cFYI(1, "F_WRLCK ");
  1076. *type |= server->vals->exclusive_lock_type;
  1077. *lock = 1;
  1078. } else if (flock->fl_type == F_UNLCK) {
  1079. cFYI(1, "F_UNLCK");
  1080. *type |= server->vals->unlock_lock_type;
  1081. *unlock = 1;
  1082. /* Check if unlock includes more than one lock range */
  1083. } else if (flock->fl_type == F_RDLCK) {
  1084. cFYI(1, "F_RDLCK");
  1085. *type |= server->vals->shared_lock_type;
  1086. *lock = 1;
  1087. } else if (flock->fl_type == F_EXLCK) {
  1088. cFYI(1, "F_EXLCK");
  1089. *type |= server->vals->exclusive_lock_type;
  1090. *lock = 1;
  1091. } else if (flock->fl_type == F_SHLCK) {
  1092. cFYI(1, "F_SHLCK");
  1093. *type |= server->vals->shared_lock_type;
  1094. *lock = 1;
  1095. } else
  1096. cFYI(1, "Unknown type of lock");
  1097. }
  1098. static int
  1099. cifs_getlk(struct file *file, struct file_lock *flock, __u32 type,
  1100. bool wait_flag, bool posix_lck, unsigned int xid)
  1101. {
  1102. int rc = 0;
  1103. __u64 length = 1 + flock->fl_end - flock->fl_start;
  1104. struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
  1105. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  1106. struct TCP_Server_Info *server = tcon->ses->server;
  1107. __u16 netfid = cfile->fid.netfid;
  1108. if (posix_lck) {
  1109. int posix_lock_type;
  1110. rc = cifs_posix_lock_test(file, flock);
  1111. if (!rc)
  1112. return rc;
  1113. if (type & server->vals->shared_lock_type)
  1114. posix_lock_type = CIFS_RDLCK;
  1115. else
  1116. posix_lock_type = CIFS_WRLCK;
  1117. rc = CIFSSMBPosixLock(xid, tcon, netfid, current->tgid,
  1118. flock->fl_start, length, flock,
  1119. posix_lock_type, wait_flag);
  1120. return rc;
  1121. }
  1122. rc = cifs_lock_test(cfile, flock->fl_start, length, type, flock);
  1123. if (!rc)
  1124. return rc;
  1125. /* BB we could chain these into one lock request BB */
  1126. rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, type,
  1127. 1, 0, false);
  1128. if (rc == 0) {
  1129. rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
  1130. type, 0, 1, false);
  1131. flock->fl_type = F_UNLCK;
  1132. if (rc != 0)
  1133. cERROR(1, "Error unlocking previously locked "
  1134. "range %d during test of lock", rc);
  1135. return 0;
  1136. }
  1137. if (type & server->vals->shared_lock_type) {
  1138. flock->fl_type = F_WRLCK;
  1139. return 0;
  1140. }
  1141. type &= ~server->vals->exclusive_lock_type;
  1142. rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
  1143. type | server->vals->shared_lock_type,
  1144. 1, 0, false);
  1145. if (rc == 0) {
  1146. rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
  1147. type | server->vals->shared_lock_type, 0, 1, false);
  1148. flock->fl_type = F_RDLCK;
  1149. if (rc != 0)
  1150. cERROR(1, "Error unlocking previously locked "
  1151. "range %d during test of lock", rc);
  1152. } else
  1153. flock->fl_type = F_WRLCK;
  1154. return 0;
  1155. }
  1156. void
  1157. cifs_move_llist(struct list_head *source, struct list_head *dest)
  1158. {
  1159. struct list_head *li, *tmp;
  1160. list_for_each_safe(li, tmp, source)
  1161. list_move(li, dest);
  1162. }
  1163. void
  1164. cifs_free_llist(struct list_head *llist)
  1165. {
  1166. struct cifsLockInfo *li, *tmp;
  1167. list_for_each_entry_safe(li, tmp, llist, llist) {
  1168. cifs_del_lock_waiters(li);
  1169. list_del(&li->llist);
  1170. kfree(li);
  1171. }
  1172. }
  1173. int
  1174. cifs_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
  1175. unsigned int xid)
  1176. {
  1177. int rc = 0, stored_rc;
  1178. int types[] = {LOCKING_ANDX_LARGE_FILES,
  1179. LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES};
  1180. unsigned int i;
  1181. unsigned int max_num, num, max_buf;
  1182. LOCKING_ANDX_RANGE *buf, *cur;
  1183. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  1184. struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode);
  1185. struct cifsLockInfo *li, *tmp;
  1186. __u64 length = 1 + flock->fl_end - flock->fl_start;
  1187. struct list_head tmp_llist;
  1188. INIT_LIST_HEAD(&tmp_llist);
  1189. /*
  1190. * Accessing maxBuf is racy with cifs_reconnect - need to store value
  1191. * and check it for zero before using.
  1192. */
  1193. max_buf = tcon->ses->server->maxBuf;
  1194. if (!max_buf)
  1195. return -EINVAL;
  1196. max_num = (max_buf - sizeof(struct smb_hdr)) /
  1197. sizeof(LOCKING_ANDX_RANGE);
  1198. buf = kzalloc(max_num * sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
  1199. if (!buf)
  1200. return -ENOMEM;
  1201. down_write(&cinode->lock_sem);
  1202. for (i = 0; i < 2; i++) {
  1203. cur = buf;
  1204. num = 0;
  1205. list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
  1206. if (flock->fl_start > li->offset ||
  1207. (flock->fl_start + length) <
  1208. (li->offset + li->length))
  1209. continue;
  1210. if (current->tgid != li->pid)
  1211. continue;
  1212. if (types[i] != li->type)
  1213. continue;
  1214. if (cinode->can_cache_brlcks) {
  1215. /*
  1216. * We can cache brlock requests - simply remove
  1217. * a lock from the file's list.
  1218. */
  1219. list_del(&li->llist);
  1220. cifs_del_lock_waiters(li);
  1221. kfree(li);
  1222. continue;
  1223. }
  1224. cur->Pid = cpu_to_le16(li->pid);
  1225. cur->LengthLow = cpu_to_le32((u32)li->length);
  1226. cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
  1227. cur->OffsetLow = cpu_to_le32((u32)li->offset);
  1228. cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
  1229. /*
  1230. * We need to save a lock here to let us add it again to
  1231. * the file's list if the unlock range request fails on
  1232. * the server.
  1233. */
  1234. list_move(&li->llist, &tmp_llist);
  1235. if (++num == max_num) {
  1236. stored_rc = cifs_lockv(xid, tcon,
  1237. cfile->fid.netfid,
  1238. li->type, num, 0, buf);
  1239. if (stored_rc) {
  1240. /*
  1241. * We failed on the unlock range
  1242. * request - add all locks from the tmp
  1243. * list to the head of the file's list.
  1244. */
  1245. cifs_move_llist(&tmp_llist,
  1246. &cfile->llist->locks);
  1247. rc = stored_rc;
  1248. } else
  1249. /*
  1250. * The unlock range request succeed -
  1251. * free the tmp list.
  1252. */
  1253. cifs_free_llist(&tmp_llist);
  1254. cur = buf;
  1255. num = 0;
  1256. } else
  1257. cur++;
  1258. }
  1259. if (num) {
  1260. stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
  1261. types[i], num, 0, buf);
  1262. if (stored_rc) {
  1263. cifs_move_llist(&tmp_llist,
  1264. &cfile->llist->locks);
  1265. rc = stored_rc;
  1266. } else
  1267. cifs_free_llist(&tmp_llist);
  1268. }
  1269. }
  1270. up_write(&cinode->lock_sem);
  1271. kfree(buf);
  1272. return rc;
  1273. }
  1274. static int
  1275. cifs_setlk(struct file *file, struct file_lock *flock, __u32 type,
  1276. bool wait_flag, bool posix_lck, int lock, int unlock,
  1277. unsigned int xid)
  1278. {
  1279. int rc = 0;
  1280. __u64 length = 1 + flock->fl_end - flock->fl_start;
  1281. struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
  1282. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  1283. struct TCP_Server_Info *server = tcon->ses->server;
  1284. struct inode *inode = cfile->dentry->d_inode;
  1285. if (posix_lck) {
  1286. int posix_lock_type;
  1287. rc = cifs_posix_lock_set(file, flock);
  1288. if (!rc || rc < 0)
  1289. return rc;
  1290. if (type & server->vals->shared_lock_type)
  1291. posix_lock_type = CIFS_RDLCK;
  1292. else
  1293. posix_lock_type = CIFS_WRLCK;
  1294. if (unlock == 1)
  1295. posix_lock_type = CIFS_UNLCK;
  1296. rc = CIFSSMBPosixLock(xid, tcon, cfile->fid.netfid,
  1297. current->tgid, flock->fl_start, length,
  1298. NULL, posix_lock_type, wait_flag);
  1299. goto out;
  1300. }
  1301. if (lock) {
  1302. struct cifsLockInfo *lock;
  1303. lock = cifs_lock_init(flock->fl_start, length, type);
  1304. if (!lock)
  1305. return -ENOMEM;
  1306. rc = cifs_lock_add_if(cfile, lock, wait_flag);
  1307. if (rc < 0) {
  1308. kfree(lock);
  1309. return rc;
  1310. }
  1311. if (!rc)
  1312. goto out;
  1313. /*
  1314. * Windows 7 server can delay breaking lease from read to None
  1315. * if we set a byte-range lock on a file - break it explicitly
  1316. * before sending the lock to the server to be sure the next
  1317. * read won't conflict with non-overlapted locks due to
  1318. * pagereading.
  1319. */
  1320. if (!CIFS_I(inode)->clientCanCacheAll &&
  1321. CIFS_I(inode)->clientCanCacheRead) {
  1322. cifs_invalidate_mapping(inode);
  1323. cFYI(1, "Set no oplock for inode=%p due to mand locks",
  1324. inode);
  1325. CIFS_I(inode)->clientCanCacheRead = false;
  1326. }
  1327. rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
  1328. type, 1, 0, wait_flag);
  1329. if (rc) {
  1330. kfree(lock);
  1331. return rc;
  1332. }
  1333. cifs_lock_add(cfile, lock);
  1334. } else if (unlock)
  1335. rc = server->ops->mand_unlock_range(cfile, flock, xid);
  1336. out:
  1337. if (flock->fl_flags & FL_POSIX)
  1338. posix_lock_file_wait(file, flock);
  1339. return rc;
  1340. }
  1341. int cifs_lock(struct file *file, int cmd, struct file_lock *flock)
  1342. {
  1343. int rc, xid;
  1344. int lock = 0, unlock = 0;
  1345. bool wait_flag = false;
  1346. bool posix_lck = false;
  1347. struct cifs_sb_info *cifs_sb;
  1348. struct cifs_tcon *tcon;
  1349. struct cifsInodeInfo *cinode;
  1350. struct cifsFileInfo *cfile;
  1351. __u16 netfid;
  1352. __u32 type;
  1353. rc = -EACCES;
  1354. xid = get_xid();
  1355. cFYI(1, "Lock parm: 0x%x flockflags: 0x%x flocktype: 0x%x start: %lld "
  1356. "end: %lld", cmd, flock->fl_flags, flock->fl_type,
  1357. flock->fl_start, flock->fl_end);
  1358. cfile = (struct cifsFileInfo *)file->private_data;
  1359. tcon = tlink_tcon(cfile->tlink);
  1360. cifs_read_flock(flock, &type, &lock, &unlock, &wait_flag,
  1361. tcon->ses->server);
  1362. cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
  1363. netfid = cfile->fid.netfid;
  1364. cinode = CIFS_I(file_inode(file));
  1365. if (cap_unix(tcon->ses) &&
  1366. (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
  1367. ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
  1368. posix_lck = true;
  1369. /*
  1370. * BB add code here to normalize offset and length to account for
  1371. * negative length which we can not accept over the wire.
  1372. */
  1373. if (IS_GETLK(cmd)) {
  1374. rc = cifs_getlk(file, flock, type, wait_flag, posix_lck, xid);
  1375. free_xid(xid);
  1376. return rc;
  1377. }
  1378. if (!lock && !unlock) {
  1379. /*
  1380. * if no lock or unlock then nothing to do since we do not
  1381. * know what it is
  1382. */
  1383. free_xid(xid);
  1384. return -EOPNOTSUPP;
  1385. }
  1386. rc = cifs_setlk(file, flock, type, wait_flag, posix_lck, lock, unlock,
  1387. xid);
  1388. free_xid(xid);
  1389. return rc;
  1390. }
  1391. /*
  1392. * update the file size (if needed) after a write. Should be called with
  1393. * the inode->i_lock held
  1394. */
  1395. void
  1396. cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset,
  1397. unsigned int bytes_written)
  1398. {
  1399. loff_t end_of_write = offset + bytes_written;
  1400. if (end_of_write > cifsi->server_eof)
  1401. cifsi->server_eof = end_of_write;
  1402. }
  1403. static ssize_t
  1404. cifs_write(struct cifsFileInfo *open_file, __u32 pid, const char *write_data,
  1405. size_t write_size, loff_t *offset)
  1406. {
  1407. int rc = 0;
  1408. unsigned int bytes_written = 0;
  1409. unsigned int total_written;
  1410. struct cifs_sb_info *cifs_sb;
  1411. struct cifs_tcon *tcon;
  1412. struct TCP_Server_Info *server;
  1413. unsigned int xid;
  1414. struct dentry *dentry = open_file->dentry;
  1415. struct cifsInodeInfo *cifsi = CIFS_I(dentry->d_inode);
  1416. struct cifs_io_parms io_parms;
  1417. cifs_sb = CIFS_SB(dentry->d_sb);
  1418. cFYI(1, "write %zd bytes to offset %lld of %s", write_size,
  1419. *offset, dentry->d_name.name);
  1420. tcon = tlink_tcon(open_file->tlink);
  1421. server = tcon->ses->server;
  1422. if (!server->ops->sync_write)
  1423. return -ENOSYS;
  1424. xid = get_xid();
  1425. for (total_written = 0; write_size > total_written;
  1426. total_written += bytes_written) {
  1427. rc = -EAGAIN;
  1428. while (rc == -EAGAIN) {
  1429. struct kvec iov[2];
  1430. unsigned int len;
  1431. if (open_file->invalidHandle) {
  1432. /* we could deadlock if we called
  1433. filemap_fdatawait from here so tell
  1434. reopen_file not to flush data to
  1435. server now */
  1436. rc = cifs_reopen_file(open_file, false);
  1437. if (rc != 0)
  1438. break;
  1439. }
  1440. len = min((size_t)cifs_sb->wsize,
  1441. write_size - total_written);
  1442. /* iov[0] is reserved for smb header */
  1443. iov[1].iov_base = (char *)write_data + total_written;
  1444. iov[1].iov_len = len;
  1445. io_parms.pid = pid;
  1446. io_parms.tcon = tcon;
  1447. io_parms.offset = *offset;
  1448. io_parms.length = len;
  1449. rc = server->ops->sync_write(xid, open_file, &io_parms,
  1450. &bytes_written, iov, 1);
  1451. }
  1452. if (rc || (bytes_written == 0)) {
  1453. if (total_written)
  1454. break;
  1455. else {
  1456. free_xid(xid);
  1457. return rc;
  1458. }
  1459. } else {
  1460. spin_lock(&dentry->d_inode->i_lock);
  1461. cifs_update_eof(cifsi, *offset, bytes_written);
  1462. spin_unlock(&dentry->d_inode->i_lock);
  1463. *offset += bytes_written;
  1464. }
  1465. }
  1466. cifs_stats_bytes_written(tcon, total_written);
  1467. if (total_written > 0) {
  1468. spin_lock(&dentry->d_inode->i_lock);
  1469. if (*offset > dentry->d_inode->i_size)
  1470. i_size_write(dentry->d_inode, *offset);
  1471. spin_unlock(&dentry->d_inode->i_lock);
  1472. }
  1473. mark_inode_dirty_sync(dentry->d_inode);
  1474. free_xid(xid);
  1475. return total_written;
  1476. }
  1477. struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode,
  1478. bool fsuid_only)
  1479. {
  1480. struct cifsFileInfo *open_file = NULL;
  1481. struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb);
  1482. /* only filter by fsuid on multiuser mounts */
  1483. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
  1484. fsuid_only = false;
  1485. spin_lock(&cifs_file_list_lock);
  1486. /* we could simply get the first_list_entry since write-only entries
  1487. are always at the end of the list but since the first entry might
  1488. have a close pending, we go through the whole list */
  1489. list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
  1490. if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
  1491. continue;
  1492. if (OPEN_FMODE(open_file->f_flags) & FMODE_READ) {
  1493. if (!open_file->invalidHandle) {
  1494. /* found a good file */
  1495. /* lock it so it will not be closed on us */
  1496. cifsFileInfo_get_locked(open_file);
  1497. spin_unlock(&cifs_file_list_lock);
  1498. return open_file;
  1499. } /* else might as well continue, and look for
  1500. another, or simply have the caller reopen it
  1501. again rather than trying to fix this handle */
  1502. } else /* write only file */
  1503. break; /* write only files are last so must be done */
  1504. }
  1505. spin_unlock(&cifs_file_list_lock);
  1506. return NULL;
  1507. }
  1508. struct cifsFileInfo *find_writable_file(struct cifsInodeInfo *cifs_inode,
  1509. bool fsuid_only)
  1510. {
  1511. struct cifsFileInfo *open_file, *inv_file = NULL;
  1512. struct cifs_sb_info *cifs_sb;
  1513. bool any_available = false;
  1514. int rc;
  1515. unsigned int refind = 0;
  1516. /* Having a null inode here (because mapping->host was set to zero by
  1517. the VFS or MM) should not happen but we had reports of on oops (due to
  1518. it being zero) during stress testcases so we need to check for it */
  1519. if (cifs_inode == NULL) {
  1520. cERROR(1, "Null inode passed to cifs_writeable_file");
  1521. dump_stack();
  1522. return NULL;
  1523. }
  1524. cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb);
  1525. /* only filter by fsuid on multiuser mounts */
  1526. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
  1527. fsuid_only = false;
  1528. spin_lock(&cifs_file_list_lock);
  1529. refind_writable:
  1530. if (refind > MAX_REOPEN_ATT) {
  1531. spin_unlock(&cifs_file_list_lock);
  1532. return NULL;
  1533. }
  1534. list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
  1535. if (!any_available && open_file->pid != current->tgid)
  1536. continue;
  1537. if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
  1538. continue;
  1539. if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
  1540. if (!open_file->invalidHandle) {
  1541. /* found a good writable file */
  1542. cifsFileInfo_get_locked(open_file);
  1543. spin_unlock(&cifs_file_list_lock);
  1544. return open_file;
  1545. } else {
  1546. if (!inv_file)
  1547. inv_file = open_file;
  1548. }
  1549. }
  1550. }
  1551. /* couldn't find useable FH with same pid, try any available */
  1552. if (!any_available) {
  1553. any_available = true;
  1554. goto refind_writable;
  1555. }
  1556. if (inv_file) {
  1557. any_available = false;
  1558. cifsFileInfo_get_locked(inv_file);
  1559. }
  1560. spin_unlock(&cifs_file_list_lock);
  1561. if (inv_file) {
  1562. rc = cifs_reopen_file(inv_file, false);
  1563. if (!rc)
  1564. return inv_file;
  1565. else {
  1566. spin_lock(&cifs_file_list_lock);
  1567. list_move_tail(&inv_file->flist,
  1568. &cifs_inode->openFileList);
  1569. spin_unlock(&cifs_file_list_lock);
  1570. cifsFileInfo_put(inv_file);
  1571. spin_lock(&cifs_file_list_lock);
  1572. ++refind;
  1573. goto refind_writable;
  1574. }
  1575. }
  1576. return NULL;
  1577. }
  1578. static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to)
  1579. {
  1580. struct address_space *mapping = page->mapping;
  1581. loff_t offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
  1582. char *write_data;
  1583. int rc = -EFAULT;
  1584. int bytes_written = 0;
  1585. struct inode *inode;
  1586. struct cifsFileInfo *open_file;
  1587. if (!mapping || !mapping->host)
  1588. return -EFAULT;
  1589. inode = page->mapping->host;
  1590. offset += (loff_t)from;
  1591. write_data = kmap(page);
  1592. write_data += from;
  1593. if ((to > PAGE_CACHE_SIZE) || (from > to)) {
  1594. kunmap(page);
  1595. return -EIO;
  1596. }
  1597. /* racing with truncate? */
  1598. if (offset > mapping->host->i_size) {
  1599. kunmap(page);
  1600. return 0; /* don't care */
  1601. }
  1602. /* check to make sure that we are not extending the file */
  1603. if (mapping->host->i_size - offset < (loff_t)to)
  1604. to = (unsigned)(mapping->host->i_size - offset);
  1605. open_file = find_writable_file(CIFS_I(mapping->host), false);
  1606. if (open_file) {
  1607. bytes_written = cifs_write(open_file, open_file->pid,
  1608. write_data, to - from, &offset);
  1609. cifsFileInfo_put(open_file);
  1610. /* Does mm or vfs already set times? */
  1611. inode->i_atime = inode->i_mtime = current_fs_time(inode->i_sb);
  1612. if ((bytes_written > 0) && (offset))
  1613. rc = 0;
  1614. else if (bytes_written < 0)
  1615. rc = bytes_written;
  1616. } else {
  1617. cFYI(1, "No writeable filehandles for inode");
  1618. rc = -EIO;
  1619. }
  1620. kunmap(page);
  1621. return rc;
  1622. }
  1623. static int cifs_writepages(struct address_space *mapping,
  1624. struct writeback_control *wbc)
  1625. {
  1626. struct cifs_sb_info *cifs_sb = CIFS_SB(mapping->host->i_sb);
  1627. bool done = false, scanned = false, range_whole = false;
  1628. pgoff_t end, index;
  1629. struct cifs_writedata *wdata;
  1630. struct TCP_Server_Info *server;
  1631. struct page *page;
  1632. int rc = 0;
  1633. /*
  1634. * If wsize is smaller than the page cache size, default to writing
  1635. * one page at a time via cifs_writepage
  1636. */
  1637. if (cifs_sb->wsize < PAGE_CACHE_SIZE)
  1638. return generic_writepages(mapping, wbc);
  1639. if (wbc->range_cyclic) {
  1640. index = mapping->writeback_index; /* Start from prev offset */
  1641. end = -1;
  1642. } else {
  1643. index = wbc->range_start >> PAGE_CACHE_SHIFT;
  1644. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  1645. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  1646. range_whole = true;
  1647. scanned = true;
  1648. }
  1649. retry:
  1650. while (!done && index <= end) {
  1651. unsigned int i, nr_pages, found_pages;
  1652. pgoff_t next = 0, tofind;
  1653. struct page **pages;
  1654. tofind = min((cifs_sb->wsize / PAGE_CACHE_SIZE) - 1,
  1655. end - index) + 1;
  1656. wdata = cifs_writedata_alloc((unsigned int)tofind,
  1657. cifs_writev_complete);
  1658. if (!wdata) {
  1659. rc = -ENOMEM;
  1660. break;
  1661. }
  1662. /*
  1663. * find_get_pages_tag seems to return a max of 256 on each
  1664. * iteration, so we must call it several times in order to
  1665. * fill the array or the wsize is effectively limited to
  1666. * 256 * PAGE_CACHE_SIZE.
  1667. */
  1668. found_pages = 0;
  1669. pages = wdata->pages;
  1670. do {
  1671. nr_pages = find_get_pages_tag(mapping, &index,
  1672. PAGECACHE_TAG_DIRTY,
  1673. tofind, pages);
  1674. found_pages += nr_pages;
  1675. tofind -= nr_pages;
  1676. pages += nr_pages;
  1677. } while (nr_pages && tofind && index <= end);
  1678. if (found_pages == 0) {
  1679. kref_put(&wdata->refcount, cifs_writedata_release);
  1680. break;
  1681. }
  1682. nr_pages = 0;
  1683. for (i = 0; i < found_pages; i++) {
  1684. page = wdata->pages[i];
  1685. /*
  1686. * At this point we hold neither mapping->tree_lock nor
  1687. * lock on the page itself: the page may be truncated or
  1688. * invalidated (changing page->mapping to NULL), or even
  1689. * swizzled back from swapper_space to tmpfs file
  1690. * mapping
  1691. */
  1692. if (nr_pages == 0)
  1693. lock_page(page);
  1694. else if (!trylock_page(page))
  1695. break;
  1696. if (unlikely(page->mapping != mapping)) {
  1697. unlock_page(page);
  1698. break;
  1699. }
  1700. if (!wbc->range_cyclic && page->index > end) {
  1701. done = true;
  1702. unlock_page(page);
  1703. break;
  1704. }
  1705. if (next && (page->index != next)) {
  1706. /* Not next consecutive page */
  1707. unlock_page(page);
  1708. break;
  1709. }
  1710. if (wbc->sync_mode != WB_SYNC_NONE)
  1711. wait_on_page_writeback(page);
  1712. if (PageWriteback(page) ||
  1713. !clear_page_dirty_for_io(page)) {
  1714. unlock_page(page);
  1715. break;
  1716. }
  1717. /*
  1718. * This actually clears the dirty bit in the radix tree.
  1719. * See cifs_writepage() for more commentary.
  1720. */
  1721. set_page_writeback(page);
  1722. if (page_offset(page) >= i_size_read(mapping->host)) {
  1723. done = true;
  1724. unlock_page(page);
  1725. end_page_writeback(page);
  1726. break;
  1727. }
  1728. wdata->pages[i] = page;
  1729. next = page->index + 1;
  1730. ++nr_pages;
  1731. }
  1732. /* reset index to refind any pages skipped */
  1733. if (nr_pages == 0)
  1734. index = wdata->pages[0]->index + 1;
  1735. /* put any pages we aren't going to use */
  1736. for (i = nr_pages; i < found_pages; i++) {
  1737. page_cache_release(wdata->pages[i]);
  1738. wdata->pages[i] = NULL;
  1739. }
  1740. /* nothing to write? */
  1741. if (nr_pages == 0) {
  1742. kref_put(&wdata->refcount, cifs_writedata_release);
  1743. continue;
  1744. }
  1745. wdata->sync_mode = wbc->sync_mode;
  1746. wdata->nr_pages = nr_pages;
  1747. wdata->offset = page_offset(wdata->pages[0]);
  1748. wdata->pagesz = PAGE_CACHE_SIZE;
  1749. wdata->tailsz =
  1750. min(i_size_read(mapping->host) -
  1751. page_offset(wdata->pages[nr_pages - 1]),
  1752. (loff_t)PAGE_CACHE_SIZE);
  1753. wdata->bytes = ((nr_pages - 1) * PAGE_CACHE_SIZE) +
  1754. wdata->tailsz;
  1755. do {
  1756. if (wdata->cfile != NULL)
  1757. cifsFileInfo_put(wdata->cfile);
  1758. wdata->cfile = find_writable_file(CIFS_I(mapping->host),
  1759. false);
  1760. if (!wdata->cfile) {
  1761. cERROR(1, "No writable handles for inode");
  1762. rc = -EBADF;
  1763. break;
  1764. }
  1765. wdata->pid = wdata->cfile->pid;
  1766. server = tlink_tcon(wdata->cfile->tlink)->ses->server;
  1767. rc = server->ops->async_writev(wdata);
  1768. } while (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN);
  1769. for (i = 0; i < nr_pages; ++i)
  1770. unlock_page(wdata->pages[i]);
  1771. /* send failure -- clean up the mess */
  1772. if (rc != 0) {
  1773. for (i = 0; i < nr_pages; ++i) {
  1774. if (rc == -EAGAIN)
  1775. redirty_page_for_writepage(wbc,
  1776. wdata->pages[i]);
  1777. else
  1778. SetPageError(wdata->pages[i]);
  1779. end_page_writeback(wdata->pages[i]);
  1780. page_cache_release(wdata->pages[i]);
  1781. }
  1782. if (rc != -EAGAIN)
  1783. mapping_set_error(mapping, rc);
  1784. }
  1785. kref_put(&wdata->refcount, cifs_writedata_release);
  1786. wbc->nr_to_write -= nr_pages;
  1787. if (wbc->nr_to_write <= 0)
  1788. done = true;
  1789. index = next;
  1790. }
  1791. if (!scanned && !done) {
  1792. /*
  1793. * We hit the last page and there is more work to be done: wrap
  1794. * back to the start of the file
  1795. */
  1796. scanned = true;
  1797. index = 0;
  1798. goto retry;
  1799. }
  1800. if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
  1801. mapping->writeback_index = index;
  1802. return rc;
  1803. }
  1804. static int
  1805. cifs_writepage_locked(struct page *page, struct writeback_control *wbc)
  1806. {
  1807. int rc;
  1808. unsigned int xid;
  1809. xid = get_xid();
  1810. /* BB add check for wbc flags */
  1811. page_cache_get(page);
  1812. if (!PageUptodate(page))
  1813. cFYI(1, "ppw - page not up to date");
  1814. /*
  1815. * Set the "writeback" flag, and clear "dirty" in the radix tree.
  1816. *
  1817. * A writepage() implementation always needs to do either this,
  1818. * or re-dirty the page with "redirty_page_for_writepage()" in
  1819. * the case of a failure.
  1820. *
  1821. * Just unlocking the page will cause the radix tree tag-bits
  1822. * to fail to update with the state of the page correctly.
  1823. */
  1824. set_page_writeback(page);
  1825. retry_write:
  1826. rc = cifs_partialpagewrite(page, 0, PAGE_CACHE_SIZE);
  1827. if (rc == -EAGAIN && wbc->sync_mode == WB_SYNC_ALL)
  1828. goto retry_write;
  1829. else if (rc == -EAGAIN)
  1830. redirty_page_for_writepage(wbc, page);
  1831. else if (rc != 0)
  1832. SetPageError(page);
  1833. else
  1834. SetPageUptodate(page);
  1835. end_page_writeback(page);
  1836. page_cache_release(page);
  1837. free_xid(xid);
  1838. return rc;
  1839. }
  1840. static int cifs_writepage(struct page *page, struct writeback_control *wbc)
  1841. {
  1842. int rc = cifs_writepage_locked(page, wbc);
  1843. unlock_page(page);
  1844. return rc;
  1845. }
  1846. static int cifs_write_end(struct file *file, struct address_space *mapping,
  1847. loff_t pos, unsigned len, unsigned copied,
  1848. struct page *page, void *fsdata)
  1849. {
  1850. int rc;
  1851. struct inode *inode = mapping->host;
  1852. struct cifsFileInfo *cfile = file->private_data;
  1853. struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
  1854. __u32 pid;
  1855. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
  1856. pid = cfile->pid;
  1857. else
  1858. pid = current->tgid;
  1859. cFYI(1, "write_end for page %p from pos %lld with %d bytes",
  1860. page, pos, copied);
  1861. if (PageChecked(page)) {
  1862. if (copied == len)
  1863. SetPageUptodate(page);
  1864. ClearPageChecked(page);
  1865. } else if (!PageUptodate(page) && copied == PAGE_CACHE_SIZE)
  1866. SetPageUptodate(page);
  1867. if (!PageUptodate(page)) {
  1868. char *page_data;
  1869. unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
  1870. unsigned int xid;
  1871. xid = get_xid();
  1872. /* this is probably better than directly calling
  1873. partialpage_write since in this function the file handle is
  1874. known which we might as well leverage */
  1875. /* BB check if anything else missing out of ppw
  1876. such as updating last write time */
  1877. page_data = kmap(page);
  1878. rc = cifs_write(cfile, pid, page_data + offset, copied, &pos);
  1879. /* if (rc < 0) should we set writebehind rc? */
  1880. kunmap(page);
  1881. free_xid(xid);
  1882. } else {
  1883. rc = copied;
  1884. pos += copied;
  1885. set_page_dirty(page);
  1886. }
  1887. if (rc > 0) {
  1888. spin_lock(&inode->i_lock);
  1889. if (pos > inode->i_size)
  1890. i_size_write(inode, pos);
  1891. spin_unlock(&inode->i_lock);
  1892. }
  1893. unlock_page(page);
  1894. page_cache_release(page);
  1895. return rc;
  1896. }
  1897. int cifs_strict_fsync(struct file *file, loff_t start, loff_t end,
  1898. int datasync)
  1899. {
  1900. unsigned int xid;
  1901. int rc = 0;
  1902. struct cifs_tcon *tcon;
  1903. struct TCP_Server_Info *server;
  1904. struct cifsFileInfo *smbfile = file->private_data;
  1905. struct inode *inode = file_inode(file);
  1906. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1907. rc = filemap_write_and_wait_range(inode->i_mapping, start, end);
  1908. if (rc)
  1909. return rc;
  1910. mutex_lock(&inode->i_mutex);
  1911. xid = get_xid();
  1912. cFYI(1, "Sync file - name: %s datasync: 0x%x",
  1913. file->f_path.dentry->d_name.name, datasync);
  1914. if (!CIFS_I(inode)->clientCanCacheRead) {
  1915. rc = cifs_invalidate_mapping(inode);
  1916. if (rc) {
  1917. cFYI(1, "rc: %d during invalidate phase", rc);
  1918. rc = 0; /* don't care about it in fsync */
  1919. }
  1920. }
  1921. tcon = tlink_tcon(smbfile->tlink);
  1922. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
  1923. server = tcon->ses->server;
  1924. if (server->ops->flush)
  1925. rc = server->ops->flush(xid, tcon, &smbfile->fid);
  1926. else
  1927. rc = -ENOSYS;
  1928. }
  1929. free_xid(xid);
  1930. mutex_unlock(&inode->i_mutex);
  1931. return rc;
  1932. }
  1933. int cifs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  1934. {
  1935. unsigned int xid;
  1936. int rc = 0;
  1937. struct cifs_tcon *tcon;
  1938. struct TCP_Server_Info *server;
  1939. struct cifsFileInfo *smbfile = file->private_data;
  1940. struct cifs_sb_info *cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
  1941. struct inode *inode = file->f_mapping->host;
  1942. rc = filemap_write_and_wait_range(inode->i_mapping, start, end);
  1943. if (rc)
  1944. return rc;
  1945. mutex_lock(&inode->i_mutex);
  1946. xid = get_xid();
  1947. cFYI(1, "Sync file - name: %s datasync: 0x%x",
  1948. file->f_path.dentry->d_name.name, datasync);
  1949. tcon = tlink_tcon(smbfile->tlink);
  1950. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
  1951. server = tcon->ses->server;
  1952. if (server->ops->flush)
  1953. rc = server->ops->flush(xid, tcon, &smbfile->fid);
  1954. else
  1955. rc = -ENOSYS;
  1956. }
  1957. free_xid(xid);
  1958. mutex_unlock(&inode->i_mutex);
  1959. return rc;
  1960. }
  1961. /*
  1962. * As file closes, flush all cached write data for this inode checking
  1963. * for write behind errors.
  1964. */
  1965. int cifs_flush(struct file *file, fl_owner_t id)
  1966. {
  1967. struct inode *inode = file_inode(file);
  1968. int rc = 0;
  1969. if (file->f_mode & FMODE_WRITE)
  1970. rc = filemap_write_and_wait(inode->i_mapping);
  1971. cFYI(1, "Flush inode %p file %p rc %d", inode, file, rc);
  1972. return rc;
  1973. }
  1974. static int
  1975. cifs_write_allocate_pages(struct page **pages, unsigned long num_pages)
  1976. {
  1977. int rc = 0;
  1978. unsigned long i;
  1979. for (i = 0; i < num_pages; i++) {
  1980. pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
  1981. if (!pages[i]) {
  1982. /*
  1983. * save number of pages we have already allocated and
  1984. * return with ENOMEM error
  1985. */
  1986. num_pages = i;
  1987. rc = -ENOMEM;
  1988. break;
  1989. }
  1990. }
  1991. if (rc) {
  1992. for (i = 0; i < num_pages; i++)
  1993. put_page(pages[i]);
  1994. }
  1995. return rc;
  1996. }
  1997. static inline
  1998. size_t get_numpages(const size_t wsize, const size_t len, size_t *cur_len)
  1999. {
  2000. size_t num_pages;
  2001. size_t clen;
  2002. clen = min_t(const size_t, len, wsize);
  2003. num_pages = DIV_ROUND_UP(clen, PAGE_SIZE);
  2004. if (cur_len)
  2005. *cur_len = clen;
  2006. return num_pages;
  2007. }
  2008. static void
  2009. cifs_uncached_writev_complete(struct work_struct *work)
  2010. {
  2011. int i;
  2012. struct cifs_writedata *wdata = container_of(work,
  2013. struct cifs_writedata, work);
  2014. struct inode *inode = wdata->cfile->dentry->d_inode;
  2015. struct cifsInodeInfo *cifsi = CIFS_I(inode);
  2016. spin_lock(&inode->i_lock);
  2017. cifs_update_eof(cifsi, wdata->offset, wdata->bytes);
  2018. if (cifsi->server_eof > inode->i_size)
  2019. i_size_write(inode, cifsi->server_eof);
  2020. spin_unlock(&inode->i_lock);
  2021. complete(&wdata->done);
  2022. if (wdata->result != -EAGAIN) {
  2023. for (i = 0; i < wdata->nr_pages; i++)
  2024. put_page(wdata->pages[i]);
  2025. }
  2026. kref_put(&wdata->refcount, cifs_writedata_release);
  2027. }
  2028. /* attempt to send write to server, retry on any -EAGAIN errors */
  2029. static int
  2030. cifs_uncached_retry_writev(struct cifs_writedata *wdata)
  2031. {
  2032. int rc;
  2033. struct TCP_Server_Info *server;
  2034. server = tlink_tcon(wdata->cfile->tlink)->ses->server;
  2035. do {
  2036. if (wdata->cfile->invalidHandle) {
  2037. rc = cifs_reopen_file(wdata->cfile, false);
  2038. if (rc != 0)
  2039. continue;
  2040. }
  2041. rc = server->ops->async_writev(wdata);
  2042. } while (rc == -EAGAIN);
  2043. return rc;
  2044. }
  2045. static ssize_t
  2046. cifs_iovec_write(struct file *file, const struct iovec *iov,
  2047. unsigned long nr_segs, loff_t *poffset)
  2048. {
  2049. unsigned long nr_pages, i;
  2050. size_t copied, len, cur_len;
  2051. ssize_t total_written = 0;
  2052. loff_t offset;
  2053. struct iov_iter it;
  2054. struct cifsFileInfo *open_file;
  2055. struct cifs_tcon *tcon;
  2056. struct cifs_sb_info *cifs_sb;
  2057. struct cifs_writedata *wdata, *tmp;
  2058. struct list_head wdata_list;
  2059. int rc;
  2060. pid_t pid;
  2061. len = iov_length(iov, nr_segs);
  2062. if (!len)
  2063. return 0;
  2064. rc = generic_write_checks(file, poffset, &len, 0);
  2065. if (rc)
  2066. return rc;
  2067. INIT_LIST_HEAD(&wdata_list);
  2068. cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
  2069. open_file = file->private_data;
  2070. tcon = tlink_tcon(open_file->tlink);
  2071. if (!tcon->ses->server->ops->async_writev)
  2072. return -ENOSYS;
  2073. offset = *poffset;
  2074. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
  2075. pid = open_file->pid;
  2076. else
  2077. pid = current->tgid;
  2078. iov_iter_init(&it, iov, nr_segs, len, 0);
  2079. do {
  2080. size_t save_len;
  2081. nr_pages = get_numpages(cifs_sb->wsize, len, &cur_len);
  2082. wdata = cifs_writedata_alloc(nr_pages,
  2083. cifs_uncached_writev_complete);
  2084. if (!wdata) {
  2085. rc = -ENOMEM;
  2086. break;
  2087. }
  2088. rc = cifs_write_allocate_pages(wdata->pages, nr_pages);
  2089. if (rc) {
  2090. kfree(wdata);
  2091. break;
  2092. }
  2093. save_len = cur_len;
  2094. for (i = 0; i < nr_pages; i++) {
  2095. copied = min_t(const size_t, cur_len, PAGE_SIZE);
  2096. copied = iov_iter_copy_from_user(wdata->pages[i], &it,
  2097. 0, copied);
  2098. cur_len -= copied;
  2099. iov_iter_advance(&it, copied);
  2100. }
  2101. cur_len = save_len - cur_len;
  2102. wdata->sync_mode = WB_SYNC_ALL;
  2103. wdata->nr_pages = nr_pages;
  2104. wdata->offset = (__u64)offset;
  2105. wdata->cfile = cifsFileInfo_get(open_file);
  2106. wdata->pid = pid;
  2107. wdata->bytes = cur_len;
  2108. wdata->pagesz = PAGE_SIZE;
  2109. wdata->tailsz = cur_len - ((nr_pages - 1) * PAGE_SIZE);
  2110. rc = cifs_uncached_retry_writev(wdata);
  2111. if (rc) {
  2112. kref_put(&wdata->refcount, cifs_writedata_release);
  2113. break;
  2114. }
  2115. list_add_tail(&wdata->list, &wdata_list);
  2116. offset += cur_len;
  2117. len -= cur_len;
  2118. } while (len > 0);
  2119. /*
  2120. * If at least one write was successfully sent, then discard any rc
  2121. * value from the later writes. If the other write succeeds, then
  2122. * we'll end up returning whatever was written. If it fails, then
  2123. * we'll get a new rc value from that.
  2124. */
  2125. if (!list_empty(&wdata_list))
  2126. rc = 0;
  2127. /*
  2128. * Wait for and collect replies for any successful sends in order of
  2129. * increasing offset. Once an error is hit or we get a fatal signal
  2130. * while waiting, then return without waiting for any more replies.
  2131. */
  2132. restart_loop:
  2133. list_for_each_entry_safe(wdata, tmp, &wdata_list, list) {
  2134. if (!rc) {
  2135. /* FIXME: freezable too? */
  2136. rc = wait_for_completion_killable(&wdata->done);
  2137. if (rc)
  2138. rc = -EINTR;
  2139. else if (wdata->result)
  2140. rc = wdata->result;
  2141. else
  2142. total_written += wdata->bytes;
  2143. /* resend call if it's a retryable error */
  2144. if (rc == -EAGAIN) {
  2145. rc = cifs_uncached_retry_writev(wdata);
  2146. goto restart_loop;
  2147. }
  2148. }
  2149. list_del_init(&wdata->list);
  2150. kref_put(&wdata->refcount, cifs_writedata_release);
  2151. }
  2152. if (total_written > 0)
  2153. *poffset += total_written;
  2154. cifs_stats_bytes_written(tcon, total_written);
  2155. return total_written ? total_written : (ssize_t)rc;
  2156. }
  2157. ssize_t cifs_user_writev(struct kiocb *iocb, const struct iovec *iov,
  2158. unsigned long nr_segs, loff_t pos)
  2159. {
  2160. ssize_t written;
  2161. struct inode *inode;
  2162. inode = file_inode(iocb->ki_filp);
  2163. /*
  2164. * BB - optimize the way when signing is disabled. We can drop this
  2165. * extra memory-to-memory copying and use iovec buffers for constructing
  2166. * write request.
  2167. */
  2168. written = cifs_iovec_write(iocb->ki_filp, iov, nr_segs, &pos);
  2169. if (written > 0) {
  2170. CIFS_I(inode)->invalid_mapping = true;
  2171. iocb->ki_pos = pos;
  2172. }
  2173. return written;
  2174. }
  2175. static ssize_t
  2176. cifs_writev(struct kiocb *iocb, const struct iovec *iov,
  2177. unsigned long nr_segs, loff_t pos)
  2178. {
  2179. struct file *file = iocb->ki_filp;
  2180. struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
  2181. struct inode *inode = file->f_mapping->host;
  2182. struct cifsInodeInfo *cinode = CIFS_I(inode);
  2183. struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
  2184. ssize_t rc = -EACCES;
  2185. BUG_ON(iocb->ki_pos != pos);
  2186. sb_start_write(inode->i_sb);
  2187. /*
  2188. * We need to hold the sem to be sure nobody modifies lock list
  2189. * with a brlock that prevents writing.
  2190. */
  2191. down_read(&cinode->lock_sem);
  2192. if (!cifs_find_lock_conflict(cfile, pos, iov_length(iov, nr_segs),
  2193. server->vals->exclusive_lock_type, NULL,
  2194. CIFS_WRITE_OP)) {
  2195. mutex_lock(&inode->i_mutex);
  2196. rc = __generic_file_aio_write(iocb, iov, nr_segs,
  2197. &iocb->ki_pos);
  2198. mutex_unlock(&inode->i_mutex);
  2199. }
  2200. if (rc > 0 || rc == -EIOCBQUEUED) {
  2201. ssize_t err;
  2202. err = generic_write_sync(file, pos, rc);
  2203. if (err < 0 && rc > 0)
  2204. rc = err;
  2205. }
  2206. up_read(&cinode->lock_sem);
  2207. sb_end_write(inode->i_sb);
  2208. return rc;
  2209. }
  2210. ssize_t
  2211. cifs_strict_writev(struct kiocb *iocb, const struct iovec *iov,
  2212. unsigned long nr_segs, loff_t pos)
  2213. {
  2214. struct inode *inode = file_inode(iocb->ki_filp);
  2215. struct cifsInodeInfo *cinode = CIFS_I(inode);
  2216. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  2217. struct cifsFileInfo *cfile = (struct cifsFileInfo *)
  2218. iocb->ki_filp->private_data;
  2219. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  2220. ssize_t written;
  2221. if (cinode->clientCanCacheAll) {
  2222. if (cap_unix(tcon->ses) &&
  2223. (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability))
  2224. && ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
  2225. return generic_file_aio_write(iocb, iov, nr_segs, pos);
  2226. return cifs_writev(iocb, iov, nr_segs, pos);
  2227. }
  2228. /*
  2229. * For non-oplocked files in strict cache mode we need to write the data
  2230. * to the server exactly from the pos to pos+len-1 rather than flush all
  2231. * affected pages because it may cause a error with mandatory locks on
  2232. * these pages but not on the region from pos to ppos+len-1.
  2233. */
  2234. written = cifs_user_writev(iocb, iov, nr_segs, pos);
  2235. if (written > 0 && cinode->clientCanCacheRead) {
  2236. /*
  2237. * Windows 7 server can delay breaking level2 oplock if a write
  2238. * request comes - break it on the client to prevent reading
  2239. * an old data.
  2240. */
  2241. cifs_invalidate_mapping(inode);
  2242. cFYI(1, "Set no oplock for inode=%p after a write operation",
  2243. inode);
  2244. cinode->clientCanCacheRead = false;
  2245. }
  2246. return written;
  2247. }
  2248. static struct cifs_readdata *
  2249. cifs_readdata_alloc(unsigned int nr_pages, work_func_t complete)
  2250. {
  2251. struct cifs_readdata *rdata;
  2252. rdata = kzalloc(sizeof(*rdata) + (sizeof(struct page *) * nr_pages),
  2253. GFP_KERNEL);
  2254. if (rdata != NULL) {
  2255. kref_init(&rdata->refcount);
  2256. INIT_LIST_HEAD(&rdata->list);
  2257. init_completion(&rdata->done);
  2258. INIT_WORK(&rdata->work, complete);
  2259. }
  2260. return rdata;
  2261. }
  2262. void
  2263. cifs_readdata_release(struct kref *refcount)
  2264. {
  2265. struct cifs_readdata *rdata = container_of(refcount,
  2266. struct cifs_readdata, refcount);
  2267. if (rdata->cfile)
  2268. cifsFileInfo_put(rdata->cfile);
  2269. kfree(rdata);
  2270. }
  2271. static int
  2272. cifs_read_allocate_pages(struct cifs_readdata *rdata, unsigned int nr_pages)
  2273. {
  2274. int rc = 0;
  2275. struct page *page;
  2276. unsigned int i;
  2277. for (i = 0; i < nr_pages; i++) {
  2278. page = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
  2279. if (!page) {
  2280. rc = -ENOMEM;
  2281. break;
  2282. }
  2283. rdata->pages[i] = page;
  2284. }
  2285. if (rc) {
  2286. for (i = 0; i < nr_pages; i++) {
  2287. put_page(rdata->pages[i]);
  2288. rdata->pages[i] = NULL;
  2289. }
  2290. }
  2291. return rc;
  2292. }
  2293. static void
  2294. cifs_uncached_readdata_release(struct kref *refcount)
  2295. {
  2296. struct cifs_readdata *rdata = container_of(refcount,
  2297. struct cifs_readdata, refcount);
  2298. unsigned int i;
  2299. for (i = 0; i < rdata->nr_pages; i++) {
  2300. put_page(rdata->pages[i]);
  2301. rdata->pages[i] = NULL;
  2302. }
  2303. cifs_readdata_release(refcount);
  2304. }
  2305. static int
  2306. cifs_retry_async_readv(struct cifs_readdata *rdata)
  2307. {
  2308. int rc;
  2309. struct TCP_Server_Info *server;
  2310. server = tlink_tcon(rdata->cfile->tlink)->ses->server;
  2311. do {
  2312. if (rdata->cfile->invalidHandle) {
  2313. rc = cifs_reopen_file(rdata->cfile, true);
  2314. if (rc != 0)
  2315. continue;
  2316. }
  2317. rc = server->ops->async_readv(rdata);
  2318. } while (rc == -EAGAIN);
  2319. return rc;
  2320. }
  2321. /**
  2322. * cifs_readdata_to_iov - copy data from pages in response to an iovec
  2323. * @rdata: the readdata response with list of pages holding data
  2324. * @iov: vector in which we should copy the data
  2325. * @nr_segs: number of segments in vector
  2326. * @offset: offset into file of the first iovec
  2327. * @copied: used to return the amount of data copied to the iov
  2328. *
  2329. * This function copies data from a list of pages in a readdata response into
  2330. * an array of iovecs. It will first calculate where the data should go
  2331. * based on the info in the readdata and then copy the data into that spot.
  2332. */
  2333. static ssize_t
  2334. cifs_readdata_to_iov(struct cifs_readdata *rdata, const struct iovec *iov,
  2335. unsigned long nr_segs, loff_t offset, ssize_t *copied)
  2336. {
  2337. int rc = 0;
  2338. struct iov_iter ii;
  2339. size_t pos = rdata->offset - offset;
  2340. ssize_t remaining = rdata->bytes;
  2341. unsigned char *pdata;
  2342. unsigned int i;
  2343. /* set up iov_iter and advance to the correct offset */
  2344. iov_iter_init(&ii, iov, nr_segs, iov_length(iov, nr_segs), 0);
  2345. iov_iter_advance(&ii, pos);
  2346. *copied = 0;
  2347. for (i = 0; i < rdata->nr_pages; i++) {
  2348. ssize_t copy;
  2349. struct page *page = rdata->pages[i];
  2350. /* copy a whole page or whatever's left */
  2351. copy = min_t(ssize_t, remaining, PAGE_SIZE);
  2352. /* ...but limit it to whatever space is left in the iov */
  2353. copy = min_t(ssize_t, copy, iov_iter_count(&ii));
  2354. /* go while there's data to be copied and no errors */
  2355. if (copy && !rc) {
  2356. pdata = kmap(page);
  2357. rc = memcpy_toiovecend(ii.iov, pdata, ii.iov_offset,
  2358. (int)copy);
  2359. kunmap(page);
  2360. if (!rc) {
  2361. *copied += copy;
  2362. remaining -= copy;
  2363. iov_iter_advance(&ii, copy);
  2364. }
  2365. }
  2366. }
  2367. return rc;
  2368. }
  2369. static void
  2370. cifs_uncached_readv_complete(struct work_struct *work)
  2371. {
  2372. struct cifs_readdata *rdata = container_of(work,
  2373. struct cifs_readdata, work);
  2374. complete(&rdata->done);
  2375. kref_put(&rdata->refcount, cifs_uncached_readdata_release);
  2376. }
  2377. static int
  2378. cifs_uncached_read_into_pages(struct TCP_Server_Info *server,
  2379. struct cifs_readdata *rdata, unsigned int len)
  2380. {
  2381. int total_read = 0, result = 0;
  2382. unsigned int i;
  2383. unsigned int nr_pages = rdata->nr_pages;
  2384. struct kvec iov;
  2385. rdata->tailsz = PAGE_SIZE;
  2386. for (i = 0; i < nr_pages; i++) {
  2387. struct page *page = rdata->pages[i];
  2388. if (len >= PAGE_SIZE) {
  2389. /* enough data to fill the page */
  2390. iov.iov_base = kmap(page);
  2391. iov.iov_len = PAGE_SIZE;
  2392. cFYI(1, "%u: iov_base=%p iov_len=%zu",
  2393. i, iov.iov_base, iov.iov_len);
  2394. len -= PAGE_SIZE;
  2395. } else if (len > 0) {
  2396. /* enough for partial page, fill and zero the rest */
  2397. iov.iov_base = kmap(page);
  2398. iov.iov_len = len;
  2399. cFYI(1, "%u: iov_base=%p iov_len=%zu",
  2400. i, iov.iov_base, iov.iov_len);
  2401. memset(iov.iov_base + len, '\0', PAGE_SIZE - len);
  2402. rdata->tailsz = len;
  2403. len = 0;
  2404. } else {
  2405. /* no need to hold page hostage */
  2406. rdata->pages[i] = NULL;
  2407. rdata->nr_pages--;
  2408. put_page(page);
  2409. continue;
  2410. }
  2411. result = cifs_readv_from_socket(server, &iov, 1, iov.iov_len);
  2412. kunmap(page);
  2413. if (result < 0)
  2414. break;
  2415. total_read += result;
  2416. }
  2417. return total_read > 0 ? total_read : result;
  2418. }
  2419. static ssize_t
  2420. cifs_iovec_read(struct file *file, const struct iovec *iov,
  2421. unsigned long nr_segs, loff_t *poffset)
  2422. {
  2423. ssize_t rc;
  2424. size_t len, cur_len;
  2425. ssize_t total_read = 0;
  2426. loff_t offset = *poffset;
  2427. unsigned int npages;
  2428. struct cifs_sb_info *cifs_sb;
  2429. struct cifs_tcon *tcon;
  2430. struct cifsFileInfo *open_file;
  2431. struct cifs_readdata *rdata, *tmp;
  2432. struct list_head rdata_list;
  2433. pid_t pid;
  2434. if (!nr_segs)
  2435. return 0;
  2436. len = iov_length(iov, nr_segs);
  2437. if (!len)
  2438. return 0;
  2439. INIT_LIST_HEAD(&rdata_list);
  2440. cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
  2441. open_file = file->private_data;
  2442. tcon = tlink_tcon(open_file->tlink);
  2443. if (!tcon->ses->server->ops->async_readv)
  2444. return -ENOSYS;
  2445. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
  2446. pid = open_file->pid;
  2447. else
  2448. pid = current->tgid;
  2449. if ((file->f_flags & O_ACCMODE) == O_WRONLY)
  2450. cFYI(1, "attempting read on write only file instance");
  2451. do {
  2452. cur_len = min_t(const size_t, len - total_read, cifs_sb->rsize);
  2453. npages = DIV_ROUND_UP(cur_len, PAGE_SIZE);
  2454. /* allocate a readdata struct */
  2455. rdata = cifs_readdata_alloc(npages,
  2456. cifs_uncached_readv_complete);
  2457. if (!rdata) {
  2458. rc = -ENOMEM;
  2459. goto error;
  2460. }
  2461. rc = cifs_read_allocate_pages(rdata, npages);
  2462. if (rc)
  2463. goto error;
  2464. rdata->cfile = cifsFileInfo_get(open_file);
  2465. rdata->nr_pages = npages;
  2466. rdata->offset = offset;
  2467. rdata->bytes = cur_len;
  2468. rdata->pid = pid;
  2469. rdata->pagesz = PAGE_SIZE;
  2470. rdata->read_into_pages = cifs_uncached_read_into_pages;
  2471. rc = cifs_retry_async_readv(rdata);
  2472. error:
  2473. if (rc) {
  2474. kref_put(&rdata->refcount,
  2475. cifs_uncached_readdata_release);
  2476. break;
  2477. }
  2478. list_add_tail(&rdata->list, &rdata_list);
  2479. offset += cur_len;
  2480. len -= cur_len;
  2481. } while (len > 0);
  2482. /* if at least one read request send succeeded, then reset rc */
  2483. if (!list_empty(&rdata_list))
  2484. rc = 0;
  2485. /* the loop below should proceed in the order of increasing offsets */
  2486. restart_loop:
  2487. list_for_each_entry_safe(rdata, tmp, &rdata_list, list) {
  2488. if (!rc) {
  2489. ssize_t copied;
  2490. /* FIXME: freezable sleep too? */
  2491. rc = wait_for_completion_killable(&rdata->done);
  2492. if (rc)
  2493. rc = -EINTR;
  2494. else if (rdata->result)
  2495. rc = rdata->result;
  2496. else {
  2497. rc = cifs_readdata_to_iov(rdata, iov,
  2498. nr_segs, *poffset,
  2499. &copied);
  2500. total_read += copied;
  2501. }
  2502. /* resend call if it's a retryable error */
  2503. if (rc == -EAGAIN) {
  2504. rc = cifs_retry_async_readv(rdata);
  2505. goto restart_loop;
  2506. }
  2507. }
  2508. list_del_init(&rdata->list);
  2509. kref_put(&rdata->refcount, cifs_uncached_readdata_release);
  2510. }
  2511. cifs_stats_bytes_read(tcon, total_read);
  2512. *poffset += total_read;
  2513. /* mask nodata case */
  2514. if (rc == -ENODATA)
  2515. rc = 0;
  2516. return total_read ? total_read : rc;
  2517. }
  2518. ssize_t cifs_user_readv(struct kiocb *iocb, const struct iovec *iov,
  2519. unsigned long nr_segs, loff_t pos)
  2520. {
  2521. ssize_t read;
  2522. read = cifs_iovec_read(iocb->ki_filp, iov, nr_segs, &pos);
  2523. if (read > 0)
  2524. iocb->ki_pos = pos;
  2525. return read;
  2526. }
  2527. ssize_t
  2528. cifs_strict_readv(struct kiocb *iocb, const struct iovec *iov,
  2529. unsigned long nr_segs, loff_t pos)
  2530. {
  2531. struct inode *inode = file_inode(iocb->ki_filp);
  2532. struct cifsInodeInfo *cinode = CIFS_I(inode);
  2533. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  2534. struct cifsFileInfo *cfile = (struct cifsFileInfo *)
  2535. iocb->ki_filp->private_data;
  2536. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  2537. int rc = -EACCES;
  2538. /*
  2539. * In strict cache mode we need to read from the server all the time
  2540. * if we don't have level II oplock because the server can delay mtime
  2541. * change - so we can't make a decision about inode invalidating.
  2542. * And we can also fail with pagereading if there are mandatory locks
  2543. * on pages affected by this read but not on the region from pos to
  2544. * pos+len-1.
  2545. */
  2546. if (!cinode->clientCanCacheRead)
  2547. return cifs_user_readv(iocb, iov, nr_segs, pos);
  2548. if (cap_unix(tcon->ses) &&
  2549. (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
  2550. ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
  2551. return generic_file_aio_read(iocb, iov, nr_segs, pos);
  2552. /*
  2553. * We need to hold the sem to be sure nobody modifies lock list
  2554. * with a brlock that prevents reading.
  2555. */
  2556. down_read(&cinode->lock_sem);
  2557. if (!cifs_find_lock_conflict(cfile, pos, iov_length(iov, nr_segs),
  2558. tcon->ses->server->vals->shared_lock_type,
  2559. NULL, CIFS_READ_OP))
  2560. rc = generic_file_aio_read(iocb, iov, nr_segs, pos);
  2561. up_read(&cinode->lock_sem);
  2562. return rc;
  2563. }
  2564. static ssize_t
  2565. cifs_read(struct file *file, char *read_data, size_t read_size, loff_t *offset)
  2566. {
  2567. int rc = -EACCES;
  2568. unsigned int bytes_read = 0;
  2569. unsigned int total_read;
  2570. unsigned int current_read_size;
  2571. unsigned int rsize;
  2572. struct cifs_sb_info *cifs_sb;
  2573. struct cifs_tcon *tcon;
  2574. struct TCP_Server_Info *server;
  2575. unsigned int xid;
  2576. char *cur_offset;
  2577. struct cifsFileInfo *open_file;
  2578. struct cifs_io_parms io_parms;
  2579. int buf_type = CIFS_NO_BUFFER;
  2580. __u32 pid;
  2581. xid = get_xid();
  2582. cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
  2583. /* FIXME: set up handlers for larger reads and/or convert to async */
  2584. rsize = min_t(unsigned int, cifs_sb->rsize, CIFSMaxBufSize);
  2585. if (file->private_data == NULL) {
  2586. rc = -EBADF;
  2587. free_xid(xid);
  2588. return rc;
  2589. }
  2590. open_file = file->private_data;
  2591. tcon = tlink_tcon(open_file->tlink);
  2592. server = tcon->ses->server;
  2593. if (!server->ops->sync_read) {
  2594. free_xid(xid);
  2595. return -ENOSYS;
  2596. }
  2597. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
  2598. pid = open_file->pid;
  2599. else
  2600. pid = current->tgid;
  2601. if ((file->f_flags & O_ACCMODE) == O_WRONLY)
  2602. cFYI(1, "attempting read on write only file instance");
  2603. for (total_read = 0, cur_offset = read_data; read_size > total_read;
  2604. total_read += bytes_read, cur_offset += bytes_read) {
  2605. current_read_size = min_t(uint, read_size - total_read, rsize);
  2606. /*
  2607. * For windows me and 9x we do not want to request more than it
  2608. * negotiated since it will refuse the read then.
  2609. */
  2610. if ((tcon->ses) && !(tcon->ses->capabilities &
  2611. tcon->ses->server->vals->cap_large_files)) {
  2612. current_read_size = min_t(uint, current_read_size,
  2613. CIFSMaxBufSize);
  2614. }
  2615. rc = -EAGAIN;
  2616. while (rc == -EAGAIN) {
  2617. if (open_file->invalidHandle) {
  2618. rc = cifs_reopen_file(open_file, true);
  2619. if (rc != 0)
  2620. break;
  2621. }
  2622. io_parms.pid = pid;
  2623. io_parms.tcon = tcon;
  2624. io_parms.offset = *offset;
  2625. io_parms.length = current_read_size;
  2626. rc = server->ops->sync_read(xid, open_file, &io_parms,
  2627. &bytes_read, &cur_offset,
  2628. &buf_type);
  2629. }
  2630. if (rc || (bytes_read == 0)) {
  2631. if (total_read) {
  2632. break;
  2633. } else {
  2634. free_xid(xid);
  2635. return rc;
  2636. }
  2637. } else {
  2638. cifs_stats_bytes_read(tcon, total_read);
  2639. *offset += bytes_read;
  2640. }
  2641. }
  2642. free_xid(xid);
  2643. return total_read;
  2644. }
  2645. /*
  2646. * If the page is mmap'ed into a process' page tables, then we need to make
  2647. * sure that it doesn't change while being written back.
  2648. */
  2649. static int
  2650. cifs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  2651. {
  2652. struct page *page = vmf->page;
  2653. lock_page(page);
  2654. return VM_FAULT_LOCKED;
  2655. }
  2656. static struct vm_operations_struct cifs_file_vm_ops = {
  2657. .fault = filemap_fault,
  2658. .page_mkwrite = cifs_page_mkwrite,
  2659. .remap_pages = generic_file_remap_pages,
  2660. };
  2661. int cifs_file_strict_mmap(struct file *file, struct vm_area_struct *vma)
  2662. {
  2663. int rc, xid;
  2664. struct inode *inode = file_inode(file);
  2665. xid = get_xid();
  2666. if (!CIFS_I(inode)->clientCanCacheRead) {
  2667. rc = cifs_invalidate_mapping(inode);
  2668. if (rc)
  2669. return rc;
  2670. }
  2671. rc = generic_file_mmap(file, vma);
  2672. if (rc == 0)
  2673. vma->vm_ops = &cifs_file_vm_ops;
  2674. free_xid(xid);
  2675. return rc;
  2676. }
  2677. int cifs_file_mmap(struct file *file, struct vm_area_struct *vma)
  2678. {
  2679. int rc, xid;
  2680. xid = get_xid();
  2681. rc = cifs_revalidate_file(file);
  2682. if (rc) {
  2683. cFYI(1, "Validation prior to mmap failed, error=%d", rc);
  2684. free_xid(xid);
  2685. return rc;
  2686. }
  2687. rc = generic_file_mmap(file, vma);
  2688. if (rc == 0)
  2689. vma->vm_ops = &cifs_file_vm_ops;
  2690. free_xid(xid);
  2691. return rc;
  2692. }
  2693. static void
  2694. cifs_readv_complete(struct work_struct *work)
  2695. {
  2696. unsigned int i;
  2697. struct cifs_readdata *rdata = container_of(work,
  2698. struct cifs_readdata, work);
  2699. for (i = 0; i < rdata->nr_pages; i++) {
  2700. struct page *page = rdata->pages[i];
  2701. lru_cache_add_file(page);
  2702. if (rdata->result == 0) {
  2703. flush_dcache_page(page);
  2704. SetPageUptodate(page);
  2705. }
  2706. unlock_page(page);
  2707. if (rdata->result == 0)
  2708. cifs_readpage_to_fscache(rdata->mapping->host, page);
  2709. page_cache_release(page);
  2710. rdata->pages[i] = NULL;
  2711. }
  2712. kref_put(&rdata->refcount, cifs_readdata_release);
  2713. }
  2714. static int
  2715. cifs_readpages_read_into_pages(struct TCP_Server_Info *server,
  2716. struct cifs_readdata *rdata, unsigned int len)
  2717. {
  2718. int total_read = 0, result = 0;
  2719. unsigned int i;
  2720. u64 eof;
  2721. pgoff_t eof_index;
  2722. unsigned int nr_pages = rdata->nr_pages;
  2723. struct kvec iov;
  2724. /* determine the eof that the server (probably) has */
  2725. eof = CIFS_I(rdata->mapping->host)->server_eof;
  2726. eof_index = eof ? (eof - 1) >> PAGE_CACHE_SHIFT : 0;
  2727. cFYI(1, "eof=%llu eof_index=%lu", eof, eof_index);
  2728. rdata->tailsz = PAGE_CACHE_SIZE;
  2729. for (i = 0; i < nr_pages; i++) {
  2730. struct page *page = rdata->pages[i];
  2731. if (len >= PAGE_CACHE_SIZE) {
  2732. /* enough data to fill the page */
  2733. iov.iov_base = kmap(page);
  2734. iov.iov_len = PAGE_CACHE_SIZE;
  2735. cFYI(1, "%u: idx=%lu iov_base=%p iov_len=%zu",
  2736. i, page->index, iov.iov_base, iov.iov_len);
  2737. len -= PAGE_CACHE_SIZE;
  2738. } else if (len > 0) {
  2739. /* enough for partial page, fill and zero the rest */
  2740. iov.iov_base = kmap(page);
  2741. iov.iov_len = len;
  2742. cFYI(1, "%u: idx=%lu iov_base=%p iov_len=%zu",
  2743. i, page->index, iov.iov_base, iov.iov_len);
  2744. memset(iov.iov_base + len,
  2745. '\0', PAGE_CACHE_SIZE - len);
  2746. rdata->tailsz = len;
  2747. len = 0;
  2748. } else if (page->index > eof_index) {
  2749. /*
  2750. * The VFS will not try to do readahead past the
  2751. * i_size, but it's possible that we have outstanding
  2752. * writes with gaps in the middle and the i_size hasn't
  2753. * caught up yet. Populate those with zeroed out pages
  2754. * to prevent the VFS from repeatedly attempting to
  2755. * fill them until the writes are flushed.
  2756. */
  2757. zero_user(page, 0, PAGE_CACHE_SIZE);
  2758. lru_cache_add_file(page);
  2759. flush_dcache_page(page);
  2760. SetPageUptodate(page);
  2761. unlock_page(page);
  2762. page_cache_release(page);
  2763. rdata->pages[i] = NULL;
  2764. rdata->nr_pages--;
  2765. continue;
  2766. } else {
  2767. /* no need to hold page hostage */
  2768. lru_cache_add_file(page);
  2769. unlock_page(page);
  2770. page_cache_release(page);
  2771. rdata->pages[i] = NULL;
  2772. rdata->nr_pages--;
  2773. continue;
  2774. }
  2775. result = cifs_readv_from_socket(server, &iov, 1, iov.iov_len);
  2776. kunmap(page);
  2777. if (result < 0)
  2778. break;
  2779. total_read += result;
  2780. }
  2781. return total_read > 0 ? total_read : result;
  2782. }
  2783. static int cifs_readpages(struct file *file, struct address_space *mapping,
  2784. struct list_head *page_list, unsigned num_pages)
  2785. {
  2786. int rc;
  2787. struct list_head tmplist;
  2788. struct cifsFileInfo *open_file = file->private_data;
  2789. struct cifs_sb_info *cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
  2790. unsigned int rsize = cifs_sb->rsize;
  2791. pid_t pid;
  2792. /*
  2793. * Give up immediately if rsize is too small to read an entire page.
  2794. * The VFS will fall back to readpage. We should never reach this
  2795. * point however since we set ra_pages to 0 when the rsize is smaller
  2796. * than a cache page.
  2797. */
  2798. if (unlikely(rsize < PAGE_CACHE_SIZE))
  2799. return 0;
  2800. /*
  2801. * Reads as many pages as possible from fscache. Returns -ENOBUFS
  2802. * immediately if the cookie is negative
  2803. */
  2804. rc = cifs_readpages_from_fscache(mapping->host, mapping, page_list,
  2805. &num_pages);
  2806. if (rc == 0)
  2807. return rc;
  2808. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
  2809. pid = open_file->pid;
  2810. else
  2811. pid = current->tgid;
  2812. rc = 0;
  2813. INIT_LIST_HEAD(&tmplist);
  2814. cFYI(1, "%s: file=%p mapping=%p num_pages=%u", __func__, file,
  2815. mapping, num_pages);
  2816. /*
  2817. * Start with the page at end of list and move it to private
  2818. * list. Do the same with any following pages until we hit
  2819. * the rsize limit, hit an index discontinuity, or run out of
  2820. * pages. Issue the async read and then start the loop again
  2821. * until the list is empty.
  2822. *
  2823. * Note that list order is important. The page_list is in
  2824. * the order of declining indexes. When we put the pages in
  2825. * the rdata->pages, then we want them in increasing order.
  2826. */
  2827. while (!list_empty(page_list)) {
  2828. unsigned int i;
  2829. unsigned int bytes = PAGE_CACHE_SIZE;
  2830. unsigned int expected_index;
  2831. unsigned int nr_pages = 1;
  2832. loff_t offset;
  2833. struct page *page, *tpage;
  2834. struct cifs_readdata *rdata;
  2835. page = list_entry(page_list->prev, struct page, lru);
  2836. /*
  2837. * Lock the page and put it in the cache. Since no one else
  2838. * should have access to this page, we're safe to simply set
  2839. * PG_locked without checking it first.
  2840. */
  2841. __set_page_locked(page);
  2842. rc = add_to_page_cache_locked(page, mapping,
  2843. page->index, GFP_KERNEL);
  2844. /* give up if we can't stick it in the cache */
  2845. if (rc) {
  2846. __clear_page_locked(page);
  2847. break;
  2848. }
  2849. /* move first page to the tmplist */
  2850. offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
  2851. list_move_tail(&page->lru, &tmplist);
  2852. /* now try and add more pages onto the request */
  2853. expected_index = page->index + 1;
  2854. list_for_each_entry_safe_reverse(page, tpage, page_list, lru) {
  2855. /* discontinuity ? */
  2856. if (page->index != expected_index)
  2857. break;
  2858. /* would this page push the read over the rsize? */
  2859. if (bytes + PAGE_CACHE_SIZE > rsize)
  2860. break;
  2861. __set_page_locked(page);
  2862. if (add_to_page_cache_locked(page, mapping,
  2863. page->index, GFP_KERNEL)) {
  2864. __clear_page_locked(page);
  2865. break;
  2866. }
  2867. list_move_tail(&page->lru, &tmplist);
  2868. bytes += PAGE_CACHE_SIZE;
  2869. expected_index++;
  2870. nr_pages++;
  2871. }
  2872. rdata = cifs_readdata_alloc(nr_pages, cifs_readv_complete);
  2873. if (!rdata) {
  2874. /* best to give up if we're out of mem */
  2875. list_for_each_entry_safe(page, tpage, &tmplist, lru) {
  2876. list_del(&page->lru);
  2877. lru_cache_add_file(page);
  2878. unlock_page(page);
  2879. page_cache_release(page);
  2880. }
  2881. rc = -ENOMEM;
  2882. break;
  2883. }
  2884. rdata->cfile = cifsFileInfo_get(open_file);
  2885. rdata->mapping = mapping;
  2886. rdata->offset = offset;
  2887. rdata->bytes = bytes;
  2888. rdata->pid = pid;
  2889. rdata->pagesz = PAGE_CACHE_SIZE;
  2890. rdata->read_into_pages = cifs_readpages_read_into_pages;
  2891. list_for_each_entry_safe(page, tpage, &tmplist, lru) {
  2892. list_del(&page->lru);
  2893. rdata->pages[rdata->nr_pages++] = page;
  2894. }
  2895. rc = cifs_retry_async_readv(rdata);
  2896. if (rc != 0) {
  2897. for (i = 0; i < rdata->nr_pages; i++) {
  2898. page = rdata->pages[i];
  2899. lru_cache_add_file(page);
  2900. unlock_page(page);
  2901. page_cache_release(page);
  2902. }
  2903. kref_put(&rdata->refcount, cifs_readdata_release);
  2904. break;
  2905. }
  2906. kref_put(&rdata->refcount, cifs_readdata_release);
  2907. }
  2908. return rc;
  2909. }
  2910. static int cifs_readpage_worker(struct file *file, struct page *page,
  2911. loff_t *poffset)
  2912. {
  2913. char *read_data;
  2914. int rc;
  2915. /* Is the page cached? */
  2916. rc = cifs_readpage_from_fscache(file_inode(file), page);
  2917. if (rc == 0)
  2918. goto read_complete;
  2919. page_cache_get(page);
  2920. read_data = kmap(page);
  2921. /* for reads over a certain size could initiate async read ahead */
  2922. rc = cifs_read(file, read_data, PAGE_CACHE_SIZE, poffset);
  2923. if (rc < 0)
  2924. goto io_error;
  2925. else
  2926. cFYI(1, "Bytes read %d", rc);
  2927. file_inode(file)->i_atime =
  2928. current_fs_time(file_inode(file)->i_sb);
  2929. if (PAGE_CACHE_SIZE > rc)
  2930. memset(read_data + rc, 0, PAGE_CACHE_SIZE - rc);
  2931. flush_dcache_page(page);
  2932. SetPageUptodate(page);
  2933. /* send this page to the cache */
  2934. cifs_readpage_to_fscache(file_inode(file), page);
  2935. rc = 0;
  2936. io_error:
  2937. kunmap(page);
  2938. page_cache_release(page);
  2939. read_complete:
  2940. return rc;
  2941. }
  2942. static int cifs_readpage(struct file *file, struct page *page)
  2943. {
  2944. loff_t offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
  2945. int rc = -EACCES;
  2946. unsigned int xid;
  2947. xid = get_xid();
  2948. if (file->private_data == NULL) {
  2949. rc = -EBADF;
  2950. free_xid(xid);
  2951. return rc;
  2952. }
  2953. cFYI(1, "readpage %p at offset %d 0x%x",
  2954. page, (int)offset, (int)offset);
  2955. rc = cifs_readpage_worker(file, page, &offset);
  2956. unlock_page(page);
  2957. free_xid(xid);
  2958. return rc;
  2959. }
  2960. static int is_inode_writable(struct cifsInodeInfo *cifs_inode)
  2961. {
  2962. struct cifsFileInfo *open_file;
  2963. spin_lock(&cifs_file_list_lock);
  2964. list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
  2965. if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
  2966. spin_unlock(&cifs_file_list_lock);
  2967. return 1;
  2968. }
  2969. }
  2970. spin_unlock(&cifs_file_list_lock);
  2971. return 0;
  2972. }
  2973. /* We do not want to update the file size from server for inodes
  2974. open for write - to avoid races with writepage extending
  2975. the file - in the future we could consider allowing
  2976. refreshing the inode only on increases in the file size
  2977. but this is tricky to do without racing with writebehind
  2978. page caching in the current Linux kernel design */
  2979. bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file)
  2980. {
  2981. if (!cifsInode)
  2982. return true;
  2983. if (is_inode_writable(cifsInode)) {
  2984. /* This inode is open for write at least once */
  2985. struct cifs_sb_info *cifs_sb;
  2986. cifs_sb = CIFS_SB(cifsInode->vfs_inode.i_sb);
  2987. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
  2988. /* since no page cache to corrupt on directio
  2989. we can change size safely */
  2990. return true;
  2991. }
  2992. if (i_size_read(&cifsInode->vfs_inode) < end_of_file)
  2993. return true;
  2994. return false;
  2995. } else
  2996. return true;
  2997. }
  2998. static int cifs_write_begin(struct file *file, struct address_space *mapping,
  2999. loff_t pos, unsigned len, unsigned flags,
  3000. struct page **pagep, void **fsdata)
  3001. {
  3002. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  3003. loff_t offset = pos & (PAGE_CACHE_SIZE - 1);
  3004. loff_t page_start = pos & PAGE_MASK;
  3005. loff_t i_size;
  3006. struct page *page;
  3007. int rc = 0;
  3008. cFYI(1, "write_begin from %lld len %d", (long long)pos, len);
  3009. page = grab_cache_page_write_begin(mapping, index, flags);
  3010. if (!page) {
  3011. rc = -ENOMEM;
  3012. goto out;
  3013. }
  3014. if (PageUptodate(page))
  3015. goto out;
  3016. /*
  3017. * If we write a full page it will be up to date, no need to read from
  3018. * the server. If the write is short, we'll end up doing a sync write
  3019. * instead.
  3020. */
  3021. if (len == PAGE_CACHE_SIZE)
  3022. goto out;
  3023. /*
  3024. * optimize away the read when we have an oplock, and we're not
  3025. * expecting to use any of the data we'd be reading in. That
  3026. * is, when the page lies beyond the EOF, or straddles the EOF
  3027. * and the write will cover all of the existing data.
  3028. */
  3029. if (CIFS_I(mapping->host)->clientCanCacheRead) {
  3030. i_size = i_size_read(mapping->host);
  3031. if (page_start >= i_size ||
  3032. (offset == 0 && (pos + len) >= i_size)) {
  3033. zero_user_segments(page, 0, offset,
  3034. offset + len,
  3035. PAGE_CACHE_SIZE);
  3036. /*
  3037. * PageChecked means that the parts of the page
  3038. * to which we're not writing are considered up
  3039. * to date. Once the data is copied to the
  3040. * page, it can be set uptodate.
  3041. */
  3042. SetPageChecked(page);
  3043. goto out;
  3044. }
  3045. }
  3046. if ((file->f_flags & O_ACCMODE) != O_WRONLY) {
  3047. /*
  3048. * might as well read a page, it is fast enough. If we get
  3049. * an error, we don't need to return it. cifs_write_end will
  3050. * do a sync write instead since PG_uptodate isn't set.
  3051. */
  3052. cifs_readpage_worker(file, page, &page_start);
  3053. } else {
  3054. /* we could try using another file handle if there is one -
  3055. but how would we lock it to prevent close of that handle
  3056. racing with this read? In any case
  3057. this will be written out by write_end so is fine */
  3058. }
  3059. out:
  3060. *pagep = page;
  3061. return rc;
  3062. }
  3063. static int cifs_release_page(struct page *page, gfp_t gfp)
  3064. {
  3065. if (PagePrivate(page))
  3066. return 0;
  3067. return cifs_fscache_release_page(page, gfp);
  3068. }
  3069. static void cifs_invalidate_page(struct page *page, unsigned long offset)
  3070. {
  3071. struct cifsInodeInfo *cifsi = CIFS_I(page->mapping->host);
  3072. if (offset == 0)
  3073. cifs_fscache_invalidate_page(page, &cifsi->vfs_inode);
  3074. }
  3075. static int cifs_launder_page(struct page *page)
  3076. {
  3077. int rc = 0;
  3078. loff_t range_start = page_offset(page);
  3079. loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
  3080. struct writeback_control wbc = {
  3081. .sync_mode = WB_SYNC_ALL,
  3082. .nr_to_write = 0,
  3083. .range_start = range_start,
  3084. .range_end = range_end,
  3085. };
  3086. cFYI(1, "Launder page: %p", page);
  3087. if (clear_page_dirty_for_io(page))
  3088. rc = cifs_writepage_locked(page, &wbc);
  3089. cifs_fscache_invalidate_page(page, page->mapping->host);
  3090. return rc;
  3091. }
  3092. void cifs_oplock_break(struct work_struct *work)
  3093. {
  3094. struct cifsFileInfo *cfile = container_of(work, struct cifsFileInfo,
  3095. oplock_break);
  3096. struct inode *inode = cfile->dentry->d_inode;
  3097. struct cifsInodeInfo *cinode = CIFS_I(inode);
  3098. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  3099. int rc = 0;
  3100. if (!cinode->clientCanCacheAll && cinode->clientCanCacheRead &&
  3101. cifs_has_mand_locks(cinode)) {
  3102. cFYI(1, "Reset oplock to None for inode=%p due to mand locks",
  3103. inode);
  3104. cinode->clientCanCacheRead = false;
  3105. }
  3106. if (inode && S_ISREG(inode->i_mode)) {
  3107. if (cinode->clientCanCacheRead)
  3108. break_lease(inode, O_RDONLY);
  3109. else
  3110. break_lease(inode, O_WRONLY);
  3111. rc = filemap_fdatawrite(inode->i_mapping);
  3112. if (cinode->clientCanCacheRead == 0) {
  3113. rc = filemap_fdatawait(inode->i_mapping);
  3114. mapping_set_error(inode->i_mapping, rc);
  3115. cifs_invalidate_mapping(inode);
  3116. }
  3117. cFYI(1, "Oplock flush inode %p rc %d", inode, rc);
  3118. }
  3119. rc = cifs_push_locks(cfile);
  3120. if (rc)
  3121. cERROR(1, "Push locks rc = %d", rc);
  3122. /*
  3123. * releasing stale oplock after recent reconnect of smb session using
  3124. * a now incorrect file handle is not a data integrity issue but do
  3125. * not bother sending an oplock release if session to server still is
  3126. * disconnected since oplock already released by the server
  3127. */
  3128. if (!cfile->oplock_break_cancelled) {
  3129. rc = tcon->ses->server->ops->oplock_response(tcon, &cfile->fid,
  3130. cinode);
  3131. cFYI(1, "Oplock release rc = %d", rc);
  3132. }
  3133. }
  3134. const struct address_space_operations cifs_addr_ops = {
  3135. .readpage = cifs_readpage,
  3136. .readpages = cifs_readpages,
  3137. .writepage = cifs_writepage,
  3138. .writepages = cifs_writepages,
  3139. .write_begin = cifs_write_begin,
  3140. .write_end = cifs_write_end,
  3141. .set_page_dirty = __set_page_dirty_nobuffers,
  3142. .releasepage = cifs_release_page,
  3143. .invalidatepage = cifs_invalidate_page,
  3144. .launder_page = cifs_launder_page,
  3145. };
  3146. /*
  3147. * cifs_readpages requires the server to support a buffer large enough to
  3148. * contain the header plus one complete page of data. Otherwise, we need
  3149. * to leave cifs_readpages out of the address space operations.
  3150. */
  3151. const struct address_space_operations cifs_addr_ops_smallbuf = {
  3152. .readpage = cifs_readpage,
  3153. .writepage = cifs_writepage,
  3154. .writepages = cifs_writepages,
  3155. .write_begin = cifs_write_begin,
  3156. .write_end = cifs_write_end,
  3157. .set_page_dirty = __set_page_dirty_nobuffers,
  3158. .releasepage = cifs_release_page,
  3159. .invalidatepage = cifs_invalidate_page,
  3160. .launder_page = cifs_launder_page,
  3161. };