nfs4proc.c 252 KB

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