nfs4proc.c 139 KB

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