inode.c 60 KB

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