nfs4proc.c 233 KB

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