nfs4proc.c 112 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169
  1. /*
  2. * fs/nfs/nfs4proc.c
  3. *
  4. * Client-side procedure declarations for NFSv4.
  5. *
  6. * Copyright (c) 2002 The Regents of the University of Michigan.
  7. * All rights reserved.
  8. *
  9. * Kendrick Smith <kmsmith@umich.edu>
  10. * Andy Adamson <andros@umich.edu>
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. *
  16. * 1. Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. * 3. Neither the name of the University nor the names of its
  22. * contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  26. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  27. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  32. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #include <linux/mm.h>
  38. #include <linux/utsname.h>
  39. #include <linux/delay.h>
  40. #include <linux/errno.h>
  41. #include <linux/string.h>
  42. #include <linux/sunrpc/clnt.h>
  43. #include <linux/nfs.h>
  44. #include <linux/nfs4.h>
  45. #include <linux/nfs_fs.h>
  46. #include <linux/nfs_page.h>
  47. #include <linux/smp_lock.h>
  48. #include <linux/namei.h>
  49. #include <linux/mount.h>
  50. #include "nfs4_fs.h"
  51. #include "delegation.h"
  52. #include "internal.h"
  53. #include "iostat.h"
  54. #define NFSDBG_FACILITY NFSDBG_PROC
  55. #define NFS4_POLL_RETRY_MIN (HZ/10)
  56. #define NFS4_POLL_RETRY_MAX (15*HZ)
  57. struct nfs4_opendata;
  58. static int _nfs4_proc_open(struct nfs4_opendata *data);
  59. static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
  60. static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
  61. static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
  62. static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
  63. /* Prevent leaks of NFSv4 errors into userland */
  64. static int nfs4_map_errors(int err)
  65. {
  66. if (err < -1000) {
  67. dprintk("%s could not handle NFSv4 error %d\n",
  68. __func__, -err);
  69. return -EIO;
  70. }
  71. return err;
  72. }
  73. /*
  74. * This is our standard bitmap for GETATTR requests.
  75. */
  76. const u32 nfs4_fattr_bitmap[2] = {
  77. FATTR4_WORD0_TYPE
  78. | FATTR4_WORD0_CHANGE
  79. | FATTR4_WORD0_SIZE
  80. | FATTR4_WORD0_FSID
  81. | FATTR4_WORD0_FILEID,
  82. FATTR4_WORD1_MODE
  83. | FATTR4_WORD1_NUMLINKS
  84. | FATTR4_WORD1_OWNER
  85. | FATTR4_WORD1_OWNER_GROUP
  86. | FATTR4_WORD1_RAWDEV
  87. | FATTR4_WORD1_SPACE_USED
  88. | FATTR4_WORD1_TIME_ACCESS
  89. | FATTR4_WORD1_TIME_METADATA
  90. | FATTR4_WORD1_TIME_MODIFY
  91. };
  92. const u32 nfs4_statfs_bitmap[2] = {
  93. FATTR4_WORD0_FILES_AVAIL
  94. | FATTR4_WORD0_FILES_FREE
  95. | FATTR4_WORD0_FILES_TOTAL,
  96. FATTR4_WORD1_SPACE_AVAIL
  97. | FATTR4_WORD1_SPACE_FREE
  98. | FATTR4_WORD1_SPACE_TOTAL
  99. };
  100. const u32 nfs4_pathconf_bitmap[2] = {
  101. FATTR4_WORD0_MAXLINK
  102. | FATTR4_WORD0_MAXNAME,
  103. 0
  104. };
  105. const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
  106. | FATTR4_WORD0_MAXREAD
  107. | FATTR4_WORD0_MAXWRITE
  108. | FATTR4_WORD0_LEASE_TIME,
  109. 0
  110. };
  111. const u32 nfs4_fs_locations_bitmap[2] = {
  112. FATTR4_WORD0_TYPE
  113. | FATTR4_WORD0_CHANGE
  114. | FATTR4_WORD0_SIZE
  115. | FATTR4_WORD0_FSID
  116. | FATTR4_WORD0_FILEID
  117. | FATTR4_WORD0_FS_LOCATIONS,
  118. FATTR4_WORD1_MODE
  119. | FATTR4_WORD1_NUMLINKS
  120. | FATTR4_WORD1_OWNER
  121. | FATTR4_WORD1_OWNER_GROUP
  122. | FATTR4_WORD1_RAWDEV
  123. | FATTR4_WORD1_SPACE_USED
  124. | FATTR4_WORD1_TIME_ACCESS
  125. | FATTR4_WORD1_TIME_METADATA
  126. | FATTR4_WORD1_TIME_MODIFY
  127. | FATTR4_WORD1_MOUNTED_ON_FILEID
  128. };
  129. static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
  130. struct nfs4_readdir_arg *readdir)
  131. {
  132. __be32 *start, *p;
  133. BUG_ON(readdir->count < 80);
  134. if (cookie > 2) {
  135. readdir->cookie = cookie;
  136. memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
  137. return;
  138. }
  139. readdir->cookie = 0;
  140. memset(&readdir->verifier, 0, sizeof(readdir->verifier));
  141. if (cookie == 2)
  142. return;
  143. /*
  144. * NFSv4 servers do not return entries for '.' and '..'
  145. * Therefore, we fake these entries here. We let '.'
  146. * have cookie 0 and '..' have cookie 1. Note that
  147. * when talking to the server, we always send cookie 0
  148. * instead of 1 or 2.
  149. */
  150. start = p = kmap_atomic(*readdir->pages, KM_USER0);
  151. if (cookie == 0) {
  152. *p++ = xdr_one; /* next */
  153. *p++ = xdr_zero; /* cookie, first word */
  154. *p++ = xdr_one; /* cookie, second word */
  155. *p++ = xdr_one; /* entry len */
  156. memcpy(p, ".\0\0\0", 4); /* entry */
  157. p++;
  158. *p++ = xdr_one; /* bitmap length */
  159. *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
  160. *p++ = htonl(8); /* attribute buffer length */
  161. p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
  162. }
  163. *p++ = xdr_one; /* next */
  164. *p++ = xdr_zero; /* cookie, first word */
  165. *p++ = xdr_two; /* cookie, second word */
  166. *p++ = xdr_two; /* entry len */
  167. memcpy(p, "..\0\0", 4); /* entry */
  168. p++;
  169. *p++ = xdr_one; /* bitmap length */
  170. *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
  171. *p++ = htonl(8); /* attribute buffer length */
  172. p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
  173. readdir->pgbase = (char *)p - (char *)start;
  174. readdir->count -= readdir->pgbase;
  175. kunmap_atomic(start, KM_USER0);
  176. }
  177. static int nfs4_wait_clnt_recover(struct nfs_client *clp)
  178. {
  179. int res;
  180. might_sleep();
  181. res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
  182. nfs_wait_bit_killable, TASK_KILLABLE);
  183. return res;
  184. }
  185. static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
  186. {
  187. int res = 0;
  188. might_sleep();
  189. if (*timeout <= 0)
  190. *timeout = NFS4_POLL_RETRY_MIN;
  191. if (*timeout > NFS4_POLL_RETRY_MAX)
  192. *timeout = NFS4_POLL_RETRY_MAX;
  193. schedule_timeout_killable(*timeout);
  194. if (fatal_signal_pending(current))
  195. res = -ERESTARTSYS;
  196. *timeout <<= 1;
  197. return res;
  198. }
  199. /* This is the error handling routine for processes that are allowed
  200. * to sleep.
  201. */
  202. static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
  203. {
  204. struct nfs_client *clp = server->nfs_client;
  205. struct nfs4_state *state = exception->state;
  206. int ret = errorcode;
  207. exception->retry = 0;
  208. switch(errorcode) {
  209. case 0:
  210. return 0;
  211. case -NFS4ERR_ADMIN_REVOKED:
  212. case -NFS4ERR_BAD_STATEID:
  213. case -NFS4ERR_OPENMODE:
  214. if (state == NULL)
  215. break;
  216. nfs4_state_mark_reclaim_nograce(clp, state);
  217. case -NFS4ERR_STALE_CLIENTID:
  218. case -NFS4ERR_STALE_STATEID:
  219. case -NFS4ERR_EXPIRED:
  220. nfs4_schedule_state_recovery(clp);
  221. ret = nfs4_wait_clnt_recover(clp);
  222. if (ret == 0)
  223. exception->retry = 1;
  224. break;
  225. case -NFS4ERR_FILE_OPEN:
  226. case -NFS4ERR_GRACE:
  227. case -NFS4ERR_DELAY:
  228. ret = nfs4_delay(server->client, &exception->timeout);
  229. if (ret != 0)
  230. break;
  231. case -NFS4ERR_OLD_STATEID:
  232. exception->retry = 1;
  233. }
  234. /* We failed to handle the error */
  235. return nfs4_map_errors(ret);
  236. }
  237. static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
  238. {
  239. struct nfs_client *clp = server->nfs_client;
  240. spin_lock(&clp->cl_lock);
  241. if (time_before(clp->cl_last_renewal,timestamp))
  242. clp->cl_last_renewal = timestamp;
  243. spin_unlock(&clp->cl_lock);
  244. }
  245. #if defined(CONFIG_NFS_V4_1)
  246. /*
  247. * nfs4_free_slot - free a slot and efficiently update slot table.
  248. *
  249. * freeing a slot is trivially done by clearing its respective bit
  250. * in the bitmap.
  251. * If the freed slotid equals highest_used_slotid we want to update it
  252. * so that the server would be able to size down the slot table if needed,
  253. * otherwise we know that the highest_used_slotid is still in use.
  254. * When updating highest_used_slotid there may be "holes" in the bitmap
  255. * so we need to scan down from highest_used_slotid to 0 looking for the now
  256. * highest slotid in use.
  257. * If none found, highest_used_slotid is set to -1.
  258. */
  259. static void
  260. nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
  261. {
  262. int slotid = free_slotid;
  263. spin_lock(&tbl->slot_tbl_lock);
  264. /* clear used bit in bitmap */
  265. __clear_bit(slotid, tbl->used_slots);
  266. /* update highest_used_slotid when it is freed */
  267. if (slotid == tbl->highest_used_slotid) {
  268. slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
  269. if (slotid >= 0 && slotid < tbl->max_slots)
  270. tbl->highest_used_slotid = slotid;
  271. else
  272. tbl->highest_used_slotid = -1;
  273. }
  274. rpc_wake_up_next(&tbl->slot_tbl_waitq);
  275. spin_unlock(&tbl->slot_tbl_lock);
  276. dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
  277. free_slotid, tbl->highest_used_slotid);
  278. }
  279. void nfs41_sequence_free_slot(const struct nfs_client *clp,
  280. struct nfs4_sequence_res *res)
  281. {
  282. struct nfs4_slot_table *tbl;
  283. if (!nfs4_has_session(clp)) {
  284. dprintk("%s: No session\n", __func__);
  285. return;
  286. }
  287. tbl = &clp->cl_session->fc_slot_table;
  288. if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
  289. dprintk("%s: No slot\n", __func__);
  290. /* just wake up the next guy waiting since
  291. * we may have not consumed a slot after all */
  292. rpc_wake_up_next(&tbl->slot_tbl_waitq);
  293. return;
  294. }
  295. nfs4_free_slot(tbl, res->sr_slotid);
  296. res->sr_slotid = NFS4_MAX_SLOT_TABLE;
  297. }
  298. static void nfs41_sequence_done(struct nfs_client *clp,
  299. struct nfs4_sequence_res *res,
  300. int rpc_status)
  301. {
  302. unsigned long timestamp;
  303. struct nfs4_slot_table *tbl;
  304. struct nfs4_slot *slot;
  305. /*
  306. * sr_status remains 1 if an RPC level error occurred. The server
  307. * may or may not have processed the sequence operation..
  308. * Proceed as if the server received and processed the sequence
  309. * operation.
  310. */
  311. if (res->sr_status == 1)
  312. res->sr_status = NFS_OK;
  313. /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
  314. if (res->sr_slotid == NFS4_MAX_SLOT_TABLE)
  315. goto out;
  316. tbl = &clp->cl_session->fc_slot_table;
  317. slot = tbl->slots + res->sr_slotid;
  318. if (res->sr_status == 0) {
  319. /* Update the slot's sequence and clientid lease timer */
  320. ++slot->seq_nr;
  321. timestamp = res->sr_renewal_time;
  322. spin_lock(&clp->cl_lock);
  323. if (time_before(clp->cl_last_renewal, timestamp))
  324. clp->cl_last_renewal = timestamp;
  325. spin_unlock(&clp->cl_lock);
  326. return;
  327. }
  328. out:
  329. /* The session may be reset by one of the error handlers. */
  330. dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
  331. nfs41_sequence_free_slot(clp, res);
  332. }
  333. /*
  334. * nfs4_find_slot - efficiently look for a free slot
  335. *
  336. * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
  337. * If found, we mark the slot as used, update the highest_used_slotid,
  338. * and respectively set up the sequence operation args.
  339. * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
  340. *
  341. * Note: must be called with under the slot_tbl_lock.
  342. */
  343. static u8
  344. nfs4_find_slot(struct nfs4_slot_table *tbl, struct rpc_task *task)
  345. {
  346. int slotid;
  347. u8 ret_id = NFS4_MAX_SLOT_TABLE;
  348. BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
  349. dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
  350. __func__, tbl->used_slots[0], tbl->highest_used_slotid,
  351. tbl->max_slots);
  352. slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
  353. if (slotid >= tbl->max_slots)
  354. goto out;
  355. __set_bit(slotid, tbl->used_slots);
  356. if (slotid > tbl->highest_used_slotid)
  357. tbl->highest_used_slotid = slotid;
  358. ret_id = slotid;
  359. out:
  360. dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
  361. __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
  362. return ret_id;
  363. }
  364. static int nfs41_setup_sequence(struct nfs4_session *session,
  365. struct nfs4_sequence_args *args,
  366. struct nfs4_sequence_res *res,
  367. int cache_reply,
  368. struct rpc_task *task)
  369. {
  370. struct nfs4_slot *slot;
  371. struct nfs4_slot_table *tbl;
  372. u8 slotid;
  373. dprintk("--> %s\n", __func__);
  374. /* slot already allocated? */
  375. if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
  376. return 0;
  377. memset(res, 0, sizeof(*res));
  378. res->sr_slotid = NFS4_MAX_SLOT_TABLE;
  379. tbl = &session->fc_slot_table;
  380. spin_lock(&tbl->slot_tbl_lock);
  381. slotid = nfs4_find_slot(tbl, task);
  382. if (slotid == NFS4_MAX_SLOT_TABLE) {
  383. rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
  384. spin_unlock(&tbl->slot_tbl_lock);
  385. dprintk("<-- %s: no free slots\n", __func__);
  386. return -EAGAIN;
  387. }
  388. spin_unlock(&tbl->slot_tbl_lock);
  389. slot = tbl->slots + slotid;
  390. args->sa_slotid = slotid;
  391. args->sa_cache_this = cache_reply;
  392. dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
  393. res->sr_slotid = slotid;
  394. res->sr_renewal_time = jiffies;
  395. /*
  396. * sr_status is only set in decode_sequence, and so will remain
  397. * set to 1 if an rpc level failure occurs.
  398. */
  399. res->sr_status = 1;
  400. return 0;
  401. }
  402. int nfs4_setup_sequence(struct nfs_client *clp,
  403. struct nfs4_sequence_args *args,
  404. struct nfs4_sequence_res *res,
  405. int cache_reply,
  406. struct rpc_task *task)
  407. {
  408. int ret = 0;
  409. dprintk("--> %s clp %p session %p sr_slotid %d\n",
  410. __func__, clp, clp->cl_session, res->sr_slotid);
  411. if (!nfs4_has_session(clp))
  412. goto out;
  413. ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
  414. task);
  415. if (ret != -EAGAIN) {
  416. /* terminate rpc task */
  417. task->tk_status = ret;
  418. task->tk_action = NULL;
  419. }
  420. out:
  421. dprintk("<-- %s status=%d\n", __func__, ret);
  422. return ret;
  423. }
  424. struct nfs41_call_sync_data {
  425. struct nfs_client *clp;
  426. struct nfs4_sequence_args *seq_args;
  427. struct nfs4_sequence_res *seq_res;
  428. int cache_reply;
  429. };
  430. static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
  431. {
  432. struct nfs41_call_sync_data *data = calldata;
  433. dprintk("--> %s data->clp->cl_session %p\n", __func__,
  434. data->clp->cl_session);
  435. if (nfs4_setup_sequence(data->clp, data->seq_args,
  436. data->seq_res, data->cache_reply, task))
  437. return;
  438. rpc_call_start(task);
  439. }
  440. static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
  441. {
  442. struct nfs41_call_sync_data *data = calldata;
  443. nfs41_sequence_done(data->clp, data->seq_res, task->tk_status);
  444. nfs41_sequence_free_slot(data->clp, data->seq_res);
  445. }
  446. struct rpc_call_ops nfs41_call_sync_ops = {
  447. .rpc_call_prepare = nfs41_call_sync_prepare,
  448. .rpc_call_done = nfs41_call_sync_done,
  449. };
  450. static int nfs4_call_sync_sequence(struct nfs_client *clp,
  451. struct rpc_clnt *clnt,
  452. struct rpc_message *msg,
  453. struct nfs4_sequence_args *args,
  454. struct nfs4_sequence_res *res,
  455. int cache_reply)
  456. {
  457. int ret;
  458. struct rpc_task *task;
  459. struct nfs41_call_sync_data data = {
  460. .clp = clp,
  461. .seq_args = args,
  462. .seq_res = res,
  463. .cache_reply = cache_reply,
  464. };
  465. struct rpc_task_setup task_setup = {
  466. .rpc_client = clnt,
  467. .rpc_message = msg,
  468. .callback_ops = &nfs41_call_sync_ops,
  469. .callback_data = &data
  470. };
  471. res->sr_slotid = NFS4_MAX_SLOT_TABLE;
  472. task = rpc_run_task(&task_setup);
  473. if (IS_ERR(task))
  474. ret = PTR_ERR(task);
  475. else {
  476. ret = task->tk_status;
  477. rpc_put_task(task);
  478. }
  479. return ret;
  480. }
  481. int _nfs4_call_sync_session(struct nfs_server *server,
  482. struct rpc_message *msg,
  483. struct nfs4_sequence_args *args,
  484. struct nfs4_sequence_res *res,
  485. int cache_reply)
  486. {
  487. return nfs4_call_sync_sequence(server->nfs_client, server->client,
  488. msg, args, res, cache_reply);
  489. }
  490. #endif /* CONFIG_NFS_V4_1 */
  491. int _nfs4_call_sync(struct nfs_server *server,
  492. struct rpc_message *msg,
  493. struct nfs4_sequence_args *args,
  494. struct nfs4_sequence_res *res,
  495. int cache_reply)
  496. {
  497. args->sa_session = res->sr_session = NULL;
  498. return rpc_call_sync(server->client, msg, 0);
  499. }
  500. #define nfs4_call_sync(server, msg, args, res, cache_reply) \
  501. (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
  502. &(res)->seq_res, (cache_reply))
  503. static void nfs4_sequence_done(const struct nfs_server *server,
  504. struct nfs4_sequence_res *res, int rpc_status)
  505. {
  506. #ifdef CONFIG_NFS_V4_1
  507. if (nfs4_has_session(server->nfs_client))
  508. nfs41_sequence_done(server->nfs_client, res, rpc_status);
  509. #endif /* CONFIG_NFS_V4_1 */
  510. }
  511. /* no restart, therefore free slot here */
  512. static void nfs4_sequence_done_free_slot(const struct nfs_server *server,
  513. struct nfs4_sequence_res *res,
  514. int rpc_status)
  515. {
  516. nfs4_sequence_done(server, res, rpc_status);
  517. nfs4_sequence_free_slot(server->nfs_client, res);
  518. }
  519. static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
  520. {
  521. struct nfs_inode *nfsi = NFS_I(dir);
  522. spin_lock(&dir->i_lock);
  523. nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
  524. if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
  525. nfs_force_lookup_revalidate(dir);
  526. nfsi->change_attr = cinfo->after;
  527. spin_unlock(&dir->i_lock);
  528. }
  529. struct nfs4_opendata {
  530. struct kref kref;
  531. struct nfs_openargs o_arg;
  532. struct nfs_openres o_res;
  533. struct nfs_open_confirmargs c_arg;
  534. struct nfs_open_confirmres c_res;
  535. struct nfs_fattr f_attr;
  536. struct nfs_fattr dir_attr;
  537. struct path path;
  538. struct dentry *dir;
  539. struct nfs4_state_owner *owner;
  540. struct nfs4_state *state;
  541. struct iattr attrs;
  542. unsigned long timestamp;
  543. unsigned int rpc_done : 1;
  544. int rpc_status;
  545. int cancelled;
  546. };
  547. static void nfs4_init_opendata_res(struct nfs4_opendata *p)
  548. {
  549. p->o_res.f_attr = &p->f_attr;
  550. p->o_res.dir_attr = &p->dir_attr;
  551. p->o_res.seqid = p->o_arg.seqid;
  552. p->c_res.seqid = p->c_arg.seqid;
  553. p->o_res.server = p->o_arg.server;
  554. nfs_fattr_init(&p->f_attr);
  555. nfs_fattr_init(&p->dir_attr);
  556. }
  557. static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
  558. struct nfs4_state_owner *sp, fmode_t fmode, int flags,
  559. const struct iattr *attrs)
  560. {
  561. struct dentry *parent = dget_parent(path->dentry);
  562. struct inode *dir = parent->d_inode;
  563. struct nfs_server *server = NFS_SERVER(dir);
  564. struct nfs4_opendata *p;
  565. p = kzalloc(sizeof(*p), GFP_KERNEL);
  566. if (p == NULL)
  567. goto err;
  568. p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
  569. if (p->o_arg.seqid == NULL)
  570. goto err_free;
  571. p->path.mnt = mntget(path->mnt);
  572. p->path.dentry = dget(path->dentry);
  573. p->dir = parent;
  574. p->owner = sp;
  575. atomic_inc(&sp->so_count);
  576. p->o_arg.fh = NFS_FH(dir);
  577. p->o_arg.open_flags = flags;
  578. p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
  579. p->o_arg.clientid = server->nfs_client->cl_clientid;
  580. p->o_arg.id = sp->so_owner_id.id;
  581. p->o_arg.name = &p->path.dentry->d_name;
  582. p->o_arg.server = server;
  583. p->o_arg.bitmask = server->attr_bitmask;
  584. p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
  585. p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
  586. if (flags & O_EXCL) {
  587. u32 *s = (u32 *) p->o_arg.u.verifier.data;
  588. s[0] = jiffies;
  589. s[1] = current->pid;
  590. } else if (flags & O_CREAT) {
  591. p->o_arg.u.attrs = &p->attrs;
  592. memcpy(&p->attrs, attrs, sizeof(p->attrs));
  593. }
  594. p->c_arg.fh = &p->o_res.fh;
  595. p->c_arg.stateid = &p->o_res.stateid;
  596. p->c_arg.seqid = p->o_arg.seqid;
  597. nfs4_init_opendata_res(p);
  598. kref_init(&p->kref);
  599. return p;
  600. err_free:
  601. kfree(p);
  602. err:
  603. dput(parent);
  604. return NULL;
  605. }
  606. static void nfs4_opendata_free(struct kref *kref)
  607. {
  608. struct nfs4_opendata *p = container_of(kref,
  609. struct nfs4_opendata, kref);
  610. nfs_free_seqid(p->o_arg.seqid);
  611. if (p->state != NULL)
  612. nfs4_put_open_state(p->state);
  613. nfs4_put_state_owner(p->owner);
  614. dput(p->dir);
  615. path_put(&p->path);
  616. kfree(p);
  617. }
  618. static void nfs4_opendata_put(struct nfs4_opendata *p)
  619. {
  620. if (p != NULL)
  621. kref_put(&p->kref, nfs4_opendata_free);
  622. }
  623. static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
  624. {
  625. int ret;
  626. ret = rpc_wait_for_completion_task(task);
  627. return ret;
  628. }
  629. static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
  630. {
  631. int ret = 0;
  632. if (open_mode & O_EXCL)
  633. goto out;
  634. switch (mode & (FMODE_READ|FMODE_WRITE)) {
  635. case FMODE_READ:
  636. ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0;
  637. break;
  638. case FMODE_WRITE:
  639. ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0;
  640. break;
  641. case FMODE_READ|FMODE_WRITE:
  642. ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
  643. }
  644. out:
  645. return ret;
  646. }
  647. static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
  648. {
  649. if ((delegation->type & fmode) != fmode)
  650. return 0;
  651. if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
  652. return 0;
  653. nfs_mark_delegation_referenced(delegation);
  654. return 1;
  655. }
  656. static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
  657. {
  658. switch (fmode) {
  659. case FMODE_WRITE:
  660. state->n_wronly++;
  661. break;
  662. case FMODE_READ:
  663. state->n_rdonly++;
  664. break;
  665. case FMODE_READ|FMODE_WRITE:
  666. state->n_rdwr++;
  667. }
  668. nfs4_state_set_mode_locked(state, state->state | fmode);
  669. }
  670. static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
  671. {
  672. if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
  673. memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
  674. memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
  675. switch (fmode) {
  676. case FMODE_READ:
  677. set_bit(NFS_O_RDONLY_STATE, &state->flags);
  678. break;
  679. case FMODE_WRITE:
  680. set_bit(NFS_O_WRONLY_STATE, &state->flags);
  681. break;
  682. case FMODE_READ|FMODE_WRITE:
  683. set_bit(NFS_O_RDWR_STATE, &state->flags);
  684. }
  685. }
  686. static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
  687. {
  688. write_seqlock(&state->seqlock);
  689. nfs_set_open_stateid_locked(state, stateid, fmode);
  690. write_sequnlock(&state->seqlock);
  691. }
  692. static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
  693. {
  694. /*
  695. * Protect the call to nfs4_state_set_mode_locked and
  696. * serialise the stateid update
  697. */
  698. write_seqlock(&state->seqlock);
  699. if (deleg_stateid != NULL) {
  700. memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
  701. set_bit(NFS_DELEGATED_STATE, &state->flags);
  702. }
  703. if (open_stateid != NULL)
  704. nfs_set_open_stateid_locked(state, open_stateid, fmode);
  705. write_sequnlock(&state->seqlock);
  706. spin_lock(&state->owner->so_lock);
  707. update_open_stateflags(state, fmode);
  708. spin_unlock(&state->owner->so_lock);
  709. }
  710. static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
  711. {
  712. struct nfs_inode *nfsi = NFS_I(state->inode);
  713. struct nfs_delegation *deleg_cur;
  714. int ret = 0;
  715. fmode &= (FMODE_READ|FMODE_WRITE);
  716. rcu_read_lock();
  717. deleg_cur = rcu_dereference(nfsi->delegation);
  718. if (deleg_cur == NULL)
  719. goto no_delegation;
  720. spin_lock(&deleg_cur->lock);
  721. if (nfsi->delegation != deleg_cur ||
  722. (deleg_cur->type & fmode) != fmode)
  723. goto no_delegation_unlock;
  724. if (delegation == NULL)
  725. delegation = &deleg_cur->stateid;
  726. else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
  727. goto no_delegation_unlock;
  728. nfs_mark_delegation_referenced(deleg_cur);
  729. __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
  730. ret = 1;
  731. no_delegation_unlock:
  732. spin_unlock(&deleg_cur->lock);
  733. no_delegation:
  734. rcu_read_unlock();
  735. if (!ret && open_stateid != NULL) {
  736. __update_open_stateid(state, open_stateid, NULL, fmode);
  737. ret = 1;
  738. }
  739. return ret;
  740. }
  741. static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
  742. {
  743. struct nfs_delegation *delegation;
  744. rcu_read_lock();
  745. delegation = rcu_dereference(NFS_I(inode)->delegation);
  746. if (delegation == NULL || (delegation->type & fmode) == fmode) {
  747. rcu_read_unlock();
  748. return;
  749. }
  750. rcu_read_unlock();
  751. nfs_inode_return_delegation(inode);
  752. }
  753. static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
  754. {
  755. struct nfs4_state *state = opendata->state;
  756. struct nfs_inode *nfsi = NFS_I(state->inode);
  757. struct nfs_delegation *delegation;
  758. int open_mode = opendata->o_arg.open_flags & O_EXCL;
  759. fmode_t fmode = opendata->o_arg.fmode;
  760. nfs4_stateid stateid;
  761. int ret = -EAGAIN;
  762. for (;;) {
  763. if (can_open_cached(state, fmode, open_mode)) {
  764. spin_lock(&state->owner->so_lock);
  765. if (can_open_cached(state, fmode, open_mode)) {
  766. update_open_stateflags(state, fmode);
  767. spin_unlock(&state->owner->so_lock);
  768. goto out_return_state;
  769. }
  770. spin_unlock(&state->owner->so_lock);
  771. }
  772. rcu_read_lock();
  773. delegation = rcu_dereference(nfsi->delegation);
  774. if (delegation == NULL ||
  775. !can_open_delegated(delegation, fmode)) {
  776. rcu_read_unlock();
  777. break;
  778. }
  779. /* Save the delegation */
  780. memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
  781. rcu_read_unlock();
  782. ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
  783. if (ret != 0)
  784. goto out;
  785. ret = -EAGAIN;
  786. /* Try to update the stateid using the delegation */
  787. if (update_open_stateid(state, NULL, &stateid, fmode))
  788. goto out_return_state;
  789. }
  790. out:
  791. return ERR_PTR(ret);
  792. out_return_state:
  793. atomic_inc(&state->count);
  794. return state;
  795. }
  796. static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
  797. {
  798. struct inode *inode;
  799. struct nfs4_state *state = NULL;
  800. struct nfs_delegation *delegation;
  801. int ret;
  802. if (!data->rpc_done) {
  803. state = nfs4_try_open_cached(data);
  804. goto out;
  805. }
  806. ret = -EAGAIN;
  807. if (!(data->f_attr.valid & NFS_ATTR_FATTR))
  808. goto err;
  809. inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
  810. ret = PTR_ERR(inode);
  811. if (IS_ERR(inode))
  812. goto err;
  813. ret = -ENOMEM;
  814. state = nfs4_get_open_state(inode, data->owner);
  815. if (state == NULL)
  816. goto err_put_inode;
  817. if (data->o_res.delegation_type != 0) {
  818. int delegation_flags = 0;
  819. rcu_read_lock();
  820. delegation = rcu_dereference(NFS_I(inode)->delegation);
  821. if (delegation)
  822. delegation_flags = delegation->flags;
  823. rcu_read_unlock();
  824. if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
  825. nfs_inode_set_delegation(state->inode,
  826. data->owner->so_cred,
  827. &data->o_res);
  828. else
  829. nfs_inode_reclaim_delegation(state->inode,
  830. data->owner->so_cred,
  831. &data->o_res);
  832. }
  833. update_open_stateid(state, &data->o_res.stateid, NULL,
  834. data->o_arg.fmode);
  835. iput(inode);
  836. out:
  837. return state;
  838. err_put_inode:
  839. iput(inode);
  840. err:
  841. return ERR_PTR(ret);
  842. }
  843. static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
  844. {
  845. struct nfs_inode *nfsi = NFS_I(state->inode);
  846. struct nfs_open_context *ctx;
  847. spin_lock(&state->inode->i_lock);
  848. list_for_each_entry(ctx, &nfsi->open_files, list) {
  849. if (ctx->state != state)
  850. continue;
  851. get_nfs_open_context(ctx);
  852. spin_unlock(&state->inode->i_lock);
  853. return ctx;
  854. }
  855. spin_unlock(&state->inode->i_lock);
  856. return ERR_PTR(-ENOENT);
  857. }
  858. static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
  859. {
  860. struct nfs4_opendata *opendata;
  861. opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
  862. if (opendata == NULL)
  863. return ERR_PTR(-ENOMEM);
  864. opendata->state = state;
  865. atomic_inc(&state->count);
  866. return opendata;
  867. }
  868. static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
  869. {
  870. struct nfs4_state *newstate;
  871. int ret;
  872. opendata->o_arg.open_flags = 0;
  873. opendata->o_arg.fmode = fmode;
  874. memset(&opendata->o_res, 0, sizeof(opendata->o_res));
  875. memset(&opendata->c_res, 0, sizeof(opendata->c_res));
  876. nfs4_init_opendata_res(opendata);
  877. ret = _nfs4_proc_open(opendata);
  878. if (ret != 0)
  879. return ret;
  880. newstate = nfs4_opendata_to_nfs4_state(opendata);
  881. if (IS_ERR(newstate))
  882. return PTR_ERR(newstate);
  883. nfs4_close_state(&opendata->path, newstate, fmode);
  884. *res = newstate;
  885. return 0;
  886. }
  887. static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
  888. {
  889. struct nfs4_state *newstate;
  890. int ret;
  891. /* memory barrier prior to reading state->n_* */
  892. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  893. smp_rmb();
  894. if (state->n_rdwr != 0) {
  895. ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
  896. if (ret != 0)
  897. return ret;
  898. if (newstate != state)
  899. return -ESTALE;
  900. }
  901. if (state->n_wronly != 0) {
  902. ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
  903. if (ret != 0)
  904. return ret;
  905. if (newstate != state)
  906. return -ESTALE;
  907. }
  908. if (state->n_rdonly != 0) {
  909. ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
  910. if (ret != 0)
  911. return ret;
  912. if (newstate != state)
  913. return -ESTALE;
  914. }
  915. /*
  916. * We may have performed cached opens for all three recoveries.
  917. * Check if we need to update the current stateid.
  918. */
  919. if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
  920. memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
  921. write_seqlock(&state->seqlock);
  922. if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
  923. memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
  924. write_sequnlock(&state->seqlock);
  925. }
  926. return 0;
  927. }
  928. /*
  929. * OPEN_RECLAIM:
  930. * reclaim state on the server after a reboot.
  931. */
  932. static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
  933. {
  934. struct nfs_delegation *delegation;
  935. struct nfs4_opendata *opendata;
  936. fmode_t delegation_type = 0;
  937. int status;
  938. opendata = nfs4_open_recoverdata_alloc(ctx, state);
  939. if (IS_ERR(opendata))
  940. return PTR_ERR(opendata);
  941. opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
  942. opendata->o_arg.fh = NFS_FH(state->inode);
  943. rcu_read_lock();
  944. delegation = rcu_dereference(NFS_I(state->inode)->delegation);
  945. if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
  946. delegation_type = delegation->type;
  947. rcu_read_unlock();
  948. opendata->o_arg.u.delegation_type = delegation_type;
  949. status = nfs4_open_recover(opendata, state);
  950. nfs4_opendata_put(opendata);
  951. return status;
  952. }
  953. static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
  954. {
  955. struct nfs_server *server = NFS_SERVER(state->inode);
  956. struct nfs4_exception exception = { };
  957. int err;
  958. do {
  959. err = _nfs4_do_open_reclaim(ctx, state);
  960. if (err != -NFS4ERR_DELAY)
  961. break;
  962. nfs4_handle_exception(server, err, &exception);
  963. } while (exception.retry);
  964. return err;
  965. }
  966. static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
  967. {
  968. struct nfs_open_context *ctx;
  969. int ret;
  970. ctx = nfs4_state_find_open_context(state);
  971. if (IS_ERR(ctx))
  972. return PTR_ERR(ctx);
  973. ret = nfs4_do_open_reclaim(ctx, state);
  974. put_nfs_open_context(ctx);
  975. return ret;
  976. }
  977. static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
  978. {
  979. struct nfs4_opendata *opendata;
  980. int ret;
  981. opendata = nfs4_open_recoverdata_alloc(ctx, state);
  982. if (IS_ERR(opendata))
  983. return PTR_ERR(opendata);
  984. opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
  985. memcpy(opendata->o_arg.u.delegation.data, stateid->data,
  986. sizeof(opendata->o_arg.u.delegation.data));
  987. ret = nfs4_open_recover(opendata, state);
  988. nfs4_opendata_put(opendata);
  989. return ret;
  990. }
  991. int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
  992. {
  993. struct nfs4_exception exception = { };
  994. struct nfs_server *server = NFS_SERVER(state->inode);
  995. int err;
  996. do {
  997. err = _nfs4_open_delegation_recall(ctx, state, stateid);
  998. switch (err) {
  999. case 0:
  1000. return err;
  1001. case -NFS4ERR_STALE_CLIENTID:
  1002. case -NFS4ERR_STALE_STATEID:
  1003. case -NFS4ERR_EXPIRED:
  1004. /* Don't recall a delegation if it was lost */
  1005. nfs4_schedule_state_recovery(server->nfs_client);
  1006. return err;
  1007. }
  1008. err = nfs4_handle_exception(server, err, &exception);
  1009. } while (exception.retry);
  1010. return err;
  1011. }
  1012. static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
  1013. {
  1014. struct nfs4_opendata *data = calldata;
  1015. data->rpc_status = task->tk_status;
  1016. if (RPC_ASSASSINATED(task))
  1017. return;
  1018. if (data->rpc_status == 0) {
  1019. memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
  1020. sizeof(data->o_res.stateid.data));
  1021. nfs_confirm_seqid(&data->owner->so_seqid, 0);
  1022. renew_lease(data->o_res.server, data->timestamp);
  1023. data->rpc_done = 1;
  1024. }
  1025. }
  1026. static void nfs4_open_confirm_release(void *calldata)
  1027. {
  1028. struct nfs4_opendata *data = calldata;
  1029. struct nfs4_state *state = NULL;
  1030. /* If this request hasn't been cancelled, do nothing */
  1031. if (data->cancelled == 0)
  1032. goto out_free;
  1033. /* In case of error, no cleanup! */
  1034. if (!data->rpc_done)
  1035. goto out_free;
  1036. state = nfs4_opendata_to_nfs4_state(data);
  1037. if (!IS_ERR(state))
  1038. nfs4_close_state(&data->path, state, data->o_arg.fmode);
  1039. out_free:
  1040. nfs4_opendata_put(data);
  1041. }
  1042. static const struct rpc_call_ops nfs4_open_confirm_ops = {
  1043. .rpc_call_done = nfs4_open_confirm_done,
  1044. .rpc_release = nfs4_open_confirm_release,
  1045. };
  1046. /*
  1047. * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
  1048. */
  1049. static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
  1050. {
  1051. struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
  1052. struct rpc_task *task;
  1053. struct rpc_message msg = {
  1054. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
  1055. .rpc_argp = &data->c_arg,
  1056. .rpc_resp = &data->c_res,
  1057. .rpc_cred = data->owner->so_cred,
  1058. };
  1059. struct rpc_task_setup task_setup_data = {
  1060. .rpc_client = server->client,
  1061. .rpc_message = &msg,
  1062. .callback_ops = &nfs4_open_confirm_ops,
  1063. .callback_data = data,
  1064. .workqueue = nfsiod_workqueue,
  1065. .flags = RPC_TASK_ASYNC,
  1066. };
  1067. int status;
  1068. kref_get(&data->kref);
  1069. data->rpc_done = 0;
  1070. data->rpc_status = 0;
  1071. data->timestamp = jiffies;
  1072. task = rpc_run_task(&task_setup_data);
  1073. if (IS_ERR(task))
  1074. return PTR_ERR(task);
  1075. status = nfs4_wait_for_completion_rpc_task(task);
  1076. if (status != 0) {
  1077. data->cancelled = 1;
  1078. smp_wmb();
  1079. } else
  1080. status = data->rpc_status;
  1081. rpc_put_task(task);
  1082. return status;
  1083. }
  1084. static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
  1085. {
  1086. struct nfs4_opendata *data = calldata;
  1087. struct nfs4_state_owner *sp = data->owner;
  1088. if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
  1089. return;
  1090. /*
  1091. * Check if we still need to send an OPEN call, or if we can use
  1092. * a delegation instead.
  1093. */
  1094. if (data->state != NULL) {
  1095. struct nfs_delegation *delegation;
  1096. if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
  1097. goto out_no_action;
  1098. rcu_read_lock();
  1099. delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
  1100. if (delegation != NULL &&
  1101. test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
  1102. rcu_read_unlock();
  1103. goto out_no_action;
  1104. }
  1105. rcu_read_unlock();
  1106. }
  1107. /* Update sequence id. */
  1108. data->o_arg.id = sp->so_owner_id.id;
  1109. data->o_arg.clientid = sp->so_client->cl_clientid;
  1110. if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
  1111. task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
  1112. nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
  1113. }
  1114. data->timestamp = jiffies;
  1115. rpc_call_start(task);
  1116. return;
  1117. out_no_action:
  1118. task->tk_action = NULL;
  1119. }
  1120. static void nfs4_open_done(struct rpc_task *task, void *calldata)
  1121. {
  1122. struct nfs4_opendata *data = calldata;
  1123. data->rpc_status = task->tk_status;
  1124. if (RPC_ASSASSINATED(task))
  1125. return;
  1126. if (task->tk_status == 0) {
  1127. switch (data->o_res.f_attr->mode & S_IFMT) {
  1128. case S_IFREG:
  1129. break;
  1130. case S_IFLNK:
  1131. data->rpc_status = -ELOOP;
  1132. break;
  1133. case S_IFDIR:
  1134. data->rpc_status = -EISDIR;
  1135. break;
  1136. default:
  1137. data->rpc_status = -ENOTDIR;
  1138. }
  1139. renew_lease(data->o_res.server, data->timestamp);
  1140. if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
  1141. nfs_confirm_seqid(&data->owner->so_seqid, 0);
  1142. }
  1143. data->rpc_done = 1;
  1144. }
  1145. static void nfs4_open_release(void *calldata)
  1146. {
  1147. struct nfs4_opendata *data = calldata;
  1148. struct nfs4_state *state = NULL;
  1149. /* If this request hasn't been cancelled, do nothing */
  1150. if (data->cancelled == 0)
  1151. goto out_free;
  1152. /* In case of error, no cleanup! */
  1153. if (data->rpc_status != 0 || !data->rpc_done)
  1154. goto out_free;
  1155. /* In case we need an open_confirm, no cleanup! */
  1156. if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
  1157. goto out_free;
  1158. state = nfs4_opendata_to_nfs4_state(data);
  1159. if (!IS_ERR(state))
  1160. nfs4_close_state(&data->path, state, data->o_arg.fmode);
  1161. out_free:
  1162. nfs4_opendata_put(data);
  1163. }
  1164. static const struct rpc_call_ops nfs4_open_ops = {
  1165. .rpc_call_prepare = nfs4_open_prepare,
  1166. .rpc_call_done = nfs4_open_done,
  1167. .rpc_release = nfs4_open_release,
  1168. };
  1169. /*
  1170. * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
  1171. */
  1172. static int _nfs4_proc_open(struct nfs4_opendata *data)
  1173. {
  1174. struct inode *dir = data->dir->d_inode;
  1175. struct nfs_server *server = NFS_SERVER(dir);
  1176. struct nfs_openargs *o_arg = &data->o_arg;
  1177. struct nfs_openres *o_res = &data->o_res;
  1178. struct rpc_task *task;
  1179. struct rpc_message msg = {
  1180. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
  1181. .rpc_argp = o_arg,
  1182. .rpc_resp = o_res,
  1183. .rpc_cred = data->owner->so_cred,
  1184. };
  1185. struct rpc_task_setup task_setup_data = {
  1186. .rpc_client = server->client,
  1187. .rpc_message = &msg,
  1188. .callback_ops = &nfs4_open_ops,
  1189. .callback_data = data,
  1190. .workqueue = nfsiod_workqueue,
  1191. .flags = RPC_TASK_ASYNC,
  1192. };
  1193. int status;
  1194. kref_get(&data->kref);
  1195. data->rpc_done = 0;
  1196. data->rpc_status = 0;
  1197. data->cancelled = 0;
  1198. task = rpc_run_task(&task_setup_data);
  1199. if (IS_ERR(task))
  1200. return PTR_ERR(task);
  1201. status = nfs4_wait_for_completion_rpc_task(task);
  1202. if (status != 0) {
  1203. data->cancelled = 1;
  1204. smp_wmb();
  1205. } else
  1206. status = data->rpc_status;
  1207. rpc_put_task(task);
  1208. if (status != 0 || !data->rpc_done)
  1209. return status;
  1210. if (o_res->fh.size == 0)
  1211. _nfs4_proc_lookup(dir, o_arg->name, &o_res->fh, o_res->f_attr);
  1212. if (o_arg->open_flags & O_CREAT) {
  1213. update_changeattr(dir, &o_res->cinfo);
  1214. nfs_post_op_update_inode(dir, o_res->dir_attr);
  1215. } else
  1216. nfs_refresh_inode(dir, o_res->dir_attr);
  1217. if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
  1218. status = _nfs4_proc_open_confirm(data);
  1219. if (status != 0)
  1220. return status;
  1221. }
  1222. if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
  1223. _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
  1224. return 0;
  1225. }
  1226. static int nfs4_recover_expired_lease(struct nfs_server *server)
  1227. {
  1228. struct nfs_client *clp = server->nfs_client;
  1229. int ret;
  1230. for (;;) {
  1231. ret = nfs4_wait_clnt_recover(clp);
  1232. if (ret != 0)
  1233. return ret;
  1234. if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
  1235. !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
  1236. break;
  1237. nfs4_schedule_state_recovery(clp);
  1238. }
  1239. return 0;
  1240. }
  1241. /*
  1242. * OPEN_EXPIRED:
  1243. * reclaim state on the server after a network partition.
  1244. * Assumes caller holds the appropriate lock
  1245. */
  1246. static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
  1247. {
  1248. struct nfs4_opendata *opendata;
  1249. int ret;
  1250. opendata = nfs4_open_recoverdata_alloc(ctx, state);
  1251. if (IS_ERR(opendata))
  1252. return PTR_ERR(opendata);
  1253. ret = nfs4_open_recover(opendata, state);
  1254. if (ret == -ESTALE)
  1255. d_drop(ctx->path.dentry);
  1256. nfs4_opendata_put(opendata);
  1257. return ret;
  1258. }
  1259. static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
  1260. {
  1261. struct nfs_server *server = NFS_SERVER(state->inode);
  1262. struct nfs4_exception exception = { };
  1263. int err;
  1264. do {
  1265. err = _nfs4_open_expired(ctx, state);
  1266. if (err != -NFS4ERR_DELAY)
  1267. break;
  1268. nfs4_handle_exception(server, err, &exception);
  1269. } while (exception.retry);
  1270. return err;
  1271. }
  1272. static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
  1273. {
  1274. struct nfs_open_context *ctx;
  1275. int ret;
  1276. ctx = nfs4_state_find_open_context(state);
  1277. if (IS_ERR(ctx))
  1278. return PTR_ERR(ctx);
  1279. ret = nfs4_do_open_expired(ctx, state);
  1280. put_nfs_open_context(ctx);
  1281. return ret;
  1282. }
  1283. /*
  1284. * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
  1285. * fields corresponding to attributes that were used to store the verifier.
  1286. * Make sure we clobber those fields in the later setattr call
  1287. */
  1288. static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
  1289. {
  1290. if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
  1291. !(sattr->ia_valid & ATTR_ATIME_SET))
  1292. sattr->ia_valid |= ATTR_ATIME;
  1293. if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
  1294. !(sattr->ia_valid & ATTR_MTIME_SET))
  1295. sattr->ia_valid |= ATTR_MTIME;
  1296. }
  1297. /*
  1298. * Returns a referenced nfs4_state
  1299. */
  1300. static int _nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
  1301. {
  1302. struct nfs4_state_owner *sp;
  1303. struct nfs4_state *state = NULL;
  1304. struct nfs_server *server = NFS_SERVER(dir);
  1305. struct nfs4_opendata *opendata;
  1306. int status;
  1307. /* Protect against reboot recovery conflicts */
  1308. status = -ENOMEM;
  1309. if (!(sp = nfs4_get_state_owner(server, cred))) {
  1310. dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
  1311. goto out_err;
  1312. }
  1313. status = nfs4_recover_expired_lease(server);
  1314. if (status != 0)
  1315. goto err_put_state_owner;
  1316. if (path->dentry->d_inode != NULL)
  1317. nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
  1318. status = -ENOMEM;
  1319. opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
  1320. if (opendata == NULL)
  1321. goto err_put_state_owner;
  1322. if (path->dentry->d_inode != NULL)
  1323. opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
  1324. status = _nfs4_proc_open(opendata);
  1325. if (status != 0)
  1326. goto err_opendata_put;
  1327. if (opendata->o_arg.open_flags & O_EXCL)
  1328. nfs4_exclusive_attrset(opendata, sattr);
  1329. state = nfs4_opendata_to_nfs4_state(opendata);
  1330. status = PTR_ERR(state);
  1331. if (IS_ERR(state))
  1332. goto err_opendata_put;
  1333. nfs4_opendata_put(opendata);
  1334. nfs4_put_state_owner(sp);
  1335. *res = state;
  1336. return 0;
  1337. err_opendata_put:
  1338. nfs4_opendata_put(opendata);
  1339. err_put_state_owner:
  1340. nfs4_put_state_owner(sp);
  1341. out_err:
  1342. *res = NULL;
  1343. return status;
  1344. }
  1345. static struct nfs4_state *nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred)
  1346. {
  1347. struct nfs4_exception exception = { };
  1348. struct nfs4_state *res;
  1349. int status;
  1350. do {
  1351. status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
  1352. if (status == 0)
  1353. break;
  1354. /* NOTE: BAD_SEQID means the server and client disagree about the
  1355. * book-keeping w.r.t. state-changing operations
  1356. * (OPEN/CLOSE/LOCK/LOCKU...)
  1357. * It is actually a sign of a bug on the client or on the server.
  1358. *
  1359. * If we receive a BAD_SEQID error in the particular case of
  1360. * doing an OPEN, we assume that nfs_increment_open_seqid() will
  1361. * have unhashed the old state_owner for us, and that we can
  1362. * therefore safely retry using a new one. We should still warn
  1363. * the user though...
  1364. */
  1365. if (status == -NFS4ERR_BAD_SEQID) {
  1366. printk(KERN_WARNING "NFS: v4 server %s "
  1367. " returned a bad sequence-id error!\n",
  1368. NFS_SERVER(dir)->nfs_client->cl_hostname);
  1369. exception.retry = 1;
  1370. continue;
  1371. }
  1372. /*
  1373. * BAD_STATEID on OPEN means that the server cancelled our
  1374. * state before it received the OPEN_CONFIRM.
  1375. * Recover by retrying the request as per the discussion
  1376. * on Page 181 of RFC3530.
  1377. */
  1378. if (status == -NFS4ERR_BAD_STATEID) {
  1379. exception.retry = 1;
  1380. continue;
  1381. }
  1382. if (status == -EAGAIN) {
  1383. /* We must have found a delegation */
  1384. exception.retry = 1;
  1385. continue;
  1386. }
  1387. res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
  1388. status, &exception));
  1389. } while (exception.retry);
  1390. return res;
  1391. }
  1392. static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
  1393. struct nfs_fattr *fattr, struct iattr *sattr,
  1394. struct nfs4_state *state)
  1395. {
  1396. struct nfs_server *server = NFS_SERVER(inode);
  1397. struct nfs_setattrargs arg = {
  1398. .fh = NFS_FH(inode),
  1399. .iap = sattr,
  1400. .server = server,
  1401. .bitmask = server->attr_bitmask,
  1402. };
  1403. struct nfs_setattrres res = {
  1404. .fattr = fattr,
  1405. .server = server,
  1406. };
  1407. struct rpc_message msg = {
  1408. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
  1409. .rpc_argp = &arg,
  1410. .rpc_resp = &res,
  1411. .rpc_cred = cred,
  1412. };
  1413. unsigned long timestamp = jiffies;
  1414. int status;
  1415. nfs_fattr_init(fattr);
  1416. if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
  1417. /* Use that stateid */
  1418. } else if (state != NULL) {
  1419. nfs4_copy_stateid(&arg.stateid, state, current->files);
  1420. } else
  1421. memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
  1422. status = nfs4_call_sync(server, &msg, &arg, &res, 1);
  1423. if (status == 0 && state != NULL)
  1424. renew_lease(server, timestamp);
  1425. return status;
  1426. }
  1427. static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
  1428. struct nfs_fattr *fattr, struct iattr *sattr,
  1429. struct nfs4_state *state)
  1430. {
  1431. struct nfs_server *server = NFS_SERVER(inode);
  1432. struct nfs4_exception exception = { };
  1433. int err;
  1434. do {
  1435. err = nfs4_handle_exception(server,
  1436. _nfs4_do_setattr(inode, cred, fattr, sattr, state),
  1437. &exception);
  1438. } while (exception.retry);
  1439. return err;
  1440. }
  1441. struct nfs4_closedata {
  1442. struct path path;
  1443. struct inode *inode;
  1444. struct nfs4_state *state;
  1445. struct nfs_closeargs arg;
  1446. struct nfs_closeres res;
  1447. struct nfs_fattr fattr;
  1448. unsigned long timestamp;
  1449. };
  1450. static void nfs4_free_closedata(void *data)
  1451. {
  1452. struct nfs4_closedata *calldata = data;
  1453. struct nfs4_state_owner *sp = calldata->state->owner;
  1454. nfs4_put_open_state(calldata->state);
  1455. nfs_free_seqid(calldata->arg.seqid);
  1456. nfs4_put_state_owner(sp);
  1457. path_put(&calldata->path);
  1458. kfree(calldata);
  1459. }
  1460. static void nfs4_close_done(struct rpc_task *task, void *data)
  1461. {
  1462. struct nfs4_closedata *calldata = data;
  1463. struct nfs4_state *state = calldata->state;
  1464. struct nfs_server *server = NFS_SERVER(calldata->inode);
  1465. nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
  1466. if (RPC_ASSASSINATED(task))
  1467. return;
  1468. /* hmm. we are done with the inode, and in the process of freeing
  1469. * the state_owner. we keep this around to process errors
  1470. */
  1471. switch (task->tk_status) {
  1472. case 0:
  1473. nfs_set_open_stateid(state, &calldata->res.stateid, 0);
  1474. renew_lease(server, calldata->timestamp);
  1475. break;
  1476. case -NFS4ERR_STALE_STATEID:
  1477. case -NFS4ERR_OLD_STATEID:
  1478. case -NFS4ERR_BAD_STATEID:
  1479. case -NFS4ERR_EXPIRED:
  1480. if (calldata->arg.fmode == 0)
  1481. break;
  1482. default:
  1483. if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
  1484. rpc_restart_call(task);
  1485. return;
  1486. }
  1487. }
  1488. nfs4_sequence_free_slot(server->nfs_client, &calldata->res.seq_res);
  1489. nfs_refresh_inode(calldata->inode, calldata->res.fattr);
  1490. }
  1491. static void nfs4_close_prepare(struct rpc_task *task, void *data)
  1492. {
  1493. struct nfs4_closedata *calldata = data;
  1494. struct nfs4_state *state = calldata->state;
  1495. int clear_rd, clear_wr, clear_rdwr;
  1496. if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
  1497. return;
  1498. clear_rd = clear_wr = clear_rdwr = 0;
  1499. spin_lock(&state->owner->so_lock);
  1500. /* Calculate the change in open mode */
  1501. if (state->n_rdwr == 0) {
  1502. if (state->n_rdonly == 0) {
  1503. clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  1504. clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
  1505. }
  1506. if (state->n_wronly == 0) {
  1507. clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  1508. clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
  1509. }
  1510. }
  1511. spin_unlock(&state->owner->so_lock);
  1512. if (!clear_rd && !clear_wr && !clear_rdwr) {
  1513. /* Note: exit _without_ calling nfs4_close_done */
  1514. task->tk_action = NULL;
  1515. return;
  1516. }
  1517. nfs_fattr_init(calldata->res.fattr);
  1518. if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0) {
  1519. task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
  1520. calldata->arg.fmode = FMODE_READ;
  1521. } else if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0) {
  1522. task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
  1523. calldata->arg.fmode = FMODE_WRITE;
  1524. }
  1525. calldata->timestamp = jiffies;
  1526. if (nfs4_setup_sequence((NFS_SERVER(calldata->inode))->nfs_client,
  1527. &calldata->arg.seq_args, &calldata->res.seq_res,
  1528. 1, task))
  1529. return;
  1530. rpc_call_start(task);
  1531. }
  1532. static const struct rpc_call_ops nfs4_close_ops = {
  1533. .rpc_call_prepare = nfs4_close_prepare,
  1534. .rpc_call_done = nfs4_close_done,
  1535. .rpc_release = nfs4_free_closedata,
  1536. };
  1537. /*
  1538. * It is possible for data to be read/written from a mem-mapped file
  1539. * after the sys_close call (which hits the vfs layer as a flush).
  1540. * This means that we can't safely call nfsv4 close on a file until
  1541. * the inode is cleared. This in turn means that we are not good
  1542. * NFSv4 citizens - we do not indicate to the server to update the file's
  1543. * share state even when we are done with one of the three share
  1544. * stateid's in the inode.
  1545. *
  1546. * NOTE: Caller must be holding the sp->so_owner semaphore!
  1547. */
  1548. int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
  1549. {
  1550. struct nfs_server *server = NFS_SERVER(state->inode);
  1551. struct nfs4_closedata *calldata;
  1552. struct nfs4_state_owner *sp = state->owner;
  1553. struct rpc_task *task;
  1554. struct rpc_message msg = {
  1555. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
  1556. .rpc_cred = state->owner->so_cred,
  1557. };
  1558. struct rpc_task_setup task_setup_data = {
  1559. .rpc_client = server->client,
  1560. .rpc_message = &msg,
  1561. .callback_ops = &nfs4_close_ops,
  1562. .workqueue = nfsiod_workqueue,
  1563. .flags = RPC_TASK_ASYNC,
  1564. };
  1565. int status = -ENOMEM;
  1566. calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
  1567. if (calldata == NULL)
  1568. goto out;
  1569. calldata->inode = state->inode;
  1570. calldata->state = state;
  1571. calldata->arg.fh = NFS_FH(state->inode);
  1572. calldata->arg.stateid = &state->open_stateid;
  1573. /* Serialization for the sequence id */
  1574. calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
  1575. if (calldata->arg.seqid == NULL)
  1576. goto out_free_calldata;
  1577. calldata->arg.fmode = 0;
  1578. calldata->arg.bitmask = server->cache_consistency_bitmask;
  1579. calldata->res.fattr = &calldata->fattr;
  1580. calldata->res.seqid = calldata->arg.seqid;
  1581. calldata->res.server = server;
  1582. calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
  1583. calldata->path.mnt = mntget(path->mnt);
  1584. calldata->path.dentry = dget(path->dentry);
  1585. msg.rpc_argp = &calldata->arg,
  1586. msg.rpc_resp = &calldata->res,
  1587. task_setup_data.callback_data = calldata;
  1588. task = rpc_run_task(&task_setup_data);
  1589. if (IS_ERR(task))
  1590. return PTR_ERR(task);
  1591. status = 0;
  1592. if (wait)
  1593. status = rpc_wait_for_completion_task(task);
  1594. rpc_put_task(task);
  1595. return status;
  1596. out_free_calldata:
  1597. kfree(calldata);
  1598. out:
  1599. nfs4_put_open_state(state);
  1600. nfs4_put_state_owner(sp);
  1601. return status;
  1602. }
  1603. static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state, fmode_t fmode)
  1604. {
  1605. struct file *filp;
  1606. int ret;
  1607. /* If the open_intent is for execute, we have an extra check to make */
  1608. if (fmode & FMODE_EXEC) {
  1609. ret = nfs_may_open(state->inode,
  1610. state->owner->so_cred,
  1611. nd->intent.open.flags);
  1612. if (ret < 0)
  1613. goto out_close;
  1614. }
  1615. filp = lookup_instantiate_filp(nd, path->dentry, NULL);
  1616. if (!IS_ERR(filp)) {
  1617. struct nfs_open_context *ctx;
  1618. ctx = nfs_file_open_context(filp);
  1619. ctx->state = state;
  1620. return 0;
  1621. }
  1622. ret = PTR_ERR(filp);
  1623. out_close:
  1624. nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
  1625. return ret;
  1626. }
  1627. struct dentry *
  1628. nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  1629. {
  1630. struct path path = {
  1631. .mnt = nd->path.mnt,
  1632. .dentry = dentry,
  1633. };
  1634. struct dentry *parent;
  1635. struct iattr attr;
  1636. struct rpc_cred *cred;
  1637. struct nfs4_state *state;
  1638. struct dentry *res;
  1639. fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
  1640. if (nd->flags & LOOKUP_CREATE) {
  1641. attr.ia_mode = nd->intent.open.create_mode;
  1642. attr.ia_valid = ATTR_MODE;
  1643. if (!IS_POSIXACL(dir))
  1644. attr.ia_mode &= ~current_umask();
  1645. } else {
  1646. attr.ia_valid = 0;
  1647. BUG_ON(nd->intent.open.flags & O_CREAT);
  1648. }
  1649. cred = rpc_lookup_cred();
  1650. if (IS_ERR(cred))
  1651. return (struct dentry *)cred;
  1652. parent = dentry->d_parent;
  1653. /* Protect against concurrent sillydeletes */
  1654. nfs_block_sillyrename(parent);
  1655. state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
  1656. put_rpccred(cred);
  1657. if (IS_ERR(state)) {
  1658. if (PTR_ERR(state) == -ENOENT) {
  1659. d_add(dentry, NULL);
  1660. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1661. }
  1662. nfs_unblock_sillyrename(parent);
  1663. return (struct dentry *)state;
  1664. }
  1665. res = d_add_unique(dentry, igrab(state->inode));
  1666. if (res != NULL)
  1667. path.dentry = res;
  1668. nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
  1669. nfs_unblock_sillyrename(parent);
  1670. nfs4_intent_set_file(nd, &path, state, fmode);
  1671. return res;
  1672. }
  1673. int
  1674. nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
  1675. {
  1676. struct path path = {
  1677. .mnt = nd->path.mnt,
  1678. .dentry = dentry,
  1679. };
  1680. struct rpc_cred *cred;
  1681. struct nfs4_state *state;
  1682. fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
  1683. cred = rpc_lookup_cred();
  1684. if (IS_ERR(cred))
  1685. return PTR_ERR(cred);
  1686. state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
  1687. put_rpccred(cred);
  1688. if (IS_ERR(state)) {
  1689. switch (PTR_ERR(state)) {
  1690. case -EPERM:
  1691. case -EACCES:
  1692. case -EDQUOT:
  1693. case -ENOSPC:
  1694. case -EROFS:
  1695. lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
  1696. return 1;
  1697. default:
  1698. goto out_drop;
  1699. }
  1700. }
  1701. if (state->inode == dentry->d_inode) {
  1702. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1703. nfs4_intent_set_file(nd, &path, state, fmode);
  1704. return 1;
  1705. }
  1706. nfs4_close_sync(&path, state, fmode);
  1707. out_drop:
  1708. d_drop(dentry);
  1709. return 0;
  1710. }
  1711. void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
  1712. {
  1713. if (ctx->state == NULL)
  1714. return;
  1715. if (is_sync)
  1716. nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
  1717. else
  1718. nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
  1719. }
  1720. static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
  1721. {
  1722. struct nfs4_server_caps_arg args = {
  1723. .fhandle = fhandle,
  1724. };
  1725. struct nfs4_server_caps_res res = {};
  1726. struct rpc_message msg = {
  1727. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
  1728. .rpc_argp = &args,
  1729. .rpc_resp = &res,
  1730. };
  1731. int status;
  1732. status = nfs4_call_sync(server, &msg, &args, &res, 0);
  1733. if (status == 0) {
  1734. memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
  1735. if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
  1736. server->caps |= NFS_CAP_ACLS;
  1737. if (res.has_links != 0)
  1738. server->caps |= NFS_CAP_HARDLINKS;
  1739. if (res.has_symlinks != 0)
  1740. server->caps |= NFS_CAP_SYMLINKS;
  1741. memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
  1742. server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
  1743. server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
  1744. server->acl_bitmask = res.acl_bitmask;
  1745. }
  1746. return status;
  1747. }
  1748. int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
  1749. {
  1750. struct nfs4_exception exception = { };
  1751. int err;
  1752. do {
  1753. err = nfs4_handle_exception(server,
  1754. _nfs4_server_capabilities(server, fhandle),
  1755. &exception);
  1756. } while (exception.retry);
  1757. return err;
  1758. }
  1759. static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
  1760. struct nfs_fsinfo *info)
  1761. {
  1762. struct nfs4_lookup_root_arg args = {
  1763. .bitmask = nfs4_fattr_bitmap,
  1764. };
  1765. struct nfs4_lookup_res res = {
  1766. .server = server,
  1767. .fattr = info->fattr,
  1768. .fh = fhandle,
  1769. };
  1770. struct rpc_message msg = {
  1771. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
  1772. .rpc_argp = &args,
  1773. .rpc_resp = &res,
  1774. };
  1775. nfs_fattr_init(info->fattr);
  1776. return nfs4_call_sync(server, &msg, &args, &res, 0);
  1777. }
  1778. static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
  1779. struct nfs_fsinfo *info)
  1780. {
  1781. struct nfs4_exception exception = { };
  1782. int err;
  1783. do {
  1784. err = nfs4_handle_exception(server,
  1785. _nfs4_lookup_root(server, fhandle, info),
  1786. &exception);
  1787. } while (exception.retry);
  1788. return err;
  1789. }
  1790. /*
  1791. * get the file handle for the "/" directory on the server
  1792. */
  1793. static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
  1794. struct nfs_fsinfo *info)
  1795. {
  1796. int status;
  1797. status = nfs4_lookup_root(server, fhandle, info);
  1798. if (status == 0)
  1799. status = nfs4_server_capabilities(server, fhandle);
  1800. if (status == 0)
  1801. status = nfs4_do_fsinfo(server, fhandle, info);
  1802. return nfs4_map_errors(status);
  1803. }
  1804. /*
  1805. * Get locations and (maybe) other attributes of a referral.
  1806. * Note that we'll actually follow the referral later when
  1807. * we detect fsid mismatch in inode revalidation
  1808. */
  1809. static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
  1810. {
  1811. int status = -ENOMEM;
  1812. struct page *page = NULL;
  1813. struct nfs4_fs_locations *locations = NULL;
  1814. page = alloc_page(GFP_KERNEL);
  1815. if (page == NULL)
  1816. goto out;
  1817. locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
  1818. if (locations == NULL)
  1819. goto out;
  1820. status = nfs4_proc_fs_locations(dir, name, locations, page);
  1821. if (status != 0)
  1822. goto out;
  1823. /* Make sure server returned a different fsid for the referral */
  1824. if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
  1825. dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
  1826. status = -EIO;
  1827. goto out;
  1828. }
  1829. memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
  1830. fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
  1831. if (!fattr->mode)
  1832. fattr->mode = S_IFDIR;
  1833. memset(fhandle, 0, sizeof(struct nfs_fh));
  1834. out:
  1835. if (page)
  1836. __free_page(page);
  1837. if (locations)
  1838. kfree(locations);
  1839. return status;
  1840. }
  1841. static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  1842. {
  1843. struct nfs4_getattr_arg args = {
  1844. .fh = fhandle,
  1845. .bitmask = server->attr_bitmask,
  1846. };
  1847. struct nfs4_getattr_res res = {
  1848. .fattr = fattr,
  1849. .server = server,
  1850. };
  1851. struct rpc_message msg = {
  1852. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
  1853. .rpc_argp = &args,
  1854. .rpc_resp = &res,
  1855. };
  1856. nfs_fattr_init(fattr);
  1857. return nfs4_call_sync(server, &msg, &args, &res, 0);
  1858. }
  1859. static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  1860. {
  1861. struct nfs4_exception exception = { };
  1862. int err;
  1863. do {
  1864. err = nfs4_handle_exception(server,
  1865. _nfs4_proc_getattr(server, fhandle, fattr),
  1866. &exception);
  1867. } while (exception.retry);
  1868. return err;
  1869. }
  1870. /*
  1871. * The file is not closed if it is opened due to the a request to change
  1872. * the size of the file. The open call will not be needed once the
  1873. * VFS layer lookup-intents are implemented.
  1874. *
  1875. * Close is called when the inode is destroyed.
  1876. * If we haven't opened the file for O_WRONLY, we
  1877. * need to in the size_change case to obtain a stateid.
  1878. *
  1879. * Got race?
  1880. * Because OPEN is always done by name in nfsv4, it is
  1881. * possible that we opened a different file by the same
  1882. * name. We can recognize this race condition, but we
  1883. * can't do anything about it besides returning an error.
  1884. *
  1885. * This will be fixed with VFS changes (lookup-intent).
  1886. */
  1887. static int
  1888. nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
  1889. struct iattr *sattr)
  1890. {
  1891. struct inode *inode = dentry->d_inode;
  1892. struct rpc_cred *cred = NULL;
  1893. struct nfs4_state *state = NULL;
  1894. int status;
  1895. nfs_fattr_init(fattr);
  1896. /* Search for an existing open(O_WRITE) file */
  1897. if (sattr->ia_valid & ATTR_FILE) {
  1898. struct nfs_open_context *ctx;
  1899. ctx = nfs_file_open_context(sattr->ia_file);
  1900. if (ctx) {
  1901. cred = ctx->cred;
  1902. state = ctx->state;
  1903. }
  1904. }
  1905. status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
  1906. if (status == 0)
  1907. nfs_setattr_update_inode(inode, sattr);
  1908. return status;
  1909. }
  1910. static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
  1911. const struct qstr *name, struct nfs_fh *fhandle,
  1912. struct nfs_fattr *fattr)
  1913. {
  1914. int status;
  1915. struct nfs4_lookup_arg args = {
  1916. .bitmask = server->attr_bitmask,
  1917. .dir_fh = dirfh,
  1918. .name = name,
  1919. };
  1920. struct nfs4_lookup_res res = {
  1921. .server = server,
  1922. .fattr = fattr,
  1923. .fh = fhandle,
  1924. };
  1925. struct rpc_message msg = {
  1926. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
  1927. .rpc_argp = &args,
  1928. .rpc_resp = &res,
  1929. };
  1930. nfs_fattr_init(fattr);
  1931. dprintk("NFS call lookupfh %s\n", name->name);
  1932. status = nfs4_call_sync(server, &msg, &args, &res, 0);
  1933. dprintk("NFS reply lookupfh: %d\n", status);
  1934. return status;
  1935. }
  1936. static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
  1937. struct qstr *name, struct nfs_fh *fhandle,
  1938. struct nfs_fattr *fattr)
  1939. {
  1940. struct nfs4_exception exception = { };
  1941. int err;
  1942. do {
  1943. err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
  1944. /* FIXME: !!!! */
  1945. if (err == -NFS4ERR_MOVED) {
  1946. err = -EREMOTE;
  1947. break;
  1948. }
  1949. err = nfs4_handle_exception(server, err, &exception);
  1950. } while (exception.retry);
  1951. return err;
  1952. }
  1953. static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
  1954. struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  1955. {
  1956. int status;
  1957. dprintk("NFS call lookup %s\n", name->name);
  1958. status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
  1959. if (status == -NFS4ERR_MOVED)
  1960. status = nfs4_get_referral(dir, name, fattr, fhandle);
  1961. dprintk("NFS reply lookup: %d\n", status);
  1962. return status;
  1963. }
  1964. static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  1965. {
  1966. struct nfs4_exception exception = { };
  1967. int err;
  1968. do {
  1969. err = nfs4_handle_exception(NFS_SERVER(dir),
  1970. _nfs4_proc_lookup(dir, name, fhandle, fattr),
  1971. &exception);
  1972. } while (exception.retry);
  1973. return err;
  1974. }
  1975. static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
  1976. {
  1977. struct nfs_server *server = NFS_SERVER(inode);
  1978. struct nfs_fattr fattr;
  1979. struct nfs4_accessargs args = {
  1980. .fh = NFS_FH(inode),
  1981. .bitmask = server->attr_bitmask,
  1982. };
  1983. struct nfs4_accessres res = {
  1984. .server = server,
  1985. .fattr = &fattr,
  1986. };
  1987. struct rpc_message msg = {
  1988. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
  1989. .rpc_argp = &args,
  1990. .rpc_resp = &res,
  1991. .rpc_cred = entry->cred,
  1992. };
  1993. int mode = entry->mask;
  1994. int status;
  1995. /*
  1996. * Determine which access bits we want to ask for...
  1997. */
  1998. if (mode & MAY_READ)
  1999. args.access |= NFS4_ACCESS_READ;
  2000. if (S_ISDIR(inode->i_mode)) {
  2001. if (mode & MAY_WRITE)
  2002. args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
  2003. if (mode & MAY_EXEC)
  2004. args.access |= NFS4_ACCESS_LOOKUP;
  2005. } else {
  2006. if (mode & MAY_WRITE)
  2007. args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
  2008. if (mode & MAY_EXEC)
  2009. args.access |= NFS4_ACCESS_EXECUTE;
  2010. }
  2011. nfs_fattr_init(&fattr);
  2012. status = nfs4_call_sync(server, &msg, &args, &res, 0);
  2013. if (!status) {
  2014. entry->mask = 0;
  2015. if (res.access & NFS4_ACCESS_READ)
  2016. entry->mask |= MAY_READ;
  2017. if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
  2018. entry->mask |= MAY_WRITE;
  2019. if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
  2020. entry->mask |= MAY_EXEC;
  2021. nfs_refresh_inode(inode, &fattr);
  2022. }
  2023. return status;
  2024. }
  2025. static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
  2026. {
  2027. struct nfs4_exception exception = { };
  2028. int err;
  2029. do {
  2030. err = nfs4_handle_exception(NFS_SERVER(inode),
  2031. _nfs4_proc_access(inode, entry),
  2032. &exception);
  2033. } while (exception.retry);
  2034. return err;
  2035. }
  2036. /*
  2037. * TODO: For the time being, we don't try to get any attributes
  2038. * along with any of the zero-copy operations READ, READDIR,
  2039. * READLINK, WRITE.
  2040. *
  2041. * In the case of the first three, we want to put the GETATTR
  2042. * after the read-type operation -- this is because it is hard
  2043. * to predict the length of a GETATTR response in v4, and thus
  2044. * align the READ data correctly. This means that the GETATTR
  2045. * may end up partially falling into the page cache, and we should
  2046. * shift it into the 'tail' of the xdr_buf before processing.
  2047. * To do this efficiently, we need to know the total length
  2048. * of data received, which doesn't seem to be available outside
  2049. * of the RPC layer.
  2050. *
  2051. * In the case of WRITE, we also want to put the GETATTR after
  2052. * the operation -- in this case because we want to make sure
  2053. * we get the post-operation mtime and size. This means that
  2054. * we can't use xdr_encode_pages() as written: we need a variant
  2055. * of it which would leave room in the 'tail' iovec.
  2056. *
  2057. * Both of these changes to the XDR layer would in fact be quite
  2058. * minor, but I decided to leave them for a subsequent patch.
  2059. */
  2060. static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
  2061. unsigned int pgbase, unsigned int pglen)
  2062. {
  2063. struct nfs4_readlink args = {
  2064. .fh = NFS_FH(inode),
  2065. .pgbase = pgbase,
  2066. .pglen = pglen,
  2067. .pages = &page,
  2068. };
  2069. struct nfs4_readlink_res res;
  2070. struct rpc_message msg = {
  2071. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
  2072. .rpc_argp = &args,
  2073. .rpc_resp = &res,
  2074. };
  2075. return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
  2076. }
  2077. static int nfs4_proc_readlink(struct inode *inode, struct page *page,
  2078. unsigned int pgbase, unsigned int pglen)
  2079. {
  2080. struct nfs4_exception exception = { };
  2081. int err;
  2082. do {
  2083. err = nfs4_handle_exception(NFS_SERVER(inode),
  2084. _nfs4_proc_readlink(inode, page, pgbase, pglen),
  2085. &exception);
  2086. } while (exception.retry);
  2087. return err;
  2088. }
  2089. /*
  2090. * Got race?
  2091. * We will need to arrange for the VFS layer to provide an atomic open.
  2092. * Until then, this create/open method is prone to inefficiency and race
  2093. * conditions due to the lookup, create, and open VFS calls from sys_open()
  2094. * placed on the wire.
  2095. *
  2096. * Given the above sorry state of affairs, I'm simply sending an OPEN.
  2097. * The file will be opened again in the subsequent VFS open call
  2098. * (nfs4_proc_file_open).
  2099. *
  2100. * The open for read will just hang around to be used by any process that
  2101. * opens the file O_RDONLY. This will all be resolved with the VFS changes.
  2102. */
  2103. static int
  2104. nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
  2105. int flags, struct nameidata *nd)
  2106. {
  2107. struct path path = {
  2108. .mnt = nd->path.mnt,
  2109. .dentry = dentry,
  2110. };
  2111. struct nfs4_state *state;
  2112. struct rpc_cred *cred;
  2113. fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
  2114. int status = 0;
  2115. cred = rpc_lookup_cred();
  2116. if (IS_ERR(cred)) {
  2117. status = PTR_ERR(cred);
  2118. goto out;
  2119. }
  2120. state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
  2121. d_drop(dentry);
  2122. if (IS_ERR(state)) {
  2123. status = PTR_ERR(state);
  2124. goto out_putcred;
  2125. }
  2126. d_add(dentry, igrab(state->inode));
  2127. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  2128. if (flags & O_EXCL) {
  2129. struct nfs_fattr fattr;
  2130. status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
  2131. if (status == 0)
  2132. nfs_setattr_update_inode(state->inode, sattr);
  2133. nfs_post_op_update_inode(state->inode, &fattr);
  2134. }
  2135. if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
  2136. status = nfs4_intent_set_file(nd, &path, state, fmode);
  2137. else
  2138. nfs4_close_sync(&path, state, fmode);
  2139. out_putcred:
  2140. put_rpccred(cred);
  2141. out:
  2142. return status;
  2143. }
  2144. static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
  2145. {
  2146. struct nfs_server *server = NFS_SERVER(dir);
  2147. struct nfs_removeargs args = {
  2148. .fh = NFS_FH(dir),
  2149. .name.len = name->len,
  2150. .name.name = name->name,
  2151. .bitmask = server->attr_bitmask,
  2152. };
  2153. struct nfs_removeres res = {
  2154. .server = server,
  2155. };
  2156. struct rpc_message msg = {
  2157. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
  2158. .rpc_argp = &args,
  2159. .rpc_resp = &res,
  2160. };
  2161. int status;
  2162. nfs_fattr_init(&res.dir_attr);
  2163. status = nfs4_call_sync(server, &msg, &args, &res, 1);
  2164. if (status == 0) {
  2165. update_changeattr(dir, &res.cinfo);
  2166. nfs_post_op_update_inode(dir, &res.dir_attr);
  2167. }
  2168. return status;
  2169. }
  2170. static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
  2171. {
  2172. struct nfs4_exception exception = { };
  2173. int err;
  2174. do {
  2175. err = nfs4_handle_exception(NFS_SERVER(dir),
  2176. _nfs4_proc_remove(dir, name),
  2177. &exception);
  2178. } while (exception.retry);
  2179. return err;
  2180. }
  2181. static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
  2182. {
  2183. struct nfs_server *server = NFS_SERVER(dir);
  2184. struct nfs_removeargs *args = msg->rpc_argp;
  2185. struct nfs_removeres *res = msg->rpc_resp;
  2186. args->bitmask = server->cache_consistency_bitmask;
  2187. res->server = server;
  2188. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
  2189. }
  2190. static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
  2191. {
  2192. struct nfs_removeres *res = task->tk_msg.rpc_resp;
  2193. if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
  2194. return 0;
  2195. update_changeattr(dir, &res->cinfo);
  2196. nfs_post_op_update_inode(dir, &res->dir_attr);
  2197. return 1;
  2198. }
  2199. static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
  2200. struct inode *new_dir, struct qstr *new_name)
  2201. {
  2202. struct nfs_server *server = NFS_SERVER(old_dir);
  2203. struct nfs4_rename_arg arg = {
  2204. .old_dir = NFS_FH(old_dir),
  2205. .new_dir = NFS_FH(new_dir),
  2206. .old_name = old_name,
  2207. .new_name = new_name,
  2208. .bitmask = server->attr_bitmask,
  2209. };
  2210. struct nfs_fattr old_fattr, new_fattr;
  2211. struct nfs4_rename_res res = {
  2212. .server = server,
  2213. .old_fattr = &old_fattr,
  2214. .new_fattr = &new_fattr,
  2215. };
  2216. struct rpc_message msg = {
  2217. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
  2218. .rpc_argp = &arg,
  2219. .rpc_resp = &res,
  2220. };
  2221. int status;
  2222. nfs_fattr_init(res.old_fattr);
  2223. nfs_fattr_init(res.new_fattr);
  2224. status = nfs4_call_sync(server, &msg, &arg, &res, 1);
  2225. if (!status) {
  2226. update_changeattr(old_dir, &res.old_cinfo);
  2227. nfs_post_op_update_inode(old_dir, res.old_fattr);
  2228. update_changeattr(new_dir, &res.new_cinfo);
  2229. nfs_post_op_update_inode(new_dir, res.new_fattr);
  2230. }
  2231. return status;
  2232. }
  2233. static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
  2234. struct inode *new_dir, struct qstr *new_name)
  2235. {
  2236. struct nfs4_exception exception = { };
  2237. int err;
  2238. do {
  2239. err = nfs4_handle_exception(NFS_SERVER(old_dir),
  2240. _nfs4_proc_rename(old_dir, old_name,
  2241. new_dir, new_name),
  2242. &exception);
  2243. } while (exception.retry);
  2244. return err;
  2245. }
  2246. static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
  2247. {
  2248. struct nfs_server *server = NFS_SERVER(inode);
  2249. struct nfs4_link_arg arg = {
  2250. .fh = NFS_FH(inode),
  2251. .dir_fh = NFS_FH(dir),
  2252. .name = name,
  2253. .bitmask = server->attr_bitmask,
  2254. };
  2255. struct nfs_fattr fattr, dir_attr;
  2256. struct nfs4_link_res res = {
  2257. .server = server,
  2258. .fattr = &fattr,
  2259. .dir_attr = &dir_attr,
  2260. };
  2261. struct rpc_message msg = {
  2262. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
  2263. .rpc_argp = &arg,
  2264. .rpc_resp = &res,
  2265. };
  2266. int status;
  2267. nfs_fattr_init(res.fattr);
  2268. nfs_fattr_init(res.dir_attr);
  2269. status = nfs4_call_sync(server, &msg, &arg, &res, 1);
  2270. if (!status) {
  2271. update_changeattr(dir, &res.cinfo);
  2272. nfs_post_op_update_inode(dir, res.dir_attr);
  2273. nfs_post_op_update_inode(inode, res.fattr);
  2274. }
  2275. return status;
  2276. }
  2277. static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
  2278. {
  2279. struct nfs4_exception exception = { };
  2280. int err;
  2281. do {
  2282. err = nfs4_handle_exception(NFS_SERVER(inode),
  2283. _nfs4_proc_link(inode, dir, name),
  2284. &exception);
  2285. } while (exception.retry);
  2286. return err;
  2287. }
  2288. struct nfs4_createdata {
  2289. struct rpc_message msg;
  2290. struct nfs4_create_arg arg;
  2291. struct nfs4_create_res res;
  2292. struct nfs_fh fh;
  2293. struct nfs_fattr fattr;
  2294. struct nfs_fattr dir_fattr;
  2295. };
  2296. static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
  2297. struct qstr *name, struct iattr *sattr, u32 ftype)
  2298. {
  2299. struct nfs4_createdata *data;
  2300. data = kzalloc(sizeof(*data), GFP_KERNEL);
  2301. if (data != NULL) {
  2302. struct nfs_server *server = NFS_SERVER(dir);
  2303. data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
  2304. data->msg.rpc_argp = &data->arg;
  2305. data->msg.rpc_resp = &data->res;
  2306. data->arg.dir_fh = NFS_FH(dir);
  2307. data->arg.server = server;
  2308. data->arg.name = name;
  2309. data->arg.attrs = sattr;
  2310. data->arg.ftype = ftype;
  2311. data->arg.bitmask = server->attr_bitmask;
  2312. data->res.server = server;
  2313. data->res.fh = &data->fh;
  2314. data->res.fattr = &data->fattr;
  2315. data->res.dir_fattr = &data->dir_fattr;
  2316. nfs_fattr_init(data->res.fattr);
  2317. nfs_fattr_init(data->res.dir_fattr);
  2318. }
  2319. return data;
  2320. }
  2321. static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
  2322. {
  2323. int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
  2324. &data->arg, &data->res, 1);
  2325. if (status == 0) {
  2326. update_changeattr(dir, &data->res.dir_cinfo);
  2327. nfs_post_op_update_inode(dir, data->res.dir_fattr);
  2328. status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
  2329. }
  2330. return status;
  2331. }
  2332. static void nfs4_free_createdata(struct nfs4_createdata *data)
  2333. {
  2334. kfree(data);
  2335. }
  2336. static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
  2337. struct page *page, unsigned int len, struct iattr *sattr)
  2338. {
  2339. struct nfs4_createdata *data;
  2340. int status = -ENAMETOOLONG;
  2341. if (len > NFS4_MAXPATHLEN)
  2342. goto out;
  2343. status = -ENOMEM;
  2344. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
  2345. if (data == NULL)
  2346. goto out;
  2347. data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
  2348. data->arg.u.symlink.pages = &page;
  2349. data->arg.u.symlink.len = len;
  2350. status = nfs4_do_create(dir, dentry, data);
  2351. nfs4_free_createdata(data);
  2352. out:
  2353. return status;
  2354. }
  2355. static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
  2356. struct page *page, unsigned int len, struct iattr *sattr)
  2357. {
  2358. struct nfs4_exception exception = { };
  2359. int err;
  2360. do {
  2361. err = nfs4_handle_exception(NFS_SERVER(dir),
  2362. _nfs4_proc_symlink(dir, dentry, page,
  2363. len, sattr),
  2364. &exception);
  2365. } while (exception.retry);
  2366. return err;
  2367. }
  2368. static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
  2369. struct iattr *sattr)
  2370. {
  2371. struct nfs4_createdata *data;
  2372. int status = -ENOMEM;
  2373. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
  2374. if (data == NULL)
  2375. goto out;
  2376. status = nfs4_do_create(dir, dentry, data);
  2377. nfs4_free_createdata(data);
  2378. out:
  2379. return status;
  2380. }
  2381. static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
  2382. struct iattr *sattr)
  2383. {
  2384. struct nfs4_exception exception = { };
  2385. int err;
  2386. do {
  2387. err = nfs4_handle_exception(NFS_SERVER(dir),
  2388. _nfs4_proc_mkdir(dir, dentry, sattr),
  2389. &exception);
  2390. } while (exception.retry);
  2391. return err;
  2392. }
  2393. static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
  2394. u64 cookie, struct page *page, unsigned int count, int plus)
  2395. {
  2396. struct inode *dir = dentry->d_inode;
  2397. struct nfs4_readdir_arg args = {
  2398. .fh = NFS_FH(dir),
  2399. .pages = &page,
  2400. .pgbase = 0,
  2401. .count = count,
  2402. .bitmask = NFS_SERVER(dentry->d_inode)->cache_consistency_bitmask,
  2403. };
  2404. struct nfs4_readdir_res res;
  2405. struct rpc_message msg = {
  2406. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
  2407. .rpc_argp = &args,
  2408. .rpc_resp = &res,
  2409. .rpc_cred = cred,
  2410. };
  2411. int status;
  2412. dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
  2413. dentry->d_parent->d_name.name,
  2414. dentry->d_name.name,
  2415. (unsigned long long)cookie);
  2416. nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
  2417. res.pgbase = args.pgbase;
  2418. status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
  2419. if (status == 0)
  2420. memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
  2421. nfs_invalidate_atime(dir);
  2422. dprintk("%s: returns %d\n", __func__, status);
  2423. return status;
  2424. }
  2425. static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
  2426. u64 cookie, struct page *page, unsigned int count, int plus)
  2427. {
  2428. struct nfs4_exception exception = { };
  2429. int err;
  2430. do {
  2431. err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
  2432. _nfs4_proc_readdir(dentry, cred, cookie,
  2433. page, count, plus),
  2434. &exception);
  2435. } while (exception.retry);
  2436. return err;
  2437. }
  2438. static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
  2439. struct iattr *sattr, dev_t rdev)
  2440. {
  2441. struct nfs4_createdata *data;
  2442. int mode = sattr->ia_mode;
  2443. int status = -ENOMEM;
  2444. BUG_ON(!(sattr->ia_valid & ATTR_MODE));
  2445. BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
  2446. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
  2447. if (data == NULL)
  2448. goto out;
  2449. if (S_ISFIFO(mode))
  2450. data->arg.ftype = NF4FIFO;
  2451. else if (S_ISBLK(mode)) {
  2452. data->arg.ftype = NF4BLK;
  2453. data->arg.u.device.specdata1 = MAJOR(rdev);
  2454. data->arg.u.device.specdata2 = MINOR(rdev);
  2455. }
  2456. else if (S_ISCHR(mode)) {
  2457. data->arg.ftype = NF4CHR;
  2458. data->arg.u.device.specdata1 = MAJOR(rdev);
  2459. data->arg.u.device.specdata2 = MINOR(rdev);
  2460. }
  2461. status = nfs4_do_create(dir, dentry, data);
  2462. nfs4_free_createdata(data);
  2463. out:
  2464. return status;
  2465. }
  2466. static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
  2467. struct iattr *sattr, dev_t rdev)
  2468. {
  2469. struct nfs4_exception exception = { };
  2470. int err;
  2471. do {
  2472. err = nfs4_handle_exception(NFS_SERVER(dir),
  2473. _nfs4_proc_mknod(dir, dentry, sattr, rdev),
  2474. &exception);
  2475. } while (exception.retry);
  2476. return err;
  2477. }
  2478. static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
  2479. struct nfs_fsstat *fsstat)
  2480. {
  2481. struct nfs4_statfs_arg args = {
  2482. .fh = fhandle,
  2483. .bitmask = server->attr_bitmask,
  2484. };
  2485. struct nfs4_statfs_res res = {
  2486. .fsstat = fsstat,
  2487. };
  2488. struct rpc_message msg = {
  2489. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
  2490. .rpc_argp = &args,
  2491. .rpc_resp = &res,
  2492. };
  2493. nfs_fattr_init(fsstat->fattr);
  2494. return nfs4_call_sync(server, &msg, &args, &res, 0);
  2495. }
  2496. static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
  2497. {
  2498. struct nfs4_exception exception = { };
  2499. int err;
  2500. do {
  2501. err = nfs4_handle_exception(server,
  2502. _nfs4_proc_statfs(server, fhandle, fsstat),
  2503. &exception);
  2504. } while (exception.retry);
  2505. return err;
  2506. }
  2507. static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
  2508. struct nfs_fsinfo *fsinfo)
  2509. {
  2510. struct nfs4_fsinfo_arg args = {
  2511. .fh = fhandle,
  2512. .bitmask = server->attr_bitmask,
  2513. };
  2514. struct nfs4_fsinfo_res res = {
  2515. .fsinfo = fsinfo,
  2516. };
  2517. struct rpc_message msg = {
  2518. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
  2519. .rpc_argp = &args,
  2520. .rpc_resp = &res,
  2521. };
  2522. return nfs4_call_sync(server, &msg, &args, &res, 0);
  2523. }
  2524. static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
  2525. {
  2526. struct nfs4_exception exception = { };
  2527. int err;
  2528. do {
  2529. err = nfs4_handle_exception(server,
  2530. _nfs4_do_fsinfo(server, fhandle, fsinfo),
  2531. &exception);
  2532. } while (exception.retry);
  2533. return err;
  2534. }
  2535. static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
  2536. {
  2537. nfs_fattr_init(fsinfo->fattr);
  2538. return nfs4_do_fsinfo(server, fhandle, fsinfo);
  2539. }
  2540. static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
  2541. struct nfs_pathconf *pathconf)
  2542. {
  2543. struct nfs4_pathconf_arg args = {
  2544. .fh = fhandle,
  2545. .bitmask = server->attr_bitmask,
  2546. };
  2547. struct nfs4_pathconf_res res = {
  2548. .pathconf = pathconf,
  2549. };
  2550. struct rpc_message msg = {
  2551. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
  2552. .rpc_argp = &args,
  2553. .rpc_resp = &res,
  2554. };
  2555. /* None of the pathconf attributes are mandatory to implement */
  2556. if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
  2557. memset(pathconf, 0, sizeof(*pathconf));
  2558. return 0;
  2559. }
  2560. nfs_fattr_init(pathconf->fattr);
  2561. return nfs4_call_sync(server, &msg, &args, &res, 0);
  2562. }
  2563. static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
  2564. struct nfs_pathconf *pathconf)
  2565. {
  2566. struct nfs4_exception exception = { };
  2567. int err;
  2568. do {
  2569. err = nfs4_handle_exception(server,
  2570. _nfs4_proc_pathconf(server, fhandle, pathconf),
  2571. &exception);
  2572. } while (exception.retry);
  2573. return err;
  2574. }
  2575. static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
  2576. {
  2577. struct nfs_server *server = NFS_SERVER(data->inode);
  2578. if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
  2579. rpc_restart_call(task);
  2580. return -EAGAIN;
  2581. }
  2582. nfs_invalidate_atime(data->inode);
  2583. if (task->tk_status > 0)
  2584. renew_lease(server, data->timestamp);
  2585. return 0;
  2586. }
  2587. static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
  2588. {
  2589. data->timestamp = jiffies;
  2590. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
  2591. }
  2592. static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
  2593. {
  2594. struct inode *inode = data->inode;
  2595. if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
  2596. rpc_restart_call(task);
  2597. return -EAGAIN;
  2598. }
  2599. if (task->tk_status >= 0) {
  2600. renew_lease(NFS_SERVER(inode), data->timestamp);
  2601. nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
  2602. }
  2603. return 0;
  2604. }
  2605. static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
  2606. {
  2607. struct nfs_server *server = NFS_SERVER(data->inode);
  2608. data->args.bitmask = server->cache_consistency_bitmask;
  2609. data->res.server = server;
  2610. data->timestamp = jiffies;
  2611. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
  2612. }
  2613. static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
  2614. {
  2615. struct inode *inode = data->inode;
  2616. if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
  2617. rpc_restart_call(task);
  2618. return -EAGAIN;
  2619. }
  2620. nfs_refresh_inode(inode, data->res.fattr);
  2621. return 0;
  2622. }
  2623. static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
  2624. {
  2625. struct nfs_server *server = NFS_SERVER(data->inode);
  2626. data->args.bitmask = server->cache_consistency_bitmask;
  2627. data->res.server = server;
  2628. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
  2629. }
  2630. /*
  2631. * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
  2632. * standalone procedure for queueing an asynchronous RENEW.
  2633. */
  2634. static void nfs4_renew_done(struct rpc_task *task, void *data)
  2635. {
  2636. struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
  2637. unsigned long timestamp = (unsigned long)data;
  2638. if (task->tk_status < 0) {
  2639. /* Unless we're shutting down, schedule state recovery! */
  2640. if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
  2641. nfs4_schedule_state_recovery(clp);
  2642. return;
  2643. }
  2644. spin_lock(&clp->cl_lock);
  2645. if (time_before(clp->cl_last_renewal,timestamp))
  2646. clp->cl_last_renewal = timestamp;
  2647. spin_unlock(&clp->cl_lock);
  2648. }
  2649. static const struct rpc_call_ops nfs4_renew_ops = {
  2650. .rpc_call_done = nfs4_renew_done,
  2651. };
  2652. int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
  2653. {
  2654. struct rpc_message msg = {
  2655. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
  2656. .rpc_argp = clp,
  2657. .rpc_cred = cred,
  2658. };
  2659. return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
  2660. &nfs4_renew_ops, (void *)jiffies);
  2661. }
  2662. int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
  2663. {
  2664. struct rpc_message msg = {
  2665. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
  2666. .rpc_argp = clp,
  2667. .rpc_cred = cred,
  2668. };
  2669. unsigned long now = jiffies;
  2670. int status;
  2671. status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
  2672. if (status < 0)
  2673. return status;
  2674. spin_lock(&clp->cl_lock);
  2675. if (time_before(clp->cl_last_renewal,now))
  2676. clp->cl_last_renewal = now;
  2677. spin_unlock(&clp->cl_lock);
  2678. return 0;
  2679. }
  2680. static inline int nfs4_server_supports_acls(struct nfs_server *server)
  2681. {
  2682. return (server->caps & NFS_CAP_ACLS)
  2683. && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
  2684. && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
  2685. }
  2686. /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
  2687. * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
  2688. * the stack.
  2689. */
  2690. #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
  2691. static void buf_to_pages(const void *buf, size_t buflen,
  2692. struct page **pages, unsigned int *pgbase)
  2693. {
  2694. const void *p = buf;
  2695. *pgbase = offset_in_page(buf);
  2696. p -= *pgbase;
  2697. while (p < buf + buflen) {
  2698. *(pages++) = virt_to_page(p);
  2699. p += PAGE_CACHE_SIZE;
  2700. }
  2701. }
  2702. struct nfs4_cached_acl {
  2703. int cached;
  2704. size_t len;
  2705. char data[0];
  2706. };
  2707. static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
  2708. {
  2709. struct nfs_inode *nfsi = NFS_I(inode);
  2710. spin_lock(&inode->i_lock);
  2711. kfree(nfsi->nfs4_acl);
  2712. nfsi->nfs4_acl = acl;
  2713. spin_unlock(&inode->i_lock);
  2714. }
  2715. static void nfs4_zap_acl_attr(struct inode *inode)
  2716. {
  2717. nfs4_set_cached_acl(inode, NULL);
  2718. }
  2719. static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
  2720. {
  2721. struct nfs_inode *nfsi = NFS_I(inode);
  2722. struct nfs4_cached_acl *acl;
  2723. int ret = -ENOENT;
  2724. spin_lock(&inode->i_lock);
  2725. acl = nfsi->nfs4_acl;
  2726. if (acl == NULL)
  2727. goto out;
  2728. if (buf == NULL) /* user is just asking for length */
  2729. goto out_len;
  2730. if (acl->cached == 0)
  2731. goto out;
  2732. ret = -ERANGE; /* see getxattr(2) man page */
  2733. if (acl->len > buflen)
  2734. goto out;
  2735. memcpy(buf, acl->data, acl->len);
  2736. out_len:
  2737. ret = acl->len;
  2738. out:
  2739. spin_unlock(&inode->i_lock);
  2740. return ret;
  2741. }
  2742. static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
  2743. {
  2744. struct nfs4_cached_acl *acl;
  2745. if (buf && acl_len <= PAGE_SIZE) {
  2746. acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
  2747. if (acl == NULL)
  2748. goto out;
  2749. acl->cached = 1;
  2750. memcpy(acl->data, buf, acl_len);
  2751. } else {
  2752. acl = kmalloc(sizeof(*acl), GFP_KERNEL);
  2753. if (acl == NULL)
  2754. goto out;
  2755. acl->cached = 0;
  2756. }
  2757. acl->len = acl_len;
  2758. out:
  2759. nfs4_set_cached_acl(inode, acl);
  2760. }
  2761. static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
  2762. {
  2763. struct page *pages[NFS4ACL_MAXPAGES];
  2764. struct nfs_getaclargs args = {
  2765. .fh = NFS_FH(inode),
  2766. .acl_pages = pages,
  2767. .acl_len = buflen,
  2768. };
  2769. struct nfs_getaclres res = {
  2770. .acl_len = buflen,
  2771. };
  2772. void *resp_buf;
  2773. struct rpc_message msg = {
  2774. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
  2775. .rpc_argp = &args,
  2776. .rpc_resp = &res,
  2777. };
  2778. struct page *localpage = NULL;
  2779. int ret;
  2780. if (buflen < PAGE_SIZE) {
  2781. /* As long as we're doing a round trip to the server anyway,
  2782. * let's be prepared for a page of acl data. */
  2783. localpage = alloc_page(GFP_KERNEL);
  2784. resp_buf = page_address(localpage);
  2785. if (localpage == NULL)
  2786. return -ENOMEM;
  2787. args.acl_pages[0] = localpage;
  2788. args.acl_pgbase = 0;
  2789. args.acl_len = PAGE_SIZE;
  2790. } else {
  2791. resp_buf = buf;
  2792. buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
  2793. }
  2794. ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
  2795. if (ret)
  2796. goto out_free;
  2797. if (res.acl_len > args.acl_len)
  2798. nfs4_write_cached_acl(inode, NULL, res.acl_len);
  2799. else
  2800. nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
  2801. if (buf) {
  2802. ret = -ERANGE;
  2803. if (res.acl_len > buflen)
  2804. goto out_free;
  2805. if (localpage)
  2806. memcpy(buf, resp_buf, res.acl_len);
  2807. }
  2808. ret = res.acl_len;
  2809. out_free:
  2810. if (localpage)
  2811. __free_page(localpage);
  2812. return ret;
  2813. }
  2814. static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
  2815. {
  2816. struct nfs4_exception exception = { };
  2817. ssize_t ret;
  2818. do {
  2819. ret = __nfs4_get_acl_uncached(inode, buf, buflen);
  2820. if (ret >= 0)
  2821. break;
  2822. ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
  2823. } while (exception.retry);
  2824. return ret;
  2825. }
  2826. static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
  2827. {
  2828. struct nfs_server *server = NFS_SERVER(inode);
  2829. int ret;
  2830. if (!nfs4_server_supports_acls(server))
  2831. return -EOPNOTSUPP;
  2832. ret = nfs_revalidate_inode(server, inode);
  2833. if (ret < 0)
  2834. return ret;
  2835. if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
  2836. nfs_zap_acl_cache(inode);
  2837. ret = nfs4_read_cached_acl(inode, buf, buflen);
  2838. if (ret != -ENOENT)
  2839. return ret;
  2840. return nfs4_get_acl_uncached(inode, buf, buflen);
  2841. }
  2842. static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
  2843. {
  2844. struct nfs_server *server = NFS_SERVER(inode);
  2845. struct page *pages[NFS4ACL_MAXPAGES];
  2846. struct nfs_setaclargs arg = {
  2847. .fh = NFS_FH(inode),
  2848. .acl_pages = pages,
  2849. .acl_len = buflen,
  2850. };
  2851. struct nfs_setaclres res;
  2852. struct rpc_message msg = {
  2853. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
  2854. .rpc_argp = &arg,
  2855. .rpc_resp = &res,
  2856. };
  2857. int ret;
  2858. if (!nfs4_server_supports_acls(server))
  2859. return -EOPNOTSUPP;
  2860. nfs_inode_return_delegation(inode);
  2861. buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
  2862. ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
  2863. nfs_access_zap_cache(inode);
  2864. nfs_zap_acl_cache(inode);
  2865. return ret;
  2866. }
  2867. static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
  2868. {
  2869. struct nfs4_exception exception = { };
  2870. int err;
  2871. do {
  2872. err = nfs4_handle_exception(NFS_SERVER(inode),
  2873. __nfs4_proc_set_acl(inode, buf, buflen),
  2874. &exception);
  2875. } while (exception.retry);
  2876. return err;
  2877. }
  2878. static int
  2879. nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
  2880. {
  2881. struct nfs_client *clp = server->nfs_client;
  2882. if (!clp || task->tk_status >= 0)
  2883. return 0;
  2884. switch(task->tk_status) {
  2885. case -NFS4ERR_ADMIN_REVOKED:
  2886. case -NFS4ERR_BAD_STATEID:
  2887. case -NFS4ERR_OPENMODE:
  2888. if (state == NULL)
  2889. break;
  2890. nfs4_state_mark_reclaim_nograce(clp, state);
  2891. case -NFS4ERR_STALE_CLIENTID:
  2892. case -NFS4ERR_STALE_STATEID:
  2893. case -NFS4ERR_EXPIRED:
  2894. rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
  2895. nfs4_schedule_state_recovery(clp);
  2896. if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
  2897. rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
  2898. task->tk_status = 0;
  2899. return -EAGAIN;
  2900. case -NFS4ERR_DELAY:
  2901. nfs_inc_server_stats(server, NFSIOS_DELAY);
  2902. case -NFS4ERR_GRACE:
  2903. rpc_delay(task, NFS4_POLL_RETRY_MAX);
  2904. task->tk_status = 0;
  2905. return -EAGAIN;
  2906. case -NFS4ERR_OLD_STATEID:
  2907. task->tk_status = 0;
  2908. return -EAGAIN;
  2909. }
  2910. task->tk_status = nfs4_map_errors(task->tk_status);
  2911. return 0;
  2912. }
  2913. int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
  2914. {
  2915. nfs4_verifier sc_verifier;
  2916. struct nfs4_setclientid setclientid = {
  2917. .sc_verifier = &sc_verifier,
  2918. .sc_prog = program,
  2919. };
  2920. struct rpc_message msg = {
  2921. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
  2922. .rpc_argp = &setclientid,
  2923. .rpc_resp = clp,
  2924. .rpc_cred = cred,
  2925. };
  2926. __be32 *p;
  2927. int loop = 0;
  2928. int status;
  2929. p = (__be32*)sc_verifier.data;
  2930. *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
  2931. *p = htonl((u32)clp->cl_boot_time.tv_nsec);
  2932. for(;;) {
  2933. setclientid.sc_name_len = scnprintf(setclientid.sc_name,
  2934. sizeof(setclientid.sc_name), "%s/%s %s %s %u",
  2935. clp->cl_ipaddr,
  2936. rpc_peeraddr2str(clp->cl_rpcclient,
  2937. RPC_DISPLAY_ADDR),
  2938. rpc_peeraddr2str(clp->cl_rpcclient,
  2939. RPC_DISPLAY_PROTO),
  2940. clp->cl_rpcclient->cl_auth->au_ops->au_name,
  2941. clp->cl_id_uniquifier);
  2942. setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
  2943. sizeof(setclientid.sc_netid),
  2944. rpc_peeraddr2str(clp->cl_rpcclient,
  2945. RPC_DISPLAY_NETID));
  2946. setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
  2947. sizeof(setclientid.sc_uaddr), "%s.%u.%u",
  2948. clp->cl_ipaddr, port >> 8, port & 255);
  2949. status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
  2950. if (status != -NFS4ERR_CLID_INUSE)
  2951. break;
  2952. if (signalled())
  2953. break;
  2954. if (loop++ & 1)
  2955. ssleep(clp->cl_lease_time + 1);
  2956. else
  2957. if (++clp->cl_id_uniquifier == 0)
  2958. break;
  2959. }
  2960. return status;
  2961. }
  2962. static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
  2963. {
  2964. struct nfs_fsinfo fsinfo;
  2965. struct rpc_message msg = {
  2966. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
  2967. .rpc_argp = clp,
  2968. .rpc_resp = &fsinfo,
  2969. .rpc_cred = cred,
  2970. };
  2971. unsigned long now;
  2972. int status;
  2973. now = jiffies;
  2974. status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
  2975. if (status == 0) {
  2976. spin_lock(&clp->cl_lock);
  2977. clp->cl_lease_time = fsinfo.lease_time * HZ;
  2978. clp->cl_last_renewal = now;
  2979. spin_unlock(&clp->cl_lock);
  2980. }
  2981. return status;
  2982. }
  2983. int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
  2984. {
  2985. long timeout = 0;
  2986. int err;
  2987. do {
  2988. err = _nfs4_proc_setclientid_confirm(clp, cred);
  2989. switch (err) {
  2990. case 0:
  2991. return err;
  2992. case -NFS4ERR_RESOURCE:
  2993. /* The IBM lawyers misread another document! */
  2994. case -NFS4ERR_DELAY:
  2995. err = nfs4_delay(clp->cl_rpcclient, &timeout);
  2996. }
  2997. } while (err == 0);
  2998. return err;
  2999. }
  3000. struct nfs4_delegreturndata {
  3001. struct nfs4_delegreturnargs args;
  3002. struct nfs4_delegreturnres res;
  3003. struct nfs_fh fh;
  3004. nfs4_stateid stateid;
  3005. unsigned long timestamp;
  3006. struct nfs_fattr fattr;
  3007. int rpc_status;
  3008. };
  3009. static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
  3010. {
  3011. struct nfs4_delegreturndata *data = calldata;
  3012. data->rpc_status = task->tk_status;
  3013. if (data->rpc_status == 0)
  3014. renew_lease(data->res.server, data->timestamp);
  3015. }
  3016. static void nfs4_delegreturn_release(void *calldata)
  3017. {
  3018. kfree(calldata);
  3019. }
  3020. static const struct rpc_call_ops nfs4_delegreturn_ops = {
  3021. .rpc_call_done = nfs4_delegreturn_done,
  3022. .rpc_release = nfs4_delegreturn_release,
  3023. };
  3024. static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
  3025. {
  3026. struct nfs4_delegreturndata *data;
  3027. struct nfs_server *server = NFS_SERVER(inode);
  3028. struct rpc_task *task;
  3029. struct rpc_message msg = {
  3030. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
  3031. .rpc_cred = cred,
  3032. };
  3033. struct rpc_task_setup task_setup_data = {
  3034. .rpc_client = server->client,
  3035. .rpc_message = &msg,
  3036. .callback_ops = &nfs4_delegreturn_ops,
  3037. .flags = RPC_TASK_ASYNC,
  3038. };
  3039. int status = 0;
  3040. data = kmalloc(sizeof(*data), GFP_KERNEL);
  3041. if (data == NULL)
  3042. return -ENOMEM;
  3043. data->args.fhandle = &data->fh;
  3044. data->args.stateid = &data->stateid;
  3045. data->args.bitmask = server->attr_bitmask;
  3046. nfs_copy_fh(&data->fh, NFS_FH(inode));
  3047. memcpy(&data->stateid, stateid, sizeof(data->stateid));
  3048. data->res.fattr = &data->fattr;
  3049. data->res.server = server;
  3050. data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
  3051. nfs_fattr_init(data->res.fattr);
  3052. data->timestamp = jiffies;
  3053. data->rpc_status = 0;
  3054. task_setup_data.callback_data = data;
  3055. msg.rpc_argp = &data->args,
  3056. msg.rpc_resp = &data->res,
  3057. task = rpc_run_task(&task_setup_data);
  3058. if (IS_ERR(task))
  3059. return PTR_ERR(task);
  3060. if (!issync)
  3061. goto out;
  3062. status = nfs4_wait_for_completion_rpc_task(task);
  3063. if (status != 0)
  3064. goto out;
  3065. status = data->rpc_status;
  3066. if (status != 0)
  3067. goto out;
  3068. nfs_refresh_inode(inode, &data->fattr);
  3069. out:
  3070. rpc_put_task(task);
  3071. return status;
  3072. }
  3073. int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
  3074. {
  3075. struct nfs_server *server = NFS_SERVER(inode);
  3076. struct nfs4_exception exception = { };
  3077. int err;
  3078. do {
  3079. err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
  3080. switch (err) {
  3081. case -NFS4ERR_STALE_STATEID:
  3082. case -NFS4ERR_EXPIRED:
  3083. case 0:
  3084. return 0;
  3085. }
  3086. err = nfs4_handle_exception(server, err, &exception);
  3087. } while (exception.retry);
  3088. return err;
  3089. }
  3090. #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
  3091. #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
  3092. /*
  3093. * sleep, with exponential backoff, and retry the LOCK operation.
  3094. */
  3095. static unsigned long
  3096. nfs4_set_lock_task_retry(unsigned long timeout)
  3097. {
  3098. schedule_timeout_killable(timeout);
  3099. timeout <<= 1;
  3100. if (timeout > NFS4_LOCK_MAXTIMEOUT)
  3101. return NFS4_LOCK_MAXTIMEOUT;
  3102. return timeout;
  3103. }
  3104. static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  3105. {
  3106. struct inode *inode = state->inode;
  3107. struct nfs_server *server = NFS_SERVER(inode);
  3108. struct nfs_client *clp = server->nfs_client;
  3109. struct nfs_lockt_args arg = {
  3110. .fh = NFS_FH(inode),
  3111. .fl = request,
  3112. };
  3113. struct nfs_lockt_res res = {
  3114. .denied = request,
  3115. };
  3116. struct rpc_message msg = {
  3117. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
  3118. .rpc_argp = &arg,
  3119. .rpc_resp = &res,
  3120. .rpc_cred = state->owner->so_cred,
  3121. };
  3122. struct nfs4_lock_state *lsp;
  3123. int status;
  3124. arg.lock_owner.clientid = clp->cl_clientid;
  3125. status = nfs4_set_lock_state(state, request);
  3126. if (status != 0)
  3127. goto out;
  3128. lsp = request->fl_u.nfs4_fl.owner;
  3129. arg.lock_owner.id = lsp->ls_id.id;
  3130. status = nfs4_call_sync(server, &msg, &arg, &res, 1);
  3131. switch (status) {
  3132. case 0:
  3133. request->fl_type = F_UNLCK;
  3134. break;
  3135. case -NFS4ERR_DENIED:
  3136. status = 0;
  3137. }
  3138. request->fl_ops->fl_release_private(request);
  3139. out:
  3140. return status;
  3141. }
  3142. static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  3143. {
  3144. struct nfs4_exception exception = { };
  3145. int err;
  3146. do {
  3147. err = nfs4_handle_exception(NFS_SERVER(state->inode),
  3148. _nfs4_proc_getlk(state, cmd, request),
  3149. &exception);
  3150. } while (exception.retry);
  3151. return err;
  3152. }
  3153. static int do_vfs_lock(struct file *file, struct file_lock *fl)
  3154. {
  3155. int res = 0;
  3156. switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
  3157. case FL_POSIX:
  3158. res = posix_lock_file_wait(file, fl);
  3159. break;
  3160. case FL_FLOCK:
  3161. res = flock_lock_file_wait(file, fl);
  3162. break;
  3163. default:
  3164. BUG();
  3165. }
  3166. return res;
  3167. }
  3168. struct nfs4_unlockdata {
  3169. struct nfs_locku_args arg;
  3170. struct nfs_locku_res res;
  3171. struct nfs4_lock_state *lsp;
  3172. struct nfs_open_context *ctx;
  3173. struct file_lock fl;
  3174. const struct nfs_server *server;
  3175. unsigned long timestamp;
  3176. };
  3177. static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
  3178. struct nfs_open_context *ctx,
  3179. struct nfs4_lock_state *lsp,
  3180. struct nfs_seqid *seqid)
  3181. {
  3182. struct nfs4_unlockdata *p;
  3183. struct inode *inode = lsp->ls_state->inode;
  3184. p = kmalloc(sizeof(*p), GFP_KERNEL);
  3185. if (p == NULL)
  3186. return NULL;
  3187. p->arg.fh = NFS_FH(inode);
  3188. p->arg.fl = &p->fl;
  3189. p->arg.seqid = seqid;
  3190. p->res.seqid = seqid;
  3191. p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
  3192. p->arg.stateid = &lsp->ls_stateid;
  3193. p->lsp = lsp;
  3194. atomic_inc(&lsp->ls_count);
  3195. /* Ensure we don't close file until we're done freeing locks! */
  3196. p->ctx = get_nfs_open_context(ctx);
  3197. memcpy(&p->fl, fl, sizeof(p->fl));
  3198. p->server = NFS_SERVER(inode);
  3199. return p;
  3200. }
  3201. static void nfs4_locku_release_calldata(void *data)
  3202. {
  3203. struct nfs4_unlockdata *calldata = data;
  3204. nfs_free_seqid(calldata->arg.seqid);
  3205. nfs4_put_lock_state(calldata->lsp);
  3206. put_nfs_open_context(calldata->ctx);
  3207. kfree(calldata);
  3208. }
  3209. static void nfs4_locku_done(struct rpc_task *task, void *data)
  3210. {
  3211. struct nfs4_unlockdata *calldata = data;
  3212. if (RPC_ASSASSINATED(task))
  3213. return;
  3214. switch (task->tk_status) {
  3215. case 0:
  3216. memcpy(calldata->lsp->ls_stateid.data,
  3217. calldata->res.stateid.data,
  3218. sizeof(calldata->lsp->ls_stateid.data));
  3219. renew_lease(calldata->server, calldata->timestamp);
  3220. break;
  3221. case -NFS4ERR_BAD_STATEID:
  3222. case -NFS4ERR_OLD_STATEID:
  3223. case -NFS4ERR_STALE_STATEID:
  3224. case -NFS4ERR_EXPIRED:
  3225. break;
  3226. default:
  3227. if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
  3228. rpc_restart_call(task);
  3229. }
  3230. }
  3231. static void nfs4_locku_prepare(struct rpc_task *task, void *data)
  3232. {
  3233. struct nfs4_unlockdata *calldata = data;
  3234. if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
  3235. return;
  3236. if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
  3237. /* Note: exit _without_ running nfs4_locku_done */
  3238. task->tk_action = NULL;
  3239. return;
  3240. }
  3241. calldata->timestamp = jiffies;
  3242. rpc_call_start(task);
  3243. }
  3244. static const struct rpc_call_ops nfs4_locku_ops = {
  3245. .rpc_call_prepare = nfs4_locku_prepare,
  3246. .rpc_call_done = nfs4_locku_done,
  3247. .rpc_release = nfs4_locku_release_calldata,
  3248. };
  3249. static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
  3250. struct nfs_open_context *ctx,
  3251. struct nfs4_lock_state *lsp,
  3252. struct nfs_seqid *seqid)
  3253. {
  3254. struct nfs4_unlockdata *data;
  3255. struct rpc_message msg = {
  3256. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
  3257. .rpc_cred = ctx->cred,
  3258. };
  3259. struct rpc_task_setup task_setup_data = {
  3260. .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
  3261. .rpc_message = &msg,
  3262. .callback_ops = &nfs4_locku_ops,
  3263. .workqueue = nfsiod_workqueue,
  3264. .flags = RPC_TASK_ASYNC,
  3265. };
  3266. /* Ensure this is an unlock - when canceling a lock, the
  3267. * canceled lock is passed in, and it won't be an unlock.
  3268. */
  3269. fl->fl_type = F_UNLCK;
  3270. data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
  3271. if (data == NULL) {
  3272. nfs_free_seqid(seqid);
  3273. return ERR_PTR(-ENOMEM);
  3274. }
  3275. msg.rpc_argp = &data->arg,
  3276. msg.rpc_resp = &data->res,
  3277. task_setup_data.callback_data = data;
  3278. return rpc_run_task(&task_setup_data);
  3279. }
  3280. static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
  3281. {
  3282. struct nfs_inode *nfsi = NFS_I(state->inode);
  3283. struct nfs_seqid *seqid;
  3284. struct nfs4_lock_state *lsp;
  3285. struct rpc_task *task;
  3286. int status = 0;
  3287. unsigned char fl_flags = request->fl_flags;
  3288. status = nfs4_set_lock_state(state, request);
  3289. /* Unlock _before_ we do the RPC call */
  3290. request->fl_flags |= FL_EXISTS;
  3291. down_read(&nfsi->rwsem);
  3292. if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
  3293. up_read(&nfsi->rwsem);
  3294. goto out;
  3295. }
  3296. up_read(&nfsi->rwsem);
  3297. if (status != 0)
  3298. goto out;
  3299. /* Is this a delegated lock? */
  3300. if (test_bit(NFS_DELEGATED_STATE, &state->flags))
  3301. goto out;
  3302. lsp = request->fl_u.nfs4_fl.owner;
  3303. seqid = nfs_alloc_seqid(&lsp->ls_seqid);
  3304. status = -ENOMEM;
  3305. if (seqid == NULL)
  3306. goto out;
  3307. task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
  3308. status = PTR_ERR(task);
  3309. if (IS_ERR(task))
  3310. goto out;
  3311. status = nfs4_wait_for_completion_rpc_task(task);
  3312. rpc_put_task(task);
  3313. out:
  3314. request->fl_flags = fl_flags;
  3315. return status;
  3316. }
  3317. struct nfs4_lockdata {
  3318. struct nfs_lock_args arg;
  3319. struct nfs_lock_res res;
  3320. struct nfs4_lock_state *lsp;
  3321. struct nfs_open_context *ctx;
  3322. struct file_lock fl;
  3323. unsigned long timestamp;
  3324. int rpc_status;
  3325. int cancelled;
  3326. };
  3327. static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
  3328. struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
  3329. {
  3330. struct nfs4_lockdata *p;
  3331. struct inode *inode = lsp->ls_state->inode;
  3332. struct nfs_server *server = NFS_SERVER(inode);
  3333. p = kzalloc(sizeof(*p), GFP_KERNEL);
  3334. if (p == NULL)
  3335. return NULL;
  3336. p->arg.fh = NFS_FH(inode);
  3337. p->arg.fl = &p->fl;
  3338. p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
  3339. if (p->arg.open_seqid == NULL)
  3340. goto out_free;
  3341. p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
  3342. if (p->arg.lock_seqid == NULL)
  3343. goto out_free_seqid;
  3344. p->arg.lock_stateid = &lsp->ls_stateid;
  3345. p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
  3346. p->arg.lock_owner.id = lsp->ls_id.id;
  3347. p->res.lock_seqid = p->arg.lock_seqid;
  3348. p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
  3349. p->lsp = lsp;
  3350. atomic_inc(&lsp->ls_count);
  3351. p->ctx = get_nfs_open_context(ctx);
  3352. memcpy(&p->fl, fl, sizeof(p->fl));
  3353. return p;
  3354. out_free_seqid:
  3355. nfs_free_seqid(p->arg.open_seqid);
  3356. out_free:
  3357. kfree(p);
  3358. return NULL;
  3359. }
  3360. static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
  3361. {
  3362. struct nfs4_lockdata *data = calldata;
  3363. struct nfs4_state *state = data->lsp->ls_state;
  3364. dprintk("%s: begin!\n", __func__);
  3365. if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
  3366. return;
  3367. /* Do we need to do an open_to_lock_owner? */
  3368. if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
  3369. if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
  3370. return;
  3371. data->arg.open_stateid = &state->stateid;
  3372. data->arg.new_lock_owner = 1;
  3373. data->res.open_seqid = data->arg.open_seqid;
  3374. } else
  3375. data->arg.new_lock_owner = 0;
  3376. data->timestamp = jiffies;
  3377. rpc_call_start(task);
  3378. dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
  3379. }
  3380. static void nfs4_lock_done(struct rpc_task *task, void *calldata)
  3381. {
  3382. struct nfs4_lockdata *data = calldata;
  3383. dprintk("%s: begin!\n", __func__);
  3384. data->rpc_status = task->tk_status;
  3385. if (RPC_ASSASSINATED(task))
  3386. goto out;
  3387. if (data->arg.new_lock_owner != 0) {
  3388. if (data->rpc_status == 0)
  3389. nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
  3390. else
  3391. goto out;
  3392. }
  3393. if (data->rpc_status == 0) {
  3394. memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
  3395. sizeof(data->lsp->ls_stateid.data));
  3396. data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
  3397. renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
  3398. }
  3399. out:
  3400. dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
  3401. }
  3402. static void nfs4_lock_release(void *calldata)
  3403. {
  3404. struct nfs4_lockdata *data = calldata;
  3405. dprintk("%s: begin!\n", __func__);
  3406. nfs_free_seqid(data->arg.open_seqid);
  3407. if (data->cancelled != 0) {
  3408. struct rpc_task *task;
  3409. task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
  3410. data->arg.lock_seqid);
  3411. if (!IS_ERR(task))
  3412. rpc_put_task(task);
  3413. dprintk("%s: cancelling lock!\n", __func__);
  3414. } else
  3415. nfs_free_seqid(data->arg.lock_seqid);
  3416. nfs4_put_lock_state(data->lsp);
  3417. put_nfs_open_context(data->ctx);
  3418. kfree(data);
  3419. dprintk("%s: done!\n", __func__);
  3420. }
  3421. static const struct rpc_call_ops nfs4_lock_ops = {
  3422. .rpc_call_prepare = nfs4_lock_prepare,
  3423. .rpc_call_done = nfs4_lock_done,
  3424. .rpc_release = nfs4_lock_release,
  3425. };
  3426. static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
  3427. {
  3428. struct nfs4_lockdata *data;
  3429. struct rpc_task *task;
  3430. struct rpc_message msg = {
  3431. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
  3432. .rpc_cred = state->owner->so_cred,
  3433. };
  3434. struct rpc_task_setup task_setup_data = {
  3435. .rpc_client = NFS_CLIENT(state->inode),
  3436. .rpc_message = &msg,
  3437. .callback_ops = &nfs4_lock_ops,
  3438. .workqueue = nfsiod_workqueue,
  3439. .flags = RPC_TASK_ASYNC,
  3440. };
  3441. int ret;
  3442. dprintk("%s: begin!\n", __func__);
  3443. data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
  3444. fl->fl_u.nfs4_fl.owner);
  3445. if (data == NULL)
  3446. return -ENOMEM;
  3447. if (IS_SETLKW(cmd))
  3448. data->arg.block = 1;
  3449. if (reclaim != 0)
  3450. data->arg.reclaim = 1;
  3451. msg.rpc_argp = &data->arg,
  3452. msg.rpc_resp = &data->res,
  3453. task_setup_data.callback_data = data;
  3454. task = rpc_run_task(&task_setup_data);
  3455. if (IS_ERR(task))
  3456. return PTR_ERR(task);
  3457. ret = nfs4_wait_for_completion_rpc_task(task);
  3458. if (ret == 0) {
  3459. ret = data->rpc_status;
  3460. if (ret == -NFS4ERR_DENIED)
  3461. ret = -EAGAIN;
  3462. } else
  3463. data->cancelled = 1;
  3464. rpc_put_task(task);
  3465. dprintk("%s: done, ret = %d!\n", __func__, ret);
  3466. return ret;
  3467. }
  3468. static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
  3469. {
  3470. struct nfs_server *server = NFS_SERVER(state->inode);
  3471. struct nfs4_exception exception = { };
  3472. int err;
  3473. do {
  3474. /* Cache the lock if possible... */
  3475. if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
  3476. return 0;
  3477. err = _nfs4_do_setlk(state, F_SETLK, request, 1);
  3478. if (err != -NFS4ERR_DELAY)
  3479. break;
  3480. nfs4_handle_exception(server, err, &exception);
  3481. } while (exception.retry);
  3482. return err;
  3483. }
  3484. static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
  3485. {
  3486. struct nfs_server *server = NFS_SERVER(state->inode);
  3487. struct nfs4_exception exception = { };
  3488. int err;
  3489. err = nfs4_set_lock_state(state, request);
  3490. if (err != 0)
  3491. return err;
  3492. do {
  3493. if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
  3494. return 0;
  3495. err = _nfs4_do_setlk(state, F_SETLK, request, 0);
  3496. if (err != -NFS4ERR_DELAY)
  3497. break;
  3498. nfs4_handle_exception(server, err, &exception);
  3499. } while (exception.retry);
  3500. return err;
  3501. }
  3502. static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  3503. {
  3504. struct nfs_inode *nfsi = NFS_I(state->inode);
  3505. unsigned char fl_flags = request->fl_flags;
  3506. int status;
  3507. /* Is this a delegated open? */
  3508. status = nfs4_set_lock_state(state, request);
  3509. if (status != 0)
  3510. goto out;
  3511. request->fl_flags |= FL_ACCESS;
  3512. status = do_vfs_lock(request->fl_file, request);
  3513. if (status < 0)
  3514. goto out;
  3515. down_read(&nfsi->rwsem);
  3516. if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
  3517. /* Yes: cache locks! */
  3518. /* ...but avoid races with delegation recall... */
  3519. request->fl_flags = fl_flags & ~FL_SLEEP;
  3520. status = do_vfs_lock(request->fl_file, request);
  3521. goto out_unlock;
  3522. }
  3523. status = _nfs4_do_setlk(state, cmd, request, 0);
  3524. if (status != 0)
  3525. goto out_unlock;
  3526. /* Note: we always want to sleep here! */
  3527. request->fl_flags = fl_flags | FL_SLEEP;
  3528. if (do_vfs_lock(request->fl_file, request) < 0)
  3529. printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
  3530. out_unlock:
  3531. up_read(&nfsi->rwsem);
  3532. out:
  3533. request->fl_flags = fl_flags;
  3534. return status;
  3535. }
  3536. static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  3537. {
  3538. struct nfs4_exception exception = { };
  3539. int err;
  3540. do {
  3541. err = nfs4_handle_exception(NFS_SERVER(state->inode),
  3542. _nfs4_proc_setlk(state, cmd, request),
  3543. &exception);
  3544. } while (exception.retry);
  3545. return err;
  3546. }
  3547. static int
  3548. nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
  3549. {
  3550. struct nfs_open_context *ctx;
  3551. struct nfs4_state *state;
  3552. unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
  3553. int status;
  3554. /* verify open state */
  3555. ctx = nfs_file_open_context(filp);
  3556. state = ctx->state;
  3557. if (request->fl_start < 0 || request->fl_end < 0)
  3558. return -EINVAL;
  3559. if (IS_GETLK(cmd))
  3560. return nfs4_proc_getlk(state, F_GETLK, request);
  3561. if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
  3562. return -EINVAL;
  3563. if (request->fl_type == F_UNLCK)
  3564. return nfs4_proc_unlck(state, cmd, request);
  3565. do {
  3566. status = nfs4_proc_setlk(state, cmd, request);
  3567. if ((status != -EAGAIN) || IS_SETLK(cmd))
  3568. break;
  3569. timeout = nfs4_set_lock_task_retry(timeout);
  3570. status = -ERESTARTSYS;
  3571. if (signalled())
  3572. break;
  3573. } while(status < 0);
  3574. return status;
  3575. }
  3576. int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
  3577. {
  3578. struct nfs_server *server = NFS_SERVER(state->inode);
  3579. struct nfs4_exception exception = { };
  3580. int err;
  3581. err = nfs4_set_lock_state(state, fl);
  3582. if (err != 0)
  3583. goto out;
  3584. do {
  3585. err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
  3586. if (err != -NFS4ERR_DELAY)
  3587. break;
  3588. err = nfs4_handle_exception(server, err, &exception);
  3589. } while (exception.retry);
  3590. out:
  3591. return err;
  3592. }
  3593. #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
  3594. int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
  3595. size_t buflen, int flags)
  3596. {
  3597. struct inode *inode = dentry->d_inode;
  3598. if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
  3599. return -EOPNOTSUPP;
  3600. return nfs4_proc_set_acl(inode, buf, buflen);
  3601. }
  3602. /* The getxattr man page suggests returning -ENODATA for unknown attributes,
  3603. * and that's what we'll do for e.g. user attributes that haven't been set.
  3604. * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
  3605. * attributes in kernel-managed attribute namespaces. */
  3606. ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
  3607. size_t buflen)
  3608. {
  3609. struct inode *inode = dentry->d_inode;
  3610. if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
  3611. return -EOPNOTSUPP;
  3612. return nfs4_proc_get_acl(inode, buf, buflen);
  3613. }
  3614. ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
  3615. {
  3616. size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
  3617. if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
  3618. return 0;
  3619. if (buf && buflen < len)
  3620. return -ERANGE;
  3621. if (buf)
  3622. memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
  3623. return len;
  3624. }
  3625. static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
  3626. {
  3627. if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
  3628. (fattr->valid & NFS_ATTR_FATTR_FSID) &&
  3629. (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
  3630. return;
  3631. fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
  3632. NFS_ATTR_FATTR_NLINK;
  3633. fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
  3634. fattr->nlink = 2;
  3635. }
  3636. int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
  3637. struct nfs4_fs_locations *fs_locations, struct page *page)
  3638. {
  3639. struct nfs_server *server = NFS_SERVER(dir);
  3640. u32 bitmask[2] = {
  3641. [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
  3642. [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
  3643. };
  3644. struct nfs4_fs_locations_arg args = {
  3645. .dir_fh = NFS_FH(dir),
  3646. .name = name,
  3647. .page = page,
  3648. .bitmask = bitmask,
  3649. };
  3650. struct nfs4_fs_locations_res res = {
  3651. .fs_locations = fs_locations,
  3652. };
  3653. struct rpc_message msg = {
  3654. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
  3655. .rpc_argp = &args,
  3656. .rpc_resp = &res,
  3657. };
  3658. int status;
  3659. dprintk("%s: start\n", __func__);
  3660. nfs_fattr_init(&fs_locations->fattr);
  3661. fs_locations->server = server;
  3662. fs_locations->nlocations = 0;
  3663. status = nfs4_call_sync(server, &msg, &args, &res, 0);
  3664. nfs_fixup_referral_attributes(&fs_locations->fattr);
  3665. dprintk("%s: returned status = %d\n", __func__, status);
  3666. return status;
  3667. }
  3668. #ifdef CONFIG_NFS_V4_1
  3669. /* Destroy the slot table */
  3670. static void nfs4_destroy_slot_table(struct nfs4_session *session)
  3671. {
  3672. if (session->fc_slot_table.slots == NULL)
  3673. return;
  3674. kfree(session->fc_slot_table.slots);
  3675. session->fc_slot_table.slots = NULL;
  3676. return;
  3677. }
  3678. struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
  3679. {
  3680. struct nfs4_session *session;
  3681. struct nfs4_slot_table *tbl;
  3682. session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
  3683. if (!session)
  3684. return NULL;
  3685. tbl = &session->fc_slot_table;
  3686. spin_lock_init(&tbl->slot_tbl_lock);
  3687. rpc_init_wait_queue(&tbl->slot_tbl_waitq, "Slot table");
  3688. session->clp = clp;
  3689. return session;
  3690. }
  3691. void nfs4_destroy_session(struct nfs4_session *session)
  3692. {
  3693. nfs4_destroy_slot_table(session);
  3694. kfree(session);
  3695. }
  3696. #endif /* CONFIG_NFS_V4_1 */
  3697. struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
  3698. .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
  3699. .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
  3700. .recover_open = nfs4_open_reclaim,
  3701. .recover_lock = nfs4_lock_reclaim,
  3702. };
  3703. struct nfs4_state_recovery_ops nfs4_nograce_recovery_ops = {
  3704. .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
  3705. .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
  3706. .recover_open = nfs4_open_expired,
  3707. .recover_lock = nfs4_lock_expired,
  3708. };
  3709. static const struct inode_operations nfs4_file_inode_operations = {
  3710. .permission = nfs_permission,
  3711. .getattr = nfs_getattr,
  3712. .setattr = nfs_setattr,
  3713. .getxattr = nfs4_getxattr,
  3714. .setxattr = nfs4_setxattr,
  3715. .listxattr = nfs4_listxattr,
  3716. };
  3717. const struct nfs_rpc_ops nfs_v4_clientops = {
  3718. .version = 4, /* protocol version */
  3719. .dentry_ops = &nfs4_dentry_operations,
  3720. .dir_inode_ops = &nfs4_dir_inode_operations,
  3721. .file_inode_ops = &nfs4_file_inode_operations,
  3722. .getroot = nfs4_proc_get_root,
  3723. .getattr = nfs4_proc_getattr,
  3724. .setattr = nfs4_proc_setattr,
  3725. .lookupfh = nfs4_proc_lookupfh,
  3726. .lookup = nfs4_proc_lookup,
  3727. .access = nfs4_proc_access,
  3728. .readlink = nfs4_proc_readlink,
  3729. .create = nfs4_proc_create,
  3730. .remove = nfs4_proc_remove,
  3731. .unlink_setup = nfs4_proc_unlink_setup,
  3732. .unlink_done = nfs4_proc_unlink_done,
  3733. .rename = nfs4_proc_rename,
  3734. .link = nfs4_proc_link,
  3735. .symlink = nfs4_proc_symlink,
  3736. .mkdir = nfs4_proc_mkdir,
  3737. .rmdir = nfs4_proc_remove,
  3738. .readdir = nfs4_proc_readdir,
  3739. .mknod = nfs4_proc_mknod,
  3740. .statfs = nfs4_proc_statfs,
  3741. .fsinfo = nfs4_proc_fsinfo,
  3742. .pathconf = nfs4_proc_pathconf,
  3743. .set_capabilities = nfs4_server_capabilities,
  3744. .decode_dirent = nfs4_decode_dirent,
  3745. .read_setup = nfs4_proc_read_setup,
  3746. .read_done = nfs4_read_done,
  3747. .write_setup = nfs4_proc_write_setup,
  3748. .write_done = nfs4_write_done,
  3749. .commit_setup = nfs4_proc_commit_setup,
  3750. .commit_done = nfs4_commit_done,
  3751. .lock = nfs4_proc_lock,
  3752. .clear_acl_cache = nfs4_zap_acl_attr,
  3753. .close_context = nfs4_close_context,
  3754. };
  3755. /*
  3756. * Local variables:
  3757. * c-basic-offset: 8
  3758. * End:
  3759. */