inode.c 57 KB

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