nfs4proc.c 102 KB

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