nfs4proc.c 236 KB

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