nfs4proc.c 239 KB

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