nfs4proc.c 237 KB

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