nfs4proc.c 252 KB

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