nfs4proc.c 103 KB

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