nfs4proc.c 101 KB

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