nfs4proc.c 143 KB

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