inode.c 78 KB

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