nfs4proc.c 233 KB

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