nfs4proc.c 100 KB

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