nfs4proc.c 187 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027
  1. /*
  2. * fs/nfs/nfs4proc.c
  3. *
  4. * Client-side procedure declarations for NFSv4.
  5. *
  6. * Copyright (c) 2002 The Regents of the University of Michigan.
  7. * All rights reserved.
  8. *
  9. * Kendrick Smith <kmsmith@umich.edu>
  10. * Andy Adamson <andros@umich.edu>
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. *
  16. * 1. Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. * 3. Neither the name of the University nor the names of its
  22. * contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  26. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  27. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  32. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #include <linux/mm.h>
  38. #include <linux/delay.h>
  39. #include <linux/errno.h>
  40. #include <linux/string.h>
  41. #include <linux/ratelimit.h>
  42. #include <linux/printk.h>
  43. #include <linux/slab.h>
  44. #include <linux/sunrpc/clnt.h>
  45. #include <linux/nfs.h>
  46. #include <linux/nfs4.h>
  47. #include <linux/nfs_fs.h>
  48. #include <linux/nfs_page.h>
  49. #include <linux/nfs_mount.h>
  50. #include <linux/namei.h>
  51. #include <linux/mount.h>
  52. #include <linux/module.h>
  53. #include <linux/nfs_idmap.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 "nfs4session.h"
  65. #include "fscache.h"
  66. #define NFSDBG_FACILITY NFSDBG_PROC
  67. #define NFS4_POLL_RETRY_MIN (HZ/10)
  68. #define NFS4_POLL_RETRY_MAX (15*HZ)
  69. struct nfs4_opendata;
  70. static int _nfs4_proc_open(struct nfs4_opendata *data);
  71. static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
  72. static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
  73. static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
  74. static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
  75. static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
  76. static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
  77. static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
  78. struct nfs_fattr *fattr, struct iattr *sattr,
  79. struct nfs4_state *state);
  80. #ifdef CONFIG_NFS_V4_1
  81. static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
  82. static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
  83. #endif
  84. /* Prevent leaks of NFSv4 errors into userland */
  85. static int nfs4_map_errors(int err)
  86. {
  87. if (err >= -1000)
  88. return err;
  89. switch (err) {
  90. case -NFS4ERR_RESOURCE:
  91. case -NFS4ERR_LAYOUTTRYLATER:
  92. case -NFS4ERR_RECALLCONFLICT:
  93. return -EREMOTEIO;
  94. case -NFS4ERR_WRONGSEC:
  95. return -EPERM;
  96. case -NFS4ERR_BADOWNER:
  97. case -NFS4ERR_BADNAME:
  98. return -EINVAL;
  99. case -NFS4ERR_SHARE_DENIED:
  100. return -EACCES;
  101. case -NFS4ERR_MINOR_VERS_MISMATCH:
  102. return -EPROTONOSUPPORT;
  103. case -NFS4ERR_ACCESS:
  104. return -EACCES;
  105. case -NFS4ERR_FILE_OPEN:
  106. return -EBUSY;
  107. default:
  108. dprintk("%s could not handle NFSv4 error %d\n",
  109. __func__, -err);
  110. break;
  111. }
  112. return -EIO;
  113. }
  114. /*
  115. * This is our standard bitmap for GETATTR requests.
  116. */
  117. const u32 nfs4_fattr_bitmap[3] = {
  118. FATTR4_WORD0_TYPE
  119. | FATTR4_WORD0_CHANGE
  120. | FATTR4_WORD0_SIZE
  121. | FATTR4_WORD0_FSID
  122. | FATTR4_WORD0_FILEID,
  123. FATTR4_WORD1_MODE
  124. | FATTR4_WORD1_NUMLINKS
  125. | FATTR4_WORD1_OWNER
  126. | FATTR4_WORD1_OWNER_GROUP
  127. | FATTR4_WORD1_RAWDEV
  128. | FATTR4_WORD1_SPACE_USED
  129. | FATTR4_WORD1_TIME_ACCESS
  130. | FATTR4_WORD1_TIME_METADATA
  131. | FATTR4_WORD1_TIME_MODIFY
  132. };
  133. static const u32 nfs4_pnfs_open_bitmap[3] = {
  134. FATTR4_WORD0_TYPE
  135. | FATTR4_WORD0_CHANGE
  136. | FATTR4_WORD0_SIZE
  137. | FATTR4_WORD0_FSID
  138. | FATTR4_WORD0_FILEID,
  139. FATTR4_WORD1_MODE
  140. | FATTR4_WORD1_NUMLINKS
  141. | FATTR4_WORD1_OWNER
  142. | FATTR4_WORD1_OWNER_GROUP
  143. | FATTR4_WORD1_RAWDEV
  144. | FATTR4_WORD1_SPACE_USED
  145. | FATTR4_WORD1_TIME_ACCESS
  146. | FATTR4_WORD1_TIME_METADATA
  147. | FATTR4_WORD1_TIME_MODIFY,
  148. FATTR4_WORD2_MDSTHRESHOLD
  149. };
  150. static const u32 nfs4_open_noattr_bitmap[3] = {
  151. FATTR4_WORD0_TYPE
  152. | FATTR4_WORD0_CHANGE
  153. | FATTR4_WORD0_FILEID,
  154. };
  155. const u32 nfs4_statfs_bitmap[2] = {
  156. FATTR4_WORD0_FILES_AVAIL
  157. | FATTR4_WORD0_FILES_FREE
  158. | FATTR4_WORD0_FILES_TOTAL,
  159. FATTR4_WORD1_SPACE_AVAIL
  160. | FATTR4_WORD1_SPACE_FREE
  161. | FATTR4_WORD1_SPACE_TOTAL
  162. };
  163. const u32 nfs4_pathconf_bitmap[2] = {
  164. FATTR4_WORD0_MAXLINK
  165. | FATTR4_WORD0_MAXNAME,
  166. 0
  167. };
  168. const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
  169. | FATTR4_WORD0_MAXREAD
  170. | FATTR4_WORD0_MAXWRITE
  171. | FATTR4_WORD0_LEASE_TIME,
  172. FATTR4_WORD1_TIME_DELTA
  173. | FATTR4_WORD1_FS_LAYOUT_TYPES,
  174. FATTR4_WORD2_LAYOUT_BLKSIZE
  175. };
  176. const u32 nfs4_fs_locations_bitmap[2] = {
  177. FATTR4_WORD0_TYPE
  178. | FATTR4_WORD0_CHANGE
  179. | FATTR4_WORD0_SIZE
  180. | FATTR4_WORD0_FSID
  181. | FATTR4_WORD0_FILEID
  182. | FATTR4_WORD0_FS_LOCATIONS,
  183. FATTR4_WORD1_MODE
  184. | FATTR4_WORD1_NUMLINKS
  185. | FATTR4_WORD1_OWNER
  186. | FATTR4_WORD1_OWNER_GROUP
  187. | FATTR4_WORD1_RAWDEV
  188. | FATTR4_WORD1_SPACE_USED
  189. | FATTR4_WORD1_TIME_ACCESS
  190. | FATTR4_WORD1_TIME_METADATA
  191. | FATTR4_WORD1_TIME_MODIFY
  192. | FATTR4_WORD1_MOUNTED_ON_FILEID
  193. };
  194. static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
  195. struct nfs4_readdir_arg *readdir)
  196. {
  197. __be32 *start, *p;
  198. if (cookie > 2) {
  199. readdir->cookie = cookie;
  200. memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
  201. return;
  202. }
  203. readdir->cookie = 0;
  204. memset(&readdir->verifier, 0, sizeof(readdir->verifier));
  205. if (cookie == 2)
  206. return;
  207. /*
  208. * NFSv4 servers do not return entries for '.' and '..'
  209. * Therefore, we fake these entries here. We let '.'
  210. * have cookie 0 and '..' have cookie 1. Note that
  211. * when talking to the server, we always send cookie 0
  212. * instead of 1 or 2.
  213. */
  214. start = p = kmap_atomic(*readdir->pages);
  215. if (cookie == 0) {
  216. *p++ = xdr_one; /* next */
  217. *p++ = xdr_zero; /* cookie, first word */
  218. *p++ = xdr_one; /* cookie, second word */
  219. *p++ = xdr_one; /* entry len */
  220. memcpy(p, ".\0\0\0", 4); /* entry */
  221. p++;
  222. *p++ = xdr_one; /* bitmap length */
  223. *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
  224. *p++ = htonl(8); /* attribute buffer length */
  225. p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
  226. }
  227. *p++ = xdr_one; /* next */
  228. *p++ = xdr_zero; /* cookie, first word */
  229. *p++ = xdr_two; /* cookie, second word */
  230. *p++ = xdr_two; /* entry len */
  231. memcpy(p, "..\0\0", 4); /* entry */
  232. p++;
  233. *p++ = xdr_one; /* bitmap length */
  234. *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
  235. *p++ = htonl(8); /* attribute buffer length */
  236. p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
  237. readdir->pgbase = (char *)p - (char *)start;
  238. readdir->count -= readdir->pgbase;
  239. kunmap_atomic(start);
  240. }
  241. static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
  242. {
  243. int res = 0;
  244. might_sleep();
  245. if (*timeout <= 0)
  246. *timeout = NFS4_POLL_RETRY_MIN;
  247. if (*timeout > NFS4_POLL_RETRY_MAX)
  248. *timeout = NFS4_POLL_RETRY_MAX;
  249. freezable_schedule_timeout_killable(*timeout);
  250. if (fatal_signal_pending(current))
  251. res = -ERESTARTSYS;
  252. *timeout <<= 1;
  253. return res;
  254. }
  255. /* This is the error handling routine for processes that are allowed
  256. * to sleep.
  257. */
  258. static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
  259. {
  260. struct nfs_client *clp = server->nfs_client;
  261. struct nfs4_state *state = exception->state;
  262. struct inode *inode = exception->inode;
  263. int ret = errorcode;
  264. exception->retry = 0;
  265. switch(errorcode) {
  266. case 0:
  267. return 0;
  268. case -NFS4ERR_OPENMODE:
  269. if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
  270. nfs4_inode_return_delegation(inode);
  271. exception->retry = 1;
  272. return 0;
  273. }
  274. if (state == NULL)
  275. break;
  276. ret = nfs4_schedule_stateid_recovery(server, state);
  277. if (ret < 0)
  278. break;
  279. goto wait_on_recovery;
  280. case -NFS4ERR_DELEG_REVOKED:
  281. case -NFS4ERR_ADMIN_REVOKED:
  282. case -NFS4ERR_BAD_STATEID:
  283. if (state == NULL)
  284. break;
  285. nfs_remove_bad_delegation(state->inode);
  286. ret = nfs4_schedule_stateid_recovery(server, state);
  287. if (ret < 0)
  288. break;
  289. goto wait_on_recovery;
  290. case -NFS4ERR_EXPIRED:
  291. if (state != NULL) {
  292. ret = nfs4_schedule_stateid_recovery(server, state);
  293. if (ret < 0)
  294. break;
  295. }
  296. case -NFS4ERR_STALE_STATEID:
  297. case -NFS4ERR_STALE_CLIENTID:
  298. nfs4_schedule_lease_recovery(clp);
  299. goto wait_on_recovery;
  300. #if defined(CONFIG_NFS_V4_1)
  301. case -NFS4ERR_BADSESSION:
  302. case -NFS4ERR_BADSLOT:
  303. case -NFS4ERR_BAD_HIGH_SLOT:
  304. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  305. case -NFS4ERR_DEADSESSION:
  306. case -NFS4ERR_SEQ_FALSE_RETRY:
  307. case -NFS4ERR_SEQ_MISORDERED:
  308. dprintk("%s ERROR: %d Reset session\n", __func__,
  309. errorcode);
  310. nfs4_schedule_session_recovery(clp->cl_session, errorcode);
  311. goto wait_on_recovery;
  312. #endif /* defined(CONFIG_NFS_V4_1) */
  313. case -NFS4ERR_FILE_OPEN:
  314. if (exception->timeout > HZ) {
  315. /* We have retried a decent amount, time to
  316. * fail
  317. */
  318. ret = -EBUSY;
  319. break;
  320. }
  321. case -NFS4ERR_GRACE:
  322. case -NFS4ERR_DELAY:
  323. ret = nfs4_delay(server->client, &exception->timeout);
  324. if (ret != 0)
  325. break;
  326. case -NFS4ERR_RETRY_UNCACHED_REP:
  327. case -NFS4ERR_OLD_STATEID:
  328. exception->retry = 1;
  329. break;
  330. case -NFS4ERR_BADOWNER:
  331. /* The following works around a Linux server bug! */
  332. case -NFS4ERR_BADNAME:
  333. if (server->caps & NFS_CAP_UIDGID_NOMAP) {
  334. server->caps &= ~NFS_CAP_UIDGID_NOMAP;
  335. exception->retry = 1;
  336. printk(KERN_WARNING "NFS: v4 server %s "
  337. "does not accept raw "
  338. "uid/gids. "
  339. "Reenabling the idmapper.\n",
  340. server->nfs_client->cl_hostname);
  341. }
  342. }
  343. /* We failed to handle the error */
  344. return nfs4_map_errors(ret);
  345. wait_on_recovery:
  346. ret = nfs4_wait_clnt_recover(clp);
  347. if (ret == 0)
  348. exception->retry = 1;
  349. return ret;
  350. }
  351. static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
  352. {
  353. spin_lock(&clp->cl_lock);
  354. if (time_before(clp->cl_last_renewal,timestamp))
  355. clp->cl_last_renewal = timestamp;
  356. spin_unlock(&clp->cl_lock);
  357. }
  358. static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
  359. {
  360. do_renew_lease(server->nfs_client, timestamp);
  361. }
  362. #if defined(CONFIG_NFS_V4_1)
  363. static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
  364. {
  365. struct nfs4_session *session;
  366. struct nfs4_slot_table *tbl;
  367. bool send_new_highest_used_slotid = false;
  368. if (!res->sr_slot) {
  369. /* just wake up the next guy waiting since
  370. * we may have not consumed a slot after all */
  371. dprintk("%s: No slot\n", __func__);
  372. return;
  373. }
  374. tbl = res->sr_slot->table;
  375. session = tbl->session;
  376. spin_lock(&tbl->slot_tbl_lock);
  377. /* Be nice to the server: try to ensure that the last transmitted
  378. * value for highest_user_slotid <= target_highest_slotid
  379. */
  380. if (tbl->highest_used_slotid > tbl->target_highest_slotid)
  381. send_new_highest_used_slotid = true;
  382. if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
  383. send_new_highest_used_slotid = false;
  384. goto out_unlock;
  385. }
  386. nfs4_free_slot(tbl, res->sr_slot);
  387. if (tbl->highest_used_slotid != NFS4_NO_SLOT)
  388. send_new_highest_used_slotid = false;
  389. out_unlock:
  390. spin_unlock(&tbl->slot_tbl_lock);
  391. res->sr_slot = NULL;
  392. if (send_new_highest_used_slotid)
  393. nfs41_server_notify_highest_slotid_update(session->clp);
  394. }
  395. static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
  396. {
  397. struct nfs4_session *session;
  398. struct nfs4_slot *slot;
  399. struct nfs_client *clp;
  400. bool interrupted = false;
  401. int ret = 1;
  402. /* don't increment the sequence number if the task wasn't sent */
  403. if (!RPC_WAS_SENT(task))
  404. goto out;
  405. slot = res->sr_slot;
  406. session = slot->table->session;
  407. if (slot->interrupted) {
  408. slot->interrupted = 0;
  409. interrupted = true;
  410. }
  411. /* Check the SEQUENCE operation status */
  412. switch (res->sr_status) {
  413. case 0:
  414. /* Update the slot's sequence and clientid lease timer */
  415. ++slot->seq_nr;
  416. clp = session->clp;
  417. do_renew_lease(clp, res->sr_timestamp);
  418. /* Check sequence flags */
  419. if (res->sr_status_flags != 0)
  420. nfs4_schedule_lease_recovery(clp);
  421. nfs41_update_target_slotid(slot->table, slot, res);
  422. break;
  423. case 1:
  424. /*
  425. * sr_status remains 1 if an RPC level error occurred.
  426. * The server may or may not have processed the sequence
  427. * operation..
  428. * Mark the slot as having hosted an interrupted RPC call.
  429. */
  430. slot->interrupted = 1;
  431. goto out;
  432. case -NFS4ERR_DELAY:
  433. /* The server detected a resend of the RPC call and
  434. * returned NFS4ERR_DELAY as per Section 2.10.6.2
  435. * of RFC5661.
  436. */
  437. dprintk("%s: slot=%u seq=%u: Operation in progress\n",
  438. __func__,
  439. slot->slot_nr,
  440. slot->seq_nr);
  441. goto out_retry;
  442. case -NFS4ERR_BADSLOT:
  443. /*
  444. * The slot id we used was probably retired. Try again
  445. * using a different slot id.
  446. */
  447. goto retry_nowait;
  448. case -NFS4ERR_SEQ_MISORDERED:
  449. /*
  450. * Was the last operation on this sequence interrupted?
  451. * If so, retry after bumping the sequence number.
  452. */
  453. if (interrupted) {
  454. ++slot->seq_nr;
  455. goto retry_nowait;
  456. }
  457. /*
  458. * Could this slot have been previously retired?
  459. * If so, then the server may be expecting seq_nr = 1!
  460. */
  461. if (slot->seq_nr != 1) {
  462. slot->seq_nr = 1;
  463. goto retry_nowait;
  464. }
  465. break;
  466. case -NFS4ERR_SEQ_FALSE_RETRY:
  467. ++slot->seq_nr;
  468. goto retry_nowait;
  469. default:
  470. /* Just update the slot sequence no. */
  471. ++slot->seq_nr;
  472. }
  473. out:
  474. /* The session may be reset by one of the error handlers. */
  475. dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
  476. nfs41_sequence_free_slot(res);
  477. return ret;
  478. retry_nowait:
  479. if (rpc_restart_call_prepare(task)) {
  480. task->tk_status = 0;
  481. ret = 0;
  482. }
  483. goto out;
  484. out_retry:
  485. if (!rpc_restart_call(task))
  486. goto out;
  487. rpc_delay(task, NFS4_POLL_RETRY_MAX);
  488. return 0;
  489. }
  490. static int nfs4_sequence_done(struct rpc_task *task,
  491. struct nfs4_sequence_res *res)
  492. {
  493. if (res->sr_slot == NULL)
  494. return 1;
  495. return nfs41_sequence_done(task, res);
  496. }
  497. static void nfs41_init_sequence(struct nfs4_sequence_args *args,
  498. struct nfs4_sequence_res *res, int cache_reply)
  499. {
  500. args->sa_slot = NULL;
  501. args->sa_cache_this = 0;
  502. args->sa_privileged = 0;
  503. if (cache_reply)
  504. args->sa_cache_this = 1;
  505. res->sr_slot = NULL;
  506. }
  507. static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
  508. {
  509. args->sa_privileged = 1;
  510. }
  511. int nfs41_setup_sequence(struct nfs4_session *session,
  512. struct nfs4_sequence_args *args,
  513. struct nfs4_sequence_res *res,
  514. struct rpc_task *task)
  515. {
  516. struct nfs4_slot *slot;
  517. struct nfs4_slot_table *tbl;
  518. dprintk("--> %s\n", __func__);
  519. /* slot already allocated? */
  520. if (res->sr_slot != NULL)
  521. goto out_success;
  522. tbl = &session->fc_slot_table;
  523. task->tk_timeout = 0;
  524. spin_lock(&tbl->slot_tbl_lock);
  525. if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
  526. !args->sa_privileged) {
  527. /* The state manager will wait until the slot table is empty */
  528. dprintk("%s session is draining\n", __func__);
  529. goto out_sleep;
  530. }
  531. slot = nfs4_alloc_slot(tbl);
  532. if (IS_ERR(slot)) {
  533. /* If out of memory, try again in 1/4 second */
  534. if (slot == ERR_PTR(-ENOMEM))
  535. task->tk_timeout = HZ >> 2;
  536. dprintk("<-- %s: no free slots\n", __func__);
  537. goto out_sleep;
  538. }
  539. spin_unlock(&tbl->slot_tbl_lock);
  540. args->sa_slot = slot;
  541. dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
  542. slot->slot_nr, slot->seq_nr);
  543. res->sr_slot = slot;
  544. res->sr_timestamp = jiffies;
  545. res->sr_status_flags = 0;
  546. /*
  547. * sr_status is only set in decode_sequence, and so will remain
  548. * set to 1 if an rpc level failure occurs.
  549. */
  550. res->sr_status = 1;
  551. out_success:
  552. rpc_call_start(task);
  553. return 0;
  554. out_sleep:
  555. /* Privileged tasks are queued with top priority */
  556. if (args->sa_privileged)
  557. rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
  558. NULL, RPC_PRIORITY_PRIVILEGED);
  559. else
  560. rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
  561. spin_unlock(&tbl->slot_tbl_lock);
  562. return -EAGAIN;
  563. }
  564. EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
  565. int nfs4_setup_sequence(const struct nfs_server *server,
  566. struct nfs4_sequence_args *args,
  567. struct nfs4_sequence_res *res,
  568. struct rpc_task *task)
  569. {
  570. struct nfs4_session *session = nfs4_get_session(server);
  571. int ret = 0;
  572. if (session == NULL) {
  573. rpc_call_start(task);
  574. goto out;
  575. }
  576. dprintk("--> %s clp %p session %p sr_slot %d\n",
  577. __func__, session->clp, session, res->sr_slot ?
  578. res->sr_slot->slot_nr : -1);
  579. ret = nfs41_setup_sequence(session, args, res, task);
  580. out:
  581. dprintk("<-- %s status=%d\n", __func__, ret);
  582. return ret;
  583. }
  584. struct nfs41_call_sync_data {
  585. const struct nfs_server *seq_server;
  586. struct nfs4_sequence_args *seq_args;
  587. struct nfs4_sequence_res *seq_res;
  588. };
  589. static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
  590. {
  591. struct nfs41_call_sync_data *data = calldata;
  592. struct nfs4_session *session = nfs4_get_session(data->seq_server);
  593. dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
  594. nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
  595. }
  596. static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
  597. {
  598. struct nfs41_call_sync_data *data = calldata;
  599. nfs41_sequence_done(task, data->seq_res);
  600. }
  601. static const struct rpc_call_ops nfs41_call_sync_ops = {
  602. .rpc_call_prepare = nfs41_call_sync_prepare,
  603. .rpc_call_done = nfs41_call_sync_done,
  604. };
  605. static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
  606. struct nfs_server *server,
  607. struct rpc_message *msg,
  608. struct nfs4_sequence_args *args,
  609. struct nfs4_sequence_res *res)
  610. {
  611. int ret;
  612. struct rpc_task *task;
  613. struct nfs41_call_sync_data data = {
  614. .seq_server = server,
  615. .seq_args = args,
  616. .seq_res = res,
  617. };
  618. struct rpc_task_setup task_setup = {
  619. .rpc_client = clnt,
  620. .rpc_message = msg,
  621. .callback_ops = &nfs41_call_sync_ops,
  622. .callback_data = &data
  623. };
  624. task = rpc_run_task(&task_setup);
  625. if (IS_ERR(task))
  626. ret = PTR_ERR(task);
  627. else {
  628. ret = task->tk_status;
  629. rpc_put_task(task);
  630. }
  631. return ret;
  632. }
  633. #else
  634. static
  635. void nfs41_init_sequence(struct nfs4_sequence_args *args,
  636. struct nfs4_sequence_res *res, int cache_reply)
  637. {
  638. }
  639. static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
  640. {
  641. }
  642. static int nfs4_sequence_done(struct rpc_task *task,
  643. struct nfs4_sequence_res *res)
  644. {
  645. return 1;
  646. }
  647. #endif /* CONFIG_NFS_V4_1 */
  648. static
  649. int _nfs4_call_sync(struct rpc_clnt *clnt,
  650. struct nfs_server *server,
  651. struct rpc_message *msg,
  652. struct nfs4_sequence_args *args,
  653. struct nfs4_sequence_res *res)
  654. {
  655. return rpc_call_sync(clnt, msg, 0);
  656. }
  657. static
  658. int nfs4_call_sync(struct rpc_clnt *clnt,
  659. struct nfs_server *server,
  660. struct rpc_message *msg,
  661. struct nfs4_sequence_args *args,
  662. struct nfs4_sequence_res *res,
  663. int cache_reply)
  664. {
  665. nfs41_init_sequence(args, res, cache_reply);
  666. return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
  667. args, res);
  668. }
  669. static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
  670. {
  671. struct nfs_inode *nfsi = NFS_I(dir);
  672. spin_lock(&dir->i_lock);
  673. nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
  674. if (!cinfo->atomic || cinfo->before != dir->i_version)
  675. nfs_force_lookup_revalidate(dir);
  676. dir->i_version = cinfo->after;
  677. nfs_fscache_invalidate(dir);
  678. spin_unlock(&dir->i_lock);
  679. }
  680. struct nfs4_opendata {
  681. struct kref kref;
  682. struct nfs_openargs o_arg;
  683. struct nfs_openres o_res;
  684. struct nfs_open_confirmargs c_arg;
  685. struct nfs_open_confirmres c_res;
  686. struct nfs4_string owner_name;
  687. struct nfs4_string group_name;
  688. struct nfs_fattr f_attr;
  689. struct dentry *dir;
  690. struct dentry *dentry;
  691. struct nfs4_state_owner *owner;
  692. struct nfs4_state *state;
  693. struct iattr attrs;
  694. unsigned long timestamp;
  695. unsigned int rpc_done : 1;
  696. int rpc_status;
  697. int cancelled;
  698. };
  699. static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
  700. int err, struct nfs4_exception *exception)
  701. {
  702. if (err != -EINVAL)
  703. return false;
  704. if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
  705. return false;
  706. server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
  707. exception->retry = 1;
  708. return true;
  709. }
  710. static enum open_claim_type4
  711. nfs4_map_atomic_open_claim(struct nfs_server *server,
  712. enum open_claim_type4 claim)
  713. {
  714. if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
  715. return claim;
  716. switch (claim) {
  717. default:
  718. return claim;
  719. case NFS4_OPEN_CLAIM_FH:
  720. return NFS4_OPEN_CLAIM_NULL;
  721. case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
  722. return NFS4_OPEN_CLAIM_DELEGATE_CUR;
  723. case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
  724. return NFS4_OPEN_CLAIM_DELEGATE_PREV;
  725. }
  726. }
  727. static void nfs4_init_opendata_res(struct nfs4_opendata *p)
  728. {
  729. p->o_res.f_attr = &p->f_attr;
  730. p->o_res.seqid = p->o_arg.seqid;
  731. p->c_res.seqid = p->c_arg.seqid;
  732. p->o_res.server = p->o_arg.server;
  733. p->o_res.access_request = p->o_arg.access;
  734. nfs_fattr_init(&p->f_attr);
  735. nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
  736. }
  737. static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
  738. struct nfs4_state_owner *sp, fmode_t fmode, int flags,
  739. const struct iattr *attrs,
  740. enum open_claim_type4 claim,
  741. gfp_t gfp_mask)
  742. {
  743. struct dentry *parent = dget_parent(dentry);
  744. struct inode *dir = parent->d_inode;
  745. struct nfs_server *server = NFS_SERVER(dir);
  746. struct nfs4_opendata *p;
  747. p = kzalloc(sizeof(*p), gfp_mask);
  748. if (p == NULL)
  749. goto err;
  750. p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
  751. if (p->o_arg.seqid == NULL)
  752. goto err_free;
  753. nfs_sb_active(dentry->d_sb);
  754. p->dentry = dget(dentry);
  755. p->dir = parent;
  756. p->owner = sp;
  757. atomic_inc(&sp->so_count);
  758. p->o_arg.open_flags = flags;
  759. p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
  760. /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
  761. * will return permission denied for all bits until close */
  762. if (!(flags & O_EXCL)) {
  763. /* ask server to check for all possible rights as results
  764. * are cached */
  765. p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
  766. NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
  767. }
  768. p->o_arg.clientid = server->nfs_client->cl_clientid;
  769. p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
  770. p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
  771. p->o_arg.name = &dentry->d_name;
  772. p->o_arg.server = server;
  773. p->o_arg.bitmask = server->attr_bitmask;
  774. p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
  775. p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
  776. switch (p->o_arg.claim) {
  777. case NFS4_OPEN_CLAIM_NULL:
  778. case NFS4_OPEN_CLAIM_DELEGATE_CUR:
  779. case NFS4_OPEN_CLAIM_DELEGATE_PREV:
  780. p->o_arg.fh = NFS_FH(dir);
  781. break;
  782. case NFS4_OPEN_CLAIM_PREVIOUS:
  783. case NFS4_OPEN_CLAIM_FH:
  784. case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
  785. case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
  786. p->o_arg.fh = NFS_FH(dentry->d_inode);
  787. }
  788. if (attrs != NULL && attrs->ia_valid != 0) {
  789. __be32 verf[2];
  790. p->o_arg.u.attrs = &p->attrs;
  791. memcpy(&p->attrs, attrs, sizeof(p->attrs));
  792. verf[0] = jiffies;
  793. verf[1] = current->pid;
  794. memcpy(p->o_arg.u.verifier.data, verf,
  795. sizeof(p->o_arg.u.verifier.data));
  796. }
  797. p->c_arg.fh = &p->o_res.fh;
  798. p->c_arg.stateid = &p->o_res.stateid;
  799. p->c_arg.seqid = p->o_arg.seqid;
  800. nfs4_init_opendata_res(p);
  801. kref_init(&p->kref);
  802. return p;
  803. err_free:
  804. kfree(p);
  805. err:
  806. dput(parent);
  807. return NULL;
  808. }
  809. static void nfs4_opendata_free(struct kref *kref)
  810. {
  811. struct nfs4_opendata *p = container_of(kref,
  812. struct nfs4_opendata, kref);
  813. struct super_block *sb = p->dentry->d_sb;
  814. nfs_free_seqid(p->o_arg.seqid);
  815. if (p->state != NULL)
  816. nfs4_put_open_state(p->state);
  817. nfs4_put_state_owner(p->owner);
  818. dput(p->dir);
  819. dput(p->dentry);
  820. nfs_sb_deactive(sb);
  821. nfs_fattr_free_names(&p->f_attr);
  822. kfree(p);
  823. }
  824. static void nfs4_opendata_put(struct nfs4_opendata *p)
  825. {
  826. if (p != NULL)
  827. kref_put(&p->kref, nfs4_opendata_free);
  828. }
  829. static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
  830. {
  831. int ret;
  832. ret = rpc_wait_for_completion_task(task);
  833. return ret;
  834. }
  835. static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
  836. {
  837. int ret = 0;
  838. if (open_mode & (O_EXCL|O_TRUNC))
  839. goto out;
  840. switch (mode & (FMODE_READ|FMODE_WRITE)) {
  841. case FMODE_READ:
  842. ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
  843. && state->n_rdonly != 0;
  844. break;
  845. case FMODE_WRITE:
  846. ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
  847. && state->n_wronly != 0;
  848. break;
  849. case FMODE_READ|FMODE_WRITE:
  850. ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
  851. && state->n_rdwr != 0;
  852. }
  853. out:
  854. return ret;
  855. }
  856. static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
  857. {
  858. if (delegation == NULL)
  859. return 0;
  860. if ((delegation->type & fmode) != fmode)
  861. return 0;
  862. if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
  863. return 0;
  864. if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
  865. return 0;
  866. nfs_mark_delegation_referenced(delegation);
  867. return 1;
  868. }
  869. static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
  870. {
  871. switch (fmode) {
  872. case FMODE_WRITE:
  873. state->n_wronly++;
  874. break;
  875. case FMODE_READ:
  876. state->n_rdonly++;
  877. break;
  878. case FMODE_READ|FMODE_WRITE:
  879. state->n_rdwr++;
  880. }
  881. nfs4_state_set_mode_locked(state, state->state | fmode);
  882. }
  883. static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
  884. {
  885. if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
  886. nfs4_stateid_copy(&state->stateid, stateid);
  887. nfs4_stateid_copy(&state->open_stateid, stateid);
  888. switch (fmode) {
  889. case FMODE_READ:
  890. set_bit(NFS_O_RDONLY_STATE, &state->flags);
  891. break;
  892. case FMODE_WRITE:
  893. set_bit(NFS_O_WRONLY_STATE, &state->flags);
  894. break;
  895. case FMODE_READ|FMODE_WRITE:
  896. set_bit(NFS_O_RDWR_STATE, &state->flags);
  897. }
  898. }
  899. static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
  900. {
  901. write_seqlock(&state->seqlock);
  902. nfs_set_open_stateid_locked(state, stateid, fmode);
  903. write_sequnlock(&state->seqlock);
  904. }
  905. static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
  906. {
  907. /*
  908. * Protect the call to nfs4_state_set_mode_locked and
  909. * serialise the stateid update
  910. */
  911. write_seqlock(&state->seqlock);
  912. if (deleg_stateid != NULL) {
  913. nfs4_stateid_copy(&state->stateid, deleg_stateid);
  914. set_bit(NFS_DELEGATED_STATE, &state->flags);
  915. }
  916. if (open_stateid != NULL)
  917. nfs_set_open_stateid_locked(state, open_stateid, fmode);
  918. write_sequnlock(&state->seqlock);
  919. spin_lock(&state->owner->so_lock);
  920. update_open_stateflags(state, fmode);
  921. spin_unlock(&state->owner->so_lock);
  922. }
  923. static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
  924. {
  925. struct nfs_inode *nfsi = NFS_I(state->inode);
  926. struct nfs_delegation *deleg_cur;
  927. int ret = 0;
  928. fmode &= (FMODE_READ|FMODE_WRITE);
  929. rcu_read_lock();
  930. deleg_cur = rcu_dereference(nfsi->delegation);
  931. if (deleg_cur == NULL)
  932. goto no_delegation;
  933. spin_lock(&deleg_cur->lock);
  934. if (nfsi->delegation != deleg_cur ||
  935. test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
  936. (deleg_cur->type & fmode) != fmode)
  937. goto no_delegation_unlock;
  938. if (delegation == NULL)
  939. delegation = &deleg_cur->stateid;
  940. else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
  941. goto no_delegation_unlock;
  942. nfs_mark_delegation_referenced(deleg_cur);
  943. __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
  944. ret = 1;
  945. no_delegation_unlock:
  946. spin_unlock(&deleg_cur->lock);
  947. no_delegation:
  948. rcu_read_unlock();
  949. if (!ret && open_stateid != NULL) {
  950. __update_open_stateid(state, open_stateid, NULL, fmode);
  951. ret = 1;
  952. }
  953. return ret;
  954. }
  955. static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
  956. {
  957. struct nfs_delegation *delegation;
  958. rcu_read_lock();
  959. delegation = rcu_dereference(NFS_I(inode)->delegation);
  960. if (delegation == NULL || (delegation->type & fmode) == fmode) {
  961. rcu_read_unlock();
  962. return;
  963. }
  964. rcu_read_unlock();
  965. nfs4_inode_return_delegation(inode);
  966. }
  967. static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
  968. {
  969. struct nfs4_state *state = opendata->state;
  970. struct nfs_inode *nfsi = NFS_I(state->inode);
  971. struct nfs_delegation *delegation;
  972. int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
  973. fmode_t fmode = opendata->o_arg.fmode;
  974. nfs4_stateid stateid;
  975. int ret = -EAGAIN;
  976. for (;;) {
  977. if (can_open_cached(state, fmode, open_mode)) {
  978. spin_lock(&state->owner->so_lock);
  979. if (can_open_cached(state, fmode, open_mode)) {
  980. update_open_stateflags(state, fmode);
  981. spin_unlock(&state->owner->so_lock);
  982. goto out_return_state;
  983. }
  984. spin_unlock(&state->owner->so_lock);
  985. }
  986. rcu_read_lock();
  987. delegation = rcu_dereference(nfsi->delegation);
  988. if (!can_open_delegated(delegation, fmode)) {
  989. rcu_read_unlock();
  990. break;
  991. }
  992. /* Save the delegation */
  993. nfs4_stateid_copy(&stateid, &delegation->stateid);
  994. rcu_read_unlock();
  995. ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
  996. if (ret != 0)
  997. goto out;
  998. ret = -EAGAIN;
  999. /* Try to update the stateid using the delegation */
  1000. if (update_open_stateid(state, NULL, &stateid, fmode))
  1001. goto out_return_state;
  1002. }
  1003. out:
  1004. return ERR_PTR(ret);
  1005. out_return_state:
  1006. atomic_inc(&state->count);
  1007. return state;
  1008. }
  1009. static void
  1010. nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
  1011. {
  1012. struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
  1013. struct nfs_delegation *delegation;
  1014. int delegation_flags = 0;
  1015. rcu_read_lock();
  1016. delegation = rcu_dereference(NFS_I(state->inode)->delegation);
  1017. if (delegation)
  1018. delegation_flags = delegation->flags;
  1019. rcu_read_unlock();
  1020. if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
  1021. pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
  1022. "returning a delegation for "
  1023. "OPEN(CLAIM_DELEGATE_CUR)\n",
  1024. clp->cl_hostname);
  1025. } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
  1026. nfs_inode_set_delegation(state->inode,
  1027. data->owner->so_cred,
  1028. &data->o_res);
  1029. else
  1030. nfs_inode_reclaim_delegation(state->inode,
  1031. data->owner->so_cred,
  1032. &data->o_res);
  1033. }
  1034. /*
  1035. * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
  1036. * and update the nfs4_state.
  1037. */
  1038. static struct nfs4_state *
  1039. _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
  1040. {
  1041. struct inode *inode = data->state->inode;
  1042. struct nfs4_state *state = data->state;
  1043. int ret;
  1044. if (!data->rpc_done) {
  1045. ret = data->rpc_status;
  1046. goto err;
  1047. }
  1048. ret = -ESTALE;
  1049. if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
  1050. !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
  1051. !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
  1052. goto err;
  1053. ret = -ENOMEM;
  1054. state = nfs4_get_open_state(inode, data->owner);
  1055. if (state == NULL)
  1056. goto err;
  1057. ret = nfs_refresh_inode(inode, &data->f_attr);
  1058. if (ret)
  1059. goto err;
  1060. if (data->o_res.delegation_type != 0)
  1061. nfs4_opendata_check_deleg(data, state);
  1062. update_open_stateid(state, &data->o_res.stateid, NULL,
  1063. data->o_arg.fmode);
  1064. return state;
  1065. err:
  1066. return ERR_PTR(ret);
  1067. }
  1068. static struct nfs4_state *
  1069. _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
  1070. {
  1071. struct inode *inode;
  1072. struct nfs4_state *state = NULL;
  1073. int ret;
  1074. if (!data->rpc_done) {
  1075. state = nfs4_try_open_cached(data);
  1076. goto out;
  1077. }
  1078. ret = -EAGAIN;
  1079. if (!(data->f_attr.valid & NFS_ATTR_FATTR))
  1080. goto err;
  1081. inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
  1082. ret = PTR_ERR(inode);
  1083. if (IS_ERR(inode))
  1084. goto err;
  1085. ret = -ENOMEM;
  1086. state = nfs4_get_open_state(inode, data->owner);
  1087. if (state == NULL)
  1088. goto err_put_inode;
  1089. if (data->o_res.delegation_type != 0)
  1090. nfs4_opendata_check_deleg(data, state);
  1091. update_open_stateid(state, &data->o_res.stateid, NULL,
  1092. data->o_arg.fmode);
  1093. iput(inode);
  1094. out:
  1095. nfs_release_seqid(data->o_arg.seqid);
  1096. return state;
  1097. err_put_inode:
  1098. iput(inode);
  1099. err:
  1100. return ERR_PTR(ret);
  1101. }
  1102. static struct nfs4_state *
  1103. nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
  1104. {
  1105. if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
  1106. return _nfs4_opendata_reclaim_to_nfs4_state(data);
  1107. return _nfs4_opendata_to_nfs4_state(data);
  1108. }
  1109. static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
  1110. {
  1111. struct nfs_inode *nfsi = NFS_I(state->inode);
  1112. struct nfs_open_context *ctx;
  1113. spin_lock(&state->inode->i_lock);
  1114. list_for_each_entry(ctx, &nfsi->open_files, list) {
  1115. if (ctx->state != state)
  1116. continue;
  1117. get_nfs_open_context(ctx);
  1118. spin_unlock(&state->inode->i_lock);
  1119. return ctx;
  1120. }
  1121. spin_unlock(&state->inode->i_lock);
  1122. return ERR_PTR(-ENOENT);
  1123. }
  1124. static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
  1125. struct nfs4_state *state, enum open_claim_type4 claim)
  1126. {
  1127. struct nfs4_opendata *opendata;
  1128. opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
  1129. NULL, claim, GFP_NOFS);
  1130. if (opendata == NULL)
  1131. return ERR_PTR(-ENOMEM);
  1132. opendata->state = state;
  1133. atomic_inc(&state->count);
  1134. return opendata;
  1135. }
  1136. static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
  1137. {
  1138. struct nfs4_state *newstate;
  1139. int ret;
  1140. opendata->o_arg.open_flags = 0;
  1141. opendata->o_arg.fmode = fmode;
  1142. memset(&opendata->o_res, 0, sizeof(opendata->o_res));
  1143. memset(&opendata->c_res, 0, sizeof(opendata->c_res));
  1144. nfs4_init_opendata_res(opendata);
  1145. ret = _nfs4_recover_proc_open(opendata);
  1146. if (ret != 0)
  1147. return ret;
  1148. newstate = nfs4_opendata_to_nfs4_state(opendata);
  1149. if (IS_ERR(newstate))
  1150. return PTR_ERR(newstate);
  1151. nfs4_close_state(newstate, fmode);
  1152. *res = newstate;
  1153. return 0;
  1154. }
  1155. static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
  1156. {
  1157. struct nfs4_state *newstate;
  1158. int ret;
  1159. /* memory barrier prior to reading state->n_* */
  1160. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  1161. smp_rmb();
  1162. if (state->n_rdwr != 0) {
  1163. clear_bit(NFS_O_RDWR_STATE, &state->flags);
  1164. ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
  1165. if (ret != 0)
  1166. return ret;
  1167. if (newstate != state)
  1168. return -ESTALE;
  1169. }
  1170. if (state->n_wronly != 0) {
  1171. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  1172. ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
  1173. if (ret != 0)
  1174. return ret;
  1175. if (newstate != state)
  1176. return -ESTALE;
  1177. }
  1178. if (state->n_rdonly != 0) {
  1179. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  1180. ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
  1181. if (ret != 0)
  1182. return ret;
  1183. if (newstate != state)
  1184. return -ESTALE;
  1185. }
  1186. /*
  1187. * We may have performed cached opens for all three recoveries.
  1188. * Check if we need to update the current stateid.
  1189. */
  1190. if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
  1191. !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
  1192. write_seqlock(&state->seqlock);
  1193. if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
  1194. nfs4_stateid_copy(&state->stateid, &state->open_stateid);
  1195. write_sequnlock(&state->seqlock);
  1196. }
  1197. return 0;
  1198. }
  1199. /*
  1200. * OPEN_RECLAIM:
  1201. * reclaim state on the server after a reboot.
  1202. */
  1203. static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
  1204. {
  1205. struct nfs_delegation *delegation;
  1206. struct nfs4_opendata *opendata;
  1207. fmode_t delegation_type = 0;
  1208. int status;
  1209. opendata = nfs4_open_recoverdata_alloc(ctx, state,
  1210. NFS4_OPEN_CLAIM_PREVIOUS);
  1211. if (IS_ERR(opendata))
  1212. return PTR_ERR(opendata);
  1213. rcu_read_lock();
  1214. delegation = rcu_dereference(NFS_I(state->inode)->delegation);
  1215. if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
  1216. delegation_type = delegation->type;
  1217. rcu_read_unlock();
  1218. opendata->o_arg.u.delegation_type = delegation_type;
  1219. status = nfs4_open_recover(opendata, state);
  1220. nfs4_opendata_put(opendata);
  1221. return status;
  1222. }
  1223. static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
  1224. {
  1225. struct nfs_server *server = NFS_SERVER(state->inode);
  1226. struct nfs4_exception exception = { };
  1227. int err;
  1228. do {
  1229. err = _nfs4_do_open_reclaim(ctx, state);
  1230. if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
  1231. continue;
  1232. if (err != -NFS4ERR_DELAY)
  1233. break;
  1234. nfs4_handle_exception(server, err, &exception);
  1235. } while (exception.retry);
  1236. return err;
  1237. }
  1238. static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
  1239. {
  1240. struct nfs_open_context *ctx;
  1241. int ret;
  1242. ctx = nfs4_state_find_open_context(state);
  1243. if (IS_ERR(ctx))
  1244. return -EAGAIN;
  1245. ret = nfs4_do_open_reclaim(ctx, state);
  1246. put_nfs_open_context(ctx);
  1247. return ret;
  1248. }
  1249. static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
  1250. {
  1251. struct nfs4_opendata *opendata;
  1252. int ret;
  1253. opendata = nfs4_open_recoverdata_alloc(ctx, state,
  1254. NFS4_OPEN_CLAIM_DELEG_CUR_FH);
  1255. if (IS_ERR(opendata))
  1256. return PTR_ERR(opendata);
  1257. nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
  1258. ret = nfs4_open_recover(opendata, state);
  1259. nfs4_opendata_put(opendata);
  1260. return ret;
  1261. }
  1262. int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
  1263. {
  1264. struct nfs4_exception exception = { };
  1265. struct nfs_server *server = NFS_SERVER(state->inode);
  1266. int err;
  1267. do {
  1268. err = _nfs4_open_delegation_recall(ctx, state, stateid);
  1269. switch (err) {
  1270. case 0:
  1271. case -ENOENT:
  1272. case -ESTALE:
  1273. goto out;
  1274. case -NFS4ERR_BADSESSION:
  1275. case -NFS4ERR_BADSLOT:
  1276. case -NFS4ERR_BAD_HIGH_SLOT:
  1277. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1278. case -NFS4ERR_DEADSESSION:
  1279. set_bit(NFS_DELEGATED_STATE, &state->flags);
  1280. nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
  1281. err = -EAGAIN;
  1282. goto out;
  1283. case -NFS4ERR_STALE_CLIENTID:
  1284. case -NFS4ERR_STALE_STATEID:
  1285. set_bit(NFS_DELEGATED_STATE, &state->flags);
  1286. case -NFS4ERR_EXPIRED:
  1287. /* Don't recall a delegation if it was lost */
  1288. nfs4_schedule_lease_recovery(server->nfs_client);
  1289. err = -EAGAIN;
  1290. goto out;
  1291. case -NFS4ERR_DELEG_REVOKED:
  1292. case -NFS4ERR_ADMIN_REVOKED:
  1293. case -NFS4ERR_BAD_STATEID:
  1294. nfs_inode_find_state_and_recover(state->inode,
  1295. stateid);
  1296. nfs4_schedule_stateid_recovery(server, state);
  1297. case -ENOMEM:
  1298. err = 0;
  1299. goto out;
  1300. }
  1301. set_bit(NFS_DELEGATED_STATE, &state->flags);
  1302. err = nfs4_handle_exception(server, err, &exception);
  1303. } while (exception.retry);
  1304. out:
  1305. return err;
  1306. }
  1307. static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
  1308. {
  1309. struct nfs4_opendata *data = calldata;
  1310. data->rpc_status = task->tk_status;
  1311. if (data->rpc_status == 0) {
  1312. nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
  1313. nfs_confirm_seqid(&data->owner->so_seqid, 0);
  1314. renew_lease(data->o_res.server, data->timestamp);
  1315. data->rpc_done = 1;
  1316. }
  1317. }
  1318. static void nfs4_open_confirm_release(void *calldata)
  1319. {
  1320. struct nfs4_opendata *data = calldata;
  1321. struct nfs4_state *state = NULL;
  1322. /* If this request hasn't been cancelled, do nothing */
  1323. if (data->cancelled == 0)
  1324. goto out_free;
  1325. /* In case of error, no cleanup! */
  1326. if (!data->rpc_done)
  1327. goto out_free;
  1328. state = nfs4_opendata_to_nfs4_state(data);
  1329. if (!IS_ERR(state))
  1330. nfs4_close_state(state, data->o_arg.fmode);
  1331. out_free:
  1332. nfs4_opendata_put(data);
  1333. }
  1334. static const struct rpc_call_ops nfs4_open_confirm_ops = {
  1335. .rpc_call_done = nfs4_open_confirm_done,
  1336. .rpc_release = nfs4_open_confirm_release,
  1337. };
  1338. /*
  1339. * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
  1340. */
  1341. static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
  1342. {
  1343. struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
  1344. struct rpc_task *task;
  1345. struct rpc_message msg = {
  1346. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
  1347. .rpc_argp = &data->c_arg,
  1348. .rpc_resp = &data->c_res,
  1349. .rpc_cred = data->owner->so_cred,
  1350. };
  1351. struct rpc_task_setup task_setup_data = {
  1352. .rpc_client = server->client,
  1353. .rpc_message = &msg,
  1354. .callback_ops = &nfs4_open_confirm_ops,
  1355. .callback_data = data,
  1356. .workqueue = nfsiod_workqueue,
  1357. .flags = RPC_TASK_ASYNC,
  1358. };
  1359. int status;
  1360. kref_get(&data->kref);
  1361. data->rpc_done = 0;
  1362. data->rpc_status = 0;
  1363. data->timestamp = jiffies;
  1364. task = rpc_run_task(&task_setup_data);
  1365. if (IS_ERR(task))
  1366. return PTR_ERR(task);
  1367. status = nfs4_wait_for_completion_rpc_task(task);
  1368. if (status != 0) {
  1369. data->cancelled = 1;
  1370. smp_wmb();
  1371. } else
  1372. status = data->rpc_status;
  1373. rpc_put_task(task);
  1374. return status;
  1375. }
  1376. static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
  1377. {
  1378. struct nfs4_opendata *data = calldata;
  1379. struct nfs4_state_owner *sp = data->owner;
  1380. if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
  1381. goto out_wait;
  1382. /*
  1383. * Check if we still need to send an OPEN call, or if we can use
  1384. * a delegation instead.
  1385. */
  1386. if (data->state != NULL) {
  1387. struct nfs_delegation *delegation;
  1388. if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
  1389. goto out_no_action;
  1390. rcu_read_lock();
  1391. delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
  1392. if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
  1393. can_open_delegated(delegation, data->o_arg.fmode))
  1394. goto unlock_no_action;
  1395. rcu_read_unlock();
  1396. }
  1397. /* Update client id. */
  1398. data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
  1399. if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
  1400. task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
  1401. data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
  1402. nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
  1403. }
  1404. data->timestamp = jiffies;
  1405. if (nfs4_setup_sequence(data->o_arg.server,
  1406. &data->o_arg.seq_args,
  1407. &data->o_res.seq_res,
  1408. task) != 0)
  1409. nfs_release_seqid(data->o_arg.seqid);
  1410. return;
  1411. unlock_no_action:
  1412. rcu_read_unlock();
  1413. out_no_action:
  1414. task->tk_action = NULL;
  1415. out_wait:
  1416. nfs4_sequence_done(task, &data->o_res.seq_res);
  1417. }
  1418. static void nfs4_open_done(struct rpc_task *task, void *calldata)
  1419. {
  1420. struct nfs4_opendata *data = calldata;
  1421. data->rpc_status = task->tk_status;
  1422. if (!nfs4_sequence_done(task, &data->o_res.seq_res))
  1423. return;
  1424. if (task->tk_status == 0) {
  1425. if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
  1426. switch (data->o_res.f_attr->mode & S_IFMT) {
  1427. case S_IFREG:
  1428. break;
  1429. case S_IFLNK:
  1430. data->rpc_status = -ELOOP;
  1431. break;
  1432. case S_IFDIR:
  1433. data->rpc_status = -EISDIR;
  1434. break;
  1435. default:
  1436. data->rpc_status = -ENOTDIR;
  1437. }
  1438. }
  1439. renew_lease(data->o_res.server, data->timestamp);
  1440. if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
  1441. nfs_confirm_seqid(&data->owner->so_seqid, 0);
  1442. }
  1443. data->rpc_done = 1;
  1444. }
  1445. static void nfs4_open_release(void *calldata)
  1446. {
  1447. struct nfs4_opendata *data = calldata;
  1448. struct nfs4_state *state = NULL;
  1449. /* If this request hasn't been cancelled, do nothing */
  1450. if (data->cancelled == 0)
  1451. goto out_free;
  1452. /* In case of error, no cleanup! */
  1453. if (data->rpc_status != 0 || !data->rpc_done)
  1454. goto out_free;
  1455. /* In case we need an open_confirm, no cleanup! */
  1456. if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
  1457. goto out_free;
  1458. state = nfs4_opendata_to_nfs4_state(data);
  1459. if (!IS_ERR(state))
  1460. nfs4_close_state(state, data->o_arg.fmode);
  1461. out_free:
  1462. nfs4_opendata_put(data);
  1463. }
  1464. static const struct rpc_call_ops nfs4_open_ops = {
  1465. .rpc_call_prepare = nfs4_open_prepare,
  1466. .rpc_call_done = nfs4_open_done,
  1467. .rpc_release = nfs4_open_release,
  1468. };
  1469. static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
  1470. {
  1471. struct inode *dir = data->dir->d_inode;
  1472. struct nfs_server *server = NFS_SERVER(dir);
  1473. struct nfs_openargs *o_arg = &data->o_arg;
  1474. struct nfs_openres *o_res = &data->o_res;
  1475. struct rpc_task *task;
  1476. struct rpc_message msg = {
  1477. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
  1478. .rpc_argp = o_arg,
  1479. .rpc_resp = o_res,
  1480. .rpc_cred = data->owner->so_cred,
  1481. };
  1482. struct rpc_task_setup task_setup_data = {
  1483. .rpc_client = server->client,
  1484. .rpc_message = &msg,
  1485. .callback_ops = &nfs4_open_ops,
  1486. .callback_data = data,
  1487. .workqueue = nfsiod_workqueue,
  1488. .flags = RPC_TASK_ASYNC,
  1489. };
  1490. int status;
  1491. nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
  1492. kref_get(&data->kref);
  1493. data->rpc_done = 0;
  1494. data->rpc_status = 0;
  1495. data->cancelled = 0;
  1496. if (isrecover)
  1497. nfs4_set_sequence_privileged(&o_arg->seq_args);
  1498. task = rpc_run_task(&task_setup_data);
  1499. if (IS_ERR(task))
  1500. return PTR_ERR(task);
  1501. status = nfs4_wait_for_completion_rpc_task(task);
  1502. if (status != 0) {
  1503. data->cancelled = 1;
  1504. smp_wmb();
  1505. } else
  1506. status = data->rpc_status;
  1507. rpc_put_task(task);
  1508. return status;
  1509. }
  1510. static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
  1511. {
  1512. struct inode *dir = data->dir->d_inode;
  1513. struct nfs_openres *o_res = &data->o_res;
  1514. int status;
  1515. status = nfs4_run_open_task(data, 1);
  1516. if (status != 0 || !data->rpc_done)
  1517. return status;
  1518. nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
  1519. if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
  1520. status = _nfs4_proc_open_confirm(data);
  1521. if (status != 0)
  1522. return status;
  1523. }
  1524. return status;
  1525. }
  1526. static int nfs4_opendata_access(struct rpc_cred *cred,
  1527. struct nfs4_opendata *opendata,
  1528. struct nfs4_state *state, fmode_t fmode,
  1529. int openflags)
  1530. {
  1531. struct nfs_access_entry cache;
  1532. u32 mask;
  1533. /* access call failed or for some reason the server doesn't
  1534. * support any access modes -- defer access call until later */
  1535. if (opendata->o_res.access_supported == 0)
  1536. return 0;
  1537. mask = 0;
  1538. /* don't check MAY_WRITE - a newly created file may not have
  1539. * write mode bits, but POSIX allows the creating process to write.
  1540. * use openflags to check for exec, because fmode won't
  1541. * always have FMODE_EXEC set when file open for exec. */
  1542. if (openflags & __FMODE_EXEC) {
  1543. /* ONLY check for exec rights */
  1544. mask = MAY_EXEC;
  1545. } else if (fmode & FMODE_READ)
  1546. mask = MAY_READ;
  1547. cache.cred = cred;
  1548. cache.jiffies = jiffies;
  1549. nfs_access_set_mask(&cache, opendata->o_res.access_result);
  1550. nfs_access_add_cache(state->inode, &cache);
  1551. if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
  1552. return 0;
  1553. /* even though OPEN succeeded, access is denied. Close the file */
  1554. nfs4_close_state(state, fmode);
  1555. return -EACCES;
  1556. }
  1557. /*
  1558. * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
  1559. */
  1560. static int _nfs4_proc_open(struct nfs4_opendata *data)
  1561. {
  1562. struct inode *dir = data->dir->d_inode;
  1563. struct nfs_server *server = NFS_SERVER(dir);
  1564. struct nfs_openargs *o_arg = &data->o_arg;
  1565. struct nfs_openres *o_res = &data->o_res;
  1566. int status;
  1567. status = nfs4_run_open_task(data, 0);
  1568. if (!data->rpc_done)
  1569. return status;
  1570. if (status != 0) {
  1571. if (status == -NFS4ERR_BADNAME &&
  1572. !(o_arg->open_flags & O_CREAT))
  1573. return -ENOENT;
  1574. return status;
  1575. }
  1576. nfs_fattr_map_and_free_names(server, &data->f_attr);
  1577. if (o_arg->open_flags & O_CREAT)
  1578. update_changeattr(dir, &o_res->cinfo);
  1579. if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
  1580. server->caps &= ~NFS_CAP_POSIX_LOCK;
  1581. if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
  1582. status = _nfs4_proc_open_confirm(data);
  1583. if (status != 0)
  1584. return status;
  1585. }
  1586. if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
  1587. _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
  1588. return 0;
  1589. }
  1590. static int nfs4_recover_expired_lease(struct nfs_server *server)
  1591. {
  1592. return nfs4_client_recover_expired_lease(server->nfs_client);
  1593. }
  1594. /*
  1595. * OPEN_EXPIRED:
  1596. * reclaim state on the server after a network partition.
  1597. * Assumes caller holds the appropriate lock
  1598. */
  1599. static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
  1600. {
  1601. struct nfs4_opendata *opendata;
  1602. int ret;
  1603. opendata = nfs4_open_recoverdata_alloc(ctx, state,
  1604. NFS4_OPEN_CLAIM_FH);
  1605. if (IS_ERR(opendata))
  1606. return PTR_ERR(opendata);
  1607. ret = nfs4_open_recover(opendata, state);
  1608. if (ret == -ESTALE)
  1609. d_drop(ctx->dentry);
  1610. nfs4_opendata_put(opendata);
  1611. return ret;
  1612. }
  1613. static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
  1614. {
  1615. struct nfs_server *server = NFS_SERVER(state->inode);
  1616. struct nfs4_exception exception = { };
  1617. int err;
  1618. do {
  1619. err = _nfs4_open_expired(ctx, state);
  1620. if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
  1621. continue;
  1622. switch (err) {
  1623. default:
  1624. goto out;
  1625. case -NFS4ERR_GRACE:
  1626. case -NFS4ERR_DELAY:
  1627. nfs4_handle_exception(server, err, &exception);
  1628. err = 0;
  1629. }
  1630. } while (exception.retry);
  1631. out:
  1632. return err;
  1633. }
  1634. static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
  1635. {
  1636. struct nfs_open_context *ctx;
  1637. int ret;
  1638. ctx = nfs4_state_find_open_context(state);
  1639. if (IS_ERR(ctx))
  1640. return -EAGAIN;
  1641. ret = nfs4_do_open_expired(ctx, state);
  1642. put_nfs_open_context(ctx);
  1643. return ret;
  1644. }
  1645. #if defined(CONFIG_NFS_V4_1)
  1646. static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
  1647. {
  1648. struct nfs_server *server = NFS_SERVER(state->inode);
  1649. nfs4_stateid *stateid = &state->stateid;
  1650. int status;
  1651. /* If a state reset has been done, test_stateid is unneeded */
  1652. if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
  1653. return;
  1654. status = nfs41_test_stateid(server, stateid);
  1655. if (status != NFS_OK) {
  1656. /* Free the stateid unless the server explicitly
  1657. * informs us the stateid is unrecognized. */
  1658. if (status != -NFS4ERR_BAD_STATEID)
  1659. nfs41_free_stateid(server, stateid);
  1660. nfs_remove_bad_delegation(state->inode);
  1661. write_seqlock(&state->seqlock);
  1662. nfs4_stateid_copy(&state->stateid, &state->open_stateid);
  1663. write_sequnlock(&state->seqlock);
  1664. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  1665. }
  1666. }
  1667. /**
  1668. * nfs41_check_open_stateid - possibly free an open stateid
  1669. *
  1670. * @state: NFSv4 state for an inode
  1671. *
  1672. * Returns NFS_OK if recovery for this stateid is now finished.
  1673. * Otherwise a negative NFS4ERR value is returned.
  1674. */
  1675. static int nfs41_check_open_stateid(struct nfs4_state *state)
  1676. {
  1677. struct nfs_server *server = NFS_SERVER(state->inode);
  1678. nfs4_stateid *stateid = &state->open_stateid;
  1679. int status;
  1680. /* If a state reset has been done, test_stateid is unneeded */
  1681. if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
  1682. (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
  1683. (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
  1684. return -NFS4ERR_BAD_STATEID;
  1685. status = nfs41_test_stateid(server, stateid);
  1686. if (status != NFS_OK) {
  1687. /* Free the stateid unless the server explicitly
  1688. * informs us the stateid is unrecognized. */
  1689. if (status != -NFS4ERR_BAD_STATEID)
  1690. nfs41_free_stateid(server, stateid);
  1691. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  1692. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  1693. clear_bit(NFS_O_RDWR_STATE, &state->flags);
  1694. }
  1695. return status;
  1696. }
  1697. static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
  1698. {
  1699. int status;
  1700. nfs41_clear_delegation_stateid(state);
  1701. status = nfs41_check_open_stateid(state);
  1702. if (status != NFS_OK)
  1703. status = nfs4_open_expired(sp, state);
  1704. return status;
  1705. }
  1706. #endif
  1707. /*
  1708. * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
  1709. * fields corresponding to attributes that were used to store the verifier.
  1710. * Make sure we clobber those fields in the later setattr call
  1711. */
  1712. static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
  1713. {
  1714. if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
  1715. !(sattr->ia_valid & ATTR_ATIME_SET))
  1716. sattr->ia_valid |= ATTR_ATIME;
  1717. if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
  1718. !(sattr->ia_valid & ATTR_MTIME_SET))
  1719. sattr->ia_valid |= ATTR_MTIME;
  1720. }
  1721. static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
  1722. fmode_t fmode,
  1723. int flags,
  1724. struct nfs4_state **res)
  1725. {
  1726. struct nfs4_state_owner *sp = opendata->owner;
  1727. struct nfs_server *server = sp->so_server;
  1728. struct nfs4_state *state;
  1729. unsigned int seq;
  1730. int ret;
  1731. seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
  1732. ret = _nfs4_proc_open(opendata);
  1733. if (ret != 0)
  1734. goto out;
  1735. state = nfs4_opendata_to_nfs4_state(opendata);
  1736. ret = PTR_ERR(state);
  1737. if (IS_ERR(state))
  1738. goto out;
  1739. if (server->caps & NFS_CAP_POSIX_LOCK)
  1740. set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
  1741. ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
  1742. if (ret != 0)
  1743. goto out;
  1744. if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
  1745. nfs4_schedule_stateid_recovery(server, state);
  1746. *res = state;
  1747. out:
  1748. return ret;
  1749. }
  1750. /*
  1751. * Returns a referenced nfs4_state
  1752. */
  1753. static int _nfs4_do_open(struct inode *dir,
  1754. struct dentry *dentry,
  1755. fmode_t fmode,
  1756. int flags,
  1757. struct iattr *sattr,
  1758. struct rpc_cred *cred,
  1759. struct nfs4_state **res,
  1760. struct nfs4_threshold **ctx_th)
  1761. {
  1762. struct nfs4_state_owner *sp;
  1763. struct nfs4_state *state = NULL;
  1764. struct nfs_server *server = NFS_SERVER(dir);
  1765. struct nfs4_opendata *opendata;
  1766. enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
  1767. int status;
  1768. /* Protect against reboot recovery conflicts */
  1769. status = -ENOMEM;
  1770. sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
  1771. if (sp == NULL) {
  1772. dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
  1773. goto out_err;
  1774. }
  1775. status = nfs4_recover_expired_lease(server);
  1776. if (status != 0)
  1777. goto err_put_state_owner;
  1778. if (dentry->d_inode != NULL)
  1779. nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
  1780. status = -ENOMEM;
  1781. if (dentry->d_inode)
  1782. claim = NFS4_OPEN_CLAIM_FH;
  1783. opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
  1784. claim, GFP_KERNEL);
  1785. if (opendata == NULL)
  1786. goto err_put_state_owner;
  1787. if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
  1788. opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
  1789. if (!opendata->f_attr.mdsthreshold)
  1790. goto err_opendata_put;
  1791. opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
  1792. }
  1793. if (dentry->d_inode != NULL)
  1794. opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
  1795. status = _nfs4_open_and_get_state(opendata, fmode, flags, &state);
  1796. if (status != 0)
  1797. goto err_opendata_put;
  1798. if (opendata->o_arg.open_flags & O_EXCL) {
  1799. nfs4_exclusive_attrset(opendata, sattr);
  1800. nfs_fattr_init(opendata->o_res.f_attr);
  1801. status = nfs4_do_setattr(state->inode, cred,
  1802. opendata->o_res.f_attr, sattr,
  1803. state);
  1804. if (status == 0)
  1805. nfs_setattr_update_inode(state->inode, sattr);
  1806. nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
  1807. }
  1808. if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
  1809. *ctx_th = opendata->f_attr.mdsthreshold;
  1810. else
  1811. kfree(opendata->f_attr.mdsthreshold);
  1812. opendata->f_attr.mdsthreshold = NULL;
  1813. nfs4_opendata_put(opendata);
  1814. nfs4_put_state_owner(sp);
  1815. *res = state;
  1816. return 0;
  1817. err_opendata_put:
  1818. kfree(opendata->f_attr.mdsthreshold);
  1819. nfs4_opendata_put(opendata);
  1820. err_put_state_owner:
  1821. nfs4_put_state_owner(sp);
  1822. out_err:
  1823. *res = NULL;
  1824. return status;
  1825. }
  1826. static struct nfs4_state *nfs4_do_open(struct inode *dir,
  1827. struct dentry *dentry,
  1828. fmode_t fmode,
  1829. int flags,
  1830. struct iattr *sattr,
  1831. struct rpc_cred *cred,
  1832. struct nfs4_threshold **ctx_th)
  1833. {
  1834. struct nfs_server *server = NFS_SERVER(dir);
  1835. struct nfs4_exception exception = { };
  1836. struct nfs4_state *res;
  1837. int status;
  1838. fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
  1839. do {
  1840. status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
  1841. &res, ctx_th);
  1842. if (status == 0)
  1843. break;
  1844. /* NOTE: BAD_SEQID means the server and client disagree about the
  1845. * book-keeping w.r.t. state-changing operations
  1846. * (OPEN/CLOSE/LOCK/LOCKU...)
  1847. * It is actually a sign of a bug on the client or on the server.
  1848. *
  1849. * If we receive a BAD_SEQID error in the particular case of
  1850. * doing an OPEN, we assume that nfs_increment_open_seqid() will
  1851. * have unhashed the old state_owner for us, and that we can
  1852. * therefore safely retry using a new one. We should still warn
  1853. * the user though...
  1854. */
  1855. if (status == -NFS4ERR_BAD_SEQID) {
  1856. pr_warn_ratelimited("NFS: v4 server %s "
  1857. " returned a bad sequence-id error!\n",
  1858. NFS_SERVER(dir)->nfs_client->cl_hostname);
  1859. exception.retry = 1;
  1860. continue;
  1861. }
  1862. /*
  1863. * BAD_STATEID on OPEN means that the server cancelled our
  1864. * state before it received the OPEN_CONFIRM.
  1865. * Recover by retrying the request as per the discussion
  1866. * on Page 181 of RFC3530.
  1867. */
  1868. if (status == -NFS4ERR_BAD_STATEID) {
  1869. exception.retry = 1;
  1870. continue;
  1871. }
  1872. if (status == -EAGAIN) {
  1873. /* We must have found a delegation */
  1874. exception.retry = 1;
  1875. continue;
  1876. }
  1877. if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
  1878. continue;
  1879. res = ERR_PTR(nfs4_handle_exception(server,
  1880. status, &exception));
  1881. } while (exception.retry);
  1882. return res;
  1883. }
  1884. static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
  1885. struct nfs_fattr *fattr, struct iattr *sattr,
  1886. struct nfs4_state *state)
  1887. {
  1888. struct nfs_server *server = NFS_SERVER(inode);
  1889. struct nfs_setattrargs arg = {
  1890. .fh = NFS_FH(inode),
  1891. .iap = sattr,
  1892. .server = server,
  1893. .bitmask = server->attr_bitmask,
  1894. };
  1895. struct nfs_setattrres res = {
  1896. .fattr = fattr,
  1897. .server = server,
  1898. };
  1899. struct rpc_message msg = {
  1900. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
  1901. .rpc_argp = &arg,
  1902. .rpc_resp = &res,
  1903. .rpc_cred = cred,
  1904. };
  1905. unsigned long timestamp = jiffies;
  1906. int status;
  1907. nfs_fattr_init(fattr);
  1908. if (state != NULL && nfs4_valid_open_stateid(state)) {
  1909. struct nfs_lockowner lockowner = {
  1910. .l_owner = current->files,
  1911. .l_pid = current->tgid,
  1912. };
  1913. nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
  1914. &lockowner);
  1915. } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
  1916. FMODE_WRITE)) {
  1917. /* Use that stateid */
  1918. } else
  1919. nfs4_stateid_copy(&arg.stateid, &zero_stateid);
  1920. status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
  1921. if (status == 0 && state != NULL)
  1922. renew_lease(server, timestamp);
  1923. return status;
  1924. }
  1925. static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
  1926. struct nfs_fattr *fattr, struct iattr *sattr,
  1927. struct nfs4_state *state)
  1928. {
  1929. struct nfs_server *server = NFS_SERVER(inode);
  1930. struct nfs4_exception exception = {
  1931. .state = state,
  1932. .inode = inode,
  1933. };
  1934. int err;
  1935. do {
  1936. err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
  1937. switch (err) {
  1938. case -NFS4ERR_OPENMODE:
  1939. if (state && !(state->state & FMODE_WRITE)) {
  1940. err = -EBADF;
  1941. if (sattr->ia_valid & ATTR_OPEN)
  1942. err = -EACCES;
  1943. goto out;
  1944. }
  1945. }
  1946. err = nfs4_handle_exception(server, err, &exception);
  1947. } while (exception.retry);
  1948. out:
  1949. return err;
  1950. }
  1951. struct nfs4_closedata {
  1952. struct inode *inode;
  1953. struct nfs4_state *state;
  1954. struct nfs_closeargs arg;
  1955. struct nfs_closeres res;
  1956. struct nfs_fattr fattr;
  1957. unsigned long timestamp;
  1958. bool roc;
  1959. u32 roc_barrier;
  1960. };
  1961. static void nfs4_free_closedata(void *data)
  1962. {
  1963. struct nfs4_closedata *calldata = data;
  1964. struct nfs4_state_owner *sp = calldata->state->owner;
  1965. struct super_block *sb = calldata->state->inode->i_sb;
  1966. if (calldata->roc)
  1967. pnfs_roc_release(calldata->state->inode);
  1968. nfs4_put_open_state(calldata->state);
  1969. nfs_free_seqid(calldata->arg.seqid);
  1970. nfs4_put_state_owner(sp);
  1971. nfs_sb_deactive(sb);
  1972. kfree(calldata);
  1973. }
  1974. static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
  1975. fmode_t fmode)
  1976. {
  1977. spin_lock(&state->owner->so_lock);
  1978. if (!(fmode & FMODE_READ))
  1979. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  1980. if (!(fmode & FMODE_WRITE))
  1981. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  1982. clear_bit(NFS_O_RDWR_STATE, &state->flags);
  1983. spin_unlock(&state->owner->so_lock);
  1984. }
  1985. static void nfs4_close_done(struct rpc_task *task, void *data)
  1986. {
  1987. struct nfs4_closedata *calldata = data;
  1988. struct nfs4_state *state = calldata->state;
  1989. struct nfs_server *server = NFS_SERVER(calldata->inode);
  1990. dprintk("%s: begin!\n", __func__);
  1991. if (!nfs4_sequence_done(task, &calldata->res.seq_res))
  1992. return;
  1993. /* hmm. we are done with the inode, and in the process of freeing
  1994. * the state_owner. we keep this around to process errors
  1995. */
  1996. switch (task->tk_status) {
  1997. case 0:
  1998. if (calldata->roc)
  1999. pnfs_roc_set_barrier(state->inode,
  2000. calldata->roc_barrier);
  2001. nfs_set_open_stateid(state, &calldata->res.stateid, 0);
  2002. renew_lease(server, calldata->timestamp);
  2003. nfs4_close_clear_stateid_flags(state,
  2004. calldata->arg.fmode);
  2005. break;
  2006. case -NFS4ERR_STALE_STATEID:
  2007. case -NFS4ERR_OLD_STATEID:
  2008. case -NFS4ERR_BAD_STATEID:
  2009. case -NFS4ERR_EXPIRED:
  2010. if (calldata->arg.fmode == 0)
  2011. break;
  2012. default:
  2013. if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
  2014. rpc_restart_call_prepare(task);
  2015. }
  2016. nfs_release_seqid(calldata->arg.seqid);
  2017. nfs_refresh_inode(calldata->inode, calldata->res.fattr);
  2018. dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
  2019. }
  2020. static void nfs4_close_prepare(struct rpc_task *task, void *data)
  2021. {
  2022. struct nfs4_closedata *calldata = data;
  2023. struct nfs4_state *state = calldata->state;
  2024. struct inode *inode = calldata->inode;
  2025. int call_close = 0;
  2026. dprintk("%s: begin!\n", __func__);
  2027. if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
  2028. goto out_wait;
  2029. task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
  2030. calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
  2031. spin_lock(&state->owner->so_lock);
  2032. /* Calculate the change in open mode */
  2033. if (state->n_rdwr == 0) {
  2034. if (state->n_rdonly == 0) {
  2035. call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
  2036. call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
  2037. calldata->arg.fmode &= ~FMODE_READ;
  2038. }
  2039. if (state->n_wronly == 0) {
  2040. call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
  2041. call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
  2042. calldata->arg.fmode &= ~FMODE_WRITE;
  2043. }
  2044. }
  2045. if (!nfs4_valid_open_stateid(state))
  2046. call_close = 0;
  2047. spin_unlock(&state->owner->so_lock);
  2048. if (!call_close) {
  2049. /* Note: exit _without_ calling nfs4_close_done */
  2050. goto out_no_action;
  2051. }
  2052. if (calldata->arg.fmode == 0) {
  2053. task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
  2054. if (calldata->roc &&
  2055. pnfs_roc_drain(inode, &calldata->roc_barrier, task))
  2056. goto out_wait;
  2057. }
  2058. nfs_fattr_init(calldata->res.fattr);
  2059. calldata->timestamp = jiffies;
  2060. if (nfs4_setup_sequence(NFS_SERVER(inode),
  2061. &calldata->arg.seq_args,
  2062. &calldata->res.seq_res,
  2063. task) != 0)
  2064. nfs_release_seqid(calldata->arg.seqid);
  2065. dprintk("%s: done!\n", __func__);
  2066. return;
  2067. out_no_action:
  2068. task->tk_action = NULL;
  2069. out_wait:
  2070. nfs4_sequence_done(task, &calldata->res.seq_res);
  2071. }
  2072. static const struct rpc_call_ops nfs4_close_ops = {
  2073. .rpc_call_prepare = nfs4_close_prepare,
  2074. .rpc_call_done = nfs4_close_done,
  2075. .rpc_release = nfs4_free_closedata,
  2076. };
  2077. /*
  2078. * It is possible for data to be read/written from a mem-mapped file
  2079. * after the sys_close call (which hits the vfs layer as a flush).
  2080. * This means that we can't safely call nfsv4 close on a file until
  2081. * the inode is cleared. This in turn means that we are not good
  2082. * NFSv4 citizens - we do not indicate to the server to update the file's
  2083. * share state even when we are done with one of the three share
  2084. * stateid's in the inode.
  2085. *
  2086. * NOTE: Caller must be holding the sp->so_owner semaphore!
  2087. */
  2088. int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
  2089. {
  2090. struct nfs_server *server = NFS_SERVER(state->inode);
  2091. struct nfs4_closedata *calldata;
  2092. struct nfs4_state_owner *sp = state->owner;
  2093. struct rpc_task *task;
  2094. struct rpc_message msg = {
  2095. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
  2096. .rpc_cred = state->owner->so_cred,
  2097. };
  2098. struct rpc_task_setup task_setup_data = {
  2099. .rpc_client = server->client,
  2100. .rpc_message = &msg,
  2101. .callback_ops = &nfs4_close_ops,
  2102. .workqueue = nfsiod_workqueue,
  2103. .flags = RPC_TASK_ASYNC,
  2104. };
  2105. int status = -ENOMEM;
  2106. calldata = kzalloc(sizeof(*calldata), gfp_mask);
  2107. if (calldata == NULL)
  2108. goto out;
  2109. nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
  2110. calldata->inode = state->inode;
  2111. calldata->state = state;
  2112. calldata->arg.fh = NFS_FH(state->inode);
  2113. calldata->arg.stateid = &state->open_stateid;
  2114. /* Serialization for the sequence id */
  2115. calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
  2116. if (calldata->arg.seqid == NULL)
  2117. goto out_free_calldata;
  2118. calldata->arg.fmode = 0;
  2119. calldata->arg.bitmask = server->cache_consistency_bitmask;
  2120. calldata->res.fattr = &calldata->fattr;
  2121. calldata->res.seqid = calldata->arg.seqid;
  2122. calldata->res.server = server;
  2123. calldata->roc = pnfs_roc(state->inode);
  2124. nfs_sb_active(calldata->inode->i_sb);
  2125. msg.rpc_argp = &calldata->arg;
  2126. msg.rpc_resp = &calldata->res;
  2127. task_setup_data.callback_data = calldata;
  2128. task = rpc_run_task(&task_setup_data);
  2129. if (IS_ERR(task))
  2130. return PTR_ERR(task);
  2131. status = 0;
  2132. if (wait)
  2133. status = rpc_wait_for_completion_task(task);
  2134. rpc_put_task(task);
  2135. return status;
  2136. out_free_calldata:
  2137. kfree(calldata);
  2138. out:
  2139. nfs4_put_open_state(state);
  2140. nfs4_put_state_owner(sp);
  2141. return status;
  2142. }
  2143. static struct inode *
  2144. nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
  2145. {
  2146. struct nfs4_state *state;
  2147. /* Protect against concurrent sillydeletes */
  2148. state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
  2149. ctx->cred, &ctx->mdsthreshold);
  2150. if (IS_ERR(state))
  2151. return ERR_CAST(state);
  2152. ctx->state = state;
  2153. return igrab(state->inode);
  2154. }
  2155. static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
  2156. {
  2157. if (ctx->state == NULL)
  2158. return;
  2159. if (is_sync)
  2160. nfs4_close_sync(ctx->state, ctx->mode);
  2161. else
  2162. nfs4_close_state(ctx->state, ctx->mode);
  2163. }
  2164. static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
  2165. {
  2166. struct nfs4_server_caps_arg args = {
  2167. .fhandle = fhandle,
  2168. };
  2169. struct nfs4_server_caps_res res = {};
  2170. struct rpc_message msg = {
  2171. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
  2172. .rpc_argp = &args,
  2173. .rpc_resp = &res,
  2174. };
  2175. int status;
  2176. status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  2177. if (status == 0) {
  2178. memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
  2179. server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
  2180. NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
  2181. NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
  2182. NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
  2183. NFS_CAP_CTIME|NFS_CAP_MTIME);
  2184. if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
  2185. server->caps |= NFS_CAP_ACLS;
  2186. if (res.has_links != 0)
  2187. server->caps |= NFS_CAP_HARDLINKS;
  2188. if (res.has_symlinks != 0)
  2189. server->caps |= NFS_CAP_SYMLINKS;
  2190. if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
  2191. server->caps |= NFS_CAP_FILEID;
  2192. if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
  2193. server->caps |= NFS_CAP_MODE;
  2194. if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
  2195. server->caps |= NFS_CAP_NLINK;
  2196. if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
  2197. server->caps |= NFS_CAP_OWNER;
  2198. if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
  2199. server->caps |= NFS_CAP_OWNER_GROUP;
  2200. if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
  2201. server->caps |= NFS_CAP_ATIME;
  2202. if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
  2203. server->caps |= NFS_CAP_CTIME;
  2204. if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
  2205. server->caps |= NFS_CAP_MTIME;
  2206. memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
  2207. server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
  2208. server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
  2209. server->acl_bitmask = res.acl_bitmask;
  2210. server->fh_expire_type = res.fh_expire_type;
  2211. }
  2212. return status;
  2213. }
  2214. int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
  2215. {
  2216. struct nfs4_exception exception = { };
  2217. int err;
  2218. do {
  2219. err = nfs4_handle_exception(server,
  2220. _nfs4_server_capabilities(server, fhandle),
  2221. &exception);
  2222. } while (exception.retry);
  2223. return err;
  2224. }
  2225. static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
  2226. struct nfs_fsinfo *info)
  2227. {
  2228. struct nfs4_lookup_root_arg args = {
  2229. .bitmask = nfs4_fattr_bitmap,
  2230. };
  2231. struct nfs4_lookup_res res = {
  2232. .server = server,
  2233. .fattr = info->fattr,
  2234. .fh = fhandle,
  2235. };
  2236. struct rpc_message msg = {
  2237. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
  2238. .rpc_argp = &args,
  2239. .rpc_resp = &res,
  2240. };
  2241. nfs_fattr_init(info->fattr);
  2242. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  2243. }
  2244. static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
  2245. struct nfs_fsinfo *info)
  2246. {
  2247. struct nfs4_exception exception = { };
  2248. int err;
  2249. do {
  2250. err = _nfs4_lookup_root(server, fhandle, info);
  2251. switch (err) {
  2252. case 0:
  2253. case -NFS4ERR_WRONGSEC:
  2254. goto out;
  2255. default:
  2256. err = nfs4_handle_exception(server, err, &exception);
  2257. }
  2258. } while (exception.retry);
  2259. out:
  2260. return err;
  2261. }
  2262. static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
  2263. struct nfs_fsinfo *info, rpc_authflavor_t flavor)
  2264. {
  2265. struct rpc_auth *auth;
  2266. int ret;
  2267. auth = rpcauth_create(flavor, server->client);
  2268. if (IS_ERR(auth)) {
  2269. ret = -EIO;
  2270. goto out;
  2271. }
  2272. ret = nfs4_lookup_root(server, fhandle, info);
  2273. out:
  2274. return ret;
  2275. }
  2276. static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
  2277. struct nfs_fsinfo *info)
  2278. {
  2279. int i, len, status = 0;
  2280. rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
  2281. len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
  2282. if (len < 0)
  2283. return len;
  2284. for (i = 0; i < len; i++) {
  2285. /* AUTH_UNIX is the default flavor if none was specified,
  2286. * thus has already been tried. */
  2287. if (flav_array[i] == RPC_AUTH_UNIX)
  2288. continue;
  2289. status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
  2290. if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
  2291. continue;
  2292. break;
  2293. }
  2294. /*
  2295. * -EACCESS could mean that the user doesn't have correct permissions
  2296. * to access the mount. It could also mean that we tried to mount
  2297. * with a gss auth flavor, but rpc.gssd isn't running. Either way,
  2298. * existing mount programs don't handle -EACCES very well so it should
  2299. * be mapped to -EPERM instead.
  2300. */
  2301. if (status == -EACCES)
  2302. status = -EPERM;
  2303. return status;
  2304. }
  2305. /*
  2306. * get the file handle for the "/" directory on the server
  2307. */
  2308. int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
  2309. struct nfs_fsinfo *info)
  2310. {
  2311. int minor_version = server->nfs_client->cl_minorversion;
  2312. int status = nfs4_lookup_root(server, fhandle, info);
  2313. if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
  2314. /*
  2315. * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
  2316. * by nfs4_map_errors() as this function exits.
  2317. */
  2318. status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
  2319. if (status == 0)
  2320. status = nfs4_server_capabilities(server, fhandle);
  2321. if (status == 0)
  2322. status = nfs4_do_fsinfo(server, fhandle, info);
  2323. return nfs4_map_errors(status);
  2324. }
  2325. static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
  2326. struct nfs_fsinfo *info)
  2327. {
  2328. int error;
  2329. struct nfs_fattr *fattr = info->fattr;
  2330. error = nfs4_server_capabilities(server, mntfh);
  2331. if (error < 0) {
  2332. dprintk("nfs4_get_root: getcaps error = %d\n", -error);
  2333. return error;
  2334. }
  2335. error = nfs4_proc_getattr(server, mntfh, fattr);
  2336. if (error < 0) {
  2337. dprintk("nfs4_get_root: getattr error = %d\n", -error);
  2338. return error;
  2339. }
  2340. if (fattr->valid & NFS_ATTR_FATTR_FSID &&
  2341. !nfs_fsid_equal(&server->fsid, &fattr->fsid))
  2342. memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
  2343. return error;
  2344. }
  2345. /*
  2346. * Get locations and (maybe) other attributes of a referral.
  2347. * Note that we'll actually follow the referral later when
  2348. * we detect fsid mismatch in inode revalidation
  2349. */
  2350. static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
  2351. const struct qstr *name, struct nfs_fattr *fattr,
  2352. struct nfs_fh *fhandle)
  2353. {
  2354. int status = -ENOMEM;
  2355. struct page *page = NULL;
  2356. struct nfs4_fs_locations *locations = NULL;
  2357. page = alloc_page(GFP_KERNEL);
  2358. if (page == NULL)
  2359. goto out;
  2360. locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
  2361. if (locations == NULL)
  2362. goto out;
  2363. status = nfs4_proc_fs_locations(client, dir, name, locations, page);
  2364. if (status != 0)
  2365. goto out;
  2366. /* Make sure server returned a different fsid for the referral */
  2367. if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
  2368. dprintk("%s: server did not return a different fsid for"
  2369. " a referral at %s\n", __func__, name->name);
  2370. status = -EIO;
  2371. goto out;
  2372. }
  2373. /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
  2374. nfs_fixup_referral_attributes(&locations->fattr);
  2375. /* replace the lookup nfs_fattr with the locations nfs_fattr */
  2376. memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
  2377. memset(fhandle, 0, sizeof(struct nfs_fh));
  2378. out:
  2379. if (page)
  2380. __free_page(page);
  2381. kfree(locations);
  2382. return status;
  2383. }
  2384. static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  2385. {
  2386. struct nfs4_getattr_arg args = {
  2387. .fh = fhandle,
  2388. .bitmask = server->attr_bitmask,
  2389. };
  2390. struct nfs4_getattr_res res = {
  2391. .fattr = fattr,
  2392. .server = server,
  2393. };
  2394. struct rpc_message msg = {
  2395. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
  2396. .rpc_argp = &args,
  2397. .rpc_resp = &res,
  2398. };
  2399. nfs_fattr_init(fattr);
  2400. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  2401. }
  2402. static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  2403. {
  2404. struct nfs4_exception exception = { };
  2405. int err;
  2406. do {
  2407. err = nfs4_handle_exception(server,
  2408. _nfs4_proc_getattr(server, fhandle, fattr),
  2409. &exception);
  2410. } while (exception.retry);
  2411. return err;
  2412. }
  2413. /*
  2414. * The file is not closed if it is opened due to the a request to change
  2415. * the size of the file. The open call will not be needed once the
  2416. * VFS layer lookup-intents are implemented.
  2417. *
  2418. * Close is called when the inode is destroyed.
  2419. * If we haven't opened the file for O_WRONLY, we
  2420. * need to in the size_change case to obtain a stateid.
  2421. *
  2422. * Got race?
  2423. * Because OPEN is always done by name in nfsv4, it is
  2424. * possible that we opened a different file by the same
  2425. * name. We can recognize this race condition, but we
  2426. * can't do anything about it besides returning an error.
  2427. *
  2428. * This will be fixed with VFS changes (lookup-intent).
  2429. */
  2430. static int
  2431. nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
  2432. struct iattr *sattr)
  2433. {
  2434. struct inode *inode = dentry->d_inode;
  2435. struct rpc_cred *cred = NULL;
  2436. struct nfs4_state *state = NULL;
  2437. int status;
  2438. if (pnfs_ld_layoutret_on_setattr(inode))
  2439. pnfs_commit_and_return_layout(inode);
  2440. nfs_fattr_init(fattr);
  2441. /* Deal with open(O_TRUNC) */
  2442. if (sattr->ia_valid & ATTR_OPEN)
  2443. sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
  2444. /* Optimization: if the end result is no change, don't RPC */
  2445. if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
  2446. return 0;
  2447. /* Search for an existing open(O_WRITE) file */
  2448. if (sattr->ia_valid & ATTR_FILE) {
  2449. struct nfs_open_context *ctx;
  2450. ctx = nfs_file_open_context(sattr->ia_file);
  2451. if (ctx) {
  2452. cred = ctx->cred;
  2453. state = ctx->state;
  2454. }
  2455. }
  2456. status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
  2457. if (status == 0)
  2458. nfs_setattr_update_inode(inode, sattr);
  2459. return status;
  2460. }
  2461. static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
  2462. const struct qstr *name, struct nfs_fh *fhandle,
  2463. struct nfs_fattr *fattr)
  2464. {
  2465. struct nfs_server *server = NFS_SERVER(dir);
  2466. int status;
  2467. struct nfs4_lookup_arg args = {
  2468. .bitmask = server->attr_bitmask,
  2469. .dir_fh = NFS_FH(dir),
  2470. .name = name,
  2471. };
  2472. struct nfs4_lookup_res res = {
  2473. .server = server,
  2474. .fattr = fattr,
  2475. .fh = fhandle,
  2476. };
  2477. struct rpc_message msg = {
  2478. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
  2479. .rpc_argp = &args,
  2480. .rpc_resp = &res,
  2481. };
  2482. nfs_fattr_init(fattr);
  2483. dprintk("NFS call lookup %s\n", name->name);
  2484. status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
  2485. dprintk("NFS reply lookup: %d\n", status);
  2486. return status;
  2487. }
  2488. static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
  2489. {
  2490. fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
  2491. NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
  2492. fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
  2493. fattr->nlink = 2;
  2494. }
  2495. static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
  2496. struct qstr *name, struct nfs_fh *fhandle,
  2497. struct nfs_fattr *fattr)
  2498. {
  2499. struct nfs4_exception exception = { };
  2500. struct rpc_clnt *client = *clnt;
  2501. int err;
  2502. do {
  2503. err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
  2504. switch (err) {
  2505. case -NFS4ERR_BADNAME:
  2506. err = -ENOENT;
  2507. goto out;
  2508. case -NFS4ERR_MOVED:
  2509. err = nfs4_get_referral(client, dir, name, fattr, fhandle);
  2510. goto out;
  2511. case -NFS4ERR_WRONGSEC:
  2512. err = -EPERM;
  2513. if (client != *clnt)
  2514. goto out;
  2515. client = nfs4_create_sec_client(client, dir, name);
  2516. if (IS_ERR(client))
  2517. return PTR_ERR(client);
  2518. exception.retry = 1;
  2519. break;
  2520. default:
  2521. err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
  2522. }
  2523. } while (exception.retry);
  2524. out:
  2525. if (err == 0)
  2526. *clnt = client;
  2527. else if (client != *clnt)
  2528. rpc_shutdown_client(client);
  2529. return err;
  2530. }
  2531. static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
  2532. struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  2533. {
  2534. int status;
  2535. struct rpc_clnt *client = NFS_CLIENT(dir);
  2536. status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
  2537. if (client != NFS_CLIENT(dir)) {
  2538. rpc_shutdown_client(client);
  2539. nfs_fixup_secinfo_attributes(fattr);
  2540. }
  2541. return status;
  2542. }
  2543. struct rpc_clnt *
  2544. nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
  2545. struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  2546. {
  2547. int status;
  2548. struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
  2549. status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
  2550. if (status < 0) {
  2551. rpc_shutdown_client(client);
  2552. return ERR_PTR(status);
  2553. }
  2554. return client;
  2555. }
  2556. static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
  2557. {
  2558. struct nfs_server *server = NFS_SERVER(inode);
  2559. struct nfs4_accessargs args = {
  2560. .fh = NFS_FH(inode),
  2561. .bitmask = server->cache_consistency_bitmask,
  2562. };
  2563. struct nfs4_accessres res = {
  2564. .server = server,
  2565. };
  2566. struct rpc_message msg = {
  2567. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
  2568. .rpc_argp = &args,
  2569. .rpc_resp = &res,
  2570. .rpc_cred = entry->cred,
  2571. };
  2572. int mode = entry->mask;
  2573. int status;
  2574. /*
  2575. * Determine which access bits we want to ask for...
  2576. */
  2577. if (mode & MAY_READ)
  2578. args.access |= NFS4_ACCESS_READ;
  2579. if (S_ISDIR(inode->i_mode)) {
  2580. if (mode & MAY_WRITE)
  2581. args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
  2582. if (mode & MAY_EXEC)
  2583. args.access |= NFS4_ACCESS_LOOKUP;
  2584. } else {
  2585. if (mode & MAY_WRITE)
  2586. args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
  2587. if (mode & MAY_EXEC)
  2588. args.access |= NFS4_ACCESS_EXECUTE;
  2589. }
  2590. res.fattr = nfs_alloc_fattr();
  2591. if (res.fattr == NULL)
  2592. return -ENOMEM;
  2593. status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  2594. if (!status) {
  2595. nfs_access_set_mask(entry, res.access);
  2596. nfs_refresh_inode(inode, res.fattr);
  2597. }
  2598. nfs_free_fattr(res.fattr);
  2599. return status;
  2600. }
  2601. static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
  2602. {
  2603. struct nfs4_exception exception = { };
  2604. int err;
  2605. do {
  2606. err = nfs4_handle_exception(NFS_SERVER(inode),
  2607. _nfs4_proc_access(inode, entry),
  2608. &exception);
  2609. } while (exception.retry);
  2610. return err;
  2611. }
  2612. /*
  2613. * TODO: For the time being, we don't try to get any attributes
  2614. * along with any of the zero-copy operations READ, READDIR,
  2615. * READLINK, WRITE.
  2616. *
  2617. * In the case of the first three, we want to put the GETATTR
  2618. * after the read-type operation -- this is because it is hard
  2619. * to predict the length of a GETATTR response in v4, and thus
  2620. * align the READ data correctly. This means that the GETATTR
  2621. * may end up partially falling into the page cache, and we should
  2622. * shift it into the 'tail' of the xdr_buf before processing.
  2623. * To do this efficiently, we need to know the total length
  2624. * of data received, which doesn't seem to be available outside
  2625. * of the RPC layer.
  2626. *
  2627. * In the case of WRITE, we also want to put the GETATTR after
  2628. * the operation -- in this case because we want to make sure
  2629. * we get the post-operation mtime and size.
  2630. *
  2631. * Both of these changes to the XDR layer would in fact be quite
  2632. * minor, but I decided to leave them for a subsequent patch.
  2633. */
  2634. static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
  2635. unsigned int pgbase, unsigned int pglen)
  2636. {
  2637. struct nfs4_readlink args = {
  2638. .fh = NFS_FH(inode),
  2639. .pgbase = pgbase,
  2640. .pglen = pglen,
  2641. .pages = &page,
  2642. };
  2643. struct nfs4_readlink_res res;
  2644. struct rpc_message msg = {
  2645. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
  2646. .rpc_argp = &args,
  2647. .rpc_resp = &res,
  2648. };
  2649. return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
  2650. }
  2651. static int nfs4_proc_readlink(struct inode *inode, struct page *page,
  2652. unsigned int pgbase, unsigned int pglen)
  2653. {
  2654. struct nfs4_exception exception = { };
  2655. int err;
  2656. do {
  2657. err = nfs4_handle_exception(NFS_SERVER(inode),
  2658. _nfs4_proc_readlink(inode, page, pgbase, pglen),
  2659. &exception);
  2660. } while (exception.retry);
  2661. return err;
  2662. }
  2663. /*
  2664. * This is just for mknod. open(O_CREAT) will always do ->open_context().
  2665. */
  2666. static int
  2667. nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
  2668. int flags)
  2669. {
  2670. struct nfs_open_context *ctx;
  2671. struct nfs4_state *state;
  2672. int status = 0;
  2673. ctx = alloc_nfs_open_context(dentry, FMODE_READ);
  2674. if (IS_ERR(ctx))
  2675. return PTR_ERR(ctx);
  2676. sattr->ia_mode &= ~current_umask();
  2677. state = nfs4_do_open(dir, dentry, ctx->mode,
  2678. flags, sattr, ctx->cred,
  2679. &ctx->mdsthreshold);
  2680. d_drop(dentry);
  2681. if (IS_ERR(state)) {
  2682. status = PTR_ERR(state);
  2683. goto out;
  2684. }
  2685. d_add(dentry, igrab(state->inode));
  2686. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  2687. ctx->state = state;
  2688. out:
  2689. put_nfs_open_context(ctx);
  2690. return status;
  2691. }
  2692. static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
  2693. {
  2694. struct nfs_server *server = NFS_SERVER(dir);
  2695. struct nfs_removeargs args = {
  2696. .fh = NFS_FH(dir),
  2697. .name = *name,
  2698. };
  2699. struct nfs_removeres res = {
  2700. .server = server,
  2701. };
  2702. struct rpc_message msg = {
  2703. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
  2704. .rpc_argp = &args,
  2705. .rpc_resp = &res,
  2706. };
  2707. int status;
  2708. status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
  2709. if (status == 0)
  2710. update_changeattr(dir, &res.cinfo);
  2711. return status;
  2712. }
  2713. static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
  2714. {
  2715. struct nfs4_exception exception = { };
  2716. int err;
  2717. do {
  2718. err = nfs4_handle_exception(NFS_SERVER(dir),
  2719. _nfs4_proc_remove(dir, name),
  2720. &exception);
  2721. } while (exception.retry);
  2722. return err;
  2723. }
  2724. static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
  2725. {
  2726. struct nfs_server *server = NFS_SERVER(dir);
  2727. struct nfs_removeargs *args = msg->rpc_argp;
  2728. struct nfs_removeres *res = msg->rpc_resp;
  2729. res->server = server;
  2730. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
  2731. nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
  2732. }
  2733. static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
  2734. {
  2735. nfs4_setup_sequence(NFS_SERVER(data->dir),
  2736. &data->args.seq_args,
  2737. &data->res.seq_res,
  2738. task);
  2739. }
  2740. static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
  2741. {
  2742. struct nfs_removeres *res = task->tk_msg.rpc_resp;
  2743. if (!nfs4_sequence_done(task, &res->seq_res))
  2744. return 0;
  2745. if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
  2746. return 0;
  2747. update_changeattr(dir, &res->cinfo);
  2748. return 1;
  2749. }
  2750. static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
  2751. {
  2752. struct nfs_server *server = NFS_SERVER(dir);
  2753. struct nfs_renameargs *arg = msg->rpc_argp;
  2754. struct nfs_renameres *res = msg->rpc_resp;
  2755. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
  2756. res->server = server;
  2757. nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
  2758. }
  2759. static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
  2760. {
  2761. nfs4_setup_sequence(NFS_SERVER(data->old_dir),
  2762. &data->args.seq_args,
  2763. &data->res.seq_res,
  2764. task);
  2765. }
  2766. static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
  2767. struct inode *new_dir)
  2768. {
  2769. struct nfs_renameres *res = task->tk_msg.rpc_resp;
  2770. if (!nfs4_sequence_done(task, &res->seq_res))
  2771. return 0;
  2772. if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
  2773. return 0;
  2774. update_changeattr(old_dir, &res->old_cinfo);
  2775. update_changeattr(new_dir, &res->new_cinfo);
  2776. return 1;
  2777. }
  2778. static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
  2779. struct inode *new_dir, struct qstr *new_name)
  2780. {
  2781. struct nfs_server *server = NFS_SERVER(old_dir);
  2782. struct nfs_renameargs arg = {
  2783. .old_dir = NFS_FH(old_dir),
  2784. .new_dir = NFS_FH(new_dir),
  2785. .old_name = old_name,
  2786. .new_name = new_name,
  2787. };
  2788. struct nfs_renameres res = {
  2789. .server = server,
  2790. };
  2791. struct rpc_message msg = {
  2792. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
  2793. .rpc_argp = &arg,
  2794. .rpc_resp = &res,
  2795. };
  2796. int status = -ENOMEM;
  2797. status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
  2798. if (!status) {
  2799. update_changeattr(old_dir, &res.old_cinfo);
  2800. update_changeattr(new_dir, &res.new_cinfo);
  2801. }
  2802. return status;
  2803. }
  2804. static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
  2805. struct inode *new_dir, struct qstr *new_name)
  2806. {
  2807. struct nfs4_exception exception = { };
  2808. int err;
  2809. do {
  2810. err = nfs4_handle_exception(NFS_SERVER(old_dir),
  2811. _nfs4_proc_rename(old_dir, old_name,
  2812. new_dir, new_name),
  2813. &exception);
  2814. } while (exception.retry);
  2815. return err;
  2816. }
  2817. static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
  2818. {
  2819. struct nfs_server *server = NFS_SERVER(inode);
  2820. struct nfs4_link_arg arg = {
  2821. .fh = NFS_FH(inode),
  2822. .dir_fh = NFS_FH(dir),
  2823. .name = name,
  2824. .bitmask = server->attr_bitmask,
  2825. };
  2826. struct nfs4_link_res res = {
  2827. .server = server,
  2828. };
  2829. struct rpc_message msg = {
  2830. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
  2831. .rpc_argp = &arg,
  2832. .rpc_resp = &res,
  2833. };
  2834. int status = -ENOMEM;
  2835. res.fattr = nfs_alloc_fattr();
  2836. if (res.fattr == NULL)
  2837. goto out;
  2838. status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
  2839. if (!status) {
  2840. update_changeattr(dir, &res.cinfo);
  2841. nfs_post_op_update_inode(inode, res.fattr);
  2842. }
  2843. out:
  2844. nfs_free_fattr(res.fattr);
  2845. return status;
  2846. }
  2847. static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
  2848. {
  2849. struct nfs4_exception exception = { };
  2850. int err;
  2851. do {
  2852. err = nfs4_handle_exception(NFS_SERVER(inode),
  2853. _nfs4_proc_link(inode, dir, name),
  2854. &exception);
  2855. } while (exception.retry);
  2856. return err;
  2857. }
  2858. struct nfs4_createdata {
  2859. struct rpc_message msg;
  2860. struct nfs4_create_arg arg;
  2861. struct nfs4_create_res res;
  2862. struct nfs_fh fh;
  2863. struct nfs_fattr fattr;
  2864. };
  2865. static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
  2866. struct qstr *name, struct iattr *sattr, u32 ftype)
  2867. {
  2868. struct nfs4_createdata *data;
  2869. data = kzalloc(sizeof(*data), GFP_KERNEL);
  2870. if (data != NULL) {
  2871. struct nfs_server *server = NFS_SERVER(dir);
  2872. data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
  2873. data->msg.rpc_argp = &data->arg;
  2874. data->msg.rpc_resp = &data->res;
  2875. data->arg.dir_fh = NFS_FH(dir);
  2876. data->arg.server = server;
  2877. data->arg.name = name;
  2878. data->arg.attrs = sattr;
  2879. data->arg.ftype = ftype;
  2880. data->arg.bitmask = server->attr_bitmask;
  2881. data->res.server = server;
  2882. data->res.fh = &data->fh;
  2883. data->res.fattr = &data->fattr;
  2884. nfs_fattr_init(data->res.fattr);
  2885. }
  2886. return data;
  2887. }
  2888. static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
  2889. {
  2890. int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
  2891. &data->arg.seq_args, &data->res.seq_res, 1);
  2892. if (status == 0) {
  2893. update_changeattr(dir, &data->res.dir_cinfo);
  2894. status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
  2895. }
  2896. return status;
  2897. }
  2898. static void nfs4_free_createdata(struct nfs4_createdata *data)
  2899. {
  2900. kfree(data);
  2901. }
  2902. static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
  2903. struct page *page, unsigned int len, struct iattr *sattr)
  2904. {
  2905. struct nfs4_createdata *data;
  2906. int status = -ENAMETOOLONG;
  2907. if (len > NFS4_MAXPATHLEN)
  2908. goto out;
  2909. status = -ENOMEM;
  2910. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
  2911. if (data == NULL)
  2912. goto out;
  2913. data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
  2914. data->arg.u.symlink.pages = &page;
  2915. data->arg.u.symlink.len = len;
  2916. status = nfs4_do_create(dir, dentry, data);
  2917. nfs4_free_createdata(data);
  2918. out:
  2919. return status;
  2920. }
  2921. static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
  2922. struct page *page, unsigned int len, struct iattr *sattr)
  2923. {
  2924. struct nfs4_exception exception = { };
  2925. int err;
  2926. do {
  2927. err = nfs4_handle_exception(NFS_SERVER(dir),
  2928. _nfs4_proc_symlink(dir, dentry, page,
  2929. len, sattr),
  2930. &exception);
  2931. } while (exception.retry);
  2932. return err;
  2933. }
  2934. static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
  2935. struct iattr *sattr)
  2936. {
  2937. struct nfs4_createdata *data;
  2938. int status = -ENOMEM;
  2939. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
  2940. if (data == NULL)
  2941. goto out;
  2942. status = nfs4_do_create(dir, dentry, data);
  2943. nfs4_free_createdata(data);
  2944. out:
  2945. return status;
  2946. }
  2947. static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
  2948. struct iattr *sattr)
  2949. {
  2950. struct nfs4_exception exception = { };
  2951. int err;
  2952. sattr->ia_mode &= ~current_umask();
  2953. do {
  2954. err = nfs4_handle_exception(NFS_SERVER(dir),
  2955. _nfs4_proc_mkdir(dir, dentry, sattr),
  2956. &exception);
  2957. } while (exception.retry);
  2958. return err;
  2959. }
  2960. static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
  2961. u64 cookie, struct page **pages, unsigned int count, int plus)
  2962. {
  2963. struct inode *dir = dentry->d_inode;
  2964. struct nfs4_readdir_arg args = {
  2965. .fh = NFS_FH(dir),
  2966. .pages = pages,
  2967. .pgbase = 0,
  2968. .count = count,
  2969. .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
  2970. .plus = plus,
  2971. };
  2972. struct nfs4_readdir_res res;
  2973. struct rpc_message msg = {
  2974. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
  2975. .rpc_argp = &args,
  2976. .rpc_resp = &res,
  2977. .rpc_cred = cred,
  2978. };
  2979. int status;
  2980. dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
  2981. dentry->d_parent->d_name.name,
  2982. dentry->d_name.name,
  2983. (unsigned long long)cookie);
  2984. nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
  2985. res.pgbase = args.pgbase;
  2986. status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
  2987. if (status >= 0) {
  2988. memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
  2989. status += args.pgbase;
  2990. }
  2991. nfs_invalidate_atime(dir);
  2992. dprintk("%s: returns %d\n", __func__, status);
  2993. return status;
  2994. }
  2995. static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
  2996. u64 cookie, struct page **pages, unsigned int count, int plus)
  2997. {
  2998. struct nfs4_exception exception = { };
  2999. int err;
  3000. do {
  3001. err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
  3002. _nfs4_proc_readdir(dentry, cred, cookie,
  3003. pages, count, plus),
  3004. &exception);
  3005. } while (exception.retry);
  3006. return err;
  3007. }
  3008. static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
  3009. struct iattr *sattr, dev_t rdev)
  3010. {
  3011. struct nfs4_createdata *data;
  3012. int mode = sattr->ia_mode;
  3013. int status = -ENOMEM;
  3014. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
  3015. if (data == NULL)
  3016. goto out;
  3017. if (S_ISFIFO(mode))
  3018. data->arg.ftype = NF4FIFO;
  3019. else if (S_ISBLK(mode)) {
  3020. data->arg.ftype = NF4BLK;
  3021. data->arg.u.device.specdata1 = MAJOR(rdev);
  3022. data->arg.u.device.specdata2 = MINOR(rdev);
  3023. }
  3024. else if (S_ISCHR(mode)) {
  3025. data->arg.ftype = NF4CHR;
  3026. data->arg.u.device.specdata1 = MAJOR(rdev);
  3027. data->arg.u.device.specdata2 = MINOR(rdev);
  3028. } else if (!S_ISSOCK(mode)) {
  3029. status = -EINVAL;
  3030. goto out_free;
  3031. }
  3032. status = nfs4_do_create(dir, dentry, data);
  3033. out_free:
  3034. nfs4_free_createdata(data);
  3035. out:
  3036. return status;
  3037. }
  3038. static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
  3039. struct iattr *sattr, dev_t rdev)
  3040. {
  3041. struct nfs4_exception exception = { };
  3042. int err;
  3043. sattr->ia_mode &= ~current_umask();
  3044. do {
  3045. err = nfs4_handle_exception(NFS_SERVER(dir),
  3046. _nfs4_proc_mknod(dir, dentry, sattr, rdev),
  3047. &exception);
  3048. } while (exception.retry);
  3049. return err;
  3050. }
  3051. static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
  3052. struct nfs_fsstat *fsstat)
  3053. {
  3054. struct nfs4_statfs_arg args = {
  3055. .fh = fhandle,
  3056. .bitmask = server->attr_bitmask,
  3057. };
  3058. struct nfs4_statfs_res res = {
  3059. .fsstat = fsstat,
  3060. };
  3061. struct rpc_message msg = {
  3062. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
  3063. .rpc_argp = &args,
  3064. .rpc_resp = &res,
  3065. };
  3066. nfs_fattr_init(fsstat->fattr);
  3067. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  3068. }
  3069. static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
  3070. {
  3071. struct nfs4_exception exception = { };
  3072. int err;
  3073. do {
  3074. err = nfs4_handle_exception(server,
  3075. _nfs4_proc_statfs(server, fhandle, fsstat),
  3076. &exception);
  3077. } while (exception.retry);
  3078. return err;
  3079. }
  3080. static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
  3081. struct nfs_fsinfo *fsinfo)
  3082. {
  3083. struct nfs4_fsinfo_arg args = {
  3084. .fh = fhandle,
  3085. .bitmask = server->attr_bitmask,
  3086. };
  3087. struct nfs4_fsinfo_res res = {
  3088. .fsinfo = fsinfo,
  3089. };
  3090. struct rpc_message msg = {
  3091. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
  3092. .rpc_argp = &args,
  3093. .rpc_resp = &res,
  3094. };
  3095. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  3096. }
  3097. static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
  3098. {
  3099. struct nfs4_exception exception = { };
  3100. int err;
  3101. do {
  3102. err = nfs4_handle_exception(server,
  3103. _nfs4_do_fsinfo(server, fhandle, fsinfo),
  3104. &exception);
  3105. } while (exception.retry);
  3106. return err;
  3107. }
  3108. static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
  3109. {
  3110. int error;
  3111. nfs_fattr_init(fsinfo->fattr);
  3112. error = nfs4_do_fsinfo(server, fhandle, fsinfo);
  3113. if (error == 0) {
  3114. /* block layout checks this! */
  3115. server->pnfs_blksize = fsinfo->blksize;
  3116. set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
  3117. }
  3118. return error;
  3119. }
  3120. static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
  3121. struct nfs_pathconf *pathconf)
  3122. {
  3123. struct nfs4_pathconf_arg args = {
  3124. .fh = fhandle,
  3125. .bitmask = server->attr_bitmask,
  3126. };
  3127. struct nfs4_pathconf_res res = {
  3128. .pathconf = pathconf,
  3129. };
  3130. struct rpc_message msg = {
  3131. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
  3132. .rpc_argp = &args,
  3133. .rpc_resp = &res,
  3134. };
  3135. /* None of the pathconf attributes are mandatory to implement */
  3136. if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
  3137. memset(pathconf, 0, sizeof(*pathconf));
  3138. return 0;
  3139. }
  3140. nfs_fattr_init(pathconf->fattr);
  3141. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  3142. }
  3143. static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
  3144. struct nfs_pathconf *pathconf)
  3145. {
  3146. struct nfs4_exception exception = { };
  3147. int err;
  3148. do {
  3149. err = nfs4_handle_exception(server,
  3150. _nfs4_proc_pathconf(server, fhandle, pathconf),
  3151. &exception);
  3152. } while (exception.retry);
  3153. return err;
  3154. }
  3155. int nfs4_set_rw_stateid(nfs4_stateid *stateid,
  3156. const struct nfs_open_context *ctx,
  3157. const struct nfs_lock_context *l_ctx,
  3158. fmode_t fmode)
  3159. {
  3160. const struct nfs_lockowner *lockowner = NULL;
  3161. if (l_ctx != NULL)
  3162. lockowner = &l_ctx->lockowner;
  3163. return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
  3164. }
  3165. EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
  3166. static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
  3167. const struct nfs_open_context *ctx,
  3168. const struct nfs_lock_context *l_ctx,
  3169. fmode_t fmode)
  3170. {
  3171. nfs4_stateid current_stateid;
  3172. if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
  3173. return false;
  3174. return nfs4_stateid_match(stateid, &current_stateid);
  3175. }
  3176. static bool nfs4_error_stateid_expired(int err)
  3177. {
  3178. switch (err) {
  3179. case -NFS4ERR_DELEG_REVOKED:
  3180. case -NFS4ERR_ADMIN_REVOKED:
  3181. case -NFS4ERR_BAD_STATEID:
  3182. case -NFS4ERR_STALE_STATEID:
  3183. case -NFS4ERR_OLD_STATEID:
  3184. case -NFS4ERR_OPENMODE:
  3185. case -NFS4ERR_EXPIRED:
  3186. return true;
  3187. }
  3188. return false;
  3189. }
  3190. void __nfs4_read_done_cb(struct nfs_read_data *data)
  3191. {
  3192. nfs_invalidate_atime(data->header->inode);
  3193. }
  3194. static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
  3195. {
  3196. struct nfs_server *server = NFS_SERVER(data->header->inode);
  3197. if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
  3198. rpc_restart_call_prepare(task);
  3199. return -EAGAIN;
  3200. }
  3201. __nfs4_read_done_cb(data);
  3202. if (task->tk_status > 0)
  3203. renew_lease(server, data->timestamp);
  3204. return 0;
  3205. }
  3206. static bool nfs4_read_stateid_changed(struct rpc_task *task,
  3207. struct nfs_readargs *args)
  3208. {
  3209. if (!nfs4_error_stateid_expired(task->tk_status) ||
  3210. nfs4_stateid_is_current(&args->stateid,
  3211. args->context,
  3212. args->lock_context,
  3213. FMODE_READ))
  3214. return false;
  3215. rpc_restart_call_prepare(task);
  3216. return true;
  3217. }
  3218. static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
  3219. {
  3220. dprintk("--> %s\n", __func__);
  3221. if (!nfs4_sequence_done(task, &data->res.seq_res))
  3222. return -EAGAIN;
  3223. if (nfs4_read_stateid_changed(task, &data->args))
  3224. return -EAGAIN;
  3225. return data->read_done_cb ? data->read_done_cb(task, data) :
  3226. nfs4_read_done_cb(task, data);
  3227. }
  3228. static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
  3229. {
  3230. data->timestamp = jiffies;
  3231. data->read_done_cb = nfs4_read_done_cb;
  3232. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
  3233. nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
  3234. }
  3235. static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
  3236. {
  3237. if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
  3238. &data->args.seq_args,
  3239. &data->res.seq_res,
  3240. task))
  3241. return;
  3242. nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
  3243. data->args.lock_context, FMODE_READ);
  3244. }
  3245. static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
  3246. {
  3247. struct inode *inode = data->header->inode;
  3248. if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
  3249. rpc_restart_call_prepare(task);
  3250. return -EAGAIN;
  3251. }
  3252. if (task->tk_status >= 0) {
  3253. renew_lease(NFS_SERVER(inode), data->timestamp);
  3254. nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
  3255. }
  3256. return 0;
  3257. }
  3258. static bool nfs4_write_stateid_changed(struct rpc_task *task,
  3259. struct nfs_writeargs *args)
  3260. {
  3261. if (!nfs4_error_stateid_expired(task->tk_status) ||
  3262. nfs4_stateid_is_current(&args->stateid,
  3263. args->context,
  3264. args->lock_context,
  3265. FMODE_WRITE))
  3266. return false;
  3267. rpc_restart_call_prepare(task);
  3268. return true;
  3269. }
  3270. static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
  3271. {
  3272. if (!nfs4_sequence_done(task, &data->res.seq_res))
  3273. return -EAGAIN;
  3274. if (nfs4_write_stateid_changed(task, &data->args))
  3275. return -EAGAIN;
  3276. return data->write_done_cb ? data->write_done_cb(task, data) :
  3277. nfs4_write_done_cb(task, data);
  3278. }
  3279. static
  3280. bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
  3281. {
  3282. const struct nfs_pgio_header *hdr = data->header;
  3283. /* Don't request attributes for pNFS or O_DIRECT writes */
  3284. if (data->ds_clp != NULL || hdr->dreq != NULL)
  3285. return false;
  3286. /* Otherwise, request attributes if and only if we don't hold
  3287. * a delegation
  3288. */
  3289. return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
  3290. }
  3291. static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
  3292. {
  3293. struct nfs_server *server = NFS_SERVER(data->header->inode);
  3294. if (!nfs4_write_need_cache_consistency_data(data)) {
  3295. data->args.bitmask = NULL;
  3296. data->res.fattr = NULL;
  3297. } else
  3298. data->args.bitmask = server->cache_consistency_bitmask;
  3299. if (!data->write_done_cb)
  3300. data->write_done_cb = nfs4_write_done_cb;
  3301. data->res.server = server;
  3302. data->timestamp = jiffies;
  3303. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
  3304. nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
  3305. }
  3306. static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
  3307. {
  3308. if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
  3309. &data->args.seq_args,
  3310. &data->res.seq_res,
  3311. task))
  3312. return;
  3313. nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
  3314. data->args.lock_context, FMODE_WRITE);
  3315. }
  3316. static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
  3317. {
  3318. nfs4_setup_sequence(NFS_SERVER(data->inode),
  3319. &data->args.seq_args,
  3320. &data->res.seq_res,
  3321. task);
  3322. }
  3323. static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
  3324. {
  3325. struct inode *inode = data->inode;
  3326. if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
  3327. rpc_restart_call_prepare(task);
  3328. return -EAGAIN;
  3329. }
  3330. return 0;
  3331. }
  3332. static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
  3333. {
  3334. if (!nfs4_sequence_done(task, &data->res.seq_res))
  3335. return -EAGAIN;
  3336. return data->commit_done_cb(task, data);
  3337. }
  3338. static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
  3339. {
  3340. struct nfs_server *server = NFS_SERVER(data->inode);
  3341. if (data->commit_done_cb == NULL)
  3342. data->commit_done_cb = nfs4_commit_done_cb;
  3343. data->res.server = server;
  3344. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
  3345. nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
  3346. }
  3347. struct nfs4_renewdata {
  3348. struct nfs_client *client;
  3349. unsigned long timestamp;
  3350. };
  3351. /*
  3352. * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
  3353. * standalone procedure for queueing an asynchronous RENEW.
  3354. */
  3355. static void nfs4_renew_release(void *calldata)
  3356. {
  3357. struct nfs4_renewdata *data = calldata;
  3358. struct nfs_client *clp = data->client;
  3359. if (atomic_read(&clp->cl_count) > 1)
  3360. nfs4_schedule_state_renewal(clp);
  3361. nfs_put_client(clp);
  3362. kfree(data);
  3363. }
  3364. static void nfs4_renew_done(struct rpc_task *task, void *calldata)
  3365. {
  3366. struct nfs4_renewdata *data = calldata;
  3367. struct nfs_client *clp = data->client;
  3368. unsigned long timestamp = data->timestamp;
  3369. if (task->tk_status < 0) {
  3370. /* Unless we're shutting down, schedule state recovery! */
  3371. if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
  3372. return;
  3373. if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
  3374. nfs4_schedule_lease_recovery(clp);
  3375. return;
  3376. }
  3377. nfs4_schedule_path_down_recovery(clp);
  3378. }
  3379. do_renew_lease(clp, timestamp);
  3380. }
  3381. static const struct rpc_call_ops nfs4_renew_ops = {
  3382. .rpc_call_done = nfs4_renew_done,
  3383. .rpc_release = nfs4_renew_release,
  3384. };
  3385. static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
  3386. {
  3387. struct rpc_message msg = {
  3388. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
  3389. .rpc_argp = clp,
  3390. .rpc_cred = cred,
  3391. };
  3392. struct nfs4_renewdata *data;
  3393. if (renew_flags == 0)
  3394. return 0;
  3395. if (!atomic_inc_not_zero(&clp->cl_count))
  3396. return -EIO;
  3397. data = kmalloc(sizeof(*data), GFP_NOFS);
  3398. if (data == NULL)
  3399. return -ENOMEM;
  3400. data->client = clp;
  3401. data->timestamp = jiffies;
  3402. return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
  3403. &nfs4_renew_ops, data);
  3404. }
  3405. static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
  3406. {
  3407. struct rpc_message msg = {
  3408. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
  3409. .rpc_argp = clp,
  3410. .rpc_cred = cred,
  3411. };
  3412. unsigned long now = jiffies;
  3413. int status;
  3414. status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
  3415. if (status < 0)
  3416. return status;
  3417. do_renew_lease(clp, now);
  3418. return 0;
  3419. }
  3420. static inline int nfs4_server_supports_acls(struct nfs_server *server)
  3421. {
  3422. return (server->caps & NFS_CAP_ACLS)
  3423. && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
  3424. && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
  3425. }
  3426. /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
  3427. * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
  3428. * the stack.
  3429. */
  3430. #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
  3431. static int buf_to_pages_noslab(const void *buf, size_t buflen,
  3432. struct page **pages, unsigned int *pgbase)
  3433. {
  3434. struct page *newpage, **spages;
  3435. int rc = 0;
  3436. size_t len;
  3437. spages = pages;
  3438. do {
  3439. len = min_t(size_t, PAGE_SIZE, buflen);
  3440. newpage = alloc_page(GFP_KERNEL);
  3441. if (newpage == NULL)
  3442. goto unwind;
  3443. memcpy(page_address(newpage), buf, len);
  3444. buf += len;
  3445. buflen -= len;
  3446. *pages++ = newpage;
  3447. rc++;
  3448. } while (buflen != 0);
  3449. return rc;
  3450. unwind:
  3451. for(; rc > 0; rc--)
  3452. __free_page(spages[rc-1]);
  3453. return -ENOMEM;
  3454. }
  3455. struct nfs4_cached_acl {
  3456. int cached;
  3457. size_t len;
  3458. char data[0];
  3459. };
  3460. static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
  3461. {
  3462. struct nfs_inode *nfsi = NFS_I(inode);
  3463. spin_lock(&inode->i_lock);
  3464. kfree(nfsi->nfs4_acl);
  3465. nfsi->nfs4_acl = acl;
  3466. spin_unlock(&inode->i_lock);
  3467. }
  3468. static void nfs4_zap_acl_attr(struct inode *inode)
  3469. {
  3470. nfs4_set_cached_acl(inode, NULL);
  3471. }
  3472. static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
  3473. {
  3474. struct nfs_inode *nfsi = NFS_I(inode);
  3475. struct nfs4_cached_acl *acl;
  3476. int ret = -ENOENT;
  3477. spin_lock(&inode->i_lock);
  3478. acl = nfsi->nfs4_acl;
  3479. if (acl == NULL)
  3480. goto out;
  3481. if (buf == NULL) /* user is just asking for length */
  3482. goto out_len;
  3483. if (acl->cached == 0)
  3484. goto out;
  3485. ret = -ERANGE; /* see getxattr(2) man page */
  3486. if (acl->len > buflen)
  3487. goto out;
  3488. memcpy(buf, acl->data, acl->len);
  3489. out_len:
  3490. ret = acl->len;
  3491. out:
  3492. spin_unlock(&inode->i_lock);
  3493. return ret;
  3494. }
  3495. static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
  3496. {
  3497. struct nfs4_cached_acl *acl;
  3498. size_t buflen = sizeof(*acl) + acl_len;
  3499. if (buflen <= PAGE_SIZE) {
  3500. acl = kmalloc(buflen, GFP_KERNEL);
  3501. if (acl == NULL)
  3502. goto out;
  3503. acl->cached = 1;
  3504. _copy_from_pages(acl->data, pages, pgbase, acl_len);
  3505. } else {
  3506. acl = kmalloc(sizeof(*acl), GFP_KERNEL);
  3507. if (acl == NULL)
  3508. goto out;
  3509. acl->cached = 0;
  3510. }
  3511. acl->len = acl_len;
  3512. out:
  3513. nfs4_set_cached_acl(inode, acl);
  3514. }
  3515. /*
  3516. * The getxattr API returns the required buffer length when called with a
  3517. * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
  3518. * the required buf. On a NULL buf, we send a page of data to the server
  3519. * guessing that the ACL request can be serviced by a page. If so, we cache
  3520. * up to the page of ACL data, and the 2nd call to getxattr is serviced by
  3521. * the cache. If not so, we throw away the page, and cache the required
  3522. * length. The next getxattr call will then produce another round trip to
  3523. * the server, this time with the input buf of the required size.
  3524. */
  3525. static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
  3526. {
  3527. struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
  3528. struct nfs_getaclargs args = {
  3529. .fh = NFS_FH(inode),
  3530. .acl_pages = pages,
  3531. .acl_len = buflen,
  3532. };
  3533. struct nfs_getaclres res = {
  3534. .acl_len = buflen,
  3535. };
  3536. struct rpc_message msg = {
  3537. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
  3538. .rpc_argp = &args,
  3539. .rpc_resp = &res,
  3540. };
  3541. unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
  3542. int ret = -ENOMEM, i;
  3543. /* As long as we're doing a round trip to the server anyway,
  3544. * let's be prepared for a page of acl data. */
  3545. if (npages == 0)
  3546. npages = 1;
  3547. if (npages > ARRAY_SIZE(pages))
  3548. return -ERANGE;
  3549. for (i = 0; i < npages; i++) {
  3550. pages[i] = alloc_page(GFP_KERNEL);
  3551. if (!pages[i])
  3552. goto out_free;
  3553. }
  3554. /* for decoding across pages */
  3555. res.acl_scratch = alloc_page(GFP_KERNEL);
  3556. if (!res.acl_scratch)
  3557. goto out_free;
  3558. args.acl_len = npages * PAGE_SIZE;
  3559. args.acl_pgbase = 0;
  3560. dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
  3561. __func__, buf, buflen, npages, args.acl_len);
  3562. ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
  3563. &msg, &args.seq_args, &res.seq_res, 0);
  3564. if (ret)
  3565. goto out_free;
  3566. /* Handle the case where the passed-in buffer is too short */
  3567. if (res.acl_flags & NFS4_ACL_TRUNC) {
  3568. /* Did the user only issue a request for the acl length? */
  3569. if (buf == NULL)
  3570. goto out_ok;
  3571. ret = -ERANGE;
  3572. goto out_free;
  3573. }
  3574. nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
  3575. if (buf) {
  3576. if (res.acl_len > buflen) {
  3577. ret = -ERANGE;
  3578. goto out_free;
  3579. }
  3580. _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
  3581. }
  3582. out_ok:
  3583. ret = res.acl_len;
  3584. out_free:
  3585. for (i = 0; i < npages; i++)
  3586. if (pages[i])
  3587. __free_page(pages[i]);
  3588. if (res.acl_scratch)
  3589. __free_page(res.acl_scratch);
  3590. return ret;
  3591. }
  3592. static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
  3593. {
  3594. struct nfs4_exception exception = { };
  3595. ssize_t ret;
  3596. do {
  3597. ret = __nfs4_get_acl_uncached(inode, buf, buflen);
  3598. if (ret >= 0)
  3599. break;
  3600. ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
  3601. } while (exception.retry);
  3602. return ret;
  3603. }
  3604. static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
  3605. {
  3606. struct nfs_server *server = NFS_SERVER(inode);
  3607. int ret;
  3608. if (!nfs4_server_supports_acls(server))
  3609. return -EOPNOTSUPP;
  3610. ret = nfs_revalidate_inode(server, inode);
  3611. if (ret < 0)
  3612. return ret;
  3613. if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
  3614. nfs_zap_acl_cache(inode);
  3615. ret = nfs4_read_cached_acl(inode, buf, buflen);
  3616. if (ret != -ENOENT)
  3617. /* -ENOENT is returned if there is no ACL or if there is an ACL
  3618. * but no cached acl data, just the acl length */
  3619. return ret;
  3620. return nfs4_get_acl_uncached(inode, buf, buflen);
  3621. }
  3622. static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
  3623. {
  3624. struct nfs_server *server = NFS_SERVER(inode);
  3625. struct page *pages[NFS4ACL_MAXPAGES];
  3626. struct nfs_setaclargs arg = {
  3627. .fh = NFS_FH(inode),
  3628. .acl_pages = pages,
  3629. .acl_len = buflen,
  3630. };
  3631. struct nfs_setaclres res;
  3632. struct rpc_message msg = {
  3633. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
  3634. .rpc_argp = &arg,
  3635. .rpc_resp = &res,
  3636. };
  3637. unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
  3638. int ret, i;
  3639. if (!nfs4_server_supports_acls(server))
  3640. return -EOPNOTSUPP;
  3641. if (npages > ARRAY_SIZE(pages))
  3642. return -ERANGE;
  3643. i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
  3644. if (i < 0)
  3645. return i;
  3646. nfs4_inode_return_delegation(inode);
  3647. ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
  3648. /*
  3649. * Free each page after tx, so the only ref left is
  3650. * held by the network stack
  3651. */
  3652. for (; i > 0; i--)
  3653. put_page(pages[i-1]);
  3654. /*
  3655. * Acl update can result in inode attribute update.
  3656. * so mark the attribute cache invalid.
  3657. */
  3658. spin_lock(&inode->i_lock);
  3659. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
  3660. spin_unlock(&inode->i_lock);
  3661. nfs_access_zap_cache(inode);
  3662. nfs_zap_acl_cache(inode);
  3663. return ret;
  3664. }
  3665. static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
  3666. {
  3667. struct nfs4_exception exception = { };
  3668. int err;
  3669. do {
  3670. err = nfs4_handle_exception(NFS_SERVER(inode),
  3671. __nfs4_proc_set_acl(inode, buf, buflen),
  3672. &exception);
  3673. } while (exception.retry);
  3674. return err;
  3675. }
  3676. static int
  3677. nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
  3678. {
  3679. struct nfs_client *clp = server->nfs_client;
  3680. if (task->tk_status >= 0)
  3681. return 0;
  3682. switch(task->tk_status) {
  3683. case -NFS4ERR_DELEG_REVOKED:
  3684. case -NFS4ERR_ADMIN_REVOKED:
  3685. case -NFS4ERR_BAD_STATEID:
  3686. if (state == NULL)
  3687. break;
  3688. nfs_remove_bad_delegation(state->inode);
  3689. case -NFS4ERR_OPENMODE:
  3690. if (state == NULL)
  3691. break;
  3692. if (nfs4_schedule_stateid_recovery(server, state) < 0)
  3693. goto stateid_invalid;
  3694. goto wait_on_recovery;
  3695. case -NFS4ERR_EXPIRED:
  3696. if (state != NULL) {
  3697. if (nfs4_schedule_stateid_recovery(server, state) < 0)
  3698. goto stateid_invalid;
  3699. }
  3700. case -NFS4ERR_STALE_STATEID:
  3701. case -NFS4ERR_STALE_CLIENTID:
  3702. nfs4_schedule_lease_recovery(clp);
  3703. goto wait_on_recovery;
  3704. #if defined(CONFIG_NFS_V4_1)
  3705. case -NFS4ERR_BADSESSION:
  3706. case -NFS4ERR_BADSLOT:
  3707. case -NFS4ERR_BAD_HIGH_SLOT:
  3708. case -NFS4ERR_DEADSESSION:
  3709. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  3710. case -NFS4ERR_SEQ_FALSE_RETRY:
  3711. case -NFS4ERR_SEQ_MISORDERED:
  3712. dprintk("%s ERROR %d, Reset session\n", __func__,
  3713. task->tk_status);
  3714. nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
  3715. task->tk_status = 0;
  3716. return -EAGAIN;
  3717. #endif /* CONFIG_NFS_V4_1 */
  3718. case -NFS4ERR_DELAY:
  3719. nfs_inc_server_stats(server, NFSIOS_DELAY);
  3720. case -NFS4ERR_GRACE:
  3721. rpc_delay(task, NFS4_POLL_RETRY_MAX);
  3722. task->tk_status = 0;
  3723. return -EAGAIN;
  3724. case -NFS4ERR_RETRY_UNCACHED_REP:
  3725. case -NFS4ERR_OLD_STATEID:
  3726. task->tk_status = 0;
  3727. return -EAGAIN;
  3728. }
  3729. task->tk_status = nfs4_map_errors(task->tk_status);
  3730. return 0;
  3731. stateid_invalid:
  3732. task->tk_status = -EIO;
  3733. return 0;
  3734. wait_on_recovery:
  3735. rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
  3736. if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
  3737. rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
  3738. task->tk_status = 0;
  3739. return -EAGAIN;
  3740. }
  3741. static void nfs4_init_boot_verifier(const struct nfs_client *clp,
  3742. nfs4_verifier *bootverf)
  3743. {
  3744. __be32 verf[2];
  3745. if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
  3746. /* An impossible timestamp guarantees this value
  3747. * will never match a generated boot time. */
  3748. verf[0] = 0;
  3749. verf[1] = (__be32)(NSEC_PER_SEC + 1);
  3750. } else {
  3751. struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
  3752. verf[0] = (__be32)nn->boot_time.tv_sec;
  3753. verf[1] = (__be32)nn->boot_time.tv_nsec;
  3754. }
  3755. memcpy(bootverf->data, verf, sizeof(bootverf->data));
  3756. }
  3757. static unsigned int
  3758. nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
  3759. char *buf, size_t len)
  3760. {
  3761. unsigned int result;
  3762. rcu_read_lock();
  3763. result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
  3764. clp->cl_ipaddr,
  3765. rpc_peeraddr2str(clp->cl_rpcclient,
  3766. RPC_DISPLAY_ADDR),
  3767. rpc_peeraddr2str(clp->cl_rpcclient,
  3768. RPC_DISPLAY_PROTO));
  3769. rcu_read_unlock();
  3770. return result;
  3771. }
  3772. static unsigned int
  3773. nfs4_init_uniform_client_string(const struct nfs_client *clp,
  3774. char *buf, size_t len)
  3775. {
  3776. char *nodename = clp->cl_rpcclient->cl_nodename;
  3777. if (nfs4_client_id_uniquifier[0] != '\0')
  3778. nodename = nfs4_client_id_uniquifier;
  3779. return scnprintf(buf, len, "Linux NFSv%u.%u %s",
  3780. clp->rpc_ops->version, clp->cl_minorversion,
  3781. nodename);
  3782. }
  3783. /**
  3784. * nfs4_proc_setclientid - Negotiate client ID
  3785. * @clp: state data structure
  3786. * @program: RPC program for NFSv4 callback service
  3787. * @port: IP port number for NFS4 callback service
  3788. * @cred: RPC credential to use for this call
  3789. * @res: where to place the result
  3790. *
  3791. * Returns zero, a negative errno, or a negative NFS4ERR status code.
  3792. */
  3793. int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
  3794. unsigned short port, struct rpc_cred *cred,
  3795. struct nfs4_setclientid_res *res)
  3796. {
  3797. nfs4_verifier sc_verifier;
  3798. struct nfs4_setclientid setclientid = {
  3799. .sc_verifier = &sc_verifier,
  3800. .sc_prog = program,
  3801. .sc_cb_ident = clp->cl_cb_ident,
  3802. };
  3803. struct rpc_message msg = {
  3804. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
  3805. .rpc_argp = &setclientid,
  3806. .rpc_resp = res,
  3807. .rpc_cred = cred,
  3808. };
  3809. int status;
  3810. /* nfs_client_id4 */
  3811. nfs4_init_boot_verifier(clp, &sc_verifier);
  3812. if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
  3813. setclientid.sc_name_len =
  3814. nfs4_init_uniform_client_string(clp,
  3815. setclientid.sc_name,
  3816. sizeof(setclientid.sc_name));
  3817. else
  3818. setclientid.sc_name_len =
  3819. nfs4_init_nonuniform_client_string(clp,
  3820. setclientid.sc_name,
  3821. sizeof(setclientid.sc_name));
  3822. /* cb_client4 */
  3823. rcu_read_lock();
  3824. setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
  3825. sizeof(setclientid.sc_netid),
  3826. rpc_peeraddr2str(clp->cl_rpcclient,
  3827. RPC_DISPLAY_NETID));
  3828. rcu_read_unlock();
  3829. setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
  3830. sizeof(setclientid.sc_uaddr), "%s.%u.%u",
  3831. clp->cl_ipaddr, port >> 8, port & 255);
  3832. dprintk("NFS call setclientid auth=%s, '%.*s'\n",
  3833. clp->cl_rpcclient->cl_auth->au_ops->au_name,
  3834. setclientid.sc_name_len, setclientid.sc_name);
  3835. status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  3836. dprintk("NFS reply setclientid: %d\n", status);
  3837. return status;
  3838. }
  3839. /**
  3840. * nfs4_proc_setclientid_confirm - Confirm client ID
  3841. * @clp: state data structure
  3842. * @res: result of a previous SETCLIENTID
  3843. * @cred: RPC credential to use for this call
  3844. *
  3845. * Returns zero, a negative errno, or a negative NFS4ERR status code.
  3846. */
  3847. int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
  3848. struct nfs4_setclientid_res *arg,
  3849. struct rpc_cred *cred)
  3850. {
  3851. struct nfs_fsinfo fsinfo;
  3852. struct rpc_message msg = {
  3853. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
  3854. .rpc_argp = arg,
  3855. .rpc_resp = &fsinfo,
  3856. .rpc_cred = cred,
  3857. };
  3858. unsigned long now;
  3859. int status;
  3860. dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
  3861. clp->cl_rpcclient->cl_auth->au_ops->au_name,
  3862. clp->cl_clientid);
  3863. now = jiffies;
  3864. status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  3865. if (status == 0) {
  3866. spin_lock(&clp->cl_lock);
  3867. clp->cl_lease_time = fsinfo.lease_time * HZ;
  3868. clp->cl_last_renewal = now;
  3869. spin_unlock(&clp->cl_lock);
  3870. }
  3871. dprintk("NFS reply setclientid_confirm: %d\n", status);
  3872. return status;
  3873. }
  3874. struct nfs4_delegreturndata {
  3875. struct nfs4_delegreturnargs args;
  3876. struct nfs4_delegreturnres res;
  3877. struct nfs_fh fh;
  3878. nfs4_stateid stateid;
  3879. unsigned long timestamp;
  3880. struct nfs_fattr fattr;
  3881. int rpc_status;
  3882. };
  3883. static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
  3884. {
  3885. struct nfs4_delegreturndata *data = calldata;
  3886. if (!nfs4_sequence_done(task, &data->res.seq_res))
  3887. return;
  3888. switch (task->tk_status) {
  3889. case -NFS4ERR_STALE_STATEID:
  3890. case -NFS4ERR_EXPIRED:
  3891. case 0:
  3892. renew_lease(data->res.server, data->timestamp);
  3893. break;
  3894. default:
  3895. if (nfs4_async_handle_error(task, data->res.server, NULL) ==
  3896. -EAGAIN) {
  3897. rpc_restart_call_prepare(task);
  3898. return;
  3899. }
  3900. }
  3901. data->rpc_status = task->tk_status;
  3902. }
  3903. static void nfs4_delegreturn_release(void *calldata)
  3904. {
  3905. kfree(calldata);
  3906. }
  3907. #if defined(CONFIG_NFS_V4_1)
  3908. static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
  3909. {
  3910. struct nfs4_delegreturndata *d_data;
  3911. d_data = (struct nfs4_delegreturndata *)data;
  3912. nfs4_setup_sequence(d_data->res.server,
  3913. &d_data->args.seq_args,
  3914. &d_data->res.seq_res,
  3915. task);
  3916. }
  3917. #endif /* CONFIG_NFS_V4_1 */
  3918. static const struct rpc_call_ops nfs4_delegreturn_ops = {
  3919. #if defined(CONFIG_NFS_V4_1)
  3920. .rpc_call_prepare = nfs4_delegreturn_prepare,
  3921. #endif /* CONFIG_NFS_V4_1 */
  3922. .rpc_call_done = nfs4_delegreturn_done,
  3923. .rpc_release = nfs4_delegreturn_release,
  3924. };
  3925. static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
  3926. {
  3927. struct nfs4_delegreturndata *data;
  3928. struct nfs_server *server = NFS_SERVER(inode);
  3929. struct rpc_task *task;
  3930. struct rpc_message msg = {
  3931. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
  3932. .rpc_cred = cred,
  3933. };
  3934. struct rpc_task_setup task_setup_data = {
  3935. .rpc_client = server->client,
  3936. .rpc_message = &msg,
  3937. .callback_ops = &nfs4_delegreturn_ops,
  3938. .flags = RPC_TASK_ASYNC,
  3939. };
  3940. int status = 0;
  3941. data = kzalloc(sizeof(*data), GFP_NOFS);
  3942. if (data == NULL)
  3943. return -ENOMEM;
  3944. nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
  3945. data->args.fhandle = &data->fh;
  3946. data->args.stateid = &data->stateid;
  3947. data->args.bitmask = server->cache_consistency_bitmask;
  3948. nfs_copy_fh(&data->fh, NFS_FH(inode));
  3949. nfs4_stateid_copy(&data->stateid, stateid);
  3950. data->res.fattr = &data->fattr;
  3951. data->res.server = server;
  3952. nfs_fattr_init(data->res.fattr);
  3953. data->timestamp = jiffies;
  3954. data->rpc_status = 0;
  3955. task_setup_data.callback_data = data;
  3956. msg.rpc_argp = &data->args;
  3957. msg.rpc_resp = &data->res;
  3958. task = rpc_run_task(&task_setup_data);
  3959. if (IS_ERR(task))
  3960. return PTR_ERR(task);
  3961. if (!issync)
  3962. goto out;
  3963. status = nfs4_wait_for_completion_rpc_task(task);
  3964. if (status != 0)
  3965. goto out;
  3966. status = data->rpc_status;
  3967. if (status == 0)
  3968. nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
  3969. else
  3970. nfs_refresh_inode(inode, &data->fattr);
  3971. out:
  3972. rpc_put_task(task);
  3973. return status;
  3974. }
  3975. int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
  3976. {
  3977. struct nfs_server *server = NFS_SERVER(inode);
  3978. struct nfs4_exception exception = { };
  3979. int err;
  3980. do {
  3981. err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
  3982. switch (err) {
  3983. case -NFS4ERR_STALE_STATEID:
  3984. case -NFS4ERR_EXPIRED:
  3985. case 0:
  3986. return 0;
  3987. }
  3988. err = nfs4_handle_exception(server, err, &exception);
  3989. } while (exception.retry);
  3990. return err;
  3991. }
  3992. #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
  3993. #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
  3994. /*
  3995. * sleep, with exponential backoff, and retry the LOCK operation.
  3996. */
  3997. static unsigned long
  3998. nfs4_set_lock_task_retry(unsigned long timeout)
  3999. {
  4000. freezable_schedule_timeout_killable(timeout);
  4001. timeout <<= 1;
  4002. if (timeout > NFS4_LOCK_MAXTIMEOUT)
  4003. return NFS4_LOCK_MAXTIMEOUT;
  4004. return timeout;
  4005. }
  4006. static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  4007. {
  4008. struct inode *inode = state->inode;
  4009. struct nfs_server *server = NFS_SERVER(inode);
  4010. struct nfs_client *clp = server->nfs_client;
  4011. struct nfs_lockt_args arg = {
  4012. .fh = NFS_FH(inode),
  4013. .fl = request,
  4014. };
  4015. struct nfs_lockt_res res = {
  4016. .denied = request,
  4017. };
  4018. struct rpc_message msg = {
  4019. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
  4020. .rpc_argp = &arg,
  4021. .rpc_resp = &res,
  4022. .rpc_cred = state->owner->so_cred,
  4023. };
  4024. struct nfs4_lock_state *lsp;
  4025. int status;
  4026. arg.lock_owner.clientid = clp->cl_clientid;
  4027. status = nfs4_set_lock_state(state, request);
  4028. if (status != 0)
  4029. goto out;
  4030. lsp = request->fl_u.nfs4_fl.owner;
  4031. arg.lock_owner.id = lsp->ls_seqid.owner_id;
  4032. arg.lock_owner.s_dev = server->s_dev;
  4033. status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
  4034. switch (status) {
  4035. case 0:
  4036. request->fl_type = F_UNLCK;
  4037. break;
  4038. case -NFS4ERR_DENIED:
  4039. status = 0;
  4040. }
  4041. request->fl_ops->fl_release_private(request);
  4042. out:
  4043. return status;
  4044. }
  4045. static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  4046. {
  4047. struct nfs4_exception exception = { };
  4048. int err;
  4049. do {
  4050. err = nfs4_handle_exception(NFS_SERVER(state->inode),
  4051. _nfs4_proc_getlk(state, cmd, request),
  4052. &exception);
  4053. } while (exception.retry);
  4054. return err;
  4055. }
  4056. static int do_vfs_lock(struct file *file, struct file_lock *fl)
  4057. {
  4058. int res = 0;
  4059. switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
  4060. case FL_POSIX:
  4061. res = posix_lock_file_wait(file, fl);
  4062. break;
  4063. case FL_FLOCK:
  4064. res = flock_lock_file_wait(file, fl);
  4065. break;
  4066. default:
  4067. BUG();
  4068. }
  4069. return res;
  4070. }
  4071. struct nfs4_unlockdata {
  4072. struct nfs_locku_args arg;
  4073. struct nfs_locku_res res;
  4074. struct nfs4_lock_state *lsp;
  4075. struct nfs_open_context *ctx;
  4076. struct file_lock fl;
  4077. const struct nfs_server *server;
  4078. unsigned long timestamp;
  4079. };
  4080. static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
  4081. struct nfs_open_context *ctx,
  4082. struct nfs4_lock_state *lsp,
  4083. struct nfs_seqid *seqid)
  4084. {
  4085. struct nfs4_unlockdata *p;
  4086. struct inode *inode = lsp->ls_state->inode;
  4087. p = kzalloc(sizeof(*p), GFP_NOFS);
  4088. if (p == NULL)
  4089. return NULL;
  4090. p->arg.fh = NFS_FH(inode);
  4091. p->arg.fl = &p->fl;
  4092. p->arg.seqid = seqid;
  4093. p->res.seqid = seqid;
  4094. p->arg.stateid = &lsp->ls_stateid;
  4095. p->lsp = lsp;
  4096. atomic_inc(&lsp->ls_count);
  4097. /* Ensure we don't close file until we're done freeing locks! */
  4098. p->ctx = get_nfs_open_context(ctx);
  4099. memcpy(&p->fl, fl, sizeof(p->fl));
  4100. p->server = NFS_SERVER(inode);
  4101. return p;
  4102. }
  4103. static void nfs4_locku_release_calldata(void *data)
  4104. {
  4105. struct nfs4_unlockdata *calldata = data;
  4106. nfs_free_seqid(calldata->arg.seqid);
  4107. nfs4_put_lock_state(calldata->lsp);
  4108. put_nfs_open_context(calldata->ctx);
  4109. kfree(calldata);
  4110. }
  4111. static void nfs4_locku_done(struct rpc_task *task, void *data)
  4112. {
  4113. struct nfs4_unlockdata *calldata = data;
  4114. if (!nfs4_sequence_done(task, &calldata->res.seq_res))
  4115. return;
  4116. switch (task->tk_status) {
  4117. case 0:
  4118. nfs4_stateid_copy(&calldata->lsp->ls_stateid,
  4119. &calldata->res.stateid);
  4120. renew_lease(calldata->server, calldata->timestamp);
  4121. break;
  4122. case -NFS4ERR_BAD_STATEID:
  4123. case -NFS4ERR_OLD_STATEID:
  4124. case -NFS4ERR_STALE_STATEID:
  4125. case -NFS4ERR_EXPIRED:
  4126. break;
  4127. default:
  4128. if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
  4129. rpc_restart_call_prepare(task);
  4130. }
  4131. nfs_release_seqid(calldata->arg.seqid);
  4132. }
  4133. static void nfs4_locku_prepare(struct rpc_task *task, void *data)
  4134. {
  4135. struct nfs4_unlockdata *calldata = data;
  4136. if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
  4137. goto out_wait;
  4138. if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
  4139. /* Note: exit _without_ running nfs4_locku_done */
  4140. goto out_no_action;
  4141. }
  4142. calldata->timestamp = jiffies;
  4143. if (nfs4_setup_sequence(calldata->server,
  4144. &calldata->arg.seq_args,
  4145. &calldata->res.seq_res,
  4146. task) != 0)
  4147. nfs_release_seqid(calldata->arg.seqid);
  4148. return;
  4149. out_no_action:
  4150. task->tk_action = NULL;
  4151. out_wait:
  4152. nfs4_sequence_done(task, &calldata->res.seq_res);
  4153. }
  4154. static const struct rpc_call_ops nfs4_locku_ops = {
  4155. .rpc_call_prepare = nfs4_locku_prepare,
  4156. .rpc_call_done = nfs4_locku_done,
  4157. .rpc_release = nfs4_locku_release_calldata,
  4158. };
  4159. static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
  4160. struct nfs_open_context *ctx,
  4161. struct nfs4_lock_state *lsp,
  4162. struct nfs_seqid *seqid)
  4163. {
  4164. struct nfs4_unlockdata *data;
  4165. struct rpc_message msg = {
  4166. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
  4167. .rpc_cred = ctx->cred,
  4168. };
  4169. struct rpc_task_setup task_setup_data = {
  4170. .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
  4171. .rpc_message = &msg,
  4172. .callback_ops = &nfs4_locku_ops,
  4173. .workqueue = nfsiod_workqueue,
  4174. .flags = RPC_TASK_ASYNC,
  4175. };
  4176. /* Ensure this is an unlock - when canceling a lock, the
  4177. * canceled lock is passed in, and it won't be an unlock.
  4178. */
  4179. fl->fl_type = F_UNLCK;
  4180. data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
  4181. if (data == NULL) {
  4182. nfs_free_seqid(seqid);
  4183. return ERR_PTR(-ENOMEM);
  4184. }
  4185. nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
  4186. msg.rpc_argp = &data->arg;
  4187. msg.rpc_resp = &data->res;
  4188. task_setup_data.callback_data = data;
  4189. return rpc_run_task(&task_setup_data);
  4190. }
  4191. static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
  4192. {
  4193. struct inode *inode = state->inode;
  4194. struct nfs4_state_owner *sp = state->owner;
  4195. struct nfs_inode *nfsi = NFS_I(inode);
  4196. struct nfs_seqid *seqid;
  4197. struct nfs4_lock_state *lsp;
  4198. struct rpc_task *task;
  4199. int status = 0;
  4200. unsigned char fl_flags = request->fl_flags;
  4201. status = nfs4_set_lock_state(state, request);
  4202. /* Unlock _before_ we do the RPC call */
  4203. request->fl_flags |= FL_EXISTS;
  4204. /* Exclude nfs_delegation_claim_locks() */
  4205. mutex_lock(&sp->so_delegreturn_mutex);
  4206. /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
  4207. down_read(&nfsi->rwsem);
  4208. if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
  4209. up_read(&nfsi->rwsem);
  4210. mutex_unlock(&sp->so_delegreturn_mutex);
  4211. goto out;
  4212. }
  4213. up_read(&nfsi->rwsem);
  4214. mutex_unlock(&sp->so_delegreturn_mutex);
  4215. if (status != 0)
  4216. goto out;
  4217. /* Is this a delegated lock? */
  4218. if (test_bit(NFS_DELEGATED_STATE, &state->flags))
  4219. goto out;
  4220. lsp = request->fl_u.nfs4_fl.owner;
  4221. seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
  4222. status = -ENOMEM;
  4223. if (seqid == NULL)
  4224. goto out;
  4225. task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
  4226. status = PTR_ERR(task);
  4227. if (IS_ERR(task))
  4228. goto out;
  4229. status = nfs4_wait_for_completion_rpc_task(task);
  4230. rpc_put_task(task);
  4231. out:
  4232. request->fl_flags = fl_flags;
  4233. return status;
  4234. }
  4235. struct nfs4_lockdata {
  4236. struct nfs_lock_args arg;
  4237. struct nfs_lock_res res;
  4238. struct nfs4_lock_state *lsp;
  4239. struct nfs_open_context *ctx;
  4240. struct file_lock fl;
  4241. unsigned long timestamp;
  4242. int rpc_status;
  4243. int cancelled;
  4244. struct nfs_server *server;
  4245. };
  4246. static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
  4247. struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
  4248. gfp_t gfp_mask)
  4249. {
  4250. struct nfs4_lockdata *p;
  4251. struct inode *inode = lsp->ls_state->inode;
  4252. struct nfs_server *server = NFS_SERVER(inode);
  4253. p = kzalloc(sizeof(*p), gfp_mask);
  4254. if (p == NULL)
  4255. return NULL;
  4256. p->arg.fh = NFS_FH(inode);
  4257. p->arg.fl = &p->fl;
  4258. p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
  4259. if (p->arg.open_seqid == NULL)
  4260. goto out_free;
  4261. p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
  4262. if (p->arg.lock_seqid == NULL)
  4263. goto out_free_seqid;
  4264. p->arg.lock_stateid = &lsp->ls_stateid;
  4265. p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
  4266. p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
  4267. p->arg.lock_owner.s_dev = server->s_dev;
  4268. p->res.lock_seqid = p->arg.lock_seqid;
  4269. p->lsp = lsp;
  4270. p->server = server;
  4271. atomic_inc(&lsp->ls_count);
  4272. p->ctx = get_nfs_open_context(ctx);
  4273. memcpy(&p->fl, fl, sizeof(p->fl));
  4274. return p;
  4275. out_free_seqid:
  4276. nfs_free_seqid(p->arg.open_seqid);
  4277. out_free:
  4278. kfree(p);
  4279. return NULL;
  4280. }
  4281. static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
  4282. {
  4283. struct nfs4_lockdata *data = calldata;
  4284. struct nfs4_state *state = data->lsp->ls_state;
  4285. dprintk("%s: begin!\n", __func__);
  4286. if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
  4287. goto out_wait;
  4288. /* Do we need to do an open_to_lock_owner? */
  4289. if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
  4290. if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
  4291. goto out_release_lock_seqid;
  4292. }
  4293. data->arg.open_stateid = &state->stateid;
  4294. data->arg.new_lock_owner = 1;
  4295. data->res.open_seqid = data->arg.open_seqid;
  4296. } else
  4297. data->arg.new_lock_owner = 0;
  4298. if (!nfs4_valid_open_stateid(state)) {
  4299. data->rpc_status = -EBADF;
  4300. task->tk_action = NULL;
  4301. goto out_release_open_seqid;
  4302. }
  4303. data->timestamp = jiffies;
  4304. if (nfs4_setup_sequence(data->server,
  4305. &data->arg.seq_args,
  4306. &data->res.seq_res,
  4307. task) == 0)
  4308. return;
  4309. out_release_open_seqid:
  4310. nfs_release_seqid(data->arg.open_seqid);
  4311. out_release_lock_seqid:
  4312. nfs_release_seqid(data->arg.lock_seqid);
  4313. out_wait:
  4314. nfs4_sequence_done(task, &data->res.seq_res);
  4315. dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
  4316. }
  4317. static void nfs4_lock_done(struct rpc_task *task, void *calldata)
  4318. {
  4319. struct nfs4_lockdata *data = calldata;
  4320. dprintk("%s: begin!\n", __func__);
  4321. if (!nfs4_sequence_done(task, &data->res.seq_res))
  4322. return;
  4323. data->rpc_status = task->tk_status;
  4324. if (data->arg.new_lock_owner != 0) {
  4325. if (data->rpc_status == 0)
  4326. nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
  4327. else
  4328. goto out;
  4329. }
  4330. if (data->rpc_status == 0) {
  4331. nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
  4332. set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
  4333. renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
  4334. }
  4335. out:
  4336. dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
  4337. }
  4338. static void nfs4_lock_release(void *calldata)
  4339. {
  4340. struct nfs4_lockdata *data = calldata;
  4341. dprintk("%s: begin!\n", __func__);
  4342. nfs_free_seqid(data->arg.open_seqid);
  4343. if (data->cancelled != 0) {
  4344. struct rpc_task *task;
  4345. task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
  4346. data->arg.lock_seqid);
  4347. if (!IS_ERR(task))
  4348. rpc_put_task_async(task);
  4349. dprintk("%s: cancelling lock!\n", __func__);
  4350. } else
  4351. nfs_free_seqid(data->arg.lock_seqid);
  4352. nfs4_put_lock_state(data->lsp);
  4353. put_nfs_open_context(data->ctx);
  4354. kfree(data);
  4355. dprintk("%s: done!\n", __func__);
  4356. }
  4357. static const struct rpc_call_ops nfs4_lock_ops = {
  4358. .rpc_call_prepare = nfs4_lock_prepare,
  4359. .rpc_call_done = nfs4_lock_done,
  4360. .rpc_release = nfs4_lock_release,
  4361. };
  4362. static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
  4363. {
  4364. switch (error) {
  4365. case -NFS4ERR_ADMIN_REVOKED:
  4366. case -NFS4ERR_BAD_STATEID:
  4367. lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
  4368. if (new_lock_owner != 0 ||
  4369. test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
  4370. nfs4_schedule_stateid_recovery(server, lsp->ls_state);
  4371. break;
  4372. case -NFS4ERR_STALE_STATEID:
  4373. lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
  4374. case -NFS4ERR_EXPIRED:
  4375. nfs4_schedule_lease_recovery(server->nfs_client);
  4376. };
  4377. }
  4378. static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
  4379. {
  4380. struct nfs4_lockdata *data;
  4381. struct rpc_task *task;
  4382. struct rpc_message msg = {
  4383. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
  4384. .rpc_cred = state->owner->so_cred,
  4385. };
  4386. struct rpc_task_setup task_setup_data = {
  4387. .rpc_client = NFS_CLIENT(state->inode),
  4388. .rpc_message = &msg,
  4389. .callback_ops = &nfs4_lock_ops,
  4390. .workqueue = nfsiod_workqueue,
  4391. .flags = RPC_TASK_ASYNC,
  4392. };
  4393. int ret;
  4394. dprintk("%s: begin!\n", __func__);
  4395. data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
  4396. fl->fl_u.nfs4_fl.owner,
  4397. recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
  4398. if (data == NULL)
  4399. return -ENOMEM;
  4400. if (IS_SETLKW(cmd))
  4401. data->arg.block = 1;
  4402. nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
  4403. msg.rpc_argp = &data->arg;
  4404. msg.rpc_resp = &data->res;
  4405. task_setup_data.callback_data = data;
  4406. if (recovery_type > NFS_LOCK_NEW) {
  4407. if (recovery_type == NFS_LOCK_RECLAIM)
  4408. data->arg.reclaim = NFS_LOCK_RECLAIM;
  4409. nfs4_set_sequence_privileged(&data->arg.seq_args);
  4410. }
  4411. task = rpc_run_task(&task_setup_data);
  4412. if (IS_ERR(task))
  4413. return PTR_ERR(task);
  4414. ret = nfs4_wait_for_completion_rpc_task(task);
  4415. if (ret == 0) {
  4416. ret = data->rpc_status;
  4417. if (ret)
  4418. nfs4_handle_setlk_error(data->server, data->lsp,
  4419. data->arg.new_lock_owner, ret);
  4420. } else
  4421. data->cancelled = 1;
  4422. rpc_put_task(task);
  4423. dprintk("%s: done, ret = %d!\n", __func__, ret);
  4424. return ret;
  4425. }
  4426. static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
  4427. {
  4428. struct nfs_server *server = NFS_SERVER(state->inode);
  4429. struct nfs4_exception exception = {
  4430. .inode = state->inode,
  4431. };
  4432. int err;
  4433. do {
  4434. /* Cache the lock if possible... */
  4435. if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
  4436. return 0;
  4437. err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
  4438. if (err != -NFS4ERR_DELAY)
  4439. break;
  4440. nfs4_handle_exception(server, err, &exception);
  4441. } while (exception.retry);
  4442. return err;
  4443. }
  4444. static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
  4445. {
  4446. struct nfs_server *server = NFS_SERVER(state->inode);
  4447. struct nfs4_exception exception = {
  4448. .inode = state->inode,
  4449. };
  4450. int err;
  4451. err = nfs4_set_lock_state(state, request);
  4452. if (err != 0)
  4453. return err;
  4454. do {
  4455. if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
  4456. return 0;
  4457. err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
  4458. switch (err) {
  4459. default:
  4460. goto out;
  4461. case -NFS4ERR_GRACE:
  4462. case -NFS4ERR_DELAY:
  4463. nfs4_handle_exception(server, err, &exception);
  4464. err = 0;
  4465. }
  4466. } while (exception.retry);
  4467. out:
  4468. return err;
  4469. }
  4470. #if defined(CONFIG_NFS_V4_1)
  4471. /**
  4472. * nfs41_check_expired_locks - possibly free a lock stateid
  4473. *
  4474. * @state: NFSv4 state for an inode
  4475. *
  4476. * Returns NFS_OK if recovery for this stateid is now finished.
  4477. * Otherwise a negative NFS4ERR value is returned.
  4478. */
  4479. static int nfs41_check_expired_locks(struct nfs4_state *state)
  4480. {
  4481. int status, ret = -NFS4ERR_BAD_STATEID;
  4482. struct nfs4_lock_state *lsp;
  4483. struct nfs_server *server = NFS_SERVER(state->inode);
  4484. list_for_each_entry(lsp, &state->lock_states, ls_locks) {
  4485. if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
  4486. status = nfs41_test_stateid(server, &lsp->ls_stateid);
  4487. if (status != NFS_OK) {
  4488. /* Free the stateid unless the server
  4489. * informs us the stateid is unrecognized. */
  4490. if (status != -NFS4ERR_BAD_STATEID)
  4491. nfs41_free_stateid(server,
  4492. &lsp->ls_stateid);
  4493. clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
  4494. ret = status;
  4495. }
  4496. }
  4497. };
  4498. return ret;
  4499. }
  4500. static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
  4501. {
  4502. int status = NFS_OK;
  4503. if (test_bit(LK_STATE_IN_USE, &state->flags))
  4504. status = nfs41_check_expired_locks(state);
  4505. if (status != NFS_OK)
  4506. status = nfs4_lock_expired(state, request);
  4507. return status;
  4508. }
  4509. #endif
  4510. static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  4511. {
  4512. struct nfs4_state_owner *sp = state->owner;
  4513. struct nfs_inode *nfsi = NFS_I(state->inode);
  4514. unsigned char fl_flags = request->fl_flags;
  4515. unsigned int seq;
  4516. int status = -ENOLCK;
  4517. if ((fl_flags & FL_POSIX) &&
  4518. !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
  4519. goto out;
  4520. /* Is this a delegated open? */
  4521. status = nfs4_set_lock_state(state, request);
  4522. if (status != 0)
  4523. goto out;
  4524. request->fl_flags |= FL_ACCESS;
  4525. status = do_vfs_lock(request->fl_file, request);
  4526. if (status < 0)
  4527. goto out;
  4528. down_read(&nfsi->rwsem);
  4529. if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
  4530. /* Yes: cache locks! */
  4531. /* ...but avoid races with delegation recall... */
  4532. request->fl_flags = fl_flags & ~FL_SLEEP;
  4533. status = do_vfs_lock(request->fl_file, request);
  4534. goto out_unlock;
  4535. }
  4536. seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
  4537. up_read(&nfsi->rwsem);
  4538. status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
  4539. if (status != 0)
  4540. goto out;
  4541. down_read(&nfsi->rwsem);
  4542. if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
  4543. status = -NFS4ERR_DELAY;
  4544. goto out_unlock;
  4545. }
  4546. /* Note: we always want to sleep here! */
  4547. request->fl_flags = fl_flags | FL_SLEEP;
  4548. if (do_vfs_lock(request->fl_file, request) < 0)
  4549. printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
  4550. "manager!\n", __func__);
  4551. out_unlock:
  4552. up_read(&nfsi->rwsem);
  4553. out:
  4554. request->fl_flags = fl_flags;
  4555. return status;
  4556. }
  4557. static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  4558. {
  4559. struct nfs4_exception exception = {
  4560. .state = state,
  4561. .inode = state->inode,
  4562. };
  4563. int err;
  4564. do {
  4565. err = _nfs4_proc_setlk(state, cmd, request);
  4566. if (err == -NFS4ERR_DENIED)
  4567. err = -EAGAIN;
  4568. err = nfs4_handle_exception(NFS_SERVER(state->inode),
  4569. err, &exception);
  4570. } while (exception.retry);
  4571. return err;
  4572. }
  4573. static int
  4574. nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
  4575. {
  4576. struct nfs_open_context *ctx;
  4577. struct nfs4_state *state;
  4578. unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
  4579. int status;
  4580. /* verify open state */
  4581. ctx = nfs_file_open_context(filp);
  4582. state = ctx->state;
  4583. if (request->fl_start < 0 || request->fl_end < 0)
  4584. return -EINVAL;
  4585. if (IS_GETLK(cmd)) {
  4586. if (state != NULL)
  4587. return nfs4_proc_getlk(state, F_GETLK, request);
  4588. return 0;
  4589. }
  4590. if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
  4591. return -EINVAL;
  4592. if (request->fl_type == F_UNLCK) {
  4593. if (state != NULL)
  4594. return nfs4_proc_unlck(state, cmd, request);
  4595. return 0;
  4596. }
  4597. if (state == NULL)
  4598. return -ENOLCK;
  4599. /*
  4600. * Don't rely on the VFS having checked the file open mode,
  4601. * since it won't do this for flock() locks.
  4602. */
  4603. switch (request->fl_type) {
  4604. case F_RDLCK:
  4605. if (!(filp->f_mode & FMODE_READ))
  4606. return -EBADF;
  4607. break;
  4608. case F_WRLCK:
  4609. if (!(filp->f_mode & FMODE_WRITE))
  4610. return -EBADF;
  4611. }
  4612. do {
  4613. status = nfs4_proc_setlk(state, cmd, request);
  4614. if ((status != -EAGAIN) || IS_SETLK(cmd))
  4615. break;
  4616. timeout = nfs4_set_lock_task_retry(timeout);
  4617. status = -ERESTARTSYS;
  4618. if (signalled())
  4619. break;
  4620. } while(status < 0);
  4621. return status;
  4622. }
  4623. int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
  4624. {
  4625. struct nfs_server *server = NFS_SERVER(state->inode);
  4626. struct nfs4_exception exception = { };
  4627. int err;
  4628. err = nfs4_set_lock_state(state, fl);
  4629. if (err != 0)
  4630. goto out;
  4631. do {
  4632. err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
  4633. switch (err) {
  4634. default:
  4635. printk(KERN_ERR "NFS: %s: unhandled error "
  4636. "%d.\n", __func__, err);
  4637. case 0:
  4638. case -ESTALE:
  4639. goto out;
  4640. case -NFS4ERR_STALE_CLIENTID:
  4641. case -NFS4ERR_STALE_STATEID:
  4642. set_bit(NFS_DELEGATED_STATE, &state->flags);
  4643. case -NFS4ERR_EXPIRED:
  4644. nfs4_schedule_lease_recovery(server->nfs_client);
  4645. err = -EAGAIN;
  4646. goto out;
  4647. case -NFS4ERR_BADSESSION:
  4648. case -NFS4ERR_BADSLOT:
  4649. case -NFS4ERR_BAD_HIGH_SLOT:
  4650. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  4651. case -NFS4ERR_DEADSESSION:
  4652. set_bit(NFS_DELEGATED_STATE, &state->flags);
  4653. nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
  4654. err = -EAGAIN;
  4655. goto out;
  4656. case -NFS4ERR_DELEG_REVOKED:
  4657. case -NFS4ERR_ADMIN_REVOKED:
  4658. case -NFS4ERR_BAD_STATEID:
  4659. case -NFS4ERR_OPENMODE:
  4660. nfs4_schedule_stateid_recovery(server, state);
  4661. err = 0;
  4662. goto out;
  4663. case -NFS4ERR_DELAY:
  4664. case -NFS4ERR_GRACE:
  4665. set_bit(NFS_DELEGATED_STATE, &state->flags);
  4666. ssleep(1);
  4667. err = -EAGAIN;
  4668. goto out;
  4669. case -ENOMEM:
  4670. case -NFS4ERR_DENIED:
  4671. /* kill_proc(fl->fl_pid, SIGLOST, 1); */
  4672. err = 0;
  4673. goto out;
  4674. }
  4675. set_bit(NFS_DELEGATED_STATE, &state->flags);
  4676. err = nfs4_handle_exception(server, err, &exception);
  4677. } while (exception.retry);
  4678. out:
  4679. return err;
  4680. }
  4681. struct nfs_release_lockowner_data {
  4682. struct nfs4_lock_state *lsp;
  4683. struct nfs_server *server;
  4684. struct nfs_release_lockowner_args args;
  4685. };
  4686. static void nfs4_release_lockowner_release(void *calldata)
  4687. {
  4688. struct nfs_release_lockowner_data *data = calldata;
  4689. nfs4_free_lock_state(data->server, data->lsp);
  4690. kfree(calldata);
  4691. }
  4692. static const struct rpc_call_ops nfs4_release_lockowner_ops = {
  4693. .rpc_release = nfs4_release_lockowner_release,
  4694. };
  4695. int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
  4696. {
  4697. struct nfs_server *server = lsp->ls_state->owner->so_server;
  4698. struct nfs_release_lockowner_data *data;
  4699. struct rpc_message msg = {
  4700. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
  4701. };
  4702. if (server->nfs_client->cl_mvops->minor_version != 0)
  4703. return -EINVAL;
  4704. data = kmalloc(sizeof(*data), GFP_NOFS);
  4705. if (!data)
  4706. return -ENOMEM;
  4707. data->lsp = lsp;
  4708. data->server = server;
  4709. data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
  4710. data->args.lock_owner.id = lsp->ls_seqid.owner_id;
  4711. data->args.lock_owner.s_dev = server->s_dev;
  4712. msg.rpc_argp = &data->args;
  4713. rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
  4714. return 0;
  4715. }
  4716. #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
  4717. static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
  4718. const void *buf, size_t buflen,
  4719. int flags, int type)
  4720. {
  4721. if (strcmp(key, "") != 0)
  4722. return -EINVAL;
  4723. return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
  4724. }
  4725. static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
  4726. void *buf, size_t buflen, int type)
  4727. {
  4728. if (strcmp(key, "") != 0)
  4729. return -EINVAL;
  4730. return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
  4731. }
  4732. static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
  4733. size_t list_len, const char *name,
  4734. size_t name_len, int type)
  4735. {
  4736. size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
  4737. if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
  4738. return 0;
  4739. if (list && len <= list_len)
  4740. memcpy(list, XATTR_NAME_NFSV4_ACL, len);
  4741. return len;
  4742. }
  4743. /*
  4744. * nfs_fhget will use either the mounted_on_fileid or the fileid
  4745. */
  4746. static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
  4747. {
  4748. if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
  4749. (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
  4750. (fattr->valid & NFS_ATTR_FATTR_FSID) &&
  4751. (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
  4752. return;
  4753. fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
  4754. NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
  4755. fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
  4756. fattr->nlink = 2;
  4757. }
  4758. static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
  4759. const struct qstr *name,
  4760. struct nfs4_fs_locations *fs_locations,
  4761. struct page *page)
  4762. {
  4763. struct nfs_server *server = NFS_SERVER(dir);
  4764. u32 bitmask[2] = {
  4765. [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
  4766. };
  4767. struct nfs4_fs_locations_arg args = {
  4768. .dir_fh = NFS_FH(dir),
  4769. .name = name,
  4770. .page = page,
  4771. .bitmask = bitmask,
  4772. };
  4773. struct nfs4_fs_locations_res res = {
  4774. .fs_locations = fs_locations,
  4775. };
  4776. struct rpc_message msg = {
  4777. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
  4778. .rpc_argp = &args,
  4779. .rpc_resp = &res,
  4780. };
  4781. int status;
  4782. dprintk("%s: start\n", __func__);
  4783. /* Ask for the fileid of the absent filesystem if mounted_on_fileid
  4784. * is not supported */
  4785. if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
  4786. bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
  4787. else
  4788. bitmask[0] |= FATTR4_WORD0_FILEID;
  4789. nfs_fattr_init(&fs_locations->fattr);
  4790. fs_locations->server = server;
  4791. fs_locations->nlocations = 0;
  4792. status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
  4793. dprintk("%s: returned status = %d\n", __func__, status);
  4794. return status;
  4795. }
  4796. int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
  4797. const struct qstr *name,
  4798. struct nfs4_fs_locations *fs_locations,
  4799. struct page *page)
  4800. {
  4801. struct nfs4_exception exception = { };
  4802. int err;
  4803. do {
  4804. err = nfs4_handle_exception(NFS_SERVER(dir),
  4805. _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
  4806. &exception);
  4807. } while (exception.retry);
  4808. return err;
  4809. }
  4810. static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
  4811. {
  4812. int status;
  4813. struct nfs4_secinfo_arg args = {
  4814. .dir_fh = NFS_FH(dir),
  4815. .name = name,
  4816. };
  4817. struct nfs4_secinfo_res res = {
  4818. .flavors = flavors,
  4819. };
  4820. struct rpc_message msg = {
  4821. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
  4822. .rpc_argp = &args,
  4823. .rpc_resp = &res,
  4824. };
  4825. dprintk("NFS call secinfo %s\n", name->name);
  4826. status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
  4827. dprintk("NFS reply secinfo: %d\n", status);
  4828. return status;
  4829. }
  4830. int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
  4831. struct nfs4_secinfo_flavors *flavors)
  4832. {
  4833. struct nfs4_exception exception = { };
  4834. int err;
  4835. do {
  4836. err = nfs4_handle_exception(NFS_SERVER(dir),
  4837. _nfs4_proc_secinfo(dir, name, flavors),
  4838. &exception);
  4839. } while (exception.retry);
  4840. return err;
  4841. }
  4842. #ifdef CONFIG_NFS_V4_1
  4843. /*
  4844. * Check the exchange flags returned by the server for invalid flags, having
  4845. * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
  4846. * DS flags set.
  4847. */
  4848. static int nfs4_check_cl_exchange_flags(u32 flags)
  4849. {
  4850. if (flags & ~EXCHGID4_FLAG_MASK_R)
  4851. goto out_inval;
  4852. if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
  4853. (flags & EXCHGID4_FLAG_USE_NON_PNFS))
  4854. goto out_inval;
  4855. if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
  4856. goto out_inval;
  4857. return NFS_OK;
  4858. out_inval:
  4859. return -NFS4ERR_INVAL;
  4860. }
  4861. static bool
  4862. nfs41_same_server_scope(struct nfs41_server_scope *a,
  4863. struct nfs41_server_scope *b)
  4864. {
  4865. if (a->server_scope_sz == b->server_scope_sz &&
  4866. memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
  4867. return true;
  4868. return false;
  4869. }
  4870. /*
  4871. * nfs4_proc_bind_conn_to_session()
  4872. *
  4873. * The 4.1 client currently uses the same TCP connection for the
  4874. * fore and backchannel.
  4875. */
  4876. int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
  4877. {
  4878. int status;
  4879. struct nfs41_bind_conn_to_session_res res;
  4880. struct rpc_message msg = {
  4881. .rpc_proc =
  4882. &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
  4883. .rpc_argp = clp,
  4884. .rpc_resp = &res,
  4885. .rpc_cred = cred,
  4886. };
  4887. dprintk("--> %s\n", __func__);
  4888. res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
  4889. if (unlikely(res.session == NULL)) {
  4890. status = -ENOMEM;
  4891. goto out;
  4892. }
  4893. status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  4894. if (status == 0) {
  4895. if (memcmp(res.session->sess_id.data,
  4896. clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
  4897. dprintk("NFS: %s: Session ID mismatch\n", __func__);
  4898. status = -EIO;
  4899. goto out_session;
  4900. }
  4901. if (res.dir != NFS4_CDFS4_BOTH) {
  4902. dprintk("NFS: %s: Unexpected direction from server\n",
  4903. __func__);
  4904. status = -EIO;
  4905. goto out_session;
  4906. }
  4907. if (res.use_conn_in_rdma_mode) {
  4908. dprintk("NFS: %s: Server returned RDMA mode = true\n",
  4909. __func__);
  4910. status = -EIO;
  4911. goto out_session;
  4912. }
  4913. }
  4914. out_session:
  4915. kfree(res.session);
  4916. out:
  4917. dprintk("<-- %s status= %d\n", __func__, status);
  4918. return status;
  4919. }
  4920. /*
  4921. * nfs4_proc_exchange_id()
  4922. *
  4923. * Returns zero, a negative errno, or a negative NFS4ERR status code.
  4924. *
  4925. * Since the clientid has expired, all compounds using sessions
  4926. * associated with the stale clientid will be returning
  4927. * NFS4ERR_BADSESSION in the sequence operation, and will therefore
  4928. * be in some phase of session reset.
  4929. */
  4930. int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
  4931. {
  4932. nfs4_verifier verifier;
  4933. struct nfs41_exchange_id_args args = {
  4934. .verifier = &verifier,
  4935. .client = clp,
  4936. .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
  4937. };
  4938. struct nfs41_exchange_id_res res = {
  4939. 0
  4940. };
  4941. int status;
  4942. struct rpc_message msg = {
  4943. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
  4944. .rpc_argp = &args,
  4945. .rpc_resp = &res,
  4946. .rpc_cred = cred,
  4947. };
  4948. nfs4_init_boot_verifier(clp, &verifier);
  4949. args.id_len = nfs4_init_uniform_client_string(clp, args.id,
  4950. sizeof(args.id));
  4951. dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
  4952. clp->cl_rpcclient->cl_auth->au_ops->au_name,
  4953. args.id_len, args.id);
  4954. res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
  4955. GFP_NOFS);
  4956. if (unlikely(res.server_owner == NULL)) {
  4957. status = -ENOMEM;
  4958. goto out;
  4959. }
  4960. res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
  4961. GFP_NOFS);
  4962. if (unlikely(res.server_scope == NULL)) {
  4963. status = -ENOMEM;
  4964. goto out_server_owner;
  4965. }
  4966. res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
  4967. if (unlikely(res.impl_id == NULL)) {
  4968. status = -ENOMEM;
  4969. goto out_server_scope;
  4970. }
  4971. status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  4972. if (status == 0)
  4973. status = nfs4_check_cl_exchange_flags(res.flags);
  4974. if (status == 0) {
  4975. clp->cl_clientid = res.clientid;
  4976. clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
  4977. if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
  4978. clp->cl_seqid = res.seqid;
  4979. kfree(clp->cl_serverowner);
  4980. clp->cl_serverowner = res.server_owner;
  4981. res.server_owner = NULL;
  4982. /* use the most recent implementation id */
  4983. kfree(clp->cl_implid);
  4984. clp->cl_implid = res.impl_id;
  4985. if (clp->cl_serverscope != NULL &&
  4986. !nfs41_same_server_scope(clp->cl_serverscope,
  4987. res.server_scope)) {
  4988. dprintk("%s: server_scope mismatch detected\n",
  4989. __func__);
  4990. set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
  4991. kfree(clp->cl_serverscope);
  4992. clp->cl_serverscope = NULL;
  4993. }
  4994. if (clp->cl_serverscope == NULL) {
  4995. clp->cl_serverscope = res.server_scope;
  4996. goto out;
  4997. }
  4998. } else
  4999. kfree(res.impl_id);
  5000. out_server_owner:
  5001. kfree(res.server_owner);
  5002. out_server_scope:
  5003. kfree(res.server_scope);
  5004. out:
  5005. if (clp->cl_implid != NULL)
  5006. dprintk("NFS reply exchange_id: Server Implementation ID: "
  5007. "domain: %s, name: %s, date: %llu,%u\n",
  5008. clp->cl_implid->domain, clp->cl_implid->name,
  5009. clp->cl_implid->date.seconds,
  5010. clp->cl_implid->date.nseconds);
  5011. dprintk("NFS reply exchange_id: %d\n", status);
  5012. return status;
  5013. }
  5014. static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
  5015. struct rpc_cred *cred)
  5016. {
  5017. struct rpc_message msg = {
  5018. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
  5019. .rpc_argp = clp,
  5020. .rpc_cred = cred,
  5021. };
  5022. int status;
  5023. status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  5024. if (status)
  5025. dprintk("NFS: Got error %d from the server %s on "
  5026. "DESTROY_CLIENTID.", status, clp->cl_hostname);
  5027. return status;
  5028. }
  5029. static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
  5030. struct rpc_cred *cred)
  5031. {
  5032. unsigned int loop;
  5033. int ret;
  5034. for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
  5035. ret = _nfs4_proc_destroy_clientid(clp, cred);
  5036. switch (ret) {
  5037. case -NFS4ERR_DELAY:
  5038. case -NFS4ERR_CLIENTID_BUSY:
  5039. ssleep(1);
  5040. break;
  5041. default:
  5042. return ret;
  5043. }
  5044. }
  5045. return 0;
  5046. }
  5047. int nfs4_destroy_clientid(struct nfs_client *clp)
  5048. {
  5049. struct rpc_cred *cred;
  5050. int ret = 0;
  5051. if (clp->cl_mvops->minor_version < 1)
  5052. goto out;
  5053. if (clp->cl_exchange_flags == 0)
  5054. goto out;
  5055. if (clp->cl_preserve_clid)
  5056. goto out;
  5057. cred = nfs4_get_exchange_id_cred(clp);
  5058. ret = nfs4_proc_destroy_clientid(clp, cred);
  5059. if (cred)
  5060. put_rpccred(cred);
  5061. switch (ret) {
  5062. case 0:
  5063. case -NFS4ERR_STALE_CLIENTID:
  5064. clp->cl_exchange_flags = 0;
  5065. }
  5066. out:
  5067. return ret;
  5068. }
  5069. struct nfs4_get_lease_time_data {
  5070. struct nfs4_get_lease_time_args *args;
  5071. struct nfs4_get_lease_time_res *res;
  5072. struct nfs_client *clp;
  5073. };
  5074. static void nfs4_get_lease_time_prepare(struct rpc_task *task,
  5075. void *calldata)
  5076. {
  5077. struct nfs4_get_lease_time_data *data =
  5078. (struct nfs4_get_lease_time_data *)calldata;
  5079. dprintk("--> %s\n", __func__);
  5080. /* just setup sequence, do not trigger session recovery
  5081. since we're invoked within one */
  5082. nfs41_setup_sequence(data->clp->cl_session,
  5083. &data->args->la_seq_args,
  5084. &data->res->lr_seq_res,
  5085. task);
  5086. dprintk("<-- %s\n", __func__);
  5087. }
  5088. /*
  5089. * Called from nfs4_state_manager thread for session setup, so don't recover
  5090. * from sequence operation or clientid errors.
  5091. */
  5092. static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
  5093. {
  5094. struct nfs4_get_lease_time_data *data =
  5095. (struct nfs4_get_lease_time_data *)calldata;
  5096. dprintk("--> %s\n", __func__);
  5097. if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
  5098. return;
  5099. switch (task->tk_status) {
  5100. case -NFS4ERR_DELAY:
  5101. case -NFS4ERR_GRACE:
  5102. dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
  5103. rpc_delay(task, NFS4_POLL_RETRY_MIN);
  5104. task->tk_status = 0;
  5105. /* fall through */
  5106. case -NFS4ERR_RETRY_UNCACHED_REP:
  5107. rpc_restart_call_prepare(task);
  5108. return;
  5109. }
  5110. dprintk("<-- %s\n", __func__);
  5111. }
  5112. static const struct rpc_call_ops nfs4_get_lease_time_ops = {
  5113. .rpc_call_prepare = nfs4_get_lease_time_prepare,
  5114. .rpc_call_done = nfs4_get_lease_time_done,
  5115. };
  5116. int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
  5117. {
  5118. struct rpc_task *task;
  5119. struct nfs4_get_lease_time_args args;
  5120. struct nfs4_get_lease_time_res res = {
  5121. .lr_fsinfo = fsinfo,
  5122. };
  5123. struct nfs4_get_lease_time_data data = {
  5124. .args = &args,
  5125. .res = &res,
  5126. .clp = clp,
  5127. };
  5128. struct rpc_message msg = {
  5129. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
  5130. .rpc_argp = &args,
  5131. .rpc_resp = &res,
  5132. };
  5133. struct rpc_task_setup task_setup = {
  5134. .rpc_client = clp->cl_rpcclient,
  5135. .rpc_message = &msg,
  5136. .callback_ops = &nfs4_get_lease_time_ops,
  5137. .callback_data = &data,
  5138. .flags = RPC_TASK_TIMEOUT,
  5139. };
  5140. int status;
  5141. nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
  5142. nfs4_set_sequence_privileged(&args.la_seq_args);
  5143. dprintk("--> %s\n", __func__);
  5144. task = rpc_run_task(&task_setup);
  5145. if (IS_ERR(task))
  5146. status = PTR_ERR(task);
  5147. else {
  5148. status = task->tk_status;
  5149. rpc_put_task(task);
  5150. }
  5151. dprintk("<-- %s return %d\n", __func__, status);
  5152. return status;
  5153. }
  5154. /*
  5155. * Initialize the values to be used by the client in CREATE_SESSION
  5156. * If nfs4_init_session set the fore channel request and response sizes,
  5157. * use them.
  5158. *
  5159. * Set the back channel max_resp_sz_cached to zero to force the client to
  5160. * always set csa_cachethis to FALSE because the current implementation
  5161. * of the back channel DRC only supports caching the CB_SEQUENCE operation.
  5162. */
  5163. static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
  5164. {
  5165. struct nfs4_session *session = args->client->cl_session;
  5166. unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
  5167. mxresp_sz = session->fc_target_max_resp_sz;
  5168. if (mxrqst_sz == 0)
  5169. mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
  5170. if (mxresp_sz == 0)
  5171. mxresp_sz = NFS_MAX_FILE_IO_SIZE;
  5172. /* Fore channel attributes */
  5173. args->fc_attrs.max_rqst_sz = mxrqst_sz;
  5174. args->fc_attrs.max_resp_sz = mxresp_sz;
  5175. args->fc_attrs.max_ops = NFS4_MAX_OPS;
  5176. args->fc_attrs.max_reqs = max_session_slots;
  5177. dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
  5178. "max_ops=%u max_reqs=%u\n",
  5179. __func__,
  5180. args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
  5181. args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
  5182. /* Back channel attributes */
  5183. args->bc_attrs.max_rqst_sz = PAGE_SIZE;
  5184. args->bc_attrs.max_resp_sz = PAGE_SIZE;
  5185. args->bc_attrs.max_resp_sz_cached = 0;
  5186. args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
  5187. args->bc_attrs.max_reqs = 1;
  5188. dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
  5189. "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
  5190. __func__,
  5191. args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
  5192. args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
  5193. args->bc_attrs.max_reqs);
  5194. }
  5195. static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
  5196. {
  5197. struct nfs4_channel_attrs *sent = &args->fc_attrs;
  5198. struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
  5199. if (rcvd->max_resp_sz > sent->max_resp_sz)
  5200. return -EINVAL;
  5201. /*
  5202. * Our requested max_ops is the minimum we need; we're not
  5203. * prepared to break up compounds into smaller pieces than that.
  5204. * So, no point even trying to continue if the server won't
  5205. * cooperate:
  5206. */
  5207. if (rcvd->max_ops < sent->max_ops)
  5208. return -EINVAL;
  5209. if (rcvd->max_reqs == 0)
  5210. return -EINVAL;
  5211. if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
  5212. rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
  5213. return 0;
  5214. }
  5215. static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
  5216. {
  5217. struct nfs4_channel_attrs *sent = &args->bc_attrs;
  5218. struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
  5219. if (rcvd->max_rqst_sz > sent->max_rqst_sz)
  5220. return -EINVAL;
  5221. if (rcvd->max_resp_sz < sent->max_resp_sz)
  5222. return -EINVAL;
  5223. if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
  5224. return -EINVAL;
  5225. /* These would render the backchannel useless: */
  5226. if (rcvd->max_ops != sent->max_ops)
  5227. return -EINVAL;
  5228. if (rcvd->max_reqs != sent->max_reqs)
  5229. return -EINVAL;
  5230. return 0;
  5231. }
  5232. static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
  5233. struct nfs4_session *session)
  5234. {
  5235. int ret;
  5236. ret = nfs4_verify_fore_channel_attrs(args, session);
  5237. if (ret)
  5238. return ret;
  5239. return nfs4_verify_back_channel_attrs(args, session);
  5240. }
  5241. static int _nfs4_proc_create_session(struct nfs_client *clp,
  5242. struct rpc_cred *cred)
  5243. {
  5244. struct nfs4_session *session = clp->cl_session;
  5245. struct nfs41_create_session_args args = {
  5246. .client = clp,
  5247. .cb_program = NFS4_CALLBACK,
  5248. };
  5249. struct nfs41_create_session_res res = {
  5250. .client = clp,
  5251. };
  5252. struct rpc_message msg = {
  5253. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
  5254. .rpc_argp = &args,
  5255. .rpc_resp = &res,
  5256. .rpc_cred = cred,
  5257. };
  5258. int status;
  5259. nfs4_init_channel_attrs(&args);
  5260. args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
  5261. status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  5262. if (!status) {
  5263. /* Verify the session's negotiated channel_attrs values */
  5264. status = nfs4_verify_channel_attrs(&args, session);
  5265. /* Increment the clientid slot sequence id */
  5266. clp->cl_seqid++;
  5267. }
  5268. return status;
  5269. }
  5270. /*
  5271. * Issues a CREATE_SESSION operation to the server.
  5272. * It is the responsibility of the caller to verify the session is
  5273. * expired before calling this routine.
  5274. */
  5275. int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
  5276. {
  5277. int status;
  5278. unsigned *ptr;
  5279. struct nfs4_session *session = clp->cl_session;
  5280. dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
  5281. status = _nfs4_proc_create_session(clp, cred);
  5282. if (status)
  5283. goto out;
  5284. /* Init or reset the session slot tables */
  5285. status = nfs4_setup_session_slot_tables(session);
  5286. dprintk("slot table setup returned %d\n", status);
  5287. if (status)
  5288. goto out;
  5289. ptr = (unsigned *)&session->sess_id.data[0];
  5290. dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
  5291. clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
  5292. out:
  5293. dprintk("<-- %s\n", __func__);
  5294. return status;
  5295. }
  5296. /*
  5297. * Issue the over-the-wire RPC DESTROY_SESSION.
  5298. * The caller must serialize access to this routine.
  5299. */
  5300. int nfs4_proc_destroy_session(struct nfs4_session *session,
  5301. struct rpc_cred *cred)
  5302. {
  5303. struct rpc_message msg = {
  5304. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
  5305. .rpc_argp = session,
  5306. .rpc_cred = cred,
  5307. };
  5308. int status = 0;
  5309. dprintk("--> nfs4_proc_destroy_session\n");
  5310. /* session is still being setup */
  5311. if (session->clp->cl_cons_state != NFS_CS_READY)
  5312. return status;
  5313. status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  5314. if (status)
  5315. dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
  5316. "Session has been destroyed regardless...\n", status);
  5317. dprintk("<-- nfs4_proc_destroy_session\n");
  5318. return status;
  5319. }
  5320. /*
  5321. * Renew the cl_session lease.
  5322. */
  5323. struct nfs4_sequence_data {
  5324. struct nfs_client *clp;
  5325. struct nfs4_sequence_args args;
  5326. struct nfs4_sequence_res res;
  5327. };
  5328. static void nfs41_sequence_release(void *data)
  5329. {
  5330. struct nfs4_sequence_data *calldata = data;
  5331. struct nfs_client *clp = calldata->clp;
  5332. if (atomic_read(&clp->cl_count) > 1)
  5333. nfs4_schedule_state_renewal(clp);
  5334. nfs_put_client(clp);
  5335. kfree(calldata);
  5336. }
  5337. static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
  5338. {
  5339. switch(task->tk_status) {
  5340. case -NFS4ERR_DELAY:
  5341. rpc_delay(task, NFS4_POLL_RETRY_MAX);
  5342. return -EAGAIN;
  5343. default:
  5344. nfs4_schedule_lease_recovery(clp);
  5345. }
  5346. return 0;
  5347. }
  5348. static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
  5349. {
  5350. struct nfs4_sequence_data *calldata = data;
  5351. struct nfs_client *clp = calldata->clp;
  5352. if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
  5353. return;
  5354. if (task->tk_status < 0) {
  5355. dprintk("%s ERROR %d\n", __func__, task->tk_status);
  5356. if (atomic_read(&clp->cl_count) == 1)
  5357. goto out;
  5358. if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
  5359. rpc_restart_call_prepare(task);
  5360. return;
  5361. }
  5362. }
  5363. dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
  5364. out:
  5365. dprintk("<-- %s\n", __func__);
  5366. }
  5367. static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
  5368. {
  5369. struct nfs4_sequence_data *calldata = data;
  5370. struct nfs_client *clp = calldata->clp;
  5371. struct nfs4_sequence_args *args;
  5372. struct nfs4_sequence_res *res;
  5373. args = task->tk_msg.rpc_argp;
  5374. res = task->tk_msg.rpc_resp;
  5375. nfs41_setup_sequence(clp->cl_session, args, res, task);
  5376. }
  5377. static const struct rpc_call_ops nfs41_sequence_ops = {
  5378. .rpc_call_done = nfs41_sequence_call_done,
  5379. .rpc_call_prepare = nfs41_sequence_prepare,
  5380. .rpc_release = nfs41_sequence_release,
  5381. };
  5382. static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
  5383. struct rpc_cred *cred,
  5384. bool is_privileged)
  5385. {
  5386. struct nfs4_sequence_data *calldata;
  5387. struct rpc_message msg = {
  5388. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
  5389. .rpc_cred = cred,
  5390. };
  5391. struct rpc_task_setup task_setup_data = {
  5392. .rpc_client = clp->cl_rpcclient,
  5393. .rpc_message = &msg,
  5394. .callback_ops = &nfs41_sequence_ops,
  5395. .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
  5396. };
  5397. if (!atomic_inc_not_zero(&clp->cl_count))
  5398. return ERR_PTR(-EIO);
  5399. calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
  5400. if (calldata == NULL) {
  5401. nfs_put_client(clp);
  5402. return ERR_PTR(-ENOMEM);
  5403. }
  5404. nfs41_init_sequence(&calldata->args, &calldata->res, 0);
  5405. if (is_privileged)
  5406. nfs4_set_sequence_privileged(&calldata->args);
  5407. msg.rpc_argp = &calldata->args;
  5408. msg.rpc_resp = &calldata->res;
  5409. calldata->clp = clp;
  5410. task_setup_data.callback_data = calldata;
  5411. return rpc_run_task(&task_setup_data);
  5412. }
  5413. static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
  5414. {
  5415. struct rpc_task *task;
  5416. int ret = 0;
  5417. if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
  5418. return 0;
  5419. task = _nfs41_proc_sequence(clp, cred, false);
  5420. if (IS_ERR(task))
  5421. ret = PTR_ERR(task);
  5422. else
  5423. rpc_put_task_async(task);
  5424. dprintk("<-- %s status=%d\n", __func__, ret);
  5425. return ret;
  5426. }
  5427. static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
  5428. {
  5429. struct rpc_task *task;
  5430. int ret;
  5431. task = _nfs41_proc_sequence(clp, cred, true);
  5432. if (IS_ERR(task)) {
  5433. ret = PTR_ERR(task);
  5434. goto out;
  5435. }
  5436. ret = rpc_wait_for_completion_task(task);
  5437. if (!ret) {
  5438. struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
  5439. if (task->tk_status == 0)
  5440. nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
  5441. ret = task->tk_status;
  5442. }
  5443. rpc_put_task(task);
  5444. out:
  5445. dprintk("<-- %s status=%d\n", __func__, ret);
  5446. return ret;
  5447. }
  5448. struct nfs4_reclaim_complete_data {
  5449. struct nfs_client *clp;
  5450. struct nfs41_reclaim_complete_args arg;
  5451. struct nfs41_reclaim_complete_res res;
  5452. };
  5453. static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
  5454. {
  5455. struct nfs4_reclaim_complete_data *calldata = data;
  5456. nfs41_setup_sequence(calldata->clp->cl_session,
  5457. &calldata->arg.seq_args,
  5458. &calldata->res.seq_res,
  5459. task);
  5460. }
  5461. static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
  5462. {
  5463. switch(task->tk_status) {
  5464. case 0:
  5465. case -NFS4ERR_COMPLETE_ALREADY:
  5466. case -NFS4ERR_WRONG_CRED: /* What to do here? */
  5467. break;
  5468. case -NFS4ERR_DELAY:
  5469. rpc_delay(task, NFS4_POLL_RETRY_MAX);
  5470. /* fall through */
  5471. case -NFS4ERR_RETRY_UNCACHED_REP:
  5472. return -EAGAIN;
  5473. default:
  5474. nfs4_schedule_lease_recovery(clp);
  5475. }
  5476. return 0;
  5477. }
  5478. static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
  5479. {
  5480. struct nfs4_reclaim_complete_data *calldata = data;
  5481. struct nfs_client *clp = calldata->clp;
  5482. struct nfs4_sequence_res *res = &calldata->res.seq_res;
  5483. dprintk("--> %s\n", __func__);
  5484. if (!nfs41_sequence_done(task, res))
  5485. return;
  5486. if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
  5487. rpc_restart_call_prepare(task);
  5488. return;
  5489. }
  5490. dprintk("<-- %s\n", __func__);
  5491. }
  5492. static void nfs4_free_reclaim_complete_data(void *data)
  5493. {
  5494. struct nfs4_reclaim_complete_data *calldata = data;
  5495. kfree(calldata);
  5496. }
  5497. static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
  5498. .rpc_call_prepare = nfs4_reclaim_complete_prepare,
  5499. .rpc_call_done = nfs4_reclaim_complete_done,
  5500. .rpc_release = nfs4_free_reclaim_complete_data,
  5501. };
  5502. /*
  5503. * Issue a global reclaim complete.
  5504. */
  5505. static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
  5506. {
  5507. struct nfs4_reclaim_complete_data *calldata;
  5508. struct rpc_task *task;
  5509. struct rpc_message msg = {
  5510. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
  5511. };
  5512. struct rpc_task_setup task_setup_data = {
  5513. .rpc_client = clp->cl_rpcclient,
  5514. .rpc_message = &msg,
  5515. .callback_ops = &nfs4_reclaim_complete_call_ops,
  5516. .flags = RPC_TASK_ASYNC,
  5517. };
  5518. int status = -ENOMEM;
  5519. dprintk("--> %s\n", __func__);
  5520. calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
  5521. if (calldata == NULL)
  5522. goto out;
  5523. calldata->clp = clp;
  5524. calldata->arg.one_fs = 0;
  5525. nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
  5526. nfs4_set_sequence_privileged(&calldata->arg.seq_args);
  5527. msg.rpc_argp = &calldata->arg;
  5528. msg.rpc_resp = &calldata->res;
  5529. task_setup_data.callback_data = calldata;
  5530. task = rpc_run_task(&task_setup_data);
  5531. if (IS_ERR(task)) {
  5532. status = PTR_ERR(task);
  5533. goto out;
  5534. }
  5535. status = nfs4_wait_for_completion_rpc_task(task);
  5536. if (status == 0)
  5537. status = task->tk_status;
  5538. rpc_put_task(task);
  5539. return 0;
  5540. out:
  5541. dprintk("<-- %s status=%d\n", __func__, status);
  5542. return status;
  5543. }
  5544. static void
  5545. nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
  5546. {
  5547. struct nfs4_layoutget *lgp = calldata;
  5548. struct nfs_server *server = NFS_SERVER(lgp->args.inode);
  5549. struct nfs4_session *session = nfs4_get_session(server);
  5550. dprintk("--> %s\n", __func__);
  5551. /* Note the is a race here, where a CB_LAYOUTRECALL can come in
  5552. * right now covering the LAYOUTGET we are about to send.
  5553. * However, that is not so catastrophic, and there seems
  5554. * to be no way to prevent it completely.
  5555. */
  5556. if (nfs41_setup_sequence(session, &lgp->args.seq_args,
  5557. &lgp->res.seq_res, task))
  5558. return;
  5559. if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
  5560. NFS_I(lgp->args.inode)->layout,
  5561. lgp->args.ctx->state)) {
  5562. rpc_exit(task, NFS4_OK);
  5563. }
  5564. }
  5565. static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
  5566. {
  5567. struct nfs4_layoutget *lgp = calldata;
  5568. struct inode *inode = lgp->args.inode;
  5569. struct nfs_server *server = NFS_SERVER(inode);
  5570. struct pnfs_layout_hdr *lo;
  5571. struct nfs4_state *state = NULL;
  5572. unsigned long timeo, giveup;
  5573. dprintk("--> %s\n", __func__);
  5574. if (!nfs41_sequence_done(task, &lgp->res.seq_res))
  5575. goto out;
  5576. switch (task->tk_status) {
  5577. case 0:
  5578. goto out;
  5579. case -NFS4ERR_LAYOUTTRYLATER:
  5580. case -NFS4ERR_RECALLCONFLICT:
  5581. timeo = rpc_get_timeout(task->tk_client);
  5582. giveup = lgp->args.timestamp + timeo;
  5583. if (time_after(giveup, jiffies))
  5584. task->tk_status = -NFS4ERR_DELAY;
  5585. break;
  5586. case -NFS4ERR_EXPIRED:
  5587. case -NFS4ERR_BAD_STATEID:
  5588. spin_lock(&inode->i_lock);
  5589. lo = NFS_I(inode)->layout;
  5590. if (!lo || list_empty(&lo->plh_segs)) {
  5591. spin_unlock(&inode->i_lock);
  5592. /* If the open stateid was bad, then recover it. */
  5593. state = lgp->args.ctx->state;
  5594. } else {
  5595. LIST_HEAD(head);
  5596. pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
  5597. spin_unlock(&inode->i_lock);
  5598. /* Mark the bad layout state as invalid, then
  5599. * retry using the open stateid. */
  5600. pnfs_free_lseg_list(&head);
  5601. }
  5602. }
  5603. if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
  5604. rpc_restart_call_prepare(task);
  5605. out:
  5606. dprintk("<-- %s\n", __func__);
  5607. }
  5608. static size_t max_response_pages(struct nfs_server *server)
  5609. {
  5610. u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
  5611. return nfs_page_array_len(0, max_resp_sz);
  5612. }
  5613. static void nfs4_free_pages(struct page **pages, size_t size)
  5614. {
  5615. int i;
  5616. if (!pages)
  5617. return;
  5618. for (i = 0; i < size; i++) {
  5619. if (!pages[i])
  5620. break;
  5621. __free_page(pages[i]);
  5622. }
  5623. kfree(pages);
  5624. }
  5625. static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
  5626. {
  5627. struct page **pages;
  5628. int i;
  5629. pages = kcalloc(size, sizeof(struct page *), gfp_flags);
  5630. if (!pages) {
  5631. dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
  5632. return NULL;
  5633. }
  5634. for (i = 0; i < size; i++) {
  5635. pages[i] = alloc_page(gfp_flags);
  5636. if (!pages[i]) {
  5637. dprintk("%s: failed to allocate page\n", __func__);
  5638. nfs4_free_pages(pages, size);
  5639. return NULL;
  5640. }
  5641. }
  5642. return pages;
  5643. }
  5644. static void nfs4_layoutget_release(void *calldata)
  5645. {
  5646. struct nfs4_layoutget *lgp = calldata;
  5647. struct inode *inode = lgp->args.inode;
  5648. struct nfs_server *server = NFS_SERVER(inode);
  5649. size_t max_pages = max_response_pages(server);
  5650. dprintk("--> %s\n", __func__);
  5651. nfs4_free_pages(lgp->args.layout.pages, max_pages);
  5652. pnfs_put_layout_hdr(NFS_I(inode)->layout);
  5653. put_nfs_open_context(lgp->args.ctx);
  5654. kfree(calldata);
  5655. dprintk("<-- %s\n", __func__);
  5656. }
  5657. static const struct rpc_call_ops nfs4_layoutget_call_ops = {
  5658. .rpc_call_prepare = nfs4_layoutget_prepare,
  5659. .rpc_call_done = nfs4_layoutget_done,
  5660. .rpc_release = nfs4_layoutget_release,
  5661. };
  5662. struct pnfs_layout_segment *
  5663. nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
  5664. {
  5665. struct inode *inode = lgp->args.inode;
  5666. struct nfs_server *server = NFS_SERVER(inode);
  5667. size_t max_pages = max_response_pages(server);
  5668. struct rpc_task *task;
  5669. struct rpc_message msg = {
  5670. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
  5671. .rpc_argp = &lgp->args,
  5672. .rpc_resp = &lgp->res,
  5673. };
  5674. struct rpc_task_setup task_setup_data = {
  5675. .rpc_client = server->client,
  5676. .rpc_message = &msg,
  5677. .callback_ops = &nfs4_layoutget_call_ops,
  5678. .callback_data = lgp,
  5679. .flags = RPC_TASK_ASYNC,
  5680. };
  5681. struct pnfs_layout_segment *lseg = NULL;
  5682. int status = 0;
  5683. dprintk("--> %s\n", __func__);
  5684. lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
  5685. if (!lgp->args.layout.pages) {
  5686. nfs4_layoutget_release(lgp);
  5687. return ERR_PTR(-ENOMEM);
  5688. }
  5689. lgp->args.layout.pglen = max_pages * PAGE_SIZE;
  5690. lgp->args.timestamp = jiffies;
  5691. lgp->res.layoutp = &lgp->args.layout;
  5692. lgp->res.seq_res.sr_slot = NULL;
  5693. nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
  5694. /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
  5695. pnfs_get_layout_hdr(NFS_I(inode)->layout);
  5696. task = rpc_run_task(&task_setup_data);
  5697. if (IS_ERR(task))
  5698. return ERR_CAST(task);
  5699. status = nfs4_wait_for_completion_rpc_task(task);
  5700. if (status == 0)
  5701. status = task->tk_status;
  5702. /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
  5703. if (status == 0 && lgp->res.layoutp->len)
  5704. lseg = pnfs_layout_process(lgp);
  5705. rpc_put_task(task);
  5706. dprintk("<-- %s status=%d\n", __func__, status);
  5707. if (status)
  5708. return ERR_PTR(status);
  5709. return lseg;
  5710. }
  5711. static void
  5712. nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
  5713. {
  5714. struct nfs4_layoutreturn *lrp = calldata;
  5715. dprintk("--> %s\n", __func__);
  5716. nfs41_setup_sequence(lrp->clp->cl_session,
  5717. &lrp->args.seq_args,
  5718. &lrp->res.seq_res,
  5719. task);
  5720. }
  5721. static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
  5722. {
  5723. struct nfs4_layoutreturn *lrp = calldata;
  5724. struct nfs_server *server;
  5725. dprintk("--> %s\n", __func__);
  5726. if (!nfs41_sequence_done(task, &lrp->res.seq_res))
  5727. return;
  5728. server = NFS_SERVER(lrp->args.inode);
  5729. if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
  5730. rpc_restart_call_prepare(task);
  5731. return;
  5732. }
  5733. dprintk("<-- %s\n", __func__);
  5734. }
  5735. static void nfs4_layoutreturn_release(void *calldata)
  5736. {
  5737. struct nfs4_layoutreturn *lrp = calldata;
  5738. struct pnfs_layout_hdr *lo = lrp->args.layout;
  5739. dprintk("--> %s\n", __func__);
  5740. spin_lock(&lo->plh_inode->i_lock);
  5741. if (lrp->res.lrs_present)
  5742. pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
  5743. lo->plh_block_lgets--;
  5744. spin_unlock(&lo->plh_inode->i_lock);
  5745. pnfs_put_layout_hdr(lrp->args.layout);
  5746. kfree(calldata);
  5747. dprintk("<-- %s\n", __func__);
  5748. }
  5749. static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
  5750. .rpc_call_prepare = nfs4_layoutreturn_prepare,
  5751. .rpc_call_done = nfs4_layoutreturn_done,
  5752. .rpc_release = nfs4_layoutreturn_release,
  5753. };
  5754. int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
  5755. {
  5756. struct rpc_task *task;
  5757. struct rpc_message msg = {
  5758. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
  5759. .rpc_argp = &lrp->args,
  5760. .rpc_resp = &lrp->res,
  5761. };
  5762. struct rpc_task_setup task_setup_data = {
  5763. .rpc_client = lrp->clp->cl_rpcclient,
  5764. .rpc_message = &msg,
  5765. .callback_ops = &nfs4_layoutreturn_call_ops,
  5766. .callback_data = lrp,
  5767. };
  5768. int status;
  5769. dprintk("--> %s\n", __func__);
  5770. nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
  5771. task = rpc_run_task(&task_setup_data);
  5772. if (IS_ERR(task))
  5773. return PTR_ERR(task);
  5774. status = task->tk_status;
  5775. dprintk("<-- %s status=%d\n", __func__, status);
  5776. rpc_put_task(task);
  5777. return status;
  5778. }
  5779. /*
  5780. * Retrieve the list of Data Server devices from the MDS.
  5781. */
  5782. static int _nfs4_getdevicelist(struct nfs_server *server,
  5783. const struct nfs_fh *fh,
  5784. struct pnfs_devicelist *devlist)
  5785. {
  5786. struct nfs4_getdevicelist_args args = {
  5787. .fh = fh,
  5788. .layoutclass = server->pnfs_curr_ld->id,
  5789. };
  5790. struct nfs4_getdevicelist_res res = {
  5791. .devlist = devlist,
  5792. };
  5793. struct rpc_message msg = {
  5794. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
  5795. .rpc_argp = &args,
  5796. .rpc_resp = &res,
  5797. };
  5798. int status;
  5799. dprintk("--> %s\n", __func__);
  5800. status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
  5801. &res.seq_res, 0);
  5802. dprintk("<-- %s status=%d\n", __func__, status);
  5803. return status;
  5804. }
  5805. int nfs4_proc_getdevicelist(struct nfs_server *server,
  5806. const struct nfs_fh *fh,
  5807. struct pnfs_devicelist *devlist)
  5808. {
  5809. struct nfs4_exception exception = { };
  5810. int err;
  5811. do {
  5812. err = nfs4_handle_exception(server,
  5813. _nfs4_getdevicelist(server, fh, devlist),
  5814. &exception);
  5815. } while (exception.retry);
  5816. dprintk("%s: err=%d, num_devs=%u\n", __func__,
  5817. err, devlist->num_devs);
  5818. return err;
  5819. }
  5820. EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
  5821. static int
  5822. _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
  5823. {
  5824. struct nfs4_getdeviceinfo_args args = {
  5825. .pdev = pdev,
  5826. };
  5827. struct nfs4_getdeviceinfo_res res = {
  5828. .pdev = pdev,
  5829. };
  5830. struct rpc_message msg = {
  5831. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
  5832. .rpc_argp = &args,
  5833. .rpc_resp = &res,
  5834. };
  5835. int status;
  5836. dprintk("--> %s\n", __func__);
  5837. status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  5838. dprintk("<-- %s status=%d\n", __func__, status);
  5839. return status;
  5840. }
  5841. int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
  5842. {
  5843. struct nfs4_exception exception = { };
  5844. int err;
  5845. do {
  5846. err = nfs4_handle_exception(server,
  5847. _nfs4_proc_getdeviceinfo(server, pdev),
  5848. &exception);
  5849. } while (exception.retry);
  5850. return err;
  5851. }
  5852. EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
  5853. static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
  5854. {
  5855. struct nfs4_layoutcommit_data *data = calldata;
  5856. struct nfs_server *server = NFS_SERVER(data->args.inode);
  5857. struct nfs4_session *session = nfs4_get_session(server);
  5858. nfs41_setup_sequence(session,
  5859. &data->args.seq_args,
  5860. &data->res.seq_res,
  5861. task);
  5862. }
  5863. static void
  5864. nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
  5865. {
  5866. struct nfs4_layoutcommit_data *data = calldata;
  5867. struct nfs_server *server = NFS_SERVER(data->args.inode);
  5868. if (!nfs41_sequence_done(task, &data->res.seq_res))
  5869. return;
  5870. switch (task->tk_status) { /* Just ignore these failures */
  5871. case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
  5872. case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
  5873. case -NFS4ERR_BADLAYOUT: /* no layout */
  5874. case -NFS4ERR_GRACE: /* loca_recalim always false */
  5875. task->tk_status = 0;
  5876. break;
  5877. case 0:
  5878. nfs_post_op_update_inode_force_wcc(data->args.inode,
  5879. data->res.fattr);
  5880. break;
  5881. default:
  5882. if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
  5883. rpc_restart_call_prepare(task);
  5884. return;
  5885. }
  5886. }
  5887. }
  5888. static void nfs4_layoutcommit_release(void *calldata)
  5889. {
  5890. struct nfs4_layoutcommit_data *data = calldata;
  5891. pnfs_cleanup_layoutcommit(data);
  5892. put_rpccred(data->cred);
  5893. kfree(data);
  5894. }
  5895. static const struct rpc_call_ops nfs4_layoutcommit_ops = {
  5896. .rpc_call_prepare = nfs4_layoutcommit_prepare,
  5897. .rpc_call_done = nfs4_layoutcommit_done,
  5898. .rpc_release = nfs4_layoutcommit_release,
  5899. };
  5900. int
  5901. nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
  5902. {
  5903. struct rpc_message msg = {
  5904. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
  5905. .rpc_argp = &data->args,
  5906. .rpc_resp = &data->res,
  5907. .rpc_cred = data->cred,
  5908. };
  5909. struct rpc_task_setup task_setup_data = {
  5910. .task = &data->task,
  5911. .rpc_client = NFS_CLIENT(data->args.inode),
  5912. .rpc_message = &msg,
  5913. .callback_ops = &nfs4_layoutcommit_ops,
  5914. .callback_data = data,
  5915. .flags = RPC_TASK_ASYNC,
  5916. };
  5917. struct rpc_task *task;
  5918. int status = 0;
  5919. dprintk("NFS: %4d initiating layoutcommit call. sync %d "
  5920. "lbw: %llu inode %lu\n",
  5921. data->task.tk_pid, sync,
  5922. data->args.lastbytewritten,
  5923. data->args.inode->i_ino);
  5924. nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
  5925. task = rpc_run_task(&task_setup_data);
  5926. if (IS_ERR(task))
  5927. return PTR_ERR(task);
  5928. if (sync == false)
  5929. goto out;
  5930. status = nfs4_wait_for_completion_rpc_task(task);
  5931. if (status != 0)
  5932. goto out;
  5933. status = task->tk_status;
  5934. out:
  5935. dprintk("%s: status %d\n", __func__, status);
  5936. rpc_put_task(task);
  5937. return status;
  5938. }
  5939. static int
  5940. _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
  5941. struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
  5942. {
  5943. struct nfs41_secinfo_no_name_args args = {
  5944. .style = SECINFO_STYLE_CURRENT_FH,
  5945. };
  5946. struct nfs4_secinfo_res res = {
  5947. .flavors = flavors,
  5948. };
  5949. struct rpc_message msg = {
  5950. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
  5951. .rpc_argp = &args,
  5952. .rpc_resp = &res,
  5953. };
  5954. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  5955. }
  5956. static int
  5957. nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
  5958. struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
  5959. {
  5960. struct nfs4_exception exception = { };
  5961. int err;
  5962. do {
  5963. err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
  5964. switch (err) {
  5965. case 0:
  5966. case -NFS4ERR_WRONGSEC:
  5967. case -NFS4ERR_NOTSUPP:
  5968. goto out;
  5969. default:
  5970. err = nfs4_handle_exception(server, err, &exception);
  5971. }
  5972. } while (exception.retry);
  5973. out:
  5974. return err;
  5975. }
  5976. static int
  5977. nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
  5978. struct nfs_fsinfo *info)
  5979. {
  5980. int err;
  5981. struct page *page;
  5982. rpc_authflavor_t flavor;
  5983. struct nfs4_secinfo_flavors *flavors;
  5984. page = alloc_page(GFP_KERNEL);
  5985. if (!page) {
  5986. err = -ENOMEM;
  5987. goto out;
  5988. }
  5989. flavors = page_address(page);
  5990. err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
  5991. /*
  5992. * Fall back on "guess and check" method if
  5993. * the server doesn't support SECINFO_NO_NAME
  5994. */
  5995. if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
  5996. err = nfs4_find_root_sec(server, fhandle, info);
  5997. goto out_freepage;
  5998. }
  5999. if (err)
  6000. goto out_freepage;
  6001. flavor = nfs_find_best_sec(flavors);
  6002. if (err == 0)
  6003. err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
  6004. out_freepage:
  6005. put_page(page);
  6006. if (err == -EACCES)
  6007. return -EPERM;
  6008. out:
  6009. return err;
  6010. }
  6011. static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
  6012. {
  6013. int status;
  6014. struct nfs41_test_stateid_args args = {
  6015. .stateid = stateid,
  6016. };
  6017. struct nfs41_test_stateid_res res;
  6018. struct rpc_message msg = {
  6019. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
  6020. .rpc_argp = &args,
  6021. .rpc_resp = &res,
  6022. };
  6023. dprintk("NFS call test_stateid %p\n", stateid);
  6024. nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
  6025. nfs4_set_sequence_privileged(&args.seq_args);
  6026. status = nfs4_call_sync_sequence(server->client, server, &msg,
  6027. &args.seq_args, &res.seq_res);
  6028. if (status != NFS_OK) {
  6029. dprintk("NFS reply test_stateid: failed, %d\n", status);
  6030. return status;
  6031. }
  6032. dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
  6033. return -res.status;
  6034. }
  6035. /**
  6036. * nfs41_test_stateid - perform a TEST_STATEID operation
  6037. *
  6038. * @server: server / transport on which to perform the operation
  6039. * @stateid: state ID to test
  6040. *
  6041. * Returns NFS_OK if the server recognizes that "stateid" is valid.
  6042. * Otherwise a negative NFS4ERR value is returned if the operation
  6043. * failed or the state ID is not currently valid.
  6044. */
  6045. static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
  6046. {
  6047. struct nfs4_exception exception = { };
  6048. int err;
  6049. do {
  6050. err = _nfs41_test_stateid(server, stateid);
  6051. if (err != -NFS4ERR_DELAY)
  6052. break;
  6053. nfs4_handle_exception(server, err, &exception);
  6054. } while (exception.retry);
  6055. return err;
  6056. }
  6057. static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
  6058. {
  6059. struct nfs41_free_stateid_args args = {
  6060. .stateid = stateid,
  6061. };
  6062. struct nfs41_free_stateid_res res;
  6063. struct rpc_message msg = {
  6064. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
  6065. .rpc_argp = &args,
  6066. .rpc_resp = &res,
  6067. };
  6068. int status;
  6069. dprintk("NFS call free_stateid %p\n", stateid);
  6070. nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
  6071. nfs4_set_sequence_privileged(&args.seq_args);
  6072. status = nfs4_call_sync_sequence(server->client, server, &msg,
  6073. &args.seq_args, &res.seq_res);
  6074. dprintk("NFS reply free_stateid: %d\n", status);
  6075. return status;
  6076. }
  6077. /**
  6078. * nfs41_free_stateid - perform a FREE_STATEID operation
  6079. *
  6080. * @server: server / transport on which to perform the operation
  6081. * @stateid: state ID to release
  6082. *
  6083. * Returns NFS_OK if the server freed "stateid". Otherwise a
  6084. * negative NFS4ERR value is returned.
  6085. */
  6086. static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
  6087. {
  6088. struct nfs4_exception exception = { };
  6089. int err;
  6090. do {
  6091. err = _nfs4_free_stateid(server, stateid);
  6092. if (err != -NFS4ERR_DELAY)
  6093. break;
  6094. nfs4_handle_exception(server, err, &exception);
  6095. } while (exception.retry);
  6096. return err;
  6097. }
  6098. static bool nfs41_match_stateid(const nfs4_stateid *s1,
  6099. const nfs4_stateid *s2)
  6100. {
  6101. if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
  6102. return false;
  6103. if (s1->seqid == s2->seqid)
  6104. return true;
  6105. if (s1->seqid == 0 || s2->seqid == 0)
  6106. return true;
  6107. return false;
  6108. }
  6109. #endif /* CONFIG_NFS_V4_1 */
  6110. static bool nfs4_match_stateid(const nfs4_stateid *s1,
  6111. const nfs4_stateid *s2)
  6112. {
  6113. return nfs4_stateid_match(s1, s2);
  6114. }
  6115. static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
  6116. .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
  6117. .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
  6118. .recover_open = nfs4_open_reclaim,
  6119. .recover_lock = nfs4_lock_reclaim,
  6120. .establish_clid = nfs4_init_clientid,
  6121. .get_clid_cred = nfs4_get_setclientid_cred,
  6122. .detect_trunking = nfs40_discover_server_trunking,
  6123. };
  6124. #if defined(CONFIG_NFS_V4_1)
  6125. static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
  6126. .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
  6127. .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
  6128. .recover_open = nfs4_open_reclaim,
  6129. .recover_lock = nfs4_lock_reclaim,
  6130. .establish_clid = nfs41_init_clientid,
  6131. .get_clid_cred = nfs4_get_exchange_id_cred,
  6132. .reclaim_complete = nfs41_proc_reclaim_complete,
  6133. .detect_trunking = nfs41_discover_server_trunking,
  6134. };
  6135. #endif /* CONFIG_NFS_V4_1 */
  6136. static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
  6137. .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
  6138. .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
  6139. .recover_open = nfs4_open_expired,
  6140. .recover_lock = nfs4_lock_expired,
  6141. .establish_clid = nfs4_init_clientid,
  6142. .get_clid_cred = nfs4_get_setclientid_cred,
  6143. };
  6144. #if defined(CONFIG_NFS_V4_1)
  6145. static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
  6146. .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
  6147. .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
  6148. .recover_open = nfs41_open_expired,
  6149. .recover_lock = nfs41_lock_expired,
  6150. .establish_clid = nfs41_init_clientid,
  6151. .get_clid_cred = nfs4_get_exchange_id_cred,
  6152. };
  6153. #endif /* CONFIG_NFS_V4_1 */
  6154. static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
  6155. .sched_state_renewal = nfs4_proc_async_renew,
  6156. .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
  6157. .renew_lease = nfs4_proc_renew,
  6158. };
  6159. #if defined(CONFIG_NFS_V4_1)
  6160. static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
  6161. .sched_state_renewal = nfs41_proc_async_sequence,
  6162. .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
  6163. .renew_lease = nfs4_proc_sequence,
  6164. };
  6165. #endif
  6166. static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
  6167. .minor_version = 0,
  6168. .init_caps = NFS_CAP_READDIRPLUS
  6169. | NFS_CAP_ATOMIC_OPEN
  6170. | NFS_CAP_CHANGE_ATTR
  6171. | NFS_CAP_POSIX_LOCK,
  6172. .call_sync = _nfs4_call_sync,
  6173. .match_stateid = nfs4_match_stateid,
  6174. .find_root_sec = nfs4_find_root_sec,
  6175. .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
  6176. .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
  6177. .state_renewal_ops = &nfs40_state_renewal_ops,
  6178. };
  6179. #if defined(CONFIG_NFS_V4_1)
  6180. static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
  6181. .minor_version = 1,
  6182. .init_caps = NFS_CAP_READDIRPLUS
  6183. | NFS_CAP_ATOMIC_OPEN
  6184. | NFS_CAP_CHANGE_ATTR
  6185. | NFS_CAP_POSIX_LOCK
  6186. | NFS_CAP_STATEID_NFSV41
  6187. | NFS_CAP_ATOMIC_OPEN_V1,
  6188. .call_sync = nfs4_call_sync_sequence,
  6189. .match_stateid = nfs41_match_stateid,
  6190. .find_root_sec = nfs41_find_root_sec,
  6191. .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
  6192. .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
  6193. .state_renewal_ops = &nfs41_state_renewal_ops,
  6194. };
  6195. #endif
  6196. const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
  6197. [0] = &nfs_v4_0_minor_ops,
  6198. #if defined(CONFIG_NFS_V4_1)
  6199. [1] = &nfs_v4_1_minor_ops,
  6200. #endif
  6201. };
  6202. const struct inode_operations nfs4_dir_inode_operations = {
  6203. .create = nfs_create,
  6204. .lookup = nfs_lookup,
  6205. .atomic_open = nfs_atomic_open,
  6206. .link = nfs_link,
  6207. .unlink = nfs_unlink,
  6208. .symlink = nfs_symlink,
  6209. .mkdir = nfs_mkdir,
  6210. .rmdir = nfs_rmdir,
  6211. .mknod = nfs_mknod,
  6212. .rename = nfs_rename,
  6213. .permission = nfs_permission,
  6214. .getattr = nfs_getattr,
  6215. .setattr = nfs_setattr,
  6216. .getxattr = generic_getxattr,
  6217. .setxattr = generic_setxattr,
  6218. .listxattr = generic_listxattr,
  6219. .removexattr = generic_removexattr,
  6220. };
  6221. static const struct inode_operations nfs4_file_inode_operations = {
  6222. .permission = nfs_permission,
  6223. .getattr = nfs_getattr,
  6224. .setattr = nfs_setattr,
  6225. .getxattr = generic_getxattr,
  6226. .setxattr = generic_setxattr,
  6227. .listxattr = generic_listxattr,
  6228. .removexattr = generic_removexattr,
  6229. };
  6230. const struct nfs_rpc_ops nfs_v4_clientops = {
  6231. .version = 4, /* protocol version */
  6232. .dentry_ops = &nfs4_dentry_operations,
  6233. .dir_inode_ops = &nfs4_dir_inode_operations,
  6234. .file_inode_ops = &nfs4_file_inode_operations,
  6235. .file_ops = &nfs4_file_operations,
  6236. .getroot = nfs4_proc_get_root,
  6237. .submount = nfs4_submount,
  6238. .try_mount = nfs4_try_mount,
  6239. .getattr = nfs4_proc_getattr,
  6240. .setattr = nfs4_proc_setattr,
  6241. .lookup = nfs4_proc_lookup,
  6242. .access = nfs4_proc_access,
  6243. .readlink = nfs4_proc_readlink,
  6244. .create = nfs4_proc_create,
  6245. .remove = nfs4_proc_remove,
  6246. .unlink_setup = nfs4_proc_unlink_setup,
  6247. .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
  6248. .unlink_done = nfs4_proc_unlink_done,
  6249. .rename = nfs4_proc_rename,
  6250. .rename_setup = nfs4_proc_rename_setup,
  6251. .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
  6252. .rename_done = nfs4_proc_rename_done,
  6253. .link = nfs4_proc_link,
  6254. .symlink = nfs4_proc_symlink,
  6255. .mkdir = nfs4_proc_mkdir,
  6256. .rmdir = nfs4_proc_remove,
  6257. .readdir = nfs4_proc_readdir,
  6258. .mknod = nfs4_proc_mknod,
  6259. .statfs = nfs4_proc_statfs,
  6260. .fsinfo = nfs4_proc_fsinfo,
  6261. .pathconf = nfs4_proc_pathconf,
  6262. .set_capabilities = nfs4_server_capabilities,
  6263. .decode_dirent = nfs4_decode_dirent,
  6264. .read_setup = nfs4_proc_read_setup,
  6265. .read_pageio_init = pnfs_pageio_init_read,
  6266. .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
  6267. .read_done = nfs4_read_done,
  6268. .write_setup = nfs4_proc_write_setup,
  6269. .write_pageio_init = pnfs_pageio_init_write,
  6270. .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
  6271. .write_done = nfs4_write_done,
  6272. .commit_setup = nfs4_proc_commit_setup,
  6273. .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
  6274. .commit_done = nfs4_commit_done,
  6275. .lock = nfs4_proc_lock,
  6276. .clear_acl_cache = nfs4_zap_acl_attr,
  6277. .close_context = nfs4_close_context,
  6278. .open_context = nfs4_atomic_open,
  6279. .have_delegation = nfs4_have_delegation,
  6280. .return_delegation = nfs4_inode_return_delegation,
  6281. .alloc_client = nfs4_alloc_client,
  6282. .init_client = nfs4_init_client,
  6283. .free_client = nfs4_free_client,
  6284. .create_server = nfs4_create_server,
  6285. .clone_server = nfs_clone_server,
  6286. };
  6287. static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
  6288. .prefix = XATTR_NAME_NFSV4_ACL,
  6289. .list = nfs4_xattr_list_nfs4_acl,
  6290. .get = nfs4_xattr_get_nfs4_acl,
  6291. .set = nfs4_xattr_set_nfs4_acl,
  6292. };
  6293. const struct xattr_handler *nfs4_xattr_handlers[] = {
  6294. &nfs4_xattr_nfs4_acl_handler,
  6295. NULL
  6296. };
  6297. /*
  6298. * Local variables:
  6299. * c-basic-offset: 8
  6300. * End:
  6301. */