inode.c 54 KB

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