nfs4proc.c 253 KB

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