nfs4proc.c 140 KB

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