nfs4proc.c 112 KB

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