nfs4proc.c 251 KB

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