inode.c 75 KB

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