nfs4proc.c 141 KB

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