inode.c 57 KB

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