nfs4proc.c 82 KB

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