nfs4proc.c 86 KB

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