nfs4proc.c 240 KB

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