nfs4proc.c 100 KB

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