inode.c 63 KB

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