nfs4proc.c 173 KB

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