inode.c 54 KB

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