inode.c 57 KB

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