nfs4proc.c 138 KB

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