file.c 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281
  1. // SPDX-License-Identifier: LGPL-2.1
  2. /*
  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. */
  11. #include <linux/fs.h>
  12. #include <linux/filelock.h>
  13. #include <linux/backing-dev.h>
  14. #include <linux/stat.h>
  15. #include <linux/fcntl.h>
  16. #include <linux/pagemap.h>
  17. #include <linux/pagevec.h>
  18. #include <linux/writeback.h>
  19. #include <linux/task_io_accounting_ops.h>
  20. #include <linux/delay.h>
  21. #include <linux/mount.h>
  22. #include <linux/slab.h>
  23. #include <linux/swap.h>
  24. #include <linux/mm.h>
  25. #include <asm/div64.h>
  26. #include "cifsfs.h"
  27. #include "cifspdu.h"
  28. #include "cifsglob.h"
  29. #include "cifsproto.h"
  30. #include "smb2proto.h"
  31. #include "cifs_unicode.h"
  32. #include "cifs_debug.h"
  33. #include "cifs_fs_sb.h"
  34. #include "fscache.h"
  35. #include "smbdirect.h"
  36. #include "fs_context.h"
  37. #include "cifs_ioctl.h"
  38. #include "cached_dir.h"
  39. #include <trace/events/netfs.h>
  40. static int cifs_reopen_file(struct cifsFileInfo *cfile, bool can_flush);
  41. /*
  42. * Prepare a subrequest to upload to the server. We need to allocate credits
  43. * so that we know the maximum amount of data that we can include in it.
  44. */
  45. static void cifs_prepare_write(struct netfs_io_subrequest *subreq)
  46. {
  47. struct cifs_io_subrequest *wdata =
  48. container_of(subreq, struct cifs_io_subrequest, subreq);
  49. struct cifs_io_request *req = wdata->req;
  50. struct netfs_io_stream *stream = &req->rreq.io_streams[subreq->stream_nr];
  51. struct TCP_Server_Info *server;
  52. struct cifsFileInfo *open_file = req->cfile;
  53. size_t wsize = req->rreq.wsize;
  54. int rc;
  55. if (!wdata->have_xid) {
  56. wdata->xid = get_xid();
  57. wdata->have_xid = true;
  58. }
  59. server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses);
  60. wdata->server = server;
  61. retry:
  62. if (open_file->invalidHandle) {
  63. rc = cifs_reopen_file(open_file, false);
  64. if (rc < 0) {
  65. if (rc == -EAGAIN)
  66. goto retry;
  67. subreq->error = rc;
  68. return netfs_prepare_write_failed(subreq);
  69. }
  70. }
  71. rc = server->ops->wait_mtu_credits(server, wsize, &stream->sreq_max_len,
  72. &wdata->credits);
  73. if (rc < 0) {
  74. subreq->error = rc;
  75. return netfs_prepare_write_failed(subreq);
  76. }
  77. wdata->credits.rreq_debug_id = subreq->rreq->debug_id;
  78. wdata->credits.rreq_debug_index = subreq->debug_index;
  79. wdata->credits.in_flight_check = 1;
  80. trace_smb3_rw_credits(wdata->rreq->debug_id,
  81. wdata->subreq.debug_index,
  82. wdata->credits.value,
  83. server->credits, server->in_flight,
  84. wdata->credits.value,
  85. cifs_trace_rw_credits_write_prepare);
  86. #ifdef CONFIG_CIFS_SMB_DIRECT
  87. if (server->smbd_conn)
  88. stream->sreq_max_segs = server->smbd_conn->max_frmr_depth;
  89. #endif
  90. }
  91. /*
  92. * Issue a subrequest to upload to the server.
  93. */
  94. static void cifs_issue_write(struct netfs_io_subrequest *subreq)
  95. {
  96. struct cifs_io_subrequest *wdata =
  97. container_of(subreq, struct cifs_io_subrequest, subreq);
  98. struct cifs_sb_info *sbi = CIFS_SB(subreq->rreq->inode->i_sb);
  99. int rc;
  100. if (cifs_forced_shutdown(sbi)) {
  101. rc = -EIO;
  102. goto fail;
  103. }
  104. rc = adjust_credits(wdata->server, wdata, cifs_trace_rw_credits_issue_write_adjust);
  105. if (rc)
  106. goto fail;
  107. rc = -EAGAIN;
  108. if (wdata->req->cfile->invalidHandle)
  109. goto fail;
  110. wdata->server->ops->async_writev(wdata);
  111. out:
  112. return;
  113. fail:
  114. if (rc == -EAGAIN)
  115. trace_netfs_sreq(subreq, netfs_sreq_trace_retry);
  116. else
  117. trace_netfs_sreq(subreq, netfs_sreq_trace_fail);
  118. add_credits_and_wake_if(wdata->server, &wdata->credits, 0);
  119. cifs_write_subrequest_terminated(wdata, rc, false);
  120. goto out;
  121. }
  122. static void cifs_netfs_invalidate_cache(struct netfs_io_request *wreq)
  123. {
  124. cifs_invalidate_cache(wreq->inode, 0);
  125. }
  126. /*
  127. * Negotiate the size of a read operation on behalf of the netfs library.
  128. */
  129. static int cifs_prepare_read(struct netfs_io_subrequest *subreq)
  130. {
  131. struct netfs_io_request *rreq = subreq->rreq;
  132. struct cifs_io_subrequest *rdata = container_of(subreq, struct cifs_io_subrequest, subreq);
  133. struct cifs_io_request *req = container_of(subreq->rreq, struct cifs_io_request, rreq);
  134. struct TCP_Server_Info *server;
  135. struct cifs_sb_info *cifs_sb = CIFS_SB(rreq->inode->i_sb);
  136. size_t size;
  137. int rc = 0;
  138. if (!rdata->have_xid) {
  139. rdata->xid = get_xid();
  140. rdata->have_xid = true;
  141. }
  142. server = cifs_pick_channel(tlink_tcon(req->cfile->tlink)->ses);
  143. rdata->server = server;
  144. if (cifs_sb->ctx->rsize == 0)
  145. cifs_sb->ctx->rsize =
  146. server->ops->negotiate_rsize(tlink_tcon(req->cfile->tlink),
  147. cifs_sb->ctx);
  148. rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->rsize,
  149. &size, &rdata->credits);
  150. if (rc)
  151. return rc;
  152. rreq->io_streams[0].sreq_max_len = size;
  153. rdata->credits.in_flight_check = 1;
  154. rdata->credits.rreq_debug_id = rreq->debug_id;
  155. rdata->credits.rreq_debug_index = subreq->debug_index;
  156. trace_smb3_rw_credits(rdata->rreq->debug_id,
  157. rdata->subreq.debug_index,
  158. rdata->credits.value,
  159. server->credits, server->in_flight, 0,
  160. cifs_trace_rw_credits_read_submit);
  161. #ifdef CONFIG_CIFS_SMB_DIRECT
  162. if (server->smbd_conn)
  163. rreq->io_streams[0].sreq_max_segs = server->smbd_conn->max_frmr_depth;
  164. #endif
  165. return 0;
  166. }
  167. /*
  168. * Issue a read operation on behalf of the netfs helper functions. We're asked
  169. * to make a read of a certain size at a point in the file. We are permitted
  170. * to only read a portion of that, but as long as we read something, the netfs
  171. * helper will call us again so that we can issue another read.
  172. */
  173. static void cifs_issue_read(struct netfs_io_subrequest *subreq)
  174. {
  175. struct netfs_io_request *rreq = subreq->rreq;
  176. struct cifs_io_subrequest *rdata = container_of(subreq, struct cifs_io_subrequest, subreq);
  177. struct cifs_io_request *req = container_of(subreq->rreq, struct cifs_io_request, rreq);
  178. struct TCP_Server_Info *server = rdata->server;
  179. int rc = 0;
  180. cifs_dbg(FYI, "%s: op=%08x[%x] mapping=%p len=%zu/%zu\n",
  181. __func__, rreq->debug_id, subreq->debug_index, rreq->mapping,
  182. subreq->transferred, subreq->len);
  183. rc = adjust_credits(server, rdata, cifs_trace_rw_credits_issue_read_adjust);
  184. if (rc)
  185. goto failed;
  186. if (req->cfile->invalidHandle) {
  187. do {
  188. rc = cifs_reopen_file(req->cfile, true);
  189. } while (rc == -EAGAIN);
  190. if (rc)
  191. goto failed;
  192. }
  193. if (subreq->rreq->origin != NETFS_DIO_READ)
  194. __set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
  195. trace_netfs_sreq(subreq, netfs_sreq_trace_submit);
  196. rc = rdata->server->ops->async_readv(rdata);
  197. if (rc)
  198. goto failed;
  199. return;
  200. failed:
  201. netfs_read_subreq_terminated(subreq, rc, false);
  202. }
  203. /*
  204. * Writeback calls this when it finds a folio that needs uploading. This isn't
  205. * called if writeback only has copy-to-cache to deal with.
  206. */
  207. static void cifs_begin_writeback(struct netfs_io_request *wreq)
  208. {
  209. struct cifs_io_request *req = container_of(wreq, struct cifs_io_request, rreq);
  210. int ret;
  211. ret = cifs_get_writable_file(CIFS_I(wreq->inode), FIND_WR_ANY, &req->cfile);
  212. if (ret) {
  213. cifs_dbg(VFS, "No writable handle in writepages ret=%d\n", ret);
  214. return;
  215. }
  216. wreq->io_streams[0].avail = true;
  217. }
  218. /*
  219. * Initialise a request.
  220. */
  221. static int cifs_init_request(struct netfs_io_request *rreq, struct file *file)
  222. {
  223. struct cifs_io_request *req = container_of(rreq, struct cifs_io_request, rreq);
  224. struct cifs_sb_info *cifs_sb = CIFS_SB(rreq->inode->i_sb);
  225. struct cifsFileInfo *open_file = NULL;
  226. rreq->rsize = cifs_sb->ctx->rsize;
  227. rreq->wsize = cifs_sb->ctx->wsize;
  228. req->pid = current->tgid; // Ummm... This may be a workqueue
  229. if (file) {
  230. open_file = file->private_data;
  231. rreq->netfs_priv = file->private_data;
  232. req->cfile = cifsFileInfo_get(open_file);
  233. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
  234. req->pid = req->cfile->pid;
  235. } else if (rreq->origin != NETFS_WRITEBACK) {
  236. WARN_ON_ONCE(1);
  237. return -EIO;
  238. }
  239. return 0;
  240. }
  241. /*
  242. * Completion of a request operation.
  243. */
  244. static void cifs_rreq_done(struct netfs_io_request *rreq)
  245. {
  246. struct timespec64 atime, mtime;
  247. struct inode *inode = rreq->inode;
  248. /* we do not want atime to be less than mtime, it broke some apps */
  249. atime = inode_set_atime_to_ts(inode, current_time(inode));
  250. mtime = inode_get_mtime(inode);
  251. if (timespec64_compare(&atime, &mtime))
  252. inode_set_atime_to_ts(inode, inode_get_mtime(inode));
  253. }
  254. static void cifs_free_request(struct netfs_io_request *rreq)
  255. {
  256. struct cifs_io_request *req = container_of(rreq, struct cifs_io_request, rreq);
  257. if (req->cfile)
  258. cifsFileInfo_put(req->cfile);
  259. }
  260. static void cifs_free_subrequest(struct netfs_io_subrequest *subreq)
  261. {
  262. struct cifs_io_subrequest *rdata =
  263. container_of(subreq, struct cifs_io_subrequest, subreq);
  264. int rc = subreq->error;
  265. if (rdata->subreq.source == NETFS_DOWNLOAD_FROM_SERVER) {
  266. #ifdef CONFIG_CIFS_SMB_DIRECT
  267. if (rdata->mr) {
  268. smbd_deregister_mr(rdata->mr);
  269. rdata->mr = NULL;
  270. }
  271. #endif
  272. }
  273. if (rdata->credits.value != 0) {
  274. trace_smb3_rw_credits(rdata->rreq->debug_id,
  275. rdata->subreq.debug_index,
  276. rdata->credits.value,
  277. rdata->server ? rdata->server->credits : 0,
  278. rdata->server ? rdata->server->in_flight : 0,
  279. -rdata->credits.value,
  280. cifs_trace_rw_credits_free_subreq);
  281. if (rdata->server)
  282. add_credits_and_wake_if(rdata->server, &rdata->credits, 0);
  283. else
  284. rdata->credits.value = 0;
  285. }
  286. if (rdata->have_xid)
  287. free_xid(rdata->xid);
  288. }
  289. const struct netfs_request_ops cifs_req_ops = {
  290. .request_pool = &cifs_io_request_pool,
  291. .subrequest_pool = &cifs_io_subrequest_pool,
  292. .init_request = cifs_init_request,
  293. .free_request = cifs_free_request,
  294. .free_subrequest = cifs_free_subrequest,
  295. .prepare_read = cifs_prepare_read,
  296. .issue_read = cifs_issue_read,
  297. .done = cifs_rreq_done,
  298. .begin_writeback = cifs_begin_writeback,
  299. .prepare_write = cifs_prepare_write,
  300. .issue_write = cifs_issue_write,
  301. .invalidate_cache = cifs_netfs_invalidate_cache,
  302. };
  303. /*
  304. * Mark as invalid, all open files on tree connections since they
  305. * were closed when session to server was lost.
  306. */
  307. void
  308. cifs_mark_open_files_invalid(struct cifs_tcon *tcon)
  309. {
  310. struct cifsFileInfo *open_file = NULL;
  311. struct list_head *tmp;
  312. struct list_head *tmp1;
  313. /* only send once per connect */
  314. spin_lock(&tcon->tc_lock);
  315. if (tcon->need_reconnect)
  316. tcon->status = TID_NEED_RECON;
  317. if (tcon->status != TID_NEED_RECON) {
  318. spin_unlock(&tcon->tc_lock);
  319. return;
  320. }
  321. tcon->status = TID_IN_FILES_INVALIDATE;
  322. spin_unlock(&tcon->tc_lock);
  323. /* list all files open on tree connection and mark them invalid */
  324. spin_lock(&tcon->open_file_lock);
  325. list_for_each_safe(tmp, tmp1, &tcon->openFileList) {
  326. open_file = list_entry(tmp, struct cifsFileInfo, tlist);
  327. open_file->invalidHandle = true;
  328. open_file->oplock_break_cancelled = true;
  329. }
  330. spin_unlock(&tcon->open_file_lock);
  331. invalidate_all_cached_dirs(tcon);
  332. spin_lock(&tcon->tc_lock);
  333. if (tcon->status == TID_IN_FILES_INVALIDATE)
  334. tcon->status = TID_NEED_TCON;
  335. spin_unlock(&tcon->tc_lock);
  336. /*
  337. * BB Add call to invalidate_inodes(sb) for all superblocks mounted
  338. * to this tcon.
  339. */
  340. }
  341. static inline int cifs_convert_flags(unsigned int flags, int rdwr_for_fscache)
  342. {
  343. if ((flags & O_ACCMODE) == O_RDONLY)
  344. return GENERIC_READ;
  345. else if ((flags & O_ACCMODE) == O_WRONLY)
  346. return rdwr_for_fscache == 1 ? (GENERIC_READ | GENERIC_WRITE) : GENERIC_WRITE;
  347. else if ((flags & O_ACCMODE) == O_RDWR) {
  348. /* GENERIC_ALL is too much permission to request
  349. can cause unnecessary access denied on create */
  350. /* return GENERIC_ALL; */
  351. return (GENERIC_READ | GENERIC_WRITE);
  352. }
  353. return (READ_CONTROL | FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES |
  354. FILE_WRITE_EA | FILE_APPEND_DATA | FILE_WRITE_DATA |
  355. FILE_READ_DATA);
  356. }
  357. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  358. static u32 cifs_posix_convert_flags(unsigned int flags)
  359. {
  360. u32 posix_flags = 0;
  361. if ((flags & O_ACCMODE) == O_RDONLY)
  362. posix_flags = SMB_O_RDONLY;
  363. else if ((flags & O_ACCMODE) == O_WRONLY)
  364. posix_flags = SMB_O_WRONLY;
  365. else if ((flags & O_ACCMODE) == O_RDWR)
  366. posix_flags = SMB_O_RDWR;
  367. if (flags & O_CREAT) {
  368. posix_flags |= SMB_O_CREAT;
  369. if (flags & O_EXCL)
  370. posix_flags |= SMB_O_EXCL;
  371. } else if (flags & O_EXCL)
  372. cifs_dbg(FYI, "Application %s pid %d has incorrectly set O_EXCL flag but not O_CREAT on file open. Ignoring O_EXCL\n",
  373. current->comm, current->tgid);
  374. if (flags & O_TRUNC)
  375. posix_flags |= SMB_O_TRUNC;
  376. /* be safe and imply O_SYNC for O_DSYNC */
  377. if (flags & O_DSYNC)
  378. posix_flags |= SMB_O_SYNC;
  379. if (flags & O_DIRECTORY)
  380. posix_flags |= SMB_O_DIRECTORY;
  381. if (flags & O_NOFOLLOW)
  382. posix_flags |= SMB_O_NOFOLLOW;
  383. if (flags & O_DIRECT)
  384. posix_flags |= SMB_O_DIRECT;
  385. return posix_flags;
  386. }
  387. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  388. static inline int cifs_get_disposition(unsigned int flags)
  389. {
  390. if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
  391. return FILE_CREATE;
  392. else if ((flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
  393. return FILE_OVERWRITE_IF;
  394. else if ((flags & O_CREAT) == O_CREAT)
  395. return FILE_OPEN_IF;
  396. else if ((flags & O_TRUNC) == O_TRUNC)
  397. return FILE_OVERWRITE;
  398. else
  399. return FILE_OPEN;
  400. }
  401. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  402. int cifs_posix_open(const char *full_path, struct inode **pinode,
  403. struct super_block *sb, int mode, unsigned int f_flags,
  404. __u32 *poplock, __u16 *pnetfid, unsigned int xid)
  405. {
  406. int rc;
  407. FILE_UNIX_BASIC_INFO *presp_data;
  408. __u32 posix_flags = 0;
  409. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  410. struct cifs_fattr fattr;
  411. struct tcon_link *tlink;
  412. struct cifs_tcon *tcon;
  413. cifs_dbg(FYI, "posix open %s\n", full_path);
  414. presp_data = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
  415. if (presp_data == NULL)
  416. return -ENOMEM;
  417. tlink = cifs_sb_tlink(cifs_sb);
  418. if (IS_ERR(tlink)) {
  419. rc = PTR_ERR(tlink);
  420. goto posix_open_ret;
  421. }
  422. tcon = tlink_tcon(tlink);
  423. mode &= ~current_umask();
  424. posix_flags = cifs_posix_convert_flags(f_flags);
  425. rc = CIFSPOSIXCreate(xid, tcon, posix_flags, mode, pnetfid, presp_data,
  426. poplock, full_path, cifs_sb->local_nls,
  427. cifs_remap(cifs_sb));
  428. cifs_put_tlink(tlink);
  429. if (rc)
  430. goto posix_open_ret;
  431. if (presp_data->Type == cpu_to_le32(-1))
  432. goto posix_open_ret; /* open ok, caller does qpathinfo */
  433. if (!pinode)
  434. goto posix_open_ret; /* caller does not need info */
  435. cifs_unix_basic_to_fattr(&fattr, presp_data, cifs_sb);
  436. /* get new inode and set it up */
  437. if (*pinode == NULL) {
  438. cifs_fill_uniqueid(sb, &fattr);
  439. *pinode = cifs_iget(sb, &fattr);
  440. if (!*pinode) {
  441. rc = -ENOMEM;
  442. goto posix_open_ret;
  443. }
  444. } else {
  445. cifs_revalidate_mapping(*pinode);
  446. rc = cifs_fattr_to_inode(*pinode, &fattr, false);
  447. }
  448. posix_open_ret:
  449. kfree(presp_data);
  450. return rc;
  451. }
  452. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  453. static int cifs_nt_open(const char *full_path, struct inode *inode, struct cifs_sb_info *cifs_sb,
  454. struct cifs_tcon *tcon, unsigned int f_flags, __u32 *oplock,
  455. struct cifs_fid *fid, unsigned int xid, struct cifs_open_info_data *buf)
  456. {
  457. int rc;
  458. int desired_access;
  459. int disposition;
  460. int create_options = CREATE_NOT_DIR;
  461. struct TCP_Server_Info *server = tcon->ses->server;
  462. struct cifs_open_parms oparms;
  463. int rdwr_for_fscache = 0;
  464. if (!server->ops->open)
  465. return -ENOSYS;
  466. /* If we're caching, we need to be able to fill in around partial writes. */
  467. if (cifs_fscache_enabled(inode) && (f_flags & O_ACCMODE) == O_WRONLY)
  468. rdwr_for_fscache = 1;
  469. desired_access = cifs_convert_flags(f_flags, rdwr_for_fscache);
  470. /*********************************************************************
  471. * open flag mapping table:
  472. *
  473. * POSIX Flag CIFS Disposition
  474. * ---------- ----------------
  475. * O_CREAT FILE_OPEN_IF
  476. * O_CREAT | O_EXCL FILE_CREATE
  477. * O_CREAT | O_TRUNC FILE_OVERWRITE_IF
  478. * O_TRUNC FILE_OVERWRITE
  479. * none of the above FILE_OPEN
  480. *
  481. * Note that there is not a direct match between disposition
  482. * FILE_SUPERSEDE (ie create whether or not file exists although
  483. * O_CREAT | O_TRUNC is similar but truncates the existing
  484. * file rather than creating a new file as FILE_SUPERSEDE does
  485. * (which uses the attributes / metadata passed in on open call)
  486. *?
  487. *? O_SYNC is a reasonable match to CIFS writethrough flag
  488. *? and the read write flags match reasonably. O_LARGEFILE
  489. *? is irrelevant because largefile support is always used
  490. *? by this client. Flags O_APPEND, O_DIRECT, O_DIRECTORY,
  491. * O_FASYNC, O_NOFOLLOW, O_NONBLOCK need further investigation
  492. *********************************************************************/
  493. disposition = cifs_get_disposition(f_flags);
  494. /* BB pass O_SYNC flag through on file attributes .. BB */
  495. /* O_SYNC also has bit for O_DSYNC so following check picks up either */
  496. if (f_flags & O_SYNC)
  497. create_options |= CREATE_WRITE_THROUGH;
  498. if (f_flags & O_DIRECT)
  499. create_options |= CREATE_NO_BUFFER;
  500. retry_open:
  501. oparms = (struct cifs_open_parms) {
  502. .tcon = tcon,
  503. .cifs_sb = cifs_sb,
  504. .desired_access = desired_access,
  505. .create_options = cifs_create_options(cifs_sb, create_options),
  506. .disposition = disposition,
  507. .path = full_path,
  508. .fid = fid,
  509. };
  510. rc = server->ops->open(xid, &oparms, oplock, buf);
  511. if (rc) {
  512. if (rc == -EACCES && rdwr_for_fscache == 1) {
  513. desired_access = cifs_convert_flags(f_flags, 0);
  514. rdwr_for_fscache = 2;
  515. goto retry_open;
  516. }
  517. return rc;
  518. }
  519. if (rdwr_for_fscache == 2)
  520. cifs_invalidate_cache(inode, FSCACHE_INVAL_DIO_WRITE);
  521. /* TODO: Add support for calling posix query info but with passing in fid */
  522. if (tcon->unix_ext)
  523. rc = cifs_get_inode_info_unix(&inode, full_path, inode->i_sb,
  524. xid);
  525. else
  526. rc = cifs_get_inode_info(&inode, full_path, buf, inode->i_sb,
  527. xid, fid);
  528. if (rc) {
  529. server->ops->close(xid, tcon, fid);
  530. if (rc == -ESTALE)
  531. rc = -EOPENSTALE;
  532. }
  533. return rc;
  534. }
  535. static bool
  536. cifs_has_mand_locks(struct cifsInodeInfo *cinode)
  537. {
  538. struct cifs_fid_locks *cur;
  539. bool has_locks = false;
  540. down_read(&cinode->lock_sem);
  541. list_for_each_entry(cur, &cinode->llist, llist) {
  542. if (!list_empty(&cur->locks)) {
  543. has_locks = true;
  544. break;
  545. }
  546. }
  547. up_read(&cinode->lock_sem);
  548. return has_locks;
  549. }
  550. void
  551. cifs_down_write(struct rw_semaphore *sem)
  552. {
  553. while (!down_write_trylock(sem))
  554. msleep(10);
  555. }
  556. static void cifsFileInfo_put_work(struct work_struct *work);
  557. void serverclose_work(struct work_struct *work);
  558. struct cifsFileInfo *cifs_new_fileinfo(struct cifs_fid *fid, struct file *file,
  559. struct tcon_link *tlink, __u32 oplock,
  560. const char *symlink_target)
  561. {
  562. struct dentry *dentry = file_dentry(file);
  563. struct inode *inode = d_inode(dentry);
  564. struct cifsInodeInfo *cinode = CIFS_I(inode);
  565. struct cifsFileInfo *cfile;
  566. struct cifs_fid_locks *fdlocks;
  567. struct cifs_tcon *tcon = tlink_tcon(tlink);
  568. struct TCP_Server_Info *server = tcon->ses->server;
  569. cfile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
  570. if (cfile == NULL)
  571. return cfile;
  572. fdlocks = kzalloc(sizeof(struct cifs_fid_locks), GFP_KERNEL);
  573. if (!fdlocks) {
  574. kfree(cfile);
  575. return NULL;
  576. }
  577. if (symlink_target) {
  578. cfile->symlink_target = kstrdup(symlink_target, GFP_KERNEL);
  579. if (!cfile->symlink_target) {
  580. kfree(fdlocks);
  581. kfree(cfile);
  582. return NULL;
  583. }
  584. }
  585. INIT_LIST_HEAD(&fdlocks->locks);
  586. fdlocks->cfile = cfile;
  587. cfile->llist = fdlocks;
  588. cfile->count = 1;
  589. cfile->pid = current->tgid;
  590. cfile->uid = current_fsuid();
  591. cfile->dentry = dget(dentry);
  592. cfile->f_flags = file->f_flags;
  593. cfile->invalidHandle = false;
  594. cfile->deferred_close_scheduled = false;
  595. cfile->tlink = cifs_get_tlink(tlink);
  596. INIT_WORK(&cfile->oplock_break, cifs_oplock_break);
  597. INIT_WORK(&cfile->put, cifsFileInfo_put_work);
  598. INIT_WORK(&cfile->serverclose, serverclose_work);
  599. INIT_DELAYED_WORK(&cfile->deferred, smb2_deferred_work_close);
  600. mutex_init(&cfile->fh_mutex);
  601. spin_lock_init(&cfile->file_info_lock);
  602. cifs_sb_active(inode->i_sb);
  603. /*
  604. * If the server returned a read oplock and we have mandatory brlocks,
  605. * set oplock level to None.
  606. */
  607. if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) {
  608. cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n");
  609. oplock = 0;
  610. }
  611. cifs_down_write(&cinode->lock_sem);
  612. list_add(&fdlocks->llist, &cinode->llist);
  613. up_write(&cinode->lock_sem);
  614. spin_lock(&tcon->open_file_lock);
  615. if (fid->pending_open->oplock != CIFS_OPLOCK_NO_CHANGE && oplock)
  616. oplock = fid->pending_open->oplock;
  617. list_del(&fid->pending_open->olist);
  618. fid->purge_cache = false;
  619. server->ops->set_fid(cfile, fid, oplock);
  620. list_add(&cfile->tlist, &tcon->openFileList);
  621. atomic_inc(&tcon->num_local_opens);
  622. /* if readable file instance put first in list*/
  623. spin_lock(&cinode->open_file_lock);
  624. if (file->f_mode & FMODE_READ)
  625. list_add(&cfile->flist, &cinode->openFileList);
  626. else
  627. list_add_tail(&cfile->flist, &cinode->openFileList);
  628. spin_unlock(&cinode->open_file_lock);
  629. spin_unlock(&tcon->open_file_lock);
  630. if (fid->purge_cache)
  631. cifs_zap_mapping(inode);
  632. file->private_data = cfile;
  633. return cfile;
  634. }
  635. struct cifsFileInfo *
  636. cifsFileInfo_get(struct cifsFileInfo *cifs_file)
  637. {
  638. spin_lock(&cifs_file->file_info_lock);
  639. cifsFileInfo_get_locked(cifs_file);
  640. spin_unlock(&cifs_file->file_info_lock);
  641. return cifs_file;
  642. }
  643. static void cifsFileInfo_put_final(struct cifsFileInfo *cifs_file)
  644. {
  645. struct inode *inode = d_inode(cifs_file->dentry);
  646. struct cifsInodeInfo *cifsi = CIFS_I(inode);
  647. struct cifsLockInfo *li, *tmp;
  648. struct super_block *sb = inode->i_sb;
  649. /*
  650. * Delete any outstanding lock records. We'll lose them when the file
  651. * is closed anyway.
  652. */
  653. cifs_down_write(&cifsi->lock_sem);
  654. list_for_each_entry_safe(li, tmp, &cifs_file->llist->locks, llist) {
  655. list_del(&li->llist);
  656. cifs_del_lock_waiters(li);
  657. kfree(li);
  658. }
  659. list_del(&cifs_file->llist->llist);
  660. kfree(cifs_file->llist);
  661. up_write(&cifsi->lock_sem);
  662. cifs_put_tlink(cifs_file->tlink);
  663. dput(cifs_file->dentry);
  664. cifs_sb_deactive(sb);
  665. kfree(cifs_file->symlink_target);
  666. kfree(cifs_file);
  667. }
  668. static void cifsFileInfo_put_work(struct work_struct *work)
  669. {
  670. struct cifsFileInfo *cifs_file = container_of(work,
  671. struct cifsFileInfo, put);
  672. cifsFileInfo_put_final(cifs_file);
  673. }
  674. void serverclose_work(struct work_struct *work)
  675. {
  676. struct cifsFileInfo *cifs_file = container_of(work,
  677. struct cifsFileInfo, serverclose);
  678. struct cifs_tcon *tcon = tlink_tcon(cifs_file->tlink);
  679. struct TCP_Server_Info *server = tcon->ses->server;
  680. int rc = 0;
  681. int retries = 0;
  682. int MAX_RETRIES = 4;
  683. do {
  684. if (server->ops->close_getattr)
  685. rc = server->ops->close_getattr(0, tcon, cifs_file);
  686. else if (server->ops->close)
  687. rc = server->ops->close(0, tcon, &cifs_file->fid);
  688. if (rc == -EBUSY || rc == -EAGAIN) {
  689. retries++;
  690. msleep(250);
  691. }
  692. } while ((rc == -EBUSY || rc == -EAGAIN) && (retries < MAX_RETRIES)
  693. );
  694. if (retries == MAX_RETRIES)
  695. pr_warn("Serverclose failed %d times, giving up\n", MAX_RETRIES);
  696. if (cifs_file->offload)
  697. queue_work(fileinfo_put_wq, &cifs_file->put);
  698. else
  699. cifsFileInfo_put_final(cifs_file);
  700. }
  701. /**
  702. * cifsFileInfo_put - release a reference of file priv data
  703. *
  704. * Always potentially wait for oplock handler. See _cifsFileInfo_put().
  705. *
  706. * @cifs_file: cifs/smb3 specific info (eg refcounts) for an open file
  707. */
  708. void cifsFileInfo_put(struct cifsFileInfo *cifs_file)
  709. {
  710. _cifsFileInfo_put(cifs_file, true, true);
  711. }
  712. /**
  713. * _cifsFileInfo_put - release a reference of file priv data
  714. *
  715. * This may involve closing the filehandle @cifs_file out on the
  716. * server. Must be called without holding tcon->open_file_lock,
  717. * cinode->open_file_lock and cifs_file->file_info_lock.
  718. *
  719. * If @wait_for_oplock_handler is true and we are releasing the last
  720. * reference, wait for any running oplock break handler of the file
  721. * and cancel any pending one.
  722. *
  723. * @cifs_file: cifs/smb3 specific info (eg refcounts) for an open file
  724. * @wait_oplock_handler: must be false if called from oplock_break_handler
  725. * @offload: not offloaded on close and oplock breaks
  726. *
  727. */
  728. void _cifsFileInfo_put(struct cifsFileInfo *cifs_file,
  729. bool wait_oplock_handler, bool offload)
  730. {
  731. struct inode *inode = d_inode(cifs_file->dentry);
  732. struct cifs_tcon *tcon = tlink_tcon(cifs_file->tlink);
  733. struct TCP_Server_Info *server = tcon->ses->server;
  734. struct cifsInodeInfo *cifsi = CIFS_I(inode);
  735. struct super_block *sb = inode->i_sb;
  736. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  737. struct cifs_fid fid = {};
  738. struct cifs_pending_open open;
  739. bool oplock_break_cancelled;
  740. bool serverclose_offloaded = false;
  741. spin_lock(&tcon->open_file_lock);
  742. spin_lock(&cifsi->open_file_lock);
  743. spin_lock(&cifs_file->file_info_lock);
  744. cifs_file->offload = offload;
  745. if (--cifs_file->count > 0) {
  746. spin_unlock(&cifs_file->file_info_lock);
  747. spin_unlock(&cifsi->open_file_lock);
  748. spin_unlock(&tcon->open_file_lock);
  749. return;
  750. }
  751. spin_unlock(&cifs_file->file_info_lock);
  752. if (server->ops->get_lease_key)
  753. server->ops->get_lease_key(inode, &fid);
  754. /* store open in pending opens to make sure we don't miss lease break */
  755. cifs_add_pending_open_locked(&fid, cifs_file->tlink, &open);
  756. /* remove it from the lists */
  757. list_del(&cifs_file->flist);
  758. list_del(&cifs_file->tlist);
  759. atomic_dec(&tcon->num_local_opens);
  760. if (list_empty(&cifsi->openFileList)) {
  761. cifs_dbg(FYI, "closing last open instance for inode %p\n",
  762. d_inode(cifs_file->dentry));
  763. /*
  764. * In strict cache mode we need invalidate mapping on the last
  765. * close because it may cause a error when we open this file
  766. * again and get at least level II oplock.
  767. */
  768. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)
  769. set_bit(CIFS_INO_INVALID_MAPPING, &cifsi->flags);
  770. cifs_set_oplock_level(cifsi, 0);
  771. }
  772. spin_unlock(&cifsi->open_file_lock);
  773. spin_unlock(&tcon->open_file_lock);
  774. oplock_break_cancelled = wait_oplock_handler ?
  775. cancel_work_sync(&cifs_file->oplock_break) : false;
  776. if (!tcon->need_reconnect && !cifs_file->invalidHandle) {
  777. struct TCP_Server_Info *server = tcon->ses->server;
  778. unsigned int xid;
  779. int rc = 0;
  780. xid = get_xid();
  781. if (server->ops->close_getattr)
  782. rc = server->ops->close_getattr(xid, tcon, cifs_file);
  783. else if (server->ops->close)
  784. rc = server->ops->close(xid, tcon, &cifs_file->fid);
  785. _free_xid(xid);
  786. if (rc == -EBUSY || rc == -EAGAIN) {
  787. // Server close failed, hence offloading it as an async op
  788. queue_work(serverclose_wq, &cifs_file->serverclose);
  789. serverclose_offloaded = true;
  790. }
  791. }
  792. if (oplock_break_cancelled)
  793. cifs_done_oplock_break(cifsi);
  794. cifs_del_pending_open(&open);
  795. // if serverclose has been offloaded to wq (on failure), it will
  796. // handle offloading put as well. If serverclose not offloaded,
  797. // we need to handle offloading put here.
  798. if (!serverclose_offloaded) {
  799. if (offload)
  800. queue_work(fileinfo_put_wq, &cifs_file->put);
  801. else
  802. cifsFileInfo_put_final(cifs_file);
  803. }
  804. }
  805. int cifs_open(struct inode *inode, struct file *file)
  806. {
  807. int rc = -EACCES;
  808. unsigned int xid;
  809. __u32 oplock;
  810. struct cifs_sb_info *cifs_sb;
  811. struct TCP_Server_Info *server;
  812. struct cifs_tcon *tcon;
  813. struct tcon_link *tlink;
  814. struct cifsFileInfo *cfile = NULL;
  815. void *page;
  816. const char *full_path;
  817. bool posix_open_ok = false;
  818. struct cifs_fid fid = {};
  819. struct cifs_pending_open open;
  820. struct cifs_open_info_data data = {};
  821. xid = get_xid();
  822. cifs_sb = CIFS_SB(inode->i_sb);
  823. if (unlikely(cifs_forced_shutdown(cifs_sb))) {
  824. free_xid(xid);
  825. return -EIO;
  826. }
  827. tlink = cifs_sb_tlink(cifs_sb);
  828. if (IS_ERR(tlink)) {
  829. free_xid(xid);
  830. return PTR_ERR(tlink);
  831. }
  832. tcon = tlink_tcon(tlink);
  833. server = tcon->ses->server;
  834. page = alloc_dentry_path();
  835. full_path = build_path_from_dentry(file_dentry(file), page);
  836. if (IS_ERR(full_path)) {
  837. rc = PTR_ERR(full_path);
  838. goto out;
  839. }
  840. cifs_dbg(FYI, "inode = 0x%p file flags are 0x%x for %s\n",
  841. inode, file->f_flags, full_path);
  842. if (file->f_flags & O_DIRECT &&
  843. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
  844. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  845. file->f_op = &cifs_file_direct_nobrl_ops;
  846. else
  847. file->f_op = &cifs_file_direct_ops;
  848. }
  849. /* Get the cached handle as SMB2 close is deferred */
  850. rc = cifs_get_readable_path(tcon, full_path, &cfile);
  851. if (rc == 0) {
  852. if (file->f_flags == cfile->f_flags) {
  853. file->private_data = cfile;
  854. spin_lock(&CIFS_I(inode)->deferred_lock);
  855. cifs_del_deferred_close(cfile);
  856. spin_unlock(&CIFS_I(inode)->deferred_lock);
  857. goto use_cache;
  858. } else {
  859. _cifsFileInfo_put(cfile, true, false);
  860. }
  861. } else {
  862. /* hard link on the defeered close file */
  863. rc = cifs_get_hardlink_path(tcon, inode, file);
  864. if (rc)
  865. cifs_close_deferred_file(CIFS_I(inode));
  866. }
  867. if (server->oplocks)
  868. oplock = REQ_OPLOCK;
  869. else
  870. oplock = 0;
  871. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  872. if (!tcon->broken_posix_open && tcon->unix_ext &&
  873. cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
  874. le64_to_cpu(tcon->fsUnixInfo.Capability))) {
  875. /* can not refresh inode info since size could be stale */
  876. rc = cifs_posix_open(full_path, &inode, inode->i_sb,
  877. cifs_sb->ctx->file_mode /* ignored */,
  878. file->f_flags, &oplock, &fid.netfid, xid);
  879. if (rc == 0) {
  880. cifs_dbg(FYI, "posix open succeeded\n");
  881. posix_open_ok = true;
  882. } else if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
  883. if (tcon->ses->serverNOS)
  884. 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",
  885. tcon->ses->ip_addr,
  886. tcon->ses->serverNOS);
  887. tcon->broken_posix_open = true;
  888. } else if ((rc != -EIO) && (rc != -EREMOTE) &&
  889. (rc != -EOPNOTSUPP)) /* path not found or net err */
  890. goto out;
  891. /*
  892. * Else fallthrough to retry open the old way on network i/o
  893. * or DFS errors.
  894. */
  895. }
  896. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  897. if (server->ops->get_lease_key)
  898. server->ops->get_lease_key(inode, &fid);
  899. cifs_add_pending_open(&fid, tlink, &open);
  900. if (!posix_open_ok) {
  901. if (server->ops->get_lease_key)
  902. server->ops->get_lease_key(inode, &fid);
  903. rc = cifs_nt_open(full_path, inode, cifs_sb, tcon, file->f_flags, &oplock, &fid,
  904. xid, &data);
  905. if (rc) {
  906. cifs_del_pending_open(&open);
  907. goto out;
  908. }
  909. }
  910. cfile = cifs_new_fileinfo(&fid, file, tlink, oplock, data.symlink_target);
  911. if (cfile == NULL) {
  912. if (server->ops->close)
  913. server->ops->close(xid, tcon, &fid);
  914. cifs_del_pending_open(&open);
  915. rc = -ENOMEM;
  916. goto out;
  917. }
  918. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  919. if ((oplock & CIFS_CREATE_ACTION) && !posix_open_ok && tcon->unix_ext) {
  920. /*
  921. * Time to set mode which we can not set earlier due to
  922. * problems creating new read-only files.
  923. */
  924. struct cifs_unix_set_info_args args = {
  925. .mode = inode->i_mode,
  926. .uid = INVALID_UID, /* no change */
  927. .gid = INVALID_GID, /* no change */
  928. .ctime = NO_CHANGE_64,
  929. .atime = NO_CHANGE_64,
  930. .mtime = NO_CHANGE_64,
  931. .device = 0,
  932. };
  933. CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid.netfid,
  934. cfile->pid);
  935. }
  936. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  937. use_cache:
  938. fscache_use_cookie(cifs_inode_cookie(file_inode(file)),
  939. file->f_mode & FMODE_WRITE);
  940. if (!(file->f_flags & O_DIRECT))
  941. goto out;
  942. if ((file->f_flags & (O_ACCMODE | O_APPEND)) == O_RDONLY)
  943. goto out;
  944. cifs_invalidate_cache(file_inode(file), FSCACHE_INVAL_DIO_WRITE);
  945. out:
  946. free_dentry_path(page);
  947. free_xid(xid);
  948. cifs_put_tlink(tlink);
  949. cifs_free_open_info(&data);
  950. return rc;
  951. }
  952. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  953. static int cifs_push_posix_locks(struct cifsFileInfo *cfile);
  954. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  955. /*
  956. * Try to reacquire byte range locks that were released when session
  957. * to server was lost.
  958. */
  959. static int
  960. cifs_relock_file(struct cifsFileInfo *cfile)
  961. {
  962. struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
  963. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  964. int rc = 0;
  965. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  966. struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
  967. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  968. down_read_nested(&cinode->lock_sem, SINGLE_DEPTH_NESTING);
  969. if (cinode->can_cache_brlcks) {
  970. /* can cache locks - no need to relock */
  971. up_read(&cinode->lock_sem);
  972. return rc;
  973. }
  974. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  975. if (cap_unix(tcon->ses) &&
  976. (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
  977. ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
  978. rc = cifs_push_posix_locks(cfile);
  979. else
  980. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  981. rc = tcon->ses->server->ops->push_mand_locks(cfile);
  982. up_read(&cinode->lock_sem);
  983. return rc;
  984. }
  985. static int
  986. cifs_reopen_file(struct cifsFileInfo *cfile, bool can_flush)
  987. {
  988. int rc = -EACCES;
  989. unsigned int xid;
  990. __u32 oplock;
  991. struct cifs_sb_info *cifs_sb;
  992. struct cifs_tcon *tcon;
  993. struct TCP_Server_Info *server;
  994. struct cifsInodeInfo *cinode;
  995. struct inode *inode;
  996. void *page;
  997. const char *full_path;
  998. int desired_access;
  999. int disposition = FILE_OPEN;
  1000. int create_options = CREATE_NOT_DIR;
  1001. struct cifs_open_parms oparms;
  1002. int rdwr_for_fscache = 0;
  1003. xid = get_xid();
  1004. mutex_lock(&cfile->fh_mutex);
  1005. if (!cfile->invalidHandle) {
  1006. mutex_unlock(&cfile->fh_mutex);
  1007. free_xid(xid);
  1008. return 0;
  1009. }
  1010. inode = d_inode(cfile->dentry);
  1011. cifs_sb = CIFS_SB(inode->i_sb);
  1012. tcon = tlink_tcon(cfile->tlink);
  1013. server = tcon->ses->server;
  1014. /*
  1015. * Can not grab rename sem here because various ops, including those
  1016. * that already have the rename sem can end up causing writepage to get
  1017. * called and if the server was down that means we end up here, and we
  1018. * can never tell if the caller already has the rename_sem.
  1019. */
  1020. page = alloc_dentry_path();
  1021. full_path = build_path_from_dentry(cfile->dentry, page);
  1022. if (IS_ERR(full_path)) {
  1023. mutex_unlock(&cfile->fh_mutex);
  1024. free_dentry_path(page);
  1025. free_xid(xid);
  1026. return PTR_ERR(full_path);
  1027. }
  1028. cifs_dbg(FYI, "inode = 0x%p file flags 0x%x for %s\n",
  1029. inode, cfile->f_flags, full_path);
  1030. if (tcon->ses->server->oplocks)
  1031. oplock = REQ_OPLOCK;
  1032. else
  1033. oplock = 0;
  1034. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  1035. if (tcon->unix_ext && cap_unix(tcon->ses) &&
  1036. (CIFS_UNIX_POSIX_PATH_OPS_CAP &
  1037. le64_to_cpu(tcon->fsUnixInfo.Capability))) {
  1038. /*
  1039. * O_CREAT, O_EXCL and O_TRUNC already had their effect on the
  1040. * original open. Must mask them off for a reopen.
  1041. */
  1042. unsigned int oflags = cfile->f_flags &
  1043. ~(O_CREAT | O_EXCL | O_TRUNC);
  1044. rc = cifs_posix_open(full_path, NULL, inode->i_sb,
  1045. cifs_sb->ctx->file_mode /* ignored */,
  1046. oflags, &oplock, &cfile->fid.netfid, xid);
  1047. if (rc == 0) {
  1048. cifs_dbg(FYI, "posix reopen succeeded\n");
  1049. oparms.reconnect = true;
  1050. goto reopen_success;
  1051. }
  1052. /*
  1053. * fallthrough to retry open the old way on errors, especially
  1054. * in the reconnect path it is important to retry hard
  1055. */
  1056. }
  1057. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  1058. /* If we're caching, we need to be able to fill in around partial writes. */
  1059. if (cifs_fscache_enabled(inode) && (cfile->f_flags & O_ACCMODE) == O_WRONLY)
  1060. rdwr_for_fscache = 1;
  1061. desired_access = cifs_convert_flags(cfile->f_flags, rdwr_for_fscache);
  1062. /* O_SYNC also has bit for O_DSYNC so following check picks up either */
  1063. if (cfile->f_flags & O_SYNC)
  1064. create_options |= CREATE_WRITE_THROUGH;
  1065. if (cfile->f_flags & O_DIRECT)
  1066. create_options |= CREATE_NO_BUFFER;
  1067. if (server->ops->get_lease_key)
  1068. server->ops->get_lease_key(inode, &cfile->fid);
  1069. retry_open:
  1070. oparms = (struct cifs_open_parms) {
  1071. .tcon = tcon,
  1072. .cifs_sb = cifs_sb,
  1073. .desired_access = desired_access,
  1074. .create_options = cifs_create_options(cifs_sb, create_options),
  1075. .disposition = disposition,
  1076. .path = full_path,
  1077. .fid = &cfile->fid,
  1078. .reconnect = true,
  1079. };
  1080. /*
  1081. * Can not refresh inode by passing in file_info buf to be returned by
  1082. * ops->open and then calling get_inode_info with returned buf since
  1083. * file might have write behind data that needs to be flushed and server
  1084. * version of file size can be stale. If we knew for sure that inode was
  1085. * not dirty locally we could do this.
  1086. */
  1087. rc = server->ops->open(xid, &oparms, &oplock, NULL);
  1088. if (rc == -ENOENT && oparms.reconnect == false) {
  1089. /* durable handle timeout is expired - open the file again */
  1090. rc = server->ops->open(xid, &oparms, &oplock, NULL);
  1091. /* indicate that we need to relock the file */
  1092. oparms.reconnect = true;
  1093. }
  1094. if (rc == -EACCES && rdwr_for_fscache == 1) {
  1095. desired_access = cifs_convert_flags(cfile->f_flags, 0);
  1096. rdwr_for_fscache = 2;
  1097. goto retry_open;
  1098. }
  1099. if (rc) {
  1100. mutex_unlock(&cfile->fh_mutex);
  1101. cifs_dbg(FYI, "cifs_reopen returned 0x%x\n", rc);
  1102. cifs_dbg(FYI, "oplock: %d\n", oplock);
  1103. goto reopen_error_exit;
  1104. }
  1105. if (rdwr_for_fscache == 2)
  1106. cifs_invalidate_cache(inode, FSCACHE_INVAL_DIO_WRITE);
  1107. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  1108. reopen_success:
  1109. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  1110. cfile->invalidHandle = false;
  1111. mutex_unlock(&cfile->fh_mutex);
  1112. cinode = CIFS_I(inode);
  1113. if (can_flush) {
  1114. rc = filemap_write_and_wait(inode->i_mapping);
  1115. if (!is_interrupt_error(rc))
  1116. mapping_set_error(inode->i_mapping, rc);
  1117. if (tcon->posix_extensions) {
  1118. rc = smb311_posix_get_inode_info(&inode, full_path,
  1119. NULL, inode->i_sb, xid);
  1120. } else if (tcon->unix_ext) {
  1121. rc = cifs_get_inode_info_unix(&inode, full_path,
  1122. inode->i_sb, xid);
  1123. } else {
  1124. rc = cifs_get_inode_info(&inode, full_path, NULL,
  1125. inode->i_sb, xid, NULL);
  1126. }
  1127. }
  1128. /*
  1129. * Else we are writing out data to server already and could deadlock if
  1130. * we tried to flush data, and since we do not know if we have data that
  1131. * would invalidate the current end of file on the server we can not go
  1132. * to the server to get the new inode info.
  1133. */
  1134. /*
  1135. * If the server returned a read oplock and we have mandatory brlocks,
  1136. * set oplock level to None.
  1137. */
  1138. if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) {
  1139. cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n");
  1140. oplock = 0;
  1141. }
  1142. server->ops->set_fid(cfile, &cfile->fid, oplock);
  1143. if (oparms.reconnect)
  1144. cifs_relock_file(cfile);
  1145. reopen_error_exit:
  1146. free_dentry_path(page);
  1147. free_xid(xid);
  1148. return rc;
  1149. }
  1150. void smb2_deferred_work_close(struct work_struct *work)
  1151. {
  1152. struct cifsFileInfo *cfile = container_of(work,
  1153. struct cifsFileInfo, deferred.work);
  1154. spin_lock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
  1155. cifs_del_deferred_close(cfile);
  1156. cfile->deferred_close_scheduled = false;
  1157. spin_unlock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
  1158. _cifsFileInfo_put(cfile, true, false);
  1159. }
  1160. static bool
  1161. smb2_can_defer_close(struct inode *inode, struct cifs_deferred_close *dclose)
  1162. {
  1163. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1164. struct cifsInodeInfo *cinode = CIFS_I(inode);
  1165. return (cifs_sb->ctx->closetimeo && cinode->lease_granted && dclose &&
  1166. (cinode->oplock == CIFS_CACHE_RHW_FLG ||
  1167. cinode->oplock == CIFS_CACHE_RH_FLG) &&
  1168. !test_bit(CIFS_INO_CLOSE_ON_LOCK, &cinode->flags));
  1169. }
  1170. int cifs_close(struct inode *inode, struct file *file)
  1171. {
  1172. struct cifsFileInfo *cfile;
  1173. struct cifsInodeInfo *cinode = CIFS_I(inode);
  1174. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1175. struct cifs_deferred_close *dclose;
  1176. cifs_fscache_unuse_inode_cookie(inode, file->f_mode & FMODE_WRITE);
  1177. if (file->private_data != NULL) {
  1178. cfile = file->private_data;
  1179. file->private_data = NULL;
  1180. dclose = kmalloc(sizeof(struct cifs_deferred_close), GFP_KERNEL);
  1181. if ((cfile->status_file_deleted == false) &&
  1182. (smb2_can_defer_close(inode, dclose))) {
  1183. if (test_and_clear_bit(NETFS_ICTX_MODIFIED_ATTR, &cinode->netfs.flags)) {
  1184. inode_set_mtime_to_ts(inode,
  1185. inode_set_ctime_current(inode));
  1186. }
  1187. spin_lock(&cinode->deferred_lock);
  1188. cifs_add_deferred_close(cfile, dclose);
  1189. if (cfile->deferred_close_scheduled &&
  1190. delayed_work_pending(&cfile->deferred)) {
  1191. /*
  1192. * If there is no pending work, mod_delayed_work queues new work.
  1193. * So, Increase the ref count to avoid use-after-free.
  1194. */
  1195. if (!mod_delayed_work(deferredclose_wq,
  1196. &cfile->deferred, cifs_sb->ctx->closetimeo))
  1197. cifsFileInfo_get(cfile);
  1198. } else {
  1199. /* Deferred close for files */
  1200. queue_delayed_work(deferredclose_wq,
  1201. &cfile->deferred, cifs_sb->ctx->closetimeo);
  1202. cfile->deferred_close_scheduled = true;
  1203. spin_unlock(&cinode->deferred_lock);
  1204. return 0;
  1205. }
  1206. spin_unlock(&cinode->deferred_lock);
  1207. _cifsFileInfo_put(cfile, true, false);
  1208. } else {
  1209. _cifsFileInfo_put(cfile, true, false);
  1210. kfree(dclose);
  1211. }
  1212. }
  1213. /* return code from the ->release op is always ignored */
  1214. return 0;
  1215. }
  1216. void
  1217. cifs_reopen_persistent_handles(struct cifs_tcon *tcon)
  1218. {
  1219. struct cifsFileInfo *open_file, *tmp;
  1220. LIST_HEAD(tmp_list);
  1221. if (!tcon->use_persistent || !tcon->need_reopen_files)
  1222. return;
  1223. tcon->need_reopen_files = false;
  1224. cifs_dbg(FYI, "Reopen persistent handles\n");
  1225. /* list all files open on tree connection, reopen resilient handles */
  1226. spin_lock(&tcon->open_file_lock);
  1227. list_for_each_entry(open_file, &tcon->openFileList, tlist) {
  1228. if (!open_file->invalidHandle)
  1229. continue;
  1230. cifsFileInfo_get(open_file);
  1231. list_add_tail(&open_file->rlist, &tmp_list);
  1232. }
  1233. spin_unlock(&tcon->open_file_lock);
  1234. list_for_each_entry_safe(open_file, tmp, &tmp_list, rlist) {
  1235. if (cifs_reopen_file(open_file, false /* do not flush */))
  1236. tcon->need_reopen_files = true;
  1237. list_del_init(&open_file->rlist);
  1238. cifsFileInfo_put(open_file);
  1239. }
  1240. }
  1241. int cifs_closedir(struct inode *inode, struct file *file)
  1242. {
  1243. int rc = 0;
  1244. unsigned int xid;
  1245. struct cifsFileInfo *cfile = file->private_data;
  1246. struct cifs_tcon *tcon;
  1247. struct TCP_Server_Info *server;
  1248. char *buf;
  1249. cifs_dbg(FYI, "Closedir inode = 0x%p\n", inode);
  1250. if (cfile == NULL)
  1251. return rc;
  1252. xid = get_xid();
  1253. tcon = tlink_tcon(cfile->tlink);
  1254. server = tcon->ses->server;
  1255. cifs_dbg(FYI, "Freeing private data in close dir\n");
  1256. spin_lock(&cfile->file_info_lock);
  1257. if (server->ops->dir_needs_close(cfile)) {
  1258. cfile->invalidHandle = true;
  1259. spin_unlock(&cfile->file_info_lock);
  1260. if (server->ops->close_dir)
  1261. rc = server->ops->close_dir(xid, tcon, &cfile->fid);
  1262. else
  1263. rc = -ENOSYS;
  1264. cifs_dbg(FYI, "Closing uncompleted readdir with rc %d\n", rc);
  1265. /* not much we can do if it fails anyway, ignore rc */
  1266. rc = 0;
  1267. } else
  1268. spin_unlock(&cfile->file_info_lock);
  1269. buf = cfile->srch_inf.ntwrk_buf_start;
  1270. if (buf) {
  1271. cifs_dbg(FYI, "closedir free smb buf in srch struct\n");
  1272. cfile->srch_inf.ntwrk_buf_start = NULL;
  1273. if (cfile->srch_inf.smallBuf)
  1274. cifs_small_buf_release(buf);
  1275. else
  1276. cifs_buf_release(buf);
  1277. }
  1278. cifs_put_tlink(cfile->tlink);
  1279. kfree(file->private_data);
  1280. file->private_data = NULL;
  1281. /* BB can we lock the filestruct while this is going on? */
  1282. free_xid(xid);
  1283. return rc;
  1284. }
  1285. static struct cifsLockInfo *
  1286. cifs_lock_init(__u64 offset, __u64 length, __u8 type, __u16 flags)
  1287. {
  1288. struct cifsLockInfo *lock =
  1289. kmalloc(sizeof(struct cifsLockInfo), GFP_KERNEL);
  1290. if (!lock)
  1291. return lock;
  1292. lock->offset = offset;
  1293. lock->length = length;
  1294. lock->type = type;
  1295. lock->pid = current->tgid;
  1296. lock->flags = flags;
  1297. INIT_LIST_HEAD(&lock->blist);
  1298. init_waitqueue_head(&lock->block_q);
  1299. return lock;
  1300. }
  1301. void
  1302. cifs_del_lock_waiters(struct cifsLockInfo *lock)
  1303. {
  1304. struct cifsLockInfo *li, *tmp;
  1305. list_for_each_entry_safe(li, tmp, &lock->blist, blist) {
  1306. list_del_init(&li->blist);
  1307. wake_up(&li->block_q);
  1308. }
  1309. }
  1310. #define CIFS_LOCK_OP 0
  1311. #define CIFS_READ_OP 1
  1312. #define CIFS_WRITE_OP 2
  1313. /* @rw_check : 0 - no op, 1 - read, 2 - write */
  1314. static bool
  1315. cifs_find_fid_lock_conflict(struct cifs_fid_locks *fdlocks, __u64 offset,
  1316. __u64 length, __u8 type, __u16 flags,
  1317. struct cifsFileInfo *cfile,
  1318. struct cifsLockInfo **conf_lock, int rw_check)
  1319. {
  1320. struct cifsLockInfo *li;
  1321. struct cifsFileInfo *cur_cfile = fdlocks->cfile;
  1322. struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
  1323. list_for_each_entry(li, &fdlocks->locks, llist) {
  1324. if (offset + length <= li->offset ||
  1325. offset >= li->offset + li->length)
  1326. continue;
  1327. if (rw_check != CIFS_LOCK_OP && current->tgid == li->pid &&
  1328. server->ops->compare_fids(cfile, cur_cfile)) {
  1329. /* shared lock prevents write op through the same fid */
  1330. if (!(li->type & server->vals->shared_lock_type) ||
  1331. rw_check != CIFS_WRITE_OP)
  1332. continue;
  1333. }
  1334. if ((type & server->vals->shared_lock_type) &&
  1335. ((server->ops->compare_fids(cfile, cur_cfile) &&
  1336. current->tgid == li->pid) || type == li->type))
  1337. continue;
  1338. if (rw_check == CIFS_LOCK_OP &&
  1339. (flags & FL_OFDLCK) && (li->flags & FL_OFDLCK) &&
  1340. server->ops->compare_fids(cfile, cur_cfile))
  1341. continue;
  1342. if (conf_lock)
  1343. *conf_lock = li;
  1344. return true;
  1345. }
  1346. return false;
  1347. }
  1348. bool
  1349. cifs_find_lock_conflict(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
  1350. __u8 type, __u16 flags,
  1351. struct cifsLockInfo **conf_lock, int rw_check)
  1352. {
  1353. bool rc = false;
  1354. struct cifs_fid_locks *cur;
  1355. struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
  1356. list_for_each_entry(cur, &cinode->llist, llist) {
  1357. rc = cifs_find_fid_lock_conflict(cur, offset, length, type,
  1358. flags, cfile, conf_lock,
  1359. rw_check);
  1360. if (rc)
  1361. break;
  1362. }
  1363. return rc;
  1364. }
  1365. /*
  1366. * Check if there is another lock that prevents us to set the lock (mandatory
  1367. * style). If such a lock exists, update the flock structure with its
  1368. * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
  1369. * or leave it the same if we can't. Returns 0 if we don't need to request to
  1370. * the server or 1 otherwise.
  1371. */
  1372. static int
  1373. cifs_lock_test(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
  1374. __u8 type, struct file_lock *flock)
  1375. {
  1376. int rc = 0;
  1377. struct cifsLockInfo *conf_lock;
  1378. struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
  1379. struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
  1380. bool exist;
  1381. down_read(&cinode->lock_sem);
  1382. exist = cifs_find_lock_conflict(cfile, offset, length, type,
  1383. flock->c.flc_flags, &conf_lock,
  1384. CIFS_LOCK_OP);
  1385. if (exist) {
  1386. flock->fl_start = conf_lock->offset;
  1387. flock->fl_end = conf_lock->offset + conf_lock->length - 1;
  1388. flock->c.flc_pid = conf_lock->pid;
  1389. if (conf_lock->type & server->vals->shared_lock_type)
  1390. flock->c.flc_type = F_RDLCK;
  1391. else
  1392. flock->c.flc_type = F_WRLCK;
  1393. } else if (!cinode->can_cache_brlcks)
  1394. rc = 1;
  1395. else
  1396. flock->c.flc_type = F_UNLCK;
  1397. up_read(&cinode->lock_sem);
  1398. return rc;
  1399. }
  1400. static void
  1401. cifs_lock_add(struct cifsFileInfo *cfile, struct cifsLockInfo *lock)
  1402. {
  1403. struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
  1404. cifs_down_write(&cinode->lock_sem);
  1405. list_add_tail(&lock->llist, &cfile->llist->locks);
  1406. up_write(&cinode->lock_sem);
  1407. }
  1408. /*
  1409. * Set the byte-range lock (mandatory style). Returns:
  1410. * 1) 0, if we set the lock and don't need to request to the server;
  1411. * 2) 1, if no locks prevent us but we need to request to the server;
  1412. * 3) -EACCES, if there is a lock that prevents us and wait is false.
  1413. */
  1414. static int
  1415. cifs_lock_add_if(struct cifsFileInfo *cfile, struct cifsLockInfo *lock,
  1416. bool wait)
  1417. {
  1418. struct cifsLockInfo *conf_lock;
  1419. struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
  1420. bool exist;
  1421. int rc = 0;
  1422. try_again:
  1423. exist = false;
  1424. cifs_down_write(&cinode->lock_sem);
  1425. exist = cifs_find_lock_conflict(cfile, lock->offset, lock->length,
  1426. lock->type, lock->flags, &conf_lock,
  1427. CIFS_LOCK_OP);
  1428. if (!exist && cinode->can_cache_brlcks) {
  1429. list_add_tail(&lock->llist, &cfile->llist->locks);
  1430. up_write(&cinode->lock_sem);
  1431. return rc;
  1432. }
  1433. if (!exist)
  1434. rc = 1;
  1435. else if (!wait)
  1436. rc = -EACCES;
  1437. else {
  1438. list_add_tail(&lock->blist, &conf_lock->blist);
  1439. up_write(&cinode->lock_sem);
  1440. rc = wait_event_interruptible(lock->block_q,
  1441. (lock->blist.prev == &lock->blist) &&
  1442. (lock->blist.next == &lock->blist));
  1443. if (!rc)
  1444. goto try_again;
  1445. cifs_down_write(&cinode->lock_sem);
  1446. list_del_init(&lock->blist);
  1447. }
  1448. up_write(&cinode->lock_sem);
  1449. return rc;
  1450. }
  1451. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  1452. /*
  1453. * Check if there is another lock that prevents us to set the lock (posix
  1454. * style). If such a lock exists, update the flock structure with its
  1455. * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
  1456. * or leave it the same if we can't. Returns 0 if we don't need to request to
  1457. * the server or 1 otherwise.
  1458. */
  1459. static int
  1460. cifs_posix_lock_test(struct file *file, struct file_lock *flock)
  1461. {
  1462. int rc = 0;
  1463. struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
  1464. unsigned char saved_type = flock->c.flc_type;
  1465. if ((flock->c.flc_flags & FL_POSIX) == 0)
  1466. return 1;
  1467. down_read(&cinode->lock_sem);
  1468. posix_test_lock(file, flock);
  1469. if (lock_is_unlock(flock) && !cinode->can_cache_brlcks) {
  1470. flock->c.flc_type = saved_type;
  1471. rc = 1;
  1472. }
  1473. up_read(&cinode->lock_sem);
  1474. return rc;
  1475. }
  1476. /*
  1477. * Set the byte-range lock (posix style). Returns:
  1478. * 1) <0, if the error occurs while setting the lock;
  1479. * 2) 0, if we set the lock and don't need to request to the server;
  1480. * 3) FILE_LOCK_DEFERRED, if we will wait for some other file_lock;
  1481. * 4) FILE_LOCK_DEFERRED + 1, if we need to request to the server.
  1482. */
  1483. static int
  1484. cifs_posix_lock_set(struct file *file, struct file_lock *flock)
  1485. {
  1486. struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
  1487. int rc = FILE_LOCK_DEFERRED + 1;
  1488. if ((flock->c.flc_flags & FL_POSIX) == 0)
  1489. return rc;
  1490. cifs_down_write(&cinode->lock_sem);
  1491. if (!cinode->can_cache_brlcks) {
  1492. up_write(&cinode->lock_sem);
  1493. return rc;
  1494. }
  1495. rc = posix_lock_file(file, flock, NULL);
  1496. up_write(&cinode->lock_sem);
  1497. return rc;
  1498. }
  1499. int
  1500. cifs_push_mandatory_locks(struct cifsFileInfo *cfile)
  1501. {
  1502. unsigned int xid;
  1503. int rc = 0, stored_rc;
  1504. struct cifsLockInfo *li, *tmp;
  1505. struct cifs_tcon *tcon;
  1506. unsigned int num, max_num, max_buf;
  1507. LOCKING_ANDX_RANGE *buf, *cur;
  1508. static const int types[] = {
  1509. LOCKING_ANDX_LARGE_FILES,
  1510. LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES
  1511. };
  1512. int i;
  1513. xid = get_xid();
  1514. tcon = tlink_tcon(cfile->tlink);
  1515. /*
  1516. * Accessing maxBuf is racy with cifs_reconnect - need to store value
  1517. * and check it before using.
  1518. */
  1519. max_buf = tcon->ses->server->maxBuf;
  1520. if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE))) {
  1521. free_xid(xid);
  1522. return -EINVAL;
  1523. }
  1524. BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) >
  1525. PAGE_SIZE);
  1526. max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr),
  1527. PAGE_SIZE);
  1528. max_num = (max_buf - sizeof(struct smb_hdr)) /
  1529. sizeof(LOCKING_ANDX_RANGE);
  1530. buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
  1531. if (!buf) {
  1532. free_xid(xid);
  1533. return -ENOMEM;
  1534. }
  1535. for (i = 0; i < 2; i++) {
  1536. cur = buf;
  1537. num = 0;
  1538. list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
  1539. if (li->type != types[i])
  1540. continue;
  1541. cur->Pid = cpu_to_le16(li->pid);
  1542. cur->LengthLow = cpu_to_le32((u32)li->length);
  1543. cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
  1544. cur->OffsetLow = cpu_to_le32((u32)li->offset);
  1545. cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
  1546. if (++num == max_num) {
  1547. stored_rc = cifs_lockv(xid, tcon,
  1548. cfile->fid.netfid,
  1549. (__u8)li->type, 0, num,
  1550. buf);
  1551. if (stored_rc)
  1552. rc = stored_rc;
  1553. cur = buf;
  1554. num = 0;
  1555. } else
  1556. cur++;
  1557. }
  1558. if (num) {
  1559. stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
  1560. (__u8)types[i], 0, num, buf);
  1561. if (stored_rc)
  1562. rc = stored_rc;
  1563. }
  1564. }
  1565. kfree(buf);
  1566. free_xid(xid);
  1567. return rc;
  1568. }
  1569. static __u32
  1570. hash_lockowner(fl_owner_t owner)
  1571. {
  1572. return cifs_lock_secret ^ hash32_ptr((const void *)owner);
  1573. }
  1574. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  1575. struct lock_to_push {
  1576. struct list_head llist;
  1577. __u64 offset;
  1578. __u64 length;
  1579. __u32 pid;
  1580. __u16 netfid;
  1581. __u8 type;
  1582. };
  1583. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  1584. static int
  1585. cifs_push_posix_locks(struct cifsFileInfo *cfile)
  1586. {
  1587. struct inode *inode = d_inode(cfile->dentry);
  1588. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  1589. struct file_lock *flock;
  1590. struct file_lock_context *flctx = locks_inode_context(inode);
  1591. unsigned int count = 0, i;
  1592. int rc = 0, xid, type;
  1593. struct list_head locks_to_send, *el;
  1594. struct lock_to_push *lck, *tmp;
  1595. __u64 length;
  1596. xid = get_xid();
  1597. if (!flctx)
  1598. goto out;
  1599. spin_lock(&flctx->flc_lock);
  1600. list_for_each(el, &flctx->flc_posix) {
  1601. count++;
  1602. }
  1603. spin_unlock(&flctx->flc_lock);
  1604. INIT_LIST_HEAD(&locks_to_send);
  1605. /*
  1606. * Allocating count locks is enough because no FL_POSIX locks can be
  1607. * added to the list while we are holding cinode->lock_sem that
  1608. * protects locking operations of this inode.
  1609. */
  1610. for (i = 0; i < count; i++) {
  1611. lck = kmalloc(sizeof(struct lock_to_push), GFP_KERNEL);
  1612. if (!lck) {
  1613. rc = -ENOMEM;
  1614. goto err_out;
  1615. }
  1616. list_add_tail(&lck->llist, &locks_to_send);
  1617. }
  1618. el = locks_to_send.next;
  1619. spin_lock(&flctx->flc_lock);
  1620. for_each_file_lock(flock, &flctx->flc_posix) {
  1621. unsigned char ftype = flock->c.flc_type;
  1622. if (el == &locks_to_send) {
  1623. /*
  1624. * The list ended. We don't have enough allocated
  1625. * structures - something is really wrong.
  1626. */
  1627. cifs_dbg(VFS, "Can't push all brlocks!\n");
  1628. break;
  1629. }
  1630. length = cifs_flock_len(flock);
  1631. if (ftype == F_RDLCK || ftype == F_SHLCK)
  1632. type = CIFS_RDLCK;
  1633. else
  1634. type = CIFS_WRLCK;
  1635. lck = list_entry(el, struct lock_to_push, llist);
  1636. lck->pid = hash_lockowner(flock->c.flc_owner);
  1637. lck->netfid = cfile->fid.netfid;
  1638. lck->length = length;
  1639. lck->type = type;
  1640. lck->offset = flock->fl_start;
  1641. }
  1642. spin_unlock(&flctx->flc_lock);
  1643. list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
  1644. int stored_rc;
  1645. stored_rc = CIFSSMBPosixLock(xid, tcon, lck->netfid, lck->pid,
  1646. lck->offset, lck->length, NULL,
  1647. lck->type, 0);
  1648. if (stored_rc)
  1649. rc = stored_rc;
  1650. list_del(&lck->llist);
  1651. kfree(lck);
  1652. }
  1653. out:
  1654. free_xid(xid);
  1655. return rc;
  1656. err_out:
  1657. list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
  1658. list_del(&lck->llist);
  1659. kfree(lck);
  1660. }
  1661. goto out;
  1662. }
  1663. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  1664. static int
  1665. cifs_push_locks(struct cifsFileInfo *cfile)
  1666. {
  1667. struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
  1668. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  1669. int rc = 0;
  1670. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  1671. struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
  1672. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  1673. /* we are going to update can_cache_brlcks here - need a write access */
  1674. cifs_down_write(&cinode->lock_sem);
  1675. if (!cinode->can_cache_brlcks) {
  1676. up_write(&cinode->lock_sem);
  1677. return rc;
  1678. }
  1679. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  1680. if (cap_unix(tcon->ses) &&
  1681. (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
  1682. ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
  1683. rc = cifs_push_posix_locks(cfile);
  1684. else
  1685. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  1686. rc = tcon->ses->server->ops->push_mand_locks(cfile);
  1687. cinode->can_cache_brlcks = false;
  1688. up_write(&cinode->lock_sem);
  1689. return rc;
  1690. }
  1691. static void
  1692. cifs_read_flock(struct file_lock *flock, __u32 *type, int *lock, int *unlock,
  1693. bool *wait_flag, struct TCP_Server_Info *server)
  1694. {
  1695. if (flock->c.flc_flags & FL_POSIX)
  1696. cifs_dbg(FYI, "Posix\n");
  1697. if (flock->c.flc_flags & FL_FLOCK)
  1698. cifs_dbg(FYI, "Flock\n");
  1699. if (flock->c.flc_flags & FL_SLEEP) {
  1700. cifs_dbg(FYI, "Blocking lock\n");
  1701. *wait_flag = true;
  1702. }
  1703. if (flock->c.flc_flags & FL_ACCESS)
  1704. cifs_dbg(FYI, "Process suspended by mandatory locking - not implemented yet\n");
  1705. if (flock->c.flc_flags & FL_LEASE)
  1706. cifs_dbg(FYI, "Lease on file - not implemented yet\n");
  1707. if (flock->c.flc_flags &
  1708. (~(FL_POSIX | FL_FLOCK | FL_SLEEP |
  1709. FL_ACCESS | FL_LEASE | FL_CLOSE | FL_OFDLCK)))
  1710. cifs_dbg(FYI, "Unknown lock flags 0x%x\n",
  1711. flock->c.flc_flags);
  1712. *type = server->vals->large_lock_type;
  1713. if (lock_is_write(flock)) {
  1714. cifs_dbg(FYI, "F_WRLCK\n");
  1715. *type |= server->vals->exclusive_lock_type;
  1716. *lock = 1;
  1717. } else if (lock_is_unlock(flock)) {
  1718. cifs_dbg(FYI, "F_UNLCK\n");
  1719. *type |= server->vals->unlock_lock_type;
  1720. *unlock = 1;
  1721. /* Check if unlock includes more than one lock range */
  1722. } else if (lock_is_read(flock)) {
  1723. cifs_dbg(FYI, "F_RDLCK\n");
  1724. *type |= server->vals->shared_lock_type;
  1725. *lock = 1;
  1726. } else if (flock->c.flc_type == F_EXLCK) {
  1727. cifs_dbg(FYI, "F_EXLCK\n");
  1728. *type |= server->vals->exclusive_lock_type;
  1729. *lock = 1;
  1730. } else if (flock->c.flc_type == F_SHLCK) {
  1731. cifs_dbg(FYI, "F_SHLCK\n");
  1732. *type |= server->vals->shared_lock_type;
  1733. *lock = 1;
  1734. } else
  1735. cifs_dbg(FYI, "Unknown type of lock\n");
  1736. }
  1737. static int
  1738. cifs_getlk(struct file *file, struct file_lock *flock, __u32 type,
  1739. bool wait_flag, bool posix_lck, unsigned int xid)
  1740. {
  1741. int rc = 0;
  1742. __u64 length = cifs_flock_len(flock);
  1743. struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
  1744. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  1745. struct TCP_Server_Info *server = tcon->ses->server;
  1746. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  1747. __u16 netfid = cfile->fid.netfid;
  1748. if (posix_lck) {
  1749. int posix_lock_type;
  1750. rc = cifs_posix_lock_test(file, flock);
  1751. if (!rc)
  1752. return rc;
  1753. if (type & server->vals->shared_lock_type)
  1754. posix_lock_type = CIFS_RDLCK;
  1755. else
  1756. posix_lock_type = CIFS_WRLCK;
  1757. rc = CIFSSMBPosixLock(xid, tcon, netfid,
  1758. hash_lockowner(flock->c.flc_owner),
  1759. flock->fl_start, length, flock,
  1760. posix_lock_type, wait_flag);
  1761. return rc;
  1762. }
  1763. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  1764. rc = cifs_lock_test(cfile, flock->fl_start, length, type, flock);
  1765. if (!rc)
  1766. return rc;
  1767. /* BB we could chain these into one lock request BB */
  1768. rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, type,
  1769. 1, 0, false);
  1770. if (rc == 0) {
  1771. rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
  1772. type, 0, 1, false);
  1773. flock->c.flc_type = F_UNLCK;
  1774. if (rc != 0)
  1775. cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
  1776. rc);
  1777. return 0;
  1778. }
  1779. if (type & server->vals->shared_lock_type) {
  1780. flock->c.flc_type = F_WRLCK;
  1781. return 0;
  1782. }
  1783. type &= ~server->vals->exclusive_lock_type;
  1784. rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
  1785. type | server->vals->shared_lock_type,
  1786. 1, 0, false);
  1787. if (rc == 0) {
  1788. rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
  1789. type | server->vals->shared_lock_type, 0, 1, false);
  1790. flock->c.flc_type = F_RDLCK;
  1791. if (rc != 0)
  1792. cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
  1793. rc);
  1794. } else
  1795. flock->c.flc_type = F_WRLCK;
  1796. return 0;
  1797. }
  1798. void
  1799. cifs_move_llist(struct list_head *source, struct list_head *dest)
  1800. {
  1801. struct list_head *li, *tmp;
  1802. list_for_each_safe(li, tmp, source)
  1803. list_move(li, dest);
  1804. }
  1805. int
  1806. cifs_get_hardlink_path(struct cifs_tcon *tcon, struct inode *inode,
  1807. struct file *file)
  1808. {
  1809. struct cifsFileInfo *open_file = NULL;
  1810. struct cifsInodeInfo *cinode = CIFS_I(inode);
  1811. int rc = 0;
  1812. spin_lock(&tcon->open_file_lock);
  1813. spin_lock(&cinode->open_file_lock);
  1814. list_for_each_entry(open_file, &cinode->openFileList, flist) {
  1815. if (file->f_flags == open_file->f_flags) {
  1816. rc = -EINVAL;
  1817. break;
  1818. }
  1819. }
  1820. spin_unlock(&cinode->open_file_lock);
  1821. spin_unlock(&tcon->open_file_lock);
  1822. return rc;
  1823. }
  1824. void
  1825. cifs_free_llist(struct list_head *llist)
  1826. {
  1827. struct cifsLockInfo *li, *tmp;
  1828. list_for_each_entry_safe(li, tmp, llist, llist) {
  1829. cifs_del_lock_waiters(li);
  1830. list_del(&li->llist);
  1831. kfree(li);
  1832. }
  1833. }
  1834. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  1835. int
  1836. cifs_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
  1837. unsigned int xid)
  1838. {
  1839. int rc = 0, stored_rc;
  1840. static const int types[] = {
  1841. LOCKING_ANDX_LARGE_FILES,
  1842. LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES
  1843. };
  1844. unsigned int i;
  1845. unsigned int max_num, num, max_buf;
  1846. LOCKING_ANDX_RANGE *buf, *cur;
  1847. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  1848. struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
  1849. struct cifsLockInfo *li, *tmp;
  1850. __u64 length = cifs_flock_len(flock);
  1851. LIST_HEAD(tmp_llist);
  1852. /*
  1853. * Accessing maxBuf is racy with cifs_reconnect - need to store value
  1854. * and check it before using.
  1855. */
  1856. max_buf = tcon->ses->server->maxBuf;
  1857. if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE)))
  1858. return -EINVAL;
  1859. BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) >
  1860. PAGE_SIZE);
  1861. max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr),
  1862. PAGE_SIZE);
  1863. max_num = (max_buf - sizeof(struct smb_hdr)) /
  1864. sizeof(LOCKING_ANDX_RANGE);
  1865. buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
  1866. if (!buf)
  1867. return -ENOMEM;
  1868. cifs_down_write(&cinode->lock_sem);
  1869. for (i = 0; i < 2; i++) {
  1870. cur = buf;
  1871. num = 0;
  1872. list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
  1873. if (flock->fl_start > li->offset ||
  1874. (flock->fl_start + length) <
  1875. (li->offset + li->length))
  1876. continue;
  1877. if (current->tgid != li->pid)
  1878. continue;
  1879. if (types[i] != li->type)
  1880. continue;
  1881. if (cinode->can_cache_brlcks) {
  1882. /*
  1883. * We can cache brlock requests - simply remove
  1884. * a lock from the file's list.
  1885. */
  1886. list_del(&li->llist);
  1887. cifs_del_lock_waiters(li);
  1888. kfree(li);
  1889. continue;
  1890. }
  1891. cur->Pid = cpu_to_le16(li->pid);
  1892. cur->LengthLow = cpu_to_le32((u32)li->length);
  1893. cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
  1894. cur->OffsetLow = cpu_to_le32((u32)li->offset);
  1895. cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
  1896. /*
  1897. * We need to save a lock here to let us add it again to
  1898. * the file's list if the unlock range request fails on
  1899. * the server.
  1900. */
  1901. list_move(&li->llist, &tmp_llist);
  1902. if (++num == max_num) {
  1903. stored_rc = cifs_lockv(xid, tcon,
  1904. cfile->fid.netfid,
  1905. li->type, num, 0, buf);
  1906. if (stored_rc) {
  1907. /*
  1908. * We failed on the unlock range
  1909. * request - add all locks from the tmp
  1910. * list to the head of the file's list.
  1911. */
  1912. cifs_move_llist(&tmp_llist,
  1913. &cfile->llist->locks);
  1914. rc = stored_rc;
  1915. } else
  1916. /*
  1917. * The unlock range request succeed -
  1918. * free the tmp list.
  1919. */
  1920. cifs_free_llist(&tmp_llist);
  1921. cur = buf;
  1922. num = 0;
  1923. } else
  1924. cur++;
  1925. }
  1926. if (num) {
  1927. stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
  1928. types[i], num, 0, buf);
  1929. if (stored_rc) {
  1930. cifs_move_llist(&tmp_llist,
  1931. &cfile->llist->locks);
  1932. rc = stored_rc;
  1933. } else
  1934. cifs_free_llist(&tmp_llist);
  1935. }
  1936. }
  1937. up_write(&cinode->lock_sem);
  1938. kfree(buf);
  1939. return rc;
  1940. }
  1941. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  1942. static int
  1943. cifs_setlk(struct file *file, struct file_lock *flock, __u32 type,
  1944. bool wait_flag, bool posix_lck, int lock, int unlock,
  1945. unsigned int xid)
  1946. {
  1947. int rc = 0;
  1948. __u64 length = cifs_flock_len(flock);
  1949. struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
  1950. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  1951. struct TCP_Server_Info *server = tcon->ses->server;
  1952. struct inode *inode = d_inode(cfile->dentry);
  1953. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  1954. if (posix_lck) {
  1955. int posix_lock_type;
  1956. rc = cifs_posix_lock_set(file, flock);
  1957. if (rc <= FILE_LOCK_DEFERRED)
  1958. return rc;
  1959. if (type & server->vals->shared_lock_type)
  1960. posix_lock_type = CIFS_RDLCK;
  1961. else
  1962. posix_lock_type = CIFS_WRLCK;
  1963. if (unlock == 1)
  1964. posix_lock_type = CIFS_UNLCK;
  1965. rc = CIFSSMBPosixLock(xid, tcon, cfile->fid.netfid,
  1966. hash_lockowner(flock->c.flc_owner),
  1967. flock->fl_start, length,
  1968. NULL, posix_lock_type, wait_flag);
  1969. goto out;
  1970. }
  1971. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  1972. if (lock) {
  1973. struct cifsLockInfo *lock;
  1974. lock = cifs_lock_init(flock->fl_start, length, type,
  1975. flock->c.flc_flags);
  1976. if (!lock)
  1977. return -ENOMEM;
  1978. rc = cifs_lock_add_if(cfile, lock, wait_flag);
  1979. if (rc < 0) {
  1980. kfree(lock);
  1981. return rc;
  1982. }
  1983. if (!rc)
  1984. goto out;
  1985. /*
  1986. * Windows 7 server can delay breaking lease from read to None
  1987. * if we set a byte-range lock on a file - break it explicitly
  1988. * before sending the lock to the server to be sure the next
  1989. * read won't conflict with non-overlapted locks due to
  1990. * pagereading.
  1991. */
  1992. if (!CIFS_CACHE_WRITE(CIFS_I(inode)) &&
  1993. CIFS_CACHE_READ(CIFS_I(inode))) {
  1994. cifs_zap_mapping(inode);
  1995. cifs_dbg(FYI, "Set no oplock for inode=%p due to mand locks\n",
  1996. inode);
  1997. CIFS_I(inode)->oplock = 0;
  1998. }
  1999. rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
  2000. type, 1, 0, wait_flag);
  2001. if (rc) {
  2002. kfree(lock);
  2003. return rc;
  2004. }
  2005. cifs_lock_add(cfile, lock);
  2006. } else if (unlock)
  2007. rc = server->ops->mand_unlock_range(cfile, flock, xid);
  2008. out:
  2009. if ((flock->c.flc_flags & FL_POSIX) || (flock->c.flc_flags & FL_FLOCK)) {
  2010. /*
  2011. * If this is a request to remove all locks because we
  2012. * are closing the file, it doesn't matter if the
  2013. * unlocking failed as both cifs.ko and the SMB server
  2014. * remove the lock on file close
  2015. */
  2016. if (rc) {
  2017. cifs_dbg(VFS, "%s failed rc=%d\n", __func__, rc);
  2018. if (!(flock->c.flc_flags & FL_CLOSE))
  2019. return rc;
  2020. }
  2021. rc = locks_lock_file_wait(file, flock);
  2022. }
  2023. return rc;
  2024. }
  2025. int cifs_flock(struct file *file, int cmd, struct file_lock *fl)
  2026. {
  2027. int rc, xid;
  2028. int lock = 0, unlock = 0;
  2029. bool wait_flag = false;
  2030. bool posix_lck = false;
  2031. struct cifs_sb_info *cifs_sb;
  2032. struct cifs_tcon *tcon;
  2033. struct cifsFileInfo *cfile;
  2034. __u32 type;
  2035. xid = get_xid();
  2036. if (!(fl->c.flc_flags & FL_FLOCK)) {
  2037. rc = -ENOLCK;
  2038. free_xid(xid);
  2039. return rc;
  2040. }
  2041. cfile = (struct cifsFileInfo *)file->private_data;
  2042. tcon = tlink_tcon(cfile->tlink);
  2043. cifs_read_flock(fl, &type, &lock, &unlock, &wait_flag,
  2044. tcon->ses->server);
  2045. cifs_sb = CIFS_FILE_SB(file);
  2046. if (cap_unix(tcon->ses) &&
  2047. (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
  2048. ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
  2049. posix_lck = true;
  2050. if (!lock && !unlock) {
  2051. /*
  2052. * if no lock or unlock then nothing to do since we do not
  2053. * know what it is
  2054. */
  2055. rc = -EOPNOTSUPP;
  2056. free_xid(xid);
  2057. return rc;
  2058. }
  2059. rc = cifs_setlk(file, fl, type, wait_flag, posix_lck, lock, unlock,
  2060. xid);
  2061. free_xid(xid);
  2062. return rc;
  2063. }
  2064. int cifs_lock(struct file *file, int cmd, struct file_lock *flock)
  2065. {
  2066. int rc, xid;
  2067. int lock = 0, unlock = 0;
  2068. bool wait_flag = false;
  2069. bool posix_lck = false;
  2070. struct cifs_sb_info *cifs_sb;
  2071. struct cifs_tcon *tcon;
  2072. struct cifsFileInfo *cfile;
  2073. __u32 type;
  2074. rc = -EACCES;
  2075. xid = get_xid();
  2076. cifs_dbg(FYI, "%s: %pD2 cmd=0x%x type=0x%x flags=0x%x r=%lld:%lld\n", __func__, file, cmd,
  2077. flock->c.flc_flags, flock->c.flc_type,
  2078. (long long)flock->fl_start,
  2079. (long long)flock->fl_end);
  2080. cfile = (struct cifsFileInfo *)file->private_data;
  2081. tcon = tlink_tcon(cfile->tlink);
  2082. cifs_read_flock(flock, &type, &lock, &unlock, &wait_flag,
  2083. tcon->ses->server);
  2084. cifs_sb = CIFS_FILE_SB(file);
  2085. set_bit(CIFS_INO_CLOSE_ON_LOCK, &CIFS_I(d_inode(cfile->dentry))->flags);
  2086. if (cap_unix(tcon->ses) &&
  2087. (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
  2088. ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
  2089. posix_lck = true;
  2090. /*
  2091. * BB add code here to normalize offset and length to account for
  2092. * negative length which we can not accept over the wire.
  2093. */
  2094. if (IS_GETLK(cmd)) {
  2095. rc = cifs_getlk(file, flock, type, wait_flag, posix_lck, xid);
  2096. free_xid(xid);
  2097. return rc;
  2098. }
  2099. if (!lock && !unlock) {
  2100. /*
  2101. * if no lock or unlock then nothing to do since we do not
  2102. * know what it is
  2103. */
  2104. free_xid(xid);
  2105. return -EOPNOTSUPP;
  2106. }
  2107. rc = cifs_setlk(file, flock, type, wait_flag, posix_lck, lock, unlock,
  2108. xid);
  2109. free_xid(xid);
  2110. return rc;
  2111. }
  2112. void cifs_write_subrequest_terminated(struct cifs_io_subrequest *wdata, ssize_t result,
  2113. bool was_async)
  2114. {
  2115. struct netfs_io_request *wreq = wdata->rreq;
  2116. struct netfs_inode *ictx = netfs_inode(wreq->inode);
  2117. loff_t wrend;
  2118. if (result > 0) {
  2119. wrend = wdata->subreq.start + wdata->subreq.transferred + result;
  2120. if (wrend > ictx->zero_point &&
  2121. (wdata->rreq->origin == NETFS_UNBUFFERED_WRITE ||
  2122. wdata->rreq->origin == NETFS_DIO_WRITE))
  2123. ictx->zero_point = wrend;
  2124. if (wrend > ictx->remote_i_size)
  2125. netfs_resize_file(ictx, wrend, true);
  2126. }
  2127. netfs_write_subrequest_terminated(&wdata->subreq, result, was_async);
  2128. }
  2129. struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode,
  2130. bool fsuid_only)
  2131. {
  2132. struct cifsFileInfo *open_file = NULL;
  2133. struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_inode->netfs.inode.i_sb);
  2134. /* only filter by fsuid on multiuser mounts */
  2135. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
  2136. fsuid_only = false;
  2137. spin_lock(&cifs_inode->open_file_lock);
  2138. /* we could simply get the first_list_entry since write-only entries
  2139. are always at the end of the list but since the first entry might
  2140. have a close pending, we go through the whole list */
  2141. list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
  2142. if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
  2143. continue;
  2144. if (OPEN_FMODE(open_file->f_flags) & FMODE_READ) {
  2145. if ((!open_file->invalidHandle)) {
  2146. /* found a good file */
  2147. /* lock it so it will not be closed on us */
  2148. cifsFileInfo_get(open_file);
  2149. spin_unlock(&cifs_inode->open_file_lock);
  2150. return open_file;
  2151. } /* else might as well continue, and look for
  2152. another, or simply have the caller reopen it
  2153. again rather than trying to fix this handle */
  2154. } else /* write only file */
  2155. break; /* write only files are last so must be done */
  2156. }
  2157. spin_unlock(&cifs_inode->open_file_lock);
  2158. return NULL;
  2159. }
  2160. /* Return -EBADF if no handle is found and general rc otherwise */
  2161. int
  2162. cifs_get_writable_file(struct cifsInodeInfo *cifs_inode, int flags,
  2163. struct cifsFileInfo **ret_file)
  2164. {
  2165. struct cifsFileInfo *open_file, *inv_file = NULL;
  2166. struct cifs_sb_info *cifs_sb;
  2167. bool any_available = false;
  2168. int rc = -EBADF;
  2169. unsigned int refind = 0;
  2170. bool fsuid_only = flags & FIND_WR_FSUID_ONLY;
  2171. bool with_delete = flags & FIND_WR_WITH_DELETE;
  2172. *ret_file = NULL;
  2173. /*
  2174. * Having a null inode here (because mapping->host was set to zero by
  2175. * the VFS or MM) should not happen but we had reports of on oops (due
  2176. * to it being zero) during stress testcases so we need to check for it
  2177. */
  2178. if (cifs_inode == NULL) {
  2179. cifs_dbg(VFS, "Null inode passed to cifs_writeable_file\n");
  2180. dump_stack();
  2181. return rc;
  2182. }
  2183. cifs_sb = CIFS_SB(cifs_inode->netfs.inode.i_sb);
  2184. /* only filter by fsuid on multiuser mounts */
  2185. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
  2186. fsuid_only = false;
  2187. spin_lock(&cifs_inode->open_file_lock);
  2188. refind_writable:
  2189. if (refind > MAX_REOPEN_ATT) {
  2190. spin_unlock(&cifs_inode->open_file_lock);
  2191. return rc;
  2192. }
  2193. list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
  2194. if (!any_available && open_file->pid != current->tgid)
  2195. continue;
  2196. if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
  2197. continue;
  2198. if (with_delete && !(open_file->fid.access & DELETE))
  2199. continue;
  2200. if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
  2201. if (!open_file->invalidHandle) {
  2202. /* found a good writable file */
  2203. cifsFileInfo_get(open_file);
  2204. spin_unlock(&cifs_inode->open_file_lock);
  2205. *ret_file = open_file;
  2206. return 0;
  2207. } else {
  2208. if (!inv_file)
  2209. inv_file = open_file;
  2210. }
  2211. }
  2212. }
  2213. /* couldn't find usable FH with same pid, try any available */
  2214. if (!any_available) {
  2215. any_available = true;
  2216. goto refind_writable;
  2217. }
  2218. if (inv_file) {
  2219. any_available = false;
  2220. cifsFileInfo_get(inv_file);
  2221. }
  2222. spin_unlock(&cifs_inode->open_file_lock);
  2223. if (inv_file) {
  2224. rc = cifs_reopen_file(inv_file, false);
  2225. if (!rc) {
  2226. *ret_file = inv_file;
  2227. return 0;
  2228. }
  2229. spin_lock(&cifs_inode->open_file_lock);
  2230. list_move_tail(&inv_file->flist, &cifs_inode->openFileList);
  2231. spin_unlock(&cifs_inode->open_file_lock);
  2232. cifsFileInfo_put(inv_file);
  2233. ++refind;
  2234. inv_file = NULL;
  2235. spin_lock(&cifs_inode->open_file_lock);
  2236. goto refind_writable;
  2237. }
  2238. return rc;
  2239. }
  2240. struct cifsFileInfo *
  2241. find_writable_file(struct cifsInodeInfo *cifs_inode, int flags)
  2242. {
  2243. struct cifsFileInfo *cfile;
  2244. int rc;
  2245. rc = cifs_get_writable_file(cifs_inode, flags, &cfile);
  2246. if (rc)
  2247. cifs_dbg(FYI, "Couldn't find writable handle rc=%d\n", rc);
  2248. return cfile;
  2249. }
  2250. int
  2251. cifs_get_writable_path(struct cifs_tcon *tcon, const char *name,
  2252. int flags,
  2253. struct cifsFileInfo **ret_file)
  2254. {
  2255. struct cifsFileInfo *cfile;
  2256. void *page = alloc_dentry_path();
  2257. *ret_file = NULL;
  2258. spin_lock(&tcon->open_file_lock);
  2259. list_for_each_entry(cfile, &tcon->openFileList, tlist) {
  2260. struct cifsInodeInfo *cinode;
  2261. const char *full_path = build_path_from_dentry(cfile->dentry, page);
  2262. if (IS_ERR(full_path)) {
  2263. spin_unlock(&tcon->open_file_lock);
  2264. free_dentry_path(page);
  2265. return PTR_ERR(full_path);
  2266. }
  2267. if (strcmp(full_path, name))
  2268. continue;
  2269. cinode = CIFS_I(d_inode(cfile->dentry));
  2270. spin_unlock(&tcon->open_file_lock);
  2271. free_dentry_path(page);
  2272. return cifs_get_writable_file(cinode, flags, ret_file);
  2273. }
  2274. spin_unlock(&tcon->open_file_lock);
  2275. free_dentry_path(page);
  2276. return -ENOENT;
  2277. }
  2278. int
  2279. cifs_get_readable_path(struct cifs_tcon *tcon, const char *name,
  2280. struct cifsFileInfo **ret_file)
  2281. {
  2282. struct cifsFileInfo *cfile;
  2283. void *page = alloc_dentry_path();
  2284. *ret_file = NULL;
  2285. spin_lock(&tcon->open_file_lock);
  2286. list_for_each_entry(cfile, &tcon->openFileList, tlist) {
  2287. struct cifsInodeInfo *cinode;
  2288. const char *full_path = build_path_from_dentry(cfile->dentry, page);
  2289. if (IS_ERR(full_path)) {
  2290. spin_unlock(&tcon->open_file_lock);
  2291. free_dentry_path(page);
  2292. return PTR_ERR(full_path);
  2293. }
  2294. if (strcmp(full_path, name))
  2295. continue;
  2296. cinode = CIFS_I(d_inode(cfile->dentry));
  2297. spin_unlock(&tcon->open_file_lock);
  2298. free_dentry_path(page);
  2299. *ret_file = find_readable_file(cinode, 0);
  2300. return *ret_file ? 0 : -ENOENT;
  2301. }
  2302. spin_unlock(&tcon->open_file_lock);
  2303. free_dentry_path(page);
  2304. return -ENOENT;
  2305. }
  2306. /*
  2307. * Flush data on a strict file.
  2308. */
  2309. int cifs_strict_fsync(struct file *file, loff_t start, loff_t end,
  2310. int datasync)
  2311. {
  2312. unsigned int xid;
  2313. int rc = 0;
  2314. struct cifs_tcon *tcon;
  2315. struct TCP_Server_Info *server;
  2316. struct cifsFileInfo *smbfile = file->private_data;
  2317. struct inode *inode = file_inode(file);
  2318. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  2319. rc = file_write_and_wait_range(file, start, end);
  2320. if (rc) {
  2321. trace_cifs_fsync_err(inode->i_ino, rc);
  2322. return rc;
  2323. }
  2324. xid = get_xid();
  2325. cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
  2326. file, datasync);
  2327. if (!CIFS_CACHE_READ(CIFS_I(inode))) {
  2328. rc = cifs_zap_mapping(inode);
  2329. if (rc) {
  2330. cifs_dbg(FYI, "rc: %d during invalidate phase\n", rc);
  2331. rc = 0; /* don't care about it in fsync */
  2332. }
  2333. }
  2334. tcon = tlink_tcon(smbfile->tlink);
  2335. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
  2336. server = tcon->ses->server;
  2337. if (server->ops->flush == NULL) {
  2338. rc = -ENOSYS;
  2339. goto strict_fsync_exit;
  2340. }
  2341. if ((OPEN_FMODE(smbfile->f_flags) & FMODE_WRITE) == 0) {
  2342. smbfile = find_writable_file(CIFS_I(inode), FIND_WR_ANY);
  2343. if (smbfile) {
  2344. rc = server->ops->flush(xid, tcon, &smbfile->fid);
  2345. cifsFileInfo_put(smbfile);
  2346. } else
  2347. cifs_dbg(FYI, "ignore fsync for file not open for write\n");
  2348. } else
  2349. rc = server->ops->flush(xid, tcon, &smbfile->fid);
  2350. }
  2351. strict_fsync_exit:
  2352. free_xid(xid);
  2353. return rc;
  2354. }
  2355. /*
  2356. * Flush data on a non-strict data.
  2357. */
  2358. int cifs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  2359. {
  2360. unsigned int xid;
  2361. int rc = 0;
  2362. struct cifs_tcon *tcon;
  2363. struct TCP_Server_Info *server;
  2364. struct cifsFileInfo *smbfile = file->private_data;
  2365. struct inode *inode = file_inode(file);
  2366. struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
  2367. rc = file_write_and_wait_range(file, start, end);
  2368. if (rc) {
  2369. trace_cifs_fsync_err(file_inode(file)->i_ino, rc);
  2370. return rc;
  2371. }
  2372. xid = get_xid();
  2373. cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
  2374. file, datasync);
  2375. tcon = tlink_tcon(smbfile->tlink);
  2376. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
  2377. server = tcon->ses->server;
  2378. if (server->ops->flush == NULL) {
  2379. rc = -ENOSYS;
  2380. goto fsync_exit;
  2381. }
  2382. if ((OPEN_FMODE(smbfile->f_flags) & FMODE_WRITE) == 0) {
  2383. smbfile = find_writable_file(CIFS_I(inode), FIND_WR_ANY);
  2384. if (smbfile) {
  2385. rc = server->ops->flush(xid, tcon, &smbfile->fid);
  2386. cifsFileInfo_put(smbfile);
  2387. } else
  2388. cifs_dbg(FYI, "ignore fsync for file not open for write\n");
  2389. } else
  2390. rc = server->ops->flush(xid, tcon, &smbfile->fid);
  2391. }
  2392. fsync_exit:
  2393. free_xid(xid);
  2394. return rc;
  2395. }
  2396. /*
  2397. * As file closes, flush all cached write data for this inode checking
  2398. * for write behind errors.
  2399. */
  2400. int cifs_flush(struct file *file, fl_owner_t id)
  2401. {
  2402. struct inode *inode = file_inode(file);
  2403. int rc = 0;
  2404. if (file->f_mode & FMODE_WRITE)
  2405. rc = filemap_write_and_wait(inode->i_mapping);
  2406. cifs_dbg(FYI, "Flush inode %p file %p rc %d\n", inode, file, rc);
  2407. if (rc) {
  2408. /* get more nuanced writeback errors */
  2409. rc = filemap_check_wb_err(file->f_mapping, 0);
  2410. trace_cifs_flush_err(inode->i_ino, rc);
  2411. }
  2412. return rc;
  2413. }
  2414. static ssize_t
  2415. cifs_writev(struct kiocb *iocb, struct iov_iter *from)
  2416. {
  2417. struct file *file = iocb->ki_filp;
  2418. struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
  2419. struct inode *inode = file->f_mapping->host;
  2420. struct cifsInodeInfo *cinode = CIFS_I(inode);
  2421. struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
  2422. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  2423. ssize_t rc;
  2424. rc = netfs_start_io_write(inode);
  2425. if (rc < 0)
  2426. return rc;
  2427. /*
  2428. * We need to hold the sem to be sure nobody modifies lock list
  2429. * with a brlock that prevents writing.
  2430. */
  2431. down_read(&cinode->lock_sem);
  2432. rc = generic_write_checks(iocb, from);
  2433. if (rc <= 0)
  2434. goto out;
  2435. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) &&
  2436. (cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(from),
  2437. server->vals->exclusive_lock_type, 0,
  2438. NULL, CIFS_WRITE_OP))) {
  2439. rc = -EACCES;
  2440. goto out;
  2441. }
  2442. rc = netfs_buffered_write_iter_locked(iocb, from, NULL);
  2443. out:
  2444. up_read(&cinode->lock_sem);
  2445. netfs_end_io_write(inode);
  2446. if (rc > 0)
  2447. rc = generic_write_sync(iocb, rc);
  2448. return rc;
  2449. }
  2450. ssize_t
  2451. cifs_strict_writev(struct kiocb *iocb, struct iov_iter *from)
  2452. {
  2453. struct inode *inode = file_inode(iocb->ki_filp);
  2454. struct cifsInodeInfo *cinode = CIFS_I(inode);
  2455. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  2456. struct cifsFileInfo *cfile = (struct cifsFileInfo *)
  2457. iocb->ki_filp->private_data;
  2458. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  2459. ssize_t written;
  2460. written = cifs_get_writer(cinode);
  2461. if (written)
  2462. return written;
  2463. if (CIFS_CACHE_WRITE(cinode)) {
  2464. if (cap_unix(tcon->ses) &&
  2465. (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
  2466. ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) {
  2467. written = netfs_file_write_iter(iocb, from);
  2468. goto out;
  2469. }
  2470. written = cifs_writev(iocb, from);
  2471. goto out;
  2472. }
  2473. /*
  2474. * For non-oplocked files in strict cache mode we need to write the data
  2475. * to the server exactly from the pos to pos+len-1 rather than flush all
  2476. * affected pages because it may cause a error with mandatory locks on
  2477. * these pages but not on the region from pos to ppos+len-1.
  2478. */
  2479. written = netfs_file_write_iter(iocb, from);
  2480. if (CIFS_CACHE_READ(cinode)) {
  2481. /*
  2482. * We have read level caching and we have just sent a write
  2483. * request to the server thus making data in the cache stale.
  2484. * Zap the cache and set oplock/lease level to NONE to avoid
  2485. * reading stale data from the cache. All subsequent read
  2486. * operations will read new data from the server.
  2487. */
  2488. cifs_zap_mapping(inode);
  2489. cifs_dbg(FYI, "Set Oplock/Lease to NONE for inode=%p after write\n",
  2490. inode);
  2491. cinode->oplock = 0;
  2492. }
  2493. out:
  2494. cifs_put_writer(cinode);
  2495. return written;
  2496. }
  2497. ssize_t cifs_loose_read_iter(struct kiocb *iocb, struct iov_iter *iter)
  2498. {
  2499. ssize_t rc;
  2500. struct inode *inode = file_inode(iocb->ki_filp);
  2501. if (iocb->ki_flags & IOCB_DIRECT)
  2502. return netfs_unbuffered_read_iter(iocb, iter);
  2503. rc = cifs_revalidate_mapping(inode);
  2504. if (rc)
  2505. return rc;
  2506. return netfs_file_read_iter(iocb, iter);
  2507. }
  2508. ssize_t cifs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
  2509. {
  2510. struct inode *inode = file_inode(iocb->ki_filp);
  2511. struct cifsInodeInfo *cinode = CIFS_I(inode);
  2512. ssize_t written;
  2513. int rc;
  2514. if (iocb->ki_filp->f_flags & O_DIRECT) {
  2515. written = netfs_unbuffered_write_iter(iocb, from);
  2516. if (written > 0 && CIFS_CACHE_READ(cinode)) {
  2517. cifs_zap_mapping(inode);
  2518. cifs_dbg(FYI,
  2519. "Set no oplock for inode=%p after a write operation\n",
  2520. inode);
  2521. cinode->oplock = 0;
  2522. }
  2523. return written;
  2524. }
  2525. written = cifs_get_writer(cinode);
  2526. if (written)
  2527. return written;
  2528. written = netfs_file_write_iter(iocb, from);
  2529. if (!CIFS_CACHE_WRITE(CIFS_I(inode))) {
  2530. rc = filemap_fdatawrite(inode->i_mapping);
  2531. if (rc)
  2532. cifs_dbg(FYI, "cifs_file_write_iter: %d rc on %p inode\n",
  2533. rc, inode);
  2534. }
  2535. cifs_put_writer(cinode);
  2536. return written;
  2537. }
  2538. ssize_t
  2539. cifs_strict_readv(struct kiocb *iocb, struct iov_iter *to)
  2540. {
  2541. struct inode *inode = file_inode(iocb->ki_filp);
  2542. struct cifsInodeInfo *cinode = CIFS_I(inode);
  2543. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  2544. struct cifsFileInfo *cfile = (struct cifsFileInfo *)
  2545. iocb->ki_filp->private_data;
  2546. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  2547. int rc = -EACCES;
  2548. /*
  2549. * In strict cache mode we need to read from the server all the time
  2550. * if we don't have level II oplock because the server can delay mtime
  2551. * change - so we can't make a decision about inode invalidating.
  2552. * And we can also fail with pagereading if there are mandatory locks
  2553. * on pages affected by this read but not on the region from pos to
  2554. * pos+len-1.
  2555. */
  2556. if (!CIFS_CACHE_READ(cinode))
  2557. return netfs_unbuffered_read_iter(iocb, to);
  2558. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0) {
  2559. if (iocb->ki_flags & IOCB_DIRECT)
  2560. return netfs_unbuffered_read_iter(iocb, to);
  2561. return netfs_buffered_read_iter(iocb, to);
  2562. }
  2563. /*
  2564. * We need to hold the sem to be sure nobody modifies lock list
  2565. * with a brlock that prevents reading.
  2566. */
  2567. if (iocb->ki_flags & IOCB_DIRECT) {
  2568. rc = netfs_start_io_direct(inode);
  2569. if (rc < 0)
  2570. goto out;
  2571. rc = -EACCES;
  2572. down_read(&cinode->lock_sem);
  2573. if (!cifs_find_lock_conflict(
  2574. cfile, iocb->ki_pos, iov_iter_count(to),
  2575. tcon->ses->server->vals->shared_lock_type,
  2576. 0, NULL, CIFS_READ_OP))
  2577. rc = netfs_unbuffered_read_iter_locked(iocb, to);
  2578. up_read(&cinode->lock_sem);
  2579. netfs_end_io_direct(inode);
  2580. } else {
  2581. rc = netfs_start_io_read(inode);
  2582. if (rc < 0)
  2583. goto out;
  2584. rc = -EACCES;
  2585. down_read(&cinode->lock_sem);
  2586. if (!cifs_find_lock_conflict(
  2587. cfile, iocb->ki_pos, iov_iter_count(to),
  2588. tcon->ses->server->vals->shared_lock_type,
  2589. 0, NULL, CIFS_READ_OP))
  2590. rc = filemap_read(iocb, to, 0);
  2591. up_read(&cinode->lock_sem);
  2592. netfs_end_io_read(inode);
  2593. }
  2594. out:
  2595. return rc;
  2596. }
  2597. static vm_fault_t cifs_page_mkwrite(struct vm_fault *vmf)
  2598. {
  2599. return netfs_page_mkwrite(vmf, NULL);
  2600. }
  2601. static const struct vm_operations_struct cifs_file_vm_ops = {
  2602. .fault = filemap_fault,
  2603. .map_pages = filemap_map_pages,
  2604. .page_mkwrite = cifs_page_mkwrite,
  2605. };
  2606. int cifs_file_strict_mmap(struct file *file, struct vm_area_struct *vma)
  2607. {
  2608. int xid, rc = 0;
  2609. struct inode *inode = file_inode(file);
  2610. xid = get_xid();
  2611. if (!CIFS_CACHE_READ(CIFS_I(inode)))
  2612. rc = cifs_zap_mapping(inode);
  2613. if (!rc)
  2614. rc = generic_file_mmap(file, vma);
  2615. if (!rc)
  2616. vma->vm_ops = &cifs_file_vm_ops;
  2617. free_xid(xid);
  2618. return rc;
  2619. }
  2620. int cifs_file_mmap(struct file *file, struct vm_area_struct *vma)
  2621. {
  2622. int rc, xid;
  2623. xid = get_xid();
  2624. rc = cifs_revalidate_file(file);
  2625. if (rc)
  2626. cifs_dbg(FYI, "Validation prior to mmap failed, error=%d\n",
  2627. rc);
  2628. if (!rc)
  2629. rc = generic_file_mmap(file, vma);
  2630. if (!rc)
  2631. vma->vm_ops = &cifs_file_vm_ops;
  2632. free_xid(xid);
  2633. return rc;
  2634. }
  2635. static int is_inode_writable(struct cifsInodeInfo *cifs_inode)
  2636. {
  2637. struct cifsFileInfo *open_file;
  2638. spin_lock(&cifs_inode->open_file_lock);
  2639. list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
  2640. if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
  2641. spin_unlock(&cifs_inode->open_file_lock);
  2642. return 1;
  2643. }
  2644. }
  2645. spin_unlock(&cifs_inode->open_file_lock);
  2646. return 0;
  2647. }
  2648. /* We do not want to update the file size from server for inodes
  2649. open for write - to avoid races with writepage extending
  2650. the file - in the future we could consider allowing
  2651. refreshing the inode only on increases in the file size
  2652. but this is tricky to do without racing with writebehind
  2653. page caching in the current Linux kernel design */
  2654. bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file,
  2655. bool from_readdir)
  2656. {
  2657. if (!cifsInode)
  2658. return true;
  2659. if (is_inode_writable(cifsInode) ||
  2660. ((cifsInode->oplock & CIFS_CACHE_RW_FLG) != 0 && from_readdir)) {
  2661. /* This inode is open for write at least once */
  2662. struct cifs_sb_info *cifs_sb;
  2663. cifs_sb = CIFS_SB(cifsInode->netfs.inode.i_sb);
  2664. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
  2665. /* since no page cache to corrupt on directio
  2666. we can change size safely */
  2667. return true;
  2668. }
  2669. if (i_size_read(&cifsInode->netfs.inode) < end_of_file)
  2670. return true;
  2671. return false;
  2672. } else
  2673. return true;
  2674. }
  2675. void cifs_oplock_break(struct work_struct *work)
  2676. {
  2677. struct cifsFileInfo *cfile = container_of(work, struct cifsFileInfo,
  2678. oplock_break);
  2679. struct inode *inode = d_inode(cfile->dentry);
  2680. struct super_block *sb = inode->i_sb;
  2681. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  2682. struct cifsInodeInfo *cinode = CIFS_I(inode);
  2683. struct cifs_tcon *tcon;
  2684. struct TCP_Server_Info *server;
  2685. struct tcon_link *tlink;
  2686. int rc = 0;
  2687. bool purge_cache = false, oplock_break_cancelled;
  2688. __u64 persistent_fid, volatile_fid;
  2689. __u16 net_fid;
  2690. /*
  2691. * Hold a reference to the superblock to prevent it and its inodes from
  2692. * being freed while we are accessing cinode. Otherwise, _cifsFileInfo_put()
  2693. * may release the last reference to the sb and trigger inode eviction.
  2694. */
  2695. cifs_sb_active(sb);
  2696. wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS,
  2697. TASK_UNINTERRUPTIBLE);
  2698. tlink = cifs_sb_tlink(cifs_sb);
  2699. if (IS_ERR(tlink))
  2700. goto out;
  2701. tcon = tlink_tcon(tlink);
  2702. server = tcon->ses->server;
  2703. server->ops->downgrade_oplock(server, cinode, cfile->oplock_level,
  2704. cfile->oplock_epoch, &purge_cache);
  2705. if (!CIFS_CACHE_WRITE(cinode) && CIFS_CACHE_READ(cinode) &&
  2706. cifs_has_mand_locks(cinode)) {
  2707. cifs_dbg(FYI, "Reset oplock to None for inode=%p due to mand locks\n",
  2708. inode);
  2709. cinode->oplock = 0;
  2710. }
  2711. if (inode && S_ISREG(inode->i_mode)) {
  2712. if (CIFS_CACHE_READ(cinode))
  2713. break_lease(inode, O_RDONLY);
  2714. else
  2715. break_lease(inode, O_WRONLY);
  2716. rc = filemap_fdatawrite(inode->i_mapping);
  2717. if (!CIFS_CACHE_READ(cinode) || purge_cache) {
  2718. rc = filemap_fdatawait(inode->i_mapping);
  2719. mapping_set_error(inode->i_mapping, rc);
  2720. cifs_zap_mapping(inode);
  2721. }
  2722. cifs_dbg(FYI, "Oplock flush inode %p rc %d\n", inode, rc);
  2723. if (CIFS_CACHE_WRITE(cinode))
  2724. goto oplock_break_ack;
  2725. }
  2726. rc = cifs_push_locks(cfile);
  2727. if (rc)
  2728. cifs_dbg(VFS, "Push locks rc = %d\n", rc);
  2729. oplock_break_ack:
  2730. /*
  2731. * When oplock break is received and there are no active
  2732. * file handles but cached, then schedule deferred close immediately.
  2733. * So, new open will not use cached handle.
  2734. */
  2735. if (!CIFS_CACHE_HANDLE(cinode) && !list_empty(&cinode->deferred_closes))
  2736. cifs_close_deferred_file(cinode);
  2737. persistent_fid = cfile->fid.persistent_fid;
  2738. volatile_fid = cfile->fid.volatile_fid;
  2739. net_fid = cfile->fid.netfid;
  2740. oplock_break_cancelled = cfile->oplock_break_cancelled;
  2741. _cifsFileInfo_put(cfile, false /* do not wait for ourself */, false);
  2742. /*
  2743. * MS-SMB2 3.2.5.19.1 and 3.2.5.19.2 (and MS-CIFS 3.2.5.42) do not require
  2744. * an acknowledgment to be sent when the file has already been closed.
  2745. */
  2746. spin_lock(&cinode->open_file_lock);
  2747. /* check list empty since can race with kill_sb calling tree disconnect */
  2748. if (!oplock_break_cancelled && !list_empty(&cinode->openFileList)) {
  2749. spin_unlock(&cinode->open_file_lock);
  2750. rc = server->ops->oplock_response(tcon, persistent_fid,
  2751. volatile_fid, net_fid, cinode);
  2752. cifs_dbg(FYI, "Oplock release rc = %d\n", rc);
  2753. } else
  2754. spin_unlock(&cinode->open_file_lock);
  2755. cifs_put_tlink(tlink);
  2756. out:
  2757. cifs_done_oplock_break(cinode);
  2758. cifs_sb_deactive(sb);
  2759. }
  2760. static int cifs_swap_activate(struct swap_info_struct *sis,
  2761. struct file *swap_file, sector_t *span)
  2762. {
  2763. struct cifsFileInfo *cfile = swap_file->private_data;
  2764. struct inode *inode = swap_file->f_mapping->host;
  2765. unsigned long blocks;
  2766. long long isize;
  2767. cifs_dbg(FYI, "swap activate\n");
  2768. if (!swap_file->f_mapping->a_ops->swap_rw)
  2769. /* Cannot support swap */
  2770. return -EINVAL;
  2771. spin_lock(&inode->i_lock);
  2772. blocks = inode->i_blocks;
  2773. isize = inode->i_size;
  2774. spin_unlock(&inode->i_lock);
  2775. if (blocks*512 < isize) {
  2776. pr_warn("swap activate: swapfile has holes\n");
  2777. return -EINVAL;
  2778. }
  2779. *span = sis->pages;
  2780. pr_warn_once("Swap support over SMB3 is experimental\n");
  2781. /*
  2782. * TODO: consider adding ACL (or documenting how) to prevent other
  2783. * users (on this or other systems) from reading it
  2784. */
  2785. /* TODO: add sk_set_memalloc(inet) or similar */
  2786. if (cfile)
  2787. cfile->swapfile = true;
  2788. /*
  2789. * TODO: Since file already open, we can't open with DENY_ALL here
  2790. * but we could add call to grab a byte range lock to prevent others
  2791. * from reading or writing the file
  2792. */
  2793. sis->flags |= SWP_FS_OPS;
  2794. return add_swap_extent(sis, 0, sis->max, 0);
  2795. }
  2796. static void cifs_swap_deactivate(struct file *file)
  2797. {
  2798. struct cifsFileInfo *cfile = file->private_data;
  2799. cifs_dbg(FYI, "swap deactivate\n");
  2800. /* TODO: undo sk_set_memalloc(inet) will eventually be needed */
  2801. if (cfile)
  2802. cfile->swapfile = false;
  2803. /* do we need to unpin (or unlock) the file */
  2804. }
  2805. /**
  2806. * cifs_swap_rw - SMB3 address space operation for swap I/O
  2807. * @iocb: target I/O control block
  2808. * @iter: I/O buffer
  2809. *
  2810. * Perform IO to the swap-file. This is much like direct IO.
  2811. */
  2812. static int cifs_swap_rw(struct kiocb *iocb, struct iov_iter *iter)
  2813. {
  2814. ssize_t ret;
  2815. if (iov_iter_rw(iter) == READ)
  2816. ret = netfs_unbuffered_read_iter_locked(iocb, iter);
  2817. else
  2818. ret = netfs_unbuffered_write_iter_locked(iocb, iter, NULL);
  2819. if (ret < 0)
  2820. return ret;
  2821. return 0;
  2822. }
  2823. const struct address_space_operations cifs_addr_ops = {
  2824. .read_folio = netfs_read_folio,
  2825. .readahead = netfs_readahead,
  2826. .writepages = netfs_writepages,
  2827. .dirty_folio = netfs_dirty_folio,
  2828. .release_folio = netfs_release_folio,
  2829. .direct_IO = noop_direct_IO,
  2830. .invalidate_folio = netfs_invalidate_folio,
  2831. .migrate_folio = filemap_migrate_folio,
  2832. /*
  2833. * TODO: investigate and if useful we could add an is_dirty_writeback
  2834. * helper if needed
  2835. */
  2836. .swap_activate = cifs_swap_activate,
  2837. .swap_deactivate = cifs_swap_deactivate,
  2838. .swap_rw = cifs_swap_rw,
  2839. };
  2840. /*
  2841. * cifs_readahead requires the server to support a buffer large enough to
  2842. * contain the header plus one complete page of data. Otherwise, we need
  2843. * to leave cifs_readahead out of the address space operations.
  2844. */
  2845. const struct address_space_operations cifs_addr_ops_smallbuf = {
  2846. .read_folio = netfs_read_folio,
  2847. .writepages = netfs_writepages,
  2848. .dirty_folio = netfs_dirty_folio,
  2849. .release_folio = netfs_release_folio,
  2850. .invalidate_folio = netfs_invalidate_folio,
  2851. .migrate_folio = filemap_migrate_folio,
  2852. };