nfs4proc.c 96 KB

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