inode.c 70 KB

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