nfs4proc.c 89 KB

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