nfs4proc.c 82 KB

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