nfs4proc.c 109 KB

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