nfs4proc.c 100 KB

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