inode.c 53 KB

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