nfs4proc.c 230 KB

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