inode.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086
  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/nfs_fs.h>
  28. #include <linux/nfs_mount.h>
  29. #include <linux/nfs4_mount.h>
  30. #include <linux/lockd/bind.h>
  31. #include <linux/smp_lock.h>
  32. #include <linux/seq_file.h>
  33. #include <linux/mount.h>
  34. #include <linux/nfs_idmap.h>
  35. #include <linux/vfs.h>
  36. #include <asm/system.h>
  37. #include <asm/uaccess.h>
  38. #include "nfs4_fs.h"
  39. #include "delegation.h"
  40. #define NFSDBG_FACILITY NFSDBG_VFS
  41. #define NFS_PARANOIA 1
  42. /* Maximum number of readahead requests
  43. * FIXME: this should really be a sysctl so that users may tune it to suit
  44. * their needs. People that do NFS over a slow network, might for
  45. * instance want to reduce it to something closer to 1 for improved
  46. * interactive response.
  47. */
  48. #define NFS_MAX_READAHEAD (RPC_DEF_SLOT_TABLE - 1)
  49. static void nfs_invalidate_inode(struct inode *);
  50. static int nfs_update_inode(struct inode *, struct nfs_fattr *, unsigned long);
  51. static struct inode *nfs_alloc_inode(struct super_block *sb);
  52. static void nfs_destroy_inode(struct inode *);
  53. static int nfs_write_inode(struct inode *,int);
  54. static void nfs_delete_inode(struct inode *);
  55. static void nfs_clear_inode(struct inode *);
  56. static void nfs_umount_begin(struct super_block *);
  57. static int nfs_statfs(struct super_block *, struct kstatfs *);
  58. static int nfs_show_options(struct seq_file *, struct vfsmount *);
  59. static void nfs_zap_acl_cache(struct inode *);
  60. static struct rpc_program nfs_program;
  61. static struct super_operations nfs_sops = {
  62. .alloc_inode = nfs_alloc_inode,
  63. .destroy_inode = nfs_destroy_inode,
  64. .write_inode = nfs_write_inode,
  65. .delete_inode = nfs_delete_inode,
  66. .statfs = nfs_statfs,
  67. .clear_inode = nfs_clear_inode,
  68. .umount_begin = nfs_umount_begin,
  69. .show_options = nfs_show_options,
  70. };
  71. /*
  72. * RPC cruft for NFS
  73. */
  74. static struct rpc_stat nfs_rpcstat = {
  75. .program = &nfs_program
  76. };
  77. static struct rpc_version * nfs_version[] = {
  78. NULL,
  79. NULL,
  80. &nfs_version2,
  81. #if defined(CONFIG_NFS_V3)
  82. &nfs_version3,
  83. #elif defined(CONFIG_NFS_V4)
  84. NULL,
  85. #endif
  86. #if defined(CONFIG_NFS_V4)
  87. &nfs_version4,
  88. #endif
  89. };
  90. static struct rpc_program nfs_program = {
  91. .name = "nfs",
  92. .number = NFS_PROGRAM,
  93. .nrvers = sizeof(nfs_version) / sizeof(nfs_version[0]),
  94. .version = nfs_version,
  95. .stats = &nfs_rpcstat,
  96. .pipe_dir_name = "/nfs",
  97. };
  98. #ifdef CONFIG_NFS_V3_ACL
  99. static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program };
  100. static struct rpc_version * nfsacl_version[] = {
  101. [3] = &nfsacl_version3,
  102. };
  103. struct rpc_program nfsacl_program = {
  104. .name = "nfsacl",
  105. .number = NFS_ACL_PROGRAM,
  106. .nrvers = sizeof(nfsacl_version) / sizeof(nfsacl_version[0]),
  107. .version = nfsacl_version,
  108. .stats = &nfsacl_rpcstat,
  109. };
  110. #endif /* CONFIG_NFS_V3_ACL */
  111. static inline unsigned long
  112. nfs_fattr_to_ino_t(struct nfs_fattr *fattr)
  113. {
  114. return nfs_fileid_to_ino_t(fattr->fileid);
  115. }
  116. static int
  117. nfs_write_inode(struct inode *inode, int sync)
  118. {
  119. int flags = sync ? FLUSH_WAIT : 0;
  120. int ret;
  121. ret = nfs_commit_inode(inode, flags);
  122. if (ret < 0)
  123. return ret;
  124. return 0;
  125. }
  126. static void
  127. nfs_delete_inode(struct inode * inode)
  128. {
  129. dprintk("NFS: delete_inode(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino);
  130. nfs_wb_all(inode);
  131. /*
  132. * The following should never happen...
  133. */
  134. if (nfs_have_writebacks(inode)) {
  135. printk(KERN_ERR "nfs_delete_inode: inode %ld has pending RPC requests\n", inode->i_ino);
  136. }
  137. clear_inode(inode);
  138. }
  139. static void
  140. nfs_clear_inode(struct inode *inode)
  141. {
  142. struct nfs_inode *nfsi = NFS_I(inode);
  143. struct rpc_cred *cred;
  144. nfs_wb_all(inode);
  145. BUG_ON (!list_empty(&nfsi->open_files));
  146. nfs_zap_acl_cache(inode);
  147. cred = nfsi->cache_access.cred;
  148. if (cred)
  149. put_rpccred(cred);
  150. BUG_ON(atomic_read(&nfsi->data_updates) != 0);
  151. }
  152. void
  153. nfs_umount_begin(struct super_block *sb)
  154. {
  155. struct rpc_clnt *rpc = NFS_SB(sb)->client;
  156. /* -EIO all pending I/O */
  157. if (!IS_ERR(rpc))
  158. rpc_killall_tasks(rpc);
  159. rpc = NFS_SB(sb)->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 < 1024)
  193. bsize = NFS_DEF_FILE_IO_BUFFER_SIZE;
  194. else if (bsize >= NFS_MAX_FILE_IO_BUFFER_SIZE)
  195. bsize = NFS_MAX_FILE_IO_BUFFER_SIZE;
  196. return nfs_block_bits(bsize, nrbitsp);
  197. }
  198. /*
  199. * Obtain the root inode of the file system.
  200. */
  201. static struct inode *
  202. nfs_get_root(struct super_block *sb, struct nfs_fh *rootfh, struct nfs_fsinfo *fsinfo)
  203. {
  204. struct nfs_server *server = NFS_SB(sb);
  205. struct inode *rooti;
  206. int error;
  207. error = server->rpc_ops->getroot(server, rootfh, fsinfo);
  208. if (error < 0) {
  209. dprintk("nfs_get_root: getattr error = %d\n", -error);
  210. return ERR_PTR(error);
  211. }
  212. rooti = nfs_fhget(sb, rootfh, fsinfo->fattr);
  213. if (!rooti)
  214. return ERR_PTR(-ENOMEM);
  215. return rooti;
  216. }
  217. /*
  218. * Do NFS version-independent mount processing, and sanity checking
  219. */
  220. static int
  221. nfs_sb_init(struct super_block *sb, rpc_authflavor_t authflavor)
  222. {
  223. struct nfs_server *server;
  224. struct inode *root_inode;
  225. struct nfs_fattr fattr;
  226. struct nfs_fsinfo fsinfo = {
  227. .fattr = &fattr,
  228. };
  229. struct nfs_pathconf pathinfo = {
  230. .fattr = &fattr,
  231. };
  232. int no_root_error = 0;
  233. unsigned long max_rpc_payload;
  234. /* We probably want something more informative here */
  235. snprintf(sb->s_id, sizeof(sb->s_id), "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
  236. server = NFS_SB(sb);
  237. sb->s_magic = NFS_SUPER_MAGIC;
  238. root_inode = nfs_get_root(sb, &server->fh, &fsinfo);
  239. /* Did getting the root inode fail? */
  240. if (IS_ERR(root_inode)) {
  241. no_root_error = PTR_ERR(root_inode);
  242. goto out_no_root;
  243. }
  244. sb->s_root = d_alloc_root(root_inode);
  245. if (!sb->s_root) {
  246. no_root_error = -ENOMEM;
  247. goto out_no_root;
  248. }
  249. sb->s_root->d_op = server->rpc_ops->dentry_ops;
  250. /* Get some general file system info */
  251. if (server->namelen == 0 &&
  252. server->rpc_ops->pathconf(server, &server->fh, &pathinfo) >= 0)
  253. server->namelen = pathinfo.max_namelen;
  254. /* Work out a lot of parameters */
  255. if (server->rsize == 0)
  256. server->rsize = nfs_block_size(fsinfo.rtpref, NULL);
  257. if (server->wsize == 0)
  258. server->wsize = nfs_block_size(fsinfo.wtpref, NULL);
  259. if (fsinfo.rtmax >= 512 && server->rsize > fsinfo.rtmax)
  260. server->rsize = nfs_block_size(fsinfo.rtmax, NULL);
  261. if (fsinfo.wtmax >= 512 && server->wsize > fsinfo.wtmax)
  262. server->wsize = nfs_block_size(fsinfo.wtmax, NULL);
  263. max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
  264. if (server->rsize > max_rpc_payload)
  265. server->rsize = max_rpc_payload;
  266. if (server->wsize > max_rpc_payload)
  267. server->wsize = max_rpc_payload;
  268. server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  269. if (server->rpages > NFS_READ_MAXIOV) {
  270. server->rpages = NFS_READ_MAXIOV;
  271. server->rsize = server->rpages << PAGE_CACHE_SHIFT;
  272. }
  273. server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  274. if (server->wpages > NFS_WRITE_MAXIOV) {
  275. server->wpages = NFS_WRITE_MAXIOV;
  276. server->wsize = server->wpages << PAGE_CACHE_SHIFT;
  277. }
  278. if (sb->s_blocksize == 0)
  279. sb->s_blocksize = nfs_block_bits(server->wsize,
  280. &sb->s_blocksize_bits);
  281. server->wtmult = nfs_block_bits(fsinfo.wtmult, NULL);
  282. server->dtsize = nfs_block_size(fsinfo.dtpref, NULL);
  283. if (server->dtsize > PAGE_CACHE_SIZE)
  284. server->dtsize = PAGE_CACHE_SIZE;
  285. if (server->dtsize > server->rsize)
  286. server->dtsize = server->rsize;
  287. if (server->flags & NFS_MOUNT_NOAC) {
  288. server->acregmin = server->acregmax = 0;
  289. server->acdirmin = server->acdirmax = 0;
  290. sb->s_flags |= MS_SYNCHRONOUS;
  291. }
  292. server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
  293. sb->s_maxbytes = fsinfo.maxfilesize;
  294. if (sb->s_maxbytes > MAX_LFS_FILESIZE)
  295. sb->s_maxbytes = MAX_LFS_FILESIZE;
  296. server->client->cl_intr = (server->flags & NFS_MOUNT_INTR) ? 1 : 0;
  297. server->client->cl_softrtry = (server->flags & NFS_MOUNT_SOFT) ? 1 : 0;
  298. /* We're airborne Set socket buffersize */
  299. rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
  300. return 0;
  301. /* Yargs. It didn't work out. */
  302. out_no_root:
  303. dprintk("nfs_sb_init: get root inode failed: errno %d\n", -no_root_error);
  304. if (!IS_ERR(root_inode))
  305. iput(root_inode);
  306. return no_root_error;
  307. }
  308. /*
  309. * Create an RPC client handle.
  310. */
  311. static struct rpc_clnt *
  312. nfs_create_client(struct nfs_server *server, const struct nfs_mount_data *data)
  313. {
  314. struct rpc_timeout timeparms;
  315. struct rpc_xprt *xprt = NULL;
  316. struct rpc_clnt *clnt = NULL;
  317. int tcp = (data->flags & NFS_MOUNT_TCP);
  318. /* Initialize timeout values */
  319. timeparms.to_initval = data->timeo * HZ / 10;
  320. timeparms.to_retries = data->retrans;
  321. timeparms.to_maxval = tcp ? RPC_MAX_TCP_TIMEOUT : RPC_MAX_UDP_TIMEOUT;
  322. timeparms.to_exponential = 1;
  323. if (!timeparms.to_initval)
  324. timeparms.to_initval = (tcp ? 600 : 11) * HZ / 10;
  325. if (!timeparms.to_retries)
  326. timeparms.to_retries = 5;
  327. /* create transport and client */
  328. xprt = xprt_create_proto(tcp ? IPPROTO_TCP : IPPROTO_UDP,
  329. &server->addr, &timeparms);
  330. if (IS_ERR(xprt)) {
  331. dprintk("%s: cannot create RPC transport. Error = %ld\n",
  332. __FUNCTION__, PTR_ERR(xprt));
  333. return (struct rpc_clnt *)xprt;
  334. }
  335. clnt = rpc_create_client(xprt, server->hostname, &nfs_program,
  336. server->rpc_ops->version, data->pseudoflavor);
  337. if (IS_ERR(clnt)) {
  338. dprintk("%s: cannot create RPC client. Error = %ld\n",
  339. __FUNCTION__, PTR_ERR(xprt));
  340. goto out_fail;
  341. }
  342. clnt->cl_intr = 1;
  343. clnt->cl_softrtry = 1;
  344. clnt->cl_chatty = 1;
  345. return clnt;
  346. out_fail:
  347. return clnt;
  348. }
  349. /*
  350. * The way this works is that the mount process passes a structure
  351. * in the data argument which contains the server's IP address
  352. * and the root file handle obtained from the server's mount
  353. * daemon. We stash these away in the private superblock fields.
  354. */
  355. static int
  356. nfs_fill_super(struct super_block *sb, struct nfs_mount_data *data, int silent)
  357. {
  358. struct nfs_server *server;
  359. rpc_authflavor_t authflavor;
  360. server = NFS_SB(sb);
  361. sb->s_blocksize_bits = 0;
  362. sb->s_blocksize = 0;
  363. if (data->bsize)
  364. sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
  365. if (data->rsize)
  366. server->rsize = nfs_block_size(data->rsize, NULL);
  367. if (data->wsize)
  368. server->wsize = nfs_block_size(data->wsize, NULL);
  369. server->flags = data->flags & NFS_MOUNT_FLAGMASK;
  370. server->acregmin = data->acregmin*HZ;
  371. server->acregmax = data->acregmax*HZ;
  372. server->acdirmin = data->acdirmin*HZ;
  373. server->acdirmax = data->acdirmax*HZ;
  374. /* Start lockd here, before we might error out */
  375. if (!(server->flags & NFS_MOUNT_NONLM))
  376. lockd_up();
  377. server->namelen = data->namlen;
  378. server->hostname = kmalloc(strlen(data->hostname) + 1, GFP_KERNEL);
  379. if (!server->hostname)
  380. return -ENOMEM;
  381. strcpy(server->hostname, data->hostname);
  382. /* Check NFS protocol revision and initialize RPC op vector
  383. * and file handle pool. */
  384. #ifdef CONFIG_NFS_V3
  385. if (server->flags & NFS_MOUNT_VER3) {
  386. server->rpc_ops = &nfs_v3_clientops;
  387. server->caps |= NFS_CAP_READDIRPLUS;
  388. } else {
  389. server->rpc_ops = &nfs_v2_clientops;
  390. }
  391. #else
  392. server->rpc_ops = &nfs_v2_clientops;
  393. #endif
  394. /* Fill in pseudoflavor for mount version < 5 */
  395. if (!(data->flags & NFS_MOUNT_SECFLAVOUR))
  396. data->pseudoflavor = RPC_AUTH_UNIX;
  397. authflavor = data->pseudoflavor; /* save for sb_init() */
  398. /* XXX maybe we want to add a server->pseudoflavor field */
  399. /* Create RPC client handles */
  400. server->client = nfs_create_client(server, data);
  401. if (IS_ERR(server->client))
  402. return PTR_ERR(server->client);
  403. /* RFC 2623, sec 2.3.2 */
  404. if (authflavor != RPC_AUTH_UNIX) {
  405. struct rpc_auth *auth;
  406. server->client_sys = rpc_clone_client(server->client);
  407. if (IS_ERR(server->client_sys))
  408. return PTR_ERR(server->client_sys);
  409. auth = rpcauth_create(RPC_AUTH_UNIX, server->client_sys);
  410. if (IS_ERR(auth))
  411. return PTR_ERR(auth);
  412. } else {
  413. atomic_inc(&server->client->cl_count);
  414. server->client_sys = server->client;
  415. }
  416. if (server->flags & NFS_MOUNT_VER3) {
  417. #ifdef CONFIG_NFS_V3_ACL
  418. if (!(server->flags & NFS_MOUNT_NOACL)) {
  419. server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
  420. /* No errors! Assume that Sun nfsacls are supported */
  421. if (!IS_ERR(server->client_acl))
  422. server->caps |= NFS_CAP_ACLS;
  423. }
  424. #else
  425. server->flags &= ~NFS_MOUNT_NOACL;
  426. #endif /* CONFIG_NFS_V3_ACL */
  427. /*
  428. * The VFS shouldn't apply the umask to mode bits. We will
  429. * do so ourselves when necessary.
  430. */
  431. sb->s_flags |= MS_POSIXACL;
  432. if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
  433. server->namelen = NFS3_MAXNAMLEN;
  434. sb->s_time_gran = 1;
  435. } else {
  436. if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
  437. server->namelen = NFS2_MAXNAMLEN;
  438. }
  439. sb->s_op = &nfs_sops;
  440. return nfs_sb_init(sb, authflavor);
  441. }
  442. static int
  443. nfs_statfs(struct super_block *sb, struct kstatfs *buf)
  444. {
  445. struct nfs_server *server = NFS_SB(sb);
  446. unsigned char blockbits;
  447. unsigned long blockres;
  448. struct nfs_fh *rootfh = NFS_FH(sb->s_root->d_inode);
  449. struct nfs_fattr fattr;
  450. struct nfs_fsstat res = {
  451. .fattr = &fattr,
  452. };
  453. int error;
  454. lock_kernel();
  455. error = server->rpc_ops->statfs(server, rootfh, &res);
  456. buf->f_type = NFS_SUPER_MAGIC;
  457. if (error < 0)
  458. goto out_err;
  459. /*
  460. * Current versions of glibc do not correctly handle the
  461. * case where f_frsize != f_bsize. Eventually we want to
  462. * report the value of wtmult in this field.
  463. */
  464. buf->f_frsize = sb->s_blocksize;
  465. /*
  466. * On most *nix systems, f_blocks, f_bfree, and f_bavail
  467. * are reported in units of f_frsize. Linux hasn't had
  468. * an f_frsize field in its statfs struct until recently,
  469. * thus historically Linux's sys_statfs reports these
  470. * fields in units of f_bsize.
  471. */
  472. buf->f_bsize = sb->s_blocksize;
  473. blockbits = sb->s_blocksize_bits;
  474. blockres = (1 << blockbits) - 1;
  475. buf->f_blocks = (res.tbytes + blockres) >> blockbits;
  476. buf->f_bfree = (res.fbytes + blockres) >> blockbits;
  477. buf->f_bavail = (res.abytes + blockres) >> blockbits;
  478. buf->f_files = res.tfiles;
  479. buf->f_ffree = res.afiles;
  480. buf->f_namelen = server->namelen;
  481. out:
  482. unlock_kernel();
  483. return 0;
  484. out_err:
  485. printk(KERN_WARNING "nfs_statfs: statfs error = %d\n", -error);
  486. buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1;
  487. goto out;
  488. }
  489. static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
  490. {
  491. static struct proc_nfs_info {
  492. int flag;
  493. char *str;
  494. char *nostr;
  495. } nfs_info[] = {
  496. { NFS_MOUNT_SOFT, ",soft", ",hard" },
  497. { NFS_MOUNT_INTR, ",intr", "" },
  498. { NFS_MOUNT_POSIX, ",posix", "" },
  499. { NFS_MOUNT_TCP, ",tcp", ",udp" },
  500. { NFS_MOUNT_NOCTO, ",nocto", "" },
  501. { NFS_MOUNT_NOAC, ",noac", "" },
  502. { NFS_MOUNT_NONLM, ",nolock", ",lock" },
  503. { NFS_MOUNT_NOACL, ",noacl", "" },
  504. { 0, NULL, NULL }
  505. };
  506. struct proc_nfs_info *nfs_infop;
  507. struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
  508. seq_printf(m, ",v%d", nfss->rpc_ops->version);
  509. seq_printf(m, ",rsize=%d", nfss->rsize);
  510. seq_printf(m, ",wsize=%d", nfss->wsize);
  511. if (nfss->acregmin != 3*HZ)
  512. seq_printf(m, ",acregmin=%d", nfss->acregmin/HZ);
  513. if (nfss->acregmax != 60*HZ)
  514. seq_printf(m, ",acregmax=%d", nfss->acregmax/HZ);
  515. if (nfss->acdirmin != 30*HZ)
  516. seq_printf(m, ",acdirmin=%d", nfss->acdirmin/HZ);
  517. if (nfss->acdirmax != 60*HZ)
  518. seq_printf(m, ",acdirmax=%d", nfss->acdirmax/HZ);
  519. for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
  520. if (nfss->flags & nfs_infop->flag)
  521. seq_puts(m, nfs_infop->str);
  522. else
  523. seq_puts(m, nfs_infop->nostr);
  524. }
  525. seq_puts(m, ",addr=");
  526. seq_escape(m, nfss->hostname, " \t\n\\");
  527. return 0;
  528. }
  529. /*
  530. * Invalidate the local caches
  531. */
  532. void
  533. nfs_zap_caches(struct inode *inode)
  534. {
  535. struct nfs_inode *nfsi = NFS_I(inode);
  536. int mode = inode->i_mode;
  537. NFS_ATTRTIMEO(inode) = NFS_MINATTRTIMEO(inode);
  538. NFS_ATTRTIMEO_UPDATE(inode) = jiffies;
  539. memset(NFS_COOKIEVERF(inode), 0, sizeof(NFS_COOKIEVERF(inode)));
  540. if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))
  541. nfsi->flags |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL|NFS_INO_REVAL_PAGECACHE;
  542. else
  543. nfsi->flags |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL|NFS_INO_REVAL_PAGECACHE;
  544. }
  545. static void nfs_zap_acl_cache(struct inode *inode)
  546. {
  547. void (*clear_acl_cache)(struct inode *);
  548. clear_acl_cache = NFS_PROTO(inode)->clear_acl_cache;
  549. if (clear_acl_cache != NULL)
  550. clear_acl_cache(inode);
  551. NFS_I(inode)->flags &= ~NFS_INO_INVALID_ACL;
  552. }
  553. /*
  554. * Invalidate, but do not unhash, the inode
  555. */
  556. static void
  557. nfs_invalidate_inode(struct inode *inode)
  558. {
  559. umode_t save_mode = inode->i_mode;
  560. make_bad_inode(inode);
  561. inode->i_mode = save_mode;
  562. nfs_zap_caches(inode);
  563. }
  564. struct nfs_find_desc {
  565. struct nfs_fh *fh;
  566. struct nfs_fattr *fattr;
  567. };
  568. /*
  569. * In NFSv3 we can have 64bit inode numbers. In order to support
  570. * this, and re-exported directories (also seen in NFSv2)
  571. * we are forced to allow 2 different inodes to have the same
  572. * i_ino.
  573. */
  574. static int
  575. nfs_find_actor(struct inode *inode, void *opaque)
  576. {
  577. struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
  578. struct nfs_fh *fh = desc->fh;
  579. struct nfs_fattr *fattr = desc->fattr;
  580. if (NFS_FILEID(inode) != fattr->fileid)
  581. return 0;
  582. if (nfs_compare_fh(NFS_FH(inode), fh))
  583. return 0;
  584. if (is_bad_inode(inode) || NFS_STALE(inode))
  585. return 0;
  586. return 1;
  587. }
  588. static int
  589. nfs_init_locked(struct inode *inode, void *opaque)
  590. {
  591. struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
  592. struct nfs_fattr *fattr = desc->fattr;
  593. NFS_FILEID(inode) = fattr->fileid;
  594. nfs_copy_fh(NFS_FH(inode), desc->fh);
  595. return 0;
  596. }
  597. /* Don't use READDIRPLUS on directories that we believe are too large */
  598. #define NFS_LIMIT_READDIRPLUS (8*PAGE_SIZE)
  599. /*
  600. * This is our front-end to iget that looks up inodes by file handle
  601. * instead of inode number.
  602. */
  603. struct inode *
  604. nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr)
  605. {
  606. struct nfs_find_desc desc = {
  607. .fh = fh,
  608. .fattr = fattr
  609. };
  610. struct inode *inode = NULL;
  611. unsigned long hash;
  612. if ((fattr->valid & NFS_ATTR_FATTR) == 0)
  613. goto out_no_inode;
  614. if (!fattr->nlink) {
  615. printk("NFS: Buggy server - nlink == 0!\n");
  616. goto out_no_inode;
  617. }
  618. hash = nfs_fattr_to_ino_t(fattr);
  619. if (!(inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc)))
  620. goto out_no_inode;
  621. if (inode->i_state & I_NEW) {
  622. struct nfs_inode *nfsi = NFS_I(inode);
  623. /* We set i_ino for the few things that still rely on it,
  624. * such as stat(2) */
  625. inode->i_ino = hash;
  626. /* We can't support update_atime(), since the server will reset it */
  627. inode->i_flags |= S_NOATIME|S_NOCMTIME;
  628. inode->i_mode = fattr->mode;
  629. /* Why so? Because we want revalidate for devices/FIFOs, and
  630. * that's precisely what we have in nfs_file_inode_operations.
  631. */
  632. inode->i_op = NFS_SB(sb)->rpc_ops->file_inode_ops;
  633. if (S_ISREG(inode->i_mode)) {
  634. inode->i_fop = &nfs_file_operations;
  635. inode->i_data.a_ops = &nfs_file_aops;
  636. inode->i_data.backing_dev_info = &NFS_SB(sb)->backing_dev_info;
  637. } else if (S_ISDIR(inode->i_mode)) {
  638. inode->i_op = NFS_SB(sb)->rpc_ops->dir_inode_ops;
  639. inode->i_fop = &nfs_dir_operations;
  640. if (nfs_server_capable(inode, NFS_CAP_READDIRPLUS)
  641. && fattr->size <= NFS_LIMIT_READDIRPLUS)
  642. NFS_FLAGS(inode) |= NFS_INO_ADVISE_RDPLUS;
  643. } else if (S_ISLNK(inode->i_mode))
  644. inode->i_op = &nfs_symlink_inode_operations;
  645. else
  646. init_special_inode(inode, inode->i_mode, fattr->rdev);
  647. nfsi->read_cache_jiffies = fattr->timestamp;
  648. inode->i_atime = fattr->atime;
  649. inode->i_mtime = fattr->mtime;
  650. inode->i_ctime = fattr->ctime;
  651. if (fattr->valid & NFS_ATTR_FATTR_V4)
  652. nfsi->change_attr = fattr->change_attr;
  653. inode->i_size = nfs_size_to_loff_t(fattr->size);
  654. inode->i_nlink = fattr->nlink;
  655. inode->i_uid = fattr->uid;
  656. inode->i_gid = fattr->gid;
  657. if (fattr->valid & (NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4)) {
  658. /*
  659. * report the blocks in 512byte units
  660. */
  661. inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
  662. inode->i_blksize = inode->i_sb->s_blocksize;
  663. } else {
  664. inode->i_blocks = fattr->du.nfs2.blocks;
  665. inode->i_blksize = fattr->du.nfs2.blocksize;
  666. }
  667. nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
  668. nfsi->attrtimeo_timestamp = jiffies;
  669. memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
  670. nfsi->cache_access.cred = NULL;
  671. unlock_new_inode(inode);
  672. } else
  673. nfs_refresh_inode(inode, fattr);
  674. dprintk("NFS: nfs_fhget(%s/%Ld ct=%d)\n",
  675. inode->i_sb->s_id,
  676. (long long)NFS_FILEID(inode),
  677. atomic_read(&inode->i_count));
  678. out:
  679. return inode;
  680. out_no_inode:
  681. printk("nfs_fhget: iget failed\n");
  682. goto out;
  683. }
  684. #define NFS_VALID_ATTRS (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE|ATTR_ATIME|ATTR_ATIME_SET|ATTR_MTIME|ATTR_MTIME_SET)
  685. int
  686. nfs_setattr(struct dentry *dentry, struct iattr *attr)
  687. {
  688. struct inode *inode = dentry->d_inode;
  689. struct nfs_fattr fattr;
  690. int error;
  691. if (attr->ia_valid & ATTR_SIZE) {
  692. if (!S_ISREG(inode->i_mode) || attr->ia_size == i_size_read(inode))
  693. attr->ia_valid &= ~ATTR_SIZE;
  694. }
  695. /* Optimization: if the end result is no change, don't RPC */
  696. attr->ia_valid &= NFS_VALID_ATTRS;
  697. if (attr->ia_valid == 0)
  698. return 0;
  699. lock_kernel();
  700. nfs_begin_data_update(inode);
  701. /* Write all dirty data if we're changing file permissions or size */
  702. if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE)) != 0) {
  703. if (filemap_fdatawrite(inode->i_mapping) == 0)
  704. filemap_fdatawait(inode->i_mapping);
  705. nfs_wb_all(inode);
  706. }
  707. error = NFS_PROTO(inode)->setattr(dentry, &fattr, attr);
  708. if (error == 0) {
  709. nfs_refresh_inode(inode, &fattr);
  710. if ((attr->ia_valid & ATTR_MODE) != 0) {
  711. int mode;
  712. mode = inode->i_mode & ~S_IALLUGO;
  713. mode |= attr->ia_mode & S_IALLUGO;
  714. inode->i_mode = mode;
  715. }
  716. if ((attr->ia_valid & ATTR_UID) != 0)
  717. inode->i_uid = attr->ia_uid;
  718. if ((attr->ia_valid & ATTR_GID) != 0)
  719. inode->i_gid = attr->ia_gid;
  720. if ((attr->ia_valid & ATTR_SIZE) != 0) {
  721. inode->i_size = attr->ia_size;
  722. vmtruncate(inode, attr->ia_size);
  723. }
  724. }
  725. if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
  726. NFS_FLAGS(inode) |= NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  727. nfs_end_data_update(inode);
  728. unlock_kernel();
  729. return error;
  730. }
  731. /*
  732. * Wait for the inode to get unlocked.
  733. * (Used for NFS_INO_LOCKED and NFS_INO_REVALIDATING).
  734. */
  735. static int
  736. nfs_wait_on_inode(struct inode *inode, int flag)
  737. {
  738. struct rpc_clnt *clnt = NFS_CLIENT(inode);
  739. struct nfs_inode *nfsi = NFS_I(inode);
  740. int error;
  741. if (!(NFS_FLAGS(inode) & flag))
  742. return 0;
  743. atomic_inc(&inode->i_count);
  744. error = nfs_wait_event(clnt, nfsi->nfs_i_wait,
  745. !(NFS_FLAGS(inode) & flag));
  746. iput(inode);
  747. return error;
  748. }
  749. int nfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  750. {
  751. struct inode *inode = dentry->d_inode;
  752. struct nfs_inode *nfsi = NFS_I(inode);
  753. int need_atime = nfsi->flags & NFS_INO_INVALID_ATIME;
  754. int err;
  755. if (__IS_FLG(inode, MS_NOATIME))
  756. need_atime = 0;
  757. else if (__IS_FLG(inode, MS_NODIRATIME) && S_ISDIR(inode->i_mode))
  758. need_atime = 0;
  759. /* We may force a getattr if the user cares about atime */
  760. if (need_atime)
  761. err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  762. else
  763. err = nfs_revalidate_inode(NFS_SERVER(inode), inode);
  764. if (!err)
  765. generic_fillattr(inode, stat);
  766. return err;
  767. }
  768. struct nfs_open_context *alloc_nfs_open_context(struct dentry *dentry, struct rpc_cred *cred)
  769. {
  770. struct nfs_open_context *ctx;
  771. ctx = (struct nfs_open_context *)kmalloc(sizeof(*ctx), GFP_KERNEL);
  772. if (ctx != NULL) {
  773. atomic_set(&ctx->count, 1);
  774. ctx->dentry = dget(dentry);
  775. ctx->cred = get_rpccred(cred);
  776. ctx->state = NULL;
  777. ctx->lockowner = current->files;
  778. ctx->error = 0;
  779. ctx->dir_cookie = 0;
  780. }
  781. return ctx;
  782. }
  783. struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx)
  784. {
  785. if (ctx != NULL)
  786. atomic_inc(&ctx->count);
  787. return ctx;
  788. }
  789. void put_nfs_open_context(struct nfs_open_context *ctx)
  790. {
  791. if (atomic_dec_and_test(&ctx->count)) {
  792. if (!list_empty(&ctx->list)) {
  793. struct inode *inode = ctx->dentry->d_inode;
  794. spin_lock(&inode->i_lock);
  795. list_del(&ctx->list);
  796. spin_unlock(&inode->i_lock);
  797. }
  798. if (ctx->state != NULL)
  799. nfs4_close_state(ctx->state, ctx->mode);
  800. if (ctx->cred != NULL)
  801. put_rpccred(ctx->cred);
  802. dput(ctx->dentry);
  803. kfree(ctx);
  804. }
  805. }
  806. /*
  807. * Ensure that mmap has a recent RPC credential for use when writing out
  808. * shared pages
  809. */
  810. void nfs_file_set_open_context(struct file *filp, struct nfs_open_context *ctx)
  811. {
  812. struct inode *inode = filp->f_dentry->d_inode;
  813. struct nfs_inode *nfsi = NFS_I(inode);
  814. filp->private_data = get_nfs_open_context(ctx);
  815. spin_lock(&inode->i_lock);
  816. list_add(&ctx->list, &nfsi->open_files);
  817. spin_unlock(&inode->i_lock);
  818. }
  819. struct nfs_open_context *nfs_find_open_context(struct inode *inode, int mode)
  820. {
  821. struct nfs_inode *nfsi = NFS_I(inode);
  822. struct nfs_open_context *pos, *ctx = NULL;
  823. spin_lock(&inode->i_lock);
  824. list_for_each_entry(pos, &nfsi->open_files, list) {
  825. if ((pos->mode & mode) == mode) {
  826. ctx = get_nfs_open_context(pos);
  827. break;
  828. }
  829. }
  830. spin_unlock(&inode->i_lock);
  831. return ctx;
  832. }
  833. void nfs_file_clear_open_context(struct file *filp)
  834. {
  835. struct inode *inode = filp->f_dentry->d_inode;
  836. struct nfs_open_context *ctx = (struct nfs_open_context *)filp->private_data;
  837. if (ctx) {
  838. filp->private_data = NULL;
  839. spin_lock(&inode->i_lock);
  840. list_move_tail(&ctx->list, &NFS_I(inode)->open_files);
  841. spin_unlock(&inode->i_lock);
  842. put_nfs_open_context(ctx);
  843. }
  844. }
  845. /*
  846. * These allocate and release file read/write context information.
  847. */
  848. int nfs_open(struct inode *inode, struct file *filp)
  849. {
  850. struct nfs_open_context *ctx;
  851. struct rpc_cred *cred;
  852. cred = rpcauth_lookupcred(NFS_CLIENT(inode)->cl_auth, 0);
  853. if (IS_ERR(cred))
  854. return PTR_ERR(cred);
  855. ctx = alloc_nfs_open_context(filp->f_dentry, cred);
  856. put_rpccred(cred);
  857. if (ctx == NULL)
  858. return -ENOMEM;
  859. ctx->mode = filp->f_mode;
  860. nfs_file_set_open_context(filp, ctx);
  861. put_nfs_open_context(ctx);
  862. if ((filp->f_mode & FMODE_WRITE) != 0)
  863. nfs_begin_data_update(inode);
  864. return 0;
  865. }
  866. int nfs_release(struct inode *inode, struct file *filp)
  867. {
  868. if ((filp->f_mode & FMODE_WRITE) != 0)
  869. nfs_end_data_update(inode);
  870. nfs_file_clear_open_context(filp);
  871. return 0;
  872. }
  873. /*
  874. * This function is called whenever some part of NFS notices that
  875. * the cached attributes have to be refreshed.
  876. */
  877. int
  878. __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
  879. {
  880. int status = -ESTALE;
  881. struct nfs_fattr fattr;
  882. struct nfs_inode *nfsi = NFS_I(inode);
  883. unsigned long verifier;
  884. unsigned int flags;
  885. dfprintk(PAGECACHE, "NFS: revalidating (%s/%Ld)\n",
  886. inode->i_sb->s_id, (long long)NFS_FILEID(inode));
  887. lock_kernel();
  888. if (!inode || is_bad_inode(inode))
  889. goto out_nowait;
  890. if (NFS_STALE(inode))
  891. goto out_nowait;
  892. while (NFS_REVALIDATING(inode)) {
  893. status = nfs_wait_on_inode(inode, NFS_INO_REVALIDATING);
  894. if (status < 0)
  895. goto out_nowait;
  896. if (NFS_ATTRTIMEO(inode) == 0)
  897. continue;
  898. if (NFS_FLAGS(inode) & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ATIME))
  899. continue;
  900. status = NFS_STALE(inode) ? -ESTALE : 0;
  901. goto out_nowait;
  902. }
  903. NFS_FLAGS(inode) |= NFS_INO_REVALIDATING;
  904. /* Protect against RPC races by saving the change attribute */
  905. verifier = nfs_save_change_attribute(inode);
  906. status = NFS_PROTO(inode)->getattr(server, NFS_FH(inode), &fattr);
  907. if (status != 0) {
  908. dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) getattr failed, error=%d\n",
  909. inode->i_sb->s_id,
  910. (long long)NFS_FILEID(inode), status);
  911. if (status == -ESTALE) {
  912. nfs_zap_caches(inode);
  913. if (!S_ISDIR(inode->i_mode))
  914. NFS_FLAGS(inode) |= NFS_INO_STALE;
  915. }
  916. goto out;
  917. }
  918. status = nfs_update_inode(inode, &fattr, verifier);
  919. if (status) {
  920. dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) refresh failed, error=%d\n",
  921. inode->i_sb->s_id,
  922. (long long)NFS_FILEID(inode), status);
  923. goto out;
  924. }
  925. flags = nfsi->flags;
  926. nfsi->flags &= ~NFS_INO_REVAL_PAGECACHE;
  927. /*
  928. * We may need to keep the attributes marked as invalid if
  929. * we raced with nfs_end_attr_update().
  930. */
  931. if (verifier == nfsi->cache_change_attribute)
  932. nfsi->flags &= ~(NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME);
  933. /* Do the page cache invalidation */
  934. nfs_revalidate_mapping(inode, inode->i_mapping);
  935. if (flags & NFS_INO_INVALID_ACL)
  936. nfs_zap_acl_cache(inode);
  937. dfprintk(PAGECACHE, "NFS: (%s/%Ld) revalidation complete\n",
  938. inode->i_sb->s_id,
  939. (long long)NFS_FILEID(inode));
  940. out:
  941. NFS_FLAGS(inode) &= ~NFS_INO_REVALIDATING;
  942. wake_up(&nfsi->nfs_i_wait);
  943. out_nowait:
  944. unlock_kernel();
  945. return status;
  946. }
  947. int nfs_attribute_timeout(struct inode *inode)
  948. {
  949. struct nfs_inode *nfsi = NFS_I(inode);
  950. if (nfs_have_delegation(inode, FMODE_READ))
  951. return 0;
  952. return time_after(jiffies, nfsi->read_cache_jiffies+nfsi->attrtimeo);
  953. }
  954. /**
  955. * nfs_revalidate_inode - Revalidate the inode attributes
  956. * @server - pointer to nfs_server struct
  957. * @inode - pointer to inode struct
  958. *
  959. * Updates inode attribute information by retrieving the data from the server.
  960. */
  961. int nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
  962. {
  963. if (!(NFS_FLAGS(inode) & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA))
  964. && !nfs_attribute_timeout(inode))
  965. return NFS_STALE(inode) ? -ESTALE : 0;
  966. return __nfs_revalidate_inode(server, inode);
  967. }
  968. /**
  969. * nfs_revalidate_mapping - Revalidate the pagecache
  970. * @inode - pointer to host inode
  971. * @mapping - pointer to mapping
  972. */
  973. void nfs_revalidate_mapping(struct inode *inode, struct address_space *mapping)
  974. {
  975. struct nfs_inode *nfsi = NFS_I(inode);
  976. if (nfsi->flags & NFS_INO_INVALID_DATA) {
  977. if (S_ISREG(inode->i_mode)) {
  978. if (filemap_fdatawrite(mapping) == 0)
  979. filemap_fdatawait(mapping);
  980. nfs_wb_all(inode);
  981. }
  982. invalidate_inode_pages2(mapping);
  983. nfsi->flags &= ~NFS_INO_INVALID_DATA;
  984. if (S_ISDIR(inode->i_mode)) {
  985. memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
  986. /* This ensures we revalidate child dentries */
  987. nfsi->cache_change_attribute++;
  988. }
  989. dfprintk(PAGECACHE, "NFS: (%s/%Ld) data cache invalidated\n",
  990. inode->i_sb->s_id,
  991. (long long)NFS_FILEID(inode));
  992. }
  993. }
  994. /**
  995. * nfs_begin_data_update
  996. * @inode - pointer to inode
  997. * Declare that a set of operations will update file data on the server
  998. */
  999. void nfs_begin_data_update(struct inode *inode)
  1000. {
  1001. atomic_inc(&NFS_I(inode)->data_updates);
  1002. }
  1003. /**
  1004. * nfs_end_data_update
  1005. * @inode - pointer to inode
  1006. * Declare end of the operations that will update file data
  1007. * This will mark the inode as immediately needing revalidation
  1008. * of its attribute cache.
  1009. */
  1010. void nfs_end_data_update(struct inode *inode)
  1011. {
  1012. struct nfs_inode *nfsi = NFS_I(inode);
  1013. if (!nfs_have_delegation(inode, FMODE_READ)) {
  1014. /* Mark the attribute cache for revalidation */
  1015. nfsi->flags |= NFS_INO_INVALID_ATTR;
  1016. /* Directories and symlinks: invalidate page cache too */
  1017. if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
  1018. nfsi->flags |= NFS_INO_INVALID_DATA;
  1019. }
  1020. nfsi->cache_change_attribute ++;
  1021. atomic_dec(&nfsi->data_updates);
  1022. }
  1023. /**
  1024. * nfs_refresh_inode - verify consistency of the inode attribute cache
  1025. * @inode - pointer to inode
  1026. * @fattr - updated attributes
  1027. *
  1028. * Verifies the attribute cache. If we have just changed the attributes,
  1029. * so that fattr carries weak cache consistency data, then it may
  1030. * also update the ctime/mtime/change_attribute.
  1031. */
  1032. int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
  1033. {
  1034. struct nfs_inode *nfsi = NFS_I(inode);
  1035. loff_t cur_size, new_isize;
  1036. int data_unstable;
  1037. /* Do we hold a delegation? */
  1038. if (nfs_have_delegation(inode, FMODE_READ))
  1039. return 0;
  1040. /* Are we in the process of updating data on the server? */
  1041. data_unstable = nfs_caches_unstable(inode);
  1042. if (fattr->valid & NFS_ATTR_FATTR_V4) {
  1043. if ((fattr->valid & NFS_ATTR_PRE_CHANGE) != 0
  1044. && nfsi->change_attr == fattr->pre_change_attr)
  1045. nfsi->change_attr = fattr->change_attr;
  1046. if (nfsi->change_attr != fattr->change_attr) {
  1047. nfsi->flags |= NFS_INO_INVALID_ATTR;
  1048. if (!data_unstable)
  1049. nfsi->flags |= NFS_INO_REVAL_PAGECACHE;
  1050. }
  1051. }
  1052. if ((fattr->valid & NFS_ATTR_FATTR) == 0)
  1053. return 0;
  1054. /* Has the inode gone and changed behind our back? */
  1055. if (nfsi->fileid != fattr->fileid
  1056. || (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
  1057. return -EIO;
  1058. cur_size = i_size_read(inode);
  1059. new_isize = nfs_size_to_loff_t(fattr->size);
  1060. /* If we have atomic WCC data, we may update some attributes */
  1061. if ((fattr->valid & NFS_ATTR_WCC) != 0) {
  1062. if (timespec_equal(&inode->i_ctime, &fattr->pre_ctime))
  1063. memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
  1064. if (timespec_equal(&inode->i_mtime, &fattr->pre_mtime))
  1065. memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
  1066. }
  1067. /* Verify a few of the more important attributes */
  1068. if (!timespec_equal(&inode->i_mtime, &fattr->mtime)) {
  1069. nfsi->flags |= NFS_INO_INVALID_ATTR;
  1070. if (!data_unstable)
  1071. nfsi->flags |= NFS_INO_REVAL_PAGECACHE;
  1072. }
  1073. if (cur_size != new_isize) {
  1074. nfsi->flags |= NFS_INO_INVALID_ATTR;
  1075. if (nfsi->npages == 0)
  1076. nfsi->flags |= NFS_INO_REVAL_PAGECACHE;
  1077. }
  1078. /* Have any file permissions changed? */
  1079. if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)
  1080. || inode->i_uid != fattr->uid
  1081. || inode->i_gid != fattr->gid)
  1082. nfsi->flags |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
  1083. /* Has the link count changed? */
  1084. if (inode->i_nlink != fattr->nlink)
  1085. nfsi->flags |= NFS_INO_INVALID_ATTR;
  1086. if (!timespec_equal(&inode->i_atime, &fattr->atime))
  1087. nfsi->flags |= NFS_INO_INVALID_ATIME;
  1088. nfsi->read_cache_jiffies = fattr->timestamp;
  1089. return 0;
  1090. }
  1091. /*
  1092. * Many nfs protocol calls return the new file attributes after
  1093. * an operation. Here we update the inode to reflect the state
  1094. * of the server's inode.
  1095. *
  1096. * This is a bit tricky because we have to make sure all dirty pages
  1097. * have been sent off to the server before calling invalidate_inode_pages.
  1098. * To make sure no other process adds more write requests while we try
  1099. * our best to flush them, we make them sleep during the attribute refresh.
  1100. *
  1101. * A very similar scenario holds for the dir cache.
  1102. */
  1103. static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr, unsigned long verifier)
  1104. {
  1105. struct nfs_inode *nfsi = NFS_I(inode);
  1106. loff_t cur_isize, new_isize;
  1107. unsigned int invalid = 0;
  1108. int data_unstable;
  1109. dfprintk(VFS, "NFS: %s(%s/%ld ct=%d info=0x%x)\n",
  1110. __FUNCTION__, inode->i_sb->s_id, inode->i_ino,
  1111. atomic_read(&inode->i_count), fattr->valid);
  1112. if ((fattr->valid & NFS_ATTR_FATTR) == 0)
  1113. return 0;
  1114. if (nfsi->fileid != fattr->fileid) {
  1115. printk(KERN_ERR "%s: inode number mismatch\n"
  1116. "expected (%s/0x%Lx), got (%s/0x%Lx)\n",
  1117. __FUNCTION__,
  1118. inode->i_sb->s_id, (long long)nfsi->fileid,
  1119. inode->i_sb->s_id, (long long)fattr->fileid);
  1120. goto out_err;
  1121. }
  1122. /*
  1123. * Make sure the inode's type hasn't changed.
  1124. */
  1125. if ((inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
  1126. goto out_changed;
  1127. /*
  1128. * Update the read time so we don't revalidate too often.
  1129. */
  1130. nfsi->read_cache_jiffies = fattr->timestamp;
  1131. /* Are we racing with known updates of the metadata on the server? */
  1132. data_unstable = ! nfs_verify_change_attribute(inode, verifier);
  1133. /* Check if our cached file size is stale */
  1134. new_isize = nfs_size_to_loff_t(fattr->size);
  1135. cur_isize = i_size_read(inode);
  1136. if (new_isize != cur_isize) {
  1137. /* Do we perhaps have any outstanding writes? */
  1138. if (nfsi->npages == 0) {
  1139. /* No, but did we race with nfs_end_data_update()? */
  1140. if (verifier == nfsi->cache_change_attribute) {
  1141. inode->i_size = new_isize;
  1142. invalid |= NFS_INO_INVALID_DATA;
  1143. }
  1144. invalid |= NFS_INO_INVALID_ATTR;
  1145. } else if (new_isize > cur_isize) {
  1146. inode->i_size = new_isize;
  1147. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
  1148. }
  1149. dprintk("NFS: isize change on server for file %s/%ld\n",
  1150. inode->i_sb->s_id, inode->i_ino);
  1151. }
  1152. /* Check if the mtime agrees */
  1153. if (!timespec_equal(&inode->i_mtime, &fattr->mtime)) {
  1154. memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
  1155. dprintk("NFS: mtime change on server for file %s/%ld\n",
  1156. inode->i_sb->s_id, inode->i_ino);
  1157. if (!data_unstable)
  1158. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
  1159. }
  1160. if ((fattr->valid & NFS_ATTR_FATTR_V4)
  1161. && nfsi->change_attr != fattr->change_attr) {
  1162. dprintk("NFS: change_attr change on server for file %s/%ld\n",
  1163. inode->i_sb->s_id, inode->i_ino);
  1164. nfsi->change_attr = fattr->change_attr;
  1165. if (!data_unstable)
  1166. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  1167. }
  1168. /* If ctime has changed we should definitely clear access+acl caches */
  1169. if (!timespec_equal(&inode->i_ctime, &fattr->ctime)) {
  1170. if (!data_unstable)
  1171. invalid |= NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  1172. memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
  1173. }
  1174. memcpy(&inode->i_atime, &fattr->atime, sizeof(inode->i_atime));
  1175. if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO) ||
  1176. inode->i_uid != fattr->uid ||
  1177. inode->i_gid != fattr->gid)
  1178. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  1179. inode->i_mode = fattr->mode;
  1180. inode->i_nlink = fattr->nlink;
  1181. inode->i_uid = fattr->uid;
  1182. inode->i_gid = fattr->gid;
  1183. if (fattr->valid & (NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4)) {
  1184. /*
  1185. * report the blocks in 512byte units
  1186. */
  1187. inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
  1188. inode->i_blksize = inode->i_sb->s_blocksize;
  1189. } else {
  1190. inode->i_blocks = fattr->du.nfs2.blocks;
  1191. inode->i_blksize = fattr->du.nfs2.blocksize;
  1192. }
  1193. /* Update attrtimeo value if we're out of the unstable period */
  1194. if (invalid & NFS_INO_INVALID_ATTR) {
  1195. nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
  1196. nfsi->attrtimeo_timestamp = jiffies;
  1197. } else if (time_after(jiffies, nfsi->attrtimeo_timestamp+nfsi->attrtimeo)) {
  1198. if ((nfsi->attrtimeo <<= 1) > NFS_MAXATTRTIMEO(inode))
  1199. nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
  1200. nfsi->attrtimeo_timestamp = jiffies;
  1201. }
  1202. /* Don't invalidate the data if we were to blame */
  1203. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
  1204. || S_ISLNK(inode->i_mode)))
  1205. invalid &= ~NFS_INO_INVALID_DATA;
  1206. if (!nfs_have_delegation(inode, FMODE_READ))
  1207. nfsi->flags |= invalid;
  1208. return 0;
  1209. out_changed:
  1210. /*
  1211. * Big trouble! The inode has become a different object.
  1212. */
  1213. #ifdef NFS_PARANOIA
  1214. printk(KERN_DEBUG "%s: inode %ld mode changed, %07o to %07o\n",
  1215. __FUNCTION__, inode->i_ino, inode->i_mode, fattr->mode);
  1216. #endif
  1217. /*
  1218. * No need to worry about unhashing the dentry, as the
  1219. * lookup validation will know that the inode is bad.
  1220. * (But we fall through to invalidate the caches.)
  1221. */
  1222. nfs_invalidate_inode(inode);
  1223. out_err:
  1224. NFS_FLAGS(inode) |= NFS_INO_STALE;
  1225. return -ESTALE;
  1226. }
  1227. /*
  1228. * File system information
  1229. */
  1230. static int nfs_set_super(struct super_block *s, void *data)
  1231. {
  1232. s->s_fs_info = data;
  1233. return set_anon_super(s, data);
  1234. }
  1235. static int nfs_compare_super(struct super_block *sb, void *data)
  1236. {
  1237. struct nfs_server *server = data;
  1238. struct nfs_server *old = NFS_SB(sb);
  1239. if (old->addr.sin_addr.s_addr != server->addr.sin_addr.s_addr)
  1240. return 0;
  1241. if (old->addr.sin_port != server->addr.sin_port)
  1242. return 0;
  1243. return !nfs_compare_fh(&old->fh, &server->fh);
  1244. }
  1245. static struct super_block *nfs_get_sb(struct file_system_type *fs_type,
  1246. int flags, const char *dev_name, void *raw_data)
  1247. {
  1248. int error;
  1249. struct nfs_server *server = NULL;
  1250. struct super_block *s;
  1251. struct nfs_fh *root;
  1252. struct nfs_mount_data *data = raw_data;
  1253. s = ERR_PTR(-EINVAL);
  1254. if (data == NULL) {
  1255. dprintk("%s: missing data argument\n", __FUNCTION__);
  1256. goto out_err;
  1257. }
  1258. if (data->version <= 0 || data->version > NFS_MOUNT_VERSION) {
  1259. dprintk("%s: bad mount version\n", __FUNCTION__);
  1260. goto out_err;
  1261. }
  1262. switch (data->version) {
  1263. case 1:
  1264. data->namlen = 0;
  1265. case 2:
  1266. data->bsize = 0;
  1267. case 3:
  1268. if (data->flags & NFS_MOUNT_VER3) {
  1269. dprintk("%s: mount structure version %d does not support NFSv3\n",
  1270. __FUNCTION__,
  1271. data->version);
  1272. goto out_err;
  1273. }
  1274. data->root.size = NFS2_FHSIZE;
  1275. memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
  1276. case 4:
  1277. if (data->flags & NFS_MOUNT_SECFLAVOUR) {
  1278. dprintk("%s: mount structure version %d does not support strong security\n",
  1279. __FUNCTION__,
  1280. data->version);
  1281. goto out_err;
  1282. }
  1283. case 5:
  1284. memset(data->context, 0, sizeof(data->context));
  1285. }
  1286. #ifndef CONFIG_NFS_V3
  1287. /* If NFSv3 is not compiled in, return -EPROTONOSUPPORT */
  1288. s = ERR_PTR(-EPROTONOSUPPORT);
  1289. if (data->flags & NFS_MOUNT_VER3) {
  1290. dprintk("%s: NFSv3 not compiled into kernel\n", __FUNCTION__);
  1291. goto out_err;
  1292. }
  1293. #endif /* CONFIG_NFS_V3 */
  1294. s = ERR_PTR(-ENOMEM);
  1295. server = kmalloc(sizeof(struct nfs_server), GFP_KERNEL);
  1296. if (!server)
  1297. goto out_err;
  1298. memset(server, 0, sizeof(struct nfs_server));
  1299. /* Zero out the NFS state stuff */
  1300. init_nfsv4_state(server);
  1301. server->client = server->client_sys = server->client_acl = ERR_PTR(-EINVAL);
  1302. root = &server->fh;
  1303. if (data->flags & NFS_MOUNT_VER3)
  1304. root->size = data->root.size;
  1305. else
  1306. root->size = NFS2_FHSIZE;
  1307. s = ERR_PTR(-EINVAL);
  1308. if (root->size > sizeof(root->data)) {
  1309. dprintk("%s: invalid root filehandle\n", __FUNCTION__);
  1310. goto out_err;
  1311. }
  1312. memcpy(root->data, data->root.data, root->size);
  1313. /* We now require that the mount process passes the remote address */
  1314. memcpy(&server->addr, &data->addr, sizeof(server->addr));
  1315. if (server->addr.sin_addr.s_addr == INADDR_ANY) {
  1316. dprintk("%s: mount program didn't pass remote address!\n",
  1317. __FUNCTION__);
  1318. goto out_err;
  1319. }
  1320. /* Fire up rpciod if not yet running */
  1321. s = ERR_PTR(rpciod_up());
  1322. if (IS_ERR(s)) {
  1323. dprintk("%s: couldn't start rpciod! Error = %ld\n",
  1324. __FUNCTION__, PTR_ERR(s));
  1325. goto out_err;
  1326. }
  1327. s = sget(fs_type, nfs_compare_super, nfs_set_super, server);
  1328. if (IS_ERR(s) || s->s_root)
  1329. goto out_rpciod_down;
  1330. s->s_flags = flags;
  1331. error = nfs_fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
  1332. if (error) {
  1333. up_write(&s->s_umount);
  1334. deactivate_super(s);
  1335. return ERR_PTR(error);
  1336. }
  1337. s->s_flags |= MS_ACTIVE;
  1338. return s;
  1339. out_rpciod_down:
  1340. rpciod_down();
  1341. out_err:
  1342. kfree(server);
  1343. return s;
  1344. }
  1345. static void nfs_kill_super(struct super_block *s)
  1346. {
  1347. struct nfs_server *server = NFS_SB(s);
  1348. kill_anon_super(s);
  1349. if (!IS_ERR(server->client))
  1350. rpc_shutdown_client(server->client);
  1351. if (!IS_ERR(server->client_sys))
  1352. rpc_shutdown_client(server->client_sys);
  1353. if (!IS_ERR(server->client_acl))
  1354. rpc_shutdown_client(server->client_acl);
  1355. if (!(server->flags & NFS_MOUNT_NONLM))
  1356. lockd_down(); /* release rpc.lockd */
  1357. rpciod_down(); /* release rpciod */
  1358. if (server->hostname != NULL)
  1359. kfree(server->hostname);
  1360. kfree(server);
  1361. }
  1362. static struct file_system_type nfs_fs_type = {
  1363. .owner = THIS_MODULE,
  1364. .name = "nfs",
  1365. .get_sb = nfs_get_sb,
  1366. .kill_sb = nfs_kill_super,
  1367. .fs_flags = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
  1368. };
  1369. #ifdef CONFIG_NFS_V4
  1370. static void nfs4_clear_inode(struct inode *);
  1371. static struct super_operations nfs4_sops = {
  1372. .alloc_inode = nfs_alloc_inode,
  1373. .destroy_inode = nfs_destroy_inode,
  1374. .write_inode = nfs_write_inode,
  1375. .delete_inode = nfs_delete_inode,
  1376. .statfs = nfs_statfs,
  1377. .clear_inode = nfs4_clear_inode,
  1378. .umount_begin = nfs_umount_begin,
  1379. .show_options = nfs_show_options,
  1380. };
  1381. /*
  1382. * Clean out any remaining NFSv4 state that might be left over due
  1383. * to open() calls that passed nfs_atomic_lookup, but failed to call
  1384. * nfs_open().
  1385. */
  1386. static void nfs4_clear_inode(struct inode *inode)
  1387. {
  1388. struct nfs_inode *nfsi = NFS_I(inode);
  1389. /* If we are holding a delegation, return it! */
  1390. if (nfsi->delegation != NULL)
  1391. nfs_inode_return_delegation(inode);
  1392. /* First call standard NFS clear_inode() code */
  1393. nfs_clear_inode(inode);
  1394. /* Now clear out any remaining state */
  1395. while (!list_empty(&nfsi->open_states)) {
  1396. struct nfs4_state *state;
  1397. state = list_entry(nfsi->open_states.next,
  1398. struct nfs4_state,
  1399. inode_states);
  1400. dprintk("%s(%s/%Ld): found unclaimed NFSv4 state %p\n",
  1401. __FUNCTION__,
  1402. inode->i_sb->s_id,
  1403. (long long)NFS_FILEID(inode),
  1404. state);
  1405. BUG_ON(atomic_read(&state->count) != 1);
  1406. nfs4_close_state(state, state->state);
  1407. }
  1408. }
  1409. static int nfs4_fill_super(struct super_block *sb, struct nfs4_mount_data *data, int silent)
  1410. {
  1411. struct nfs_server *server;
  1412. struct nfs4_client *clp = NULL;
  1413. struct rpc_xprt *xprt = NULL;
  1414. struct rpc_clnt *clnt = NULL;
  1415. struct rpc_timeout timeparms;
  1416. rpc_authflavor_t authflavour;
  1417. int proto, err = -EIO;
  1418. sb->s_blocksize_bits = 0;
  1419. sb->s_blocksize = 0;
  1420. server = NFS_SB(sb);
  1421. if (data->rsize != 0)
  1422. server->rsize = nfs_block_size(data->rsize, NULL);
  1423. if (data->wsize != 0)
  1424. server->wsize = nfs_block_size(data->wsize, NULL);
  1425. server->flags = data->flags & NFS_MOUNT_FLAGMASK;
  1426. server->caps = NFS_CAP_ATOMIC_OPEN;
  1427. server->acregmin = data->acregmin*HZ;
  1428. server->acregmax = data->acregmax*HZ;
  1429. server->acdirmin = data->acdirmin*HZ;
  1430. server->acdirmax = data->acdirmax*HZ;
  1431. server->rpc_ops = &nfs_v4_clientops;
  1432. /* Initialize timeout values */
  1433. timeparms.to_initval = data->timeo * HZ / 10;
  1434. timeparms.to_retries = data->retrans;
  1435. timeparms.to_exponential = 1;
  1436. if (!timeparms.to_retries)
  1437. timeparms.to_retries = 5;
  1438. proto = data->proto;
  1439. /* Which IP protocol do we use? */
  1440. switch (proto) {
  1441. case IPPROTO_TCP:
  1442. timeparms.to_maxval = RPC_MAX_TCP_TIMEOUT;
  1443. if (!timeparms.to_initval)
  1444. timeparms.to_initval = 600 * HZ / 10;
  1445. break;
  1446. case IPPROTO_UDP:
  1447. timeparms.to_maxval = RPC_MAX_UDP_TIMEOUT;
  1448. if (!timeparms.to_initval)
  1449. timeparms.to_initval = 11 * HZ / 10;
  1450. break;
  1451. default:
  1452. return -EINVAL;
  1453. }
  1454. clp = nfs4_get_client(&server->addr.sin_addr);
  1455. if (!clp) {
  1456. dprintk("%s: failed to create NFS4 client.\n", __FUNCTION__);
  1457. return -EIO;
  1458. }
  1459. /* Now create transport and client */
  1460. authflavour = RPC_AUTH_UNIX;
  1461. if (data->auth_flavourlen != 0) {
  1462. if (data->auth_flavourlen != 1) {
  1463. dprintk("%s: Invalid number of RPC auth flavours %d.\n",
  1464. __FUNCTION__, data->auth_flavourlen);
  1465. err = -EINVAL;
  1466. goto out_fail;
  1467. }
  1468. if (copy_from_user(&authflavour, data->auth_flavours, sizeof(authflavour))) {
  1469. err = -EFAULT;
  1470. goto out_fail;
  1471. }
  1472. }
  1473. down_write(&clp->cl_sem);
  1474. if (IS_ERR(clp->cl_rpcclient)) {
  1475. xprt = xprt_create_proto(proto, &server->addr, &timeparms);
  1476. if (IS_ERR(xprt)) {
  1477. up_write(&clp->cl_sem);
  1478. err = PTR_ERR(xprt);
  1479. dprintk("%s: cannot create RPC transport. Error = %d\n",
  1480. __FUNCTION__, err);
  1481. goto out_fail;
  1482. }
  1483. clnt = rpc_create_client(xprt, server->hostname, &nfs_program,
  1484. server->rpc_ops->version, authflavour);
  1485. if (IS_ERR(clnt)) {
  1486. up_write(&clp->cl_sem);
  1487. err = PTR_ERR(clnt);
  1488. dprintk("%s: cannot create RPC client. Error = %d\n",
  1489. __FUNCTION__, err);
  1490. goto out_fail;
  1491. }
  1492. clnt->cl_intr = 1;
  1493. clnt->cl_softrtry = 1;
  1494. clnt->cl_chatty = 1;
  1495. clp->cl_rpcclient = clnt;
  1496. clp->cl_cred = rpcauth_lookupcred(clnt->cl_auth, 0);
  1497. if (IS_ERR(clp->cl_cred)) {
  1498. up_write(&clp->cl_sem);
  1499. err = PTR_ERR(clp->cl_cred);
  1500. clp->cl_cred = NULL;
  1501. goto out_fail;
  1502. }
  1503. memcpy(clp->cl_ipaddr, server->ip_addr, sizeof(clp->cl_ipaddr));
  1504. nfs_idmap_new(clp);
  1505. }
  1506. if (list_empty(&clp->cl_superblocks)) {
  1507. err = nfs4_init_client(clp);
  1508. if (err != 0) {
  1509. up_write(&clp->cl_sem);
  1510. goto out_fail;
  1511. }
  1512. }
  1513. list_add_tail(&server->nfs4_siblings, &clp->cl_superblocks);
  1514. clnt = rpc_clone_client(clp->cl_rpcclient);
  1515. if (!IS_ERR(clnt))
  1516. server->nfs4_state = clp;
  1517. up_write(&clp->cl_sem);
  1518. clp = NULL;
  1519. if (IS_ERR(clnt)) {
  1520. err = PTR_ERR(clnt);
  1521. dprintk("%s: cannot create RPC client. Error = %d\n",
  1522. __FUNCTION__, err);
  1523. return err;
  1524. }
  1525. server->client = clnt;
  1526. if (server->nfs4_state->cl_idmap == NULL) {
  1527. dprintk("%s: failed to create idmapper.\n", __FUNCTION__);
  1528. return -ENOMEM;
  1529. }
  1530. if (clnt->cl_auth->au_flavor != authflavour) {
  1531. struct rpc_auth *auth;
  1532. auth = rpcauth_create(authflavour, clnt);
  1533. if (IS_ERR(auth)) {
  1534. dprintk("%s: couldn't create credcache!\n", __FUNCTION__);
  1535. return PTR_ERR(auth);
  1536. }
  1537. }
  1538. sb->s_time_gran = 1;
  1539. sb->s_op = &nfs4_sops;
  1540. err = nfs_sb_init(sb, authflavour);
  1541. if (err == 0)
  1542. return 0;
  1543. out_fail:
  1544. if (clp)
  1545. nfs4_put_client(clp);
  1546. return err;
  1547. }
  1548. static int nfs4_compare_super(struct super_block *sb, void *data)
  1549. {
  1550. struct nfs_server *server = data;
  1551. struct nfs_server *old = NFS_SB(sb);
  1552. if (strcmp(server->hostname, old->hostname) != 0)
  1553. return 0;
  1554. if (strcmp(server->mnt_path, old->mnt_path) != 0)
  1555. return 0;
  1556. return 1;
  1557. }
  1558. static void *
  1559. nfs_copy_user_string(char *dst, struct nfs_string *src, int maxlen)
  1560. {
  1561. void *p = NULL;
  1562. if (!src->len)
  1563. return ERR_PTR(-EINVAL);
  1564. if (src->len < maxlen)
  1565. maxlen = src->len;
  1566. if (dst == NULL) {
  1567. p = dst = kmalloc(maxlen + 1, GFP_KERNEL);
  1568. if (p == NULL)
  1569. return ERR_PTR(-ENOMEM);
  1570. }
  1571. if (copy_from_user(dst, src->data, maxlen)) {
  1572. if (p != NULL)
  1573. kfree(p);
  1574. return ERR_PTR(-EFAULT);
  1575. }
  1576. dst[maxlen] = '\0';
  1577. return dst;
  1578. }
  1579. static struct super_block *nfs4_get_sb(struct file_system_type *fs_type,
  1580. int flags, const char *dev_name, void *raw_data)
  1581. {
  1582. int error;
  1583. struct nfs_server *server;
  1584. struct super_block *s;
  1585. struct nfs4_mount_data *data = raw_data;
  1586. void *p;
  1587. if (data == NULL) {
  1588. dprintk("%s: missing data argument\n", __FUNCTION__);
  1589. return ERR_PTR(-EINVAL);
  1590. }
  1591. if (data->version <= 0 || data->version > NFS4_MOUNT_VERSION) {
  1592. dprintk("%s: bad mount version\n", __FUNCTION__);
  1593. return ERR_PTR(-EINVAL);
  1594. }
  1595. server = kmalloc(sizeof(struct nfs_server), GFP_KERNEL);
  1596. if (!server)
  1597. return ERR_PTR(-ENOMEM);
  1598. memset(server, 0, sizeof(struct nfs_server));
  1599. /* Zero out the NFS state stuff */
  1600. init_nfsv4_state(server);
  1601. server->client = server->client_sys = server->client_acl = ERR_PTR(-EINVAL);
  1602. p = nfs_copy_user_string(NULL, &data->hostname, 256);
  1603. if (IS_ERR(p))
  1604. goto out_err;
  1605. server->hostname = p;
  1606. p = nfs_copy_user_string(NULL, &data->mnt_path, 1024);
  1607. if (IS_ERR(p))
  1608. goto out_err;
  1609. server->mnt_path = p;
  1610. p = nfs_copy_user_string(server->ip_addr, &data->client_addr,
  1611. sizeof(server->ip_addr) - 1);
  1612. if (IS_ERR(p))
  1613. goto out_err;
  1614. /* We now require that the mount process passes the remote address */
  1615. if (data->host_addrlen != sizeof(server->addr)) {
  1616. s = ERR_PTR(-EINVAL);
  1617. goto out_free;
  1618. }
  1619. if (copy_from_user(&server->addr, data->host_addr, sizeof(server->addr))) {
  1620. s = ERR_PTR(-EFAULT);
  1621. goto out_free;
  1622. }
  1623. if (server->addr.sin_family != AF_INET ||
  1624. server->addr.sin_addr.s_addr == INADDR_ANY) {
  1625. dprintk("%s: mount program didn't pass remote IP address!\n",
  1626. __FUNCTION__);
  1627. s = ERR_PTR(-EINVAL);
  1628. goto out_free;
  1629. }
  1630. /* Fire up rpciod if not yet running */
  1631. s = ERR_PTR(rpciod_up());
  1632. if (IS_ERR(s)) {
  1633. dprintk("%s: couldn't start rpciod! Error = %ld\n",
  1634. __FUNCTION__, PTR_ERR(s));
  1635. goto out_free;
  1636. }
  1637. s = sget(fs_type, nfs4_compare_super, nfs_set_super, server);
  1638. if (IS_ERR(s) || s->s_root)
  1639. goto out_free;
  1640. s->s_flags = flags;
  1641. error = nfs4_fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
  1642. if (error) {
  1643. up_write(&s->s_umount);
  1644. deactivate_super(s);
  1645. return ERR_PTR(error);
  1646. }
  1647. s->s_flags |= MS_ACTIVE;
  1648. return s;
  1649. out_err:
  1650. s = (struct super_block *)p;
  1651. out_free:
  1652. if (server->mnt_path)
  1653. kfree(server->mnt_path);
  1654. if (server->hostname)
  1655. kfree(server->hostname);
  1656. kfree(server);
  1657. return s;
  1658. }
  1659. static void nfs4_kill_super(struct super_block *sb)
  1660. {
  1661. struct nfs_server *server = NFS_SB(sb);
  1662. nfs_return_all_delegations(sb);
  1663. kill_anon_super(sb);
  1664. nfs4_renewd_prepare_shutdown(server);
  1665. if (server->client != NULL && !IS_ERR(server->client))
  1666. rpc_shutdown_client(server->client);
  1667. rpciod_down(); /* release rpciod */
  1668. destroy_nfsv4_state(server);
  1669. if (server->hostname != NULL)
  1670. kfree(server->hostname);
  1671. kfree(server);
  1672. }
  1673. static struct file_system_type nfs4_fs_type = {
  1674. .owner = THIS_MODULE,
  1675. .name = "nfs4",
  1676. .get_sb = nfs4_get_sb,
  1677. .kill_sb = nfs4_kill_super,
  1678. .fs_flags = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
  1679. };
  1680. #define nfs4_init_once(nfsi) \
  1681. do { \
  1682. INIT_LIST_HEAD(&(nfsi)->open_states); \
  1683. nfsi->delegation = NULL; \
  1684. nfsi->delegation_state = 0; \
  1685. init_rwsem(&nfsi->rwsem); \
  1686. } while(0)
  1687. #define register_nfs4fs() register_filesystem(&nfs4_fs_type)
  1688. #define unregister_nfs4fs() unregister_filesystem(&nfs4_fs_type)
  1689. #else
  1690. #define nfs4_init_once(nfsi) \
  1691. do { } while (0)
  1692. #define register_nfs4fs() (0)
  1693. #define unregister_nfs4fs()
  1694. #endif
  1695. extern int nfs_init_nfspagecache(void);
  1696. extern void nfs_destroy_nfspagecache(void);
  1697. extern int nfs_init_readpagecache(void);
  1698. extern void nfs_destroy_readpagecache(void);
  1699. extern int nfs_init_writepagecache(void);
  1700. extern void nfs_destroy_writepagecache(void);
  1701. #ifdef CONFIG_NFS_DIRECTIO
  1702. extern int nfs_init_directcache(void);
  1703. extern void nfs_destroy_directcache(void);
  1704. #endif
  1705. static kmem_cache_t * nfs_inode_cachep;
  1706. static struct inode *nfs_alloc_inode(struct super_block *sb)
  1707. {
  1708. struct nfs_inode *nfsi;
  1709. nfsi = (struct nfs_inode *)kmem_cache_alloc(nfs_inode_cachep, SLAB_KERNEL);
  1710. if (!nfsi)
  1711. return NULL;
  1712. nfsi->flags = 0;
  1713. #ifdef CONFIG_NFS_V3_ACL
  1714. nfsi->acl_access = ERR_PTR(-EAGAIN);
  1715. nfsi->acl_default = ERR_PTR(-EAGAIN);
  1716. #endif
  1717. #ifdef CONFIG_NFS_V4
  1718. nfsi->nfs4_acl = NULL;
  1719. #endif /* CONFIG_NFS_V4 */
  1720. return &nfsi->vfs_inode;
  1721. }
  1722. static void nfs_destroy_inode(struct inode *inode)
  1723. {
  1724. kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
  1725. }
  1726. static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
  1727. {
  1728. struct nfs_inode *nfsi = (struct nfs_inode *) foo;
  1729. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  1730. SLAB_CTOR_CONSTRUCTOR) {
  1731. inode_init_once(&nfsi->vfs_inode);
  1732. spin_lock_init(&nfsi->req_lock);
  1733. INIT_LIST_HEAD(&nfsi->dirty);
  1734. INIT_LIST_HEAD(&nfsi->commit);
  1735. INIT_LIST_HEAD(&nfsi->open_files);
  1736. INIT_RADIX_TREE(&nfsi->nfs_page_tree, GFP_ATOMIC);
  1737. atomic_set(&nfsi->data_updates, 0);
  1738. nfsi->ndirty = 0;
  1739. nfsi->ncommit = 0;
  1740. nfsi->npages = 0;
  1741. init_waitqueue_head(&nfsi->nfs_i_wait);
  1742. nfs4_init_once(nfsi);
  1743. }
  1744. }
  1745. static int nfs_init_inodecache(void)
  1746. {
  1747. nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
  1748. sizeof(struct nfs_inode),
  1749. 0, SLAB_RECLAIM_ACCOUNT,
  1750. init_once, NULL);
  1751. if (nfs_inode_cachep == NULL)
  1752. return -ENOMEM;
  1753. return 0;
  1754. }
  1755. static void nfs_destroy_inodecache(void)
  1756. {
  1757. if (kmem_cache_destroy(nfs_inode_cachep))
  1758. printk(KERN_INFO "nfs_inode_cache: not all structures were freed\n");
  1759. }
  1760. /*
  1761. * Initialize NFS
  1762. */
  1763. static int __init init_nfs_fs(void)
  1764. {
  1765. int err;
  1766. err = nfs_init_nfspagecache();
  1767. if (err)
  1768. goto out4;
  1769. err = nfs_init_inodecache();
  1770. if (err)
  1771. goto out3;
  1772. err = nfs_init_readpagecache();
  1773. if (err)
  1774. goto out2;
  1775. err = nfs_init_writepagecache();
  1776. if (err)
  1777. goto out1;
  1778. #ifdef CONFIG_NFS_DIRECTIO
  1779. err = nfs_init_directcache();
  1780. if (err)
  1781. goto out0;
  1782. #endif
  1783. #ifdef CONFIG_PROC_FS
  1784. rpc_proc_register(&nfs_rpcstat);
  1785. #endif
  1786. err = register_filesystem(&nfs_fs_type);
  1787. if (err)
  1788. goto out;
  1789. if ((err = register_nfs4fs()) != 0)
  1790. goto out;
  1791. return 0;
  1792. out:
  1793. #ifdef CONFIG_PROC_FS
  1794. rpc_proc_unregister("nfs");
  1795. #endif
  1796. nfs_destroy_writepagecache();
  1797. #ifdef CONFIG_NFS_DIRECTIO
  1798. out0:
  1799. nfs_destroy_directcache();
  1800. #endif
  1801. out1:
  1802. nfs_destroy_readpagecache();
  1803. out2:
  1804. nfs_destroy_inodecache();
  1805. out3:
  1806. nfs_destroy_nfspagecache();
  1807. out4:
  1808. return err;
  1809. }
  1810. static void __exit exit_nfs_fs(void)
  1811. {
  1812. #ifdef CONFIG_NFS_DIRECTIO
  1813. nfs_destroy_directcache();
  1814. #endif
  1815. nfs_destroy_writepagecache();
  1816. nfs_destroy_readpagecache();
  1817. nfs_destroy_inodecache();
  1818. nfs_destroy_nfspagecache();
  1819. #ifdef CONFIG_PROC_FS
  1820. rpc_proc_unregister("nfs");
  1821. #endif
  1822. unregister_filesystem(&nfs_fs_type);
  1823. unregister_nfs4fs();
  1824. }
  1825. /* Not quite true; I just maintain it */
  1826. MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
  1827. MODULE_LICENSE("GPL");
  1828. module_init(init_nfs_fs)
  1829. module_exit(exit_nfs_fs)