nfs4proc.c 99 KB

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