nfs4proc.c 92 KB

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