nfs4proc.c 99 KB

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