file.c 102 KB

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