nfs4proc.c 101 KB

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