nfs4proc.c 101 KB

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