nfs4proc.c 103 KB

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