inode.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352
  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 super_block *, 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 super_block *sb, struct kstatfs *buf)
  466. {
  467. struct nfs_server *server = NFS_SB(sb);
  468. unsigned char blockbits;
  469. unsigned long blockres;
  470. struct nfs_fh *rootfh = NFS_FH(sb->s_root->d_inode);
  471. struct nfs_fattr fattr;
  472. struct nfs_fsstat res = {
  473. .fattr = &fattr,
  474. };
  475. int error;
  476. lock_kernel();
  477. error = server->rpc_ops->statfs(server, rootfh, &res);
  478. buf->f_type = NFS_SUPER_MAGIC;
  479. if (error < 0)
  480. goto out_err;
  481. /*
  482. * Current versions of glibc do not correctly handle the
  483. * case where f_frsize != f_bsize. Eventually we want to
  484. * report the value of wtmult in this field.
  485. */
  486. buf->f_frsize = sb->s_blocksize;
  487. /*
  488. * On most *nix systems, f_blocks, f_bfree, and f_bavail
  489. * are reported in units of f_frsize. Linux hasn't had
  490. * an f_frsize field in its statfs struct until recently,
  491. * thus historically Linux's sys_statfs reports these
  492. * fields in units of f_bsize.
  493. */
  494. buf->f_bsize = sb->s_blocksize;
  495. blockbits = sb->s_blocksize_bits;
  496. blockres = (1 << blockbits) - 1;
  497. buf->f_blocks = (res.tbytes + blockres) >> blockbits;
  498. buf->f_bfree = (res.fbytes + blockres) >> blockbits;
  499. buf->f_bavail = (res.abytes + blockres) >> blockbits;
  500. buf->f_files = res.tfiles;
  501. buf->f_ffree = res.afiles;
  502. buf->f_namelen = server->namelen;
  503. out:
  504. unlock_kernel();
  505. return 0;
  506. out_err:
  507. dprintk("%s: statfs error = %d\n", __FUNCTION__, -error);
  508. buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1;
  509. goto out;
  510. }
  511. static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss, int showdefaults)
  512. {
  513. static struct proc_nfs_info {
  514. int flag;
  515. char *str;
  516. char *nostr;
  517. } nfs_info[] = {
  518. { NFS_MOUNT_SOFT, ",soft", ",hard" },
  519. { NFS_MOUNT_INTR, ",intr", "" },
  520. { NFS_MOUNT_NOCTO, ",nocto", "" },
  521. { NFS_MOUNT_NOAC, ",noac", "" },
  522. { NFS_MOUNT_NONLM, ",nolock", "" },
  523. { NFS_MOUNT_NOACL, ",noacl", "" },
  524. { 0, NULL, NULL }
  525. };
  526. struct proc_nfs_info *nfs_infop;
  527. char buf[12];
  528. char *proto;
  529. seq_printf(m, ",vers=%d", nfss->rpc_ops->version);
  530. seq_printf(m, ",rsize=%d", nfss->rsize);
  531. seq_printf(m, ",wsize=%d", nfss->wsize);
  532. if (nfss->acregmin != 3*HZ || showdefaults)
  533. seq_printf(m, ",acregmin=%d", nfss->acregmin/HZ);
  534. if (nfss->acregmax != 60*HZ || showdefaults)
  535. seq_printf(m, ",acregmax=%d", nfss->acregmax/HZ);
  536. if (nfss->acdirmin != 30*HZ || showdefaults)
  537. seq_printf(m, ",acdirmin=%d", nfss->acdirmin/HZ);
  538. if (nfss->acdirmax != 60*HZ || showdefaults)
  539. seq_printf(m, ",acdirmax=%d", nfss->acdirmax/HZ);
  540. for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
  541. if (nfss->flags & nfs_infop->flag)
  542. seq_puts(m, nfs_infop->str);
  543. else
  544. seq_puts(m, nfs_infop->nostr);
  545. }
  546. switch (nfss->client->cl_xprt->prot) {
  547. case IPPROTO_TCP:
  548. proto = "tcp";
  549. break;
  550. case IPPROTO_UDP:
  551. proto = "udp";
  552. break;
  553. default:
  554. snprintf(buf, sizeof(buf), "%u", nfss->client->cl_xprt->prot);
  555. proto = buf;
  556. }
  557. seq_printf(m, ",proto=%s", proto);
  558. seq_printf(m, ",timeo=%lu", 10U * nfss->retrans_timeo / HZ);
  559. seq_printf(m, ",retrans=%u", nfss->retrans_count);
  560. }
  561. static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
  562. {
  563. struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
  564. nfs_show_mount_options(m, nfss, 0);
  565. seq_puts(m, ",addr=");
  566. seq_escape(m, nfss->hostname, " \t\n\\");
  567. return 0;
  568. }
  569. static int nfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
  570. {
  571. int i, cpu;
  572. struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
  573. struct rpc_auth *auth = nfss->client->cl_auth;
  574. struct nfs_iostats totals = { };
  575. seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
  576. /*
  577. * Display all mount option settings
  578. */
  579. seq_printf(m, "\n\topts:\t");
  580. seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? "ro" : "rw");
  581. seq_puts(m, mnt->mnt_sb->s_flags & MS_SYNCHRONOUS ? ",sync" : "");
  582. seq_puts(m, mnt->mnt_sb->s_flags & MS_NOATIME ? ",noatime" : "");
  583. seq_puts(m, mnt->mnt_sb->s_flags & MS_NODIRATIME ? ",nodiratime" : "");
  584. nfs_show_mount_options(m, nfss, 1);
  585. seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
  586. seq_printf(m, "\n\tcaps:\t");
  587. seq_printf(m, "caps=0x%x", nfss->caps);
  588. seq_printf(m, ",wtmult=%d", nfss->wtmult);
  589. seq_printf(m, ",dtsize=%d", nfss->dtsize);
  590. seq_printf(m, ",bsize=%d", nfss->bsize);
  591. seq_printf(m, ",namelen=%d", nfss->namelen);
  592. #ifdef CONFIG_NFS_V4
  593. if (nfss->rpc_ops->version == 4) {
  594. seq_printf(m, "\n\tnfsv4:\t");
  595. seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
  596. seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
  597. seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
  598. }
  599. #endif
  600. /*
  601. * Display security flavor in effect for this mount
  602. */
  603. seq_printf(m, "\n\tsec:\tflavor=%d", auth->au_ops->au_flavor);
  604. if (auth->au_flavor)
  605. seq_printf(m, ",pseudoflavor=%d", auth->au_flavor);
  606. /*
  607. * Display superblock I/O counters
  608. */
  609. for_each_possible_cpu(cpu) {
  610. struct nfs_iostats *stats;
  611. preempt_disable();
  612. stats = per_cpu_ptr(nfss->io_stats, cpu);
  613. for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
  614. totals.events[i] += stats->events[i];
  615. for (i = 0; i < __NFSIOS_BYTESMAX; i++)
  616. totals.bytes[i] += stats->bytes[i];
  617. preempt_enable();
  618. }
  619. seq_printf(m, "\n\tevents:\t");
  620. for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
  621. seq_printf(m, "%lu ", totals.events[i]);
  622. seq_printf(m, "\n\tbytes:\t");
  623. for (i = 0; i < __NFSIOS_BYTESMAX; i++)
  624. seq_printf(m, "%Lu ", totals.bytes[i]);
  625. seq_printf(m, "\n");
  626. rpc_print_iostats(m, nfss->client);
  627. return 0;
  628. }
  629. /**
  630. * nfs_sync_mapping - helper to flush all mmapped dirty data to disk
  631. */
  632. int nfs_sync_mapping(struct address_space *mapping)
  633. {
  634. int ret;
  635. if (mapping->nrpages == 0)
  636. return 0;
  637. unmap_mapping_range(mapping, 0, 0, 0);
  638. ret = filemap_write_and_wait(mapping);
  639. if (ret != 0)
  640. goto out;
  641. ret = nfs_wb_all(mapping->host);
  642. out:
  643. return ret;
  644. }
  645. /*
  646. * Invalidate the local caches
  647. */
  648. static void nfs_zap_caches_locked(struct inode *inode)
  649. {
  650. struct nfs_inode *nfsi = NFS_I(inode);
  651. int mode = inode->i_mode;
  652. nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
  653. NFS_ATTRTIMEO(inode) = NFS_MINATTRTIMEO(inode);
  654. NFS_ATTRTIMEO_UPDATE(inode) = jiffies;
  655. memset(NFS_COOKIEVERF(inode), 0, sizeof(NFS_COOKIEVERF(inode)));
  656. if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))
  657. nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL|NFS_INO_REVAL_PAGECACHE;
  658. else
  659. nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL|NFS_INO_REVAL_PAGECACHE;
  660. }
  661. void nfs_zap_caches(struct inode *inode)
  662. {
  663. spin_lock(&inode->i_lock);
  664. nfs_zap_caches_locked(inode);
  665. spin_unlock(&inode->i_lock);
  666. }
  667. static void nfs_zap_acl_cache(struct inode *inode)
  668. {
  669. void (*clear_acl_cache)(struct inode *);
  670. clear_acl_cache = NFS_PROTO(inode)->clear_acl_cache;
  671. if (clear_acl_cache != NULL)
  672. clear_acl_cache(inode);
  673. spin_lock(&inode->i_lock);
  674. NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_ACL;
  675. spin_unlock(&inode->i_lock);
  676. }
  677. /*
  678. * Invalidate, but do not unhash, the inode.
  679. * NB: must be called with inode->i_lock held!
  680. */
  681. static void nfs_invalidate_inode(struct inode *inode)
  682. {
  683. set_bit(NFS_INO_STALE, &NFS_FLAGS(inode));
  684. nfs_zap_caches_locked(inode);
  685. }
  686. struct nfs_find_desc {
  687. struct nfs_fh *fh;
  688. struct nfs_fattr *fattr;
  689. };
  690. /*
  691. * In NFSv3 we can have 64bit inode numbers. In order to support
  692. * this, and re-exported directories (also seen in NFSv2)
  693. * we are forced to allow 2 different inodes to have the same
  694. * i_ino.
  695. */
  696. static int
  697. nfs_find_actor(struct inode *inode, void *opaque)
  698. {
  699. struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
  700. struct nfs_fh *fh = desc->fh;
  701. struct nfs_fattr *fattr = desc->fattr;
  702. if (NFS_FILEID(inode) != fattr->fileid)
  703. return 0;
  704. if (nfs_compare_fh(NFS_FH(inode), fh))
  705. return 0;
  706. if (is_bad_inode(inode) || NFS_STALE(inode))
  707. return 0;
  708. return 1;
  709. }
  710. static int
  711. nfs_init_locked(struct inode *inode, void *opaque)
  712. {
  713. struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
  714. struct nfs_fattr *fattr = desc->fattr;
  715. NFS_FILEID(inode) = fattr->fileid;
  716. nfs_copy_fh(NFS_FH(inode), desc->fh);
  717. return 0;
  718. }
  719. /* Don't use READDIRPLUS on directories that we believe are too large */
  720. #define NFS_LIMIT_READDIRPLUS (8*PAGE_SIZE)
  721. /*
  722. * This is our front-end to iget that looks up inodes by file handle
  723. * instead of inode number.
  724. */
  725. struct inode *
  726. nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr)
  727. {
  728. struct nfs_find_desc desc = {
  729. .fh = fh,
  730. .fattr = fattr
  731. };
  732. struct inode *inode = ERR_PTR(-ENOENT);
  733. unsigned long hash;
  734. if ((fattr->valid & NFS_ATTR_FATTR) == 0)
  735. goto out_no_inode;
  736. if (!fattr->nlink) {
  737. printk("NFS: Buggy server - nlink == 0!\n");
  738. goto out_no_inode;
  739. }
  740. hash = nfs_fattr_to_ino_t(fattr);
  741. inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc);
  742. if (inode == NULL) {
  743. inode = ERR_PTR(-ENOMEM);
  744. goto out_no_inode;
  745. }
  746. if (inode->i_state & I_NEW) {
  747. struct nfs_inode *nfsi = NFS_I(inode);
  748. /* We set i_ino for the few things that still rely on it,
  749. * such as stat(2) */
  750. inode->i_ino = hash;
  751. /* We can't support update_atime(), since the server will reset it */
  752. inode->i_flags |= S_NOATIME|S_NOCMTIME;
  753. inode->i_mode = fattr->mode;
  754. /* Why so? Because we want revalidate for devices/FIFOs, and
  755. * that's precisely what we have in nfs_file_inode_operations.
  756. */
  757. inode->i_op = NFS_SB(sb)->rpc_ops->file_inode_ops;
  758. if (S_ISREG(inode->i_mode)) {
  759. inode->i_fop = &nfs_file_operations;
  760. inode->i_data.a_ops = &nfs_file_aops;
  761. inode->i_data.backing_dev_info = &NFS_SB(sb)->backing_dev_info;
  762. } else if (S_ISDIR(inode->i_mode)) {
  763. inode->i_op = NFS_SB(sb)->rpc_ops->dir_inode_ops;
  764. inode->i_fop = &nfs_dir_operations;
  765. if (nfs_server_capable(inode, NFS_CAP_READDIRPLUS)
  766. && fattr->size <= NFS_LIMIT_READDIRPLUS)
  767. set_bit(NFS_INO_ADVISE_RDPLUS, &NFS_FLAGS(inode));
  768. } else if (S_ISLNK(inode->i_mode))
  769. inode->i_op = &nfs_symlink_inode_operations;
  770. else
  771. init_special_inode(inode, inode->i_mode, fattr->rdev);
  772. nfsi->read_cache_jiffies = fattr->time_start;
  773. nfsi->last_updated = jiffies;
  774. inode->i_atime = fattr->atime;
  775. inode->i_mtime = fattr->mtime;
  776. inode->i_ctime = fattr->ctime;
  777. if (fattr->valid & NFS_ATTR_FATTR_V4)
  778. nfsi->change_attr = fattr->change_attr;
  779. inode->i_size = nfs_size_to_loff_t(fattr->size);
  780. inode->i_nlink = fattr->nlink;
  781. inode->i_uid = fattr->uid;
  782. inode->i_gid = fattr->gid;
  783. if (fattr->valid & (NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4)) {
  784. /*
  785. * report the blocks in 512byte units
  786. */
  787. inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
  788. inode->i_blksize = inode->i_sb->s_blocksize;
  789. } else {
  790. inode->i_blocks = fattr->du.nfs2.blocks;
  791. inode->i_blksize = fattr->du.nfs2.blocksize;
  792. }
  793. nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
  794. nfsi->attrtimeo_timestamp = jiffies;
  795. memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
  796. nfsi->cache_access.cred = NULL;
  797. unlock_new_inode(inode);
  798. } else
  799. nfs_refresh_inode(inode, fattr);
  800. dprintk("NFS: nfs_fhget(%s/%Ld ct=%d)\n",
  801. inode->i_sb->s_id,
  802. (long long)NFS_FILEID(inode),
  803. atomic_read(&inode->i_count));
  804. out:
  805. return inode;
  806. out_no_inode:
  807. dprintk("nfs_fhget: iget failed with error %ld\n", PTR_ERR(inode));
  808. goto out;
  809. }
  810. #define NFS_VALID_ATTRS (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE|ATTR_ATIME|ATTR_ATIME_SET|ATTR_MTIME|ATTR_MTIME_SET)
  811. int
  812. nfs_setattr(struct dentry *dentry, struct iattr *attr)
  813. {
  814. struct inode *inode = dentry->d_inode;
  815. struct nfs_fattr fattr;
  816. int error;
  817. nfs_inc_stats(inode, NFSIOS_VFSSETATTR);
  818. if (attr->ia_valid & ATTR_SIZE) {
  819. if (!S_ISREG(inode->i_mode) || attr->ia_size == i_size_read(inode))
  820. attr->ia_valid &= ~ATTR_SIZE;
  821. }
  822. /* Optimization: if the end result is no change, don't RPC */
  823. attr->ia_valid &= NFS_VALID_ATTRS;
  824. if (attr->ia_valid == 0)
  825. return 0;
  826. lock_kernel();
  827. nfs_begin_data_update(inode);
  828. /* Write all dirty data */
  829. filemap_write_and_wait(inode->i_mapping);
  830. nfs_wb_all(inode);
  831. /*
  832. * Return any delegations if we're going to change ACLs
  833. */
  834. if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
  835. nfs_inode_return_delegation(inode);
  836. error = NFS_PROTO(inode)->setattr(dentry, &fattr, attr);
  837. if (error == 0)
  838. nfs_refresh_inode(inode, &fattr);
  839. nfs_end_data_update(inode);
  840. unlock_kernel();
  841. return error;
  842. }
  843. /**
  844. * nfs_setattr_update_inode - Update inode metadata after a setattr call.
  845. * @inode: pointer to struct inode
  846. * @attr: pointer to struct iattr
  847. *
  848. * Note: we do this in the *proc.c in order to ensure that
  849. * it works for things like exclusive creates too.
  850. */
  851. void nfs_setattr_update_inode(struct inode *inode, struct iattr *attr)
  852. {
  853. if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) {
  854. if ((attr->ia_valid & ATTR_MODE) != 0) {
  855. int mode = attr->ia_mode & S_IALLUGO;
  856. mode |= inode->i_mode & ~S_IALLUGO;
  857. inode->i_mode = mode;
  858. }
  859. if ((attr->ia_valid & ATTR_UID) != 0)
  860. inode->i_uid = attr->ia_uid;
  861. if ((attr->ia_valid & ATTR_GID) != 0)
  862. inode->i_gid = attr->ia_gid;
  863. spin_lock(&inode->i_lock);
  864. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  865. spin_unlock(&inode->i_lock);
  866. }
  867. if ((attr->ia_valid & ATTR_SIZE) != 0) {
  868. nfs_inc_stats(inode, NFSIOS_SETATTRTRUNC);
  869. inode->i_size = attr->ia_size;
  870. vmtruncate(inode, attr->ia_size);
  871. }
  872. }
  873. static int nfs_wait_schedule(void *word)
  874. {
  875. if (signal_pending(current))
  876. return -ERESTARTSYS;
  877. schedule();
  878. return 0;
  879. }
  880. /*
  881. * Wait for the inode to get unlocked.
  882. */
  883. static int nfs_wait_on_inode(struct inode *inode)
  884. {
  885. struct rpc_clnt *clnt = NFS_CLIENT(inode);
  886. struct nfs_inode *nfsi = NFS_I(inode);
  887. sigset_t oldmask;
  888. int error;
  889. rpc_clnt_sigmask(clnt, &oldmask);
  890. error = wait_on_bit_lock(&nfsi->flags, NFS_INO_REVALIDATING,
  891. nfs_wait_schedule, TASK_INTERRUPTIBLE);
  892. rpc_clnt_sigunmask(clnt, &oldmask);
  893. return error;
  894. }
  895. static void nfs_wake_up_inode(struct inode *inode)
  896. {
  897. struct nfs_inode *nfsi = NFS_I(inode);
  898. clear_bit(NFS_INO_REVALIDATING, &nfsi->flags);
  899. smp_mb__after_clear_bit();
  900. wake_up_bit(&nfsi->flags, NFS_INO_REVALIDATING);
  901. }
  902. int nfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  903. {
  904. struct inode *inode = dentry->d_inode;
  905. int need_atime = NFS_I(inode)->cache_validity & NFS_INO_INVALID_ATIME;
  906. int err;
  907. /* Flush out writes to the server in order to update c/mtime */
  908. nfs_sync_inode_wait(inode, 0, 0, FLUSH_NOCOMMIT);
  909. /*
  910. * We may force a getattr if the user cares about atime.
  911. *
  912. * Note that we only have to check the vfsmount flags here:
  913. * - NFS always sets S_NOATIME by so checking it would give a
  914. * bogus result
  915. * - NFS never sets MS_NOATIME or MS_NODIRATIME so there is
  916. * no point in checking those.
  917. */
  918. if ((mnt->mnt_flags & MNT_NOATIME) ||
  919. ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
  920. need_atime = 0;
  921. if (need_atime)
  922. err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  923. else
  924. err = nfs_revalidate_inode(NFS_SERVER(inode), inode);
  925. if (!err)
  926. generic_fillattr(inode, stat);
  927. return err;
  928. }
  929. static struct nfs_open_context *alloc_nfs_open_context(struct vfsmount *mnt, struct dentry *dentry, struct rpc_cred *cred)
  930. {
  931. struct nfs_open_context *ctx;
  932. ctx = (struct nfs_open_context *)kmalloc(sizeof(*ctx), GFP_KERNEL);
  933. if (ctx != NULL) {
  934. atomic_set(&ctx->count, 1);
  935. ctx->dentry = dget(dentry);
  936. ctx->vfsmnt = mntget(mnt);
  937. ctx->cred = get_rpccred(cred);
  938. ctx->state = NULL;
  939. ctx->lockowner = current->files;
  940. ctx->error = 0;
  941. ctx->dir_cookie = 0;
  942. }
  943. return ctx;
  944. }
  945. struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx)
  946. {
  947. if (ctx != NULL)
  948. atomic_inc(&ctx->count);
  949. return ctx;
  950. }
  951. void put_nfs_open_context(struct nfs_open_context *ctx)
  952. {
  953. if (atomic_dec_and_test(&ctx->count)) {
  954. if (!list_empty(&ctx->list)) {
  955. struct inode *inode = ctx->dentry->d_inode;
  956. spin_lock(&inode->i_lock);
  957. list_del(&ctx->list);
  958. spin_unlock(&inode->i_lock);
  959. }
  960. if (ctx->state != NULL)
  961. nfs4_close_state(ctx->state, ctx->mode);
  962. if (ctx->cred != NULL)
  963. put_rpccred(ctx->cred);
  964. dput(ctx->dentry);
  965. mntput(ctx->vfsmnt);
  966. kfree(ctx);
  967. }
  968. }
  969. /*
  970. * Ensure that mmap has a recent RPC credential for use when writing out
  971. * shared pages
  972. */
  973. static void nfs_file_set_open_context(struct file *filp, struct nfs_open_context *ctx)
  974. {
  975. struct inode *inode = filp->f_dentry->d_inode;
  976. struct nfs_inode *nfsi = NFS_I(inode);
  977. filp->private_data = get_nfs_open_context(ctx);
  978. spin_lock(&inode->i_lock);
  979. list_add(&ctx->list, &nfsi->open_files);
  980. spin_unlock(&inode->i_lock);
  981. }
  982. /*
  983. * Given an inode, search for an open context with the desired characteristics
  984. */
  985. struct nfs_open_context *nfs_find_open_context(struct inode *inode, struct rpc_cred *cred, int mode)
  986. {
  987. struct nfs_inode *nfsi = NFS_I(inode);
  988. struct nfs_open_context *pos, *ctx = NULL;
  989. spin_lock(&inode->i_lock);
  990. list_for_each_entry(pos, &nfsi->open_files, list) {
  991. if (cred != NULL && pos->cred != cred)
  992. continue;
  993. if ((pos->mode & mode) == mode) {
  994. ctx = get_nfs_open_context(pos);
  995. break;
  996. }
  997. }
  998. spin_unlock(&inode->i_lock);
  999. return ctx;
  1000. }
  1001. static void nfs_file_clear_open_context(struct file *filp)
  1002. {
  1003. struct inode *inode = filp->f_dentry->d_inode;
  1004. struct nfs_open_context *ctx = (struct nfs_open_context *)filp->private_data;
  1005. if (ctx) {
  1006. filp->private_data = NULL;
  1007. spin_lock(&inode->i_lock);
  1008. list_move_tail(&ctx->list, &NFS_I(inode)->open_files);
  1009. spin_unlock(&inode->i_lock);
  1010. put_nfs_open_context(ctx);
  1011. }
  1012. }
  1013. /*
  1014. * These allocate and release file read/write context information.
  1015. */
  1016. int nfs_open(struct inode *inode, struct file *filp)
  1017. {
  1018. struct nfs_open_context *ctx;
  1019. struct rpc_cred *cred;
  1020. cred = rpcauth_lookupcred(NFS_CLIENT(inode)->cl_auth, 0);
  1021. if (IS_ERR(cred))
  1022. return PTR_ERR(cred);
  1023. ctx = alloc_nfs_open_context(filp->f_vfsmnt, filp->f_dentry, cred);
  1024. put_rpccred(cred);
  1025. if (ctx == NULL)
  1026. return -ENOMEM;
  1027. ctx->mode = filp->f_mode;
  1028. nfs_file_set_open_context(filp, ctx);
  1029. put_nfs_open_context(ctx);
  1030. return 0;
  1031. }
  1032. int nfs_release(struct inode *inode, struct file *filp)
  1033. {
  1034. nfs_file_clear_open_context(filp);
  1035. return 0;
  1036. }
  1037. /*
  1038. * This function is called whenever some part of NFS notices that
  1039. * the cached attributes have to be refreshed.
  1040. */
  1041. int
  1042. __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
  1043. {
  1044. int status = -ESTALE;
  1045. struct nfs_fattr fattr;
  1046. struct nfs_inode *nfsi = NFS_I(inode);
  1047. dfprintk(PAGECACHE, "NFS: revalidating (%s/%Ld)\n",
  1048. inode->i_sb->s_id, (long long)NFS_FILEID(inode));
  1049. nfs_inc_stats(inode, NFSIOS_INODEREVALIDATE);
  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_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. if (nfsi->cache_validity & NFS_INO_INVALID_ACL)
  1091. nfs_zap_acl_cache(inode);
  1092. dfprintk(PAGECACHE, "NFS: (%s/%Ld) revalidation complete\n",
  1093. inode->i_sb->s_id,
  1094. (long long)NFS_FILEID(inode));
  1095. out:
  1096. nfs_wake_up_inode(inode);
  1097. out_nowait:
  1098. unlock_kernel();
  1099. return status;
  1100. }
  1101. int nfs_attribute_timeout(struct inode *inode)
  1102. {
  1103. struct nfs_inode *nfsi = NFS_I(inode);
  1104. if (nfs_have_delegation(inode, FMODE_READ))
  1105. return 0;
  1106. return time_after(jiffies, nfsi->read_cache_jiffies+nfsi->attrtimeo);
  1107. }
  1108. /**
  1109. * nfs_revalidate_inode - Revalidate the inode attributes
  1110. * @server - pointer to nfs_server struct
  1111. * @inode - pointer to inode struct
  1112. *
  1113. * Updates inode attribute information by retrieving the data from the server.
  1114. */
  1115. int nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
  1116. {
  1117. if (!(NFS_I(inode)->cache_validity & NFS_INO_INVALID_ATTR)
  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. int nfs_revalidate_mapping(struct inode *inode, struct address_space *mapping)
  1128. {
  1129. struct nfs_inode *nfsi = NFS_I(inode);
  1130. int ret = 0;
  1131. if (NFS_STALE(inode))
  1132. ret = -ESTALE;
  1133. if ((nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
  1134. || nfs_attribute_timeout(inode))
  1135. ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  1136. if (nfsi->cache_validity & NFS_INO_INVALID_DATA) {
  1137. nfs_inc_stats(inode, NFSIOS_DATAINVALIDATE);
  1138. if (S_ISREG(inode->i_mode))
  1139. nfs_sync_mapping(mapping);
  1140. invalidate_inode_pages2(mapping);
  1141. spin_lock(&inode->i_lock);
  1142. nfsi->cache_validity &= ~NFS_INO_INVALID_DATA;
  1143. if (S_ISDIR(inode->i_mode)) {
  1144. memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
  1145. /* This ensures we revalidate child dentries */
  1146. nfsi->cache_change_attribute = jiffies;
  1147. }
  1148. spin_unlock(&inode->i_lock);
  1149. dfprintk(PAGECACHE, "NFS: (%s/%Ld) data cache invalidated\n",
  1150. inode->i_sb->s_id,
  1151. (long long)NFS_FILEID(inode));
  1152. }
  1153. return ret;
  1154. }
  1155. /**
  1156. * nfs_begin_data_update
  1157. * @inode - pointer to inode
  1158. * Declare that a set of operations will update file data on the server
  1159. */
  1160. void nfs_begin_data_update(struct inode *inode)
  1161. {
  1162. atomic_inc(&NFS_I(inode)->data_updates);
  1163. }
  1164. /**
  1165. * nfs_end_data_update
  1166. * @inode - pointer to inode
  1167. * Declare end of the operations that will update file data
  1168. * This will mark the inode as immediately needing revalidation
  1169. * of its attribute cache.
  1170. */
  1171. void nfs_end_data_update(struct inode *inode)
  1172. {
  1173. struct nfs_inode *nfsi = NFS_I(inode);
  1174. if (!nfs_have_delegation(inode, FMODE_READ)) {
  1175. /* Directories and symlinks: invalidate page cache */
  1176. if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) {
  1177. spin_lock(&inode->i_lock);
  1178. nfsi->cache_validity |= NFS_INO_INVALID_DATA;
  1179. spin_unlock(&inode->i_lock);
  1180. }
  1181. }
  1182. nfsi->cache_change_attribute = jiffies;
  1183. atomic_dec(&nfsi->data_updates);
  1184. }
  1185. static void nfs_wcc_update_inode(struct inode *inode, struct nfs_fattr *fattr)
  1186. {
  1187. struct nfs_inode *nfsi = NFS_I(inode);
  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. /* 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. nfsi->change_attr != fattr->change_attr)
  1229. nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
  1230. /* Verify a few of the more important attributes */
  1231. if (!timespec_equal(&inode->i_mtime, &fattr->mtime))
  1232. nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
  1233. cur_size = i_size_read(inode);
  1234. new_isize = nfs_size_to_loff_t(fattr->size);
  1235. if (cur_size != new_isize && nfsi->npages == 0)
  1236. nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
  1237. /* Have any file permissions changed? */
  1238. if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)
  1239. || inode->i_uid != fattr->uid
  1240. || inode->i_gid != fattr->gid)
  1241. nfsi->cache_validity |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
  1242. /* Has the link count changed? */
  1243. if (inode->i_nlink != fattr->nlink)
  1244. nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
  1245. if (!timespec_equal(&inode->i_atime, &fattr->atime))
  1246. nfsi->cache_validity |= NFS_INO_INVALID_ATIME;
  1247. nfsi->read_cache_jiffies = fattr->time_start;
  1248. return 0;
  1249. }
  1250. /**
  1251. * nfs_refresh_inode - try to update the inode attribute cache
  1252. * @inode - pointer to inode
  1253. * @fattr - updated attributes
  1254. *
  1255. * Check that an RPC call that returned attributes has not overlapped with
  1256. * other recent updates of the inode metadata, then decide whether it is
  1257. * safe to do a full update of the inode attributes, or whether just to
  1258. * call nfs_check_inode_attributes.
  1259. */
  1260. int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
  1261. {
  1262. struct nfs_inode *nfsi = NFS_I(inode);
  1263. int status;
  1264. if ((fattr->valid & NFS_ATTR_FATTR) == 0)
  1265. return 0;
  1266. spin_lock(&inode->i_lock);
  1267. if (time_after(fattr->time_start, nfsi->last_updated))
  1268. status = nfs_update_inode(inode, fattr);
  1269. else
  1270. status = nfs_check_inode_attributes(inode, fattr);
  1271. spin_unlock(&inode->i_lock);
  1272. return status;
  1273. }
  1274. /**
  1275. * nfs_post_op_update_inode - try to update the inode attribute cache
  1276. * @inode - pointer to inode
  1277. * @fattr - updated attributes
  1278. *
  1279. * After an operation that has changed the inode metadata, mark the
  1280. * attribute cache as being invalid, then try to update it.
  1281. */
  1282. int nfs_post_op_update_inode(struct inode *inode, struct nfs_fattr *fattr)
  1283. {
  1284. struct nfs_inode *nfsi = NFS_I(inode);
  1285. int status = 0;
  1286. spin_lock(&inode->i_lock);
  1287. if (unlikely((fattr->valid & NFS_ATTR_FATTR) == 0)) {
  1288. nfsi->cache_validity |= NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
  1289. goto out;
  1290. }
  1291. status = nfs_update_inode(inode, fattr);
  1292. out:
  1293. spin_unlock(&inode->i_lock);
  1294. return status;
  1295. }
  1296. /*
  1297. * Many nfs protocol calls return the new file attributes after
  1298. * an operation. Here we update the inode to reflect the state
  1299. * of the server's inode.
  1300. *
  1301. * This is a bit tricky because we have to make sure all dirty pages
  1302. * have been sent off to the server before calling invalidate_inode_pages.
  1303. * To make sure no other process adds more write requests while we try
  1304. * our best to flush them, we make them sleep during the attribute refresh.
  1305. *
  1306. * A very similar scenario holds for the dir cache.
  1307. */
  1308. static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr)
  1309. {
  1310. struct nfs_inode *nfsi = NFS_I(inode);
  1311. loff_t cur_isize, new_isize;
  1312. unsigned int invalid = 0;
  1313. int data_stable;
  1314. dfprintk(VFS, "NFS: %s(%s/%ld ct=%d info=0x%x)\n",
  1315. __FUNCTION__, inode->i_sb->s_id, inode->i_ino,
  1316. atomic_read(&inode->i_count), fattr->valid);
  1317. if (nfsi->fileid != fattr->fileid)
  1318. goto out_fileid;
  1319. /*
  1320. * Make sure the inode's type hasn't changed.
  1321. */
  1322. if ((inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
  1323. goto out_changed;
  1324. /*
  1325. * Update the read time so we don't revalidate too often.
  1326. */
  1327. nfsi->read_cache_jiffies = fattr->time_start;
  1328. nfsi->last_updated = jiffies;
  1329. /* Are we racing with known updates of the metadata on the server? */
  1330. data_stable = nfs_verify_change_attribute(inode, fattr->time_start);
  1331. if (data_stable)
  1332. nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_ATIME);
  1333. /* Do atomic weak cache consistency updates */
  1334. nfs_wcc_update_inode(inode, fattr);
  1335. /* Check if our cached file size is stale */
  1336. new_isize = nfs_size_to_loff_t(fattr->size);
  1337. cur_isize = i_size_read(inode);
  1338. if (new_isize != cur_isize) {
  1339. /* Do we perhaps have any outstanding writes? */
  1340. if (nfsi->npages == 0) {
  1341. /* No, but did we race with nfs_end_data_update()? */
  1342. if (data_stable) {
  1343. inode->i_size = new_isize;
  1344. invalid |= NFS_INO_INVALID_DATA;
  1345. }
  1346. invalid |= NFS_INO_INVALID_ATTR;
  1347. } else if (new_isize > cur_isize) {
  1348. inode->i_size = new_isize;
  1349. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
  1350. }
  1351. nfsi->cache_change_attribute = jiffies;
  1352. dprintk("NFS: isize change on server for file %s/%ld\n",
  1353. inode->i_sb->s_id, inode->i_ino);
  1354. }
  1355. /* Check if the mtime agrees */
  1356. if (!timespec_equal(&inode->i_mtime, &fattr->mtime)) {
  1357. memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
  1358. dprintk("NFS: mtime change on server for file %s/%ld\n",
  1359. inode->i_sb->s_id, inode->i_ino);
  1360. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
  1361. nfsi->cache_change_attribute = jiffies;
  1362. }
  1363. /* If ctime has changed we should definitely clear access+acl caches */
  1364. if (!timespec_equal(&inode->i_ctime, &fattr->ctime)) {
  1365. invalid |= NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  1366. memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
  1367. nfsi->cache_change_attribute = jiffies;
  1368. }
  1369. memcpy(&inode->i_atime, &fattr->atime, sizeof(inode->i_atime));
  1370. if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO) ||
  1371. inode->i_uid != fattr->uid ||
  1372. inode->i_gid != fattr->gid)
  1373. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  1374. inode->i_mode = fattr->mode;
  1375. inode->i_nlink = fattr->nlink;
  1376. inode->i_uid = fattr->uid;
  1377. inode->i_gid = fattr->gid;
  1378. if (fattr->valid & (NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4)) {
  1379. /*
  1380. * report the blocks in 512byte units
  1381. */
  1382. inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
  1383. inode->i_blksize = inode->i_sb->s_blocksize;
  1384. } else {
  1385. inode->i_blocks = fattr->du.nfs2.blocks;
  1386. inode->i_blksize = fattr->du.nfs2.blocksize;
  1387. }
  1388. if ((fattr->valid & NFS_ATTR_FATTR_V4) != 0 &&
  1389. nfsi->change_attr != fattr->change_attr) {
  1390. dprintk("NFS: change_attr change on server for file %s/%ld\n",
  1391. inode->i_sb->s_id, inode->i_ino);
  1392. nfsi->change_attr = fattr->change_attr;
  1393. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  1394. nfsi->cache_change_attribute = jiffies;
  1395. }
  1396. /* Update attrtimeo value if we're out of the unstable period */
  1397. if (invalid & NFS_INO_INVALID_ATTR) {
  1398. nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
  1399. nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
  1400. nfsi->attrtimeo_timestamp = jiffies;
  1401. } else if (time_after(jiffies, nfsi->attrtimeo_timestamp+nfsi->attrtimeo)) {
  1402. if ((nfsi->attrtimeo <<= 1) > NFS_MAXATTRTIMEO(inode))
  1403. nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
  1404. nfsi->attrtimeo_timestamp = jiffies;
  1405. }
  1406. /* Don't invalidate the data if we were to blame */
  1407. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
  1408. || S_ISLNK(inode->i_mode)))
  1409. invalid &= ~NFS_INO_INVALID_DATA;
  1410. if (data_stable)
  1411. invalid &= ~(NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME|NFS_INO_REVAL_PAGECACHE);
  1412. if (!nfs_have_delegation(inode, FMODE_READ))
  1413. nfsi->cache_validity |= invalid;
  1414. return 0;
  1415. out_changed:
  1416. /*
  1417. * Big trouble! The inode has become a different object.
  1418. */
  1419. #ifdef NFS_PARANOIA
  1420. printk(KERN_DEBUG "%s: inode %ld mode changed, %07o to %07o\n",
  1421. __FUNCTION__, inode->i_ino, inode->i_mode, fattr->mode);
  1422. #endif
  1423. out_err:
  1424. /*
  1425. * No need to worry about unhashing the dentry, as the
  1426. * lookup validation will know that the inode is bad.
  1427. * (But we fall through to invalidate the caches.)
  1428. */
  1429. nfs_invalidate_inode(inode);
  1430. return -ESTALE;
  1431. out_fileid:
  1432. printk(KERN_ERR "NFS: server %s error: fileid changed\n"
  1433. "fsid %s: expected fileid 0x%Lx, got 0x%Lx\n",
  1434. NFS_SERVER(inode)->hostname, inode->i_sb->s_id,
  1435. (long long)nfsi->fileid, (long long)fattr->fileid);
  1436. goto out_err;
  1437. }
  1438. /*
  1439. * File system information
  1440. */
  1441. static int nfs_set_super(struct super_block *s, void *data)
  1442. {
  1443. s->s_fs_info = data;
  1444. return set_anon_super(s, data);
  1445. }
  1446. static int nfs_compare_super(struct super_block *sb, void *data)
  1447. {
  1448. struct nfs_server *server = data;
  1449. struct nfs_server *old = NFS_SB(sb);
  1450. if (old->addr.sin_addr.s_addr != server->addr.sin_addr.s_addr)
  1451. return 0;
  1452. if (old->addr.sin_port != server->addr.sin_port)
  1453. return 0;
  1454. return !nfs_compare_fh(&old->fh, &server->fh);
  1455. }
  1456. static struct super_block *nfs_get_sb(struct file_system_type *fs_type,
  1457. int flags, const char *dev_name, void *raw_data)
  1458. {
  1459. int error;
  1460. struct nfs_server *server = NULL;
  1461. struct super_block *s;
  1462. struct nfs_fh *root;
  1463. struct nfs_mount_data *data = raw_data;
  1464. s = ERR_PTR(-EINVAL);
  1465. if (data == NULL) {
  1466. dprintk("%s: missing data argument\n", __FUNCTION__);
  1467. goto out_err;
  1468. }
  1469. if (data->version <= 0 || data->version > NFS_MOUNT_VERSION) {
  1470. dprintk("%s: bad mount version\n", __FUNCTION__);
  1471. goto out_err;
  1472. }
  1473. switch (data->version) {
  1474. case 1:
  1475. data->namlen = 0;
  1476. case 2:
  1477. data->bsize = 0;
  1478. case 3:
  1479. if (data->flags & NFS_MOUNT_VER3) {
  1480. dprintk("%s: mount structure version %d does not support NFSv3\n",
  1481. __FUNCTION__,
  1482. data->version);
  1483. goto out_err;
  1484. }
  1485. data->root.size = NFS2_FHSIZE;
  1486. memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
  1487. case 4:
  1488. if (data->flags & NFS_MOUNT_SECFLAVOUR) {
  1489. dprintk("%s: mount structure version %d does not support strong security\n",
  1490. __FUNCTION__,
  1491. data->version);
  1492. goto out_err;
  1493. }
  1494. case 5:
  1495. memset(data->context, 0, sizeof(data->context));
  1496. }
  1497. #ifndef CONFIG_NFS_V3
  1498. /* If NFSv3 is not compiled in, return -EPROTONOSUPPORT */
  1499. s = ERR_PTR(-EPROTONOSUPPORT);
  1500. if (data->flags & NFS_MOUNT_VER3) {
  1501. dprintk("%s: NFSv3 not compiled into kernel\n", __FUNCTION__);
  1502. goto out_err;
  1503. }
  1504. #endif /* CONFIG_NFS_V3 */
  1505. s = ERR_PTR(-ENOMEM);
  1506. server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
  1507. if (!server)
  1508. goto out_err;
  1509. /* Zero out the NFS state stuff */
  1510. init_nfsv4_state(server);
  1511. server->client = server->client_sys = server->client_acl = ERR_PTR(-EINVAL);
  1512. root = &server->fh;
  1513. if (data->flags & NFS_MOUNT_VER3)
  1514. root->size = data->root.size;
  1515. else
  1516. root->size = NFS2_FHSIZE;
  1517. s = ERR_PTR(-EINVAL);
  1518. if (root->size > sizeof(root->data)) {
  1519. dprintk("%s: invalid root filehandle\n", __FUNCTION__);
  1520. goto out_err;
  1521. }
  1522. memcpy(root->data, data->root.data, root->size);
  1523. /* We now require that the mount process passes the remote address */
  1524. memcpy(&server->addr, &data->addr, sizeof(server->addr));
  1525. if (server->addr.sin_addr.s_addr == INADDR_ANY) {
  1526. dprintk("%s: mount program didn't pass remote address!\n",
  1527. __FUNCTION__);
  1528. goto out_err;
  1529. }
  1530. /* Fire up rpciod if not yet running */
  1531. s = ERR_PTR(rpciod_up());
  1532. if (IS_ERR(s)) {
  1533. dprintk("%s: couldn't start rpciod! Error = %ld\n",
  1534. __FUNCTION__, PTR_ERR(s));
  1535. goto out_err;
  1536. }
  1537. s = sget(fs_type, nfs_compare_super, nfs_set_super, server);
  1538. if (IS_ERR(s) || s->s_root)
  1539. goto out_rpciod_down;
  1540. s->s_flags = flags;
  1541. error = nfs_fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  1542. if (error) {
  1543. up_write(&s->s_umount);
  1544. deactivate_super(s);
  1545. return ERR_PTR(error);
  1546. }
  1547. s->s_flags |= MS_ACTIVE;
  1548. return s;
  1549. out_rpciod_down:
  1550. rpciod_down();
  1551. out_err:
  1552. kfree(server);
  1553. return s;
  1554. }
  1555. static void nfs_kill_super(struct super_block *s)
  1556. {
  1557. struct nfs_server *server = NFS_SB(s);
  1558. kill_anon_super(s);
  1559. if (!IS_ERR(server->client))
  1560. rpc_shutdown_client(server->client);
  1561. if (!IS_ERR(server->client_sys))
  1562. rpc_shutdown_client(server->client_sys);
  1563. if (!IS_ERR(server->client_acl))
  1564. rpc_shutdown_client(server->client_acl);
  1565. if (!(server->flags & NFS_MOUNT_NONLM))
  1566. lockd_down(); /* release rpc.lockd */
  1567. rpciod_down(); /* release rpciod */
  1568. nfs_free_iostats(server->io_stats);
  1569. kfree(server->hostname);
  1570. kfree(server);
  1571. }
  1572. static struct file_system_type nfs_fs_type = {
  1573. .owner = THIS_MODULE,
  1574. .name = "nfs",
  1575. .get_sb = nfs_get_sb,
  1576. .kill_sb = nfs_kill_super,
  1577. .fs_flags = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
  1578. };
  1579. #ifdef CONFIG_NFS_V4
  1580. static void nfs4_clear_inode(struct inode *);
  1581. static struct super_operations nfs4_sops = {
  1582. .alloc_inode = nfs_alloc_inode,
  1583. .destroy_inode = nfs_destroy_inode,
  1584. .write_inode = nfs_write_inode,
  1585. .delete_inode = nfs_delete_inode,
  1586. .statfs = nfs_statfs,
  1587. .clear_inode = nfs4_clear_inode,
  1588. .umount_begin = nfs_umount_begin,
  1589. .show_options = nfs_show_options,
  1590. .show_stats = nfs_show_stats,
  1591. };
  1592. /*
  1593. * Clean out any remaining NFSv4 state that might be left over due
  1594. * to open() calls that passed nfs_atomic_lookup, but failed to call
  1595. * nfs_open().
  1596. */
  1597. static void nfs4_clear_inode(struct inode *inode)
  1598. {
  1599. struct nfs_inode *nfsi = NFS_I(inode);
  1600. /* If we are holding a delegation, return it! */
  1601. nfs_inode_return_delegation(inode);
  1602. /* First call standard NFS clear_inode() code */
  1603. nfs_clear_inode(inode);
  1604. /* Now clear out any remaining state */
  1605. while (!list_empty(&nfsi->open_states)) {
  1606. struct nfs4_state *state;
  1607. state = list_entry(nfsi->open_states.next,
  1608. struct nfs4_state,
  1609. inode_states);
  1610. dprintk("%s(%s/%Ld): found unclaimed NFSv4 state %p\n",
  1611. __FUNCTION__,
  1612. inode->i_sb->s_id,
  1613. (long long)NFS_FILEID(inode),
  1614. state);
  1615. BUG_ON(atomic_read(&state->count) != 1);
  1616. nfs4_close_state(state, state->state);
  1617. }
  1618. }
  1619. static int nfs4_fill_super(struct super_block *sb, struct nfs4_mount_data *data, int silent)
  1620. {
  1621. struct nfs_server *server;
  1622. struct nfs4_client *clp = NULL;
  1623. struct rpc_xprt *xprt = NULL;
  1624. struct rpc_clnt *clnt = NULL;
  1625. struct rpc_timeout timeparms;
  1626. rpc_authflavor_t authflavour;
  1627. int err = -EIO;
  1628. sb->s_blocksize_bits = 0;
  1629. sb->s_blocksize = 0;
  1630. server = NFS_SB(sb);
  1631. if (data->rsize != 0)
  1632. server->rsize = nfs_block_size(data->rsize, NULL);
  1633. if (data->wsize != 0)
  1634. server->wsize = nfs_block_size(data->wsize, NULL);
  1635. server->flags = data->flags & NFS_MOUNT_FLAGMASK;
  1636. server->caps = NFS_CAP_ATOMIC_OPEN;
  1637. server->acregmin = data->acregmin*HZ;
  1638. server->acregmax = data->acregmax*HZ;
  1639. server->acdirmin = data->acdirmin*HZ;
  1640. server->acdirmax = data->acdirmax*HZ;
  1641. server->rpc_ops = &nfs_v4_clientops;
  1642. nfs_init_timeout_values(&timeparms, data->proto, data->timeo, data->retrans);
  1643. server->retrans_timeo = timeparms.to_initval;
  1644. server->retrans_count = timeparms.to_retries;
  1645. clp = nfs4_get_client(&server->addr.sin_addr);
  1646. if (!clp) {
  1647. dprintk("%s: failed to create NFS4 client.\n", __FUNCTION__);
  1648. return -EIO;
  1649. }
  1650. /* Now create transport and client */
  1651. authflavour = RPC_AUTH_UNIX;
  1652. if (data->auth_flavourlen != 0) {
  1653. if (data->auth_flavourlen != 1) {
  1654. dprintk("%s: Invalid number of RPC auth flavours %d.\n",
  1655. __FUNCTION__, data->auth_flavourlen);
  1656. err = -EINVAL;
  1657. goto out_fail;
  1658. }
  1659. if (copy_from_user(&authflavour, data->auth_flavours, sizeof(authflavour))) {
  1660. err = -EFAULT;
  1661. goto out_fail;
  1662. }
  1663. }
  1664. down_write(&clp->cl_sem);
  1665. if (IS_ERR(clp->cl_rpcclient)) {
  1666. xprt = xprt_create_proto(data->proto, &server->addr, &timeparms);
  1667. if (IS_ERR(xprt)) {
  1668. up_write(&clp->cl_sem);
  1669. err = PTR_ERR(xprt);
  1670. dprintk("%s: cannot create RPC transport. Error = %d\n",
  1671. __FUNCTION__, err);
  1672. goto out_fail;
  1673. }
  1674. clnt = rpc_create_client(xprt, server->hostname, &nfs_program,
  1675. server->rpc_ops->version, authflavour);
  1676. if (IS_ERR(clnt)) {
  1677. up_write(&clp->cl_sem);
  1678. err = PTR_ERR(clnt);
  1679. dprintk("%s: cannot create RPC client. Error = %d\n",
  1680. __FUNCTION__, err);
  1681. goto out_fail;
  1682. }
  1683. clnt->cl_intr = 1;
  1684. clnt->cl_softrtry = 1;
  1685. clp->cl_rpcclient = clnt;
  1686. memcpy(clp->cl_ipaddr, server->ip_addr, sizeof(clp->cl_ipaddr));
  1687. nfs_idmap_new(clp);
  1688. }
  1689. list_add_tail(&server->nfs4_siblings, &clp->cl_superblocks);
  1690. clnt = rpc_clone_client(clp->cl_rpcclient);
  1691. if (!IS_ERR(clnt))
  1692. server->nfs4_state = clp;
  1693. up_write(&clp->cl_sem);
  1694. clp = NULL;
  1695. if (IS_ERR(clnt)) {
  1696. err = PTR_ERR(clnt);
  1697. dprintk("%s: cannot create RPC client. Error = %d\n",
  1698. __FUNCTION__, err);
  1699. return err;
  1700. }
  1701. server->client = clnt;
  1702. if (server->nfs4_state->cl_idmap == NULL) {
  1703. dprintk("%s: failed to create idmapper.\n", __FUNCTION__);
  1704. return -ENOMEM;
  1705. }
  1706. if (clnt->cl_auth->au_flavor != authflavour) {
  1707. struct rpc_auth *auth;
  1708. auth = rpcauth_create(authflavour, clnt);
  1709. if (IS_ERR(auth)) {
  1710. dprintk("%s: couldn't create credcache!\n", __FUNCTION__);
  1711. return PTR_ERR(auth);
  1712. }
  1713. }
  1714. sb->s_time_gran = 1;
  1715. sb->s_op = &nfs4_sops;
  1716. err = nfs_sb_init(sb, authflavour);
  1717. if (err == 0)
  1718. return 0;
  1719. out_fail:
  1720. if (clp)
  1721. nfs4_put_client(clp);
  1722. return err;
  1723. }
  1724. static int nfs4_compare_super(struct super_block *sb, void *data)
  1725. {
  1726. struct nfs_server *server = data;
  1727. struct nfs_server *old = NFS_SB(sb);
  1728. if (strcmp(server->hostname, old->hostname) != 0)
  1729. return 0;
  1730. if (strcmp(server->mnt_path, old->mnt_path) != 0)
  1731. return 0;
  1732. return 1;
  1733. }
  1734. static void *
  1735. nfs_copy_user_string(char *dst, struct nfs_string *src, int maxlen)
  1736. {
  1737. void *p = NULL;
  1738. if (!src->len)
  1739. return ERR_PTR(-EINVAL);
  1740. if (src->len < maxlen)
  1741. maxlen = src->len;
  1742. if (dst == NULL) {
  1743. p = dst = kmalloc(maxlen + 1, GFP_KERNEL);
  1744. if (p == NULL)
  1745. return ERR_PTR(-ENOMEM);
  1746. }
  1747. if (copy_from_user(dst, src->data, maxlen)) {
  1748. kfree(p);
  1749. return ERR_PTR(-EFAULT);
  1750. }
  1751. dst[maxlen] = '\0';
  1752. return dst;
  1753. }
  1754. static struct super_block *nfs4_get_sb(struct file_system_type *fs_type,
  1755. int flags, const char *dev_name, void *raw_data)
  1756. {
  1757. int error;
  1758. struct nfs_server *server;
  1759. struct super_block *s;
  1760. struct nfs4_mount_data *data = raw_data;
  1761. void *p;
  1762. if (data == NULL) {
  1763. dprintk("%s: missing data argument\n", __FUNCTION__);
  1764. return ERR_PTR(-EINVAL);
  1765. }
  1766. if (data->version <= 0 || data->version > NFS4_MOUNT_VERSION) {
  1767. dprintk("%s: bad mount version\n", __FUNCTION__);
  1768. return ERR_PTR(-EINVAL);
  1769. }
  1770. server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
  1771. if (!server)
  1772. return ERR_PTR(-ENOMEM);
  1773. /* Zero out the NFS state stuff */
  1774. init_nfsv4_state(server);
  1775. server->client = server->client_sys = server->client_acl = ERR_PTR(-EINVAL);
  1776. p = nfs_copy_user_string(NULL, &data->hostname, 256);
  1777. if (IS_ERR(p))
  1778. goto out_err;
  1779. server->hostname = p;
  1780. p = nfs_copy_user_string(NULL, &data->mnt_path, 1024);
  1781. if (IS_ERR(p))
  1782. goto out_err;
  1783. server->mnt_path = p;
  1784. p = nfs_copy_user_string(server->ip_addr, &data->client_addr,
  1785. sizeof(server->ip_addr) - 1);
  1786. if (IS_ERR(p))
  1787. goto out_err;
  1788. /* We now require that the mount process passes the remote address */
  1789. if (data->host_addrlen != sizeof(server->addr)) {
  1790. s = ERR_PTR(-EINVAL);
  1791. goto out_free;
  1792. }
  1793. if (copy_from_user(&server->addr, data->host_addr, sizeof(server->addr))) {
  1794. s = ERR_PTR(-EFAULT);
  1795. goto out_free;
  1796. }
  1797. if (server->addr.sin_family != AF_INET ||
  1798. server->addr.sin_addr.s_addr == INADDR_ANY) {
  1799. dprintk("%s: mount program didn't pass remote IP address!\n",
  1800. __FUNCTION__);
  1801. s = ERR_PTR(-EINVAL);
  1802. goto out_free;
  1803. }
  1804. /* Fire up rpciod if not yet running */
  1805. s = ERR_PTR(rpciod_up());
  1806. if (IS_ERR(s)) {
  1807. dprintk("%s: couldn't start rpciod! Error = %ld\n",
  1808. __FUNCTION__, PTR_ERR(s));
  1809. goto out_free;
  1810. }
  1811. s = sget(fs_type, nfs4_compare_super, nfs_set_super, server);
  1812. if (IS_ERR(s) || s->s_root)
  1813. goto out_free;
  1814. s->s_flags = flags;
  1815. error = nfs4_fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  1816. if (error) {
  1817. up_write(&s->s_umount);
  1818. deactivate_super(s);
  1819. return ERR_PTR(error);
  1820. }
  1821. s->s_flags |= MS_ACTIVE;
  1822. return s;
  1823. out_err:
  1824. s = (struct super_block *)p;
  1825. out_free:
  1826. kfree(server->mnt_path);
  1827. kfree(server->hostname);
  1828. kfree(server);
  1829. return s;
  1830. }
  1831. static void nfs4_kill_super(struct super_block *sb)
  1832. {
  1833. struct nfs_server *server = NFS_SB(sb);
  1834. nfs_return_all_delegations(sb);
  1835. kill_anon_super(sb);
  1836. nfs4_renewd_prepare_shutdown(server);
  1837. if (server->client != NULL && !IS_ERR(server->client))
  1838. rpc_shutdown_client(server->client);
  1839. destroy_nfsv4_state(server);
  1840. rpciod_down();
  1841. nfs_free_iostats(server->io_stats);
  1842. kfree(server->hostname);
  1843. kfree(server);
  1844. }
  1845. static struct file_system_type nfs4_fs_type = {
  1846. .owner = THIS_MODULE,
  1847. .name = "nfs4",
  1848. .get_sb = nfs4_get_sb,
  1849. .kill_sb = nfs4_kill_super,
  1850. .fs_flags = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
  1851. };
  1852. static const int nfs_set_port_min = 0;
  1853. static const int nfs_set_port_max = 65535;
  1854. static int param_set_port(const char *val, struct kernel_param *kp)
  1855. {
  1856. char *endp;
  1857. int num = simple_strtol(val, &endp, 0);
  1858. if (endp == val || *endp || num < nfs_set_port_min || num > nfs_set_port_max)
  1859. return -EINVAL;
  1860. *((int *)kp->arg) = num;
  1861. return 0;
  1862. }
  1863. module_param_call(callback_tcpport, param_set_port, param_get_int,
  1864. &nfs_callback_set_tcpport, 0644);
  1865. static int param_set_idmap_timeout(const char *val, struct kernel_param *kp)
  1866. {
  1867. char *endp;
  1868. int num = simple_strtol(val, &endp, 0);
  1869. int jif = num * HZ;
  1870. if (endp == val || *endp || num < 0 || jif < num)
  1871. return -EINVAL;
  1872. *((int *)kp->arg) = jif;
  1873. return 0;
  1874. }
  1875. module_param_call(idmap_cache_timeout, param_set_idmap_timeout, param_get_int,
  1876. &nfs_idmap_cache_timeout, 0644);
  1877. #define nfs4_init_once(nfsi) \
  1878. do { \
  1879. INIT_LIST_HEAD(&(nfsi)->open_states); \
  1880. nfsi->delegation = NULL; \
  1881. nfsi->delegation_state = 0; \
  1882. init_rwsem(&nfsi->rwsem); \
  1883. } while(0)
  1884. static inline int register_nfs4fs(void)
  1885. {
  1886. int ret;
  1887. ret = nfs_register_sysctl();
  1888. if (ret != 0)
  1889. return ret;
  1890. ret = register_filesystem(&nfs4_fs_type);
  1891. if (ret != 0)
  1892. nfs_unregister_sysctl();
  1893. return ret;
  1894. }
  1895. static inline void unregister_nfs4fs(void)
  1896. {
  1897. unregister_filesystem(&nfs4_fs_type);
  1898. nfs_unregister_sysctl();
  1899. }
  1900. #else
  1901. #define nfs4_init_once(nfsi) \
  1902. do { } while (0)
  1903. #define register_nfs4fs() (0)
  1904. #define unregister_nfs4fs()
  1905. #endif
  1906. extern int nfs_init_nfspagecache(void);
  1907. extern void nfs_destroy_nfspagecache(void);
  1908. extern int nfs_init_readpagecache(void);
  1909. extern void nfs_destroy_readpagecache(void);
  1910. extern int nfs_init_writepagecache(void);
  1911. extern void nfs_destroy_writepagecache(void);
  1912. #ifdef CONFIG_NFS_DIRECTIO
  1913. extern int nfs_init_directcache(void);
  1914. extern void nfs_destroy_directcache(void);
  1915. #endif
  1916. static kmem_cache_t * nfs_inode_cachep;
  1917. static struct inode *nfs_alloc_inode(struct super_block *sb)
  1918. {
  1919. struct nfs_inode *nfsi;
  1920. nfsi = (struct nfs_inode *)kmem_cache_alloc(nfs_inode_cachep, SLAB_KERNEL);
  1921. if (!nfsi)
  1922. return NULL;
  1923. nfsi->flags = 0UL;
  1924. nfsi->cache_validity = 0UL;
  1925. nfsi->cache_change_attribute = jiffies;
  1926. #ifdef CONFIG_NFS_V3_ACL
  1927. nfsi->acl_access = ERR_PTR(-EAGAIN);
  1928. nfsi->acl_default = ERR_PTR(-EAGAIN);
  1929. #endif
  1930. #ifdef CONFIG_NFS_V4
  1931. nfsi->nfs4_acl = NULL;
  1932. #endif /* CONFIG_NFS_V4 */
  1933. return &nfsi->vfs_inode;
  1934. }
  1935. static void nfs_destroy_inode(struct inode *inode)
  1936. {
  1937. kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
  1938. }
  1939. static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
  1940. {
  1941. struct nfs_inode *nfsi = (struct nfs_inode *) foo;
  1942. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  1943. SLAB_CTOR_CONSTRUCTOR) {
  1944. inode_init_once(&nfsi->vfs_inode);
  1945. spin_lock_init(&nfsi->req_lock);
  1946. INIT_LIST_HEAD(&nfsi->dirty);
  1947. INIT_LIST_HEAD(&nfsi->commit);
  1948. INIT_LIST_HEAD(&nfsi->open_files);
  1949. INIT_RADIX_TREE(&nfsi->nfs_page_tree, GFP_ATOMIC);
  1950. atomic_set(&nfsi->data_updates, 0);
  1951. nfsi->ndirty = 0;
  1952. nfsi->ncommit = 0;
  1953. nfsi->npages = 0;
  1954. nfs4_init_once(nfsi);
  1955. }
  1956. }
  1957. static int nfs_init_inodecache(void)
  1958. {
  1959. nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
  1960. sizeof(struct nfs_inode),
  1961. 0, (SLAB_RECLAIM_ACCOUNT|
  1962. SLAB_MEM_SPREAD),
  1963. init_once, NULL);
  1964. if (nfs_inode_cachep == NULL)
  1965. return -ENOMEM;
  1966. return 0;
  1967. }
  1968. static void nfs_destroy_inodecache(void)
  1969. {
  1970. if (kmem_cache_destroy(nfs_inode_cachep))
  1971. printk(KERN_INFO "nfs_inode_cache: not all structures were freed\n");
  1972. }
  1973. /*
  1974. * Initialize NFS
  1975. */
  1976. static int __init init_nfs_fs(void)
  1977. {
  1978. int err;
  1979. err = nfs_init_nfspagecache();
  1980. if (err)
  1981. goto out4;
  1982. err = nfs_init_inodecache();
  1983. if (err)
  1984. goto out3;
  1985. err = nfs_init_readpagecache();
  1986. if (err)
  1987. goto out2;
  1988. err = nfs_init_writepagecache();
  1989. if (err)
  1990. goto out1;
  1991. #ifdef CONFIG_NFS_DIRECTIO
  1992. err = nfs_init_directcache();
  1993. if (err)
  1994. goto out0;
  1995. #endif
  1996. #ifdef CONFIG_PROC_FS
  1997. rpc_proc_register(&nfs_rpcstat);
  1998. #endif
  1999. err = register_filesystem(&nfs_fs_type);
  2000. if (err)
  2001. goto out;
  2002. if ((err = register_nfs4fs()) != 0)
  2003. goto out;
  2004. return 0;
  2005. out:
  2006. #ifdef CONFIG_PROC_FS
  2007. rpc_proc_unregister("nfs");
  2008. #endif
  2009. #ifdef CONFIG_NFS_DIRECTIO
  2010. nfs_destroy_directcache();
  2011. out0:
  2012. #endif
  2013. nfs_destroy_writepagecache();
  2014. out1:
  2015. nfs_destroy_readpagecache();
  2016. out2:
  2017. nfs_destroy_inodecache();
  2018. out3:
  2019. nfs_destroy_nfspagecache();
  2020. out4:
  2021. return err;
  2022. }
  2023. static void __exit exit_nfs_fs(void)
  2024. {
  2025. #ifdef CONFIG_NFS_DIRECTIO
  2026. nfs_destroy_directcache();
  2027. #endif
  2028. nfs_destroy_writepagecache();
  2029. nfs_destroy_readpagecache();
  2030. nfs_destroy_inodecache();
  2031. nfs_destroy_nfspagecache();
  2032. #ifdef CONFIG_PROC_FS
  2033. rpc_proc_unregister("nfs");
  2034. #endif
  2035. unregister_filesystem(&nfs_fs_type);
  2036. unregister_nfs4fs();
  2037. }
  2038. /* Not quite true; I just maintain it */
  2039. MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
  2040. MODULE_LICENSE("GPL");
  2041. module_init(init_nfs_fs)
  2042. module_exit(exit_nfs_fs)