inode.c 63 KB

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