nfs4proc.c 74 KB

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