nfs4proc.c 253 KB

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