nfs4proc.c 142 KB

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