inode.c 54 KB

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