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