inode.c 54 KB

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