nfs4proc.c 111 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163
  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. if (RPC_ASSASSINATED(task))
  1466. return;
  1467. /* hmm. we are done with the inode, and in the process of freeing
  1468. * the state_owner. we keep this around to process errors
  1469. */
  1470. switch (task->tk_status) {
  1471. case 0:
  1472. nfs_set_open_stateid(state, &calldata->res.stateid, 0);
  1473. renew_lease(server, calldata->timestamp);
  1474. break;
  1475. case -NFS4ERR_STALE_STATEID:
  1476. case -NFS4ERR_OLD_STATEID:
  1477. case -NFS4ERR_BAD_STATEID:
  1478. case -NFS4ERR_EXPIRED:
  1479. if (calldata->arg.fmode == 0)
  1480. break;
  1481. default:
  1482. if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
  1483. rpc_restart_call(task);
  1484. return;
  1485. }
  1486. }
  1487. nfs_refresh_inode(calldata->inode, calldata->res.fattr);
  1488. }
  1489. static void nfs4_close_prepare(struct rpc_task *task, void *data)
  1490. {
  1491. struct nfs4_closedata *calldata = data;
  1492. struct nfs4_state *state = calldata->state;
  1493. int clear_rd, clear_wr, clear_rdwr;
  1494. if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
  1495. return;
  1496. clear_rd = clear_wr = clear_rdwr = 0;
  1497. spin_lock(&state->owner->so_lock);
  1498. /* Calculate the change in open mode */
  1499. if (state->n_rdwr == 0) {
  1500. if (state->n_rdonly == 0) {
  1501. clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  1502. clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
  1503. }
  1504. if (state->n_wronly == 0) {
  1505. clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  1506. clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
  1507. }
  1508. }
  1509. spin_unlock(&state->owner->so_lock);
  1510. if (!clear_rd && !clear_wr && !clear_rdwr) {
  1511. /* Note: exit _without_ calling nfs4_close_done */
  1512. task->tk_action = NULL;
  1513. return;
  1514. }
  1515. nfs_fattr_init(calldata->res.fattr);
  1516. if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0) {
  1517. task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
  1518. calldata->arg.fmode = FMODE_READ;
  1519. } else if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0) {
  1520. task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
  1521. calldata->arg.fmode = FMODE_WRITE;
  1522. }
  1523. calldata->timestamp = jiffies;
  1524. rpc_call_start(task);
  1525. }
  1526. static const struct rpc_call_ops nfs4_close_ops = {
  1527. .rpc_call_prepare = nfs4_close_prepare,
  1528. .rpc_call_done = nfs4_close_done,
  1529. .rpc_release = nfs4_free_closedata,
  1530. };
  1531. /*
  1532. * It is possible for data to be read/written from a mem-mapped file
  1533. * after the sys_close call (which hits the vfs layer as a flush).
  1534. * This means that we can't safely call nfsv4 close on a file until
  1535. * the inode is cleared. This in turn means that we are not good
  1536. * NFSv4 citizens - we do not indicate to the server to update the file's
  1537. * share state even when we are done with one of the three share
  1538. * stateid's in the inode.
  1539. *
  1540. * NOTE: Caller must be holding the sp->so_owner semaphore!
  1541. */
  1542. int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
  1543. {
  1544. struct nfs_server *server = NFS_SERVER(state->inode);
  1545. struct nfs4_closedata *calldata;
  1546. struct nfs4_state_owner *sp = state->owner;
  1547. struct rpc_task *task;
  1548. struct rpc_message msg = {
  1549. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
  1550. .rpc_cred = state->owner->so_cred,
  1551. };
  1552. struct rpc_task_setup task_setup_data = {
  1553. .rpc_client = server->client,
  1554. .rpc_message = &msg,
  1555. .callback_ops = &nfs4_close_ops,
  1556. .workqueue = nfsiod_workqueue,
  1557. .flags = RPC_TASK_ASYNC,
  1558. };
  1559. int status = -ENOMEM;
  1560. calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
  1561. if (calldata == NULL)
  1562. goto out;
  1563. calldata->inode = state->inode;
  1564. calldata->state = state;
  1565. calldata->arg.fh = NFS_FH(state->inode);
  1566. calldata->arg.stateid = &state->open_stateid;
  1567. /* Serialization for the sequence id */
  1568. calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
  1569. if (calldata->arg.seqid == NULL)
  1570. goto out_free_calldata;
  1571. calldata->arg.fmode = 0;
  1572. calldata->arg.bitmask = server->cache_consistency_bitmask;
  1573. calldata->res.fattr = &calldata->fattr;
  1574. calldata->res.seqid = calldata->arg.seqid;
  1575. calldata->res.server = server;
  1576. calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
  1577. calldata->path.mnt = mntget(path->mnt);
  1578. calldata->path.dentry = dget(path->dentry);
  1579. msg.rpc_argp = &calldata->arg,
  1580. msg.rpc_resp = &calldata->res,
  1581. task_setup_data.callback_data = calldata;
  1582. task = rpc_run_task(&task_setup_data);
  1583. if (IS_ERR(task))
  1584. return PTR_ERR(task);
  1585. status = 0;
  1586. if (wait)
  1587. status = rpc_wait_for_completion_task(task);
  1588. rpc_put_task(task);
  1589. return status;
  1590. out_free_calldata:
  1591. kfree(calldata);
  1592. out:
  1593. nfs4_put_open_state(state);
  1594. nfs4_put_state_owner(sp);
  1595. return status;
  1596. }
  1597. static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state, fmode_t fmode)
  1598. {
  1599. struct file *filp;
  1600. int ret;
  1601. /* If the open_intent is for execute, we have an extra check to make */
  1602. if (fmode & FMODE_EXEC) {
  1603. ret = nfs_may_open(state->inode,
  1604. state->owner->so_cred,
  1605. nd->intent.open.flags);
  1606. if (ret < 0)
  1607. goto out_close;
  1608. }
  1609. filp = lookup_instantiate_filp(nd, path->dentry, NULL);
  1610. if (!IS_ERR(filp)) {
  1611. struct nfs_open_context *ctx;
  1612. ctx = nfs_file_open_context(filp);
  1613. ctx->state = state;
  1614. return 0;
  1615. }
  1616. ret = PTR_ERR(filp);
  1617. out_close:
  1618. nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
  1619. return ret;
  1620. }
  1621. struct dentry *
  1622. nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  1623. {
  1624. struct path path = {
  1625. .mnt = nd->path.mnt,
  1626. .dentry = dentry,
  1627. };
  1628. struct dentry *parent;
  1629. struct iattr attr;
  1630. struct rpc_cred *cred;
  1631. struct nfs4_state *state;
  1632. struct dentry *res;
  1633. fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
  1634. if (nd->flags & LOOKUP_CREATE) {
  1635. attr.ia_mode = nd->intent.open.create_mode;
  1636. attr.ia_valid = ATTR_MODE;
  1637. if (!IS_POSIXACL(dir))
  1638. attr.ia_mode &= ~current_umask();
  1639. } else {
  1640. attr.ia_valid = 0;
  1641. BUG_ON(nd->intent.open.flags & O_CREAT);
  1642. }
  1643. cred = rpc_lookup_cred();
  1644. if (IS_ERR(cred))
  1645. return (struct dentry *)cred;
  1646. parent = dentry->d_parent;
  1647. /* Protect against concurrent sillydeletes */
  1648. nfs_block_sillyrename(parent);
  1649. state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
  1650. put_rpccred(cred);
  1651. if (IS_ERR(state)) {
  1652. if (PTR_ERR(state) == -ENOENT) {
  1653. d_add(dentry, NULL);
  1654. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1655. }
  1656. nfs_unblock_sillyrename(parent);
  1657. return (struct dentry *)state;
  1658. }
  1659. res = d_add_unique(dentry, igrab(state->inode));
  1660. if (res != NULL)
  1661. path.dentry = res;
  1662. nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
  1663. nfs_unblock_sillyrename(parent);
  1664. nfs4_intent_set_file(nd, &path, state, fmode);
  1665. return res;
  1666. }
  1667. int
  1668. nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
  1669. {
  1670. struct path path = {
  1671. .mnt = nd->path.mnt,
  1672. .dentry = dentry,
  1673. };
  1674. struct rpc_cred *cred;
  1675. struct nfs4_state *state;
  1676. fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
  1677. cred = rpc_lookup_cred();
  1678. if (IS_ERR(cred))
  1679. return PTR_ERR(cred);
  1680. state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
  1681. put_rpccred(cred);
  1682. if (IS_ERR(state)) {
  1683. switch (PTR_ERR(state)) {
  1684. case -EPERM:
  1685. case -EACCES:
  1686. case -EDQUOT:
  1687. case -ENOSPC:
  1688. case -EROFS:
  1689. lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
  1690. return 1;
  1691. default:
  1692. goto out_drop;
  1693. }
  1694. }
  1695. if (state->inode == dentry->d_inode) {
  1696. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1697. nfs4_intent_set_file(nd, &path, state, fmode);
  1698. return 1;
  1699. }
  1700. nfs4_close_sync(&path, state, fmode);
  1701. out_drop:
  1702. d_drop(dentry);
  1703. return 0;
  1704. }
  1705. void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
  1706. {
  1707. if (ctx->state == NULL)
  1708. return;
  1709. if (is_sync)
  1710. nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
  1711. else
  1712. nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
  1713. }
  1714. static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
  1715. {
  1716. struct nfs4_server_caps_arg args = {
  1717. .fhandle = fhandle,
  1718. };
  1719. struct nfs4_server_caps_res res = {};
  1720. struct rpc_message msg = {
  1721. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
  1722. .rpc_argp = &args,
  1723. .rpc_resp = &res,
  1724. };
  1725. int status;
  1726. status = nfs4_call_sync(server, &msg, &args, &res, 0);
  1727. if (status == 0) {
  1728. memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
  1729. if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
  1730. server->caps |= NFS_CAP_ACLS;
  1731. if (res.has_links != 0)
  1732. server->caps |= NFS_CAP_HARDLINKS;
  1733. if (res.has_symlinks != 0)
  1734. server->caps |= NFS_CAP_SYMLINKS;
  1735. memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
  1736. server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
  1737. server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
  1738. server->acl_bitmask = res.acl_bitmask;
  1739. }
  1740. return status;
  1741. }
  1742. int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
  1743. {
  1744. struct nfs4_exception exception = { };
  1745. int err;
  1746. do {
  1747. err = nfs4_handle_exception(server,
  1748. _nfs4_server_capabilities(server, fhandle),
  1749. &exception);
  1750. } while (exception.retry);
  1751. return err;
  1752. }
  1753. static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
  1754. struct nfs_fsinfo *info)
  1755. {
  1756. struct nfs4_lookup_root_arg args = {
  1757. .bitmask = nfs4_fattr_bitmap,
  1758. };
  1759. struct nfs4_lookup_res res = {
  1760. .server = server,
  1761. .fattr = info->fattr,
  1762. .fh = fhandle,
  1763. };
  1764. struct rpc_message msg = {
  1765. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
  1766. .rpc_argp = &args,
  1767. .rpc_resp = &res,
  1768. };
  1769. nfs_fattr_init(info->fattr);
  1770. return nfs4_call_sync(server, &msg, &args, &res, 0);
  1771. }
  1772. static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
  1773. struct nfs_fsinfo *info)
  1774. {
  1775. struct nfs4_exception exception = { };
  1776. int err;
  1777. do {
  1778. err = nfs4_handle_exception(server,
  1779. _nfs4_lookup_root(server, fhandle, info),
  1780. &exception);
  1781. } while (exception.retry);
  1782. return err;
  1783. }
  1784. /*
  1785. * get the file handle for the "/" directory on the server
  1786. */
  1787. static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
  1788. struct nfs_fsinfo *info)
  1789. {
  1790. int status;
  1791. status = nfs4_lookup_root(server, fhandle, info);
  1792. if (status == 0)
  1793. status = nfs4_server_capabilities(server, fhandle);
  1794. if (status == 0)
  1795. status = nfs4_do_fsinfo(server, fhandle, info);
  1796. return nfs4_map_errors(status);
  1797. }
  1798. /*
  1799. * Get locations and (maybe) other attributes of a referral.
  1800. * Note that we'll actually follow the referral later when
  1801. * we detect fsid mismatch in inode revalidation
  1802. */
  1803. static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
  1804. {
  1805. int status = -ENOMEM;
  1806. struct page *page = NULL;
  1807. struct nfs4_fs_locations *locations = NULL;
  1808. page = alloc_page(GFP_KERNEL);
  1809. if (page == NULL)
  1810. goto out;
  1811. locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
  1812. if (locations == NULL)
  1813. goto out;
  1814. status = nfs4_proc_fs_locations(dir, name, locations, page);
  1815. if (status != 0)
  1816. goto out;
  1817. /* Make sure server returned a different fsid for the referral */
  1818. if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
  1819. dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
  1820. status = -EIO;
  1821. goto out;
  1822. }
  1823. memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
  1824. fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
  1825. if (!fattr->mode)
  1826. fattr->mode = S_IFDIR;
  1827. memset(fhandle, 0, sizeof(struct nfs_fh));
  1828. out:
  1829. if (page)
  1830. __free_page(page);
  1831. if (locations)
  1832. kfree(locations);
  1833. return status;
  1834. }
  1835. static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  1836. {
  1837. struct nfs4_getattr_arg args = {
  1838. .fh = fhandle,
  1839. .bitmask = server->attr_bitmask,
  1840. };
  1841. struct nfs4_getattr_res res = {
  1842. .fattr = fattr,
  1843. .server = server,
  1844. };
  1845. struct rpc_message msg = {
  1846. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
  1847. .rpc_argp = &args,
  1848. .rpc_resp = &res,
  1849. };
  1850. nfs_fattr_init(fattr);
  1851. return nfs4_call_sync(server, &msg, &args, &res, 0);
  1852. }
  1853. static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  1854. {
  1855. struct nfs4_exception exception = { };
  1856. int err;
  1857. do {
  1858. err = nfs4_handle_exception(server,
  1859. _nfs4_proc_getattr(server, fhandle, fattr),
  1860. &exception);
  1861. } while (exception.retry);
  1862. return err;
  1863. }
  1864. /*
  1865. * The file is not closed if it is opened due to the a request to change
  1866. * the size of the file. The open call will not be needed once the
  1867. * VFS layer lookup-intents are implemented.
  1868. *
  1869. * Close is called when the inode is destroyed.
  1870. * If we haven't opened the file for O_WRONLY, we
  1871. * need to in the size_change case to obtain a stateid.
  1872. *
  1873. * Got race?
  1874. * Because OPEN is always done by name in nfsv4, it is
  1875. * possible that we opened a different file by the same
  1876. * name. We can recognize this race condition, but we
  1877. * can't do anything about it besides returning an error.
  1878. *
  1879. * This will be fixed with VFS changes (lookup-intent).
  1880. */
  1881. static int
  1882. nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
  1883. struct iattr *sattr)
  1884. {
  1885. struct inode *inode = dentry->d_inode;
  1886. struct rpc_cred *cred = NULL;
  1887. struct nfs4_state *state = NULL;
  1888. int status;
  1889. nfs_fattr_init(fattr);
  1890. /* Search for an existing open(O_WRITE) file */
  1891. if (sattr->ia_valid & ATTR_FILE) {
  1892. struct nfs_open_context *ctx;
  1893. ctx = nfs_file_open_context(sattr->ia_file);
  1894. if (ctx) {
  1895. cred = ctx->cred;
  1896. state = ctx->state;
  1897. }
  1898. }
  1899. status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
  1900. if (status == 0)
  1901. nfs_setattr_update_inode(inode, sattr);
  1902. return status;
  1903. }
  1904. static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
  1905. const struct qstr *name, struct nfs_fh *fhandle,
  1906. struct nfs_fattr *fattr)
  1907. {
  1908. int status;
  1909. struct nfs4_lookup_arg args = {
  1910. .bitmask = server->attr_bitmask,
  1911. .dir_fh = dirfh,
  1912. .name = name,
  1913. };
  1914. struct nfs4_lookup_res res = {
  1915. .server = server,
  1916. .fattr = fattr,
  1917. .fh = fhandle,
  1918. };
  1919. struct rpc_message msg = {
  1920. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
  1921. .rpc_argp = &args,
  1922. .rpc_resp = &res,
  1923. };
  1924. nfs_fattr_init(fattr);
  1925. dprintk("NFS call lookupfh %s\n", name->name);
  1926. status = nfs4_call_sync(server, &msg, &args, &res, 0);
  1927. dprintk("NFS reply lookupfh: %d\n", status);
  1928. return status;
  1929. }
  1930. static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
  1931. struct qstr *name, struct nfs_fh *fhandle,
  1932. struct nfs_fattr *fattr)
  1933. {
  1934. struct nfs4_exception exception = { };
  1935. int err;
  1936. do {
  1937. err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
  1938. /* FIXME: !!!! */
  1939. if (err == -NFS4ERR_MOVED) {
  1940. err = -EREMOTE;
  1941. break;
  1942. }
  1943. err = nfs4_handle_exception(server, err, &exception);
  1944. } while (exception.retry);
  1945. return err;
  1946. }
  1947. static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
  1948. struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  1949. {
  1950. int status;
  1951. dprintk("NFS call lookup %s\n", name->name);
  1952. status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
  1953. if (status == -NFS4ERR_MOVED)
  1954. status = nfs4_get_referral(dir, name, fattr, fhandle);
  1955. dprintk("NFS reply lookup: %d\n", status);
  1956. return status;
  1957. }
  1958. static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  1959. {
  1960. struct nfs4_exception exception = { };
  1961. int err;
  1962. do {
  1963. err = nfs4_handle_exception(NFS_SERVER(dir),
  1964. _nfs4_proc_lookup(dir, name, fhandle, fattr),
  1965. &exception);
  1966. } while (exception.retry);
  1967. return err;
  1968. }
  1969. static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
  1970. {
  1971. struct nfs_server *server = NFS_SERVER(inode);
  1972. struct nfs_fattr fattr;
  1973. struct nfs4_accessargs args = {
  1974. .fh = NFS_FH(inode),
  1975. .bitmask = server->attr_bitmask,
  1976. };
  1977. struct nfs4_accessres res = {
  1978. .server = server,
  1979. .fattr = &fattr,
  1980. };
  1981. struct rpc_message msg = {
  1982. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
  1983. .rpc_argp = &args,
  1984. .rpc_resp = &res,
  1985. .rpc_cred = entry->cred,
  1986. };
  1987. int mode = entry->mask;
  1988. int status;
  1989. /*
  1990. * Determine which access bits we want to ask for...
  1991. */
  1992. if (mode & MAY_READ)
  1993. args.access |= NFS4_ACCESS_READ;
  1994. if (S_ISDIR(inode->i_mode)) {
  1995. if (mode & MAY_WRITE)
  1996. args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
  1997. if (mode & MAY_EXEC)
  1998. args.access |= NFS4_ACCESS_LOOKUP;
  1999. } else {
  2000. if (mode & MAY_WRITE)
  2001. args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
  2002. if (mode & MAY_EXEC)
  2003. args.access |= NFS4_ACCESS_EXECUTE;
  2004. }
  2005. nfs_fattr_init(&fattr);
  2006. status = nfs4_call_sync(server, &msg, &args, &res, 0);
  2007. if (!status) {
  2008. entry->mask = 0;
  2009. if (res.access & NFS4_ACCESS_READ)
  2010. entry->mask |= MAY_READ;
  2011. if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
  2012. entry->mask |= MAY_WRITE;
  2013. if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
  2014. entry->mask |= MAY_EXEC;
  2015. nfs_refresh_inode(inode, &fattr);
  2016. }
  2017. return status;
  2018. }
  2019. static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
  2020. {
  2021. struct nfs4_exception exception = { };
  2022. int err;
  2023. do {
  2024. err = nfs4_handle_exception(NFS_SERVER(inode),
  2025. _nfs4_proc_access(inode, entry),
  2026. &exception);
  2027. } while (exception.retry);
  2028. return err;
  2029. }
  2030. /*
  2031. * TODO: For the time being, we don't try to get any attributes
  2032. * along with any of the zero-copy operations READ, READDIR,
  2033. * READLINK, WRITE.
  2034. *
  2035. * In the case of the first three, we want to put the GETATTR
  2036. * after the read-type operation -- this is because it is hard
  2037. * to predict the length of a GETATTR response in v4, and thus
  2038. * align the READ data correctly. This means that the GETATTR
  2039. * may end up partially falling into the page cache, and we should
  2040. * shift it into the 'tail' of the xdr_buf before processing.
  2041. * To do this efficiently, we need to know the total length
  2042. * of data received, which doesn't seem to be available outside
  2043. * of the RPC layer.
  2044. *
  2045. * In the case of WRITE, we also want to put the GETATTR after
  2046. * the operation -- in this case because we want to make sure
  2047. * we get the post-operation mtime and size. This means that
  2048. * we can't use xdr_encode_pages() as written: we need a variant
  2049. * of it which would leave room in the 'tail' iovec.
  2050. *
  2051. * Both of these changes to the XDR layer would in fact be quite
  2052. * minor, but I decided to leave them for a subsequent patch.
  2053. */
  2054. static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
  2055. unsigned int pgbase, unsigned int pglen)
  2056. {
  2057. struct nfs4_readlink args = {
  2058. .fh = NFS_FH(inode),
  2059. .pgbase = pgbase,
  2060. .pglen = pglen,
  2061. .pages = &page,
  2062. };
  2063. struct nfs4_readlink_res res;
  2064. struct rpc_message msg = {
  2065. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
  2066. .rpc_argp = &args,
  2067. .rpc_resp = &res,
  2068. };
  2069. return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
  2070. }
  2071. static int nfs4_proc_readlink(struct inode *inode, struct page *page,
  2072. unsigned int pgbase, unsigned int pglen)
  2073. {
  2074. struct nfs4_exception exception = { };
  2075. int err;
  2076. do {
  2077. err = nfs4_handle_exception(NFS_SERVER(inode),
  2078. _nfs4_proc_readlink(inode, page, pgbase, pglen),
  2079. &exception);
  2080. } while (exception.retry);
  2081. return err;
  2082. }
  2083. /*
  2084. * Got race?
  2085. * We will need to arrange for the VFS layer to provide an atomic open.
  2086. * Until then, this create/open method is prone to inefficiency and race
  2087. * conditions due to the lookup, create, and open VFS calls from sys_open()
  2088. * placed on the wire.
  2089. *
  2090. * Given the above sorry state of affairs, I'm simply sending an OPEN.
  2091. * The file will be opened again in the subsequent VFS open call
  2092. * (nfs4_proc_file_open).
  2093. *
  2094. * The open for read will just hang around to be used by any process that
  2095. * opens the file O_RDONLY. This will all be resolved with the VFS changes.
  2096. */
  2097. static int
  2098. nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
  2099. int flags, struct nameidata *nd)
  2100. {
  2101. struct path path = {
  2102. .mnt = nd->path.mnt,
  2103. .dentry = dentry,
  2104. };
  2105. struct nfs4_state *state;
  2106. struct rpc_cred *cred;
  2107. fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
  2108. int status = 0;
  2109. cred = rpc_lookup_cred();
  2110. if (IS_ERR(cred)) {
  2111. status = PTR_ERR(cred);
  2112. goto out;
  2113. }
  2114. state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
  2115. d_drop(dentry);
  2116. if (IS_ERR(state)) {
  2117. status = PTR_ERR(state);
  2118. goto out_putcred;
  2119. }
  2120. d_add(dentry, igrab(state->inode));
  2121. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  2122. if (flags & O_EXCL) {
  2123. struct nfs_fattr fattr;
  2124. status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
  2125. if (status == 0)
  2126. nfs_setattr_update_inode(state->inode, sattr);
  2127. nfs_post_op_update_inode(state->inode, &fattr);
  2128. }
  2129. if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
  2130. status = nfs4_intent_set_file(nd, &path, state, fmode);
  2131. else
  2132. nfs4_close_sync(&path, state, fmode);
  2133. out_putcred:
  2134. put_rpccred(cred);
  2135. out:
  2136. return status;
  2137. }
  2138. static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
  2139. {
  2140. struct nfs_server *server = NFS_SERVER(dir);
  2141. struct nfs_removeargs args = {
  2142. .fh = NFS_FH(dir),
  2143. .name.len = name->len,
  2144. .name.name = name->name,
  2145. .bitmask = server->attr_bitmask,
  2146. };
  2147. struct nfs_removeres res = {
  2148. .server = server,
  2149. };
  2150. struct rpc_message msg = {
  2151. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
  2152. .rpc_argp = &args,
  2153. .rpc_resp = &res,
  2154. };
  2155. int status;
  2156. nfs_fattr_init(&res.dir_attr);
  2157. status = nfs4_call_sync(server, &msg, &args, &res, 1);
  2158. if (status == 0) {
  2159. update_changeattr(dir, &res.cinfo);
  2160. nfs_post_op_update_inode(dir, &res.dir_attr);
  2161. }
  2162. return status;
  2163. }
  2164. static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
  2165. {
  2166. struct nfs4_exception exception = { };
  2167. int err;
  2168. do {
  2169. err = nfs4_handle_exception(NFS_SERVER(dir),
  2170. _nfs4_proc_remove(dir, name),
  2171. &exception);
  2172. } while (exception.retry);
  2173. return err;
  2174. }
  2175. static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
  2176. {
  2177. struct nfs_server *server = NFS_SERVER(dir);
  2178. struct nfs_removeargs *args = msg->rpc_argp;
  2179. struct nfs_removeres *res = msg->rpc_resp;
  2180. args->bitmask = server->cache_consistency_bitmask;
  2181. res->server = server;
  2182. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
  2183. }
  2184. static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
  2185. {
  2186. struct nfs_removeres *res = task->tk_msg.rpc_resp;
  2187. if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
  2188. return 0;
  2189. update_changeattr(dir, &res->cinfo);
  2190. nfs_post_op_update_inode(dir, &res->dir_attr);
  2191. return 1;
  2192. }
  2193. static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
  2194. struct inode *new_dir, struct qstr *new_name)
  2195. {
  2196. struct nfs_server *server = NFS_SERVER(old_dir);
  2197. struct nfs4_rename_arg arg = {
  2198. .old_dir = NFS_FH(old_dir),
  2199. .new_dir = NFS_FH(new_dir),
  2200. .old_name = old_name,
  2201. .new_name = new_name,
  2202. .bitmask = server->attr_bitmask,
  2203. };
  2204. struct nfs_fattr old_fattr, new_fattr;
  2205. struct nfs4_rename_res res = {
  2206. .server = server,
  2207. .old_fattr = &old_fattr,
  2208. .new_fattr = &new_fattr,
  2209. };
  2210. struct rpc_message msg = {
  2211. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
  2212. .rpc_argp = &arg,
  2213. .rpc_resp = &res,
  2214. };
  2215. int status;
  2216. nfs_fattr_init(res.old_fattr);
  2217. nfs_fattr_init(res.new_fattr);
  2218. status = nfs4_call_sync(server, &msg, &arg, &res, 1);
  2219. if (!status) {
  2220. update_changeattr(old_dir, &res.old_cinfo);
  2221. nfs_post_op_update_inode(old_dir, res.old_fattr);
  2222. update_changeattr(new_dir, &res.new_cinfo);
  2223. nfs_post_op_update_inode(new_dir, res.new_fattr);
  2224. }
  2225. return status;
  2226. }
  2227. static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
  2228. struct inode *new_dir, struct qstr *new_name)
  2229. {
  2230. struct nfs4_exception exception = { };
  2231. int err;
  2232. do {
  2233. err = nfs4_handle_exception(NFS_SERVER(old_dir),
  2234. _nfs4_proc_rename(old_dir, old_name,
  2235. new_dir, new_name),
  2236. &exception);
  2237. } while (exception.retry);
  2238. return err;
  2239. }
  2240. static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
  2241. {
  2242. struct nfs_server *server = NFS_SERVER(inode);
  2243. struct nfs4_link_arg arg = {
  2244. .fh = NFS_FH(inode),
  2245. .dir_fh = NFS_FH(dir),
  2246. .name = name,
  2247. .bitmask = server->attr_bitmask,
  2248. };
  2249. struct nfs_fattr fattr, dir_attr;
  2250. struct nfs4_link_res res = {
  2251. .server = server,
  2252. .fattr = &fattr,
  2253. .dir_attr = &dir_attr,
  2254. };
  2255. struct rpc_message msg = {
  2256. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
  2257. .rpc_argp = &arg,
  2258. .rpc_resp = &res,
  2259. };
  2260. int status;
  2261. nfs_fattr_init(res.fattr);
  2262. nfs_fattr_init(res.dir_attr);
  2263. status = nfs4_call_sync(server, &msg, &arg, &res, 1);
  2264. if (!status) {
  2265. update_changeattr(dir, &res.cinfo);
  2266. nfs_post_op_update_inode(dir, res.dir_attr);
  2267. nfs_post_op_update_inode(inode, res.fattr);
  2268. }
  2269. return status;
  2270. }
  2271. static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
  2272. {
  2273. struct nfs4_exception exception = { };
  2274. int err;
  2275. do {
  2276. err = nfs4_handle_exception(NFS_SERVER(inode),
  2277. _nfs4_proc_link(inode, dir, name),
  2278. &exception);
  2279. } while (exception.retry);
  2280. return err;
  2281. }
  2282. struct nfs4_createdata {
  2283. struct rpc_message msg;
  2284. struct nfs4_create_arg arg;
  2285. struct nfs4_create_res res;
  2286. struct nfs_fh fh;
  2287. struct nfs_fattr fattr;
  2288. struct nfs_fattr dir_fattr;
  2289. };
  2290. static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
  2291. struct qstr *name, struct iattr *sattr, u32 ftype)
  2292. {
  2293. struct nfs4_createdata *data;
  2294. data = kzalloc(sizeof(*data), GFP_KERNEL);
  2295. if (data != NULL) {
  2296. struct nfs_server *server = NFS_SERVER(dir);
  2297. data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
  2298. data->msg.rpc_argp = &data->arg;
  2299. data->msg.rpc_resp = &data->res;
  2300. data->arg.dir_fh = NFS_FH(dir);
  2301. data->arg.server = server;
  2302. data->arg.name = name;
  2303. data->arg.attrs = sattr;
  2304. data->arg.ftype = ftype;
  2305. data->arg.bitmask = server->attr_bitmask;
  2306. data->res.server = server;
  2307. data->res.fh = &data->fh;
  2308. data->res.fattr = &data->fattr;
  2309. data->res.dir_fattr = &data->dir_fattr;
  2310. nfs_fattr_init(data->res.fattr);
  2311. nfs_fattr_init(data->res.dir_fattr);
  2312. }
  2313. return data;
  2314. }
  2315. static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
  2316. {
  2317. int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
  2318. &data->arg, &data->res, 1);
  2319. if (status == 0) {
  2320. update_changeattr(dir, &data->res.dir_cinfo);
  2321. nfs_post_op_update_inode(dir, data->res.dir_fattr);
  2322. status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
  2323. }
  2324. return status;
  2325. }
  2326. static void nfs4_free_createdata(struct nfs4_createdata *data)
  2327. {
  2328. kfree(data);
  2329. }
  2330. static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
  2331. struct page *page, unsigned int len, struct iattr *sattr)
  2332. {
  2333. struct nfs4_createdata *data;
  2334. int status = -ENAMETOOLONG;
  2335. if (len > NFS4_MAXPATHLEN)
  2336. goto out;
  2337. status = -ENOMEM;
  2338. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
  2339. if (data == NULL)
  2340. goto out;
  2341. data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
  2342. data->arg.u.symlink.pages = &page;
  2343. data->arg.u.symlink.len = len;
  2344. status = nfs4_do_create(dir, dentry, data);
  2345. nfs4_free_createdata(data);
  2346. out:
  2347. return status;
  2348. }
  2349. static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
  2350. struct page *page, unsigned int len, struct iattr *sattr)
  2351. {
  2352. struct nfs4_exception exception = { };
  2353. int err;
  2354. do {
  2355. err = nfs4_handle_exception(NFS_SERVER(dir),
  2356. _nfs4_proc_symlink(dir, dentry, page,
  2357. len, sattr),
  2358. &exception);
  2359. } while (exception.retry);
  2360. return err;
  2361. }
  2362. static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
  2363. struct iattr *sattr)
  2364. {
  2365. struct nfs4_createdata *data;
  2366. int status = -ENOMEM;
  2367. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
  2368. if (data == NULL)
  2369. goto out;
  2370. status = nfs4_do_create(dir, dentry, data);
  2371. nfs4_free_createdata(data);
  2372. out:
  2373. return status;
  2374. }
  2375. static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
  2376. struct iattr *sattr)
  2377. {
  2378. struct nfs4_exception exception = { };
  2379. int err;
  2380. do {
  2381. err = nfs4_handle_exception(NFS_SERVER(dir),
  2382. _nfs4_proc_mkdir(dir, dentry, sattr),
  2383. &exception);
  2384. } while (exception.retry);
  2385. return err;
  2386. }
  2387. static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
  2388. u64 cookie, struct page *page, unsigned int count, int plus)
  2389. {
  2390. struct inode *dir = dentry->d_inode;
  2391. struct nfs4_readdir_arg args = {
  2392. .fh = NFS_FH(dir),
  2393. .pages = &page,
  2394. .pgbase = 0,
  2395. .count = count,
  2396. .bitmask = NFS_SERVER(dentry->d_inode)->cache_consistency_bitmask,
  2397. };
  2398. struct nfs4_readdir_res res;
  2399. struct rpc_message msg = {
  2400. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
  2401. .rpc_argp = &args,
  2402. .rpc_resp = &res,
  2403. .rpc_cred = cred,
  2404. };
  2405. int status;
  2406. dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
  2407. dentry->d_parent->d_name.name,
  2408. dentry->d_name.name,
  2409. (unsigned long long)cookie);
  2410. nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
  2411. res.pgbase = args.pgbase;
  2412. status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
  2413. if (status == 0)
  2414. memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
  2415. nfs_invalidate_atime(dir);
  2416. dprintk("%s: returns %d\n", __func__, status);
  2417. return status;
  2418. }
  2419. static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
  2420. u64 cookie, struct page *page, unsigned int count, int plus)
  2421. {
  2422. struct nfs4_exception exception = { };
  2423. int err;
  2424. do {
  2425. err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
  2426. _nfs4_proc_readdir(dentry, cred, cookie,
  2427. page, count, plus),
  2428. &exception);
  2429. } while (exception.retry);
  2430. return err;
  2431. }
  2432. static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
  2433. struct iattr *sattr, dev_t rdev)
  2434. {
  2435. struct nfs4_createdata *data;
  2436. int mode = sattr->ia_mode;
  2437. int status = -ENOMEM;
  2438. BUG_ON(!(sattr->ia_valid & ATTR_MODE));
  2439. BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
  2440. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
  2441. if (data == NULL)
  2442. goto out;
  2443. if (S_ISFIFO(mode))
  2444. data->arg.ftype = NF4FIFO;
  2445. else if (S_ISBLK(mode)) {
  2446. data->arg.ftype = NF4BLK;
  2447. data->arg.u.device.specdata1 = MAJOR(rdev);
  2448. data->arg.u.device.specdata2 = MINOR(rdev);
  2449. }
  2450. else if (S_ISCHR(mode)) {
  2451. data->arg.ftype = NF4CHR;
  2452. data->arg.u.device.specdata1 = MAJOR(rdev);
  2453. data->arg.u.device.specdata2 = MINOR(rdev);
  2454. }
  2455. status = nfs4_do_create(dir, dentry, data);
  2456. nfs4_free_createdata(data);
  2457. out:
  2458. return status;
  2459. }
  2460. static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
  2461. struct iattr *sattr, dev_t rdev)
  2462. {
  2463. struct nfs4_exception exception = { };
  2464. int err;
  2465. do {
  2466. err = nfs4_handle_exception(NFS_SERVER(dir),
  2467. _nfs4_proc_mknod(dir, dentry, sattr, rdev),
  2468. &exception);
  2469. } while (exception.retry);
  2470. return err;
  2471. }
  2472. static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
  2473. struct nfs_fsstat *fsstat)
  2474. {
  2475. struct nfs4_statfs_arg args = {
  2476. .fh = fhandle,
  2477. .bitmask = server->attr_bitmask,
  2478. };
  2479. struct nfs4_statfs_res res = {
  2480. .fsstat = fsstat,
  2481. };
  2482. struct rpc_message msg = {
  2483. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
  2484. .rpc_argp = &args,
  2485. .rpc_resp = &res,
  2486. };
  2487. nfs_fattr_init(fsstat->fattr);
  2488. return nfs4_call_sync(server, &msg, &args, &res, 0);
  2489. }
  2490. static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
  2491. {
  2492. struct nfs4_exception exception = { };
  2493. int err;
  2494. do {
  2495. err = nfs4_handle_exception(server,
  2496. _nfs4_proc_statfs(server, fhandle, fsstat),
  2497. &exception);
  2498. } while (exception.retry);
  2499. return err;
  2500. }
  2501. static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
  2502. struct nfs_fsinfo *fsinfo)
  2503. {
  2504. struct nfs4_fsinfo_arg args = {
  2505. .fh = fhandle,
  2506. .bitmask = server->attr_bitmask,
  2507. };
  2508. struct nfs4_fsinfo_res res = {
  2509. .fsinfo = fsinfo,
  2510. };
  2511. struct rpc_message msg = {
  2512. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
  2513. .rpc_argp = &args,
  2514. .rpc_resp = &res,
  2515. };
  2516. return nfs4_call_sync(server, &msg, &args, &res, 0);
  2517. }
  2518. static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
  2519. {
  2520. struct nfs4_exception exception = { };
  2521. int err;
  2522. do {
  2523. err = nfs4_handle_exception(server,
  2524. _nfs4_do_fsinfo(server, fhandle, fsinfo),
  2525. &exception);
  2526. } while (exception.retry);
  2527. return err;
  2528. }
  2529. static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
  2530. {
  2531. nfs_fattr_init(fsinfo->fattr);
  2532. return nfs4_do_fsinfo(server, fhandle, fsinfo);
  2533. }
  2534. static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
  2535. struct nfs_pathconf *pathconf)
  2536. {
  2537. struct nfs4_pathconf_arg args = {
  2538. .fh = fhandle,
  2539. .bitmask = server->attr_bitmask,
  2540. };
  2541. struct nfs4_pathconf_res res = {
  2542. .pathconf = pathconf,
  2543. };
  2544. struct rpc_message msg = {
  2545. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
  2546. .rpc_argp = &args,
  2547. .rpc_resp = &res,
  2548. };
  2549. /* None of the pathconf attributes are mandatory to implement */
  2550. if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
  2551. memset(pathconf, 0, sizeof(*pathconf));
  2552. return 0;
  2553. }
  2554. nfs_fattr_init(pathconf->fattr);
  2555. return nfs4_call_sync(server, &msg, &args, &res, 0);
  2556. }
  2557. static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
  2558. struct nfs_pathconf *pathconf)
  2559. {
  2560. struct nfs4_exception exception = { };
  2561. int err;
  2562. do {
  2563. err = nfs4_handle_exception(server,
  2564. _nfs4_proc_pathconf(server, fhandle, pathconf),
  2565. &exception);
  2566. } while (exception.retry);
  2567. return err;
  2568. }
  2569. static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
  2570. {
  2571. struct nfs_server *server = NFS_SERVER(data->inode);
  2572. if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
  2573. rpc_restart_call(task);
  2574. return -EAGAIN;
  2575. }
  2576. nfs_invalidate_atime(data->inode);
  2577. if (task->tk_status > 0)
  2578. renew_lease(server, data->timestamp);
  2579. return 0;
  2580. }
  2581. static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
  2582. {
  2583. data->timestamp = jiffies;
  2584. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
  2585. }
  2586. static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
  2587. {
  2588. struct inode *inode = data->inode;
  2589. if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
  2590. rpc_restart_call(task);
  2591. return -EAGAIN;
  2592. }
  2593. if (task->tk_status >= 0) {
  2594. renew_lease(NFS_SERVER(inode), data->timestamp);
  2595. nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
  2596. }
  2597. return 0;
  2598. }
  2599. static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
  2600. {
  2601. struct nfs_server *server = NFS_SERVER(data->inode);
  2602. data->args.bitmask = server->cache_consistency_bitmask;
  2603. data->res.server = server;
  2604. data->timestamp = jiffies;
  2605. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
  2606. }
  2607. static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
  2608. {
  2609. struct inode *inode = data->inode;
  2610. if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
  2611. rpc_restart_call(task);
  2612. return -EAGAIN;
  2613. }
  2614. nfs_refresh_inode(inode, data->res.fattr);
  2615. return 0;
  2616. }
  2617. static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
  2618. {
  2619. struct nfs_server *server = NFS_SERVER(data->inode);
  2620. data->args.bitmask = server->cache_consistency_bitmask;
  2621. data->res.server = server;
  2622. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
  2623. }
  2624. /*
  2625. * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
  2626. * standalone procedure for queueing an asynchronous RENEW.
  2627. */
  2628. static void nfs4_renew_done(struct rpc_task *task, void *data)
  2629. {
  2630. struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
  2631. unsigned long timestamp = (unsigned long)data;
  2632. if (task->tk_status < 0) {
  2633. /* Unless we're shutting down, schedule state recovery! */
  2634. if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
  2635. nfs4_schedule_state_recovery(clp);
  2636. return;
  2637. }
  2638. spin_lock(&clp->cl_lock);
  2639. if (time_before(clp->cl_last_renewal,timestamp))
  2640. clp->cl_last_renewal = timestamp;
  2641. spin_unlock(&clp->cl_lock);
  2642. }
  2643. static const struct rpc_call_ops nfs4_renew_ops = {
  2644. .rpc_call_done = nfs4_renew_done,
  2645. };
  2646. int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
  2647. {
  2648. struct rpc_message msg = {
  2649. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
  2650. .rpc_argp = clp,
  2651. .rpc_cred = cred,
  2652. };
  2653. return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
  2654. &nfs4_renew_ops, (void *)jiffies);
  2655. }
  2656. int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
  2657. {
  2658. struct rpc_message msg = {
  2659. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
  2660. .rpc_argp = clp,
  2661. .rpc_cred = cred,
  2662. };
  2663. unsigned long now = jiffies;
  2664. int status;
  2665. status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
  2666. if (status < 0)
  2667. return status;
  2668. spin_lock(&clp->cl_lock);
  2669. if (time_before(clp->cl_last_renewal,now))
  2670. clp->cl_last_renewal = now;
  2671. spin_unlock(&clp->cl_lock);
  2672. return 0;
  2673. }
  2674. static inline int nfs4_server_supports_acls(struct nfs_server *server)
  2675. {
  2676. return (server->caps & NFS_CAP_ACLS)
  2677. && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
  2678. && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
  2679. }
  2680. /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
  2681. * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
  2682. * the stack.
  2683. */
  2684. #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
  2685. static void buf_to_pages(const void *buf, size_t buflen,
  2686. struct page **pages, unsigned int *pgbase)
  2687. {
  2688. const void *p = buf;
  2689. *pgbase = offset_in_page(buf);
  2690. p -= *pgbase;
  2691. while (p < buf + buflen) {
  2692. *(pages++) = virt_to_page(p);
  2693. p += PAGE_CACHE_SIZE;
  2694. }
  2695. }
  2696. struct nfs4_cached_acl {
  2697. int cached;
  2698. size_t len;
  2699. char data[0];
  2700. };
  2701. static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
  2702. {
  2703. struct nfs_inode *nfsi = NFS_I(inode);
  2704. spin_lock(&inode->i_lock);
  2705. kfree(nfsi->nfs4_acl);
  2706. nfsi->nfs4_acl = acl;
  2707. spin_unlock(&inode->i_lock);
  2708. }
  2709. static void nfs4_zap_acl_attr(struct inode *inode)
  2710. {
  2711. nfs4_set_cached_acl(inode, NULL);
  2712. }
  2713. static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
  2714. {
  2715. struct nfs_inode *nfsi = NFS_I(inode);
  2716. struct nfs4_cached_acl *acl;
  2717. int ret = -ENOENT;
  2718. spin_lock(&inode->i_lock);
  2719. acl = nfsi->nfs4_acl;
  2720. if (acl == NULL)
  2721. goto out;
  2722. if (buf == NULL) /* user is just asking for length */
  2723. goto out_len;
  2724. if (acl->cached == 0)
  2725. goto out;
  2726. ret = -ERANGE; /* see getxattr(2) man page */
  2727. if (acl->len > buflen)
  2728. goto out;
  2729. memcpy(buf, acl->data, acl->len);
  2730. out_len:
  2731. ret = acl->len;
  2732. out:
  2733. spin_unlock(&inode->i_lock);
  2734. return ret;
  2735. }
  2736. static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
  2737. {
  2738. struct nfs4_cached_acl *acl;
  2739. if (buf && acl_len <= PAGE_SIZE) {
  2740. acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
  2741. if (acl == NULL)
  2742. goto out;
  2743. acl->cached = 1;
  2744. memcpy(acl->data, buf, acl_len);
  2745. } else {
  2746. acl = kmalloc(sizeof(*acl), GFP_KERNEL);
  2747. if (acl == NULL)
  2748. goto out;
  2749. acl->cached = 0;
  2750. }
  2751. acl->len = acl_len;
  2752. out:
  2753. nfs4_set_cached_acl(inode, acl);
  2754. }
  2755. static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
  2756. {
  2757. struct page *pages[NFS4ACL_MAXPAGES];
  2758. struct nfs_getaclargs args = {
  2759. .fh = NFS_FH(inode),
  2760. .acl_pages = pages,
  2761. .acl_len = buflen,
  2762. };
  2763. struct nfs_getaclres res = {
  2764. .acl_len = buflen,
  2765. };
  2766. void *resp_buf;
  2767. struct rpc_message msg = {
  2768. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
  2769. .rpc_argp = &args,
  2770. .rpc_resp = &res,
  2771. };
  2772. struct page *localpage = NULL;
  2773. int ret;
  2774. if (buflen < PAGE_SIZE) {
  2775. /* As long as we're doing a round trip to the server anyway,
  2776. * let's be prepared for a page of acl data. */
  2777. localpage = alloc_page(GFP_KERNEL);
  2778. resp_buf = page_address(localpage);
  2779. if (localpage == NULL)
  2780. return -ENOMEM;
  2781. args.acl_pages[0] = localpage;
  2782. args.acl_pgbase = 0;
  2783. args.acl_len = PAGE_SIZE;
  2784. } else {
  2785. resp_buf = buf;
  2786. buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
  2787. }
  2788. ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
  2789. if (ret)
  2790. goto out_free;
  2791. if (res.acl_len > args.acl_len)
  2792. nfs4_write_cached_acl(inode, NULL, res.acl_len);
  2793. else
  2794. nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
  2795. if (buf) {
  2796. ret = -ERANGE;
  2797. if (res.acl_len > buflen)
  2798. goto out_free;
  2799. if (localpage)
  2800. memcpy(buf, resp_buf, res.acl_len);
  2801. }
  2802. ret = res.acl_len;
  2803. out_free:
  2804. if (localpage)
  2805. __free_page(localpage);
  2806. return ret;
  2807. }
  2808. static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
  2809. {
  2810. struct nfs4_exception exception = { };
  2811. ssize_t ret;
  2812. do {
  2813. ret = __nfs4_get_acl_uncached(inode, buf, buflen);
  2814. if (ret >= 0)
  2815. break;
  2816. ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
  2817. } while (exception.retry);
  2818. return ret;
  2819. }
  2820. static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
  2821. {
  2822. struct nfs_server *server = NFS_SERVER(inode);
  2823. int ret;
  2824. if (!nfs4_server_supports_acls(server))
  2825. return -EOPNOTSUPP;
  2826. ret = nfs_revalidate_inode(server, inode);
  2827. if (ret < 0)
  2828. return ret;
  2829. if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
  2830. nfs_zap_acl_cache(inode);
  2831. ret = nfs4_read_cached_acl(inode, buf, buflen);
  2832. if (ret != -ENOENT)
  2833. return ret;
  2834. return nfs4_get_acl_uncached(inode, buf, buflen);
  2835. }
  2836. static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
  2837. {
  2838. struct nfs_server *server = NFS_SERVER(inode);
  2839. struct page *pages[NFS4ACL_MAXPAGES];
  2840. struct nfs_setaclargs arg = {
  2841. .fh = NFS_FH(inode),
  2842. .acl_pages = pages,
  2843. .acl_len = buflen,
  2844. };
  2845. struct nfs_setaclres res;
  2846. struct rpc_message msg = {
  2847. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
  2848. .rpc_argp = &arg,
  2849. .rpc_resp = &res,
  2850. };
  2851. int ret;
  2852. if (!nfs4_server_supports_acls(server))
  2853. return -EOPNOTSUPP;
  2854. nfs_inode_return_delegation(inode);
  2855. buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
  2856. ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
  2857. nfs_access_zap_cache(inode);
  2858. nfs_zap_acl_cache(inode);
  2859. return ret;
  2860. }
  2861. static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
  2862. {
  2863. struct nfs4_exception exception = { };
  2864. int err;
  2865. do {
  2866. err = nfs4_handle_exception(NFS_SERVER(inode),
  2867. __nfs4_proc_set_acl(inode, buf, buflen),
  2868. &exception);
  2869. } while (exception.retry);
  2870. return err;
  2871. }
  2872. static int
  2873. nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
  2874. {
  2875. struct nfs_client *clp = server->nfs_client;
  2876. if (!clp || task->tk_status >= 0)
  2877. return 0;
  2878. switch(task->tk_status) {
  2879. case -NFS4ERR_ADMIN_REVOKED:
  2880. case -NFS4ERR_BAD_STATEID:
  2881. case -NFS4ERR_OPENMODE:
  2882. if (state == NULL)
  2883. break;
  2884. nfs4_state_mark_reclaim_nograce(clp, state);
  2885. case -NFS4ERR_STALE_CLIENTID:
  2886. case -NFS4ERR_STALE_STATEID:
  2887. case -NFS4ERR_EXPIRED:
  2888. rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
  2889. nfs4_schedule_state_recovery(clp);
  2890. if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
  2891. rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
  2892. task->tk_status = 0;
  2893. return -EAGAIN;
  2894. case -NFS4ERR_DELAY:
  2895. nfs_inc_server_stats(server, NFSIOS_DELAY);
  2896. case -NFS4ERR_GRACE:
  2897. rpc_delay(task, NFS4_POLL_RETRY_MAX);
  2898. task->tk_status = 0;
  2899. return -EAGAIN;
  2900. case -NFS4ERR_OLD_STATEID:
  2901. task->tk_status = 0;
  2902. return -EAGAIN;
  2903. }
  2904. task->tk_status = nfs4_map_errors(task->tk_status);
  2905. return 0;
  2906. }
  2907. int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
  2908. {
  2909. nfs4_verifier sc_verifier;
  2910. struct nfs4_setclientid setclientid = {
  2911. .sc_verifier = &sc_verifier,
  2912. .sc_prog = program,
  2913. };
  2914. struct rpc_message msg = {
  2915. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
  2916. .rpc_argp = &setclientid,
  2917. .rpc_resp = clp,
  2918. .rpc_cred = cred,
  2919. };
  2920. __be32 *p;
  2921. int loop = 0;
  2922. int status;
  2923. p = (__be32*)sc_verifier.data;
  2924. *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
  2925. *p = htonl((u32)clp->cl_boot_time.tv_nsec);
  2926. for(;;) {
  2927. setclientid.sc_name_len = scnprintf(setclientid.sc_name,
  2928. sizeof(setclientid.sc_name), "%s/%s %s %s %u",
  2929. clp->cl_ipaddr,
  2930. rpc_peeraddr2str(clp->cl_rpcclient,
  2931. RPC_DISPLAY_ADDR),
  2932. rpc_peeraddr2str(clp->cl_rpcclient,
  2933. RPC_DISPLAY_PROTO),
  2934. clp->cl_rpcclient->cl_auth->au_ops->au_name,
  2935. clp->cl_id_uniquifier);
  2936. setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
  2937. sizeof(setclientid.sc_netid),
  2938. rpc_peeraddr2str(clp->cl_rpcclient,
  2939. RPC_DISPLAY_NETID));
  2940. setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
  2941. sizeof(setclientid.sc_uaddr), "%s.%u.%u",
  2942. clp->cl_ipaddr, port >> 8, port & 255);
  2943. status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
  2944. if (status != -NFS4ERR_CLID_INUSE)
  2945. break;
  2946. if (signalled())
  2947. break;
  2948. if (loop++ & 1)
  2949. ssleep(clp->cl_lease_time + 1);
  2950. else
  2951. if (++clp->cl_id_uniquifier == 0)
  2952. break;
  2953. }
  2954. return status;
  2955. }
  2956. static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
  2957. {
  2958. struct nfs_fsinfo fsinfo;
  2959. struct rpc_message msg = {
  2960. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
  2961. .rpc_argp = clp,
  2962. .rpc_resp = &fsinfo,
  2963. .rpc_cred = cred,
  2964. };
  2965. unsigned long now;
  2966. int status;
  2967. now = jiffies;
  2968. status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
  2969. if (status == 0) {
  2970. spin_lock(&clp->cl_lock);
  2971. clp->cl_lease_time = fsinfo.lease_time * HZ;
  2972. clp->cl_last_renewal = now;
  2973. spin_unlock(&clp->cl_lock);
  2974. }
  2975. return status;
  2976. }
  2977. int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
  2978. {
  2979. long timeout = 0;
  2980. int err;
  2981. do {
  2982. err = _nfs4_proc_setclientid_confirm(clp, cred);
  2983. switch (err) {
  2984. case 0:
  2985. return err;
  2986. case -NFS4ERR_RESOURCE:
  2987. /* The IBM lawyers misread another document! */
  2988. case -NFS4ERR_DELAY:
  2989. err = nfs4_delay(clp->cl_rpcclient, &timeout);
  2990. }
  2991. } while (err == 0);
  2992. return err;
  2993. }
  2994. struct nfs4_delegreturndata {
  2995. struct nfs4_delegreturnargs args;
  2996. struct nfs4_delegreturnres res;
  2997. struct nfs_fh fh;
  2998. nfs4_stateid stateid;
  2999. unsigned long timestamp;
  3000. struct nfs_fattr fattr;
  3001. int rpc_status;
  3002. };
  3003. static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
  3004. {
  3005. struct nfs4_delegreturndata *data = calldata;
  3006. data->rpc_status = task->tk_status;
  3007. if (data->rpc_status == 0)
  3008. renew_lease(data->res.server, data->timestamp);
  3009. }
  3010. static void nfs4_delegreturn_release(void *calldata)
  3011. {
  3012. kfree(calldata);
  3013. }
  3014. static const struct rpc_call_ops nfs4_delegreturn_ops = {
  3015. .rpc_call_done = nfs4_delegreturn_done,
  3016. .rpc_release = nfs4_delegreturn_release,
  3017. };
  3018. static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
  3019. {
  3020. struct nfs4_delegreturndata *data;
  3021. struct nfs_server *server = NFS_SERVER(inode);
  3022. struct rpc_task *task;
  3023. struct rpc_message msg = {
  3024. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
  3025. .rpc_cred = cred,
  3026. };
  3027. struct rpc_task_setup task_setup_data = {
  3028. .rpc_client = server->client,
  3029. .rpc_message = &msg,
  3030. .callback_ops = &nfs4_delegreturn_ops,
  3031. .flags = RPC_TASK_ASYNC,
  3032. };
  3033. int status = 0;
  3034. data = kmalloc(sizeof(*data), GFP_KERNEL);
  3035. if (data == NULL)
  3036. return -ENOMEM;
  3037. data->args.fhandle = &data->fh;
  3038. data->args.stateid = &data->stateid;
  3039. data->args.bitmask = server->attr_bitmask;
  3040. nfs_copy_fh(&data->fh, NFS_FH(inode));
  3041. memcpy(&data->stateid, stateid, sizeof(data->stateid));
  3042. data->res.fattr = &data->fattr;
  3043. data->res.server = server;
  3044. data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
  3045. nfs_fattr_init(data->res.fattr);
  3046. data->timestamp = jiffies;
  3047. data->rpc_status = 0;
  3048. task_setup_data.callback_data = data;
  3049. msg.rpc_argp = &data->args,
  3050. msg.rpc_resp = &data->res,
  3051. task = rpc_run_task(&task_setup_data);
  3052. if (IS_ERR(task))
  3053. return PTR_ERR(task);
  3054. if (!issync)
  3055. goto out;
  3056. status = nfs4_wait_for_completion_rpc_task(task);
  3057. if (status != 0)
  3058. goto out;
  3059. status = data->rpc_status;
  3060. if (status != 0)
  3061. goto out;
  3062. nfs_refresh_inode(inode, &data->fattr);
  3063. out:
  3064. rpc_put_task(task);
  3065. return status;
  3066. }
  3067. int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
  3068. {
  3069. struct nfs_server *server = NFS_SERVER(inode);
  3070. struct nfs4_exception exception = { };
  3071. int err;
  3072. do {
  3073. err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
  3074. switch (err) {
  3075. case -NFS4ERR_STALE_STATEID:
  3076. case -NFS4ERR_EXPIRED:
  3077. case 0:
  3078. return 0;
  3079. }
  3080. err = nfs4_handle_exception(server, err, &exception);
  3081. } while (exception.retry);
  3082. return err;
  3083. }
  3084. #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
  3085. #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
  3086. /*
  3087. * sleep, with exponential backoff, and retry the LOCK operation.
  3088. */
  3089. static unsigned long
  3090. nfs4_set_lock_task_retry(unsigned long timeout)
  3091. {
  3092. schedule_timeout_killable(timeout);
  3093. timeout <<= 1;
  3094. if (timeout > NFS4_LOCK_MAXTIMEOUT)
  3095. return NFS4_LOCK_MAXTIMEOUT;
  3096. return timeout;
  3097. }
  3098. static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  3099. {
  3100. struct inode *inode = state->inode;
  3101. struct nfs_server *server = NFS_SERVER(inode);
  3102. struct nfs_client *clp = server->nfs_client;
  3103. struct nfs_lockt_args arg = {
  3104. .fh = NFS_FH(inode),
  3105. .fl = request,
  3106. };
  3107. struct nfs_lockt_res res = {
  3108. .denied = request,
  3109. };
  3110. struct rpc_message msg = {
  3111. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
  3112. .rpc_argp = &arg,
  3113. .rpc_resp = &res,
  3114. .rpc_cred = state->owner->so_cred,
  3115. };
  3116. struct nfs4_lock_state *lsp;
  3117. int status;
  3118. arg.lock_owner.clientid = clp->cl_clientid;
  3119. status = nfs4_set_lock_state(state, request);
  3120. if (status != 0)
  3121. goto out;
  3122. lsp = request->fl_u.nfs4_fl.owner;
  3123. arg.lock_owner.id = lsp->ls_id.id;
  3124. status = nfs4_call_sync(server, &msg, &arg, &res, 1);
  3125. switch (status) {
  3126. case 0:
  3127. request->fl_type = F_UNLCK;
  3128. break;
  3129. case -NFS4ERR_DENIED:
  3130. status = 0;
  3131. }
  3132. request->fl_ops->fl_release_private(request);
  3133. out:
  3134. return status;
  3135. }
  3136. static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  3137. {
  3138. struct nfs4_exception exception = { };
  3139. int err;
  3140. do {
  3141. err = nfs4_handle_exception(NFS_SERVER(state->inode),
  3142. _nfs4_proc_getlk(state, cmd, request),
  3143. &exception);
  3144. } while (exception.retry);
  3145. return err;
  3146. }
  3147. static int do_vfs_lock(struct file *file, struct file_lock *fl)
  3148. {
  3149. int res = 0;
  3150. switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
  3151. case FL_POSIX:
  3152. res = posix_lock_file_wait(file, fl);
  3153. break;
  3154. case FL_FLOCK:
  3155. res = flock_lock_file_wait(file, fl);
  3156. break;
  3157. default:
  3158. BUG();
  3159. }
  3160. return res;
  3161. }
  3162. struct nfs4_unlockdata {
  3163. struct nfs_locku_args arg;
  3164. struct nfs_locku_res res;
  3165. struct nfs4_lock_state *lsp;
  3166. struct nfs_open_context *ctx;
  3167. struct file_lock fl;
  3168. const struct nfs_server *server;
  3169. unsigned long timestamp;
  3170. };
  3171. static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
  3172. struct nfs_open_context *ctx,
  3173. struct nfs4_lock_state *lsp,
  3174. struct nfs_seqid *seqid)
  3175. {
  3176. struct nfs4_unlockdata *p;
  3177. struct inode *inode = lsp->ls_state->inode;
  3178. p = kmalloc(sizeof(*p), GFP_KERNEL);
  3179. if (p == NULL)
  3180. return NULL;
  3181. p->arg.fh = NFS_FH(inode);
  3182. p->arg.fl = &p->fl;
  3183. p->arg.seqid = seqid;
  3184. p->res.seqid = seqid;
  3185. p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
  3186. p->arg.stateid = &lsp->ls_stateid;
  3187. p->lsp = lsp;
  3188. atomic_inc(&lsp->ls_count);
  3189. /* Ensure we don't close file until we're done freeing locks! */
  3190. p->ctx = get_nfs_open_context(ctx);
  3191. memcpy(&p->fl, fl, sizeof(p->fl));
  3192. p->server = NFS_SERVER(inode);
  3193. return p;
  3194. }
  3195. static void nfs4_locku_release_calldata(void *data)
  3196. {
  3197. struct nfs4_unlockdata *calldata = data;
  3198. nfs_free_seqid(calldata->arg.seqid);
  3199. nfs4_put_lock_state(calldata->lsp);
  3200. put_nfs_open_context(calldata->ctx);
  3201. kfree(calldata);
  3202. }
  3203. static void nfs4_locku_done(struct rpc_task *task, void *data)
  3204. {
  3205. struct nfs4_unlockdata *calldata = data;
  3206. if (RPC_ASSASSINATED(task))
  3207. return;
  3208. switch (task->tk_status) {
  3209. case 0:
  3210. memcpy(calldata->lsp->ls_stateid.data,
  3211. calldata->res.stateid.data,
  3212. sizeof(calldata->lsp->ls_stateid.data));
  3213. renew_lease(calldata->server, calldata->timestamp);
  3214. break;
  3215. case -NFS4ERR_BAD_STATEID:
  3216. case -NFS4ERR_OLD_STATEID:
  3217. case -NFS4ERR_STALE_STATEID:
  3218. case -NFS4ERR_EXPIRED:
  3219. break;
  3220. default:
  3221. if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
  3222. rpc_restart_call(task);
  3223. }
  3224. }
  3225. static void nfs4_locku_prepare(struct rpc_task *task, void *data)
  3226. {
  3227. struct nfs4_unlockdata *calldata = data;
  3228. if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
  3229. return;
  3230. if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
  3231. /* Note: exit _without_ running nfs4_locku_done */
  3232. task->tk_action = NULL;
  3233. return;
  3234. }
  3235. calldata->timestamp = jiffies;
  3236. rpc_call_start(task);
  3237. }
  3238. static const struct rpc_call_ops nfs4_locku_ops = {
  3239. .rpc_call_prepare = nfs4_locku_prepare,
  3240. .rpc_call_done = nfs4_locku_done,
  3241. .rpc_release = nfs4_locku_release_calldata,
  3242. };
  3243. static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
  3244. struct nfs_open_context *ctx,
  3245. struct nfs4_lock_state *lsp,
  3246. struct nfs_seqid *seqid)
  3247. {
  3248. struct nfs4_unlockdata *data;
  3249. struct rpc_message msg = {
  3250. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
  3251. .rpc_cred = ctx->cred,
  3252. };
  3253. struct rpc_task_setup task_setup_data = {
  3254. .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
  3255. .rpc_message = &msg,
  3256. .callback_ops = &nfs4_locku_ops,
  3257. .workqueue = nfsiod_workqueue,
  3258. .flags = RPC_TASK_ASYNC,
  3259. };
  3260. /* Ensure this is an unlock - when canceling a lock, the
  3261. * canceled lock is passed in, and it won't be an unlock.
  3262. */
  3263. fl->fl_type = F_UNLCK;
  3264. data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
  3265. if (data == NULL) {
  3266. nfs_free_seqid(seqid);
  3267. return ERR_PTR(-ENOMEM);
  3268. }
  3269. msg.rpc_argp = &data->arg,
  3270. msg.rpc_resp = &data->res,
  3271. task_setup_data.callback_data = data;
  3272. return rpc_run_task(&task_setup_data);
  3273. }
  3274. static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
  3275. {
  3276. struct nfs_inode *nfsi = NFS_I(state->inode);
  3277. struct nfs_seqid *seqid;
  3278. struct nfs4_lock_state *lsp;
  3279. struct rpc_task *task;
  3280. int status = 0;
  3281. unsigned char fl_flags = request->fl_flags;
  3282. status = nfs4_set_lock_state(state, request);
  3283. /* Unlock _before_ we do the RPC call */
  3284. request->fl_flags |= FL_EXISTS;
  3285. down_read(&nfsi->rwsem);
  3286. if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
  3287. up_read(&nfsi->rwsem);
  3288. goto out;
  3289. }
  3290. up_read(&nfsi->rwsem);
  3291. if (status != 0)
  3292. goto out;
  3293. /* Is this a delegated lock? */
  3294. if (test_bit(NFS_DELEGATED_STATE, &state->flags))
  3295. goto out;
  3296. lsp = request->fl_u.nfs4_fl.owner;
  3297. seqid = nfs_alloc_seqid(&lsp->ls_seqid);
  3298. status = -ENOMEM;
  3299. if (seqid == NULL)
  3300. goto out;
  3301. task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
  3302. status = PTR_ERR(task);
  3303. if (IS_ERR(task))
  3304. goto out;
  3305. status = nfs4_wait_for_completion_rpc_task(task);
  3306. rpc_put_task(task);
  3307. out:
  3308. request->fl_flags = fl_flags;
  3309. return status;
  3310. }
  3311. struct nfs4_lockdata {
  3312. struct nfs_lock_args arg;
  3313. struct nfs_lock_res res;
  3314. struct nfs4_lock_state *lsp;
  3315. struct nfs_open_context *ctx;
  3316. struct file_lock fl;
  3317. unsigned long timestamp;
  3318. int rpc_status;
  3319. int cancelled;
  3320. };
  3321. static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
  3322. struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
  3323. {
  3324. struct nfs4_lockdata *p;
  3325. struct inode *inode = lsp->ls_state->inode;
  3326. struct nfs_server *server = NFS_SERVER(inode);
  3327. p = kzalloc(sizeof(*p), GFP_KERNEL);
  3328. if (p == NULL)
  3329. return NULL;
  3330. p->arg.fh = NFS_FH(inode);
  3331. p->arg.fl = &p->fl;
  3332. p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
  3333. if (p->arg.open_seqid == NULL)
  3334. goto out_free;
  3335. p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
  3336. if (p->arg.lock_seqid == NULL)
  3337. goto out_free_seqid;
  3338. p->arg.lock_stateid = &lsp->ls_stateid;
  3339. p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
  3340. p->arg.lock_owner.id = lsp->ls_id.id;
  3341. p->res.lock_seqid = p->arg.lock_seqid;
  3342. p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
  3343. p->lsp = lsp;
  3344. atomic_inc(&lsp->ls_count);
  3345. p->ctx = get_nfs_open_context(ctx);
  3346. memcpy(&p->fl, fl, sizeof(p->fl));
  3347. return p;
  3348. out_free_seqid:
  3349. nfs_free_seqid(p->arg.open_seqid);
  3350. out_free:
  3351. kfree(p);
  3352. return NULL;
  3353. }
  3354. static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
  3355. {
  3356. struct nfs4_lockdata *data = calldata;
  3357. struct nfs4_state *state = data->lsp->ls_state;
  3358. dprintk("%s: begin!\n", __func__);
  3359. if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
  3360. return;
  3361. /* Do we need to do an open_to_lock_owner? */
  3362. if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
  3363. if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
  3364. return;
  3365. data->arg.open_stateid = &state->stateid;
  3366. data->arg.new_lock_owner = 1;
  3367. data->res.open_seqid = data->arg.open_seqid;
  3368. } else
  3369. data->arg.new_lock_owner = 0;
  3370. data->timestamp = jiffies;
  3371. rpc_call_start(task);
  3372. dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
  3373. }
  3374. static void nfs4_lock_done(struct rpc_task *task, void *calldata)
  3375. {
  3376. struct nfs4_lockdata *data = calldata;
  3377. dprintk("%s: begin!\n", __func__);
  3378. data->rpc_status = task->tk_status;
  3379. if (RPC_ASSASSINATED(task))
  3380. goto out;
  3381. if (data->arg.new_lock_owner != 0) {
  3382. if (data->rpc_status == 0)
  3383. nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
  3384. else
  3385. goto out;
  3386. }
  3387. if (data->rpc_status == 0) {
  3388. memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
  3389. sizeof(data->lsp->ls_stateid.data));
  3390. data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
  3391. renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
  3392. }
  3393. out:
  3394. dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
  3395. }
  3396. static void nfs4_lock_release(void *calldata)
  3397. {
  3398. struct nfs4_lockdata *data = calldata;
  3399. dprintk("%s: begin!\n", __func__);
  3400. nfs_free_seqid(data->arg.open_seqid);
  3401. if (data->cancelled != 0) {
  3402. struct rpc_task *task;
  3403. task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
  3404. data->arg.lock_seqid);
  3405. if (!IS_ERR(task))
  3406. rpc_put_task(task);
  3407. dprintk("%s: cancelling lock!\n", __func__);
  3408. } else
  3409. nfs_free_seqid(data->arg.lock_seqid);
  3410. nfs4_put_lock_state(data->lsp);
  3411. put_nfs_open_context(data->ctx);
  3412. kfree(data);
  3413. dprintk("%s: done!\n", __func__);
  3414. }
  3415. static const struct rpc_call_ops nfs4_lock_ops = {
  3416. .rpc_call_prepare = nfs4_lock_prepare,
  3417. .rpc_call_done = nfs4_lock_done,
  3418. .rpc_release = nfs4_lock_release,
  3419. };
  3420. static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
  3421. {
  3422. struct nfs4_lockdata *data;
  3423. struct rpc_task *task;
  3424. struct rpc_message msg = {
  3425. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
  3426. .rpc_cred = state->owner->so_cred,
  3427. };
  3428. struct rpc_task_setup task_setup_data = {
  3429. .rpc_client = NFS_CLIENT(state->inode),
  3430. .rpc_message = &msg,
  3431. .callback_ops = &nfs4_lock_ops,
  3432. .workqueue = nfsiod_workqueue,
  3433. .flags = RPC_TASK_ASYNC,
  3434. };
  3435. int ret;
  3436. dprintk("%s: begin!\n", __func__);
  3437. data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
  3438. fl->fl_u.nfs4_fl.owner);
  3439. if (data == NULL)
  3440. return -ENOMEM;
  3441. if (IS_SETLKW(cmd))
  3442. data->arg.block = 1;
  3443. if (reclaim != 0)
  3444. data->arg.reclaim = 1;
  3445. msg.rpc_argp = &data->arg,
  3446. msg.rpc_resp = &data->res,
  3447. task_setup_data.callback_data = data;
  3448. task = rpc_run_task(&task_setup_data);
  3449. if (IS_ERR(task))
  3450. return PTR_ERR(task);
  3451. ret = nfs4_wait_for_completion_rpc_task(task);
  3452. if (ret == 0) {
  3453. ret = data->rpc_status;
  3454. if (ret == -NFS4ERR_DENIED)
  3455. ret = -EAGAIN;
  3456. } else
  3457. data->cancelled = 1;
  3458. rpc_put_task(task);
  3459. dprintk("%s: done, ret = %d!\n", __func__, ret);
  3460. return ret;
  3461. }
  3462. static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
  3463. {
  3464. struct nfs_server *server = NFS_SERVER(state->inode);
  3465. struct nfs4_exception exception = { };
  3466. int err;
  3467. do {
  3468. /* Cache the lock if possible... */
  3469. if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
  3470. return 0;
  3471. err = _nfs4_do_setlk(state, F_SETLK, request, 1);
  3472. if (err != -NFS4ERR_DELAY)
  3473. break;
  3474. nfs4_handle_exception(server, err, &exception);
  3475. } while (exception.retry);
  3476. return err;
  3477. }
  3478. static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
  3479. {
  3480. struct nfs_server *server = NFS_SERVER(state->inode);
  3481. struct nfs4_exception exception = { };
  3482. int err;
  3483. err = nfs4_set_lock_state(state, request);
  3484. if (err != 0)
  3485. return err;
  3486. do {
  3487. if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
  3488. return 0;
  3489. err = _nfs4_do_setlk(state, F_SETLK, request, 0);
  3490. if (err != -NFS4ERR_DELAY)
  3491. break;
  3492. nfs4_handle_exception(server, err, &exception);
  3493. } while (exception.retry);
  3494. return err;
  3495. }
  3496. static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  3497. {
  3498. struct nfs_inode *nfsi = NFS_I(state->inode);
  3499. unsigned char fl_flags = request->fl_flags;
  3500. int status;
  3501. /* Is this a delegated open? */
  3502. status = nfs4_set_lock_state(state, request);
  3503. if (status != 0)
  3504. goto out;
  3505. request->fl_flags |= FL_ACCESS;
  3506. status = do_vfs_lock(request->fl_file, request);
  3507. if (status < 0)
  3508. goto out;
  3509. down_read(&nfsi->rwsem);
  3510. if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
  3511. /* Yes: cache locks! */
  3512. /* ...but avoid races with delegation recall... */
  3513. request->fl_flags = fl_flags & ~FL_SLEEP;
  3514. status = do_vfs_lock(request->fl_file, request);
  3515. goto out_unlock;
  3516. }
  3517. status = _nfs4_do_setlk(state, cmd, request, 0);
  3518. if (status != 0)
  3519. goto out_unlock;
  3520. /* Note: we always want to sleep here! */
  3521. request->fl_flags = fl_flags | FL_SLEEP;
  3522. if (do_vfs_lock(request->fl_file, request) < 0)
  3523. printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
  3524. out_unlock:
  3525. up_read(&nfsi->rwsem);
  3526. out:
  3527. request->fl_flags = fl_flags;
  3528. return status;
  3529. }
  3530. static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  3531. {
  3532. struct nfs4_exception exception = { };
  3533. int err;
  3534. do {
  3535. err = nfs4_handle_exception(NFS_SERVER(state->inode),
  3536. _nfs4_proc_setlk(state, cmd, request),
  3537. &exception);
  3538. } while (exception.retry);
  3539. return err;
  3540. }
  3541. static int
  3542. nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
  3543. {
  3544. struct nfs_open_context *ctx;
  3545. struct nfs4_state *state;
  3546. unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
  3547. int status;
  3548. /* verify open state */
  3549. ctx = nfs_file_open_context(filp);
  3550. state = ctx->state;
  3551. if (request->fl_start < 0 || request->fl_end < 0)
  3552. return -EINVAL;
  3553. if (IS_GETLK(cmd))
  3554. return nfs4_proc_getlk(state, F_GETLK, request);
  3555. if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
  3556. return -EINVAL;
  3557. if (request->fl_type == F_UNLCK)
  3558. return nfs4_proc_unlck(state, cmd, request);
  3559. do {
  3560. status = nfs4_proc_setlk(state, cmd, request);
  3561. if ((status != -EAGAIN) || IS_SETLK(cmd))
  3562. break;
  3563. timeout = nfs4_set_lock_task_retry(timeout);
  3564. status = -ERESTARTSYS;
  3565. if (signalled())
  3566. break;
  3567. } while(status < 0);
  3568. return status;
  3569. }
  3570. int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
  3571. {
  3572. struct nfs_server *server = NFS_SERVER(state->inode);
  3573. struct nfs4_exception exception = { };
  3574. int err;
  3575. err = nfs4_set_lock_state(state, fl);
  3576. if (err != 0)
  3577. goto out;
  3578. do {
  3579. err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
  3580. if (err != -NFS4ERR_DELAY)
  3581. break;
  3582. err = nfs4_handle_exception(server, err, &exception);
  3583. } while (exception.retry);
  3584. out:
  3585. return err;
  3586. }
  3587. #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
  3588. int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
  3589. size_t buflen, int flags)
  3590. {
  3591. struct inode *inode = dentry->d_inode;
  3592. if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
  3593. return -EOPNOTSUPP;
  3594. return nfs4_proc_set_acl(inode, buf, buflen);
  3595. }
  3596. /* The getxattr man page suggests returning -ENODATA for unknown attributes,
  3597. * and that's what we'll do for e.g. user attributes that haven't been set.
  3598. * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
  3599. * attributes in kernel-managed attribute namespaces. */
  3600. ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
  3601. size_t buflen)
  3602. {
  3603. struct inode *inode = dentry->d_inode;
  3604. if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
  3605. return -EOPNOTSUPP;
  3606. return nfs4_proc_get_acl(inode, buf, buflen);
  3607. }
  3608. ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
  3609. {
  3610. size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
  3611. if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
  3612. return 0;
  3613. if (buf && buflen < len)
  3614. return -ERANGE;
  3615. if (buf)
  3616. memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
  3617. return len;
  3618. }
  3619. static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
  3620. {
  3621. if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
  3622. (fattr->valid & NFS_ATTR_FATTR_FSID) &&
  3623. (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
  3624. return;
  3625. fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
  3626. NFS_ATTR_FATTR_NLINK;
  3627. fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
  3628. fattr->nlink = 2;
  3629. }
  3630. int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
  3631. struct nfs4_fs_locations *fs_locations, struct page *page)
  3632. {
  3633. struct nfs_server *server = NFS_SERVER(dir);
  3634. u32 bitmask[2] = {
  3635. [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
  3636. [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
  3637. };
  3638. struct nfs4_fs_locations_arg args = {
  3639. .dir_fh = NFS_FH(dir),
  3640. .name = name,
  3641. .page = page,
  3642. .bitmask = bitmask,
  3643. };
  3644. struct nfs4_fs_locations_res res = {
  3645. .fs_locations = fs_locations,
  3646. };
  3647. struct rpc_message msg = {
  3648. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
  3649. .rpc_argp = &args,
  3650. .rpc_resp = &res,
  3651. };
  3652. int status;
  3653. dprintk("%s: start\n", __func__);
  3654. nfs_fattr_init(&fs_locations->fattr);
  3655. fs_locations->server = server;
  3656. fs_locations->nlocations = 0;
  3657. status = nfs4_call_sync(server, &msg, &args, &res, 0);
  3658. nfs_fixup_referral_attributes(&fs_locations->fattr);
  3659. dprintk("%s: returned status = %d\n", __func__, status);
  3660. return status;
  3661. }
  3662. #ifdef CONFIG_NFS_V4_1
  3663. /* Destroy the slot table */
  3664. static void nfs4_destroy_slot_table(struct nfs4_session *session)
  3665. {
  3666. if (session->fc_slot_table.slots == NULL)
  3667. return;
  3668. kfree(session->fc_slot_table.slots);
  3669. session->fc_slot_table.slots = NULL;
  3670. return;
  3671. }
  3672. struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
  3673. {
  3674. struct nfs4_session *session;
  3675. struct nfs4_slot_table *tbl;
  3676. session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
  3677. if (!session)
  3678. return NULL;
  3679. tbl = &session->fc_slot_table;
  3680. spin_lock_init(&tbl->slot_tbl_lock);
  3681. rpc_init_wait_queue(&tbl->slot_tbl_waitq, "Slot table");
  3682. session->clp = clp;
  3683. return session;
  3684. }
  3685. void nfs4_destroy_session(struct nfs4_session *session)
  3686. {
  3687. nfs4_destroy_slot_table(session);
  3688. kfree(session);
  3689. }
  3690. #endif /* CONFIG_NFS_V4_1 */
  3691. struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
  3692. .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
  3693. .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
  3694. .recover_open = nfs4_open_reclaim,
  3695. .recover_lock = nfs4_lock_reclaim,
  3696. };
  3697. struct nfs4_state_recovery_ops nfs4_nograce_recovery_ops = {
  3698. .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
  3699. .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
  3700. .recover_open = nfs4_open_expired,
  3701. .recover_lock = nfs4_lock_expired,
  3702. };
  3703. static const struct inode_operations nfs4_file_inode_operations = {
  3704. .permission = nfs_permission,
  3705. .getattr = nfs_getattr,
  3706. .setattr = nfs_setattr,
  3707. .getxattr = nfs4_getxattr,
  3708. .setxattr = nfs4_setxattr,
  3709. .listxattr = nfs4_listxattr,
  3710. };
  3711. const struct nfs_rpc_ops nfs_v4_clientops = {
  3712. .version = 4, /* protocol version */
  3713. .dentry_ops = &nfs4_dentry_operations,
  3714. .dir_inode_ops = &nfs4_dir_inode_operations,
  3715. .file_inode_ops = &nfs4_file_inode_operations,
  3716. .getroot = nfs4_proc_get_root,
  3717. .getattr = nfs4_proc_getattr,
  3718. .setattr = nfs4_proc_setattr,
  3719. .lookupfh = nfs4_proc_lookupfh,
  3720. .lookup = nfs4_proc_lookup,
  3721. .access = nfs4_proc_access,
  3722. .readlink = nfs4_proc_readlink,
  3723. .create = nfs4_proc_create,
  3724. .remove = nfs4_proc_remove,
  3725. .unlink_setup = nfs4_proc_unlink_setup,
  3726. .unlink_done = nfs4_proc_unlink_done,
  3727. .rename = nfs4_proc_rename,
  3728. .link = nfs4_proc_link,
  3729. .symlink = nfs4_proc_symlink,
  3730. .mkdir = nfs4_proc_mkdir,
  3731. .rmdir = nfs4_proc_remove,
  3732. .readdir = nfs4_proc_readdir,
  3733. .mknod = nfs4_proc_mknod,
  3734. .statfs = nfs4_proc_statfs,
  3735. .fsinfo = nfs4_proc_fsinfo,
  3736. .pathconf = nfs4_proc_pathconf,
  3737. .set_capabilities = nfs4_server_capabilities,
  3738. .decode_dirent = nfs4_decode_dirent,
  3739. .read_setup = nfs4_proc_read_setup,
  3740. .read_done = nfs4_read_done,
  3741. .write_setup = nfs4_proc_write_setup,
  3742. .write_done = nfs4_write_done,
  3743. .commit_setup = nfs4_proc_commit_setup,
  3744. .commit_done = nfs4_commit_done,
  3745. .lock = nfs4_proc_lock,
  3746. .clear_acl_cache = nfs4_zap_acl_attr,
  3747. .close_context = nfs4_close_context,
  3748. };
  3749. /*
  3750. * Local variables:
  3751. * c-basic-offset: 8
  3752. * End:
  3753. */