nfs4proc.c 132 KB

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