inode.c 63 KB

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