nfs4proc.c 144 KB

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