nfs4proc.c 136 KB

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