inode.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271
  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@lxorguk.ukuu.org.uk>, 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/module.h>
  16. #include <linux/init.h>
  17. #include <linux/sched/signal.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/seq_file.h>
  33. #include <linux/mount.h>
  34. #include <linux/vfs.h>
  35. #include <linux/inet.h>
  36. #include <linux/nfs_xdr.h>
  37. #include <linux/slab.h>
  38. #include <linux/compat.h>
  39. #include <linux/freezer.h>
  40. #include <linux/uaccess.h>
  41. #include <linux/iversion.h>
  42. #include "nfs4_fs.h"
  43. #include "callback.h"
  44. #include "delegation.h"
  45. #include "iostat.h"
  46. #include "internal.h"
  47. #include "fscache.h"
  48. #include "pnfs.h"
  49. #include "nfs.h"
  50. #include "netns.h"
  51. #include "nfstrace.h"
  52. #define NFSDBG_FACILITY NFSDBG_VFS
  53. #define NFS_64_BIT_INODE_NUMBERS_ENABLED 1
  54. /* Default is to see 64-bit inode numbers */
  55. static bool enable_ino64 = NFS_64_BIT_INODE_NUMBERS_ENABLED;
  56. static void nfs_invalidate_inode(struct inode *);
  57. static int nfs_update_inode(struct inode *, struct nfs_fattr *);
  58. static struct kmem_cache * nfs_inode_cachep;
  59. static inline unsigned long
  60. nfs_fattr_to_ino_t(struct nfs_fattr *fattr)
  61. {
  62. return nfs_fileid_to_ino_t(fattr->fileid);
  63. }
  64. static int nfs_wait_killable(int mode)
  65. {
  66. freezable_schedule_unsafe();
  67. if (signal_pending_state(mode, current))
  68. return -ERESTARTSYS;
  69. return 0;
  70. }
  71. int nfs_wait_bit_killable(struct wait_bit_key *key, int mode)
  72. {
  73. return nfs_wait_killable(mode);
  74. }
  75. EXPORT_SYMBOL_GPL(nfs_wait_bit_killable);
  76. /**
  77. * nfs_compat_user_ino64 - returns the user-visible inode number
  78. * @fileid: 64-bit fileid
  79. *
  80. * This function returns a 32-bit inode number if the boot parameter
  81. * nfs.enable_ino64 is zero.
  82. */
  83. u64 nfs_compat_user_ino64(u64 fileid)
  84. {
  85. #ifdef CONFIG_COMPAT
  86. compat_ulong_t ino;
  87. #else
  88. unsigned long ino;
  89. #endif
  90. if (enable_ino64)
  91. return fileid;
  92. ino = fileid;
  93. if (sizeof(ino) < sizeof(fileid))
  94. ino ^= fileid >> (sizeof(fileid)-sizeof(ino)) * 8;
  95. return ino;
  96. }
  97. int nfs_drop_inode(struct inode *inode)
  98. {
  99. return NFS_STALE(inode) || generic_drop_inode(inode);
  100. }
  101. EXPORT_SYMBOL_GPL(nfs_drop_inode);
  102. void nfs_clear_inode(struct inode *inode)
  103. {
  104. /*
  105. * The following should never happen...
  106. */
  107. WARN_ON_ONCE(nfs_have_writebacks(inode));
  108. WARN_ON_ONCE(!list_empty(&NFS_I(inode)->open_files));
  109. nfs_zap_acl_cache(inode);
  110. nfs_access_zap_cache(inode);
  111. nfs_fscache_clear_inode(inode);
  112. }
  113. EXPORT_SYMBOL_GPL(nfs_clear_inode);
  114. void nfs_evict_inode(struct inode *inode)
  115. {
  116. truncate_inode_pages_final(&inode->i_data);
  117. clear_inode(inode);
  118. nfs_clear_inode(inode);
  119. }
  120. int nfs_sync_inode(struct inode *inode)
  121. {
  122. inode_dio_wait(inode);
  123. return nfs_wb_all(inode);
  124. }
  125. EXPORT_SYMBOL_GPL(nfs_sync_inode);
  126. /**
  127. * nfs_sync_mapping - helper to flush all mmapped dirty data to disk
  128. */
  129. int nfs_sync_mapping(struct address_space *mapping)
  130. {
  131. int ret = 0;
  132. if (mapping->nrpages != 0) {
  133. unmap_mapping_range(mapping, 0, 0, 0);
  134. ret = nfs_wb_all(mapping->host);
  135. }
  136. return ret;
  137. }
  138. static int nfs_attribute_timeout(struct inode *inode)
  139. {
  140. struct nfs_inode *nfsi = NFS_I(inode);
  141. return !time_in_range_open(jiffies, nfsi->read_cache_jiffies, nfsi->read_cache_jiffies + nfsi->attrtimeo);
  142. }
  143. static bool nfs_check_cache_invalid_delegated(struct inode *inode, unsigned long flags)
  144. {
  145. unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
  146. /* Special case for the pagecache or access cache */
  147. if (flags == NFS_INO_REVAL_PAGECACHE &&
  148. !(cache_validity & NFS_INO_REVAL_FORCED))
  149. return false;
  150. return (cache_validity & flags) != 0;
  151. }
  152. static bool nfs_check_cache_invalid_not_delegated(struct inode *inode, unsigned long flags)
  153. {
  154. unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
  155. if ((cache_validity & flags) != 0)
  156. return true;
  157. if (nfs_attribute_timeout(inode))
  158. return true;
  159. return false;
  160. }
  161. bool nfs_check_cache_invalid(struct inode *inode, unsigned long flags)
  162. {
  163. if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
  164. return nfs_check_cache_invalid_delegated(inode, flags);
  165. return nfs_check_cache_invalid_not_delegated(inode, flags);
  166. }
  167. static void nfs_set_cache_invalid(struct inode *inode, unsigned long flags)
  168. {
  169. struct nfs_inode *nfsi = NFS_I(inode);
  170. bool have_delegation = NFS_PROTO(inode)->have_delegation(inode, FMODE_READ);
  171. if (have_delegation) {
  172. if (!(flags & NFS_INO_REVAL_FORCED))
  173. flags &= ~NFS_INO_INVALID_OTHER;
  174. flags &= ~(NFS_INO_INVALID_CHANGE
  175. | NFS_INO_INVALID_SIZE
  176. | NFS_INO_REVAL_PAGECACHE);
  177. }
  178. if (inode->i_mapping->nrpages == 0)
  179. flags &= ~NFS_INO_INVALID_DATA;
  180. nfsi->cache_validity |= flags;
  181. if (flags & NFS_INO_INVALID_DATA)
  182. nfs_fscache_invalidate(inode);
  183. }
  184. /*
  185. * Invalidate the local caches
  186. */
  187. static void nfs_zap_caches_locked(struct inode *inode)
  188. {
  189. struct nfs_inode *nfsi = NFS_I(inode);
  190. int mode = inode->i_mode;
  191. nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
  192. nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
  193. nfsi->attrtimeo_timestamp = jiffies;
  194. memset(NFS_I(inode)->cookieverf, 0, sizeof(NFS_I(inode)->cookieverf));
  195. if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) {
  196. nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR
  197. | NFS_INO_INVALID_DATA
  198. | NFS_INO_INVALID_ACCESS
  199. | NFS_INO_INVALID_ACL
  200. | NFS_INO_REVAL_PAGECACHE);
  201. } else
  202. nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR
  203. | NFS_INO_INVALID_ACCESS
  204. | NFS_INO_INVALID_ACL
  205. | NFS_INO_REVAL_PAGECACHE);
  206. nfs_zap_label_cache_locked(nfsi);
  207. }
  208. void nfs_zap_caches(struct inode *inode)
  209. {
  210. spin_lock(&inode->i_lock);
  211. nfs_zap_caches_locked(inode);
  212. spin_unlock(&inode->i_lock);
  213. }
  214. void nfs_zap_mapping(struct inode *inode, struct address_space *mapping)
  215. {
  216. if (mapping->nrpages != 0) {
  217. spin_lock(&inode->i_lock);
  218. nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA);
  219. spin_unlock(&inode->i_lock);
  220. }
  221. }
  222. void nfs_zap_acl_cache(struct inode *inode)
  223. {
  224. void (*clear_acl_cache)(struct inode *);
  225. clear_acl_cache = NFS_PROTO(inode)->clear_acl_cache;
  226. if (clear_acl_cache != NULL)
  227. clear_acl_cache(inode);
  228. spin_lock(&inode->i_lock);
  229. NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_ACL;
  230. spin_unlock(&inode->i_lock);
  231. }
  232. EXPORT_SYMBOL_GPL(nfs_zap_acl_cache);
  233. void nfs_invalidate_atime(struct inode *inode)
  234. {
  235. spin_lock(&inode->i_lock);
  236. nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME);
  237. spin_unlock(&inode->i_lock);
  238. }
  239. EXPORT_SYMBOL_GPL(nfs_invalidate_atime);
  240. /*
  241. * Invalidate, but do not unhash, the inode.
  242. * NB: must be called with inode->i_lock held!
  243. */
  244. static void nfs_invalidate_inode(struct inode *inode)
  245. {
  246. set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
  247. nfs_zap_caches_locked(inode);
  248. }
  249. struct nfs_find_desc {
  250. struct nfs_fh *fh;
  251. struct nfs_fattr *fattr;
  252. };
  253. /*
  254. * In NFSv3 we can have 64bit inode numbers. In order to support
  255. * this, and re-exported directories (also seen in NFSv2)
  256. * we are forced to allow 2 different inodes to have the same
  257. * i_ino.
  258. */
  259. static int
  260. nfs_find_actor(struct inode *inode, void *opaque)
  261. {
  262. struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
  263. struct nfs_fh *fh = desc->fh;
  264. struct nfs_fattr *fattr = desc->fattr;
  265. if (NFS_FILEID(inode) != fattr->fileid)
  266. return 0;
  267. if ((S_IFMT & inode->i_mode) != (S_IFMT & fattr->mode))
  268. return 0;
  269. if (nfs_compare_fh(NFS_FH(inode), fh))
  270. return 0;
  271. if (is_bad_inode(inode) || NFS_STALE(inode))
  272. return 0;
  273. return 1;
  274. }
  275. static int
  276. nfs_init_locked(struct inode *inode, void *opaque)
  277. {
  278. struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
  279. struct nfs_fattr *fattr = desc->fattr;
  280. set_nfs_fileid(inode, fattr->fileid);
  281. inode->i_mode = fattr->mode;
  282. nfs_copy_fh(NFS_FH(inode), desc->fh);
  283. return 0;
  284. }
  285. #ifdef CONFIG_NFS_V4_SECURITY_LABEL
  286. static void nfs_clear_label_invalid(struct inode *inode)
  287. {
  288. spin_lock(&inode->i_lock);
  289. NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_LABEL;
  290. spin_unlock(&inode->i_lock);
  291. }
  292. void nfs_setsecurity(struct inode *inode, struct nfs_fattr *fattr,
  293. struct nfs4_label *label)
  294. {
  295. int error;
  296. if (label == NULL)
  297. return;
  298. if ((fattr->valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL) && inode->i_security) {
  299. error = security_inode_notifysecctx(inode, label->label,
  300. label->len);
  301. if (error)
  302. printk(KERN_ERR "%s() %s %d "
  303. "security_inode_notifysecctx() %d\n",
  304. __func__,
  305. (char *)label->label,
  306. label->len, error);
  307. nfs_clear_label_invalid(inode);
  308. }
  309. }
  310. struct nfs4_label *nfs4_label_alloc(struct nfs_server *server, gfp_t flags)
  311. {
  312. struct nfs4_label *label = NULL;
  313. int minor_version = server->nfs_client->cl_minorversion;
  314. if (minor_version < 2)
  315. return label;
  316. if (!(server->caps & NFS_CAP_SECURITY_LABEL))
  317. return label;
  318. label = kzalloc(sizeof(struct nfs4_label), flags);
  319. if (label == NULL)
  320. return ERR_PTR(-ENOMEM);
  321. label->label = kzalloc(NFS4_MAXLABELLEN, flags);
  322. if (label->label == NULL) {
  323. kfree(label);
  324. return ERR_PTR(-ENOMEM);
  325. }
  326. label->len = NFS4_MAXLABELLEN;
  327. return label;
  328. }
  329. EXPORT_SYMBOL_GPL(nfs4_label_alloc);
  330. #else
  331. void nfs_setsecurity(struct inode *inode, struct nfs_fattr *fattr,
  332. struct nfs4_label *label)
  333. {
  334. }
  335. #endif
  336. EXPORT_SYMBOL_GPL(nfs_setsecurity);
  337. /* Search for inode identified by fh, fileid and i_mode in inode cache. */
  338. struct inode *
  339. nfs_ilookup(struct super_block *sb, struct nfs_fattr *fattr, struct nfs_fh *fh)
  340. {
  341. struct nfs_find_desc desc = {
  342. .fh = fh,
  343. .fattr = fattr,
  344. };
  345. struct inode *inode;
  346. unsigned long hash;
  347. if (!(fattr->valid & NFS_ATTR_FATTR_FILEID) ||
  348. !(fattr->valid & NFS_ATTR_FATTR_TYPE))
  349. return NULL;
  350. hash = nfs_fattr_to_ino_t(fattr);
  351. inode = ilookup5(sb, hash, nfs_find_actor, &desc);
  352. dprintk("%s: returning %p\n", __func__, inode);
  353. return inode;
  354. }
  355. /*
  356. * This is our front-end to iget that looks up inodes by file handle
  357. * instead of inode number.
  358. */
  359. struct inode *
  360. nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr, struct nfs4_label *label)
  361. {
  362. struct nfs_find_desc desc = {
  363. .fh = fh,
  364. .fattr = fattr
  365. };
  366. struct inode *inode = ERR_PTR(-ENOENT);
  367. unsigned long hash;
  368. nfs_attr_check_mountpoint(sb, fattr);
  369. if (nfs_attr_use_mounted_on_fileid(fattr))
  370. fattr->fileid = fattr->mounted_on_fileid;
  371. else if ((fattr->valid & NFS_ATTR_FATTR_FILEID) == 0)
  372. goto out_no_inode;
  373. if ((fattr->valid & NFS_ATTR_FATTR_TYPE) == 0)
  374. goto out_no_inode;
  375. hash = nfs_fattr_to_ino_t(fattr);
  376. inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc);
  377. if (inode == NULL) {
  378. inode = ERR_PTR(-ENOMEM);
  379. goto out_no_inode;
  380. }
  381. if (inode->i_state & I_NEW) {
  382. struct nfs_inode *nfsi = NFS_I(inode);
  383. unsigned long now = jiffies;
  384. /* We set i_ino for the few things that still rely on it,
  385. * such as stat(2) */
  386. inode->i_ino = hash;
  387. /* We can't support update_atime(), since the server will reset it */
  388. inode->i_flags |= S_NOATIME|S_NOCMTIME;
  389. inode->i_mode = fattr->mode;
  390. nfsi->cache_validity = 0;
  391. if ((fattr->valid & NFS_ATTR_FATTR_MODE) == 0
  392. && nfs_server_capable(inode, NFS_CAP_MODE))
  393. nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER);
  394. /* Why so? Because we want revalidate for devices/FIFOs, and
  395. * that's precisely what we have in nfs_file_inode_operations.
  396. */
  397. inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->file_inode_ops;
  398. if (S_ISREG(inode->i_mode)) {
  399. inode->i_fop = NFS_SB(sb)->nfs_client->rpc_ops->file_ops;
  400. inode->i_data.a_ops = &nfs_file_aops;
  401. } else if (S_ISDIR(inode->i_mode)) {
  402. inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->dir_inode_ops;
  403. inode->i_fop = &nfs_dir_operations;
  404. inode->i_data.a_ops = &nfs_dir_aops;
  405. /* Deal with crossing mountpoints */
  406. if (fattr->valid & NFS_ATTR_FATTR_MOUNTPOINT ||
  407. fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) {
  408. if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)
  409. inode->i_op = &nfs_referral_inode_operations;
  410. else
  411. inode->i_op = &nfs_mountpoint_inode_operations;
  412. inode->i_fop = NULL;
  413. inode->i_flags |= S_AUTOMOUNT;
  414. }
  415. } else if (S_ISLNK(inode->i_mode)) {
  416. inode->i_op = &nfs_symlink_inode_operations;
  417. inode_nohighmem(inode);
  418. } else
  419. init_special_inode(inode, inode->i_mode, fattr->rdev);
  420. memset(&inode->i_atime, 0, sizeof(inode->i_atime));
  421. memset(&inode->i_mtime, 0, sizeof(inode->i_mtime));
  422. memset(&inode->i_ctime, 0, sizeof(inode->i_ctime));
  423. inode_set_iversion_raw(inode, 0);
  424. inode->i_size = 0;
  425. clear_nlink(inode);
  426. inode->i_uid = make_kuid(&init_user_ns, -2);
  427. inode->i_gid = make_kgid(&init_user_ns, -2);
  428. inode->i_blocks = 0;
  429. memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
  430. nfsi->write_io = 0;
  431. nfsi->read_io = 0;
  432. nfsi->read_cache_jiffies = fattr->time_start;
  433. nfsi->attr_gencount = fattr->gencount;
  434. if (fattr->valid & NFS_ATTR_FATTR_ATIME)
  435. inode->i_atime = timespec_to_timespec64(fattr->atime);
  436. else if (nfs_server_capable(inode, NFS_CAP_ATIME))
  437. nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME);
  438. if (fattr->valid & NFS_ATTR_FATTR_MTIME)
  439. inode->i_mtime = timespec_to_timespec64(fattr->mtime);
  440. else if (nfs_server_capable(inode, NFS_CAP_MTIME))
  441. nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME);
  442. if (fattr->valid & NFS_ATTR_FATTR_CTIME)
  443. inode->i_ctime = timespec_to_timespec64(fattr->ctime);
  444. else if (nfs_server_capable(inode, NFS_CAP_CTIME))
  445. nfs_set_cache_invalid(inode, NFS_INO_INVALID_CTIME);
  446. if (fattr->valid & NFS_ATTR_FATTR_CHANGE)
  447. inode_set_iversion_raw(inode, fattr->change_attr);
  448. else
  449. nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE);
  450. if (fattr->valid & NFS_ATTR_FATTR_SIZE)
  451. inode->i_size = nfs_size_to_loff_t(fattr->size);
  452. else
  453. nfs_set_cache_invalid(inode, NFS_INO_INVALID_SIZE);
  454. if (fattr->valid & NFS_ATTR_FATTR_NLINK)
  455. set_nlink(inode, fattr->nlink);
  456. else if (nfs_server_capable(inode, NFS_CAP_NLINK))
  457. nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER);
  458. if (fattr->valid & NFS_ATTR_FATTR_OWNER)
  459. inode->i_uid = fattr->uid;
  460. else if (nfs_server_capable(inode, NFS_CAP_OWNER))
  461. nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER);
  462. if (fattr->valid & NFS_ATTR_FATTR_GROUP)
  463. inode->i_gid = fattr->gid;
  464. else if (nfs_server_capable(inode, NFS_CAP_OWNER_GROUP))
  465. nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER);
  466. if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
  467. inode->i_blocks = fattr->du.nfs2.blocks;
  468. if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
  469. /*
  470. * report the blocks in 512byte units
  471. */
  472. inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
  473. }
  474. if (nfsi->cache_validity != 0)
  475. nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
  476. nfs_setsecurity(inode, fattr, label);
  477. nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
  478. nfsi->attrtimeo_timestamp = now;
  479. nfsi->access_cache = RB_ROOT;
  480. nfs_fscache_init_inode(inode);
  481. unlock_new_inode(inode);
  482. } else {
  483. int err = nfs_refresh_inode(inode, fattr);
  484. if (err < 0) {
  485. iput(inode);
  486. inode = ERR_PTR(err);
  487. goto out_no_inode;
  488. }
  489. }
  490. dprintk("NFS: nfs_fhget(%s/%Lu fh_crc=0x%08x ct=%d)\n",
  491. inode->i_sb->s_id,
  492. (unsigned long long)NFS_FILEID(inode),
  493. nfs_display_fhandle_hash(fh),
  494. atomic_read(&inode->i_count));
  495. out:
  496. return inode;
  497. out_no_inode:
  498. dprintk("nfs_fhget: iget failed with error %ld\n", PTR_ERR(inode));
  499. goto out;
  500. }
  501. EXPORT_SYMBOL_GPL(nfs_fhget);
  502. #define NFS_VALID_ATTRS (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE|ATTR_ATIME|ATTR_ATIME_SET|ATTR_MTIME|ATTR_MTIME_SET|ATTR_FILE|ATTR_OPEN)
  503. int
  504. nfs_setattr(struct dentry *dentry, struct iattr *attr)
  505. {
  506. struct inode *inode = d_inode(dentry);
  507. struct nfs_fattr *fattr;
  508. int error = 0;
  509. nfs_inc_stats(inode, NFSIOS_VFSSETATTR);
  510. /* skip mode change if it's just for clearing setuid/setgid */
  511. if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
  512. attr->ia_valid &= ~ATTR_MODE;
  513. if (attr->ia_valid & ATTR_SIZE) {
  514. BUG_ON(!S_ISREG(inode->i_mode));
  515. error = inode_newsize_ok(inode, attr->ia_size);
  516. if (error)
  517. return error;
  518. if (attr->ia_size == i_size_read(inode))
  519. attr->ia_valid &= ~ATTR_SIZE;
  520. }
  521. /* Optimization: if the end result is no change, don't RPC */
  522. attr->ia_valid &= NFS_VALID_ATTRS;
  523. if ((attr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
  524. return 0;
  525. trace_nfs_setattr_enter(inode);
  526. /* Write all dirty data */
  527. if (S_ISREG(inode->i_mode))
  528. nfs_sync_inode(inode);
  529. fattr = nfs_alloc_fattr();
  530. if (fattr == NULL) {
  531. error = -ENOMEM;
  532. goto out;
  533. }
  534. error = NFS_PROTO(inode)->setattr(dentry, fattr, attr);
  535. if (error == 0)
  536. error = nfs_refresh_inode(inode, fattr);
  537. nfs_free_fattr(fattr);
  538. out:
  539. trace_nfs_setattr_exit(inode, error);
  540. return error;
  541. }
  542. EXPORT_SYMBOL_GPL(nfs_setattr);
  543. /**
  544. * nfs_vmtruncate - unmap mappings "freed" by truncate() syscall
  545. * @inode: inode of the file used
  546. * @offset: file offset to start truncating
  547. *
  548. * This is a copy of the common vmtruncate, but with the locking
  549. * corrected to take into account the fact that NFS requires
  550. * inode->i_size to be updated under the inode->i_lock.
  551. * Note: must be called with inode->i_lock held!
  552. */
  553. static int nfs_vmtruncate(struct inode * inode, loff_t offset)
  554. {
  555. int err;
  556. err = inode_newsize_ok(inode, offset);
  557. if (err)
  558. goto out;
  559. i_size_write(inode, offset);
  560. /* Optimisation */
  561. if (offset == 0)
  562. NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_DATA;
  563. NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_SIZE;
  564. spin_unlock(&inode->i_lock);
  565. truncate_pagecache(inode, offset);
  566. spin_lock(&inode->i_lock);
  567. out:
  568. return err;
  569. }
  570. /**
  571. * nfs_setattr_update_inode - Update inode metadata after a setattr call.
  572. * @inode: pointer to struct inode
  573. * @attr: pointer to struct iattr
  574. * @fattr: pointer to struct nfs_fattr
  575. *
  576. * Note: we do this in the *proc.c in order to ensure that
  577. * it works for things like exclusive creates too.
  578. */
  579. void nfs_setattr_update_inode(struct inode *inode, struct iattr *attr,
  580. struct nfs_fattr *fattr)
  581. {
  582. /* Barrier: bump the attribute generation count. */
  583. nfs_fattr_set_barrier(fattr);
  584. spin_lock(&inode->i_lock);
  585. NFS_I(inode)->attr_gencount = fattr->gencount;
  586. if ((attr->ia_valid & ATTR_SIZE) != 0) {
  587. nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME);
  588. nfs_inc_stats(inode, NFSIOS_SETATTRTRUNC);
  589. nfs_vmtruncate(inode, attr->ia_size);
  590. }
  591. if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) {
  592. NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_CTIME;
  593. if ((attr->ia_valid & ATTR_MODE) != 0) {
  594. int mode = attr->ia_mode & S_IALLUGO;
  595. mode |= inode->i_mode & ~S_IALLUGO;
  596. inode->i_mode = mode;
  597. }
  598. if ((attr->ia_valid & ATTR_UID) != 0)
  599. inode->i_uid = attr->ia_uid;
  600. if ((attr->ia_valid & ATTR_GID) != 0)
  601. inode->i_gid = attr->ia_gid;
  602. if (fattr->valid & NFS_ATTR_FATTR_CTIME)
  603. inode->i_ctime = timespec_to_timespec64(fattr->ctime);
  604. else
  605. nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE
  606. | NFS_INO_INVALID_CTIME);
  607. nfs_set_cache_invalid(inode, NFS_INO_INVALID_ACCESS
  608. | NFS_INO_INVALID_ACL);
  609. }
  610. if (attr->ia_valid & (ATTR_ATIME_SET|ATTR_ATIME)) {
  611. NFS_I(inode)->cache_validity &= ~(NFS_INO_INVALID_ATIME
  612. | NFS_INO_INVALID_CTIME);
  613. if (fattr->valid & NFS_ATTR_FATTR_ATIME)
  614. inode->i_atime = timespec_to_timespec64(fattr->atime);
  615. else if (attr->ia_valid & ATTR_ATIME_SET)
  616. inode->i_atime = attr->ia_atime;
  617. else
  618. nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME);
  619. if (fattr->valid & NFS_ATTR_FATTR_CTIME)
  620. inode->i_ctime = timespec_to_timespec64(fattr->ctime);
  621. else
  622. nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE
  623. | NFS_INO_INVALID_CTIME);
  624. }
  625. if (attr->ia_valid & (ATTR_MTIME_SET|ATTR_MTIME)) {
  626. NFS_I(inode)->cache_validity &= ~(NFS_INO_INVALID_MTIME
  627. | NFS_INO_INVALID_CTIME);
  628. if (fattr->valid & NFS_ATTR_FATTR_MTIME)
  629. inode->i_mtime = timespec_to_timespec64(fattr->mtime);
  630. else if (attr->ia_valid & ATTR_MTIME_SET)
  631. inode->i_mtime = attr->ia_mtime;
  632. else
  633. nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME);
  634. if (fattr->valid & NFS_ATTR_FATTR_CTIME)
  635. inode->i_ctime = timespec_to_timespec64(fattr->ctime);
  636. else
  637. nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE
  638. | NFS_INO_INVALID_CTIME);
  639. }
  640. if (fattr->valid)
  641. nfs_update_inode(inode, fattr);
  642. spin_unlock(&inode->i_lock);
  643. }
  644. EXPORT_SYMBOL_GPL(nfs_setattr_update_inode);
  645. static void nfs_readdirplus_parent_cache_miss(struct dentry *dentry)
  646. {
  647. struct dentry *parent;
  648. if (!nfs_server_capable(d_inode(dentry), NFS_CAP_READDIRPLUS))
  649. return;
  650. parent = dget_parent(dentry);
  651. nfs_force_use_readdirplus(d_inode(parent));
  652. dput(parent);
  653. }
  654. static void nfs_readdirplus_parent_cache_hit(struct dentry *dentry)
  655. {
  656. struct dentry *parent;
  657. if (!nfs_server_capable(d_inode(dentry), NFS_CAP_READDIRPLUS))
  658. return;
  659. parent = dget_parent(dentry);
  660. nfs_advise_use_readdirplus(d_inode(parent));
  661. dput(parent);
  662. }
  663. static bool nfs_need_revalidate_inode(struct inode *inode)
  664. {
  665. if (NFS_I(inode)->cache_validity &
  666. (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_LABEL))
  667. return true;
  668. if (nfs_attribute_cache_expired(inode))
  669. return true;
  670. return false;
  671. }
  672. int nfs_getattr(const struct path *path, struct kstat *stat,
  673. u32 request_mask, unsigned int query_flags)
  674. {
  675. struct inode *inode = d_inode(path->dentry);
  676. struct nfs_server *server = NFS_SERVER(inode);
  677. unsigned long cache_validity;
  678. int err = 0;
  679. bool force_sync = query_flags & AT_STATX_FORCE_SYNC;
  680. bool do_update = false;
  681. trace_nfs_getattr_enter(inode);
  682. if ((query_flags & AT_STATX_DONT_SYNC) && !force_sync)
  683. goto out_no_update;
  684. /* Flush out writes to the server in order to update c/mtime. */
  685. if ((request_mask & (STATX_CTIME|STATX_MTIME)) &&
  686. S_ISREG(inode->i_mode)) {
  687. err = filemap_write_and_wait(inode->i_mapping);
  688. if (err)
  689. goto out;
  690. }
  691. /*
  692. * We may force a getattr if the user cares about atime.
  693. *
  694. * Note that we only have to check the vfsmount flags here:
  695. * - NFS always sets S_NOATIME by so checking it would give a
  696. * bogus result
  697. * - NFS never sets SB_NOATIME or SB_NODIRATIME so there is
  698. * no point in checking those.
  699. */
  700. if ((path->mnt->mnt_flags & MNT_NOATIME) ||
  701. ((path->mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
  702. request_mask &= ~STATX_ATIME;
  703. /* Is the user requesting attributes that might need revalidation? */
  704. if (!(request_mask & (STATX_MODE|STATX_NLINK|STATX_ATIME|STATX_CTIME|
  705. STATX_MTIME|STATX_UID|STATX_GID|
  706. STATX_SIZE|STATX_BLOCKS)))
  707. goto out_no_revalidate;
  708. /* Check whether the cached attributes are stale */
  709. do_update |= force_sync || nfs_attribute_cache_expired(inode);
  710. cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
  711. do_update |= cache_validity &
  712. (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_LABEL);
  713. if (request_mask & STATX_ATIME)
  714. do_update |= cache_validity & NFS_INO_INVALID_ATIME;
  715. if (request_mask & (STATX_CTIME|STATX_MTIME))
  716. do_update |= cache_validity & NFS_INO_REVAL_PAGECACHE;
  717. if (do_update) {
  718. /* Update the attribute cache */
  719. if (!(server->flags & NFS_MOUNT_NOAC))
  720. nfs_readdirplus_parent_cache_miss(path->dentry);
  721. else
  722. nfs_readdirplus_parent_cache_hit(path->dentry);
  723. err = __nfs_revalidate_inode(server, inode);
  724. if (err)
  725. goto out;
  726. } else
  727. nfs_readdirplus_parent_cache_hit(path->dentry);
  728. out_no_revalidate:
  729. /* Only return attributes that were revalidated. */
  730. stat->result_mask &= request_mask;
  731. out_no_update:
  732. generic_fillattr(inode, stat);
  733. stat->ino = nfs_compat_user_ino64(NFS_FILEID(inode));
  734. if (S_ISDIR(inode->i_mode))
  735. stat->blksize = NFS_SERVER(inode)->dtsize;
  736. out:
  737. trace_nfs_getattr_exit(inode, err);
  738. return err;
  739. }
  740. EXPORT_SYMBOL_GPL(nfs_getattr);
  741. static void nfs_init_lock_context(struct nfs_lock_context *l_ctx)
  742. {
  743. refcount_set(&l_ctx->count, 1);
  744. l_ctx->lockowner = current->files;
  745. INIT_LIST_HEAD(&l_ctx->list);
  746. atomic_set(&l_ctx->io_count, 0);
  747. }
  748. static struct nfs_lock_context *__nfs_find_lock_context(struct nfs_open_context *ctx)
  749. {
  750. struct nfs_lock_context *pos;
  751. list_for_each_entry_rcu(pos, &ctx->lock_context.list, list) {
  752. if (pos->lockowner != current->files)
  753. continue;
  754. if (refcount_inc_not_zero(&pos->count))
  755. return pos;
  756. }
  757. return NULL;
  758. }
  759. struct nfs_lock_context *nfs_get_lock_context(struct nfs_open_context *ctx)
  760. {
  761. struct nfs_lock_context *res, *new = NULL;
  762. struct inode *inode = d_inode(ctx->dentry);
  763. rcu_read_lock();
  764. res = __nfs_find_lock_context(ctx);
  765. rcu_read_unlock();
  766. if (res == NULL) {
  767. new = kmalloc(sizeof(*new), GFP_KERNEL);
  768. if (new == NULL)
  769. return ERR_PTR(-ENOMEM);
  770. nfs_init_lock_context(new);
  771. spin_lock(&inode->i_lock);
  772. res = __nfs_find_lock_context(ctx);
  773. if (res == NULL) {
  774. list_add_tail_rcu(&new->list, &ctx->lock_context.list);
  775. new->open_context = ctx;
  776. res = new;
  777. new = NULL;
  778. }
  779. spin_unlock(&inode->i_lock);
  780. kfree(new);
  781. }
  782. return res;
  783. }
  784. EXPORT_SYMBOL_GPL(nfs_get_lock_context);
  785. void nfs_put_lock_context(struct nfs_lock_context *l_ctx)
  786. {
  787. struct nfs_open_context *ctx = l_ctx->open_context;
  788. struct inode *inode = d_inode(ctx->dentry);
  789. if (!refcount_dec_and_lock(&l_ctx->count, &inode->i_lock))
  790. return;
  791. list_del_rcu(&l_ctx->list);
  792. spin_unlock(&inode->i_lock);
  793. kfree_rcu(l_ctx, rcu_head);
  794. }
  795. EXPORT_SYMBOL_GPL(nfs_put_lock_context);
  796. /**
  797. * nfs_close_context - Common close_context() routine NFSv2/v3
  798. * @ctx: pointer to context
  799. * @is_sync: is this a synchronous close
  800. *
  801. * Ensure that the attributes are up to date if we're mounted
  802. * with close-to-open semantics and we have cached data that will
  803. * need to be revalidated on open.
  804. */
  805. void nfs_close_context(struct nfs_open_context *ctx, int is_sync)
  806. {
  807. struct nfs_inode *nfsi;
  808. struct inode *inode;
  809. struct nfs_server *server;
  810. if (!(ctx->mode & FMODE_WRITE))
  811. return;
  812. if (!is_sync)
  813. return;
  814. inode = d_inode(ctx->dentry);
  815. if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
  816. return;
  817. nfsi = NFS_I(inode);
  818. if (inode->i_mapping->nrpages == 0)
  819. return;
  820. if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
  821. return;
  822. if (!list_empty(&nfsi->open_files))
  823. return;
  824. server = NFS_SERVER(inode);
  825. if (server->flags & NFS_MOUNT_NOCTO)
  826. return;
  827. nfs_revalidate_inode(server, inode);
  828. }
  829. EXPORT_SYMBOL_GPL(nfs_close_context);
  830. struct nfs_open_context *alloc_nfs_open_context(struct dentry *dentry,
  831. fmode_t f_mode,
  832. struct file *filp)
  833. {
  834. struct nfs_open_context *ctx;
  835. struct rpc_cred *cred = rpc_lookup_cred();
  836. if (IS_ERR(cred))
  837. return ERR_CAST(cred);
  838. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  839. if (!ctx) {
  840. put_rpccred(cred);
  841. return ERR_PTR(-ENOMEM);
  842. }
  843. nfs_sb_active(dentry->d_sb);
  844. ctx->dentry = dget(dentry);
  845. ctx->cred = cred;
  846. ctx->state = NULL;
  847. ctx->mode = f_mode;
  848. ctx->flags = 0;
  849. ctx->error = 0;
  850. ctx->flock_owner = (fl_owner_t)filp;
  851. nfs_init_lock_context(&ctx->lock_context);
  852. ctx->lock_context.open_context = ctx;
  853. INIT_LIST_HEAD(&ctx->list);
  854. ctx->mdsthreshold = NULL;
  855. return ctx;
  856. }
  857. EXPORT_SYMBOL_GPL(alloc_nfs_open_context);
  858. struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx)
  859. {
  860. if (ctx != NULL && refcount_inc_not_zero(&ctx->lock_context.count))
  861. return ctx;
  862. return NULL;
  863. }
  864. EXPORT_SYMBOL_GPL(get_nfs_open_context);
  865. static void __put_nfs_open_context(struct nfs_open_context *ctx, int is_sync)
  866. {
  867. struct inode *inode = d_inode(ctx->dentry);
  868. struct super_block *sb = ctx->dentry->d_sb;
  869. if (!refcount_dec_and_test(&ctx->lock_context.count))
  870. return;
  871. if (!list_empty(&ctx->list)) {
  872. spin_lock(&inode->i_lock);
  873. list_del_rcu(&ctx->list);
  874. spin_unlock(&inode->i_lock);
  875. }
  876. if (inode != NULL)
  877. NFS_PROTO(inode)->close_context(ctx, is_sync);
  878. if (ctx->cred != NULL)
  879. put_rpccred(ctx->cred);
  880. dput(ctx->dentry);
  881. nfs_sb_deactive(sb);
  882. kfree(ctx->mdsthreshold);
  883. kfree_rcu(ctx, rcu_head);
  884. }
  885. void put_nfs_open_context(struct nfs_open_context *ctx)
  886. {
  887. __put_nfs_open_context(ctx, 0);
  888. }
  889. EXPORT_SYMBOL_GPL(put_nfs_open_context);
  890. static void put_nfs_open_context_sync(struct nfs_open_context *ctx)
  891. {
  892. __put_nfs_open_context(ctx, 1);
  893. }
  894. /*
  895. * Ensure that mmap has a recent RPC credential for use when writing out
  896. * shared pages
  897. */
  898. void nfs_inode_attach_open_context(struct nfs_open_context *ctx)
  899. {
  900. struct inode *inode = d_inode(ctx->dentry);
  901. struct nfs_inode *nfsi = NFS_I(inode);
  902. spin_lock(&inode->i_lock);
  903. list_add_tail_rcu(&ctx->list, &nfsi->open_files);
  904. spin_unlock(&inode->i_lock);
  905. }
  906. EXPORT_SYMBOL_GPL(nfs_inode_attach_open_context);
  907. void nfs_file_set_open_context(struct file *filp, struct nfs_open_context *ctx)
  908. {
  909. filp->private_data = get_nfs_open_context(ctx);
  910. if (list_empty(&ctx->list))
  911. nfs_inode_attach_open_context(ctx);
  912. }
  913. EXPORT_SYMBOL_GPL(nfs_file_set_open_context);
  914. /*
  915. * Given an inode, search for an open context with the desired characteristics
  916. */
  917. struct nfs_open_context *nfs_find_open_context(struct inode *inode, struct rpc_cred *cred, fmode_t mode)
  918. {
  919. struct nfs_inode *nfsi = NFS_I(inode);
  920. struct nfs_open_context *pos, *ctx = NULL;
  921. rcu_read_lock();
  922. list_for_each_entry_rcu(pos, &nfsi->open_files, list) {
  923. if (cred != NULL && pos->cred != cred)
  924. continue;
  925. if ((pos->mode & (FMODE_READ|FMODE_WRITE)) != mode)
  926. continue;
  927. ctx = get_nfs_open_context(pos);
  928. if (ctx)
  929. break;
  930. }
  931. rcu_read_unlock();
  932. return ctx;
  933. }
  934. void nfs_file_clear_open_context(struct file *filp)
  935. {
  936. struct nfs_open_context *ctx = nfs_file_open_context(filp);
  937. if (ctx) {
  938. struct inode *inode = d_inode(ctx->dentry);
  939. /*
  940. * We fatal error on write before. Try to writeback
  941. * every page again.
  942. */
  943. if (ctx->error < 0)
  944. invalidate_inode_pages2(inode->i_mapping);
  945. filp->private_data = NULL;
  946. put_nfs_open_context_sync(ctx);
  947. }
  948. }
  949. /*
  950. * These allocate and release file read/write context information.
  951. */
  952. int nfs_open(struct inode *inode, struct file *filp)
  953. {
  954. struct nfs_open_context *ctx;
  955. ctx = alloc_nfs_open_context(file_dentry(filp), filp->f_mode, filp);
  956. if (IS_ERR(ctx))
  957. return PTR_ERR(ctx);
  958. nfs_file_set_open_context(filp, ctx);
  959. put_nfs_open_context(ctx);
  960. nfs_fscache_open_file(inode, filp);
  961. return 0;
  962. }
  963. /*
  964. * This function is called whenever some part of NFS notices that
  965. * the cached attributes have to be refreshed.
  966. */
  967. int
  968. __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
  969. {
  970. int status = -ESTALE;
  971. struct nfs4_label *label = NULL;
  972. struct nfs_fattr *fattr = NULL;
  973. struct nfs_inode *nfsi = NFS_I(inode);
  974. dfprintk(PAGECACHE, "NFS: revalidating (%s/%Lu)\n",
  975. inode->i_sb->s_id, (unsigned long long)NFS_FILEID(inode));
  976. trace_nfs_revalidate_inode_enter(inode);
  977. if (is_bad_inode(inode))
  978. goto out;
  979. if (NFS_STALE(inode))
  980. goto out;
  981. /* pNFS: Attributes aren't updated until we layoutcommit */
  982. if (S_ISREG(inode->i_mode)) {
  983. status = pnfs_sync_inode(inode, false);
  984. if (status)
  985. goto out;
  986. }
  987. status = -ENOMEM;
  988. fattr = nfs_alloc_fattr();
  989. if (fattr == NULL)
  990. goto out;
  991. nfs_inc_stats(inode, NFSIOS_INODEREVALIDATE);
  992. label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
  993. if (IS_ERR(label)) {
  994. status = PTR_ERR(label);
  995. goto out;
  996. }
  997. status = NFS_PROTO(inode)->getattr(server, NFS_FH(inode), fattr,
  998. label, inode);
  999. if (status != 0) {
  1000. dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Lu) getattr failed, error=%d\n",
  1001. inode->i_sb->s_id,
  1002. (unsigned long long)NFS_FILEID(inode), status);
  1003. if (status == -ESTALE) {
  1004. nfs_zap_caches(inode);
  1005. if (!S_ISDIR(inode->i_mode))
  1006. set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
  1007. }
  1008. goto err_out;
  1009. }
  1010. status = nfs_refresh_inode(inode, fattr);
  1011. if (status) {
  1012. dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Lu) refresh failed, error=%d\n",
  1013. inode->i_sb->s_id,
  1014. (unsigned long long)NFS_FILEID(inode), status);
  1015. goto err_out;
  1016. }
  1017. if (nfsi->cache_validity & NFS_INO_INVALID_ACL)
  1018. nfs_zap_acl_cache(inode);
  1019. nfs_setsecurity(inode, fattr, label);
  1020. dfprintk(PAGECACHE, "NFS: (%s/%Lu) revalidation complete\n",
  1021. inode->i_sb->s_id,
  1022. (unsigned long long)NFS_FILEID(inode));
  1023. err_out:
  1024. nfs4_label_free(label);
  1025. out:
  1026. nfs_free_fattr(fattr);
  1027. trace_nfs_revalidate_inode_exit(inode, status);
  1028. return status;
  1029. }
  1030. int nfs_attribute_cache_expired(struct inode *inode)
  1031. {
  1032. if (nfs_have_delegated_attributes(inode))
  1033. return 0;
  1034. return nfs_attribute_timeout(inode);
  1035. }
  1036. /**
  1037. * nfs_revalidate_inode - Revalidate the inode attributes
  1038. * @server - pointer to nfs_server struct
  1039. * @inode - pointer to inode struct
  1040. *
  1041. * Updates inode attribute information by retrieving the data from the server.
  1042. */
  1043. int nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
  1044. {
  1045. if (!nfs_need_revalidate_inode(inode))
  1046. return NFS_STALE(inode) ? -ESTALE : 0;
  1047. return __nfs_revalidate_inode(server, inode);
  1048. }
  1049. EXPORT_SYMBOL_GPL(nfs_revalidate_inode);
  1050. static int nfs_invalidate_mapping(struct inode *inode, struct address_space *mapping)
  1051. {
  1052. struct nfs_inode *nfsi = NFS_I(inode);
  1053. int ret;
  1054. if (mapping->nrpages != 0) {
  1055. if (S_ISREG(inode->i_mode)) {
  1056. ret = nfs_sync_mapping(mapping);
  1057. if (ret < 0)
  1058. return ret;
  1059. }
  1060. ret = invalidate_inode_pages2(mapping);
  1061. if (ret < 0)
  1062. return ret;
  1063. }
  1064. if (S_ISDIR(inode->i_mode)) {
  1065. spin_lock(&inode->i_lock);
  1066. memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
  1067. spin_unlock(&inode->i_lock);
  1068. }
  1069. nfs_inc_stats(inode, NFSIOS_DATAINVALIDATE);
  1070. nfs_fscache_wait_on_invalidate(inode);
  1071. dfprintk(PAGECACHE, "NFS: (%s/%Lu) data cache invalidated\n",
  1072. inode->i_sb->s_id,
  1073. (unsigned long long)NFS_FILEID(inode));
  1074. return 0;
  1075. }
  1076. bool nfs_mapping_need_revalidate_inode(struct inode *inode)
  1077. {
  1078. return nfs_check_cache_invalid(inode, NFS_INO_REVAL_PAGECACHE) ||
  1079. NFS_STALE(inode);
  1080. }
  1081. int nfs_revalidate_mapping_rcu(struct inode *inode)
  1082. {
  1083. struct nfs_inode *nfsi = NFS_I(inode);
  1084. unsigned long *bitlock = &nfsi->flags;
  1085. int ret = 0;
  1086. if (IS_SWAPFILE(inode))
  1087. goto out;
  1088. if (nfs_mapping_need_revalidate_inode(inode)) {
  1089. ret = -ECHILD;
  1090. goto out;
  1091. }
  1092. spin_lock(&inode->i_lock);
  1093. if (test_bit(NFS_INO_INVALIDATING, bitlock) ||
  1094. (nfsi->cache_validity & NFS_INO_INVALID_DATA))
  1095. ret = -ECHILD;
  1096. spin_unlock(&inode->i_lock);
  1097. out:
  1098. return ret;
  1099. }
  1100. /**
  1101. * nfs_revalidate_mapping - Revalidate the pagecache
  1102. * @inode - pointer to host inode
  1103. * @mapping - pointer to mapping
  1104. */
  1105. int nfs_revalidate_mapping(struct inode *inode,
  1106. struct address_space *mapping)
  1107. {
  1108. struct nfs_inode *nfsi = NFS_I(inode);
  1109. unsigned long *bitlock = &nfsi->flags;
  1110. int ret = 0;
  1111. /* swapfiles are not supposed to be shared. */
  1112. if (IS_SWAPFILE(inode))
  1113. goto out;
  1114. if (nfs_mapping_need_revalidate_inode(inode)) {
  1115. ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  1116. if (ret < 0)
  1117. goto out;
  1118. }
  1119. /*
  1120. * We must clear NFS_INO_INVALID_DATA first to ensure that
  1121. * invalidations that come in while we're shooting down the mappings
  1122. * are respected. But, that leaves a race window where one revalidator
  1123. * can clear the flag, and then another checks it before the mapping
  1124. * gets invalidated. Fix that by serializing access to this part of
  1125. * the function.
  1126. *
  1127. * At the same time, we need to allow other tasks to see whether we
  1128. * might be in the middle of invalidating the pages, so we only set
  1129. * the bit lock here if it looks like we're going to be doing that.
  1130. */
  1131. for (;;) {
  1132. ret = wait_on_bit_action(bitlock, NFS_INO_INVALIDATING,
  1133. nfs_wait_bit_killable, TASK_KILLABLE);
  1134. if (ret)
  1135. goto out;
  1136. spin_lock(&inode->i_lock);
  1137. if (test_bit(NFS_INO_INVALIDATING, bitlock)) {
  1138. spin_unlock(&inode->i_lock);
  1139. continue;
  1140. }
  1141. if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
  1142. break;
  1143. spin_unlock(&inode->i_lock);
  1144. goto out;
  1145. }
  1146. set_bit(NFS_INO_INVALIDATING, bitlock);
  1147. smp_wmb();
  1148. nfsi->cache_validity &= ~NFS_INO_INVALID_DATA;
  1149. spin_unlock(&inode->i_lock);
  1150. trace_nfs_invalidate_mapping_enter(inode);
  1151. ret = nfs_invalidate_mapping(inode, mapping);
  1152. trace_nfs_invalidate_mapping_exit(inode, ret);
  1153. clear_bit_unlock(NFS_INO_INVALIDATING, bitlock);
  1154. smp_mb__after_atomic();
  1155. wake_up_bit(bitlock, NFS_INO_INVALIDATING);
  1156. out:
  1157. return ret;
  1158. }
  1159. static bool nfs_file_has_writers(struct nfs_inode *nfsi)
  1160. {
  1161. struct inode *inode = &nfsi->vfs_inode;
  1162. if (!S_ISREG(inode->i_mode))
  1163. return false;
  1164. if (list_empty(&nfsi->open_files))
  1165. return false;
  1166. return inode_is_open_for_write(inode);
  1167. }
  1168. static bool nfs_file_has_buffered_writers(struct nfs_inode *nfsi)
  1169. {
  1170. return nfs_file_has_writers(nfsi) && nfs_file_io_is_buffered(nfsi);
  1171. }
  1172. static void nfs_wcc_update_inode(struct inode *inode, struct nfs_fattr *fattr)
  1173. {
  1174. struct timespec ts;
  1175. if ((fattr->valid & NFS_ATTR_FATTR_PRECHANGE)
  1176. && (fattr->valid & NFS_ATTR_FATTR_CHANGE)
  1177. && inode_eq_iversion_raw(inode, fattr->pre_change_attr)) {
  1178. inode_set_iversion_raw(inode, fattr->change_attr);
  1179. if (S_ISDIR(inode->i_mode))
  1180. nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA);
  1181. }
  1182. /* If we have atomic WCC data, we may update some attributes */
  1183. ts = timespec64_to_timespec(inode->i_ctime);
  1184. if ((fattr->valid & NFS_ATTR_FATTR_PRECTIME)
  1185. && (fattr->valid & NFS_ATTR_FATTR_CTIME)
  1186. && timespec_equal(&ts, &fattr->pre_ctime)) {
  1187. inode->i_ctime = timespec_to_timespec64(fattr->ctime);
  1188. }
  1189. ts = timespec64_to_timespec(inode->i_mtime);
  1190. if ((fattr->valid & NFS_ATTR_FATTR_PREMTIME)
  1191. && (fattr->valid & NFS_ATTR_FATTR_MTIME)
  1192. && timespec_equal(&ts, &fattr->pre_mtime)) {
  1193. inode->i_mtime = timespec_to_timespec64(fattr->mtime);
  1194. if (S_ISDIR(inode->i_mode))
  1195. nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA);
  1196. }
  1197. if ((fattr->valid & NFS_ATTR_FATTR_PRESIZE)
  1198. && (fattr->valid & NFS_ATTR_FATTR_SIZE)
  1199. && i_size_read(inode) == nfs_size_to_loff_t(fattr->pre_size)
  1200. && !nfs_have_writebacks(inode)) {
  1201. i_size_write(inode, nfs_size_to_loff_t(fattr->size));
  1202. }
  1203. }
  1204. /**
  1205. * nfs_check_inode_attributes - verify consistency of the inode attribute cache
  1206. * @inode - pointer to inode
  1207. * @fattr - updated attributes
  1208. *
  1209. * Verifies the attribute cache. If we have just changed the attributes,
  1210. * so that fattr carries weak cache consistency data, then it may
  1211. * also update the ctime/mtime/change_attribute.
  1212. */
  1213. static int nfs_check_inode_attributes(struct inode *inode, struct nfs_fattr *fattr)
  1214. {
  1215. struct nfs_inode *nfsi = NFS_I(inode);
  1216. loff_t cur_size, new_isize;
  1217. unsigned long invalid = 0;
  1218. struct timespec ts;
  1219. if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
  1220. return 0;
  1221. /* Has the inode gone and changed behind our back? */
  1222. if ((fattr->valid & NFS_ATTR_FATTR_FILEID) && nfsi->fileid != fattr->fileid)
  1223. return -ESTALE;
  1224. if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
  1225. return -ESTALE;
  1226. if (!nfs_file_has_buffered_writers(nfsi)) {
  1227. /* Verify a few of the more important attributes */
  1228. if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 && !inode_eq_iversion_raw(inode, fattr->change_attr))
  1229. invalid |= NFS_INO_INVALID_CHANGE
  1230. | NFS_INO_REVAL_PAGECACHE;
  1231. ts = timespec64_to_timespec(inode->i_mtime);
  1232. if ((fattr->valid & NFS_ATTR_FATTR_MTIME) && !timespec_equal(&ts, &fattr->mtime))
  1233. invalid |= NFS_INO_INVALID_MTIME;
  1234. ts = timespec64_to_timespec(inode->i_ctime);
  1235. if ((fattr->valid & NFS_ATTR_FATTR_CTIME) && !timespec_equal(&ts, &fattr->ctime))
  1236. invalid |= NFS_INO_INVALID_CTIME;
  1237. if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
  1238. cur_size = i_size_read(inode);
  1239. new_isize = nfs_size_to_loff_t(fattr->size);
  1240. if (cur_size != new_isize)
  1241. invalid |= NFS_INO_INVALID_SIZE
  1242. | NFS_INO_REVAL_PAGECACHE;
  1243. }
  1244. }
  1245. /* Have any file permissions changed? */
  1246. if ((fattr->valid & NFS_ATTR_FATTR_MODE) && (inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO))
  1247. invalid |= NFS_INO_INVALID_ACCESS
  1248. | NFS_INO_INVALID_ACL
  1249. | NFS_INO_INVALID_OTHER;
  1250. if ((fattr->valid & NFS_ATTR_FATTR_OWNER) && !uid_eq(inode->i_uid, fattr->uid))
  1251. invalid |= NFS_INO_INVALID_ACCESS
  1252. | NFS_INO_INVALID_ACL
  1253. | NFS_INO_INVALID_OTHER;
  1254. if ((fattr->valid & NFS_ATTR_FATTR_GROUP) && !gid_eq(inode->i_gid, fattr->gid))
  1255. invalid |= NFS_INO_INVALID_ACCESS
  1256. | NFS_INO_INVALID_ACL
  1257. | NFS_INO_INVALID_OTHER;
  1258. /* Has the link count changed? */
  1259. if ((fattr->valid & NFS_ATTR_FATTR_NLINK) && inode->i_nlink != fattr->nlink)
  1260. invalid |= NFS_INO_INVALID_OTHER;
  1261. ts = timespec64_to_timespec(inode->i_atime);
  1262. if ((fattr->valid & NFS_ATTR_FATTR_ATIME) && !timespec_equal(&ts, &fattr->atime))
  1263. invalid |= NFS_INO_INVALID_ATIME;
  1264. if (invalid != 0)
  1265. nfs_set_cache_invalid(inode, invalid);
  1266. nfsi->read_cache_jiffies = fattr->time_start;
  1267. return 0;
  1268. }
  1269. static atomic_long_t nfs_attr_generation_counter;
  1270. static unsigned long nfs_read_attr_generation_counter(void)
  1271. {
  1272. return atomic_long_read(&nfs_attr_generation_counter);
  1273. }
  1274. unsigned long nfs_inc_attr_generation_counter(void)
  1275. {
  1276. return atomic_long_inc_return(&nfs_attr_generation_counter);
  1277. }
  1278. EXPORT_SYMBOL_GPL(nfs_inc_attr_generation_counter);
  1279. void nfs_fattr_init(struct nfs_fattr *fattr)
  1280. {
  1281. fattr->valid = 0;
  1282. fattr->time_start = jiffies;
  1283. fattr->gencount = nfs_inc_attr_generation_counter();
  1284. fattr->owner_name = NULL;
  1285. fattr->group_name = NULL;
  1286. }
  1287. EXPORT_SYMBOL_GPL(nfs_fattr_init);
  1288. /**
  1289. * nfs_fattr_set_barrier
  1290. * @fattr: attributes
  1291. *
  1292. * Used to set a barrier after an attribute was updated. This
  1293. * barrier ensures that older attributes from RPC calls that may
  1294. * have raced with our update cannot clobber these new values.
  1295. * Note that you are still responsible for ensuring that other
  1296. * operations which change the attribute on the server do not
  1297. * collide.
  1298. */
  1299. void nfs_fattr_set_barrier(struct nfs_fattr *fattr)
  1300. {
  1301. fattr->gencount = nfs_inc_attr_generation_counter();
  1302. }
  1303. struct nfs_fattr *nfs_alloc_fattr(void)
  1304. {
  1305. struct nfs_fattr *fattr;
  1306. fattr = kmalloc(sizeof(*fattr), GFP_NOFS);
  1307. if (fattr != NULL)
  1308. nfs_fattr_init(fattr);
  1309. return fattr;
  1310. }
  1311. EXPORT_SYMBOL_GPL(nfs_alloc_fattr);
  1312. struct nfs_fh *nfs_alloc_fhandle(void)
  1313. {
  1314. struct nfs_fh *fh;
  1315. fh = kmalloc(sizeof(struct nfs_fh), GFP_NOFS);
  1316. if (fh != NULL)
  1317. fh->size = 0;
  1318. return fh;
  1319. }
  1320. EXPORT_SYMBOL_GPL(nfs_alloc_fhandle);
  1321. #ifdef NFS_DEBUG
  1322. /*
  1323. * _nfs_display_fhandle_hash - calculate the crc32 hash for the filehandle
  1324. * in the same way that wireshark does
  1325. *
  1326. * @fh: file handle
  1327. *
  1328. * For debugging only.
  1329. */
  1330. u32 _nfs_display_fhandle_hash(const struct nfs_fh *fh)
  1331. {
  1332. /* wireshark uses 32-bit AUTODIN crc and does a bitwise
  1333. * not on the result */
  1334. return nfs_fhandle_hash(fh);
  1335. }
  1336. EXPORT_SYMBOL_GPL(_nfs_display_fhandle_hash);
  1337. /*
  1338. * _nfs_display_fhandle - display an NFS file handle on the console
  1339. *
  1340. * @fh: file handle to display
  1341. * @caption: display caption
  1342. *
  1343. * For debugging only.
  1344. */
  1345. void _nfs_display_fhandle(const struct nfs_fh *fh, const char *caption)
  1346. {
  1347. unsigned short i;
  1348. if (fh == NULL || fh->size == 0) {
  1349. printk(KERN_DEFAULT "%s at %p is empty\n", caption, fh);
  1350. return;
  1351. }
  1352. printk(KERN_DEFAULT "%s at %p is %u bytes, crc: 0x%08x:\n",
  1353. caption, fh, fh->size, _nfs_display_fhandle_hash(fh));
  1354. for (i = 0; i < fh->size; i += 16) {
  1355. __be32 *pos = (__be32 *)&fh->data[i];
  1356. switch ((fh->size - i - 1) >> 2) {
  1357. case 0:
  1358. printk(KERN_DEFAULT " %08x\n",
  1359. be32_to_cpup(pos));
  1360. break;
  1361. case 1:
  1362. printk(KERN_DEFAULT " %08x %08x\n",
  1363. be32_to_cpup(pos), be32_to_cpup(pos + 1));
  1364. break;
  1365. case 2:
  1366. printk(KERN_DEFAULT " %08x %08x %08x\n",
  1367. be32_to_cpup(pos), be32_to_cpup(pos + 1),
  1368. be32_to_cpup(pos + 2));
  1369. break;
  1370. default:
  1371. printk(KERN_DEFAULT " %08x %08x %08x %08x\n",
  1372. be32_to_cpup(pos), be32_to_cpup(pos + 1),
  1373. be32_to_cpup(pos + 2), be32_to_cpup(pos + 3));
  1374. }
  1375. }
  1376. }
  1377. EXPORT_SYMBOL_GPL(_nfs_display_fhandle);
  1378. #endif
  1379. /**
  1380. * nfs_inode_attrs_need_update - check if the inode attributes need updating
  1381. * @inode - pointer to inode
  1382. * @fattr - attributes
  1383. *
  1384. * Attempt to divine whether or not an RPC call reply carrying stale
  1385. * attributes got scheduled after another call carrying updated ones.
  1386. *
  1387. * To do so, the function first assumes that a more recent ctime means
  1388. * that the attributes in fattr are newer, however it also attempt to
  1389. * catch the case where ctime either didn't change, or went backwards
  1390. * (if someone reset the clock on the server) by looking at whether
  1391. * or not this RPC call was started after the inode was last updated.
  1392. * Note also the check for wraparound of 'attr_gencount'
  1393. *
  1394. * The function returns 'true' if it thinks the attributes in 'fattr' are
  1395. * more recent than the ones cached in the inode.
  1396. *
  1397. */
  1398. static int nfs_inode_attrs_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
  1399. {
  1400. const struct nfs_inode *nfsi = NFS_I(inode);
  1401. return ((long)fattr->gencount - (long)nfsi->attr_gencount) > 0 ||
  1402. ((long)nfsi->attr_gencount - (long)nfs_read_attr_generation_counter() > 0);
  1403. }
  1404. static int nfs_refresh_inode_locked(struct inode *inode, struct nfs_fattr *fattr)
  1405. {
  1406. int ret;
  1407. trace_nfs_refresh_inode_enter(inode);
  1408. if (nfs_inode_attrs_need_update(inode, fattr))
  1409. ret = nfs_update_inode(inode, fattr);
  1410. else
  1411. ret = nfs_check_inode_attributes(inode, fattr);
  1412. trace_nfs_refresh_inode_exit(inode, ret);
  1413. return ret;
  1414. }
  1415. /**
  1416. * nfs_refresh_inode - try to update the inode attribute cache
  1417. * @inode - pointer to inode
  1418. * @fattr - updated attributes
  1419. *
  1420. * Check that an RPC call that returned attributes has not overlapped with
  1421. * other recent updates of the inode metadata, then decide whether it is
  1422. * safe to do a full update of the inode attributes, or whether just to
  1423. * call nfs_check_inode_attributes.
  1424. */
  1425. int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
  1426. {
  1427. int status;
  1428. if ((fattr->valid & NFS_ATTR_FATTR) == 0)
  1429. return 0;
  1430. spin_lock(&inode->i_lock);
  1431. status = nfs_refresh_inode_locked(inode, fattr);
  1432. spin_unlock(&inode->i_lock);
  1433. return status;
  1434. }
  1435. EXPORT_SYMBOL_GPL(nfs_refresh_inode);
  1436. static int nfs_post_op_update_inode_locked(struct inode *inode,
  1437. struct nfs_fattr *fattr, unsigned int invalid)
  1438. {
  1439. if (S_ISDIR(inode->i_mode))
  1440. invalid |= NFS_INO_INVALID_DATA;
  1441. nfs_set_cache_invalid(inode, invalid);
  1442. if ((fattr->valid & NFS_ATTR_FATTR) == 0)
  1443. return 0;
  1444. return nfs_refresh_inode_locked(inode, fattr);
  1445. }
  1446. /**
  1447. * nfs_post_op_update_inode - try to update the inode attribute cache
  1448. * @inode - pointer to inode
  1449. * @fattr - updated attributes
  1450. *
  1451. * After an operation that has changed the inode metadata, mark the
  1452. * attribute cache as being invalid, then try to update it.
  1453. *
  1454. * NB: if the server didn't return any post op attributes, this
  1455. * function will force the retrieval of attributes before the next
  1456. * NFS request. Thus it should be used only for operations that
  1457. * are expected to change one or more attributes, to avoid
  1458. * unnecessary NFS requests and trips through nfs_update_inode().
  1459. */
  1460. int nfs_post_op_update_inode(struct inode *inode, struct nfs_fattr *fattr)
  1461. {
  1462. int status;
  1463. spin_lock(&inode->i_lock);
  1464. nfs_fattr_set_barrier(fattr);
  1465. status = nfs_post_op_update_inode_locked(inode, fattr,
  1466. NFS_INO_INVALID_CHANGE
  1467. | NFS_INO_INVALID_CTIME
  1468. | NFS_INO_REVAL_FORCED);
  1469. spin_unlock(&inode->i_lock);
  1470. return status;
  1471. }
  1472. EXPORT_SYMBOL_GPL(nfs_post_op_update_inode);
  1473. /**
  1474. * nfs_post_op_update_inode_force_wcc_locked - update the inode attribute cache
  1475. * @inode - pointer to inode
  1476. * @fattr - updated attributes
  1477. *
  1478. * After an operation that has changed the inode metadata, mark the
  1479. * attribute cache as being invalid, then try to update it. Fake up
  1480. * weak cache consistency data, if none exist.
  1481. *
  1482. * This function is mainly designed to be used by the ->write_done() functions.
  1483. */
  1484. int nfs_post_op_update_inode_force_wcc_locked(struct inode *inode, struct nfs_fattr *fattr)
  1485. {
  1486. int status;
  1487. /* Don't do a WCC update if these attributes are already stale */
  1488. if ((fattr->valid & NFS_ATTR_FATTR) == 0 ||
  1489. !nfs_inode_attrs_need_update(inode, fattr)) {
  1490. fattr->valid &= ~(NFS_ATTR_FATTR_PRECHANGE
  1491. | NFS_ATTR_FATTR_PRESIZE
  1492. | NFS_ATTR_FATTR_PREMTIME
  1493. | NFS_ATTR_FATTR_PRECTIME);
  1494. goto out_noforce;
  1495. }
  1496. if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
  1497. (fattr->valid & NFS_ATTR_FATTR_PRECHANGE) == 0) {
  1498. fattr->pre_change_attr = inode_peek_iversion_raw(inode);
  1499. fattr->valid |= NFS_ATTR_FATTR_PRECHANGE;
  1500. }
  1501. if ((fattr->valid & NFS_ATTR_FATTR_CTIME) != 0 &&
  1502. (fattr->valid & NFS_ATTR_FATTR_PRECTIME) == 0) {
  1503. fattr->pre_ctime = timespec64_to_timespec(inode->i_ctime);
  1504. fattr->valid |= NFS_ATTR_FATTR_PRECTIME;
  1505. }
  1506. if ((fattr->valid & NFS_ATTR_FATTR_MTIME) != 0 &&
  1507. (fattr->valid & NFS_ATTR_FATTR_PREMTIME) == 0) {
  1508. fattr->pre_mtime = timespec64_to_timespec(inode->i_mtime);
  1509. fattr->valid |= NFS_ATTR_FATTR_PREMTIME;
  1510. }
  1511. if ((fattr->valid & NFS_ATTR_FATTR_SIZE) != 0 &&
  1512. (fattr->valid & NFS_ATTR_FATTR_PRESIZE) == 0) {
  1513. fattr->pre_size = i_size_read(inode);
  1514. fattr->valid |= NFS_ATTR_FATTR_PRESIZE;
  1515. }
  1516. out_noforce:
  1517. status = nfs_post_op_update_inode_locked(inode, fattr,
  1518. NFS_INO_INVALID_CHANGE
  1519. | NFS_INO_INVALID_CTIME
  1520. | NFS_INO_INVALID_MTIME);
  1521. return status;
  1522. }
  1523. /**
  1524. * nfs_post_op_update_inode_force_wcc - try to update the inode attribute cache
  1525. * @inode - pointer to inode
  1526. * @fattr - updated attributes
  1527. *
  1528. * After an operation that has changed the inode metadata, mark the
  1529. * attribute cache as being invalid, then try to update it. Fake up
  1530. * weak cache consistency data, if none exist.
  1531. *
  1532. * This function is mainly designed to be used by the ->write_done() functions.
  1533. */
  1534. int nfs_post_op_update_inode_force_wcc(struct inode *inode, struct nfs_fattr *fattr)
  1535. {
  1536. int status;
  1537. spin_lock(&inode->i_lock);
  1538. nfs_fattr_set_barrier(fattr);
  1539. status = nfs_post_op_update_inode_force_wcc_locked(inode, fattr);
  1540. spin_unlock(&inode->i_lock);
  1541. return status;
  1542. }
  1543. EXPORT_SYMBOL_GPL(nfs_post_op_update_inode_force_wcc);
  1544. static inline bool nfs_fileid_valid(struct nfs_inode *nfsi,
  1545. struct nfs_fattr *fattr)
  1546. {
  1547. bool ret1 = true, ret2 = true;
  1548. if (fattr->valid & NFS_ATTR_FATTR_FILEID)
  1549. ret1 = (nfsi->fileid == fattr->fileid);
  1550. if (fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID)
  1551. ret2 = (nfsi->fileid == fattr->mounted_on_fileid);
  1552. return ret1 || ret2;
  1553. }
  1554. /*
  1555. * Many nfs protocol calls return the new file attributes after
  1556. * an operation. Here we update the inode to reflect the state
  1557. * of the server's inode.
  1558. *
  1559. * This is a bit tricky because we have to make sure all dirty pages
  1560. * have been sent off to the server before calling invalidate_inode_pages.
  1561. * To make sure no other process adds more write requests while we try
  1562. * our best to flush them, we make them sleep during the attribute refresh.
  1563. *
  1564. * A very similar scenario holds for the dir cache.
  1565. */
  1566. static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr)
  1567. {
  1568. struct nfs_server *server;
  1569. struct nfs_inode *nfsi = NFS_I(inode);
  1570. loff_t cur_isize, new_isize;
  1571. unsigned long invalid = 0;
  1572. unsigned long now = jiffies;
  1573. unsigned long save_cache_validity;
  1574. bool have_writers = nfs_file_has_buffered_writers(nfsi);
  1575. bool cache_revalidated = true;
  1576. bool attr_changed = false;
  1577. bool have_delegation;
  1578. dfprintk(VFS, "NFS: %s(%s/%lu fh_crc=0x%08x ct=%d info=0x%x)\n",
  1579. __func__, inode->i_sb->s_id, inode->i_ino,
  1580. nfs_display_fhandle_hash(NFS_FH(inode)),
  1581. atomic_read(&inode->i_count), fattr->valid);
  1582. if (!nfs_fileid_valid(nfsi, fattr)) {
  1583. printk(KERN_ERR "NFS: server %s error: fileid changed\n"
  1584. "fsid %s: expected fileid 0x%Lx, got 0x%Lx\n",
  1585. NFS_SERVER(inode)->nfs_client->cl_hostname,
  1586. inode->i_sb->s_id, (long long)nfsi->fileid,
  1587. (long long)fattr->fileid);
  1588. goto out_err;
  1589. }
  1590. /*
  1591. * Make sure the inode's type hasn't changed.
  1592. */
  1593. if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT)) {
  1594. /*
  1595. * Big trouble! The inode has become a different object.
  1596. */
  1597. printk(KERN_DEBUG "NFS: %s: inode %lu mode changed, %07o to %07o\n",
  1598. __func__, inode->i_ino, inode->i_mode, fattr->mode);
  1599. goto out_err;
  1600. }
  1601. server = NFS_SERVER(inode);
  1602. /* Update the fsid? */
  1603. if (S_ISDIR(inode->i_mode) && (fattr->valid & NFS_ATTR_FATTR_FSID) &&
  1604. !nfs_fsid_equal(&server->fsid, &fattr->fsid) &&
  1605. !IS_AUTOMOUNT(inode))
  1606. server->fsid = fattr->fsid;
  1607. /* Save the delegation state before clearing cache_validity */
  1608. have_delegation = nfs_have_delegated_attributes(inode);
  1609. /*
  1610. * Update the read time so we don't revalidate too often.
  1611. */
  1612. nfsi->read_cache_jiffies = fattr->time_start;
  1613. save_cache_validity = nfsi->cache_validity;
  1614. nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR
  1615. | NFS_INO_INVALID_ATIME
  1616. | NFS_INO_REVAL_FORCED
  1617. | NFS_INO_REVAL_PAGECACHE);
  1618. /* Do atomic weak cache consistency updates */
  1619. nfs_wcc_update_inode(inode, fattr);
  1620. if (pnfs_layoutcommit_outstanding(inode)) {
  1621. nfsi->cache_validity |= save_cache_validity & NFS_INO_INVALID_ATTR;
  1622. cache_revalidated = false;
  1623. }
  1624. /* More cache consistency checks */
  1625. if (fattr->valid & NFS_ATTR_FATTR_CHANGE) {
  1626. if (!inode_eq_iversion_raw(inode, fattr->change_attr)) {
  1627. /* Could it be a race with writeback? */
  1628. if (!(have_writers || have_delegation)) {
  1629. invalid |= NFS_INO_INVALID_DATA
  1630. | NFS_INO_INVALID_ACCESS
  1631. | NFS_INO_INVALID_ACL;
  1632. /* Force revalidate of all attributes */
  1633. save_cache_validity |= NFS_INO_INVALID_CTIME
  1634. | NFS_INO_INVALID_MTIME
  1635. | NFS_INO_INVALID_SIZE
  1636. | NFS_INO_INVALID_OTHER;
  1637. if (S_ISDIR(inode->i_mode))
  1638. nfs_force_lookup_revalidate(inode);
  1639. dprintk("NFS: change_attr change on server for file %s/%ld\n",
  1640. inode->i_sb->s_id,
  1641. inode->i_ino);
  1642. }
  1643. inode_set_iversion_raw(inode, fattr->change_attr);
  1644. attr_changed = true;
  1645. }
  1646. } else {
  1647. nfsi->cache_validity |= save_cache_validity &
  1648. (NFS_INO_INVALID_CHANGE
  1649. | NFS_INO_REVAL_PAGECACHE
  1650. | NFS_INO_REVAL_FORCED);
  1651. cache_revalidated = false;
  1652. }
  1653. if (fattr->valid & NFS_ATTR_FATTR_MTIME) {
  1654. inode->i_mtime = timespec_to_timespec64(fattr->mtime);
  1655. } else if (server->caps & NFS_CAP_MTIME) {
  1656. nfsi->cache_validity |= save_cache_validity &
  1657. (NFS_INO_INVALID_MTIME
  1658. | NFS_INO_REVAL_FORCED);
  1659. cache_revalidated = false;
  1660. }
  1661. if (fattr->valid & NFS_ATTR_FATTR_CTIME) {
  1662. inode->i_ctime = timespec_to_timespec64(fattr->ctime);
  1663. } else if (server->caps & NFS_CAP_CTIME) {
  1664. nfsi->cache_validity |= save_cache_validity &
  1665. (NFS_INO_INVALID_CTIME
  1666. | NFS_INO_REVAL_FORCED);
  1667. cache_revalidated = false;
  1668. }
  1669. /* Check if our cached file size is stale */
  1670. if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
  1671. new_isize = nfs_size_to_loff_t(fattr->size);
  1672. cur_isize = i_size_read(inode);
  1673. if (new_isize != cur_isize && !have_delegation) {
  1674. /* Do we perhaps have any outstanding writes, or has
  1675. * the file grown beyond our last write? */
  1676. if (!nfs_have_writebacks(inode) || new_isize > cur_isize) {
  1677. i_size_write(inode, new_isize);
  1678. if (!have_writers)
  1679. invalid |= NFS_INO_INVALID_DATA;
  1680. attr_changed = true;
  1681. }
  1682. dprintk("NFS: isize change on server for file %s/%ld "
  1683. "(%Ld to %Ld)\n",
  1684. inode->i_sb->s_id,
  1685. inode->i_ino,
  1686. (long long)cur_isize,
  1687. (long long)new_isize);
  1688. }
  1689. } else {
  1690. nfsi->cache_validity |= save_cache_validity &
  1691. (NFS_INO_INVALID_SIZE
  1692. | NFS_INO_REVAL_PAGECACHE
  1693. | NFS_INO_REVAL_FORCED);
  1694. cache_revalidated = false;
  1695. }
  1696. if (fattr->valid & NFS_ATTR_FATTR_ATIME)
  1697. inode->i_atime = timespec_to_timespec64(fattr->atime);
  1698. else if (server->caps & NFS_CAP_ATIME) {
  1699. nfsi->cache_validity |= save_cache_validity &
  1700. (NFS_INO_INVALID_ATIME
  1701. | NFS_INO_REVAL_FORCED);
  1702. cache_revalidated = false;
  1703. }
  1704. if (fattr->valid & NFS_ATTR_FATTR_MODE) {
  1705. if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)) {
  1706. umode_t newmode = inode->i_mode & S_IFMT;
  1707. newmode |= fattr->mode & S_IALLUGO;
  1708. inode->i_mode = newmode;
  1709. invalid |= NFS_INO_INVALID_ACCESS
  1710. | NFS_INO_INVALID_ACL;
  1711. attr_changed = true;
  1712. }
  1713. } else if (server->caps & NFS_CAP_MODE) {
  1714. nfsi->cache_validity |= save_cache_validity &
  1715. (NFS_INO_INVALID_OTHER
  1716. | NFS_INO_REVAL_FORCED);
  1717. cache_revalidated = false;
  1718. }
  1719. if (fattr->valid & NFS_ATTR_FATTR_OWNER) {
  1720. if (!uid_eq(inode->i_uid, fattr->uid)) {
  1721. invalid |= NFS_INO_INVALID_ACCESS
  1722. | NFS_INO_INVALID_ACL;
  1723. inode->i_uid = fattr->uid;
  1724. attr_changed = true;
  1725. }
  1726. } else if (server->caps & NFS_CAP_OWNER) {
  1727. nfsi->cache_validity |= save_cache_validity &
  1728. (NFS_INO_INVALID_OTHER
  1729. | NFS_INO_REVAL_FORCED);
  1730. cache_revalidated = false;
  1731. }
  1732. if (fattr->valid & NFS_ATTR_FATTR_GROUP) {
  1733. if (!gid_eq(inode->i_gid, fattr->gid)) {
  1734. invalid |= NFS_INO_INVALID_ACCESS
  1735. | NFS_INO_INVALID_ACL;
  1736. inode->i_gid = fattr->gid;
  1737. attr_changed = true;
  1738. }
  1739. } else if (server->caps & NFS_CAP_OWNER_GROUP) {
  1740. nfsi->cache_validity |= save_cache_validity &
  1741. (NFS_INO_INVALID_OTHER
  1742. | NFS_INO_REVAL_FORCED);
  1743. cache_revalidated = false;
  1744. }
  1745. if (fattr->valid & NFS_ATTR_FATTR_NLINK) {
  1746. if (inode->i_nlink != fattr->nlink) {
  1747. if (S_ISDIR(inode->i_mode))
  1748. invalid |= NFS_INO_INVALID_DATA;
  1749. set_nlink(inode, fattr->nlink);
  1750. attr_changed = true;
  1751. }
  1752. } else if (server->caps & NFS_CAP_NLINK) {
  1753. nfsi->cache_validity |= save_cache_validity &
  1754. (NFS_INO_INVALID_OTHER
  1755. | NFS_INO_REVAL_FORCED);
  1756. cache_revalidated = false;
  1757. }
  1758. if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
  1759. /*
  1760. * report the blocks in 512byte units
  1761. */
  1762. inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
  1763. } else if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
  1764. inode->i_blocks = fattr->du.nfs2.blocks;
  1765. else
  1766. cache_revalidated = false;
  1767. /* Update attrtimeo value if we're out of the unstable period */
  1768. if (attr_changed) {
  1769. invalid &= ~NFS_INO_INVALID_ATTR;
  1770. nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
  1771. nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
  1772. nfsi->attrtimeo_timestamp = now;
  1773. /* Set barrier to be more recent than all outstanding updates */
  1774. nfsi->attr_gencount = nfs_inc_attr_generation_counter();
  1775. } else {
  1776. if (cache_revalidated) {
  1777. if (!time_in_range_open(now, nfsi->attrtimeo_timestamp,
  1778. nfsi->attrtimeo_timestamp + nfsi->attrtimeo)) {
  1779. nfsi->attrtimeo <<= 1;
  1780. if (nfsi->attrtimeo > NFS_MAXATTRTIMEO(inode))
  1781. nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
  1782. }
  1783. nfsi->attrtimeo_timestamp = now;
  1784. }
  1785. /* Set the barrier to be more recent than this fattr */
  1786. if ((long)fattr->gencount - (long)nfsi->attr_gencount > 0)
  1787. nfsi->attr_gencount = fattr->gencount;
  1788. }
  1789. /* Don't invalidate the data if we were to blame */
  1790. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
  1791. || S_ISLNK(inode->i_mode)))
  1792. invalid &= ~NFS_INO_INVALID_DATA;
  1793. nfs_set_cache_invalid(inode, invalid);
  1794. return 0;
  1795. out_err:
  1796. /*
  1797. * No need to worry about unhashing the dentry, as the
  1798. * lookup validation will know that the inode is bad.
  1799. * (But we fall through to invalidate the caches.)
  1800. */
  1801. nfs_invalidate_inode(inode);
  1802. return -ESTALE;
  1803. }
  1804. struct inode *nfs_alloc_inode(struct super_block *sb)
  1805. {
  1806. struct nfs_inode *nfsi;
  1807. nfsi = kmem_cache_alloc(nfs_inode_cachep, GFP_KERNEL);
  1808. if (!nfsi)
  1809. return NULL;
  1810. nfsi->flags = 0UL;
  1811. nfsi->cache_validity = 0UL;
  1812. #if IS_ENABLED(CONFIG_NFS_V4)
  1813. nfsi->nfs4_acl = NULL;
  1814. #endif /* CONFIG_NFS_V4 */
  1815. return &nfsi->vfs_inode;
  1816. }
  1817. EXPORT_SYMBOL_GPL(nfs_alloc_inode);
  1818. static void nfs_i_callback(struct rcu_head *head)
  1819. {
  1820. struct inode *inode = container_of(head, struct inode, i_rcu);
  1821. kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
  1822. }
  1823. void nfs_destroy_inode(struct inode *inode)
  1824. {
  1825. call_rcu(&inode->i_rcu, nfs_i_callback);
  1826. }
  1827. EXPORT_SYMBOL_GPL(nfs_destroy_inode);
  1828. static inline void nfs4_init_once(struct nfs_inode *nfsi)
  1829. {
  1830. #if IS_ENABLED(CONFIG_NFS_V4)
  1831. INIT_LIST_HEAD(&nfsi->open_states);
  1832. nfsi->delegation = NULL;
  1833. init_rwsem(&nfsi->rwsem);
  1834. nfsi->layout = NULL;
  1835. #endif
  1836. }
  1837. static void init_once(void *foo)
  1838. {
  1839. struct nfs_inode *nfsi = (struct nfs_inode *) foo;
  1840. inode_init_once(&nfsi->vfs_inode);
  1841. INIT_LIST_HEAD(&nfsi->open_files);
  1842. INIT_LIST_HEAD(&nfsi->access_cache_entry_lru);
  1843. INIT_LIST_HEAD(&nfsi->access_cache_inode_lru);
  1844. INIT_LIST_HEAD(&nfsi->commit_info.list);
  1845. atomic_long_set(&nfsi->nrequests, 0);
  1846. atomic_long_set(&nfsi->commit_info.ncommit, 0);
  1847. atomic_set(&nfsi->commit_info.rpcs_out, 0);
  1848. init_rwsem(&nfsi->rmdir_sem);
  1849. mutex_init(&nfsi->commit_mutex);
  1850. nfs4_init_once(nfsi);
  1851. }
  1852. static int __init nfs_init_inodecache(void)
  1853. {
  1854. nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
  1855. sizeof(struct nfs_inode),
  1856. 0, (SLAB_RECLAIM_ACCOUNT|
  1857. SLAB_MEM_SPREAD|SLAB_ACCOUNT),
  1858. init_once);
  1859. if (nfs_inode_cachep == NULL)
  1860. return -ENOMEM;
  1861. return 0;
  1862. }
  1863. static void nfs_destroy_inodecache(void)
  1864. {
  1865. /*
  1866. * Make sure all delayed rcu free inodes are flushed before we
  1867. * destroy cache.
  1868. */
  1869. rcu_barrier();
  1870. kmem_cache_destroy(nfs_inode_cachep);
  1871. }
  1872. struct workqueue_struct *nfsiod_workqueue;
  1873. EXPORT_SYMBOL_GPL(nfsiod_workqueue);
  1874. /*
  1875. * start up the nfsiod workqueue
  1876. */
  1877. static int nfsiod_start(void)
  1878. {
  1879. struct workqueue_struct *wq;
  1880. dprintk("RPC: creating workqueue nfsiod\n");
  1881. wq = alloc_workqueue("nfsiod", WQ_MEM_RECLAIM, 0);
  1882. if (wq == NULL)
  1883. return -ENOMEM;
  1884. nfsiod_workqueue = wq;
  1885. return 0;
  1886. }
  1887. /*
  1888. * Destroy the nfsiod workqueue
  1889. */
  1890. static void nfsiod_stop(void)
  1891. {
  1892. struct workqueue_struct *wq;
  1893. wq = nfsiod_workqueue;
  1894. if (wq == NULL)
  1895. return;
  1896. nfsiod_workqueue = NULL;
  1897. destroy_workqueue(wq);
  1898. }
  1899. unsigned int nfs_net_id;
  1900. EXPORT_SYMBOL_GPL(nfs_net_id);
  1901. static int nfs_net_init(struct net *net)
  1902. {
  1903. nfs_clients_init(net);
  1904. return nfs_fs_proc_net_init(net);
  1905. }
  1906. static void nfs_net_exit(struct net *net)
  1907. {
  1908. struct nfs_net *nn = net_generic(net, nfs_net_id);
  1909. nfs_fs_proc_net_exit(net);
  1910. nfs_cleanup_cb_ident_idr(net);
  1911. WARN_ON_ONCE(!list_empty(&nn->nfs_client_list));
  1912. WARN_ON_ONCE(!list_empty(&nn->nfs_volume_list));
  1913. }
  1914. static struct pernet_operations nfs_net_ops = {
  1915. .init = nfs_net_init,
  1916. .exit = nfs_net_exit,
  1917. .id = &nfs_net_id,
  1918. .size = sizeof(struct nfs_net),
  1919. };
  1920. /*
  1921. * Initialize NFS
  1922. */
  1923. static int __init init_nfs_fs(void)
  1924. {
  1925. int err;
  1926. err = register_pernet_subsys(&nfs_net_ops);
  1927. if (err < 0)
  1928. goto out9;
  1929. err = nfs_fscache_register();
  1930. if (err < 0)
  1931. goto out8;
  1932. err = nfsiod_start();
  1933. if (err)
  1934. goto out7;
  1935. err = nfs_fs_proc_init();
  1936. if (err)
  1937. goto out6;
  1938. err = nfs_init_nfspagecache();
  1939. if (err)
  1940. goto out5;
  1941. err = nfs_init_inodecache();
  1942. if (err)
  1943. goto out4;
  1944. err = nfs_init_readpagecache();
  1945. if (err)
  1946. goto out3;
  1947. err = nfs_init_writepagecache();
  1948. if (err)
  1949. goto out2;
  1950. err = nfs_init_directcache();
  1951. if (err)
  1952. goto out1;
  1953. rpc_proc_register(&init_net, &nfs_rpcstat);
  1954. err = register_nfs_fs();
  1955. if (err)
  1956. goto out0;
  1957. return 0;
  1958. out0:
  1959. rpc_proc_unregister(&init_net, "nfs");
  1960. nfs_destroy_directcache();
  1961. out1:
  1962. nfs_destroy_writepagecache();
  1963. out2:
  1964. nfs_destroy_readpagecache();
  1965. out3:
  1966. nfs_destroy_inodecache();
  1967. out4:
  1968. nfs_destroy_nfspagecache();
  1969. out5:
  1970. nfs_fs_proc_exit();
  1971. out6:
  1972. nfsiod_stop();
  1973. out7:
  1974. nfs_fscache_unregister();
  1975. out8:
  1976. unregister_pernet_subsys(&nfs_net_ops);
  1977. out9:
  1978. return err;
  1979. }
  1980. static void __exit exit_nfs_fs(void)
  1981. {
  1982. nfs_destroy_directcache();
  1983. nfs_destroy_writepagecache();
  1984. nfs_destroy_readpagecache();
  1985. nfs_destroy_inodecache();
  1986. nfs_destroy_nfspagecache();
  1987. nfs_fscache_unregister();
  1988. unregister_pernet_subsys(&nfs_net_ops);
  1989. rpc_proc_unregister(&init_net, "nfs");
  1990. unregister_nfs_fs();
  1991. nfs_fs_proc_exit();
  1992. nfsiod_stop();
  1993. }
  1994. /* Not quite true; I just maintain it */
  1995. MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
  1996. MODULE_LICENSE("GPL");
  1997. module_param(enable_ino64, bool, 0644);
  1998. module_init(init_nfs_fs)
  1999. module_exit(exit_nfs_fs)