inode.c 59 KB

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