nfs4proc.c 133 KB

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