nfs4proc.c 252 KB

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