nfs4proc.c 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037
  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 "nfs4_fs.h"
  50. #include "delegation.h"
  51. #define NFSDBG_FACILITY NFSDBG_PROC
  52. #define NFS4_POLL_RETRY_MIN (1*HZ)
  53. #define NFS4_POLL_RETRY_MAX (15*HZ)
  54. static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
  55. static int nfs4_async_handle_error(struct rpc_task *, struct nfs_server *);
  56. static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry);
  57. static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception);
  58. extern u32 *nfs4_decode_dirent(u32 *p, struct nfs_entry *entry, int plus);
  59. extern struct rpc_procinfo nfs4_procedures[];
  60. /* Prevent leaks of NFSv4 errors into userland */
  61. int nfs4_map_errors(int err)
  62. {
  63. if (err < -1000) {
  64. dprintk("%s could not handle NFSv4 error %d\n",
  65. __FUNCTION__, -err);
  66. return -EIO;
  67. }
  68. return err;
  69. }
  70. /*
  71. * This is our standard bitmap for GETATTR requests.
  72. */
  73. const u32 nfs4_fattr_bitmap[2] = {
  74. FATTR4_WORD0_TYPE
  75. | FATTR4_WORD0_CHANGE
  76. | FATTR4_WORD0_SIZE
  77. | FATTR4_WORD0_FSID
  78. | FATTR4_WORD0_FILEID,
  79. FATTR4_WORD1_MODE
  80. | FATTR4_WORD1_NUMLINKS
  81. | FATTR4_WORD1_OWNER
  82. | FATTR4_WORD1_OWNER_GROUP
  83. | FATTR4_WORD1_RAWDEV
  84. | FATTR4_WORD1_SPACE_USED
  85. | FATTR4_WORD1_TIME_ACCESS
  86. | FATTR4_WORD1_TIME_METADATA
  87. | FATTR4_WORD1_TIME_MODIFY
  88. };
  89. const u32 nfs4_statfs_bitmap[2] = {
  90. FATTR4_WORD0_FILES_AVAIL
  91. | FATTR4_WORD0_FILES_FREE
  92. | FATTR4_WORD0_FILES_TOTAL,
  93. FATTR4_WORD1_SPACE_AVAIL
  94. | FATTR4_WORD1_SPACE_FREE
  95. | FATTR4_WORD1_SPACE_TOTAL
  96. };
  97. const u32 nfs4_pathconf_bitmap[2] = {
  98. FATTR4_WORD0_MAXLINK
  99. | FATTR4_WORD0_MAXNAME,
  100. 0
  101. };
  102. const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
  103. | FATTR4_WORD0_MAXREAD
  104. | FATTR4_WORD0_MAXWRITE
  105. | FATTR4_WORD0_LEASE_TIME,
  106. 0
  107. };
  108. static void nfs4_setup_readdir(u64 cookie, u32 *verifier, struct dentry *dentry,
  109. struct nfs4_readdir_arg *readdir)
  110. {
  111. u32 *start, *p;
  112. BUG_ON(readdir->count < 80);
  113. if (cookie > 2) {
  114. readdir->cookie = cookie;
  115. memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
  116. return;
  117. }
  118. readdir->cookie = 0;
  119. memset(&readdir->verifier, 0, sizeof(readdir->verifier));
  120. if (cookie == 2)
  121. return;
  122. /*
  123. * NFSv4 servers do not return entries for '.' and '..'
  124. * Therefore, we fake these entries here. We let '.'
  125. * have cookie 0 and '..' have cookie 1. Note that
  126. * when talking to the server, we always send cookie 0
  127. * instead of 1 or 2.
  128. */
  129. start = p = (u32 *)kmap_atomic(*readdir->pages, KM_USER0);
  130. if (cookie == 0) {
  131. *p++ = xdr_one; /* next */
  132. *p++ = xdr_zero; /* cookie, first word */
  133. *p++ = xdr_one; /* cookie, second word */
  134. *p++ = xdr_one; /* entry len */
  135. memcpy(p, ".\0\0\0", 4); /* entry */
  136. p++;
  137. *p++ = xdr_one; /* bitmap length */
  138. *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
  139. *p++ = htonl(8); /* attribute buffer length */
  140. p = xdr_encode_hyper(p, dentry->d_inode->i_ino);
  141. }
  142. *p++ = xdr_one; /* next */
  143. *p++ = xdr_zero; /* cookie, first word */
  144. *p++ = xdr_two; /* cookie, second word */
  145. *p++ = xdr_two; /* entry len */
  146. memcpy(p, "..\0\0", 4); /* entry */
  147. p++;
  148. *p++ = xdr_one; /* bitmap length */
  149. *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
  150. *p++ = htonl(8); /* attribute buffer length */
  151. p = xdr_encode_hyper(p, dentry->d_parent->d_inode->i_ino);
  152. readdir->pgbase = (char *)p - (char *)start;
  153. readdir->count -= readdir->pgbase;
  154. kunmap_atomic(start, KM_USER0);
  155. }
  156. static void
  157. renew_lease(struct nfs_server *server, unsigned long timestamp)
  158. {
  159. struct nfs4_client *clp = server->nfs4_state;
  160. spin_lock(&clp->cl_lock);
  161. if (time_before(clp->cl_last_renewal,timestamp))
  162. clp->cl_last_renewal = timestamp;
  163. spin_unlock(&clp->cl_lock);
  164. }
  165. static void update_changeattr(struct inode *inode, struct nfs4_change_info *cinfo)
  166. {
  167. struct nfs_inode *nfsi = NFS_I(inode);
  168. if (cinfo->before == nfsi->change_attr && cinfo->atomic)
  169. nfsi->change_attr = cinfo->after;
  170. }
  171. static void update_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
  172. {
  173. struct inode *inode = state->inode;
  174. open_flags &= (FMODE_READ|FMODE_WRITE);
  175. /* Protect against nfs4_find_state() */
  176. spin_lock(&inode->i_lock);
  177. state->state |= open_flags;
  178. /* NB! List reordering - see the reclaim code for why. */
  179. if ((open_flags & FMODE_WRITE) && 0 == state->nwriters++)
  180. list_move(&state->open_states, &state->owner->so_states);
  181. if (open_flags & FMODE_READ)
  182. state->nreaders++;
  183. memcpy(&state->stateid, stateid, sizeof(state->stateid));
  184. spin_unlock(&inode->i_lock);
  185. }
  186. /*
  187. * OPEN_RECLAIM:
  188. * reclaim state on the server after a reboot.
  189. * Assumes caller is holding the sp->so_sem
  190. */
  191. static int _nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
  192. {
  193. struct inode *inode = state->inode;
  194. struct nfs_server *server = NFS_SERVER(inode);
  195. struct nfs_delegation *delegation = NFS_I(inode)->delegation;
  196. struct nfs_openargs o_arg = {
  197. .fh = NFS_FH(inode),
  198. .seqid = sp->so_seqid,
  199. .id = sp->so_id,
  200. .open_flags = state->state,
  201. .clientid = server->nfs4_state->cl_clientid,
  202. .claim = NFS4_OPEN_CLAIM_PREVIOUS,
  203. .bitmask = server->attr_bitmask,
  204. };
  205. struct nfs_openres o_res = {
  206. .server = server, /* Grrr */
  207. };
  208. struct rpc_message msg = {
  209. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR],
  210. .rpc_argp = &o_arg,
  211. .rpc_resp = &o_res,
  212. .rpc_cred = sp->so_cred,
  213. };
  214. int status;
  215. if (delegation != NULL) {
  216. if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
  217. memcpy(&state->stateid, &delegation->stateid,
  218. sizeof(state->stateid));
  219. set_bit(NFS_DELEGATED_STATE, &state->flags);
  220. return 0;
  221. }
  222. o_arg.u.delegation_type = delegation->type;
  223. }
  224. status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
  225. nfs4_increment_seqid(status, sp);
  226. if (status == 0) {
  227. memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid));
  228. if (o_res.delegation_type != 0) {
  229. nfs_inode_reclaim_delegation(inode, sp->so_cred, &o_res);
  230. /* Did the server issue an immediate delegation recall? */
  231. if (o_res.do_recall)
  232. nfs_async_inode_return_delegation(inode, &o_res.stateid);
  233. }
  234. }
  235. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  236. /* Ensure we update the inode attributes */
  237. NFS_CACHEINV(inode);
  238. return status;
  239. }
  240. static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
  241. {
  242. struct nfs_server *server = NFS_SERVER(state->inode);
  243. struct nfs4_exception exception = { };
  244. int err;
  245. do {
  246. err = _nfs4_open_reclaim(sp, state);
  247. if (err != -NFS4ERR_DELAY)
  248. break;
  249. nfs4_handle_exception(server, err, &exception);
  250. } while (exception.retry);
  251. return err;
  252. }
  253. static int _nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
  254. {
  255. struct nfs4_state_owner *sp = state->owner;
  256. struct inode *inode = dentry->d_inode;
  257. struct nfs_server *server = NFS_SERVER(inode);
  258. struct dentry *parent = dget_parent(dentry);
  259. struct nfs_openargs arg = {
  260. .fh = NFS_FH(parent->d_inode),
  261. .clientid = server->nfs4_state->cl_clientid,
  262. .name = &dentry->d_name,
  263. .id = sp->so_id,
  264. .server = server,
  265. .bitmask = server->attr_bitmask,
  266. .claim = NFS4_OPEN_CLAIM_DELEGATE_CUR,
  267. };
  268. struct nfs_openres res = {
  269. .server = server,
  270. };
  271. struct rpc_message msg = {
  272. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR],
  273. .rpc_argp = &arg,
  274. .rpc_resp = &res,
  275. .rpc_cred = sp->so_cred,
  276. };
  277. int status = 0;
  278. down(&sp->so_sema);
  279. if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
  280. goto out;
  281. if (state->state == 0)
  282. goto out;
  283. arg.seqid = sp->so_seqid;
  284. arg.open_flags = state->state;
  285. memcpy(arg.u.delegation.data, state->stateid.data, sizeof(arg.u.delegation.data));
  286. status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
  287. nfs4_increment_seqid(status, sp);
  288. if (status >= 0) {
  289. memcpy(state->stateid.data, res.stateid.data,
  290. sizeof(state->stateid.data));
  291. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  292. }
  293. out:
  294. up(&sp->so_sema);
  295. dput(parent);
  296. return status;
  297. }
  298. int nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
  299. {
  300. struct nfs4_exception exception = { };
  301. struct nfs_server *server = NFS_SERVER(dentry->d_inode);
  302. int err;
  303. do {
  304. err = _nfs4_open_delegation_recall(dentry, state);
  305. switch (err) {
  306. case 0:
  307. return err;
  308. case -NFS4ERR_STALE_CLIENTID:
  309. case -NFS4ERR_STALE_STATEID:
  310. case -NFS4ERR_EXPIRED:
  311. /* Don't recall a delegation if it was lost */
  312. nfs4_schedule_state_recovery(server->nfs4_state);
  313. return err;
  314. }
  315. err = nfs4_handle_exception(server, err, &exception);
  316. } while (exception.retry);
  317. return err;
  318. }
  319. static inline int _nfs4_proc_open_confirm(struct rpc_clnt *clnt, const struct nfs_fh *fh, struct nfs4_state_owner *sp, nfs4_stateid *stateid)
  320. {
  321. struct nfs_open_confirmargs arg = {
  322. .fh = fh,
  323. .seqid = sp->so_seqid,
  324. .stateid = *stateid,
  325. };
  326. struct nfs_open_confirmres res;
  327. struct rpc_message msg = {
  328. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
  329. .rpc_argp = &arg,
  330. .rpc_resp = &res,
  331. .rpc_cred = sp->so_cred,
  332. };
  333. int status;
  334. status = rpc_call_sync(clnt, &msg, RPC_TASK_NOINTR);
  335. nfs4_increment_seqid(status, sp);
  336. if (status >= 0)
  337. memcpy(stateid, &res.stateid, sizeof(*stateid));
  338. return status;
  339. }
  340. static int _nfs4_proc_open(struct inode *dir, struct nfs4_state_owner *sp, struct nfs_openargs *o_arg, struct nfs_openres *o_res)
  341. {
  342. struct nfs_server *server = NFS_SERVER(dir);
  343. struct rpc_message msg = {
  344. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
  345. .rpc_argp = o_arg,
  346. .rpc_resp = o_res,
  347. .rpc_cred = sp->so_cred,
  348. };
  349. int status;
  350. /* Update sequence id. The caller must serialize! */
  351. o_arg->seqid = sp->so_seqid;
  352. o_arg->id = sp->so_id;
  353. o_arg->clientid = sp->so_client->cl_clientid;
  354. status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
  355. nfs4_increment_seqid(status, sp);
  356. if (status != 0)
  357. goto out;
  358. update_changeattr(dir, &o_res->cinfo);
  359. if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
  360. status = _nfs4_proc_open_confirm(server->client, &o_res->fh,
  361. sp, &o_res->stateid);
  362. if (status != 0)
  363. goto out;
  364. }
  365. if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
  366. status = server->rpc_ops->getattr(server, &o_res->fh, o_res->f_attr);
  367. out:
  368. return status;
  369. }
  370. static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags)
  371. {
  372. struct nfs_access_entry cache;
  373. int mask = 0;
  374. int status;
  375. if (openflags & FMODE_READ)
  376. mask |= MAY_READ;
  377. if (openflags & FMODE_WRITE)
  378. mask |= MAY_WRITE;
  379. status = nfs_access_get_cached(inode, cred, &cache);
  380. if (status == 0)
  381. goto out;
  382. /* Be clever: ask server to check for all possible rights */
  383. cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
  384. cache.cred = cred;
  385. cache.jiffies = jiffies;
  386. status = _nfs4_proc_access(inode, &cache);
  387. if (status != 0)
  388. return status;
  389. nfs_access_add_cache(inode, &cache);
  390. out:
  391. if ((cache.mask & mask) == mask)
  392. return 0;
  393. return -EACCES;
  394. }
  395. /*
  396. * OPEN_EXPIRED:
  397. * reclaim state on the server after a network partition.
  398. * Assumes caller holds the appropriate lock
  399. */
  400. static int _nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
  401. {
  402. struct dentry *parent = dget_parent(dentry);
  403. struct inode *dir = parent->d_inode;
  404. struct inode *inode = state->inode;
  405. struct nfs_server *server = NFS_SERVER(dir);
  406. struct nfs_delegation *delegation = NFS_I(inode)->delegation;
  407. struct nfs_fattr f_attr = {
  408. .valid = 0,
  409. };
  410. struct nfs_openargs o_arg = {
  411. .fh = NFS_FH(dir),
  412. .open_flags = state->state,
  413. .name = &dentry->d_name,
  414. .bitmask = server->attr_bitmask,
  415. .claim = NFS4_OPEN_CLAIM_NULL,
  416. };
  417. struct nfs_openres o_res = {
  418. .f_attr = &f_attr,
  419. .server = server,
  420. };
  421. int status = 0;
  422. if (delegation != NULL && !(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
  423. status = _nfs4_do_access(inode, sp->so_cred, state->state);
  424. if (status < 0)
  425. goto out;
  426. memcpy(&state->stateid, &delegation->stateid, sizeof(state->stateid));
  427. set_bit(NFS_DELEGATED_STATE, &state->flags);
  428. goto out;
  429. }
  430. status = _nfs4_proc_open(dir, sp, &o_arg, &o_res);
  431. if (status != 0)
  432. goto out_nodeleg;
  433. /* Check if files differ */
  434. if ((f_attr.mode & S_IFMT) != (inode->i_mode & S_IFMT))
  435. goto out_stale;
  436. /* Has the file handle changed? */
  437. if (nfs_compare_fh(&o_res.fh, NFS_FH(inode)) != 0) {
  438. /* Verify if the change attributes are the same */
  439. if (f_attr.change_attr != NFS_I(inode)->change_attr)
  440. goto out_stale;
  441. if (nfs_size_to_loff_t(f_attr.size) != inode->i_size)
  442. goto out_stale;
  443. /* Lets just pretend that this is the same file */
  444. nfs_copy_fh(NFS_FH(inode), &o_res.fh);
  445. NFS_I(inode)->fileid = f_attr.fileid;
  446. }
  447. memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid));
  448. if (o_res.delegation_type != 0) {
  449. if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM))
  450. nfs_inode_set_delegation(inode, sp->so_cred, &o_res);
  451. else
  452. nfs_inode_reclaim_delegation(inode, sp->so_cred, &o_res);
  453. }
  454. out_nodeleg:
  455. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  456. out:
  457. dput(parent);
  458. return status;
  459. out_stale:
  460. status = -ESTALE;
  461. /* Invalidate the state owner so we don't ever use it again */
  462. nfs4_drop_state_owner(sp);
  463. d_drop(dentry);
  464. /* Should we be trying to close that stateid? */
  465. goto out_nodeleg;
  466. }
  467. static inline int nfs4_do_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
  468. {
  469. struct nfs_server *server = NFS_SERVER(dentry->d_inode);
  470. struct nfs4_exception exception = { };
  471. int err;
  472. do {
  473. err = _nfs4_open_expired(sp, state, dentry);
  474. if (err == -NFS4ERR_DELAY)
  475. nfs4_handle_exception(server, err, &exception);
  476. } while (exception.retry);
  477. return err;
  478. }
  479. static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
  480. {
  481. struct nfs_inode *nfsi = NFS_I(state->inode);
  482. struct nfs_open_context *ctx;
  483. int status;
  484. spin_lock(&state->inode->i_lock);
  485. list_for_each_entry(ctx, &nfsi->open_files, list) {
  486. if (ctx->state != state)
  487. continue;
  488. get_nfs_open_context(ctx);
  489. spin_unlock(&state->inode->i_lock);
  490. status = nfs4_do_open_expired(sp, state, ctx->dentry);
  491. put_nfs_open_context(ctx);
  492. return status;
  493. }
  494. spin_unlock(&state->inode->i_lock);
  495. return -ENOENT;
  496. }
  497. /*
  498. * Returns an nfs4_state + an extra reference to the inode
  499. */
  500. static int _nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred, struct nfs4_state **res)
  501. {
  502. struct nfs_delegation *delegation;
  503. struct nfs_server *server = NFS_SERVER(inode);
  504. struct nfs4_client *clp = server->nfs4_state;
  505. struct nfs_inode *nfsi = NFS_I(inode);
  506. struct nfs4_state_owner *sp = NULL;
  507. struct nfs4_state *state = NULL;
  508. int open_flags = flags & (FMODE_READ|FMODE_WRITE);
  509. int err;
  510. /* Protect against reboot recovery - NOTE ORDER! */
  511. down_read(&clp->cl_sem);
  512. /* Protect against delegation recall */
  513. down_read(&nfsi->rwsem);
  514. delegation = NFS_I(inode)->delegation;
  515. err = -ENOENT;
  516. if (delegation == NULL || (delegation->type & open_flags) != open_flags)
  517. goto out_err;
  518. err = -ENOMEM;
  519. if (!(sp = nfs4_get_state_owner(server, cred))) {
  520. dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__);
  521. goto out_err;
  522. }
  523. down(&sp->so_sema);
  524. state = nfs4_get_open_state(inode, sp);
  525. if (state == NULL)
  526. goto out_err;
  527. err = -ENOENT;
  528. if ((state->state & open_flags) == open_flags) {
  529. spin_lock(&inode->i_lock);
  530. if (open_flags & FMODE_READ)
  531. state->nreaders++;
  532. if (open_flags & FMODE_WRITE)
  533. state->nwriters++;
  534. spin_unlock(&inode->i_lock);
  535. goto out_ok;
  536. } else if (state->state != 0)
  537. goto out_err;
  538. lock_kernel();
  539. err = _nfs4_do_access(inode, cred, open_flags);
  540. unlock_kernel();
  541. if (err != 0)
  542. goto out_err;
  543. set_bit(NFS_DELEGATED_STATE, &state->flags);
  544. update_open_stateid(state, &delegation->stateid, open_flags);
  545. out_ok:
  546. up(&sp->so_sema);
  547. nfs4_put_state_owner(sp);
  548. up_read(&nfsi->rwsem);
  549. up_read(&clp->cl_sem);
  550. igrab(inode);
  551. *res = state;
  552. return 0;
  553. out_err:
  554. if (sp != NULL) {
  555. if (state != NULL)
  556. nfs4_put_open_state(state);
  557. up(&sp->so_sema);
  558. nfs4_put_state_owner(sp);
  559. }
  560. up_read(&nfsi->rwsem);
  561. up_read(&clp->cl_sem);
  562. return err;
  563. }
  564. static struct nfs4_state *nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred)
  565. {
  566. struct nfs4_exception exception = { };
  567. struct nfs4_state *res;
  568. int err;
  569. do {
  570. err = _nfs4_open_delegated(inode, flags, cred, &res);
  571. if (err == 0)
  572. break;
  573. res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(inode),
  574. err, &exception));
  575. } while (exception.retry);
  576. return res;
  577. }
  578. /*
  579. * Returns an nfs4_state + an referenced inode
  580. */
  581. static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
  582. {
  583. struct nfs4_state_owner *sp;
  584. struct nfs4_state *state = NULL;
  585. struct nfs_server *server = NFS_SERVER(dir);
  586. struct nfs4_client *clp = server->nfs4_state;
  587. struct inode *inode = NULL;
  588. int status;
  589. struct nfs_fattr f_attr = {
  590. .valid = 0,
  591. };
  592. struct nfs_openargs o_arg = {
  593. .fh = NFS_FH(dir),
  594. .open_flags = flags,
  595. .name = &dentry->d_name,
  596. .server = server,
  597. .bitmask = server->attr_bitmask,
  598. .claim = NFS4_OPEN_CLAIM_NULL,
  599. };
  600. struct nfs_openres o_res = {
  601. .f_attr = &f_attr,
  602. .server = server,
  603. };
  604. /* Protect against reboot recovery conflicts */
  605. down_read(&clp->cl_sem);
  606. status = -ENOMEM;
  607. if (!(sp = nfs4_get_state_owner(server, cred))) {
  608. dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
  609. goto out_err;
  610. }
  611. if (flags & O_EXCL) {
  612. u32 *p = (u32 *) o_arg.u.verifier.data;
  613. p[0] = jiffies;
  614. p[1] = current->pid;
  615. } else
  616. o_arg.u.attrs = sattr;
  617. /* Serialization for the sequence id */
  618. down(&sp->so_sema);
  619. status = _nfs4_proc_open(dir, sp, &o_arg, &o_res);
  620. if (status != 0)
  621. goto out_err;
  622. status = -ENOMEM;
  623. inode = nfs_fhget(dir->i_sb, &o_res.fh, &f_attr);
  624. if (!inode)
  625. goto out_err;
  626. state = nfs4_get_open_state(inode, sp);
  627. if (!state)
  628. goto out_err;
  629. update_open_stateid(state, &o_res.stateid, flags);
  630. if (o_res.delegation_type != 0)
  631. nfs_inode_set_delegation(inode, cred, &o_res);
  632. up(&sp->so_sema);
  633. nfs4_put_state_owner(sp);
  634. up_read(&clp->cl_sem);
  635. *res = state;
  636. return 0;
  637. out_err:
  638. if (sp != NULL) {
  639. if (state != NULL)
  640. nfs4_put_open_state(state);
  641. up(&sp->so_sema);
  642. nfs4_put_state_owner(sp);
  643. }
  644. /* Note: clp->cl_sem must be released before nfs4_put_open_state()! */
  645. up_read(&clp->cl_sem);
  646. if (inode != NULL)
  647. iput(inode);
  648. *res = NULL;
  649. return status;
  650. }
  651. static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred)
  652. {
  653. struct nfs4_exception exception = { };
  654. struct nfs4_state *res;
  655. int status;
  656. do {
  657. status = _nfs4_do_open(dir, dentry, flags, sattr, cred, &res);
  658. if (status == 0)
  659. break;
  660. /* NOTE: BAD_SEQID means the server and client disagree about the
  661. * book-keeping w.r.t. state-changing operations
  662. * (OPEN/CLOSE/LOCK/LOCKU...)
  663. * It is actually a sign of a bug on the client or on the server.
  664. *
  665. * If we receive a BAD_SEQID error in the particular case of
  666. * doing an OPEN, we assume that nfs4_increment_seqid() will
  667. * have unhashed the old state_owner for us, and that we can
  668. * therefore safely retry using a new one. We should still warn
  669. * the user though...
  670. */
  671. if (status == -NFS4ERR_BAD_SEQID) {
  672. printk(KERN_WARNING "NFS: v4 server returned a bad sequence-id error!\n");
  673. exception.retry = 1;
  674. continue;
  675. }
  676. res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
  677. status, &exception));
  678. } while (exception.retry);
  679. return res;
  680. }
  681. static int _nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
  682. struct nfs_fh *fhandle, struct iattr *sattr,
  683. struct nfs4_state *state)
  684. {
  685. struct nfs_setattrargs arg = {
  686. .fh = fhandle,
  687. .iap = sattr,
  688. .server = server,
  689. .bitmask = server->attr_bitmask,
  690. };
  691. struct nfs_setattrres res = {
  692. .fattr = fattr,
  693. .server = server,
  694. };
  695. struct rpc_message msg = {
  696. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
  697. .rpc_argp = &arg,
  698. .rpc_resp = &res,
  699. };
  700. int status;
  701. fattr->valid = 0;
  702. if (state != NULL) {
  703. msg.rpc_cred = state->owner->so_cred;
  704. nfs4_copy_stateid(&arg.stateid, state, current->files);
  705. } else
  706. memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
  707. status = rpc_call_sync(server->client, &msg, 0);
  708. return status;
  709. }
  710. static int nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
  711. struct nfs_fh *fhandle, struct iattr *sattr,
  712. struct nfs4_state *state)
  713. {
  714. struct nfs4_exception exception = { };
  715. int err;
  716. do {
  717. err = nfs4_handle_exception(server,
  718. _nfs4_do_setattr(server, fattr, fhandle, sattr,
  719. state),
  720. &exception);
  721. } while (exception.retry);
  722. return err;
  723. }
  724. struct nfs4_closedata {
  725. struct inode *inode;
  726. struct nfs4_state *state;
  727. struct nfs_closeargs arg;
  728. struct nfs_closeres res;
  729. };
  730. static void nfs4_close_done(struct rpc_task *task)
  731. {
  732. struct nfs4_closedata *calldata = (struct nfs4_closedata *)task->tk_calldata;
  733. struct nfs4_state *state = calldata->state;
  734. struct nfs4_state_owner *sp = state->owner;
  735. struct nfs_server *server = NFS_SERVER(calldata->inode);
  736. /* hmm. we are done with the inode, and in the process of freeing
  737. * the state_owner. we keep this around to process errors
  738. */
  739. nfs4_increment_seqid(task->tk_status, sp);
  740. switch (task->tk_status) {
  741. case 0:
  742. memcpy(&state->stateid, &calldata->res.stateid,
  743. sizeof(state->stateid));
  744. break;
  745. case -NFS4ERR_STALE_STATEID:
  746. case -NFS4ERR_EXPIRED:
  747. state->state = calldata->arg.open_flags;
  748. nfs4_schedule_state_recovery(server->nfs4_state);
  749. break;
  750. default:
  751. if (nfs4_async_handle_error(task, server) == -EAGAIN) {
  752. rpc_restart_call(task);
  753. return;
  754. }
  755. }
  756. state->state = calldata->arg.open_flags;
  757. nfs4_put_open_state(state);
  758. up(&sp->so_sema);
  759. nfs4_put_state_owner(sp);
  760. up_read(&server->nfs4_state->cl_sem);
  761. kfree(calldata);
  762. }
  763. static inline int nfs4_close_call(struct rpc_clnt *clnt, struct nfs4_closedata *calldata)
  764. {
  765. struct rpc_message msg = {
  766. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
  767. .rpc_argp = &calldata->arg,
  768. .rpc_resp = &calldata->res,
  769. .rpc_cred = calldata->state->owner->so_cred,
  770. };
  771. if (calldata->arg.open_flags != 0)
  772. msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
  773. return rpc_call_async(clnt, &msg, 0, nfs4_close_done, calldata);
  774. }
  775. /*
  776. * It is possible for data to be read/written from a mem-mapped file
  777. * after the sys_close call (which hits the vfs layer as a flush).
  778. * This means that we can't safely call nfsv4 close on a file until
  779. * the inode is cleared. This in turn means that we are not good
  780. * NFSv4 citizens - we do not indicate to the server to update the file's
  781. * share state even when we are done with one of the three share
  782. * stateid's in the inode.
  783. *
  784. * NOTE: Caller must be holding the sp->so_owner semaphore!
  785. */
  786. int nfs4_do_close(struct inode *inode, struct nfs4_state *state, mode_t mode)
  787. {
  788. struct nfs4_closedata *calldata;
  789. int status;
  790. /* Tell caller we're done */
  791. if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
  792. state->state = mode;
  793. return 0;
  794. }
  795. calldata = (struct nfs4_closedata *)kmalloc(sizeof(*calldata), GFP_KERNEL);
  796. if (calldata == NULL)
  797. return -ENOMEM;
  798. calldata->inode = inode;
  799. calldata->state = state;
  800. calldata->arg.fh = NFS_FH(inode);
  801. /* Serialization for the sequence id */
  802. calldata->arg.seqid = state->owner->so_seqid;
  803. calldata->arg.open_flags = mode;
  804. memcpy(&calldata->arg.stateid, &state->stateid,
  805. sizeof(calldata->arg.stateid));
  806. status = nfs4_close_call(NFS_SERVER(inode)->client, calldata);
  807. /*
  808. * Return -EINPROGRESS on success in order to indicate to the
  809. * caller that an asynchronous RPC call has been launched, and
  810. * that it will release the semaphores on completion.
  811. */
  812. return (status == 0) ? -EINPROGRESS : status;
  813. }
  814. struct inode *
  815. nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  816. {
  817. struct iattr attr;
  818. struct rpc_cred *cred;
  819. struct nfs4_state *state;
  820. if (nd->flags & LOOKUP_CREATE) {
  821. attr.ia_mode = nd->intent.open.create_mode;
  822. attr.ia_valid = ATTR_MODE;
  823. if (!IS_POSIXACL(dir))
  824. attr.ia_mode &= ~current->fs->umask;
  825. } else {
  826. attr.ia_valid = 0;
  827. BUG_ON(nd->intent.open.flags & O_CREAT);
  828. }
  829. cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
  830. if (IS_ERR(cred))
  831. return (struct inode *)cred;
  832. state = nfs4_do_open(dir, dentry, nd->intent.open.flags, &attr, cred);
  833. put_rpccred(cred);
  834. if (IS_ERR(state))
  835. return (struct inode *)state;
  836. return state->inode;
  837. }
  838. int
  839. nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags)
  840. {
  841. struct rpc_cred *cred;
  842. struct nfs4_state *state;
  843. struct inode *inode;
  844. cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
  845. if (IS_ERR(cred))
  846. return PTR_ERR(cred);
  847. state = nfs4_open_delegated(dentry->d_inode, openflags, cred);
  848. if (IS_ERR(state))
  849. state = nfs4_do_open(dir, dentry, openflags, NULL, cred);
  850. put_rpccred(cred);
  851. if (state == ERR_PTR(-ENOENT) && dentry->d_inode == 0)
  852. return 1;
  853. if (IS_ERR(state))
  854. return 0;
  855. inode = state->inode;
  856. if (inode == dentry->d_inode) {
  857. iput(inode);
  858. return 1;
  859. }
  860. d_drop(dentry);
  861. nfs4_close_state(state, openflags);
  862. iput(inode);
  863. return 0;
  864. }
  865. static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
  866. {
  867. struct nfs4_server_caps_res res = {};
  868. struct rpc_message msg = {
  869. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
  870. .rpc_argp = fhandle,
  871. .rpc_resp = &res,
  872. };
  873. int status;
  874. status = rpc_call_sync(server->client, &msg, 0);
  875. if (status == 0) {
  876. memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
  877. if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
  878. server->caps |= NFS_CAP_ACLS;
  879. if (res.has_links != 0)
  880. server->caps |= NFS_CAP_HARDLINKS;
  881. if (res.has_symlinks != 0)
  882. server->caps |= NFS_CAP_SYMLINKS;
  883. server->acl_bitmask = res.acl_bitmask;
  884. }
  885. return status;
  886. }
  887. static int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
  888. {
  889. struct nfs4_exception exception = { };
  890. int err;
  891. do {
  892. err = nfs4_handle_exception(server,
  893. _nfs4_server_capabilities(server, fhandle),
  894. &exception);
  895. } while (exception.retry);
  896. return err;
  897. }
  898. static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
  899. struct nfs_fsinfo *info)
  900. {
  901. struct nfs_fattr * fattr = info->fattr;
  902. struct nfs4_lookup_root_arg args = {
  903. .bitmask = nfs4_fattr_bitmap,
  904. };
  905. struct nfs4_lookup_res res = {
  906. .server = server,
  907. .fattr = fattr,
  908. .fh = fhandle,
  909. };
  910. struct rpc_message msg = {
  911. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
  912. .rpc_argp = &args,
  913. .rpc_resp = &res,
  914. };
  915. fattr->valid = 0;
  916. return rpc_call_sync(server->client, &msg, 0);
  917. }
  918. static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
  919. struct nfs_fsinfo *info)
  920. {
  921. struct nfs4_exception exception = { };
  922. int err;
  923. do {
  924. err = nfs4_handle_exception(server,
  925. _nfs4_lookup_root(server, fhandle, info),
  926. &exception);
  927. } while (exception.retry);
  928. return err;
  929. }
  930. static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
  931. struct nfs_fsinfo *info)
  932. {
  933. struct nfs_fattr * fattr = info->fattr;
  934. unsigned char * p;
  935. struct qstr q;
  936. struct nfs4_lookup_arg args = {
  937. .dir_fh = fhandle,
  938. .name = &q,
  939. .bitmask = nfs4_fattr_bitmap,
  940. };
  941. struct nfs4_lookup_res res = {
  942. .server = server,
  943. .fattr = fattr,
  944. .fh = fhandle,
  945. };
  946. struct rpc_message msg = {
  947. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
  948. .rpc_argp = &args,
  949. .rpc_resp = &res,
  950. };
  951. int status;
  952. /*
  953. * Now we do a separate LOOKUP for each component of the mount path.
  954. * The LOOKUPs are done separately so that we can conveniently
  955. * catch an ERR_WRONGSEC if it occurs along the way...
  956. */
  957. status = nfs4_lookup_root(server, fhandle, info);
  958. if (status)
  959. goto out;
  960. p = server->mnt_path;
  961. for (;;) {
  962. struct nfs4_exception exception = { };
  963. while (*p == '/')
  964. p++;
  965. if (!*p)
  966. break;
  967. q.name = p;
  968. while (*p && (*p != '/'))
  969. p++;
  970. q.len = p - q.name;
  971. do {
  972. fattr->valid = 0;
  973. status = nfs4_handle_exception(server,
  974. rpc_call_sync(server->client, &msg, 0),
  975. &exception);
  976. } while (exception.retry);
  977. if (status == 0)
  978. continue;
  979. if (status == -ENOENT) {
  980. printk(KERN_NOTICE "NFS: mount path %s does not exist!\n", server->mnt_path);
  981. printk(KERN_NOTICE "NFS: suggestion: try mounting '/' instead.\n");
  982. }
  983. break;
  984. }
  985. if (status == 0)
  986. status = nfs4_server_capabilities(server, fhandle);
  987. if (status == 0)
  988. status = nfs4_do_fsinfo(server, fhandle, info);
  989. out:
  990. return status;
  991. }
  992. static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  993. {
  994. struct nfs4_getattr_arg args = {
  995. .fh = fhandle,
  996. .bitmask = server->attr_bitmask,
  997. };
  998. struct nfs4_getattr_res res = {
  999. .fattr = fattr,
  1000. .server = server,
  1001. };
  1002. struct rpc_message msg = {
  1003. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
  1004. .rpc_argp = &args,
  1005. .rpc_resp = &res,
  1006. };
  1007. fattr->valid = 0;
  1008. return rpc_call_sync(server->client, &msg, 0);
  1009. }
  1010. static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  1011. {
  1012. struct nfs4_exception exception = { };
  1013. int err;
  1014. do {
  1015. err = nfs4_handle_exception(server,
  1016. _nfs4_proc_getattr(server, fhandle, fattr),
  1017. &exception);
  1018. } while (exception.retry);
  1019. return err;
  1020. }
  1021. /*
  1022. * The file is not closed if it is opened due to the a request to change
  1023. * the size of the file. The open call will not be needed once the
  1024. * VFS layer lookup-intents are implemented.
  1025. *
  1026. * Close is called when the inode is destroyed.
  1027. * If we haven't opened the file for O_WRONLY, we
  1028. * need to in the size_change case to obtain a stateid.
  1029. *
  1030. * Got race?
  1031. * Because OPEN is always done by name in nfsv4, it is
  1032. * possible that we opened a different file by the same
  1033. * name. We can recognize this race condition, but we
  1034. * can't do anything about it besides returning an error.
  1035. *
  1036. * This will be fixed with VFS changes (lookup-intent).
  1037. */
  1038. static int
  1039. nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
  1040. struct iattr *sattr)
  1041. {
  1042. struct rpc_cred *cred;
  1043. struct inode *inode = dentry->d_inode;
  1044. struct nfs4_state *state;
  1045. int status;
  1046. fattr->valid = 0;
  1047. cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0);
  1048. if (IS_ERR(cred))
  1049. return PTR_ERR(cred);
  1050. /* Search for an existing WRITE delegation first */
  1051. state = nfs4_open_delegated(inode, FMODE_WRITE, cred);
  1052. if (!IS_ERR(state)) {
  1053. /* NB: nfs4_open_delegated() bumps the inode->i_count */
  1054. iput(inode);
  1055. } else {
  1056. /* Search for an existing open(O_WRITE) stateid */
  1057. state = nfs4_find_state(inode, cred, FMODE_WRITE);
  1058. }
  1059. status = nfs4_do_setattr(NFS_SERVER(inode), fattr,
  1060. NFS_FH(inode), sattr, state);
  1061. if (status == 0)
  1062. nfs_setattr_update_inode(inode, sattr);
  1063. if (state != NULL)
  1064. nfs4_close_state(state, FMODE_WRITE);
  1065. put_rpccred(cred);
  1066. return status;
  1067. }
  1068. static int _nfs4_proc_lookup(struct inode *dir, struct qstr *name,
  1069. struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  1070. {
  1071. int status;
  1072. struct nfs_server *server = NFS_SERVER(dir);
  1073. struct nfs4_lookup_arg args = {
  1074. .bitmask = server->attr_bitmask,
  1075. .dir_fh = NFS_FH(dir),
  1076. .name = name,
  1077. };
  1078. struct nfs4_lookup_res res = {
  1079. .server = server,
  1080. .fattr = fattr,
  1081. .fh = fhandle,
  1082. };
  1083. struct rpc_message msg = {
  1084. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
  1085. .rpc_argp = &args,
  1086. .rpc_resp = &res,
  1087. };
  1088. fattr->valid = 0;
  1089. dprintk("NFS call lookup %s\n", name->name);
  1090. status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
  1091. dprintk("NFS reply lookup: %d\n", status);
  1092. return status;
  1093. }
  1094. static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  1095. {
  1096. struct nfs4_exception exception = { };
  1097. int err;
  1098. do {
  1099. err = nfs4_handle_exception(NFS_SERVER(dir),
  1100. _nfs4_proc_lookup(dir, name, fhandle, fattr),
  1101. &exception);
  1102. } while (exception.retry);
  1103. return err;
  1104. }
  1105. static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
  1106. {
  1107. struct nfs4_accessargs args = {
  1108. .fh = NFS_FH(inode),
  1109. };
  1110. struct nfs4_accessres res = { 0 };
  1111. struct rpc_message msg = {
  1112. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
  1113. .rpc_argp = &args,
  1114. .rpc_resp = &res,
  1115. .rpc_cred = entry->cred,
  1116. };
  1117. int mode = entry->mask;
  1118. int status;
  1119. /*
  1120. * Determine which access bits we want to ask for...
  1121. */
  1122. if (mode & MAY_READ)
  1123. args.access |= NFS4_ACCESS_READ;
  1124. if (S_ISDIR(inode->i_mode)) {
  1125. if (mode & MAY_WRITE)
  1126. args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
  1127. if (mode & MAY_EXEC)
  1128. args.access |= NFS4_ACCESS_LOOKUP;
  1129. } else {
  1130. if (mode & MAY_WRITE)
  1131. args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
  1132. if (mode & MAY_EXEC)
  1133. args.access |= NFS4_ACCESS_EXECUTE;
  1134. }
  1135. status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
  1136. if (!status) {
  1137. entry->mask = 0;
  1138. if (res.access & NFS4_ACCESS_READ)
  1139. entry->mask |= MAY_READ;
  1140. if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
  1141. entry->mask |= MAY_WRITE;
  1142. if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
  1143. entry->mask |= MAY_EXEC;
  1144. }
  1145. return status;
  1146. }
  1147. static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
  1148. {
  1149. struct nfs4_exception exception = { };
  1150. int err;
  1151. do {
  1152. err = nfs4_handle_exception(NFS_SERVER(inode),
  1153. _nfs4_proc_access(inode, entry),
  1154. &exception);
  1155. } while (exception.retry);
  1156. return err;
  1157. }
  1158. /*
  1159. * TODO: For the time being, we don't try to get any attributes
  1160. * along with any of the zero-copy operations READ, READDIR,
  1161. * READLINK, WRITE.
  1162. *
  1163. * In the case of the first three, we want to put the GETATTR
  1164. * after the read-type operation -- this is because it is hard
  1165. * to predict the length of a GETATTR response in v4, and thus
  1166. * align the READ data correctly. This means that the GETATTR
  1167. * may end up partially falling into the page cache, and we should
  1168. * shift it into the 'tail' of the xdr_buf before processing.
  1169. * To do this efficiently, we need to know the total length
  1170. * of data received, which doesn't seem to be available outside
  1171. * of the RPC layer.
  1172. *
  1173. * In the case of WRITE, we also want to put the GETATTR after
  1174. * the operation -- in this case because we want to make sure
  1175. * we get the post-operation mtime and size. This means that
  1176. * we can't use xdr_encode_pages() as written: we need a variant
  1177. * of it which would leave room in the 'tail' iovec.
  1178. *
  1179. * Both of these changes to the XDR layer would in fact be quite
  1180. * minor, but I decided to leave them for a subsequent patch.
  1181. */
  1182. static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
  1183. unsigned int pgbase, unsigned int pglen)
  1184. {
  1185. struct nfs4_readlink args = {
  1186. .fh = NFS_FH(inode),
  1187. .pgbase = pgbase,
  1188. .pglen = pglen,
  1189. .pages = &page,
  1190. };
  1191. struct rpc_message msg = {
  1192. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
  1193. .rpc_argp = &args,
  1194. .rpc_resp = NULL,
  1195. };
  1196. return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
  1197. }
  1198. static int nfs4_proc_readlink(struct inode *inode, struct page *page,
  1199. unsigned int pgbase, unsigned int pglen)
  1200. {
  1201. struct nfs4_exception exception = { };
  1202. int err;
  1203. do {
  1204. err = nfs4_handle_exception(NFS_SERVER(inode),
  1205. _nfs4_proc_readlink(inode, page, pgbase, pglen),
  1206. &exception);
  1207. } while (exception.retry);
  1208. return err;
  1209. }
  1210. static int _nfs4_proc_read(struct nfs_read_data *rdata)
  1211. {
  1212. int flags = rdata->flags;
  1213. struct inode *inode = rdata->inode;
  1214. struct nfs_fattr *fattr = rdata->res.fattr;
  1215. struct nfs_server *server = NFS_SERVER(inode);
  1216. struct rpc_message msg = {
  1217. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
  1218. .rpc_argp = &rdata->args,
  1219. .rpc_resp = &rdata->res,
  1220. .rpc_cred = rdata->cred,
  1221. };
  1222. unsigned long timestamp = jiffies;
  1223. int status;
  1224. dprintk("NFS call read %d @ %Ld\n", rdata->args.count,
  1225. (long long) rdata->args.offset);
  1226. fattr->valid = 0;
  1227. status = rpc_call_sync(server->client, &msg, flags);
  1228. if (!status)
  1229. renew_lease(server, timestamp);
  1230. dprintk("NFS reply read: %d\n", status);
  1231. return status;
  1232. }
  1233. static int nfs4_proc_read(struct nfs_read_data *rdata)
  1234. {
  1235. struct nfs4_exception exception = { };
  1236. int err;
  1237. do {
  1238. err = nfs4_handle_exception(NFS_SERVER(rdata->inode),
  1239. _nfs4_proc_read(rdata),
  1240. &exception);
  1241. } while (exception.retry);
  1242. return err;
  1243. }
  1244. static int _nfs4_proc_write(struct nfs_write_data *wdata)
  1245. {
  1246. int rpcflags = wdata->flags;
  1247. struct inode *inode = wdata->inode;
  1248. struct nfs_fattr *fattr = wdata->res.fattr;
  1249. struct nfs_server *server = NFS_SERVER(inode);
  1250. struct rpc_message msg = {
  1251. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
  1252. .rpc_argp = &wdata->args,
  1253. .rpc_resp = &wdata->res,
  1254. .rpc_cred = wdata->cred,
  1255. };
  1256. int status;
  1257. dprintk("NFS call write %d @ %Ld\n", wdata->args.count,
  1258. (long long) wdata->args.offset);
  1259. fattr->valid = 0;
  1260. status = rpc_call_sync(server->client, &msg, rpcflags);
  1261. dprintk("NFS reply write: %d\n", status);
  1262. return status;
  1263. }
  1264. static int nfs4_proc_write(struct nfs_write_data *wdata)
  1265. {
  1266. struct nfs4_exception exception = { };
  1267. int err;
  1268. do {
  1269. err = nfs4_handle_exception(NFS_SERVER(wdata->inode),
  1270. _nfs4_proc_write(wdata),
  1271. &exception);
  1272. } while (exception.retry);
  1273. return err;
  1274. }
  1275. static int _nfs4_proc_commit(struct nfs_write_data *cdata)
  1276. {
  1277. struct inode *inode = cdata->inode;
  1278. struct nfs_fattr *fattr = cdata->res.fattr;
  1279. struct nfs_server *server = NFS_SERVER(inode);
  1280. struct rpc_message msg = {
  1281. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
  1282. .rpc_argp = &cdata->args,
  1283. .rpc_resp = &cdata->res,
  1284. .rpc_cred = cdata->cred,
  1285. };
  1286. int status;
  1287. dprintk("NFS call commit %d @ %Ld\n", cdata->args.count,
  1288. (long long) cdata->args.offset);
  1289. fattr->valid = 0;
  1290. status = rpc_call_sync(server->client, &msg, 0);
  1291. dprintk("NFS reply commit: %d\n", status);
  1292. return status;
  1293. }
  1294. static int nfs4_proc_commit(struct nfs_write_data *cdata)
  1295. {
  1296. struct nfs4_exception exception = { };
  1297. int err;
  1298. do {
  1299. err = nfs4_handle_exception(NFS_SERVER(cdata->inode),
  1300. _nfs4_proc_commit(cdata),
  1301. &exception);
  1302. } while (exception.retry);
  1303. return err;
  1304. }
  1305. /*
  1306. * Got race?
  1307. * We will need to arrange for the VFS layer to provide an atomic open.
  1308. * Until then, this create/open method is prone to inefficiency and race
  1309. * conditions due to the lookup, create, and open VFS calls from sys_open()
  1310. * placed on the wire.
  1311. *
  1312. * Given the above sorry state of affairs, I'm simply sending an OPEN.
  1313. * The file will be opened again in the subsequent VFS open call
  1314. * (nfs4_proc_file_open).
  1315. *
  1316. * The open for read will just hang around to be used by any process that
  1317. * opens the file O_RDONLY. This will all be resolved with the VFS changes.
  1318. */
  1319. static int
  1320. nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
  1321. int flags)
  1322. {
  1323. struct nfs4_state *state;
  1324. struct rpc_cred *cred;
  1325. int status = 0;
  1326. cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
  1327. if (IS_ERR(cred)) {
  1328. status = PTR_ERR(cred);
  1329. goto out;
  1330. }
  1331. state = nfs4_do_open(dir, dentry, flags, sattr, cred);
  1332. put_rpccred(cred);
  1333. if (IS_ERR(state)) {
  1334. status = PTR_ERR(state);
  1335. goto out;
  1336. }
  1337. d_instantiate(dentry, state->inode);
  1338. if (flags & O_EXCL) {
  1339. struct nfs_fattr fattr;
  1340. status = nfs4_do_setattr(NFS_SERVER(dir), &fattr,
  1341. NFS_FH(state->inode), sattr, state);
  1342. if (status == 0) {
  1343. nfs_setattr_update_inode(state->inode, sattr);
  1344. goto out;
  1345. }
  1346. } else if (flags != 0)
  1347. goto out;
  1348. nfs4_close_state(state, flags);
  1349. out:
  1350. return status;
  1351. }
  1352. static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
  1353. {
  1354. struct nfs4_remove_arg args = {
  1355. .fh = NFS_FH(dir),
  1356. .name = name,
  1357. };
  1358. struct nfs4_change_info res;
  1359. struct rpc_message msg = {
  1360. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
  1361. .rpc_argp = &args,
  1362. .rpc_resp = &res,
  1363. };
  1364. int status;
  1365. status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
  1366. if (status == 0)
  1367. update_changeattr(dir, &res);
  1368. return status;
  1369. }
  1370. static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
  1371. {
  1372. struct nfs4_exception exception = { };
  1373. int err;
  1374. do {
  1375. err = nfs4_handle_exception(NFS_SERVER(dir),
  1376. _nfs4_proc_remove(dir, name),
  1377. &exception);
  1378. } while (exception.retry);
  1379. return err;
  1380. }
  1381. struct unlink_desc {
  1382. struct nfs4_remove_arg args;
  1383. struct nfs4_change_info res;
  1384. };
  1385. static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
  1386. struct qstr *name)
  1387. {
  1388. struct unlink_desc *up;
  1389. up = (struct unlink_desc *) kmalloc(sizeof(*up), GFP_KERNEL);
  1390. if (!up)
  1391. return -ENOMEM;
  1392. up->args.fh = NFS_FH(dir->d_inode);
  1393. up->args.name = name;
  1394. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
  1395. msg->rpc_argp = &up->args;
  1396. msg->rpc_resp = &up->res;
  1397. return 0;
  1398. }
  1399. static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
  1400. {
  1401. struct rpc_message *msg = &task->tk_msg;
  1402. struct unlink_desc *up;
  1403. if (msg->rpc_resp != NULL) {
  1404. up = container_of(msg->rpc_resp, struct unlink_desc, res);
  1405. update_changeattr(dir->d_inode, &up->res);
  1406. kfree(up);
  1407. msg->rpc_resp = NULL;
  1408. msg->rpc_argp = NULL;
  1409. }
  1410. return 0;
  1411. }
  1412. static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
  1413. struct inode *new_dir, struct qstr *new_name)
  1414. {
  1415. struct nfs4_rename_arg arg = {
  1416. .old_dir = NFS_FH(old_dir),
  1417. .new_dir = NFS_FH(new_dir),
  1418. .old_name = old_name,
  1419. .new_name = new_name,
  1420. };
  1421. struct nfs4_rename_res res = { };
  1422. struct rpc_message msg = {
  1423. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
  1424. .rpc_argp = &arg,
  1425. .rpc_resp = &res,
  1426. };
  1427. int status;
  1428. status = rpc_call_sync(NFS_CLIENT(old_dir), &msg, 0);
  1429. if (!status) {
  1430. update_changeattr(old_dir, &res.old_cinfo);
  1431. update_changeattr(new_dir, &res.new_cinfo);
  1432. }
  1433. return status;
  1434. }
  1435. static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
  1436. struct inode *new_dir, struct qstr *new_name)
  1437. {
  1438. struct nfs4_exception exception = { };
  1439. int err;
  1440. do {
  1441. err = nfs4_handle_exception(NFS_SERVER(old_dir),
  1442. _nfs4_proc_rename(old_dir, old_name,
  1443. new_dir, new_name),
  1444. &exception);
  1445. } while (exception.retry);
  1446. return err;
  1447. }
  1448. static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
  1449. {
  1450. struct nfs4_link_arg arg = {
  1451. .fh = NFS_FH(inode),
  1452. .dir_fh = NFS_FH(dir),
  1453. .name = name,
  1454. };
  1455. struct nfs4_change_info cinfo = { };
  1456. struct rpc_message msg = {
  1457. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
  1458. .rpc_argp = &arg,
  1459. .rpc_resp = &cinfo,
  1460. };
  1461. int status;
  1462. status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
  1463. if (!status)
  1464. update_changeattr(dir, &cinfo);
  1465. return status;
  1466. }
  1467. static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
  1468. {
  1469. struct nfs4_exception exception = { };
  1470. int err;
  1471. do {
  1472. err = nfs4_handle_exception(NFS_SERVER(inode),
  1473. _nfs4_proc_link(inode, dir, name),
  1474. &exception);
  1475. } while (exception.retry);
  1476. return err;
  1477. }
  1478. static int _nfs4_proc_symlink(struct inode *dir, struct qstr *name,
  1479. struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
  1480. struct nfs_fattr *fattr)
  1481. {
  1482. struct nfs_server *server = NFS_SERVER(dir);
  1483. struct nfs4_create_arg arg = {
  1484. .dir_fh = NFS_FH(dir),
  1485. .server = server,
  1486. .name = name,
  1487. .attrs = sattr,
  1488. .ftype = NF4LNK,
  1489. .bitmask = server->attr_bitmask,
  1490. };
  1491. struct nfs4_create_res res = {
  1492. .server = server,
  1493. .fh = fhandle,
  1494. .fattr = fattr,
  1495. };
  1496. struct rpc_message msg = {
  1497. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
  1498. .rpc_argp = &arg,
  1499. .rpc_resp = &res,
  1500. };
  1501. int status;
  1502. if (path->len > NFS4_MAXPATHLEN)
  1503. return -ENAMETOOLONG;
  1504. arg.u.symlink = path;
  1505. fattr->valid = 0;
  1506. status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
  1507. if (!status)
  1508. update_changeattr(dir, &res.dir_cinfo);
  1509. return status;
  1510. }
  1511. static int nfs4_proc_symlink(struct inode *dir, struct qstr *name,
  1512. struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
  1513. struct nfs_fattr *fattr)
  1514. {
  1515. struct nfs4_exception exception = { };
  1516. int err;
  1517. do {
  1518. err = nfs4_handle_exception(NFS_SERVER(dir),
  1519. _nfs4_proc_symlink(dir, name, path, sattr,
  1520. fhandle, fattr),
  1521. &exception);
  1522. } while (exception.retry);
  1523. return err;
  1524. }
  1525. static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
  1526. struct iattr *sattr)
  1527. {
  1528. struct nfs_server *server = NFS_SERVER(dir);
  1529. struct nfs_fh fhandle;
  1530. struct nfs_fattr fattr;
  1531. struct nfs4_create_arg arg = {
  1532. .dir_fh = NFS_FH(dir),
  1533. .server = server,
  1534. .name = &dentry->d_name,
  1535. .attrs = sattr,
  1536. .ftype = NF4DIR,
  1537. .bitmask = server->attr_bitmask,
  1538. };
  1539. struct nfs4_create_res res = {
  1540. .server = server,
  1541. .fh = &fhandle,
  1542. .fattr = &fattr,
  1543. };
  1544. struct rpc_message msg = {
  1545. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
  1546. .rpc_argp = &arg,
  1547. .rpc_resp = &res,
  1548. };
  1549. int status;
  1550. fattr.valid = 0;
  1551. status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
  1552. if (!status) {
  1553. update_changeattr(dir, &res.dir_cinfo);
  1554. status = nfs_instantiate(dentry, &fhandle, &fattr);
  1555. }
  1556. return status;
  1557. }
  1558. static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
  1559. struct iattr *sattr)
  1560. {
  1561. struct nfs4_exception exception = { };
  1562. int err;
  1563. do {
  1564. err = nfs4_handle_exception(NFS_SERVER(dir),
  1565. _nfs4_proc_mkdir(dir, dentry, sattr),
  1566. &exception);
  1567. } while (exception.retry);
  1568. return err;
  1569. }
  1570. static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
  1571. u64 cookie, struct page *page, unsigned int count, int plus)
  1572. {
  1573. struct inode *dir = dentry->d_inode;
  1574. struct nfs4_readdir_arg args = {
  1575. .fh = NFS_FH(dir),
  1576. .pages = &page,
  1577. .pgbase = 0,
  1578. .count = count,
  1579. .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
  1580. };
  1581. struct nfs4_readdir_res res;
  1582. struct rpc_message msg = {
  1583. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
  1584. .rpc_argp = &args,
  1585. .rpc_resp = &res,
  1586. .rpc_cred = cred,
  1587. };
  1588. int status;
  1589. dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__,
  1590. dentry->d_parent->d_name.name,
  1591. dentry->d_name.name,
  1592. (unsigned long long)cookie);
  1593. lock_kernel();
  1594. nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
  1595. res.pgbase = args.pgbase;
  1596. status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
  1597. if (status == 0)
  1598. memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
  1599. unlock_kernel();
  1600. dprintk("%s: returns %d\n", __FUNCTION__, status);
  1601. return status;
  1602. }
  1603. static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
  1604. u64 cookie, struct page *page, unsigned int count, int plus)
  1605. {
  1606. struct nfs4_exception exception = { };
  1607. int err;
  1608. do {
  1609. err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
  1610. _nfs4_proc_readdir(dentry, cred, cookie,
  1611. page, count, plus),
  1612. &exception);
  1613. } while (exception.retry);
  1614. return err;
  1615. }
  1616. static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
  1617. struct iattr *sattr, dev_t rdev)
  1618. {
  1619. struct nfs_server *server = NFS_SERVER(dir);
  1620. struct nfs_fh fh;
  1621. struct nfs_fattr fattr;
  1622. struct nfs4_create_arg arg = {
  1623. .dir_fh = NFS_FH(dir),
  1624. .server = server,
  1625. .name = &dentry->d_name,
  1626. .attrs = sattr,
  1627. .bitmask = server->attr_bitmask,
  1628. };
  1629. struct nfs4_create_res res = {
  1630. .server = server,
  1631. .fh = &fh,
  1632. .fattr = &fattr,
  1633. };
  1634. struct rpc_message msg = {
  1635. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
  1636. .rpc_argp = &arg,
  1637. .rpc_resp = &res,
  1638. };
  1639. int status;
  1640. int mode = sattr->ia_mode;
  1641. fattr.valid = 0;
  1642. BUG_ON(!(sattr->ia_valid & ATTR_MODE));
  1643. BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
  1644. if (S_ISFIFO(mode))
  1645. arg.ftype = NF4FIFO;
  1646. else if (S_ISBLK(mode)) {
  1647. arg.ftype = NF4BLK;
  1648. arg.u.device.specdata1 = MAJOR(rdev);
  1649. arg.u.device.specdata2 = MINOR(rdev);
  1650. }
  1651. else if (S_ISCHR(mode)) {
  1652. arg.ftype = NF4CHR;
  1653. arg.u.device.specdata1 = MAJOR(rdev);
  1654. arg.u.device.specdata2 = MINOR(rdev);
  1655. }
  1656. else
  1657. arg.ftype = NF4SOCK;
  1658. status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
  1659. if (status == 0) {
  1660. update_changeattr(dir, &res.dir_cinfo);
  1661. status = nfs_instantiate(dentry, &fh, &fattr);
  1662. }
  1663. return status;
  1664. }
  1665. static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
  1666. struct iattr *sattr, dev_t rdev)
  1667. {
  1668. struct nfs4_exception exception = { };
  1669. int err;
  1670. do {
  1671. err = nfs4_handle_exception(NFS_SERVER(dir),
  1672. _nfs4_proc_mknod(dir, dentry, sattr, rdev),
  1673. &exception);
  1674. } while (exception.retry);
  1675. return err;
  1676. }
  1677. static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
  1678. struct nfs_fsstat *fsstat)
  1679. {
  1680. struct nfs4_statfs_arg args = {
  1681. .fh = fhandle,
  1682. .bitmask = server->attr_bitmask,
  1683. };
  1684. struct rpc_message msg = {
  1685. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
  1686. .rpc_argp = &args,
  1687. .rpc_resp = fsstat,
  1688. };
  1689. fsstat->fattr->valid = 0;
  1690. return rpc_call_sync(server->client, &msg, 0);
  1691. }
  1692. static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
  1693. {
  1694. struct nfs4_exception exception = { };
  1695. int err;
  1696. do {
  1697. err = nfs4_handle_exception(server,
  1698. _nfs4_proc_statfs(server, fhandle, fsstat),
  1699. &exception);
  1700. } while (exception.retry);
  1701. return err;
  1702. }
  1703. static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
  1704. struct nfs_fsinfo *fsinfo)
  1705. {
  1706. struct nfs4_fsinfo_arg args = {
  1707. .fh = fhandle,
  1708. .bitmask = server->attr_bitmask,
  1709. };
  1710. struct rpc_message msg = {
  1711. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
  1712. .rpc_argp = &args,
  1713. .rpc_resp = fsinfo,
  1714. };
  1715. return rpc_call_sync(server->client, &msg, 0);
  1716. }
  1717. static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
  1718. {
  1719. struct nfs4_exception exception = { };
  1720. int err;
  1721. do {
  1722. err = nfs4_handle_exception(server,
  1723. _nfs4_do_fsinfo(server, fhandle, fsinfo),
  1724. &exception);
  1725. } while (exception.retry);
  1726. return err;
  1727. }
  1728. static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
  1729. {
  1730. fsinfo->fattr->valid = 0;
  1731. return nfs4_do_fsinfo(server, fhandle, fsinfo);
  1732. }
  1733. static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
  1734. struct nfs_pathconf *pathconf)
  1735. {
  1736. struct nfs4_pathconf_arg args = {
  1737. .fh = fhandle,
  1738. .bitmask = server->attr_bitmask,
  1739. };
  1740. struct rpc_message msg = {
  1741. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
  1742. .rpc_argp = &args,
  1743. .rpc_resp = pathconf,
  1744. };
  1745. /* None of the pathconf attributes are mandatory to implement */
  1746. if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
  1747. memset(pathconf, 0, sizeof(*pathconf));
  1748. return 0;
  1749. }
  1750. pathconf->fattr->valid = 0;
  1751. return rpc_call_sync(server->client, &msg, 0);
  1752. }
  1753. static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
  1754. struct nfs_pathconf *pathconf)
  1755. {
  1756. struct nfs4_exception exception = { };
  1757. int err;
  1758. do {
  1759. err = nfs4_handle_exception(server,
  1760. _nfs4_proc_pathconf(server, fhandle, pathconf),
  1761. &exception);
  1762. } while (exception.retry);
  1763. return err;
  1764. }
  1765. static void
  1766. nfs4_read_done(struct rpc_task *task)
  1767. {
  1768. struct nfs_read_data *data = (struct nfs_read_data *) task->tk_calldata;
  1769. struct inode *inode = data->inode;
  1770. if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
  1771. rpc_restart_call(task);
  1772. return;
  1773. }
  1774. if (task->tk_status > 0)
  1775. renew_lease(NFS_SERVER(inode), data->timestamp);
  1776. /* Call back common NFS readpage processing */
  1777. nfs_readpage_result(task);
  1778. }
  1779. static void
  1780. nfs4_proc_read_setup(struct nfs_read_data *data)
  1781. {
  1782. struct rpc_task *task = &data->task;
  1783. struct rpc_message msg = {
  1784. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
  1785. .rpc_argp = &data->args,
  1786. .rpc_resp = &data->res,
  1787. .rpc_cred = data->cred,
  1788. };
  1789. struct inode *inode = data->inode;
  1790. int flags;
  1791. data->timestamp = jiffies;
  1792. /* N.B. Do we need to test? Never called for swapfile inode */
  1793. flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
  1794. /* Finalize the task. */
  1795. rpc_init_task(task, NFS_CLIENT(inode), nfs4_read_done, flags);
  1796. rpc_call_setup(task, &msg, 0);
  1797. }
  1798. static void
  1799. nfs4_write_done(struct rpc_task *task)
  1800. {
  1801. struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
  1802. struct inode *inode = data->inode;
  1803. if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
  1804. rpc_restart_call(task);
  1805. return;
  1806. }
  1807. if (task->tk_status >= 0)
  1808. renew_lease(NFS_SERVER(inode), data->timestamp);
  1809. /* Call back common NFS writeback processing */
  1810. nfs_writeback_done(task);
  1811. }
  1812. static void
  1813. nfs4_proc_write_setup(struct nfs_write_data *data, int how)
  1814. {
  1815. struct rpc_task *task = &data->task;
  1816. struct rpc_message msg = {
  1817. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
  1818. .rpc_argp = &data->args,
  1819. .rpc_resp = &data->res,
  1820. .rpc_cred = data->cred,
  1821. };
  1822. struct inode *inode = data->inode;
  1823. int stable;
  1824. int flags;
  1825. if (how & FLUSH_STABLE) {
  1826. if (!NFS_I(inode)->ncommit)
  1827. stable = NFS_FILE_SYNC;
  1828. else
  1829. stable = NFS_DATA_SYNC;
  1830. } else
  1831. stable = NFS_UNSTABLE;
  1832. data->args.stable = stable;
  1833. data->timestamp = jiffies;
  1834. /* Set the initial flags for the task. */
  1835. flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
  1836. /* Finalize the task. */
  1837. rpc_init_task(task, NFS_CLIENT(inode), nfs4_write_done, flags);
  1838. rpc_call_setup(task, &msg, 0);
  1839. }
  1840. static void
  1841. nfs4_commit_done(struct rpc_task *task)
  1842. {
  1843. struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
  1844. struct inode *inode = data->inode;
  1845. if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
  1846. rpc_restart_call(task);
  1847. return;
  1848. }
  1849. /* Call back common NFS writeback processing */
  1850. nfs_commit_done(task);
  1851. }
  1852. static void
  1853. nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
  1854. {
  1855. struct rpc_task *task = &data->task;
  1856. struct rpc_message msg = {
  1857. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
  1858. .rpc_argp = &data->args,
  1859. .rpc_resp = &data->res,
  1860. .rpc_cred = data->cred,
  1861. };
  1862. struct inode *inode = data->inode;
  1863. int flags;
  1864. /* Set the initial flags for the task. */
  1865. flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
  1866. /* Finalize the task. */
  1867. rpc_init_task(task, NFS_CLIENT(inode), nfs4_commit_done, flags);
  1868. rpc_call_setup(task, &msg, 0);
  1869. }
  1870. /*
  1871. * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
  1872. * standalone procedure for queueing an asynchronous RENEW.
  1873. */
  1874. static void
  1875. renew_done(struct rpc_task *task)
  1876. {
  1877. struct nfs4_client *clp = (struct nfs4_client *)task->tk_msg.rpc_argp;
  1878. unsigned long timestamp = (unsigned long)task->tk_calldata;
  1879. if (task->tk_status < 0) {
  1880. switch (task->tk_status) {
  1881. case -NFS4ERR_STALE_CLIENTID:
  1882. case -NFS4ERR_EXPIRED:
  1883. case -NFS4ERR_CB_PATH_DOWN:
  1884. nfs4_schedule_state_recovery(clp);
  1885. }
  1886. return;
  1887. }
  1888. spin_lock(&clp->cl_lock);
  1889. if (time_before(clp->cl_last_renewal,timestamp))
  1890. clp->cl_last_renewal = timestamp;
  1891. spin_unlock(&clp->cl_lock);
  1892. }
  1893. int
  1894. nfs4_proc_async_renew(struct nfs4_client *clp)
  1895. {
  1896. struct rpc_message msg = {
  1897. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
  1898. .rpc_argp = clp,
  1899. .rpc_cred = clp->cl_cred,
  1900. };
  1901. return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
  1902. renew_done, (void *)jiffies);
  1903. }
  1904. int
  1905. nfs4_proc_renew(struct nfs4_client *clp)
  1906. {
  1907. struct rpc_message msg = {
  1908. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
  1909. .rpc_argp = clp,
  1910. .rpc_cred = clp->cl_cred,
  1911. };
  1912. unsigned long now = jiffies;
  1913. int status;
  1914. status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
  1915. if (status < 0)
  1916. return status;
  1917. spin_lock(&clp->cl_lock);
  1918. if (time_before(clp->cl_last_renewal,now))
  1919. clp->cl_last_renewal = now;
  1920. spin_unlock(&clp->cl_lock);
  1921. return 0;
  1922. }
  1923. /*
  1924. * We will need to arrange for the VFS layer to provide an atomic open.
  1925. * Until then, this open method is prone to inefficiency and race conditions
  1926. * due to the lookup, potential create, and open VFS calls from sys_open()
  1927. * placed on the wire.
  1928. */
  1929. static int
  1930. nfs4_proc_file_open(struct inode *inode, struct file *filp)
  1931. {
  1932. struct dentry *dentry = filp->f_dentry;
  1933. struct nfs_open_context *ctx;
  1934. struct nfs4_state *state = NULL;
  1935. struct rpc_cred *cred;
  1936. int status = -ENOMEM;
  1937. dprintk("nfs4_proc_file_open: starting on (%.*s/%.*s)\n",
  1938. (int)dentry->d_parent->d_name.len,
  1939. dentry->d_parent->d_name.name,
  1940. (int)dentry->d_name.len, dentry->d_name.name);
  1941. /* Find our open stateid */
  1942. cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0);
  1943. if (IS_ERR(cred))
  1944. return PTR_ERR(cred);
  1945. ctx = alloc_nfs_open_context(dentry, cred);
  1946. put_rpccred(cred);
  1947. if (unlikely(ctx == NULL))
  1948. return -ENOMEM;
  1949. status = -EIO; /* ERACE actually */
  1950. state = nfs4_find_state(inode, cred, filp->f_mode);
  1951. if (unlikely(state == NULL))
  1952. goto no_state;
  1953. ctx->state = state;
  1954. nfs4_close_state(state, filp->f_mode);
  1955. ctx->mode = filp->f_mode;
  1956. nfs_file_set_open_context(filp, ctx);
  1957. put_nfs_open_context(ctx);
  1958. if (filp->f_mode & FMODE_WRITE)
  1959. nfs_begin_data_update(inode);
  1960. return 0;
  1961. no_state:
  1962. printk(KERN_WARNING "NFS: v4 raced in function %s\n", __FUNCTION__);
  1963. put_nfs_open_context(ctx);
  1964. return status;
  1965. }
  1966. /*
  1967. * Release our state
  1968. */
  1969. static int
  1970. nfs4_proc_file_release(struct inode *inode, struct file *filp)
  1971. {
  1972. if (filp->f_mode & FMODE_WRITE)
  1973. nfs_end_data_update(inode);
  1974. nfs_file_clear_open_context(filp);
  1975. return 0;
  1976. }
  1977. static inline int nfs4_server_supports_acls(struct nfs_server *server)
  1978. {
  1979. return (server->caps & NFS_CAP_ACLS)
  1980. && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
  1981. && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
  1982. }
  1983. /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
  1984. * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
  1985. * the stack.
  1986. */
  1987. #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
  1988. static void buf_to_pages(const void *buf, size_t buflen,
  1989. struct page **pages, unsigned int *pgbase)
  1990. {
  1991. const void *p = buf;
  1992. *pgbase = offset_in_page(buf);
  1993. p -= *pgbase;
  1994. while (p < buf + buflen) {
  1995. *(pages++) = virt_to_page(p);
  1996. p += PAGE_CACHE_SIZE;
  1997. }
  1998. }
  1999. struct nfs4_cached_acl {
  2000. int cached;
  2001. size_t len;
  2002. char data[0];
  2003. };
  2004. static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
  2005. {
  2006. struct nfs_inode *nfsi = NFS_I(inode);
  2007. spin_lock(&inode->i_lock);
  2008. kfree(nfsi->nfs4_acl);
  2009. nfsi->nfs4_acl = acl;
  2010. spin_unlock(&inode->i_lock);
  2011. }
  2012. static void nfs4_zap_acl_attr(struct inode *inode)
  2013. {
  2014. nfs4_set_cached_acl(inode, NULL);
  2015. }
  2016. static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
  2017. {
  2018. struct nfs_inode *nfsi = NFS_I(inode);
  2019. struct nfs4_cached_acl *acl;
  2020. int ret = -ENOENT;
  2021. spin_lock(&inode->i_lock);
  2022. acl = nfsi->nfs4_acl;
  2023. if (acl == NULL)
  2024. goto out;
  2025. if (buf == NULL) /* user is just asking for length */
  2026. goto out_len;
  2027. if (acl->cached == 0)
  2028. goto out;
  2029. ret = -ERANGE; /* see getxattr(2) man page */
  2030. if (acl->len > buflen)
  2031. goto out;
  2032. memcpy(buf, acl->data, acl->len);
  2033. out_len:
  2034. ret = acl->len;
  2035. out:
  2036. spin_unlock(&inode->i_lock);
  2037. return ret;
  2038. }
  2039. static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
  2040. {
  2041. struct nfs4_cached_acl *acl;
  2042. if (buf && acl_len <= PAGE_SIZE) {
  2043. acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
  2044. if (acl == NULL)
  2045. goto out;
  2046. acl->cached = 1;
  2047. memcpy(acl->data, buf, acl_len);
  2048. } else {
  2049. acl = kmalloc(sizeof(*acl), GFP_KERNEL);
  2050. if (acl == NULL)
  2051. goto out;
  2052. acl->cached = 0;
  2053. }
  2054. acl->len = acl_len;
  2055. out:
  2056. nfs4_set_cached_acl(inode, acl);
  2057. }
  2058. static inline ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
  2059. {
  2060. struct page *pages[NFS4ACL_MAXPAGES];
  2061. struct nfs_getaclargs args = {
  2062. .fh = NFS_FH(inode),
  2063. .acl_pages = pages,
  2064. .acl_len = buflen,
  2065. };
  2066. size_t resp_len = buflen;
  2067. void *resp_buf;
  2068. struct rpc_message msg = {
  2069. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
  2070. .rpc_argp = &args,
  2071. .rpc_resp = &resp_len,
  2072. };
  2073. struct page *localpage = NULL;
  2074. int ret;
  2075. if (buflen < PAGE_SIZE) {
  2076. /* As long as we're doing a round trip to the server anyway,
  2077. * let's be prepared for a page of acl data. */
  2078. localpage = alloc_page(GFP_KERNEL);
  2079. resp_buf = page_address(localpage);
  2080. if (localpage == NULL)
  2081. return -ENOMEM;
  2082. args.acl_pages[0] = localpage;
  2083. args.acl_pgbase = 0;
  2084. args.acl_len = PAGE_SIZE;
  2085. } else {
  2086. resp_buf = buf;
  2087. buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
  2088. }
  2089. ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
  2090. if (ret)
  2091. goto out_free;
  2092. if (resp_len > args.acl_len)
  2093. nfs4_write_cached_acl(inode, NULL, resp_len);
  2094. else
  2095. nfs4_write_cached_acl(inode, resp_buf, resp_len);
  2096. if (buf) {
  2097. ret = -ERANGE;
  2098. if (resp_len > buflen)
  2099. goto out_free;
  2100. if (localpage)
  2101. memcpy(buf, resp_buf, resp_len);
  2102. }
  2103. ret = resp_len;
  2104. out_free:
  2105. if (localpage)
  2106. __free_page(localpage);
  2107. return ret;
  2108. }
  2109. static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
  2110. {
  2111. struct nfs_server *server = NFS_SERVER(inode);
  2112. int ret;
  2113. if (!nfs4_server_supports_acls(server))
  2114. return -EOPNOTSUPP;
  2115. ret = nfs_revalidate_inode(server, inode);
  2116. if (ret < 0)
  2117. return ret;
  2118. ret = nfs4_read_cached_acl(inode, buf, buflen);
  2119. if (ret != -ENOENT)
  2120. return ret;
  2121. return nfs4_get_acl_uncached(inode, buf, buflen);
  2122. }
  2123. static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
  2124. {
  2125. struct nfs_server *server = NFS_SERVER(inode);
  2126. struct page *pages[NFS4ACL_MAXPAGES];
  2127. struct nfs_setaclargs arg = {
  2128. .fh = NFS_FH(inode),
  2129. .acl_pages = pages,
  2130. .acl_len = buflen,
  2131. };
  2132. struct rpc_message msg = {
  2133. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
  2134. .rpc_argp = &arg,
  2135. .rpc_resp = NULL,
  2136. };
  2137. int ret;
  2138. if (!nfs4_server_supports_acls(server))
  2139. return -EOPNOTSUPP;
  2140. buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
  2141. ret = rpc_call_sync(NFS_SERVER(inode)->client, &msg, 0);
  2142. if (ret == 0)
  2143. nfs4_write_cached_acl(inode, buf, buflen);
  2144. return ret;
  2145. }
  2146. static int
  2147. nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server)
  2148. {
  2149. struct nfs4_client *clp = server->nfs4_state;
  2150. if (!clp || task->tk_status >= 0)
  2151. return 0;
  2152. switch(task->tk_status) {
  2153. case -NFS4ERR_STALE_CLIENTID:
  2154. case -NFS4ERR_STALE_STATEID:
  2155. case -NFS4ERR_EXPIRED:
  2156. rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
  2157. nfs4_schedule_state_recovery(clp);
  2158. if (test_bit(NFS4CLNT_OK, &clp->cl_state))
  2159. rpc_wake_up_task(task);
  2160. task->tk_status = 0;
  2161. return -EAGAIN;
  2162. case -NFS4ERR_GRACE:
  2163. case -NFS4ERR_DELAY:
  2164. rpc_delay(task, NFS4_POLL_RETRY_MAX);
  2165. task->tk_status = 0;
  2166. return -EAGAIN;
  2167. case -NFS4ERR_OLD_STATEID:
  2168. task->tk_status = 0;
  2169. return -EAGAIN;
  2170. }
  2171. task->tk_status = nfs4_map_errors(task->tk_status);
  2172. return 0;
  2173. }
  2174. static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs4_client *clp)
  2175. {
  2176. DEFINE_WAIT(wait);
  2177. sigset_t oldset;
  2178. int interruptible, res = 0;
  2179. might_sleep();
  2180. rpc_clnt_sigmask(clnt, &oldset);
  2181. interruptible = TASK_UNINTERRUPTIBLE;
  2182. if (clnt->cl_intr)
  2183. interruptible = TASK_INTERRUPTIBLE;
  2184. prepare_to_wait(&clp->cl_waitq, &wait, interruptible);
  2185. nfs4_schedule_state_recovery(clp);
  2186. if (clnt->cl_intr && signalled())
  2187. res = -ERESTARTSYS;
  2188. else if (!test_bit(NFS4CLNT_OK, &clp->cl_state))
  2189. schedule();
  2190. finish_wait(&clp->cl_waitq, &wait);
  2191. rpc_clnt_sigunmask(clnt, &oldset);
  2192. return res;
  2193. }
  2194. static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
  2195. {
  2196. sigset_t oldset;
  2197. int res = 0;
  2198. might_sleep();
  2199. if (*timeout <= 0)
  2200. *timeout = NFS4_POLL_RETRY_MIN;
  2201. if (*timeout > NFS4_POLL_RETRY_MAX)
  2202. *timeout = NFS4_POLL_RETRY_MAX;
  2203. rpc_clnt_sigmask(clnt, &oldset);
  2204. if (clnt->cl_intr) {
  2205. schedule_timeout_interruptible(*timeout);
  2206. if (signalled())
  2207. res = -ERESTARTSYS;
  2208. } else
  2209. schedule_timeout_uninterruptible(*timeout);
  2210. rpc_clnt_sigunmask(clnt, &oldset);
  2211. *timeout <<= 1;
  2212. return res;
  2213. }
  2214. /* This is the error handling routine for processes that are allowed
  2215. * to sleep.
  2216. */
  2217. int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
  2218. {
  2219. struct nfs4_client *clp = server->nfs4_state;
  2220. int ret = errorcode;
  2221. exception->retry = 0;
  2222. switch(errorcode) {
  2223. case 0:
  2224. return 0;
  2225. case -NFS4ERR_STALE_CLIENTID:
  2226. case -NFS4ERR_STALE_STATEID:
  2227. case -NFS4ERR_EXPIRED:
  2228. ret = nfs4_wait_clnt_recover(server->client, clp);
  2229. if (ret == 0)
  2230. exception->retry = 1;
  2231. break;
  2232. case -NFS4ERR_GRACE:
  2233. case -NFS4ERR_DELAY:
  2234. ret = nfs4_delay(server->client, &exception->timeout);
  2235. if (ret == 0)
  2236. exception->retry = 1;
  2237. break;
  2238. case -NFS4ERR_OLD_STATEID:
  2239. if (ret == 0)
  2240. exception->retry = 1;
  2241. }
  2242. /* We failed to handle the error */
  2243. return nfs4_map_errors(ret);
  2244. }
  2245. int nfs4_proc_setclientid(struct nfs4_client *clp, u32 program, unsigned short port)
  2246. {
  2247. nfs4_verifier sc_verifier;
  2248. struct nfs4_setclientid setclientid = {
  2249. .sc_verifier = &sc_verifier,
  2250. .sc_prog = program,
  2251. };
  2252. struct rpc_message msg = {
  2253. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
  2254. .rpc_argp = &setclientid,
  2255. .rpc_resp = clp,
  2256. .rpc_cred = clp->cl_cred,
  2257. };
  2258. u32 *p;
  2259. int loop = 0;
  2260. int status;
  2261. p = (u32*)sc_verifier.data;
  2262. *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
  2263. *p = htonl((u32)clp->cl_boot_time.tv_nsec);
  2264. for(;;) {
  2265. setclientid.sc_name_len = scnprintf(setclientid.sc_name,
  2266. sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
  2267. clp->cl_ipaddr, NIPQUAD(clp->cl_addr.s_addr),
  2268. clp->cl_cred->cr_ops->cr_name,
  2269. clp->cl_id_uniquifier);
  2270. setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
  2271. sizeof(setclientid.sc_netid), "tcp");
  2272. setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
  2273. sizeof(setclientid.sc_uaddr), "%s.%d.%d",
  2274. clp->cl_ipaddr, port >> 8, port & 255);
  2275. status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
  2276. if (status != -NFS4ERR_CLID_INUSE)
  2277. break;
  2278. if (signalled())
  2279. break;
  2280. if (loop++ & 1)
  2281. ssleep(clp->cl_lease_time + 1);
  2282. else
  2283. if (++clp->cl_id_uniquifier == 0)
  2284. break;
  2285. }
  2286. return status;
  2287. }
  2288. int
  2289. nfs4_proc_setclientid_confirm(struct nfs4_client *clp)
  2290. {
  2291. struct nfs_fsinfo fsinfo;
  2292. struct rpc_message msg = {
  2293. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
  2294. .rpc_argp = clp,
  2295. .rpc_resp = &fsinfo,
  2296. .rpc_cred = clp->cl_cred,
  2297. };
  2298. unsigned long now;
  2299. int status;
  2300. now = jiffies;
  2301. status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
  2302. if (status == 0) {
  2303. spin_lock(&clp->cl_lock);
  2304. clp->cl_lease_time = fsinfo.lease_time * HZ;
  2305. clp->cl_last_renewal = now;
  2306. spin_unlock(&clp->cl_lock);
  2307. }
  2308. return status;
  2309. }
  2310. static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
  2311. {
  2312. struct nfs4_delegreturnargs args = {
  2313. .fhandle = NFS_FH(inode),
  2314. .stateid = stateid,
  2315. };
  2316. struct rpc_message msg = {
  2317. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
  2318. .rpc_argp = &args,
  2319. .rpc_cred = cred,
  2320. };
  2321. return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
  2322. }
  2323. int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
  2324. {
  2325. struct nfs_server *server = NFS_SERVER(inode);
  2326. struct nfs4_exception exception = { };
  2327. int err;
  2328. do {
  2329. err = _nfs4_proc_delegreturn(inode, cred, stateid);
  2330. switch (err) {
  2331. case -NFS4ERR_STALE_STATEID:
  2332. case -NFS4ERR_EXPIRED:
  2333. nfs4_schedule_state_recovery(server->nfs4_state);
  2334. case 0:
  2335. return 0;
  2336. }
  2337. err = nfs4_handle_exception(server, err, &exception);
  2338. } while (exception.retry);
  2339. return err;
  2340. }
  2341. #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
  2342. #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
  2343. /*
  2344. * sleep, with exponential backoff, and retry the LOCK operation.
  2345. */
  2346. static unsigned long
  2347. nfs4_set_lock_task_retry(unsigned long timeout)
  2348. {
  2349. schedule_timeout_interruptible(timeout);
  2350. timeout <<= 1;
  2351. if (timeout > NFS4_LOCK_MAXTIMEOUT)
  2352. return NFS4_LOCK_MAXTIMEOUT;
  2353. return timeout;
  2354. }
  2355. static inline int
  2356. nfs4_lck_type(int cmd, struct file_lock *request)
  2357. {
  2358. /* set lock type */
  2359. switch (request->fl_type) {
  2360. case F_RDLCK:
  2361. return IS_SETLKW(cmd) ? NFS4_READW_LT : NFS4_READ_LT;
  2362. case F_WRLCK:
  2363. return IS_SETLKW(cmd) ? NFS4_WRITEW_LT : NFS4_WRITE_LT;
  2364. case F_UNLCK:
  2365. return NFS4_WRITE_LT;
  2366. }
  2367. BUG();
  2368. return 0;
  2369. }
  2370. static inline uint64_t
  2371. nfs4_lck_length(struct file_lock *request)
  2372. {
  2373. if (request->fl_end == OFFSET_MAX)
  2374. return ~(uint64_t)0;
  2375. return request->fl_end - request->fl_start + 1;
  2376. }
  2377. static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  2378. {
  2379. struct inode *inode = state->inode;
  2380. struct nfs_server *server = NFS_SERVER(inode);
  2381. struct nfs4_client *clp = server->nfs4_state;
  2382. struct nfs_lockargs arg = {
  2383. .fh = NFS_FH(inode),
  2384. .type = nfs4_lck_type(cmd, request),
  2385. .offset = request->fl_start,
  2386. .length = nfs4_lck_length(request),
  2387. };
  2388. struct nfs_lockres res = {
  2389. .server = server,
  2390. };
  2391. struct rpc_message msg = {
  2392. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
  2393. .rpc_argp = &arg,
  2394. .rpc_resp = &res,
  2395. .rpc_cred = state->owner->so_cred,
  2396. };
  2397. struct nfs_lowner nlo;
  2398. struct nfs4_lock_state *lsp;
  2399. int status;
  2400. down_read(&clp->cl_sem);
  2401. nlo.clientid = clp->cl_clientid;
  2402. down(&state->lock_sema);
  2403. status = nfs4_set_lock_state(state, request);
  2404. if (status != 0)
  2405. goto out;
  2406. lsp = request->fl_u.nfs4_fl.owner;
  2407. nlo.id = lsp->ls_id;
  2408. arg.u.lockt = &nlo;
  2409. status = rpc_call_sync(server->client, &msg, 0);
  2410. if (!status) {
  2411. request->fl_type = F_UNLCK;
  2412. } else if (status == -NFS4ERR_DENIED) {
  2413. int64_t len, start, end;
  2414. start = res.u.denied.offset;
  2415. len = res.u.denied.length;
  2416. end = start + len - 1;
  2417. if (end < 0 || len == 0)
  2418. request->fl_end = OFFSET_MAX;
  2419. else
  2420. request->fl_end = (loff_t)end;
  2421. request->fl_start = (loff_t)start;
  2422. request->fl_type = F_WRLCK;
  2423. if (res.u.denied.type & 1)
  2424. request->fl_type = F_RDLCK;
  2425. request->fl_pid = 0;
  2426. status = 0;
  2427. }
  2428. out:
  2429. up(&state->lock_sema);
  2430. up_read(&clp->cl_sem);
  2431. return status;
  2432. }
  2433. static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  2434. {
  2435. struct nfs4_exception exception = { };
  2436. int err;
  2437. do {
  2438. err = nfs4_handle_exception(NFS_SERVER(state->inode),
  2439. _nfs4_proc_getlk(state, cmd, request),
  2440. &exception);
  2441. } while (exception.retry);
  2442. return err;
  2443. }
  2444. static int do_vfs_lock(struct file *file, struct file_lock *fl)
  2445. {
  2446. int res = 0;
  2447. switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
  2448. case FL_POSIX:
  2449. res = posix_lock_file_wait(file, fl);
  2450. break;
  2451. case FL_FLOCK:
  2452. res = flock_lock_file_wait(file, fl);
  2453. break;
  2454. default:
  2455. BUG();
  2456. }
  2457. if (res < 0)
  2458. printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
  2459. __FUNCTION__);
  2460. return res;
  2461. }
  2462. static int _nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
  2463. {
  2464. struct inode *inode = state->inode;
  2465. struct nfs_server *server = NFS_SERVER(inode);
  2466. struct nfs4_client *clp = server->nfs4_state;
  2467. struct nfs_lockargs arg = {
  2468. .fh = NFS_FH(inode),
  2469. .type = nfs4_lck_type(cmd, request),
  2470. .offset = request->fl_start,
  2471. .length = nfs4_lck_length(request),
  2472. };
  2473. struct nfs_lockres res = {
  2474. .server = server,
  2475. };
  2476. struct rpc_message msg = {
  2477. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
  2478. .rpc_argp = &arg,
  2479. .rpc_resp = &res,
  2480. .rpc_cred = state->owner->so_cred,
  2481. };
  2482. struct nfs4_lock_state *lsp;
  2483. struct nfs_locku_opargs luargs;
  2484. int status;
  2485. down_read(&clp->cl_sem);
  2486. down(&state->lock_sema);
  2487. status = nfs4_set_lock_state(state, request);
  2488. if (status != 0)
  2489. goto out;
  2490. lsp = request->fl_u.nfs4_fl.owner;
  2491. /* We might have lost the locks! */
  2492. if ((lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0)
  2493. goto out;
  2494. luargs.seqid = lsp->ls_seqid;
  2495. memcpy(&luargs.stateid, &lsp->ls_stateid, sizeof(luargs.stateid));
  2496. arg.u.locku = &luargs;
  2497. status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
  2498. nfs4_increment_lock_seqid(status, lsp);
  2499. if (status == 0)
  2500. memcpy(&lsp->ls_stateid, &res.u.stateid,
  2501. sizeof(lsp->ls_stateid));
  2502. out:
  2503. up(&state->lock_sema);
  2504. if (status == 0)
  2505. do_vfs_lock(request->fl_file, request);
  2506. up_read(&clp->cl_sem);
  2507. return status;
  2508. }
  2509. static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
  2510. {
  2511. struct nfs4_exception exception = { };
  2512. int err;
  2513. do {
  2514. err = nfs4_handle_exception(NFS_SERVER(state->inode),
  2515. _nfs4_proc_unlck(state, cmd, request),
  2516. &exception);
  2517. } while (exception.retry);
  2518. return err;
  2519. }
  2520. static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *request, int reclaim)
  2521. {
  2522. struct inode *inode = state->inode;
  2523. struct nfs_server *server = NFS_SERVER(inode);
  2524. struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
  2525. struct nfs_lockargs arg = {
  2526. .fh = NFS_FH(inode),
  2527. .type = nfs4_lck_type(cmd, request),
  2528. .offset = request->fl_start,
  2529. .length = nfs4_lck_length(request),
  2530. };
  2531. struct nfs_lockres res = {
  2532. .server = server,
  2533. };
  2534. struct rpc_message msg = {
  2535. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
  2536. .rpc_argp = &arg,
  2537. .rpc_resp = &res,
  2538. .rpc_cred = state->owner->so_cred,
  2539. };
  2540. struct nfs_lock_opargs largs = {
  2541. .reclaim = reclaim,
  2542. .new_lock_owner = 0,
  2543. };
  2544. int status;
  2545. if (!(lsp->ls_flags & NFS_LOCK_INITIALIZED)) {
  2546. struct nfs4_state_owner *owner = state->owner;
  2547. struct nfs_open_to_lock otl = {
  2548. .lock_owner = {
  2549. .clientid = server->nfs4_state->cl_clientid,
  2550. },
  2551. };
  2552. otl.lock_seqid = lsp->ls_seqid;
  2553. otl.lock_owner.id = lsp->ls_id;
  2554. memcpy(&otl.open_stateid, &state->stateid, sizeof(otl.open_stateid));
  2555. largs.u.open_lock = &otl;
  2556. largs.new_lock_owner = 1;
  2557. arg.u.lock = &largs;
  2558. down(&owner->so_sema);
  2559. otl.open_seqid = owner->so_seqid;
  2560. status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
  2561. /* increment open_owner seqid on success, and
  2562. * seqid mutating errors */
  2563. nfs4_increment_seqid(status, owner);
  2564. up(&owner->so_sema);
  2565. if (status == 0) {
  2566. lsp->ls_flags |= NFS_LOCK_INITIALIZED;
  2567. lsp->ls_seqid++;
  2568. }
  2569. } else {
  2570. struct nfs_exist_lock el = {
  2571. .seqid = lsp->ls_seqid,
  2572. };
  2573. memcpy(&el.stateid, &lsp->ls_stateid, sizeof(el.stateid));
  2574. largs.u.exist_lock = &el;
  2575. arg.u.lock = &largs;
  2576. status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
  2577. /* increment seqid on success, and * seqid mutating errors*/
  2578. nfs4_increment_lock_seqid(status, lsp);
  2579. }
  2580. /* save the returned stateid. */
  2581. if (status == 0)
  2582. memcpy(&lsp->ls_stateid, &res.u.stateid, sizeof(nfs4_stateid));
  2583. else if (status == -NFS4ERR_DENIED)
  2584. status = -EAGAIN;
  2585. return status;
  2586. }
  2587. static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
  2588. {
  2589. struct nfs_server *server = NFS_SERVER(state->inode);
  2590. struct nfs4_exception exception = { };
  2591. int err;
  2592. do {
  2593. err = _nfs4_do_setlk(state, F_SETLK, request, 1);
  2594. if (err != -NFS4ERR_DELAY)
  2595. break;
  2596. nfs4_handle_exception(server, err, &exception);
  2597. } while (exception.retry);
  2598. return err;
  2599. }
  2600. static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
  2601. {
  2602. struct nfs_server *server = NFS_SERVER(state->inode);
  2603. struct nfs4_exception exception = { };
  2604. int err;
  2605. do {
  2606. err = _nfs4_do_setlk(state, F_SETLK, request, 0);
  2607. if (err != -NFS4ERR_DELAY)
  2608. break;
  2609. nfs4_handle_exception(server, err, &exception);
  2610. } while (exception.retry);
  2611. return err;
  2612. }
  2613. static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  2614. {
  2615. struct nfs4_client *clp = state->owner->so_client;
  2616. int status;
  2617. down_read(&clp->cl_sem);
  2618. down(&state->lock_sema);
  2619. status = nfs4_set_lock_state(state, request);
  2620. if (status == 0)
  2621. status = _nfs4_do_setlk(state, cmd, request, 0);
  2622. up(&state->lock_sema);
  2623. if (status == 0) {
  2624. /* Note: we always want to sleep here! */
  2625. request->fl_flags |= FL_SLEEP;
  2626. if (do_vfs_lock(request->fl_file, request) < 0)
  2627. printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
  2628. }
  2629. up_read(&clp->cl_sem);
  2630. return status;
  2631. }
  2632. static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  2633. {
  2634. struct nfs4_exception exception = { };
  2635. int err;
  2636. do {
  2637. err = nfs4_handle_exception(NFS_SERVER(state->inode),
  2638. _nfs4_proc_setlk(state, cmd, request),
  2639. &exception);
  2640. } while (exception.retry);
  2641. return err;
  2642. }
  2643. static int
  2644. nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
  2645. {
  2646. struct nfs_open_context *ctx;
  2647. struct nfs4_state *state;
  2648. unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
  2649. int status;
  2650. /* verify open state */
  2651. ctx = (struct nfs_open_context *)filp->private_data;
  2652. state = ctx->state;
  2653. if (request->fl_start < 0 || request->fl_end < 0)
  2654. return -EINVAL;
  2655. if (IS_GETLK(cmd))
  2656. return nfs4_proc_getlk(state, F_GETLK, request);
  2657. if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
  2658. return -EINVAL;
  2659. if (request->fl_type == F_UNLCK)
  2660. return nfs4_proc_unlck(state, cmd, request);
  2661. do {
  2662. status = nfs4_proc_setlk(state, cmd, request);
  2663. if ((status != -EAGAIN) || IS_SETLK(cmd))
  2664. break;
  2665. timeout = nfs4_set_lock_task_retry(timeout);
  2666. status = -ERESTARTSYS;
  2667. if (signalled())
  2668. break;
  2669. } while(status < 0);
  2670. return status;
  2671. }
  2672. #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
  2673. int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
  2674. size_t buflen, int flags)
  2675. {
  2676. struct inode *inode = dentry->d_inode;
  2677. if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
  2678. return -EOPNOTSUPP;
  2679. if (!S_ISREG(inode->i_mode) &&
  2680. (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
  2681. return -EPERM;
  2682. return nfs4_proc_set_acl(inode, buf, buflen);
  2683. }
  2684. /* The getxattr man page suggests returning -ENODATA for unknown attributes,
  2685. * and that's what we'll do for e.g. user attributes that haven't been set.
  2686. * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
  2687. * attributes in kernel-managed attribute namespaces. */
  2688. ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
  2689. size_t buflen)
  2690. {
  2691. struct inode *inode = dentry->d_inode;
  2692. if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
  2693. return -EOPNOTSUPP;
  2694. return nfs4_proc_get_acl(inode, buf, buflen);
  2695. }
  2696. ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
  2697. {
  2698. size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
  2699. if (buf && buflen < len)
  2700. return -ERANGE;
  2701. if (buf)
  2702. memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
  2703. return len;
  2704. }
  2705. struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
  2706. .recover_open = nfs4_open_reclaim,
  2707. .recover_lock = nfs4_lock_reclaim,
  2708. };
  2709. struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
  2710. .recover_open = nfs4_open_expired,
  2711. .recover_lock = nfs4_lock_expired,
  2712. };
  2713. static struct inode_operations nfs4_file_inode_operations = {
  2714. .permission = nfs_permission,
  2715. .getattr = nfs_getattr,
  2716. .setattr = nfs_setattr,
  2717. .getxattr = nfs4_getxattr,
  2718. .setxattr = nfs4_setxattr,
  2719. .listxattr = nfs4_listxattr,
  2720. };
  2721. struct nfs_rpc_ops nfs_v4_clientops = {
  2722. .version = 4, /* protocol version */
  2723. .dentry_ops = &nfs4_dentry_operations,
  2724. .dir_inode_ops = &nfs4_dir_inode_operations,
  2725. .file_inode_ops = &nfs4_file_inode_operations,
  2726. .getroot = nfs4_proc_get_root,
  2727. .getattr = nfs4_proc_getattr,
  2728. .setattr = nfs4_proc_setattr,
  2729. .lookup = nfs4_proc_lookup,
  2730. .access = nfs4_proc_access,
  2731. .readlink = nfs4_proc_readlink,
  2732. .read = nfs4_proc_read,
  2733. .write = nfs4_proc_write,
  2734. .commit = nfs4_proc_commit,
  2735. .create = nfs4_proc_create,
  2736. .remove = nfs4_proc_remove,
  2737. .unlink_setup = nfs4_proc_unlink_setup,
  2738. .unlink_done = nfs4_proc_unlink_done,
  2739. .rename = nfs4_proc_rename,
  2740. .link = nfs4_proc_link,
  2741. .symlink = nfs4_proc_symlink,
  2742. .mkdir = nfs4_proc_mkdir,
  2743. .rmdir = nfs4_proc_remove,
  2744. .readdir = nfs4_proc_readdir,
  2745. .mknod = nfs4_proc_mknod,
  2746. .statfs = nfs4_proc_statfs,
  2747. .fsinfo = nfs4_proc_fsinfo,
  2748. .pathconf = nfs4_proc_pathconf,
  2749. .decode_dirent = nfs4_decode_dirent,
  2750. .read_setup = nfs4_proc_read_setup,
  2751. .write_setup = nfs4_proc_write_setup,
  2752. .commit_setup = nfs4_proc_commit_setup,
  2753. .file_open = nfs4_proc_file_open,
  2754. .file_release = nfs4_proc_file_release,
  2755. .lock = nfs4_proc_lock,
  2756. .clear_acl_cache = nfs4_zap_acl_attr,
  2757. };
  2758. /*
  2759. * Local variables:
  2760. * c-basic-offset: 8
  2761. * End:
  2762. */