nfs4proc.c 86 KB

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