nfs4proc.c 100 KB

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