file.c 101 KB

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