nfs4proc.c 96 KB

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