nfs4proc.c 100 KB

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