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