inode.c 63 KB

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