nfs4proc.c 100 KB

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