nfs4proc.c 143 KB

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