nfs4proc.c 101 KB

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