nfs4proc.c 99 KB

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