inode.c 57 KB

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