nfs4proc.c 143 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365
  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 nfs_fattr fattr;
  2172. struct nfs4_accessargs args = {
  2173. .fh = NFS_FH(inode),
  2174. .bitmask = server->attr_bitmask,
  2175. };
  2176. struct nfs4_accessres res = {
  2177. .server = server,
  2178. .fattr = &fattr,
  2179. };
  2180. struct rpc_message msg = {
  2181. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
  2182. .rpc_argp = &args,
  2183. .rpc_resp = &res,
  2184. .rpc_cred = entry->cred,
  2185. };
  2186. int mode = entry->mask;
  2187. int status;
  2188. /*
  2189. * Determine which access bits we want to ask for...
  2190. */
  2191. if (mode & MAY_READ)
  2192. args.access |= NFS4_ACCESS_READ;
  2193. if (S_ISDIR(inode->i_mode)) {
  2194. if (mode & MAY_WRITE)
  2195. args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
  2196. if (mode & MAY_EXEC)
  2197. args.access |= NFS4_ACCESS_LOOKUP;
  2198. } else {
  2199. if (mode & MAY_WRITE)
  2200. args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
  2201. if (mode & MAY_EXEC)
  2202. args.access |= NFS4_ACCESS_EXECUTE;
  2203. }
  2204. nfs_fattr_init(&fattr);
  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, &fattr);
  2215. }
  2216. return status;
  2217. }
  2218. static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
  2219. {
  2220. struct nfs4_exception exception = { };
  2221. int err;
  2222. do {
  2223. err = nfs4_handle_exception(NFS_SERVER(inode),
  2224. _nfs4_proc_access(inode, entry),
  2225. &exception);
  2226. } while (exception.retry);
  2227. return err;
  2228. }
  2229. /*
  2230. * TODO: For the time being, we don't try to get any attributes
  2231. * along with any of the zero-copy operations READ, READDIR,
  2232. * READLINK, WRITE.
  2233. *
  2234. * In the case of the first three, we want to put the GETATTR
  2235. * after the read-type operation -- this is because it is hard
  2236. * to predict the length of a GETATTR response in v4, and thus
  2237. * align the READ data correctly. This means that the GETATTR
  2238. * may end up partially falling into the page cache, and we should
  2239. * shift it into the 'tail' of the xdr_buf before processing.
  2240. * To do this efficiently, we need to know the total length
  2241. * of data received, which doesn't seem to be available outside
  2242. * of the RPC layer.
  2243. *
  2244. * In the case of WRITE, we also want to put the GETATTR after
  2245. * the operation -- in this case because we want to make sure
  2246. * we get the post-operation mtime and size. This means that
  2247. * we can't use xdr_encode_pages() as written: we need a variant
  2248. * of it which would leave room in the 'tail' iovec.
  2249. *
  2250. * Both of these changes to the XDR layer would in fact be quite
  2251. * minor, but I decided to leave them for a subsequent patch.
  2252. */
  2253. static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
  2254. unsigned int pgbase, unsigned int pglen)
  2255. {
  2256. struct nfs4_readlink args = {
  2257. .fh = NFS_FH(inode),
  2258. .pgbase = pgbase,
  2259. .pglen = pglen,
  2260. .pages = &page,
  2261. };
  2262. struct nfs4_readlink_res res;
  2263. struct rpc_message msg = {
  2264. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
  2265. .rpc_argp = &args,
  2266. .rpc_resp = &res,
  2267. };
  2268. return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
  2269. }
  2270. static int nfs4_proc_readlink(struct inode *inode, struct page *page,
  2271. unsigned int pgbase, unsigned int pglen)
  2272. {
  2273. struct nfs4_exception exception = { };
  2274. int err;
  2275. do {
  2276. err = nfs4_handle_exception(NFS_SERVER(inode),
  2277. _nfs4_proc_readlink(inode, page, pgbase, pglen),
  2278. &exception);
  2279. } while (exception.retry);
  2280. return err;
  2281. }
  2282. /*
  2283. * Got race?
  2284. * We will need to arrange for the VFS layer to provide an atomic open.
  2285. * Until then, this create/open method is prone to inefficiency and race
  2286. * conditions due to the lookup, create, and open VFS calls from sys_open()
  2287. * placed on the wire.
  2288. *
  2289. * Given the above sorry state of affairs, I'm simply sending an OPEN.
  2290. * The file will be opened again in the subsequent VFS open call
  2291. * (nfs4_proc_file_open).
  2292. *
  2293. * The open for read will just hang around to be used by any process that
  2294. * opens the file O_RDONLY. This will all be resolved with the VFS changes.
  2295. */
  2296. static int
  2297. nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
  2298. int flags, struct nameidata *nd)
  2299. {
  2300. struct path path = {
  2301. .mnt = nd->path.mnt,
  2302. .dentry = dentry,
  2303. };
  2304. struct nfs4_state *state;
  2305. struct rpc_cred *cred;
  2306. fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
  2307. int status = 0;
  2308. cred = rpc_lookup_cred();
  2309. if (IS_ERR(cred)) {
  2310. status = PTR_ERR(cred);
  2311. goto out;
  2312. }
  2313. state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
  2314. d_drop(dentry);
  2315. if (IS_ERR(state)) {
  2316. status = PTR_ERR(state);
  2317. goto out_putcred;
  2318. }
  2319. d_add(dentry, igrab(state->inode));
  2320. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  2321. if (flags & O_EXCL) {
  2322. struct nfs_fattr fattr;
  2323. status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
  2324. if (status == 0)
  2325. nfs_setattr_update_inode(state->inode, sattr);
  2326. nfs_post_op_update_inode(state->inode, &fattr);
  2327. }
  2328. if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
  2329. status = nfs4_intent_set_file(nd, &path, state, fmode);
  2330. else
  2331. nfs4_close_sync(&path, state, fmode);
  2332. out_putcred:
  2333. put_rpccred(cred);
  2334. out:
  2335. return status;
  2336. }
  2337. static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
  2338. {
  2339. struct nfs_server *server = NFS_SERVER(dir);
  2340. struct nfs_removeargs args = {
  2341. .fh = NFS_FH(dir),
  2342. .name.len = name->len,
  2343. .name.name = name->name,
  2344. .bitmask = server->attr_bitmask,
  2345. };
  2346. struct nfs_removeres res = {
  2347. .server = server,
  2348. };
  2349. struct rpc_message msg = {
  2350. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
  2351. .rpc_argp = &args,
  2352. .rpc_resp = &res,
  2353. };
  2354. int status;
  2355. nfs_fattr_init(&res.dir_attr);
  2356. status = nfs4_call_sync(server, &msg, &args, &res, 1);
  2357. if (status == 0) {
  2358. update_changeattr(dir, &res.cinfo);
  2359. nfs_post_op_update_inode(dir, &res.dir_attr);
  2360. }
  2361. return status;
  2362. }
  2363. static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
  2364. {
  2365. struct nfs4_exception exception = { };
  2366. int err;
  2367. do {
  2368. err = nfs4_handle_exception(NFS_SERVER(dir),
  2369. _nfs4_proc_remove(dir, name),
  2370. &exception);
  2371. } while (exception.retry);
  2372. return err;
  2373. }
  2374. static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
  2375. {
  2376. struct nfs_server *server = NFS_SERVER(dir);
  2377. struct nfs_removeargs *args = msg->rpc_argp;
  2378. struct nfs_removeres *res = msg->rpc_resp;
  2379. args->bitmask = server->cache_consistency_bitmask;
  2380. res->server = server;
  2381. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
  2382. }
  2383. static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
  2384. {
  2385. struct nfs_removeres *res = task->tk_msg.rpc_resp;
  2386. nfs4_sequence_done(res->server, &res->seq_res, task->tk_status);
  2387. if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
  2388. return 0;
  2389. update_changeattr(dir, &res->cinfo);
  2390. nfs_post_op_update_inode(dir, &res->dir_attr);
  2391. return 1;
  2392. }
  2393. static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
  2394. struct inode *new_dir, struct qstr *new_name)
  2395. {
  2396. struct nfs_server *server = NFS_SERVER(old_dir);
  2397. struct nfs4_rename_arg arg = {
  2398. .old_dir = NFS_FH(old_dir),
  2399. .new_dir = NFS_FH(new_dir),
  2400. .old_name = old_name,
  2401. .new_name = new_name,
  2402. .bitmask = server->attr_bitmask,
  2403. };
  2404. struct nfs_fattr old_fattr, new_fattr;
  2405. struct nfs4_rename_res res = {
  2406. .server = server,
  2407. .old_fattr = &old_fattr,
  2408. .new_fattr = &new_fattr,
  2409. };
  2410. struct rpc_message msg = {
  2411. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
  2412. .rpc_argp = &arg,
  2413. .rpc_resp = &res,
  2414. };
  2415. int status;
  2416. nfs_fattr_init(res.old_fattr);
  2417. nfs_fattr_init(res.new_fattr);
  2418. status = nfs4_call_sync(server, &msg, &arg, &res, 1);
  2419. if (!status) {
  2420. update_changeattr(old_dir, &res.old_cinfo);
  2421. nfs_post_op_update_inode(old_dir, res.old_fattr);
  2422. update_changeattr(new_dir, &res.new_cinfo);
  2423. nfs_post_op_update_inode(new_dir, res.new_fattr);
  2424. }
  2425. return status;
  2426. }
  2427. static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
  2428. struct inode *new_dir, struct qstr *new_name)
  2429. {
  2430. struct nfs4_exception exception = { };
  2431. int err;
  2432. do {
  2433. err = nfs4_handle_exception(NFS_SERVER(old_dir),
  2434. _nfs4_proc_rename(old_dir, old_name,
  2435. new_dir, new_name),
  2436. &exception);
  2437. } while (exception.retry);
  2438. return err;
  2439. }
  2440. static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
  2441. {
  2442. struct nfs_server *server = NFS_SERVER(inode);
  2443. struct nfs4_link_arg arg = {
  2444. .fh = NFS_FH(inode),
  2445. .dir_fh = NFS_FH(dir),
  2446. .name = name,
  2447. .bitmask = server->attr_bitmask,
  2448. };
  2449. struct nfs_fattr fattr, dir_attr;
  2450. struct nfs4_link_res res = {
  2451. .server = server,
  2452. .fattr = &fattr,
  2453. .dir_attr = &dir_attr,
  2454. };
  2455. struct rpc_message msg = {
  2456. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
  2457. .rpc_argp = &arg,
  2458. .rpc_resp = &res,
  2459. };
  2460. int status;
  2461. nfs_fattr_init(res.fattr);
  2462. nfs_fattr_init(res.dir_attr);
  2463. status = nfs4_call_sync(server, &msg, &arg, &res, 1);
  2464. if (!status) {
  2465. update_changeattr(dir, &res.cinfo);
  2466. nfs_post_op_update_inode(dir, res.dir_attr);
  2467. nfs_post_op_update_inode(inode, res.fattr);
  2468. }
  2469. return status;
  2470. }
  2471. static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
  2472. {
  2473. struct nfs4_exception exception = { };
  2474. int err;
  2475. do {
  2476. err = nfs4_handle_exception(NFS_SERVER(inode),
  2477. _nfs4_proc_link(inode, dir, name),
  2478. &exception);
  2479. } while (exception.retry);
  2480. return err;
  2481. }
  2482. struct nfs4_createdata {
  2483. struct rpc_message msg;
  2484. struct nfs4_create_arg arg;
  2485. struct nfs4_create_res res;
  2486. struct nfs_fh fh;
  2487. struct nfs_fattr fattr;
  2488. struct nfs_fattr dir_fattr;
  2489. };
  2490. static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
  2491. struct qstr *name, struct iattr *sattr, u32 ftype)
  2492. {
  2493. struct nfs4_createdata *data;
  2494. data = kzalloc(sizeof(*data), GFP_KERNEL);
  2495. if (data != NULL) {
  2496. struct nfs_server *server = NFS_SERVER(dir);
  2497. data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
  2498. data->msg.rpc_argp = &data->arg;
  2499. data->msg.rpc_resp = &data->res;
  2500. data->arg.dir_fh = NFS_FH(dir);
  2501. data->arg.server = server;
  2502. data->arg.name = name;
  2503. data->arg.attrs = sattr;
  2504. data->arg.ftype = ftype;
  2505. data->arg.bitmask = server->attr_bitmask;
  2506. data->res.server = server;
  2507. data->res.fh = &data->fh;
  2508. data->res.fattr = &data->fattr;
  2509. data->res.dir_fattr = &data->dir_fattr;
  2510. nfs_fattr_init(data->res.fattr);
  2511. nfs_fattr_init(data->res.dir_fattr);
  2512. }
  2513. return data;
  2514. }
  2515. static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
  2516. {
  2517. int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
  2518. &data->arg, &data->res, 1);
  2519. if (status == 0) {
  2520. update_changeattr(dir, &data->res.dir_cinfo);
  2521. nfs_post_op_update_inode(dir, data->res.dir_fattr);
  2522. status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
  2523. }
  2524. return status;
  2525. }
  2526. static void nfs4_free_createdata(struct nfs4_createdata *data)
  2527. {
  2528. kfree(data);
  2529. }
  2530. static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
  2531. struct page *page, unsigned int len, struct iattr *sattr)
  2532. {
  2533. struct nfs4_createdata *data;
  2534. int status = -ENAMETOOLONG;
  2535. if (len > NFS4_MAXPATHLEN)
  2536. goto out;
  2537. status = -ENOMEM;
  2538. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
  2539. if (data == NULL)
  2540. goto out;
  2541. data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
  2542. data->arg.u.symlink.pages = &page;
  2543. data->arg.u.symlink.len = len;
  2544. status = nfs4_do_create(dir, dentry, data);
  2545. nfs4_free_createdata(data);
  2546. out:
  2547. return status;
  2548. }
  2549. static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
  2550. struct page *page, unsigned int len, struct iattr *sattr)
  2551. {
  2552. struct nfs4_exception exception = { };
  2553. int err;
  2554. do {
  2555. err = nfs4_handle_exception(NFS_SERVER(dir),
  2556. _nfs4_proc_symlink(dir, dentry, page,
  2557. len, sattr),
  2558. &exception);
  2559. } while (exception.retry);
  2560. return err;
  2561. }
  2562. static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
  2563. struct iattr *sattr)
  2564. {
  2565. struct nfs4_createdata *data;
  2566. int status = -ENOMEM;
  2567. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
  2568. if (data == NULL)
  2569. goto out;
  2570. status = nfs4_do_create(dir, dentry, data);
  2571. nfs4_free_createdata(data);
  2572. out:
  2573. return status;
  2574. }
  2575. static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
  2576. struct iattr *sattr)
  2577. {
  2578. struct nfs4_exception exception = { };
  2579. int err;
  2580. do {
  2581. err = nfs4_handle_exception(NFS_SERVER(dir),
  2582. _nfs4_proc_mkdir(dir, dentry, sattr),
  2583. &exception);
  2584. } while (exception.retry);
  2585. return err;
  2586. }
  2587. static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
  2588. u64 cookie, struct page *page, unsigned int count, int plus)
  2589. {
  2590. struct inode *dir = dentry->d_inode;
  2591. struct nfs4_readdir_arg args = {
  2592. .fh = NFS_FH(dir),
  2593. .pages = &page,
  2594. .pgbase = 0,
  2595. .count = count,
  2596. .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
  2597. };
  2598. struct nfs4_readdir_res res;
  2599. struct rpc_message msg = {
  2600. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
  2601. .rpc_argp = &args,
  2602. .rpc_resp = &res,
  2603. .rpc_cred = cred,
  2604. };
  2605. int status;
  2606. dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
  2607. dentry->d_parent->d_name.name,
  2608. dentry->d_name.name,
  2609. (unsigned long long)cookie);
  2610. nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
  2611. res.pgbase = args.pgbase;
  2612. status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
  2613. if (status == 0)
  2614. memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
  2615. nfs_invalidate_atime(dir);
  2616. dprintk("%s: returns %d\n", __func__, status);
  2617. return status;
  2618. }
  2619. static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
  2620. u64 cookie, struct page *page, unsigned int count, int plus)
  2621. {
  2622. struct nfs4_exception exception = { };
  2623. int err;
  2624. do {
  2625. err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
  2626. _nfs4_proc_readdir(dentry, cred, cookie,
  2627. page, count, plus),
  2628. &exception);
  2629. } while (exception.retry);
  2630. return err;
  2631. }
  2632. static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
  2633. struct iattr *sattr, dev_t rdev)
  2634. {
  2635. struct nfs4_createdata *data;
  2636. int mode = sattr->ia_mode;
  2637. int status = -ENOMEM;
  2638. BUG_ON(!(sattr->ia_valid & ATTR_MODE));
  2639. BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
  2640. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
  2641. if (data == NULL)
  2642. goto out;
  2643. if (S_ISFIFO(mode))
  2644. data->arg.ftype = NF4FIFO;
  2645. else if (S_ISBLK(mode)) {
  2646. data->arg.ftype = NF4BLK;
  2647. data->arg.u.device.specdata1 = MAJOR(rdev);
  2648. data->arg.u.device.specdata2 = MINOR(rdev);
  2649. }
  2650. else if (S_ISCHR(mode)) {
  2651. data->arg.ftype = NF4CHR;
  2652. data->arg.u.device.specdata1 = MAJOR(rdev);
  2653. data->arg.u.device.specdata2 = MINOR(rdev);
  2654. }
  2655. status = nfs4_do_create(dir, dentry, data);
  2656. nfs4_free_createdata(data);
  2657. out:
  2658. return status;
  2659. }
  2660. static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
  2661. struct iattr *sattr, dev_t rdev)
  2662. {
  2663. struct nfs4_exception exception = { };
  2664. int err;
  2665. do {
  2666. err = nfs4_handle_exception(NFS_SERVER(dir),
  2667. _nfs4_proc_mknod(dir, dentry, sattr, rdev),
  2668. &exception);
  2669. } while (exception.retry);
  2670. return err;
  2671. }
  2672. static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
  2673. struct nfs_fsstat *fsstat)
  2674. {
  2675. struct nfs4_statfs_arg args = {
  2676. .fh = fhandle,
  2677. .bitmask = server->attr_bitmask,
  2678. };
  2679. struct nfs4_statfs_res res = {
  2680. .fsstat = fsstat,
  2681. };
  2682. struct rpc_message msg = {
  2683. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
  2684. .rpc_argp = &args,
  2685. .rpc_resp = &res,
  2686. };
  2687. nfs_fattr_init(fsstat->fattr);
  2688. return nfs4_call_sync(server, &msg, &args, &res, 0);
  2689. }
  2690. static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
  2691. {
  2692. struct nfs4_exception exception = { };
  2693. int err;
  2694. do {
  2695. err = nfs4_handle_exception(server,
  2696. _nfs4_proc_statfs(server, fhandle, fsstat),
  2697. &exception);
  2698. } while (exception.retry);
  2699. return err;
  2700. }
  2701. static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
  2702. struct nfs_fsinfo *fsinfo)
  2703. {
  2704. struct nfs4_fsinfo_arg args = {
  2705. .fh = fhandle,
  2706. .bitmask = server->attr_bitmask,
  2707. };
  2708. struct nfs4_fsinfo_res res = {
  2709. .fsinfo = fsinfo,
  2710. };
  2711. struct rpc_message msg = {
  2712. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
  2713. .rpc_argp = &args,
  2714. .rpc_resp = &res,
  2715. };
  2716. return nfs4_call_sync(server, &msg, &args, &res, 0);
  2717. }
  2718. static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
  2719. {
  2720. struct nfs4_exception exception = { };
  2721. int err;
  2722. do {
  2723. err = nfs4_handle_exception(server,
  2724. _nfs4_do_fsinfo(server, fhandle, fsinfo),
  2725. &exception);
  2726. } while (exception.retry);
  2727. return err;
  2728. }
  2729. static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
  2730. {
  2731. nfs_fattr_init(fsinfo->fattr);
  2732. return nfs4_do_fsinfo(server, fhandle, fsinfo);
  2733. }
  2734. static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
  2735. struct nfs_pathconf *pathconf)
  2736. {
  2737. struct nfs4_pathconf_arg args = {
  2738. .fh = fhandle,
  2739. .bitmask = server->attr_bitmask,
  2740. };
  2741. struct nfs4_pathconf_res res = {
  2742. .pathconf = pathconf,
  2743. };
  2744. struct rpc_message msg = {
  2745. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
  2746. .rpc_argp = &args,
  2747. .rpc_resp = &res,
  2748. };
  2749. /* None of the pathconf attributes are mandatory to implement */
  2750. if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
  2751. memset(pathconf, 0, sizeof(*pathconf));
  2752. return 0;
  2753. }
  2754. nfs_fattr_init(pathconf->fattr);
  2755. return nfs4_call_sync(server, &msg, &args, &res, 0);
  2756. }
  2757. static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
  2758. struct nfs_pathconf *pathconf)
  2759. {
  2760. struct nfs4_exception exception = { };
  2761. int err;
  2762. do {
  2763. err = nfs4_handle_exception(server,
  2764. _nfs4_proc_pathconf(server, fhandle, pathconf),
  2765. &exception);
  2766. } while (exception.retry);
  2767. return err;
  2768. }
  2769. static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
  2770. {
  2771. struct nfs_server *server = NFS_SERVER(data->inode);
  2772. dprintk("--> %s\n", __func__);
  2773. nfs4_sequence_done(server, &data->res.seq_res, task->tk_status);
  2774. if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
  2775. nfs_restart_rpc(task, server->nfs_client);
  2776. return -EAGAIN;
  2777. }
  2778. nfs_invalidate_atime(data->inode);
  2779. if (task->tk_status > 0)
  2780. renew_lease(server, data->timestamp);
  2781. return 0;
  2782. }
  2783. static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
  2784. {
  2785. data->timestamp = jiffies;
  2786. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
  2787. }
  2788. static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
  2789. {
  2790. struct inode *inode = data->inode;
  2791. nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
  2792. task->tk_status);
  2793. if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
  2794. nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
  2795. return -EAGAIN;
  2796. }
  2797. if (task->tk_status >= 0) {
  2798. renew_lease(NFS_SERVER(inode), data->timestamp);
  2799. nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
  2800. }
  2801. return 0;
  2802. }
  2803. static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
  2804. {
  2805. struct nfs_server *server = NFS_SERVER(data->inode);
  2806. data->args.bitmask = server->cache_consistency_bitmask;
  2807. data->res.server = server;
  2808. data->timestamp = jiffies;
  2809. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
  2810. }
  2811. static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
  2812. {
  2813. struct inode *inode = data->inode;
  2814. nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
  2815. task->tk_status);
  2816. if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
  2817. nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
  2818. return -EAGAIN;
  2819. }
  2820. nfs_refresh_inode(inode, data->res.fattr);
  2821. return 0;
  2822. }
  2823. static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
  2824. {
  2825. struct nfs_server *server = NFS_SERVER(data->inode);
  2826. data->args.bitmask = server->cache_consistency_bitmask;
  2827. data->res.server = server;
  2828. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
  2829. }
  2830. /*
  2831. * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
  2832. * standalone procedure for queueing an asynchronous RENEW.
  2833. */
  2834. static void nfs4_renew_release(void *data)
  2835. {
  2836. struct nfs_client *clp = data;
  2837. if (atomic_read(&clp->cl_count) > 1)
  2838. nfs4_schedule_state_renewal(clp);
  2839. nfs_put_client(clp);
  2840. }
  2841. static void nfs4_renew_done(struct rpc_task *task, void *data)
  2842. {
  2843. struct nfs_client *clp = data;
  2844. unsigned long timestamp = task->tk_start;
  2845. if (task->tk_status < 0) {
  2846. /* Unless we're shutting down, schedule state recovery! */
  2847. if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
  2848. nfs4_schedule_state_recovery(clp);
  2849. return;
  2850. }
  2851. spin_lock(&clp->cl_lock);
  2852. if (time_before(clp->cl_last_renewal,timestamp))
  2853. clp->cl_last_renewal = timestamp;
  2854. spin_unlock(&clp->cl_lock);
  2855. }
  2856. static const struct rpc_call_ops nfs4_renew_ops = {
  2857. .rpc_call_done = nfs4_renew_done,
  2858. .rpc_release = nfs4_renew_release,
  2859. };
  2860. int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
  2861. {
  2862. struct rpc_message msg = {
  2863. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
  2864. .rpc_argp = clp,
  2865. .rpc_cred = cred,
  2866. };
  2867. if (!atomic_inc_not_zero(&clp->cl_count))
  2868. return -EIO;
  2869. return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
  2870. &nfs4_renew_ops, clp);
  2871. }
  2872. int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
  2873. {
  2874. struct rpc_message msg = {
  2875. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
  2876. .rpc_argp = clp,
  2877. .rpc_cred = cred,
  2878. };
  2879. unsigned long now = jiffies;
  2880. int status;
  2881. status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
  2882. if (status < 0)
  2883. return status;
  2884. spin_lock(&clp->cl_lock);
  2885. if (time_before(clp->cl_last_renewal,now))
  2886. clp->cl_last_renewal = now;
  2887. spin_unlock(&clp->cl_lock);
  2888. return 0;
  2889. }
  2890. static inline int nfs4_server_supports_acls(struct nfs_server *server)
  2891. {
  2892. return (server->caps & NFS_CAP_ACLS)
  2893. && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
  2894. && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
  2895. }
  2896. /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
  2897. * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
  2898. * the stack.
  2899. */
  2900. #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
  2901. static void buf_to_pages(const void *buf, size_t buflen,
  2902. struct page **pages, unsigned int *pgbase)
  2903. {
  2904. const void *p = buf;
  2905. *pgbase = offset_in_page(buf);
  2906. p -= *pgbase;
  2907. while (p < buf + buflen) {
  2908. *(pages++) = virt_to_page(p);
  2909. p += PAGE_CACHE_SIZE;
  2910. }
  2911. }
  2912. struct nfs4_cached_acl {
  2913. int cached;
  2914. size_t len;
  2915. char data[0];
  2916. };
  2917. static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
  2918. {
  2919. struct nfs_inode *nfsi = NFS_I(inode);
  2920. spin_lock(&inode->i_lock);
  2921. kfree(nfsi->nfs4_acl);
  2922. nfsi->nfs4_acl = acl;
  2923. spin_unlock(&inode->i_lock);
  2924. }
  2925. static void nfs4_zap_acl_attr(struct inode *inode)
  2926. {
  2927. nfs4_set_cached_acl(inode, NULL);
  2928. }
  2929. static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
  2930. {
  2931. struct nfs_inode *nfsi = NFS_I(inode);
  2932. struct nfs4_cached_acl *acl;
  2933. int ret = -ENOENT;
  2934. spin_lock(&inode->i_lock);
  2935. acl = nfsi->nfs4_acl;
  2936. if (acl == NULL)
  2937. goto out;
  2938. if (buf == NULL) /* user is just asking for length */
  2939. goto out_len;
  2940. if (acl->cached == 0)
  2941. goto out;
  2942. ret = -ERANGE; /* see getxattr(2) man page */
  2943. if (acl->len > buflen)
  2944. goto out;
  2945. memcpy(buf, acl->data, acl->len);
  2946. out_len:
  2947. ret = acl->len;
  2948. out:
  2949. spin_unlock(&inode->i_lock);
  2950. return ret;
  2951. }
  2952. static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
  2953. {
  2954. struct nfs4_cached_acl *acl;
  2955. if (buf && acl_len <= PAGE_SIZE) {
  2956. acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
  2957. if (acl == NULL)
  2958. goto out;
  2959. acl->cached = 1;
  2960. memcpy(acl->data, buf, acl_len);
  2961. } else {
  2962. acl = kmalloc(sizeof(*acl), GFP_KERNEL);
  2963. if (acl == NULL)
  2964. goto out;
  2965. acl->cached = 0;
  2966. }
  2967. acl->len = acl_len;
  2968. out:
  2969. nfs4_set_cached_acl(inode, acl);
  2970. }
  2971. static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
  2972. {
  2973. struct page *pages[NFS4ACL_MAXPAGES];
  2974. struct nfs_getaclargs args = {
  2975. .fh = NFS_FH(inode),
  2976. .acl_pages = pages,
  2977. .acl_len = buflen,
  2978. };
  2979. struct nfs_getaclres res = {
  2980. .acl_len = buflen,
  2981. };
  2982. void *resp_buf;
  2983. struct rpc_message msg = {
  2984. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
  2985. .rpc_argp = &args,
  2986. .rpc_resp = &res,
  2987. };
  2988. struct page *localpage = NULL;
  2989. int ret;
  2990. if (buflen < PAGE_SIZE) {
  2991. /* As long as we're doing a round trip to the server anyway,
  2992. * let's be prepared for a page of acl data. */
  2993. localpage = alloc_page(GFP_KERNEL);
  2994. resp_buf = page_address(localpage);
  2995. if (localpage == NULL)
  2996. return -ENOMEM;
  2997. args.acl_pages[0] = localpage;
  2998. args.acl_pgbase = 0;
  2999. args.acl_len = PAGE_SIZE;
  3000. } else {
  3001. resp_buf = buf;
  3002. buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
  3003. }
  3004. ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
  3005. if (ret)
  3006. goto out_free;
  3007. if (res.acl_len > args.acl_len)
  3008. nfs4_write_cached_acl(inode, NULL, res.acl_len);
  3009. else
  3010. nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
  3011. if (buf) {
  3012. ret = -ERANGE;
  3013. if (res.acl_len > buflen)
  3014. goto out_free;
  3015. if (localpage)
  3016. memcpy(buf, resp_buf, res.acl_len);
  3017. }
  3018. ret = res.acl_len;
  3019. out_free:
  3020. if (localpage)
  3021. __free_page(localpage);
  3022. return ret;
  3023. }
  3024. static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
  3025. {
  3026. struct nfs4_exception exception = { };
  3027. ssize_t ret;
  3028. do {
  3029. ret = __nfs4_get_acl_uncached(inode, buf, buflen);
  3030. if (ret >= 0)
  3031. break;
  3032. ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
  3033. } while (exception.retry);
  3034. return ret;
  3035. }
  3036. static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
  3037. {
  3038. struct nfs_server *server = NFS_SERVER(inode);
  3039. int ret;
  3040. if (!nfs4_server_supports_acls(server))
  3041. return -EOPNOTSUPP;
  3042. ret = nfs_revalidate_inode(server, inode);
  3043. if (ret < 0)
  3044. return ret;
  3045. ret = nfs4_read_cached_acl(inode, buf, buflen);
  3046. if (ret != -ENOENT)
  3047. return ret;
  3048. return nfs4_get_acl_uncached(inode, buf, buflen);
  3049. }
  3050. static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
  3051. {
  3052. struct nfs_server *server = NFS_SERVER(inode);
  3053. struct page *pages[NFS4ACL_MAXPAGES];
  3054. struct nfs_setaclargs arg = {
  3055. .fh = NFS_FH(inode),
  3056. .acl_pages = pages,
  3057. .acl_len = buflen,
  3058. };
  3059. struct nfs_setaclres res;
  3060. struct rpc_message msg = {
  3061. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
  3062. .rpc_argp = &arg,
  3063. .rpc_resp = &res,
  3064. };
  3065. int ret;
  3066. if (!nfs4_server_supports_acls(server))
  3067. return -EOPNOTSUPP;
  3068. nfs_inode_return_delegation(inode);
  3069. buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
  3070. ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
  3071. nfs_access_zap_cache(inode);
  3072. nfs_zap_acl_cache(inode);
  3073. return ret;
  3074. }
  3075. static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
  3076. {
  3077. struct nfs4_exception exception = { };
  3078. int err;
  3079. do {
  3080. err = nfs4_handle_exception(NFS_SERVER(inode),
  3081. __nfs4_proc_set_acl(inode, buf, buflen),
  3082. &exception);
  3083. } while (exception.retry);
  3084. return err;
  3085. }
  3086. static int
  3087. _nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs_client *clp, struct nfs4_state *state)
  3088. {
  3089. if (!clp || task->tk_status >= 0)
  3090. return 0;
  3091. switch(task->tk_status) {
  3092. case -NFS4ERR_ADMIN_REVOKED:
  3093. case -NFS4ERR_BAD_STATEID:
  3094. case -NFS4ERR_OPENMODE:
  3095. if (state == NULL)
  3096. break;
  3097. nfs4_state_mark_reclaim_nograce(clp, state);
  3098. goto do_state_recovery;
  3099. case -NFS4ERR_STALE_STATEID:
  3100. if (state == NULL)
  3101. break;
  3102. nfs4_state_mark_reclaim_reboot(clp, state);
  3103. case -NFS4ERR_STALE_CLIENTID:
  3104. case -NFS4ERR_EXPIRED:
  3105. goto do_state_recovery;
  3106. #if defined(CONFIG_NFS_V4_1)
  3107. case -NFS4ERR_BADSESSION:
  3108. case -NFS4ERR_BADSLOT:
  3109. case -NFS4ERR_BAD_HIGH_SLOT:
  3110. case -NFS4ERR_DEADSESSION:
  3111. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  3112. case -NFS4ERR_SEQ_FALSE_RETRY:
  3113. case -NFS4ERR_SEQ_MISORDERED:
  3114. dprintk("%s ERROR %d, Reset session\n", __func__,
  3115. task->tk_status);
  3116. nfs4_schedule_state_recovery(clp);
  3117. task->tk_status = 0;
  3118. return -EAGAIN;
  3119. #endif /* CONFIG_NFS_V4_1 */
  3120. case -NFS4ERR_DELAY:
  3121. if (server)
  3122. nfs_inc_server_stats(server, NFSIOS_DELAY);
  3123. case -NFS4ERR_GRACE:
  3124. case -EKEYEXPIRED:
  3125. rpc_delay(task, NFS4_POLL_RETRY_MAX);
  3126. task->tk_status = 0;
  3127. return -EAGAIN;
  3128. case -NFS4ERR_OLD_STATEID:
  3129. task->tk_status = 0;
  3130. return -EAGAIN;
  3131. }
  3132. task->tk_status = nfs4_map_errors(task->tk_status);
  3133. return 0;
  3134. do_state_recovery:
  3135. rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
  3136. nfs4_schedule_state_recovery(clp);
  3137. if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
  3138. rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
  3139. task->tk_status = 0;
  3140. return -EAGAIN;
  3141. }
  3142. static int
  3143. nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
  3144. {
  3145. return _nfs4_async_handle_error(task, server, server->nfs_client, state);
  3146. }
  3147. int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
  3148. {
  3149. nfs4_verifier sc_verifier;
  3150. struct nfs4_setclientid setclientid = {
  3151. .sc_verifier = &sc_verifier,
  3152. .sc_prog = program,
  3153. };
  3154. struct rpc_message msg = {
  3155. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
  3156. .rpc_argp = &setclientid,
  3157. .rpc_resp = clp,
  3158. .rpc_cred = cred,
  3159. };
  3160. __be32 *p;
  3161. int loop = 0;
  3162. int status;
  3163. p = (__be32*)sc_verifier.data;
  3164. *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
  3165. *p = htonl((u32)clp->cl_boot_time.tv_nsec);
  3166. for(;;) {
  3167. setclientid.sc_name_len = scnprintf(setclientid.sc_name,
  3168. sizeof(setclientid.sc_name), "%s/%s %s %s %u",
  3169. clp->cl_ipaddr,
  3170. rpc_peeraddr2str(clp->cl_rpcclient,
  3171. RPC_DISPLAY_ADDR),
  3172. rpc_peeraddr2str(clp->cl_rpcclient,
  3173. RPC_DISPLAY_PROTO),
  3174. clp->cl_rpcclient->cl_auth->au_ops->au_name,
  3175. clp->cl_id_uniquifier);
  3176. setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
  3177. sizeof(setclientid.sc_netid),
  3178. rpc_peeraddr2str(clp->cl_rpcclient,
  3179. RPC_DISPLAY_NETID));
  3180. setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
  3181. sizeof(setclientid.sc_uaddr), "%s.%u.%u",
  3182. clp->cl_ipaddr, port >> 8, port & 255);
  3183. status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
  3184. if (status != -NFS4ERR_CLID_INUSE)
  3185. break;
  3186. if (signalled())
  3187. break;
  3188. if (loop++ & 1)
  3189. ssleep(clp->cl_lease_time + 1);
  3190. else
  3191. if (++clp->cl_id_uniquifier == 0)
  3192. break;
  3193. }
  3194. return status;
  3195. }
  3196. static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
  3197. {
  3198. struct nfs_fsinfo fsinfo;
  3199. struct rpc_message msg = {
  3200. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
  3201. .rpc_argp = clp,
  3202. .rpc_resp = &fsinfo,
  3203. .rpc_cred = cred,
  3204. };
  3205. unsigned long now;
  3206. int status;
  3207. now = jiffies;
  3208. status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
  3209. if (status == 0) {
  3210. spin_lock(&clp->cl_lock);
  3211. clp->cl_lease_time = fsinfo.lease_time * HZ;
  3212. clp->cl_last_renewal = now;
  3213. spin_unlock(&clp->cl_lock);
  3214. }
  3215. return status;
  3216. }
  3217. int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
  3218. {
  3219. long timeout = 0;
  3220. int err;
  3221. do {
  3222. err = _nfs4_proc_setclientid_confirm(clp, cred);
  3223. switch (err) {
  3224. case 0:
  3225. return err;
  3226. case -NFS4ERR_RESOURCE:
  3227. /* The IBM lawyers misread another document! */
  3228. case -NFS4ERR_DELAY:
  3229. case -EKEYEXPIRED:
  3230. err = nfs4_delay(clp->cl_rpcclient, &timeout);
  3231. }
  3232. } while (err == 0);
  3233. return err;
  3234. }
  3235. struct nfs4_delegreturndata {
  3236. struct nfs4_delegreturnargs args;
  3237. struct nfs4_delegreturnres res;
  3238. struct nfs_fh fh;
  3239. nfs4_stateid stateid;
  3240. unsigned long timestamp;
  3241. struct nfs_fattr fattr;
  3242. int rpc_status;
  3243. };
  3244. static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
  3245. {
  3246. struct nfs4_delegreturndata *data = calldata;
  3247. nfs4_sequence_done(data->res.server, &data->res.seq_res,
  3248. task->tk_status);
  3249. switch (task->tk_status) {
  3250. case -NFS4ERR_STALE_STATEID:
  3251. case -NFS4ERR_EXPIRED:
  3252. case 0:
  3253. renew_lease(data->res.server, data->timestamp);
  3254. break;
  3255. default:
  3256. if (nfs4_async_handle_error(task, data->res.server, NULL) ==
  3257. -EAGAIN) {
  3258. nfs_restart_rpc(task, data->res.server->nfs_client);
  3259. return;
  3260. }
  3261. }
  3262. data->rpc_status = task->tk_status;
  3263. }
  3264. static void nfs4_delegreturn_release(void *calldata)
  3265. {
  3266. kfree(calldata);
  3267. }
  3268. #if defined(CONFIG_NFS_V4_1)
  3269. static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
  3270. {
  3271. struct nfs4_delegreturndata *d_data;
  3272. d_data = (struct nfs4_delegreturndata *)data;
  3273. if (nfs4_setup_sequence(d_data->res.server->nfs_client,
  3274. &d_data->args.seq_args,
  3275. &d_data->res.seq_res, 1, task))
  3276. return;
  3277. rpc_call_start(task);
  3278. }
  3279. #endif /* CONFIG_NFS_V4_1 */
  3280. static const struct rpc_call_ops nfs4_delegreturn_ops = {
  3281. #if defined(CONFIG_NFS_V4_1)
  3282. .rpc_call_prepare = nfs4_delegreturn_prepare,
  3283. #endif /* CONFIG_NFS_V4_1 */
  3284. .rpc_call_done = nfs4_delegreturn_done,
  3285. .rpc_release = nfs4_delegreturn_release,
  3286. };
  3287. static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
  3288. {
  3289. struct nfs4_delegreturndata *data;
  3290. struct nfs_server *server = NFS_SERVER(inode);
  3291. struct rpc_task *task;
  3292. struct rpc_message msg = {
  3293. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
  3294. .rpc_cred = cred,
  3295. };
  3296. struct rpc_task_setup task_setup_data = {
  3297. .rpc_client = server->client,
  3298. .rpc_message = &msg,
  3299. .callback_ops = &nfs4_delegreturn_ops,
  3300. .flags = RPC_TASK_ASYNC,
  3301. };
  3302. int status = 0;
  3303. data = kzalloc(sizeof(*data), GFP_KERNEL);
  3304. if (data == NULL)
  3305. return -ENOMEM;
  3306. data->args.fhandle = &data->fh;
  3307. data->args.stateid = &data->stateid;
  3308. data->args.bitmask = server->attr_bitmask;
  3309. nfs_copy_fh(&data->fh, NFS_FH(inode));
  3310. memcpy(&data->stateid, stateid, sizeof(data->stateid));
  3311. data->res.fattr = &data->fattr;
  3312. data->res.server = server;
  3313. data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
  3314. nfs_fattr_init(data->res.fattr);
  3315. data->timestamp = jiffies;
  3316. data->rpc_status = 0;
  3317. task_setup_data.callback_data = data;
  3318. msg.rpc_argp = &data->args,
  3319. msg.rpc_resp = &data->res,
  3320. task = rpc_run_task(&task_setup_data);
  3321. if (IS_ERR(task))
  3322. return PTR_ERR(task);
  3323. if (!issync)
  3324. goto out;
  3325. status = nfs4_wait_for_completion_rpc_task(task);
  3326. if (status != 0)
  3327. goto out;
  3328. status = data->rpc_status;
  3329. if (status != 0)
  3330. goto out;
  3331. nfs_refresh_inode(inode, &data->fattr);
  3332. out:
  3333. rpc_put_task(task);
  3334. return status;
  3335. }
  3336. int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
  3337. {
  3338. struct nfs_server *server = NFS_SERVER(inode);
  3339. struct nfs4_exception exception = { };
  3340. int err;
  3341. do {
  3342. err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
  3343. switch (err) {
  3344. case -NFS4ERR_STALE_STATEID:
  3345. case -NFS4ERR_EXPIRED:
  3346. case 0:
  3347. return 0;
  3348. }
  3349. err = nfs4_handle_exception(server, err, &exception);
  3350. } while (exception.retry);
  3351. return err;
  3352. }
  3353. #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
  3354. #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
  3355. /*
  3356. * sleep, with exponential backoff, and retry the LOCK operation.
  3357. */
  3358. static unsigned long
  3359. nfs4_set_lock_task_retry(unsigned long timeout)
  3360. {
  3361. schedule_timeout_killable(timeout);
  3362. timeout <<= 1;
  3363. if (timeout > NFS4_LOCK_MAXTIMEOUT)
  3364. return NFS4_LOCK_MAXTIMEOUT;
  3365. return timeout;
  3366. }
  3367. static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  3368. {
  3369. struct inode *inode = state->inode;
  3370. struct nfs_server *server = NFS_SERVER(inode);
  3371. struct nfs_client *clp = server->nfs_client;
  3372. struct nfs_lockt_args arg = {
  3373. .fh = NFS_FH(inode),
  3374. .fl = request,
  3375. };
  3376. struct nfs_lockt_res res = {
  3377. .denied = request,
  3378. };
  3379. struct rpc_message msg = {
  3380. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
  3381. .rpc_argp = &arg,
  3382. .rpc_resp = &res,
  3383. .rpc_cred = state->owner->so_cred,
  3384. };
  3385. struct nfs4_lock_state *lsp;
  3386. int status;
  3387. arg.lock_owner.clientid = clp->cl_clientid;
  3388. status = nfs4_set_lock_state(state, request);
  3389. if (status != 0)
  3390. goto out;
  3391. lsp = request->fl_u.nfs4_fl.owner;
  3392. arg.lock_owner.id = lsp->ls_id.id;
  3393. status = nfs4_call_sync(server, &msg, &arg, &res, 1);
  3394. switch (status) {
  3395. case 0:
  3396. request->fl_type = F_UNLCK;
  3397. break;
  3398. case -NFS4ERR_DENIED:
  3399. status = 0;
  3400. }
  3401. request->fl_ops->fl_release_private(request);
  3402. out:
  3403. return status;
  3404. }
  3405. static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  3406. {
  3407. struct nfs4_exception exception = { };
  3408. int err;
  3409. do {
  3410. err = nfs4_handle_exception(NFS_SERVER(state->inode),
  3411. _nfs4_proc_getlk(state, cmd, request),
  3412. &exception);
  3413. } while (exception.retry);
  3414. return err;
  3415. }
  3416. static int do_vfs_lock(struct file *file, struct file_lock *fl)
  3417. {
  3418. int res = 0;
  3419. switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
  3420. case FL_POSIX:
  3421. res = posix_lock_file_wait(file, fl);
  3422. break;
  3423. case FL_FLOCK:
  3424. res = flock_lock_file_wait(file, fl);
  3425. break;
  3426. default:
  3427. BUG();
  3428. }
  3429. return res;
  3430. }
  3431. struct nfs4_unlockdata {
  3432. struct nfs_locku_args arg;
  3433. struct nfs_locku_res res;
  3434. struct nfs4_lock_state *lsp;
  3435. struct nfs_open_context *ctx;
  3436. struct file_lock fl;
  3437. const struct nfs_server *server;
  3438. unsigned long timestamp;
  3439. };
  3440. static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
  3441. struct nfs_open_context *ctx,
  3442. struct nfs4_lock_state *lsp,
  3443. struct nfs_seqid *seqid)
  3444. {
  3445. struct nfs4_unlockdata *p;
  3446. struct inode *inode = lsp->ls_state->inode;
  3447. p = kzalloc(sizeof(*p), GFP_KERNEL);
  3448. if (p == NULL)
  3449. return NULL;
  3450. p->arg.fh = NFS_FH(inode);
  3451. p->arg.fl = &p->fl;
  3452. p->arg.seqid = seqid;
  3453. p->res.seqid = seqid;
  3454. p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
  3455. p->arg.stateid = &lsp->ls_stateid;
  3456. p->lsp = lsp;
  3457. atomic_inc(&lsp->ls_count);
  3458. /* Ensure we don't close file until we're done freeing locks! */
  3459. p->ctx = get_nfs_open_context(ctx);
  3460. memcpy(&p->fl, fl, sizeof(p->fl));
  3461. p->server = NFS_SERVER(inode);
  3462. return p;
  3463. }
  3464. static void nfs4_locku_release_calldata(void *data)
  3465. {
  3466. struct nfs4_unlockdata *calldata = data;
  3467. nfs_free_seqid(calldata->arg.seqid);
  3468. nfs4_put_lock_state(calldata->lsp);
  3469. put_nfs_open_context(calldata->ctx);
  3470. kfree(calldata);
  3471. }
  3472. static void nfs4_locku_done(struct rpc_task *task, void *data)
  3473. {
  3474. struct nfs4_unlockdata *calldata = data;
  3475. nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
  3476. task->tk_status);
  3477. if (RPC_ASSASSINATED(task))
  3478. return;
  3479. switch (task->tk_status) {
  3480. case 0:
  3481. memcpy(calldata->lsp->ls_stateid.data,
  3482. calldata->res.stateid.data,
  3483. sizeof(calldata->lsp->ls_stateid.data));
  3484. renew_lease(calldata->server, calldata->timestamp);
  3485. break;
  3486. case -NFS4ERR_BAD_STATEID:
  3487. case -NFS4ERR_OLD_STATEID:
  3488. case -NFS4ERR_STALE_STATEID:
  3489. case -NFS4ERR_EXPIRED:
  3490. break;
  3491. default:
  3492. if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
  3493. nfs_restart_rpc(task,
  3494. calldata->server->nfs_client);
  3495. }
  3496. }
  3497. static void nfs4_locku_prepare(struct rpc_task *task, void *data)
  3498. {
  3499. struct nfs4_unlockdata *calldata = data;
  3500. if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
  3501. return;
  3502. if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
  3503. /* Note: exit _without_ running nfs4_locku_done */
  3504. task->tk_action = NULL;
  3505. return;
  3506. }
  3507. calldata->timestamp = jiffies;
  3508. if (nfs4_setup_sequence(calldata->server->nfs_client,
  3509. &calldata->arg.seq_args,
  3510. &calldata->res.seq_res, 1, task))
  3511. return;
  3512. rpc_call_start(task);
  3513. }
  3514. static const struct rpc_call_ops nfs4_locku_ops = {
  3515. .rpc_call_prepare = nfs4_locku_prepare,
  3516. .rpc_call_done = nfs4_locku_done,
  3517. .rpc_release = nfs4_locku_release_calldata,
  3518. };
  3519. static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
  3520. struct nfs_open_context *ctx,
  3521. struct nfs4_lock_state *lsp,
  3522. struct nfs_seqid *seqid)
  3523. {
  3524. struct nfs4_unlockdata *data;
  3525. struct rpc_message msg = {
  3526. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
  3527. .rpc_cred = ctx->cred,
  3528. };
  3529. struct rpc_task_setup task_setup_data = {
  3530. .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
  3531. .rpc_message = &msg,
  3532. .callback_ops = &nfs4_locku_ops,
  3533. .workqueue = nfsiod_workqueue,
  3534. .flags = RPC_TASK_ASYNC,
  3535. };
  3536. /* Ensure this is an unlock - when canceling a lock, the
  3537. * canceled lock is passed in, and it won't be an unlock.
  3538. */
  3539. fl->fl_type = F_UNLCK;
  3540. data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
  3541. if (data == NULL) {
  3542. nfs_free_seqid(seqid);
  3543. return ERR_PTR(-ENOMEM);
  3544. }
  3545. msg.rpc_argp = &data->arg,
  3546. msg.rpc_resp = &data->res,
  3547. task_setup_data.callback_data = data;
  3548. return rpc_run_task(&task_setup_data);
  3549. }
  3550. static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
  3551. {
  3552. struct nfs_inode *nfsi = NFS_I(state->inode);
  3553. struct nfs_seqid *seqid;
  3554. struct nfs4_lock_state *lsp;
  3555. struct rpc_task *task;
  3556. int status = 0;
  3557. unsigned char fl_flags = request->fl_flags;
  3558. status = nfs4_set_lock_state(state, request);
  3559. /* Unlock _before_ we do the RPC call */
  3560. request->fl_flags |= FL_EXISTS;
  3561. down_read(&nfsi->rwsem);
  3562. if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
  3563. up_read(&nfsi->rwsem);
  3564. goto out;
  3565. }
  3566. up_read(&nfsi->rwsem);
  3567. if (status != 0)
  3568. goto out;
  3569. /* Is this a delegated lock? */
  3570. if (test_bit(NFS_DELEGATED_STATE, &state->flags))
  3571. goto out;
  3572. lsp = request->fl_u.nfs4_fl.owner;
  3573. seqid = nfs_alloc_seqid(&lsp->ls_seqid);
  3574. status = -ENOMEM;
  3575. if (seqid == NULL)
  3576. goto out;
  3577. task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
  3578. status = PTR_ERR(task);
  3579. if (IS_ERR(task))
  3580. goto out;
  3581. status = nfs4_wait_for_completion_rpc_task(task);
  3582. rpc_put_task(task);
  3583. out:
  3584. request->fl_flags = fl_flags;
  3585. return status;
  3586. }
  3587. struct nfs4_lockdata {
  3588. struct nfs_lock_args arg;
  3589. struct nfs_lock_res res;
  3590. struct nfs4_lock_state *lsp;
  3591. struct nfs_open_context *ctx;
  3592. struct file_lock fl;
  3593. unsigned long timestamp;
  3594. int rpc_status;
  3595. int cancelled;
  3596. struct nfs_server *server;
  3597. };
  3598. static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
  3599. struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
  3600. {
  3601. struct nfs4_lockdata *p;
  3602. struct inode *inode = lsp->ls_state->inode;
  3603. struct nfs_server *server = NFS_SERVER(inode);
  3604. p = kzalloc(sizeof(*p), GFP_KERNEL);
  3605. if (p == NULL)
  3606. return NULL;
  3607. p->arg.fh = NFS_FH(inode);
  3608. p->arg.fl = &p->fl;
  3609. p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
  3610. if (p->arg.open_seqid == NULL)
  3611. goto out_free;
  3612. p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
  3613. if (p->arg.lock_seqid == NULL)
  3614. goto out_free_seqid;
  3615. p->arg.lock_stateid = &lsp->ls_stateid;
  3616. p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
  3617. p->arg.lock_owner.id = lsp->ls_id.id;
  3618. p->res.lock_seqid = p->arg.lock_seqid;
  3619. p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
  3620. p->lsp = lsp;
  3621. p->server = server;
  3622. atomic_inc(&lsp->ls_count);
  3623. p->ctx = get_nfs_open_context(ctx);
  3624. memcpy(&p->fl, fl, sizeof(p->fl));
  3625. return p;
  3626. out_free_seqid:
  3627. nfs_free_seqid(p->arg.open_seqid);
  3628. out_free:
  3629. kfree(p);
  3630. return NULL;
  3631. }
  3632. static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
  3633. {
  3634. struct nfs4_lockdata *data = calldata;
  3635. struct nfs4_state *state = data->lsp->ls_state;
  3636. dprintk("%s: begin!\n", __func__);
  3637. if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
  3638. return;
  3639. /* Do we need to do an open_to_lock_owner? */
  3640. if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
  3641. if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
  3642. return;
  3643. data->arg.open_stateid = &state->stateid;
  3644. data->arg.new_lock_owner = 1;
  3645. data->res.open_seqid = data->arg.open_seqid;
  3646. } else
  3647. data->arg.new_lock_owner = 0;
  3648. data->timestamp = jiffies;
  3649. if (nfs4_setup_sequence(data->server->nfs_client, &data->arg.seq_args,
  3650. &data->res.seq_res, 1, task))
  3651. return;
  3652. rpc_call_start(task);
  3653. dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
  3654. }
  3655. static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
  3656. {
  3657. rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
  3658. nfs4_lock_prepare(task, calldata);
  3659. }
  3660. static void nfs4_lock_done(struct rpc_task *task, void *calldata)
  3661. {
  3662. struct nfs4_lockdata *data = calldata;
  3663. dprintk("%s: begin!\n", __func__);
  3664. nfs4_sequence_done(data->server, &data->res.seq_res,
  3665. task->tk_status);
  3666. data->rpc_status = task->tk_status;
  3667. if (RPC_ASSASSINATED(task))
  3668. goto out;
  3669. if (data->arg.new_lock_owner != 0) {
  3670. if (data->rpc_status == 0)
  3671. nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
  3672. else
  3673. goto out;
  3674. }
  3675. if (data->rpc_status == 0) {
  3676. memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
  3677. sizeof(data->lsp->ls_stateid.data));
  3678. data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
  3679. renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
  3680. }
  3681. out:
  3682. dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
  3683. }
  3684. static void nfs4_lock_release(void *calldata)
  3685. {
  3686. struct nfs4_lockdata *data = calldata;
  3687. dprintk("%s: begin!\n", __func__);
  3688. nfs_free_seqid(data->arg.open_seqid);
  3689. if (data->cancelled != 0) {
  3690. struct rpc_task *task;
  3691. task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
  3692. data->arg.lock_seqid);
  3693. if (!IS_ERR(task))
  3694. rpc_put_task(task);
  3695. dprintk("%s: cancelling lock!\n", __func__);
  3696. } else
  3697. nfs_free_seqid(data->arg.lock_seqid);
  3698. nfs4_put_lock_state(data->lsp);
  3699. put_nfs_open_context(data->ctx);
  3700. kfree(data);
  3701. dprintk("%s: done!\n", __func__);
  3702. }
  3703. static const struct rpc_call_ops nfs4_lock_ops = {
  3704. .rpc_call_prepare = nfs4_lock_prepare,
  3705. .rpc_call_done = nfs4_lock_done,
  3706. .rpc_release = nfs4_lock_release,
  3707. };
  3708. static const struct rpc_call_ops nfs4_recover_lock_ops = {
  3709. .rpc_call_prepare = nfs4_recover_lock_prepare,
  3710. .rpc_call_done = nfs4_lock_done,
  3711. .rpc_release = nfs4_lock_release,
  3712. };
  3713. static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
  3714. {
  3715. struct nfs_client *clp = server->nfs_client;
  3716. struct nfs4_state *state = lsp->ls_state;
  3717. switch (error) {
  3718. case -NFS4ERR_ADMIN_REVOKED:
  3719. case -NFS4ERR_BAD_STATEID:
  3720. case -NFS4ERR_EXPIRED:
  3721. if (new_lock_owner != 0 ||
  3722. (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
  3723. nfs4_state_mark_reclaim_nograce(clp, state);
  3724. lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
  3725. break;
  3726. case -NFS4ERR_STALE_STATEID:
  3727. if (new_lock_owner != 0 ||
  3728. (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
  3729. nfs4_state_mark_reclaim_reboot(clp, state);
  3730. lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
  3731. };
  3732. }
  3733. static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
  3734. {
  3735. struct nfs4_lockdata *data;
  3736. struct rpc_task *task;
  3737. struct rpc_message msg = {
  3738. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
  3739. .rpc_cred = state->owner->so_cred,
  3740. };
  3741. struct rpc_task_setup task_setup_data = {
  3742. .rpc_client = NFS_CLIENT(state->inode),
  3743. .rpc_message = &msg,
  3744. .callback_ops = &nfs4_lock_ops,
  3745. .workqueue = nfsiod_workqueue,
  3746. .flags = RPC_TASK_ASYNC,
  3747. };
  3748. int ret;
  3749. dprintk("%s: begin!\n", __func__);
  3750. data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
  3751. fl->fl_u.nfs4_fl.owner);
  3752. if (data == NULL)
  3753. return -ENOMEM;
  3754. if (IS_SETLKW(cmd))
  3755. data->arg.block = 1;
  3756. if (recovery_type > NFS_LOCK_NEW) {
  3757. if (recovery_type == NFS_LOCK_RECLAIM)
  3758. data->arg.reclaim = NFS_LOCK_RECLAIM;
  3759. task_setup_data.callback_ops = &nfs4_recover_lock_ops;
  3760. }
  3761. msg.rpc_argp = &data->arg,
  3762. msg.rpc_resp = &data->res,
  3763. task_setup_data.callback_data = data;
  3764. task = rpc_run_task(&task_setup_data);
  3765. if (IS_ERR(task))
  3766. return PTR_ERR(task);
  3767. ret = nfs4_wait_for_completion_rpc_task(task);
  3768. if (ret == 0) {
  3769. ret = data->rpc_status;
  3770. if (ret)
  3771. nfs4_handle_setlk_error(data->server, data->lsp,
  3772. data->arg.new_lock_owner, ret);
  3773. } else
  3774. data->cancelled = 1;
  3775. rpc_put_task(task);
  3776. dprintk("%s: done, ret = %d!\n", __func__, ret);
  3777. return ret;
  3778. }
  3779. static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
  3780. {
  3781. struct nfs_server *server = NFS_SERVER(state->inode);
  3782. struct nfs4_exception exception = { };
  3783. int err;
  3784. do {
  3785. /* Cache the lock if possible... */
  3786. if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
  3787. return 0;
  3788. err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
  3789. if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
  3790. break;
  3791. nfs4_handle_exception(server, err, &exception);
  3792. } while (exception.retry);
  3793. return err;
  3794. }
  3795. static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
  3796. {
  3797. struct nfs_server *server = NFS_SERVER(state->inode);
  3798. struct nfs4_exception exception = { };
  3799. int err;
  3800. err = nfs4_set_lock_state(state, request);
  3801. if (err != 0)
  3802. return err;
  3803. do {
  3804. if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
  3805. return 0;
  3806. err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
  3807. switch (err) {
  3808. default:
  3809. goto out;
  3810. case -NFS4ERR_GRACE:
  3811. case -NFS4ERR_DELAY:
  3812. case -EKEYEXPIRED:
  3813. nfs4_handle_exception(server, err, &exception);
  3814. err = 0;
  3815. }
  3816. } while (exception.retry);
  3817. out:
  3818. return err;
  3819. }
  3820. static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  3821. {
  3822. struct nfs_inode *nfsi = NFS_I(state->inode);
  3823. unsigned char fl_flags = request->fl_flags;
  3824. int status = -ENOLCK;
  3825. if ((fl_flags & FL_POSIX) &&
  3826. !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
  3827. goto out;
  3828. /* Is this a delegated open? */
  3829. status = nfs4_set_lock_state(state, request);
  3830. if (status != 0)
  3831. goto out;
  3832. request->fl_flags |= FL_ACCESS;
  3833. status = do_vfs_lock(request->fl_file, request);
  3834. if (status < 0)
  3835. goto out;
  3836. down_read(&nfsi->rwsem);
  3837. if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
  3838. /* Yes: cache locks! */
  3839. /* ...but avoid races with delegation recall... */
  3840. request->fl_flags = fl_flags & ~FL_SLEEP;
  3841. status = do_vfs_lock(request->fl_file, request);
  3842. goto out_unlock;
  3843. }
  3844. status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
  3845. if (status != 0)
  3846. goto out_unlock;
  3847. /* Note: we always want to sleep here! */
  3848. request->fl_flags = fl_flags | FL_SLEEP;
  3849. if (do_vfs_lock(request->fl_file, request) < 0)
  3850. printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
  3851. out_unlock:
  3852. up_read(&nfsi->rwsem);
  3853. out:
  3854. request->fl_flags = fl_flags;
  3855. return status;
  3856. }
  3857. static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  3858. {
  3859. struct nfs4_exception exception = { };
  3860. int err;
  3861. do {
  3862. err = _nfs4_proc_setlk(state, cmd, request);
  3863. if (err == -NFS4ERR_DENIED)
  3864. err = -EAGAIN;
  3865. err = nfs4_handle_exception(NFS_SERVER(state->inode),
  3866. err, &exception);
  3867. } while (exception.retry);
  3868. return err;
  3869. }
  3870. static int
  3871. nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
  3872. {
  3873. struct nfs_open_context *ctx;
  3874. struct nfs4_state *state;
  3875. unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
  3876. int status;
  3877. /* verify open state */
  3878. ctx = nfs_file_open_context(filp);
  3879. state = ctx->state;
  3880. if (request->fl_start < 0 || request->fl_end < 0)
  3881. return -EINVAL;
  3882. if (IS_GETLK(cmd)) {
  3883. if (state != NULL)
  3884. return nfs4_proc_getlk(state, F_GETLK, request);
  3885. return 0;
  3886. }
  3887. if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
  3888. return -EINVAL;
  3889. if (request->fl_type == F_UNLCK) {
  3890. if (state != NULL)
  3891. return nfs4_proc_unlck(state, cmd, request);
  3892. return 0;
  3893. }
  3894. if (state == NULL)
  3895. return -ENOLCK;
  3896. do {
  3897. status = nfs4_proc_setlk(state, cmd, request);
  3898. if ((status != -EAGAIN) || IS_SETLK(cmd))
  3899. break;
  3900. timeout = nfs4_set_lock_task_retry(timeout);
  3901. status = -ERESTARTSYS;
  3902. if (signalled())
  3903. break;
  3904. } while(status < 0);
  3905. return status;
  3906. }
  3907. int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
  3908. {
  3909. struct nfs_server *server = NFS_SERVER(state->inode);
  3910. struct nfs4_exception exception = { };
  3911. int err;
  3912. err = nfs4_set_lock_state(state, fl);
  3913. if (err != 0)
  3914. goto out;
  3915. do {
  3916. err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
  3917. switch (err) {
  3918. default:
  3919. printk(KERN_ERR "%s: unhandled error %d.\n",
  3920. __func__, err);
  3921. case 0:
  3922. case -ESTALE:
  3923. goto out;
  3924. case -NFS4ERR_EXPIRED:
  3925. case -NFS4ERR_STALE_CLIENTID:
  3926. case -NFS4ERR_STALE_STATEID:
  3927. case -NFS4ERR_BADSESSION:
  3928. case -NFS4ERR_BADSLOT:
  3929. case -NFS4ERR_BAD_HIGH_SLOT:
  3930. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  3931. case -NFS4ERR_DEADSESSION:
  3932. nfs4_schedule_state_recovery(server->nfs_client);
  3933. goto out;
  3934. case -ERESTARTSYS:
  3935. /*
  3936. * The show must go on: exit, but mark the
  3937. * stateid as needing recovery.
  3938. */
  3939. case -NFS4ERR_ADMIN_REVOKED:
  3940. case -NFS4ERR_BAD_STATEID:
  3941. case -NFS4ERR_OPENMODE:
  3942. nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
  3943. err = 0;
  3944. goto out;
  3945. case -ENOMEM:
  3946. case -NFS4ERR_DENIED:
  3947. /* kill_proc(fl->fl_pid, SIGLOST, 1); */
  3948. err = 0;
  3949. goto out;
  3950. case -NFS4ERR_DELAY:
  3951. case -EKEYEXPIRED:
  3952. break;
  3953. }
  3954. err = nfs4_handle_exception(server, err, &exception);
  3955. } while (exception.retry);
  3956. out:
  3957. return err;
  3958. }
  3959. #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
  3960. int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
  3961. size_t buflen, int flags)
  3962. {
  3963. struct inode *inode = dentry->d_inode;
  3964. if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
  3965. return -EOPNOTSUPP;
  3966. return nfs4_proc_set_acl(inode, buf, buflen);
  3967. }
  3968. /* The getxattr man page suggests returning -ENODATA for unknown attributes,
  3969. * and that's what we'll do for e.g. user attributes that haven't been set.
  3970. * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
  3971. * attributes in kernel-managed attribute namespaces. */
  3972. ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
  3973. size_t buflen)
  3974. {
  3975. struct inode *inode = dentry->d_inode;
  3976. if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
  3977. return -EOPNOTSUPP;
  3978. return nfs4_proc_get_acl(inode, buf, buflen);
  3979. }
  3980. ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
  3981. {
  3982. size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
  3983. if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
  3984. return 0;
  3985. if (buf && buflen < len)
  3986. return -ERANGE;
  3987. if (buf)
  3988. memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
  3989. return len;
  3990. }
  3991. static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
  3992. {
  3993. if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
  3994. (fattr->valid & NFS_ATTR_FATTR_FSID) &&
  3995. (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
  3996. return;
  3997. fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
  3998. NFS_ATTR_FATTR_NLINK;
  3999. fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
  4000. fattr->nlink = 2;
  4001. }
  4002. int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
  4003. struct nfs4_fs_locations *fs_locations, struct page *page)
  4004. {
  4005. struct nfs_server *server = NFS_SERVER(dir);
  4006. u32 bitmask[2] = {
  4007. [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
  4008. [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
  4009. };
  4010. struct nfs4_fs_locations_arg args = {
  4011. .dir_fh = NFS_FH(dir),
  4012. .name = name,
  4013. .page = page,
  4014. .bitmask = bitmask,
  4015. };
  4016. struct nfs4_fs_locations_res res = {
  4017. .fs_locations = fs_locations,
  4018. };
  4019. struct rpc_message msg = {
  4020. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
  4021. .rpc_argp = &args,
  4022. .rpc_resp = &res,
  4023. };
  4024. int status;
  4025. dprintk("%s: start\n", __func__);
  4026. nfs_fattr_init(&fs_locations->fattr);
  4027. fs_locations->server = server;
  4028. fs_locations->nlocations = 0;
  4029. status = nfs4_call_sync(server, &msg, &args, &res, 0);
  4030. nfs_fixup_referral_attributes(&fs_locations->fattr);
  4031. dprintk("%s: returned status = %d\n", __func__, status);
  4032. return status;
  4033. }
  4034. #ifdef CONFIG_NFS_V4_1
  4035. /*
  4036. * nfs4_proc_exchange_id()
  4037. *
  4038. * Since the clientid has expired, all compounds using sessions
  4039. * associated with the stale clientid will be returning
  4040. * NFS4ERR_BADSESSION in the sequence operation, and will therefore
  4041. * be in some phase of session reset.
  4042. */
  4043. int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
  4044. {
  4045. nfs4_verifier verifier;
  4046. struct nfs41_exchange_id_args args = {
  4047. .client = clp,
  4048. .flags = clp->cl_exchange_flags,
  4049. };
  4050. struct nfs41_exchange_id_res res = {
  4051. .client = clp,
  4052. };
  4053. int status;
  4054. struct rpc_message msg = {
  4055. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
  4056. .rpc_argp = &args,
  4057. .rpc_resp = &res,
  4058. .rpc_cred = cred,
  4059. };
  4060. __be32 *p;
  4061. dprintk("--> %s\n", __func__);
  4062. BUG_ON(clp == NULL);
  4063. /* Remove server-only flags */
  4064. args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
  4065. p = (u32 *)verifier.data;
  4066. *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
  4067. *p = htonl((u32)clp->cl_boot_time.tv_nsec);
  4068. args.verifier = &verifier;
  4069. while (1) {
  4070. args.id_len = scnprintf(args.id, sizeof(args.id),
  4071. "%s/%s %u",
  4072. clp->cl_ipaddr,
  4073. rpc_peeraddr2str(clp->cl_rpcclient,
  4074. RPC_DISPLAY_ADDR),
  4075. clp->cl_id_uniquifier);
  4076. status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
  4077. if (status != -NFS4ERR_CLID_INUSE)
  4078. break;
  4079. if (signalled())
  4080. break;
  4081. if (++clp->cl_id_uniquifier == 0)
  4082. break;
  4083. }
  4084. dprintk("<-- %s status= %d\n", __func__, status);
  4085. return status;
  4086. }
  4087. struct nfs4_get_lease_time_data {
  4088. struct nfs4_get_lease_time_args *args;
  4089. struct nfs4_get_lease_time_res *res;
  4090. struct nfs_client *clp;
  4091. };
  4092. static void nfs4_get_lease_time_prepare(struct rpc_task *task,
  4093. void *calldata)
  4094. {
  4095. int ret;
  4096. struct nfs4_get_lease_time_data *data =
  4097. (struct nfs4_get_lease_time_data *)calldata;
  4098. dprintk("--> %s\n", __func__);
  4099. rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
  4100. /* just setup sequence, do not trigger session recovery
  4101. since we're invoked within one */
  4102. ret = nfs41_setup_sequence(data->clp->cl_session,
  4103. &data->args->la_seq_args,
  4104. &data->res->lr_seq_res, 0, task);
  4105. BUG_ON(ret == -EAGAIN);
  4106. rpc_call_start(task);
  4107. dprintk("<-- %s\n", __func__);
  4108. }
  4109. /*
  4110. * Called from nfs4_state_manager thread for session setup, so don't recover
  4111. * from sequence operation or clientid errors.
  4112. */
  4113. static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
  4114. {
  4115. struct nfs4_get_lease_time_data *data =
  4116. (struct nfs4_get_lease_time_data *)calldata;
  4117. dprintk("--> %s\n", __func__);
  4118. nfs41_sequence_done(data->clp, &data->res->lr_seq_res, task->tk_status);
  4119. switch (task->tk_status) {
  4120. case -NFS4ERR_DELAY:
  4121. case -NFS4ERR_GRACE:
  4122. case -EKEYEXPIRED:
  4123. dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
  4124. rpc_delay(task, NFS4_POLL_RETRY_MIN);
  4125. task->tk_status = 0;
  4126. nfs_restart_rpc(task, data->clp);
  4127. return;
  4128. }
  4129. dprintk("<-- %s\n", __func__);
  4130. }
  4131. struct rpc_call_ops nfs4_get_lease_time_ops = {
  4132. .rpc_call_prepare = nfs4_get_lease_time_prepare,
  4133. .rpc_call_done = nfs4_get_lease_time_done,
  4134. };
  4135. int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
  4136. {
  4137. struct rpc_task *task;
  4138. struct nfs4_get_lease_time_args args;
  4139. struct nfs4_get_lease_time_res res = {
  4140. .lr_fsinfo = fsinfo,
  4141. };
  4142. struct nfs4_get_lease_time_data data = {
  4143. .args = &args,
  4144. .res = &res,
  4145. .clp = clp,
  4146. };
  4147. struct rpc_message msg = {
  4148. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
  4149. .rpc_argp = &args,
  4150. .rpc_resp = &res,
  4151. };
  4152. struct rpc_task_setup task_setup = {
  4153. .rpc_client = clp->cl_rpcclient,
  4154. .rpc_message = &msg,
  4155. .callback_ops = &nfs4_get_lease_time_ops,
  4156. .callback_data = &data
  4157. };
  4158. int status;
  4159. res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
  4160. dprintk("--> %s\n", __func__);
  4161. task = rpc_run_task(&task_setup);
  4162. if (IS_ERR(task))
  4163. status = PTR_ERR(task);
  4164. else {
  4165. status = task->tk_status;
  4166. rpc_put_task(task);
  4167. }
  4168. dprintk("<-- %s return %d\n", __func__, status);
  4169. return status;
  4170. }
  4171. /*
  4172. * Reset a slot table
  4173. */
  4174. static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
  4175. int ivalue)
  4176. {
  4177. struct nfs4_slot *new = NULL;
  4178. int i;
  4179. int ret = 0;
  4180. dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
  4181. max_reqs, tbl->max_slots);
  4182. /* Does the newly negotiated max_reqs match the existing slot table? */
  4183. if (max_reqs != tbl->max_slots) {
  4184. ret = -ENOMEM;
  4185. new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
  4186. GFP_KERNEL);
  4187. if (!new)
  4188. goto out;
  4189. ret = 0;
  4190. kfree(tbl->slots);
  4191. }
  4192. spin_lock(&tbl->slot_tbl_lock);
  4193. if (new) {
  4194. tbl->slots = new;
  4195. tbl->max_slots = max_reqs;
  4196. }
  4197. for (i = 0; i < tbl->max_slots; ++i)
  4198. tbl->slots[i].seq_nr = ivalue;
  4199. spin_unlock(&tbl->slot_tbl_lock);
  4200. dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
  4201. tbl, tbl->slots, tbl->max_slots);
  4202. out:
  4203. dprintk("<-- %s: return %d\n", __func__, ret);
  4204. return ret;
  4205. }
  4206. /*
  4207. * Reset the forechannel and backchannel slot tables
  4208. */
  4209. static int nfs4_reset_slot_tables(struct nfs4_session *session)
  4210. {
  4211. int status;
  4212. status = nfs4_reset_slot_table(&session->fc_slot_table,
  4213. session->fc_attrs.max_reqs, 1);
  4214. if (status)
  4215. return status;
  4216. status = nfs4_reset_slot_table(&session->bc_slot_table,
  4217. session->bc_attrs.max_reqs, 0);
  4218. return status;
  4219. }
  4220. /* Destroy the slot table */
  4221. static void nfs4_destroy_slot_tables(struct nfs4_session *session)
  4222. {
  4223. if (session->fc_slot_table.slots != NULL) {
  4224. kfree(session->fc_slot_table.slots);
  4225. session->fc_slot_table.slots = NULL;
  4226. }
  4227. if (session->bc_slot_table.slots != NULL) {
  4228. kfree(session->bc_slot_table.slots);
  4229. session->bc_slot_table.slots = NULL;
  4230. }
  4231. return;
  4232. }
  4233. /*
  4234. * Initialize slot table
  4235. */
  4236. static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
  4237. int max_slots, int ivalue)
  4238. {
  4239. struct nfs4_slot *slot;
  4240. int ret = -ENOMEM;
  4241. BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
  4242. dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
  4243. slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_KERNEL);
  4244. if (!slot)
  4245. goto out;
  4246. ret = 0;
  4247. spin_lock(&tbl->slot_tbl_lock);
  4248. tbl->max_slots = max_slots;
  4249. tbl->slots = slot;
  4250. tbl->highest_used_slotid = -1; /* no slot is currently used */
  4251. spin_unlock(&tbl->slot_tbl_lock);
  4252. dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
  4253. tbl, tbl->slots, tbl->max_slots);
  4254. out:
  4255. dprintk("<-- %s: return %d\n", __func__, ret);
  4256. return ret;
  4257. }
  4258. /*
  4259. * Initialize the forechannel and backchannel tables
  4260. */
  4261. static int nfs4_init_slot_tables(struct nfs4_session *session)
  4262. {
  4263. struct nfs4_slot_table *tbl;
  4264. int status = 0;
  4265. tbl = &session->fc_slot_table;
  4266. if (tbl->slots == NULL) {
  4267. status = nfs4_init_slot_table(tbl,
  4268. session->fc_attrs.max_reqs, 1);
  4269. if (status)
  4270. return status;
  4271. }
  4272. tbl = &session->bc_slot_table;
  4273. if (tbl->slots == NULL) {
  4274. status = nfs4_init_slot_table(tbl,
  4275. session->bc_attrs.max_reqs, 0);
  4276. if (status)
  4277. nfs4_destroy_slot_tables(session);
  4278. }
  4279. return status;
  4280. }
  4281. struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
  4282. {
  4283. struct nfs4_session *session;
  4284. struct nfs4_slot_table *tbl;
  4285. session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
  4286. if (!session)
  4287. return NULL;
  4288. /*
  4289. * The create session reply races with the server back
  4290. * channel probe. Mark the client NFS_CS_SESSION_INITING
  4291. * so that the client back channel can find the
  4292. * nfs_client struct
  4293. */
  4294. clp->cl_cons_state = NFS_CS_SESSION_INITING;
  4295. init_completion(&session->complete);
  4296. tbl = &session->fc_slot_table;
  4297. tbl->highest_used_slotid = -1;
  4298. spin_lock_init(&tbl->slot_tbl_lock);
  4299. rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
  4300. tbl = &session->bc_slot_table;
  4301. tbl->highest_used_slotid = -1;
  4302. spin_lock_init(&tbl->slot_tbl_lock);
  4303. rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
  4304. session->clp = clp;
  4305. return session;
  4306. }
  4307. void nfs4_destroy_session(struct nfs4_session *session)
  4308. {
  4309. nfs4_proc_destroy_session(session);
  4310. dprintk("%s Destroy backchannel for xprt %p\n",
  4311. __func__, session->clp->cl_rpcclient->cl_xprt);
  4312. xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
  4313. NFS41_BC_MIN_CALLBACKS);
  4314. nfs4_destroy_slot_tables(session);
  4315. kfree(session);
  4316. }
  4317. /*
  4318. * Initialize the values to be used by the client in CREATE_SESSION
  4319. * If nfs4_init_session set the fore channel request and response sizes,
  4320. * use them.
  4321. *
  4322. * Set the back channel max_resp_sz_cached to zero to force the client to
  4323. * always set csa_cachethis to FALSE because the current implementation
  4324. * of the back channel DRC only supports caching the CB_SEQUENCE operation.
  4325. */
  4326. static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
  4327. {
  4328. struct nfs4_session *session = args->client->cl_session;
  4329. unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
  4330. mxresp_sz = session->fc_attrs.max_resp_sz;
  4331. if (mxrqst_sz == 0)
  4332. mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
  4333. if (mxresp_sz == 0)
  4334. mxresp_sz = NFS_MAX_FILE_IO_SIZE;
  4335. /* Fore channel attributes */
  4336. args->fc_attrs.headerpadsz = 0;
  4337. args->fc_attrs.max_rqst_sz = mxrqst_sz;
  4338. args->fc_attrs.max_resp_sz = mxresp_sz;
  4339. args->fc_attrs.max_ops = NFS4_MAX_OPS;
  4340. args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
  4341. dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
  4342. "max_ops=%u max_reqs=%u\n",
  4343. __func__,
  4344. args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
  4345. args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
  4346. /* Back channel attributes */
  4347. args->bc_attrs.headerpadsz = 0;
  4348. args->bc_attrs.max_rqst_sz = PAGE_SIZE;
  4349. args->bc_attrs.max_resp_sz = PAGE_SIZE;
  4350. args->bc_attrs.max_resp_sz_cached = 0;
  4351. args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
  4352. args->bc_attrs.max_reqs = 1;
  4353. dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
  4354. "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
  4355. __func__,
  4356. args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
  4357. args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
  4358. args->bc_attrs.max_reqs);
  4359. }
  4360. static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
  4361. {
  4362. if (rcvd <= sent)
  4363. return 0;
  4364. printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
  4365. "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
  4366. return -EINVAL;
  4367. }
  4368. #define _verify_fore_channel_attr(_name_) \
  4369. _verify_channel_attr("fore", #_name_, \
  4370. args->fc_attrs._name_, \
  4371. session->fc_attrs._name_)
  4372. #define _verify_back_channel_attr(_name_) \
  4373. _verify_channel_attr("back", #_name_, \
  4374. args->bc_attrs._name_, \
  4375. session->bc_attrs._name_)
  4376. /*
  4377. * The server is not allowed to increase the fore channel header pad size,
  4378. * maximum response size, or maximum number of operations.
  4379. *
  4380. * The back channel attributes are only negotiatied down: We send what the
  4381. * (back channel) server insists upon.
  4382. */
  4383. static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
  4384. struct nfs4_session *session)
  4385. {
  4386. int ret = 0;
  4387. ret |= _verify_fore_channel_attr(headerpadsz);
  4388. ret |= _verify_fore_channel_attr(max_resp_sz);
  4389. ret |= _verify_fore_channel_attr(max_ops);
  4390. ret |= _verify_back_channel_attr(headerpadsz);
  4391. ret |= _verify_back_channel_attr(max_rqst_sz);
  4392. ret |= _verify_back_channel_attr(max_resp_sz);
  4393. ret |= _verify_back_channel_attr(max_resp_sz_cached);
  4394. ret |= _verify_back_channel_attr(max_ops);
  4395. ret |= _verify_back_channel_attr(max_reqs);
  4396. return ret;
  4397. }
  4398. static int _nfs4_proc_create_session(struct nfs_client *clp)
  4399. {
  4400. struct nfs4_session *session = clp->cl_session;
  4401. struct nfs41_create_session_args args = {
  4402. .client = clp,
  4403. .cb_program = NFS4_CALLBACK,
  4404. };
  4405. struct nfs41_create_session_res res = {
  4406. .client = clp,
  4407. };
  4408. struct rpc_message msg = {
  4409. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
  4410. .rpc_argp = &args,
  4411. .rpc_resp = &res,
  4412. };
  4413. int status;
  4414. nfs4_init_channel_attrs(&args);
  4415. args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
  4416. status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
  4417. if (!status)
  4418. /* Verify the session's negotiated channel_attrs values */
  4419. status = nfs4_verify_channel_attrs(&args, session);
  4420. if (!status) {
  4421. /* Increment the clientid slot sequence id */
  4422. clp->cl_seqid++;
  4423. }
  4424. return status;
  4425. }
  4426. /*
  4427. * Issues a CREATE_SESSION operation to the server.
  4428. * It is the responsibility of the caller to verify the session is
  4429. * expired before calling this routine.
  4430. */
  4431. int nfs4_proc_create_session(struct nfs_client *clp)
  4432. {
  4433. int status;
  4434. unsigned *ptr;
  4435. struct nfs4_session *session = clp->cl_session;
  4436. dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
  4437. status = _nfs4_proc_create_session(clp);
  4438. if (status)
  4439. goto out;
  4440. /* Init and reset the fore channel */
  4441. status = nfs4_init_slot_tables(session);
  4442. dprintk("slot table initialization returned %d\n", status);
  4443. if (status)
  4444. goto out;
  4445. status = nfs4_reset_slot_tables(session);
  4446. dprintk("slot table reset returned %d\n", status);
  4447. if (status)
  4448. goto out;
  4449. ptr = (unsigned *)&session->sess_id.data[0];
  4450. dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
  4451. clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
  4452. out:
  4453. dprintk("<-- %s\n", __func__);
  4454. return status;
  4455. }
  4456. /*
  4457. * Issue the over-the-wire RPC DESTROY_SESSION.
  4458. * The caller must serialize access to this routine.
  4459. */
  4460. int nfs4_proc_destroy_session(struct nfs4_session *session)
  4461. {
  4462. int status = 0;
  4463. struct rpc_message msg;
  4464. dprintk("--> nfs4_proc_destroy_session\n");
  4465. /* session is still being setup */
  4466. if (session->clp->cl_cons_state != NFS_CS_READY)
  4467. return status;
  4468. msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
  4469. msg.rpc_argp = session;
  4470. msg.rpc_resp = NULL;
  4471. msg.rpc_cred = NULL;
  4472. status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
  4473. if (status)
  4474. printk(KERN_WARNING
  4475. "Got error %d from the server on DESTROY_SESSION. "
  4476. "Session has been destroyed regardless...\n", status);
  4477. dprintk("<-- nfs4_proc_destroy_session\n");
  4478. return status;
  4479. }
  4480. int nfs4_init_session(struct nfs_server *server)
  4481. {
  4482. struct nfs_client *clp = server->nfs_client;
  4483. struct nfs4_session *session;
  4484. unsigned int rsize, wsize;
  4485. int ret;
  4486. if (!nfs4_has_session(clp))
  4487. return 0;
  4488. rsize = server->rsize;
  4489. if (rsize == 0)
  4490. rsize = NFS_MAX_FILE_IO_SIZE;
  4491. wsize = server->wsize;
  4492. if (wsize == 0)
  4493. wsize = NFS_MAX_FILE_IO_SIZE;
  4494. session = clp->cl_session;
  4495. session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
  4496. session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
  4497. ret = nfs4_recover_expired_lease(server);
  4498. if (!ret)
  4499. ret = nfs4_check_client_ready(clp);
  4500. return ret;
  4501. }
  4502. /*
  4503. * Renew the cl_session lease.
  4504. */
  4505. static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
  4506. {
  4507. struct nfs4_sequence_args args;
  4508. struct nfs4_sequence_res res;
  4509. struct rpc_message msg = {
  4510. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
  4511. .rpc_argp = &args,
  4512. .rpc_resp = &res,
  4513. .rpc_cred = cred,
  4514. };
  4515. args.sa_cache_this = 0;
  4516. return nfs4_call_sync_sequence(clp, clp->cl_rpcclient, &msg, &args,
  4517. &res, args.sa_cache_this, 1);
  4518. }
  4519. static void nfs41_sequence_release(void *data)
  4520. {
  4521. struct nfs_client *clp = (struct nfs_client *)data;
  4522. if (atomic_read(&clp->cl_count) > 1)
  4523. nfs4_schedule_state_renewal(clp);
  4524. nfs_put_client(clp);
  4525. }
  4526. static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
  4527. {
  4528. struct nfs_client *clp = (struct nfs_client *)data;
  4529. nfs41_sequence_done(clp, task->tk_msg.rpc_resp, task->tk_status);
  4530. if (task->tk_status < 0) {
  4531. dprintk("%s ERROR %d\n", __func__, task->tk_status);
  4532. if (atomic_read(&clp->cl_count) == 1)
  4533. goto out;
  4534. if (_nfs4_async_handle_error(task, NULL, clp, NULL)
  4535. == -EAGAIN) {
  4536. nfs_restart_rpc(task, clp);
  4537. return;
  4538. }
  4539. }
  4540. dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
  4541. out:
  4542. kfree(task->tk_msg.rpc_argp);
  4543. kfree(task->tk_msg.rpc_resp);
  4544. dprintk("<-- %s\n", __func__);
  4545. }
  4546. static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
  4547. {
  4548. struct nfs_client *clp;
  4549. struct nfs4_sequence_args *args;
  4550. struct nfs4_sequence_res *res;
  4551. clp = (struct nfs_client *)data;
  4552. args = task->tk_msg.rpc_argp;
  4553. res = task->tk_msg.rpc_resp;
  4554. if (nfs4_setup_sequence(clp, args, res, 0, task))
  4555. return;
  4556. rpc_call_start(task);
  4557. }
  4558. static const struct rpc_call_ops nfs41_sequence_ops = {
  4559. .rpc_call_done = nfs41_sequence_call_done,
  4560. .rpc_call_prepare = nfs41_sequence_prepare,
  4561. .rpc_release = nfs41_sequence_release,
  4562. };
  4563. static int nfs41_proc_async_sequence(struct nfs_client *clp,
  4564. struct rpc_cred *cred)
  4565. {
  4566. struct nfs4_sequence_args *args;
  4567. struct nfs4_sequence_res *res;
  4568. struct rpc_message msg = {
  4569. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
  4570. .rpc_cred = cred,
  4571. };
  4572. if (!atomic_inc_not_zero(&clp->cl_count))
  4573. return -EIO;
  4574. args = kzalloc(sizeof(*args), GFP_KERNEL);
  4575. res = kzalloc(sizeof(*res), GFP_KERNEL);
  4576. if (!args || !res) {
  4577. kfree(args);
  4578. kfree(res);
  4579. nfs_put_client(clp);
  4580. return -ENOMEM;
  4581. }
  4582. res->sr_slotid = NFS4_MAX_SLOT_TABLE;
  4583. msg.rpc_argp = args;
  4584. msg.rpc_resp = res;
  4585. return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
  4586. &nfs41_sequence_ops, (void *)clp);
  4587. }
  4588. struct nfs4_reclaim_complete_data {
  4589. struct nfs_client *clp;
  4590. struct nfs41_reclaim_complete_args arg;
  4591. struct nfs41_reclaim_complete_res res;
  4592. };
  4593. static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
  4594. {
  4595. struct nfs4_reclaim_complete_data *calldata = data;
  4596. rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
  4597. if (nfs4_setup_sequence(calldata->clp, &calldata->arg.seq_args,
  4598. &calldata->res.seq_res, 0, task))
  4599. return;
  4600. rpc_call_start(task);
  4601. }
  4602. static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
  4603. {
  4604. struct nfs4_reclaim_complete_data *calldata = data;
  4605. struct nfs_client *clp = calldata->clp;
  4606. struct nfs4_sequence_res *res = &calldata->res.seq_res;
  4607. dprintk("--> %s\n", __func__);
  4608. nfs41_sequence_done(clp, res, task->tk_status);
  4609. switch (task->tk_status) {
  4610. case 0:
  4611. case -NFS4ERR_COMPLETE_ALREADY:
  4612. break;
  4613. case -NFS4ERR_BADSESSION:
  4614. case -NFS4ERR_DEADSESSION:
  4615. /*
  4616. * Handle the session error, but do not retry the operation, as
  4617. * we have no way of telling whether the clientid had to be
  4618. * reset before we got our reply. If reset, a new wave of
  4619. * reclaim operations will follow, containing their own reclaim
  4620. * complete. We don't want our retry to get on the way of
  4621. * recovery by incorrectly indicating to the server that we're
  4622. * done reclaiming state since the process had to be restarted.
  4623. */
  4624. _nfs4_async_handle_error(task, NULL, clp, NULL);
  4625. break;
  4626. default:
  4627. if (_nfs4_async_handle_error(
  4628. task, NULL, clp, NULL) == -EAGAIN) {
  4629. rpc_restart_call_prepare(task);
  4630. return;
  4631. }
  4632. }
  4633. dprintk("<-- %s\n", __func__);
  4634. }
  4635. static void nfs4_free_reclaim_complete_data(void *data)
  4636. {
  4637. struct nfs4_reclaim_complete_data *calldata = data;
  4638. kfree(calldata);
  4639. }
  4640. static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
  4641. .rpc_call_prepare = nfs4_reclaim_complete_prepare,
  4642. .rpc_call_done = nfs4_reclaim_complete_done,
  4643. .rpc_release = nfs4_free_reclaim_complete_data,
  4644. };
  4645. /*
  4646. * Issue a global reclaim complete.
  4647. */
  4648. static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
  4649. {
  4650. struct nfs4_reclaim_complete_data *calldata;
  4651. struct rpc_task *task;
  4652. struct rpc_message msg = {
  4653. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
  4654. };
  4655. struct rpc_task_setup task_setup_data = {
  4656. .rpc_client = clp->cl_rpcclient,
  4657. .rpc_message = &msg,
  4658. .callback_ops = &nfs4_reclaim_complete_call_ops,
  4659. .flags = RPC_TASK_ASYNC,
  4660. };
  4661. int status = -ENOMEM;
  4662. dprintk("--> %s\n", __func__);
  4663. calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
  4664. if (calldata == NULL)
  4665. goto out;
  4666. calldata->clp = clp;
  4667. calldata->arg.one_fs = 0;
  4668. calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
  4669. msg.rpc_argp = &calldata->arg;
  4670. msg.rpc_resp = &calldata->res;
  4671. task_setup_data.callback_data = calldata;
  4672. task = rpc_run_task(&task_setup_data);
  4673. if (IS_ERR(task)) {
  4674. status = PTR_ERR(task);
  4675. goto out;
  4676. }
  4677. rpc_put_task(task);
  4678. return 0;
  4679. out:
  4680. dprintk("<-- %s status=%d\n", __func__, status);
  4681. return status;
  4682. }
  4683. #endif /* CONFIG_NFS_V4_1 */
  4684. struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
  4685. .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
  4686. .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
  4687. .recover_open = nfs4_open_reclaim,
  4688. .recover_lock = nfs4_lock_reclaim,
  4689. .establish_clid = nfs4_init_clientid,
  4690. .get_clid_cred = nfs4_get_setclientid_cred,
  4691. };
  4692. #if defined(CONFIG_NFS_V4_1)
  4693. struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
  4694. .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
  4695. .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
  4696. .recover_open = nfs4_open_reclaim,
  4697. .recover_lock = nfs4_lock_reclaim,
  4698. .establish_clid = nfs41_init_clientid,
  4699. .get_clid_cred = nfs4_get_exchange_id_cred,
  4700. .reclaim_complete = nfs41_proc_reclaim_complete,
  4701. };
  4702. #endif /* CONFIG_NFS_V4_1 */
  4703. struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
  4704. .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
  4705. .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
  4706. .recover_open = nfs4_open_expired,
  4707. .recover_lock = nfs4_lock_expired,
  4708. .establish_clid = nfs4_init_clientid,
  4709. .get_clid_cred = nfs4_get_setclientid_cred,
  4710. };
  4711. #if defined(CONFIG_NFS_V4_1)
  4712. struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
  4713. .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
  4714. .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
  4715. .recover_open = nfs4_open_expired,
  4716. .recover_lock = nfs4_lock_expired,
  4717. .establish_clid = nfs41_init_clientid,
  4718. .get_clid_cred = nfs4_get_exchange_id_cred,
  4719. };
  4720. #endif /* CONFIG_NFS_V4_1 */
  4721. struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
  4722. .sched_state_renewal = nfs4_proc_async_renew,
  4723. .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
  4724. .renew_lease = nfs4_proc_renew,
  4725. };
  4726. #if defined(CONFIG_NFS_V4_1)
  4727. struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
  4728. .sched_state_renewal = nfs41_proc_async_sequence,
  4729. .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
  4730. .renew_lease = nfs4_proc_sequence,
  4731. };
  4732. #endif
  4733. /*
  4734. * Per minor version reboot and network partition recovery ops
  4735. */
  4736. struct nfs4_state_recovery_ops *nfs4_reboot_recovery_ops[] = {
  4737. &nfs40_reboot_recovery_ops,
  4738. #if defined(CONFIG_NFS_V4_1)
  4739. &nfs41_reboot_recovery_ops,
  4740. #endif
  4741. };
  4742. struct nfs4_state_recovery_ops *nfs4_nograce_recovery_ops[] = {
  4743. &nfs40_nograce_recovery_ops,
  4744. #if defined(CONFIG_NFS_V4_1)
  4745. &nfs41_nograce_recovery_ops,
  4746. #endif
  4747. };
  4748. struct nfs4_state_maintenance_ops *nfs4_state_renewal_ops[] = {
  4749. &nfs40_state_renewal_ops,
  4750. #if defined(CONFIG_NFS_V4_1)
  4751. &nfs41_state_renewal_ops,
  4752. #endif
  4753. };
  4754. static const struct inode_operations nfs4_file_inode_operations = {
  4755. .permission = nfs_permission,
  4756. .getattr = nfs_getattr,
  4757. .setattr = nfs_setattr,
  4758. .getxattr = nfs4_getxattr,
  4759. .setxattr = nfs4_setxattr,
  4760. .listxattr = nfs4_listxattr,
  4761. };
  4762. const struct nfs_rpc_ops nfs_v4_clientops = {
  4763. .version = 4, /* protocol version */
  4764. .dentry_ops = &nfs4_dentry_operations,
  4765. .dir_inode_ops = &nfs4_dir_inode_operations,
  4766. .file_inode_ops = &nfs4_file_inode_operations,
  4767. .getroot = nfs4_proc_get_root,
  4768. .getattr = nfs4_proc_getattr,
  4769. .setattr = nfs4_proc_setattr,
  4770. .lookupfh = nfs4_proc_lookupfh,
  4771. .lookup = nfs4_proc_lookup,
  4772. .access = nfs4_proc_access,
  4773. .readlink = nfs4_proc_readlink,
  4774. .create = nfs4_proc_create,
  4775. .remove = nfs4_proc_remove,
  4776. .unlink_setup = nfs4_proc_unlink_setup,
  4777. .unlink_done = nfs4_proc_unlink_done,
  4778. .rename = nfs4_proc_rename,
  4779. .link = nfs4_proc_link,
  4780. .symlink = nfs4_proc_symlink,
  4781. .mkdir = nfs4_proc_mkdir,
  4782. .rmdir = nfs4_proc_remove,
  4783. .readdir = nfs4_proc_readdir,
  4784. .mknod = nfs4_proc_mknod,
  4785. .statfs = nfs4_proc_statfs,
  4786. .fsinfo = nfs4_proc_fsinfo,
  4787. .pathconf = nfs4_proc_pathconf,
  4788. .set_capabilities = nfs4_server_capabilities,
  4789. .decode_dirent = nfs4_decode_dirent,
  4790. .read_setup = nfs4_proc_read_setup,
  4791. .read_done = nfs4_read_done,
  4792. .write_setup = nfs4_proc_write_setup,
  4793. .write_done = nfs4_write_done,
  4794. .commit_setup = nfs4_proc_commit_setup,
  4795. .commit_done = nfs4_commit_done,
  4796. .lock = nfs4_proc_lock,
  4797. .clear_acl_cache = nfs4_zap_acl_attr,
  4798. .close_context = nfs4_close_context,
  4799. };
  4800. /*
  4801. * Local variables:
  4802. * c-basic-offset: 8
  4803. * End:
  4804. */