nfs4proc.c 88 KB

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