file.c 101 KB

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