nfs4proc.c 100 KB

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