nfs4proc.c 100 KB

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