nfs4proc.c 252 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409
  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/ratelimit.h>
  42. #include <linux/printk.h>
  43. #include <linux/slab.h>
  44. #include <linux/sunrpc/clnt.h>
  45. #include <linux/nfs.h>
  46. #include <linux/nfs4.h>
  47. #include <linux/nfs_fs.h>
  48. #include <linux/nfs_page.h>
  49. #include <linux/nfs_mount.h>
  50. #include <linux/namei.h>
  51. #include <linux/mount.h>
  52. #include <linux/module.h>
  53. #include <linux/xattr.h>
  54. #include <linux/utsname.h>
  55. #include <linux/freezer.h>
  56. #include "nfs4_fs.h"
  57. #include "delegation.h"
  58. #include "internal.h"
  59. #include "iostat.h"
  60. #include "callback.h"
  61. #include "pnfs.h"
  62. #include "netns.h"
  63. #include "nfs4idmap.h"
  64. #include "nfs4session.h"
  65. #include "fscache.h"
  66. #include "nfs4trace.h"
  67. #define NFSDBG_FACILITY NFSDBG_PROC
  68. #define NFS4_POLL_RETRY_MIN (HZ/10)
  69. #define NFS4_POLL_RETRY_MAX (15*HZ)
  70. /* file attributes which can be mapped to nfs attributes */
  71. #define NFS4_VALID_ATTRS (ATTR_MODE \
  72. | ATTR_UID \
  73. | ATTR_GID \
  74. | ATTR_SIZE \
  75. | ATTR_ATIME \
  76. | ATTR_MTIME \
  77. | ATTR_CTIME \
  78. | ATTR_ATIME_SET \
  79. | ATTR_MTIME_SET)
  80. struct nfs4_opendata;
  81. static int _nfs4_proc_open(struct nfs4_opendata *data);
  82. static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
  83. static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
  84. static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
  85. static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
  86. static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
  87. static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
  88. struct nfs_fattr *fattr, struct iattr *sattr,
  89. struct nfs4_state *state, struct nfs4_label *ilabel,
  90. struct nfs4_label *olabel);
  91. #ifdef CONFIG_NFS_V4_1
  92. static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
  93. struct rpc_cred *);
  94. static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
  95. struct rpc_cred *, bool);
  96. #endif
  97. #ifdef CONFIG_NFS_V4_SECURITY_LABEL
  98. static inline struct nfs4_label *
  99. nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
  100. struct iattr *sattr, struct nfs4_label *label)
  101. {
  102. int err;
  103. if (label == NULL)
  104. return NULL;
  105. if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
  106. return NULL;
  107. err = security_dentry_init_security(dentry, sattr->ia_mode,
  108. &dentry->d_name, (void **)&label->label, &label->len);
  109. if (err == 0)
  110. return label;
  111. return NULL;
  112. }
  113. static inline void
  114. nfs4_label_release_security(struct nfs4_label *label)
  115. {
  116. if (label)
  117. security_release_secctx(label->label, label->len);
  118. }
  119. static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
  120. {
  121. if (label)
  122. return server->attr_bitmask;
  123. return server->attr_bitmask_nl;
  124. }
  125. #else
  126. static inline struct nfs4_label *
  127. nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
  128. struct iattr *sattr, struct nfs4_label *l)
  129. { return NULL; }
  130. static inline void
  131. nfs4_label_release_security(struct nfs4_label *label)
  132. { return; }
  133. static inline u32 *
  134. nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
  135. { return server->attr_bitmask; }
  136. #endif
  137. /* Prevent leaks of NFSv4 errors into userland */
  138. static int nfs4_map_errors(int err)
  139. {
  140. if (err >= -1000)
  141. return err;
  142. switch (err) {
  143. case -NFS4ERR_RESOURCE:
  144. case -NFS4ERR_LAYOUTTRYLATER:
  145. case -NFS4ERR_RECALLCONFLICT:
  146. return -EREMOTEIO;
  147. case -NFS4ERR_WRONGSEC:
  148. case -NFS4ERR_WRONG_CRED:
  149. return -EPERM;
  150. case -NFS4ERR_BADOWNER:
  151. case -NFS4ERR_BADNAME:
  152. return -EINVAL;
  153. case -NFS4ERR_SHARE_DENIED:
  154. return -EACCES;
  155. case -NFS4ERR_MINOR_VERS_MISMATCH:
  156. return -EPROTONOSUPPORT;
  157. case -NFS4ERR_FILE_OPEN:
  158. return -EBUSY;
  159. default:
  160. dprintk("%s could not handle NFSv4 error %d\n",
  161. __func__, -err);
  162. break;
  163. }
  164. return -EIO;
  165. }
  166. /*
  167. * This is our standard bitmap for GETATTR requests.
  168. */
  169. const u32 nfs4_fattr_bitmap[3] = {
  170. FATTR4_WORD0_TYPE
  171. | FATTR4_WORD0_CHANGE
  172. | FATTR4_WORD0_SIZE
  173. | FATTR4_WORD0_FSID
  174. | FATTR4_WORD0_FILEID,
  175. FATTR4_WORD1_MODE
  176. | FATTR4_WORD1_NUMLINKS
  177. | FATTR4_WORD1_OWNER
  178. | FATTR4_WORD1_OWNER_GROUP
  179. | FATTR4_WORD1_RAWDEV
  180. | FATTR4_WORD1_SPACE_USED
  181. | FATTR4_WORD1_TIME_ACCESS
  182. | FATTR4_WORD1_TIME_METADATA
  183. | FATTR4_WORD1_TIME_MODIFY
  184. | FATTR4_WORD1_MOUNTED_ON_FILEID,
  185. #ifdef CONFIG_NFS_V4_SECURITY_LABEL
  186. FATTR4_WORD2_SECURITY_LABEL
  187. #endif
  188. };
  189. static const u32 nfs4_pnfs_open_bitmap[3] = {
  190. FATTR4_WORD0_TYPE
  191. | FATTR4_WORD0_CHANGE
  192. | FATTR4_WORD0_SIZE
  193. | FATTR4_WORD0_FSID
  194. | FATTR4_WORD0_FILEID,
  195. FATTR4_WORD1_MODE
  196. | FATTR4_WORD1_NUMLINKS
  197. | FATTR4_WORD1_OWNER
  198. | FATTR4_WORD1_OWNER_GROUP
  199. | FATTR4_WORD1_RAWDEV
  200. | FATTR4_WORD1_SPACE_USED
  201. | FATTR4_WORD1_TIME_ACCESS
  202. | FATTR4_WORD1_TIME_METADATA
  203. | FATTR4_WORD1_TIME_MODIFY,
  204. FATTR4_WORD2_MDSTHRESHOLD
  205. #ifdef CONFIG_NFS_V4_SECURITY_LABEL
  206. | FATTR4_WORD2_SECURITY_LABEL
  207. #endif
  208. };
  209. static const u32 nfs4_open_noattr_bitmap[3] = {
  210. FATTR4_WORD0_TYPE
  211. | FATTR4_WORD0_CHANGE
  212. | FATTR4_WORD0_FILEID,
  213. };
  214. const u32 nfs4_statfs_bitmap[3] = {
  215. FATTR4_WORD0_FILES_AVAIL
  216. | FATTR4_WORD0_FILES_FREE
  217. | FATTR4_WORD0_FILES_TOTAL,
  218. FATTR4_WORD1_SPACE_AVAIL
  219. | FATTR4_WORD1_SPACE_FREE
  220. | FATTR4_WORD1_SPACE_TOTAL
  221. };
  222. const u32 nfs4_pathconf_bitmap[3] = {
  223. FATTR4_WORD0_MAXLINK
  224. | FATTR4_WORD0_MAXNAME,
  225. 0
  226. };
  227. const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
  228. | FATTR4_WORD0_MAXREAD
  229. | FATTR4_WORD0_MAXWRITE
  230. | FATTR4_WORD0_LEASE_TIME,
  231. FATTR4_WORD1_TIME_DELTA
  232. | FATTR4_WORD1_FS_LAYOUT_TYPES,
  233. FATTR4_WORD2_LAYOUT_BLKSIZE
  234. | FATTR4_WORD2_CLONE_BLKSIZE
  235. };
  236. const u32 nfs4_fs_locations_bitmap[3] = {
  237. FATTR4_WORD0_CHANGE
  238. | FATTR4_WORD0_SIZE
  239. | FATTR4_WORD0_FSID
  240. | FATTR4_WORD0_FILEID
  241. | FATTR4_WORD0_FS_LOCATIONS,
  242. FATTR4_WORD1_OWNER
  243. | FATTR4_WORD1_OWNER_GROUP
  244. | FATTR4_WORD1_RAWDEV
  245. | FATTR4_WORD1_SPACE_USED
  246. | FATTR4_WORD1_TIME_ACCESS
  247. | FATTR4_WORD1_TIME_METADATA
  248. | FATTR4_WORD1_TIME_MODIFY
  249. | FATTR4_WORD1_MOUNTED_ON_FILEID,
  250. };
  251. static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
  252. struct nfs4_readdir_arg *readdir)
  253. {
  254. __be32 *start, *p;
  255. if (cookie > 2) {
  256. readdir->cookie = cookie;
  257. memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
  258. return;
  259. }
  260. readdir->cookie = 0;
  261. memset(&readdir->verifier, 0, sizeof(readdir->verifier));
  262. if (cookie == 2)
  263. return;
  264. /*
  265. * NFSv4 servers do not return entries for '.' and '..'
  266. * Therefore, we fake these entries here. We let '.'
  267. * have cookie 0 and '..' have cookie 1. Note that
  268. * when talking to the server, we always send cookie 0
  269. * instead of 1 or 2.
  270. */
  271. start = p = kmap_atomic(*readdir->pages);
  272. if (cookie == 0) {
  273. *p++ = xdr_one; /* next */
  274. *p++ = xdr_zero; /* cookie, first word */
  275. *p++ = xdr_one; /* cookie, second word */
  276. *p++ = xdr_one; /* entry len */
  277. memcpy(p, ".\0\0\0", 4); /* entry */
  278. p++;
  279. *p++ = xdr_one; /* bitmap length */
  280. *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
  281. *p++ = htonl(8); /* attribute buffer length */
  282. p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
  283. }
  284. *p++ = xdr_one; /* next */
  285. *p++ = xdr_zero; /* cookie, first word */
  286. *p++ = xdr_two; /* cookie, second word */
  287. *p++ = xdr_two; /* entry len */
  288. memcpy(p, "..\0\0", 4); /* entry */
  289. p++;
  290. *p++ = xdr_one; /* bitmap length */
  291. *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
  292. *p++ = htonl(8); /* attribute buffer length */
  293. p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
  294. readdir->pgbase = (char *)p - (char *)start;
  295. readdir->count -= readdir->pgbase;
  296. kunmap_atomic(start);
  297. }
  298. static void nfs4_test_and_free_stateid(struct nfs_server *server,
  299. nfs4_stateid *stateid,
  300. struct rpc_cred *cred)
  301. {
  302. const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
  303. ops->test_and_free_expired(server, stateid, cred);
  304. }
  305. static void __nfs4_free_revoked_stateid(struct nfs_server *server,
  306. nfs4_stateid *stateid,
  307. struct rpc_cred *cred)
  308. {
  309. stateid->type = NFS4_REVOKED_STATEID_TYPE;
  310. nfs4_test_and_free_stateid(server, stateid, cred);
  311. }
  312. static void nfs4_free_revoked_stateid(struct nfs_server *server,
  313. const nfs4_stateid *stateid,
  314. struct rpc_cred *cred)
  315. {
  316. nfs4_stateid tmp;
  317. nfs4_stateid_copy(&tmp, stateid);
  318. __nfs4_free_revoked_stateid(server, &tmp, cred);
  319. }
  320. static long nfs4_update_delay(long *timeout)
  321. {
  322. long ret;
  323. if (!timeout)
  324. return NFS4_POLL_RETRY_MAX;
  325. if (*timeout <= 0)
  326. *timeout = NFS4_POLL_RETRY_MIN;
  327. if (*timeout > NFS4_POLL_RETRY_MAX)
  328. *timeout = NFS4_POLL_RETRY_MAX;
  329. ret = *timeout;
  330. *timeout <<= 1;
  331. return ret;
  332. }
  333. static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
  334. {
  335. int res = 0;
  336. might_sleep();
  337. freezable_schedule_timeout_killable_unsafe(
  338. nfs4_update_delay(timeout));
  339. if (fatal_signal_pending(current))
  340. res = -ERESTARTSYS;
  341. return res;
  342. }
  343. /* This is the error handling routine for processes that are allowed
  344. * to sleep.
  345. */
  346. static int nfs4_do_handle_exception(struct nfs_server *server,
  347. int errorcode, struct nfs4_exception *exception)
  348. {
  349. struct nfs_client *clp = server->nfs_client;
  350. struct nfs4_state *state = exception->state;
  351. const nfs4_stateid *stateid = exception->stateid;
  352. struct inode *inode = exception->inode;
  353. int ret = errorcode;
  354. exception->delay = 0;
  355. exception->recovering = 0;
  356. exception->retry = 0;
  357. if (stateid == NULL && state != NULL)
  358. stateid = &state->stateid;
  359. switch(errorcode) {
  360. case 0:
  361. return 0;
  362. case -NFS4ERR_DELEG_REVOKED:
  363. case -NFS4ERR_ADMIN_REVOKED:
  364. case -NFS4ERR_EXPIRED:
  365. case -NFS4ERR_BAD_STATEID:
  366. if (inode != NULL && stateid != NULL) {
  367. nfs_inode_find_state_and_recover(inode,
  368. stateid);
  369. goto wait_on_recovery;
  370. }
  371. case -NFS4ERR_OPENMODE:
  372. if (inode) {
  373. int err;
  374. err = nfs_async_inode_return_delegation(inode,
  375. stateid);
  376. if (err == 0)
  377. goto wait_on_recovery;
  378. if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
  379. exception->retry = 1;
  380. break;
  381. }
  382. }
  383. if (state == NULL)
  384. break;
  385. ret = nfs4_schedule_stateid_recovery(server, state);
  386. if (ret < 0)
  387. break;
  388. goto wait_on_recovery;
  389. case -NFS4ERR_STALE_STATEID:
  390. case -NFS4ERR_STALE_CLIENTID:
  391. nfs4_schedule_lease_recovery(clp);
  392. goto wait_on_recovery;
  393. case -NFS4ERR_MOVED:
  394. ret = nfs4_schedule_migration_recovery(server);
  395. if (ret < 0)
  396. break;
  397. goto wait_on_recovery;
  398. case -NFS4ERR_LEASE_MOVED:
  399. nfs4_schedule_lease_moved_recovery(clp);
  400. goto wait_on_recovery;
  401. #if defined(CONFIG_NFS_V4_1)
  402. case -NFS4ERR_BADSESSION:
  403. case -NFS4ERR_BADSLOT:
  404. case -NFS4ERR_BAD_HIGH_SLOT:
  405. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  406. case -NFS4ERR_DEADSESSION:
  407. case -NFS4ERR_SEQ_FALSE_RETRY:
  408. case -NFS4ERR_SEQ_MISORDERED:
  409. dprintk("%s ERROR: %d Reset session\n", __func__,
  410. errorcode);
  411. nfs4_schedule_session_recovery(clp->cl_session, errorcode);
  412. goto wait_on_recovery;
  413. #endif /* defined(CONFIG_NFS_V4_1) */
  414. case -NFS4ERR_FILE_OPEN:
  415. if (exception->timeout > HZ) {
  416. /* We have retried a decent amount, time to
  417. * fail
  418. */
  419. ret = -EBUSY;
  420. break;
  421. }
  422. case -NFS4ERR_DELAY:
  423. nfs_inc_server_stats(server, NFSIOS_DELAY);
  424. case -NFS4ERR_GRACE:
  425. case -NFS4ERR_LAYOUTTRYLATER:
  426. case -NFS4ERR_RECALLCONFLICT:
  427. exception->delay = 1;
  428. return 0;
  429. case -NFS4ERR_RETRY_UNCACHED_REP:
  430. case -NFS4ERR_OLD_STATEID:
  431. exception->retry = 1;
  432. break;
  433. case -NFS4ERR_BADOWNER:
  434. /* The following works around a Linux server bug! */
  435. case -NFS4ERR_BADNAME:
  436. if (server->caps & NFS_CAP_UIDGID_NOMAP) {
  437. server->caps &= ~NFS_CAP_UIDGID_NOMAP;
  438. exception->retry = 1;
  439. printk(KERN_WARNING "NFS: v4 server %s "
  440. "does not accept raw "
  441. "uid/gids. "
  442. "Reenabling the idmapper.\n",
  443. server->nfs_client->cl_hostname);
  444. }
  445. }
  446. /* We failed to handle the error */
  447. return nfs4_map_errors(ret);
  448. wait_on_recovery:
  449. exception->recovering = 1;
  450. return 0;
  451. }
  452. /* This is the error handling routine for processes that are allowed
  453. * to sleep.
  454. */
  455. int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
  456. {
  457. struct nfs_client *clp = server->nfs_client;
  458. int ret;
  459. ret = nfs4_do_handle_exception(server, errorcode, exception);
  460. if (exception->delay) {
  461. ret = nfs4_delay(server->client, &exception->timeout);
  462. goto out_retry;
  463. }
  464. if (exception->recovering) {
  465. ret = nfs4_wait_clnt_recover(clp);
  466. if (test_bit(NFS_MIG_FAILED, &server->mig_status))
  467. return -EIO;
  468. goto out_retry;
  469. }
  470. return ret;
  471. out_retry:
  472. if (ret == 0)
  473. exception->retry = 1;
  474. return ret;
  475. }
  476. static int
  477. nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
  478. int errorcode, struct nfs4_exception *exception)
  479. {
  480. struct nfs_client *clp = server->nfs_client;
  481. int ret;
  482. ret = nfs4_do_handle_exception(server, errorcode, exception);
  483. if (exception->delay) {
  484. rpc_delay(task, nfs4_update_delay(&exception->timeout));
  485. goto out_retry;
  486. }
  487. if (exception->recovering) {
  488. rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
  489. if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
  490. rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
  491. goto out_retry;
  492. }
  493. if (test_bit(NFS_MIG_FAILED, &server->mig_status))
  494. ret = -EIO;
  495. return ret;
  496. out_retry:
  497. if (ret == 0)
  498. exception->retry = 1;
  499. return ret;
  500. }
  501. static int
  502. nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
  503. struct nfs4_state *state, long *timeout)
  504. {
  505. struct nfs4_exception exception = {
  506. .state = state,
  507. };
  508. if (task->tk_status >= 0)
  509. return 0;
  510. if (timeout)
  511. exception.timeout = *timeout;
  512. task->tk_status = nfs4_async_handle_exception(task, server,
  513. task->tk_status,
  514. &exception);
  515. if (exception.delay && timeout)
  516. *timeout = exception.timeout;
  517. if (exception.retry)
  518. return -EAGAIN;
  519. return 0;
  520. }
  521. /*
  522. * Return 'true' if 'clp' is using an rpc_client that is integrity protected
  523. * or 'false' otherwise.
  524. */
  525. static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
  526. {
  527. rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
  528. if (flavor == RPC_AUTH_GSS_KRB5I ||
  529. flavor == RPC_AUTH_GSS_KRB5P)
  530. return true;
  531. return false;
  532. }
  533. static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
  534. {
  535. spin_lock(&clp->cl_lock);
  536. if (time_before(clp->cl_last_renewal,timestamp))
  537. clp->cl_last_renewal = timestamp;
  538. spin_unlock(&clp->cl_lock);
  539. }
  540. static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
  541. {
  542. struct nfs_client *clp = server->nfs_client;
  543. if (!nfs4_has_session(clp))
  544. do_renew_lease(clp, timestamp);
  545. }
  546. struct nfs4_call_sync_data {
  547. const struct nfs_server *seq_server;
  548. struct nfs4_sequence_args *seq_args;
  549. struct nfs4_sequence_res *seq_res;
  550. };
  551. void nfs4_init_sequence(struct nfs4_sequence_args *args,
  552. struct nfs4_sequence_res *res, int cache_reply)
  553. {
  554. args->sa_slot = NULL;
  555. args->sa_cache_this = cache_reply;
  556. args->sa_privileged = 0;
  557. res->sr_slot = NULL;
  558. }
  559. static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
  560. {
  561. args->sa_privileged = 1;
  562. }
  563. int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
  564. struct nfs4_sequence_args *args,
  565. struct nfs4_sequence_res *res,
  566. struct rpc_task *task)
  567. {
  568. struct nfs4_slot *slot;
  569. /* slot already allocated? */
  570. if (res->sr_slot != NULL)
  571. goto out_start;
  572. spin_lock(&tbl->slot_tbl_lock);
  573. if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
  574. goto out_sleep;
  575. slot = nfs4_alloc_slot(tbl);
  576. if (IS_ERR(slot)) {
  577. if (slot == ERR_PTR(-ENOMEM))
  578. task->tk_timeout = HZ >> 2;
  579. goto out_sleep;
  580. }
  581. spin_unlock(&tbl->slot_tbl_lock);
  582. slot->privileged = args->sa_privileged ? 1 : 0;
  583. args->sa_slot = slot;
  584. res->sr_slot = slot;
  585. out_start:
  586. rpc_call_start(task);
  587. return 0;
  588. out_sleep:
  589. if (args->sa_privileged)
  590. rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
  591. NULL, RPC_PRIORITY_PRIVILEGED);
  592. else
  593. rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
  594. spin_unlock(&tbl->slot_tbl_lock);
  595. return -EAGAIN;
  596. }
  597. EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
  598. static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
  599. {
  600. struct nfs4_slot *slot = res->sr_slot;
  601. struct nfs4_slot_table *tbl;
  602. tbl = slot->table;
  603. spin_lock(&tbl->slot_tbl_lock);
  604. if (!nfs41_wake_and_assign_slot(tbl, slot))
  605. nfs4_free_slot(tbl, slot);
  606. spin_unlock(&tbl->slot_tbl_lock);
  607. res->sr_slot = NULL;
  608. }
  609. static int nfs40_sequence_done(struct rpc_task *task,
  610. struct nfs4_sequence_res *res)
  611. {
  612. if (res->sr_slot != NULL)
  613. nfs40_sequence_free_slot(res);
  614. return 1;
  615. }
  616. #if defined(CONFIG_NFS_V4_1)
  617. static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
  618. {
  619. struct nfs4_session *session;
  620. struct nfs4_slot_table *tbl;
  621. struct nfs4_slot *slot = res->sr_slot;
  622. bool send_new_highest_used_slotid = false;
  623. tbl = slot->table;
  624. session = tbl->session;
  625. /* Bump the slot sequence number */
  626. if (slot->seq_done)
  627. slot->seq_nr++;
  628. slot->seq_done = 0;
  629. spin_lock(&tbl->slot_tbl_lock);
  630. /* Be nice to the server: try to ensure that the last transmitted
  631. * value for highest_user_slotid <= target_highest_slotid
  632. */
  633. if (tbl->highest_used_slotid > tbl->target_highest_slotid)
  634. send_new_highest_used_slotid = true;
  635. if (nfs41_wake_and_assign_slot(tbl, slot)) {
  636. send_new_highest_used_slotid = false;
  637. goto out_unlock;
  638. }
  639. nfs4_free_slot(tbl, slot);
  640. if (tbl->highest_used_slotid != NFS4_NO_SLOT)
  641. send_new_highest_used_slotid = false;
  642. out_unlock:
  643. spin_unlock(&tbl->slot_tbl_lock);
  644. res->sr_slot = NULL;
  645. if (send_new_highest_used_slotid)
  646. nfs41_notify_server(session->clp);
  647. if (waitqueue_active(&tbl->slot_waitq))
  648. wake_up_all(&tbl->slot_waitq);
  649. }
  650. static int nfs41_sequence_process(struct rpc_task *task,
  651. struct nfs4_sequence_res *res)
  652. {
  653. struct nfs4_session *session;
  654. struct nfs4_slot *slot = res->sr_slot;
  655. struct nfs_client *clp;
  656. bool interrupted = false;
  657. int ret = 1;
  658. if (slot == NULL)
  659. goto out_noaction;
  660. /* don't increment the sequence number if the task wasn't sent */
  661. if (!RPC_WAS_SENT(task))
  662. goto out;
  663. session = slot->table->session;
  664. if (slot->interrupted) {
  665. slot->interrupted = 0;
  666. interrupted = true;
  667. }
  668. trace_nfs4_sequence_done(session, res);
  669. /* Check the SEQUENCE operation status */
  670. switch (res->sr_status) {
  671. case 0:
  672. /* If previous op on slot was interrupted and we reused
  673. * the seq# and got a reply from the cache, then retry
  674. */
  675. if (task->tk_status == -EREMOTEIO && interrupted) {
  676. ++slot->seq_nr;
  677. goto retry_nowait;
  678. }
  679. /* Update the slot's sequence and clientid lease timer */
  680. slot->seq_done = 1;
  681. clp = session->clp;
  682. do_renew_lease(clp, res->sr_timestamp);
  683. /* Check sequence flags */
  684. nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
  685. !!slot->privileged);
  686. nfs41_update_target_slotid(slot->table, slot, res);
  687. break;
  688. case 1:
  689. /*
  690. * sr_status remains 1 if an RPC level error occurred.
  691. * The server may or may not have processed the sequence
  692. * operation..
  693. * Mark the slot as having hosted an interrupted RPC call.
  694. */
  695. slot->interrupted = 1;
  696. goto out;
  697. case -NFS4ERR_DELAY:
  698. /* The server detected a resend of the RPC call and
  699. * returned NFS4ERR_DELAY as per Section 2.10.6.2
  700. * of RFC5661.
  701. */
  702. dprintk("%s: slot=%u seq=%u: Operation in progress\n",
  703. __func__,
  704. slot->slot_nr,
  705. slot->seq_nr);
  706. goto out_retry;
  707. case -NFS4ERR_BADSLOT:
  708. /*
  709. * The slot id we used was probably retired. Try again
  710. * using a different slot id.
  711. */
  712. goto retry_nowait;
  713. case -NFS4ERR_SEQ_MISORDERED:
  714. /*
  715. * Was the last operation on this sequence interrupted?
  716. * If so, retry after bumping the sequence number.
  717. */
  718. if (interrupted) {
  719. ++slot->seq_nr;
  720. goto retry_nowait;
  721. }
  722. /*
  723. * Could this slot have been previously retired?
  724. * If so, then the server may be expecting seq_nr = 1!
  725. */
  726. if (slot->seq_nr != 1) {
  727. slot->seq_nr = 1;
  728. goto retry_nowait;
  729. }
  730. break;
  731. case -NFS4ERR_SEQ_FALSE_RETRY:
  732. ++slot->seq_nr;
  733. goto retry_nowait;
  734. default:
  735. /* Just update the slot sequence no. */
  736. slot->seq_done = 1;
  737. }
  738. out:
  739. /* The session may be reset by one of the error handlers. */
  740. dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
  741. out_noaction:
  742. return ret;
  743. retry_nowait:
  744. if (rpc_restart_call_prepare(task)) {
  745. nfs41_sequence_free_slot(res);
  746. task->tk_status = 0;
  747. ret = 0;
  748. }
  749. goto out;
  750. out_retry:
  751. if (!rpc_restart_call(task))
  752. goto out;
  753. rpc_delay(task, NFS4_POLL_RETRY_MAX);
  754. return 0;
  755. }
  756. int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
  757. {
  758. if (!nfs41_sequence_process(task, res))
  759. return 0;
  760. if (res->sr_slot != NULL)
  761. nfs41_sequence_free_slot(res);
  762. return 1;
  763. }
  764. EXPORT_SYMBOL_GPL(nfs41_sequence_done);
  765. static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
  766. {
  767. if (res->sr_slot == NULL)
  768. return 1;
  769. if (res->sr_slot->table->session != NULL)
  770. return nfs41_sequence_process(task, res);
  771. return nfs40_sequence_done(task, res);
  772. }
  773. static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
  774. {
  775. if (res->sr_slot != NULL) {
  776. if (res->sr_slot->table->session != NULL)
  777. nfs41_sequence_free_slot(res);
  778. else
  779. nfs40_sequence_free_slot(res);
  780. }
  781. }
  782. int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
  783. {
  784. if (res->sr_slot == NULL)
  785. return 1;
  786. if (!res->sr_slot->table->session)
  787. return nfs40_sequence_done(task, res);
  788. return nfs41_sequence_done(task, res);
  789. }
  790. EXPORT_SYMBOL_GPL(nfs4_sequence_done);
  791. int nfs41_setup_sequence(struct nfs4_session *session,
  792. struct nfs4_sequence_args *args,
  793. struct nfs4_sequence_res *res,
  794. struct rpc_task *task)
  795. {
  796. struct nfs4_slot *slot;
  797. struct nfs4_slot_table *tbl;
  798. dprintk("--> %s\n", __func__);
  799. /* slot already allocated? */
  800. if (res->sr_slot != NULL)
  801. goto out_success;
  802. tbl = &session->fc_slot_table;
  803. task->tk_timeout = 0;
  804. spin_lock(&tbl->slot_tbl_lock);
  805. if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
  806. !args->sa_privileged) {
  807. /* The state manager will wait until the slot table is empty */
  808. dprintk("%s session is draining\n", __func__);
  809. goto out_sleep;
  810. }
  811. slot = nfs4_alloc_slot(tbl);
  812. if (IS_ERR(slot)) {
  813. /* If out of memory, try again in 1/4 second */
  814. if (slot == ERR_PTR(-ENOMEM))
  815. task->tk_timeout = HZ >> 2;
  816. dprintk("<-- %s: no free slots\n", __func__);
  817. goto out_sleep;
  818. }
  819. spin_unlock(&tbl->slot_tbl_lock);
  820. slot->privileged = args->sa_privileged ? 1 : 0;
  821. args->sa_slot = slot;
  822. dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
  823. slot->slot_nr, slot->seq_nr);
  824. res->sr_slot = slot;
  825. res->sr_timestamp = jiffies;
  826. res->sr_status_flags = 0;
  827. /*
  828. * sr_status is only set in decode_sequence, and so will remain
  829. * set to 1 if an rpc level failure occurs.
  830. */
  831. res->sr_status = 1;
  832. trace_nfs4_setup_sequence(session, args);
  833. out_success:
  834. rpc_call_start(task);
  835. return 0;
  836. out_sleep:
  837. /* Privileged tasks are queued with top priority */
  838. if (args->sa_privileged)
  839. rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
  840. NULL, RPC_PRIORITY_PRIVILEGED);
  841. else
  842. rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
  843. spin_unlock(&tbl->slot_tbl_lock);
  844. return -EAGAIN;
  845. }
  846. EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
  847. static int nfs4_setup_sequence(const struct nfs_server *server,
  848. struct nfs4_sequence_args *args,
  849. struct nfs4_sequence_res *res,
  850. struct rpc_task *task)
  851. {
  852. struct nfs4_session *session = nfs4_get_session(server);
  853. int ret = 0;
  854. if (!session)
  855. return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
  856. args, res, task);
  857. dprintk("--> %s clp %p session %p sr_slot %u\n",
  858. __func__, session->clp, session, res->sr_slot ?
  859. res->sr_slot->slot_nr : NFS4_NO_SLOT);
  860. ret = nfs41_setup_sequence(session, args, res, task);
  861. dprintk("<-- %s status=%d\n", __func__, ret);
  862. return ret;
  863. }
  864. static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
  865. {
  866. struct nfs4_call_sync_data *data = calldata;
  867. struct nfs4_session *session = nfs4_get_session(data->seq_server);
  868. dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
  869. nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
  870. }
  871. static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
  872. {
  873. struct nfs4_call_sync_data *data = calldata;
  874. nfs41_sequence_done(task, data->seq_res);
  875. }
  876. static const struct rpc_call_ops nfs41_call_sync_ops = {
  877. .rpc_call_prepare = nfs41_call_sync_prepare,
  878. .rpc_call_done = nfs41_call_sync_done,
  879. };
  880. #else /* !CONFIG_NFS_V4_1 */
  881. static int nfs4_setup_sequence(const struct nfs_server *server,
  882. struct nfs4_sequence_args *args,
  883. struct nfs4_sequence_res *res,
  884. struct rpc_task *task)
  885. {
  886. return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
  887. args, res, task);
  888. }
  889. static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
  890. {
  891. return nfs40_sequence_done(task, res);
  892. }
  893. static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
  894. {
  895. if (res->sr_slot != NULL)
  896. nfs40_sequence_free_slot(res);
  897. }
  898. int nfs4_sequence_done(struct rpc_task *task,
  899. struct nfs4_sequence_res *res)
  900. {
  901. return nfs40_sequence_done(task, res);
  902. }
  903. EXPORT_SYMBOL_GPL(nfs4_sequence_done);
  904. #endif /* !CONFIG_NFS_V4_1 */
  905. static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
  906. {
  907. struct nfs4_call_sync_data *data = calldata;
  908. nfs4_setup_sequence(data->seq_server,
  909. data->seq_args, data->seq_res, task);
  910. }
  911. static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
  912. {
  913. struct nfs4_call_sync_data *data = calldata;
  914. nfs4_sequence_done(task, data->seq_res);
  915. }
  916. static const struct rpc_call_ops nfs40_call_sync_ops = {
  917. .rpc_call_prepare = nfs40_call_sync_prepare,
  918. .rpc_call_done = nfs40_call_sync_done,
  919. };
  920. static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
  921. struct nfs_server *server,
  922. struct rpc_message *msg,
  923. struct nfs4_sequence_args *args,
  924. struct nfs4_sequence_res *res)
  925. {
  926. int ret;
  927. struct rpc_task *task;
  928. struct nfs_client *clp = server->nfs_client;
  929. struct nfs4_call_sync_data data = {
  930. .seq_server = server,
  931. .seq_args = args,
  932. .seq_res = res,
  933. };
  934. struct rpc_task_setup task_setup = {
  935. .rpc_client = clnt,
  936. .rpc_message = msg,
  937. .callback_ops = clp->cl_mvops->call_sync_ops,
  938. .callback_data = &data
  939. };
  940. task = rpc_run_task(&task_setup);
  941. if (IS_ERR(task))
  942. ret = PTR_ERR(task);
  943. else {
  944. ret = task->tk_status;
  945. rpc_put_task(task);
  946. }
  947. return ret;
  948. }
  949. int nfs4_call_sync(struct rpc_clnt *clnt,
  950. struct nfs_server *server,
  951. struct rpc_message *msg,
  952. struct nfs4_sequence_args *args,
  953. struct nfs4_sequence_res *res,
  954. int cache_reply)
  955. {
  956. nfs4_init_sequence(args, res, cache_reply);
  957. return nfs4_call_sync_sequence(clnt, server, msg, args, res);
  958. }
  959. static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
  960. {
  961. struct nfs_inode *nfsi = NFS_I(dir);
  962. spin_lock(&dir->i_lock);
  963. nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
  964. if (!cinfo->atomic || cinfo->before != dir->i_version)
  965. nfs_force_lookup_revalidate(dir);
  966. dir->i_version = cinfo->after;
  967. nfsi->attr_gencount = nfs_inc_attr_generation_counter();
  968. nfs_fscache_invalidate(dir);
  969. spin_unlock(&dir->i_lock);
  970. }
  971. struct nfs4_opendata {
  972. struct kref kref;
  973. struct nfs_openargs o_arg;
  974. struct nfs_openres o_res;
  975. struct nfs_open_confirmargs c_arg;
  976. struct nfs_open_confirmres c_res;
  977. struct nfs4_string owner_name;
  978. struct nfs4_string group_name;
  979. struct nfs4_label *a_label;
  980. struct nfs_fattr f_attr;
  981. struct nfs4_label *f_label;
  982. struct dentry *dir;
  983. struct dentry *dentry;
  984. struct nfs4_state_owner *owner;
  985. struct nfs4_state *state;
  986. struct iattr attrs;
  987. unsigned long timestamp;
  988. unsigned int rpc_done : 1;
  989. unsigned int file_created : 1;
  990. unsigned int is_recover : 1;
  991. int rpc_status;
  992. int cancelled;
  993. };
  994. static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
  995. int err, struct nfs4_exception *exception)
  996. {
  997. if (err != -EINVAL)
  998. return false;
  999. if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
  1000. return false;
  1001. server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
  1002. exception->retry = 1;
  1003. return true;
  1004. }
  1005. static u32
  1006. nfs4_map_atomic_open_share(struct nfs_server *server,
  1007. fmode_t fmode, int openflags)
  1008. {
  1009. u32 res = 0;
  1010. switch (fmode & (FMODE_READ | FMODE_WRITE)) {
  1011. case FMODE_READ:
  1012. res = NFS4_SHARE_ACCESS_READ;
  1013. break;
  1014. case FMODE_WRITE:
  1015. res = NFS4_SHARE_ACCESS_WRITE;
  1016. break;
  1017. case FMODE_READ|FMODE_WRITE:
  1018. res = NFS4_SHARE_ACCESS_BOTH;
  1019. }
  1020. if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
  1021. goto out;
  1022. /* Want no delegation if we're using O_DIRECT */
  1023. if (openflags & O_DIRECT)
  1024. res |= NFS4_SHARE_WANT_NO_DELEG;
  1025. out:
  1026. return res;
  1027. }
  1028. static enum open_claim_type4
  1029. nfs4_map_atomic_open_claim(struct nfs_server *server,
  1030. enum open_claim_type4 claim)
  1031. {
  1032. if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
  1033. return claim;
  1034. switch (claim) {
  1035. default:
  1036. return claim;
  1037. case NFS4_OPEN_CLAIM_FH:
  1038. return NFS4_OPEN_CLAIM_NULL;
  1039. case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
  1040. return NFS4_OPEN_CLAIM_DELEGATE_CUR;
  1041. case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
  1042. return NFS4_OPEN_CLAIM_DELEGATE_PREV;
  1043. }
  1044. }
  1045. static void nfs4_init_opendata_res(struct nfs4_opendata *p)
  1046. {
  1047. p->o_res.f_attr = &p->f_attr;
  1048. p->o_res.f_label = p->f_label;
  1049. p->o_res.seqid = p->o_arg.seqid;
  1050. p->c_res.seqid = p->c_arg.seqid;
  1051. p->o_res.server = p->o_arg.server;
  1052. p->o_res.access_request = p->o_arg.access;
  1053. nfs_fattr_init(&p->f_attr);
  1054. nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
  1055. }
  1056. static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
  1057. struct nfs4_state_owner *sp, fmode_t fmode, int flags,
  1058. const struct iattr *attrs,
  1059. struct nfs4_label *label,
  1060. enum open_claim_type4 claim,
  1061. gfp_t gfp_mask)
  1062. {
  1063. struct dentry *parent = dget_parent(dentry);
  1064. struct inode *dir = d_inode(parent);
  1065. struct nfs_server *server = NFS_SERVER(dir);
  1066. struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
  1067. struct nfs4_opendata *p;
  1068. p = kzalloc(sizeof(*p), gfp_mask);
  1069. if (p == NULL)
  1070. goto err;
  1071. p->f_label = nfs4_label_alloc(server, gfp_mask);
  1072. if (IS_ERR(p->f_label))
  1073. goto err_free_p;
  1074. p->a_label = nfs4_label_alloc(server, gfp_mask);
  1075. if (IS_ERR(p->a_label))
  1076. goto err_free_f;
  1077. alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
  1078. p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
  1079. if (IS_ERR(p->o_arg.seqid))
  1080. goto err_free_label;
  1081. nfs_sb_active(dentry->d_sb);
  1082. p->dentry = dget(dentry);
  1083. p->dir = parent;
  1084. p->owner = sp;
  1085. atomic_inc(&sp->so_count);
  1086. p->o_arg.open_flags = flags;
  1087. p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
  1088. p->o_arg.share_access = nfs4_map_atomic_open_share(server,
  1089. fmode, flags);
  1090. /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
  1091. * will return permission denied for all bits until close */
  1092. if (!(flags & O_EXCL)) {
  1093. /* ask server to check for all possible rights as results
  1094. * are cached */
  1095. p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
  1096. NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
  1097. }
  1098. p->o_arg.clientid = server->nfs_client->cl_clientid;
  1099. p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
  1100. p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
  1101. p->o_arg.name = &dentry->d_name;
  1102. p->o_arg.server = server;
  1103. p->o_arg.bitmask = nfs4_bitmask(server, label);
  1104. p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
  1105. p->o_arg.label = nfs4_label_copy(p->a_label, label);
  1106. p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
  1107. switch (p->o_arg.claim) {
  1108. case NFS4_OPEN_CLAIM_NULL:
  1109. case NFS4_OPEN_CLAIM_DELEGATE_CUR:
  1110. case NFS4_OPEN_CLAIM_DELEGATE_PREV:
  1111. p->o_arg.fh = NFS_FH(dir);
  1112. break;
  1113. case NFS4_OPEN_CLAIM_PREVIOUS:
  1114. case NFS4_OPEN_CLAIM_FH:
  1115. case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
  1116. case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
  1117. p->o_arg.fh = NFS_FH(d_inode(dentry));
  1118. }
  1119. if (attrs != NULL && attrs->ia_valid != 0) {
  1120. __u32 verf[2];
  1121. p->o_arg.u.attrs = &p->attrs;
  1122. memcpy(&p->attrs, attrs, sizeof(p->attrs));
  1123. verf[0] = jiffies;
  1124. verf[1] = current->pid;
  1125. memcpy(p->o_arg.u.verifier.data, verf,
  1126. sizeof(p->o_arg.u.verifier.data));
  1127. }
  1128. p->c_arg.fh = &p->o_res.fh;
  1129. p->c_arg.stateid = &p->o_res.stateid;
  1130. p->c_arg.seqid = p->o_arg.seqid;
  1131. nfs4_init_opendata_res(p);
  1132. kref_init(&p->kref);
  1133. return p;
  1134. err_free_label:
  1135. nfs4_label_free(p->a_label);
  1136. err_free_f:
  1137. nfs4_label_free(p->f_label);
  1138. err_free_p:
  1139. kfree(p);
  1140. err:
  1141. dput(parent);
  1142. return NULL;
  1143. }
  1144. static void nfs4_opendata_free(struct kref *kref)
  1145. {
  1146. struct nfs4_opendata *p = container_of(kref,
  1147. struct nfs4_opendata, kref);
  1148. struct super_block *sb = p->dentry->d_sb;
  1149. nfs_free_seqid(p->o_arg.seqid);
  1150. nfs4_sequence_free_slot(&p->o_res.seq_res);
  1151. if (p->state != NULL)
  1152. nfs4_put_open_state(p->state);
  1153. nfs4_put_state_owner(p->owner);
  1154. nfs4_label_free(p->a_label);
  1155. nfs4_label_free(p->f_label);
  1156. dput(p->dir);
  1157. dput(p->dentry);
  1158. nfs_sb_deactive(sb);
  1159. nfs_fattr_free_names(&p->f_attr);
  1160. kfree(p->f_attr.mdsthreshold);
  1161. kfree(p);
  1162. }
  1163. static void nfs4_opendata_put(struct nfs4_opendata *p)
  1164. {
  1165. if (p != NULL)
  1166. kref_put(&p->kref, nfs4_opendata_free);
  1167. }
  1168. static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
  1169. {
  1170. int ret;
  1171. ret = rpc_wait_for_completion_task(task);
  1172. return ret;
  1173. }
  1174. static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
  1175. fmode_t fmode)
  1176. {
  1177. switch(fmode & (FMODE_READ|FMODE_WRITE)) {
  1178. case FMODE_READ|FMODE_WRITE:
  1179. return state->n_rdwr != 0;
  1180. case FMODE_WRITE:
  1181. return state->n_wronly != 0;
  1182. case FMODE_READ:
  1183. return state->n_rdonly != 0;
  1184. }
  1185. WARN_ON_ONCE(1);
  1186. return false;
  1187. }
  1188. static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
  1189. {
  1190. int ret = 0;
  1191. if (open_mode & (O_EXCL|O_TRUNC))
  1192. goto out;
  1193. switch (mode & (FMODE_READ|FMODE_WRITE)) {
  1194. case FMODE_READ:
  1195. ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
  1196. && state->n_rdonly != 0;
  1197. break;
  1198. case FMODE_WRITE:
  1199. ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
  1200. && state->n_wronly != 0;
  1201. break;
  1202. case FMODE_READ|FMODE_WRITE:
  1203. ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
  1204. && state->n_rdwr != 0;
  1205. }
  1206. out:
  1207. return ret;
  1208. }
  1209. static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
  1210. enum open_claim_type4 claim)
  1211. {
  1212. if (delegation == NULL)
  1213. return 0;
  1214. if ((delegation->type & fmode) != fmode)
  1215. return 0;
  1216. if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
  1217. return 0;
  1218. switch (claim) {
  1219. case NFS4_OPEN_CLAIM_NULL:
  1220. case NFS4_OPEN_CLAIM_FH:
  1221. break;
  1222. case NFS4_OPEN_CLAIM_PREVIOUS:
  1223. if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
  1224. break;
  1225. default:
  1226. return 0;
  1227. }
  1228. nfs_mark_delegation_referenced(delegation);
  1229. return 1;
  1230. }
  1231. static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
  1232. {
  1233. switch (fmode) {
  1234. case FMODE_WRITE:
  1235. state->n_wronly++;
  1236. break;
  1237. case FMODE_READ:
  1238. state->n_rdonly++;
  1239. break;
  1240. case FMODE_READ|FMODE_WRITE:
  1241. state->n_rdwr++;
  1242. }
  1243. nfs4_state_set_mode_locked(state, state->state | fmode);
  1244. }
  1245. #ifdef CONFIG_NFS_V4_1
  1246. static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
  1247. {
  1248. if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
  1249. return true;
  1250. if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
  1251. return true;
  1252. if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
  1253. return true;
  1254. return false;
  1255. }
  1256. #endif /* CONFIG_NFS_V4_1 */
  1257. static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
  1258. {
  1259. struct nfs_client *clp = state->owner->so_server->nfs_client;
  1260. bool need_recover = false;
  1261. if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
  1262. need_recover = true;
  1263. if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
  1264. need_recover = true;
  1265. if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
  1266. need_recover = true;
  1267. if (need_recover)
  1268. nfs4_state_mark_reclaim_nograce(clp, state);
  1269. }
  1270. static bool nfs_need_update_open_stateid(struct nfs4_state *state,
  1271. const nfs4_stateid *stateid, nfs4_stateid *freeme)
  1272. {
  1273. if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
  1274. return true;
  1275. if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
  1276. nfs4_stateid_copy(freeme, &state->open_stateid);
  1277. nfs_test_and_clear_all_open_stateid(state);
  1278. return true;
  1279. }
  1280. if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
  1281. return true;
  1282. return false;
  1283. }
  1284. static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
  1285. {
  1286. if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
  1287. return;
  1288. if (state->n_wronly)
  1289. set_bit(NFS_O_WRONLY_STATE, &state->flags);
  1290. if (state->n_rdonly)
  1291. set_bit(NFS_O_RDONLY_STATE, &state->flags);
  1292. if (state->n_rdwr)
  1293. set_bit(NFS_O_RDWR_STATE, &state->flags);
  1294. set_bit(NFS_OPEN_STATE, &state->flags);
  1295. }
  1296. static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
  1297. nfs4_stateid *stateid, fmode_t fmode)
  1298. {
  1299. clear_bit(NFS_O_RDWR_STATE, &state->flags);
  1300. switch (fmode & (FMODE_READ|FMODE_WRITE)) {
  1301. case FMODE_WRITE:
  1302. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  1303. break;
  1304. case FMODE_READ:
  1305. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  1306. break;
  1307. case 0:
  1308. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  1309. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  1310. clear_bit(NFS_OPEN_STATE, &state->flags);
  1311. }
  1312. if (stateid == NULL)
  1313. return;
  1314. /* Handle OPEN+OPEN_DOWNGRADE races */
  1315. if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
  1316. !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
  1317. nfs_resync_open_stateid_locked(state);
  1318. return;
  1319. }
  1320. if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
  1321. nfs4_stateid_copy(&state->stateid, stateid);
  1322. nfs4_stateid_copy(&state->open_stateid, stateid);
  1323. }
  1324. static void nfs_clear_open_stateid(struct nfs4_state *state,
  1325. nfs4_stateid *arg_stateid,
  1326. nfs4_stateid *stateid, fmode_t fmode)
  1327. {
  1328. write_seqlock(&state->seqlock);
  1329. /* Ignore, if the CLOSE argment doesn't match the current stateid */
  1330. if (nfs4_state_match_open_stateid_other(state, arg_stateid))
  1331. nfs_clear_open_stateid_locked(state, stateid, fmode);
  1332. write_sequnlock(&state->seqlock);
  1333. if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
  1334. nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
  1335. }
  1336. static void nfs_set_open_stateid_locked(struct nfs4_state *state,
  1337. const nfs4_stateid *stateid, fmode_t fmode,
  1338. nfs4_stateid *freeme)
  1339. {
  1340. switch (fmode) {
  1341. case FMODE_READ:
  1342. set_bit(NFS_O_RDONLY_STATE, &state->flags);
  1343. break;
  1344. case FMODE_WRITE:
  1345. set_bit(NFS_O_WRONLY_STATE, &state->flags);
  1346. break;
  1347. case FMODE_READ|FMODE_WRITE:
  1348. set_bit(NFS_O_RDWR_STATE, &state->flags);
  1349. }
  1350. if (!nfs_need_update_open_stateid(state, stateid, freeme))
  1351. return;
  1352. if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
  1353. nfs4_stateid_copy(&state->stateid, stateid);
  1354. nfs4_stateid_copy(&state->open_stateid, stateid);
  1355. }
  1356. static void __update_open_stateid(struct nfs4_state *state,
  1357. const nfs4_stateid *open_stateid,
  1358. const nfs4_stateid *deleg_stateid,
  1359. fmode_t fmode,
  1360. nfs4_stateid *freeme)
  1361. {
  1362. /*
  1363. * Protect the call to nfs4_state_set_mode_locked and
  1364. * serialise the stateid update
  1365. */
  1366. spin_lock(&state->owner->so_lock);
  1367. write_seqlock(&state->seqlock);
  1368. if (deleg_stateid != NULL) {
  1369. nfs4_stateid_copy(&state->stateid, deleg_stateid);
  1370. set_bit(NFS_DELEGATED_STATE, &state->flags);
  1371. }
  1372. if (open_stateid != NULL)
  1373. nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
  1374. write_sequnlock(&state->seqlock);
  1375. update_open_stateflags(state, fmode);
  1376. spin_unlock(&state->owner->so_lock);
  1377. }
  1378. static int update_open_stateid(struct nfs4_state *state,
  1379. const nfs4_stateid *open_stateid,
  1380. const nfs4_stateid *delegation,
  1381. fmode_t fmode)
  1382. {
  1383. struct nfs_server *server = NFS_SERVER(state->inode);
  1384. struct nfs_client *clp = server->nfs_client;
  1385. struct nfs_inode *nfsi = NFS_I(state->inode);
  1386. struct nfs_delegation *deleg_cur;
  1387. nfs4_stateid freeme = { };
  1388. int ret = 0;
  1389. fmode &= (FMODE_READ|FMODE_WRITE);
  1390. rcu_read_lock();
  1391. deleg_cur = rcu_dereference(nfsi->delegation);
  1392. if (deleg_cur == NULL)
  1393. goto no_delegation;
  1394. spin_lock(&deleg_cur->lock);
  1395. if (rcu_dereference(nfsi->delegation) != deleg_cur ||
  1396. test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
  1397. (deleg_cur->type & fmode) != fmode)
  1398. goto no_delegation_unlock;
  1399. if (delegation == NULL)
  1400. delegation = &deleg_cur->stateid;
  1401. else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
  1402. goto no_delegation_unlock;
  1403. nfs_mark_delegation_referenced(deleg_cur);
  1404. __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
  1405. fmode, &freeme);
  1406. ret = 1;
  1407. no_delegation_unlock:
  1408. spin_unlock(&deleg_cur->lock);
  1409. no_delegation:
  1410. rcu_read_unlock();
  1411. if (!ret && open_stateid != NULL) {
  1412. __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
  1413. ret = 1;
  1414. }
  1415. if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
  1416. nfs4_schedule_state_manager(clp);
  1417. if (freeme.type != 0)
  1418. nfs4_test_and_free_stateid(server, &freeme,
  1419. state->owner->so_cred);
  1420. return ret;
  1421. }
  1422. static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
  1423. const nfs4_stateid *stateid)
  1424. {
  1425. struct nfs4_state *state = lsp->ls_state;
  1426. bool ret = false;
  1427. spin_lock(&state->state_lock);
  1428. if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
  1429. goto out_noupdate;
  1430. if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
  1431. goto out_noupdate;
  1432. nfs4_stateid_copy(&lsp->ls_stateid, stateid);
  1433. ret = true;
  1434. out_noupdate:
  1435. spin_unlock(&state->state_lock);
  1436. return ret;
  1437. }
  1438. static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
  1439. {
  1440. struct nfs_delegation *delegation;
  1441. rcu_read_lock();
  1442. delegation = rcu_dereference(NFS_I(inode)->delegation);
  1443. if (delegation == NULL || (delegation->type & fmode) == fmode) {
  1444. rcu_read_unlock();
  1445. return;
  1446. }
  1447. rcu_read_unlock();
  1448. nfs4_inode_return_delegation(inode);
  1449. }
  1450. static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
  1451. {
  1452. struct nfs4_state *state = opendata->state;
  1453. struct nfs_inode *nfsi = NFS_I(state->inode);
  1454. struct nfs_delegation *delegation;
  1455. int open_mode = opendata->o_arg.open_flags;
  1456. fmode_t fmode = opendata->o_arg.fmode;
  1457. enum open_claim_type4 claim = opendata->o_arg.claim;
  1458. nfs4_stateid stateid;
  1459. int ret = -EAGAIN;
  1460. for (;;) {
  1461. spin_lock(&state->owner->so_lock);
  1462. if (can_open_cached(state, fmode, open_mode)) {
  1463. update_open_stateflags(state, fmode);
  1464. spin_unlock(&state->owner->so_lock);
  1465. goto out_return_state;
  1466. }
  1467. spin_unlock(&state->owner->so_lock);
  1468. rcu_read_lock();
  1469. delegation = rcu_dereference(nfsi->delegation);
  1470. if (!can_open_delegated(delegation, fmode, claim)) {
  1471. rcu_read_unlock();
  1472. break;
  1473. }
  1474. /* Save the delegation */
  1475. nfs4_stateid_copy(&stateid, &delegation->stateid);
  1476. rcu_read_unlock();
  1477. nfs_release_seqid(opendata->o_arg.seqid);
  1478. if (!opendata->is_recover) {
  1479. ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
  1480. if (ret != 0)
  1481. goto out;
  1482. }
  1483. ret = -EAGAIN;
  1484. /* Try to update the stateid using the delegation */
  1485. if (update_open_stateid(state, NULL, &stateid, fmode))
  1486. goto out_return_state;
  1487. }
  1488. out:
  1489. return ERR_PTR(ret);
  1490. out_return_state:
  1491. atomic_inc(&state->count);
  1492. return state;
  1493. }
  1494. static void
  1495. nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
  1496. {
  1497. struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
  1498. struct nfs_delegation *delegation;
  1499. int delegation_flags = 0;
  1500. rcu_read_lock();
  1501. delegation = rcu_dereference(NFS_I(state->inode)->delegation);
  1502. if (delegation)
  1503. delegation_flags = delegation->flags;
  1504. rcu_read_unlock();
  1505. switch (data->o_arg.claim) {
  1506. default:
  1507. break;
  1508. case NFS4_OPEN_CLAIM_DELEGATE_CUR:
  1509. case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
  1510. pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
  1511. "returning a delegation for "
  1512. "OPEN(CLAIM_DELEGATE_CUR)\n",
  1513. clp->cl_hostname);
  1514. return;
  1515. }
  1516. if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
  1517. nfs_inode_set_delegation(state->inode,
  1518. data->owner->so_cred,
  1519. &data->o_res);
  1520. else
  1521. nfs_inode_reclaim_delegation(state->inode,
  1522. data->owner->so_cred,
  1523. &data->o_res);
  1524. }
  1525. /*
  1526. * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
  1527. * and update the nfs4_state.
  1528. */
  1529. static struct nfs4_state *
  1530. _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
  1531. {
  1532. struct inode *inode = data->state->inode;
  1533. struct nfs4_state *state = data->state;
  1534. int ret;
  1535. if (!data->rpc_done) {
  1536. if (data->rpc_status) {
  1537. ret = data->rpc_status;
  1538. goto err;
  1539. }
  1540. /* cached opens have already been processed */
  1541. goto update;
  1542. }
  1543. ret = nfs_refresh_inode(inode, &data->f_attr);
  1544. if (ret)
  1545. goto err;
  1546. if (data->o_res.delegation_type != 0)
  1547. nfs4_opendata_check_deleg(data, state);
  1548. update:
  1549. update_open_stateid(state, &data->o_res.stateid, NULL,
  1550. data->o_arg.fmode);
  1551. atomic_inc(&state->count);
  1552. return state;
  1553. err:
  1554. return ERR_PTR(ret);
  1555. }
  1556. static struct nfs4_state *
  1557. _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
  1558. {
  1559. struct inode *inode;
  1560. struct nfs4_state *state = NULL;
  1561. int ret;
  1562. if (!data->rpc_done) {
  1563. state = nfs4_try_open_cached(data);
  1564. trace_nfs4_cached_open(data->state);
  1565. goto out;
  1566. }
  1567. ret = -EAGAIN;
  1568. if (!(data->f_attr.valid & NFS_ATTR_FATTR))
  1569. goto err;
  1570. inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
  1571. ret = PTR_ERR(inode);
  1572. if (IS_ERR(inode))
  1573. goto err;
  1574. ret = -ENOMEM;
  1575. state = nfs4_get_open_state(inode, data->owner);
  1576. if (state == NULL)
  1577. goto err_put_inode;
  1578. if (data->o_res.delegation_type != 0)
  1579. nfs4_opendata_check_deleg(data, state);
  1580. update_open_stateid(state, &data->o_res.stateid, NULL,
  1581. data->o_arg.fmode);
  1582. iput(inode);
  1583. out:
  1584. nfs_release_seqid(data->o_arg.seqid);
  1585. return state;
  1586. err_put_inode:
  1587. iput(inode);
  1588. err:
  1589. return ERR_PTR(ret);
  1590. }
  1591. static struct nfs4_state *
  1592. nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
  1593. {
  1594. struct nfs4_state *ret;
  1595. if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
  1596. ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
  1597. else
  1598. ret = _nfs4_opendata_to_nfs4_state(data);
  1599. nfs4_sequence_free_slot(&data->o_res.seq_res);
  1600. return ret;
  1601. }
  1602. static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
  1603. {
  1604. struct nfs_inode *nfsi = NFS_I(state->inode);
  1605. struct nfs_open_context *ctx;
  1606. spin_lock(&state->inode->i_lock);
  1607. list_for_each_entry(ctx, &nfsi->open_files, list) {
  1608. if (ctx->state != state)
  1609. continue;
  1610. get_nfs_open_context(ctx);
  1611. spin_unlock(&state->inode->i_lock);
  1612. return ctx;
  1613. }
  1614. spin_unlock(&state->inode->i_lock);
  1615. return ERR_PTR(-ENOENT);
  1616. }
  1617. static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
  1618. struct nfs4_state *state, enum open_claim_type4 claim)
  1619. {
  1620. struct nfs4_opendata *opendata;
  1621. opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
  1622. NULL, NULL, claim, GFP_NOFS);
  1623. if (opendata == NULL)
  1624. return ERR_PTR(-ENOMEM);
  1625. opendata->state = state;
  1626. atomic_inc(&state->count);
  1627. return opendata;
  1628. }
  1629. static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
  1630. fmode_t fmode)
  1631. {
  1632. struct nfs4_state *newstate;
  1633. int ret;
  1634. if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
  1635. return 0;
  1636. opendata->o_arg.open_flags = 0;
  1637. opendata->o_arg.fmode = fmode;
  1638. opendata->o_arg.share_access = nfs4_map_atomic_open_share(
  1639. NFS_SB(opendata->dentry->d_sb),
  1640. fmode, 0);
  1641. memset(&opendata->o_res, 0, sizeof(opendata->o_res));
  1642. memset(&opendata->c_res, 0, sizeof(opendata->c_res));
  1643. nfs4_init_opendata_res(opendata);
  1644. ret = _nfs4_recover_proc_open(opendata);
  1645. if (ret != 0)
  1646. return ret;
  1647. newstate = nfs4_opendata_to_nfs4_state(opendata);
  1648. if (IS_ERR(newstate))
  1649. return PTR_ERR(newstate);
  1650. if (newstate != opendata->state)
  1651. ret = -ESTALE;
  1652. nfs4_close_state(newstate, fmode);
  1653. return ret;
  1654. }
  1655. static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
  1656. {
  1657. int ret;
  1658. /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
  1659. clear_bit(NFS_O_RDWR_STATE, &state->flags);
  1660. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  1661. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  1662. /* memory barrier prior to reading state->n_* */
  1663. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  1664. clear_bit(NFS_OPEN_STATE, &state->flags);
  1665. smp_rmb();
  1666. ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
  1667. if (ret != 0)
  1668. return ret;
  1669. ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
  1670. if (ret != 0)
  1671. return ret;
  1672. ret = nfs4_open_recover_helper(opendata, FMODE_READ);
  1673. if (ret != 0)
  1674. return ret;
  1675. /*
  1676. * We may have performed cached opens for all three recoveries.
  1677. * Check if we need to update the current stateid.
  1678. */
  1679. if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
  1680. !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
  1681. write_seqlock(&state->seqlock);
  1682. if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
  1683. nfs4_stateid_copy(&state->stateid, &state->open_stateid);
  1684. write_sequnlock(&state->seqlock);
  1685. }
  1686. return 0;
  1687. }
  1688. /*
  1689. * OPEN_RECLAIM:
  1690. * reclaim state on the server after a reboot.
  1691. */
  1692. static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
  1693. {
  1694. struct nfs_delegation *delegation;
  1695. struct nfs4_opendata *opendata;
  1696. fmode_t delegation_type = 0;
  1697. int status;
  1698. opendata = nfs4_open_recoverdata_alloc(ctx, state,
  1699. NFS4_OPEN_CLAIM_PREVIOUS);
  1700. if (IS_ERR(opendata))
  1701. return PTR_ERR(opendata);
  1702. rcu_read_lock();
  1703. delegation = rcu_dereference(NFS_I(state->inode)->delegation);
  1704. if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
  1705. delegation_type = delegation->type;
  1706. rcu_read_unlock();
  1707. opendata->o_arg.u.delegation_type = delegation_type;
  1708. status = nfs4_open_recover(opendata, state);
  1709. nfs4_opendata_put(opendata);
  1710. return status;
  1711. }
  1712. static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
  1713. {
  1714. struct nfs_server *server = NFS_SERVER(state->inode);
  1715. struct nfs4_exception exception = { };
  1716. int err;
  1717. do {
  1718. err = _nfs4_do_open_reclaim(ctx, state);
  1719. trace_nfs4_open_reclaim(ctx, 0, err);
  1720. if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
  1721. continue;
  1722. if (err != -NFS4ERR_DELAY)
  1723. break;
  1724. nfs4_handle_exception(server, err, &exception);
  1725. } while (exception.retry);
  1726. return err;
  1727. }
  1728. static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
  1729. {
  1730. struct nfs_open_context *ctx;
  1731. int ret;
  1732. ctx = nfs4_state_find_open_context(state);
  1733. if (IS_ERR(ctx))
  1734. return -EAGAIN;
  1735. ret = nfs4_do_open_reclaim(ctx, state);
  1736. put_nfs_open_context(ctx);
  1737. return ret;
  1738. }
  1739. static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, struct file_lock *fl, int err)
  1740. {
  1741. switch (err) {
  1742. default:
  1743. printk(KERN_ERR "NFS: %s: unhandled error "
  1744. "%d.\n", __func__, err);
  1745. case 0:
  1746. case -ENOENT:
  1747. case -EAGAIN:
  1748. case -ESTALE:
  1749. break;
  1750. case -NFS4ERR_BADSESSION:
  1751. case -NFS4ERR_BADSLOT:
  1752. case -NFS4ERR_BAD_HIGH_SLOT:
  1753. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1754. case -NFS4ERR_DEADSESSION:
  1755. set_bit(NFS_DELEGATED_STATE, &state->flags);
  1756. nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
  1757. return -EAGAIN;
  1758. case -NFS4ERR_STALE_CLIENTID:
  1759. case -NFS4ERR_STALE_STATEID:
  1760. set_bit(NFS_DELEGATED_STATE, &state->flags);
  1761. /* Don't recall a delegation if it was lost */
  1762. nfs4_schedule_lease_recovery(server->nfs_client);
  1763. return -EAGAIN;
  1764. case -NFS4ERR_MOVED:
  1765. nfs4_schedule_migration_recovery(server);
  1766. return -EAGAIN;
  1767. case -NFS4ERR_LEASE_MOVED:
  1768. nfs4_schedule_lease_moved_recovery(server->nfs_client);
  1769. return -EAGAIN;
  1770. case -NFS4ERR_DELEG_REVOKED:
  1771. case -NFS4ERR_ADMIN_REVOKED:
  1772. case -NFS4ERR_EXPIRED:
  1773. case -NFS4ERR_BAD_STATEID:
  1774. case -NFS4ERR_OPENMODE:
  1775. nfs_inode_find_state_and_recover(state->inode,
  1776. stateid);
  1777. nfs4_schedule_stateid_recovery(server, state);
  1778. return -EAGAIN;
  1779. case -NFS4ERR_DELAY:
  1780. case -NFS4ERR_GRACE:
  1781. set_bit(NFS_DELEGATED_STATE, &state->flags);
  1782. ssleep(1);
  1783. return -EAGAIN;
  1784. case -ENOMEM:
  1785. case -NFS4ERR_DENIED:
  1786. if (fl) {
  1787. struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
  1788. if (lsp)
  1789. set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
  1790. }
  1791. return 0;
  1792. }
  1793. return err;
  1794. }
  1795. int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
  1796. struct nfs4_state *state, const nfs4_stateid *stateid,
  1797. fmode_t type)
  1798. {
  1799. struct nfs_server *server = NFS_SERVER(state->inode);
  1800. struct nfs4_opendata *opendata;
  1801. int err = 0;
  1802. opendata = nfs4_open_recoverdata_alloc(ctx, state,
  1803. NFS4_OPEN_CLAIM_DELEG_CUR_FH);
  1804. if (IS_ERR(opendata))
  1805. return PTR_ERR(opendata);
  1806. nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
  1807. write_seqlock(&state->seqlock);
  1808. nfs4_stateid_copy(&state->stateid, &state->open_stateid);
  1809. write_sequnlock(&state->seqlock);
  1810. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  1811. switch (type & (FMODE_READ|FMODE_WRITE)) {
  1812. case FMODE_READ|FMODE_WRITE:
  1813. case FMODE_WRITE:
  1814. err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
  1815. if (err)
  1816. break;
  1817. err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
  1818. if (err)
  1819. break;
  1820. case FMODE_READ:
  1821. err = nfs4_open_recover_helper(opendata, FMODE_READ);
  1822. }
  1823. nfs4_opendata_put(opendata);
  1824. return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
  1825. }
  1826. static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
  1827. {
  1828. struct nfs4_opendata *data = calldata;
  1829. nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
  1830. &data->c_arg.seq_args, &data->c_res.seq_res, task);
  1831. }
  1832. static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
  1833. {
  1834. struct nfs4_opendata *data = calldata;
  1835. nfs40_sequence_done(task, &data->c_res.seq_res);
  1836. data->rpc_status = task->tk_status;
  1837. if (data->rpc_status == 0) {
  1838. nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
  1839. nfs_confirm_seqid(&data->owner->so_seqid, 0);
  1840. renew_lease(data->o_res.server, data->timestamp);
  1841. data->rpc_done = 1;
  1842. }
  1843. }
  1844. static void nfs4_open_confirm_release(void *calldata)
  1845. {
  1846. struct nfs4_opendata *data = calldata;
  1847. struct nfs4_state *state = NULL;
  1848. /* If this request hasn't been cancelled, do nothing */
  1849. if (data->cancelled == 0)
  1850. goto out_free;
  1851. /* In case of error, no cleanup! */
  1852. if (!data->rpc_done)
  1853. goto out_free;
  1854. state = nfs4_opendata_to_nfs4_state(data);
  1855. if (!IS_ERR(state))
  1856. nfs4_close_state(state, data->o_arg.fmode);
  1857. out_free:
  1858. nfs4_opendata_put(data);
  1859. }
  1860. static const struct rpc_call_ops nfs4_open_confirm_ops = {
  1861. .rpc_call_prepare = nfs4_open_confirm_prepare,
  1862. .rpc_call_done = nfs4_open_confirm_done,
  1863. .rpc_release = nfs4_open_confirm_release,
  1864. };
  1865. /*
  1866. * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
  1867. */
  1868. static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
  1869. {
  1870. struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
  1871. struct rpc_task *task;
  1872. struct rpc_message msg = {
  1873. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
  1874. .rpc_argp = &data->c_arg,
  1875. .rpc_resp = &data->c_res,
  1876. .rpc_cred = data->owner->so_cred,
  1877. };
  1878. struct rpc_task_setup task_setup_data = {
  1879. .rpc_client = server->client,
  1880. .rpc_message = &msg,
  1881. .callback_ops = &nfs4_open_confirm_ops,
  1882. .callback_data = data,
  1883. .workqueue = nfsiod_workqueue,
  1884. .flags = RPC_TASK_ASYNC,
  1885. };
  1886. int status;
  1887. nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
  1888. kref_get(&data->kref);
  1889. data->rpc_done = 0;
  1890. data->rpc_status = 0;
  1891. data->timestamp = jiffies;
  1892. if (data->is_recover)
  1893. nfs4_set_sequence_privileged(&data->c_arg.seq_args);
  1894. task = rpc_run_task(&task_setup_data);
  1895. if (IS_ERR(task))
  1896. return PTR_ERR(task);
  1897. status = nfs4_wait_for_completion_rpc_task(task);
  1898. if (status != 0) {
  1899. data->cancelled = 1;
  1900. smp_wmb();
  1901. } else
  1902. status = data->rpc_status;
  1903. rpc_put_task(task);
  1904. return status;
  1905. }
  1906. static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
  1907. {
  1908. struct nfs4_opendata *data = calldata;
  1909. struct nfs4_state_owner *sp = data->owner;
  1910. struct nfs_client *clp = sp->so_server->nfs_client;
  1911. enum open_claim_type4 claim = data->o_arg.claim;
  1912. if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
  1913. goto out_wait;
  1914. /*
  1915. * Check if we still need to send an OPEN call, or if we can use
  1916. * a delegation instead.
  1917. */
  1918. if (data->state != NULL) {
  1919. struct nfs_delegation *delegation;
  1920. if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
  1921. goto out_no_action;
  1922. rcu_read_lock();
  1923. delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
  1924. if (can_open_delegated(delegation, data->o_arg.fmode, claim))
  1925. goto unlock_no_action;
  1926. rcu_read_unlock();
  1927. }
  1928. /* Update client id. */
  1929. data->o_arg.clientid = clp->cl_clientid;
  1930. switch (claim) {
  1931. default:
  1932. break;
  1933. case NFS4_OPEN_CLAIM_PREVIOUS:
  1934. case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
  1935. case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
  1936. data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
  1937. case NFS4_OPEN_CLAIM_FH:
  1938. task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
  1939. nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
  1940. }
  1941. data->timestamp = jiffies;
  1942. if (nfs4_setup_sequence(data->o_arg.server,
  1943. &data->o_arg.seq_args,
  1944. &data->o_res.seq_res,
  1945. task) != 0)
  1946. nfs_release_seqid(data->o_arg.seqid);
  1947. /* Set the create mode (note dependency on the session type) */
  1948. data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
  1949. if (data->o_arg.open_flags & O_EXCL) {
  1950. data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
  1951. if (nfs4_has_persistent_session(clp))
  1952. data->o_arg.createmode = NFS4_CREATE_GUARDED;
  1953. else if (clp->cl_mvops->minor_version > 0)
  1954. data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
  1955. }
  1956. return;
  1957. unlock_no_action:
  1958. trace_nfs4_cached_open(data->state);
  1959. rcu_read_unlock();
  1960. out_no_action:
  1961. task->tk_action = NULL;
  1962. out_wait:
  1963. nfs4_sequence_done(task, &data->o_res.seq_res);
  1964. }
  1965. static void nfs4_open_done(struct rpc_task *task, void *calldata)
  1966. {
  1967. struct nfs4_opendata *data = calldata;
  1968. data->rpc_status = task->tk_status;
  1969. if (!nfs4_sequence_process(task, &data->o_res.seq_res))
  1970. return;
  1971. if (task->tk_status == 0) {
  1972. if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
  1973. switch (data->o_res.f_attr->mode & S_IFMT) {
  1974. case S_IFREG:
  1975. break;
  1976. case S_IFLNK:
  1977. data->rpc_status = -ELOOP;
  1978. break;
  1979. case S_IFDIR:
  1980. data->rpc_status = -EISDIR;
  1981. break;
  1982. default:
  1983. data->rpc_status = -ENOTDIR;
  1984. }
  1985. }
  1986. renew_lease(data->o_res.server, data->timestamp);
  1987. if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
  1988. nfs_confirm_seqid(&data->owner->so_seqid, 0);
  1989. }
  1990. data->rpc_done = 1;
  1991. }
  1992. static void nfs4_open_release(void *calldata)
  1993. {
  1994. struct nfs4_opendata *data = calldata;
  1995. struct nfs4_state *state = NULL;
  1996. /* If this request hasn't been cancelled, do nothing */
  1997. if (data->cancelled == 0)
  1998. goto out_free;
  1999. /* In case of error, no cleanup! */
  2000. if (data->rpc_status != 0 || !data->rpc_done)
  2001. goto out_free;
  2002. /* In case we need an open_confirm, no cleanup! */
  2003. if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
  2004. goto out_free;
  2005. state = nfs4_opendata_to_nfs4_state(data);
  2006. if (!IS_ERR(state))
  2007. nfs4_close_state(state, data->o_arg.fmode);
  2008. out_free:
  2009. nfs4_opendata_put(data);
  2010. }
  2011. static const struct rpc_call_ops nfs4_open_ops = {
  2012. .rpc_call_prepare = nfs4_open_prepare,
  2013. .rpc_call_done = nfs4_open_done,
  2014. .rpc_release = nfs4_open_release,
  2015. };
  2016. static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
  2017. {
  2018. struct inode *dir = d_inode(data->dir);
  2019. struct nfs_server *server = NFS_SERVER(dir);
  2020. struct nfs_openargs *o_arg = &data->o_arg;
  2021. struct nfs_openres *o_res = &data->o_res;
  2022. struct rpc_task *task;
  2023. struct rpc_message msg = {
  2024. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
  2025. .rpc_argp = o_arg,
  2026. .rpc_resp = o_res,
  2027. .rpc_cred = data->owner->so_cred,
  2028. };
  2029. struct rpc_task_setup task_setup_data = {
  2030. .rpc_client = server->client,
  2031. .rpc_message = &msg,
  2032. .callback_ops = &nfs4_open_ops,
  2033. .callback_data = data,
  2034. .workqueue = nfsiod_workqueue,
  2035. .flags = RPC_TASK_ASYNC,
  2036. };
  2037. int status;
  2038. nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
  2039. kref_get(&data->kref);
  2040. data->rpc_done = 0;
  2041. data->rpc_status = 0;
  2042. data->cancelled = 0;
  2043. data->is_recover = 0;
  2044. if (isrecover) {
  2045. nfs4_set_sequence_privileged(&o_arg->seq_args);
  2046. data->is_recover = 1;
  2047. }
  2048. task = rpc_run_task(&task_setup_data);
  2049. if (IS_ERR(task))
  2050. return PTR_ERR(task);
  2051. status = nfs4_wait_for_completion_rpc_task(task);
  2052. if (status != 0) {
  2053. data->cancelled = 1;
  2054. smp_wmb();
  2055. } else
  2056. status = data->rpc_status;
  2057. rpc_put_task(task);
  2058. return status;
  2059. }
  2060. static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
  2061. {
  2062. struct inode *dir = d_inode(data->dir);
  2063. struct nfs_openres *o_res = &data->o_res;
  2064. int status;
  2065. status = nfs4_run_open_task(data, 1);
  2066. if (status != 0 || !data->rpc_done)
  2067. return status;
  2068. nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
  2069. if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
  2070. status = _nfs4_proc_open_confirm(data);
  2071. if (status != 0)
  2072. return status;
  2073. }
  2074. return status;
  2075. }
  2076. /*
  2077. * Additional permission checks in order to distinguish between an
  2078. * open for read, and an open for execute. This works around the
  2079. * fact that NFSv4 OPEN treats read and execute permissions as being
  2080. * the same.
  2081. * Note that in the non-execute case, we want to turn off permission
  2082. * checking if we just created a new file (POSIX open() semantics).
  2083. */
  2084. static int nfs4_opendata_access(struct rpc_cred *cred,
  2085. struct nfs4_opendata *opendata,
  2086. struct nfs4_state *state, fmode_t fmode,
  2087. int openflags)
  2088. {
  2089. struct nfs_access_entry cache;
  2090. u32 mask;
  2091. /* access call failed or for some reason the server doesn't
  2092. * support any access modes -- defer access call until later */
  2093. if (opendata->o_res.access_supported == 0)
  2094. return 0;
  2095. mask = 0;
  2096. /*
  2097. * Use openflags to check for exec, because fmode won't
  2098. * always have FMODE_EXEC set when file open for exec.
  2099. */
  2100. if (openflags & __FMODE_EXEC) {
  2101. /* ONLY check for exec rights */
  2102. mask = MAY_EXEC;
  2103. } else if ((fmode & FMODE_READ) && !opendata->file_created)
  2104. mask = MAY_READ;
  2105. cache.cred = cred;
  2106. cache.jiffies = jiffies;
  2107. nfs_access_set_mask(&cache, opendata->o_res.access_result);
  2108. nfs_access_add_cache(state->inode, &cache);
  2109. if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
  2110. return 0;
  2111. return -EACCES;
  2112. }
  2113. /*
  2114. * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
  2115. */
  2116. static int _nfs4_proc_open(struct nfs4_opendata *data)
  2117. {
  2118. struct inode *dir = d_inode(data->dir);
  2119. struct nfs_server *server = NFS_SERVER(dir);
  2120. struct nfs_openargs *o_arg = &data->o_arg;
  2121. struct nfs_openres *o_res = &data->o_res;
  2122. int status;
  2123. status = nfs4_run_open_task(data, 0);
  2124. if (!data->rpc_done)
  2125. return status;
  2126. if (status != 0) {
  2127. if (status == -NFS4ERR_BADNAME &&
  2128. !(o_arg->open_flags & O_CREAT))
  2129. return -ENOENT;
  2130. return status;
  2131. }
  2132. nfs_fattr_map_and_free_names(server, &data->f_attr);
  2133. if (o_arg->open_flags & O_CREAT) {
  2134. update_changeattr(dir, &o_res->cinfo);
  2135. if (o_arg->open_flags & O_EXCL)
  2136. data->file_created = 1;
  2137. else if (o_res->cinfo.before != o_res->cinfo.after)
  2138. data->file_created = 1;
  2139. }
  2140. if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
  2141. server->caps &= ~NFS_CAP_POSIX_LOCK;
  2142. if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
  2143. status = _nfs4_proc_open_confirm(data);
  2144. if (status != 0)
  2145. return status;
  2146. }
  2147. if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
  2148. nfs4_sequence_free_slot(&o_res->seq_res);
  2149. nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
  2150. }
  2151. return 0;
  2152. }
  2153. static int nfs4_recover_expired_lease(struct nfs_server *server)
  2154. {
  2155. return nfs4_client_recover_expired_lease(server->nfs_client);
  2156. }
  2157. /*
  2158. * OPEN_EXPIRED:
  2159. * reclaim state on the server after a network partition.
  2160. * Assumes caller holds the appropriate lock
  2161. */
  2162. static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
  2163. {
  2164. struct nfs4_opendata *opendata;
  2165. int ret;
  2166. opendata = nfs4_open_recoverdata_alloc(ctx, state,
  2167. NFS4_OPEN_CLAIM_FH);
  2168. if (IS_ERR(opendata))
  2169. return PTR_ERR(opendata);
  2170. ret = nfs4_open_recover(opendata, state);
  2171. if (ret == -ESTALE)
  2172. d_drop(ctx->dentry);
  2173. nfs4_opendata_put(opendata);
  2174. return ret;
  2175. }
  2176. static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
  2177. {
  2178. struct nfs_server *server = NFS_SERVER(state->inode);
  2179. struct nfs4_exception exception = { };
  2180. int err;
  2181. do {
  2182. err = _nfs4_open_expired(ctx, state);
  2183. trace_nfs4_open_expired(ctx, 0, err);
  2184. if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
  2185. continue;
  2186. switch (err) {
  2187. default:
  2188. goto out;
  2189. case -NFS4ERR_GRACE:
  2190. case -NFS4ERR_DELAY:
  2191. nfs4_handle_exception(server, err, &exception);
  2192. err = 0;
  2193. }
  2194. } while (exception.retry);
  2195. out:
  2196. return err;
  2197. }
  2198. static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
  2199. {
  2200. struct nfs_open_context *ctx;
  2201. int ret;
  2202. ctx = nfs4_state_find_open_context(state);
  2203. if (IS_ERR(ctx))
  2204. return -EAGAIN;
  2205. ret = nfs4_do_open_expired(ctx, state);
  2206. put_nfs_open_context(ctx);
  2207. return ret;
  2208. }
  2209. static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
  2210. const nfs4_stateid *stateid)
  2211. {
  2212. nfs_remove_bad_delegation(state->inode, stateid);
  2213. write_seqlock(&state->seqlock);
  2214. nfs4_stateid_copy(&state->stateid, &state->open_stateid);
  2215. write_sequnlock(&state->seqlock);
  2216. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  2217. }
  2218. static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
  2219. {
  2220. if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
  2221. nfs_finish_clear_delegation_stateid(state, NULL);
  2222. }
  2223. static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
  2224. {
  2225. /* NFSv4.0 doesn't allow for delegation recovery on open expire */
  2226. nfs40_clear_delegation_stateid(state);
  2227. return nfs4_open_expired(sp, state);
  2228. }
  2229. static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
  2230. nfs4_stateid *stateid,
  2231. struct rpc_cred *cred)
  2232. {
  2233. return -NFS4ERR_BAD_STATEID;
  2234. }
  2235. #if defined(CONFIG_NFS_V4_1)
  2236. static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
  2237. nfs4_stateid *stateid,
  2238. struct rpc_cred *cred)
  2239. {
  2240. int status;
  2241. switch (stateid->type) {
  2242. default:
  2243. break;
  2244. case NFS4_INVALID_STATEID_TYPE:
  2245. case NFS4_SPECIAL_STATEID_TYPE:
  2246. return -NFS4ERR_BAD_STATEID;
  2247. case NFS4_REVOKED_STATEID_TYPE:
  2248. goto out_free;
  2249. }
  2250. status = nfs41_test_stateid(server, stateid, cred);
  2251. switch (status) {
  2252. case -NFS4ERR_EXPIRED:
  2253. case -NFS4ERR_ADMIN_REVOKED:
  2254. case -NFS4ERR_DELEG_REVOKED:
  2255. break;
  2256. default:
  2257. return status;
  2258. }
  2259. out_free:
  2260. /* Ack the revoked state to the server */
  2261. nfs41_free_stateid(server, stateid, cred, true);
  2262. return -NFS4ERR_EXPIRED;
  2263. }
  2264. static void nfs41_check_delegation_stateid(struct nfs4_state *state)
  2265. {
  2266. struct nfs_server *server = NFS_SERVER(state->inode);
  2267. nfs4_stateid stateid;
  2268. struct nfs_delegation *delegation;
  2269. struct rpc_cred *cred;
  2270. int status;
  2271. /* Get the delegation credential for use by test/free_stateid */
  2272. rcu_read_lock();
  2273. delegation = rcu_dereference(NFS_I(state->inode)->delegation);
  2274. if (delegation == NULL) {
  2275. rcu_read_unlock();
  2276. return;
  2277. }
  2278. nfs4_stateid_copy(&stateid, &delegation->stateid);
  2279. if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) ||
  2280. !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
  2281. &delegation->flags)) {
  2282. rcu_read_unlock();
  2283. nfs_finish_clear_delegation_stateid(state, &stateid);
  2284. return;
  2285. }
  2286. cred = get_rpccred(delegation->cred);
  2287. rcu_read_unlock();
  2288. status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
  2289. trace_nfs4_test_delegation_stateid(state, NULL, status);
  2290. if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
  2291. nfs_finish_clear_delegation_stateid(state, &stateid);
  2292. put_rpccred(cred);
  2293. }
  2294. /**
  2295. * nfs41_check_expired_locks - possibly free a lock stateid
  2296. *
  2297. * @state: NFSv4 state for an inode
  2298. *
  2299. * Returns NFS_OK if recovery for this stateid is now finished.
  2300. * Otherwise a negative NFS4ERR value is returned.
  2301. */
  2302. static int nfs41_check_expired_locks(struct nfs4_state *state)
  2303. {
  2304. int status, ret = NFS_OK;
  2305. struct nfs4_lock_state *lsp, *prev = NULL;
  2306. struct nfs_server *server = NFS_SERVER(state->inode);
  2307. if (!test_bit(LK_STATE_IN_USE, &state->flags))
  2308. goto out;
  2309. spin_lock(&state->state_lock);
  2310. list_for_each_entry(lsp, &state->lock_states, ls_locks) {
  2311. if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
  2312. struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
  2313. atomic_inc(&lsp->ls_count);
  2314. spin_unlock(&state->state_lock);
  2315. nfs4_put_lock_state(prev);
  2316. prev = lsp;
  2317. status = nfs41_test_and_free_expired_stateid(server,
  2318. &lsp->ls_stateid,
  2319. cred);
  2320. trace_nfs4_test_lock_stateid(state, lsp, status);
  2321. if (status == -NFS4ERR_EXPIRED ||
  2322. status == -NFS4ERR_BAD_STATEID) {
  2323. clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
  2324. lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
  2325. if (!recover_lost_locks)
  2326. set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
  2327. } else if (status != NFS_OK) {
  2328. ret = status;
  2329. nfs4_put_lock_state(prev);
  2330. goto out;
  2331. }
  2332. spin_lock(&state->state_lock);
  2333. }
  2334. }
  2335. spin_unlock(&state->state_lock);
  2336. nfs4_put_lock_state(prev);
  2337. out:
  2338. return ret;
  2339. }
  2340. /**
  2341. * nfs41_check_open_stateid - possibly free an open stateid
  2342. *
  2343. * @state: NFSv4 state for an inode
  2344. *
  2345. * Returns NFS_OK if recovery for this stateid is now finished.
  2346. * Otherwise a negative NFS4ERR value is returned.
  2347. */
  2348. static int nfs41_check_open_stateid(struct nfs4_state *state)
  2349. {
  2350. struct nfs_server *server = NFS_SERVER(state->inode);
  2351. nfs4_stateid *stateid = &state->open_stateid;
  2352. struct rpc_cred *cred = state->owner->so_cred;
  2353. int status;
  2354. if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
  2355. if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
  2356. if (nfs4_have_delegation(state->inode, state->state))
  2357. return NFS_OK;
  2358. return -NFS4ERR_OPENMODE;
  2359. }
  2360. return -NFS4ERR_BAD_STATEID;
  2361. }
  2362. status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
  2363. trace_nfs4_test_open_stateid(state, NULL, status);
  2364. if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
  2365. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  2366. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  2367. clear_bit(NFS_O_RDWR_STATE, &state->flags);
  2368. clear_bit(NFS_OPEN_STATE, &state->flags);
  2369. stateid->type = NFS4_INVALID_STATEID_TYPE;
  2370. }
  2371. if (status != NFS_OK)
  2372. return status;
  2373. if (nfs_open_stateid_recover_openmode(state))
  2374. return -NFS4ERR_OPENMODE;
  2375. return NFS_OK;
  2376. }
  2377. static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
  2378. {
  2379. int status;
  2380. nfs41_check_delegation_stateid(state);
  2381. status = nfs41_check_expired_locks(state);
  2382. if (status != NFS_OK)
  2383. return status;
  2384. status = nfs41_check_open_stateid(state);
  2385. if (status != NFS_OK)
  2386. status = nfs4_open_expired(sp, state);
  2387. return status;
  2388. }
  2389. #endif
  2390. /*
  2391. * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
  2392. * fields corresponding to attributes that were used to store the verifier.
  2393. * Make sure we clobber those fields in the later setattr call
  2394. */
  2395. static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
  2396. struct iattr *sattr, struct nfs4_label **label)
  2397. {
  2398. const u32 *attrset = opendata->o_res.attrset;
  2399. if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
  2400. !(sattr->ia_valid & ATTR_ATIME_SET))
  2401. sattr->ia_valid |= ATTR_ATIME;
  2402. if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
  2403. !(sattr->ia_valid & ATTR_MTIME_SET))
  2404. sattr->ia_valid |= ATTR_MTIME;
  2405. /* Except MODE, it seems harmless of setting twice. */
  2406. if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
  2407. attrset[1] & FATTR4_WORD1_MODE)
  2408. sattr->ia_valid &= ~ATTR_MODE;
  2409. if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
  2410. *label = NULL;
  2411. }
  2412. static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
  2413. fmode_t fmode,
  2414. int flags,
  2415. struct nfs_open_context *ctx)
  2416. {
  2417. struct nfs4_state_owner *sp = opendata->owner;
  2418. struct nfs_server *server = sp->so_server;
  2419. struct dentry *dentry;
  2420. struct nfs4_state *state;
  2421. unsigned int seq;
  2422. int ret;
  2423. seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
  2424. ret = _nfs4_proc_open(opendata);
  2425. if (ret != 0)
  2426. goto out;
  2427. state = _nfs4_opendata_to_nfs4_state(opendata);
  2428. ret = PTR_ERR(state);
  2429. if (IS_ERR(state))
  2430. goto out;
  2431. ctx->state = state;
  2432. if (server->caps & NFS_CAP_POSIX_LOCK)
  2433. set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
  2434. if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
  2435. set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
  2436. dentry = opendata->dentry;
  2437. if (d_really_is_negative(dentry)) {
  2438. struct dentry *alias;
  2439. d_drop(dentry);
  2440. alias = d_exact_alias(dentry, state->inode);
  2441. if (!alias)
  2442. alias = d_splice_alias(igrab(state->inode), dentry);
  2443. /* d_splice_alias() can't fail here - it's a non-directory */
  2444. if (alias) {
  2445. dput(ctx->dentry);
  2446. ctx->dentry = dentry = alias;
  2447. }
  2448. nfs_set_verifier(dentry,
  2449. nfs_save_change_attribute(d_inode(opendata->dir)));
  2450. }
  2451. ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
  2452. if (ret != 0)
  2453. goto out;
  2454. if (d_inode(dentry) == state->inode) {
  2455. nfs_inode_attach_open_context(ctx);
  2456. if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
  2457. nfs4_schedule_stateid_recovery(server, state);
  2458. }
  2459. out:
  2460. nfs4_sequence_free_slot(&opendata->o_res.seq_res);
  2461. return ret;
  2462. }
  2463. /*
  2464. * Returns a referenced nfs4_state
  2465. */
  2466. static int _nfs4_do_open(struct inode *dir,
  2467. struct nfs_open_context *ctx,
  2468. int flags,
  2469. struct iattr *sattr,
  2470. struct nfs4_label *label,
  2471. int *opened)
  2472. {
  2473. struct nfs4_state_owner *sp;
  2474. struct nfs4_state *state = NULL;
  2475. struct nfs_server *server = NFS_SERVER(dir);
  2476. struct nfs4_opendata *opendata;
  2477. struct dentry *dentry = ctx->dentry;
  2478. struct rpc_cred *cred = ctx->cred;
  2479. struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
  2480. fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
  2481. enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
  2482. struct nfs4_label *olabel = NULL;
  2483. int status;
  2484. /* Protect against reboot recovery conflicts */
  2485. status = -ENOMEM;
  2486. sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
  2487. if (sp == NULL) {
  2488. dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
  2489. goto out_err;
  2490. }
  2491. status = nfs4_recover_expired_lease(server);
  2492. if (status != 0)
  2493. goto err_put_state_owner;
  2494. if (d_really_is_positive(dentry))
  2495. nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
  2496. status = -ENOMEM;
  2497. if (d_really_is_positive(dentry))
  2498. claim = NFS4_OPEN_CLAIM_FH;
  2499. opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
  2500. label, claim, GFP_KERNEL);
  2501. if (opendata == NULL)
  2502. goto err_put_state_owner;
  2503. if (label) {
  2504. olabel = nfs4_label_alloc(server, GFP_KERNEL);
  2505. if (IS_ERR(olabel)) {
  2506. status = PTR_ERR(olabel);
  2507. goto err_opendata_put;
  2508. }
  2509. }
  2510. if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
  2511. if (!opendata->f_attr.mdsthreshold) {
  2512. opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
  2513. if (!opendata->f_attr.mdsthreshold)
  2514. goto err_free_label;
  2515. }
  2516. opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
  2517. }
  2518. if (d_really_is_positive(dentry))
  2519. opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
  2520. status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
  2521. if (status != 0)
  2522. goto err_free_label;
  2523. state = ctx->state;
  2524. if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
  2525. (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
  2526. nfs4_exclusive_attrset(opendata, sattr, &label);
  2527. /*
  2528. * send create attributes which was not set by open
  2529. * with an extra setattr.
  2530. */
  2531. if (sattr->ia_valid & NFS4_VALID_ATTRS) {
  2532. nfs_fattr_init(opendata->o_res.f_attr);
  2533. status = nfs4_do_setattr(state->inode, cred,
  2534. opendata->o_res.f_attr, sattr,
  2535. state, label, olabel);
  2536. if (status == 0) {
  2537. nfs_setattr_update_inode(state->inode, sattr,
  2538. opendata->o_res.f_attr);
  2539. nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
  2540. }
  2541. }
  2542. }
  2543. if (opened && opendata->file_created)
  2544. *opened |= FILE_CREATED;
  2545. if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
  2546. *ctx_th = opendata->f_attr.mdsthreshold;
  2547. opendata->f_attr.mdsthreshold = NULL;
  2548. }
  2549. nfs4_label_free(olabel);
  2550. nfs4_opendata_put(opendata);
  2551. nfs4_put_state_owner(sp);
  2552. return 0;
  2553. err_free_label:
  2554. nfs4_label_free(olabel);
  2555. err_opendata_put:
  2556. nfs4_opendata_put(opendata);
  2557. err_put_state_owner:
  2558. nfs4_put_state_owner(sp);
  2559. out_err:
  2560. return status;
  2561. }
  2562. static struct nfs4_state *nfs4_do_open(struct inode *dir,
  2563. struct nfs_open_context *ctx,
  2564. int flags,
  2565. struct iattr *sattr,
  2566. struct nfs4_label *label,
  2567. int *opened)
  2568. {
  2569. struct nfs_server *server = NFS_SERVER(dir);
  2570. struct nfs4_exception exception = { };
  2571. struct nfs4_state *res;
  2572. int status;
  2573. do {
  2574. status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
  2575. res = ctx->state;
  2576. trace_nfs4_open_file(ctx, flags, status);
  2577. if (status == 0)
  2578. break;
  2579. /* NOTE: BAD_SEQID means the server and client disagree about the
  2580. * book-keeping w.r.t. state-changing operations
  2581. * (OPEN/CLOSE/LOCK/LOCKU...)
  2582. * It is actually a sign of a bug on the client or on the server.
  2583. *
  2584. * If we receive a BAD_SEQID error in the particular case of
  2585. * doing an OPEN, we assume that nfs_increment_open_seqid() will
  2586. * have unhashed the old state_owner for us, and that we can
  2587. * therefore safely retry using a new one. We should still warn
  2588. * the user though...
  2589. */
  2590. if (status == -NFS4ERR_BAD_SEQID) {
  2591. pr_warn_ratelimited("NFS: v4 server %s "
  2592. " returned a bad sequence-id error!\n",
  2593. NFS_SERVER(dir)->nfs_client->cl_hostname);
  2594. exception.retry = 1;
  2595. continue;
  2596. }
  2597. /*
  2598. * BAD_STATEID on OPEN means that the server cancelled our
  2599. * state before it received the OPEN_CONFIRM.
  2600. * Recover by retrying the request as per the discussion
  2601. * on Page 181 of RFC3530.
  2602. */
  2603. if (status == -NFS4ERR_BAD_STATEID) {
  2604. exception.retry = 1;
  2605. continue;
  2606. }
  2607. if (status == -EAGAIN) {
  2608. /* We must have found a delegation */
  2609. exception.retry = 1;
  2610. continue;
  2611. }
  2612. if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
  2613. continue;
  2614. res = ERR_PTR(nfs4_handle_exception(server,
  2615. status, &exception));
  2616. } while (exception.retry);
  2617. return res;
  2618. }
  2619. static int _nfs4_do_setattr(struct inode *inode,
  2620. struct nfs_setattrargs *arg,
  2621. struct nfs_setattrres *res,
  2622. struct rpc_cred *cred,
  2623. struct nfs4_state *state)
  2624. {
  2625. struct nfs_server *server = NFS_SERVER(inode);
  2626. struct rpc_message msg = {
  2627. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
  2628. .rpc_argp = arg,
  2629. .rpc_resp = res,
  2630. .rpc_cred = cred,
  2631. };
  2632. struct rpc_cred *delegation_cred = NULL;
  2633. unsigned long timestamp = jiffies;
  2634. fmode_t fmode;
  2635. bool truncate;
  2636. int status;
  2637. nfs_fattr_init(res->fattr);
  2638. /* Servers should only apply open mode checks for file size changes */
  2639. truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
  2640. fmode = truncate ? FMODE_WRITE : FMODE_READ;
  2641. if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
  2642. /* Use that stateid */
  2643. } else if (truncate && state != NULL) {
  2644. struct nfs_lockowner lockowner = {
  2645. .l_owner = current->files,
  2646. .l_pid = current->tgid,
  2647. };
  2648. if (!nfs4_valid_open_stateid(state))
  2649. return -EBADF;
  2650. if (nfs4_select_rw_stateid(state, FMODE_WRITE, &lockowner,
  2651. &arg->stateid, &delegation_cred) == -EIO)
  2652. return -EBADF;
  2653. } else
  2654. nfs4_stateid_copy(&arg->stateid, &zero_stateid);
  2655. if (delegation_cred)
  2656. msg.rpc_cred = delegation_cred;
  2657. status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
  2658. put_rpccred(delegation_cred);
  2659. if (status == 0 && state != NULL)
  2660. renew_lease(server, timestamp);
  2661. trace_nfs4_setattr(inode, &arg->stateid, status);
  2662. return status;
  2663. }
  2664. static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
  2665. struct nfs_fattr *fattr, struct iattr *sattr,
  2666. struct nfs4_state *state, struct nfs4_label *ilabel,
  2667. struct nfs4_label *olabel)
  2668. {
  2669. struct nfs_server *server = NFS_SERVER(inode);
  2670. struct nfs_setattrargs arg = {
  2671. .fh = NFS_FH(inode),
  2672. .iap = sattr,
  2673. .server = server,
  2674. .bitmask = server->attr_bitmask,
  2675. .label = ilabel,
  2676. };
  2677. struct nfs_setattrres res = {
  2678. .fattr = fattr,
  2679. .label = olabel,
  2680. .server = server,
  2681. };
  2682. struct nfs4_exception exception = {
  2683. .state = state,
  2684. .inode = inode,
  2685. .stateid = &arg.stateid,
  2686. };
  2687. int err;
  2688. arg.bitmask = nfs4_bitmask(server, ilabel);
  2689. if (ilabel)
  2690. arg.bitmask = nfs4_bitmask(server, olabel);
  2691. do {
  2692. err = _nfs4_do_setattr(inode, &arg, &res, cred, state);
  2693. switch (err) {
  2694. case -NFS4ERR_OPENMODE:
  2695. if (!(sattr->ia_valid & ATTR_SIZE)) {
  2696. pr_warn_once("NFSv4: server %s is incorrectly "
  2697. "applying open mode checks to "
  2698. "a SETATTR that is not "
  2699. "changing file size.\n",
  2700. server->nfs_client->cl_hostname);
  2701. }
  2702. if (state && !(state->state & FMODE_WRITE)) {
  2703. err = -EBADF;
  2704. if (sattr->ia_valid & ATTR_OPEN)
  2705. err = -EACCES;
  2706. goto out;
  2707. }
  2708. }
  2709. err = nfs4_handle_exception(server, err, &exception);
  2710. } while (exception.retry);
  2711. out:
  2712. return err;
  2713. }
  2714. static bool
  2715. nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
  2716. {
  2717. if (inode == NULL || !nfs_have_layout(inode))
  2718. return false;
  2719. return pnfs_wait_on_layoutreturn(inode, task);
  2720. }
  2721. struct nfs4_closedata {
  2722. struct inode *inode;
  2723. struct nfs4_state *state;
  2724. struct nfs_closeargs arg;
  2725. struct nfs_closeres res;
  2726. struct nfs_fattr fattr;
  2727. unsigned long timestamp;
  2728. bool roc;
  2729. u32 roc_barrier;
  2730. };
  2731. static void nfs4_free_closedata(void *data)
  2732. {
  2733. struct nfs4_closedata *calldata = data;
  2734. struct nfs4_state_owner *sp = calldata->state->owner;
  2735. struct super_block *sb = calldata->state->inode->i_sb;
  2736. if (calldata->roc)
  2737. pnfs_roc_release(calldata->state->inode);
  2738. nfs4_put_open_state(calldata->state);
  2739. nfs_free_seqid(calldata->arg.seqid);
  2740. nfs4_put_state_owner(sp);
  2741. nfs_sb_deactive(sb);
  2742. kfree(calldata);
  2743. }
  2744. static void nfs4_close_done(struct rpc_task *task, void *data)
  2745. {
  2746. struct nfs4_closedata *calldata = data;
  2747. struct nfs4_state *state = calldata->state;
  2748. struct nfs_server *server = NFS_SERVER(calldata->inode);
  2749. nfs4_stateid *res_stateid = NULL;
  2750. dprintk("%s: begin!\n", __func__);
  2751. if (!nfs4_sequence_done(task, &calldata->res.seq_res))
  2752. return;
  2753. trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
  2754. /* hmm. we are done with the inode, and in the process of freeing
  2755. * the state_owner. we keep this around to process errors
  2756. */
  2757. switch (task->tk_status) {
  2758. case 0:
  2759. res_stateid = &calldata->res.stateid;
  2760. if (calldata->roc)
  2761. pnfs_roc_set_barrier(state->inode,
  2762. calldata->roc_barrier);
  2763. renew_lease(server, calldata->timestamp);
  2764. break;
  2765. case -NFS4ERR_ADMIN_REVOKED:
  2766. case -NFS4ERR_STALE_STATEID:
  2767. case -NFS4ERR_EXPIRED:
  2768. nfs4_free_revoked_stateid(server,
  2769. &calldata->arg.stateid,
  2770. task->tk_msg.rpc_cred);
  2771. case -NFS4ERR_OLD_STATEID:
  2772. case -NFS4ERR_BAD_STATEID:
  2773. if (!nfs4_stateid_match(&calldata->arg.stateid,
  2774. &state->open_stateid)) {
  2775. rpc_restart_call_prepare(task);
  2776. goto out_release;
  2777. }
  2778. if (calldata->arg.fmode == 0)
  2779. break;
  2780. default:
  2781. if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
  2782. rpc_restart_call_prepare(task);
  2783. goto out_release;
  2784. }
  2785. }
  2786. nfs_clear_open_stateid(state, &calldata->arg.stateid,
  2787. res_stateid, calldata->arg.fmode);
  2788. out_release:
  2789. nfs_release_seqid(calldata->arg.seqid);
  2790. nfs_refresh_inode(calldata->inode, calldata->res.fattr);
  2791. dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
  2792. }
  2793. static void nfs4_close_prepare(struct rpc_task *task, void *data)
  2794. {
  2795. struct nfs4_closedata *calldata = data;
  2796. struct nfs4_state *state = calldata->state;
  2797. struct inode *inode = calldata->inode;
  2798. bool is_rdonly, is_wronly, is_rdwr;
  2799. int call_close = 0;
  2800. dprintk("%s: begin!\n", __func__);
  2801. if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
  2802. goto out_wait;
  2803. task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
  2804. spin_lock(&state->owner->so_lock);
  2805. is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
  2806. is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
  2807. is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
  2808. nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
  2809. /* Calculate the change in open mode */
  2810. calldata->arg.fmode = 0;
  2811. if (state->n_rdwr == 0) {
  2812. if (state->n_rdonly == 0)
  2813. call_close |= is_rdonly;
  2814. else if (is_rdonly)
  2815. calldata->arg.fmode |= FMODE_READ;
  2816. if (state->n_wronly == 0)
  2817. call_close |= is_wronly;
  2818. else if (is_wronly)
  2819. calldata->arg.fmode |= FMODE_WRITE;
  2820. if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
  2821. call_close |= is_rdwr;
  2822. } else if (is_rdwr)
  2823. calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
  2824. if (!nfs4_valid_open_stateid(state) ||
  2825. test_bit(NFS_OPEN_STATE, &state->flags) == 0)
  2826. call_close = 0;
  2827. spin_unlock(&state->owner->so_lock);
  2828. if (!call_close) {
  2829. /* Note: exit _without_ calling nfs4_close_done */
  2830. goto out_no_action;
  2831. }
  2832. if (nfs4_wait_on_layoutreturn(inode, task)) {
  2833. nfs_release_seqid(calldata->arg.seqid);
  2834. goto out_wait;
  2835. }
  2836. if (calldata->arg.fmode == 0)
  2837. task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
  2838. if (calldata->roc)
  2839. pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
  2840. calldata->arg.share_access =
  2841. nfs4_map_atomic_open_share(NFS_SERVER(inode),
  2842. calldata->arg.fmode, 0);
  2843. nfs_fattr_init(calldata->res.fattr);
  2844. calldata->timestamp = jiffies;
  2845. if (nfs4_setup_sequence(NFS_SERVER(inode),
  2846. &calldata->arg.seq_args,
  2847. &calldata->res.seq_res,
  2848. task) != 0)
  2849. nfs_release_seqid(calldata->arg.seqid);
  2850. dprintk("%s: done!\n", __func__);
  2851. return;
  2852. out_no_action:
  2853. task->tk_action = NULL;
  2854. out_wait:
  2855. nfs4_sequence_done(task, &calldata->res.seq_res);
  2856. }
  2857. static const struct rpc_call_ops nfs4_close_ops = {
  2858. .rpc_call_prepare = nfs4_close_prepare,
  2859. .rpc_call_done = nfs4_close_done,
  2860. .rpc_release = nfs4_free_closedata,
  2861. };
  2862. static bool nfs4_roc(struct inode *inode)
  2863. {
  2864. if (!nfs_have_layout(inode))
  2865. return false;
  2866. return pnfs_roc(inode);
  2867. }
  2868. /*
  2869. * It is possible for data to be read/written from a mem-mapped file
  2870. * after the sys_close call (which hits the vfs layer as a flush).
  2871. * This means that we can't safely call nfsv4 close on a file until
  2872. * the inode is cleared. This in turn means that we are not good
  2873. * NFSv4 citizens - we do not indicate to the server to update the file's
  2874. * share state even when we are done with one of the three share
  2875. * stateid's in the inode.
  2876. *
  2877. * NOTE: Caller must be holding the sp->so_owner semaphore!
  2878. */
  2879. int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
  2880. {
  2881. struct nfs_server *server = NFS_SERVER(state->inode);
  2882. struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
  2883. struct nfs4_closedata *calldata;
  2884. struct nfs4_state_owner *sp = state->owner;
  2885. struct rpc_task *task;
  2886. struct rpc_message msg = {
  2887. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
  2888. .rpc_cred = state->owner->so_cred,
  2889. };
  2890. struct rpc_task_setup task_setup_data = {
  2891. .rpc_client = server->client,
  2892. .rpc_message = &msg,
  2893. .callback_ops = &nfs4_close_ops,
  2894. .workqueue = nfsiod_workqueue,
  2895. .flags = RPC_TASK_ASYNC,
  2896. };
  2897. int status = -ENOMEM;
  2898. nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
  2899. &task_setup_data.rpc_client, &msg);
  2900. calldata = kzalloc(sizeof(*calldata), gfp_mask);
  2901. if (calldata == NULL)
  2902. goto out;
  2903. nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
  2904. calldata->inode = state->inode;
  2905. calldata->state = state;
  2906. calldata->arg.fh = NFS_FH(state->inode);
  2907. /* Serialization for the sequence id */
  2908. alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
  2909. calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
  2910. if (IS_ERR(calldata->arg.seqid))
  2911. goto out_free_calldata;
  2912. calldata->arg.fmode = 0;
  2913. calldata->arg.bitmask = server->cache_consistency_bitmask;
  2914. calldata->res.fattr = &calldata->fattr;
  2915. calldata->res.seqid = calldata->arg.seqid;
  2916. calldata->res.server = server;
  2917. calldata->roc = nfs4_roc(state->inode);
  2918. nfs_sb_active(calldata->inode->i_sb);
  2919. msg.rpc_argp = &calldata->arg;
  2920. msg.rpc_resp = &calldata->res;
  2921. task_setup_data.callback_data = calldata;
  2922. task = rpc_run_task(&task_setup_data);
  2923. if (IS_ERR(task))
  2924. return PTR_ERR(task);
  2925. status = 0;
  2926. if (wait)
  2927. status = rpc_wait_for_completion_task(task);
  2928. rpc_put_task(task);
  2929. return status;
  2930. out_free_calldata:
  2931. kfree(calldata);
  2932. out:
  2933. nfs4_put_open_state(state);
  2934. nfs4_put_state_owner(sp);
  2935. return status;
  2936. }
  2937. static struct inode *
  2938. nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
  2939. int open_flags, struct iattr *attr, int *opened)
  2940. {
  2941. struct nfs4_state *state;
  2942. struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
  2943. label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
  2944. /* Protect against concurrent sillydeletes */
  2945. state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
  2946. nfs4_label_release_security(label);
  2947. if (IS_ERR(state))
  2948. return ERR_CAST(state);
  2949. return state->inode;
  2950. }
  2951. static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
  2952. {
  2953. if (ctx->state == NULL)
  2954. return;
  2955. if (is_sync)
  2956. nfs4_close_sync(ctx->state, ctx->mode);
  2957. else
  2958. nfs4_close_state(ctx->state, ctx->mode);
  2959. }
  2960. #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
  2961. #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
  2962. #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
  2963. static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
  2964. {
  2965. u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
  2966. struct nfs4_server_caps_arg args = {
  2967. .fhandle = fhandle,
  2968. .bitmask = bitmask,
  2969. };
  2970. struct nfs4_server_caps_res res = {};
  2971. struct rpc_message msg = {
  2972. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
  2973. .rpc_argp = &args,
  2974. .rpc_resp = &res,
  2975. };
  2976. int status;
  2977. int i;
  2978. bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
  2979. FATTR4_WORD0_FH_EXPIRE_TYPE |
  2980. FATTR4_WORD0_LINK_SUPPORT |
  2981. FATTR4_WORD0_SYMLINK_SUPPORT |
  2982. FATTR4_WORD0_ACLSUPPORT;
  2983. if (minorversion)
  2984. bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
  2985. status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  2986. if (status == 0) {
  2987. /* Sanity check the server answers */
  2988. switch (minorversion) {
  2989. case 0:
  2990. res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
  2991. res.attr_bitmask[2] = 0;
  2992. break;
  2993. case 1:
  2994. res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
  2995. break;
  2996. case 2:
  2997. res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
  2998. }
  2999. memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
  3000. server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
  3001. NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
  3002. NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
  3003. NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
  3004. NFS_CAP_CTIME|NFS_CAP_MTIME|
  3005. NFS_CAP_SECURITY_LABEL);
  3006. if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
  3007. res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
  3008. server->caps |= NFS_CAP_ACLS;
  3009. if (res.has_links != 0)
  3010. server->caps |= NFS_CAP_HARDLINKS;
  3011. if (res.has_symlinks != 0)
  3012. server->caps |= NFS_CAP_SYMLINKS;
  3013. if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
  3014. server->caps |= NFS_CAP_FILEID;
  3015. if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
  3016. server->caps |= NFS_CAP_MODE;
  3017. if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
  3018. server->caps |= NFS_CAP_NLINK;
  3019. if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
  3020. server->caps |= NFS_CAP_OWNER;
  3021. if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
  3022. server->caps |= NFS_CAP_OWNER_GROUP;
  3023. if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
  3024. server->caps |= NFS_CAP_ATIME;
  3025. if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
  3026. server->caps |= NFS_CAP_CTIME;
  3027. if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
  3028. server->caps |= NFS_CAP_MTIME;
  3029. #ifdef CONFIG_NFS_V4_SECURITY_LABEL
  3030. if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
  3031. server->caps |= NFS_CAP_SECURITY_LABEL;
  3032. #endif
  3033. memcpy(server->attr_bitmask_nl, res.attr_bitmask,
  3034. sizeof(server->attr_bitmask));
  3035. server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
  3036. memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
  3037. server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
  3038. server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
  3039. server->cache_consistency_bitmask[2] = 0;
  3040. /* Avoid a regression due to buggy server */
  3041. for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
  3042. res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
  3043. memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
  3044. sizeof(server->exclcreat_bitmask));
  3045. server->acl_bitmask = res.acl_bitmask;
  3046. server->fh_expire_type = res.fh_expire_type;
  3047. }
  3048. return status;
  3049. }
  3050. int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
  3051. {
  3052. struct nfs4_exception exception = { };
  3053. int err;
  3054. do {
  3055. err = nfs4_handle_exception(server,
  3056. _nfs4_server_capabilities(server, fhandle),
  3057. &exception);
  3058. } while (exception.retry);
  3059. return err;
  3060. }
  3061. static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
  3062. struct nfs_fsinfo *info)
  3063. {
  3064. u32 bitmask[3];
  3065. struct nfs4_lookup_root_arg args = {
  3066. .bitmask = bitmask,
  3067. };
  3068. struct nfs4_lookup_res res = {
  3069. .server = server,
  3070. .fattr = info->fattr,
  3071. .fh = fhandle,
  3072. };
  3073. struct rpc_message msg = {
  3074. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
  3075. .rpc_argp = &args,
  3076. .rpc_resp = &res,
  3077. };
  3078. bitmask[0] = nfs4_fattr_bitmap[0];
  3079. bitmask[1] = nfs4_fattr_bitmap[1];
  3080. /*
  3081. * Process the label in the upcoming getfattr
  3082. */
  3083. bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
  3084. nfs_fattr_init(info->fattr);
  3085. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  3086. }
  3087. static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
  3088. struct nfs_fsinfo *info)
  3089. {
  3090. struct nfs4_exception exception = { };
  3091. int err;
  3092. do {
  3093. err = _nfs4_lookup_root(server, fhandle, info);
  3094. trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
  3095. switch (err) {
  3096. case 0:
  3097. case -NFS4ERR_WRONGSEC:
  3098. goto out;
  3099. default:
  3100. err = nfs4_handle_exception(server, err, &exception);
  3101. }
  3102. } while (exception.retry);
  3103. out:
  3104. return err;
  3105. }
  3106. static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
  3107. struct nfs_fsinfo *info, rpc_authflavor_t flavor)
  3108. {
  3109. struct rpc_auth_create_args auth_args = {
  3110. .pseudoflavor = flavor,
  3111. };
  3112. struct rpc_auth *auth;
  3113. int ret;
  3114. auth = rpcauth_create(&auth_args, server->client);
  3115. if (IS_ERR(auth)) {
  3116. ret = -EACCES;
  3117. goto out;
  3118. }
  3119. ret = nfs4_lookup_root(server, fhandle, info);
  3120. out:
  3121. return ret;
  3122. }
  3123. /*
  3124. * Retry pseudoroot lookup with various security flavors. We do this when:
  3125. *
  3126. * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
  3127. * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
  3128. *
  3129. * Returns zero on success, or a negative NFS4ERR value, or a
  3130. * negative errno value.
  3131. */
  3132. static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
  3133. struct nfs_fsinfo *info)
  3134. {
  3135. /* Per 3530bis 15.33.5 */
  3136. static const rpc_authflavor_t flav_array[] = {
  3137. RPC_AUTH_GSS_KRB5P,
  3138. RPC_AUTH_GSS_KRB5I,
  3139. RPC_AUTH_GSS_KRB5,
  3140. RPC_AUTH_UNIX, /* courtesy */
  3141. RPC_AUTH_NULL,
  3142. };
  3143. int status = -EPERM;
  3144. size_t i;
  3145. if (server->auth_info.flavor_len > 0) {
  3146. /* try each flavor specified by user */
  3147. for (i = 0; i < server->auth_info.flavor_len; i++) {
  3148. status = nfs4_lookup_root_sec(server, fhandle, info,
  3149. server->auth_info.flavors[i]);
  3150. if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
  3151. continue;
  3152. break;
  3153. }
  3154. } else {
  3155. /* no flavors specified by user, try default list */
  3156. for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
  3157. status = nfs4_lookup_root_sec(server, fhandle, info,
  3158. flav_array[i]);
  3159. if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
  3160. continue;
  3161. break;
  3162. }
  3163. }
  3164. /*
  3165. * -EACCESS could mean that the user doesn't have correct permissions
  3166. * to access the mount. It could also mean that we tried to mount
  3167. * with a gss auth flavor, but rpc.gssd isn't running. Either way,
  3168. * existing mount programs don't handle -EACCES very well so it should
  3169. * be mapped to -EPERM instead.
  3170. */
  3171. if (status == -EACCES)
  3172. status = -EPERM;
  3173. return status;
  3174. }
  3175. /**
  3176. * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
  3177. * @server: initialized nfs_server handle
  3178. * @fhandle: we fill in the pseudo-fs root file handle
  3179. * @info: we fill in an FSINFO struct
  3180. * @auth_probe: probe the auth flavours
  3181. *
  3182. * Returns zero on success, or a negative errno.
  3183. */
  3184. int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
  3185. struct nfs_fsinfo *info,
  3186. bool auth_probe)
  3187. {
  3188. int status = 0;
  3189. if (!auth_probe)
  3190. status = nfs4_lookup_root(server, fhandle, info);
  3191. if (auth_probe || status == NFS4ERR_WRONGSEC)
  3192. status = server->nfs_client->cl_mvops->find_root_sec(server,
  3193. fhandle, info);
  3194. if (status == 0)
  3195. status = nfs4_server_capabilities(server, fhandle);
  3196. if (status == 0)
  3197. status = nfs4_do_fsinfo(server, fhandle, info);
  3198. return nfs4_map_errors(status);
  3199. }
  3200. static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
  3201. struct nfs_fsinfo *info)
  3202. {
  3203. int error;
  3204. struct nfs_fattr *fattr = info->fattr;
  3205. struct nfs4_label *label = NULL;
  3206. error = nfs4_server_capabilities(server, mntfh);
  3207. if (error < 0) {
  3208. dprintk("nfs4_get_root: getcaps error = %d\n", -error);
  3209. return error;
  3210. }
  3211. label = nfs4_label_alloc(server, GFP_KERNEL);
  3212. if (IS_ERR(label))
  3213. return PTR_ERR(label);
  3214. error = nfs4_proc_getattr(server, mntfh, fattr, label);
  3215. if (error < 0) {
  3216. dprintk("nfs4_get_root: getattr error = %d\n", -error);
  3217. goto err_free_label;
  3218. }
  3219. if (fattr->valid & NFS_ATTR_FATTR_FSID &&
  3220. !nfs_fsid_equal(&server->fsid, &fattr->fsid))
  3221. memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
  3222. err_free_label:
  3223. nfs4_label_free(label);
  3224. return error;
  3225. }
  3226. /*
  3227. * Get locations and (maybe) other attributes of a referral.
  3228. * Note that we'll actually follow the referral later when
  3229. * we detect fsid mismatch in inode revalidation
  3230. */
  3231. static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
  3232. const struct qstr *name, struct nfs_fattr *fattr,
  3233. struct nfs_fh *fhandle)
  3234. {
  3235. int status = -ENOMEM;
  3236. struct page *page = NULL;
  3237. struct nfs4_fs_locations *locations = NULL;
  3238. page = alloc_page(GFP_KERNEL);
  3239. if (page == NULL)
  3240. goto out;
  3241. locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
  3242. if (locations == NULL)
  3243. goto out;
  3244. status = nfs4_proc_fs_locations(client, dir, name, locations, page);
  3245. if (status != 0)
  3246. goto out;
  3247. /*
  3248. * If the fsid didn't change, this is a migration event, not a
  3249. * referral. Cause us to drop into the exception handler, which
  3250. * will kick off migration recovery.
  3251. */
  3252. if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
  3253. dprintk("%s: server did not return a different fsid for"
  3254. " a referral at %s\n", __func__, name->name);
  3255. status = -NFS4ERR_MOVED;
  3256. goto out;
  3257. }
  3258. /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
  3259. nfs_fixup_referral_attributes(&locations->fattr);
  3260. /* replace the lookup nfs_fattr with the locations nfs_fattr */
  3261. memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
  3262. memset(fhandle, 0, sizeof(struct nfs_fh));
  3263. out:
  3264. if (page)
  3265. __free_page(page);
  3266. kfree(locations);
  3267. return status;
  3268. }
  3269. static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
  3270. struct nfs_fattr *fattr, struct nfs4_label *label)
  3271. {
  3272. struct nfs4_getattr_arg args = {
  3273. .fh = fhandle,
  3274. .bitmask = server->attr_bitmask,
  3275. };
  3276. struct nfs4_getattr_res res = {
  3277. .fattr = fattr,
  3278. .label = label,
  3279. .server = server,
  3280. };
  3281. struct rpc_message msg = {
  3282. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
  3283. .rpc_argp = &args,
  3284. .rpc_resp = &res,
  3285. };
  3286. args.bitmask = nfs4_bitmask(server, label);
  3287. nfs_fattr_init(fattr);
  3288. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  3289. }
  3290. static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
  3291. struct nfs_fattr *fattr, struct nfs4_label *label)
  3292. {
  3293. struct nfs4_exception exception = { };
  3294. int err;
  3295. do {
  3296. err = _nfs4_proc_getattr(server, fhandle, fattr, label);
  3297. trace_nfs4_getattr(server, fhandle, fattr, err);
  3298. err = nfs4_handle_exception(server, err,
  3299. &exception);
  3300. } while (exception.retry);
  3301. return err;
  3302. }
  3303. /*
  3304. * The file is not closed if it is opened due to the a request to change
  3305. * the size of the file. The open call will not be needed once the
  3306. * VFS layer lookup-intents are implemented.
  3307. *
  3308. * Close is called when the inode is destroyed.
  3309. * If we haven't opened the file for O_WRONLY, we
  3310. * need to in the size_change case to obtain a stateid.
  3311. *
  3312. * Got race?
  3313. * Because OPEN is always done by name in nfsv4, it is
  3314. * possible that we opened a different file by the same
  3315. * name. We can recognize this race condition, but we
  3316. * can't do anything about it besides returning an error.
  3317. *
  3318. * This will be fixed with VFS changes (lookup-intent).
  3319. */
  3320. static int
  3321. nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
  3322. struct iattr *sattr)
  3323. {
  3324. struct inode *inode = d_inode(dentry);
  3325. struct rpc_cred *cred = NULL;
  3326. struct nfs4_state *state = NULL;
  3327. struct nfs4_label *label = NULL;
  3328. int status;
  3329. if (pnfs_ld_layoutret_on_setattr(inode) &&
  3330. sattr->ia_valid & ATTR_SIZE &&
  3331. sattr->ia_size < i_size_read(inode))
  3332. pnfs_commit_and_return_layout(inode);
  3333. nfs_fattr_init(fattr);
  3334. /* Deal with open(O_TRUNC) */
  3335. if (sattr->ia_valid & ATTR_OPEN)
  3336. sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
  3337. /* Optimization: if the end result is no change, don't RPC */
  3338. if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
  3339. return 0;
  3340. /* Search for an existing open(O_WRITE) file */
  3341. if (sattr->ia_valid & ATTR_FILE) {
  3342. struct nfs_open_context *ctx;
  3343. ctx = nfs_file_open_context(sattr->ia_file);
  3344. if (ctx) {
  3345. cred = ctx->cred;
  3346. state = ctx->state;
  3347. }
  3348. }
  3349. label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
  3350. if (IS_ERR(label))
  3351. return PTR_ERR(label);
  3352. status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
  3353. if (status == 0) {
  3354. nfs_setattr_update_inode(inode, sattr, fattr);
  3355. nfs_setsecurity(inode, fattr, label);
  3356. }
  3357. nfs4_label_free(label);
  3358. return status;
  3359. }
  3360. static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
  3361. const struct qstr *name, struct nfs_fh *fhandle,
  3362. struct nfs_fattr *fattr, struct nfs4_label *label)
  3363. {
  3364. struct nfs_server *server = NFS_SERVER(dir);
  3365. int status;
  3366. struct nfs4_lookup_arg args = {
  3367. .bitmask = server->attr_bitmask,
  3368. .dir_fh = NFS_FH(dir),
  3369. .name = name,
  3370. };
  3371. struct nfs4_lookup_res res = {
  3372. .server = server,
  3373. .fattr = fattr,
  3374. .label = label,
  3375. .fh = fhandle,
  3376. };
  3377. struct rpc_message msg = {
  3378. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
  3379. .rpc_argp = &args,
  3380. .rpc_resp = &res,
  3381. };
  3382. args.bitmask = nfs4_bitmask(server, label);
  3383. nfs_fattr_init(fattr);
  3384. dprintk("NFS call lookup %s\n", name->name);
  3385. status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
  3386. dprintk("NFS reply lookup: %d\n", status);
  3387. return status;
  3388. }
  3389. static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
  3390. {
  3391. fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
  3392. NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
  3393. fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
  3394. fattr->nlink = 2;
  3395. }
  3396. static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
  3397. const struct qstr *name, struct nfs_fh *fhandle,
  3398. struct nfs_fattr *fattr, struct nfs4_label *label)
  3399. {
  3400. struct nfs4_exception exception = { };
  3401. struct rpc_clnt *client = *clnt;
  3402. int err;
  3403. do {
  3404. err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
  3405. trace_nfs4_lookup(dir, name, err);
  3406. switch (err) {
  3407. case -NFS4ERR_BADNAME:
  3408. err = -ENOENT;
  3409. goto out;
  3410. case -NFS4ERR_MOVED:
  3411. err = nfs4_get_referral(client, dir, name, fattr, fhandle);
  3412. if (err == -NFS4ERR_MOVED)
  3413. err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
  3414. goto out;
  3415. case -NFS4ERR_WRONGSEC:
  3416. err = -EPERM;
  3417. if (client != *clnt)
  3418. goto out;
  3419. client = nfs4_negotiate_security(client, dir, name);
  3420. if (IS_ERR(client))
  3421. return PTR_ERR(client);
  3422. exception.retry = 1;
  3423. break;
  3424. default:
  3425. err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
  3426. }
  3427. } while (exception.retry);
  3428. out:
  3429. if (err == 0)
  3430. *clnt = client;
  3431. else if (client != *clnt)
  3432. rpc_shutdown_client(client);
  3433. return err;
  3434. }
  3435. static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
  3436. struct nfs_fh *fhandle, struct nfs_fattr *fattr,
  3437. struct nfs4_label *label)
  3438. {
  3439. int status;
  3440. struct rpc_clnt *client = NFS_CLIENT(dir);
  3441. status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
  3442. if (client != NFS_CLIENT(dir)) {
  3443. rpc_shutdown_client(client);
  3444. nfs_fixup_secinfo_attributes(fattr);
  3445. }
  3446. return status;
  3447. }
  3448. struct rpc_clnt *
  3449. nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
  3450. struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  3451. {
  3452. struct rpc_clnt *client = NFS_CLIENT(dir);
  3453. int status;
  3454. status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
  3455. if (status < 0)
  3456. return ERR_PTR(status);
  3457. return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
  3458. }
  3459. static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
  3460. {
  3461. struct nfs_server *server = NFS_SERVER(inode);
  3462. struct nfs4_accessargs args = {
  3463. .fh = NFS_FH(inode),
  3464. .bitmask = server->cache_consistency_bitmask,
  3465. };
  3466. struct nfs4_accessres res = {
  3467. .server = server,
  3468. };
  3469. struct rpc_message msg = {
  3470. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
  3471. .rpc_argp = &args,
  3472. .rpc_resp = &res,
  3473. .rpc_cred = entry->cred,
  3474. };
  3475. int mode = entry->mask;
  3476. int status = 0;
  3477. /*
  3478. * Determine which access bits we want to ask for...
  3479. */
  3480. if (mode & MAY_READ)
  3481. args.access |= NFS4_ACCESS_READ;
  3482. if (S_ISDIR(inode->i_mode)) {
  3483. if (mode & MAY_WRITE)
  3484. args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
  3485. if (mode & MAY_EXEC)
  3486. args.access |= NFS4_ACCESS_LOOKUP;
  3487. } else {
  3488. if (mode & MAY_WRITE)
  3489. args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
  3490. if (mode & MAY_EXEC)
  3491. args.access |= NFS4_ACCESS_EXECUTE;
  3492. }
  3493. res.fattr = nfs_alloc_fattr();
  3494. if (res.fattr == NULL)
  3495. return -ENOMEM;
  3496. status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  3497. if (!status) {
  3498. nfs_access_set_mask(entry, res.access);
  3499. nfs_refresh_inode(inode, res.fattr);
  3500. }
  3501. nfs_free_fattr(res.fattr);
  3502. return status;
  3503. }
  3504. static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
  3505. {
  3506. struct nfs4_exception exception = { };
  3507. int err;
  3508. do {
  3509. err = _nfs4_proc_access(inode, entry);
  3510. trace_nfs4_access(inode, err);
  3511. err = nfs4_handle_exception(NFS_SERVER(inode), err,
  3512. &exception);
  3513. } while (exception.retry);
  3514. return err;
  3515. }
  3516. /*
  3517. * TODO: For the time being, we don't try to get any attributes
  3518. * along with any of the zero-copy operations READ, READDIR,
  3519. * READLINK, WRITE.
  3520. *
  3521. * In the case of the first three, we want to put the GETATTR
  3522. * after the read-type operation -- this is because it is hard
  3523. * to predict the length of a GETATTR response in v4, and thus
  3524. * align the READ data correctly. This means that the GETATTR
  3525. * may end up partially falling into the page cache, and we should
  3526. * shift it into the 'tail' of the xdr_buf before processing.
  3527. * To do this efficiently, we need to know the total length
  3528. * of data received, which doesn't seem to be available outside
  3529. * of the RPC layer.
  3530. *
  3531. * In the case of WRITE, we also want to put the GETATTR after
  3532. * the operation -- in this case because we want to make sure
  3533. * we get the post-operation mtime and size.
  3534. *
  3535. * Both of these changes to the XDR layer would in fact be quite
  3536. * minor, but I decided to leave them for a subsequent patch.
  3537. */
  3538. static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
  3539. unsigned int pgbase, unsigned int pglen)
  3540. {
  3541. struct nfs4_readlink args = {
  3542. .fh = NFS_FH(inode),
  3543. .pgbase = pgbase,
  3544. .pglen = pglen,
  3545. .pages = &page,
  3546. };
  3547. struct nfs4_readlink_res res;
  3548. struct rpc_message msg = {
  3549. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
  3550. .rpc_argp = &args,
  3551. .rpc_resp = &res,
  3552. };
  3553. return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
  3554. }
  3555. static int nfs4_proc_readlink(struct inode *inode, struct page *page,
  3556. unsigned int pgbase, unsigned int pglen)
  3557. {
  3558. struct nfs4_exception exception = { };
  3559. int err;
  3560. do {
  3561. err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
  3562. trace_nfs4_readlink(inode, err);
  3563. err = nfs4_handle_exception(NFS_SERVER(inode), err,
  3564. &exception);
  3565. } while (exception.retry);
  3566. return err;
  3567. }
  3568. /*
  3569. * This is just for mknod. open(O_CREAT) will always do ->open_context().
  3570. */
  3571. static int
  3572. nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
  3573. int flags)
  3574. {
  3575. struct nfs4_label l, *ilabel = NULL;
  3576. struct nfs_open_context *ctx;
  3577. struct nfs4_state *state;
  3578. int status = 0;
  3579. ctx = alloc_nfs_open_context(dentry, FMODE_READ);
  3580. if (IS_ERR(ctx))
  3581. return PTR_ERR(ctx);
  3582. ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
  3583. sattr->ia_mode &= ~current_umask();
  3584. state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
  3585. if (IS_ERR(state)) {
  3586. status = PTR_ERR(state);
  3587. goto out;
  3588. }
  3589. out:
  3590. nfs4_label_release_security(ilabel);
  3591. put_nfs_open_context(ctx);
  3592. return status;
  3593. }
  3594. static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
  3595. {
  3596. struct nfs_server *server = NFS_SERVER(dir);
  3597. struct nfs_removeargs args = {
  3598. .fh = NFS_FH(dir),
  3599. .name = *name,
  3600. };
  3601. struct nfs_removeres res = {
  3602. .server = server,
  3603. };
  3604. struct rpc_message msg = {
  3605. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
  3606. .rpc_argp = &args,
  3607. .rpc_resp = &res,
  3608. };
  3609. int status;
  3610. status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
  3611. if (status == 0)
  3612. update_changeattr(dir, &res.cinfo);
  3613. return status;
  3614. }
  3615. static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
  3616. {
  3617. struct nfs4_exception exception = { };
  3618. int err;
  3619. do {
  3620. err = _nfs4_proc_remove(dir, name);
  3621. trace_nfs4_remove(dir, name, err);
  3622. err = nfs4_handle_exception(NFS_SERVER(dir), err,
  3623. &exception);
  3624. } while (exception.retry);
  3625. return err;
  3626. }
  3627. static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
  3628. {
  3629. struct nfs_server *server = NFS_SERVER(dir);
  3630. struct nfs_removeargs *args = msg->rpc_argp;
  3631. struct nfs_removeres *res = msg->rpc_resp;
  3632. res->server = server;
  3633. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
  3634. nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
  3635. nfs_fattr_init(res->dir_attr);
  3636. }
  3637. static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
  3638. {
  3639. nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
  3640. &data->args.seq_args,
  3641. &data->res.seq_res,
  3642. task);
  3643. }
  3644. static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
  3645. {
  3646. struct nfs_unlinkdata *data = task->tk_calldata;
  3647. struct nfs_removeres *res = &data->res;
  3648. if (!nfs4_sequence_done(task, &res->seq_res))
  3649. return 0;
  3650. if (nfs4_async_handle_error(task, res->server, NULL,
  3651. &data->timeout) == -EAGAIN)
  3652. return 0;
  3653. update_changeattr(dir, &res->cinfo);
  3654. return 1;
  3655. }
  3656. static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
  3657. {
  3658. struct nfs_server *server = NFS_SERVER(dir);
  3659. struct nfs_renameargs *arg = msg->rpc_argp;
  3660. struct nfs_renameres *res = msg->rpc_resp;
  3661. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
  3662. res->server = server;
  3663. nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
  3664. }
  3665. static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
  3666. {
  3667. nfs4_setup_sequence(NFS_SERVER(data->old_dir),
  3668. &data->args.seq_args,
  3669. &data->res.seq_res,
  3670. task);
  3671. }
  3672. static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
  3673. struct inode *new_dir)
  3674. {
  3675. struct nfs_renamedata *data = task->tk_calldata;
  3676. struct nfs_renameres *res = &data->res;
  3677. if (!nfs4_sequence_done(task, &res->seq_res))
  3678. return 0;
  3679. if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
  3680. return 0;
  3681. update_changeattr(old_dir, &res->old_cinfo);
  3682. update_changeattr(new_dir, &res->new_cinfo);
  3683. return 1;
  3684. }
  3685. static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
  3686. {
  3687. struct nfs_server *server = NFS_SERVER(inode);
  3688. struct nfs4_link_arg arg = {
  3689. .fh = NFS_FH(inode),
  3690. .dir_fh = NFS_FH(dir),
  3691. .name = name,
  3692. .bitmask = server->attr_bitmask,
  3693. };
  3694. struct nfs4_link_res res = {
  3695. .server = server,
  3696. .label = NULL,
  3697. };
  3698. struct rpc_message msg = {
  3699. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
  3700. .rpc_argp = &arg,
  3701. .rpc_resp = &res,
  3702. };
  3703. int status = -ENOMEM;
  3704. res.fattr = nfs_alloc_fattr();
  3705. if (res.fattr == NULL)
  3706. goto out;
  3707. res.label = nfs4_label_alloc(server, GFP_KERNEL);
  3708. if (IS_ERR(res.label)) {
  3709. status = PTR_ERR(res.label);
  3710. goto out;
  3711. }
  3712. arg.bitmask = nfs4_bitmask(server, res.label);
  3713. status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
  3714. if (!status) {
  3715. update_changeattr(dir, &res.cinfo);
  3716. status = nfs_post_op_update_inode(inode, res.fattr);
  3717. if (!status)
  3718. nfs_setsecurity(inode, res.fattr, res.label);
  3719. }
  3720. nfs4_label_free(res.label);
  3721. out:
  3722. nfs_free_fattr(res.fattr);
  3723. return status;
  3724. }
  3725. static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
  3726. {
  3727. struct nfs4_exception exception = { };
  3728. int err;
  3729. do {
  3730. err = nfs4_handle_exception(NFS_SERVER(inode),
  3731. _nfs4_proc_link(inode, dir, name),
  3732. &exception);
  3733. } while (exception.retry);
  3734. return err;
  3735. }
  3736. struct nfs4_createdata {
  3737. struct rpc_message msg;
  3738. struct nfs4_create_arg arg;
  3739. struct nfs4_create_res res;
  3740. struct nfs_fh fh;
  3741. struct nfs_fattr fattr;
  3742. struct nfs4_label *label;
  3743. };
  3744. static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
  3745. const struct qstr *name, struct iattr *sattr, u32 ftype)
  3746. {
  3747. struct nfs4_createdata *data;
  3748. data = kzalloc(sizeof(*data), GFP_KERNEL);
  3749. if (data != NULL) {
  3750. struct nfs_server *server = NFS_SERVER(dir);
  3751. data->label = nfs4_label_alloc(server, GFP_KERNEL);
  3752. if (IS_ERR(data->label))
  3753. goto out_free;
  3754. data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
  3755. data->msg.rpc_argp = &data->arg;
  3756. data->msg.rpc_resp = &data->res;
  3757. data->arg.dir_fh = NFS_FH(dir);
  3758. data->arg.server = server;
  3759. data->arg.name = name;
  3760. data->arg.attrs = sattr;
  3761. data->arg.ftype = ftype;
  3762. data->arg.bitmask = nfs4_bitmask(server, data->label);
  3763. data->res.server = server;
  3764. data->res.fh = &data->fh;
  3765. data->res.fattr = &data->fattr;
  3766. data->res.label = data->label;
  3767. nfs_fattr_init(data->res.fattr);
  3768. }
  3769. return data;
  3770. out_free:
  3771. kfree(data);
  3772. return NULL;
  3773. }
  3774. static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
  3775. {
  3776. int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
  3777. &data->arg.seq_args, &data->res.seq_res, 1);
  3778. if (status == 0) {
  3779. update_changeattr(dir, &data->res.dir_cinfo);
  3780. status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
  3781. }
  3782. return status;
  3783. }
  3784. static void nfs4_free_createdata(struct nfs4_createdata *data)
  3785. {
  3786. nfs4_label_free(data->label);
  3787. kfree(data);
  3788. }
  3789. static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
  3790. struct page *page, unsigned int len, struct iattr *sattr,
  3791. struct nfs4_label *label)
  3792. {
  3793. struct nfs4_createdata *data;
  3794. int status = -ENAMETOOLONG;
  3795. if (len > NFS4_MAXPATHLEN)
  3796. goto out;
  3797. status = -ENOMEM;
  3798. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
  3799. if (data == NULL)
  3800. goto out;
  3801. data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
  3802. data->arg.u.symlink.pages = &page;
  3803. data->arg.u.symlink.len = len;
  3804. data->arg.label = label;
  3805. status = nfs4_do_create(dir, dentry, data);
  3806. nfs4_free_createdata(data);
  3807. out:
  3808. return status;
  3809. }
  3810. static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
  3811. struct page *page, unsigned int len, struct iattr *sattr)
  3812. {
  3813. struct nfs4_exception exception = { };
  3814. struct nfs4_label l, *label = NULL;
  3815. int err;
  3816. label = nfs4_label_init_security(dir, dentry, sattr, &l);
  3817. do {
  3818. err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
  3819. trace_nfs4_symlink(dir, &dentry->d_name, err);
  3820. err = nfs4_handle_exception(NFS_SERVER(dir), err,
  3821. &exception);
  3822. } while (exception.retry);
  3823. nfs4_label_release_security(label);
  3824. return err;
  3825. }
  3826. static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
  3827. struct iattr *sattr, struct nfs4_label *label)
  3828. {
  3829. struct nfs4_createdata *data;
  3830. int status = -ENOMEM;
  3831. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
  3832. if (data == NULL)
  3833. goto out;
  3834. data->arg.label = label;
  3835. status = nfs4_do_create(dir, dentry, data);
  3836. nfs4_free_createdata(data);
  3837. out:
  3838. return status;
  3839. }
  3840. static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
  3841. struct iattr *sattr)
  3842. {
  3843. struct nfs4_exception exception = { };
  3844. struct nfs4_label l, *label = NULL;
  3845. int err;
  3846. label = nfs4_label_init_security(dir, dentry, sattr, &l);
  3847. sattr->ia_mode &= ~current_umask();
  3848. do {
  3849. err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
  3850. trace_nfs4_mkdir(dir, &dentry->d_name, err);
  3851. err = nfs4_handle_exception(NFS_SERVER(dir), err,
  3852. &exception);
  3853. } while (exception.retry);
  3854. nfs4_label_release_security(label);
  3855. return err;
  3856. }
  3857. static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
  3858. u64 cookie, struct page **pages, unsigned int count, int plus)
  3859. {
  3860. struct inode *dir = d_inode(dentry);
  3861. struct nfs4_readdir_arg args = {
  3862. .fh = NFS_FH(dir),
  3863. .pages = pages,
  3864. .pgbase = 0,
  3865. .count = count,
  3866. .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
  3867. .plus = plus,
  3868. };
  3869. struct nfs4_readdir_res res;
  3870. struct rpc_message msg = {
  3871. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
  3872. .rpc_argp = &args,
  3873. .rpc_resp = &res,
  3874. .rpc_cred = cred,
  3875. };
  3876. int status;
  3877. dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
  3878. dentry,
  3879. (unsigned long long)cookie);
  3880. nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
  3881. res.pgbase = args.pgbase;
  3882. status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
  3883. if (status >= 0) {
  3884. memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
  3885. status += args.pgbase;
  3886. }
  3887. nfs_invalidate_atime(dir);
  3888. dprintk("%s: returns %d\n", __func__, status);
  3889. return status;
  3890. }
  3891. static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
  3892. u64 cookie, struct page **pages, unsigned int count, int plus)
  3893. {
  3894. struct nfs4_exception exception = { };
  3895. int err;
  3896. do {
  3897. err = _nfs4_proc_readdir(dentry, cred, cookie,
  3898. pages, count, plus);
  3899. trace_nfs4_readdir(d_inode(dentry), err);
  3900. err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
  3901. &exception);
  3902. } while (exception.retry);
  3903. return err;
  3904. }
  3905. static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
  3906. struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
  3907. {
  3908. struct nfs4_createdata *data;
  3909. int mode = sattr->ia_mode;
  3910. int status = -ENOMEM;
  3911. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
  3912. if (data == NULL)
  3913. goto out;
  3914. if (S_ISFIFO(mode))
  3915. data->arg.ftype = NF4FIFO;
  3916. else if (S_ISBLK(mode)) {
  3917. data->arg.ftype = NF4BLK;
  3918. data->arg.u.device.specdata1 = MAJOR(rdev);
  3919. data->arg.u.device.specdata2 = MINOR(rdev);
  3920. }
  3921. else if (S_ISCHR(mode)) {
  3922. data->arg.ftype = NF4CHR;
  3923. data->arg.u.device.specdata1 = MAJOR(rdev);
  3924. data->arg.u.device.specdata2 = MINOR(rdev);
  3925. } else if (!S_ISSOCK(mode)) {
  3926. status = -EINVAL;
  3927. goto out_free;
  3928. }
  3929. data->arg.label = label;
  3930. status = nfs4_do_create(dir, dentry, data);
  3931. out_free:
  3932. nfs4_free_createdata(data);
  3933. out:
  3934. return status;
  3935. }
  3936. static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
  3937. struct iattr *sattr, dev_t rdev)
  3938. {
  3939. struct nfs4_exception exception = { };
  3940. struct nfs4_label l, *label = NULL;
  3941. int err;
  3942. label = nfs4_label_init_security(dir, dentry, sattr, &l);
  3943. sattr->ia_mode &= ~current_umask();
  3944. do {
  3945. err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
  3946. trace_nfs4_mknod(dir, &dentry->d_name, err);
  3947. err = nfs4_handle_exception(NFS_SERVER(dir), err,
  3948. &exception);
  3949. } while (exception.retry);
  3950. nfs4_label_release_security(label);
  3951. return err;
  3952. }
  3953. static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
  3954. struct nfs_fsstat *fsstat)
  3955. {
  3956. struct nfs4_statfs_arg args = {
  3957. .fh = fhandle,
  3958. .bitmask = server->attr_bitmask,
  3959. };
  3960. struct nfs4_statfs_res res = {
  3961. .fsstat = fsstat,
  3962. };
  3963. struct rpc_message msg = {
  3964. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
  3965. .rpc_argp = &args,
  3966. .rpc_resp = &res,
  3967. };
  3968. nfs_fattr_init(fsstat->fattr);
  3969. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  3970. }
  3971. static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
  3972. {
  3973. struct nfs4_exception exception = { };
  3974. int err;
  3975. do {
  3976. err = nfs4_handle_exception(server,
  3977. _nfs4_proc_statfs(server, fhandle, fsstat),
  3978. &exception);
  3979. } while (exception.retry);
  3980. return err;
  3981. }
  3982. static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
  3983. struct nfs_fsinfo *fsinfo)
  3984. {
  3985. struct nfs4_fsinfo_arg args = {
  3986. .fh = fhandle,
  3987. .bitmask = server->attr_bitmask,
  3988. };
  3989. struct nfs4_fsinfo_res res = {
  3990. .fsinfo = fsinfo,
  3991. };
  3992. struct rpc_message msg = {
  3993. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
  3994. .rpc_argp = &args,
  3995. .rpc_resp = &res,
  3996. };
  3997. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  3998. }
  3999. static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
  4000. {
  4001. struct nfs4_exception exception = { };
  4002. unsigned long now = jiffies;
  4003. int err;
  4004. do {
  4005. err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
  4006. trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
  4007. if (err == 0) {
  4008. nfs4_set_lease_period(server->nfs_client,
  4009. fsinfo->lease_time * HZ,
  4010. now);
  4011. break;
  4012. }
  4013. err = nfs4_handle_exception(server, err, &exception);
  4014. } while (exception.retry);
  4015. return err;
  4016. }
  4017. static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
  4018. {
  4019. int error;
  4020. nfs_fattr_init(fsinfo->fattr);
  4021. error = nfs4_do_fsinfo(server, fhandle, fsinfo);
  4022. if (error == 0) {
  4023. /* block layout checks this! */
  4024. server->pnfs_blksize = fsinfo->blksize;
  4025. set_pnfs_layoutdriver(server, fhandle, fsinfo);
  4026. }
  4027. return error;
  4028. }
  4029. static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
  4030. struct nfs_pathconf *pathconf)
  4031. {
  4032. struct nfs4_pathconf_arg args = {
  4033. .fh = fhandle,
  4034. .bitmask = server->attr_bitmask,
  4035. };
  4036. struct nfs4_pathconf_res res = {
  4037. .pathconf = pathconf,
  4038. };
  4039. struct rpc_message msg = {
  4040. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
  4041. .rpc_argp = &args,
  4042. .rpc_resp = &res,
  4043. };
  4044. /* None of the pathconf attributes are mandatory to implement */
  4045. if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
  4046. memset(pathconf, 0, sizeof(*pathconf));
  4047. return 0;
  4048. }
  4049. nfs_fattr_init(pathconf->fattr);
  4050. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  4051. }
  4052. static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
  4053. struct nfs_pathconf *pathconf)
  4054. {
  4055. struct nfs4_exception exception = { };
  4056. int err;
  4057. do {
  4058. err = nfs4_handle_exception(server,
  4059. _nfs4_proc_pathconf(server, fhandle, pathconf),
  4060. &exception);
  4061. } while (exception.retry);
  4062. return err;
  4063. }
  4064. int nfs4_set_rw_stateid(nfs4_stateid *stateid,
  4065. const struct nfs_open_context *ctx,
  4066. const struct nfs_lock_context *l_ctx,
  4067. fmode_t fmode)
  4068. {
  4069. const struct nfs_lockowner *lockowner = NULL;
  4070. if (l_ctx != NULL)
  4071. lockowner = &l_ctx->lockowner;
  4072. return nfs4_select_rw_stateid(ctx->state, fmode, lockowner, stateid, NULL);
  4073. }
  4074. EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
  4075. static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
  4076. const struct nfs_open_context *ctx,
  4077. const struct nfs_lock_context *l_ctx,
  4078. fmode_t fmode)
  4079. {
  4080. nfs4_stateid current_stateid;
  4081. /* If the current stateid represents a lost lock, then exit */
  4082. if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
  4083. return true;
  4084. return nfs4_stateid_match(stateid, &current_stateid);
  4085. }
  4086. static bool nfs4_error_stateid_expired(int err)
  4087. {
  4088. switch (err) {
  4089. case -NFS4ERR_DELEG_REVOKED:
  4090. case -NFS4ERR_ADMIN_REVOKED:
  4091. case -NFS4ERR_BAD_STATEID:
  4092. case -NFS4ERR_STALE_STATEID:
  4093. case -NFS4ERR_OLD_STATEID:
  4094. case -NFS4ERR_OPENMODE:
  4095. case -NFS4ERR_EXPIRED:
  4096. return true;
  4097. }
  4098. return false;
  4099. }
  4100. static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
  4101. {
  4102. struct nfs_server *server = NFS_SERVER(hdr->inode);
  4103. trace_nfs4_read(hdr, task->tk_status);
  4104. if (task->tk_status < 0) {
  4105. struct nfs4_exception exception = {
  4106. .inode = hdr->inode,
  4107. .state = hdr->args.context->state,
  4108. .stateid = &hdr->args.stateid,
  4109. };
  4110. task->tk_status = nfs4_async_handle_exception(task,
  4111. server, task->tk_status, &exception);
  4112. if (exception.retry) {
  4113. rpc_restart_call_prepare(task);
  4114. return -EAGAIN;
  4115. }
  4116. }
  4117. if (task->tk_status > 0)
  4118. renew_lease(server, hdr->timestamp);
  4119. return 0;
  4120. }
  4121. static bool nfs4_read_stateid_changed(struct rpc_task *task,
  4122. struct nfs_pgio_args *args)
  4123. {
  4124. if (!nfs4_error_stateid_expired(task->tk_status) ||
  4125. nfs4_stateid_is_current(&args->stateid,
  4126. args->context,
  4127. args->lock_context,
  4128. FMODE_READ))
  4129. return false;
  4130. rpc_restart_call_prepare(task);
  4131. return true;
  4132. }
  4133. static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
  4134. {
  4135. dprintk("--> %s\n", __func__);
  4136. if (!nfs4_sequence_done(task, &hdr->res.seq_res))
  4137. return -EAGAIN;
  4138. if (nfs4_read_stateid_changed(task, &hdr->args))
  4139. return -EAGAIN;
  4140. if (task->tk_status > 0)
  4141. nfs_invalidate_atime(hdr->inode);
  4142. return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
  4143. nfs4_read_done_cb(task, hdr);
  4144. }
  4145. static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
  4146. struct rpc_message *msg)
  4147. {
  4148. hdr->timestamp = jiffies;
  4149. if (!hdr->pgio_done_cb)
  4150. hdr->pgio_done_cb = nfs4_read_done_cb;
  4151. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
  4152. nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
  4153. }
  4154. static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
  4155. struct nfs_pgio_header *hdr)
  4156. {
  4157. if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
  4158. &hdr->args.seq_args,
  4159. &hdr->res.seq_res,
  4160. task))
  4161. return 0;
  4162. if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
  4163. hdr->args.lock_context,
  4164. hdr->rw_ops->rw_mode) == -EIO)
  4165. return -EIO;
  4166. if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
  4167. return -EIO;
  4168. return 0;
  4169. }
  4170. static int nfs4_write_done_cb(struct rpc_task *task,
  4171. struct nfs_pgio_header *hdr)
  4172. {
  4173. struct inode *inode = hdr->inode;
  4174. trace_nfs4_write(hdr, task->tk_status);
  4175. if (task->tk_status < 0) {
  4176. struct nfs4_exception exception = {
  4177. .inode = hdr->inode,
  4178. .state = hdr->args.context->state,
  4179. .stateid = &hdr->args.stateid,
  4180. };
  4181. task->tk_status = nfs4_async_handle_exception(task,
  4182. NFS_SERVER(inode), task->tk_status,
  4183. &exception);
  4184. if (exception.retry) {
  4185. rpc_restart_call_prepare(task);
  4186. return -EAGAIN;
  4187. }
  4188. }
  4189. if (task->tk_status >= 0) {
  4190. renew_lease(NFS_SERVER(inode), hdr->timestamp);
  4191. nfs_writeback_update_inode(hdr);
  4192. }
  4193. return 0;
  4194. }
  4195. static bool nfs4_write_stateid_changed(struct rpc_task *task,
  4196. struct nfs_pgio_args *args)
  4197. {
  4198. if (!nfs4_error_stateid_expired(task->tk_status) ||
  4199. nfs4_stateid_is_current(&args->stateid,
  4200. args->context,
  4201. args->lock_context,
  4202. FMODE_WRITE))
  4203. return false;
  4204. rpc_restart_call_prepare(task);
  4205. return true;
  4206. }
  4207. static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
  4208. {
  4209. if (!nfs4_sequence_done(task, &hdr->res.seq_res))
  4210. return -EAGAIN;
  4211. if (nfs4_write_stateid_changed(task, &hdr->args))
  4212. return -EAGAIN;
  4213. return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
  4214. nfs4_write_done_cb(task, hdr);
  4215. }
  4216. static
  4217. bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
  4218. {
  4219. /* Don't request attributes for pNFS or O_DIRECT writes */
  4220. if (hdr->ds_clp != NULL || hdr->dreq != NULL)
  4221. return false;
  4222. /* Otherwise, request attributes if and only if we don't hold
  4223. * a delegation
  4224. */
  4225. return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
  4226. }
  4227. static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
  4228. struct rpc_message *msg)
  4229. {
  4230. struct nfs_server *server = NFS_SERVER(hdr->inode);
  4231. if (!nfs4_write_need_cache_consistency_data(hdr)) {
  4232. hdr->args.bitmask = NULL;
  4233. hdr->res.fattr = NULL;
  4234. } else
  4235. hdr->args.bitmask = server->cache_consistency_bitmask;
  4236. if (!hdr->pgio_done_cb)
  4237. hdr->pgio_done_cb = nfs4_write_done_cb;
  4238. hdr->res.server = server;
  4239. hdr->timestamp = jiffies;
  4240. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
  4241. nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
  4242. }
  4243. static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
  4244. {
  4245. nfs4_setup_sequence(NFS_SERVER(data->inode),
  4246. &data->args.seq_args,
  4247. &data->res.seq_res,
  4248. task);
  4249. }
  4250. static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
  4251. {
  4252. struct inode *inode = data->inode;
  4253. trace_nfs4_commit(data, task->tk_status);
  4254. if (nfs4_async_handle_error(task, NFS_SERVER(inode),
  4255. NULL, NULL) == -EAGAIN) {
  4256. rpc_restart_call_prepare(task);
  4257. return -EAGAIN;
  4258. }
  4259. return 0;
  4260. }
  4261. static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
  4262. {
  4263. if (!nfs4_sequence_done(task, &data->res.seq_res))
  4264. return -EAGAIN;
  4265. return data->commit_done_cb(task, data);
  4266. }
  4267. static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
  4268. {
  4269. struct nfs_server *server = NFS_SERVER(data->inode);
  4270. if (data->commit_done_cb == NULL)
  4271. data->commit_done_cb = nfs4_commit_done_cb;
  4272. data->res.server = server;
  4273. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
  4274. nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
  4275. }
  4276. struct nfs4_renewdata {
  4277. struct nfs_client *client;
  4278. unsigned long timestamp;
  4279. };
  4280. /*
  4281. * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
  4282. * standalone procedure for queueing an asynchronous RENEW.
  4283. */
  4284. static void nfs4_renew_release(void *calldata)
  4285. {
  4286. struct nfs4_renewdata *data = calldata;
  4287. struct nfs_client *clp = data->client;
  4288. if (atomic_read(&clp->cl_count) > 1)
  4289. nfs4_schedule_state_renewal(clp);
  4290. nfs_put_client(clp);
  4291. kfree(data);
  4292. }
  4293. static void nfs4_renew_done(struct rpc_task *task, void *calldata)
  4294. {
  4295. struct nfs4_renewdata *data = calldata;
  4296. struct nfs_client *clp = data->client;
  4297. unsigned long timestamp = data->timestamp;
  4298. trace_nfs4_renew_async(clp, task->tk_status);
  4299. switch (task->tk_status) {
  4300. case 0:
  4301. break;
  4302. case -NFS4ERR_LEASE_MOVED:
  4303. nfs4_schedule_lease_moved_recovery(clp);
  4304. break;
  4305. default:
  4306. /* Unless we're shutting down, schedule state recovery! */
  4307. if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
  4308. return;
  4309. if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
  4310. nfs4_schedule_lease_recovery(clp);
  4311. return;
  4312. }
  4313. nfs4_schedule_path_down_recovery(clp);
  4314. }
  4315. do_renew_lease(clp, timestamp);
  4316. }
  4317. static const struct rpc_call_ops nfs4_renew_ops = {
  4318. .rpc_call_done = nfs4_renew_done,
  4319. .rpc_release = nfs4_renew_release,
  4320. };
  4321. static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
  4322. {
  4323. struct rpc_message msg = {
  4324. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
  4325. .rpc_argp = clp,
  4326. .rpc_cred = cred,
  4327. };
  4328. struct nfs4_renewdata *data;
  4329. if (renew_flags == 0)
  4330. return 0;
  4331. if (!atomic_inc_not_zero(&clp->cl_count))
  4332. return -EIO;
  4333. data = kmalloc(sizeof(*data), GFP_NOFS);
  4334. if (data == NULL)
  4335. return -ENOMEM;
  4336. data->client = clp;
  4337. data->timestamp = jiffies;
  4338. return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
  4339. &nfs4_renew_ops, data);
  4340. }
  4341. static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
  4342. {
  4343. struct rpc_message msg = {
  4344. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
  4345. .rpc_argp = clp,
  4346. .rpc_cred = cred,
  4347. };
  4348. unsigned long now = jiffies;
  4349. int status;
  4350. status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  4351. if (status < 0)
  4352. return status;
  4353. do_renew_lease(clp, now);
  4354. return 0;
  4355. }
  4356. static inline int nfs4_server_supports_acls(struct nfs_server *server)
  4357. {
  4358. return server->caps & NFS_CAP_ACLS;
  4359. }
  4360. /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
  4361. * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
  4362. * the stack.
  4363. */
  4364. #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
  4365. static int buf_to_pages_noslab(const void *buf, size_t buflen,
  4366. struct page **pages)
  4367. {
  4368. struct page *newpage, **spages;
  4369. int rc = 0;
  4370. size_t len;
  4371. spages = pages;
  4372. do {
  4373. len = min_t(size_t, PAGE_SIZE, buflen);
  4374. newpage = alloc_page(GFP_KERNEL);
  4375. if (newpage == NULL)
  4376. goto unwind;
  4377. memcpy(page_address(newpage), buf, len);
  4378. buf += len;
  4379. buflen -= len;
  4380. *pages++ = newpage;
  4381. rc++;
  4382. } while (buflen != 0);
  4383. return rc;
  4384. unwind:
  4385. for(; rc > 0; rc--)
  4386. __free_page(spages[rc-1]);
  4387. return -ENOMEM;
  4388. }
  4389. struct nfs4_cached_acl {
  4390. int cached;
  4391. size_t len;
  4392. char data[0];
  4393. };
  4394. static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
  4395. {
  4396. struct nfs_inode *nfsi = NFS_I(inode);
  4397. spin_lock(&inode->i_lock);
  4398. kfree(nfsi->nfs4_acl);
  4399. nfsi->nfs4_acl = acl;
  4400. spin_unlock(&inode->i_lock);
  4401. }
  4402. static void nfs4_zap_acl_attr(struct inode *inode)
  4403. {
  4404. nfs4_set_cached_acl(inode, NULL);
  4405. }
  4406. static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
  4407. {
  4408. struct nfs_inode *nfsi = NFS_I(inode);
  4409. struct nfs4_cached_acl *acl;
  4410. int ret = -ENOENT;
  4411. spin_lock(&inode->i_lock);
  4412. acl = nfsi->nfs4_acl;
  4413. if (acl == NULL)
  4414. goto out;
  4415. if (buf == NULL) /* user is just asking for length */
  4416. goto out_len;
  4417. if (acl->cached == 0)
  4418. goto out;
  4419. ret = -ERANGE; /* see getxattr(2) man page */
  4420. if (acl->len > buflen)
  4421. goto out;
  4422. memcpy(buf, acl->data, acl->len);
  4423. out_len:
  4424. ret = acl->len;
  4425. out:
  4426. spin_unlock(&inode->i_lock);
  4427. return ret;
  4428. }
  4429. static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
  4430. {
  4431. struct nfs4_cached_acl *acl;
  4432. size_t buflen = sizeof(*acl) + acl_len;
  4433. if (buflen <= PAGE_SIZE) {
  4434. acl = kmalloc(buflen, GFP_KERNEL);
  4435. if (acl == NULL)
  4436. goto out;
  4437. acl->cached = 1;
  4438. _copy_from_pages(acl->data, pages, pgbase, acl_len);
  4439. } else {
  4440. acl = kmalloc(sizeof(*acl), GFP_KERNEL);
  4441. if (acl == NULL)
  4442. goto out;
  4443. acl->cached = 0;
  4444. }
  4445. acl->len = acl_len;
  4446. out:
  4447. nfs4_set_cached_acl(inode, acl);
  4448. }
  4449. /*
  4450. * The getxattr API returns the required buffer length when called with a
  4451. * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
  4452. * the required buf. On a NULL buf, we send a page of data to the server
  4453. * guessing that the ACL request can be serviced by a page. If so, we cache
  4454. * up to the page of ACL data, and the 2nd call to getxattr is serviced by
  4455. * the cache. If not so, we throw away the page, and cache the required
  4456. * length. The next getxattr call will then produce another round trip to
  4457. * the server, this time with the input buf of the required size.
  4458. */
  4459. static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
  4460. {
  4461. struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
  4462. struct nfs_getaclargs args = {
  4463. .fh = NFS_FH(inode),
  4464. .acl_pages = pages,
  4465. .acl_len = buflen,
  4466. };
  4467. struct nfs_getaclres res = {
  4468. .acl_len = buflen,
  4469. };
  4470. struct rpc_message msg = {
  4471. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
  4472. .rpc_argp = &args,
  4473. .rpc_resp = &res,
  4474. };
  4475. unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
  4476. int ret = -ENOMEM, i;
  4477. if (npages > ARRAY_SIZE(pages))
  4478. return -ERANGE;
  4479. for (i = 0; i < npages; i++) {
  4480. pages[i] = alloc_page(GFP_KERNEL);
  4481. if (!pages[i])
  4482. goto out_free;
  4483. }
  4484. /* for decoding across pages */
  4485. res.acl_scratch = alloc_page(GFP_KERNEL);
  4486. if (!res.acl_scratch)
  4487. goto out_free;
  4488. args.acl_len = npages * PAGE_SIZE;
  4489. dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
  4490. __func__, buf, buflen, npages, args.acl_len);
  4491. ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
  4492. &msg, &args.seq_args, &res.seq_res, 0);
  4493. if (ret)
  4494. goto out_free;
  4495. /* Handle the case where the passed-in buffer is too short */
  4496. if (res.acl_flags & NFS4_ACL_TRUNC) {
  4497. /* Did the user only issue a request for the acl length? */
  4498. if (buf == NULL)
  4499. goto out_ok;
  4500. ret = -ERANGE;
  4501. goto out_free;
  4502. }
  4503. nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
  4504. if (buf) {
  4505. if (res.acl_len > buflen) {
  4506. ret = -ERANGE;
  4507. goto out_free;
  4508. }
  4509. _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
  4510. }
  4511. out_ok:
  4512. ret = res.acl_len;
  4513. out_free:
  4514. for (i = 0; i < npages; i++)
  4515. if (pages[i])
  4516. __free_page(pages[i]);
  4517. if (res.acl_scratch)
  4518. __free_page(res.acl_scratch);
  4519. return ret;
  4520. }
  4521. static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
  4522. {
  4523. struct nfs4_exception exception = { };
  4524. ssize_t ret;
  4525. do {
  4526. ret = __nfs4_get_acl_uncached(inode, buf, buflen);
  4527. trace_nfs4_get_acl(inode, ret);
  4528. if (ret >= 0)
  4529. break;
  4530. ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
  4531. } while (exception.retry);
  4532. return ret;
  4533. }
  4534. static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
  4535. {
  4536. struct nfs_server *server = NFS_SERVER(inode);
  4537. int ret;
  4538. if (!nfs4_server_supports_acls(server))
  4539. return -EOPNOTSUPP;
  4540. ret = nfs_revalidate_inode(server, inode);
  4541. if (ret < 0)
  4542. return ret;
  4543. if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
  4544. nfs_zap_acl_cache(inode);
  4545. ret = nfs4_read_cached_acl(inode, buf, buflen);
  4546. if (ret != -ENOENT)
  4547. /* -ENOENT is returned if there is no ACL or if there is an ACL
  4548. * but no cached acl data, just the acl length */
  4549. return ret;
  4550. return nfs4_get_acl_uncached(inode, buf, buflen);
  4551. }
  4552. static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
  4553. {
  4554. struct nfs_server *server = NFS_SERVER(inode);
  4555. struct page *pages[NFS4ACL_MAXPAGES];
  4556. struct nfs_setaclargs arg = {
  4557. .fh = NFS_FH(inode),
  4558. .acl_pages = pages,
  4559. .acl_len = buflen,
  4560. };
  4561. struct nfs_setaclres res;
  4562. struct rpc_message msg = {
  4563. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
  4564. .rpc_argp = &arg,
  4565. .rpc_resp = &res,
  4566. };
  4567. unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
  4568. int ret, i;
  4569. if (!nfs4_server_supports_acls(server))
  4570. return -EOPNOTSUPP;
  4571. if (npages > ARRAY_SIZE(pages))
  4572. return -ERANGE;
  4573. i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
  4574. if (i < 0)
  4575. return i;
  4576. nfs4_inode_return_delegation(inode);
  4577. ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
  4578. /*
  4579. * Free each page after tx, so the only ref left is
  4580. * held by the network stack
  4581. */
  4582. for (; i > 0; i--)
  4583. put_page(pages[i-1]);
  4584. /*
  4585. * Acl update can result in inode attribute update.
  4586. * so mark the attribute cache invalid.
  4587. */
  4588. spin_lock(&inode->i_lock);
  4589. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
  4590. spin_unlock(&inode->i_lock);
  4591. nfs_access_zap_cache(inode);
  4592. nfs_zap_acl_cache(inode);
  4593. return ret;
  4594. }
  4595. static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
  4596. {
  4597. struct nfs4_exception exception = { };
  4598. int err;
  4599. do {
  4600. err = __nfs4_proc_set_acl(inode, buf, buflen);
  4601. trace_nfs4_set_acl(inode, err);
  4602. err = nfs4_handle_exception(NFS_SERVER(inode), err,
  4603. &exception);
  4604. } while (exception.retry);
  4605. return err;
  4606. }
  4607. #ifdef CONFIG_NFS_V4_SECURITY_LABEL
  4608. static int _nfs4_get_security_label(struct inode *inode, void *buf,
  4609. size_t buflen)
  4610. {
  4611. struct nfs_server *server = NFS_SERVER(inode);
  4612. struct nfs_fattr fattr;
  4613. struct nfs4_label label = {0, 0, buflen, buf};
  4614. u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
  4615. struct nfs4_getattr_arg arg = {
  4616. .fh = NFS_FH(inode),
  4617. .bitmask = bitmask,
  4618. };
  4619. struct nfs4_getattr_res res = {
  4620. .fattr = &fattr,
  4621. .label = &label,
  4622. .server = server,
  4623. };
  4624. struct rpc_message msg = {
  4625. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
  4626. .rpc_argp = &arg,
  4627. .rpc_resp = &res,
  4628. };
  4629. int ret;
  4630. nfs_fattr_init(&fattr);
  4631. ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
  4632. if (ret)
  4633. return ret;
  4634. if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
  4635. return -ENOENT;
  4636. if (buflen < label.len)
  4637. return -ERANGE;
  4638. return 0;
  4639. }
  4640. static int nfs4_get_security_label(struct inode *inode, void *buf,
  4641. size_t buflen)
  4642. {
  4643. struct nfs4_exception exception = { };
  4644. int err;
  4645. if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
  4646. return -EOPNOTSUPP;
  4647. do {
  4648. err = _nfs4_get_security_label(inode, buf, buflen);
  4649. trace_nfs4_get_security_label(inode, err);
  4650. err = nfs4_handle_exception(NFS_SERVER(inode), err,
  4651. &exception);
  4652. } while (exception.retry);
  4653. return err;
  4654. }
  4655. static int _nfs4_do_set_security_label(struct inode *inode,
  4656. struct nfs4_label *ilabel,
  4657. struct nfs_fattr *fattr,
  4658. struct nfs4_label *olabel)
  4659. {
  4660. struct iattr sattr = {0};
  4661. struct nfs_server *server = NFS_SERVER(inode);
  4662. const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
  4663. struct nfs_setattrargs arg = {
  4664. .fh = NFS_FH(inode),
  4665. .iap = &sattr,
  4666. .server = server,
  4667. .bitmask = bitmask,
  4668. .label = ilabel,
  4669. };
  4670. struct nfs_setattrres res = {
  4671. .fattr = fattr,
  4672. .label = olabel,
  4673. .server = server,
  4674. };
  4675. struct rpc_message msg = {
  4676. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
  4677. .rpc_argp = &arg,
  4678. .rpc_resp = &res,
  4679. };
  4680. int status;
  4681. nfs4_stateid_copy(&arg.stateid, &zero_stateid);
  4682. status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
  4683. if (status)
  4684. dprintk("%s failed: %d\n", __func__, status);
  4685. return status;
  4686. }
  4687. static int nfs4_do_set_security_label(struct inode *inode,
  4688. struct nfs4_label *ilabel,
  4689. struct nfs_fattr *fattr,
  4690. struct nfs4_label *olabel)
  4691. {
  4692. struct nfs4_exception exception = { };
  4693. int err;
  4694. do {
  4695. err = _nfs4_do_set_security_label(inode, ilabel,
  4696. fattr, olabel);
  4697. trace_nfs4_set_security_label(inode, err);
  4698. err = nfs4_handle_exception(NFS_SERVER(inode), err,
  4699. &exception);
  4700. } while (exception.retry);
  4701. return err;
  4702. }
  4703. static int
  4704. nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
  4705. {
  4706. struct nfs4_label ilabel, *olabel = NULL;
  4707. struct nfs_fattr fattr;
  4708. struct rpc_cred *cred;
  4709. int status;
  4710. if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
  4711. return -EOPNOTSUPP;
  4712. nfs_fattr_init(&fattr);
  4713. ilabel.pi = 0;
  4714. ilabel.lfs = 0;
  4715. ilabel.label = (char *)buf;
  4716. ilabel.len = buflen;
  4717. cred = rpc_lookup_cred();
  4718. if (IS_ERR(cred))
  4719. return PTR_ERR(cred);
  4720. olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
  4721. if (IS_ERR(olabel)) {
  4722. status = -PTR_ERR(olabel);
  4723. goto out;
  4724. }
  4725. status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
  4726. if (status == 0)
  4727. nfs_setsecurity(inode, &fattr, olabel);
  4728. nfs4_label_free(olabel);
  4729. out:
  4730. put_rpccred(cred);
  4731. return status;
  4732. }
  4733. #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
  4734. static void nfs4_init_boot_verifier(const struct nfs_client *clp,
  4735. nfs4_verifier *bootverf)
  4736. {
  4737. __be32 verf[2];
  4738. if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
  4739. /* An impossible timestamp guarantees this value
  4740. * will never match a generated boot time. */
  4741. verf[0] = cpu_to_be32(U32_MAX);
  4742. verf[1] = cpu_to_be32(U32_MAX);
  4743. } else {
  4744. struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
  4745. u64 ns = ktime_to_ns(nn->boot_time);
  4746. verf[0] = cpu_to_be32(ns >> 32);
  4747. verf[1] = cpu_to_be32(ns);
  4748. }
  4749. memcpy(bootverf->data, verf, sizeof(bootverf->data));
  4750. }
  4751. static int
  4752. nfs4_init_nonuniform_client_string(struct nfs_client *clp)
  4753. {
  4754. size_t len;
  4755. char *str;
  4756. if (clp->cl_owner_id != NULL)
  4757. return 0;
  4758. rcu_read_lock();
  4759. len = 14 + strlen(clp->cl_ipaddr) + 1 +
  4760. strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
  4761. 1 +
  4762. strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
  4763. 1;
  4764. rcu_read_unlock();
  4765. if (len > NFS4_OPAQUE_LIMIT + 1)
  4766. return -EINVAL;
  4767. /*
  4768. * Since this string is allocated at mount time, and held until the
  4769. * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
  4770. * about a memory-reclaim deadlock.
  4771. */
  4772. str = kmalloc(len, GFP_KERNEL);
  4773. if (!str)
  4774. return -ENOMEM;
  4775. rcu_read_lock();
  4776. scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
  4777. clp->cl_ipaddr,
  4778. rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
  4779. rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
  4780. rcu_read_unlock();
  4781. clp->cl_owner_id = str;
  4782. return 0;
  4783. }
  4784. static int
  4785. nfs4_init_uniquifier_client_string(struct nfs_client *clp)
  4786. {
  4787. size_t len;
  4788. char *str;
  4789. len = 10 + 10 + 1 + 10 + 1 +
  4790. strlen(nfs4_client_id_uniquifier) + 1 +
  4791. strlen(clp->cl_rpcclient->cl_nodename) + 1;
  4792. if (len > NFS4_OPAQUE_LIMIT + 1)
  4793. return -EINVAL;
  4794. /*
  4795. * Since this string is allocated at mount time, and held until the
  4796. * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
  4797. * about a memory-reclaim deadlock.
  4798. */
  4799. str = kmalloc(len, GFP_KERNEL);
  4800. if (!str)
  4801. return -ENOMEM;
  4802. scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
  4803. clp->rpc_ops->version, clp->cl_minorversion,
  4804. nfs4_client_id_uniquifier,
  4805. clp->cl_rpcclient->cl_nodename);
  4806. clp->cl_owner_id = str;
  4807. return 0;
  4808. }
  4809. static int
  4810. nfs4_init_uniform_client_string(struct nfs_client *clp)
  4811. {
  4812. size_t len;
  4813. char *str;
  4814. if (clp->cl_owner_id != NULL)
  4815. return 0;
  4816. if (nfs4_client_id_uniquifier[0] != '\0')
  4817. return nfs4_init_uniquifier_client_string(clp);
  4818. len = 10 + 10 + 1 + 10 + 1 +
  4819. strlen(clp->cl_rpcclient->cl_nodename) + 1;
  4820. if (len > NFS4_OPAQUE_LIMIT + 1)
  4821. return -EINVAL;
  4822. /*
  4823. * Since this string is allocated at mount time, and held until the
  4824. * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
  4825. * about a memory-reclaim deadlock.
  4826. */
  4827. str = kmalloc(len, GFP_KERNEL);
  4828. if (!str)
  4829. return -ENOMEM;
  4830. scnprintf(str, len, "Linux NFSv%u.%u %s",
  4831. clp->rpc_ops->version, clp->cl_minorversion,
  4832. clp->cl_rpcclient->cl_nodename);
  4833. clp->cl_owner_id = str;
  4834. return 0;
  4835. }
  4836. /*
  4837. * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
  4838. * services. Advertise one based on the address family of the
  4839. * clientaddr.
  4840. */
  4841. static unsigned int
  4842. nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
  4843. {
  4844. if (strchr(clp->cl_ipaddr, ':') != NULL)
  4845. return scnprintf(buf, len, "tcp6");
  4846. else
  4847. return scnprintf(buf, len, "tcp");
  4848. }
  4849. static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
  4850. {
  4851. struct nfs4_setclientid *sc = calldata;
  4852. if (task->tk_status == 0)
  4853. sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
  4854. }
  4855. static const struct rpc_call_ops nfs4_setclientid_ops = {
  4856. .rpc_call_done = nfs4_setclientid_done,
  4857. };
  4858. /**
  4859. * nfs4_proc_setclientid - Negotiate client ID
  4860. * @clp: state data structure
  4861. * @program: RPC program for NFSv4 callback service
  4862. * @port: IP port number for NFS4 callback service
  4863. * @cred: RPC credential to use for this call
  4864. * @res: where to place the result
  4865. *
  4866. * Returns zero, a negative errno, or a negative NFS4ERR status code.
  4867. */
  4868. int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
  4869. unsigned short port, struct rpc_cred *cred,
  4870. struct nfs4_setclientid_res *res)
  4871. {
  4872. nfs4_verifier sc_verifier;
  4873. struct nfs4_setclientid setclientid = {
  4874. .sc_verifier = &sc_verifier,
  4875. .sc_prog = program,
  4876. .sc_clnt = clp,
  4877. };
  4878. struct rpc_message msg = {
  4879. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
  4880. .rpc_argp = &setclientid,
  4881. .rpc_resp = res,
  4882. .rpc_cred = cred,
  4883. };
  4884. struct rpc_task *task;
  4885. struct rpc_task_setup task_setup_data = {
  4886. .rpc_client = clp->cl_rpcclient,
  4887. .rpc_message = &msg,
  4888. .callback_ops = &nfs4_setclientid_ops,
  4889. .callback_data = &setclientid,
  4890. .flags = RPC_TASK_TIMEOUT,
  4891. };
  4892. int status;
  4893. /* nfs_client_id4 */
  4894. nfs4_init_boot_verifier(clp, &sc_verifier);
  4895. if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
  4896. status = nfs4_init_uniform_client_string(clp);
  4897. else
  4898. status = nfs4_init_nonuniform_client_string(clp);
  4899. if (status)
  4900. goto out;
  4901. /* cb_client4 */
  4902. setclientid.sc_netid_len =
  4903. nfs4_init_callback_netid(clp,
  4904. setclientid.sc_netid,
  4905. sizeof(setclientid.sc_netid));
  4906. setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
  4907. sizeof(setclientid.sc_uaddr), "%s.%u.%u",
  4908. clp->cl_ipaddr, port >> 8, port & 255);
  4909. dprintk("NFS call setclientid auth=%s, '%s'\n",
  4910. clp->cl_rpcclient->cl_auth->au_ops->au_name,
  4911. clp->cl_owner_id);
  4912. task = rpc_run_task(&task_setup_data);
  4913. if (IS_ERR(task)) {
  4914. status = PTR_ERR(task);
  4915. goto out;
  4916. }
  4917. status = task->tk_status;
  4918. if (setclientid.sc_cred) {
  4919. clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
  4920. put_rpccred(setclientid.sc_cred);
  4921. }
  4922. rpc_put_task(task);
  4923. out:
  4924. trace_nfs4_setclientid(clp, status);
  4925. dprintk("NFS reply setclientid: %d\n", status);
  4926. return status;
  4927. }
  4928. /**
  4929. * nfs4_proc_setclientid_confirm - Confirm client ID
  4930. * @clp: state data structure
  4931. * @res: result of a previous SETCLIENTID
  4932. * @cred: RPC credential to use for this call
  4933. *
  4934. * Returns zero, a negative errno, or a negative NFS4ERR status code.
  4935. */
  4936. int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
  4937. struct nfs4_setclientid_res *arg,
  4938. struct rpc_cred *cred)
  4939. {
  4940. struct rpc_message msg = {
  4941. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
  4942. .rpc_argp = arg,
  4943. .rpc_cred = cred,
  4944. };
  4945. int status;
  4946. dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
  4947. clp->cl_rpcclient->cl_auth->au_ops->au_name,
  4948. clp->cl_clientid);
  4949. status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  4950. trace_nfs4_setclientid_confirm(clp, status);
  4951. dprintk("NFS reply setclientid_confirm: %d\n", status);
  4952. return status;
  4953. }
  4954. struct nfs4_delegreturndata {
  4955. struct nfs4_delegreturnargs args;
  4956. struct nfs4_delegreturnres res;
  4957. struct nfs_fh fh;
  4958. nfs4_stateid stateid;
  4959. unsigned long timestamp;
  4960. struct nfs_fattr fattr;
  4961. int rpc_status;
  4962. struct inode *inode;
  4963. bool roc;
  4964. u32 roc_barrier;
  4965. };
  4966. static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
  4967. {
  4968. struct nfs4_delegreturndata *data = calldata;
  4969. if (!nfs4_sequence_done(task, &data->res.seq_res))
  4970. return;
  4971. trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
  4972. switch (task->tk_status) {
  4973. case 0:
  4974. renew_lease(data->res.server, data->timestamp);
  4975. break;
  4976. case -NFS4ERR_ADMIN_REVOKED:
  4977. case -NFS4ERR_DELEG_REVOKED:
  4978. case -NFS4ERR_EXPIRED:
  4979. nfs4_free_revoked_stateid(data->res.server,
  4980. data->args.stateid,
  4981. task->tk_msg.rpc_cred);
  4982. case -NFS4ERR_BAD_STATEID:
  4983. case -NFS4ERR_OLD_STATEID:
  4984. case -NFS4ERR_STALE_STATEID:
  4985. task->tk_status = 0;
  4986. break;
  4987. default:
  4988. if (nfs4_async_handle_error(task, data->res.server,
  4989. NULL, NULL) == -EAGAIN) {
  4990. rpc_restart_call_prepare(task);
  4991. return;
  4992. }
  4993. }
  4994. data->rpc_status = task->tk_status;
  4995. if (data->roc && data->rpc_status == 0)
  4996. pnfs_roc_set_barrier(data->inode, data->roc_barrier);
  4997. }
  4998. static void nfs4_delegreturn_release(void *calldata)
  4999. {
  5000. struct nfs4_delegreturndata *data = calldata;
  5001. struct inode *inode = data->inode;
  5002. if (inode) {
  5003. if (data->roc)
  5004. pnfs_roc_release(inode);
  5005. nfs_iput_and_deactive(inode);
  5006. }
  5007. kfree(calldata);
  5008. }
  5009. static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
  5010. {
  5011. struct nfs4_delegreturndata *d_data;
  5012. d_data = (struct nfs4_delegreturndata *)data;
  5013. if (nfs4_wait_on_layoutreturn(d_data->inode, task))
  5014. return;
  5015. if (d_data->roc)
  5016. pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
  5017. nfs4_setup_sequence(d_data->res.server,
  5018. &d_data->args.seq_args,
  5019. &d_data->res.seq_res,
  5020. task);
  5021. }
  5022. static const struct rpc_call_ops nfs4_delegreturn_ops = {
  5023. .rpc_call_prepare = nfs4_delegreturn_prepare,
  5024. .rpc_call_done = nfs4_delegreturn_done,
  5025. .rpc_release = nfs4_delegreturn_release,
  5026. };
  5027. static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
  5028. {
  5029. struct nfs4_delegreturndata *data;
  5030. struct nfs_server *server = NFS_SERVER(inode);
  5031. struct rpc_task *task;
  5032. struct rpc_message msg = {
  5033. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
  5034. .rpc_cred = cred,
  5035. };
  5036. struct rpc_task_setup task_setup_data = {
  5037. .rpc_client = server->client,
  5038. .rpc_message = &msg,
  5039. .callback_ops = &nfs4_delegreturn_ops,
  5040. .flags = RPC_TASK_ASYNC,
  5041. };
  5042. int status = 0;
  5043. data = kzalloc(sizeof(*data), GFP_NOFS);
  5044. if (data == NULL)
  5045. return -ENOMEM;
  5046. nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
  5047. nfs4_state_protect(server->nfs_client,
  5048. NFS_SP4_MACH_CRED_CLEANUP,
  5049. &task_setup_data.rpc_client, &msg);
  5050. data->args.fhandle = &data->fh;
  5051. data->args.stateid = &data->stateid;
  5052. data->args.bitmask = server->cache_consistency_bitmask;
  5053. nfs_copy_fh(&data->fh, NFS_FH(inode));
  5054. nfs4_stateid_copy(&data->stateid, stateid);
  5055. data->res.fattr = &data->fattr;
  5056. data->res.server = server;
  5057. nfs_fattr_init(data->res.fattr);
  5058. data->timestamp = jiffies;
  5059. data->rpc_status = 0;
  5060. data->inode = nfs_igrab_and_active(inode);
  5061. if (data->inode)
  5062. data->roc = nfs4_roc(inode);
  5063. task_setup_data.callback_data = data;
  5064. msg.rpc_argp = &data->args;
  5065. msg.rpc_resp = &data->res;
  5066. task = rpc_run_task(&task_setup_data);
  5067. if (IS_ERR(task))
  5068. return PTR_ERR(task);
  5069. if (!issync)
  5070. goto out;
  5071. status = nfs4_wait_for_completion_rpc_task(task);
  5072. if (status != 0)
  5073. goto out;
  5074. status = data->rpc_status;
  5075. if (status == 0)
  5076. nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
  5077. else
  5078. nfs_refresh_inode(inode, &data->fattr);
  5079. out:
  5080. rpc_put_task(task);
  5081. return status;
  5082. }
  5083. int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
  5084. {
  5085. struct nfs_server *server = NFS_SERVER(inode);
  5086. struct nfs4_exception exception = { };
  5087. int err;
  5088. do {
  5089. err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
  5090. trace_nfs4_delegreturn(inode, stateid, err);
  5091. switch (err) {
  5092. case -NFS4ERR_STALE_STATEID:
  5093. case -NFS4ERR_EXPIRED:
  5094. case 0:
  5095. return 0;
  5096. }
  5097. err = nfs4_handle_exception(server, err, &exception);
  5098. } while (exception.retry);
  5099. return err;
  5100. }
  5101. static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  5102. {
  5103. struct inode *inode = state->inode;
  5104. struct nfs_server *server = NFS_SERVER(inode);
  5105. struct nfs_client *clp = server->nfs_client;
  5106. struct nfs_lockt_args arg = {
  5107. .fh = NFS_FH(inode),
  5108. .fl = request,
  5109. };
  5110. struct nfs_lockt_res res = {
  5111. .denied = request,
  5112. };
  5113. struct rpc_message msg = {
  5114. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
  5115. .rpc_argp = &arg,
  5116. .rpc_resp = &res,
  5117. .rpc_cred = state->owner->so_cred,
  5118. };
  5119. struct nfs4_lock_state *lsp;
  5120. int status;
  5121. arg.lock_owner.clientid = clp->cl_clientid;
  5122. status = nfs4_set_lock_state(state, request);
  5123. if (status != 0)
  5124. goto out;
  5125. lsp = request->fl_u.nfs4_fl.owner;
  5126. arg.lock_owner.id = lsp->ls_seqid.owner_id;
  5127. arg.lock_owner.s_dev = server->s_dev;
  5128. status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
  5129. switch (status) {
  5130. case 0:
  5131. request->fl_type = F_UNLCK;
  5132. break;
  5133. case -NFS4ERR_DENIED:
  5134. status = 0;
  5135. }
  5136. request->fl_ops->fl_release_private(request);
  5137. request->fl_ops = NULL;
  5138. out:
  5139. return status;
  5140. }
  5141. static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  5142. {
  5143. struct nfs4_exception exception = { };
  5144. int err;
  5145. do {
  5146. err = _nfs4_proc_getlk(state, cmd, request);
  5147. trace_nfs4_get_lock(request, state, cmd, err);
  5148. err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
  5149. &exception);
  5150. } while (exception.retry);
  5151. return err;
  5152. }
  5153. struct nfs4_unlockdata {
  5154. struct nfs_locku_args arg;
  5155. struct nfs_locku_res res;
  5156. struct nfs4_lock_state *lsp;
  5157. struct nfs_open_context *ctx;
  5158. struct file_lock fl;
  5159. struct nfs_server *server;
  5160. unsigned long timestamp;
  5161. };
  5162. static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
  5163. struct nfs_open_context *ctx,
  5164. struct nfs4_lock_state *lsp,
  5165. struct nfs_seqid *seqid)
  5166. {
  5167. struct nfs4_unlockdata *p;
  5168. struct inode *inode = lsp->ls_state->inode;
  5169. p = kzalloc(sizeof(*p), GFP_NOFS);
  5170. if (p == NULL)
  5171. return NULL;
  5172. p->arg.fh = NFS_FH(inode);
  5173. p->arg.fl = &p->fl;
  5174. p->arg.seqid = seqid;
  5175. p->res.seqid = seqid;
  5176. p->lsp = lsp;
  5177. atomic_inc(&lsp->ls_count);
  5178. /* Ensure we don't close file until we're done freeing locks! */
  5179. p->ctx = get_nfs_open_context(ctx);
  5180. memcpy(&p->fl, fl, sizeof(p->fl));
  5181. p->server = NFS_SERVER(inode);
  5182. return p;
  5183. }
  5184. static void nfs4_locku_release_calldata(void *data)
  5185. {
  5186. struct nfs4_unlockdata *calldata = data;
  5187. nfs_free_seqid(calldata->arg.seqid);
  5188. nfs4_put_lock_state(calldata->lsp);
  5189. put_nfs_open_context(calldata->ctx);
  5190. kfree(calldata);
  5191. }
  5192. static void nfs4_locku_done(struct rpc_task *task, void *data)
  5193. {
  5194. struct nfs4_unlockdata *calldata = data;
  5195. if (!nfs4_sequence_done(task, &calldata->res.seq_res))
  5196. return;
  5197. switch (task->tk_status) {
  5198. case 0:
  5199. renew_lease(calldata->server, calldata->timestamp);
  5200. locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
  5201. if (nfs4_update_lock_stateid(calldata->lsp,
  5202. &calldata->res.stateid))
  5203. break;
  5204. case -NFS4ERR_ADMIN_REVOKED:
  5205. case -NFS4ERR_EXPIRED:
  5206. nfs4_free_revoked_stateid(calldata->server,
  5207. &calldata->arg.stateid,
  5208. task->tk_msg.rpc_cred);
  5209. case -NFS4ERR_BAD_STATEID:
  5210. case -NFS4ERR_OLD_STATEID:
  5211. case -NFS4ERR_STALE_STATEID:
  5212. if (!nfs4_stateid_match(&calldata->arg.stateid,
  5213. &calldata->lsp->ls_stateid))
  5214. rpc_restart_call_prepare(task);
  5215. break;
  5216. default:
  5217. if (nfs4_async_handle_error(task, calldata->server,
  5218. NULL, NULL) == -EAGAIN)
  5219. rpc_restart_call_prepare(task);
  5220. }
  5221. nfs_release_seqid(calldata->arg.seqid);
  5222. }
  5223. static void nfs4_locku_prepare(struct rpc_task *task, void *data)
  5224. {
  5225. struct nfs4_unlockdata *calldata = data;
  5226. if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
  5227. goto out_wait;
  5228. nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
  5229. if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
  5230. /* Note: exit _without_ running nfs4_locku_done */
  5231. goto out_no_action;
  5232. }
  5233. calldata->timestamp = jiffies;
  5234. if (nfs4_setup_sequence(calldata->server,
  5235. &calldata->arg.seq_args,
  5236. &calldata->res.seq_res,
  5237. task) != 0)
  5238. nfs_release_seqid(calldata->arg.seqid);
  5239. return;
  5240. out_no_action:
  5241. task->tk_action = NULL;
  5242. out_wait:
  5243. nfs4_sequence_done(task, &calldata->res.seq_res);
  5244. }
  5245. static const struct rpc_call_ops nfs4_locku_ops = {
  5246. .rpc_call_prepare = nfs4_locku_prepare,
  5247. .rpc_call_done = nfs4_locku_done,
  5248. .rpc_release = nfs4_locku_release_calldata,
  5249. };
  5250. static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
  5251. struct nfs_open_context *ctx,
  5252. struct nfs4_lock_state *lsp,
  5253. struct nfs_seqid *seqid)
  5254. {
  5255. struct nfs4_unlockdata *data;
  5256. struct rpc_message msg = {
  5257. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
  5258. .rpc_cred = ctx->cred,
  5259. };
  5260. struct rpc_task_setup task_setup_data = {
  5261. .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
  5262. .rpc_message = &msg,
  5263. .callback_ops = &nfs4_locku_ops,
  5264. .workqueue = nfsiod_workqueue,
  5265. .flags = RPC_TASK_ASYNC,
  5266. };
  5267. nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
  5268. NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
  5269. /* Ensure this is an unlock - when canceling a lock, the
  5270. * canceled lock is passed in, and it won't be an unlock.
  5271. */
  5272. fl->fl_type = F_UNLCK;
  5273. data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
  5274. if (data == NULL) {
  5275. nfs_free_seqid(seqid);
  5276. return ERR_PTR(-ENOMEM);
  5277. }
  5278. nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
  5279. msg.rpc_argp = &data->arg;
  5280. msg.rpc_resp = &data->res;
  5281. task_setup_data.callback_data = data;
  5282. return rpc_run_task(&task_setup_data);
  5283. }
  5284. static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
  5285. {
  5286. struct inode *inode = state->inode;
  5287. struct nfs4_state_owner *sp = state->owner;
  5288. struct nfs_inode *nfsi = NFS_I(inode);
  5289. struct nfs_seqid *seqid;
  5290. struct nfs4_lock_state *lsp;
  5291. struct rpc_task *task;
  5292. struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
  5293. int status = 0;
  5294. unsigned char fl_flags = request->fl_flags;
  5295. status = nfs4_set_lock_state(state, request);
  5296. /* Unlock _before_ we do the RPC call */
  5297. request->fl_flags |= FL_EXISTS;
  5298. /* Exclude nfs_delegation_claim_locks() */
  5299. mutex_lock(&sp->so_delegreturn_mutex);
  5300. /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
  5301. down_read(&nfsi->rwsem);
  5302. if (locks_lock_inode_wait(inode, request) == -ENOENT) {
  5303. up_read(&nfsi->rwsem);
  5304. mutex_unlock(&sp->so_delegreturn_mutex);
  5305. goto out;
  5306. }
  5307. up_read(&nfsi->rwsem);
  5308. mutex_unlock(&sp->so_delegreturn_mutex);
  5309. if (status != 0)
  5310. goto out;
  5311. /* Is this a delegated lock? */
  5312. lsp = request->fl_u.nfs4_fl.owner;
  5313. if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
  5314. goto out;
  5315. alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
  5316. seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
  5317. status = -ENOMEM;
  5318. if (IS_ERR(seqid))
  5319. goto out;
  5320. task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
  5321. status = PTR_ERR(task);
  5322. if (IS_ERR(task))
  5323. goto out;
  5324. status = nfs4_wait_for_completion_rpc_task(task);
  5325. rpc_put_task(task);
  5326. out:
  5327. request->fl_flags = fl_flags;
  5328. trace_nfs4_unlock(request, state, F_SETLK, status);
  5329. return status;
  5330. }
  5331. struct nfs4_lockdata {
  5332. struct nfs_lock_args arg;
  5333. struct nfs_lock_res res;
  5334. struct nfs4_lock_state *lsp;
  5335. struct nfs_open_context *ctx;
  5336. struct file_lock fl;
  5337. unsigned long timestamp;
  5338. int rpc_status;
  5339. int cancelled;
  5340. struct nfs_server *server;
  5341. };
  5342. static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
  5343. struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
  5344. gfp_t gfp_mask)
  5345. {
  5346. struct nfs4_lockdata *p;
  5347. struct inode *inode = lsp->ls_state->inode;
  5348. struct nfs_server *server = NFS_SERVER(inode);
  5349. struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
  5350. p = kzalloc(sizeof(*p), gfp_mask);
  5351. if (p == NULL)
  5352. return NULL;
  5353. p->arg.fh = NFS_FH(inode);
  5354. p->arg.fl = &p->fl;
  5355. p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
  5356. if (IS_ERR(p->arg.open_seqid))
  5357. goto out_free;
  5358. alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
  5359. p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
  5360. if (IS_ERR(p->arg.lock_seqid))
  5361. goto out_free_seqid;
  5362. p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
  5363. p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
  5364. p->arg.lock_owner.s_dev = server->s_dev;
  5365. p->res.lock_seqid = p->arg.lock_seqid;
  5366. p->lsp = lsp;
  5367. p->server = server;
  5368. atomic_inc(&lsp->ls_count);
  5369. p->ctx = get_nfs_open_context(ctx);
  5370. memcpy(&p->fl, fl, sizeof(p->fl));
  5371. return p;
  5372. out_free_seqid:
  5373. nfs_free_seqid(p->arg.open_seqid);
  5374. out_free:
  5375. kfree(p);
  5376. return NULL;
  5377. }
  5378. static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
  5379. {
  5380. struct nfs4_lockdata *data = calldata;
  5381. struct nfs4_state *state = data->lsp->ls_state;
  5382. dprintk("%s: begin!\n", __func__);
  5383. if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
  5384. goto out_wait;
  5385. /* Do we need to do an open_to_lock_owner? */
  5386. if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
  5387. if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
  5388. goto out_release_lock_seqid;
  5389. }
  5390. nfs4_stateid_copy(&data->arg.open_stateid,
  5391. &state->open_stateid);
  5392. data->arg.new_lock_owner = 1;
  5393. data->res.open_seqid = data->arg.open_seqid;
  5394. } else {
  5395. data->arg.new_lock_owner = 0;
  5396. nfs4_stateid_copy(&data->arg.lock_stateid,
  5397. &data->lsp->ls_stateid);
  5398. }
  5399. if (!nfs4_valid_open_stateid(state)) {
  5400. data->rpc_status = -EBADF;
  5401. task->tk_action = NULL;
  5402. goto out_release_open_seqid;
  5403. }
  5404. data->timestamp = jiffies;
  5405. if (nfs4_setup_sequence(data->server,
  5406. &data->arg.seq_args,
  5407. &data->res.seq_res,
  5408. task) == 0)
  5409. return;
  5410. out_release_open_seqid:
  5411. nfs_release_seqid(data->arg.open_seqid);
  5412. out_release_lock_seqid:
  5413. nfs_release_seqid(data->arg.lock_seqid);
  5414. out_wait:
  5415. nfs4_sequence_done(task, &data->res.seq_res);
  5416. dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
  5417. }
  5418. static void nfs4_lock_done(struct rpc_task *task, void *calldata)
  5419. {
  5420. struct nfs4_lockdata *data = calldata;
  5421. struct nfs4_lock_state *lsp = data->lsp;
  5422. dprintk("%s: begin!\n", __func__);
  5423. if (!nfs4_sequence_done(task, &data->res.seq_res))
  5424. return;
  5425. data->rpc_status = task->tk_status;
  5426. switch (task->tk_status) {
  5427. case 0:
  5428. renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
  5429. data->timestamp);
  5430. if (data->arg.new_lock) {
  5431. data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
  5432. if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
  5433. rpc_restart_call_prepare(task);
  5434. break;
  5435. }
  5436. }
  5437. if (data->arg.new_lock_owner != 0) {
  5438. nfs_confirm_seqid(&lsp->ls_seqid, 0);
  5439. nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
  5440. set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
  5441. } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
  5442. rpc_restart_call_prepare(task);
  5443. break;
  5444. case -NFS4ERR_BAD_STATEID:
  5445. case -NFS4ERR_OLD_STATEID:
  5446. case -NFS4ERR_STALE_STATEID:
  5447. case -NFS4ERR_EXPIRED:
  5448. if (data->arg.new_lock_owner != 0) {
  5449. if (!nfs4_stateid_match(&data->arg.open_stateid,
  5450. &lsp->ls_state->open_stateid))
  5451. rpc_restart_call_prepare(task);
  5452. } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
  5453. &lsp->ls_stateid))
  5454. rpc_restart_call_prepare(task);
  5455. }
  5456. dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
  5457. }
  5458. static void nfs4_lock_release(void *calldata)
  5459. {
  5460. struct nfs4_lockdata *data = calldata;
  5461. dprintk("%s: begin!\n", __func__);
  5462. nfs_free_seqid(data->arg.open_seqid);
  5463. if (data->cancelled != 0) {
  5464. struct rpc_task *task;
  5465. task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
  5466. data->arg.lock_seqid);
  5467. if (!IS_ERR(task))
  5468. rpc_put_task_async(task);
  5469. dprintk("%s: cancelling lock!\n", __func__);
  5470. } else
  5471. nfs_free_seqid(data->arg.lock_seqid);
  5472. nfs4_put_lock_state(data->lsp);
  5473. put_nfs_open_context(data->ctx);
  5474. kfree(data);
  5475. dprintk("%s: done!\n", __func__);
  5476. }
  5477. static const struct rpc_call_ops nfs4_lock_ops = {
  5478. .rpc_call_prepare = nfs4_lock_prepare,
  5479. .rpc_call_done = nfs4_lock_done,
  5480. .rpc_release = nfs4_lock_release,
  5481. };
  5482. static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
  5483. {
  5484. switch (error) {
  5485. case -NFS4ERR_ADMIN_REVOKED:
  5486. case -NFS4ERR_EXPIRED:
  5487. case -NFS4ERR_BAD_STATEID:
  5488. lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
  5489. if (new_lock_owner != 0 ||
  5490. test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
  5491. nfs4_schedule_stateid_recovery(server, lsp->ls_state);
  5492. break;
  5493. case -NFS4ERR_STALE_STATEID:
  5494. lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
  5495. nfs4_schedule_lease_recovery(server->nfs_client);
  5496. };
  5497. }
  5498. static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
  5499. {
  5500. struct nfs4_lockdata *data;
  5501. struct rpc_task *task;
  5502. struct rpc_message msg = {
  5503. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
  5504. .rpc_cred = state->owner->so_cred,
  5505. };
  5506. struct rpc_task_setup task_setup_data = {
  5507. .rpc_client = NFS_CLIENT(state->inode),
  5508. .rpc_message = &msg,
  5509. .callback_ops = &nfs4_lock_ops,
  5510. .workqueue = nfsiod_workqueue,
  5511. .flags = RPC_TASK_ASYNC,
  5512. };
  5513. int ret;
  5514. dprintk("%s: begin!\n", __func__);
  5515. data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
  5516. fl->fl_u.nfs4_fl.owner,
  5517. recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
  5518. if (data == NULL)
  5519. return -ENOMEM;
  5520. if (IS_SETLKW(cmd))
  5521. data->arg.block = 1;
  5522. nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
  5523. msg.rpc_argp = &data->arg;
  5524. msg.rpc_resp = &data->res;
  5525. task_setup_data.callback_data = data;
  5526. if (recovery_type > NFS_LOCK_NEW) {
  5527. if (recovery_type == NFS_LOCK_RECLAIM)
  5528. data->arg.reclaim = NFS_LOCK_RECLAIM;
  5529. nfs4_set_sequence_privileged(&data->arg.seq_args);
  5530. } else
  5531. data->arg.new_lock = 1;
  5532. task = rpc_run_task(&task_setup_data);
  5533. if (IS_ERR(task))
  5534. return PTR_ERR(task);
  5535. ret = nfs4_wait_for_completion_rpc_task(task);
  5536. if (ret == 0) {
  5537. ret = data->rpc_status;
  5538. if (ret)
  5539. nfs4_handle_setlk_error(data->server, data->lsp,
  5540. data->arg.new_lock_owner, ret);
  5541. } else
  5542. data->cancelled = 1;
  5543. rpc_put_task(task);
  5544. dprintk("%s: done, ret = %d!\n", __func__, ret);
  5545. trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
  5546. return ret;
  5547. }
  5548. static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
  5549. {
  5550. struct nfs_server *server = NFS_SERVER(state->inode);
  5551. struct nfs4_exception exception = {
  5552. .inode = state->inode,
  5553. };
  5554. int err;
  5555. do {
  5556. /* Cache the lock if possible... */
  5557. if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
  5558. return 0;
  5559. err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
  5560. if (err != -NFS4ERR_DELAY)
  5561. break;
  5562. nfs4_handle_exception(server, err, &exception);
  5563. } while (exception.retry);
  5564. return err;
  5565. }
  5566. static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
  5567. {
  5568. struct nfs_server *server = NFS_SERVER(state->inode);
  5569. struct nfs4_exception exception = {
  5570. .inode = state->inode,
  5571. };
  5572. int err;
  5573. err = nfs4_set_lock_state(state, request);
  5574. if (err != 0)
  5575. return err;
  5576. if (!recover_lost_locks) {
  5577. set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
  5578. return 0;
  5579. }
  5580. do {
  5581. if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
  5582. return 0;
  5583. err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
  5584. switch (err) {
  5585. default:
  5586. goto out;
  5587. case -NFS4ERR_GRACE:
  5588. case -NFS4ERR_DELAY:
  5589. nfs4_handle_exception(server, err, &exception);
  5590. err = 0;
  5591. }
  5592. } while (exception.retry);
  5593. out:
  5594. return err;
  5595. }
  5596. #if defined(CONFIG_NFS_V4_1)
  5597. static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
  5598. {
  5599. struct nfs4_lock_state *lsp;
  5600. int status;
  5601. status = nfs4_set_lock_state(state, request);
  5602. if (status != 0)
  5603. return status;
  5604. lsp = request->fl_u.nfs4_fl.owner;
  5605. if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
  5606. test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
  5607. return 0;
  5608. status = nfs4_lock_expired(state, request);
  5609. return status;
  5610. }
  5611. #endif
  5612. static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  5613. {
  5614. struct nfs_inode *nfsi = NFS_I(state->inode);
  5615. struct nfs4_state_owner *sp = state->owner;
  5616. unsigned char fl_flags = request->fl_flags;
  5617. int status;
  5618. request->fl_flags |= FL_ACCESS;
  5619. status = locks_lock_inode_wait(state->inode, request);
  5620. if (status < 0)
  5621. goto out;
  5622. mutex_lock(&sp->so_delegreturn_mutex);
  5623. down_read(&nfsi->rwsem);
  5624. if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
  5625. /* Yes: cache locks! */
  5626. /* ...but avoid races with delegation recall... */
  5627. request->fl_flags = fl_flags & ~FL_SLEEP;
  5628. status = locks_lock_inode_wait(state->inode, request);
  5629. up_read(&nfsi->rwsem);
  5630. mutex_unlock(&sp->so_delegreturn_mutex);
  5631. goto out;
  5632. }
  5633. up_read(&nfsi->rwsem);
  5634. mutex_unlock(&sp->so_delegreturn_mutex);
  5635. status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
  5636. out:
  5637. request->fl_flags = fl_flags;
  5638. return status;
  5639. }
  5640. static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  5641. {
  5642. struct nfs4_exception exception = {
  5643. .state = state,
  5644. .inode = state->inode,
  5645. };
  5646. int err;
  5647. do {
  5648. err = _nfs4_proc_setlk(state, cmd, request);
  5649. if (err == -NFS4ERR_DENIED)
  5650. err = -EAGAIN;
  5651. err = nfs4_handle_exception(NFS_SERVER(state->inode),
  5652. err, &exception);
  5653. } while (exception.retry);
  5654. return err;
  5655. }
  5656. #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
  5657. #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
  5658. static int
  5659. nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
  5660. struct file_lock *request)
  5661. {
  5662. int status = -ERESTARTSYS;
  5663. unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
  5664. while(!signalled()) {
  5665. status = nfs4_proc_setlk(state, cmd, request);
  5666. if ((status != -EAGAIN) || IS_SETLK(cmd))
  5667. break;
  5668. freezable_schedule_timeout_interruptible(timeout);
  5669. timeout *= 2;
  5670. timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
  5671. status = -ERESTARTSYS;
  5672. }
  5673. return status;
  5674. }
  5675. #ifdef CONFIG_NFS_V4_1
  5676. struct nfs4_lock_waiter {
  5677. struct task_struct *task;
  5678. struct inode *inode;
  5679. struct nfs_lowner *owner;
  5680. bool notified;
  5681. };
  5682. static int
  5683. nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
  5684. {
  5685. int ret;
  5686. struct cb_notify_lock_args *cbnl = key;
  5687. struct nfs4_lock_waiter *waiter = wait->private;
  5688. struct nfs_lowner *lowner = &cbnl->cbnl_owner,
  5689. *wowner = waiter->owner;
  5690. /* Only wake if the callback was for the same owner */
  5691. if (lowner->clientid != wowner->clientid ||
  5692. lowner->id != wowner->id ||
  5693. lowner->s_dev != wowner->s_dev)
  5694. return 0;
  5695. /* Make sure it's for the right inode */
  5696. if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
  5697. return 0;
  5698. waiter->notified = true;
  5699. /* override "private" so we can use default_wake_function */
  5700. wait->private = waiter->task;
  5701. ret = autoremove_wake_function(wait, mode, flags, key);
  5702. wait->private = waiter;
  5703. return ret;
  5704. }
  5705. static int
  5706. nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  5707. {
  5708. int status = -ERESTARTSYS;
  5709. unsigned long flags;
  5710. struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
  5711. struct nfs_server *server = NFS_SERVER(state->inode);
  5712. struct nfs_client *clp = server->nfs_client;
  5713. wait_queue_head_t *q = &clp->cl_lock_waitq;
  5714. struct nfs_lowner owner = { .clientid = clp->cl_clientid,
  5715. .id = lsp->ls_seqid.owner_id,
  5716. .s_dev = server->s_dev };
  5717. struct nfs4_lock_waiter waiter = { .task = current,
  5718. .inode = state->inode,
  5719. .owner = &owner,
  5720. .notified = false };
  5721. wait_queue_t wait;
  5722. /* Don't bother with waitqueue if we don't expect a callback */
  5723. if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
  5724. return nfs4_retry_setlk_simple(state, cmd, request);
  5725. init_wait(&wait);
  5726. wait.private = &waiter;
  5727. wait.func = nfs4_wake_lock_waiter;
  5728. add_wait_queue(q, &wait);
  5729. while(!signalled()) {
  5730. status = nfs4_proc_setlk(state, cmd, request);
  5731. if ((status != -EAGAIN) || IS_SETLK(cmd))
  5732. break;
  5733. status = -ERESTARTSYS;
  5734. spin_lock_irqsave(&q->lock, flags);
  5735. if (waiter.notified) {
  5736. spin_unlock_irqrestore(&q->lock, flags);
  5737. continue;
  5738. }
  5739. set_current_state(TASK_INTERRUPTIBLE);
  5740. spin_unlock_irqrestore(&q->lock, flags);
  5741. freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
  5742. }
  5743. finish_wait(q, &wait);
  5744. return status;
  5745. }
  5746. #else /* !CONFIG_NFS_V4_1 */
  5747. static inline int
  5748. nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  5749. {
  5750. return nfs4_retry_setlk_simple(state, cmd, request);
  5751. }
  5752. #endif
  5753. static int
  5754. nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
  5755. {
  5756. struct nfs_open_context *ctx;
  5757. struct nfs4_state *state;
  5758. int status;
  5759. /* verify open state */
  5760. ctx = nfs_file_open_context(filp);
  5761. state = ctx->state;
  5762. if (request->fl_start < 0 || request->fl_end < 0)
  5763. return -EINVAL;
  5764. if (IS_GETLK(cmd)) {
  5765. if (state != NULL)
  5766. return nfs4_proc_getlk(state, F_GETLK, request);
  5767. return 0;
  5768. }
  5769. if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
  5770. return -EINVAL;
  5771. if (request->fl_type == F_UNLCK) {
  5772. if (state != NULL)
  5773. return nfs4_proc_unlck(state, cmd, request);
  5774. return 0;
  5775. }
  5776. if (state == NULL)
  5777. return -ENOLCK;
  5778. if ((request->fl_flags & FL_POSIX) &&
  5779. !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
  5780. return -ENOLCK;
  5781. /*
  5782. * Don't rely on the VFS having checked the file open mode,
  5783. * since it won't do this for flock() locks.
  5784. */
  5785. switch (request->fl_type) {
  5786. case F_RDLCK:
  5787. if (!(filp->f_mode & FMODE_READ))
  5788. return -EBADF;
  5789. break;
  5790. case F_WRLCK:
  5791. if (!(filp->f_mode & FMODE_WRITE))
  5792. return -EBADF;
  5793. }
  5794. status = nfs4_set_lock_state(state, request);
  5795. if (status != 0)
  5796. return status;
  5797. return nfs4_retry_setlk(state, cmd, request);
  5798. }
  5799. int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
  5800. {
  5801. struct nfs_server *server = NFS_SERVER(state->inode);
  5802. int err;
  5803. err = nfs4_set_lock_state(state, fl);
  5804. if (err != 0)
  5805. return err;
  5806. err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
  5807. return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
  5808. }
  5809. struct nfs_release_lockowner_data {
  5810. struct nfs4_lock_state *lsp;
  5811. struct nfs_server *server;
  5812. struct nfs_release_lockowner_args args;
  5813. struct nfs_release_lockowner_res res;
  5814. unsigned long timestamp;
  5815. };
  5816. static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
  5817. {
  5818. struct nfs_release_lockowner_data *data = calldata;
  5819. struct nfs_server *server = data->server;
  5820. nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
  5821. &data->args.seq_args, &data->res.seq_res, task);
  5822. data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
  5823. data->timestamp = jiffies;
  5824. }
  5825. static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
  5826. {
  5827. struct nfs_release_lockowner_data *data = calldata;
  5828. struct nfs_server *server = data->server;
  5829. nfs40_sequence_done(task, &data->res.seq_res);
  5830. switch (task->tk_status) {
  5831. case 0:
  5832. renew_lease(server, data->timestamp);
  5833. break;
  5834. case -NFS4ERR_STALE_CLIENTID:
  5835. case -NFS4ERR_EXPIRED:
  5836. nfs4_schedule_lease_recovery(server->nfs_client);
  5837. break;
  5838. case -NFS4ERR_LEASE_MOVED:
  5839. case -NFS4ERR_DELAY:
  5840. if (nfs4_async_handle_error(task, server,
  5841. NULL, NULL) == -EAGAIN)
  5842. rpc_restart_call_prepare(task);
  5843. }
  5844. }
  5845. static void nfs4_release_lockowner_release(void *calldata)
  5846. {
  5847. struct nfs_release_lockowner_data *data = calldata;
  5848. nfs4_free_lock_state(data->server, data->lsp);
  5849. kfree(calldata);
  5850. }
  5851. static const struct rpc_call_ops nfs4_release_lockowner_ops = {
  5852. .rpc_call_prepare = nfs4_release_lockowner_prepare,
  5853. .rpc_call_done = nfs4_release_lockowner_done,
  5854. .rpc_release = nfs4_release_lockowner_release,
  5855. };
  5856. static void
  5857. nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
  5858. {
  5859. struct nfs_release_lockowner_data *data;
  5860. struct rpc_message msg = {
  5861. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
  5862. };
  5863. if (server->nfs_client->cl_mvops->minor_version != 0)
  5864. return;
  5865. data = kmalloc(sizeof(*data), GFP_NOFS);
  5866. if (!data)
  5867. return;
  5868. data->lsp = lsp;
  5869. data->server = server;
  5870. data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
  5871. data->args.lock_owner.id = lsp->ls_seqid.owner_id;
  5872. data->args.lock_owner.s_dev = server->s_dev;
  5873. msg.rpc_argp = &data->args;
  5874. msg.rpc_resp = &data->res;
  5875. nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
  5876. rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
  5877. }
  5878. #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
  5879. static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
  5880. struct dentry *unused, struct inode *inode,
  5881. const char *key, const void *buf,
  5882. size_t buflen, int flags)
  5883. {
  5884. return nfs4_proc_set_acl(inode, buf, buflen);
  5885. }
  5886. static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
  5887. struct dentry *unused, struct inode *inode,
  5888. const char *key, void *buf, size_t buflen)
  5889. {
  5890. return nfs4_proc_get_acl(inode, buf, buflen);
  5891. }
  5892. static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
  5893. {
  5894. return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
  5895. }
  5896. #ifdef CONFIG_NFS_V4_SECURITY_LABEL
  5897. static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
  5898. struct dentry *unused, struct inode *inode,
  5899. const char *key, const void *buf,
  5900. size_t buflen, int flags)
  5901. {
  5902. if (security_ismaclabel(key))
  5903. return nfs4_set_security_label(inode, buf, buflen);
  5904. return -EOPNOTSUPP;
  5905. }
  5906. static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
  5907. struct dentry *unused, struct inode *inode,
  5908. const char *key, void *buf, size_t buflen)
  5909. {
  5910. if (security_ismaclabel(key))
  5911. return nfs4_get_security_label(inode, buf, buflen);
  5912. return -EOPNOTSUPP;
  5913. }
  5914. static ssize_t
  5915. nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
  5916. {
  5917. int len = 0;
  5918. if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
  5919. len = security_inode_listsecurity(inode, list, list_len);
  5920. if (list_len && len > list_len)
  5921. return -ERANGE;
  5922. }
  5923. return len;
  5924. }
  5925. static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
  5926. .prefix = XATTR_SECURITY_PREFIX,
  5927. .get = nfs4_xattr_get_nfs4_label,
  5928. .set = nfs4_xattr_set_nfs4_label,
  5929. };
  5930. #else
  5931. static ssize_t
  5932. nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
  5933. {
  5934. return 0;
  5935. }
  5936. #endif
  5937. /*
  5938. * nfs_fhget will use either the mounted_on_fileid or the fileid
  5939. */
  5940. static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
  5941. {
  5942. if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
  5943. (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
  5944. (fattr->valid & NFS_ATTR_FATTR_FSID) &&
  5945. (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
  5946. return;
  5947. fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
  5948. NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
  5949. fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
  5950. fattr->nlink = 2;
  5951. }
  5952. static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
  5953. const struct qstr *name,
  5954. struct nfs4_fs_locations *fs_locations,
  5955. struct page *page)
  5956. {
  5957. struct nfs_server *server = NFS_SERVER(dir);
  5958. u32 bitmask[3];
  5959. struct nfs4_fs_locations_arg args = {
  5960. .dir_fh = NFS_FH(dir),
  5961. .name = name,
  5962. .page = page,
  5963. .bitmask = bitmask,
  5964. };
  5965. struct nfs4_fs_locations_res res = {
  5966. .fs_locations = fs_locations,
  5967. };
  5968. struct rpc_message msg = {
  5969. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
  5970. .rpc_argp = &args,
  5971. .rpc_resp = &res,
  5972. };
  5973. int status;
  5974. dprintk("%s: start\n", __func__);
  5975. bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
  5976. bitmask[1] = nfs4_fattr_bitmap[1];
  5977. /* Ask for the fileid of the absent filesystem if mounted_on_fileid
  5978. * is not supported */
  5979. if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
  5980. bitmask[0] &= ~FATTR4_WORD0_FILEID;
  5981. else
  5982. bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
  5983. nfs_fattr_init(&fs_locations->fattr);
  5984. fs_locations->server = server;
  5985. fs_locations->nlocations = 0;
  5986. status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
  5987. dprintk("%s: returned status = %d\n", __func__, status);
  5988. return status;
  5989. }
  5990. int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
  5991. const struct qstr *name,
  5992. struct nfs4_fs_locations *fs_locations,
  5993. struct page *page)
  5994. {
  5995. struct nfs4_exception exception = { };
  5996. int err;
  5997. do {
  5998. err = _nfs4_proc_fs_locations(client, dir, name,
  5999. fs_locations, page);
  6000. trace_nfs4_get_fs_locations(dir, name, err);
  6001. err = nfs4_handle_exception(NFS_SERVER(dir), err,
  6002. &exception);
  6003. } while (exception.retry);
  6004. return err;
  6005. }
  6006. /*
  6007. * This operation also signals the server that this client is
  6008. * performing migration recovery. The server can stop returning
  6009. * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
  6010. * appended to this compound to identify the client ID which is
  6011. * performing recovery.
  6012. */
  6013. static int _nfs40_proc_get_locations(struct inode *inode,
  6014. struct nfs4_fs_locations *locations,
  6015. struct page *page, struct rpc_cred *cred)
  6016. {
  6017. struct nfs_server *server = NFS_SERVER(inode);
  6018. struct rpc_clnt *clnt = server->client;
  6019. u32 bitmask[2] = {
  6020. [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
  6021. };
  6022. struct nfs4_fs_locations_arg args = {
  6023. .clientid = server->nfs_client->cl_clientid,
  6024. .fh = NFS_FH(inode),
  6025. .page = page,
  6026. .bitmask = bitmask,
  6027. .migration = 1, /* skip LOOKUP */
  6028. .renew = 1, /* append RENEW */
  6029. };
  6030. struct nfs4_fs_locations_res res = {
  6031. .fs_locations = locations,
  6032. .migration = 1,
  6033. .renew = 1,
  6034. };
  6035. struct rpc_message msg = {
  6036. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
  6037. .rpc_argp = &args,
  6038. .rpc_resp = &res,
  6039. .rpc_cred = cred,
  6040. };
  6041. unsigned long now = jiffies;
  6042. int status;
  6043. nfs_fattr_init(&locations->fattr);
  6044. locations->server = server;
  6045. locations->nlocations = 0;
  6046. nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
  6047. nfs4_set_sequence_privileged(&args.seq_args);
  6048. status = nfs4_call_sync_sequence(clnt, server, &msg,
  6049. &args.seq_args, &res.seq_res);
  6050. if (status)
  6051. return status;
  6052. renew_lease(server, now);
  6053. return 0;
  6054. }
  6055. #ifdef CONFIG_NFS_V4_1
  6056. /*
  6057. * This operation also signals the server that this client is
  6058. * performing migration recovery. The server can stop asserting
  6059. * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
  6060. * performing this operation is identified in the SEQUENCE
  6061. * operation in this compound.
  6062. *
  6063. * When the client supports GETATTR(fs_locations_info), it can
  6064. * be plumbed in here.
  6065. */
  6066. static int _nfs41_proc_get_locations(struct inode *inode,
  6067. struct nfs4_fs_locations *locations,
  6068. struct page *page, struct rpc_cred *cred)
  6069. {
  6070. struct nfs_server *server = NFS_SERVER(inode);
  6071. struct rpc_clnt *clnt = server->client;
  6072. u32 bitmask[2] = {
  6073. [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
  6074. };
  6075. struct nfs4_fs_locations_arg args = {
  6076. .fh = NFS_FH(inode),
  6077. .page = page,
  6078. .bitmask = bitmask,
  6079. .migration = 1, /* skip LOOKUP */
  6080. };
  6081. struct nfs4_fs_locations_res res = {
  6082. .fs_locations = locations,
  6083. .migration = 1,
  6084. };
  6085. struct rpc_message msg = {
  6086. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
  6087. .rpc_argp = &args,
  6088. .rpc_resp = &res,
  6089. .rpc_cred = cred,
  6090. };
  6091. int status;
  6092. nfs_fattr_init(&locations->fattr);
  6093. locations->server = server;
  6094. locations->nlocations = 0;
  6095. nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
  6096. nfs4_set_sequence_privileged(&args.seq_args);
  6097. status = nfs4_call_sync_sequence(clnt, server, &msg,
  6098. &args.seq_args, &res.seq_res);
  6099. if (status == NFS4_OK &&
  6100. res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
  6101. status = -NFS4ERR_LEASE_MOVED;
  6102. return status;
  6103. }
  6104. #endif /* CONFIG_NFS_V4_1 */
  6105. /**
  6106. * nfs4_proc_get_locations - discover locations for a migrated FSID
  6107. * @inode: inode on FSID that is migrating
  6108. * @locations: result of query
  6109. * @page: buffer
  6110. * @cred: credential to use for this operation
  6111. *
  6112. * Returns NFS4_OK on success, a negative NFS4ERR status code if the
  6113. * operation failed, or a negative errno if a local error occurred.
  6114. *
  6115. * On success, "locations" is filled in, but if the server has
  6116. * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
  6117. * asserted.
  6118. *
  6119. * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
  6120. * from this client that require migration recovery.
  6121. */
  6122. int nfs4_proc_get_locations(struct inode *inode,
  6123. struct nfs4_fs_locations *locations,
  6124. struct page *page, struct rpc_cred *cred)
  6125. {
  6126. struct nfs_server *server = NFS_SERVER(inode);
  6127. struct nfs_client *clp = server->nfs_client;
  6128. const struct nfs4_mig_recovery_ops *ops =
  6129. clp->cl_mvops->mig_recovery_ops;
  6130. struct nfs4_exception exception = { };
  6131. int status;
  6132. dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
  6133. (unsigned long long)server->fsid.major,
  6134. (unsigned long long)server->fsid.minor,
  6135. clp->cl_hostname);
  6136. nfs_display_fhandle(NFS_FH(inode), __func__);
  6137. do {
  6138. status = ops->get_locations(inode, locations, page, cred);
  6139. if (status != -NFS4ERR_DELAY)
  6140. break;
  6141. nfs4_handle_exception(server, status, &exception);
  6142. } while (exception.retry);
  6143. return status;
  6144. }
  6145. /*
  6146. * This operation also signals the server that this client is
  6147. * performing "lease moved" recovery. The server can stop
  6148. * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
  6149. * is appended to this compound to identify the client ID which is
  6150. * performing recovery.
  6151. */
  6152. static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
  6153. {
  6154. struct nfs_server *server = NFS_SERVER(inode);
  6155. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  6156. struct rpc_clnt *clnt = server->client;
  6157. struct nfs4_fsid_present_arg args = {
  6158. .fh = NFS_FH(inode),
  6159. .clientid = clp->cl_clientid,
  6160. .renew = 1, /* append RENEW */
  6161. };
  6162. struct nfs4_fsid_present_res res = {
  6163. .renew = 1,
  6164. };
  6165. struct rpc_message msg = {
  6166. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
  6167. .rpc_argp = &args,
  6168. .rpc_resp = &res,
  6169. .rpc_cred = cred,
  6170. };
  6171. unsigned long now = jiffies;
  6172. int status;
  6173. res.fh = nfs_alloc_fhandle();
  6174. if (res.fh == NULL)
  6175. return -ENOMEM;
  6176. nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
  6177. nfs4_set_sequence_privileged(&args.seq_args);
  6178. status = nfs4_call_sync_sequence(clnt, server, &msg,
  6179. &args.seq_args, &res.seq_res);
  6180. nfs_free_fhandle(res.fh);
  6181. if (status)
  6182. return status;
  6183. do_renew_lease(clp, now);
  6184. return 0;
  6185. }
  6186. #ifdef CONFIG_NFS_V4_1
  6187. /*
  6188. * This operation also signals the server that this client is
  6189. * performing "lease moved" recovery. The server can stop asserting
  6190. * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
  6191. * this operation is identified in the SEQUENCE operation in this
  6192. * compound.
  6193. */
  6194. static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
  6195. {
  6196. struct nfs_server *server = NFS_SERVER(inode);
  6197. struct rpc_clnt *clnt = server->client;
  6198. struct nfs4_fsid_present_arg args = {
  6199. .fh = NFS_FH(inode),
  6200. };
  6201. struct nfs4_fsid_present_res res = {
  6202. };
  6203. struct rpc_message msg = {
  6204. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
  6205. .rpc_argp = &args,
  6206. .rpc_resp = &res,
  6207. .rpc_cred = cred,
  6208. };
  6209. int status;
  6210. res.fh = nfs_alloc_fhandle();
  6211. if (res.fh == NULL)
  6212. return -ENOMEM;
  6213. nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
  6214. nfs4_set_sequence_privileged(&args.seq_args);
  6215. status = nfs4_call_sync_sequence(clnt, server, &msg,
  6216. &args.seq_args, &res.seq_res);
  6217. nfs_free_fhandle(res.fh);
  6218. if (status == NFS4_OK &&
  6219. res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
  6220. status = -NFS4ERR_LEASE_MOVED;
  6221. return status;
  6222. }
  6223. #endif /* CONFIG_NFS_V4_1 */
  6224. /**
  6225. * nfs4_proc_fsid_present - Is this FSID present or absent on server?
  6226. * @inode: inode on FSID to check
  6227. * @cred: credential to use for this operation
  6228. *
  6229. * Server indicates whether the FSID is present, moved, or not
  6230. * recognized. This operation is necessary to clear a LEASE_MOVED
  6231. * condition for this client ID.
  6232. *
  6233. * Returns NFS4_OK if the FSID is present on this server,
  6234. * -NFS4ERR_MOVED if the FSID is no longer present, a negative
  6235. * NFS4ERR code if some error occurred on the server, or a
  6236. * negative errno if a local failure occurred.
  6237. */
  6238. int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
  6239. {
  6240. struct nfs_server *server = NFS_SERVER(inode);
  6241. struct nfs_client *clp = server->nfs_client;
  6242. const struct nfs4_mig_recovery_ops *ops =
  6243. clp->cl_mvops->mig_recovery_ops;
  6244. struct nfs4_exception exception = { };
  6245. int status;
  6246. dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
  6247. (unsigned long long)server->fsid.major,
  6248. (unsigned long long)server->fsid.minor,
  6249. clp->cl_hostname);
  6250. nfs_display_fhandle(NFS_FH(inode), __func__);
  6251. do {
  6252. status = ops->fsid_present(inode, cred);
  6253. if (status != -NFS4ERR_DELAY)
  6254. break;
  6255. nfs4_handle_exception(server, status, &exception);
  6256. } while (exception.retry);
  6257. return status;
  6258. }
  6259. /**
  6260. * If 'use_integrity' is true and the state managment nfs_client
  6261. * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
  6262. * and the machine credential as per RFC3530bis and RFC5661 Security
  6263. * Considerations sections. Otherwise, just use the user cred with the
  6264. * filesystem's rpc_client.
  6265. */
  6266. static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
  6267. {
  6268. int status;
  6269. struct nfs4_secinfo_arg args = {
  6270. .dir_fh = NFS_FH(dir),
  6271. .name = name,
  6272. };
  6273. struct nfs4_secinfo_res res = {
  6274. .flavors = flavors,
  6275. };
  6276. struct rpc_message msg = {
  6277. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
  6278. .rpc_argp = &args,
  6279. .rpc_resp = &res,
  6280. };
  6281. struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
  6282. struct rpc_cred *cred = NULL;
  6283. if (use_integrity) {
  6284. clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
  6285. cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
  6286. msg.rpc_cred = cred;
  6287. }
  6288. dprintk("NFS call secinfo %s\n", name->name);
  6289. nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
  6290. NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
  6291. status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
  6292. &res.seq_res, 0);
  6293. dprintk("NFS reply secinfo: %d\n", status);
  6294. if (cred)
  6295. put_rpccred(cred);
  6296. return status;
  6297. }
  6298. int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
  6299. struct nfs4_secinfo_flavors *flavors)
  6300. {
  6301. struct nfs4_exception exception = { };
  6302. int err;
  6303. do {
  6304. err = -NFS4ERR_WRONGSEC;
  6305. /* try to use integrity protection with machine cred */
  6306. if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
  6307. err = _nfs4_proc_secinfo(dir, name, flavors, true);
  6308. /*
  6309. * if unable to use integrity protection, or SECINFO with
  6310. * integrity protection returns NFS4ERR_WRONGSEC (which is
  6311. * disallowed by spec, but exists in deployed servers) use
  6312. * the current filesystem's rpc_client and the user cred.
  6313. */
  6314. if (err == -NFS4ERR_WRONGSEC)
  6315. err = _nfs4_proc_secinfo(dir, name, flavors, false);
  6316. trace_nfs4_secinfo(dir, name, err);
  6317. err = nfs4_handle_exception(NFS_SERVER(dir), err,
  6318. &exception);
  6319. } while (exception.retry);
  6320. return err;
  6321. }
  6322. #ifdef CONFIG_NFS_V4_1
  6323. /*
  6324. * Check the exchange flags returned by the server for invalid flags, having
  6325. * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
  6326. * DS flags set.
  6327. */
  6328. static int nfs4_check_cl_exchange_flags(u32 flags)
  6329. {
  6330. if (flags & ~EXCHGID4_FLAG_MASK_R)
  6331. goto out_inval;
  6332. if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
  6333. (flags & EXCHGID4_FLAG_USE_NON_PNFS))
  6334. goto out_inval;
  6335. if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
  6336. goto out_inval;
  6337. return NFS_OK;
  6338. out_inval:
  6339. return -NFS4ERR_INVAL;
  6340. }
  6341. static bool
  6342. nfs41_same_server_scope(struct nfs41_server_scope *a,
  6343. struct nfs41_server_scope *b)
  6344. {
  6345. if (a->server_scope_sz == b->server_scope_sz &&
  6346. memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
  6347. return true;
  6348. return false;
  6349. }
  6350. static void
  6351. nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
  6352. {
  6353. }
  6354. static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
  6355. .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
  6356. };
  6357. /*
  6358. * nfs4_proc_bind_one_conn_to_session()
  6359. *
  6360. * The 4.1 client currently uses the same TCP connection for the
  6361. * fore and backchannel.
  6362. */
  6363. static
  6364. int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
  6365. struct rpc_xprt *xprt,
  6366. struct nfs_client *clp,
  6367. struct rpc_cred *cred)
  6368. {
  6369. int status;
  6370. struct nfs41_bind_conn_to_session_args args = {
  6371. .client = clp,
  6372. .dir = NFS4_CDFC4_FORE_OR_BOTH,
  6373. };
  6374. struct nfs41_bind_conn_to_session_res res;
  6375. struct rpc_message msg = {
  6376. .rpc_proc =
  6377. &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
  6378. .rpc_argp = &args,
  6379. .rpc_resp = &res,
  6380. .rpc_cred = cred,
  6381. };
  6382. struct rpc_task_setup task_setup_data = {
  6383. .rpc_client = clnt,
  6384. .rpc_xprt = xprt,
  6385. .callback_ops = &nfs4_bind_one_conn_to_session_ops,
  6386. .rpc_message = &msg,
  6387. .flags = RPC_TASK_TIMEOUT,
  6388. };
  6389. struct rpc_task *task;
  6390. dprintk("--> %s\n", __func__);
  6391. nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
  6392. if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
  6393. args.dir = NFS4_CDFC4_FORE;
  6394. /* Do not set the backchannel flag unless this is clnt->cl_xprt */
  6395. if (xprt != rcu_access_pointer(clnt->cl_xprt))
  6396. args.dir = NFS4_CDFC4_FORE;
  6397. task = rpc_run_task(&task_setup_data);
  6398. if (!IS_ERR(task)) {
  6399. status = task->tk_status;
  6400. rpc_put_task(task);
  6401. } else
  6402. status = PTR_ERR(task);
  6403. trace_nfs4_bind_conn_to_session(clp, status);
  6404. if (status == 0) {
  6405. if (memcmp(res.sessionid.data,
  6406. clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
  6407. dprintk("NFS: %s: Session ID mismatch\n", __func__);
  6408. status = -EIO;
  6409. goto out;
  6410. }
  6411. if ((res.dir & args.dir) != res.dir || res.dir == 0) {
  6412. dprintk("NFS: %s: Unexpected direction from server\n",
  6413. __func__);
  6414. status = -EIO;
  6415. goto out;
  6416. }
  6417. if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
  6418. dprintk("NFS: %s: Server returned RDMA mode = true\n",
  6419. __func__);
  6420. status = -EIO;
  6421. goto out;
  6422. }
  6423. }
  6424. out:
  6425. dprintk("<-- %s status= %d\n", __func__, status);
  6426. return status;
  6427. }
  6428. struct rpc_bind_conn_calldata {
  6429. struct nfs_client *clp;
  6430. struct rpc_cred *cred;
  6431. };
  6432. static int
  6433. nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
  6434. struct rpc_xprt *xprt,
  6435. void *calldata)
  6436. {
  6437. struct rpc_bind_conn_calldata *p = calldata;
  6438. return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
  6439. }
  6440. int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
  6441. {
  6442. struct rpc_bind_conn_calldata data = {
  6443. .clp = clp,
  6444. .cred = cred,
  6445. };
  6446. return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
  6447. nfs4_proc_bind_conn_to_session_callback, &data);
  6448. }
  6449. /*
  6450. * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
  6451. * and operations we'd like to see to enable certain features in the allow map
  6452. */
  6453. static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
  6454. .how = SP4_MACH_CRED,
  6455. .enforce.u.words = {
  6456. [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
  6457. 1 << (OP_EXCHANGE_ID - 32) |
  6458. 1 << (OP_CREATE_SESSION - 32) |
  6459. 1 << (OP_DESTROY_SESSION - 32) |
  6460. 1 << (OP_DESTROY_CLIENTID - 32)
  6461. },
  6462. .allow.u.words = {
  6463. [0] = 1 << (OP_CLOSE) |
  6464. 1 << (OP_OPEN_DOWNGRADE) |
  6465. 1 << (OP_LOCKU) |
  6466. 1 << (OP_DELEGRETURN) |
  6467. 1 << (OP_COMMIT),
  6468. [1] = 1 << (OP_SECINFO - 32) |
  6469. 1 << (OP_SECINFO_NO_NAME - 32) |
  6470. 1 << (OP_LAYOUTRETURN - 32) |
  6471. 1 << (OP_TEST_STATEID - 32) |
  6472. 1 << (OP_FREE_STATEID - 32) |
  6473. 1 << (OP_WRITE - 32)
  6474. }
  6475. };
  6476. /*
  6477. * Select the state protection mode for client `clp' given the server results
  6478. * from exchange_id in `sp'.
  6479. *
  6480. * Returns 0 on success, negative errno otherwise.
  6481. */
  6482. static int nfs4_sp4_select_mode(struct nfs_client *clp,
  6483. struct nfs41_state_protection *sp)
  6484. {
  6485. static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
  6486. [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
  6487. 1 << (OP_EXCHANGE_ID - 32) |
  6488. 1 << (OP_CREATE_SESSION - 32) |
  6489. 1 << (OP_DESTROY_SESSION - 32) |
  6490. 1 << (OP_DESTROY_CLIENTID - 32)
  6491. };
  6492. unsigned int i;
  6493. if (sp->how == SP4_MACH_CRED) {
  6494. /* Print state protect result */
  6495. dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
  6496. for (i = 0; i <= LAST_NFS4_OP; i++) {
  6497. if (test_bit(i, sp->enforce.u.longs))
  6498. dfprintk(MOUNT, " enforce op %d\n", i);
  6499. if (test_bit(i, sp->allow.u.longs))
  6500. dfprintk(MOUNT, " allow op %d\n", i);
  6501. }
  6502. /* make sure nothing is on enforce list that isn't supported */
  6503. for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
  6504. if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
  6505. dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
  6506. return -EINVAL;
  6507. }
  6508. }
  6509. /*
  6510. * Minimal mode - state operations are allowed to use machine
  6511. * credential. Note this already happens by default, so the
  6512. * client doesn't have to do anything more than the negotiation.
  6513. *
  6514. * NOTE: we don't care if EXCHANGE_ID is in the list -
  6515. * we're already using the machine cred for exchange_id
  6516. * and will never use a different cred.
  6517. */
  6518. if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
  6519. test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
  6520. test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
  6521. test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
  6522. dfprintk(MOUNT, "sp4_mach_cred:\n");
  6523. dfprintk(MOUNT, " minimal mode enabled\n");
  6524. set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
  6525. } else {
  6526. dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
  6527. return -EINVAL;
  6528. }
  6529. if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
  6530. test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
  6531. test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
  6532. test_bit(OP_LOCKU, sp->allow.u.longs)) {
  6533. dfprintk(MOUNT, " cleanup mode enabled\n");
  6534. set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
  6535. }
  6536. if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
  6537. dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
  6538. set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
  6539. &clp->cl_sp4_flags);
  6540. }
  6541. if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
  6542. test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
  6543. dfprintk(MOUNT, " secinfo mode enabled\n");
  6544. set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
  6545. }
  6546. if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
  6547. test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
  6548. dfprintk(MOUNT, " stateid mode enabled\n");
  6549. set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
  6550. }
  6551. if (test_bit(OP_WRITE, sp->allow.u.longs)) {
  6552. dfprintk(MOUNT, " write mode enabled\n");
  6553. set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
  6554. }
  6555. if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
  6556. dfprintk(MOUNT, " commit mode enabled\n");
  6557. set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
  6558. }
  6559. }
  6560. return 0;
  6561. }
  6562. struct nfs41_exchange_id_data {
  6563. struct nfs41_exchange_id_res res;
  6564. struct nfs41_exchange_id_args args;
  6565. struct rpc_xprt *xprt;
  6566. int rpc_status;
  6567. };
  6568. static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
  6569. {
  6570. struct nfs41_exchange_id_data *cdata =
  6571. (struct nfs41_exchange_id_data *)data;
  6572. struct nfs_client *clp = cdata->args.client;
  6573. int status = task->tk_status;
  6574. trace_nfs4_exchange_id(clp, status);
  6575. if (status == 0)
  6576. status = nfs4_check_cl_exchange_flags(cdata->res.flags);
  6577. if (cdata->xprt && status == 0) {
  6578. status = nfs4_detect_session_trunking(clp, &cdata->res,
  6579. cdata->xprt);
  6580. goto out;
  6581. }
  6582. if (status == 0)
  6583. status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
  6584. if (status == 0) {
  6585. clp->cl_clientid = cdata->res.clientid;
  6586. clp->cl_exchange_flags = cdata->res.flags;
  6587. /* Client ID is not confirmed */
  6588. if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
  6589. clear_bit(NFS4_SESSION_ESTABLISHED,
  6590. &clp->cl_session->session_state);
  6591. clp->cl_seqid = cdata->res.seqid;
  6592. }
  6593. kfree(clp->cl_serverowner);
  6594. clp->cl_serverowner = cdata->res.server_owner;
  6595. cdata->res.server_owner = NULL;
  6596. /* use the most recent implementation id */
  6597. kfree(clp->cl_implid);
  6598. clp->cl_implid = cdata->res.impl_id;
  6599. cdata->res.impl_id = NULL;
  6600. if (clp->cl_serverscope != NULL &&
  6601. !nfs41_same_server_scope(clp->cl_serverscope,
  6602. cdata->res.server_scope)) {
  6603. dprintk("%s: server_scope mismatch detected\n",
  6604. __func__);
  6605. set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
  6606. kfree(clp->cl_serverscope);
  6607. clp->cl_serverscope = NULL;
  6608. }
  6609. if (clp->cl_serverscope == NULL) {
  6610. clp->cl_serverscope = cdata->res.server_scope;
  6611. cdata->res.server_scope = NULL;
  6612. }
  6613. /* Save the EXCHANGE_ID verifier session trunk tests */
  6614. memcpy(clp->cl_confirm.data, cdata->args.verifier.data,
  6615. sizeof(clp->cl_confirm.data));
  6616. }
  6617. out:
  6618. cdata->rpc_status = status;
  6619. return;
  6620. }
  6621. static void nfs4_exchange_id_release(void *data)
  6622. {
  6623. struct nfs41_exchange_id_data *cdata =
  6624. (struct nfs41_exchange_id_data *)data;
  6625. if (cdata->xprt) {
  6626. xprt_put(cdata->xprt);
  6627. rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
  6628. }
  6629. nfs_put_client(cdata->args.client);
  6630. kfree(cdata->res.impl_id);
  6631. kfree(cdata->res.server_scope);
  6632. kfree(cdata->res.server_owner);
  6633. kfree(cdata);
  6634. }
  6635. static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
  6636. .rpc_call_done = nfs4_exchange_id_done,
  6637. .rpc_release = nfs4_exchange_id_release,
  6638. };
  6639. /*
  6640. * _nfs4_proc_exchange_id()
  6641. *
  6642. * Wrapper for EXCHANGE_ID operation.
  6643. */
  6644. static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
  6645. u32 sp4_how, struct rpc_xprt *xprt)
  6646. {
  6647. struct rpc_message msg = {
  6648. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
  6649. .rpc_cred = cred,
  6650. };
  6651. struct rpc_task_setup task_setup_data = {
  6652. .rpc_client = clp->cl_rpcclient,
  6653. .callback_ops = &nfs4_exchange_id_call_ops,
  6654. .rpc_message = &msg,
  6655. .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
  6656. };
  6657. struct nfs41_exchange_id_data *calldata;
  6658. struct rpc_task *task;
  6659. int status = -EIO;
  6660. if (!atomic_inc_not_zero(&clp->cl_count))
  6661. goto out;
  6662. status = -ENOMEM;
  6663. calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
  6664. if (!calldata)
  6665. goto out;
  6666. nfs4_init_boot_verifier(clp, &calldata->args.verifier);
  6667. status = nfs4_init_uniform_client_string(clp);
  6668. if (status)
  6669. goto out_calldata;
  6670. dprintk("NFS call exchange_id auth=%s, '%s'\n",
  6671. clp->cl_rpcclient->cl_auth->au_ops->au_name,
  6672. clp->cl_owner_id);
  6673. calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
  6674. GFP_NOFS);
  6675. status = -ENOMEM;
  6676. if (unlikely(calldata->res.server_owner == NULL))
  6677. goto out_calldata;
  6678. calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
  6679. GFP_NOFS);
  6680. if (unlikely(calldata->res.server_scope == NULL))
  6681. goto out_server_owner;
  6682. calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
  6683. if (unlikely(calldata->res.impl_id == NULL))
  6684. goto out_server_scope;
  6685. switch (sp4_how) {
  6686. case SP4_NONE:
  6687. calldata->args.state_protect.how = SP4_NONE;
  6688. break;
  6689. case SP4_MACH_CRED:
  6690. calldata->args.state_protect = nfs4_sp4_mach_cred_request;
  6691. break;
  6692. default:
  6693. /* unsupported! */
  6694. WARN_ON_ONCE(1);
  6695. status = -EINVAL;
  6696. goto out_impl_id;
  6697. }
  6698. if (xprt) {
  6699. calldata->xprt = xprt;
  6700. task_setup_data.rpc_xprt = xprt;
  6701. task_setup_data.flags =
  6702. RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
  6703. memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
  6704. sizeof(calldata->args.verifier.data));
  6705. }
  6706. calldata->args.client = clp;
  6707. #ifdef CONFIG_NFS_V4_1_MIGRATION
  6708. calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
  6709. EXCHGID4_FLAG_BIND_PRINC_STATEID |
  6710. EXCHGID4_FLAG_SUPP_MOVED_MIGR,
  6711. #else
  6712. calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
  6713. EXCHGID4_FLAG_BIND_PRINC_STATEID,
  6714. #endif
  6715. msg.rpc_argp = &calldata->args;
  6716. msg.rpc_resp = &calldata->res;
  6717. task_setup_data.callback_data = calldata;
  6718. task = rpc_run_task(&task_setup_data);
  6719. if (IS_ERR(task))
  6720. return PTR_ERR(task);
  6721. if (!xprt) {
  6722. status = rpc_wait_for_completion_task(task);
  6723. if (!status)
  6724. status = calldata->rpc_status;
  6725. } else /* session trunking test */
  6726. status = calldata->rpc_status;
  6727. rpc_put_task(task);
  6728. out:
  6729. if (clp->cl_implid != NULL)
  6730. dprintk("NFS reply exchange_id: Server Implementation ID: "
  6731. "domain: %s, name: %s, date: %llu,%u\n",
  6732. clp->cl_implid->domain, clp->cl_implid->name,
  6733. clp->cl_implid->date.seconds,
  6734. clp->cl_implid->date.nseconds);
  6735. dprintk("NFS reply exchange_id: %d\n", status);
  6736. return status;
  6737. out_impl_id:
  6738. kfree(calldata->res.impl_id);
  6739. out_server_scope:
  6740. kfree(calldata->res.server_scope);
  6741. out_server_owner:
  6742. kfree(calldata->res.server_owner);
  6743. out_calldata:
  6744. kfree(calldata);
  6745. nfs_put_client(clp);
  6746. goto out;
  6747. }
  6748. /*
  6749. * nfs4_proc_exchange_id()
  6750. *
  6751. * Returns zero, a negative errno, or a negative NFS4ERR status code.
  6752. *
  6753. * Since the clientid has expired, all compounds using sessions
  6754. * associated with the stale clientid will be returning
  6755. * NFS4ERR_BADSESSION in the sequence operation, and will therefore
  6756. * be in some phase of session reset.
  6757. *
  6758. * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
  6759. */
  6760. int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
  6761. {
  6762. rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
  6763. int status;
  6764. /* try SP4_MACH_CRED if krb5i/p */
  6765. if (authflavor == RPC_AUTH_GSS_KRB5I ||
  6766. authflavor == RPC_AUTH_GSS_KRB5P) {
  6767. status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
  6768. if (!status)
  6769. return 0;
  6770. }
  6771. /* try SP4_NONE */
  6772. return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
  6773. }
  6774. /**
  6775. * nfs4_test_session_trunk
  6776. *
  6777. * This is an add_xprt_test() test function called from
  6778. * rpc_clnt_setup_test_and_add_xprt.
  6779. *
  6780. * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
  6781. * and is dereferrenced in nfs4_exchange_id_release
  6782. *
  6783. * Upon success, add the new transport to the rpc_clnt
  6784. *
  6785. * @clnt: struct rpc_clnt to get new transport
  6786. * @xprt: the rpc_xprt to test
  6787. * @data: call data for _nfs4_proc_exchange_id.
  6788. */
  6789. int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
  6790. void *data)
  6791. {
  6792. struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
  6793. u32 sp4_how;
  6794. dprintk("--> %s try %s\n", __func__,
  6795. xprt->address_strings[RPC_DISPLAY_ADDR]);
  6796. sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
  6797. /* Test connection for session trunking. Async exchange_id call */
  6798. return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
  6799. }
  6800. EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
  6801. static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
  6802. struct rpc_cred *cred)
  6803. {
  6804. struct rpc_message msg = {
  6805. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
  6806. .rpc_argp = clp,
  6807. .rpc_cred = cred,
  6808. };
  6809. int status;
  6810. status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  6811. trace_nfs4_destroy_clientid(clp, status);
  6812. if (status)
  6813. dprintk("NFS: Got error %d from the server %s on "
  6814. "DESTROY_CLIENTID.", status, clp->cl_hostname);
  6815. return status;
  6816. }
  6817. static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
  6818. struct rpc_cred *cred)
  6819. {
  6820. unsigned int loop;
  6821. int ret;
  6822. for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
  6823. ret = _nfs4_proc_destroy_clientid(clp, cred);
  6824. switch (ret) {
  6825. case -NFS4ERR_DELAY:
  6826. case -NFS4ERR_CLIENTID_BUSY:
  6827. ssleep(1);
  6828. break;
  6829. default:
  6830. return ret;
  6831. }
  6832. }
  6833. return 0;
  6834. }
  6835. int nfs4_destroy_clientid(struct nfs_client *clp)
  6836. {
  6837. struct rpc_cred *cred;
  6838. int ret = 0;
  6839. if (clp->cl_mvops->minor_version < 1)
  6840. goto out;
  6841. if (clp->cl_exchange_flags == 0)
  6842. goto out;
  6843. if (clp->cl_preserve_clid)
  6844. goto out;
  6845. cred = nfs4_get_clid_cred(clp);
  6846. ret = nfs4_proc_destroy_clientid(clp, cred);
  6847. if (cred)
  6848. put_rpccred(cred);
  6849. switch (ret) {
  6850. case 0:
  6851. case -NFS4ERR_STALE_CLIENTID:
  6852. clp->cl_exchange_flags = 0;
  6853. }
  6854. out:
  6855. return ret;
  6856. }
  6857. struct nfs4_get_lease_time_data {
  6858. struct nfs4_get_lease_time_args *args;
  6859. struct nfs4_get_lease_time_res *res;
  6860. struct nfs_client *clp;
  6861. };
  6862. static void nfs4_get_lease_time_prepare(struct rpc_task *task,
  6863. void *calldata)
  6864. {
  6865. struct nfs4_get_lease_time_data *data =
  6866. (struct nfs4_get_lease_time_data *)calldata;
  6867. dprintk("--> %s\n", __func__);
  6868. /* just setup sequence, do not trigger session recovery
  6869. since we're invoked within one */
  6870. nfs41_setup_sequence(data->clp->cl_session,
  6871. &data->args->la_seq_args,
  6872. &data->res->lr_seq_res,
  6873. task);
  6874. dprintk("<-- %s\n", __func__);
  6875. }
  6876. /*
  6877. * Called from nfs4_state_manager thread for session setup, so don't recover
  6878. * from sequence operation or clientid errors.
  6879. */
  6880. static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
  6881. {
  6882. struct nfs4_get_lease_time_data *data =
  6883. (struct nfs4_get_lease_time_data *)calldata;
  6884. dprintk("--> %s\n", __func__);
  6885. if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
  6886. return;
  6887. switch (task->tk_status) {
  6888. case -NFS4ERR_DELAY:
  6889. case -NFS4ERR_GRACE:
  6890. dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
  6891. rpc_delay(task, NFS4_POLL_RETRY_MIN);
  6892. task->tk_status = 0;
  6893. /* fall through */
  6894. case -NFS4ERR_RETRY_UNCACHED_REP:
  6895. rpc_restart_call_prepare(task);
  6896. return;
  6897. }
  6898. dprintk("<-- %s\n", __func__);
  6899. }
  6900. static const struct rpc_call_ops nfs4_get_lease_time_ops = {
  6901. .rpc_call_prepare = nfs4_get_lease_time_prepare,
  6902. .rpc_call_done = nfs4_get_lease_time_done,
  6903. };
  6904. int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
  6905. {
  6906. struct rpc_task *task;
  6907. struct nfs4_get_lease_time_args args;
  6908. struct nfs4_get_lease_time_res res = {
  6909. .lr_fsinfo = fsinfo,
  6910. };
  6911. struct nfs4_get_lease_time_data data = {
  6912. .args = &args,
  6913. .res = &res,
  6914. .clp = clp,
  6915. };
  6916. struct rpc_message msg = {
  6917. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
  6918. .rpc_argp = &args,
  6919. .rpc_resp = &res,
  6920. };
  6921. struct rpc_task_setup task_setup = {
  6922. .rpc_client = clp->cl_rpcclient,
  6923. .rpc_message = &msg,
  6924. .callback_ops = &nfs4_get_lease_time_ops,
  6925. .callback_data = &data,
  6926. .flags = RPC_TASK_TIMEOUT,
  6927. };
  6928. int status;
  6929. nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
  6930. nfs4_set_sequence_privileged(&args.la_seq_args);
  6931. dprintk("--> %s\n", __func__);
  6932. task = rpc_run_task(&task_setup);
  6933. if (IS_ERR(task))
  6934. status = PTR_ERR(task);
  6935. else {
  6936. status = task->tk_status;
  6937. rpc_put_task(task);
  6938. }
  6939. dprintk("<-- %s return %d\n", __func__, status);
  6940. return status;
  6941. }
  6942. /*
  6943. * Initialize the values to be used by the client in CREATE_SESSION
  6944. * If nfs4_init_session set the fore channel request and response sizes,
  6945. * use them.
  6946. *
  6947. * Set the back channel max_resp_sz_cached to zero to force the client to
  6948. * always set csa_cachethis to FALSE because the current implementation
  6949. * of the back channel DRC only supports caching the CB_SEQUENCE operation.
  6950. */
  6951. static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
  6952. struct rpc_clnt *clnt)
  6953. {
  6954. unsigned int max_rqst_sz, max_resp_sz;
  6955. unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
  6956. max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
  6957. max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
  6958. /* Fore channel attributes */
  6959. args->fc_attrs.max_rqst_sz = max_rqst_sz;
  6960. args->fc_attrs.max_resp_sz = max_resp_sz;
  6961. args->fc_attrs.max_ops = NFS4_MAX_OPS;
  6962. args->fc_attrs.max_reqs = max_session_slots;
  6963. dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
  6964. "max_ops=%u max_reqs=%u\n",
  6965. __func__,
  6966. args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
  6967. args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
  6968. /* Back channel attributes */
  6969. args->bc_attrs.max_rqst_sz = max_bc_payload;
  6970. args->bc_attrs.max_resp_sz = max_bc_payload;
  6971. args->bc_attrs.max_resp_sz_cached = 0;
  6972. args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
  6973. args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
  6974. dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
  6975. "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
  6976. __func__,
  6977. args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
  6978. args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
  6979. args->bc_attrs.max_reqs);
  6980. }
  6981. static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
  6982. struct nfs41_create_session_res *res)
  6983. {
  6984. struct nfs4_channel_attrs *sent = &args->fc_attrs;
  6985. struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
  6986. if (rcvd->max_resp_sz > sent->max_resp_sz)
  6987. return -EINVAL;
  6988. /*
  6989. * Our requested max_ops is the minimum we need; we're not
  6990. * prepared to break up compounds into smaller pieces than that.
  6991. * So, no point even trying to continue if the server won't
  6992. * cooperate:
  6993. */
  6994. if (rcvd->max_ops < sent->max_ops)
  6995. return -EINVAL;
  6996. if (rcvd->max_reqs == 0)
  6997. return -EINVAL;
  6998. if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
  6999. rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
  7000. return 0;
  7001. }
  7002. static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
  7003. struct nfs41_create_session_res *res)
  7004. {
  7005. struct nfs4_channel_attrs *sent = &args->bc_attrs;
  7006. struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
  7007. if (!(res->flags & SESSION4_BACK_CHAN))
  7008. goto out;
  7009. if (rcvd->max_rqst_sz > sent->max_rqst_sz)
  7010. return -EINVAL;
  7011. if (rcvd->max_resp_sz < sent->max_resp_sz)
  7012. return -EINVAL;
  7013. if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
  7014. return -EINVAL;
  7015. if (rcvd->max_ops > sent->max_ops)
  7016. return -EINVAL;
  7017. if (rcvd->max_reqs > sent->max_reqs)
  7018. return -EINVAL;
  7019. out:
  7020. return 0;
  7021. }
  7022. static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
  7023. struct nfs41_create_session_res *res)
  7024. {
  7025. int ret;
  7026. ret = nfs4_verify_fore_channel_attrs(args, res);
  7027. if (ret)
  7028. return ret;
  7029. return nfs4_verify_back_channel_attrs(args, res);
  7030. }
  7031. static void nfs4_update_session(struct nfs4_session *session,
  7032. struct nfs41_create_session_res *res)
  7033. {
  7034. nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
  7035. /* Mark client id and session as being confirmed */
  7036. session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
  7037. set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
  7038. session->flags = res->flags;
  7039. memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
  7040. if (res->flags & SESSION4_BACK_CHAN)
  7041. memcpy(&session->bc_attrs, &res->bc_attrs,
  7042. sizeof(session->bc_attrs));
  7043. }
  7044. static int _nfs4_proc_create_session(struct nfs_client *clp,
  7045. struct rpc_cred *cred)
  7046. {
  7047. struct nfs4_session *session = clp->cl_session;
  7048. struct nfs41_create_session_args args = {
  7049. .client = clp,
  7050. .clientid = clp->cl_clientid,
  7051. .seqid = clp->cl_seqid,
  7052. .cb_program = NFS4_CALLBACK,
  7053. };
  7054. struct nfs41_create_session_res res;
  7055. struct rpc_message msg = {
  7056. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
  7057. .rpc_argp = &args,
  7058. .rpc_resp = &res,
  7059. .rpc_cred = cred,
  7060. };
  7061. int status;
  7062. nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
  7063. args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
  7064. status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  7065. trace_nfs4_create_session(clp, status);
  7066. switch (status) {
  7067. case -NFS4ERR_STALE_CLIENTID:
  7068. case -NFS4ERR_DELAY:
  7069. case -ETIMEDOUT:
  7070. case -EACCES:
  7071. case -EAGAIN:
  7072. goto out;
  7073. };
  7074. clp->cl_seqid++;
  7075. if (!status) {
  7076. /* Verify the session's negotiated channel_attrs values */
  7077. status = nfs4_verify_channel_attrs(&args, &res);
  7078. /* Increment the clientid slot sequence id */
  7079. if (status)
  7080. goto out;
  7081. nfs4_update_session(session, &res);
  7082. }
  7083. out:
  7084. return status;
  7085. }
  7086. /*
  7087. * Issues a CREATE_SESSION operation to the server.
  7088. * It is the responsibility of the caller to verify the session is
  7089. * expired before calling this routine.
  7090. */
  7091. int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
  7092. {
  7093. int status;
  7094. unsigned *ptr;
  7095. struct nfs4_session *session = clp->cl_session;
  7096. dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
  7097. status = _nfs4_proc_create_session(clp, cred);
  7098. if (status)
  7099. goto out;
  7100. /* Init or reset the session slot tables */
  7101. status = nfs4_setup_session_slot_tables(session);
  7102. dprintk("slot table setup returned %d\n", status);
  7103. if (status)
  7104. goto out;
  7105. ptr = (unsigned *)&session->sess_id.data[0];
  7106. dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
  7107. clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
  7108. out:
  7109. dprintk("<-- %s\n", __func__);
  7110. return status;
  7111. }
  7112. /*
  7113. * Issue the over-the-wire RPC DESTROY_SESSION.
  7114. * The caller must serialize access to this routine.
  7115. */
  7116. int nfs4_proc_destroy_session(struct nfs4_session *session,
  7117. struct rpc_cred *cred)
  7118. {
  7119. struct rpc_message msg = {
  7120. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
  7121. .rpc_argp = session,
  7122. .rpc_cred = cred,
  7123. };
  7124. int status = 0;
  7125. dprintk("--> nfs4_proc_destroy_session\n");
  7126. /* session is still being setup */
  7127. if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
  7128. return 0;
  7129. status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  7130. trace_nfs4_destroy_session(session->clp, status);
  7131. if (status)
  7132. dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
  7133. "Session has been destroyed regardless...\n", status);
  7134. dprintk("<-- nfs4_proc_destroy_session\n");
  7135. return status;
  7136. }
  7137. /*
  7138. * Renew the cl_session lease.
  7139. */
  7140. struct nfs4_sequence_data {
  7141. struct nfs_client *clp;
  7142. struct nfs4_sequence_args args;
  7143. struct nfs4_sequence_res res;
  7144. };
  7145. static void nfs41_sequence_release(void *data)
  7146. {
  7147. struct nfs4_sequence_data *calldata = data;
  7148. struct nfs_client *clp = calldata->clp;
  7149. if (atomic_read(&clp->cl_count) > 1)
  7150. nfs4_schedule_state_renewal(clp);
  7151. nfs_put_client(clp);
  7152. kfree(calldata);
  7153. }
  7154. static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
  7155. {
  7156. switch(task->tk_status) {
  7157. case -NFS4ERR_DELAY:
  7158. rpc_delay(task, NFS4_POLL_RETRY_MAX);
  7159. return -EAGAIN;
  7160. default:
  7161. nfs4_schedule_lease_recovery(clp);
  7162. }
  7163. return 0;
  7164. }
  7165. static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
  7166. {
  7167. struct nfs4_sequence_data *calldata = data;
  7168. struct nfs_client *clp = calldata->clp;
  7169. if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
  7170. return;
  7171. trace_nfs4_sequence(clp, task->tk_status);
  7172. if (task->tk_status < 0) {
  7173. dprintk("%s ERROR %d\n", __func__, task->tk_status);
  7174. if (atomic_read(&clp->cl_count) == 1)
  7175. goto out;
  7176. if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
  7177. rpc_restart_call_prepare(task);
  7178. return;
  7179. }
  7180. }
  7181. dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
  7182. out:
  7183. dprintk("<-- %s\n", __func__);
  7184. }
  7185. static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
  7186. {
  7187. struct nfs4_sequence_data *calldata = data;
  7188. struct nfs_client *clp = calldata->clp;
  7189. struct nfs4_sequence_args *args;
  7190. struct nfs4_sequence_res *res;
  7191. args = task->tk_msg.rpc_argp;
  7192. res = task->tk_msg.rpc_resp;
  7193. nfs41_setup_sequence(clp->cl_session, args, res, task);
  7194. }
  7195. static const struct rpc_call_ops nfs41_sequence_ops = {
  7196. .rpc_call_done = nfs41_sequence_call_done,
  7197. .rpc_call_prepare = nfs41_sequence_prepare,
  7198. .rpc_release = nfs41_sequence_release,
  7199. };
  7200. static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
  7201. struct rpc_cred *cred,
  7202. bool is_privileged)
  7203. {
  7204. struct nfs4_sequence_data *calldata;
  7205. struct rpc_message msg = {
  7206. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
  7207. .rpc_cred = cred,
  7208. };
  7209. struct rpc_task_setup task_setup_data = {
  7210. .rpc_client = clp->cl_rpcclient,
  7211. .rpc_message = &msg,
  7212. .callback_ops = &nfs41_sequence_ops,
  7213. .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
  7214. };
  7215. if (!atomic_inc_not_zero(&clp->cl_count))
  7216. return ERR_PTR(-EIO);
  7217. calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
  7218. if (calldata == NULL) {
  7219. nfs_put_client(clp);
  7220. return ERR_PTR(-ENOMEM);
  7221. }
  7222. nfs4_init_sequence(&calldata->args, &calldata->res, 0);
  7223. if (is_privileged)
  7224. nfs4_set_sequence_privileged(&calldata->args);
  7225. msg.rpc_argp = &calldata->args;
  7226. msg.rpc_resp = &calldata->res;
  7227. calldata->clp = clp;
  7228. task_setup_data.callback_data = calldata;
  7229. return rpc_run_task(&task_setup_data);
  7230. }
  7231. static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
  7232. {
  7233. struct rpc_task *task;
  7234. int ret = 0;
  7235. if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
  7236. return -EAGAIN;
  7237. task = _nfs41_proc_sequence(clp, cred, false);
  7238. if (IS_ERR(task))
  7239. ret = PTR_ERR(task);
  7240. else
  7241. rpc_put_task_async(task);
  7242. dprintk("<-- %s status=%d\n", __func__, ret);
  7243. return ret;
  7244. }
  7245. static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
  7246. {
  7247. struct rpc_task *task;
  7248. int ret;
  7249. task = _nfs41_proc_sequence(clp, cred, true);
  7250. if (IS_ERR(task)) {
  7251. ret = PTR_ERR(task);
  7252. goto out;
  7253. }
  7254. ret = rpc_wait_for_completion_task(task);
  7255. if (!ret)
  7256. ret = task->tk_status;
  7257. rpc_put_task(task);
  7258. out:
  7259. dprintk("<-- %s status=%d\n", __func__, ret);
  7260. return ret;
  7261. }
  7262. struct nfs4_reclaim_complete_data {
  7263. struct nfs_client *clp;
  7264. struct nfs41_reclaim_complete_args arg;
  7265. struct nfs41_reclaim_complete_res res;
  7266. };
  7267. static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
  7268. {
  7269. struct nfs4_reclaim_complete_data *calldata = data;
  7270. nfs41_setup_sequence(calldata->clp->cl_session,
  7271. &calldata->arg.seq_args,
  7272. &calldata->res.seq_res,
  7273. task);
  7274. }
  7275. static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
  7276. {
  7277. switch(task->tk_status) {
  7278. case 0:
  7279. case -NFS4ERR_COMPLETE_ALREADY:
  7280. case -NFS4ERR_WRONG_CRED: /* What to do here? */
  7281. break;
  7282. case -NFS4ERR_DELAY:
  7283. rpc_delay(task, NFS4_POLL_RETRY_MAX);
  7284. /* fall through */
  7285. case -NFS4ERR_RETRY_UNCACHED_REP:
  7286. return -EAGAIN;
  7287. case -NFS4ERR_BADSESSION:
  7288. case -NFS4ERR_DEADSESSION:
  7289. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  7290. nfs4_schedule_session_recovery(clp->cl_session,
  7291. task->tk_status);
  7292. break;
  7293. default:
  7294. nfs4_schedule_lease_recovery(clp);
  7295. }
  7296. return 0;
  7297. }
  7298. static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
  7299. {
  7300. struct nfs4_reclaim_complete_data *calldata = data;
  7301. struct nfs_client *clp = calldata->clp;
  7302. struct nfs4_sequence_res *res = &calldata->res.seq_res;
  7303. dprintk("--> %s\n", __func__);
  7304. if (!nfs41_sequence_done(task, res))
  7305. return;
  7306. trace_nfs4_reclaim_complete(clp, task->tk_status);
  7307. if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
  7308. rpc_restart_call_prepare(task);
  7309. return;
  7310. }
  7311. dprintk("<-- %s\n", __func__);
  7312. }
  7313. static void nfs4_free_reclaim_complete_data(void *data)
  7314. {
  7315. struct nfs4_reclaim_complete_data *calldata = data;
  7316. kfree(calldata);
  7317. }
  7318. static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
  7319. .rpc_call_prepare = nfs4_reclaim_complete_prepare,
  7320. .rpc_call_done = nfs4_reclaim_complete_done,
  7321. .rpc_release = nfs4_free_reclaim_complete_data,
  7322. };
  7323. /*
  7324. * Issue a global reclaim complete.
  7325. */
  7326. static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
  7327. struct rpc_cred *cred)
  7328. {
  7329. struct nfs4_reclaim_complete_data *calldata;
  7330. struct rpc_task *task;
  7331. struct rpc_message msg = {
  7332. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
  7333. .rpc_cred = cred,
  7334. };
  7335. struct rpc_task_setup task_setup_data = {
  7336. .rpc_client = clp->cl_rpcclient,
  7337. .rpc_message = &msg,
  7338. .callback_ops = &nfs4_reclaim_complete_call_ops,
  7339. .flags = RPC_TASK_ASYNC,
  7340. };
  7341. int status = -ENOMEM;
  7342. dprintk("--> %s\n", __func__);
  7343. calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
  7344. if (calldata == NULL)
  7345. goto out;
  7346. calldata->clp = clp;
  7347. calldata->arg.one_fs = 0;
  7348. nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
  7349. nfs4_set_sequence_privileged(&calldata->arg.seq_args);
  7350. msg.rpc_argp = &calldata->arg;
  7351. msg.rpc_resp = &calldata->res;
  7352. task_setup_data.callback_data = calldata;
  7353. task = rpc_run_task(&task_setup_data);
  7354. if (IS_ERR(task)) {
  7355. status = PTR_ERR(task);
  7356. goto out;
  7357. }
  7358. status = nfs4_wait_for_completion_rpc_task(task);
  7359. if (status == 0)
  7360. status = task->tk_status;
  7361. rpc_put_task(task);
  7362. out:
  7363. dprintk("<-- %s status=%d\n", __func__, status);
  7364. return status;
  7365. }
  7366. static void
  7367. nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
  7368. {
  7369. struct nfs4_layoutget *lgp = calldata;
  7370. struct nfs_server *server = NFS_SERVER(lgp->args.inode);
  7371. struct nfs4_session *session = nfs4_get_session(server);
  7372. dprintk("--> %s\n", __func__);
  7373. nfs41_setup_sequence(session, &lgp->args.seq_args,
  7374. &lgp->res.seq_res, task);
  7375. dprintk("<-- %s\n", __func__);
  7376. }
  7377. static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
  7378. {
  7379. struct nfs4_layoutget *lgp = calldata;
  7380. dprintk("--> %s\n", __func__);
  7381. nfs41_sequence_process(task, &lgp->res.seq_res);
  7382. dprintk("<-- %s\n", __func__);
  7383. }
  7384. static int
  7385. nfs4_layoutget_handle_exception(struct rpc_task *task,
  7386. struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
  7387. {
  7388. struct inode *inode = lgp->args.inode;
  7389. struct nfs_server *server = NFS_SERVER(inode);
  7390. struct pnfs_layout_hdr *lo;
  7391. int nfs4err = task->tk_status;
  7392. int err, status = 0;
  7393. LIST_HEAD(head);
  7394. dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
  7395. switch (nfs4err) {
  7396. case 0:
  7397. goto out;
  7398. /*
  7399. * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
  7400. * on the file. set tk_status to -ENODATA to tell upper layer to
  7401. * retry go inband.
  7402. */
  7403. case -NFS4ERR_LAYOUTUNAVAILABLE:
  7404. status = -ENODATA;
  7405. goto out;
  7406. /*
  7407. * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
  7408. * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
  7409. */
  7410. case -NFS4ERR_BADLAYOUT:
  7411. status = -EOVERFLOW;
  7412. goto out;
  7413. /*
  7414. * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
  7415. * (or clients) writing to the same RAID stripe except when
  7416. * the minlength argument is 0 (see RFC5661 section 18.43.3).
  7417. *
  7418. * Treat it like we would RECALLCONFLICT -- we retry for a little
  7419. * while, and then eventually give up.
  7420. */
  7421. case -NFS4ERR_LAYOUTTRYLATER:
  7422. if (lgp->args.minlength == 0) {
  7423. status = -EOVERFLOW;
  7424. goto out;
  7425. }
  7426. status = -EBUSY;
  7427. break;
  7428. case -NFS4ERR_RECALLCONFLICT:
  7429. status = -ERECALLCONFLICT;
  7430. break;
  7431. case -NFS4ERR_DELEG_REVOKED:
  7432. case -NFS4ERR_ADMIN_REVOKED:
  7433. case -NFS4ERR_EXPIRED:
  7434. case -NFS4ERR_BAD_STATEID:
  7435. exception->timeout = 0;
  7436. spin_lock(&inode->i_lock);
  7437. lo = NFS_I(inode)->layout;
  7438. /* If the open stateid was bad, then recover it. */
  7439. if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
  7440. nfs4_stateid_match_other(&lgp->args.stateid,
  7441. &lgp->args.ctx->state->stateid)) {
  7442. spin_unlock(&inode->i_lock);
  7443. exception->state = lgp->args.ctx->state;
  7444. exception->stateid = &lgp->args.stateid;
  7445. break;
  7446. }
  7447. /*
  7448. * Mark the bad layout state as invalid, then retry
  7449. */
  7450. pnfs_mark_layout_stateid_invalid(lo, &head);
  7451. spin_unlock(&inode->i_lock);
  7452. pnfs_free_lseg_list(&head);
  7453. status = -EAGAIN;
  7454. goto out;
  7455. }
  7456. nfs4_sequence_free_slot(&lgp->res.seq_res);
  7457. err = nfs4_handle_exception(server, nfs4err, exception);
  7458. if (!status) {
  7459. if (exception->retry)
  7460. status = -EAGAIN;
  7461. else
  7462. status = err;
  7463. }
  7464. out:
  7465. dprintk("<-- %s\n", __func__);
  7466. return status;
  7467. }
  7468. static size_t max_response_pages(struct nfs_server *server)
  7469. {
  7470. u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
  7471. return nfs_page_array_len(0, max_resp_sz);
  7472. }
  7473. static void nfs4_free_pages(struct page **pages, size_t size)
  7474. {
  7475. int i;
  7476. if (!pages)
  7477. return;
  7478. for (i = 0; i < size; i++) {
  7479. if (!pages[i])
  7480. break;
  7481. __free_page(pages[i]);
  7482. }
  7483. kfree(pages);
  7484. }
  7485. static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
  7486. {
  7487. struct page **pages;
  7488. int i;
  7489. pages = kcalloc(size, sizeof(struct page *), gfp_flags);
  7490. if (!pages) {
  7491. dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
  7492. return NULL;
  7493. }
  7494. for (i = 0; i < size; i++) {
  7495. pages[i] = alloc_page(gfp_flags);
  7496. if (!pages[i]) {
  7497. dprintk("%s: failed to allocate page\n", __func__);
  7498. nfs4_free_pages(pages, size);
  7499. return NULL;
  7500. }
  7501. }
  7502. return pages;
  7503. }
  7504. static void nfs4_layoutget_release(void *calldata)
  7505. {
  7506. struct nfs4_layoutget *lgp = calldata;
  7507. struct inode *inode = lgp->args.inode;
  7508. struct nfs_server *server = NFS_SERVER(inode);
  7509. size_t max_pages = max_response_pages(server);
  7510. dprintk("--> %s\n", __func__);
  7511. nfs4_sequence_free_slot(&lgp->res.seq_res);
  7512. nfs4_free_pages(lgp->args.layout.pages, max_pages);
  7513. pnfs_put_layout_hdr(NFS_I(inode)->layout);
  7514. put_nfs_open_context(lgp->args.ctx);
  7515. kfree(calldata);
  7516. dprintk("<-- %s\n", __func__);
  7517. }
  7518. static const struct rpc_call_ops nfs4_layoutget_call_ops = {
  7519. .rpc_call_prepare = nfs4_layoutget_prepare,
  7520. .rpc_call_done = nfs4_layoutget_done,
  7521. .rpc_release = nfs4_layoutget_release,
  7522. };
  7523. struct pnfs_layout_segment *
  7524. nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
  7525. {
  7526. struct inode *inode = lgp->args.inode;
  7527. struct nfs_server *server = NFS_SERVER(inode);
  7528. size_t max_pages = max_response_pages(server);
  7529. struct rpc_task *task;
  7530. struct rpc_message msg = {
  7531. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
  7532. .rpc_argp = &lgp->args,
  7533. .rpc_resp = &lgp->res,
  7534. .rpc_cred = lgp->cred,
  7535. };
  7536. struct rpc_task_setup task_setup_data = {
  7537. .rpc_client = server->client,
  7538. .rpc_message = &msg,
  7539. .callback_ops = &nfs4_layoutget_call_ops,
  7540. .callback_data = lgp,
  7541. .flags = RPC_TASK_ASYNC,
  7542. };
  7543. struct pnfs_layout_segment *lseg = NULL;
  7544. struct nfs4_exception exception = {
  7545. .inode = inode,
  7546. .timeout = *timeout,
  7547. };
  7548. int status = 0;
  7549. dprintk("--> %s\n", __func__);
  7550. /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
  7551. pnfs_get_layout_hdr(NFS_I(inode)->layout);
  7552. lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
  7553. if (!lgp->args.layout.pages) {
  7554. nfs4_layoutget_release(lgp);
  7555. return ERR_PTR(-ENOMEM);
  7556. }
  7557. lgp->args.layout.pglen = max_pages * PAGE_SIZE;
  7558. lgp->res.layoutp = &lgp->args.layout;
  7559. lgp->res.seq_res.sr_slot = NULL;
  7560. nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
  7561. task = rpc_run_task(&task_setup_data);
  7562. if (IS_ERR(task))
  7563. return ERR_CAST(task);
  7564. status = nfs4_wait_for_completion_rpc_task(task);
  7565. if (status == 0) {
  7566. status = nfs4_layoutget_handle_exception(task, lgp, &exception);
  7567. *timeout = exception.timeout;
  7568. }
  7569. trace_nfs4_layoutget(lgp->args.ctx,
  7570. &lgp->args.range,
  7571. &lgp->res.range,
  7572. &lgp->res.stateid,
  7573. status);
  7574. /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
  7575. if (status == 0 && lgp->res.layoutp->len)
  7576. lseg = pnfs_layout_process(lgp);
  7577. rpc_put_task(task);
  7578. dprintk("<-- %s status=%d\n", __func__, status);
  7579. if (status)
  7580. return ERR_PTR(status);
  7581. return lseg;
  7582. }
  7583. static void
  7584. nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
  7585. {
  7586. struct nfs4_layoutreturn *lrp = calldata;
  7587. dprintk("--> %s\n", __func__);
  7588. nfs41_setup_sequence(lrp->clp->cl_session,
  7589. &lrp->args.seq_args,
  7590. &lrp->res.seq_res,
  7591. task);
  7592. }
  7593. static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
  7594. {
  7595. struct nfs4_layoutreturn *lrp = calldata;
  7596. struct nfs_server *server;
  7597. dprintk("--> %s\n", __func__);
  7598. if (!nfs41_sequence_process(task, &lrp->res.seq_res))
  7599. return;
  7600. server = NFS_SERVER(lrp->args.inode);
  7601. switch (task->tk_status) {
  7602. default:
  7603. task->tk_status = 0;
  7604. case 0:
  7605. break;
  7606. case -NFS4ERR_DELAY:
  7607. if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
  7608. break;
  7609. nfs4_sequence_free_slot(&lrp->res.seq_res);
  7610. rpc_restart_call_prepare(task);
  7611. return;
  7612. }
  7613. dprintk("<-- %s\n", __func__);
  7614. }
  7615. static void nfs4_layoutreturn_release(void *calldata)
  7616. {
  7617. struct nfs4_layoutreturn *lrp = calldata;
  7618. struct pnfs_layout_hdr *lo = lrp->args.layout;
  7619. LIST_HEAD(freeme);
  7620. dprintk("--> %s\n", __func__);
  7621. spin_lock(&lo->plh_inode->i_lock);
  7622. if (lrp->res.lrs_present) {
  7623. pnfs_mark_matching_lsegs_invalid(lo, &freeme,
  7624. &lrp->args.range,
  7625. be32_to_cpu(lrp->args.stateid.seqid));
  7626. pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
  7627. } else
  7628. pnfs_mark_layout_stateid_invalid(lo, &freeme);
  7629. pnfs_clear_layoutreturn_waitbit(lo);
  7630. spin_unlock(&lo->plh_inode->i_lock);
  7631. nfs4_sequence_free_slot(&lrp->res.seq_res);
  7632. pnfs_free_lseg_list(&freeme);
  7633. pnfs_put_layout_hdr(lrp->args.layout);
  7634. nfs_iput_and_deactive(lrp->inode);
  7635. kfree(calldata);
  7636. dprintk("<-- %s\n", __func__);
  7637. }
  7638. static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
  7639. .rpc_call_prepare = nfs4_layoutreturn_prepare,
  7640. .rpc_call_done = nfs4_layoutreturn_done,
  7641. .rpc_release = nfs4_layoutreturn_release,
  7642. };
  7643. int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
  7644. {
  7645. struct rpc_task *task;
  7646. struct rpc_message msg = {
  7647. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
  7648. .rpc_argp = &lrp->args,
  7649. .rpc_resp = &lrp->res,
  7650. .rpc_cred = lrp->cred,
  7651. };
  7652. struct rpc_task_setup task_setup_data = {
  7653. .rpc_client = NFS_SERVER(lrp->args.inode)->client,
  7654. .rpc_message = &msg,
  7655. .callback_ops = &nfs4_layoutreturn_call_ops,
  7656. .callback_data = lrp,
  7657. };
  7658. int status = 0;
  7659. nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
  7660. NFS_SP4_MACH_CRED_PNFS_CLEANUP,
  7661. &task_setup_data.rpc_client, &msg);
  7662. dprintk("--> %s\n", __func__);
  7663. if (!sync) {
  7664. lrp->inode = nfs_igrab_and_active(lrp->args.inode);
  7665. if (!lrp->inode) {
  7666. nfs4_layoutreturn_release(lrp);
  7667. return -EAGAIN;
  7668. }
  7669. task_setup_data.flags |= RPC_TASK_ASYNC;
  7670. }
  7671. nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
  7672. task = rpc_run_task(&task_setup_data);
  7673. if (IS_ERR(task))
  7674. return PTR_ERR(task);
  7675. if (sync)
  7676. status = task->tk_status;
  7677. trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
  7678. dprintk("<-- %s status=%d\n", __func__, status);
  7679. rpc_put_task(task);
  7680. return status;
  7681. }
  7682. static int
  7683. _nfs4_proc_getdeviceinfo(struct nfs_server *server,
  7684. struct pnfs_device *pdev,
  7685. struct rpc_cred *cred)
  7686. {
  7687. struct nfs4_getdeviceinfo_args args = {
  7688. .pdev = pdev,
  7689. .notify_types = NOTIFY_DEVICEID4_CHANGE |
  7690. NOTIFY_DEVICEID4_DELETE,
  7691. };
  7692. struct nfs4_getdeviceinfo_res res = {
  7693. .pdev = pdev,
  7694. };
  7695. struct rpc_message msg = {
  7696. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
  7697. .rpc_argp = &args,
  7698. .rpc_resp = &res,
  7699. .rpc_cred = cred,
  7700. };
  7701. int status;
  7702. dprintk("--> %s\n", __func__);
  7703. status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  7704. if (res.notification & ~args.notify_types)
  7705. dprintk("%s: unsupported notification\n", __func__);
  7706. if (res.notification != args.notify_types)
  7707. pdev->nocache = 1;
  7708. dprintk("<-- %s status=%d\n", __func__, status);
  7709. return status;
  7710. }
  7711. int nfs4_proc_getdeviceinfo(struct nfs_server *server,
  7712. struct pnfs_device *pdev,
  7713. struct rpc_cred *cred)
  7714. {
  7715. struct nfs4_exception exception = { };
  7716. int err;
  7717. do {
  7718. err = nfs4_handle_exception(server,
  7719. _nfs4_proc_getdeviceinfo(server, pdev, cred),
  7720. &exception);
  7721. } while (exception.retry);
  7722. return err;
  7723. }
  7724. EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
  7725. static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
  7726. {
  7727. struct nfs4_layoutcommit_data *data = calldata;
  7728. struct nfs_server *server = NFS_SERVER(data->args.inode);
  7729. struct nfs4_session *session = nfs4_get_session(server);
  7730. nfs41_setup_sequence(session,
  7731. &data->args.seq_args,
  7732. &data->res.seq_res,
  7733. task);
  7734. }
  7735. static void
  7736. nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
  7737. {
  7738. struct nfs4_layoutcommit_data *data = calldata;
  7739. struct nfs_server *server = NFS_SERVER(data->args.inode);
  7740. if (!nfs41_sequence_done(task, &data->res.seq_res))
  7741. return;
  7742. switch (task->tk_status) { /* Just ignore these failures */
  7743. case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
  7744. case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
  7745. case -NFS4ERR_BADLAYOUT: /* no layout */
  7746. case -NFS4ERR_GRACE: /* loca_recalim always false */
  7747. task->tk_status = 0;
  7748. case 0:
  7749. break;
  7750. default:
  7751. if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
  7752. rpc_restart_call_prepare(task);
  7753. return;
  7754. }
  7755. }
  7756. }
  7757. static void nfs4_layoutcommit_release(void *calldata)
  7758. {
  7759. struct nfs4_layoutcommit_data *data = calldata;
  7760. pnfs_cleanup_layoutcommit(data);
  7761. nfs_post_op_update_inode_force_wcc(data->args.inode,
  7762. data->res.fattr);
  7763. put_rpccred(data->cred);
  7764. nfs_iput_and_deactive(data->inode);
  7765. kfree(data);
  7766. }
  7767. static const struct rpc_call_ops nfs4_layoutcommit_ops = {
  7768. .rpc_call_prepare = nfs4_layoutcommit_prepare,
  7769. .rpc_call_done = nfs4_layoutcommit_done,
  7770. .rpc_release = nfs4_layoutcommit_release,
  7771. };
  7772. int
  7773. nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
  7774. {
  7775. struct rpc_message msg = {
  7776. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
  7777. .rpc_argp = &data->args,
  7778. .rpc_resp = &data->res,
  7779. .rpc_cred = data->cred,
  7780. };
  7781. struct rpc_task_setup task_setup_data = {
  7782. .task = &data->task,
  7783. .rpc_client = NFS_CLIENT(data->args.inode),
  7784. .rpc_message = &msg,
  7785. .callback_ops = &nfs4_layoutcommit_ops,
  7786. .callback_data = data,
  7787. };
  7788. struct rpc_task *task;
  7789. int status = 0;
  7790. dprintk("NFS: initiating layoutcommit call. sync %d "
  7791. "lbw: %llu inode %lu\n", sync,
  7792. data->args.lastbytewritten,
  7793. data->args.inode->i_ino);
  7794. if (!sync) {
  7795. data->inode = nfs_igrab_and_active(data->args.inode);
  7796. if (data->inode == NULL) {
  7797. nfs4_layoutcommit_release(data);
  7798. return -EAGAIN;
  7799. }
  7800. task_setup_data.flags = RPC_TASK_ASYNC;
  7801. }
  7802. nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
  7803. task = rpc_run_task(&task_setup_data);
  7804. if (IS_ERR(task))
  7805. return PTR_ERR(task);
  7806. if (sync)
  7807. status = task->tk_status;
  7808. trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
  7809. dprintk("%s: status %d\n", __func__, status);
  7810. rpc_put_task(task);
  7811. return status;
  7812. }
  7813. /**
  7814. * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
  7815. * possible) as per RFC3530bis and RFC5661 Security Considerations sections
  7816. */
  7817. static int
  7818. _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
  7819. struct nfs_fsinfo *info,
  7820. struct nfs4_secinfo_flavors *flavors, bool use_integrity)
  7821. {
  7822. struct nfs41_secinfo_no_name_args args = {
  7823. .style = SECINFO_STYLE_CURRENT_FH,
  7824. };
  7825. struct nfs4_secinfo_res res = {
  7826. .flavors = flavors,
  7827. };
  7828. struct rpc_message msg = {
  7829. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
  7830. .rpc_argp = &args,
  7831. .rpc_resp = &res,
  7832. };
  7833. struct rpc_clnt *clnt = server->client;
  7834. struct rpc_cred *cred = NULL;
  7835. int status;
  7836. if (use_integrity) {
  7837. clnt = server->nfs_client->cl_rpcclient;
  7838. cred = nfs4_get_clid_cred(server->nfs_client);
  7839. msg.rpc_cred = cred;
  7840. }
  7841. dprintk("--> %s\n", __func__);
  7842. status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
  7843. &res.seq_res, 0);
  7844. dprintk("<-- %s status=%d\n", __func__, status);
  7845. if (cred)
  7846. put_rpccred(cred);
  7847. return status;
  7848. }
  7849. static int
  7850. nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
  7851. struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
  7852. {
  7853. struct nfs4_exception exception = { };
  7854. int err;
  7855. do {
  7856. /* first try using integrity protection */
  7857. err = -NFS4ERR_WRONGSEC;
  7858. /* try to use integrity protection with machine cred */
  7859. if (_nfs4_is_integrity_protected(server->nfs_client))
  7860. err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
  7861. flavors, true);
  7862. /*
  7863. * if unable to use integrity protection, or SECINFO with
  7864. * integrity protection returns NFS4ERR_WRONGSEC (which is
  7865. * disallowed by spec, but exists in deployed servers) use
  7866. * the current filesystem's rpc_client and the user cred.
  7867. */
  7868. if (err == -NFS4ERR_WRONGSEC)
  7869. err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
  7870. flavors, false);
  7871. switch (err) {
  7872. case 0:
  7873. case -NFS4ERR_WRONGSEC:
  7874. case -ENOTSUPP:
  7875. goto out;
  7876. default:
  7877. err = nfs4_handle_exception(server, err, &exception);
  7878. }
  7879. } while (exception.retry);
  7880. out:
  7881. return err;
  7882. }
  7883. static int
  7884. nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
  7885. struct nfs_fsinfo *info)
  7886. {
  7887. int err;
  7888. struct page *page;
  7889. rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
  7890. struct nfs4_secinfo_flavors *flavors;
  7891. struct nfs4_secinfo4 *secinfo;
  7892. int i;
  7893. page = alloc_page(GFP_KERNEL);
  7894. if (!page) {
  7895. err = -ENOMEM;
  7896. goto out;
  7897. }
  7898. flavors = page_address(page);
  7899. err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
  7900. /*
  7901. * Fall back on "guess and check" method if
  7902. * the server doesn't support SECINFO_NO_NAME
  7903. */
  7904. if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
  7905. err = nfs4_find_root_sec(server, fhandle, info);
  7906. goto out_freepage;
  7907. }
  7908. if (err)
  7909. goto out_freepage;
  7910. for (i = 0; i < flavors->num_flavors; i++) {
  7911. secinfo = &flavors->flavors[i];
  7912. switch (secinfo->flavor) {
  7913. case RPC_AUTH_NULL:
  7914. case RPC_AUTH_UNIX:
  7915. case RPC_AUTH_GSS:
  7916. flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
  7917. &secinfo->flavor_info);
  7918. break;
  7919. default:
  7920. flavor = RPC_AUTH_MAXFLAVOR;
  7921. break;
  7922. }
  7923. if (!nfs_auth_info_match(&server->auth_info, flavor))
  7924. flavor = RPC_AUTH_MAXFLAVOR;
  7925. if (flavor != RPC_AUTH_MAXFLAVOR) {
  7926. err = nfs4_lookup_root_sec(server, fhandle,
  7927. info, flavor);
  7928. if (!err)
  7929. break;
  7930. }
  7931. }
  7932. if (flavor == RPC_AUTH_MAXFLAVOR)
  7933. err = -EPERM;
  7934. out_freepage:
  7935. put_page(page);
  7936. if (err == -EACCES)
  7937. return -EPERM;
  7938. out:
  7939. return err;
  7940. }
  7941. static int _nfs41_test_stateid(struct nfs_server *server,
  7942. nfs4_stateid *stateid,
  7943. struct rpc_cred *cred)
  7944. {
  7945. int status;
  7946. struct nfs41_test_stateid_args args = {
  7947. .stateid = stateid,
  7948. };
  7949. struct nfs41_test_stateid_res res;
  7950. struct rpc_message msg = {
  7951. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
  7952. .rpc_argp = &args,
  7953. .rpc_resp = &res,
  7954. .rpc_cred = cred,
  7955. };
  7956. struct rpc_clnt *rpc_client = server->client;
  7957. nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
  7958. &rpc_client, &msg);
  7959. dprintk("NFS call test_stateid %p\n", stateid);
  7960. nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
  7961. nfs4_set_sequence_privileged(&args.seq_args);
  7962. status = nfs4_call_sync_sequence(rpc_client, server, &msg,
  7963. &args.seq_args, &res.seq_res);
  7964. if (status != NFS_OK) {
  7965. dprintk("NFS reply test_stateid: failed, %d\n", status);
  7966. return status;
  7967. }
  7968. dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
  7969. return -res.status;
  7970. }
  7971. static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
  7972. int err, struct nfs4_exception *exception)
  7973. {
  7974. exception->retry = 0;
  7975. switch(err) {
  7976. case -NFS4ERR_DELAY:
  7977. case -NFS4ERR_RETRY_UNCACHED_REP:
  7978. nfs4_handle_exception(server, err, exception);
  7979. break;
  7980. case -NFS4ERR_BADSESSION:
  7981. case -NFS4ERR_BADSLOT:
  7982. case -NFS4ERR_BAD_HIGH_SLOT:
  7983. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  7984. case -NFS4ERR_DEADSESSION:
  7985. nfs4_do_handle_exception(server, err, exception);
  7986. }
  7987. }
  7988. /**
  7989. * nfs41_test_stateid - perform a TEST_STATEID operation
  7990. *
  7991. * @server: server / transport on which to perform the operation
  7992. * @stateid: state ID to test
  7993. * @cred: credential
  7994. *
  7995. * Returns NFS_OK if the server recognizes that "stateid" is valid.
  7996. * Otherwise a negative NFS4ERR value is returned if the operation
  7997. * failed or the state ID is not currently valid.
  7998. */
  7999. static int nfs41_test_stateid(struct nfs_server *server,
  8000. nfs4_stateid *stateid,
  8001. struct rpc_cred *cred)
  8002. {
  8003. struct nfs4_exception exception = { };
  8004. int err;
  8005. do {
  8006. err = _nfs41_test_stateid(server, stateid, cred);
  8007. nfs4_handle_delay_or_session_error(server, err, &exception);
  8008. } while (exception.retry);
  8009. return err;
  8010. }
  8011. struct nfs_free_stateid_data {
  8012. struct nfs_server *server;
  8013. struct nfs41_free_stateid_args args;
  8014. struct nfs41_free_stateid_res res;
  8015. };
  8016. static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
  8017. {
  8018. struct nfs_free_stateid_data *data = calldata;
  8019. nfs41_setup_sequence(nfs4_get_session(data->server),
  8020. &data->args.seq_args,
  8021. &data->res.seq_res,
  8022. task);
  8023. }
  8024. static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
  8025. {
  8026. struct nfs_free_stateid_data *data = calldata;
  8027. nfs41_sequence_done(task, &data->res.seq_res);
  8028. switch (task->tk_status) {
  8029. case -NFS4ERR_DELAY:
  8030. if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
  8031. rpc_restart_call_prepare(task);
  8032. }
  8033. }
  8034. static void nfs41_free_stateid_release(void *calldata)
  8035. {
  8036. kfree(calldata);
  8037. }
  8038. static const struct rpc_call_ops nfs41_free_stateid_ops = {
  8039. .rpc_call_prepare = nfs41_free_stateid_prepare,
  8040. .rpc_call_done = nfs41_free_stateid_done,
  8041. .rpc_release = nfs41_free_stateid_release,
  8042. };
  8043. static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
  8044. const nfs4_stateid *stateid,
  8045. struct rpc_cred *cred,
  8046. bool privileged)
  8047. {
  8048. struct rpc_message msg = {
  8049. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
  8050. .rpc_cred = cred,
  8051. };
  8052. struct rpc_task_setup task_setup = {
  8053. .rpc_client = server->client,
  8054. .rpc_message = &msg,
  8055. .callback_ops = &nfs41_free_stateid_ops,
  8056. .flags = RPC_TASK_ASYNC,
  8057. };
  8058. struct nfs_free_stateid_data *data;
  8059. nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
  8060. &task_setup.rpc_client, &msg);
  8061. dprintk("NFS call free_stateid %p\n", stateid);
  8062. data = kmalloc(sizeof(*data), GFP_NOFS);
  8063. if (!data)
  8064. return ERR_PTR(-ENOMEM);
  8065. data->server = server;
  8066. nfs4_stateid_copy(&data->args.stateid, stateid);
  8067. task_setup.callback_data = data;
  8068. msg.rpc_argp = &data->args;
  8069. msg.rpc_resp = &data->res;
  8070. nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
  8071. if (privileged)
  8072. nfs4_set_sequence_privileged(&data->args.seq_args);
  8073. return rpc_run_task(&task_setup);
  8074. }
  8075. /**
  8076. * nfs41_free_stateid - perform a FREE_STATEID operation
  8077. *
  8078. * @server: server / transport on which to perform the operation
  8079. * @stateid: state ID to release
  8080. * @cred: credential
  8081. * @is_recovery: set to true if this call needs to be privileged
  8082. *
  8083. * Note: this function is always asynchronous.
  8084. */
  8085. static int nfs41_free_stateid(struct nfs_server *server,
  8086. const nfs4_stateid *stateid,
  8087. struct rpc_cred *cred,
  8088. bool is_recovery)
  8089. {
  8090. struct rpc_task *task;
  8091. task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
  8092. if (IS_ERR(task))
  8093. return PTR_ERR(task);
  8094. rpc_put_task(task);
  8095. return 0;
  8096. }
  8097. static void
  8098. nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
  8099. {
  8100. struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
  8101. nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
  8102. nfs4_free_lock_state(server, lsp);
  8103. }
  8104. static bool nfs41_match_stateid(const nfs4_stateid *s1,
  8105. const nfs4_stateid *s2)
  8106. {
  8107. if (s1->type != s2->type)
  8108. return false;
  8109. if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
  8110. return false;
  8111. if (s1->seqid == s2->seqid)
  8112. return true;
  8113. if (s1->seqid == 0 || s2->seqid == 0)
  8114. return true;
  8115. return false;
  8116. }
  8117. #endif /* CONFIG_NFS_V4_1 */
  8118. static bool nfs4_match_stateid(const nfs4_stateid *s1,
  8119. const nfs4_stateid *s2)
  8120. {
  8121. return nfs4_stateid_match(s1, s2);
  8122. }
  8123. static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
  8124. .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
  8125. .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
  8126. .recover_open = nfs4_open_reclaim,
  8127. .recover_lock = nfs4_lock_reclaim,
  8128. .establish_clid = nfs4_init_clientid,
  8129. .detect_trunking = nfs40_discover_server_trunking,
  8130. };
  8131. #if defined(CONFIG_NFS_V4_1)
  8132. static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
  8133. .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
  8134. .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
  8135. .recover_open = nfs4_open_reclaim,
  8136. .recover_lock = nfs4_lock_reclaim,
  8137. .establish_clid = nfs41_init_clientid,
  8138. .reclaim_complete = nfs41_proc_reclaim_complete,
  8139. .detect_trunking = nfs41_discover_server_trunking,
  8140. };
  8141. #endif /* CONFIG_NFS_V4_1 */
  8142. static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
  8143. .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
  8144. .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
  8145. .recover_open = nfs40_open_expired,
  8146. .recover_lock = nfs4_lock_expired,
  8147. .establish_clid = nfs4_init_clientid,
  8148. };
  8149. #if defined(CONFIG_NFS_V4_1)
  8150. static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
  8151. .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
  8152. .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
  8153. .recover_open = nfs41_open_expired,
  8154. .recover_lock = nfs41_lock_expired,
  8155. .establish_clid = nfs41_init_clientid,
  8156. };
  8157. #endif /* CONFIG_NFS_V4_1 */
  8158. static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
  8159. .sched_state_renewal = nfs4_proc_async_renew,
  8160. .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
  8161. .renew_lease = nfs4_proc_renew,
  8162. };
  8163. #if defined(CONFIG_NFS_V4_1)
  8164. static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
  8165. .sched_state_renewal = nfs41_proc_async_sequence,
  8166. .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
  8167. .renew_lease = nfs4_proc_sequence,
  8168. };
  8169. #endif
  8170. static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
  8171. .get_locations = _nfs40_proc_get_locations,
  8172. .fsid_present = _nfs40_proc_fsid_present,
  8173. };
  8174. #if defined(CONFIG_NFS_V4_1)
  8175. static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
  8176. .get_locations = _nfs41_proc_get_locations,
  8177. .fsid_present = _nfs41_proc_fsid_present,
  8178. };
  8179. #endif /* CONFIG_NFS_V4_1 */
  8180. static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
  8181. .minor_version = 0,
  8182. .init_caps = NFS_CAP_READDIRPLUS
  8183. | NFS_CAP_ATOMIC_OPEN
  8184. | NFS_CAP_POSIX_LOCK,
  8185. .init_client = nfs40_init_client,
  8186. .shutdown_client = nfs40_shutdown_client,
  8187. .match_stateid = nfs4_match_stateid,
  8188. .find_root_sec = nfs4_find_root_sec,
  8189. .free_lock_state = nfs4_release_lockowner,
  8190. .test_and_free_expired = nfs40_test_and_free_expired_stateid,
  8191. .alloc_seqid = nfs_alloc_seqid,
  8192. .call_sync_ops = &nfs40_call_sync_ops,
  8193. .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
  8194. .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
  8195. .state_renewal_ops = &nfs40_state_renewal_ops,
  8196. .mig_recovery_ops = &nfs40_mig_recovery_ops,
  8197. };
  8198. #if defined(CONFIG_NFS_V4_1)
  8199. static struct nfs_seqid *
  8200. nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
  8201. {
  8202. return NULL;
  8203. }
  8204. static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
  8205. .minor_version = 1,
  8206. .init_caps = NFS_CAP_READDIRPLUS
  8207. | NFS_CAP_ATOMIC_OPEN
  8208. | NFS_CAP_POSIX_LOCK
  8209. | NFS_CAP_STATEID_NFSV41
  8210. | NFS_CAP_ATOMIC_OPEN_V1,
  8211. .init_client = nfs41_init_client,
  8212. .shutdown_client = nfs41_shutdown_client,
  8213. .match_stateid = nfs41_match_stateid,
  8214. .find_root_sec = nfs41_find_root_sec,
  8215. .free_lock_state = nfs41_free_lock_state,
  8216. .test_and_free_expired = nfs41_test_and_free_expired_stateid,
  8217. .alloc_seqid = nfs_alloc_no_seqid,
  8218. .session_trunk = nfs4_test_session_trunk,
  8219. .call_sync_ops = &nfs41_call_sync_ops,
  8220. .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
  8221. .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
  8222. .state_renewal_ops = &nfs41_state_renewal_ops,
  8223. .mig_recovery_ops = &nfs41_mig_recovery_ops,
  8224. };
  8225. #endif
  8226. #if defined(CONFIG_NFS_V4_2)
  8227. static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
  8228. .minor_version = 2,
  8229. .init_caps = NFS_CAP_READDIRPLUS
  8230. | NFS_CAP_ATOMIC_OPEN
  8231. | NFS_CAP_POSIX_LOCK
  8232. | NFS_CAP_STATEID_NFSV41
  8233. | NFS_CAP_ATOMIC_OPEN_V1
  8234. | NFS_CAP_ALLOCATE
  8235. | NFS_CAP_COPY
  8236. | NFS_CAP_DEALLOCATE
  8237. | NFS_CAP_SEEK
  8238. | NFS_CAP_LAYOUTSTATS
  8239. | NFS_CAP_CLONE,
  8240. .init_client = nfs41_init_client,
  8241. .shutdown_client = nfs41_shutdown_client,
  8242. .match_stateid = nfs41_match_stateid,
  8243. .find_root_sec = nfs41_find_root_sec,
  8244. .free_lock_state = nfs41_free_lock_state,
  8245. .call_sync_ops = &nfs41_call_sync_ops,
  8246. .test_and_free_expired = nfs41_test_and_free_expired_stateid,
  8247. .alloc_seqid = nfs_alloc_no_seqid,
  8248. .session_trunk = nfs4_test_session_trunk,
  8249. .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
  8250. .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
  8251. .state_renewal_ops = &nfs41_state_renewal_ops,
  8252. .mig_recovery_ops = &nfs41_mig_recovery_ops,
  8253. };
  8254. #endif
  8255. const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
  8256. [0] = &nfs_v4_0_minor_ops,
  8257. #if defined(CONFIG_NFS_V4_1)
  8258. [1] = &nfs_v4_1_minor_ops,
  8259. #endif
  8260. #if defined(CONFIG_NFS_V4_2)
  8261. [2] = &nfs_v4_2_minor_ops,
  8262. #endif
  8263. };
  8264. static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
  8265. {
  8266. ssize_t error, error2;
  8267. error = generic_listxattr(dentry, list, size);
  8268. if (error < 0)
  8269. return error;
  8270. if (list) {
  8271. list += error;
  8272. size -= error;
  8273. }
  8274. error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
  8275. if (error2 < 0)
  8276. return error2;
  8277. return error + error2;
  8278. }
  8279. static const struct inode_operations nfs4_dir_inode_operations = {
  8280. .create = nfs_create,
  8281. .lookup = nfs_lookup,
  8282. .atomic_open = nfs_atomic_open,
  8283. .link = nfs_link,
  8284. .unlink = nfs_unlink,
  8285. .symlink = nfs_symlink,
  8286. .mkdir = nfs_mkdir,
  8287. .rmdir = nfs_rmdir,
  8288. .mknod = nfs_mknod,
  8289. .rename = nfs_rename,
  8290. .permission = nfs_permission,
  8291. .getattr = nfs_getattr,
  8292. .setattr = nfs_setattr,
  8293. .listxattr = nfs4_listxattr,
  8294. };
  8295. static const struct inode_operations nfs4_file_inode_operations = {
  8296. .permission = nfs_permission,
  8297. .getattr = nfs_getattr,
  8298. .setattr = nfs_setattr,
  8299. .listxattr = nfs4_listxattr,
  8300. };
  8301. const struct nfs_rpc_ops nfs_v4_clientops = {
  8302. .version = 4, /* protocol version */
  8303. .dentry_ops = &nfs4_dentry_operations,
  8304. .dir_inode_ops = &nfs4_dir_inode_operations,
  8305. .file_inode_ops = &nfs4_file_inode_operations,
  8306. .file_ops = &nfs4_file_operations,
  8307. .getroot = nfs4_proc_get_root,
  8308. .submount = nfs4_submount,
  8309. .try_mount = nfs4_try_mount,
  8310. .getattr = nfs4_proc_getattr,
  8311. .setattr = nfs4_proc_setattr,
  8312. .lookup = nfs4_proc_lookup,
  8313. .access = nfs4_proc_access,
  8314. .readlink = nfs4_proc_readlink,
  8315. .create = nfs4_proc_create,
  8316. .remove = nfs4_proc_remove,
  8317. .unlink_setup = nfs4_proc_unlink_setup,
  8318. .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
  8319. .unlink_done = nfs4_proc_unlink_done,
  8320. .rename_setup = nfs4_proc_rename_setup,
  8321. .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
  8322. .rename_done = nfs4_proc_rename_done,
  8323. .link = nfs4_proc_link,
  8324. .symlink = nfs4_proc_symlink,
  8325. .mkdir = nfs4_proc_mkdir,
  8326. .rmdir = nfs4_proc_remove,
  8327. .readdir = nfs4_proc_readdir,
  8328. .mknod = nfs4_proc_mknod,
  8329. .statfs = nfs4_proc_statfs,
  8330. .fsinfo = nfs4_proc_fsinfo,
  8331. .pathconf = nfs4_proc_pathconf,
  8332. .set_capabilities = nfs4_server_capabilities,
  8333. .decode_dirent = nfs4_decode_dirent,
  8334. .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
  8335. .read_setup = nfs4_proc_read_setup,
  8336. .read_done = nfs4_read_done,
  8337. .write_setup = nfs4_proc_write_setup,
  8338. .write_done = nfs4_write_done,
  8339. .commit_setup = nfs4_proc_commit_setup,
  8340. .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
  8341. .commit_done = nfs4_commit_done,
  8342. .lock = nfs4_proc_lock,
  8343. .clear_acl_cache = nfs4_zap_acl_attr,
  8344. .close_context = nfs4_close_context,
  8345. .open_context = nfs4_atomic_open,
  8346. .have_delegation = nfs4_have_delegation,
  8347. .return_delegation = nfs4_inode_return_delegation,
  8348. .alloc_client = nfs4_alloc_client,
  8349. .init_client = nfs4_init_client,
  8350. .free_client = nfs4_free_client,
  8351. .create_server = nfs4_create_server,
  8352. .clone_server = nfs_clone_server,
  8353. };
  8354. static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
  8355. .name = XATTR_NAME_NFSV4_ACL,
  8356. .list = nfs4_xattr_list_nfs4_acl,
  8357. .get = nfs4_xattr_get_nfs4_acl,
  8358. .set = nfs4_xattr_set_nfs4_acl,
  8359. };
  8360. const struct xattr_handler *nfs4_xattr_handlers[] = {
  8361. &nfs4_xattr_nfs4_acl_handler,
  8362. #ifdef CONFIG_NFS_V4_SECURITY_LABEL
  8363. &nfs4_xattr_nfs4_label_handler,
  8364. #endif
  8365. NULL
  8366. };
  8367. /*
  8368. * Local variables:
  8369. * c-basic-offset: 8
  8370. * End:
  8371. */