nfs4proc.c 255 KB

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