nfs4proc.c 80 KB

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