inode.c 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962
  1. /*
  2. * linux/fs/nfs/inode.c
  3. *
  4. * Copyright (C) 1992 Rick Sladkey
  5. *
  6. * nfs inode and superblock handling functions
  7. *
  8. * Modularised by Alan Cox <Alan.Cox@linux.org>, while hacking some
  9. * experimental NFS changes. Modularisation taken straight from SYS5 fs.
  10. *
  11. * Change to nfs_read_super() to permit NFS mounts to multi-homed hosts.
  12. * J.S.Peatfield@damtp.cam.ac.uk
  13. *
  14. */
  15. #include <linux/config.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/time.h>
  19. #include <linux/kernel.h>
  20. #include <linux/mm.h>
  21. #include <linux/string.h>
  22. #include <linux/stat.h>
  23. #include <linux/errno.h>
  24. #include <linux/unistd.h>
  25. #include <linux/sunrpc/clnt.h>
  26. #include <linux/sunrpc/stats.h>
  27. #include <linux/sunrpc/metrics.h>
  28. #include <linux/nfs_fs.h>
  29. #include <linux/nfs_mount.h>
  30. #include <linux/nfs4_mount.h>
  31. #include <linux/lockd/bind.h>
  32. #include <linux/smp_lock.h>
  33. #include <linux/seq_file.h>
  34. #include <linux/mount.h>
  35. #include <linux/nfs_idmap.h>
  36. #include <linux/vfs.h>
  37. #include <linux/inet.h>
  38. #include <linux/nfs_xdr.h>
  39. #include <asm/system.h>
  40. #include <asm/uaccess.h>
  41. #include "nfs4_fs.h"
  42. #include "callback.h"
  43. #include "delegation.h"
  44. #include "iostat.h"
  45. #define NFSDBG_FACILITY NFSDBG_VFS
  46. #define NFS_PARANOIA 1
  47. /* Maximum number of readahead requests
  48. * FIXME: this should really be a sysctl so that users may tune it to suit
  49. * their needs. People that do NFS over a slow network, might for
  50. * instance want to reduce it to something closer to 1 for improved
  51. * interactive response.
  52. */
  53. #define NFS_MAX_READAHEAD (RPC_DEF_SLOT_TABLE - 1)
  54. static void nfs_invalidate_inode(struct inode *);
  55. static int nfs_update_inode(struct inode *, struct nfs_fattr *);
  56. static struct inode *nfs_alloc_inode(struct super_block *sb);
  57. static void nfs_destroy_inode(struct inode *);
  58. static int nfs_write_inode(struct inode *,int);
  59. static void nfs_clear_inode(struct inode *);
  60. static void nfs_umount_begin(struct vfsmount *, int);
  61. static int nfs_statfs(struct super_block *, struct kstatfs *);
  62. static int nfs_show_options(struct seq_file *, struct vfsmount *);
  63. static int nfs_show_stats(struct seq_file *, struct vfsmount *);
  64. static void nfs_zap_acl_cache(struct inode *);
  65. static struct rpc_program nfs_program;
  66. static struct super_operations nfs_sops = {
  67. .alloc_inode = nfs_alloc_inode,
  68. .destroy_inode = nfs_destroy_inode,
  69. .write_inode = nfs_write_inode,
  70. .statfs = nfs_statfs,
  71. .clear_inode = nfs_clear_inode,
  72. .umount_begin = nfs_umount_begin,
  73. .show_options = nfs_show_options,
  74. .show_stats = nfs_show_stats,
  75. };
  76. /*
  77. * RPC cruft for NFS
  78. */
  79. static struct rpc_stat nfs_rpcstat = {
  80. .program = &nfs_program
  81. };
  82. static struct rpc_version * nfs_version[] = {
  83. NULL,
  84. NULL,
  85. &nfs_version2,
  86. #if defined(CONFIG_NFS_V3)
  87. &nfs_version3,
  88. #elif defined(CONFIG_NFS_V4)
  89. NULL,
  90. #endif
  91. #if defined(CONFIG_NFS_V4)
  92. &nfs_version4,
  93. #endif
  94. };
  95. static struct rpc_program nfs_program = {
  96. .name = "nfs",
  97. .number = NFS_PROGRAM,
  98. .nrvers = ARRAY_SIZE(nfs_version),
  99. .version = nfs_version,
  100. .stats = &nfs_rpcstat,
  101. .pipe_dir_name = "/nfs",
  102. };
  103. #ifdef CONFIG_NFS_V3_ACL
  104. static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program };
  105. static struct rpc_version * nfsacl_version[] = {
  106. [3] = &nfsacl_version3,
  107. };
  108. struct rpc_program nfsacl_program = {
  109. .name = "nfsacl",
  110. .number = NFS_ACL_PROGRAM,
  111. .nrvers = ARRAY_SIZE(nfsacl_version),
  112. .version = nfsacl_version,
  113. .stats = &nfsacl_rpcstat,
  114. };
  115. #endif /* CONFIG_NFS_V3_ACL */
  116. static inline unsigned long
  117. nfs_fattr_to_ino_t(struct nfs_fattr *fattr)
  118. {
  119. return nfs_fileid_to_ino_t(fattr->fileid);
  120. }
  121. static int
  122. nfs_write_inode(struct inode *inode, int sync)
  123. {
  124. int flags = sync ? FLUSH_SYNC : 0;
  125. int ret;
  126. ret = nfs_commit_inode(inode, flags);
  127. if (ret < 0)
  128. return ret;
  129. return 0;
  130. }
  131. static void
  132. nfs_clear_inode(struct inode *inode)
  133. {
  134. struct nfs_inode *nfsi = NFS_I(inode);
  135. struct rpc_cred *cred;
  136. /*
  137. * The following should never happen...
  138. */
  139. BUG_ON(nfs_have_writebacks(inode));
  140. BUG_ON (!list_empty(&nfsi->open_files));
  141. nfs_zap_acl_cache(inode);
  142. cred = nfsi->cache_access.cred;
  143. if (cred)
  144. put_rpccred(cred);
  145. BUG_ON(atomic_read(&nfsi->data_updates) != 0);
  146. }
  147. static void nfs_umount_begin(struct vfsmount *vfsmnt, int flags)
  148. {
  149. struct nfs_server *server;
  150. struct rpc_clnt *rpc;
  151. shrink_submounts(vfsmnt, &nfs_automount_list);
  152. if (!(flags & MNT_FORCE))
  153. return;
  154. /* -EIO all pending I/O */
  155. server = NFS_SB(vfsmnt->mnt_sb);
  156. rpc = server->client;
  157. if (!IS_ERR(rpc))
  158. rpc_killall_tasks(rpc);
  159. rpc = server->client_acl;
  160. if (!IS_ERR(rpc))
  161. rpc_killall_tasks(rpc);
  162. }
  163. static inline unsigned long
  164. nfs_block_bits(unsigned long bsize, unsigned char *nrbitsp)
  165. {
  166. /* make sure blocksize is a power of two */
  167. if ((bsize & (bsize - 1)) || nrbitsp) {
  168. unsigned char nrbits;
  169. for (nrbits = 31; nrbits && !(bsize & (1 << nrbits)); nrbits--)
  170. ;
  171. bsize = 1 << nrbits;
  172. if (nrbitsp)
  173. *nrbitsp = nrbits;
  174. }
  175. return bsize;
  176. }
  177. /*
  178. * Calculate the number of 512byte blocks used.
  179. */
  180. static inline unsigned long
  181. nfs_calc_block_size(u64 tsize)
  182. {
  183. loff_t used = (tsize + 511) >> 9;
  184. return (used > ULONG_MAX) ? ULONG_MAX : used;
  185. }
  186. /*
  187. * Compute and set NFS server blocksize
  188. */
  189. static inline unsigned long
  190. nfs_block_size(unsigned long bsize, unsigned char *nrbitsp)
  191. {
  192. if (bsize < NFS_MIN_FILE_IO_SIZE)
  193. bsize = NFS_DEF_FILE_IO_SIZE;
  194. else if (bsize >= NFS_MAX_FILE_IO_SIZE)
  195. bsize = NFS_MAX_FILE_IO_SIZE;
  196. return nfs_block_bits(bsize, nrbitsp);
  197. }
  198. static inline void
  199. nfs_super_set_maxbytes(struct super_block *sb, __u64 maxfilesize)
  200. {
  201. sb->s_maxbytes = (loff_t)maxfilesize;
  202. if (sb->s_maxbytes > MAX_LFS_FILESIZE || sb->s_maxbytes <= 0)
  203. sb->s_maxbytes = MAX_LFS_FILESIZE;
  204. }
  205. /*
  206. * Obtain the root inode of the file system.
  207. */
  208. static struct inode *
  209. nfs_get_root(struct super_block *sb, struct nfs_fh *rootfh, struct nfs_fsinfo *fsinfo)
  210. {
  211. struct nfs_server *server = NFS_SB(sb);
  212. int error;
  213. error = server->rpc_ops->getroot(server, rootfh, fsinfo);
  214. if (error < 0) {
  215. dprintk("nfs_get_root: getattr error = %d\n", -error);
  216. return ERR_PTR(error);
  217. }
  218. server->fsid = fsinfo->fattr->fsid;
  219. return nfs_fhget(sb, rootfh, fsinfo->fattr);
  220. }
  221. /*
  222. * Do NFS version-independent mount processing, and sanity checking
  223. */
  224. static int
  225. nfs_sb_init(struct super_block *sb, rpc_authflavor_t authflavor)
  226. {
  227. struct nfs_server *server;
  228. struct inode *root_inode;
  229. struct nfs_fattr fattr;
  230. struct nfs_fsinfo fsinfo = {
  231. .fattr = &fattr,
  232. };
  233. struct nfs_pathconf pathinfo = {
  234. .fattr = &fattr,
  235. };
  236. int no_root_error = 0;
  237. unsigned long max_rpc_payload;
  238. /* We probably want something more informative here */
  239. snprintf(sb->s_id, sizeof(sb->s_id), "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
  240. server = NFS_SB(sb);
  241. sb->s_magic = NFS_SUPER_MAGIC;
  242. server->io_stats = nfs_alloc_iostats();
  243. if (server->io_stats == NULL)
  244. return -ENOMEM;
  245. root_inode = nfs_get_root(sb, &server->fh, &fsinfo);
  246. /* Did getting the root inode fail? */
  247. if (IS_ERR(root_inode)) {
  248. no_root_error = PTR_ERR(root_inode);
  249. goto out_no_root;
  250. }
  251. sb->s_root = d_alloc_root(root_inode);
  252. if (!sb->s_root) {
  253. no_root_error = -ENOMEM;
  254. goto out_no_root;
  255. }
  256. sb->s_root->d_op = server->rpc_ops->dentry_ops;
  257. /* mount time stamp, in seconds */
  258. server->mount_time = jiffies;
  259. /* Get some general file system info */
  260. if (server->namelen == 0 &&
  261. server->rpc_ops->pathconf(server, &server->fh, &pathinfo) >= 0)
  262. server->namelen = pathinfo.max_namelen;
  263. /* Work out a lot of parameters */
  264. if (server->rsize == 0)
  265. server->rsize = nfs_block_size(fsinfo.rtpref, NULL);
  266. if (server->wsize == 0)
  267. server->wsize = nfs_block_size(fsinfo.wtpref, NULL);
  268. if (fsinfo.rtmax >= 512 && server->rsize > fsinfo.rtmax)
  269. server->rsize = nfs_block_size(fsinfo.rtmax, NULL);
  270. if (fsinfo.wtmax >= 512 && server->wsize > fsinfo.wtmax)
  271. server->wsize = nfs_block_size(fsinfo.wtmax, NULL);
  272. max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
  273. if (server->rsize > max_rpc_payload)
  274. server->rsize = max_rpc_payload;
  275. if (server->rsize > NFS_MAX_FILE_IO_SIZE)
  276. server->rsize = NFS_MAX_FILE_IO_SIZE;
  277. server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  278. if (server->wsize > max_rpc_payload)
  279. server->wsize = max_rpc_payload;
  280. if (server->wsize > NFS_MAX_FILE_IO_SIZE)
  281. server->wsize = NFS_MAX_FILE_IO_SIZE;
  282. server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  283. if (sb->s_blocksize == 0)
  284. sb->s_blocksize = nfs_block_bits(server->wsize,
  285. &sb->s_blocksize_bits);
  286. server->wtmult = nfs_block_bits(fsinfo.wtmult, NULL);
  287. server->dtsize = nfs_block_size(fsinfo.dtpref, NULL);
  288. if (server->dtsize > PAGE_CACHE_SIZE)
  289. server->dtsize = PAGE_CACHE_SIZE;
  290. if (server->dtsize > server->rsize)
  291. server->dtsize = server->rsize;
  292. if (server->flags & NFS_MOUNT_NOAC) {
  293. server->acregmin = server->acregmax = 0;
  294. server->acdirmin = server->acdirmax = 0;
  295. sb->s_flags |= MS_SYNCHRONOUS;
  296. }
  297. server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
  298. nfs_super_set_maxbytes(sb, fsinfo.maxfilesize);
  299. server->client->cl_intr = (server->flags & NFS_MOUNT_INTR) ? 1 : 0;
  300. server->client->cl_softrtry = (server->flags & NFS_MOUNT_SOFT) ? 1 : 0;
  301. /* We're airborne Set socket buffersize */
  302. rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
  303. return 0;
  304. /* Yargs. It didn't work out. */
  305. out_no_root:
  306. dprintk("nfs_sb_init: get root inode failed: errno %d\n", -no_root_error);
  307. if (!IS_ERR(root_inode))
  308. iput(root_inode);
  309. return no_root_error;
  310. }
  311. static void nfs_init_timeout_values(struct rpc_timeout *to, int proto, unsigned int timeo, unsigned int retrans)
  312. {
  313. to->to_initval = timeo * HZ / 10;
  314. to->to_retries = retrans;
  315. if (!to->to_retries)
  316. to->to_retries = 2;
  317. switch (proto) {
  318. case IPPROTO_TCP:
  319. if (!to->to_initval)
  320. to->to_initval = 60 * HZ;
  321. if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
  322. to->to_initval = NFS_MAX_TCP_TIMEOUT;
  323. to->to_increment = to->to_initval;
  324. to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
  325. to->to_exponential = 0;
  326. break;
  327. case IPPROTO_UDP:
  328. default:
  329. if (!to->to_initval)
  330. to->to_initval = 11 * HZ / 10;
  331. if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
  332. to->to_initval = NFS_MAX_UDP_TIMEOUT;
  333. to->to_maxval = NFS_MAX_UDP_TIMEOUT;
  334. to->to_exponential = 1;
  335. break;
  336. }
  337. }
  338. /*
  339. * Create an RPC client handle.
  340. */
  341. static struct rpc_clnt *
  342. nfs_create_client(struct nfs_server *server, const struct nfs_mount_data *data)
  343. {
  344. struct rpc_timeout timeparms;
  345. struct rpc_xprt *xprt = NULL;
  346. struct rpc_clnt *clnt = NULL;
  347. int proto = (data->flags & NFS_MOUNT_TCP) ? IPPROTO_TCP : IPPROTO_UDP;
  348. nfs_init_timeout_values(&timeparms, proto, data->timeo, data->retrans);
  349. server->retrans_timeo = timeparms.to_initval;
  350. server->retrans_count = timeparms.to_retries;
  351. /* create transport and client */
  352. xprt = xprt_create_proto(proto, &server->addr, &timeparms);
  353. if (IS_ERR(xprt)) {
  354. dprintk("%s: cannot create RPC transport. Error = %ld\n",
  355. __FUNCTION__, PTR_ERR(xprt));
  356. return (struct rpc_clnt *)xprt;
  357. }
  358. clnt = rpc_create_client(xprt, server->hostname, &nfs_program,
  359. server->rpc_ops->version, data->pseudoflavor);
  360. if (IS_ERR(clnt)) {
  361. dprintk("%s: cannot create RPC client. Error = %ld\n",
  362. __FUNCTION__, PTR_ERR(xprt));
  363. goto out_fail;
  364. }
  365. clnt->cl_intr = 1;
  366. clnt->cl_softrtry = 1;
  367. return clnt;
  368. out_fail:
  369. return clnt;
  370. }
  371. /*
  372. * The way this works is that the mount process passes a structure
  373. * in the data argument which contains the server's IP address
  374. * and the root file handle obtained from the server's mount
  375. * daemon. We stash these away in the private superblock fields.
  376. */
  377. static int
  378. nfs_fill_super(struct super_block *sb, struct nfs_mount_data *data, int silent)
  379. {
  380. struct nfs_server *server;
  381. rpc_authflavor_t authflavor;
  382. server = NFS_SB(sb);
  383. sb->s_blocksize_bits = 0;
  384. sb->s_blocksize = 0;
  385. if (data->bsize)
  386. sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
  387. if (data->rsize)
  388. server->rsize = nfs_block_size(data->rsize, NULL);
  389. if (data->wsize)
  390. server->wsize = nfs_block_size(data->wsize, NULL);
  391. server->flags = data->flags & NFS_MOUNT_FLAGMASK;
  392. server->acregmin = data->acregmin*HZ;
  393. server->acregmax = data->acregmax*HZ;
  394. server->acdirmin = data->acdirmin*HZ;
  395. server->acdirmax = data->acdirmax*HZ;
  396. /* Start lockd here, before we might error out */
  397. if (!(server->flags & NFS_MOUNT_NONLM))
  398. lockd_up();
  399. server->namelen = data->namlen;
  400. server->hostname = kmalloc(strlen(data->hostname) + 1, GFP_KERNEL);
  401. if (!server->hostname)
  402. return -ENOMEM;
  403. strcpy(server->hostname, data->hostname);
  404. /* Check NFS protocol revision and initialize RPC op vector
  405. * and file handle pool. */
  406. #ifdef CONFIG_NFS_V3
  407. if (server->flags & NFS_MOUNT_VER3) {
  408. server->rpc_ops = &nfs_v3_clientops;
  409. server->caps |= NFS_CAP_READDIRPLUS;
  410. } else {
  411. server->rpc_ops = &nfs_v2_clientops;
  412. }
  413. #else
  414. server->rpc_ops = &nfs_v2_clientops;
  415. #endif
  416. /* Fill in pseudoflavor for mount version < 5 */
  417. if (!(data->flags & NFS_MOUNT_SECFLAVOUR))
  418. data->pseudoflavor = RPC_AUTH_UNIX;
  419. authflavor = data->pseudoflavor; /* save for sb_init() */
  420. /* XXX maybe we want to add a server->pseudoflavor field */
  421. /* Create RPC client handles */
  422. server->client = nfs_create_client(server, data);
  423. if (IS_ERR(server->client))
  424. return PTR_ERR(server->client);
  425. /* RFC 2623, sec 2.3.2 */
  426. if (authflavor != RPC_AUTH_UNIX) {
  427. struct rpc_auth *auth;
  428. server->client_sys = rpc_clone_client(server->client);
  429. if (IS_ERR(server->client_sys))
  430. return PTR_ERR(server->client_sys);
  431. auth = rpcauth_create(RPC_AUTH_UNIX, server->client_sys);
  432. if (IS_ERR(auth))
  433. return PTR_ERR(auth);
  434. } else {
  435. atomic_inc(&server->client->cl_count);
  436. server->client_sys = server->client;
  437. }
  438. if (server->flags & NFS_MOUNT_VER3) {
  439. #ifdef CONFIG_NFS_V3_ACL
  440. if (!(server->flags & NFS_MOUNT_NOACL)) {
  441. server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
  442. /* No errors! Assume that Sun nfsacls are supported */
  443. if (!IS_ERR(server->client_acl))
  444. server->caps |= NFS_CAP_ACLS;
  445. }
  446. #else
  447. server->flags &= ~NFS_MOUNT_NOACL;
  448. #endif /* CONFIG_NFS_V3_ACL */
  449. /*
  450. * The VFS shouldn't apply the umask to mode bits. We will
  451. * do so ourselves when necessary.
  452. */
  453. sb->s_flags |= MS_POSIXACL;
  454. if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
  455. server->namelen = NFS3_MAXNAMLEN;
  456. sb->s_time_gran = 1;
  457. } else {
  458. if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
  459. server->namelen = NFS2_MAXNAMLEN;
  460. }
  461. sb->s_op = &nfs_sops;
  462. return nfs_sb_init(sb, authflavor);
  463. }
  464. static int
  465. nfs_statfs(struct super_block *sb, struct kstatfs *buf)
  466. {
  467. struct nfs_server *server = NFS_SB(sb);
  468. unsigned char blockbits;
  469. unsigned long blockres;
  470. struct nfs_fh *rootfh = NFS_FH(sb->s_root->d_inode);
  471. struct nfs_fattr fattr;
  472. struct nfs_fsstat res = {
  473. .fattr = &fattr,
  474. };
  475. int error;
  476. lock_kernel();
  477. error = server->rpc_ops->statfs(server, rootfh, &res);
  478. buf->f_type = NFS_SUPER_MAGIC;
  479. if (error < 0)
  480. goto out_err;
  481. /*
  482. * Current versions of glibc do not correctly handle the
  483. * case where f_frsize != f_bsize. Eventually we want to
  484. * report the value of wtmult in this field.
  485. */
  486. buf->f_frsize = sb->s_blocksize;
  487. /*
  488. * On most *nix systems, f_blocks, f_bfree, and f_bavail
  489. * are reported in units of f_frsize. Linux hasn't had
  490. * an f_frsize field in its statfs struct until recently,
  491. * thus historically Linux's sys_statfs reports these
  492. * fields in units of f_bsize.
  493. */
  494. buf->f_bsize = sb->s_blocksize;
  495. blockbits = sb->s_blocksize_bits;
  496. blockres = (1 << blockbits) - 1;
  497. buf->f_blocks = (res.tbytes + blockres) >> blockbits;
  498. buf->f_bfree = (res.fbytes + blockres) >> blockbits;
  499. buf->f_bavail = (res.abytes + blockres) >> blockbits;
  500. buf->f_files = res.tfiles;
  501. buf->f_ffree = res.afiles;
  502. buf->f_namelen = server->namelen;
  503. out:
  504. unlock_kernel();
  505. return 0;
  506. out_err:
  507. dprintk("%s: statfs error = %d\n", __FUNCTION__, -error);
  508. buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1;
  509. goto out;
  510. }
  511. static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss, int showdefaults)
  512. {
  513. static struct proc_nfs_info {
  514. int flag;
  515. char *str;
  516. char *nostr;
  517. } nfs_info[] = {
  518. { NFS_MOUNT_SOFT, ",soft", ",hard" },
  519. { NFS_MOUNT_INTR, ",intr", "" },
  520. { NFS_MOUNT_NOCTO, ",nocto", "" },
  521. { NFS_MOUNT_NOAC, ",noac", "" },
  522. { NFS_MOUNT_NONLM, ",nolock", "" },
  523. { NFS_MOUNT_NOACL, ",noacl", "" },
  524. { 0, NULL, NULL }
  525. };
  526. struct proc_nfs_info *nfs_infop;
  527. char buf[12];
  528. char *proto;
  529. seq_printf(m, ",vers=%d", nfss->rpc_ops->version);
  530. seq_printf(m, ",rsize=%d", nfss->rsize);
  531. seq_printf(m, ",wsize=%d", nfss->wsize);
  532. if (nfss->acregmin != 3*HZ || showdefaults)
  533. seq_printf(m, ",acregmin=%d", nfss->acregmin/HZ);
  534. if (nfss->acregmax != 60*HZ || showdefaults)
  535. seq_printf(m, ",acregmax=%d", nfss->acregmax/HZ);
  536. if (nfss->acdirmin != 30*HZ || showdefaults)
  537. seq_printf(m, ",acdirmin=%d", nfss->acdirmin/HZ);
  538. if (nfss->acdirmax != 60*HZ || showdefaults)
  539. seq_printf(m, ",acdirmax=%d", nfss->acdirmax/HZ);
  540. for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
  541. if (nfss->flags & nfs_infop->flag)
  542. seq_puts(m, nfs_infop->str);
  543. else
  544. seq_puts(m, nfs_infop->nostr);
  545. }
  546. switch (nfss->client->cl_xprt->prot) {
  547. case IPPROTO_TCP:
  548. proto = "tcp";
  549. break;
  550. case IPPROTO_UDP:
  551. proto = "udp";
  552. break;
  553. default:
  554. snprintf(buf, sizeof(buf), "%u", nfss->client->cl_xprt->prot);
  555. proto = buf;
  556. }
  557. seq_printf(m, ",proto=%s", proto);
  558. seq_printf(m, ",timeo=%lu", 10U * nfss->retrans_timeo / HZ);
  559. seq_printf(m, ",retrans=%u", nfss->retrans_count);
  560. }
  561. static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
  562. {
  563. struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
  564. nfs_show_mount_options(m, nfss, 0);
  565. seq_puts(m, ",addr=");
  566. seq_escape(m, nfss->hostname, " \t\n\\");
  567. return 0;
  568. }
  569. static int nfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
  570. {
  571. int i, cpu;
  572. struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
  573. struct rpc_auth *auth = nfss->client->cl_auth;
  574. struct nfs_iostats totals = { };
  575. seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
  576. /*
  577. * Display all mount option settings
  578. */
  579. seq_printf(m, "\n\topts:\t");
  580. seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? "ro" : "rw");
  581. seq_puts(m, mnt->mnt_sb->s_flags & MS_SYNCHRONOUS ? ",sync" : "");
  582. seq_puts(m, mnt->mnt_sb->s_flags & MS_NOATIME ? ",noatime" : "");
  583. seq_puts(m, mnt->mnt_sb->s_flags & MS_NODIRATIME ? ",nodiratime" : "");
  584. nfs_show_mount_options(m, nfss, 1);
  585. seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
  586. seq_printf(m, "\n\tcaps:\t");
  587. seq_printf(m, "caps=0x%x", nfss->caps);
  588. seq_printf(m, ",wtmult=%d", nfss->wtmult);
  589. seq_printf(m, ",dtsize=%d", nfss->dtsize);
  590. seq_printf(m, ",bsize=%d", nfss->bsize);
  591. seq_printf(m, ",namelen=%d", nfss->namelen);
  592. #ifdef CONFIG_NFS_V4
  593. if (nfss->rpc_ops->version == 4) {
  594. seq_printf(m, "\n\tnfsv4:\t");
  595. seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
  596. seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
  597. seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
  598. }
  599. #endif
  600. /*
  601. * Display security flavor in effect for this mount
  602. */
  603. seq_printf(m, "\n\tsec:\tflavor=%d", auth->au_ops->au_flavor);
  604. if (auth->au_flavor)
  605. seq_printf(m, ",pseudoflavor=%d", auth->au_flavor);
  606. /*
  607. * Display superblock I/O counters
  608. */
  609. for_each_possible_cpu(cpu) {
  610. struct nfs_iostats *stats;
  611. preempt_disable();
  612. stats = per_cpu_ptr(nfss->io_stats, cpu);
  613. for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
  614. totals.events[i] += stats->events[i];
  615. for (i = 0; i < __NFSIOS_BYTESMAX; i++)
  616. totals.bytes[i] += stats->bytes[i];
  617. preempt_enable();
  618. }
  619. seq_printf(m, "\n\tevents:\t");
  620. for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
  621. seq_printf(m, "%lu ", totals.events[i]);
  622. seq_printf(m, "\n\tbytes:\t");
  623. for (i = 0; i < __NFSIOS_BYTESMAX; i++)
  624. seq_printf(m, "%Lu ", totals.bytes[i]);
  625. seq_printf(m, "\n");
  626. rpc_print_iostats(m, nfss->client);
  627. return 0;
  628. }
  629. /**
  630. * nfs_sync_mapping - helper to flush all mmapped dirty data to disk
  631. */
  632. int nfs_sync_mapping(struct address_space *mapping)
  633. {
  634. int ret;
  635. if (mapping->nrpages == 0)
  636. return 0;
  637. unmap_mapping_range(mapping, 0, 0, 0);
  638. ret = filemap_write_and_wait(mapping);
  639. if (ret != 0)
  640. goto out;
  641. ret = nfs_wb_all(mapping->host);
  642. out:
  643. return ret;
  644. }
  645. /*
  646. * Invalidate the local caches
  647. */
  648. static void nfs_zap_caches_locked(struct inode *inode)
  649. {
  650. struct nfs_inode *nfsi = NFS_I(inode);
  651. int mode = inode->i_mode;
  652. nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
  653. NFS_ATTRTIMEO(inode) = NFS_MINATTRTIMEO(inode);
  654. NFS_ATTRTIMEO_UPDATE(inode) = jiffies;
  655. memset(NFS_COOKIEVERF(inode), 0, sizeof(NFS_COOKIEVERF(inode)));
  656. if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))
  657. nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL|NFS_INO_REVAL_PAGECACHE;
  658. else
  659. nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL|NFS_INO_REVAL_PAGECACHE;
  660. }
  661. void nfs_zap_caches(struct inode *inode)
  662. {
  663. spin_lock(&inode->i_lock);
  664. nfs_zap_caches_locked(inode);
  665. spin_unlock(&inode->i_lock);
  666. }
  667. static void nfs_zap_acl_cache(struct inode *inode)
  668. {
  669. void (*clear_acl_cache)(struct inode *);
  670. clear_acl_cache = NFS_PROTO(inode)->clear_acl_cache;
  671. if (clear_acl_cache != NULL)
  672. clear_acl_cache(inode);
  673. spin_lock(&inode->i_lock);
  674. NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_ACL;
  675. spin_unlock(&inode->i_lock);
  676. }
  677. /*
  678. * Invalidate, but do not unhash, the inode.
  679. * NB: must be called with inode->i_lock held!
  680. */
  681. static void nfs_invalidate_inode(struct inode *inode)
  682. {
  683. set_bit(NFS_INO_STALE, &NFS_FLAGS(inode));
  684. nfs_zap_caches_locked(inode);
  685. }
  686. struct nfs_find_desc {
  687. struct nfs_fh *fh;
  688. struct nfs_fattr *fattr;
  689. };
  690. /*
  691. * In NFSv3 we can have 64bit inode numbers. In order to support
  692. * this, and re-exported directories (also seen in NFSv2)
  693. * we are forced to allow 2 different inodes to have the same
  694. * i_ino.
  695. */
  696. static int
  697. nfs_find_actor(struct inode *inode, void *opaque)
  698. {
  699. struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
  700. struct nfs_fh *fh = desc->fh;
  701. struct nfs_fattr *fattr = desc->fattr;
  702. if (NFS_FILEID(inode) != fattr->fileid)
  703. return 0;
  704. if (nfs_compare_fh(NFS_FH(inode), fh))
  705. return 0;
  706. if (is_bad_inode(inode) || NFS_STALE(inode))
  707. return 0;
  708. return 1;
  709. }
  710. static int
  711. nfs_init_locked(struct inode *inode, void *opaque)
  712. {
  713. struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
  714. struct nfs_fattr *fattr = desc->fattr;
  715. NFS_FILEID(inode) = fattr->fileid;
  716. nfs_copy_fh(NFS_FH(inode), desc->fh);
  717. return 0;
  718. }
  719. /* Don't use READDIRPLUS on directories that we believe are too large */
  720. #define NFS_LIMIT_READDIRPLUS (8*PAGE_SIZE)
  721. /*
  722. * This is our front-end to iget that looks up inodes by file handle
  723. * instead of inode number.
  724. */
  725. struct inode *
  726. nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr)
  727. {
  728. struct nfs_find_desc desc = {
  729. .fh = fh,
  730. .fattr = fattr
  731. };
  732. struct inode *inode = ERR_PTR(-ENOENT);
  733. unsigned long hash;
  734. if ((fattr->valid & NFS_ATTR_FATTR) == 0)
  735. goto out_no_inode;
  736. if (!fattr->nlink) {
  737. printk("NFS: Buggy server - nlink == 0!\n");
  738. goto out_no_inode;
  739. }
  740. hash = nfs_fattr_to_ino_t(fattr);
  741. inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc);
  742. if (inode == NULL) {
  743. inode = ERR_PTR(-ENOMEM);
  744. goto out_no_inode;
  745. }
  746. if (inode->i_state & I_NEW) {
  747. struct nfs_inode *nfsi = NFS_I(inode);
  748. /* We set i_ino for the few things that still rely on it,
  749. * such as stat(2) */
  750. inode->i_ino = hash;
  751. /* We can't support update_atime(), since the server will reset it */
  752. inode->i_flags |= S_NOATIME|S_NOCMTIME;
  753. inode->i_mode = fattr->mode;
  754. /* Why so? Because we want revalidate for devices/FIFOs, and
  755. * that's precisely what we have in nfs_file_inode_operations.
  756. */
  757. inode->i_op = NFS_SB(sb)->rpc_ops->file_inode_ops;
  758. if (S_ISREG(inode->i_mode)) {
  759. inode->i_fop = &nfs_file_operations;
  760. inode->i_data.a_ops = &nfs_file_aops;
  761. inode->i_data.backing_dev_info = &NFS_SB(sb)->backing_dev_info;
  762. } else if (S_ISDIR(inode->i_mode)) {
  763. inode->i_op = NFS_SB(sb)->rpc_ops->dir_inode_ops;
  764. inode->i_fop = &nfs_dir_operations;
  765. if (nfs_server_capable(inode, NFS_CAP_READDIRPLUS)
  766. && fattr->size <= NFS_LIMIT_READDIRPLUS)
  767. set_bit(NFS_INO_ADVISE_RDPLUS, &NFS_FLAGS(inode));
  768. /* Deal with crossing mountpoints */
  769. if (!nfs_fsid_equal(&NFS_SB(sb)->fsid, &fattr->fsid)) {
  770. if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)
  771. inode->i_op = &nfs_referral_inode_operations;
  772. else
  773. inode->i_op = &nfs_mountpoint_inode_operations;
  774. inode->i_fop = NULL;
  775. }
  776. } else if (S_ISLNK(inode->i_mode))
  777. inode->i_op = &nfs_symlink_inode_operations;
  778. else
  779. init_special_inode(inode, inode->i_mode, fattr->rdev);
  780. nfsi->read_cache_jiffies = fattr->time_start;
  781. nfsi->last_updated = jiffies;
  782. inode->i_atime = fattr->atime;
  783. inode->i_mtime = fattr->mtime;
  784. inode->i_ctime = fattr->ctime;
  785. if (fattr->valid & NFS_ATTR_FATTR_V4)
  786. nfsi->change_attr = fattr->change_attr;
  787. inode->i_size = nfs_size_to_loff_t(fattr->size);
  788. inode->i_nlink = fattr->nlink;
  789. inode->i_uid = fattr->uid;
  790. inode->i_gid = fattr->gid;
  791. if (fattr->valid & (NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4)) {
  792. /*
  793. * report the blocks in 512byte units
  794. */
  795. inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
  796. inode->i_blksize = inode->i_sb->s_blocksize;
  797. } else {
  798. inode->i_blocks = fattr->du.nfs2.blocks;
  799. inode->i_blksize = fattr->du.nfs2.blocksize;
  800. }
  801. nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
  802. nfsi->attrtimeo_timestamp = jiffies;
  803. memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
  804. nfsi->cache_access.cred = NULL;
  805. unlock_new_inode(inode);
  806. } else
  807. nfs_refresh_inode(inode, fattr);
  808. dprintk("NFS: nfs_fhget(%s/%Ld ct=%d)\n",
  809. inode->i_sb->s_id,
  810. (long long)NFS_FILEID(inode),
  811. atomic_read(&inode->i_count));
  812. out:
  813. return inode;
  814. out_no_inode:
  815. dprintk("nfs_fhget: iget failed with error %ld\n", PTR_ERR(inode));
  816. goto out;
  817. }
  818. #define NFS_VALID_ATTRS (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE|ATTR_ATIME|ATTR_ATIME_SET|ATTR_MTIME|ATTR_MTIME_SET)
  819. int
  820. nfs_setattr(struct dentry *dentry, struct iattr *attr)
  821. {
  822. struct inode *inode = dentry->d_inode;
  823. struct nfs_fattr fattr;
  824. int error;
  825. nfs_inc_stats(inode, NFSIOS_VFSSETATTR);
  826. if (attr->ia_valid & ATTR_SIZE) {
  827. if (!S_ISREG(inode->i_mode) || attr->ia_size == i_size_read(inode))
  828. attr->ia_valid &= ~ATTR_SIZE;
  829. }
  830. /* Optimization: if the end result is no change, don't RPC */
  831. attr->ia_valid &= NFS_VALID_ATTRS;
  832. if (attr->ia_valid == 0)
  833. return 0;
  834. lock_kernel();
  835. nfs_begin_data_update(inode);
  836. /* Write all dirty data */
  837. filemap_write_and_wait(inode->i_mapping);
  838. nfs_wb_all(inode);
  839. /*
  840. * Return any delegations if we're going to change ACLs
  841. */
  842. if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
  843. nfs_inode_return_delegation(inode);
  844. error = NFS_PROTO(inode)->setattr(dentry, &fattr, attr);
  845. if (error == 0)
  846. nfs_refresh_inode(inode, &fattr);
  847. nfs_end_data_update(inode);
  848. unlock_kernel();
  849. return error;
  850. }
  851. /**
  852. * nfs_setattr_update_inode - Update inode metadata after a setattr call.
  853. * @inode: pointer to struct inode
  854. * @attr: pointer to struct iattr
  855. *
  856. * Note: we do this in the *proc.c in order to ensure that
  857. * it works for things like exclusive creates too.
  858. */
  859. void nfs_setattr_update_inode(struct inode *inode, struct iattr *attr)
  860. {
  861. if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) {
  862. if ((attr->ia_valid & ATTR_MODE) != 0) {
  863. int mode = attr->ia_mode & S_IALLUGO;
  864. mode |= inode->i_mode & ~S_IALLUGO;
  865. inode->i_mode = mode;
  866. }
  867. if ((attr->ia_valid & ATTR_UID) != 0)
  868. inode->i_uid = attr->ia_uid;
  869. if ((attr->ia_valid & ATTR_GID) != 0)
  870. inode->i_gid = attr->ia_gid;
  871. spin_lock(&inode->i_lock);
  872. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  873. spin_unlock(&inode->i_lock);
  874. }
  875. if ((attr->ia_valid & ATTR_SIZE) != 0) {
  876. nfs_inc_stats(inode, NFSIOS_SETATTRTRUNC);
  877. inode->i_size = attr->ia_size;
  878. vmtruncate(inode, attr->ia_size);
  879. }
  880. }
  881. static int nfs_wait_schedule(void *word)
  882. {
  883. if (signal_pending(current))
  884. return -ERESTARTSYS;
  885. schedule();
  886. return 0;
  887. }
  888. /*
  889. * Wait for the inode to get unlocked.
  890. */
  891. static int nfs_wait_on_inode(struct inode *inode)
  892. {
  893. struct rpc_clnt *clnt = NFS_CLIENT(inode);
  894. struct nfs_inode *nfsi = NFS_I(inode);
  895. sigset_t oldmask;
  896. int error;
  897. rpc_clnt_sigmask(clnt, &oldmask);
  898. error = wait_on_bit_lock(&nfsi->flags, NFS_INO_REVALIDATING,
  899. nfs_wait_schedule, TASK_INTERRUPTIBLE);
  900. rpc_clnt_sigunmask(clnt, &oldmask);
  901. return error;
  902. }
  903. static void nfs_wake_up_inode(struct inode *inode)
  904. {
  905. struct nfs_inode *nfsi = NFS_I(inode);
  906. clear_bit(NFS_INO_REVALIDATING, &nfsi->flags);
  907. smp_mb__after_clear_bit();
  908. wake_up_bit(&nfsi->flags, NFS_INO_REVALIDATING);
  909. }
  910. int nfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  911. {
  912. struct inode *inode = dentry->d_inode;
  913. int need_atime = NFS_I(inode)->cache_validity & NFS_INO_INVALID_ATIME;
  914. int err;
  915. /* Flush out writes to the server in order to update c/mtime */
  916. nfs_sync_inode_wait(inode, 0, 0, FLUSH_NOCOMMIT);
  917. /*
  918. * We may force a getattr if the user cares about atime.
  919. *
  920. * Note that we only have to check the vfsmount flags here:
  921. * - NFS always sets S_NOATIME by so checking it would give a
  922. * bogus result
  923. * - NFS never sets MS_NOATIME or MS_NODIRATIME so there is
  924. * no point in checking those.
  925. */
  926. if ((mnt->mnt_flags & MNT_NOATIME) ||
  927. ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
  928. need_atime = 0;
  929. if (need_atime)
  930. err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  931. else
  932. err = nfs_revalidate_inode(NFS_SERVER(inode), inode);
  933. if (!err)
  934. generic_fillattr(inode, stat);
  935. return err;
  936. }
  937. static struct nfs_open_context *alloc_nfs_open_context(struct vfsmount *mnt, struct dentry *dentry, struct rpc_cred *cred)
  938. {
  939. struct nfs_open_context *ctx;
  940. ctx = (struct nfs_open_context *)kmalloc(sizeof(*ctx), GFP_KERNEL);
  941. if (ctx != NULL) {
  942. atomic_set(&ctx->count, 1);
  943. ctx->dentry = dget(dentry);
  944. ctx->vfsmnt = mntget(mnt);
  945. ctx->cred = get_rpccred(cred);
  946. ctx->state = NULL;
  947. ctx->lockowner = current->files;
  948. ctx->error = 0;
  949. ctx->dir_cookie = 0;
  950. }
  951. return ctx;
  952. }
  953. struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx)
  954. {
  955. if (ctx != NULL)
  956. atomic_inc(&ctx->count);
  957. return ctx;
  958. }
  959. void put_nfs_open_context(struct nfs_open_context *ctx)
  960. {
  961. if (atomic_dec_and_test(&ctx->count)) {
  962. if (!list_empty(&ctx->list)) {
  963. struct inode *inode = ctx->dentry->d_inode;
  964. spin_lock(&inode->i_lock);
  965. list_del(&ctx->list);
  966. spin_unlock(&inode->i_lock);
  967. }
  968. if (ctx->state != NULL)
  969. nfs4_close_state(ctx->state, ctx->mode);
  970. if (ctx->cred != NULL)
  971. put_rpccred(ctx->cred);
  972. dput(ctx->dentry);
  973. mntput(ctx->vfsmnt);
  974. kfree(ctx);
  975. }
  976. }
  977. /*
  978. * Ensure that mmap has a recent RPC credential for use when writing out
  979. * shared pages
  980. */
  981. static void nfs_file_set_open_context(struct file *filp, struct nfs_open_context *ctx)
  982. {
  983. struct inode *inode = filp->f_dentry->d_inode;
  984. struct nfs_inode *nfsi = NFS_I(inode);
  985. filp->private_data = get_nfs_open_context(ctx);
  986. spin_lock(&inode->i_lock);
  987. list_add(&ctx->list, &nfsi->open_files);
  988. spin_unlock(&inode->i_lock);
  989. }
  990. /*
  991. * Given an inode, search for an open context with the desired characteristics
  992. */
  993. struct nfs_open_context *nfs_find_open_context(struct inode *inode, struct rpc_cred *cred, int mode)
  994. {
  995. struct nfs_inode *nfsi = NFS_I(inode);
  996. struct nfs_open_context *pos, *ctx = NULL;
  997. spin_lock(&inode->i_lock);
  998. list_for_each_entry(pos, &nfsi->open_files, list) {
  999. if (cred != NULL && pos->cred != cred)
  1000. continue;
  1001. if ((pos->mode & mode) == mode) {
  1002. ctx = get_nfs_open_context(pos);
  1003. break;
  1004. }
  1005. }
  1006. spin_unlock(&inode->i_lock);
  1007. return ctx;
  1008. }
  1009. static void nfs_file_clear_open_context(struct file *filp)
  1010. {
  1011. struct inode *inode = filp->f_dentry->d_inode;
  1012. struct nfs_open_context *ctx = (struct nfs_open_context *)filp->private_data;
  1013. if (ctx) {
  1014. filp->private_data = NULL;
  1015. spin_lock(&inode->i_lock);
  1016. list_move_tail(&ctx->list, &NFS_I(inode)->open_files);
  1017. spin_unlock(&inode->i_lock);
  1018. put_nfs_open_context(ctx);
  1019. }
  1020. }
  1021. /*
  1022. * These allocate and release file read/write context information.
  1023. */
  1024. int nfs_open(struct inode *inode, struct file *filp)
  1025. {
  1026. struct nfs_open_context *ctx;
  1027. struct rpc_cred *cred;
  1028. cred = rpcauth_lookupcred(NFS_CLIENT(inode)->cl_auth, 0);
  1029. if (IS_ERR(cred))
  1030. return PTR_ERR(cred);
  1031. ctx = alloc_nfs_open_context(filp->f_vfsmnt, filp->f_dentry, cred);
  1032. put_rpccred(cred);
  1033. if (ctx == NULL)
  1034. return -ENOMEM;
  1035. ctx->mode = filp->f_mode;
  1036. nfs_file_set_open_context(filp, ctx);
  1037. put_nfs_open_context(ctx);
  1038. return 0;
  1039. }
  1040. int nfs_release(struct inode *inode, struct file *filp)
  1041. {
  1042. nfs_file_clear_open_context(filp);
  1043. return 0;
  1044. }
  1045. /*
  1046. * This function is called whenever some part of NFS notices that
  1047. * the cached attributes have to be refreshed.
  1048. */
  1049. int
  1050. __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
  1051. {
  1052. int status = -ESTALE;
  1053. struct nfs_fattr fattr;
  1054. struct nfs_inode *nfsi = NFS_I(inode);
  1055. dfprintk(PAGECACHE, "NFS: revalidating (%s/%Ld)\n",
  1056. inode->i_sb->s_id, (long long)NFS_FILEID(inode));
  1057. nfs_inc_stats(inode, NFSIOS_INODEREVALIDATE);
  1058. lock_kernel();
  1059. if (!inode || is_bad_inode(inode))
  1060. goto out_nowait;
  1061. if (NFS_STALE(inode))
  1062. goto out_nowait;
  1063. status = nfs_wait_on_inode(inode);
  1064. if (status < 0)
  1065. goto out;
  1066. if (NFS_STALE(inode)) {
  1067. status = -ESTALE;
  1068. /* Do we trust the cached ESTALE? */
  1069. if (NFS_ATTRTIMEO(inode) != 0) {
  1070. if (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME)) {
  1071. /* no */
  1072. } else
  1073. goto out;
  1074. }
  1075. }
  1076. status = NFS_PROTO(inode)->getattr(server, NFS_FH(inode), &fattr);
  1077. if (status != 0) {
  1078. dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) getattr failed, error=%d\n",
  1079. inode->i_sb->s_id,
  1080. (long long)NFS_FILEID(inode), status);
  1081. if (status == -ESTALE) {
  1082. nfs_zap_caches(inode);
  1083. if (!S_ISDIR(inode->i_mode))
  1084. set_bit(NFS_INO_STALE, &NFS_FLAGS(inode));
  1085. }
  1086. goto out;
  1087. }
  1088. spin_lock(&inode->i_lock);
  1089. status = nfs_update_inode(inode, &fattr);
  1090. if (status) {
  1091. spin_unlock(&inode->i_lock);
  1092. dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) refresh failed, error=%d\n",
  1093. inode->i_sb->s_id,
  1094. (long long)NFS_FILEID(inode), status);
  1095. goto out;
  1096. }
  1097. spin_unlock(&inode->i_lock);
  1098. if (nfsi->cache_validity & NFS_INO_INVALID_ACL)
  1099. nfs_zap_acl_cache(inode);
  1100. dfprintk(PAGECACHE, "NFS: (%s/%Ld) revalidation complete\n",
  1101. inode->i_sb->s_id,
  1102. (long long)NFS_FILEID(inode));
  1103. out:
  1104. nfs_wake_up_inode(inode);
  1105. out_nowait:
  1106. unlock_kernel();
  1107. return status;
  1108. }
  1109. int nfs_attribute_timeout(struct inode *inode)
  1110. {
  1111. struct nfs_inode *nfsi = NFS_I(inode);
  1112. if (nfs_have_delegation(inode, FMODE_READ))
  1113. return 0;
  1114. return time_after(jiffies, nfsi->read_cache_jiffies+nfsi->attrtimeo);
  1115. }
  1116. /**
  1117. * nfs_revalidate_inode - Revalidate the inode attributes
  1118. * @server - pointer to nfs_server struct
  1119. * @inode - pointer to inode struct
  1120. *
  1121. * Updates inode attribute information by retrieving the data from the server.
  1122. */
  1123. int nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
  1124. {
  1125. if (!(NFS_I(inode)->cache_validity & NFS_INO_INVALID_ATTR)
  1126. && !nfs_attribute_timeout(inode))
  1127. return NFS_STALE(inode) ? -ESTALE : 0;
  1128. return __nfs_revalidate_inode(server, inode);
  1129. }
  1130. /**
  1131. * nfs_revalidate_mapping - Revalidate the pagecache
  1132. * @inode - pointer to host inode
  1133. * @mapping - pointer to mapping
  1134. */
  1135. int nfs_revalidate_mapping(struct inode *inode, struct address_space *mapping)
  1136. {
  1137. struct nfs_inode *nfsi = NFS_I(inode);
  1138. int ret = 0;
  1139. if (NFS_STALE(inode))
  1140. ret = -ESTALE;
  1141. if ((nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
  1142. || nfs_attribute_timeout(inode))
  1143. ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  1144. if (nfsi->cache_validity & NFS_INO_INVALID_DATA) {
  1145. nfs_inc_stats(inode, NFSIOS_DATAINVALIDATE);
  1146. if (S_ISREG(inode->i_mode))
  1147. nfs_sync_mapping(mapping);
  1148. invalidate_inode_pages2(mapping);
  1149. spin_lock(&inode->i_lock);
  1150. nfsi->cache_validity &= ~NFS_INO_INVALID_DATA;
  1151. if (S_ISDIR(inode->i_mode)) {
  1152. memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
  1153. /* This ensures we revalidate child dentries */
  1154. nfsi->cache_change_attribute = jiffies;
  1155. }
  1156. spin_unlock(&inode->i_lock);
  1157. dfprintk(PAGECACHE, "NFS: (%s/%Ld) data cache invalidated\n",
  1158. inode->i_sb->s_id,
  1159. (long long)NFS_FILEID(inode));
  1160. }
  1161. return ret;
  1162. }
  1163. /**
  1164. * nfs_begin_data_update
  1165. * @inode - pointer to inode
  1166. * Declare that a set of operations will update file data on the server
  1167. */
  1168. void nfs_begin_data_update(struct inode *inode)
  1169. {
  1170. atomic_inc(&NFS_I(inode)->data_updates);
  1171. }
  1172. /**
  1173. * nfs_end_data_update
  1174. * @inode - pointer to inode
  1175. * Declare end of the operations that will update file data
  1176. * This will mark the inode as immediately needing revalidation
  1177. * of its attribute cache.
  1178. */
  1179. void nfs_end_data_update(struct inode *inode)
  1180. {
  1181. struct nfs_inode *nfsi = NFS_I(inode);
  1182. if (!nfs_have_delegation(inode, FMODE_READ)) {
  1183. /* Directories and symlinks: invalidate page cache */
  1184. if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) {
  1185. spin_lock(&inode->i_lock);
  1186. nfsi->cache_validity |= NFS_INO_INVALID_DATA;
  1187. spin_unlock(&inode->i_lock);
  1188. }
  1189. }
  1190. nfsi->cache_change_attribute = jiffies;
  1191. atomic_dec(&nfsi->data_updates);
  1192. }
  1193. static void nfs_wcc_update_inode(struct inode *inode, struct nfs_fattr *fattr)
  1194. {
  1195. struct nfs_inode *nfsi = NFS_I(inode);
  1196. /* If we have atomic WCC data, we may update some attributes */
  1197. if ((fattr->valid & NFS_ATTR_WCC) != 0) {
  1198. if (timespec_equal(&inode->i_ctime, &fattr->pre_ctime)) {
  1199. memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
  1200. nfsi->cache_change_attribute = jiffies;
  1201. }
  1202. if (timespec_equal(&inode->i_mtime, &fattr->pre_mtime)) {
  1203. memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
  1204. nfsi->cache_change_attribute = jiffies;
  1205. }
  1206. if (inode->i_size == fattr->pre_size && nfsi->npages == 0) {
  1207. inode->i_size = fattr->size;
  1208. nfsi->cache_change_attribute = jiffies;
  1209. }
  1210. }
  1211. }
  1212. /**
  1213. * nfs_check_inode_attributes - verify consistency of the inode attribute cache
  1214. * @inode - pointer to inode
  1215. * @fattr - updated attributes
  1216. *
  1217. * Verifies the attribute cache. If we have just changed the attributes,
  1218. * so that fattr carries weak cache consistency data, then it may
  1219. * also update the ctime/mtime/change_attribute.
  1220. */
  1221. static int nfs_check_inode_attributes(struct inode *inode, struct nfs_fattr *fattr)
  1222. {
  1223. struct nfs_inode *nfsi = NFS_I(inode);
  1224. loff_t cur_size, new_isize;
  1225. int data_unstable;
  1226. /* Has the inode gone and changed behind our back? */
  1227. if (nfsi->fileid != fattr->fileid
  1228. || (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT)) {
  1229. return -EIO;
  1230. }
  1231. /* Are we in the process of updating data on the server? */
  1232. data_unstable = nfs_caches_unstable(inode);
  1233. /* Do atomic weak cache consistency updates */
  1234. nfs_wcc_update_inode(inode, fattr);
  1235. if ((fattr->valid & NFS_ATTR_FATTR_V4) != 0 &&
  1236. nfsi->change_attr != fattr->change_attr)
  1237. nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
  1238. /* Verify a few of the more important attributes */
  1239. if (!timespec_equal(&inode->i_mtime, &fattr->mtime))
  1240. nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
  1241. cur_size = i_size_read(inode);
  1242. new_isize = nfs_size_to_loff_t(fattr->size);
  1243. if (cur_size != new_isize && nfsi->npages == 0)
  1244. nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
  1245. /* Have any file permissions changed? */
  1246. if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)
  1247. || inode->i_uid != fattr->uid
  1248. || inode->i_gid != fattr->gid)
  1249. nfsi->cache_validity |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
  1250. /* Has the link count changed? */
  1251. if (inode->i_nlink != fattr->nlink)
  1252. nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
  1253. if (!timespec_equal(&inode->i_atime, &fattr->atime))
  1254. nfsi->cache_validity |= NFS_INO_INVALID_ATIME;
  1255. nfsi->read_cache_jiffies = fattr->time_start;
  1256. return 0;
  1257. }
  1258. /**
  1259. * nfs_refresh_inode - try to update the inode attribute cache
  1260. * @inode - pointer to inode
  1261. * @fattr - updated attributes
  1262. *
  1263. * Check that an RPC call that returned attributes has not overlapped with
  1264. * other recent updates of the inode metadata, then decide whether it is
  1265. * safe to do a full update of the inode attributes, or whether just to
  1266. * call nfs_check_inode_attributes.
  1267. */
  1268. int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
  1269. {
  1270. struct nfs_inode *nfsi = NFS_I(inode);
  1271. int status;
  1272. if ((fattr->valid & NFS_ATTR_FATTR) == 0)
  1273. return 0;
  1274. spin_lock(&inode->i_lock);
  1275. if (time_after(fattr->time_start, nfsi->last_updated))
  1276. status = nfs_update_inode(inode, fattr);
  1277. else
  1278. status = nfs_check_inode_attributes(inode, fattr);
  1279. spin_unlock(&inode->i_lock);
  1280. return status;
  1281. }
  1282. /**
  1283. * nfs_post_op_update_inode - try to update the inode attribute cache
  1284. * @inode - pointer to inode
  1285. * @fattr - updated attributes
  1286. *
  1287. * After an operation that has changed the inode metadata, mark the
  1288. * attribute cache as being invalid, then try to update it.
  1289. */
  1290. int nfs_post_op_update_inode(struct inode *inode, struct nfs_fattr *fattr)
  1291. {
  1292. struct nfs_inode *nfsi = NFS_I(inode);
  1293. int status = 0;
  1294. spin_lock(&inode->i_lock);
  1295. if (unlikely((fattr->valid & NFS_ATTR_FATTR) == 0)) {
  1296. nfsi->cache_validity |= NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
  1297. goto out;
  1298. }
  1299. status = nfs_update_inode(inode, fattr);
  1300. out:
  1301. spin_unlock(&inode->i_lock);
  1302. return status;
  1303. }
  1304. /*
  1305. * Many nfs protocol calls return the new file attributes after
  1306. * an operation. Here we update the inode to reflect the state
  1307. * of the server's inode.
  1308. *
  1309. * This is a bit tricky because we have to make sure all dirty pages
  1310. * have been sent off to the server before calling invalidate_inode_pages.
  1311. * To make sure no other process adds more write requests while we try
  1312. * our best to flush them, we make them sleep during the attribute refresh.
  1313. *
  1314. * A very similar scenario holds for the dir cache.
  1315. */
  1316. static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr)
  1317. {
  1318. struct nfs_server *server;
  1319. struct nfs_inode *nfsi = NFS_I(inode);
  1320. loff_t cur_isize, new_isize;
  1321. unsigned int invalid = 0;
  1322. int data_stable;
  1323. dfprintk(VFS, "NFS: %s(%s/%ld ct=%d info=0x%x)\n",
  1324. __FUNCTION__, inode->i_sb->s_id, inode->i_ino,
  1325. atomic_read(&inode->i_count), fattr->valid);
  1326. if (nfsi->fileid != fattr->fileid)
  1327. goto out_fileid;
  1328. /*
  1329. * Make sure the inode's type hasn't changed.
  1330. */
  1331. if ((inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
  1332. goto out_changed;
  1333. server = NFS_SERVER(inode);
  1334. /* Update the fsid if and only if this is the root directory */
  1335. if (inode == inode->i_sb->s_root->d_inode
  1336. && !nfs_fsid_equal(&server->fsid, &fattr->fsid))
  1337. server->fsid = fattr->fsid;
  1338. /*
  1339. * Update the read time so we don't revalidate too often.
  1340. */
  1341. nfsi->read_cache_jiffies = fattr->time_start;
  1342. nfsi->last_updated = jiffies;
  1343. /* Are we racing with known updates of the metadata on the server? */
  1344. data_stable = nfs_verify_change_attribute(inode, fattr->time_start);
  1345. if (data_stable)
  1346. nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_ATIME);
  1347. /* Do atomic weak cache consistency updates */
  1348. nfs_wcc_update_inode(inode, fattr);
  1349. /* Check if our cached file size is stale */
  1350. new_isize = nfs_size_to_loff_t(fattr->size);
  1351. cur_isize = i_size_read(inode);
  1352. if (new_isize != cur_isize) {
  1353. /* Do we perhaps have any outstanding writes? */
  1354. if (nfsi->npages == 0) {
  1355. /* No, but did we race with nfs_end_data_update()? */
  1356. if (data_stable) {
  1357. inode->i_size = new_isize;
  1358. invalid |= NFS_INO_INVALID_DATA;
  1359. }
  1360. invalid |= NFS_INO_INVALID_ATTR;
  1361. } else if (new_isize > cur_isize) {
  1362. inode->i_size = new_isize;
  1363. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
  1364. }
  1365. nfsi->cache_change_attribute = jiffies;
  1366. dprintk("NFS: isize change on server for file %s/%ld\n",
  1367. inode->i_sb->s_id, inode->i_ino);
  1368. }
  1369. /* Check if the mtime agrees */
  1370. if (!timespec_equal(&inode->i_mtime, &fattr->mtime)) {
  1371. memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
  1372. dprintk("NFS: mtime change on server for file %s/%ld\n",
  1373. inode->i_sb->s_id, inode->i_ino);
  1374. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
  1375. nfsi->cache_change_attribute = jiffies;
  1376. }
  1377. /* If ctime has changed we should definitely clear access+acl caches */
  1378. if (!timespec_equal(&inode->i_ctime, &fattr->ctime)) {
  1379. invalid |= NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  1380. memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
  1381. nfsi->cache_change_attribute = jiffies;
  1382. }
  1383. memcpy(&inode->i_atime, &fattr->atime, sizeof(inode->i_atime));
  1384. if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO) ||
  1385. inode->i_uid != fattr->uid ||
  1386. inode->i_gid != fattr->gid)
  1387. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  1388. inode->i_mode = fattr->mode;
  1389. inode->i_nlink = fattr->nlink;
  1390. inode->i_uid = fattr->uid;
  1391. inode->i_gid = fattr->gid;
  1392. if (fattr->valid & (NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4)) {
  1393. /*
  1394. * report the blocks in 512byte units
  1395. */
  1396. inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
  1397. inode->i_blksize = inode->i_sb->s_blocksize;
  1398. } else {
  1399. inode->i_blocks = fattr->du.nfs2.blocks;
  1400. inode->i_blksize = fattr->du.nfs2.blocksize;
  1401. }
  1402. if ((fattr->valid & NFS_ATTR_FATTR_V4) != 0 &&
  1403. nfsi->change_attr != fattr->change_attr) {
  1404. dprintk("NFS: change_attr change on server for file %s/%ld\n",
  1405. inode->i_sb->s_id, inode->i_ino);
  1406. nfsi->change_attr = fattr->change_attr;
  1407. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  1408. nfsi->cache_change_attribute = jiffies;
  1409. }
  1410. /* Update attrtimeo value if we're out of the unstable period */
  1411. if (invalid & NFS_INO_INVALID_ATTR) {
  1412. nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
  1413. nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
  1414. nfsi->attrtimeo_timestamp = jiffies;
  1415. } else if (time_after(jiffies, nfsi->attrtimeo_timestamp+nfsi->attrtimeo)) {
  1416. if ((nfsi->attrtimeo <<= 1) > NFS_MAXATTRTIMEO(inode))
  1417. nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
  1418. nfsi->attrtimeo_timestamp = jiffies;
  1419. }
  1420. /* Don't invalidate the data if we were to blame */
  1421. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
  1422. || S_ISLNK(inode->i_mode)))
  1423. invalid &= ~NFS_INO_INVALID_DATA;
  1424. if (data_stable)
  1425. invalid &= ~(NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME|NFS_INO_REVAL_PAGECACHE);
  1426. if (!nfs_have_delegation(inode, FMODE_READ))
  1427. nfsi->cache_validity |= invalid;
  1428. return 0;
  1429. out_changed:
  1430. /*
  1431. * Big trouble! The inode has become a different object.
  1432. */
  1433. #ifdef NFS_PARANOIA
  1434. printk(KERN_DEBUG "%s: inode %ld mode changed, %07o to %07o\n",
  1435. __FUNCTION__, inode->i_ino, inode->i_mode, fattr->mode);
  1436. #endif
  1437. out_err:
  1438. /*
  1439. * No need to worry about unhashing the dentry, as the
  1440. * lookup validation will know that the inode is bad.
  1441. * (But we fall through to invalidate the caches.)
  1442. */
  1443. nfs_invalidate_inode(inode);
  1444. return -ESTALE;
  1445. out_fileid:
  1446. printk(KERN_ERR "NFS: server %s error: fileid changed\n"
  1447. "fsid %s: expected fileid 0x%Lx, got 0x%Lx\n",
  1448. NFS_SERVER(inode)->hostname, inode->i_sb->s_id,
  1449. (long long)nfsi->fileid, (long long)fattr->fileid);
  1450. goto out_err;
  1451. }
  1452. /*
  1453. * File system information
  1454. */
  1455. /*
  1456. * nfs_path - reconstruct the path given an arbitrary dentry
  1457. * @base - arbitrary string to prepend to the path
  1458. * @dentry - pointer to dentry
  1459. * @buffer - result buffer
  1460. * @buflen - length of buffer
  1461. *
  1462. * Helper function for constructing the path from the
  1463. * root dentry to an arbitrary hashed dentry.
  1464. *
  1465. * This is mainly for use in figuring out the path on the
  1466. * server side when automounting on top of an existing partition.
  1467. */
  1468. static char *nfs_path(const char *base, const struct dentry *dentry,
  1469. char *buffer, ssize_t buflen)
  1470. {
  1471. char *end = buffer+buflen;
  1472. int namelen;
  1473. *--end = '\0';
  1474. buflen--;
  1475. spin_lock(&dcache_lock);
  1476. while (!IS_ROOT(dentry)) {
  1477. namelen = dentry->d_name.len;
  1478. buflen -= namelen + 1;
  1479. if (buflen < 0)
  1480. goto Elong;
  1481. end -= namelen;
  1482. memcpy(end, dentry->d_name.name, namelen);
  1483. *--end = '/';
  1484. dentry = dentry->d_parent;
  1485. }
  1486. spin_unlock(&dcache_lock);
  1487. namelen = strlen(base);
  1488. /* Strip off excess slashes in base string */
  1489. while (namelen > 0 && base[namelen - 1] == '/')
  1490. namelen--;
  1491. buflen -= namelen;
  1492. if (buflen < 0)
  1493. goto Elong;
  1494. end -= namelen;
  1495. memcpy(end, base, namelen);
  1496. return end;
  1497. Elong:
  1498. return ERR_PTR(-ENAMETOOLONG);
  1499. }
  1500. struct nfs_clone_mount {
  1501. const struct super_block *sb;
  1502. const struct dentry *dentry;
  1503. struct nfs_fh *fh;
  1504. struct nfs_fattr *fattr;
  1505. char *hostname;
  1506. char *mnt_path;
  1507. struct sockaddr_in *addr;
  1508. rpc_authflavor_t authflavor;
  1509. };
  1510. static struct super_block *nfs_clone_generic_sb(struct nfs_clone_mount *data,
  1511. struct super_block *(*fill_sb)(struct nfs_server *, struct nfs_clone_mount *),
  1512. struct nfs_server *(*fill_server)(struct super_block *, struct nfs_clone_mount *))
  1513. {
  1514. struct nfs_server *server;
  1515. struct nfs_server *parent = NFS_SB(data->sb);
  1516. struct super_block *sb = ERR_PTR(-EINVAL);
  1517. void *err = ERR_PTR(-ENOMEM);
  1518. char *hostname;
  1519. int len;
  1520. server = kmalloc(sizeof(struct nfs_server), GFP_KERNEL);
  1521. if (server == NULL)
  1522. goto out_err;
  1523. memcpy(server, parent, sizeof(*server));
  1524. hostname = (data->hostname != NULL) ? data->hostname : parent->hostname;
  1525. len = strlen(hostname) + 1;
  1526. server->hostname = kmalloc(len, GFP_KERNEL);
  1527. if (server->hostname == NULL)
  1528. goto free_server;
  1529. memcpy(server->hostname, hostname, len);
  1530. if (rpciod_up() != 0)
  1531. goto free_hostname;
  1532. sb = fill_sb(server, data);
  1533. if (IS_ERR((err = sb)) || sb->s_root)
  1534. goto kill_rpciod;
  1535. server = fill_server(sb, data);
  1536. if (IS_ERR((err = server)))
  1537. goto out_deactivate;
  1538. return sb;
  1539. out_deactivate:
  1540. up_write(&sb->s_umount);
  1541. deactivate_super(sb);
  1542. return (struct super_block *)err;
  1543. kill_rpciod:
  1544. rpciod_down();
  1545. free_hostname:
  1546. kfree(server->hostname);
  1547. free_server:
  1548. kfree(server);
  1549. out_err:
  1550. return (struct super_block *)err;
  1551. }
  1552. static int nfs_set_super(struct super_block *s, void *data)
  1553. {
  1554. s->s_fs_info = data;
  1555. return set_anon_super(s, data);
  1556. }
  1557. static int nfs_compare_super(struct super_block *sb, void *data)
  1558. {
  1559. struct nfs_server *server = data;
  1560. struct nfs_server *old = NFS_SB(sb);
  1561. if (old->addr.sin_addr.s_addr != server->addr.sin_addr.s_addr)
  1562. return 0;
  1563. if (old->addr.sin_port != server->addr.sin_port)
  1564. return 0;
  1565. return !nfs_compare_fh(&old->fh, &server->fh);
  1566. }
  1567. static struct super_block *nfs_get_sb(struct file_system_type *fs_type,
  1568. int flags, const char *dev_name, void *raw_data)
  1569. {
  1570. int error;
  1571. struct nfs_server *server = NULL;
  1572. struct super_block *s;
  1573. struct nfs_fh *root;
  1574. struct nfs_mount_data *data = raw_data;
  1575. s = ERR_PTR(-EINVAL);
  1576. if (data == NULL) {
  1577. dprintk("%s: missing data argument\n", __FUNCTION__);
  1578. goto out_err;
  1579. }
  1580. if (data->version <= 0 || data->version > NFS_MOUNT_VERSION) {
  1581. dprintk("%s: bad mount version\n", __FUNCTION__);
  1582. goto out_err;
  1583. }
  1584. switch (data->version) {
  1585. case 1:
  1586. data->namlen = 0;
  1587. case 2:
  1588. data->bsize = 0;
  1589. case 3:
  1590. if (data->flags & NFS_MOUNT_VER3) {
  1591. dprintk("%s: mount structure version %d does not support NFSv3\n",
  1592. __FUNCTION__,
  1593. data->version);
  1594. goto out_err;
  1595. }
  1596. data->root.size = NFS2_FHSIZE;
  1597. memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
  1598. case 4:
  1599. if (data->flags & NFS_MOUNT_SECFLAVOUR) {
  1600. dprintk("%s: mount structure version %d does not support strong security\n",
  1601. __FUNCTION__,
  1602. data->version);
  1603. goto out_err;
  1604. }
  1605. case 5:
  1606. memset(data->context, 0, sizeof(data->context));
  1607. }
  1608. #ifndef CONFIG_NFS_V3
  1609. /* If NFSv3 is not compiled in, return -EPROTONOSUPPORT */
  1610. s = ERR_PTR(-EPROTONOSUPPORT);
  1611. if (data->flags & NFS_MOUNT_VER3) {
  1612. dprintk("%s: NFSv3 not compiled into kernel\n", __FUNCTION__);
  1613. goto out_err;
  1614. }
  1615. #endif /* CONFIG_NFS_V3 */
  1616. s = ERR_PTR(-ENOMEM);
  1617. server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
  1618. if (!server)
  1619. goto out_err;
  1620. /* Zero out the NFS state stuff */
  1621. init_nfsv4_state(server);
  1622. server->client = server->client_sys = server->client_acl = ERR_PTR(-EINVAL);
  1623. root = &server->fh;
  1624. if (data->flags & NFS_MOUNT_VER3)
  1625. root->size = data->root.size;
  1626. else
  1627. root->size = NFS2_FHSIZE;
  1628. s = ERR_PTR(-EINVAL);
  1629. if (root->size > sizeof(root->data)) {
  1630. dprintk("%s: invalid root filehandle\n", __FUNCTION__);
  1631. goto out_err;
  1632. }
  1633. memcpy(root->data, data->root.data, root->size);
  1634. /* We now require that the mount process passes the remote address */
  1635. memcpy(&server->addr, &data->addr, sizeof(server->addr));
  1636. if (server->addr.sin_addr.s_addr == INADDR_ANY) {
  1637. dprintk("%s: mount program didn't pass remote address!\n",
  1638. __FUNCTION__);
  1639. goto out_err;
  1640. }
  1641. /* Fire up rpciod if not yet running */
  1642. s = ERR_PTR(rpciod_up());
  1643. if (IS_ERR(s)) {
  1644. dprintk("%s: couldn't start rpciod! Error = %ld\n",
  1645. __FUNCTION__, PTR_ERR(s));
  1646. goto out_err;
  1647. }
  1648. s = sget(fs_type, nfs_compare_super, nfs_set_super, server);
  1649. if (IS_ERR(s) || s->s_root)
  1650. goto out_rpciod_down;
  1651. s->s_flags = flags;
  1652. error = nfs_fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  1653. if (error) {
  1654. up_write(&s->s_umount);
  1655. deactivate_super(s);
  1656. return ERR_PTR(error);
  1657. }
  1658. s->s_flags |= MS_ACTIVE;
  1659. return s;
  1660. out_rpciod_down:
  1661. rpciod_down();
  1662. out_err:
  1663. kfree(server);
  1664. return s;
  1665. }
  1666. static void nfs_kill_super(struct super_block *s)
  1667. {
  1668. struct nfs_server *server = NFS_SB(s);
  1669. kill_anon_super(s);
  1670. if (!IS_ERR(server->client))
  1671. rpc_shutdown_client(server->client);
  1672. if (!IS_ERR(server->client_sys))
  1673. rpc_shutdown_client(server->client_sys);
  1674. if (!IS_ERR(server->client_acl))
  1675. rpc_shutdown_client(server->client_acl);
  1676. if (!(server->flags & NFS_MOUNT_NONLM))
  1677. lockd_down(); /* release rpc.lockd */
  1678. rpciod_down(); /* release rpciod */
  1679. nfs_free_iostats(server->io_stats);
  1680. kfree(server->hostname);
  1681. kfree(server);
  1682. nfs_release_automount_timer();
  1683. }
  1684. static struct file_system_type nfs_fs_type = {
  1685. .owner = THIS_MODULE,
  1686. .name = "nfs",
  1687. .get_sb = nfs_get_sb,
  1688. .kill_sb = nfs_kill_super,
  1689. .fs_flags = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
  1690. };
  1691. static struct super_block *nfs_clone_sb(struct nfs_server *server, struct nfs_clone_mount *data)
  1692. {
  1693. struct super_block *sb;
  1694. server->fsid = data->fattr->fsid;
  1695. nfs_copy_fh(&server->fh, data->fh);
  1696. sb = sget(&nfs_fs_type, nfs_compare_super, nfs_set_super, server);
  1697. if (!IS_ERR(sb) && sb->s_root == NULL && !(server->flags & NFS_MOUNT_NONLM))
  1698. lockd_up();
  1699. return sb;
  1700. }
  1701. static struct nfs_server *nfs_clone_server(struct super_block *sb, struct nfs_clone_mount *data)
  1702. {
  1703. struct nfs_server *server = NFS_SB(sb);
  1704. struct nfs_server *parent = NFS_SB(data->sb);
  1705. struct inode *root_inode;
  1706. struct nfs_fsinfo fsinfo;
  1707. void *err = ERR_PTR(-ENOMEM);
  1708. sb->s_op = data->sb->s_op;
  1709. sb->s_blocksize = data->sb->s_blocksize;
  1710. sb->s_blocksize_bits = data->sb->s_blocksize_bits;
  1711. sb->s_maxbytes = data->sb->s_maxbytes;
  1712. server->client_sys = server->client_acl = ERR_PTR(-EINVAL);
  1713. server->io_stats = nfs_alloc_iostats();
  1714. if (server->io_stats == NULL)
  1715. goto out;
  1716. server->client = rpc_clone_client(parent->client);
  1717. if (IS_ERR((err = server->client)))
  1718. goto out;
  1719. if (!IS_ERR(parent->client_sys)) {
  1720. server->client_sys = rpc_clone_client(parent->client_sys);
  1721. if (IS_ERR((err = server->client_sys)))
  1722. goto out;
  1723. }
  1724. if (!IS_ERR(parent->client_acl)) {
  1725. server->client_acl = rpc_clone_client(parent->client_acl);
  1726. if (IS_ERR((err = server->client_acl)))
  1727. goto out;
  1728. }
  1729. root_inode = nfs_fhget(sb, data->fh, data->fattr);
  1730. if (!root_inode)
  1731. goto out;
  1732. sb->s_root = d_alloc_root(root_inode);
  1733. if (!sb->s_root)
  1734. goto out_put_root;
  1735. fsinfo.fattr = data->fattr;
  1736. if (NFS_PROTO(root_inode)->fsinfo(server, data->fh, &fsinfo) == 0)
  1737. nfs_super_set_maxbytes(sb, fsinfo.maxfilesize);
  1738. sb->s_root->d_op = server->rpc_ops->dentry_ops;
  1739. sb->s_flags |= MS_ACTIVE;
  1740. return server;
  1741. out_put_root:
  1742. iput(root_inode);
  1743. out:
  1744. return err;
  1745. }
  1746. static struct super_block *nfs_clone_nfs_sb(struct file_system_type *fs_type,
  1747. int flags, const char *dev_name, void *raw_data)
  1748. {
  1749. struct nfs_clone_mount *data = raw_data;
  1750. return nfs_clone_generic_sb(data, nfs_clone_sb, nfs_clone_server);
  1751. }
  1752. static struct file_system_type clone_nfs_fs_type = {
  1753. .owner = THIS_MODULE,
  1754. .name = "nfs",
  1755. .get_sb = nfs_clone_nfs_sb,
  1756. .kill_sb = nfs_kill_super,
  1757. .fs_flags = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
  1758. };
  1759. #ifdef CONFIG_NFS_V4
  1760. static void nfs4_clear_inode(struct inode *);
  1761. static struct super_operations nfs4_sops = {
  1762. .alloc_inode = nfs_alloc_inode,
  1763. .destroy_inode = nfs_destroy_inode,
  1764. .write_inode = nfs_write_inode,
  1765. .statfs = nfs_statfs,
  1766. .clear_inode = nfs4_clear_inode,
  1767. .umount_begin = nfs_umount_begin,
  1768. .show_options = nfs_show_options,
  1769. .show_stats = nfs_show_stats,
  1770. };
  1771. /*
  1772. * Clean out any remaining NFSv4 state that might be left over due
  1773. * to open() calls that passed nfs_atomic_lookup, but failed to call
  1774. * nfs_open().
  1775. */
  1776. static void nfs4_clear_inode(struct inode *inode)
  1777. {
  1778. struct nfs_inode *nfsi = NFS_I(inode);
  1779. /* If we are holding a delegation, return it! */
  1780. nfs_inode_return_delegation(inode);
  1781. /* First call standard NFS clear_inode() code */
  1782. nfs_clear_inode(inode);
  1783. /* Now clear out any remaining state */
  1784. while (!list_empty(&nfsi->open_states)) {
  1785. struct nfs4_state *state;
  1786. state = list_entry(nfsi->open_states.next,
  1787. struct nfs4_state,
  1788. inode_states);
  1789. dprintk("%s(%s/%Ld): found unclaimed NFSv4 state %p\n",
  1790. __FUNCTION__,
  1791. inode->i_sb->s_id,
  1792. (long long)NFS_FILEID(inode),
  1793. state);
  1794. BUG_ON(atomic_read(&state->count) != 1);
  1795. nfs4_close_state(state, state->state);
  1796. }
  1797. }
  1798. static struct rpc_clnt *nfs4_create_client(struct nfs_server *server,
  1799. struct rpc_timeout *timeparms, int proto, rpc_authflavor_t flavor)
  1800. {
  1801. struct nfs4_client *clp;
  1802. struct rpc_xprt *xprt = NULL;
  1803. struct rpc_clnt *clnt = NULL;
  1804. int err = -EIO;
  1805. clp = nfs4_get_client(&server->addr.sin_addr);
  1806. if (!clp) {
  1807. dprintk("%s: failed to create NFS4 client.\n", __FUNCTION__);
  1808. return ERR_PTR(err);
  1809. }
  1810. /* Now create transport and client */
  1811. down_write(&clp->cl_sem);
  1812. if (IS_ERR(clp->cl_rpcclient)) {
  1813. xprt = xprt_create_proto(proto, &server->addr, timeparms);
  1814. if (IS_ERR(xprt)) {
  1815. up_write(&clp->cl_sem);
  1816. err = PTR_ERR(xprt);
  1817. dprintk("%s: cannot create RPC transport. Error = %d\n",
  1818. __FUNCTION__, err);
  1819. goto out_fail;
  1820. }
  1821. /* Bind to a reserved port! */
  1822. xprt->resvport = 1;
  1823. clnt = rpc_create_client(xprt, server->hostname, &nfs_program,
  1824. server->rpc_ops->version, flavor);
  1825. if (IS_ERR(clnt)) {
  1826. up_write(&clp->cl_sem);
  1827. err = PTR_ERR(clnt);
  1828. dprintk("%s: cannot create RPC client. Error = %d\n",
  1829. __FUNCTION__, err);
  1830. goto out_fail;
  1831. }
  1832. clnt->cl_intr = 1;
  1833. clnt->cl_softrtry = 1;
  1834. clp->cl_rpcclient = clnt;
  1835. memcpy(clp->cl_ipaddr, server->ip_addr, sizeof(clp->cl_ipaddr));
  1836. nfs_idmap_new(clp);
  1837. }
  1838. list_add_tail(&server->nfs4_siblings, &clp->cl_superblocks);
  1839. clnt = rpc_clone_client(clp->cl_rpcclient);
  1840. if (!IS_ERR(clnt))
  1841. server->nfs4_state = clp;
  1842. up_write(&clp->cl_sem);
  1843. clp = NULL;
  1844. if (IS_ERR(clnt)) {
  1845. dprintk("%s: cannot create RPC client. Error = %d\n",
  1846. __FUNCTION__, err);
  1847. return clnt;
  1848. }
  1849. if (server->nfs4_state->cl_idmap == NULL) {
  1850. dprintk("%s: failed to create idmapper.\n", __FUNCTION__);
  1851. return ERR_PTR(-ENOMEM);
  1852. }
  1853. if (clnt->cl_auth->au_flavor != flavor) {
  1854. struct rpc_auth *auth;
  1855. auth = rpcauth_create(flavor, clnt);
  1856. if (IS_ERR(auth)) {
  1857. dprintk("%s: couldn't create credcache!\n", __FUNCTION__);
  1858. return (struct rpc_clnt *)auth;
  1859. }
  1860. }
  1861. return clnt;
  1862. out_fail:
  1863. if (clp)
  1864. nfs4_put_client(clp);
  1865. return ERR_PTR(err);
  1866. }
  1867. static int nfs4_fill_super(struct super_block *sb, struct nfs4_mount_data *data, int silent)
  1868. {
  1869. struct nfs_server *server;
  1870. struct rpc_timeout timeparms;
  1871. rpc_authflavor_t authflavour;
  1872. int err = -EIO;
  1873. sb->s_blocksize_bits = 0;
  1874. sb->s_blocksize = 0;
  1875. server = NFS_SB(sb);
  1876. if (data->rsize != 0)
  1877. server->rsize = nfs_block_size(data->rsize, NULL);
  1878. if (data->wsize != 0)
  1879. server->wsize = nfs_block_size(data->wsize, NULL);
  1880. server->flags = data->flags & NFS_MOUNT_FLAGMASK;
  1881. server->caps = NFS_CAP_ATOMIC_OPEN;
  1882. server->acregmin = data->acregmin*HZ;
  1883. server->acregmax = data->acregmax*HZ;
  1884. server->acdirmin = data->acdirmin*HZ;
  1885. server->acdirmax = data->acdirmax*HZ;
  1886. server->rpc_ops = &nfs_v4_clientops;
  1887. nfs_init_timeout_values(&timeparms, data->proto, data->timeo, data->retrans);
  1888. server->retrans_timeo = timeparms.to_initval;
  1889. server->retrans_count = timeparms.to_retries;
  1890. /* Now create transport and client */
  1891. authflavour = RPC_AUTH_UNIX;
  1892. if (data->auth_flavourlen != 0) {
  1893. if (data->auth_flavourlen != 1) {
  1894. dprintk("%s: Invalid number of RPC auth flavours %d.\n",
  1895. __FUNCTION__, data->auth_flavourlen);
  1896. err = -EINVAL;
  1897. goto out_fail;
  1898. }
  1899. if (copy_from_user(&authflavour, data->auth_flavours, sizeof(authflavour))) {
  1900. err = -EFAULT;
  1901. goto out_fail;
  1902. }
  1903. }
  1904. server->client = nfs4_create_client(server, &timeparms, data->proto, authflavour);
  1905. if (IS_ERR(server->client)) {
  1906. err = PTR_ERR(server->client);
  1907. dprintk("%s: cannot create RPC client. Error = %d\n",
  1908. __FUNCTION__, err);
  1909. goto out_fail;
  1910. }
  1911. sb->s_time_gran = 1;
  1912. sb->s_op = &nfs4_sops;
  1913. err = nfs_sb_init(sb, authflavour);
  1914. out_fail:
  1915. return err;
  1916. }
  1917. static int nfs4_compare_super(struct super_block *sb, void *data)
  1918. {
  1919. struct nfs_server *server = data;
  1920. struct nfs_server *old = NFS_SB(sb);
  1921. if (strcmp(server->hostname, old->hostname) != 0)
  1922. return 0;
  1923. if (strcmp(server->mnt_path, old->mnt_path) != 0)
  1924. return 0;
  1925. return 1;
  1926. }
  1927. static void *
  1928. nfs_copy_user_string(char *dst, struct nfs_string *src, int maxlen)
  1929. {
  1930. void *p = NULL;
  1931. if (!src->len)
  1932. return ERR_PTR(-EINVAL);
  1933. if (src->len < maxlen)
  1934. maxlen = src->len;
  1935. if (dst == NULL) {
  1936. p = dst = kmalloc(maxlen + 1, GFP_KERNEL);
  1937. if (p == NULL)
  1938. return ERR_PTR(-ENOMEM);
  1939. }
  1940. if (copy_from_user(dst, src->data, maxlen)) {
  1941. kfree(p);
  1942. return ERR_PTR(-EFAULT);
  1943. }
  1944. dst[maxlen] = '\0';
  1945. return dst;
  1946. }
  1947. static struct super_block *nfs4_get_sb(struct file_system_type *fs_type,
  1948. int flags, const char *dev_name, void *raw_data)
  1949. {
  1950. int error;
  1951. struct nfs_server *server;
  1952. struct super_block *s;
  1953. struct nfs4_mount_data *data = raw_data;
  1954. void *p;
  1955. if (data == NULL) {
  1956. dprintk("%s: missing data argument\n", __FUNCTION__);
  1957. return ERR_PTR(-EINVAL);
  1958. }
  1959. if (data->version <= 0 || data->version > NFS4_MOUNT_VERSION) {
  1960. dprintk("%s: bad mount version\n", __FUNCTION__);
  1961. return ERR_PTR(-EINVAL);
  1962. }
  1963. server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
  1964. if (!server)
  1965. return ERR_PTR(-ENOMEM);
  1966. /* Zero out the NFS state stuff */
  1967. init_nfsv4_state(server);
  1968. server->client = server->client_sys = server->client_acl = ERR_PTR(-EINVAL);
  1969. p = nfs_copy_user_string(NULL, &data->hostname, 256);
  1970. if (IS_ERR(p))
  1971. goto out_err;
  1972. server->hostname = p;
  1973. p = nfs_copy_user_string(NULL, &data->mnt_path, 1024);
  1974. if (IS_ERR(p))
  1975. goto out_err;
  1976. server->mnt_path = p;
  1977. p = nfs_copy_user_string(server->ip_addr, &data->client_addr,
  1978. sizeof(server->ip_addr) - 1);
  1979. if (IS_ERR(p))
  1980. goto out_err;
  1981. /* We now require that the mount process passes the remote address */
  1982. if (data->host_addrlen != sizeof(server->addr)) {
  1983. s = ERR_PTR(-EINVAL);
  1984. goto out_free;
  1985. }
  1986. if (copy_from_user(&server->addr, data->host_addr, sizeof(server->addr))) {
  1987. s = ERR_PTR(-EFAULT);
  1988. goto out_free;
  1989. }
  1990. if (server->addr.sin_family != AF_INET ||
  1991. server->addr.sin_addr.s_addr == INADDR_ANY) {
  1992. dprintk("%s: mount program didn't pass remote IP address!\n",
  1993. __FUNCTION__);
  1994. s = ERR_PTR(-EINVAL);
  1995. goto out_free;
  1996. }
  1997. /* Fire up rpciod if not yet running */
  1998. s = ERR_PTR(rpciod_up());
  1999. if (IS_ERR(s)) {
  2000. dprintk("%s: couldn't start rpciod! Error = %ld\n",
  2001. __FUNCTION__, PTR_ERR(s));
  2002. goto out_free;
  2003. }
  2004. s = sget(fs_type, nfs4_compare_super, nfs_set_super, server);
  2005. if (IS_ERR(s) || s->s_root)
  2006. goto out_free;
  2007. s->s_flags = flags;
  2008. error = nfs4_fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  2009. if (error) {
  2010. up_write(&s->s_umount);
  2011. deactivate_super(s);
  2012. return ERR_PTR(error);
  2013. }
  2014. s->s_flags |= MS_ACTIVE;
  2015. return s;
  2016. out_err:
  2017. s = (struct super_block *)p;
  2018. out_free:
  2019. kfree(server->mnt_path);
  2020. kfree(server->hostname);
  2021. kfree(server);
  2022. return s;
  2023. }
  2024. static void nfs4_kill_super(struct super_block *sb)
  2025. {
  2026. struct nfs_server *server = NFS_SB(sb);
  2027. nfs_return_all_delegations(sb);
  2028. kill_anon_super(sb);
  2029. nfs4_renewd_prepare_shutdown(server);
  2030. if (server->client != NULL && !IS_ERR(server->client))
  2031. rpc_shutdown_client(server->client);
  2032. destroy_nfsv4_state(server);
  2033. rpciod_down();
  2034. nfs_free_iostats(server->io_stats);
  2035. kfree(server->hostname);
  2036. kfree(server);
  2037. nfs_release_automount_timer();
  2038. }
  2039. static struct file_system_type nfs4_fs_type = {
  2040. .owner = THIS_MODULE,
  2041. .name = "nfs4",
  2042. .get_sb = nfs4_get_sb,
  2043. .kill_sb = nfs4_kill_super,
  2044. .fs_flags = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
  2045. };
  2046. static const int nfs_set_port_min = 0;
  2047. static const int nfs_set_port_max = 65535;
  2048. static int param_set_port(const char *val, struct kernel_param *kp)
  2049. {
  2050. char *endp;
  2051. int num = simple_strtol(val, &endp, 0);
  2052. if (endp == val || *endp || num < nfs_set_port_min || num > nfs_set_port_max)
  2053. return -EINVAL;
  2054. *((int *)kp->arg) = num;
  2055. return 0;
  2056. }
  2057. module_param_call(callback_tcpport, param_set_port, param_get_int,
  2058. &nfs_callback_set_tcpport, 0644);
  2059. static int param_set_idmap_timeout(const char *val, struct kernel_param *kp)
  2060. {
  2061. char *endp;
  2062. int num = simple_strtol(val, &endp, 0);
  2063. int jif = num * HZ;
  2064. if (endp == val || *endp || num < 0 || jif < num)
  2065. return -EINVAL;
  2066. *((int *)kp->arg) = jif;
  2067. return 0;
  2068. }
  2069. module_param_call(idmap_cache_timeout, param_set_idmap_timeout, param_get_int,
  2070. &nfs_idmap_cache_timeout, 0644);
  2071. /* Constructs the SERVER-side path */
  2072. static inline char *nfs4_path(const struct dentry *dentry, char *buffer, ssize_t buflen)
  2073. {
  2074. return nfs_path(NFS_SB(dentry->d_sb)->mnt_path, dentry, buffer, buflen);
  2075. }
  2076. static inline char *nfs4_dup_path(const struct dentry *dentry)
  2077. {
  2078. char *page = (char *) __get_free_page(GFP_USER);
  2079. char *path;
  2080. path = nfs4_path(dentry, page, PAGE_SIZE);
  2081. if (!IS_ERR(path)) {
  2082. int len = PAGE_SIZE + page - path;
  2083. char *tmp = path;
  2084. path = kmalloc(len, GFP_KERNEL);
  2085. if (path)
  2086. memcpy(path, tmp, len);
  2087. else
  2088. path = ERR_PTR(-ENOMEM);
  2089. }
  2090. free_page((unsigned long)page);
  2091. return path;
  2092. }
  2093. static struct super_block *nfs4_clone_sb(struct nfs_server *server, struct nfs_clone_mount *data)
  2094. {
  2095. const struct dentry *dentry = data->dentry;
  2096. struct nfs4_client *clp = server->nfs4_state;
  2097. struct super_block *sb;
  2098. server->fsid = data->fattr->fsid;
  2099. nfs_copy_fh(&server->fh, data->fh);
  2100. server->mnt_path = nfs4_dup_path(dentry);
  2101. if (IS_ERR(server->mnt_path)) {
  2102. sb = (struct super_block *)server->mnt_path;
  2103. goto err;
  2104. }
  2105. sb = sget(&nfs4_fs_type, nfs4_compare_super, nfs_set_super, server);
  2106. if (IS_ERR(sb) || sb->s_root)
  2107. goto free_path;
  2108. nfs4_server_capabilities(server, &server->fh);
  2109. down_write(&clp->cl_sem);
  2110. atomic_inc(&clp->cl_count);
  2111. list_add_tail(&server->nfs4_siblings, &clp->cl_superblocks);
  2112. up_write(&clp->cl_sem);
  2113. return sb;
  2114. free_path:
  2115. kfree(server->mnt_path);
  2116. err:
  2117. server->mnt_path = NULL;
  2118. return sb;
  2119. }
  2120. static struct super_block *nfs_clone_nfs4_sb(struct file_system_type *fs_type,
  2121. int flags, const char *dev_name, void *raw_data)
  2122. {
  2123. struct nfs_clone_mount *data = raw_data;
  2124. return nfs_clone_generic_sb(data, nfs4_clone_sb, nfs_clone_server);
  2125. }
  2126. static struct file_system_type clone_nfs4_fs_type = {
  2127. .owner = THIS_MODULE,
  2128. .name = "nfs4",
  2129. .get_sb = nfs_clone_nfs4_sb,
  2130. .kill_sb = nfs4_kill_super,
  2131. .fs_flags = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
  2132. };
  2133. static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server, char *devname, struct nfs_clone_mount *mountdata)
  2134. {
  2135. struct vfsmount *mnt = NULL;
  2136. switch (server->rpc_ops->version) {
  2137. case 2:
  2138. case 3:
  2139. mnt = vfs_kern_mount(&clone_nfs_fs_type, 0, devname, &mountdata);
  2140. break;
  2141. case 4:
  2142. mnt = vfs_kern_mount(&clone_nfs4_fs_type, 0, devname, mountdata);
  2143. }
  2144. return mnt;
  2145. }
  2146. #define nfs4_init_once(nfsi) \
  2147. do { \
  2148. INIT_LIST_HEAD(&(nfsi)->open_states); \
  2149. nfsi->delegation = NULL; \
  2150. nfsi->delegation_state = 0; \
  2151. init_rwsem(&nfsi->rwsem); \
  2152. } while(0)
  2153. static inline int register_nfs4fs(void)
  2154. {
  2155. int ret;
  2156. ret = nfs_register_sysctl();
  2157. if (ret != 0)
  2158. return ret;
  2159. ret = register_filesystem(&nfs4_fs_type);
  2160. if (ret != 0)
  2161. nfs_unregister_sysctl();
  2162. return ret;
  2163. }
  2164. static inline void unregister_nfs4fs(void)
  2165. {
  2166. unregister_filesystem(&nfs4_fs_type);
  2167. nfs_unregister_sysctl();
  2168. }
  2169. #else
  2170. #define nfs4_fill_sb(a,b) ERR_PTR(-EINVAL)
  2171. #define nfs4_fill_super(a,b) ERR_PTR(-EINVAL)
  2172. #define nfs4_init_once(nfsi) \
  2173. do { } while (0)
  2174. #define register_nfs4fs() (0)
  2175. #define unregister_nfs4fs()
  2176. static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server, char *devname, struct nfs_clone_mount *mountdata)
  2177. {
  2178. return vfs_kern_mount(&clone_nfs_fs_type, 0, devname, &mountdata);
  2179. }
  2180. #endif
  2181. static inline char *nfs_devname(const struct vfsmount *mnt_parent,
  2182. const struct dentry *dentry,
  2183. char *buffer, ssize_t buflen)
  2184. {
  2185. return nfs_path(mnt_parent->mnt_devname, dentry, buffer, buflen);
  2186. }
  2187. /**
  2188. * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
  2189. * @mnt_parent - mountpoint of parent directory
  2190. * @dentry - parent directory
  2191. * @fh - filehandle for new root dentry
  2192. * @fattr - attributes for new root inode
  2193. *
  2194. */
  2195. struct vfsmount *nfs_do_submount(const struct vfsmount *mnt_parent,
  2196. const struct dentry *dentry, struct nfs_fh *fh,
  2197. struct nfs_fattr *fattr)
  2198. {
  2199. struct nfs_clone_mount mountdata = {
  2200. .sb = mnt_parent->mnt_sb,
  2201. .dentry = dentry,
  2202. .fh = fh,
  2203. .fattr = fattr,
  2204. };
  2205. struct vfsmount *mnt = ERR_PTR(-ENOMEM);
  2206. char *page = (char *) __get_free_page(GFP_USER);
  2207. char *devname;
  2208. dprintk("%s: submounting on %s/%s\n", __FUNCTION__,
  2209. dentry->d_parent->d_name.name,
  2210. dentry->d_name.name);
  2211. if (page == NULL)
  2212. goto out;
  2213. devname = nfs_devname(mnt_parent, dentry, page, PAGE_SIZE);
  2214. mnt = (struct vfsmount *)devname;
  2215. if (IS_ERR(devname))
  2216. goto free_page;
  2217. mnt = nfs_do_clone_mount(NFS_SB(mnt_parent->mnt_sb), devname, &mountdata);
  2218. free_page:
  2219. free_page((unsigned long)page);
  2220. out:
  2221. dprintk("%s: done\n", __FUNCTION__);
  2222. return mnt;
  2223. }
  2224. #ifdef CONFIG_NFS_V4
  2225. /* Check if fs_root is valid */
  2226. static inline char *nfs4_pathname_string(struct nfs4_pathname *pathname, char *buffer, ssize_t buflen)
  2227. {
  2228. char *end = buffer + buflen;
  2229. int n;
  2230. *--end = '\0';
  2231. buflen--;
  2232. n = pathname->ncomponents;
  2233. while (--n >= 0) {
  2234. struct nfs4_string *component = &pathname->components[n];
  2235. buflen -= component->len + 1;
  2236. if (buflen < 0)
  2237. goto Elong;
  2238. end -= component->len;
  2239. memcpy(end, component->data, component->len);
  2240. *--end = '/';
  2241. }
  2242. return end;
  2243. Elong:
  2244. return ERR_PTR(-ENAMETOOLONG);
  2245. }
  2246. /* Check if the string represents a "valid" IPv4 address */
  2247. static inline int valid_ipaddr4(const char *buf)
  2248. {
  2249. int rc, count, in[4];
  2250. rc = sscanf(buf, "%d.%d.%d.%d", &in[0], &in[1], &in[2], &in[3]);
  2251. if (rc != 4)
  2252. return -EINVAL;
  2253. for (count = 0; count < 4; count++) {
  2254. if (in[count] > 255)
  2255. return -EINVAL;
  2256. }
  2257. return 0;
  2258. }
  2259. static struct super_block *nfs4_referral_sb(struct nfs_server *server, struct nfs_clone_mount *data)
  2260. {
  2261. struct super_block *sb = ERR_PTR(-ENOMEM);
  2262. int len;
  2263. len = strlen(data->mnt_path) + 1;
  2264. server->mnt_path = kmalloc(len, GFP_KERNEL);
  2265. if (server->mnt_path == NULL)
  2266. goto err;
  2267. memcpy(server->mnt_path, data->mnt_path, len);
  2268. memcpy(&server->addr, data->addr, sizeof(struct sockaddr_in));
  2269. sb = sget(&nfs4_fs_type, nfs4_compare_super, nfs_set_super, server);
  2270. if (IS_ERR(sb) || sb->s_root)
  2271. goto free_path;
  2272. return sb;
  2273. free_path:
  2274. kfree(server->mnt_path);
  2275. err:
  2276. server->mnt_path = NULL;
  2277. return sb;
  2278. }
  2279. static struct nfs_server *nfs4_referral_server(struct super_block *sb, struct nfs_clone_mount *data)
  2280. {
  2281. struct nfs_server *server = NFS_SB(sb);
  2282. struct rpc_timeout timeparms;
  2283. int proto, timeo, retrans;
  2284. void *err;
  2285. proto = IPPROTO_TCP;
  2286. /* Since we are following a referral and there may be alternatives,
  2287. set the timeouts and retries to low values */
  2288. timeo = 2;
  2289. retrans = 1;
  2290. nfs_init_timeout_values(&timeparms, proto, timeo, retrans);
  2291. server->client = nfs4_create_client(server, &timeparms, proto, data->authflavor);
  2292. if (IS_ERR((err = server->client)))
  2293. goto out_err;
  2294. sb->s_time_gran = 1;
  2295. sb->s_op = &nfs4_sops;
  2296. err = ERR_PTR(nfs_sb_init(sb, data->authflavor));
  2297. if (!IS_ERR(err))
  2298. return server;
  2299. out_err:
  2300. return (struct nfs_server *)err;
  2301. }
  2302. static struct super_block *nfs_referral_nfs4_sb(struct file_system_type *fs_type,
  2303. int flags, const char *dev_name, void *raw_data)
  2304. {
  2305. struct nfs_clone_mount *data = raw_data;
  2306. return nfs_clone_generic_sb(data, nfs4_referral_sb, nfs4_referral_server);
  2307. }
  2308. static struct file_system_type nfs_referral_nfs4_fs_type = {
  2309. .owner = THIS_MODULE,
  2310. .name = "nfs4",
  2311. .get_sb = nfs_referral_nfs4_sb,
  2312. .kill_sb = nfs4_kill_super,
  2313. .fs_flags = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
  2314. };
  2315. /**
  2316. * nfs_follow_referral - set up mountpoint when hitting a referral on moved error
  2317. * @mnt_parent - mountpoint of parent directory
  2318. * @dentry - parent directory
  2319. * @fspath - fs path returned in fs_locations
  2320. * @mntpath - mount path to new server
  2321. * @hostname - hostname of new server
  2322. * @addr - host addr of new server
  2323. *
  2324. */
  2325. struct vfsmount *nfs_follow_referral(const struct vfsmount *mnt_parent,
  2326. const struct dentry *dentry, struct nfs4_fs_locations *locations)
  2327. {
  2328. struct vfsmount *mnt = ERR_PTR(-ENOENT);
  2329. struct nfs_clone_mount mountdata = {
  2330. .sb = mnt_parent->mnt_sb,
  2331. .dentry = dentry,
  2332. .authflavor = NFS_SB(mnt_parent->mnt_sb)->client->cl_auth->au_flavor,
  2333. };
  2334. char *page, *page2;
  2335. char *path, *fs_path;
  2336. char *devname;
  2337. int loc, s;
  2338. if (locations == NULL || locations->nlocations <= 0)
  2339. goto out;
  2340. dprintk("%s: referral at %s/%s\n", __FUNCTION__,
  2341. dentry->d_parent->d_name.name, dentry->d_name.name);
  2342. /* Ensure fs path is a prefix of current dentry path */
  2343. page = (char *) __get_free_page(GFP_USER);
  2344. if (page == NULL)
  2345. goto out;
  2346. page2 = (char *) __get_free_page(GFP_USER);
  2347. if (page2 == NULL)
  2348. goto out;
  2349. path = nfs4_path(dentry, page, PAGE_SIZE);
  2350. if (IS_ERR(path))
  2351. goto out_free;
  2352. fs_path = nfs4_pathname_string(&locations->fs_path, page2, PAGE_SIZE);
  2353. if (IS_ERR(fs_path))
  2354. goto out_free;
  2355. if (strncmp(path, fs_path, strlen(fs_path)) != 0) {
  2356. dprintk("%s: path %s does not begin with fsroot %s\n", __FUNCTION__, path, fs_path);
  2357. goto out_free;
  2358. }
  2359. devname = nfs_devname(mnt_parent, dentry, page, PAGE_SIZE);
  2360. if (IS_ERR(devname)) {
  2361. mnt = (struct vfsmount *)devname;
  2362. goto out_free;
  2363. }
  2364. loc = 0;
  2365. while (loc < locations->nlocations && IS_ERR(mnt)) {
  2366. struct nfs4_fs_location *location = &locations->locations[loc];
  2367. char *mnt_path;
  2368. if (location == NULL || location->nservers <= 0 ||
  2369. location->rootpath.ncomponents == 0) {
  2370. loc++;
  2371. continue;
  2372. }
  2373. mnt_path = nfs4_pathname_string(&location->rootpath, page2, PAGE_SIZE);
  2374. if (IS_ERR(mnt_path)) {
  2375. loc++;
  2376. continue;
  2377. }
  2378. mountdata.mnt_path = mnt_path;
  2379. s = 0;
  2380. while (s < location->nservers) {
  2381. struct sockaddr_in addr = {};
  2382. if (location->servers[s].len <= 0 ||
  2383. valid_ipaddr4(location->servers[s].data) < 0) {
  2384. s++;
  2385. continue;
  2386. }
  2387. mountdata.hostname = location->servers[s].data;
  2388. addr.sin_addr.s_addr = in_aton(mountdata.hostname);
  2389. addr.sin_family = AF_INET;
  2390. addr.sin_port = htons(NFS_PORT);
  2391. mountdata.addr = &addr;
  2392. mnt = vfs_kern_mount(&nfs_referral_nfs4_fs_type, 0, devname, &mountdata);
  2393. if (!IS_ERR(mnt)) {
  2394. break;
  2395. }
  2396. s++;
  2397. }
  2398. loc++;
  2399. }
  2400. out_free:
  2401. free_page((unsigned long)page);
  2402. free_page((unsigned long)page2);
  2403. out:
  2404. dprintk("%s: done\n", __FUNCTION__);
  2405. return mnt;
  2406. }
  2407. /*
  2408. * nfs_do_refmount - handle crossing a referral on server
  2409. * @dentry - dentry of referral
  2410. * @nd - nameidata info
  2411. *
  2412. */
  2413. struct vfsmount *nfs_do_refmount(const struct vfsmount *mnt_parent, struct dentry *dentry)
  2414. {
  2415. struct vfsmount *mnt = ERR_PTR(-ENOENT);
  2416. struct dentry *parent;
  2417. struct nfs4_fs_locations *fs_locations = NULL;
  2418. struct page *page;
  2419. int err;
  2420. /* BUG_ON(IS_ROOT(dentry)); */
  2421. dprintk("%s: enter\n", __FUNCTION__);
  2422. page = alloc_page(GFP_KERNEL);
  2423. if (page == NULL)
  2424. goto out;
  2425. fs_locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
  2426. if (fs_locations == NULL)
  2427. goto out_free;
  2428. /* Get locations */
  2429. parent = dget_parent(dentry);
  2430. dprintk("%s: getting locations for %s/%s\n", __FUNCTION__, parent->d_name.name, dentry->d_name.name);
  2431. err = nfs4_proc_fs_locations(parent->d_inode, dentry, fs_locations, page);
  2432. dput(parent);
  2433. if (err != 0 || fs_locations->nlocations <= 0 ||
  2434. fs_locations->fs_path.ncomponents <= 0)
  2435. goto out_free;
  2436. mnt = nfs_follow_referral(mnt_parent, dentry, fs_locations);
  2437. out_free:
  2438. __free_page(page);
  2439. kfree(fs_locations);
  2440. out:
  2441. dprintk("%s: done\n", __FUNCTION__);
  2442. return mnt;
  2443. }
  2444. #else
  2445. struct vfsmount *nfs_do_refmount(const struct vfsmount *mnt_parent, struct dentry *dentry)
  2446. {
  2447. return ERR_PTR(-ENOENT);
  2448. }
  2449. #endif
  2450. extern int nfs_init_nfspagecache(void);
  2451. extern void nfs_destroy_nfspagecache(void);
  2452. extern int nfs_init_readpagecache(void);
  2453. extern void nfs_destroy_readpagecache(void);
  2454. extern int nfs_init_writepagecache(void);
  2455. extern void nfs_destroy_writepagecache(void);
  2456. #ifdef CONFIG_NFS_DIRECTIO
  2457. extern int nfs_init_directcache(void);
  2458. extern void nfs_destroy_directcache(void);
  2459. #endif
  2460. static kmem_cache_t * nfs_inode_cachep;
  2461. static struct inode *nfs_alloc_inode(struct super_block *sb)
  2462. {
  2463. struct nfs_inode *nfsi;
  2464. nfsi = (struct nfs_inode *)kmem_cache_alloc(nfs_inode_cachep, SLAB_KERNEL);
  2465. if (!nfsi)
  2466. return NULL;
  2467. nfsi->flags = 0UL;
  2468. nfsi->cache_validity = 0UL;
  2469. nfsi->cache_change_attribute = jiffies;
  2470. #ifdef CONFIG_NFS_V3_ACL
  2471. nfsi->acl_access = ERR_PTR(-EAGAIN);
  2472. nfsi->acl_default = ERR_PTR(-EAGAIN);
  2473. #endif
  2474. #ifdef CONFIG_NFS_V4
  2475. nfsi->nfs4_acl = NULL;
  2476. #endif /* CONFIG_NFS_V4 */
  2477. return &nfsi->vfs_inode;
  2478. }
  2479. static void nfs_destroy_inode(struct inode *inode)
  2480. {
  2481. kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
  2482. }
  2483. static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
  2484. {
  2485. struct nfs_inode *nfsi = (struct nfs_inode *) foo;
  2486. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  2487. SLAB_CTOR_CONSTRUCTOR) {
  2488. inode_init_once(&nfsi->vfs_inode);
  2489. spin_lock_init(&nfsi->req_lock);
  2490. INIT_LIST_HEAD(&nfsi->dirty);
  2491. INIT_LIST_HEAD(&nfsi->commit);
  2492. INIT_LIST_HEAD(&nfsi->open_files);
  2493. INIT_RADIX_TREE(&nfsi->nfs_page_tree, GFP_ATOMIC);
  2494. atomic_set(&nfsi->data_updates, 0);
  2495. nfsi->ndirty = 0;
  2496. nfsi->ncommit = 0;
  2497. nfsi->npages = 0;
  2498. nfs4_init_once(nfsi);
  2499. }
  2500. }
  2501. static int nfs_init_inodecache(void)
  2502. {
  2503. nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
  2504. sizeof(struct nfs_inode),
  2505. 0, (SLAB_RECLAIM_ACCOUNT|
  2506. SLAB_MEM_SPREAD),
  2507. init_once, NULL);
  2508. if (nfs_inode_cachep == NULL)
  2509. return -ENOMEM;
  2510. return 0;
  2511. }
  2512. static void nfs_destroy_inodecache(void)
  2513. {
  2514. if (kmem_cache_destroy(nfs_inode_cachep))
  2515. printk(KERN_INFO "nfs_inode_cache: not all structures were freed\n");
  2516. }
  2517. /*
  2518. * Initialize NFS
  2519. */
  2520. static int __init init_nfs_fs(void)
  2521. {
  2522. int err;
  2523. err = nfs_init_nfspagecache();
  2524. if (err)
  2525. goto out4;
  2526. err = nfs_init_inodecache();
  2527. if (err)
  2528. goto out3;
  2529. err = nfs_init_readpagecache();
  2530. if (err)
  2531. goto out2;
  2532. err = nfs_init_writepagecache();
  2533. if (err)
  2534. goto out1;
  2535. #ifdef CONFIG_NFS_DIRECTIO
  2536. err = nfs_init_directcache();
  2537. if (err)
  2538. goto out0;
  2539. #endif
  2540. #ifdef CONFIG_PROC_FS
  2541. rpc_proc_register(&nfs_rpcstat);
  2542. #endif
  2543. err = register_filesystem(&nfs_fs_type);
  2544. if (err)
  2545. goto out;
  2546. if ((err = register_nfs4fs()) != 0)
  2547. goto out;
  2548. return 0;
  2549. out:
  2550. #ifdef CONFIG_PROC_FS
  2551. rpc_proc_unregister("nfs");
  2552. #endif
  2553. #ifdef CONFIG_NFS_DIRECTIO
  2554. nfs_destroy_directcache();
  2555. out0:
  2556. #endif
  2557. nfs_destroy_writepagecache();
  2558. out1:
  2559. nfs_destroy_readpagecache();
  2560. out2:
  2561. nfs_destroy_inodecache();
  2562. out3:
  2563. nfs_destroy_nfspagecache();
  2564. out4:
  2565. return err;
  2566. }
  2567. static void __exit exit_nfs_fs(void)
  2568. {
  2569. #ifdef CONFIG_NFS_DIRECTIO
  2570. nfs_destroy_directcache();
  2571. #endif
  2572. nfs_destroy_writepagecache();
  2573. nfs_destroy_readpagecache();
  2574. nfs_destroy_inodecache();
  2575. nfs_destroy_nfspagecache();
  2576. #ifdef CONFIG_PROC_FS
  2577. rpc_proc_unregister("nfs");
  2578. #endif
  2579. unregister_filesystem(&nfs_fs_type);
  2580. unregister_nfs4fs();
  2581. }
  2582. /* Not quite true; I just maintain it */
  2583. MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
  2584. MODULE_LICENSE("GPL");
  2585. module_init(init_nfs_fs)
  2586. module_exit(exit_nfs_fs)