file.c 109 KB

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