nfs4proc.c 230 KB

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