nfs4proc.c 238 KB

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