nfs4proc.c 128 KB

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